sysfs: make s_elem an anonymous union
[safe/jmp/linux-2.6] / fs / sysfs / symlink.c
1 /*
2  * symlink.c - operations for sysfs symlinks.
3  */
4
5 #include <linux/fs.h>
6 #include <linux/mount.h>
7 #include <linux/module.h>
8 #include <linux/kobject.h>
9 #include <linux/namei.h>
10 #include <linux/mutex.h>
11
12 #include "sysfs.h"
13
14 static int object_depth(struct sysfs_dirent *sd)
15 {
16         int depth = 0;
17
18         for (; sd->s_parent; sd = sd->s_parent)
19                 depth++;
20
21         return depth;
22 }
23
24 static int object_path_length(struct sysfs_dirent * sd)
25 {
26         int length = 1;
27
28         for (; sd->s_parent; sd = sd->s_parent)
29                 length += strlen(sd->s_name) + 1;
30
31         return length;
32 }
33
34 static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
35 {
36         --length;
37         for (; sd->s_parent; sd = sd->s_parent) {
38                 int cur = strlen(sd->s_name);
39
40                 /* back up enough to print this bus id with '/' */
41                 length -= cur;
42                 strncpy(buffer + length, sd->s_name, cur);
43                 *(buffer + --length) = '/';
44         }
45 }
46
47 /**
48  *      sysfs_create_link - create symlink between two objects.
49  *      @kobj:  object whose directory we're creating the link in.
50  *      @target:        object we're pointing to.
51  *      @name:          name of the symlink.
52  */
53 int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
54 {
55         struct sysfs_dirent *parent_sd = NULL;
56         struct sysfs_dirent *target_sd = NULL;
57         struct sysfs_dirent *sd = NULL;
58         struct sysfs_addrm_cxt acxt;
59         int error;
60
61         BUG_ON(!name);
62
63         if (!kobj)
64                 parent_sd = &sysfs_root;
65         else
66                 parent_sd = kobj->sd;
67
68         error = -EFAULT;
69         if (!parent_sd)
70                 goto out_put;
71
72         /* target->sd can go away beneath us but is protected with
73          * sysfs_assoc_lock.  Fetch target_sd from it.
74          */
75         spin_lock(&sysfs_assoc_lock);
76         if (target->sd)
77                 target_sd = sysfs_get(target->sd);
78         spin_unlock(&sysfs_assoc_lock);
79
80         error = -ENOENT;
81         if (!target_sd)
82                 goto out_put;
83
84         error = -ENOMEM;
85         sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
86         if (!sd)
87                 goto out_put;
88
89         sd->s_symlink.target_sd = target_sd;
90         target_sd = NULL;       /* reference is now owned by the symlink */
91
92         sysfs_addrm_start(&acxt, parent_sd);
93         error = sysfs_add_one(&acxt, sd);
94         sysfs_addrm_finish(&acxt);
95
96         if (error)
97                 goto out_put;
98
99         return 0;
100
101  out_put:
102         sysfs_put(target_sd);
103         sysfs_put(sd);
104         return error;
105 }
106
107
108 /**
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.
112  */
113
114 void sysfs_remove_link(struct kobject * kobj, const char * name)
115 {
116         sysfs_hash_and_remove(kobj->sd, name);
117 }
118
119 static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
120                                  struct sysfs_dirent * target_sd, char *path)
121 {
122         char * s;
123         int depth, size;
124
125         depth = object_depth(parent_sd);
126         size = object_path_length(target_sd) + depth * 3 - 1;
127         if (size > PATH_MAX)
128                 return -ENAMETOOLONG;
129
130         pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
131
132         for (s = path; depth--; s += 3)
133                 strcpy(s,"../");
134
135         fill_object_path(target_sd, path, size);
136         pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
137
138         return 0;
139 }
140
141 static int sysfs_getlink(struct dentry *dentry, char * path)
142 {
143         struct sysfs_dirent *sd = dentry->d_fsdata;
144         struct sysfs_dirent *parent_sd = sd->s_parent;
145         struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
146         int error;
147
148         mutex_lock(&sysfs_mutex);
149         error = sysfs_get_target_path(parent_sd, target_sd, path);
150         mutex_unlock(&sysfs_mutex);
151
152         return error;
153 }
154
155 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
156 {
157         int error = -ENOMEM;
158         unsigned long page = get_zeroed_page(GFP_KERNEL);
159         if (page)
160                 error = sysfs_getlink(dentry, (char *) page); 
161         nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
162         return NULL;
163 }
164
165 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
166 {
167         char *page = nd_get_link(nd);
168         if (!IS_ERR(page))
169                 free_page((unsigned long)page);
170 }
171
172 const struct inode_operations sysfs_symlink_inode_operations = {
173         .readlink = generic_readlink,
174         .follow_link = sysfs_follow_link,
175         .put_link = sysfs_put_link,
176 };
177
178
179 EXPORT_SYMBOL_GPL(sysfs_create_link);
180 EXPORT_SYMBOL_GPL(sysfs_remove_link);