blob: b137aa3a486cf73a1093de039c3ea3714368ec7a [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;
Tejun Heoccf73cf2013-11-28 14:54:30 -050047 kernfs_get(target); /* ref owned by symlink */
Tejun Heo5d0e26b2013-11-23 17:21:50 -050048
49 sysfs_addrm_start(&acxt);
Tejun Heo2d0cfbe2013-11-28 14:54:25 -050050 error = sysfs_add_one(&acxt, sd, parent);
Tejun Heo5d0e26b2013-11-23 17:21:50 -050051 sysfs_addrm_finish(&acxt);
52
53 if (!error)
54 return sd;
55
Tejun Heoccf73cf2013-11-28 14:54:30 -050056 kernfs_put(sd);
Tejun Heo5d0e26b2013-11-23 17:21:50 -050057 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 Heoccf73cf2013-11-28 14:54:30 -050075 if (target->sd) {
76 target_sd = target->sd;
77 kernfs_get(target_sd);
78 }
Tejun Heo0cae60f2013-10-30 10:28:36 -040079 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heo2b29ac22007-06-14 03:45:15 +090080
81 if (!target_sd)
Tejun Heo5d0e26b2013-11-23 17:21:50 -050082 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Tejun Heo5d0e26b2013-11-23 17:21:50 -050084 sd = kernfs_create_link(parent_sd, name, target_sd);
Tejun Heoccf73cf2013-11-28 14:54:30 -050085 kernfs_put(target_sd);
Tejun Heo5d0e26b2013-11-23 17:21:50 -050086
87 if (!IS_ERR(sd))
88 return 0;
89
90 if (warn && PTR_ERR(sd) == -EEXIST)
91 sysfs_warn_dup(parent_sd, name);
92 return PTR_ERR(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/**
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +010096 * sysfs_create_link_sd - create symlink to a given object.
97 * @sd: directory we're creating the link in.
98 * @target: object we're pointing to.
99 * @name: name of the symlink.
100 */
101int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
102 const char *name)
103{
104 return sysfs_do_create_link_sd(sd, target, name, 1);
105}
106
107static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
108 const char *name, int warn)
109{
110 struct sysfs_dirent *parent_sd = NULL;
111
112 if (!kobj)
113 parent_sd = &sysfs_root;
114 else
115 parent_sd = kobj->sd;
116
117 if (!parent_sd)
118 return -EFAULT;
119
120 return sysfs_do_create_link_sd(parent_sd, target, name, warn);
121}
122
123/**
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200124 * sysfs_create_link - create symlink between two objects.
125 * @kobj: object whose directory we're creating the link in.
126 * @target: object we're pointing to.
127 * @name: name of the symlink.
128 */
129int sysfs_create_link(struct kobject *kobj, struct kobject *target,
130 const char *name)
131{
132 return sysfs_do_create_link(kobj, target, name, 1);
133}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700134EXPORT_SYMBOL_GPL(sysfs_create_link);
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200135
136/**
137 * sysfs_create_link_nowarn - create symlink between two objects.
138 * @kobj: object whose directory we're creating the link in.
139 * @target: object we're pointing to.
140 * @name: name of the symlink.
141 *
Robert P. J. Day6f1cbd42012-09-04 07:23:35 -0400142 * This function does the same as sysfs_create_link(), but it
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200143 * doesn't warn if the link already exists.
144 */
145int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
146 const char *name)
147{
148 return sysfs_do_create_link(kobj, target, name, 0);
149}
150
151/**
Eric W. Biederman746edb72010-03-30 11:31:28 -0700152 * sysfs_delete_link - remove symlink in object's directory.
153 * @kobj: object we're acting for.
154 * @targ: object we're pointing to.
155 * @name: name of the symlink to remove.
156 *
157 * Unlike sysfs_remove_link sysfs_delete_link has enough information
158 * to successfully delete symlinks in tagged directories.
159 */
160void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
161 const char *name)
162{
163 const void *ns = NULL;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400164
165 /*
166 * We don't own @target and it may be removed at any time.
167 * Synchronize using sysfs_symlink_target_lock. See
168 * sysfs_remove_dir() for details.
169 */
170 spin_lock(&sysfs_symlink_target_lock);
Tejun Heoc84a3b22013-11-23 18:01:46 -0500171 if (targ->sd && (kobj->sd->s_flags & SYSFS_FLAG_NS))
Eric W. Biederman746edb72010-03-30 11:31:28 -0700172 ns = targ->sd->s_ns;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400173 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heo879f40d2013-11-23 17:21:49 -0500174 kernfs_remove_by_name_ns(kobj->sd, name, ns);
Eric W. Biederman746edb72010-03-30 11:31:28 -0700175}
176
177/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * sysfs_remove_link - remove symlink in object's directory.
179 * @kobj: object we're acting for.
180 * @name: name of the symlink to remove.
181 */
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700182void sysfs_remove_link(struct kobject *kobj, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Mark Fasheha839c5a2008-01-29 14:35:18 -0800184 struct sysfs_dirent *parent_sd = NULL;
185
186 if (!kobj)
187 parent_sd = &sysfs_root;
188 else
189 parent_sd = kobj->sd;
190
Tejun Heo879f40d2013-11-23 17:21:49 -0500191 kernfs_remove_by_name(parent_sd, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700193EXPORT_SYMBOL_GPL(sysfs_remove_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800195/**
Tejun Heo4b30ee52013-09-11 22:29:06 -0400196 * sysfs_rename_link_ns - rename symlink in object's directory.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800197 * @kobj: object we're acting for.
198 * @targ: object we're pointing to.
199 * @old: previous name of the symlink.
200 * @new: new name of the symlink.
Tejun Heo4b30ee52013-09-11 22:29:06 -0400201 * @new_ns: new namespace of the symlink.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800202 *
203 * A helper function for the common rename symlink idiom.
204 */
Tejun Heo4b30ee52013-09-11 22:29:06 -0400205int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
206 const char *old, const char *new, const void *new_ns)
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800207{
208 struct sysfs_dirent *parent_sd, *sd = NULL;
Tejun Heo4b30ee52013-09-11 22:29:06 -0400209 const void *old_ns = NULL;
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800210 int result;
211
212 if (!kobj)
213 parent_sd = &sysfs_root;
214 else
215 parent_sd = kobj->sd;
216
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700217 if (targ->sd)
218 old_ns = targ->sd->s_ns;
219
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800220 result = -ENOENT;
Tejun Heoccf73cf2013-11-28 14:54:30 -0500221 sd = kernfs_find_and_get_ns(parent_sd, old, old_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800222 if (!sd)
223 goto out;
224
225 result = -EINVAL;
226 if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
227 goto out;
Tejun Heo7c6e2d32013-11-28 14:54:14 -0500228 if (sd->s_symlink.target_sd->priv != targ)
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800229 goto out;
230
Tejun Heo890ece12013-11-23 17:21:51 -0500231 result = kernfs_rename_ns(sd, parent_sd, new, new_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800232
233out:
Tejun Heoccf73cf2013-11-28 14:54:30 -0500234 kernfs_put(sd);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800235 return result;
236}
Tejun Heo4b30ee52013-09-11 22:29:06 -0400237EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800238
Kay Sievers2f90a852007-11-01 20:20:52 +0100239static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
240 struct sysfs_dirent *target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Kay Sievers2f90a852007-11-01 20:20:52 +0100242 struct sysfs_dirent *base, *sd;
243 char *s = path;
244 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Kay Sievers2f90a852007-11-01 20:20:52 +0100246 /* go up to the root, stop at the base */
247 base = parent_sd;
248 while (base->s_parent) {
249 sd = target_sd->s_parent;
250 while (sd->s_parent && base != sd)
251 sd = sd->s_parent;
252
253 if (base == sd)
254 break;
255
256 strcpy(s, "../");
257 s += 3;
258 base = base->s_parent;
259 }
260
261 /* determine end of target string for reverse fillup */
262 sd = target_sd;
263 while (sd->s_parent && sd != base) {
264 len += strlen(sd->s_name) + 1;
265 sd = sd->s_parent;
266 }
267
268 /* check limits */
269 if (len < 2)
270 return -EINVAL;
271 len--;
272 if ((s - path) + len > PATH_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return -ENAMETOOLONG;
274
Kay Sievers2f90a852007-11-01 20:20:52 +0100275 /* reverse fillup of target string from target to base */
276 sd = target_sd;
277 while (sd->s_parent && sd != base) {
278 int slen = strlen(sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Kay Sievers2f90a852007-11-01 20:20:52 +0100280 len -= slen;
281 strncpy(s + len, sd->s_name, slen);
282 if (len)
283 s[--len] = '/';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Kay Sievers2f90a852007-11-01 20:20:52 +0100285 sd = sd->s_parent;
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 return 0;
289}
290
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700291static int sysfs_getlink(struct dentry *dentry, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900293 struct sysfs_dirent *sd = dentry->d_fsdata;
294 struct sysfs_dirent *parent_sd = sd->s_parent;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900295 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +0900296 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Tejun Heo3007e992007-06-14 04:27:23 +0900298 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900299 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900300 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Tejun Heo2b29ac22007-06-14 03:45:15 +0900302 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700305static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 int error = -ENOMEM;
308 unsigned long page = get_zeroed_page(GFP_KERNEL);
Armin Kuster557411e2009-04-29 07:29:59 -1000309 if (page) {
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700310 error = sysfs_getlink(dentry, (char *) page);
Armin Kuster557411e2009-04-29 07:29:59 -1000311 if (error < 0)
312 free_page((unsigned long)page);
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700315 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700318static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
319 void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 char *page = nd_get_link(nd);
322 if (!IS_ERR(page))
323 free_page((unsigned long)page);
324}
325
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800326const struct inode_operations sysfs_symlink_inode_operations = {
Eric W. Biedermanc099aac2009-11-20 16:08:52 -0800327 .setxattr = sysfs_setxattr,
328 .readlink = generic_readlink,
329 .follow_link = sysfs_follow_link,
330 .put_link = sysfs_put_link,
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800331 .setattr = sysfs_setattr,
332 .getattr = sysfs_getattr,
333 .permission = sysfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};