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
;
58 ktime_get_real_ts(&iattrs
->ia_atime
);
59 iattrs
->ia_mtime
= iattrs
->ia_atime
;
60 iattrs
->ia_ctime
= iattrs
->ia_atime
;
62 simple_xattrs_init(&kn
->iattr
->xattrs
);
65 mutex_unlock(&iattr_mutex
);
69 static int __kernfs_setattr(struct kernfs_node
*kn
, const struct iattr
*iattr
)
71 struct kernfs_iattrs
*attrs
;
73 unsigned int ia_valid
= iattr
->ia_valid
;
75 attrs
= kernfs_iattrs(kn
);
79 iattrs
= &attrs
->ia_iattr
;
81 if (ia_valid
& ATTR_UID
)
82 iattrs
->ia_uid
= iattr
->ia_uid
;
83 if (ia_valid
& ATTR_GID
)
84 iattrs
->ia_gid
= iattr
->ia_gid
;
85 if (ia_valid
& ATTR_ATIME
)
86 iattrs
->ia_atime
= iattr
->ia_atime
;
87 if (ia_valid
& ATTR_MTIME
)
88 iattrs
->ia_mtime
= iattr
->ia_mtime
;
89 if (ia_valid
& ATTR_CTIME
)
90 iattrs
->ia_ctime
= iattr
->ia_ctime
;
91 if (ia_valid
& ATTR_MODE
) {
92 umode_t mode
= iattr
->ia_mode
;
93 iattrs
->ia_mode
= kn
->mode
= mode
;
99 * kernfs_setattr - set iattr on a node
101 * @iattr: iattr to set
103 * Returns 0 on success, -errno on failure.
105 int kernfs_setattr(struct kernfs_node
*kn
, const struct iattr
*iattr
)
109 mutex_lock(&kernfs_mutex
);
110 ret
= __kernfs_setattr(kn
, iattr
);
111 mutex_unlock(&kernfs_mutex
);
115 int kernfs_iop_setattr(struct dentry
*dentry
, struct iattr
*iattr
)
117 struct inode
*inode
= d_inode(dentry
);
118 struct kernfs_node
*kn
= dentry
->d_fsdata
;
124 mutex_lock(&kernfs_mutex
);
125 error
= inode_change_ok(inode
, iattr
);
129 error
= __kernfs_setattr(kn
, iattr
);
133 /* this ignores size changes */
134 setattr_copy(inode
, iattr
);
137 mutex_unlock(&kernfs_mutex
);
141 static int kernfs_node_setsecdata(struct kernfs_node
*kn
, void **secdata
,
144 struct kernfs_iattrs
*attrs
;
146 size_t old_secdata_len
;
148 attrs
= kernfs_iattrs(kn
);
152 old_secdata
= attrs
->ia_secdata
;
153 old_secdata_len
= attrs
->ia_secdata_len
;
155 attrs
->ia_secdata
= *secdata
;
156 attrs
->ia_secdata_len
= *secdata_len
;
158 *secdata
= old_secdata
;
159 *secdata_len
= old_secdata_len
;
163 int kernfs_iop_setxattr(struct dentry
*unused
, struct inode
*inode
,
164 const char *name
, const void *value
,
165 size_t size
, int flags
)
167 struct kernfs_node
*kn
= inode
->i_private
;
168 struct kernfs_iattrs
*attrs
;
173 attrs
= kernfs_iattrs(kn
);
177 if (!strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
)) {
178 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
179 error
= security_inode_setsecurity(inode
, suffix
,
183 error
= security_inode_getsecctx(inode
,
184 &secdata
, &secdata_len
);
188 mutex_lock(&kernfs_mutex
);
189 error
= kernfs_node_setsecdata(kn
, &secdata
, &secdata_len
);
190 mutex_unlock(&kernfs_mutex
);
193 security_release_secctx(secdata
, secdata_len
);
195 } else if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
)) {
196 return simple_xattr_set(&attrs
->xattrs
, name
, value
, size
,
203 int kernfs_iop_removexattr(struct dentry
*dentry
, const char *name
)
205 struct kernfs_node
*kn
= dentry
->d_fsdata
;
206 struct kernfs_iattrs
*attrs
;
208 attrs
= kernfs_iattrs(kn
);
212 return simple_xattr_set(&attrs
->xattrs
, name
, NULL
, 0, XATTR_REPLACE
);
215 ssize_t
kernfs_iop_getxattr(struct dentry
*unused
, struct inode
*inode
,
216 const char *name
, void *buf
, size_t size
)
218 struct kernfs_node
*kn
= inode
->i_private
;
219 struct kernfs_iattrs
*attrs
;
221 attrs
= kernfs_iattrs(kn
);
225 return simple_xattr_get(&attrs
->xattrs
, name
, buf
, size
);
228 ssize_t
kernfs_iop_listxattr(struct dentry
*dentry
, char *buf
, size_t size
)
230 struct kernfs_node
*kn
= dentry
->d_fsdata
;
231 struct kernfs_iattrs
*attrs
;
233 attrs
= kernfs_iattrs(kn
);
237 return simple_xattr_list(d_inode(dentry
), &attrs
->xattrs
, buf
, size
);
240 static inline void set_default_inode_attr(struct inode
*inode
, umode_t mode
)
242 inode
->i_mode
= mode
;
243 inode
->i_atime
= inode
->i_mtime
=
244 inode
->i_ctime
= current_fs_time(inode
->i_sb
);
247 static inline void set_inode_attr(struct inode
*inode
, struct iattr
*iattr
)
249 struct super_block
*sb
= inode
->i_sb
;
250 inode
->i_uid
= iattr
->ia_uid
;
251 inode
->i_gid
= iattr
->ia_gid
;
252 inode
->i_atime
= timespec_trunc(iattr
->ia_atime
, sb
->s_time_gran
);
253 inode
->i_mtime
= timespec_trunc(iattr
->ia_mtime
, sb
->s_time_gran
);
254 inode
->i_ctime
= timespec_trunc(iattr
->ia_ctime
, sb
->s_time_gran
);
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
= d_inode(dentry
);
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_op
= &kernfs_iops
;
297 set_default_inode_attr(inode
, kn
->mode
);
298 kernfs_refresh_inode(kn
, inode
);
300 /* initialize inode according to type */
301 switch (kernfs_type(kn
)) {
303 inode
->i_op
= &kernfs_dir_iops
;
304 inode
->i_fop
= &kernfs_dir_fops
;
305 if (kn
->flags
& KERNFS_EMPTY_DIR
)
306 make_empty_dir_inode(inode
);
309 inode
->i_size
= kn
->attr
.size
;
310 inode
->i_fop
= &kernfs_file_fops
;
313 inode
->i_op
= &kernfs_symlink_iops
;
319 unlock_new_inode(inode
);
323 * kernfs_get_inode - get inode for kernfs_node
325 * @kn: kernfs_node to allocate inode for
327 * Get inode for @kn. If such inode doesn't exist, a new inode is
328 * allocated and basics are initialized. New inode is returned
332 * Kernel thread context (may sleep).
335 * Pointer to allocated inode on success, NULL on failure.
337 struct inode
*kernfs_get_inode(struct super_block
*sb
, struct kernfs_node
*kn
)
341 inode
= iget_locked(sb
, kn
->ino
);
342 if (inode
&& (inode
->i_state
& I_NEW
))
343 kernfs_init_inode(kn
, inode
);
349 * The kernfs_node serves as both an inode and a directory entry for
350 * kernfs. To prevent the kernfs inode numbers from being freed
351 * prematurely we take a reference to kernfs_node from the kernfs inode. A
352 * super_operations.evict_inode() implementation is needed to drop that
353 * reference upon inode destruction.
355 void kernfs_evict_inode(struct inode
*inode
)
357 struct kernfs_node
*kn
= inode
->i_private
;
359 truncate_inode_pages_final(&inode
->i_data
);
364 int kernfs_iop_permission(struct inode
*inode
, int mask
)
366 struct kernfs_node
*kn
;
368 if (mask
& MAY_NOT_BLOCK
)
371 kn
= inode
->i_private
;
373 mutex_lock(&kernfs_mutex
);
374 kernfs_refresh_inode(kn
, inode
);
375 mutex_unlock(&kernfs_mutex
);
377 return generic_permission(inode
, mask
);