]>
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, | |
38 | }; | |
39 | ||
4b93dc9b | 40 | void __init sysfs_inode_init(void) |
ffed24e2 | 41 | { |
4b93dc9b TH |
42 | if (bdi_init(&sysfs_backing_dev_info)) |
43 | panic("failed to init sysfs_backing_dev_info"); | |
ffed24e2 TH |
44 | } |
45 | ||
46 | static struct sysfs_inode_attrs *sysfs_init_inode_attrs(struct sysfs_dirent *sd) | |
47 | { | |
48 | struct sysfs_inode_attrs *attrs; | |
49 | struct iattr *iattrs; | |
50 | ||
51 | attrs = kzalloc(sizeof(struct sysfs_inode_attrs), GFP_KERNEL); | |
52 | if (!attrs) | |
53 | return NULL; | |
54 | iattrs = &attrs->ia_iattr; | |
55 | ||
56 | /* assign default attributes */ | |
57 | iattrs->ia_mode = sd->s_mode; | |
58 | iattrs->ia_uid = GLOBAL_ROOT_UID; | |
59 | iattrs->ia_gid = GLOBAL_ROOT_GID; | |
60 | iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME; | |
61 | ||
62 | return attrs; | |
63 | } | |
64 | ||
65 | static int __kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr) | |
66 | { | |
67 | struct sysfs_inode_attrs *sd_attrs; | |
68 | struct iattr *iattrs; | |
69 | unsigned int ia_valid = iattr->ia_valid; | |
70 | ||
71 | sd_attrs = sd->s_iattr; | |
72 | ||
73 | if (!sd_attrs) { | |
74 | /* setting attributes for the first time, allocate now */ | |
75 | sd_attrs = sysfs_init_inode_attrs(sd); | |
76 | if (!sd_attrs) | |
77 | return -ENOMEM; | |
78 | sd->s_iattr = sd_attrs; | |
79 | } | |
80 | /* attributes were changed at least once in past */ | |
81 | iattrs = &sd_attrs->ia_iattr; | |
82 | ||
83 | if (ia_valid & ATTR_UID) | |
84 | iattrs->ia_uid = iattr->ia_uid; | |
85 | if (ia_valid & ATTR_GID) | |
86 | iattrs->ia_gid = iattr->ia_gid; | |
87 | if (ia_valid & ATTR_ATIME) | |
88 | iattrs->ia_atime = iattr->ia_atime; | |
89 | if (ia_valid & ATTR_MTIME) | |
90 | iattrs->ia_mtime = iattr->ia_mtime; | |
91 | if (ia_valid & ATTR_CTIME) | |
92 | iattrs->ia_ctime = iattr->ia_ctime; | |
93 | if (ia_valid & ATTR_MODE) { | |
94 | umode_t mode = iattr->ia_mode; | |
95 | iattrs->ia_mode = sd->s_mode = mode; | |
96 | } | |
97 | return 0; | |
98 | } | |
99 | ||
100 | /** | |
101 | * kernfs_setattr - set iattr on a node | |
102 | * @sd: target node | |
103 | * @iattr: iattr to set | |
104 | * | |
105 | * Returns 0 on success, -errno on failure. | |
106 | */ | |
107 | int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr) | |
108 | { | |
109 | int ret; | |
110 | ||
111 | mutex_lock(&sysfs_mutex); | |
112 | ret = __kernfs_setattr(sd, iattr); | |
113 | mutex_unlock(&sysfs_mutex); | |
114 | return ret; | |
115 | } | |
116 | ||
117 | int sysfs_setattr(struct dentry *dentry, struct iattr *iattr) | |
118 | { | |
119 | struct inode *inode = dentry->d_inode; | |
120 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
121 | int error; | |
122 | ||
123 | if (!sd) | |
124 | return -EINVAL; | |
125 | ||
126 | mutex_lock(&sysfs_mutex); | |
127 | error = inode_change_ok(inode, iattr); | |
128 | if (error) | |
129 | goto out; | |
130 | ||
131 | error = __kernfs_setattr(sd, iattr); | |
132 | if (error) | |
133 | goto out; | |
134 | ||
135 | /* this ignores size changes */ | |
136 | setattr_copy(inode, iattr); | |
137 | ||
138 | out: | |
139 | mutex_unlock(&sysfs_mutex); | |
140 | return error; | |
141 | } | |
142 | ||
143 | static int sysfs_sd_setsecdata(struct sysfs_dirent *sd, void **secdata, | |
144 | u32 *secdata_len) | |
145 | { | |
146 | struct sysfs_inode_attrs *iattrs; | |
147 | void *old_secdata; | |
148 | size_t old_secdata_len; | |
149 | ||
150 | if (!sd->s_iattr) { | |
151 | sd->s_iattr = sysfs_init_inode_attrs(sd); | |
152 | if (!sd->s_iattr) | |
153 | return -ENOMEM; | |
154 | } | |
155 | ||
156 | iattrs = sd->s_iattr; | |
157 | old_secdata = iattrs->ia_secdata; | |
158 | old_secdata_len = iattrs->ia_secdata_len; | |
159 | ||
160 | iattrs->ia_secdata = *secdata; | |
161 | iattrs->ia_secdata_len = *secdata_len; | |
162 | ||
163 | *secdata = old_secdata; | |
164 | *secdata_len = old_secdata_len; | |
165 | return 0; | |
166 | } | |
167 | ||
168 | int sysfs_setxattr(struct dentry *dentry, const char *name, const void *value, | |
169 | size_t size, int flags) | |
170 | { | |
171 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
172 | void *secdata; | |
173 | int error; | |
174 | u32 secdata_len = 0; | |
175 | ||
176 | if (!sd) | |
177 | return -EINVAL; | |
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) | |
184 | goto out; | |
185 | error = security_inode_getsecctx(dentry->d_inode, | |
186 | &secdata, &secdata_len); | |
187 | if (error) | |
188 | goto out; | |
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); | |
196 | } else | |
197 | return -EINVAL; | |
198 | out: | |
199 | return error; | |
200 | } | |
201 | ||
202 | static inline void set_default_inode_attr(struct inode *inode, umode_t mode) | |
203 | { | |
204 | inode->i_mode = mode; | |
205 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
206 | } | |
207 | ||
208 | static inline void set_inode_attr(struct inode *inode, struct iattr *iattr) | |
209 | { | |
210 | inode->i_uid = iattr->ia_uid; | |
211 | inode->i_gid = iattr->ia_gid; | |
212 | inode->i_atime = iattr->ia_atime; | |
213 | inode->i_mtime = iattr->ia_mtime; | |
214 | inode->i_ctime = iattr->ia_ctime; | |
215 | } | |
216 | ||
217 | static void sysfs_refresh_inode(struct sysfs_dirent *sd, struct inode *inode) | |
218 | { | |
219 | struct sysfs_inode_attrs *iattrs = sd->s_iattr; | |
220 | ||
221 | inode->i_mode = sd->s_mode; | |
222 | if (iattrs) { | |
223 | /* sysfs_dirent has non-default attributes | |
224 | * get them from persistent copy in sysfs_dirent | |
225 | */ | |
226 | set_inode_attr(inode, &iattrs->ia_iattr); | |
227 | security_inode_notifysecctx(inode, | |
228 | iattrs->ia_secdata, | |
229 | iattrs->ia_secdata_len); | |
230 | } | |
231 | ||
232 | if (sysfs_type(sd) == SYSFS_DIR) | |
233 | set_nlink(inode, sd->s_dir.subdirs + 2); | |
234 | } | |
235 | ||
236 | int sysfs_getattr(struct vfsmount *mnt, struct dentry *dentry, | |
237 | struct kstat *stat) | |
238 | { | |
239 | struct sysfs_dirent *sd = dentry->d_fsdata; | |
240 | struct inode *inode = dentry->d_inode; | |
241 | ||
242 | mutex_lock(&sysfs_mutex); | |
243 | sysfs_refresh_inode(sd, inode); | |
244 | mutex_unlock(&sysfs_mutex); | |
245 | ||
246 | generic_fillattr(inode, stat); | |
247 | return 0; | |
248 | } | |
249 | ||
250 | static void sysfs_init_inode(struct sysfs_dirent *sd, struct inode *inode) | |
251 | { | |
252 | kernfs_get(sd); | |
253 | inode->i_private = sd; | |
254 | inode->i_mapping->a_ops = &sysfs_aops; | |
255 | inode->i_mapping->backing_dev_info = &sysfs_backing_dev_info; | |
256 | inode->i_op = &sysfs_inode_operations; | |
257 | ||
258 | set_default_inode_attr(inode, sd->s_mode); | |
259 | sysfs_refresh_inode(sd, inode); | |
260 | ||
261 | /* initialize inode according to type */ | |
262 | switch (sysfs_type(sd)) { | |
263 | case SYSFS_DIR: | |
264 | inode->i_op = &sysfs_dir_inode_operations; | |
265 | inode->i_fop = &sysfs_dir_operations; | |
266 | break; | |
267 | case SYSFS_KOBJ_ATTR: | |
268 | inode->i_size = sd->s_attr.size; | |
269 | inode->i_fop = &kernfs_file_operations; | |
270 | break; | |
271 | case SYSFS_KOBJ_LINK: | |
272 | inode->i_op = &sysfs_symlink_inode_operations; | |
273 | break; | |
274 | default: | |
275 | BUG(); | |
276 | } | |
277 | ||
278 | unlock_new_inode(inode); | |
279 | } | |
280 | ||
281 | /** | |
282 | * sysfs_get_inode - get inode for sysfs_dirent | |
283 | * @sb: super block | |
284 | * @sd: sysfs_dirent to allocate inode for | |
285 | * | |
286 | * Get inode for @sd. If such inode doesn't exist, a new inode | |
287 | * is allocated and basics are initialized. New inode is | |
288 | * returned locked. | |
289 | * | |
290 | * LOCKING: | |
291 | * Kernel thread context (may sleep). | |
292 | * | |
293 | * RETURNS: | |
294 | * Pointer to allocated inode on success, NULL on failure. | |
295 | */ | |
296 | struct inode *sysfs_get_inode(struct super_block *sb, struct sysfs_dirent *sd) | |
297 | { | |
298 | struct inode *inode; | |
299 | ||
300 | inode = iget_locked(sb, sd->s_ino); | |
301 | if (inode && (inode->i_state & I_NEW)) | |
302 | sysfs_init_inode(sd, inode); | |
303 | ||
304 | return inode; | |
305 | } | |
306 | ||
307 | /* | |
308 | * The sysfs_dirent serves as both an inode and a directory entry for sysfs. | |
309 | * To prevent the sysfs inode numbers from being freed prematurely we take a | |
310 | * reference to sysfs_dirent from the sysfs inode. A | |
311 | * super_operations.evict_inode() implementation is needed to drop that | |
312 | * reference upon inode destruction. | |
313 | */ | |
314 | void sysfs_evict_inode(struct inode *inode) | |
315 | { | |
316 | struct sysfs_dirent *sd = inode->i_private; | |
317 | ||
318 | truncate_inode_pages(&inode->i_data, 0); | |
319 | clear_inode(inode); | |
320 | kernfs_put(sd); | |
321 | } | |
322 | ||
323 | int sysfs_permission(struct inode *inode, int mask) | |
324 | { | |
325 | struct sysfs_dirent *sd; | |
326 | ||
327 | if (mask & MAY_NOT_BLOCK) | |
328 | return -ECHILD; | |
329 | ||
330 | sd = inode->i_private; | |
331 | ||
332 | mutex_lock(&sysfs_mutex); | |
333 | sysfs_refresh_inode(sd, inode); | |
334 | mutex_unlock(&sysfs_mutex); | |
335 | ||
336 | return generic_permission(inode, mask); | |
337 | } |