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 const struct inode_operations kernfs_iops
= {
28 .permission
= kernfs_iop_permission
,
29 .setattr
= kernfs_iop_setattr
,
30 .getattr
= kernfs_iop_getattr
,
31 .setxattr
= kernfs_iop_setxattr
,
32 .removexattr
= kernfs_iop_removexattr
,
33 .getxattr
= kernfs_iop_getxattr
,
34 .listxattr
= kernfs_iop_listxattr
,
37 static struct kernfs_iattrs
*kernfs_iattrs(struct kernfs_node
*kn
)
39 static DEFINE_MUTEX(iattr_mutex
);
40 struct kernfs_iattrs
*ret
;
43 mutex_lock(&iattr_mutex
);
48 kn
->iattr
= kzalloc(sizeof(struct kernfs_iattrs
), GFP_KERNEL
);
51 iattrs
= &kn
->iattr
->ia_iattr
;
53 /* assign default attributes */
54 iattrs
->ia_mode
= kn
->mode
;
55 iattrs
->ia_uid
= GLOBAL_ROOT_UID
;
56 iattrs
->ia_gid
= GLOBAL_ROOT_GID
;
57 iattrs
->ia_atime
= iattrs
->ia_mtime
= iattrs
->ia_ctime
= CURRENT_TIME
;
59 simple_xattrs_init(&kn
->iattr
->xattrs
);
62 mutex_unlock(&iattr_mutex
);
66 static int __kernfs_setattr(struct kernfs_node
*kn
, const struct iattr
*iattr
)
68 struct kernfs_iattrs
*attrs
;
70 unsigned int ia_valid
= iattr
->ia_valid
;
72 attrs
= kernfs_iattrs(kn
);
76 iattrs
= &attrs
->ia_iattr
;
78 if (ia_valid
& ATTR_UID
)
79 iattrs
->ia_uid
= iattr
->ia_uid
;
80 if (ia_valid
& ATTR_GID
)
81 iattrs
->ia_gid
= iattr
->ia_gid
;
82 if (ia_valid
& ATTR_ATIME
)
83 iattrs
->ia_atime
= iattr
->ia_atime
;
84 if (ia_valid
& ATTR_MTIME
)
85 iattrs
->ia_mtime
= iattr
->ia_mtime
;
86 if (ia_valid
& ATTR_CTIME
)
87 iattrs
->ia_ctime
= iattr
->ia_ctime
;
88 if (ia_valid
& ATTR_MODE
) {
89 umode_t mode
= iattr
->ia_mode
;
90 iattrs
->ia_mode
= kn
->mode
= mode
;
96 * kernfs_setattr - set iattr on a node
98 * @iattr: iattr to set
100 * Returns 0 on success, -errno on failure.
102 int kernfs_setattr(struct kernfs_node
*kn
, const struct iattr
*iattr
)
106 mutex_lock(&kernfs_mutex
);
107 ret
= __kernfs_setattr(kn
, iattr
);
108 mutex_unlock(&kernfs_mutex
);
112 int kernfs_iop_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
114 struct inode
*inode
= dentry
->d_inode
;
115 struct kernfs_node
*kn
= dentry
->d_fsdata
;
121 mutex_lock(&kernfs_mutex
);
122 error
= inode_change_ok(inode
, iattr
);
126 error
= __kernfs_setattr(kn
, iattr
);
130 /* this ignores size changes */
131 setattr_copy(inode
, iattr
);
134 mutex_unlock(&kernfs_mutex
);
138 static int kernfs_node_setsecdata(struct kernfs_node
*kn
, void **secdata
,
141 struct kernfs_iattrs
*attrs
;
143 size_t old_secdata_len
;
145 attrs
= kernfs_iattrs(kn
);
149 old_secdata
= attrs
->ia_secdata
;
150 old_secdata_len
= attrs
->ia_secdata_len
;
152 attrs
->ia_secdata
= *secdata
;
153 attrs
->ia_secdata_len
= *secdata_len
;
155 *secdata
= old_secdata
;
156 *secdata_len
= old_secdata_len
;
160 int kernfs_iop_setxattr(struct dentry
*dentry
, const char *name
,
161 const void *value
, size_t size
, int flags
)
163 struct kernfs_node
*kn
= dentry
->d_fsdata
;
164 struct kernfs_iattrs
*attrs
;
169 attrs
= kernfs_iattrs(kn
);
173 if (!strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
)) {
174 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
175 error
= security_inode_setsecurity(dentry
->d_inode
, suffix
,
179 error
= security_inode_getsecctx(dentry
->d_inode
,
180 &secdata
, &secdata_len
);
184 mutex_lock(&kernfs_mutex
);
185 error
= kernfs_node_setsecdata(kn
, &secdata
, &secdata_len
);
186 mutex_unlock(&kernfs_mutex
);
189 security_release_secctx(secdata
, secdata_len
);
191 } else if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
)) {
192 return simple_xattr_set(&attrs
->xattrs
, name
, value
, size
,
199 int kernfs_iop_removexattr(struct dentry
*dentry
, const char *name
)
201 struct kernfs_node
*kn
= dentry
->d_fsdata
;
202 struct kernfs_iattrs
*attrs
;
204 attrs
= kernfs_iattrs(kn
);
208 return simple_xattr_remove(&attrs
->xattrs
, name
);
211 ssize_t
kernfs_iop_getxattr(struct dentry
*dentry
, const char *name
, void *buf
,
214 struct kernfs_node
*kn
= dentry
->d_fsdata
;
215 struct kernfs_iattrs
*attrs
;
217 attrs
= kernfs_iattrs(kn
);
221 return simple_xattr_get(&attrs
->xattrs
, name
, buf
, size
);
224 ssize_t
kernfs_iop_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
226 struct kernfs_node
*kn
= dentry
->d_fsdata
;
227 struct kernfs_iattrs
*attrs
;
229 attrs
= kernfs_iattrs(kn
);
233 return simple_xattr_list(&attrs
->xattrs
, buf
, size
);
236 static inline void set_default_inode_attr(struct inode
*inode
, umode_t mode
)
238 inode
->i_mode
= mode
;
239 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
242 static inline void set_inode_attr(struct inode
*inode
, struct iattr
*iattr
)
244 inode
->i_uid
= iattr
->ia_uid
;
245 inode
->i_gid
= iattr
->ia_gid
;
246 inode
->i_atime
= iattr
->ia_atime
;
247 inode
->i_mtime
= iattr
->ia_mtime
;
248 inode
->i_ctime
= iattr
->ia_ctime
;
251 static void kernfs_refresh_inode(struct kernfs_node
*kn
, struct inode
*inode
)
253 struct kernfs_iattrs
*attrs
= kn
->iattr
;
255 inode
->i_mode
= kn
->mode
;
258 * kernfs_node has non-default attributes get them from
259 * persistent copy in kernfs_node.
261 set_inode_attr(inode
, &attrs
->ia_iattr
);
262 security_inode_notifysecctx(inode
, attrs
->ia_secdata
,
263 attrs
->ia_secdata_len
);
266 if (kernfs_type(kn
) == KERNFS_DIR
)
267 set_nlink(inode
, kn
->dir
.subdirs
+ 2);
270 int kernfs_iop_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
273 struct kernfs_node
*kn
= dentry
->d_fsdata
;
274 struct inode
*inode
= dentry
->d_inode
;
276 mutex_lock(&kernfs_mutex
);
277 kernfs_refresh_inode(kn
, inode
);
278 mutex_unlock(&kernfs_mutex
);
280 generic_fillattr(inode
, stat
);
284 static void kernfs_init_inode(struct kernfs_node
*kn
, struct inode
*inode
)
287 inode
->i_private
= kn
;
288 inode
->i_mapping
->a_ops
= &kernfs_aops
;
289 inode
->i_op
= &kernfs_iops
;
291 set_default_inode_attr(inode
, kn
->mode
);
292 kernfs_refresh_inode(kn
, inode
);
294 /* initialize inode according to type */
295 switch (kernfs_type(kn
)) {
297 inode
->i_op
= &kernfs_dir_iops
;
298 inode
->i_fop
= &kernfs_dir_fops
;
301 inode
->i_size
= kn
->attr
.size
;
302 inode
->i_fop
= &kernfs_file_fops
;
305 inode
->i_op
= &kernfs_symlink_iops
;
311 unlock_new_inode(inode
);
315 * kernfs_get_inode - get inode for kernfs_node
317 * @kn: kernfs_node to allocate inode for
319 * Get inode for @kn. If such inode doesn't exist, a new inode is
320 * allocated and basics are initialized. New inode is returned
324 * Kernel thread context (may sleep).
327 * Pointer to allocated inode on success, NULL on failure.
329 struct inode
*kernfs_get_inode(struct super_block
*sb
, struct kernfs_node
*kn
)
333 inode
= iget_locked(sb
, kn
->ino
);
334 if (inode
&& (inode
->i_state
& I_NEW
))
335 kernfs_init_inode(kn
, inode
);
341 * The kernfs_node serves as both an inode and a directory entry for
342 * kernfs. To prevent the kernfs inode numbers from being freed
343 * prematurely we take a reference to kernfs_node from the kernfs inode. A
344 * super_operations.evict_inode() implementation is needed to drop that
345 * reference upon inode destruction.
347 void kernfs_evict_inode(struct inode
*inode
)
349 struct kernfs_node
*kn
= inode
->i_private
;
351 truncate_inode_pages_final(&inode
->i_data
);
356 int kernfs_iop_permission(struct inode
*inode
, int mask
)
358 struct kernfs_node
*kn
;
360 if (mask
& MAY_NOT_BLOCK
)
363 kn
= inode
->i_private
;
365 mutex_lock(&kernfs_mutex
);
366 kernfs_refresh_inode(kn
, inode
);
367 mutex_unlock(&kernfs_mutex
);
369 return generic_permission(inode
, mask
);