MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / sysfs / symlink.c
blob3169fe516e6f6c21b8dc61e6596ba1fbe3ad0f30
1 /*
2 * symlink.c - operations for sysfs symlinks.
3 */
5 #include <linux/fs.h>
6 #include <linux/module.h>
7 #include <linux/kobject.h>
8 #include <linux/namei.h>
10 #include "sysfs.h"
12 static struct inode_operations sysfs_symlink_inode_operations = {
13 .readlink = generic_readlink,
14 .follow_link = sysfs_follow_link,
15 .put_link = sysfs_put_link,
18 static int init_symlink(struct inode * inode)
20 inode->i_op = &sysfs_symlink_inode_operations;
21 return 0;
24 static int object_depth(struct kobject * kobj)
26 struct kobject * p = kobj;
27 int depth = 0;
28 do { depth++; } while ((p = p->parent));
29 return depth;
32 static int object_path_length(struct kobject * kobj)
34 struct kobject * p = kobj;
35 int length = 1;
36 do {
37 length += strlen(kobject_name(p)) + 1;
38 p = p->parent;
39 } while (p);
40 return length;
43 static void fill_object_path(struct kobject * kobj, char * buffer, int length)
45 struct kobject * p;
47 --length;
48 for (p = kobj; p; p = p->parent) {
49 int cur = strlen(kobject_name(p));
51 /* back up enough to print this bus id with '/' */
52 length -= cur;
53 strncpy(buffer + length,kobject_name(p),cur);
54 *(buffer + --length) = '/';
58 /**
59 * sysfs_create_link - create symlink between two objects.
60 * @kobj: object whose directory we're creating the link in.
61 * @target: object we're pointing to.
62 * @name: name of the symlink.
64 int sysfs_create_link(struct kobject * kobj, struct kobject * target, char * name)
66 struct dentry * dentry = kobj->dentry;
67 struct dentry * d;
68 int error = 0;
70 down(&dentry->d_inode->i_sem);
71 d = sysfs_get_dentry(dentry,name);
72 if (!IS_ERR(d)) {
73 error = sysfs_create(d, S_IFLNK|S_IRWXUGO, init_symlink);
74 if (!error)
75 /*
76 * associate the link dentry with the target kobject
78 d->d_fsdata = kobject_get(target);
79 dput(d);
80 } else
81 error = PTR_ERR(d);
82 up(&dentry->d_inode->i_sem);
83 return error;
87 /**
88 * sysfs_remove_link - remove symlink in object's directory.
89 * @kobj: object we're acting for.
90 * @name: name of the symlink to remove.
93 void sysfs_remove_link(struct kobject * kobj, char * name)
95 sysfs_hash_and_remove(kobj->dentry,name);
98 static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target,
99 char *path)
101 char * s;
102 int depth, size;
104 depth = object_depth(kobj);
105 size = object_path_length(target) + depth * 3 - 1;
106 if (size > PATH_MAX)
107 return -ENAMETOOLONG;
109 pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
111 for (s = path; depth--; s += 3)
112 strcpy(s,"../");
114 fill_object_path(target, path, size);
115 pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
117 return 0;
120 static int sysfs_getlink(struct dentry *dentry, char * path)
122 struct kobject *kobj, *target_kobj;
123 int error = 0;
125 kobj = sysfs_get_kobject(dentry->d_parent);
126 if (!kobj)
127 return -EINVAL;
129 target_kobj = sysfs_get_kobject(dentry);
130 if (!target_kobj) {
131 kobject_put(kobj);
132 return -EINVAL;
135 down_read(&sysfs_rename_sem);
136 error = sysfs_get_target_path(kobj, target_kobj, path);
137 up_read(&sysfs_rename_sem);
139 kobject_put(kobj);
140 kobject_put(target_kobj);
141 return error;
145 int sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
147 int error = -ENOMEM;
148 unsigned long page = get_zeroed_page(GFP_KERNEL);
149 if (page)
150 error = sysfs_getlink(dentry, (char *) page);
151 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
152 return 0;
155 void sysfs_put_link(struct dentry *dentry, struct nameidata *nd)
157 char *page = nd_get_link(nd);
158 if (!IS_ERR(page))
159 free_page((unsigned long)page);
162 EXPORT_SYMBOL(sysfs_create_link);
163 EXPORT_SYMBOL(sysfs_remove_link);