]>
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 | ||
a797bfc3 | 21 | static const struct address_space_operations kernfs_aops = { |
ffed24e2 TH |
22 | .readpage = simple_readpage, |
23 | .write_begin = simple_write_begin, | |
24 | .write_end = simple_write_end, | |
25 | }; | |
26 | ||
a797bfc3 TH |
27 | static struct backing_dev_info kernfs_bdi = { |
28 | .name = "kernfs", | |
ffed24e2 TH |
29 | .ra_pages = 0, /* No readahead */ |
30 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK, | |
31 | }; | |
32 | ||
a797bfc3 | 33 | static const struct inode_operations kernfs_iops = { |
c637b8ac TH |
34 | .permission = kernfs_iop_permission, |
35 | .setattr = kernfs_iop_setattr, | |
36 | .getattr = kernfs_iop_getattr, | |
37 | .setxattr = kernfs_iop_setxattr, | |
38 | .removexattr = kernfs_iop_removexattr, | |
39 | .getxattr = kernfs_iop_getxattr, | |
40 | .listxattr = kernfs_iop_listxattr, | |
ffed24e2 TH |
41 | }; |
42 | ||
c637b8ac | 43 | void __init kernfs_inode_init(void) |
ffed24e2 | 44 | { |
a797bfc3 TH |
45 | if (bdi_init(&kernfs_bdi)) |
46 | panic("failed to init kernfs_bdi"); | |
ffed24e2 TH |
47 | } |
48 | ||
c525aadd | 49 | static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn) |
ffed24e2 | 50 | { |
ffed24e2 TH |
51 | struct iattr *iattrs; |
52 | ||
adc5e8b5 TH |
53 | if (kn->iattr) |
54 | return kn->iattr; | |
9a8049af | 55 | |
c525aadd | 56 | kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL); |
adc5e8b5 | 57 | if (!kn->iattr) |
ffed24e2 | 58 | return NULL; |
adc5e8b5 | 59 | iattrs = &kn->iattr->ia_iattr; |
ffed24e2 TH |
60 | |
61 | /* assign default attributes */ | |
adc5e8b5 | 62 | iattrs->ia_mode = kn->mode; |
ffed24e2 TH |
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 | ||
adc5e8b5 | 67 | simple_xattrs_init(&kn->iattr->xattrs); |
2322392b | 68 | |
adc5e8b5 | 69 | return kn->iattr; |
ffed24e2 TH |
70 | } |
71 | ||
324a56e1 | 72 | static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) |
ffed24e2 | 73 | { |
c525aadd | 74 | struct kernfs_iattrs *attrs; |
ffed24e2 TH |
75 | struct iattr *iattrs; |
76 | unsigned int ia_valid = iattr->ia_valid; | |
77 | ||
c525aadd | 78 | attrs = kernfs_iattrs(kn); |
9a8049af TH |
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; | |
adc5e8b5 | 96 | iattrs->ia_mode = kn->mode = mode; |
ffed24e2 TH |
97 | } |
98 | return 0; | |
99 | } | |
100 | ||
101 | /** | |
102 | * kernfs_setattr - set iattr on a node | |
324a56e1 | 103 | * @kn: target node |
ffed24e2 TH |
104 | * @iattr: iattr to set |
105 | * | |
106 | * Returns 0 on success, -errno on failure. | |
107 | */ | |
324a56e1 | 108 | int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr) |
ffed24e2 TH |
109 | { |
110 | int ret; | |
111 | ||
a797bfc3 | 112 | mutex_lock(&kernfs_mutex); |
324a56e1 | 113 | ret = __kernfs_setattr(kn, iattr); |
a797bfc3 | 114 | mutex_unlock(&kernfs_mutex); |
ffed24e2 TH |
115 | return ret; |
116 | } | |
117 | ||
c637b8ac | 118 | int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr) |
ffed24e2 TH |
119 | { |
120 | struct inode *inode = dentry->d_inode; | |
324a56e1 | 121 | struct kernfs_node *kn = dentry->d_fsdata; |
ffed24e2 TH |
122 | int error; |
123 | ||
324a56e1 | 124 | if (!kn) |
ffed24e2 TH |
125 | return -EINVAL; |
126 | ||
a797bfc3 | 127 | mutex_lock(&kernfs_mutex); |
ffed24e2 TH |
128 | error = inode_change_ok(inode, iattr); |
129 | if (error) | |
130 | goto out; | |
131 | ||
324a56e1 | 132 | error = __kernfs_setattr(kn, iattr); |
ffed24e2 TH |
133 | if (error) |
134 | goto out; | |
135 | ||
136 | /* this ignores size changes */ | |
137 | setattr_copy(inode, iattr); | |
138 | ||
139 | out: | |
a797bfc3 | 140 | mutex_unlock(&kernfs_mutex); |
ffed24e2 TH |
141 | return error; |
142 | } | |
143 | ||
c637b8ac TH |
144 | static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata, |
145 | u32 *secdata_len) | |
ffed24e2 | 146 | { |
c525aadd | 147 | struct kernfs_iattrs *attrs; |
ffed24e2 TH |
148 | void *old_secdata; |
149 | size_t old_secdata_len; | |
150 | ||
c525aadd | 151 | attrs = kernfs_iattrs(kn); |
9a8049af TH |
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 | ||
c637b8ac TH |
166 | int kernfs_iop_setxattr(struct dentry *dentry, const char *name, |
167 | const void *value, size_t size, int flags) | |
ffed24e2 | 168 | { |
324a56e1 | 169 | struct kernfs_node *kn = dentry->d_fsdata; |
c525aadd | 170 | struct kernfs_iattrs *attrs; |
ffed24e2 TH |
171 | void *secdata; |
172 | int error; | |
173 | u32 secdata_len = 0; | |
174 | ||
c525aadd | 175 | attrs = kernfs_iattrs(kn); |
2322392b TH |
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 | 189 | |
a797bfc3 | 190 | mutex_lock(&kernfs_mutex); |
c637b8ac | 191 | error = kernfs_node_setsecdata(kn, &secdata, &secdata_len); |
a797bfc3 | 192 | mutex_unlock(&kernfs_mutex); |
ffed24e2 TH |
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 | ||
c637b8ac | 205 | int kernfs_iop_removexattr(struct dentry *dentry, const char *name) |
2322392b | 206 | { |
324a56e1 | 207 | struct kernfs_node *kn = dentry->d_fsdata; |
c525aadd | 208 | struct kernfs_iattrs *attrs; |
2322392b | 209 | |
c525aadd | 210 | attrs = kernfs_iattrs(kn); |
2322392b TH |
211 | if (!attrs) |
212 | return -ENOMEM; | |
213 | ||
214 | return simple_xattr_remove(&attrs->xattrs, name); | |
215 | } | |
216 | ||
c637b8ac TH |
217 | ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf, |
218 | size_t size) | |
2322392b | 219 | { |
324a56e1 | 220 | struct kernfs_node *kn = dentry->d_fsdata; |
c525aadd | 221 | struct kernfs_iattrs *attrs; |
2322392b | 222 | |
c525aadd | 223 | attrs = kernfs_iattrs(kn); |
2322392b TH |
224 | if (!attrs) |
225 | return -ENOMEM; | |
226 | ||
227 | return simple_xattr_get(&attrs->xattrs, name, buf, size); | |
228 | } | |
229 | ||
c637b8ac | 230 | ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size) |
2322392b | 231 | { |
324a56e1 | 232 | struct kernfs_node *kn = dentry->d_fsdata; |
c525aadd | 233 | struct kernfs_iattrs *attrs; |
2322392b | 234 | |
c525aadd | 235 | attrs = kernfs_iattrs(kn); |
2322392b TH |
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 | ||
c637b8ac | 257 | static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode) |
ffed24e2 | 258 | { |
c525aadd | 259 | struct kernfs_iattrs *attrs = kn->iattr; |
ffed24e2 | 260 | |
adc5e8b5 | 261 | inode->i_mode = kn->mode; |
9a8049af | 262 | if (attrs) { |
324a56e1 TH |
263 | /* |
264 | * kernfs_node has non-default attributes get them from | |
265 | * persistent copy in kernfs_node. | |
ffed24e2 | 266 | */ |
9a8049af TH |
267 | set_inode_attr(inode, &attrs->ia_iattr); |
268 | security_inode_notifysecctx(inode, attrs->ia_secdata, | |
269 | attrs->ia_secdata_len); | |
ffed24e2 TH |
270 | } |
271 | ||
df23fc39 | 272 | if (kernfs_type(kn) == KERNFS_DIR) |
adc5e8b5 | 273 | set_nlink(inode, kn->dir.subdirs + 2); |
ffed24e2 TH |
274 | } |
275 | ||
c637b8ac TH |
276 | int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry, |
277 | struct kstat *stat) | |
ffed24e2 | 278 | { |
324a56e1 | 279 | struct kernfs_node *kn = dentry->d_fsdata; |
ffed24e2 TH |
280 | struct inode *inode = dentry->d_inode; |
281 | ||
a797bfc3 | 282 | mutex_lock(&kernfs_mutex); |
c637b8ac | 283 | kernfs_refresh_inode(kn, inode); |
a797bfc3 | 284 | mutex_unlock(&kernfs_mutex); |
ffed24e2 TH |
285 | |
286 | generic_fillattr(inode, stat); | |
287 | return 0; | |
288 | } | |
289 | ||
c637b8ac | 290 | static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) |
ffed24e2 | 291 | { |
324a56e1 TH |
292 | kernfs_get(kn); |
293 | inode->i_private = kn; | |
a797bfc3 TH |
294 | inode->i_mapping->a_ops = &kernfs_aops; |
295 | inode->i_mapping->backing_dev_info = &kernfs_bdi; | |
296 | inode->i_op = &kernfs_iops; | |
ffed24e2 | 297 | |
adc5e8b5 | 298 | set_default_inode_attr(inode, kn->mode); |
c637b8ac | 299 | kernfs_refresh_inode(kn, inode); |
ffed24e2 TH |
300 | |
301 | /* initialize inode according to type */ | |
df23fc39 TH |
302 | switch (kernfs_type(kn)) { |
303 | case KERNFS_DIR: | |
a797bfc3 TH |
304 | inode->i_op = &kernfs_dir_iops; |
305 | inode->i_fop = &kernfs_dir_fops; | |
ffed24e2 | 306 | break; |
df23fc39 | 307 | case KERNFS_FILE: |
adc5e8b5 | 308 | inode->i_size = kn->attr.size; |
a797bfc3 | 309 | inode->i_fop = &kernfs_file_fops; |
ffed24e2 | 310 | break; |
df23fc39 | 311 | case KERNFS_LINK: |
a797bfc3 | 312 | inode->i_op = &kernfs_symlink_iops; |
ffed24e2 TH |
313 | break; |
314 | default: | |
315 | BUG(); | |
316 | } | |
317 | ||
318 | unlock_new_inode(inode); | |
319 | } | |
320 | ||
321 | /** | |
c637b8ac | 322 | * kernfs_get_inode - get inode for kernfs_node |
ffed24e2 | 323 | * @sb: super block |
324a56e1 | 324 | * @kn: kernfs_node to allocate inode for |
ffed24e2 | 325 | * |
324a56e1 TH |
326 | * Get inode for @kn. If such inode doesn't exist, a new inode is |
327 | * allocated and basics are initialized. New inode is returned | |
328 | * locked. | |
ffed24e2 TH |
329 | * |
330 | * LOCKING: | |
331 | * Kernel thread context (may sleep). | |
332 | * | |
333 | * RETURNS: | |
334 | * Pointer to allocated inode on success, NULL on failure. | |
335 | */ | |
c637b8ac | 336 | struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn) |
ffed24e2 TH |
337 | { |
338 | struct inode *inode; | |
339 | ||
adc5e8b5 | 340 | inode = iget_locked(sb, kn->ino); |
ffed24e2 | 341 | if (inode && (inode->i_state & I_NEW)) |
c637b8ac | 342 | kernfs_init_inode(kn, inode); |
ffed24e2 TH |
343 | |
344 | return inode; | |
345 | } | |
346 | ||
347 | /* | |
c637b8ac TH |
348 | * The kernfs_node serves as both an inode and a directory entry for |
349 | * kernfs. To prevent the kernfs inode numbers from being freed | |
350 | * prematurely we take a reference to kernfs_node from the kernfs inode. A | |
ffed24e2 TH |
351 | * super_operations.evict_inode() implementation is needed to drop that |
352 | * reference upon inode destruction. | |
353 | */ | |
c637b8ac | 354 | void kernfs_evict_inode(struct inode *inode) |
ffed24e2 | 355 | { |
324a56e1 | 356 | struct kernfs_node *kn = inode->i_private; |
ffed24e2 | 357 | |
91b0abe3 | 358 | truncate_inode_pages_final(&inode->i_data); |
ffed24e2 | 359 | clear_inode(inode); |
324a56e1 | 360 | kernfs_put(kn); |
ffed24e2 TH |
361 | } |
362 | ||
c637b8ac | 363 | int kernfs_iop_permission(struct inode *inode, int mask) |
ffed24e2 | 364 | { |
324a56e1 | 365 | struct kernfs_node *kn; |
ffed24e2 TH |
366 | |
367 | if (mask & MAY_NOT_BLOCK) | |
368 | return -ECHILD; | |
369 | ||
324a56e1 | 370 | kn = inode->i_private; |
ffed24e2 | 371 | |
a797bfc3 | 372 | mutex_lock(&kernfs_mutex); |
c637b8ac | 373 | kernfs_refresh_inode(kn, inode); |
a797bfc3 | 374 | mutex_unlock(&kernfs_mutex); |
ffed24e2 TH |
375 | |
376 | return generic_permission(inode, mask); | |
377 | } |