2 * symlink.c - operations for sysfs symlinks.
6 #include <linux/module.h>
7 #include <linux/kobject.h>
12 static int init_symlink(struct inode * inode)
14 inode->i_op = &page_symlink_inode_operations;
18 static int sysfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
22 error = sysfs_create(dentry, S_IFLNK|S_IRWXUGO, init_symlink);
24 int l = strlen(symname)+1;
25 error = page_symlink(dentry->d_inode, symname, l);
27 iput(dentry->d_inode);
32 static int object_depth(struct kobject * kobj)
34 struct kobject * p = kobj;
36 do { depth++; } while ((p = p->parent));
40 static int object_path_length(struct kobject * kobj)
42 struct kobject * p = kobj;
45 length += strlen(kobject_name(p)) + 1;
51 static void fill_object_path(struct kobject * kobj, char * buffer, int length)
56 for (p = kobj; p; p = p->parent) {
57 int cur = strlen(kobject_name(p));
59 /* back up enough to print this bus id with '/' */
61 strncpy(buffer + length,kobject_name(p),cur);
62 *(buffer + --length) = '/';
67 * sysfs_create_link - create symlink between two objects.
68 * @kobj: object whose directory we're creating the link in.
69 * @target: object we're pointing to.
70 * @name: name of the symlink.
72 int sysfs_create_link(struct kobject * kobj, struct kobject * target, char * name)
74 struct dentry * dentry = kobj->dentry;
82 depth = object_depth(kobj);
83 size = object_path_length(target) + depth * 3 - 1;
86 pr_debug("%s: depth = %d, size = %d\n",__FUNCTION__,depth,size);
88 path = kmalloc(size,GFP_KERNEL);
93 for (s = path; depth--; s += 3)
96 fill_object_path(target,path,size);
97 pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
99 down(&dentry->d_inode->i_sem);
100 d = sysfs_get_dentry(dentry,name);
102 error = sysfs_symlink(dentry->d_inode,d,path);
106 up(&dentry->d_inode->i_sem);
113 * sysfs_remove_link - remove symlink in object's directory.
114 * @kobj: object we're acting for.
115 * @name: name of the symlink to remove.
118 void sysfs_remove_link(struct kobject * kobj, char * name)
120 sysfs_hash_and_remove(kobj->dentry,name);
124 EXPORT_SYMBOL(sysfs_create_link);
125 EXPORT_SYMBOL(sysfs_remove_link);