blob: 3ae3f1bf1a0957075bec9d36d8834550000c3b14 [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 Heofb6896d2007-06-14 04:27:24 +090030 struct sysfs_addrm_cxt acxt;
Linus Torvaldsa1212d22013-11-07 20:47:28 +090031 enum kobj_ns_type ns_type;
Tejun Heo3007e992007-06-14 04:27:23 +090032 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +010034 BUG_ON(!name || !parent_sd);
Tejun Heo2b29ac22007-06-14 03:45:15 +090035
Tejun Heo0cae60f2013-10-30 10:28:36 -040036 /*
37 * We don't own @target and it may be removed at any time.
38 * Synchronize using sysfs_symlink_target_lock. See
39 * sysfs_remove_dir() for details.
Tejun Heo2b29ac22007-06-14 03:45:15 +090040 */
Tejun Heo0cae60f2013-10-30 10:28:36 -040041 spin_lock(&sysfs_symlink_target_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090042 if (target->sd)
43 target_sd = sysfs_get(target->sd);
Tejun Heo0cae60f2013-10-30 10:28:36 -040044 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heo2b29ac22007-06-14 03:45:15 +090045
Tejun Heo3007e992007-06-14 04:27:23 +090046 error = -ENOENT;
Tejun Heo2b29ac22007-06-14 03:45:15 +090047 if (!target_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090048 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tejun Heo3007e992007-06-14 04:27:23 +090050 error = -ENOMEM;
51 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
52 if (!sd)
53 goto out_put;
Tejun Heoa1da4df2007-07-18 16:14:45 +090054
Linus Torvaldsa1212d22013-11-07 20:47:28 +090055 ns_type = sysfs_ns_type(parent_sd);
56 if (ns_type)
57 sd->s_ns = target_sd->s_ns;
Tejun Heob1fc3d62007-09-20 16:05:11 +090058 sd->s_symlink.target_sd = target_sd;
Tejun Heoa1da4df2007-07-18 16:14:45 +090059 target_sd = NULL; /* reference is now owned by the symlink */
Tejun Heo2b29ac22007-06-14 03:45:15 +090060
Tejun Heod69ac5a2013-09-18 17:15:35 -040061 sysfs_addrm_start(&acxt);
Linus Torvaldsa1212d22013-11-07 20:47:28 +090062 /* Symlinks must be between directories with the same ns_type */
63 if (!ns_type ||
64 (ns_type == sysfs_ns_type(sd->s_symlink.target_sd->s_parent))) {
65 if (warn)
66 error = sysfs_add_one(&acxt, sd, parent_sd);
67 else
68 error = __sysfs_add_one(&acxt, sd, parent_sd);
69 } else {
70 error = -EINVAL;
71 WARN(1, KERN_WARNING
72 "sysfs: symlink across ns_types %s/%s -> %s/%s\n",
73 parent_sd->s_name,
74 sd->s_name,
75 sd->s_symlink.target_sd->s_parent->s_name,
76 sd->s_symlink.target_sd->s_name);
77 }
Tejun Heo23dc2792007-08-02 21:38:03 +090078 sysfs_addrm_finish(&acxt);
Tejun Heofb6896d2007-06-14 04:27:24 +090079
Tejun Heo23dc2792007-08-02 21:38:03 +090080 if (error)
Tejun Heo967e35d2007-07-18 16:38:11 +090081 goto out_put;
Tejun Heofb6896d2007-06-14 04:27:24 +090082
Tejun Heo967e35d2007-07-18 16:38:11 +090083 return 0;
84
Tejun Heo3007e992007-06-14 04:27:23 +090085 out_put:
86 sysfs_put(target_sd);
87 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return error;
89}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/**
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +010092 * sysfs_create_link_sd - create symlink to a given object.
93 * @sd: directory we're creating the link in.
94 * @target: object we're pointing to.
95 * @name: name of the symlink.
96 */
97int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
98 const char *name)
99{
100 return sysfs_do_create_link_sd(sd, target, name, 1);
101}
102
103static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
104 const char *name, int warn)
105{
106 struct sysfs_dirent *parent_sd = NULL;
107
108 if (!kobj)
109 parent_sd = &sysfs_root;
110 else
111 parent_sd = kobj->sd;
112
113 if (!parent_sd)
114 return -EFAULT;
115
116 return sysfs_do_create_link_sd(parent_sd, target, name, warn);
117}
118
119/**
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200120 * sysfs_create_link - create symlink between two objects.
121 * @kobj: object whose directory we're creating the link in.
122 * @target: object we're pointing to.
123 * @name: name of the symlink.
124 */
125int sysfs_create_link(struct kobject *kobj, struct kobject *target,
126 const char *name)
127{
128 return sysfs_do_create_link(kobj, target, name, 1);
129}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700130EXPORT_SYMBOL_GPL(sysfs_create_link);
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200131
132/**
133 * sysfs_create_link_nowarn - create symlink between two objects.
134 * @kobj: object whose directory we're creating the link in.
135 * @target: object we're pointing to.
136 * @name: name of the symlink.
137 *
Robert P. J. Day6f1cbd42012-09-04 07:23:35 -0400138 * This function does the same as sysfs_create_link(), but it
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200139 * doesn't warn if the link already exists.
140 */
141int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
142 const char *name)
143{
144 return sysfs_do_create_link(kobj, target, name, 0);
145}
146
147/**
Eric W. Biederman746edb72010-03-30 11:31:28 -0700148 * sysfs_delete_link - remove symlink in object's directory.
149 * @kobj: object we're acting for.
150 * @targ: object we're pointing to.
151 * @name: name of the symlink to remove.
152 *
153 * Unlike sysfs_remove_link sysfs_delete_link has enough information
154 * to successfully delete symlinks in tagged directories.
155 */
156void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
157 const char *name)
158{
159 const void *ns = NULL;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400160
161 /*
162 * We don't own @target and it may be removed at any time.
163 * Synchronize using sysfs_symlink_target_lock. See
164 * sysfs_remove_dir() for details.
165 */
166 spin_lock(&sysfs_symlink_target_lock);
Linus Torvaldsa1212d22013-11-07 20:47:28 +0900167 if (targ->sd && sysfs_ns_type(kobj->sd))
Eric W. Biederman746edb72010-03-30 11:31:28 -0700168 ns = targ->sd->s_ns;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400169 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heocfec0bc2013-09-11 22:29:09 -0400170 sysfs_hash_and_remove(kobj->sd, name, ns);
Eric W. Biederman746edb72010-03-30 11:31:28 -0700171}
172
173/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * sysfs_remove_link - remove symlink in object's directory.
175 * @kobj: object we're acting for.
176 * @name: name of the symlink to remove.
177 */
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700178void sysfs_remove_link(struct kobject *kobj, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Mark Fasheha839c5a2008-01-29 14:35:18 -0800180 struct sysfs_dirent *parent_sd = NULL;
181
182 if (!kobj)
183 parent_sd = &sysfs_root;
184 else
185 parent_sd = kobj->sd;
186
Tejun Heocfec0bc2013-09-11 22:29:09 -0400187 sysfs_hash_and_remove(parent_sd, name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700189EXPORT_SYMBOL_GPL(sysfs_remove_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800191/**
Tejun Heo4b30ee52013-09-11 22:29:06 -0400192 * sysfs_rename_link_ns - rename symlink in object's directory.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800193 * @kobj: object we're acting for.
194 * @targ: object we're pointing to.
195 * @old: previous name of the symlink.
196 * @new: new name of the symlink.
Tejun Heo4b30ee52013-09-11 22:29:06 -0400197 * @new_ns: new namespace of the symlink.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800198 *
199 * A helper function for the common rename symlink idiom.
200 */
Tejun Heo4b30ee52013-09-11 22:29:06 -0400201int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
202 const char *old, const char *new, const void *new_ns)
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800203{
204 struct sysfs_dirent *parent_sd, *sd = NULL;
Tejun Heo4b30ee52013-09-11 22:29:06 -0400205 const void *old_ns = NULL;
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800206 int result;
207
208 if (!kobj)
209 parent_sd = &sysfs_root;
210 else
211 parent_sd = kobj->sd;
212
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700213 if (targ->sd)
214 old_ns = targ->sd->s_ns;
215
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800216 result = -ENOENT;
Tejun Heo388975c2013-09-11 23:19:13 -0400217 sd = sysfs_get_dirent_ns(parent_sd, old, old_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800218 if (!sd)
219 goto out;
220
221 result = -EINVAL;
222 if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
223 goto out;
224 if (sd->s_symlink.target_sd->s_dir.kobj != targ)
225 goto out;
226
Tejun Heocfec0bc2013-09-11 22:29:09 -0400227 result = sysfs_rename(sd, parent_sd, new, new_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800228
229out:
230 sysfs_put(sd);
231 return result;
232}
Tejun Heo4b30ee52013-09-11 22:29:06 -0400233EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800234
Kay Sievers2f90a852007-11-01 20:20:52 +0100235static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
236 struct sysfs_dirent *target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Kay Sievers2f90a852007-11-01 20:20:52 +0100238 struct sysfs_dirent *base, *sd;
239 char *s = path;
240 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Kay Sievers2f90a852007-11-01 20:20:52 +0100242 /* go up to the root, stop at the base */
243 base = parent_sd;
244 while (base->s_parent) {
245 sd = target_sd->s_parent;
246 while (sd->s_parent && base != sd)
247 sd = sd->s_parent;
248
249 if (base == sd)
250 break;
251
252 strcpy(s, "../");
253 s += 3;
254 base = base->s_parent;
255 }
256
257 /* determine end of target string for reverse fillup */
258 sd = target_sd;
259 while (sd->s_parent && sd != base) {
260 len += strlen(sd->s_name) + 1;
261 sd = sd->s_parent;
262 }
263
264 /* check limits */
265 if (len < 2)
266 return -EINVAL;
267 len--;
268 if ((s - path) + len > PATH_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -ENAMETOOLONG;
270
Kay Sievers2f90a852007-11-01 20:20:52 +0100271 /* reverse fillup of target string from target to base */
272 sd = target_sd;
273 while (sd->s_parent && sd != base) {
274 int slen = strlen(sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Kay Sievers2f90a852007-11-01 20:20:52 +0100276 len -= slen;
277 strncpy(s + len, sd->s_name, slen);
278 if (len)
279 s[--len] = '/';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Kay Sievers2f90a852007-11-01 20:20:52 +0100281 sd = sd->s_parent;
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 return 0;
285}
286
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700287static int sysfs_getlink(struct dentry *dentry, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900289 struct sysfs_dirent *sd = dentry->d_fsdata;
290 struct sysfs_dirent *parent_sd = sd->s_parent;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900291 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +0900292 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Tejun Heo3007e992007-06-14 04:27:23 +0900294 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900295 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900296 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Tejun Heo2b29ac22007-06-14 03:45:15 +0900298 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700301static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 int error = -ENOMEM;
304 unsigned long page = get_zeroed_page(GFP_KERNEL);
Armin Kuster557411e2009-04-29 07:29:59 -1000305 if (page) {
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700306 error = sysfs_getlink(dentry, (char *) page);
Armin Kuster557411e2009-04-29 07:29:59 -1000307 if (error < 0)
308 free_page((unsigned long)page);
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700311 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700314static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
315 void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 char *page = nd_get_link(nd);
318 if (!IS_ERR(page))
319 free_page((unsigned long)page);
320}
321
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800322const struct inode_operations sysfs_symlink_inode_operations = {
Eric W. Biedermanc099aac2009-11-20 16:08:52 -0800323 .setxattr = sysfs_setxattr,
324 .readlink = generic_readlink,
325 .follow_link = sysfs_follow_link,
326 .put_link = sysfs_put_link,
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800327 .setattr = sysfs_setattr,
328 .getattr = sysfs_getattr,
329 .permission = sysfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330};