]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/sysfs/symlink.c
LSM/SELinux: inode_{get,set,notify}secctx hooks to access LSM security context inform...
[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>
ceeee1fb 14#include <linux/mount.h>
1da177e4
LT
15#include <linux/module.h>
16#include <linux/kobject.h>
17#include <linux/namei.h>
869512ab 18#include <linux/mutex.h>
1da177e4
LT
19
20#include "sysfs.h"
21
36ce6dad
CH
22static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
23 const char *name, int warn)
1da177e4 24{
2b29ac25
TH
25 struct sysfs_dirent *parent_sd = NULL;
26 struct sysfs_dirent *target_sd = NULL;
3007e997 27 struct sysfs_dirent *sd = NULL;
fb6896da 28 struct sysfs_addrm_cxt acxt;
3007e997 29 int error;
1da177e4 30
ceeee1fb
GKH
31 BUG_ON(!name);
32
7d0c7d67
EB
33 if (!kobj)
34 parent_sd = &sysfs_root;
35 else
608e266a 36 parent_sd = kobj->sd;
ceeee1fb 37
3007e997 38 error = -EFAULT;
608e266a 39 if (!parent_sd)
3007e997 40 goto out_put;
2b29ac25 41
608e266a 42 /* target->sd can go away beneath us but is protected with
5f995323 43 * sysfs_assoc_lock. Fetch target_sd from it.
2b29ac25 44 */
5f995323 45 spin_lock(&sysfs_assoc_lock);
608e266a
TH
46 if (target->sd)
47 target_sd = sysfs_get(target->sd);
5f995323 48 spin_unlock(&sysfs_assoc_lock);
2b29ac25 49
3007e997 50 error = -ENOENT;
2b29ac25 51 if (!target_sd)
3007e997
TH
52 goto out_put;
53
54 error = -ENOMEM;
55 sd = sysfs_new_dirent(name, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
56 if (!sd)
57 goto out_put;
a1da4dfe 58
b1fc3d61 59 sd->s_symlink.target_sd = target_sd;
a1da4dfe 60 target_sd = NULL; /* reference is now owned by the symlink */
1da177e4 61
fb6896da 62 sysfs_addrm_start(&acxt, parent_sd);
36ce6dad
CH
63 if (warn)
64 error = sysfs_add_one(&acxt, sd);
65 else
66 error = __sysfs_add_one(&acxt, sd);
23dc2799 67 sysfs_addrm_finish(&acxt);
2b29ac25 68
23dc2799 69 if (error)
967e35dc 70 goto out_put;
967e35dc
TH
71
72 return 0;
fb6896da 73
3007e997
TH
74 out_put:
75 sysfs_put(target_sd);
76 sysfs_put(sd);
1da177e4
LT
77 return error;
78}
79
36ce6dad
CH
80/**
81 * sysfs_create_link - create symlink between two objects.
82 * @kobj: object whose directory we're creating the link in.
83 * @target: object we're pointing to.
84 * @name: name of the symlink.
85 */
86int sysfs_create_link(struct kobject *kobj, struct kobject *target,
87 const char *name)
88{
89 return sysfs_do_create_link(kobj, target, name, 1);
90}
91
92/**
93 * sysfs_create_link_nowarn - create symlink between two objects.
94 * @kobj: object whose directory we're creating the link in.
95 * @target: object we're pointing to.
96 * @name: name of the symlink.
97 *
98 * This function does the same as sysf_create_link(), but it
99 * doesn't warn if the link already exists.
100 */
101int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
102 const char *name)
103{
104 return sysfs_do_create_link(kobj, target, name, 0);
105}
106
1da177e4
LT
107/**
108 * sysfs_remove_link - remove symlink in object's directory.
109 * @kobj: object we're acting for.
110 * @name: name of the symlink to remove.
111 */
112
e3a15db2 113void sysfs_remove_link(struct kobject * kobj, const char * name)
1da177e4 114{
a839c5af
MF
115 struct sysfs_dirent *parent_sd = NULL;
116
117 if (!kobj)
118 parent_sd = &sysfs_root;
119 else
120 parent_sd = kobj->sd;
121
122 sysfs_hash_and_remove(parent_sd, name);
1da177e4
LT
123}
124
2f90a851
KS
125static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
126 struct sysfs_dirent *target_sd, char *path)
1da177e4 127{
2f90a851
KS
128 struct sysfs_dirent *base, *sd;
129 char *s = path;
130 int len = 0;
131
132 /* go up to the root, stop at the base */
133 base = parent_sd;
134 while (base->s_parent) {
135 sd = target_sd->s_parent;
136 while (sd->s_parent && base != sd)
137 sd = sd->s_parent;
138
139 if (base == sd)
140 break;
141
142 strcpy(s, "../");
143 s += 3;
144 base = base->s_parent;
145 }
146
147 /* determine end of target string for reverse fillup */
148 sd = target_sd;
149 while (sd->s_parent && sd != base) {
150 len += strlen(sd->s_name) + 1;
151 sd = sd->s_parent;
152 }
1da177e4 153
2f90a851
KS
154 /* check limits */
155 if (len < 2)
156 return -EINVAL;
157 len--;
158 if ((s - path) + len > PATH_MAX)
1da177e4
LT
159 return -ENAMETOOLONG;
160
2f90a851
KS
161 /* reverse fillup of target string from target to base */
162 sd = target_sd;
163 while (sd->s_parent && sd != base) {
164 int slen = strlen(sd->s_name);
1da177e4 165
2f90a851
KS
166 len -= slen;
167 strncpy(s + len, sd->s_name, slen);
168 if (len)
169 s[--len] = '/';
1da177e4 170
2f90a851
KS
171 sd = sd->s_parent;
172 }
1da177e4
LT
173
174 return 0;
175}
176
177static int sysfs_getlink(struct dentry *dentry, char * path)
178{
2b29ac25
TH
179 struct sysfs_dirent *sd = dentry->d_fsdata;
180 struct sysfs_dirent *parent_sd = sd->s_parent;
b1fc3d61 181 struct sysfs_dirent *target_sd = sd->s_symlink.target_sd;
2b29ac25 182 int error;
1da177e4 183
3007e997 184 mutex_lock(&sysfs_mutex);
2b29ac25 185 error = sysfs_get_target_path(parent_sd, target_sd, path);
3007e997 186 mutex_unlock(&sysfs_mutex);
1da177e4 187
2b29ac25 188 return error;
1da177e4
LT
189}
190
cc314eef 191static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
192{
193 int error = -ENOMEM;
194 unsigned long page = get_zeroed_page(GFP_KERNEL);
557411eb 195 if (page) {
1da177e4 196 error = sysfs_getlink(dentry, (char *) page);
557411eb
AK
197 if (error < 0)
198 free_page((unsigned long)page);
199 }
1da177e4 200 nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
cc314eef 201 return NULL;
1da177e4
LT
202}
203
cc314eef 204static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
1da177e4
LT
205{
206 char *page = nd_get_link(nd);
207 if (!IS_ERR(page))
208 free_page((unsigned long)page);
209}
210
c5ef1c42 211const struct inode_operations sysfs_symlink_inode_operations = {
1da177e4
LT
212 .readlink = generic_readlink,
213 .follow_link = sysfs_follow_link,
214 .put_link = sysfs_put_link,
215};
216
217
218EXPORT_SYMBOL_GPL(sysfs_create_link);
219EXPORT_SYMBOL_GPL(sysfs_remove_link);