blob: c660363fdaeadb086733c322ae062174ce8f7b55 [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
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +010024static int sysfs_do_create_link_sd(struct sysfs_dirent *parent_sd,
25 struct kobject *target,
26 const char *name, int warn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Tejun Heo2b29ac22007-06-14 03:45:15 +090028 struct sysfs_dirent *target_sd = NULL;
Tejun Heo3007e992007-06-14 04:27:23 +090029 struct sysfs_dirent *sd = NULL;
Tejun Heofb6896da2007-06-14 04:27:24 +090030 struct sysfs_addrm_cxt acxt;
Tejun Heo3007e992007-06-14 04:27:23 +090031 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +010033 BUG_ON(!name || !parent_sd);
Tejun Heo2b29ac22007-06-14 03:45:15 +090034
Tejun Heo0cae60f2013-10-30 10:28:36 -040035 /*
36 * We don't own @target and it may be removed at any time.
37 * Synchronize using sysfs_symlink_target_lock. See
38 * sysfs_remove_dir() for details.
Tejun Heo2b29ac22007-06-14 03:45:15 +090039 */
Tejun Heo0cae60f2013-10-30 10:28:36 -040040 spin_lock(&sysfs_symlink_target_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090041 if (target->sd)
42 target_sd = sysfs_get(target->sd);
Tejun Heo0cae60f2013-10-30 10:28:36 -040043 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heo2b29ac22007-06-14 03:45:15 +090044
Tejun Heo3007e992007-06-14 04:27:23 +090045 error = -ENOENT;
Tejun Heo2b29ac22007-06-14 03:45:15 +090046 if (!target_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090047 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Tejun Heo3007e992007-06-14 04:27:23 +090049 error = -ENOMEM;
50 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
51 if (!sd)
52 goto out_put;
Tejun Heoa1da4df2007-07-18 16:14:45 +090053
Tejun Heoc84a3b22013-11-23 18:01:46 -050054 if (parent_sd->s_flags & SYSFS_FLAG_NS)
Linus Torvaldsa1212d22013-11-07 20:47:28 +090055 sd->s_ns = target_sd->s_ns;
Tejun Heob1fc3d62007-09-20 16:05:11 +090056 sd->s_symlink.target_sd = target_sd;
Tejun Heoa1da4df2007-07-18 16:14:45 +090057 target_sd = NULL; /* reference is now owned by the symlink */
Tejun Heo2b29ac22007-06-14 03:45:15 +090058
Tejun Heod69ac5a2013-09-18 17:15:35 -040059 sysfs_addrm_start(&acxt);
Tejun Heoc84a3b22013-11-23 18:01:46 -050060 if (warn)
61 error = sysfs_add_one(&acxt, sd, parent_sd);
62 else
63 error = __sysfs_add_one(&acxt, sd, parent_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +090064 sysfs_addrm_finish(&acxt);
Tejun Heofb6896da2007-06-14 04:27:24 +090065
Tejun Heo23dc2792007-08-02 21:38:03 +090066 if (error)
Tejun Heo967e35d2007-07-18 16:38:11 +090067 goto out_put;
Tejun Heofb6896da2007-06-14 04:27:24 +090068
Tejun Heo967e35d2007-07-18 16:38:11 +090069 return 0;
70
Tejun Heo3007e992007-06-14 04:27:23 +090071 out_put:
72 sysfs_put(target_sd);
73 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return error;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/**
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +010078 * sysfs_create_link_sd - create symlink to a given object.
79 * @sd: directory we're creating the link in.
80 * @target: object we're pointing to.
81 * @name: name of the symlink.
82 */
83int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
84 const char *name)
85{
86 return sysfs_do_create_link_sd(sd, target, name, 1);
87}
88
89static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
90 const char *name, int warn)
91{
92 struct sysfs_dirent *parent_sd = NULL;
93
94 if (!kobj)
95 parent_sd = &sysfs_root;
96 else
97 parent_sd = kobj->sd;
98
99 if (!parent_sd)
100 return -EFAULT;
101
102 return sysfs_do_create_link_sd(parent_sd, target, name, warn);
103}
104
105/**
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200106 * sysfs_create_link - create symlink between two objects.
107 * @kobj: object whose directory we're creating the link in.
108 * @target: object we're pointing to.
109 * @name: name of the symlink.
110 */
111int sysfs_create_link(struct kobject *kobj, struct kobject *target,
112 const char *name)
113{
114 return sysfs_do_create_link(kobj, target, name, 1);
115}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700116EXPORT_SYMBOL_GPL(sysfs_create_link);
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200117
118/**
119 * sysfs_create_link_nowarn - create symlink between two objects.
120 * @kobj: object whose directory we're creating the link in.
121 * @target: object we're pointing to.
122 * @name: name of the symlink.
123 *
Robert P. J. Day6f1cbd42012-09-04 07:23:35 -0400124 * This function does the same as sysfs_create_link(), but it
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200125 * doesn't warn if the link already exists.
126 */
127int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
128 const char *name)
129{
130 return sysfs_do_create_link(kobj, target, name, 0);
131}
132
133/**
Eric W. Biederman746edb72010-03-30 11:31:28 -0700134 * sysfs_delete_link - remove symlink in object's directory.
135 * @kobj: object we're acting for.
136 * @targ: object we're pointing to.
137 * @name: name of the symlink to remove.
138 *
139 * Unlike sysfs_remove_link sysfs_delete_link has enough information
140 * to successfully delete symlinks in tagged directories.
141 */
142void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
143 const char *name)
144{
145 const void *ns = NULL;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400146
147 /*
148 * We don't own @target and it may be removed at any time.
149 * Synchronize using sysfs_symlink_target_lock. See
150 * sysfs_remove_dir() for details.
151 */
152 spin_lock(&sysfs_symlink_target_lock);
Tejun Heoc84a3b22013-11-23 18:01:46 -0500153 if (targ->sd && (kobj->sd->s_flags & SYSFS_FLAG_NS))
Eric W. Biederman746edb72010-03-30 11:31:28 -0700154 ns = targ->sd->s_ns;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400155 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heocfec0bc2013-09-11 22:29:09 -0400156 sysfs_hash_and_remove(kobj->sd, name, ns);
Eric W. Biederman746edb72010-03-30 11:31:28 -0700157}
158
159/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 * sysfs_remove_link - remove symlink in object's directory.
161 * @kobj: object we're acting for.
162 * @name: name of the symlink to remove.
163 */
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700164void sysfs_remove_link(struct kobject *kobj, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Mark Fasheha839c5a2008-01-29 14:35:18 -0800166 struct sysfs_dirent *parent_sd = NULL;
167
168 if (!kobj)
169 parent_sd = &sysfs_root;
170 else
171 parent_sd = kobj->sd;
172
Tejun Heocfec0bc2013-09-11 22:29:09 -0400173 sysfs_hash_and_remove(parent_sd, name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700175EXPORT_SYMBOL_GPL(sysfs_remove_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800177/**
Tejun Heo4b30ee52013-09-11 22:29:06 -0400178 * sysfs_rename_link_ns - rename symlink in object's directory.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800179 * @kobj: object we're acting for.
180 * @targ: object we're pointing to.
181 * @old: previous name of the symlink.
182 * @new: new name of the symlink.
Tejun Heo4b30ee52013-09-11 22:29:06 -0400183 * @new_ns: new namespace of the symlink.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800184 *
185 * A helper function for the common rename symlink idiom.
186 */
Tejun Heo4b30ee52013-09-11 22:29:06 -0400187int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
188 const char *old, const char *new, const void *new_ns)
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800189{
190 struct sysfs_dirent *parent_sd, *sd = NULL;
Tejun Heo4b30ee52013-09-11 22:29:06 -0400191 const void *old_ns = NULL;
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800192 int result;
193
194 if (!kobj)
195 parent_sd = &sysfs_root;
196 else
197 parent_sd = kobj->sd;
198
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700199 if (targ->sd)
200 old_ns = targ->sd->s_ns;
201
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800202 result = -ENOENT;
Tejun Heo388975c2013-09-11 23:19:13 -0400203 sd = sysfs_get_dirent_ns(parent_sd, old, old_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800204 if (!sd)
205 goto out;
206
207 result = -EINVAL;
208 if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
209 goto out;
210 if (sd->s_symlink.target_sd->s_dir.kobj != targ)
211 goto out;
212
Tejun Heocfec0bc2013-09-11 22:29:09 -0400213 result = sysfs_rename(sd, parent_sd, new, new_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800214
215out:
216 sysfs_put(sd);
217 return result;
218}
Tejun Heo4b30ee52013-09-11 22:29:06 -0400219EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800220
Kay Sievers2f90a852007-11-01 20:20:52 +0100221static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
222 struct sysfs_dirent *target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Kay Sievers2f90a852007-11-01 20:20:52 +0100224 struct sysfs_dirent *base, *sd;
225 char *s = path;
226 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Kay Sievers2f90a852007-11-01 20:20:52 +0100228 /* go up to the root, stop at the base */
229 base = parent_sd;
230 while (base->s_parent) {
231 sd = target_sd->s_parent;
232 while (sd->s_parent && base != sd)
233 sd = sd->s_parent;
234
235 if (base == sd)
236 break;
237
238 strcpy(s, "../");
239 s += 3;
240 base = base->s_parent;
241 }
242
243 /* determine end of target string for reverse fillup */
244 sd = target_sd;
245 while (sd->s_parent && sd != base) {
246 len += strlen(sd->s_name) + 1;
247 sd = sd->s_parent;
248 }
249
250 /* check limits */
251 if (len < 2)
252 return -EINVAL;
253 len--;
254 if ((s - path) + len > PATH_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -ENAMETOOLONG;
256
Kay Sievers2f90a852007-11-01 20:20:52 +0100257 /* reverse fillup of target string from target to base */
258 sd = target_sd;
259 while (sd->s_parent && sd != base) {
260 int slen = strlen(sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Kay Sievers2f90a852007-11-01 20:20:52 +0100262 len -= slen;
263 strncpy(s + len, sd->s_name, slen);
264 if (len)
265 s[--len] = '/';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Kay Sievers2f90a852007-11-01 20:20:52 +0100267 sd = sd->s_parent;
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 return 0;
271}
272
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700273static int sysfs_getlink(struct dentry *dentry, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900275 struct sysfs_dirent *sd = dentry->d_fsdata;
276 struct sysfs_dirent *parent_sd = sd->s_parent;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900277 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +0900278 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Tejun Heo3007e992007-06-14 04:27:23 +0900280 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900281 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900282 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Tejun Heo2b29ac22007-06-14 03:45:15 +0900284 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700287static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 int error = -ENOMEM;
290 unsigned long page = get_zeroed_page(GFP_KERNEL);
Armin Kuster557411e2009-04-29 07:29:59 -1000291 if (page) {
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700292 error = sysfs_getlink(dentry, (char *) page);
Armin Kuster557411e2009-04-29 07:29:59 -1000293 if (error < 0)
294 free_page((unsigned long)page);
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700297 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700300static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
301 void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 char *page = nd_get_link(nd);
304 if (!IS_ERR(page))
305 free_page((unsigned long)page);
306}
307
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800308const struct inode_operations sysfs_symlink_inode_operations = {
Eric W. Biedermanc099aac2009-11-20 16:08:52 -0800309 .setxattr = sysfs_setxattr,
310 .readlink = generic_readlink,
311 .follow_link = sysfs_follow_link,
312 .put_link = sysfs_put_link,
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800313 .setattr = sysfs_setattr,
314 .getattr = sysfs_getattr,
315 .permission = sysfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316};