Commit | Line | Data |
---|---|---|
b8441ed2 TH |
1 | /* |
2 | * fs/kernfs/inode.c - kernfs inode implementation | |
3 | * | |
4 | * Copyright (c) 2001-3 Patrick Mochel | |
5 | * Copyright (c) 2007 SUSE Linux Products GmbH | |
6 | * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> | |
7 | * | |
8 | * This file is released under the GPLv2. | |
9 | */ | |
ffed24e2 TH |
10 | |
11 | #include <linux/pagemap.h> | |
12 | #include <linux/backing-dev.h> | |
13 | #include <linux/capability.h> | |
14 | #include <linux/errno.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/xattr.h> | |
17 | #include <linux/security.h> | |
18 | ||
19 | #include "kernfs-internal.h" | |
20 | ||
21 | static const struct address_space_operations sysfs_aops = { | |
22 | .readpage = simple_readpage, | |
23 | .write_begin = simple_write_begin, | |
24 | .write_end = simple_write_end, | |
25 | }; | |
26 | ||
27 | static struct backing_dev_info sysfs_backing_dev_info = { | |
28 | .name = "sysfs", | |
29 | .ra_pages = 0, /* No readahead */ | |
30 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, | |
31 | }; | |
32 | ||
33 | static const struct inode_operations sysfs_inode_operations = { | |
34 | .permission = sysfs_permission, | |
35 | .setattr = sysfs_setattr, | |
36 | .getattr = sysfs_getattr, | |
37 | .setxattr = sysfs_setxattr, | |
2322392b TH |
38 | .removexattr = sysfs_removexattr, |
39 | .getxattr = sysfs_getxattr, | |
40 | .listxattr = sysfs_listxattr, | |
ffed24e2 TH |
41 | }; |
42 | ||
4b93dc9b | 43 | void __init sysfs_inode_init(void) |
ffed24e2 | 44 | { |
4b93dc9b TH |
45 | if (bdi_init(&sysfs_backing_dev_info)) |
46 | panic("failed to init sysfs_backing_dev_info"); | |
ffed24e2 TH |
47 | } |
48 | ||
9a8049af | 49 | static struct sysfs_inode_attrs *sysfs_inode_attrs(struct sysfs_dirent *sd) |
ffed24e2 | 50 | { |
ffed24e2 TH |
51 | struct iattr *iattrs; |
52 | ||
9a8049af TH |
53 | if (sd->s_iattr) |
54 | return sd->s_iattr; | |
55 | ||
56 | sd->s_iattr = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL); | |
57 | if (!sd->s_iattr) | |
ffed24e2 | 58 | return NULL; |
9a8049af | 59 | iattrs = &sd->s_iattr->ia_iattr; |
ffed24e2 TH |
60 | |
61 | /* assign default attributes */ | |
62 | iattrs->ia_mode = sd->s_mode; | |
63 | iattrs->ia_uid = GLOBAL_ROOT_UID; | |
64 | iattrs->ia_gid = GLOBAL_ROOT_GID; | |
65 | iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; | |
66 | ||
2322392b TH |
67 | simple_xattrs_init(&sd->s_iattr->xattrs); |
68 | ||
9a8049af | 69 | return sd->s_iattr; |
ffed24e2 TH |
70 | } |
71 | ||
72 | static int __kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr) | |
73 | { | |
9a8049af | 74 | struct sysfs_inode_attrs *attrs; |
ffed24e2 TH |
75 | struct iattr *iattrs; |
76 | unsigned int ia_valid = iattr->ia_valid; | |
77 | ||
9a8049af TH |
78 | attrs = sysfs_inode_attrs(sd); |
79 | if (!attrs) | |
80 | return -ENOMEM; | |
ffed24e2 | 81 | |
9a8049af | 82 | iattrs = &attrs->ia_iattr; |
ffed24e2 TH |
83 | |
84 | if (ia_valid & ATTR_UID) | |
85 | iattrs->ia_uid = iattr->ia_uid; | |
86 | if (ia_valid & ATTR_GID) | |
87 | iattrs->ia_gid = iattr->ia_gid; | |
88 | if (ia_valid & ATTR_ATIME) | |
89 | iattrs->ia_atime = iattr->ia_atime; | |
90 | if (ia_valid & ATTR_MTIME) | |
91 | iattrs->ia_mtime = iattr->ia_mtime; | |
92 | if (ia_valid & ATTR_CTIME) | |
93 | iattrs->ia_ctime = iattr->ia_ctime; | |
94 | if (ia_valid & ATTR_MODE) { | |
95 | umode_t mode = iattr->ia_mode; | |
96 | iattrs->ia_mode = sd->s_mode = mode; | |
97 | } | |
98 | return 0; | |
99 | } | |
100 | ||
101 | /** | |
102 | * kernfs_setattr - set iattr on a node | |
103 | * @sd: target node | |
104 | * @iattr: iattr to set | |
105 | * | |
106 | * Returns 0 on success, -errno on failure. | |
107 | */ | |
108 | int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr) | |
109 | { | |
110 | int ret; | |
111 | ||
112 | mutex_lock(&sysfs_mutex); | |
113 | ret = __kernfs_setattr(sd, iattr); | |
114 | mutex_unlock(&sysfs_mutex); | |
115 | return ret; | |
116 | } | |
117 | ||
118 | int sysfs_setattr(struct dentry *dentry, struct iattr *iattr) | |
119 | { | |
120 | struct inode *inode = dentry->d_inode; | |
121 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
122 | int error; | |
123 | ||
124 | if (!sd) | |
125 | return -EINVAL; | |
126 | ||
127 | mutex_lock(&sysfs_mutex); | |
128 | error = inode_change_ok(inode, iattr); | |
129 | if (error) | |
130 | goto out; | |
131 | ||
132 | error = __kernfs_setattr(sd, iattr); | |
133 | if (error) | |
134 | goto out; | |
135 | ||
136 | /* this ignores size changes */ | |
137 | setattr_copy(inode, iattr); | |
138 | ||
139 | out: | |
140 | mutex_unlock(&sysfs_mutex); | |
141 | return error; | |
142 | } | |
143 | ||
144 | static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, | |
145 | u32 *secdata_len) | |
146 | { | |
9a8049af | 147 | struct sysfs_inode_attrs *attrs; |
ffed24e2 TH |
148 | void *old_secdata; |
149 | size_t old_secdata_len; | |
150 | ||
9a8049af TH |
151 | attrs = sysfs_inode_attrs(sd); |
152 | if (!attrs) | |
153 | return -ENOMEM; | |
ffed24e2 | 154 | |
9a8049af TH |
155 | old_secdata = attrs->ia_secdata; |
156 | old_secdata_len = attrs->ia_secdata_len; | |
ffed24e2 | 157 | |
9a8049af TH |
158 | attrs->ia_secdata = *secdata; |
159 | attrs->ia_secdata_len = *secdata_len; | |
ffed24e2 TH |
160 | |
161 | *secdata = old_secdata; | |
162 | *secdata_len = old_secdata_len; | |
163 | return 0; | |
164 | } | |
165 | ||
166 | int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, | |
167 | size_t size, int flags) | |
168 | { | |
169 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
2322392b | 170 | struct sysfs_inode_attrs *attrs; |
ffed24e2 TH |
171 | void *secdata; |
172 | int error; | |
173 | u32 secdata_len = 0; | |
174 | ||
2322392b TH |
175 | attrs = sysfs_inode_attrs(sd); |
176 | if (!attrs) | |
177 | return -ENOMEM; | |
ffed24e2 TH |
178 | |
179 | if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) { | |
180 | const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; | |
181 | error = security_inode_setsecurity(dentry->d_inode, suffix, | |
182 | value, size, flags); | |
183 | if (error) | |
2322392b | 184 | return error; |
ffed24e2 TH |
185 | error = security_inode_getsecctx(dentry->d_inode, |
186 | &secdata, &secdata_len); | |
187 | if (error) | |
2322392b | 188 | return error; |
ffed24e2 TH |
189 | |
190 | mutex_lock(&sysfs_mutex); | |
191 | error = sysfs_sd_setsecdata(sd, &secdata, &secdata_len); | |
192 | mutex_unlock(&sysfs_mutex); | |
193 | ||
194 | if (secdata) | |
195 | security_release_secctx(secdata, secdata_len); | |
2322392b TH |
196 | return error; |
197 | } else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) { | |
198 | return simple_xattr_set(&attrs->xattrs, name, value, size, | |
199 | flags); | |
200 | } | |
201 | ||
202 | return -EINVAL; | |
203 | } | |
204 | ||
205 | int sysfs_removexattr(struct dentry *dentry, const char *name) | |
206 | { | |
207 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
208 | struct sysfs_inode_attrs *attrs; | |
209 | ||
210 | attrs = sysfs_inode_attrs(sd); | |
211 | if (!attrs) | |
212 | return -ENOMEM; | |
213 | ||
214 | return simple_xattr_remove(&attrs->xattrs, name); | |
215 | } | |
216 | ||
217 | ssize_t sysfs_getxattr(struct dentry *dentry, const char *name, void *buf, | |
218 | size_t size) | |
219 | { | |
220 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
221 | struct sysfs_inode_attrs *attrs; | |
222 | ||
223 | attrs = sysfs_inode_attrs(sd); | |
224 | if (!attrs) | |
225 | return -ENOMEM; | |
226 | ||
227 | return simple_xattr_get(&attrs->xattrs, name, buf, size); | |
228 | } | |
229 | ||
230 | ssize_t sysfs_listxattr(struct dentry *dentry, char *buf, size_t size) | |
231 | { | |
232 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
233 | struct sysfs_inode_attrs *attrs; | |
234 | ||
235 | attrs = sysfs_inode_attrs(sd); | |
236 | if (!attrs) | |
237 | return -ENOMEM; | |
238 | ||
239 | return simple_xattr_list(&attrs->xattrs, buf, size); | |
ffed24e2 TH |
240 | } |
241 | ||
242 | static inline void set_default_inode_attr(struct inode *inode, umode_t mode) | |
243 | { | |
244 | inode->i_mode = mode; | |
245 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
246 | } | |
247 | ||
248 | static inline void set_inode_attr(struct inode *inode, struct iattr *iattr) | |
249 | { | |
250 | inode->i_uid = iattr->ia_uid; | |
251 | inode->i_gid = iattr->ia_gid; | |
252 | inode->i_atime = iattr->ia_atime; | |
253 | inode->i_mtime = iattr->ia_mtime; | |
254 | inode->i_ctime = iattr->ia_ctime; | |
255 | } | |
256 | ||
257 | static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode) | |
258 | { | |
9a8049af | 259 | struct sysfs_inode_attrs *attrs = sd->s_iattr; |
ffed24e2 TH |
260 | |
261 | inode->i_mode = sd->s_mode; | |
9a8049af | 262 | if (attrs) { |
ffed24e2 TH |
263 | /* sysfs_dirent has non-default attributes |
264 | * get them from persistent copy in sysfs_dirent | |
265 | */ | |
9a8049af TH |
266 | set_inode_attr(inode, &attrs->ia_iattr); |
267 | security_inode_notifysecctx(inode, attrs->ia_secdata, | |
268 | attrs->ia_secdata_len); | |
ffed24e2 TH |
269 | } |
270 | ||
271 | if (sysfs_type(sd) == SYSFS_DIR) | |
272 | set_nlink(inode, sd->s_dir.subdirs + 2); | |
273 | } | |
274 | ||
275 | int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, | |
276 | struct kstat *stat) | |
277 | { | |
278 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
279 | struct inode *inode = dentry->d_inode; | |
280 | ||
281 | mutex_lock(&sysfs_mutex); | |
282 | sysfs_refresh_inode(sd, inode); | |
283 | mutex_unlock(&sysfs_mutex); | |
284 | ||
285 | generic_fillattr(inode, stat); | |
286 | return 0; | |
287 | } | |
288 | ||
289 | static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) | |
290 | { | |
291 | kernfs_get(sd); | |
292 | inode->i_private = sd; | |
293 | inode->i_mapping->a_ops = &sysfs_aops; | |
294 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; | |
295 | inode->i_op = &sysfs_inode_operations; | |
296 | ||
297 | set_default_inode_attr(inode, sd->s_mode); | |
298 | sysfs_refresh_inode(sd, inode); | |
299 | ||
300 | /* initialize inode according to type */ | |
301 | switch (sysfs_type(sd)) { | |
302 | case SYSFS_DIR: | |
303 | inode->i_op = &sysfs_dir_inode_operations; | |
304 | inode->i_fop = &sysfs_dir_operations; | |
305 | break; | |
306 | case SYSFS_KOBJ_ATTR: | |
307 | inode->i_size = sd->s_attr.size; | |
308 | inode->i_fop = &kernfs_file_operations; | |
309 | break; | |
310 | case SYSFS_KOBJ_LINK: | |
311 | inode->i_op = &sysfs_symlink_inode_operations; | |
312 | break; | |
313 | default: | |
314 | BUG(); | |
315 | } | |
316 | ||
317 | unlock_new_inode(inode); | |
318 | } | |
319 | ||
320 | /** | |
321 | * sysfs_get_inode - get inode for sysfs_dirent | |
322 | * @sb: super block | |
323 | * @sd: sysfs_dirent to allocate inode for | |
324 | * | |
325 | * Get inode for @sd. If such inode doesn't exist, a new inode | |
326 | * is allocated and basics are initialized. New inode is | |
327 | * returned locked. | |
328 | * | |
329 | * LOCKING: | |
330 | * Kernel thread context (may sleep). | |
331 | * | |
332 | * RETURNS: | |
333 | * Pointer to allocated inode on success, NULL on failure. | |
334 | */ | |
335 | struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd) | |
336 | { | |
337 | struct inode *inode; | |
338 | ||
339 | inode = iget_locked(sb, sd->s_ino); | |
340 | if (inode && (inode->i_state & I_NEW)) | |
341 | sysfs_init_inode(sd, inode); | |
342 | ||
343 | return inode; | |
344 | } | |
345 | ||
346 | /* | |
347 | * The sysfs_dirent serves as both an inode and a directory entry for sysfs. | |
348 | * To prevent the sysfs inode numbers from being freed prematurely we take a | |
349 | * reference to sysfs_dirent from the sysfs inode. A | |
350 | * super_operations.evict_inode() implementation is needed to drop that | |
351 | * reference upon inode destruction. | |
352 | */ | |
353 | void sysfs_evict_inode(struct inode *inode) | |
354 | { | |
355 | struct sysfs_dirent *sd = inode->i_private; | |
356 | ||
357 | truncate_inode_pages(&inode->i_data, 0); | |
358 | clear_inode(inode); | |
359 | kernfs_put(sd); | |
360 | } | |
361 | ||
362 | int sysfs_permission(struct inode *inode, int mask) | |
363 | { | |
364 | struct sysfs_dirent *sd; | |
365 | ||
366 | if (mask & MAY_NOT_BLOCK) | |
367 | return -ECHILD; | |
368 | ||
369 | sd = inode->i_private; | |
370 | ||
371 | mutex_lock(&sysfs_mutex); | |
372 | sysfs_refresh_inode(sd, inode); | |
373 | mutex_unlock(&sysfs_mutex); | |
374 | ||
375 | return generic_permission(inode, mask); | |
376 | } |