]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/sysfs/symlink.c
arm: imx6: defconfig: update tx6 defconfigs
[karo-tx-linux.git] / fs / sysfs / symlink.c
1 /*
2  * fs/sysfs/symlink.c - sysfs symlink implementation
3  *
4  * Copyright (c) 2001-3 Patrick Mochel
5  * Copyright (c) 2007 SUSE Linux Products GmbH
6  * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7  *
8  * This file is released under the GPLv2.
9  *
10  * Please see Documentation/filesystems/sysfs.txt for more information.
11  */
12
13 #include <linux/fs.h>
14 #include <linux/gfp.h>
15 #include <linux/mount.h>
16 #include <linux/module.h>
17 #include <linux/kobject.h>
18 #include <linux/namei.h>
19 #include <linux/mutex.h>
20 #include <linux/security.h>
21
22 #include "sysfs.h"
23
24 static int sysfs_do_create_link_sd(struct sysfs_dirent *parent_sd,
25                                    struct kobject *target,
26                                    const char *name, int warn)
27 {
28         struct sysfs_dirent *target_sd = NULL;
29         struct sysfs_dirent *sd = NULL;
30         struct sysfs_addrm_cxt acxt;
31         int error;
32
33         BUG_ON(!name || !parent_sd);
34
35         /* target->sd can go away beneath us but is protected with
36          * sysfs_assoc_lock.  Fetch target_sd from it.
37          */
38         spin_lock(&sysfs_assoc_lock);
39         if (target->sd)
40                 target_sd = sysfs_get(target->sd);
41         spin_unlock(&sysfs_assoc_lock);
42
43         error = -ENOENT;
44         if (!target_sd)
45                 goto out_put;
46
47         error = -ENOMEM;
48         sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
49         if (!sd)
50                 goto out_put;
51
52         sd->s_ns = target_sd->s_ns;
53         sd->s_symlink.target_sd = target_sd;
54         target_sd = NULL;       /* reference is now owned by the symlink */
55
56         sysfs_addrm_start(&acxt);
57         if (warn)
58                 error = sysfs_add_one(&acxt, sd, parent_sd);
59         else
60                 error = __sysfs_add_one(&acxt, sd, parent_sd);
61         sysfs_addrm_finish(&acxt);
62
63         if (error)
64                 goto out_put;
65
66         return 0;
67
68  out_put:
69         sysfs_put(target_sd);
70         sysfs_put(sd);
71         return error;
72 }
73
74 /**
75  *      sysfs_create_link_sd - create symlink to a given object.
76  *      @sd:            directory we're creating the link in.
77  *      @target:        object we're pointing to.
78  *      @name:          name of the symlink.
79  */
80 int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
81                          const char *name)
82 {
83         return sysfs_do_create_link_sd(sd, target, name, 1);
84 }
85
86 static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
87                                 const char *name, int warn)
88 {
89         struct sysfs_dirent *parent_sd = NULL;
90
91         if (!kobj)
92                 parent_sd = &sysfs_root;
93         else
94                 parent_sd = kobj->sd;
95
96         if (!parent_sd)
97                 return -EFAULT;
98
99         return sysfs_do_create_link_sd(parent_sd, target, name, warn);
100 }
101
102 /**
103  *      sysfs_create_link - create symlink between two objects.
104  *      @kobj:  object whose directory we're creating the link in.
105  *      @target:        object we're pointing to.
106  *      @name:          name of the symlink.
107  */
108 int sysfs_create_link(struct kobject *kobj, struct kobject *target,
109                       const char *name)
110 {
111         return sysfs_do_create_link(kobj, target, name, 1);
112 }
113 EXPORT_SYMBOL_GPL(sysfs_create_link);
114
115 /**
116  *      sysfs_create_link_nowarn - create symlink between two objects.
117  *      @kobj:  object whose directory we're creating the link in.
118  *      @target:        object we're pointing to.
119  *      @name:          name of the symlink.
120  *
121  *      This function does the same as sysfs_create_link(), but it
122  *      doesn't warn if the link already exists.
123  */
124 int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
125                              const char *name)
126 {
127         return sysfs_do_create_link(kobj, target, name, 0);
128 }
129
130 /**
131  *      sysfs_delete_link - remove symlink in object's directory.
132  *      @kobj:  object we're acting for.
133  *      @targ:  object we're pointing to.
134  *      @name:  name of the symlink to remove.
135  *
136  *      Unlike sysfs_remove_link sysfs_delete_link has enough information
137  *      to successfully delete symlinks in tagged directories.
138  */
139 void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
140                         const char *name)
141 {
142         const void *ns = NULL;
143         spin_lock(&sysfs_assoc_lock);
144         if (targ->sd)
145                 ns = targ->sd->s_ns;
146         spin_unlock(&sysfs_assoc_lock);
147         sysfs_hash_and_remove(kobj->sd, name, ns);
148 }
149
150 /**
151  *      sysfs_remove_link - remove symlink in object's directory.
152  *      @kobj:  object we're acting for.
153  *      @name:  name of the symlink to remove.
154  */
155 void sysfs_remove_link(struct kobject *kobj, const char *name)
156 {
157         struct sysfs_dirent *parent_sd = NULL;
158
159         if (!kobj)
160                 parent_sd = &sysfs_root;
161         else
162                 parent_sd = kobj->sd;
163
164         sysfs_hash_and_remove(parent_sd, name, NULL);
165 }
166 EXPORT_SYMBOL_GPL(sysfs_remove_link);
167
168 /**
169  *      sysfs_rename_link_ns - rename symlink in object's directory.
170  *      @kobj:  object we're acting for.
171  *      @targ:  object we're pointing to.
172  *      @old:   previous name of the symlink.
173  *      @new:   new name of the symlink.
174  *      @new_ns: new namespace of the symlink.
175  *
176  *      A helper function for the common rename symlink idiom.
177  */
178 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
179                          const char *old, const char *new, const void *new_ns)
180 {
181         struct sysfs_dirent *parent_sd, *sd = NULL;
182         const void *old_ns = NULL;
183         int result;
184
185         if (!kobj)
186                 parent_sd = &sysfs_root;
187         else
188                 parent_sd = kobj->sd;
189
190         if (targ->sd)
191                 old_ns = targ->sd->s_ns;
192
193         result = -ENOENT;
194         sd = sysfs_get_dirent_ns(parent_sd, old, old_ns);
195         if (!sd)
196                 goto out;
197
198         result = -EINVAL;
199         if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
200                 goto out;
201         if (sd->s_symlink.target_sd->s_dir.kobj != targ)
202                 goto out;
203
204         result = sysfs_rename(sd, parent_sd, new, new_ns);
205
206 out:
207         sysfs_put(sd);
208         return result;
209 }
210 EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
211
212 static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
213                                  struct sysfs_dirent *target_sd, char *path)
214 {
215         struct sysfs_dirent *base, *sd;
216         char *s = path;
217         int len = 0;
218
219         /* go up to the root, stop at the base */
220         base = parent_sd;
221         while (base->s_parent) {
222                 sd = target_sd->s_parent;
223                 while (sd->s_parent && base != sd)
224                         sd = sd->s_parent;
225
226                 if (base == sd)
227                         break;
228
229                 strcpy(s, "../");
230                 s += 3;
231                 base = base->s_parent;
232         }
233
234         /* determine end of target string for reverse fillup */
235         sd = target_sd;
236         while (sd->s_parent && sd != base) {
237                 len += strlen(sd->s_name) + 1;
238                 sd = sd->s_parent;
239         }
240
241         /* check limits */
242         if (len < 2)
243                 return -EINVAL;
244         len--;
245         if ((s - path) + len > PATH_MAX)
246                 return -ENAMETOOLONG;
247
248         /* reverse fillup of target string from target to base */
249         sd = target_sd;
250         while (sd->s_parent && sd != base) {
251                 int slen = strlen(sd->s_name);
252
253                 len -= slen;
254                 strncpy(s + len, sd->s_name, slen);
255                 if (len)
256                         s[--len] = '/';
257
258                 sd = sd->s_parent;
259         }
260
261         return 0;
262 }
263
264 static int sysfs_getlink(struct dentry *dentry, char *path)
265 {
266         struct sysfs_dirent *sd = dentry->d_fsdata;
267         struct sysfs_dirent *parent_sd = sd->s_parent;
268         struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
269         int error;
270
271         mutex_lock(&sysfs_mutex);
272         error = sysfs_get_target_path(parent_sd, target_sd, path);
273         mutex_unlock(&sysfs_mutex);
274
275         return error;
276 }
277
278 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
279 {
280         int error = -ENOMEM;
281         unsigned long page = get_zeroed_page(GFP_KERNEL);
282         if (page) {
283                 error = sysfs_getlink(dentry, (char *) page);
284                 if (error < 0)
285                         free_page((unsigned long)page);
286         }
287         nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
288         return NULL;
289 }
290
291 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
292                            void *cookie)
293 {
294         char *page = nd_get_link(nd);
295         if (!IS_ERR(page))
296                 free_page((unsigned long)page);
297 }
298
299 const struct inode_operations sysfs_symlink_inode_operations = {
300         .setxattr       = sysfs_setxattr,
301         .readlink       = generic_readlink,
302         .follow_link    = sysfs_follow_link,
303         .put_link       = sysfs_put_link,
304         .setattr        = sysfs_setattr,
305         .getattr        = sysfs_getattr,
306         .permission     = sysfs_permission,
307 };