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