blob: 41138e91947a83119d1951db8e4609c81047e3bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/symlink.c - sysfs symlink implementation
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
13#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
Greg Kroah-Hartmanceeee1f2002-04-09 12:14:34 -070015#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/kobject.h>
18#include <linux/namei.h>
Dave Young869512a2007-07-26 14:53:53 +000019#include <linux/mutex.h>
David P. Quigleyddd29ec2009-09-09 14:25:37 -040020#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include "sysfs.h"
23
Tejun Heo5d0e26b2013-11-23 17:21:50 -050024/**
25 * kernfs_create_link - create a symlink
26 * @parent: directory to create the symlink in
27 * @name: name of the symlink
28 * @target: target node for the symlink to point to
29 *
30 * Returns the created node on success, ERR_PTR() value on error.
31 */
32struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
33 const char *name,
34 struct sysfs_dirent *target)
35{
36 struct sysfs_dirent *sd;
37 struct sysfs_addrm_cxt acxt;
38 int error;
39
40 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
41 if (!sd)
42 return ERR_PTR(-ENOMEM);
43
44 if (parent->s_flags & SYSFS_FLAG_NS)
45 sd->s_ns = target->s_ns;
46 sd->s_symlink.target_sd = target;
47 sysfs_get(target); /* ref owned by symlink */
48
49 sysfs_addrm_start(&acxt);
50 error = __sysfs_add_one(&acxt, sd, parent);
51 sysfs_addrm_finish(&acxt);
52
53 if (!error)
54 return sd;
55
56 sysfs_put(sd);
57 return ERR_PTR(error);
58}
59
60
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +010061static int sysfs_do_create_link_sd(struct sysfs_dirent *parent_sd,
62 struct kobject *target,
63 const char *name, int warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Tejun Heo5d0e26b2013-11-23 17:21:50 -050065 struct sysfs_dirent *sd, *target_sd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +010067 BUG_ON(!name || !parent_sd);
Tejun Heo2b29ac22007-06-14 03:45:15 +090068
Tejun Heo0cae60f2013-10-30 10:28:36 -040069 /*
70 * We don't own @target and it may be removed at any time.
71 * Synchronize using sysfs_symlink_target_lock. See
72 * sysfs_remove_dir() for details.
Tejun Heo2b29ac22007-06-14 03:45:15 +090073 */
Tejun Heo0cae60f2013-10-30 10:28:36 -040074 spin_lock(&sysfs_symlink_target_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090075 if (target->sd)
76 target_sd = sysfs_get(target->sd);
Tejun Heo0cae60f2013-10-30 10:28:36 -040077 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heo2b29ac22007-06-14 03:45:15 +090078
79 if (!target_sd)
Tejun Heo5d0e26b2013-11-23 17:21:50 -050080 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Tejun Heo5d0e26b2013-11-23 17:21:50 -050082 sd = kernfs_create_link(parent_sd, name, target_sd);
Tejun Heo3007e992007-06-14 04:27:23 +090083 sysfs_put(target_sd);
Tejun Heo5d0e26b2013-11-23 17:21:50 -050084
85 if (!IS_ERR(sd))
86 return 0;
87
88 if (warn && PTR_ERR(sd) == -EEXIST)
89 sysfs_warn_dup(parent_sd, name);
90 return PTR_ERR(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/**
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +010094 * sysfs_create_link_sd - create symlink to a given object.
95 * @sd: directory we're creating the link in.
96 * @target: object we're pointing to.
97 * @name: name of the symlink.
98 */
99int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
100 const char *name)
101{
102 return sysfs_do_create_link_sd(sd, target, name, 1);
103}
104
105static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
106 const char *name, int warn)
107{
108 struct sysfs_dirent *parent_sd = NULL;
109
110 if (!kobj)
111 parent_sd = &sysfs_root;
112 else
113 parent_sd = kobj->sd;
114
115 if (!parent_sd)
116 return -EFAULT;
117
118 return sysfs_do_create_link_sd(parent_sd, target, name, warn);
119}
120
121/**
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200122 * sysfs_create_link - create symlink between two objects.
123 * @kobj: object whose directory we're creating the link in.
124 * @target: object we're pointing to.
125 * @name: name of the symlink.
126 */
127int sysfs_create_link(struct kobject *kobj, struct kobject *target,
128 const char *name)
129{
130 return sysfs_do_create_link(kobj, target, name, 1);
131}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700132EXPORT_SYMBOL_GPL(sysfs_create_link);
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200133
134/**
135 * sysfs_create_link_nowarn - create symlink between two objects.
136 * @kobj: object whose directory we're creating the link in.
137 * @target: object we're pointing to.
138 * @name: name of the symlink.
139 *
Robert P. J. Day6f1cbd42012-09-04 07:23:35 -0400140 * This function does the same as sysfs_create_link(), but it
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200141 * doesn't warn if the link already exists.
142 */
143int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
144 const char *name)
145{
146 return sysfs_do_create_link(kobj, target, name, 0);
147}
148
149/**
Eric W. Biederman746edb72010-03-30 11:31:28 -0700150 * sysfs_delete_link - remove symlink in object's directory.
151 * @kobj: object we're acting for.
152 * @targ: object we're pointing to.
153 * @name: name of the symlink to remove.
154 *
155 * Unlike sysfs_remove_link sysfs_delete_link has enough information
156 * to successfully delete symlinks in tagged directories.
157 */
158void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
159 const char *name)
160{
161 const void *ns = NULL;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400162
163 /*
164 * We don't own @target and it may be removed at any time.
165 * Synchronize using sysfs_symlink_target_lock. See
166 * sysfs_remove_dir() for details.
167 */
168 spin_lock(&sysfs_symlink_target_lock);
Tejun Heoc84a3b22013-11-23 18:01:46 -0500169 if (targ->sd && (kobj->sd->s_flags & SYSFS_FLAG_NS))
Eric W. Biederman746edb72010-03-30 11:31:28 -0700170 ns = targ->sd->s_ns;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400171 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heo879f40d2013-11-23 17:21:49 -0500172 kernfs_remove_by_name_ns(kobj->sd, name, ns);
Eric W. Biederman746edb72010-03-30 11:31:28 -0700173}
174
175/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * sysfs_remove_link - remove symlink in object's directory.
177 * @kobj: object we're acting for.
178 * @name: name of the symlink to remove.
179 */
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700180void sysfs_remove_link(struct kobject *kobj, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Mark Fasheha839c5a2008-01-29 14:35:18 -0800182 struct sysfs_dirent *parent_sd = NULL;
183
184 if (!kobj)
185 parent_sd = &sysfs_root;
186 else
187 parent_sd = kobj->sd;
188
Tejun Heo879f40d2013-11-23 17:21:49 -0500189 kernfs_remove_by_name(parent_sd, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700191EXPORT_SYMBOL_GPL(sysfs_remove_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800193/**
Tejun Heo4b30ee52013-09-11 22:29:06 -0400194 * sysfs_rename_link_ns - rename symlink in object's directory.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800195 * @kobj: object we're acting for.
196 * @targ: object we're pointing to.
197 * @old: previous name of the symlink.
198 * @new: new name of the symlink.
Tejun Heo4b30ee52013-09-11 22:29:06 -0400199 * @new_ns: new namespace of the symlink.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800200 *
201 * A helper function for the common rename symlink idiom.
202 */
Tejun Heo4b30ee52013-09-11 22:29:06 -0400203int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
204 const char *old, const char *new, const void *new_ns)
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800205{
206 struct sysfs_dirent *parent_sd, *sd = NULL;
Tejun Heo4b30ee52013-09-11 22:29:06 -0400207 const void *old_ns = NULL;
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800208 int result;
209
210 if (!kobj)
211 parent_sd = &sysfs_root;
212 else
213 parent_sd = kobj->sd;
214
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700215 if (targ->sd)
216 old_ns = targ->sd->s_ns;
217
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800218 result = -ENOENT;
Tejun Heo388975c2013-09-11 23:19:13 -0400219 sd = sysfs_get_dirent_ns(parent_sd, old, old_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800220 if (!sd)
221 goto out;
222
223 result = -EINVAL;
224 if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
225 goto out;
226 if (sd->s_symlink.target_sd->s_dir.kobj != targ)
227 goto out;
228
Tejun Heocfec0bc2013-09-11 22:29:09 -0400229 result = sysfs_rename(sd, parent_sd, new, new_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800230
231out:
232 sysfs_put(sd);
233 return result;
234}
Tejun Heo4b30ee52013-09-11 22:29:06 -0400235EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800236
Kay Sievers2f90a852007-11-01 20:20:52 +0100237static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
238 struct sysfs_dirent *target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Kay Sievers2f90a852007-11-01 20:20:52 +0100240 struct sysfs_dirent *base, *sd;
241 char *s = path;
242 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Kay Sievers2f90a852007-11-01 20:20:52 +0100244 /* go up to the root, stop at the base */
245 base = parent_sd;
246 while (base->s_parent) {
247 sd = target_sd->s_parent;
248 while (sd->s_parent && base != sd)
249 sd = sd->s_parent;
250
251 if (base == sd)
252 break;
253
254 strcpy(s, "../");
255 s += 3;
256 base = base->s_parent;
257 }
258
259 /* determine end of target string for reverse fillup */
260 sd = target_sd;
261 while (sd->s_parent && sd != base) {
262 len += strlen(sd->s_name) + 1;
263 sd = sd->s_parent;
264 }
265
266 /* check limits */
267 if (len < 2)
268 return -EINVAL;
269 len--;
270 if ((s - path) + len > PATH_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return -ENAMETOOLONG;
272
Kay Sievers2f90a852007-11-01 20:20:52 +0100273 /* reverse fillup of target string from target to base */
274 sd = target_sd;
275 while (sd->s_parent && sd != base) {
276 int slen = strlen(sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Kay Sievers2f90a852007-11-01 20:20:52 +0100278 len -= slen;
279 strncpy(s + len, sd->s_name, slen);
280 if (len)
281 s[--len] = '/';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Kay Sievers2f90a852007-11-01 20:20:52 +0100283 sd = sd->s_parent;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 return 0;
287}
288
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700289static int sysfs_getlink(struct dentry *dentry, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900291 struct sysfs_dirent *sd = dentry->d_fsdata;
292 struct sysfs_dirent *parent_sd = sd->s_parent;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900293 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +0900294 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Tejun Heo3007e992007-06-14 04:27:23 +0900296 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900297 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900298 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Tejun Heo2b29ac22007-06-14 03:45:15 +0900300 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700303static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 int error = -ENOMEM;
306 unsigned long page = get_zeroed_page(GFP_KERNEL);
Armin Kuster557411e2009-04-29 07:29:59 -1000307 if (page) {
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700308 error = sysfs_getlink(dentry, (char *) page);
Armin Kuster557411e2009-04-29 07:29:59 -1000309 if (error < 0)
310 free_page((unsigned long)page);
311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700313 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700316static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
317 void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 char *page = nd_get_link(nd);
320 if (!IS_ERR(page))
321 free_page((unsigned long)page);
322}
323
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800324const struct inode_operations sysfs_symlink_inode_operations = {
Eric W. Biedermanc099aac2009-11-20 16:08:52 -0800325 .setxattr = sysfs_setxattr,
326 .readlink = generic_readlink,
327 .follow_link = sysfs_follow_link,
328 .put_link = sysfs_put_link,
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800329 .setattr = sysfs_setattr,
330 .getattr = sysfs_getattr,
331 .permission = sysfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332};