blob: 88c8bc5e891196f8a1c794f8ab89f6fef5006eb0 [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;
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 Heo608e2662007-06-14 04:27:22 +090035 /* target->sd can go away beneath us but is protected with
Tejun Heo5f995322007-06-14 04:27:23 +090036 * sysfs_assoc_lock. Fetch target_sd from it.
Tejun Heo2b29ac22007-06-14 03:45:15 +090037 */
Tejun Heo5f995322007-06-14 04:27:23 +090038 spin_lock(&sysfs_assoc_lock);
Tejun Heo608e2662007-06-14 04:27:22 +090039 if (target->sd)
40 target_sd = sysfs_get(target->sd);
Tejun Heo5f995322007-06-14 04:27:23 +090041 spin_unlock(&sysfs_assoc_lock);
Tejun Heo2b29ac22007-06-14 03:45:15 +090042
Tejun Heo3007e992007-06-14 04:27:23 +090043 error = -ENOENT;
Tejun Heo2b29ac22007-06-14 03:45:15 +090044 if (!target_sd)
Tejun Heo3007e992007-06-14 04:27:23 +090045 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Tejun Heo3007e992007-06-14 04:27:23 +090047 error = -ENOMEM;
48 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
49 if (!sd)
50 goto out_put;
Tejun Heoa1da4df2007-07-18 16:14:45 +090051
Tejun Heocb26a312013-09-11 22:29:07 -040052 sd->s_ns = target_sd->s_ns;
Tejun Heob1fc3d62007-09-20 16:05:11 +090053 sd->s_symlink.target_sd = target_sd;
Tejun Heoa1da4df2007-07-18 16:14:45 +090054 target_sd = NULL; /* reference is now owned by the symlink */
Tejun Heo2b29ac22007-06-14 03:45:15 +090055
Tejun Heofb6896d2007-06-14 04:27:24 +090056 sysfs_addrm_start(&acxt, parent_sd);
Tejun Heocb26a312013-09-11 22:29:07 -040057 if (warn)
58 error = sysfs_add_one(&acxt, sd);
59 else
60 error = __sysfs_add_one(&acxt, sd);
Tejun Heo23dc2792007-08-02 21:38:03 +090061 sysfs_addrm_finish(&acxt);
Tejun Heofb6896d2007-06-14 04:27:24 +090062
Tejun Heo23dc2792007-08-02 21:38:03 +090063 if (error)
Tejun Heo967e35d2007-07-18 16:38:11 +090064 goto out_put;
Tejun Heofb6896d2007-06-14 04:27:24 +090065
Tejun Heo967e35d2007-07-18 16:38:11 +090066 return 0;
67
Tejun Heo3007e992007-06-14 04:27:23 +090068 out_put:
69 sysfs_put(target_sd);
70 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 return error;
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/**
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +010075 * sysfs_create_link_sd - create symlink to a given object.
76 * @sd: directory we're creating the link in.
77 * @target: object we're pointing to.
78 * @name: name of the symlink.
79 */
80int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
81 const char *name)
82{
83 return sysfs_do_create_link_sd(sd, target, name, 1);
84}
85
86static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
87 const char *name, int warn)
88{
89 struct sysfs_dirent *parent_sd = NULL;
90
91 if (!kobj)
92 parent_sd = &sysfs_root;
93 else
94 parent_sd = kobj->sd;
95
96 if (!parent_sd)
97 return -EFAULT;
98
99 return sysfs_do_create_link_sd(parent_sd, target, name, warn);
100}
101
102/**
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200103 * sysfs_create_link - create symlink between two objects.
104 * @kobj: object whose directory we're creating the link in.
105 * @target: object we're pointing to.
106 * @name: name of the symlink.
107 */
108int sysfs_create_link(struct kobject *kobj, struct kobject *target,
109 const char *name)
110{
111 return sysfs_do_create_link(kobj, target, name, 1);
112}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700113EXPORT_SYMBOL_GPL(sysfs_create_link);
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200114
115/**
116 * sysfs_create_link_nowarn - create symlink between two objects.
117 * @kobj: object whose directory we're creating the link in.
118 * @target: object we're pointing to.
119 * @name: name of the symlink.
120 *
Robert P. J. Day6f1cbd42012-09-04 07:23:35 -0400121 * This function does the same as sysfs_create_link(), but it
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200122 * doesn't warn if the link already exists.
123 */
124int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
125 const char *name)
126{
127 return sysfs_do_create_link(kobj, target, name, 0);
128}
129
130/**
Eric W. Biederman746edb72010-03-30 11:31:28 -0700131 * sysfs_delete_link - remove symlink in object's directory.
132 * @kobj: object we're acting for.
133 * @targ: object we're pointing to.
134 * @name: name of the symlink to remove.
135 *
136 * Unlike sysfs_remove_link sysfs_delete_link has enough information
137 * to successfully delete symlinks in tagged directories.
138 */
139void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
140 const char *name)
141{
142 const void *ns = NULL;
143 spin_lock(&sysfs_assoc_lock);
Tejun Heocb26a312013-09-11 22:29:07 -0400144 if (targ->sd)
Eric W. Biederman746edb72010-03-30 11:31:28 -0700145 ns = targ->sd->s_ns;
146 spin_unlock(&sysfs_assoc_lock);
Tejun Heocfec0bc2013-09-11 22:29:09 -0400147 sysfs_hash_and_remove(kobj->sd, name, ns);
Eric W. Biederman746edb72010-03-30 11:31:28 -0700148}
149
150/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * sysfs_remove_link - remove symlink in object's directory.
152 * @kobj: object we're acting for.
153 * @name: name of the symlink to remove.
154 */
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700155void sysfs_remove_link(struct kobject *kobj, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Mark Fasheha839c5a2008-01-29 14:35:18 -0800157 struct sysfs_dirent *parent_sd = NULL;
158
159 if (!kobj)
160 parent_sd = &sysfs_root;
161 else
162 parent_sd = kobj->sd;
163
Tejun Heocfec0bc2013-09-11 22:29:09 -0400164 sysfs_hash_and_remove(parent_sd, name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700166EXPORT_SYMBOL_GPL(sysfs_remove_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800168/**
Tejun Heo4b30ee52013-09-11 22:29:06 -0400169 * sysfs_rename_link_ns - rename symlink in object's directory.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800170 * @kobj: object we're acting for.
171 * @targ: object we're pointing to.
172 * @old: previous name of the symlink.
173 * @new: new name of the symlink.
Tejun Heo4b30ee52013-09-11 22:29:06 -0400174 * @new_ns: new namespace of the symlink.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800175 *
176 * A helper function for the common rename symlink idiom.
177 */
Tejun Heo4b30ee52013-09-11 22:29:06 -0400178int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
179 const char *old, const char *new, const void *new_ns)
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800180{
181 struct sysfs_dirent *parent_sd, *sd = NULL;
Tejun Heo4b30ee52013-09-11 22:29:06 -0400182 const void *old_ns = NULL;
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800183 int result;
184
185 if (!kobj)
186 parent_sd = &sysfs_root;
187 else
188 parent_sd = kobj->sd;
189
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700190 if (targ->sd)
191 old_ns = targ->sd->s_ns;
192
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800193 result = -ENOENT;
Tejun Heo388975c2013-09-11 23:19:13 -0400194 sd = sysfs_get_dirent_ns(parent_sd, old, old_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800195 if (!sd)
196 goto out;
197
198 result = -EINVAL;
199 if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
200 goto out;
201 if (sd->s_symlink.target_sd->s_dir.kobj != targ)
202 goto out;
203
Tejun Heocfec0bc2013-09-11 22:29:09 -0400204 result = sysfs_rename(sd, parent_sd, new, new_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800205
206out:
207 sysfs_put(sd);
208 return result;
209}
Tejun Heo4b30ee52013-09-11 22:29:06 -0400210EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800211
Kay Sievers2f90a852007-11-01 20:20:52 +0100212static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
213 struct sysfs_dirent *target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Kay Sievers2f90a852007-11-01 20:20:52 +0100215 struct sysfs_dirent *base, *sd;
216 char *s = path;
217 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Kay Sievers2f90a852007-11-01 20:20:52 +0100219 /* go up to the root, stop at the base */
220 base = parent_sd;
221 while (base->s_parent) {
222 sd = target_sd->s_parent;
223 while (sd->s_parent && base != sd)
224 sd = sd->s_parent;
225
226 if (base == sd)
227 break;
228
229 strcpy(s, "../");
230 s += 3;
231 base = base->s_parent;
232 }
233
234 /* determine end of target string for reverse fillup */
235 sd = target_sd;
236 while (sd->s_parent && sd != base) {
237 len += strlen(sd->s_name) + 1;
238 sd = sd->s_parent;
239 }
240
241 /* check limits */
242 if (len < 2)
243 return -EINVAL;
244 len--;
245 if ((s - path) + len > PATH_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return -ENAMETOOLONG;
247
Kay Sievers2f90a852007-11-01 20:20:52 +0100248 /* reverse fillup of target string from target to base */
249 sd = target_sd;
250 while (sd->s_parent && sd != base) {
251 int slen = strlen(sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Kay Sievers2f90a852007-11-01 20:20:52 +0100253 len -= slen;
254 strncpy(s + len, sd->s_name, slen);
255 if (len)
256 s[--len] = '/';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Kay Sievers2f90a852007-11-01 20:20:52 +0100258 sd = sd->s_parent;
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 return 0;
262}
263
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700264static int sysfs_getlink(struct dentry *dentry, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900266 struct sysfs_dirent *sd = dentry->d_fsdata;
267 struct sysfs_dirent *parent_sd = sd->s_parent;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900268 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +0900269 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Tejun Heo3007e992007-06-14 04:27:23 +0900271 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900272 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900273 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Tejun Heo2b29ac22007-06-14 03:45:15 +0900275 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700278static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int error = -ENOMEM;
281 unsigned long page = get_zeroed_page(GFP_KERNEL);
Armin Kuster557411e2009-04-29 07:29:59 -1000282 if (page) {
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700283 error = sysfs_getlink(dentry, (char *) page);
Armin Kuster557411e2009-04-29 07:29:59 -1000284 if (error < 0)
285 free_page((unsigned long)page);
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700288 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700291static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
292 void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 char *page = nd_get_link(nd);
295 if (!IS_ERR(page))
296 free_page((unsigned long)page);
297}
298
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800299const struct inode_operations sysfs_symlink_inode_operations = {
Eric W. Biedermanc099aac2009-11-20 16:08:52 -0800300 .setxattr = sysfs_setxattr,
301 .readlink = generic_readlink,
302 .follow_link = sysfs_follow_link,
303 .put_link = sysfs_put_link,
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800304 .setattr = sysfs_setattr,
305 .getattr = sysfs_getattr,
306 .permission = sysfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307};