vserver 1.9.3
[linux-2.6.git] / fs / sysfs / symlink.c
1 /*
2  * symlink.c - operations for sysfs symlinks.
3  */
4
5 #include <linux/fs.h>
6 #include <linux/module.h>
7 #include <linux/kobject.h>
8 #include <linux/namei.h>
9
10 #include "sysfs.h"
11
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,
16 };
17
18 static int init_symlink(struct inode * inode)
19 {
20         inode->i_op = &sysfs_symlink_inode_operations;
21         return 0;
22 }
23
24 static int object_depth(struct kobject * kobj)
25 {
26         struct kobject * p = kobj;
27         int depth = 0;
28         do { depth++; } while ((p = p->parent));
29         return depth;
30 }
31
32 static int object_path_length(struct kobject * kobj)
33 {
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;
41 }
42
43 static void fill_object_path(struct kobject * kobj, char * buffer, int length)
44 {
45         struct kobject * p;
46
47         --length;
48         for (p = kobj; p; p = p->parent) {
49                 int cur = strlen(kobject_name(p));
50
51                 /* back up enough to print this bus id with '/' */
52                 length -= cur;
53                 strncpy(buffer + length,kobject_name(p),cur);
54                 *(buffer + --length) = '/';
55         }
56 }
57
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.
63  */
64 int sysfs_create_link(struct kobject * kobj, struct kobject * target, char * name)
65 {
66         struct dentry * dentry = kobj->dentry;
67         struct dentry * d;
68         int error = 0;
69
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 
77                          */
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;
84 }
85
86
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.
91  */
92
93 void sysfs_remove_link(struct kobject * kobj, char * name)
94 {
95         sysfs_hash_and_remove(kobj->dentry,name);
96 }
97
98 static int sysfs_get_target_path(struct kobject * kobj, struct kobject * target,
99                                    char *path)
100 {
101         char * s;
102         int depth, size;
103
104         depth = object_depth(kobj);
105         size = object_path_length(target) + depth * 3 - 1;
106         if (size > PATH_MAX)
107                 return -ENAMETOOLONG;
108
109         pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
110
111         for (s = path; depth--; s += 3)
112                 strcpy(s,"../");
113
114         fill_object_path(target, path, size);
115         pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
116
117         return 0;
118 }
119
120 static int sysfs_getlink(struct dentry *dentry, char * path)
121 {
122         struct kobject *kobj, *target_kobj;
123         int error = 0;
124
125         kobj = sysfs_get_kobject(dentry->d_parent);
126         if (!kobj)
127                 return -EINVAL;
128
129         target_kobj = sysfs_get_kobject(dentry);
130         if (!target_kobj) {
131                 kobject_put(kobj);
132                 return -EINVAL;
133         }
134
135         down_read(&sysfs_rename_sem);
136         error = sysfs_get_target_path(kobj, target_kobj, path);
137         up_read(&sysfs_rename_sem);
138         
139         kobject_put(kobj);
140         kobject_put(target_kobj);
141         return error;
142
143 }
144
145 int sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
146 {
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;
153 }
154
155 void sysfs_put_link(struct dentry *dentry, struct nameidata *nd)
156 {
157         char *page = nd_get_link(nd);
158         if (!IS_ERR(page))
159                 free_page((unsigned long)page);
160 }
161
162 EXPORT_SYMBOL(sysfs_create_link);
163 EXPORT_SYMBOL(sysfs_remove_link);
164