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>
25 * kernfs_create_link - create a symlink
26 * @parent: directory to create the symlink in
27 * @name: name of the symlink
28 * @target: target node for the symlink to point to
30 * Returns the created node on success, ERR_PTR() value on error.
32 struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
34 struct sysfs_dirent *target)
36 struct sysfs_dirent *sd;
37 struct sysfs_addrm_cxt acxt;
40 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
42 return ERR_PTR(-ENOMEM);
44 if (parent->s_flags & SYSFS_FLAG_NS)
45 sd->s_ns = target->s_ns;
46 sd->s_symlink.target_sd = target;
47 sysfs_get(target); /* ref owned by symlink */
49 sysfs_addrm_start(&acxt);
50 error = sysfs_add_one(&acxt, sd, parent);
51 sysfs_addrm_finish(&acxt);
57 return ERR_PTR(error);
61 static int sysfs_do_create_link_sd(struct sysfs_dirent *parent_sd,
62 struct kobject *target,
63 const char *name, int warn)
65 struct sysfs_dirent *sd, *target_sd = NULL;
67 BUG_ON(!name || !parent_sd);
70 * We don't own @target and it may be removed at any time.
71 * Synchronize using sysfs_symlink_target_lock. See
72 * sysfs_remove_dir() for details.
74 spin_lock(&sysfs_symlink_target_lock);
76 target_sd = sysfs_get(target->sd);
77 spin_unlock(&sysfs_symlink_target_lock);
82 sd = kernfs_create_link(parent_sd, name, target_sd);
88 if (warn && PTR_ERR(sd) == -EEXIST)
89 sysfs_warn_dup(parent_sd, name);
94 * sysfs_create_link_sd - create symlink to a given object.
95 * @sd: directory we're creating the link in.
96 * @target: object we're pointing to.
97 * @name: name of the symlink.
99 int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
102 return sysfs_do_create_link_sd(sd, target, name, 1);
105 static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
106 const char *name, int warn)
108 struct sysfs_dirent *parent_sd = NULL;
111 parent_sd = &sysfs_root;
113 parent_sd = kobj->sd;
118 return sysfs_do_create_link_sd(parent_sd, target, name, warn);
122 * sysfs_create_link - create symlink between two objects.
123 * @kobj: object whose directory we're creating the link in.
124 * @target: object we're pointing to.
125 * @name: name of the symlink.
127 int sysfs_create_link(struct kobject *kobj, struct kobject *target,
130 return sysfs_do_create_link(kobj, target, name, 1);
132 EXPORT_SYMBOL_GPL(sysfs_create_link);
135 * sysfs_create_link_nowarn - create symlink between two objects.
136 * @kobj: object whose directory we're creating the link in.
137 * @target: object we're pointing to.
138 * @name: name of the symlink.
140 * This function does the same as sysfs_create_link(), but it
141 * doesn't warn if the link already exists.
143 int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
146 return sysfs_do_create_link(kobj, target, name, 0);
150 * sysfs_delete_link - remove symlink in object's directory.
151 * @kobj: object we're acting for.
152 * @targ: object we're pointing to.
153 * @name: name of the symlink to remove.
155 * Unlike sysfs_remove_link sysfs_delete_link has enough information
156 * to successfully delete symlinks in tagged directories.
158 void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
161 const void *ns = NULL;
164 * We don't own @target and it may be removed at any time.
165 * Synchronize using sysfs_symlink_target_lock. See
166 * sysfs_remove_dir() for details.
168 spin_lock(&sysfs_symlink_target_lock);
169 if (targ->sd && (kobj->sd->s_flags & SYSFS_FLAG_NS))
171 spin_unlock(&sysfs_symlink_target_lock);
172 kernfs_remove_by_name_ns(kobj->sd, name, ns);
176 * sysfs_remove_link - remove symlink in object's directory.
177 * @kobj: object we're acting for.
178 * @name: name of the symlink to remove.
180 void sysfs_remove_link(struct kobject *kobj, const char *name)
182 struct sysfs_dirent *parent_sd = NULL;
185 parent_sd = &sysfs_root;
187 parent_sd = kobj->sd;
189 kernfs_remove_by_name(parent_sd, name);
191 EXPORT_SYMBOL_GPL(sysfs_remove_link);
194 * sysfs_rename_link_ns - rename symlink in object's directory.
195 * @kobj: object we're acting for.
196 * @targ: object we're pointing to.
197 * @old: previous name of the symlink.
198 * @new: new name of the symlink.
199 * @new_ns: new namespace of the symlink.
201 * A helper function for the common rename symlink idiom.
203 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
204 const char *old, const char *new, const void *new_ns)
206 struct sysfs_dirent *parent_sd, *sd = NULL;
207 const void *old_ns = NULL;
211 parent_sd = &sysfs_root;
213 parent_sd = kobj->sd;
216 old_ns = targ->sd->s_ns;
219 sd = sysfs_get_dirent_ns(parent_sd, old, old_ns);
224 if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
226 if (sd->s_symlink.target_sd->priv != targ)
229 result = kernfs_rename_ns(sd, parent_sd, new, new_ns);
235 EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
237 static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
238 struct sysfs_dirent *target_sd, char *path)
240 struct sysfs_dirent *base, *sd;
244 /* go up to the root, stop at the base */
246 while (base->s_parent) {
247 sd = target_sd->s_parent;
248 while (sd->s_parent && base != sd)
256 base = base->s_parent;
259 /* determine end of target string for reverse fillup */
261 while (sd->s_parent && sd != base) {
262 len += strlen(sd->s_name) + 1;
270 if ((s - path) + len > PATH_MAX)
271 return -ENAMETOOLONG;
273 /* reverse fillup of target string from target to base */
275 while (sd->s_parent && sd != base) {
276 int slen = strlen(sd->s_name);
279 strncpy(s + len, sd->s_name, slen);
289 static int sysfs_getlink(struct dentry *dentry, char *path)
291 struct sysfs_dirent *sd = dentry->d_fsdata;
292 struct sysfs_dirent *parent_sd = sd->s_parent;
293 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
296 mutex_lock(&sysfs_mutex);
297 error = sysfs_get_target_path(parent_sd, target_sd, path);
298 mutex_unlock(&sysfs_mutex);
303 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
306 unsigned long page = get_zeroed_page(GFP_KERNEL);
308 error = sysfs_getlink(dentry, (char *) page);
310 free_page((unsigned long)page);
312 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
316 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
319 char *page = nd_get_link(nd);
321 free_page((unsigned long)page);
324 const struct inode_operations sysfs_symlink_inode_operations = {
325 .setxattr = sysfs_setxattr,
326 .readlink = generic_readlink,
327 .follow_link = sysfs_follow_link,
328 .put_link = sysfs_put_link,
329 .setattr = sysfs_setattr,
330 .getattr = sysfs_getattr,
331 .permission = sysfs_permission,