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 kobject * kobj)
16 struct kobject * p = kobj;
18 do { depth++; } while ((p = p->parent));
22 static int object_path_length(struct kobject * kobj)
24 struct kobject * p = kobj;
27 length += strlen(kobject_name(p)) + 1;
33 static void fill_object_path(struct kobject * kobj, char * buffer, int length)
38 for (p = kobj; p; p = p->parent) {
39 int cur = strlen(kobject_name(p));
41 /* back up enough to print this bus id with '/' */
43 strncpy(buffer + length,kobject_name(p),cur);
44 *(buffer + --length) = '/';
48 static int sysfs_add_link(struct dentry * parent, const char * name, struct kobject * target)
50 struct sysfs_dirent * parent_sd = parent->d_fsdata;
51 struct sysfs_symlink * sl;
52 struct sysfs_dirent * sd;
56 sl = kzalloc(sizeof(*sl), GFP_KERNEL);
60 sl->link_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
64 strcpy(sl->link_name, name);
65 sl->target_kobj = kobject_get(target);
67 sd = sysfs_new_dirent(sl, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
70 sysfs_attach_dirent(sd, parent_sd, NULL);
76 kobject_put(sl->target_kobj);
84 * sysfs_create_link - create symlink between two objects.
85 * @kobj: object whose directory we're creating the link in.
86 * @target: object we're pointing to.
87 * @name: name of the symlink.
89 int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
91 struct dentry *dentry = NULL;
97 if (sysfs_mount && sysfs_mount->mnt_sb)
98 dentry = sysfs_mount->mnt_sb->s_root;
100 dentry = kobj->dentry;
105 mutex_lock(&dentry->d_inode->i_mutex);
106 if (!sysfs_dirent_exist(dentry->d_fsdata, name))
107 error = sysfs_add_link(dentry, name, target);
108 mutex_unlock(&dentry->d_inode->i_mutex);
114 * sysfs_remove_link - remove symlink in object's directory.
115 * @kobj: object we're acting for.
116 * @name: name of the symlink to remove.
119 void sysfs_remove_link(struct kobject * kobj, const char * name)
121 sysfs_hash_and_remove(kobj->dentry,name);
124 static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target,
130 depth = object_depth(kobj);
131 size = object_path_length(target) + depth * 3 - 1;
133 return -ENAMETOOLONG;
135 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
137 for (s = path; depth--; s += 3)
140 fill_object_path(target, path, size);
141 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
146 static int sysfs_getlink(struct dentry *dentry, char * path)
148 struct kobject *kobj, *target_kobj;
151 kobj = sysfs_get_kobject(dentry->d_parent);
155 target_kobj = sysfs_get_kobject(dentry);
161 down_read(&sysfs_rename_sem);
162 error = sysfs_get_target_path(kobj, target_kobj, path);
163 up_read(&sysfs_rename_sem);
166 kobject_put(target_kobj);
171 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
174 unsigned long page = get_zeroed_page(GFP_KERNEL);
176 error = sysfs_getlink(dentry, (char *) page);
177 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
181 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
183 char *page = nd_get_link(nd);
185 free_page((unsigned long)page);
188 const struct inode_operations sysfs_symlink_inode_operations = {
189 .readlink = generic_readlink,
190 .follow_link = sysfs_follow_link,
191 .put_link = sysfs_put_link,
195 EXPORT_SYMBOL_GPL(sysfs_create_link);
196 EXPORT_SYMBOL_GPL(sysfs_remove_link);