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