2 * symlink.c - operations for sysfs symlinks.
6 #include <linux/module.h>
7 #include <linux/kobject.h>
11 static struct inode_operations sysfs_symlink_inode_operations = {
12 .readlink = sysfs_readlink,
13 .follow_link = sysfs_follow_link,
16 static int init_symlink(struct inode * inode)
18 inode->i_op = &sysfs_symlink_inode_operations;
22 static int object_depth(struct kobject * kobj)
24 struct kobject * p = kobj;
26 do { depth++; } while ((p = p->parent));
30 static int object_path_length(struct kobject * kobj)
32 struct kobject * p = kobj;
35 length += strlen(kobject_name(p)) + 1;
41 static void fill_object_path(struct kobject * kobj, char * buffer, int length)
46 for (p = kobj; p; p = p->parent) {
47 int cur = strlen(kobject_name(p));
49 /* back up enough to print this bus id with '/' */
51 strncpy(buffer + length,kobject_name(p),cur);
52 *(buffer + --length) = '/';
57 * sysfs_create_link - create symlink between two objects.
58 * @kobj: object whose directory we're creating the link in.
59 * @target: object we're pointing to.
60 * @name: name of the symlink.
62 int sysfs_create_link(struct kobject * kobj, struct kobject * target, char * name)
64 struct dentry * dentry = kobj->dentry;
68 down(&dentry->d_inode->i_sem);
69 d = sysfs_get_dentry(dentry,name);
71 error = sysfs_create(d, S_IFLNK|S_IRWXUGO, init_symlink);
74 * associate the link dentry with the target kobject
76 d->d_fsdata = kobject_get(target);
80 up(&dentry->d_inode->i_sem);
86 * sysfs_remove_link - remove symlink in object's directory.
87 * @kobj: object we're acting for.
88 * @name: name of the symlink to remove.
91 void sysfs_remove_link(struct kobject * kobj, char * name)
93 sysfs_hash_and_remove(kobj->dentry,name);
96 static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target,
102 depth = object_depth(kobj);
103 size = object_path_length(target) + depth * 3 - 1;
105 return -ENAMETOOLONG;
107 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
109 for (s = path; depth--; s += 3)
112 fill_object_path(target, path, size);
113 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
118 static int sysfs_getlink(struct dentry *dentry, char * path)
120 struct kobject *kobj, *target_kobj;
123 kobj = sysfs_get_kobject(dentry->d_parent);
127 target_kobj = sysfs_get_kobject(dentry);
133 down_read(&sysfs_rename_sem);
134 error = sysfs_get_target_path(kobj, target_kobj, path);
135 up_read(&sysfs_rename_sem);
138 kobject_put(target_kobj);
143 int sysfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
146 unsigned long page = get_zeroed_page(GFP_KERNEL);
151 error = sysfs_getlink(dentry, (char *) page);
153 error = vfs_readlink(dentry, buffer, buflen, (char *) page);
160 int sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
163 unsigned long page = get_zeroed_page(GFP_KERNEL);
168 error = sysfs_getlink(dentry, (char *) page);
170 error = vfs_follow_link(nd, (char *) page);
177 EXPORT_SYMBOL(sysfs_create_link);
178 EXPORT_SYMBOL(sysfs_remove_link);