2 * fs/kernfs/inode.c - kernfs inode implementation
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>
8 * This file is released under the GPLv2.
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>
19 #include "kernfs-internal.h"
21 static const struct address_space_operations kernfs_aops = {
22 .readpage = simple_readpage,
23 .write_begin = simple_write_begin,
24 .write_end = simple_write_end,
27 static struct backing_dev_info kernfs_bdi = {
29 .ra_pages = 0, /* No readahead */
30 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
33 static const struct inode_operations kernfs_iops = {
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,
43 void __init kernfs_inode_init(void)
45 if (bdi_init(&kernfs_bdi))
46 panic("failed to init kernfs_bdi");
49 static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
56 kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL);
59 iattrs = &kn->iattr->ia_iattr;
61 /* assign default attributes */
62 iattrs->ia_mode = kn->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;
67 simple_xattrs_init(&kn->iattr->xattrs);
72 static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
74 struct kernfs_iattrs *attrs;
76 unsigned int ia_valid = iattr->ia_valid;
78 attrs = kernfs_iattrs(kn);
82 iattrs = &attrs->ia_iattr;
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 = kn->mode = mode;
102 * kernfs_setattr - set iattr on a node
104 * @iattr: iattr to set
106 * Returns 0 on success, -errno on failure.
108 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
112 mutex_lock(&kernfs_mutex);
113 ret = __kernfs_setattr(kn, iattr);
114 mutex_unlock(&kernfs_mutex);
118 int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
120 struct inode *inode = dentry->d_inode;
121 struct kernfs_node *kn = dentry->d_fsdata;
127 mutex_lock(&kernfs_mutex);
128 error = inode_change_ok(inode, iattr);
132 error = __kernfs_setattr(kn, iattr);
136 /* this ignores size changes */
137 setattr_copy(inode, iattr);
140 mutex_unlock(&kernfs_mutex);
144 static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata,
147 struct kernfs_iattrs *attrs;
149 size_t old_secdata_len;
151 attrs = kernfs_iattrs(kn);
155 old_secdata = attrs->ia_secdata;
156 old_secdata_len = attrs->ia_secdata_len;
158 attrs->ia_secdata = *secdata;
159 attrs->ia_secdata_len = *secdata_len;
161 *secdata = old_secdata;
162 *secdata_len = old_secdata_len;
166 int kernfs_iop_setxattr(struct dentry *dentry, const char *name,
167 const void *value, size_t size, int flags)
169 struct kernfs_node *kn = dentry->d_fsdata;
170 struct kernfs_iattrs *attrs;
175 attrs = kernfs_iattrs(kn);
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,
185 error = security_inode_getsecctx(dentry->d_inode,
186 &secdata, &secdata_len);
190 mutex_lock(&kernfs_mutex);
191 error = kernfs_node_setsecdata(kn, &secdata, &secdata_len);
192 mutex_unlock(&kernfs_mutex);
195 security_release_secctx(secdata, secdata_len);
197 } else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
198 return simple_xattr_set(&attrs->xattrs, name, value, size,
205 int kernfs_iop_removexattr(struct dentry *dentry, const char *name)
207 struct kernfs_node *kn = dentry->d_fsdata;
208 struct kernfs_iattrs *attrs;
210 attrs = kernfs_iattrs(kn);
214 return simple_xattr_remove(&attrs->xattrs, name);
217 ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf,
220 struct kernfs_node *kn = dentry->d_fsdata;
221 struct kernfs_iattrs *attrs;
223 attrs = kernfs_iattrs(kn);
227 return simple_xattr_get(&attrs->xattrs, name, buf, size);
230 ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
232 struct kernfs_node *kn = dentry->d_fsdata;
233 struct kernfs_iattrs *attrs;
235 attrs = kernfs_iattrs(kn);
239 return simple_xattr_list(&attrs->xattrs, buf, size);
242 static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
244 inode->i_mode = mode;
245 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
248 static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
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;
257 static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
259 struct kernfs_iattrs *attrs = kn->iattr;
261 inode->i_mode = kn->mode;
264 * kernfs_node has non-default attributes get them from
265 * persistent copy in kernfs_node.
267 set_inode_attr(inode, &attrs->ia_iattr);
268 security_inode_notifysecctx(inode, attrs->ia_secdata,
269 attrs->ia_secdata_len);
272 if (kernfs_type(kn) == KERNFS_DIR)
273 set_nlink(inode, kn->dir.subdirs + 2);
276 int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
279 struct kernfs_node *kn = dentry->d_fsdata;
280 struct inode *inode = dentry->d_inode;
282 mutex_lock(&kernfs_mutex);
283 kernfs_refresh_inode(kn, inode);
284 mutex_unlock(&kernfs_mutex);
286 generic_fillattr(inode, stat);
290 static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
293 inode->i_private = kn;
294 inode->i_mapping->a_ops = &kernfs_aops;
295 inode->i_mapping->backing_dev_info = &kernfs_bdi;
296 inode->i_op = &kernfs_iops;
298 set_default_inode_attr(inode, kn->mode);
299 kernfs_refresh_inode(kn, inode);
301 /* initialize inode according to type */
302 switch (kernfs_type(kn)) {
304 inode->i_op = &kernfs_dir_iops;
305 inode->i_fop = &kernfs_dir_fops;
308 inode->i_size = kn->attr.size;
309 inode->i_fop = &kernfs_file_fops;
312 inode->i_op = &kernfs_symlink_iops;
318 unlock_new_inode(inode);
322 * kernfs_get_inode - get inode for kernfs_node
324 * @kn: kernfs_node to allocate inode for
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
331 * Kernel thread context (may sleep).
334 * Pointer to allocated inode on success, NULL on failure.
336 struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
340 inode = iget_locked(sb, kn->ino);
341 if (inode && (inode->i_state & I_NEW))
342 kernfs_init_inode(kn, inode);
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
351 * super_operations.evict_inode() implementation is needed to drop that
352 * reference upon inode destruction.
354 void kernfs_evict_inode(struct inode *inode)
356 struct kernfs_node *kn = inode->i_private;
358 truncate_inode_pages(&inode->i_data, 0);
363 int kernfs_iop_permission(struct inode *inode, int mask)
365 struct kernfs_node *kn;
367 if (mask & MAY_NOT_BLOCK)
370 kn = inode->i_private;
372 mutex_lock(&kernfs_mutex);
373 kernfs_refresh_inode(kn, inode);
374 mutex_unlock(&kernfs_mutex);
376 return generic_permission(inode, mask);