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