blob: 1a23681b817901a46dad3e4432c54de147248d37 [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 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 Heocb26a312013-09-11 22:29:07 -040054 sd->s_ns = target_sd->s_ns;
Tejun Heob1fc3d62007-09-20 16:05:11 +090055 sd->s_symlink.target_sd = target_sd;
Tejun Heoa1da4df2007-07-18 16:14:45 +090056 target_sd = NULL; /* reference is now owned by the symlink */
Tejun Heo2b29ac22007-06-14 03:45:15 +090057
Tejun Heod69ac5a2013-09-18 17:15:35 -040058 sysfs_addrm_start(&acxt);
Tejun Heocb26a312013-09-11 22:29:07 -040059 if (warn)
Tejun Heod69ac5a2013-09-18 17:15:35 -040060 error = sysfs_add_one(&acxt, sd, parent_sd);
Tejun Heocb26a312013-09-11 22:29:07 -040061 else
Tejun Heod69ac5a2013-09-18 17:15:35 -040062 error = __sysfs_add_one(&acxt, sd, parent_sd);
Tejun Heo23dc2792007-08-02 21:38:03 +090063 sysfs_addrm_finish(&acxt);
Tejun Heofb6896d2007-06-14 04:27:24 +090064
Tejun Heo23dc2792007-08-02 21:38:03 +090065 if (error)
Tejun Heo967e35d2007-07-18 16:38:11 +090066 goto out_put;
Tejun Heofb6896d2007-06-14 04:27:24 +090067
Tejun Heo967e35d2007-07-18 16:38:11 +090068 return 0;
69
Tejun Heo3007e992007-06-14 04:27:23 +090070 out_put:
71 sysfs_put(target_sd);
72 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return error;
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/**
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +010077 * sysfs_create_link_sd - create symlink to a given object.
78 * @sd: directory we're creating the link in.
79 * @target: object we're pointing to.
80 * @name: name of the symlink.
81 */
82int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
83 const char *name)
84{
85 return sysfs_do_create_link_sd(sd, target, name, 1);
86}
87
88static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
89 const char *name, int warn)
90{
91 struct sysfs_dirent *parent_sd = NULL;
92
93 if (!kobj)
94 parent_sd = &sysfs_root;
95 else
96 parent_sd = kobj->sd;
97
98 if (!parent_sd)
99 return -EFAULT;
100
101 return sysfs_do_create_link_sd(parent_sd, target, name, warn);
102}
103
104/**
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200105 * sysfs_create_link - create symlink between two objects.
106 * @kobj: object whose directory we're creating the link in.
107 * @target: object we're pointing to.
108 * @name: name of the symlink.
109 */
110int sysfs_create_link(struct kobject *kobj, struct kobject *target,
111 const char *name)
112{
113 return sysfs_do_create_link(kobj, target, name, 1);
114}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700115EXPORT_SYMBOL_GPL(sysfs_create_link);
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200116
117/**
118 * sysfs_create_link_nowarn - create symlink between two objects.
119 * @kobj: object whose directory we're creating the link in.
120 * @target: object we're pointing to.
121 * @name: name of the symlink.
122 *
Robert P. J. Day6f1cbd42012-09-04 07:23:35 -0400123 * This function does the same as sysfs_create_link(), but it
Cornelia Huck36ce6da2008-06-10 11:09:08 +0200124 * doesn't warn if the link already exists.
125 */
126int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
127 const char *name)
128{
129 return sysfs_do_create_link(kobj, target, name, 0);
130}
131
132/**
Eric W. Biederman746edb72010-03-30 11:31:28 -0700133 * sysfs_delete_link - remove symlink in object's directory.
134 * @kobj: object we're acting for.
135 * @targ: object we're pointing to.
136 * @name: name of the symlink to remove.
137 *
138 * Unlike sysfs_remove_link sysfs_delete_link has enough information
139 * to successfully delete symlinks in tagged directories.
140 */
141void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
142 const char *name)
143{
144 const void *ns = NULL;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400145
146 /*
147 * We don't own @target and it may be removed at any time.
148 * Synchronize using sysfs_symlink_target_lock. See
149 * sysfs_remove_dir() for details.
150 */
151 spin_lock(&sysfs_symlink_target_lock);
Tejun Heocb26a312013-09-11 22:29:07 -0400152 if (targ->sd)
Eric W. Biederman746edb72010-03-30 11:31:28 -0700153 ns = targ->sd->s_ns;
Tejun Heo0cae60f2013-10-30 10:28:36 -0400154 spin_unlock(&sysfs_symlink_target_lock);
Tejun Heocfec0bc2013-09-11 22:29:09 -0400155 sysfs_hash_and_remove(kobj->sd, name, ns);
Eric W. Biederman746edb72010-03-30 11:31:28 -0700156}
157
158/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * sysfs_remove_link - remove symlink in object's directory.
160 * @kobj: object we're acting for.
161 * @name: name of the symlink to remove.
162 */
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700163void sysfs_remove_link(struct kobject *kobj, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Mark Fasheha839c5a2008-01-29 14:35:18 -0800165 struct sysfs_dirent *parent_sd = NULL;
166
167 if (!kobj)
168 parent_sd = &sysfs_root;
169 else
170 parent_sd = kobj->sd;
171
Tejun Heocfec0bc2013-09-11 22:29:09 -0400172 sysfs_hash_and_remove(parent_sd, name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700174EXPORT_SYMBOL_GPL(sysfs_remove_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800176/**
Tejun Heo4b30ee52013-09-11 22:29:06 -0400177 * sysfs_rename_link_ns - rename symlink in object's directory.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800178 * @kobj: object we're acting for.
179 * @targ: object we're pointing to.
180 * @old: previous name of the symlink.
181 * @new: new name of the symlink.
Tejun Heo4b30ee52013-09-11 22:29:06 -0400182 * @new_ns: new namespace of the symlink.
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800183 *
184 * A helper function for the common rename symlink idiom.
185 */
Tejun Heo4b30ee52013-09-11 22:29:06 -0400186int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
187 const char *old, const char *new, const void *new_ns)
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800188{
189 struct sysfs_dirent *parent_sd, *sd = NULL;
Tejun Heo4b30ee52013-09-11 22:29:06 -0400190 const void *old_ns = NULL;
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800191 int result;
192
193 if (!kobj)
194 parent_sd = &sysfs_root;
195 else
196 parent_sd = kobj->sd;
197
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700198 if (targ->sd)
199 old_ns = targ->sd->s_ns;
200
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800201 result = -ENOENT;
Tejun Heo388975c2013-09-11 23:19:13 -0400202 sd = sysfs_get_dirent_ns(parent_sd, old, old_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800203 if (!sd)
204 goto out;
205
206 result = -EINVAL;
207 if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
208 goto out;
209 if (sd->s_symlink.target_sd->s_dir.kobj != targ)
210 goto out;
211
Tejun Heocfec0bc2013-09-11 22:29:09 -0400212 result = sysfs_rename(sd, parent_sd, new, new_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800213
214out:
215 sysfs_put(sd);
216 return result;
217}
Tejun Heo4b30ee52013-09-11 22:29:06 -0400218EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
Eric W. Biederman7cb32942010-02-12 19:22:25 -0800219
Kay Sievers2f90a852007-11-01 20:20:52 +0100220static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
221 struct sysfs_dirent *target_sd, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Kay Sievers2f90a852007-11-01 20:20:52 +0100223 struct sysfs_dirent *base, *sd;
224 char *s = path;
225 int len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Kay Sievers2f90a852007-11-01 20:20:52 +0100227 /* go up to the root, stop at the base */
228 base = parent_sd;
229 while (base->s_parent) {
230 sd = target_sd->s_parent;
231 while (sd->s_parent && base != sd)
232 sd = sd->s_parent;
233
234 if (base == sd)
235 break;
236
237 strcpy(s, "../");
238 s += 3;
239 base = base->s_parent;
240 }
241
242 /* determine end of target string for reverse fillup */
243 sd = target_sd;
244 while (sd->s_parent && sd != base) {
245 len += strlen(sd->s_name) + 1;
246 sd = sd->s_parent;
247 }
248
249 /* check limits */
250 if (len < 2)
251 return -EINVAL;
252 len--;
253 if ((s - path) + len > PATH_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return -ENAMETOOLONG;
255
Kay Sievers2f90a852007-11-01 20:20:52 +0100256 /* reverse fillup of target string from target to base */
257 sd = target_sd;
258 while (sd->s_parent && sd != base) {
259 int slen = strlen(sd->s_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Kay Sievers2f90a852007-11-01 20:20:52 +0100261 len -= slen;
262 strncpy(s + len, sd->s_name, slen);
263 if (len)
264 s[--len] = '/';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Kay Sievers2f90a852007-11-01 20:20:52 +0100266 sd = sd->s_parent;
267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 return 0;
270}
271
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700272static int sysfs_getlink(struct dentry *dentry, char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Tejun Heo2b29ac22007-06-14 03:45:15 +0900274 struct sysfs_dirent *sd = dentry->d_fsdata;
275 struct sysfs_dirent *parent_sd = sd->s_parent;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900276 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
Tejun Heo2b29ac22007-06-14 03:45:15 +0900277 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Tejun Heo3007e992007-06-14 04:27:23 +0900279 mutex_lock(&sysfs_mutex);
Tejun Heo2b29ac22007-06-14 03:45:15 +0900280 error = sysfs_get_target_path(parent_sd, target_sd, path);
Tejun Heo3007e992007-06-14 04:27:23 +0900281 mutex_unlock(&sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Tejun Heo2b29ac22007-06-14 03:45:15 +0900283 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700286static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 int error = -ENOMEM;
289 unsigned long page = get_zeroed_page(GFP_KERNEL);
Armin Kuster557411e2009-04-29 07:29:59 -1000290 if (page) {
Greg Kroah-Hartmanab9bf4b2013-08-21 16:21:17 -0700291 error = sysfs_getlink(dentry, (char *) page);
Armin Kuster557411e2009-04-29 07:29:59 -1000292 if (error < 0)
293 free_page((unsigned long)page);
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
Linus Torvaldscc314ee2005-08-19 18:02:56 -0700296 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Greg Kroah-Hartmanddfd6d02013-08-21 16:33:34 -0700299static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
300 void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 char *page = nd_get_link(nd);
303 if (!IS_ERR(page))
304 free_page((unsigned long)page);
305}
306
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800307const struct inode_operations sysfs_symlink_inode_operations = {
Eric W. Biedermanc099aac2009-11-20 16:08:52 -0800308 .setxattr = sysfs_setxattr,
309 .readlink = generic_readlink,
310 .follow_link = sysfs_follow_link,
311 .put_link = sysfs_put_link,
Eric W. Biedermane61ab4a2009-11-20 16:08:53 -0800312 .setattr = sysfs_setattr,
313 .getattr = sysfs_getattr,
314 .permission = sysfs_permission,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315};