Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
6d66f5cd | 2 | * fs/sysfs/inode.c - basic sysfs inode and dentry operations |
1da177e4 | 3 | * |
6d66f5cd TH |
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. | |
1da177e4 LT |
9 | * |
10 | * Please see Documentation/filesystems/sysfs.txt for more information. | |
11 | */ | |
12 | ||
13 | #undef DEBUG | |
14 | ||
15 | #include <linux/pagemap.h> | |
16 | #include <linux/namei.h> | |
17 | #include <linux/backing-dev.h> | |
16f7e0fe | 18 | #include <linux/capability.h> |
995982ca | 19 | #include <linux/errno.h> |
e8edc6e0 | 20 | #include <linux/sched.h> |
ddd29ec6 DQ |
21 | #include <linux/xattr.h> |
22 | #include <linux/security.h> | |
1da177e4 LT |
23 | #include "sysfs.h" |
24 | ||
25 | extern struct super_block * sysfs_sb; | |
26 | ||
f5e54d6e | 27 | static const struct address_space_operations sysfs_aops = { |
1da177e4 | 28 | .readpage = simple_readpage, |
800d15a5 NP |
29 | .write_begin = simple_write_begin, |
30 | .write_end = simple_write_end, | |
1da177e4 LT |
31 | }; |
32 | ||
33 | static struct backing_dev_info sysfs_backing_dev_info = { | |
d993831f | 34 | .name = "sysfs", |
1da177e4 | 35 | .ra_pages = 0, /* No readahead */ |
e4ad08fe | 36 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, |
1da177e4 LT |
37 | }; |
38 | ||
c5ef1c42 | 39 | static const struct inode_operations sysfs_inode_operations ={ |
e61ab4ae | 40 | .permission = sysfs_permission, |
988d186d | 41 | .setattr = sysfs_setattr, |
e61ab4ae | 42 | .getattr = sysfs_getattr, |
ddd29ec6 | 43 | .setxattr = sysfs_setxattr, |
988d186d MS |
44 | }; |
45 | ||
e0bf68dd PZ |
46 | int __init sysfs_inode_init(void) |
47 | { | |
48 | return bdi_init(&sysfs_backing_dev_info); | |
49 | } | |
50 | ||
f38506c4 | 51 | static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd) |
ddd29ec6 DQ |
52 | { |
53 | struct sysfs_inode_attrs *attrs; | |
54 | struct iattr *iattrs; | |
55 | ||
56 | attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL); | |
57 | if (!attrs) | |
58 | return NULL; | |
59 | iattrs = &attrs->ia_iattr; | |
60 | ||
61 | /* assign default attributes */ | |
62 | iattrs->ia_mode = sd->s_mode; | |
63 | iattrs->ia_uid = 0; | |
64 | iattrs->ia_gid = 0; | |
65 | iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; | |
66 | ||
67 | return attrs; | |
68 | } | |
f38506c4 | 69 | |
35df63c4 | 70 | int sysfs_sd_setattr(struct sysfs_dirent *sd, struct iattr * iattr) |
988d186d | 71 | { |
ddd29ec6 DQ |
72 | struct sysfs_inode_attrs *sd_attrs; |
73 | struct iattr *iattrs; | |
988d186d | 74 | unsigned int ia_valid = iattr->ia_valid; |
988d186d | 75 | |
ddd29ec6 | 76 | sd_attrs = sd->s_iattr; |
988d186d | 77 | |
ddd29ec6 | 78 | if (!sd_attrs) { |
988d186d | 79 | /* setting attributes for the first time, allocate now */ |
ddd29ec6 DQ |
80 | sd_attrs = sysfs_init_inode_attrs(sd); |
81 | if (!sd_attrs) | |
988d186d | 82 | return -ENOMEM; |
ddd29ec6 DQ |
83 | sd->s_iattr = sd_attrs; |
84 | } else { | |
85 | /* attributes were changed at least once in past */ | |
86 | iattrs = &sd_attrs->ia_iattr; | |
87 | ||
88 | if (ia_valid & ATTR_UID) | |
89 | iattrs->ia_uid = iattr->ia_uid; | |
90 | if (ia_valid & ATTR_GID) | |
91 | iattrs->ia_gid = iattr->ia_gid; | |
92 | if (ia_valid & ATTR_ATIME) | |
4be3df28 | 93 | iattrs->ia_atime = iattr->ia_atime; |
ddd29ec6 | 94 | if (ia_valid & ATTR_MTIME) |
4be3df28 | 95 | iattrs->ia_mtime = iattr->ia_mtime; |
ddd29ec6 | 96 | if (ia_valid & ATTR_CTIME) |
4be3df28 | 97 | iattrs->ia_ctime = iattr->ia_ctime; |
ddd29ec6 DQ |
98 | if (ia_valid & ATTR_MODE) { |
99 | umode_t mode = iattr->ia_mode; | |
ddd29ec6 DQ |
100 | iattrs->ia_mode = sd->s_mode = mode; |
101 | } | |
988d186d | 102 | } |
35df63c4 EB |
103 | return 0; |
104 | } | |
105 | ||
106 | int sysfs_setattr(struct dentry *dentry, struct iattr *iattr) | |
107 | { | |
108 | struct inode *inode = dentry->d_inode; | |
109 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
110 | int error; | |
111 | ||
112 | if (!sd) | |
113 | return -EINVAL; | |
114 | ||
115 | error = inode_change_ok(inode, iattr); | |
116 | if (error) | |
117 | return error; | |
118 | ||
119 | iattr->ia_valid &= ~ATTR_SIZE; /* ignore size changes */ | |
35df63c4 EB |
120 | |
121 | error = inode_setattr(inode, iattr); | |
122 | if (error) | |
123 | return error; | |
124 | ||
125 | mutex_lock(&sysfs_mutex); | |
126 | error = sysfs_sd_setattr(sd, iattr); | |
127 | mutex_unlock(&sysfs_mutex); | |
128 | ||
ddd29ec6 DQ |
129 | return error; |
130 | } | |
988d186d | 131 | |
f44d3e78 EB |
132 | static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, u32 *secdata_len) |
133 | { | |
134 | struct sysfs_inode_attrs *iattrs; | |
135 | void *old_secdata; | |
136 | size_t old_secdata_len; | |
137 | ||
138 | iattrs = sd->s_iattr; | |
139 | if (!iattrs) | |
140 | iattrs = sysfs_init_inode_attrs(sd); | |
141 | if (!iattrs) | |
142 | return -ENOMEM; | |
143 | ||
144 | old_secdata = iattrs->ia_secdata; | |
145 | old_secdata_len = iattrs->ia_secdata_len; | |
146 | ||
147 | iattrs->ia_secdata = *secdata; | |
148 | iattrs->ia_secdata_len = *secdata_len; | |
149 | ||
150 | *secdata = old_secdata; | |
151 | *secdata_len = old_secdata_len; | |
152 | return 0; | |
153 | } | |
154 | ||
ddd29ec6 DQ |
155 | int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
156 | size_t size, int flags) | |
157 | { | |
158 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
ddd29ec6 DQ |
159 | void *secdata; |
160 | int error; | |
161 | u32 secdata_len = 0; | |
162 | ||
163 | if (!sd) | |
164 | return -EINVAL; | |
ddd29ec6 DQ |
165 | |
166 | if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) { | |
167 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; | |
168 | error = security_inode_setsecurity(dentry->d_inode, suffix, | |
169 | value, size, flags); | |
170 | if (error) | |
171 | goto out; | |
172 | error = security_inode_getsecctx(dentry->d_inode, | |
173 | &secdata, &secdata_len); | |
174 | if (error) | |
175 | goto out; | |
988d186d | 176 | |
f44d3e78 EB |
177 | mutex_lock(&sysfs_mutex); |
178 | error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len); | |
179 | mutex_unlock(&sysfs_mutex); | |
180 | ||
181 | if (secdata) | |
182 | security_release_secctx(secdata, secdata_len); | |
ddd29ec6 DQ |
183 | } else |
184 | return -EINVAL; | |
185 | out: | |
988d186d MS |
186 | return error; |
187 | } | |
188 | ||
8215534c MS |
189 | static inline void set_default_inode_attr(struct inode * inode, mode_t mode) |
190 | { | |
191 | inode->i_mode = mode; | |
8215534c MS |
192 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
193 | } | |
194 | ||
195 | static inline void set_inode_attr(struct inode * inode, struct iattr * iattr) | |
196 | { | |
8215534c MS |
197 | inode->i_uid = iattr->ia_uid; |
198 | inode->i_gid = iattr->ia_gid; | |
199 | inode->i_atime = iattr->ia_atime; | |
200 | inode->i_mtime = iattr->ia_mtime; | |
201 | inode->i_ctime = iattr->ia_ctime; | |
202 | } | |
203 | ||
372e88bd EB |
204 | static int sysfs_count_nlink(struct sysfs_dirent *sd) |
205 | { | |
206 | struct sysfs_dirent *child; | |
207 | int nr = 0; | |
208 | ||
bc747f37 | 209 | for (child = sd->s_dir.children; child; child = child->s_sibling) |
372e88bd EB |
210 | if (sysfs_type(child) == SYSFS_DIR) |
211 | nr++; | |
212 | ||
213 | return nr + 2; | |
214 | } | |
215 | ||
e61ab4ae EB |
216 | static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode) |
217 | { | |
218 | struct sysfs_inode_attrs *iattrs = sd->s_iattr; | |
219 | ||
220 | inode->i_mode = sd->s_mode; | |
221 | if (iattrs) { | |
222 | /* sysfs_dirent has non-default attributes | |
223 | * get them from persistent copy in sysfs_dirent | |
224 | */ | |
225 | set_inode_attr(inode, &iattrs->ia_iattr); | |
226 | security_inode_notifysecctx(inode, | |
227 | iattrs->ia_secdata, | |
228 | iattrs->ia_secdata_len); | |
229 | } | |
230 | ||
231 | if (sysfs_type(sd) == SYSFS_DIR) | |
232 | inode->i_nlink = sysfs_count_nlink(sd); | |
233 | } | |
234 | ||
235 | int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) | |
236 | { | |
237 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
238 | struct inode *inode = dentry->d_inode; | |
239 | ||
240 | mutex_lock(&sysfs_mutex); | |
241 | sysfs_refresh_inode(sd, inode); | |
242 | mutex_unlock(&sysfs_mutex); | |
243 | ||
244 | generic_fillattr(inode, stat); | |
245 | return 0; | |
246 | } | |
247 | ||
bc37e283 | 248 | static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) |
1da177e4 | 249 | { |
372e88bd EB |
250 | struct bin_attribute *bin_attr; |
251 | ||
04256b4a | 252 | inode->i_private = sysfs_get(sd); |
fc9f54b9 TH |
253 | inode->i_mapping->a_ops = &sysfs_aops; |
254 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; | |
255 | inode->i_op = &sysfs_inode_operations; | |
fc9f54b9 | 256 | |
e61ab4ae EB |
257 | set_default_inode_attr(inode, sd->s_mode); |
258 | sysfs_refresh_inode(sd, inode); | |
372e88bd | 259 | |
372e88bd EB |
260 | /* initialize inode according to type */ |
261 | switch (sysfs_type(sd)) { | |
372e88bd EB |
262 | case SYSFS_DIR: |
263 | inode->i_op = &sysfs_dir_inode_operations; | |
264 | inode->i_fop = &sysfs_dir_operations; | |
372e88bd EB |
265 | break; |
266 | case SYSFS_KOBJ_ATTR: | |
267 | inode->i_size = PAGE_SIZE; | |
268 | inode->i_fop = &sysfs_file_operations; | |
269 | break; | |
270 | case SYSFS_KOBJ_BIN_ATTR: | |
b1fc3d61 | 271 | bin_attr = sd->s_bin_attr.bin_attr; |
372e88bd EB |
272 | inode->i_size = bin_attr->size; |
273 | inode->i_fop = &bin_fops; | |
274 | break; | |
275 | case SYSFS_KOBJ_LINK: | |
276 | inode->i_op = &sysfs_symlink_inode_operations; | |
277 | break; | |
278 | default: | |
279 | BUG(); | |
280 | } | |
281 | ||
282 | unlock_new_inode(inode); | |
fc9f54b9 TH |
283 | } |
284 | ||
285 | /** | |
8312a8d7 | 286 | * sysfs_get_inode - get inode for sysfs_dirent |
fc9f54b9 TH |
287 | * @sd: sysfs_dirent to allocate inode for |
288 | * | |
8312a8d7 TH |
289 | * Get inode for @sd. If such inode doesn't exist, a new inode |
290 | * is allocated and basics are initialized. New inode is | |
291 | * returned locked. | |
fc9f54b9 TH |
292 | * |
293 | * LOCKING: | |
294 | * Kernel thread context (may sleep). | |
295 | * | |
296 | * RETURNS: | |
297 | * Pointer to allocated inode on success, NULL on failure. | |
298 | */ | |
8312a8d7 | 299 | struct inode * sysfs_get_inode(struct sysfs_dirent *sd) |
fc9f54b9 TH |
300 | { |
301 | struct inode *inode; | |
302 | ||
8312a8d7 TH |
303 | inode = iget_locked(sysfs_sb, sd->s_ino); |
304 | if (inode && (inode->i_state & I_NEW)) | |
fc9f54b9 TH |
305 | sysfs_init_inode(sd, inode); |
306 | ||
1da177e4 LT |
307 | return inode; |
308 | } | |
309 | ||
04256b4a EB |
310 | /* |
311 | * The sysfs_dirent serves as both an inode and a directory entry for sysfs. | |
312 | * To prevent the sysfs inode numbers from being freed prematurely we take a | |
313 | * reference to sysfs_dirent from the sysfs inode. A | |
314 | * super_operations.delete_inode() implementation is needed to drop that | |
315 | * reference upon inode destruction. | |
316 | */ | |
317 | void sysfs_delete_inode(struct inode *inode) | |
318 | { | |
319 | struct sysfs_dirent *sd = inode->i_private; | |
320 | ||
321 | truncate_inode_pages(&inode->i_data, 0); | |
322 | clear_inode(inode); | |
323 | sysfs_put(sd); | |
324 | } | |
325 | ||
608e266a | 326 | int sysfs_hash_and_remove(struct sysfs_dirent *dir_sd, const char *name) |
1da177e4 | 327 | { |
fb6896da | 328 | struct sysfs_addrm_cxt acxt; |
41fc1c27 | 329 | struct sysfs_dirent *sd; |
641e6f30 | 330 | |
608e266a | 331 | if (!dir_sd) |
995982ca | 332 | return -ENOENT; |
1da177e4 | 333 | |
fb6896da | 334 | sysfs_addrm_start(&acxt, dir_sd); |
608e266a | 335 | |
41fc1c27 TH |
336 | sd = sysfs_find_dirent(dir_sd, name); |
337 | if (sd) | |
338 | sysfs_remove_one(&acxt, sd); | |
3007e997 | 339 | |
990e53f8 TH |
340 | sysfs_addrm_finish(&acxt); |
341 | ||
342 | if (sd) | |
fb6896da | 343 | return 0; |
990e53f8 TH |
344 | else |
345 | return -ENOENT; | |
1da177e4 | 346 | } |
e61ab4ae EB |
347 | |
348 | int sysfs_permission(struct inode *inode, int mask) | |
349 | { | |
350 | struct sysfs_dirent *sd = inode->i_private; | |
351 | ||
352 | mutex_lock(&sysfs_mutex); | |
353 | sysfs_refresh_inode(sd, inode); | |
354 | mutex_unlock(&sysfs_mutex); | |
355 | ||
356 | return generic_permission(inode, mask, NULL); | |
357 | } |