2 * symlink.c - operations for sysfs symlinks.
6 #include <linux/mount.h>
7 #include <linux/module.h>
8 #include <linux/kobject.h>
9 #include <linux/namei.h>
10 #include <asm/semaphore.h>
14 static int object_depth(struct sysfs_dirent *sd)
18 for (; sd->s_parent; sd = sd->s_parent)
24 static int object_path_length(struct sysfs_dirent * sd)
28 for (; sd->s_parent; sd = sd->s_parent)
29 length += strlen(sd->s_name) + 1;
34 static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
37 for (; sd->s_parent; sd = sd->s_parent) {
38 int cur = strlen(sd->s_name);
40 /* back up enough to print this bus id with '/' */
42 strncpy(buffer + length, sd->s_name, cur);
43 *(buffer + --length) = '/';
47 static int sysfs_add_link(struct sysfs_dirent * parent_sd, const char * name,
48 struct sysfs_dirent * target_sd)
50 struct sysfs_dirent * sd;
52 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
56 sd->s_elem.symlink.target_sd = target_sd;
57 sysfs_attach_dirent(sd, parent_sd, NULL);
62 * sysfs_create_link - create symlink between two objects.
63 * @kobj: object whose directory we're creating the link in.
64 * @target: object we're pointing to.
65 * @name: name of the symlink.
67 int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
69 struct sysfs_dirent *parent_sd = NULL;
70 struct sysfs_dirent *target_sd = NULL;
76 if (sysfs_mount && sysfs_mount->mnt_sb)
77 parent_sd = sysfs_mount->mnt_sb->s_root->d_fsdata;
84 /* target->sd can go away beneath us but is protected with
85 * sysfs_assoc_lock. Fetch target_sd from it.
87 spin_lock(&sysfs_assoc_lock);
89 target_sd = sysfs_get(target->sd);
90 spin_unlock(&sysfs_assoc_lock);
95 mutex_lock(&parent_sd->s_dentry->d_inode->i_mutex);
96 if (!sysfs_find_dirent(parent_sd, name))
97 error = sysfs_add_link(parent_sd, name, target_sd);
98 mutex_unlock(&parent_sd->s_dentry->d_inode->i_mutex);
101 sysfs_put(target_sd);
108 * sysfs_remove_link - remove symlink in object's directory.
109 * @kobj: object we're acting for.
110 * @name: name of the symlink to remove.
113 void sysfs_remove_link(struct kobject * kobj, const char * name)
115 sysfs_hash_and_remove(kobj->sd, name);
118 static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
119 struct sysfs_dirent * target_sd, char *path)
124 depth = object_depth(parent_sd);
125 size = object_path_length(target_sd) + depth * 3 - 1;
127 return -ENAMETOOLONG;
129 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
131 for (s = path; depth--; s += 3)
134 fill_object_path(target_sd, path, size);
135 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
140 static int sysfs_getlink(struct dentry *dentry, char * path)
142 struct sysfs_dirent *sd = dentry->d_fsdata;
143 struct sysfs_dirent *parent_sd = sd->s_parent;
144 struct sysfs_dirent *target_sd = sd->s_elem.symlink.target_sd;
147 down_read(&sysfs_rename_sem);
148 error = sysfs_get_target_path(parent_sd, target_sd, path);
149 up_read(&sysfs_rename_sem);
154 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
157 unsigned long page = get_zeroed_page(GFP_KERNEL);
159 error = sysfs_getlink(dentry, (char *) page);
160 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
164 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
166 char *page = nd_get_link(nd);
168 free_page((unsigned long)page);
171 const struct inode_operations sysfs_symlink_inode_operations = {
172 .readlink = generic_readlink,
173 .follow_link = sysfs_follow_link,
174 .put_link = sysfs_put_link,
178 EXPORT_SYMBOL_GPL(sysfs_create_link);
179 EXPORT_SYMBOL_GPL(sysfs_remove_link);