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;
31 enum kobj_ns_type ns_type;
34 BUG_ON(!name || !parent_sd);
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.
41 spin_lock(&sysfs_symlink_target_lock);
43 target_sd = sysfs_get(target->sd);
44 spin_unlock(&sysfs_symlink_target_lock);
51 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
55 ns_type = sysfs_ns_type(parent_sd);
57 sd->s_ns = target_sd->s_ns;
58 sd->s_symlink.target_sd = target_sd;
59 target_sd = NULL; /* reference is now owned by the symlink */
61 sysfs_addrm_start(&acxt);
62 /* Symlinks must be between directories with the same ns_type */
64 (ns_type == sysfs_ns_type(sd->s_symlink.target_sd->s_parent))) {
66 error = sysfs_add_one(&acxt, sd, parent_sd);
68 error = __sysfs_add_one(&acxt, sd, parent_sd);
72 "sysfs: symlink across ns_types %s/%s -> %s/%s\n",
75 sd->s_symlink.target_sd->s_parent->s_name,
76 sd->s_symlink.target_sd->s_name);
78 sysfs_addrm_finish(&acxt);
92 * 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.
97 int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
100 return sysfs_do_create_link_sd(sd, target, name, 1);
103 static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
104 const char *name, int warn)
106 struct sysfs_dirent *parent_sd = NULL;
109 parent_sd = &sysfs_root;
111 parent_sd = kobj->sd;
116 return sysfs_do_create_link_sd(parent_sd, target, name, warn);
120 * 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.
125 int sysfs_create_link(struct kobject *kobj, struct kobject *target,
128 return sysfs_do_create_link(kobj, target, name, 1);
130 EXPORT_SYMBOL_GPL(sysfs_create_link);
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.
138 * This function does the same as sysfs_create_link(), but it
139 * doesn't warn if the link already exists.
141 int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
144 return sysfs_do_create_link(kobj, target, name, 0);
148 * 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.
153 * Unlike sysfs_remove_link sysfs_delete_link has enough information
154 * to successfully delete symlinks in tagged directories.
156 void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
159 const void *ns = NULL;
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.
166 spin_lock(&sysfs_symlink_target_lock);
167 if (targ->sd && sysfs_ns_type(kobj->sd))
169 spin_unlock(&sysfs_symlink_target_lock);
170 sysfs_hash_and_remove(kobj->sd, name, ns);
174 * sysfs_remove_link - remove symlink in object's directory.
175 * @kobj: object we're acting for.
176 * @name: name of the symlink to remove.
178 void sysfs_remove_link(struct kobject *kobj, const char *name)
180 struct sysfs_dirent *parent_sd = NULL;
183 parent_sd = &sysfs_root;
185 parent_sd = kobj->sd;
187 sysfs_hash_and_remove(parent_sd, name, NULL);
189 EXPORT_SYMBOL_GPL(sysfs_remove_link);
192 * sysfs_rename_link_ns - rename symlink in object's directory.
193 * @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.
197 * @new_ns: new namespace of the symlink.
199 * A helper function for the common rename symlink idiom.
201 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
202 const char *old, const char *new, const void *new_ns)
204 struct sysfs_dirent *parent_sd, *sd = NULL;
205 const void *old_ns = NULL;
209 parent_sd = &sysfs_root;
211 parent_sd = kobj->sd;
214 old_ns = targ->sd->s_ns;
217 sd = sysfs_get_dirent_ns(parent_sd, old, old_ns);
222 if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
224 if (sd->s_symlink.target_sd->s_dir.kobj != targ)
227 result = sysfs_rename(sd, parent_sd, new, new_ns);
233 EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
235 static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
236 struct sysfs_dirent *target_sd, char *path)
238 struct sysfs_dirent *base, *sd;
242 /* go up to the root, stop at the base */
244 while (base->s_parent) {
245 sd = target_sd->s_parent;
246 while (sd->s_parent && base != sd)
254 base = base->s_parent;
257 /* determine end of target string for reverse fillup */
259 while (sd->s_parent && sd != base) {
260 len += strlen(sd->s_name) + 1;
268 if ((s - path) + len > PATH_MAX)
269 return -ENAMETOOLONG;
271 /* reverse fillup of target string from target to base */
273 while (sd->s_parent && sd != base) {
274 int slen = strlen(sd->s_name);
277 strncpy(s + len, sd->s_name, slen);
287 static int sysfs_getlink(struct dentry *dentry, char *path)
289 struct sysfs_dirent *sd = dentry->d_fsdata;
290 struct sysfs_dirent *parent_sd = sd->s_parent;
291 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
294 mutex_lock(&sysfs_mutex);
295 error = sysfs_get_target_path(parent_sd, target_sd, path);
296 mutex_unlock(&sysfs_mutex);
301 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
304 unsigned long page = get_zeroed_page(GFP_KERNEL);
306 error = sysfs_getlink(dentry, (char *) page);
308 free_page((unsigned long)page);
310 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
314 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
317 char *page = nd_get_link(nd);
319 free_page((unsigned long)page);
322 const struct inode_operations sysfs_symlink_inode_operations = {
323 .setxattr = sysfs_setxattr,
324 .readlink = generic_readlink,
325 .follow_link = sysfs_follow_link,
326 .put_link = sysfs_put_link,
327 .setattr = sysfs_setattr,
328 .getattr = sysfs_getattr,
329 .permission = sysfs_permission,