]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/sysfs/symlink.c
352fbbbc0551f2dbd1c9dba40945179176946ce0
[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 /**
25  * kernfs_create_link - create a symlink
26  * @parent: directory to create the symlink in
27  * @name: name of the symlink
28  * @target: target node for the symlink to point to
29  *
30  * Returns the created node on success, ERR_PTR() value on error.
31  */
32 struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
33                                         const char *name,
34                                         struct sysfs_dirent *target)
35 {
36         struct sysfs_dirent *sd;
37         struct sysfs_addrm_cxt acxt;
38         int error;
39
40         sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
41         if (!sd)
42                 return ERR_PTR(-ENOMEM);
43
44         if (parent->s_flags & SYSFS_FLAG_NS)
45                 sd->s_ns = target->s_ns;
46         sd->s_symlink.target_sd = target;
47         sysfs_get(target);      /* ref owned by symlink */
48
49         sysfs_addrm_start(&acxt);
50         error = __sysfs_add_one(&acxt, sd, parent);
51         sysfs_addrm_finish(&acxt);
52
53         if (!error)
54                 return sd;
55
56         sysfs_put(sd);
57         return ERR_PTR(error);
58 }
59
60
61 static int sysfs_do_create_link_sd(struct sysfs_dirent *parent_sd,
62                                    struct kobject *target,
63                                    const char *name, int warn)
64 {
65         struct sysfs_dirent *sd, *target_sd = NULL;
66
67         BUG_ON(!name || !parent_sd);
68
69         /*
70          * We don't own @target and it may be removed at any time.
71          * Synchronize using sysfs_symlink_target_lock.  See
72          * sysfs_remove_dir() for details.
73          */
74         spin_lock(&sysfs_symlink_target_lock);
75         if (target->sd)
76                 target_sd = sysfs_get(target->sd);
77         spin_unlock(&sysfs_symlink_target_lock);
78
79         if (!target_sd)
80                 return -ENOENT;
81
82         sd = kernfs_create_link(parent_sd, name, target_sd);
83         sysfs_put(target_sd);
84
85         if (!IS_ERR(sd))
86                 return 0;
87
88         if (warn && PTR_ERR(sd) == -EEXIST)
89                 sysfs_warn_dup(parent_sd, name);
90         return PTR_ERR(sd);
91 }
92
93 /**
94  *      sysfs_create_link_sd - create symlink to a given object.
95  *      @sd:            directory we're creating the link in.
96  *      @target:        object we're pointing to.
97  *      @name:          name of the symlink.
98  */
99 int sysfs_create_link_sd(struct sysfs_dirent *sd, struct kobject *target,
100                          const char *name)
101 {
102         return sysfs_do_create_link_sd(sd, target, name, 1);
103 }
104
105 static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
106                                 const char *name, int warn)
107 {
108         struct sysfs_dirent *parent_sd = NULL;
109
110         if (!kobj)
111                 parent_sd = &sysfs_root;
112         else
113                 parent_sd = kobj->sd;
114
115         if (!parent_sd)
116                 return -EFAULT;
117
118         return sysfs_do_create_link_sd(parent_sd, target, name, warn);
119 }
120
121 /**
122  *      sysfs_create_link - create symlink between two objects.
123  *      @kobj:  object whose directory we're creating the link in.
124  *      @target:        object we're pointing to.
125  *      @name:          name of the symlink.
126  */
127 int sysfs_create_link(struct kobject *kobj, struct kobject *target,
128                       const char *name)
129 {
130         return sysfs_do_create_link(kobj, target, name, 1);
131 }
132 EXPORT_SYMBOL_GPL(sysfs_create_link);
133
134 /**
135  *      sysfs_create_link_nowarn - create symlink between two objects.
136  *      @kobj:  object whose directory we're creating the link in.
137  *      @target:        object we're pointing to.
138  *      @name:          name of the symlink.
139  *
140  *      This function does the same as sysfs_create_link(), but it
141  *      doesn't warn if the link already exists.
142  */
143 int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
144                              const char *name)
145 {
146         return sysfs_do_create_link(kobj, target, name, 0);
147 }
148
149 /**
150  *      sysfs_delete_link - remove symlink in object's directory.
151  *      @kobj:  object we're acting for.
152  *      @targ:  object we're pointing to.
153  *      @name:  name of the symlink to remove.
154  *
155  *      Unlike sysfs_remove_link sysfs_delete_link has enough information
156  *      to successfully delete symlinks in tagged directories.
157  */
158 void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
159                         const char *name)
160 {
161         const void *ns = NULL;
162
163         /*
164          * We don't own @target and it may be removed at any time.
165          * Synchronize using sysfs_symlink_target_lock.  See
166          * sysfs_remove_dir() for details.
167          */
168         spin_lock(&sysfs_symlink_target_lock);
169         if (targ->sd && (kobj->sd->s_flags & SYSFS_FLAG_NS))
170                 ns = targ->sd->s_ns;
171         spin_unlock(&sysfs_symlink_target_lock);
172         kernfs_remove_by_name_ns(kobj->sd, name, ns);
173 }
174
175 /**
176  *      sysfs_remove_link - remove symlink in object's directory.
177  *      @kobj:  object we're acting for.
178  *      @name:  name of the symlink to remove.
179  */
180 void sysfs_remove_link(struct kobject *kobj, const char *name)
181 {
182         struct sysfs_dirent *parent_sd = NULL;
183
184         if (!kobj)
185                 parent_sd = &sysfs_root;
186         else
187                 parent_sd = kobj->sd;
188
189         kernfs_remove_by_name(parent_sd, name);
190 }
191 EXPORT_SYMBOL_GPL(sysfs_remove_link);
192
193 /**
194  *      sysfs_rename_link_ns - rename symlink in object's directory.
195  *      @kobj:  object we're acting for.
196  *      @targ:  object we're pointing to.
197  *      @old:   previous name of the symlink.
198  *      @new:   new name of the symlink.
199  *      @new_ns: new namespace of the symlink.
200  *
201  *      A helper function for the common rename symlink idiom.
202  */
203 int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *targ,
204                          const char *old, const char *new, const void *new_ns)
205 {
206         struct sysfs_dirent *parent_sd, *sd = NULL;
207         const void *old_ns = NULL;
208         int result;
209
210         if (!kobj)
211                 parent_sd = &sysfs_root;
212         else
213                 parent_sd = kobj->sd;
214
215         if (targ->sd)
216                 old_ns = targ->sd->s_ns;
217
218         result = -ENOENT;
219         sd = sysfs_get_dirent_ns(parent_sd, old, old_ns);
220         if (!sd)
221                 goto out;
222
223         result = -EINVAL;
224         if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
225                 goto out;
226         if (sd->s_symlink.target_sd->priv != targ)
227                 goto out;
228
229         result = kernfs_rename_ns(sd, parent_sd, new, new_ns);
230
231 out:
232         sysfs_put(sd);
233         return result;
234 }
235 EXPORT_SYMBOL_GPL(sysfs_rename_link_ns);
236
237 static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
238                                  struct sysfs_dirent *target_sd, char *path)
239 {
240         struct sysfs_dirent *base, *sd;
241         char *s = path;
242         int len = 0;
243
244         /* go up to the root, stop at the base */
245         base = parent_sd;
246         while (base->s_parent) {
247                 sd = target_sd->s_parent;
248                 while (sd->s_parent && base != sd)
249                         sd = sd->s_parent;
250
251                 if (base == sd)
252                         break;
253
254                 strcpy(s, "../");
255                 s += 3;
256                 base = base->s_parent;
257         }
258
259         /* determine end of target string for reverse fillup */
260         sd = target_sd;
261         while (sd->s_parent && sd != base) {
262                 len += strlen(sd->s_name) + 1;
263                 sd = sd->s_parent;
264         }
265
266         /* check limits */
267         if (len < 2)
268                 return -EINVAL;
269         len--;
270         if ((s - path) + len > PATH_MAX)
271                 return -ENAMETOOLONG;
272
273         /* reverse fillup of target string from target to base */
274         sd = target_sd;
275         while (sd->s_parent && sd != base) {
276                 int slen = strlen(sd->s_name);
277
278                 len -= slen;
279                 strncpy(s + len, sd->s_name, slen);
280                 if (len)
281                         s[--len] = '/';
282
283                 sd = sd->s_parent;
284         }
285
286         return 0;
287 }
288
289 static int sysfs_getlink(struct dentry *dentry, char *path)
290 {
291         struct sysfs_dirent *sd = dentry->d_fsdata;
292         struct sysfs_dirent *parent_sd = sd->s_parent;
293         struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
294         int error;
295
296         mutex_lock(&sysfs_mutex);
297         error = sysfs_get_target_path(parent_sd, target_sd, path);
298         mutex_unlock(&sysfs_mutex);
299
300         return error;
301 }
302
303 static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
304 {
305         int error = -ENOMEM;
306         unsigned long page = get_zeroed_page(GFP_KERNEL);
307         if (page) {
308                 error = sysfs_getlink(dentry, (char *) page);
309                 if (error < 0)
310                         free_page((unsigned long)page);
311         }
312         nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
313         return NULL;
314 }
315
316 static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd,
317                            void *cookie)
318 {
319         char *page = nd_get_link(nd);
320         if (!IS_ERR(page))
321                 free_page((unsigned long)page);
322 }
323
324 const struct inode_operations sysfs_symlink_inode_operations = {
325         .setxattr       = sysfs_setxattr,
326         .readlink       = generic_readlink,
327         .follow_link    = sysfs_follow_link,
328         .put_link       = sysfs_put_link,
329         .setattr        = sysfs_setattr,
330         .getattr        = sysfs_getattr,
331         .permission     = sysfs_permission,
332 };