2 * fs/sysfs/symlink.c - sysfs symlink implementation
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 * This file is released under the GPLv2.
10 * Please see Documentation/filesystems/sysfs.txt for more information.
14 #include <linux/gfp.h>
15 #include <linux/mount.h>
16 #include <linux/module.h>
17 #include <linux/kobject.h>
18 #include <linux/namei.h>
19 #include <linux/mutex.h>
20 #include <linux/security.h>
24 static int sysfs_do_create_link_sd(struct sysfs_dirent *parent_sd,
25 struct kobject *target,
26 const char *name, int warn)
28 struct sysfs_dirent *target_sd = NULL;
29 struct sysfs_dirent *sd = NULL;
30 struct sysfs_addrm_cxt acxt;
33 BUG_ON(!name || !parent_sd);
35 /* target->sd can go away beneath us but is protected with
36 * sysfs_assoc_lock. Fetch target_sd from it.
38 spin_lock(&sysfs_assoc_lock);
40 target_sd = sysfs_get(target->sd);
41 spin_unlock(&sysfs_assoc_lock);
48 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
52 sd->s_ns = target_sd->s_ns;
53 sd->s_symlink.target_sd = target_sd;
54 target_sd = NULL; /* reference is now owned by the symlink */
56 sysfs_addrm_start(&acxt);
58 error = sysfs_add_one(&acxt, sd, parent_sd);
60 error = __sysfs_add_one(&acxt, sd, parent_sd);
61 sysfs_addrm_finish(&acxt);
75 * 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.
80 int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
83 return sysfs_do_create_link_sd(sd, target, name, 1);
86 static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
87 const char *name, int warn)
89 struct sysfs_dirent *parent_sd = NULL;
92 parent_sd = &sysfs_root;
99 return sysfs_do_create_link_sd(parent_sd, target, name, warn);
103 * 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.
108 int sysfs_create_link(struct kobject *kobj, struct kobject *target,
111 return sysfs_do_create_link(kobj, target, name, 1);
113 EXPORT_SYMBOL_GPL(sysfs_create_link);
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.
121 * This function does the same as sysfs_create_link(), but it
122 * doesn't warn if the link already exists.
124 int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
127 return sysfs_do_create_link(kobj, target, name, 0);
131 * 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.
136 * Unlike sysfs_remove_link sysfs_delete_link has enough information
137 * to successfully delete symlinks in tagged directories.
139 void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
142 const void *ns = NULL;
143 spin_lock(&sysfs_assoc_lock);
146 spin_unlock(&sysfs_assoc_lock);
147 sysfs_hash_and_remove(kobj->sd, name, ns);
151 * sysfs_remove_link - remove symlink in object's directory.
152 * @kobj: object we're acting for.
153 * @name: name of the symlink to remove.
155 void sysfs_remove_link(struct kobject *kobj, const char *name)
157 struct sysfs_dirent *parent_sd = NULL;
160 parent_sd = &sysfs_root;
162 parent_sd = kobj->sd;
164 sysfs_hash_and_remove(parent_sd, name, NULL);
166 EXPORT_SYMBOL_GPL(sysfs_remove_link);
169 * sysfs_rename_link_ns - rename symlink in object's directory.
170 * @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.
174 * @new_ns: new namespace of the symlink.
176 * A helper function for the common rename symlink idiom.
178 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
179 const char *old, const char *new, const void *new_ns)
181 struct sysfs_dirent *parent_sd, *sd = NULL;
182 const void *old_ns = NULL;
186 parent_sd = &sysfs_root;
188 parent_sd = kobj->sd;
191 old_ns = targ->sd->s_ns;
194 sd = sysfs_get_dirent_ns(parent_sd, old, old_ns);
199 if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
201 if (sd->s_symlink.target_sd->s_dir.kobj != targ)
204 result = sysfs_rename(sd, parent_sd, new, new_ns);
210 EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
212 static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
213 struct sysfs_dirent *target_sd, char *path)
215 struct sysfs_dirent *base, *sd;
219 /* go up to the root, stop at the base */
221 while (base->s_parent) {
222 sd = target_sd->s_parent;
223 while (sd->s_parent && base != sd)
231 base = base->s_parent;
234 /* determine end of target string for reverse fillup */
236 while (sd->s_parent && sd != base) {
237 len += strlen(sd->s_name) + 1;
245 if ((s - path) + len > PATH_MAX)
246 return -ENAMETOOLONG;
248 /* reverse fillup of target string from target to base */
250 while (sd->s_parent && sd != base) {
251 int slen = strlen(sd->s_name);
254 strncpy(s + len, sd->s_name, slen);
264 static int sysfs_getlink(struct dentry *dentry, char *path)
266 struct sysfs_dirent *sd = dentry->d_fsdata;
267 struct sysfs_dirent *parent_sd = sd->s_parent;
268 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
271 mutex_lock(&sysfs_mutex);
272 error = sysfs_get_target_path(parent_sd, target_sd, path);
273 mutex_unlock(&sysfs_mutex);
278 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
281 unsigned long page = get_zeroed_page(GFP_KERNEL);
283 error = sysfs_getlink(dentry, (char *) page);
285 free_page((unsigned long)page);
287 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
291 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
294 char *page = nd_get_link(nd);
296 free_page((unsigned long)page);
299 const struct inode_operations sysfs_symlink_inode_operations = {
300 .setxattr = sysfs_setxattr,
301 .readlink = generic_readlink,
302 .follow_link = sysfs_follow_link,
303 .put_link = sysfs_put_link,
304 .setattr = sysfs_setattr,
305 .getattr = sysfs_getattr,
306 .permission = sysfs_permission,