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>
13 static int object_depth(struct kobject * kobj)
15 struct kobject * p = kobj;
17 do { depth++; } while ((p = p->parent));
21 static int object_path_length(struct kobject * kobj)
23 struct kobject * p = kobj;
26 length += strlen(kobject_name(p)) + 1;
32 static void fill_object_path(struct kobject * kobj, char * buffer, int length)
37 for (p = kobj; p; p = p->parent) {
38 int cur = strlen(kobject_name(p));
40 /* back up enough to print this bus id with '/' */
42 strncpy(buffer + length,kobject_name(p),cur);
43 *(buffer + --length) = '/';
47 static int sysfs_add_link(struct dentry * parent, const char * name, struct kobject * target)
49 struct sysfs_dirent * parent_sd = parent->d_fsdata;
50 struct sysfs_symlink * sl;
54 sl = kmalloc(sizeof(*sl), GFP_KERNEL);
58 sl->link_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
62 strcpy(sl->link_name, name);
63 sl->target_kobj = kobject_get(target);
65 error = sysfs_make_dirent(parent_sd, NULL, sl, S_IFLNK|S_IRWXUGO,
79 * sysfs_create_link - create symlink between two objects.
80 * @kobj: object whose directory we're creating the link in.
81 * @target: object we're pointing to.
82 * @name: name of the symlink.
84 int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
86 struct dentry *dentry = NULL;
92 if (sysfs_mount && sysfs_mount->mnt_sb)
93 dentry = sysfs_mount->mnt_sb->s_root;
95 dentry = kobj->dentry;
100 mutex_lock(&dentry->d_inode->i_mutex);
101 if (!sysfs_dirent_exist(dentry->d_fsdata, name))
102 error = sysfs_add_link(dentry, name, target);
103 mutex_unlock(&dentry->d_inode->i_mutex);
109 * sysfs_remove_link - remove symlink in object's directory.
110 * @kobj: object we're acting for.
111 * @name: name of the symlink to remove.
114 void sysfs_remove_link(struct kobject * kobj, const char * name)
116 sysfs_hash_and_remove(kobj->dentry,name);
119 static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target,
125 depth = object_depth(kobj);
126 size = object_path_length(target) + depth * 3 - 1;
128 return -ENAMETOOLONG;
130 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
132 for (s = path; depth--; s += 3)
135 fill_object_path(target, path, size);
136 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
141 static int sysfs_getlink(struct dentry *dentry, char * path)
143 struct kobject *kobj, *target_kobj;
146 kobj = sysfs_get_kobject(dentry->d_parent);
150 target_kobj = sysfs_get_kobject(dentry);
156 down_read(&sysfs_rename_sem);
157 error = sysfs_get_target_path(kobj, target_kobj, path);
158 up_read(&sysfs_rename_sem);
161 kobject_put(target_kobj);
166 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
169 unsigned long page = get_zeroed_page(GFP_KERNEL);
171 error = sysfs_getlink(dentry, (char *) page);
172 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
176 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
178 char *page = nd_get_link(nd);
180 free_page((unsigned long)page);
183 struct inode_operations sysfs_symlink_inode_operations = {
184 .readlink = generic_readlink,
185 .follow_link = sysfs_follow_link,
186 .put_link = sysfs_put_link,
190 EXPORT_SYMBOL_GPL(sysfs_create_link);
191 EXPORT_SYMBOL_GPL(sysfs_remove_link);