blob: e55126f85bd200d1519793cf82ab9127dd41601c [file] [log] [blame]
Tejun Heob8441ed2013-11-24 09:54:58 -05001/*
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 */
Tejun Heoffed24e2013-11-28 14:54:32 -050010
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
Tejun Heoa797bfc2013-12-11 14:11:57 -050021static const struct address_space_operations kernfs_aops = {
Tejun Heoffed24e2013-11-28 14:54:32 -050022 .readpage = simple_readpage,
23 .write_begin = simple_write_begin,
24 .write_end = simple_write_end,
25};
26
Tejun Heoa797bfc2013-12-11 14:11:57 -050027static struct backing_dev_info kernfs_bdi = {
28 .name = "kernfs",
Tejun Heoffed24e2013-11-28 14:54:32 -050029 .ra_pages = 0, /* No readahead */
30 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
31};
32
Tejun Heoa797bfc2013-12-11 14:11:57 -050033static const struct inode_operations kernfs_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -050034 .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,
Tejun Heoffed24e2013-11-28 14:54:32 -050041};
42
Tejun Heoc637b8a2013-12-11 14:11:58 -050043void __init kernfs_inode_init(void)
Tejun Heoffed24e2013-11-28 14:54:32 -050044{
Tejun Heoa797bfc2013-12-11 14:11:57 -050045 if (bdi_init(&kernfs_bdi))
46 panic("failed to init kernfs_bdi");
Tejun Heoffed24e2013-11-28 14:54:32 -050047}
48
Tejun Heoc525aad2013-12-11 14:11:55 -050049static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
Tejun Heoffed24e2013-11-28 14:54:32 -050050{
Tejun Heoffed24e2013-11-28 14:54:32 -050051 struct iattr *iattrs;
52
Tejun Heoadc5e8b2013-12-11 14:11:54 -050053 if (kn->iattr)
54 return kn->iattr;
Tejun Heo9a8049a2013-11-23 17:40:01 -050055
Tejun Heoc525aad2013-12-11 14:11:55 -050056 kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL);
Tejun Heoadc5e8b2013-12-11 14:11:54 -050057 if (!kn->iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -050058 return NULL;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050059 iattrs = &kn->iattr->ia_iattr;
Tejun Heoffed24e2013-11-28 14:54:32 -050060
61 /* assign default attributes */
Tejun Heoadc5e8b2013-12-11 14:11:54 -050062 iattrs->ia_mode = kn->mode;
Tejun Heoffed24e2013-11-28 14:54:32 -050063 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
Tejun Heoadc5e8b2013-12-11 14:11:54 -050067 simple_xattrs_init(&kn->iattr->xattrs);
Tejun Heo23223922013-11-23 17:40:02 -050068
Tejun Heoadc5e8b2013-12-11 14:11:54 -050069 return kn->iattr;
Tejun Heoffed24e2013-11-28 14:54:32 -050070}
71
Tejun Heo324a56e2013-12-11 14:11:53 -050072static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -050073{
Tejun Heoc525aad2013-12-11 14:11:55 -050074 struct kernfs_iattrs *attrs;
Tejun Heoffed24e2013-11-28 14:54:32 -050075 struct iattr *iattrs;
76 unsigned int ia_valid = iattr->ia_valid;
77
Tejun Heoc525aad2013-12-11 14:11:55 -050078 attrs = kernfs_iattrs(kn);
Tejun Heo9a8049a2013-11-23 17:40:01 -050079 if (!attrs)
80 return -ENOMEM;
Tejun Heoffed24e2013-11-28 14:54:32 -050081
Tejun Heo9a8049a2013-11-23 17:40:01 -050082 iattrs = &attrs->ia_iattr;
Tejun Heoffed24e2013-11-28 14:54:32 -050083
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;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050096 iattrs->ia_mode = kn->mode = mode;
Tejun Heoffed24e2013-11-28 14:54:32 -050097 }
98 return 0;
99}
100
101/**
102 * kernfs_setattr - set iattr on a node
Tejun Heo324a56e2013-12-11 14:11:53 -0500103 * @kn: target node
Tejun Heoffed24e2013-11-28 14:54:32 -0500104 * @iattr: iattr to set
105 *
106 * Returns 0 on success, -errno on failure.
107 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500108int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -0500109{
110 int ret;
111
Tejun Heoa797bfc2013-12-11 14:11:57 -0500112 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500113 ret = __kernfs_setattr(kn, iattr);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500114 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500115 return ret;
116}
117
Tejun Heoc637b8a2013-12-11 14:11:58 -0500118int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -0500119{
120 struct inode *inode = dentry->d_inode;
Tejun Heo324a56e2013-12-11 14:11:53 -0500121 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoffed24e2013-11-28 14:54:32 -0500122 int error;
123
Tejun Heo324a56e2013-12-11 14:11:53 -0500124 if (!kn)
Tejun Heoffed24e2013-11-28 14:54:32 -0500125 return -EINVAL;
126
Tejun Heoa797bfc2013-12-11 14:11:57 -0500127 mutex_lock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500128 error = inode_change_ok(inode, iattr);
129 if (error)
130 goto out;
131
Tejun Heo324a56e2013-12-11 14:11:53 -0500132 error = __kernfs_setattr(kn, iattr);
Tejun Heoffed24e2013-11-28 14:54:32 -0500133 if (error)
134 goto out;
135
136 /* this ignores size changes */
137 setattr_copy(inode, iattr);
138
139out:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500140 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500141 return error;
142}
143
Tejun Heoc637b8a2013-12-11 14:11:58 -0500144static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata,
145 u32 *secdata_len)
Tejun Heoffed24e2013-11-28 14:54:32 -0500146{
Tejun Heoc525aad2013-12-11 14:11:55 -0500147 struct kernfs_iattrs *attrs;
Tejun Heoffed24e2013-11-28 14:54:32 -0500148 void *old_secdata;
149 size_t old_secdata_len;
150
Tejun Heoc525aad2013-12-11 14:11:55 -0500151 attrs = kernfs_iattrs(kn);
Tejun Heo9a8049a2013-11-23 17:40:01 -0500152 if (!attrs)
153 return -ENOMEM;
Tejun Heoffed24e2013-11-28 14:54:32 -0500154
Tejun Heo9a8049a2013-11-23 17:40:01 -0500155 old_secdata = attrs->ia_secdata;
156 old_secdata_len = attrs->ia_secdata_len;
Tejun Heoffed24e2013-11-28 14:54:32 -0500157
Tejun Heo9a8049a2013-11-23 17:40:01 -0500158 attrs->ia_secdata = *secdata;
159 attrs->ia_secdata_len = *secdata_len;
Tejun Heoffed24e2013-11-28 14:54:32 -0500160
161 *secdata = old_secdata;
162 *secdata_len = old_secdata_len;
163 return 0;
164}
165
Tejun Heoc637b8a2013-12-11 14:11:58 -0500166int kernfs_iop_setxattr(struct dentry *dentry, const char *name,
167 const void *value, size_t size, int flags)
Tejun Heoffed24e2013-11-28 14:54:32 -0500168{
Tejun Heo324a56e2013-12-11 14:11:53 -0500169 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoc525aad2013-12-11 14:11:55 -0500170 struct kernfs_iattrs *attrs;
Tejun Heoffed24e2013-11-28 14:54:32 -0500171 void *secdata;
172 int error;
173 u32 secdata_len = 0;
174
Tejun Heoc525aad2013-12-11 14:11:55 -0500175 attrs = kernfs_iattrs(kn);
Tejun Heo23223922013-11-23 17:40:02 -0500176 if (!attrs)
177 return -ENOMEM;
Tejun Heoffed24e2013-11-28 14:54:32 -0500178
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)
Tejun Heo23223922013-11-23 17:40:02 -0500184 return error;
Tejun Heoffed24e2013-11-28 14:54:32 -0500185 error = security_inode_getsecctx(dentry->d_inode,
186 &secdata, &secdata_len);
187 if (error)
Tejun Heo23223922013-11-23 17:40:02 -0500188 return error;
Tejun Heoffed24e2013-11-28 14:54:32 -0500189
Tejun Heoa797bfc2013-12-11 14:11:57 -0500190 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500191 error = kernfs_node_setsecdata(kn, &secdata, &secdata_len);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500192 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500193
194 if (secdata)
195 security_release_secctx(secdata, secdata_len);
Tejun Heo23223922013-11-23 17:40:02 -0500196 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
Tejun Heoc637b8a2013-12-11 14:11:58 -0500205int kernfs_iop_removexattr(struct dentry *dentry, const char *name)
Tejun Heo23223922013-11-23 17:40:02 -0500206{
Tejun Heo324a56e2013-12-11 14:11:53 -0500207 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoc525aad2013-12-11 14:11:55 -0500208 struct kernfs_iattrs *attrs;
Tejun Heo23223922013-11-23 17:40:02 -0500209
Tejun Heoc525aad2013-12-11 14:11:55 -0500210 attrs = kernfs_iattrs(kn);
Tejun Heo23223922013-11-23 17:40:02 -0500211 if (!attrs)
212 return -ENOMEM;
213
214 return simple_xattr_remove(&attrs->xattrs, name);
215}
216
Tejun Heoc637b8a2013-12-11 14:11:58 -0500217ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf,
218 size_t size)
Tejun Heo23223922013-11-23 17:40:02 -0500219{
Tejun Heo324a56e2013-12-11 14:11:53 -0500220 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoc525aad2013-12-11 14:11:55 -0500221 struct kernfs_iattrs *attrs;
Tejun Heo23223922013-11-23 17:40:02 -0500222
Tejun Heoc525aad2013-12-11 14:11:55 -0500223 attrs = kernfs_iattrs(kn);
Tejun Heo23223922013-11-23 17:40:02 -0500224 if (!attrs)
225 return -ENOMEM;
226
227 return simple_xattr_get(&attrs->xattrs, name, buf, size);
228}
229
Tejun Heoc637b8a2013-12-11 14:11:58 -0500230ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
Tejun Heo23223922013-11-23 17:40:02 -0500231{
Tejun Heo324a56e2013-12-11 14:11:53 -0500232 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoc525aad2013-12-11 14:11:55 -0500233 struct kernfs_iattrs *attrs;
Tejun Heo23223922013-11-23 17:40:02 -0500234
Tejun Heoc525aad2013-12-11 14:11:55 -0500235 attrs = kernfs_iattrs(kn);
Tejun Heo23223922013-11-23 17:40:02 -0500236 if (!attrs)
237 return -ENOMEM;
238
239 return simple_xattr_list(&attrs->xattrs, buf, size);
Tejun Heoffed24e2013-11-28 14:54:32 -0500240}
241
242static 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
248static 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
Tejun Heoc637b8a2013-12-11 14:11:58 -0500257static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500258{
Tejun Heoc525aad2013-12-11 14:11:55 -0500259 struct kernfs_iattrs *attrs = kn->iattr;
Tejun Heoffed24e2013-11-28 14:54:32 -0500260
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500261 inode->i_mode = kn->mode;
Tejun Heo9a8049a2013-11-23 17:40:01 -0500262 if (attrs) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500263 /*
264 * kernfs_node has non-default attributes get them from
265 * persistent copy in kernfs_node.
Tejun Heoffed24e2013-11-28 14:54:32 -0500266 */
Tejun Heo9a8049a2013-11-23 17:40:01 -0500267 set_inode_attr(inode, &attrs->ia_iattr);
268 security_inode_notifysecctx(inode, attrs->ia_secdata,
269 attrs->ia_secdata_len);
Tejun Heoffed24e2013-11-28 14:54:32 -0500270 }
271
Tejun Heodf23fc32013-12-11 14:11:56 -0500272 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500273 set_nlink(inode, kn->dir.subdirs + 2);
Tejun Heoffed24e2013-11-28 14:54:32 -0500274}
275
Tejun Heoc637b8a2013-12-11 14:11:58 -0500276int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
277 struct kstat *stat)
Tejun Heoffed24e2013-11-28 14:54:32 -0500278{
Tejun Heo324a56e2013-12-11 14:11:53 -0500279 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoffed24e2013-11-28 14:54:32 -0500280 struct inode *inode = dentry->d_inode;
281
Tejun Heoa797bfc2013-12-11 14:11:57 -0500282 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500283 kernfs_refresh_inode(kn, inode);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500284 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500285
286 generic_fillattr(inode, stat);
287 return 0;
288}
289
Tejun Heoc637b8a2013-12-11 14:11:58 -0500290static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500291{
Tejun Heo324a56e2013-12-11 14:11:53 -0500292 kernfs_get(kn);
293 inode->i_private = kn;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500294 inode->i_mapping->a_ops = &kernfs_aops;
295 inode->i_mapping->backing_dev_info = &kernfs_bdi;
296 inode->i_op = &kernfs_iops;
Tejun Heoffed24e2013-11-28 14:54:32 -0500297
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500298 set_default_inode_attr(inode, kn->mode);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500299 kernfs_refresh_inode(kn, inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500300
301 /* initialize inode according to type */
Tejun Heodf23fc32013-12-11 14:11:56 -0500302 switch (kernfs_type(kn)) {
303 case KERNFS_DIR:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500304 inode->i_op = &kernfs_dir_iops;
305 inode->i_fop = &kernfs_dir_fops;
Tejun Heoffed24e2013-11-28 14:54:32 -0500306 break;
Tejun Heodf23fc32013-12-11 14:11:56 -0500307 case KERNFS_FILE:
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500308 inode->i_size = kn->attr.size;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500309 inode->i_fop = &kernfs_file_fops;
Tejun Heoffed24e2013-11-28 14:54:32 -0500310 break;
Tejun Heodf23fc32013-12-11 14:11:56 -0500311 case KERNFS_LINK:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500312 inode->i_op = &kernfs_symlink_iops;
Tejun Heoffed24e2013-11-28 14:54:32 -0500313 break;
314 default:
315 BUG();
316 }
317
318 unlock_new_inode(inode);
319}
320
321/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500322 * kernfs_get_inode - get inode for kernfs_node
Tejun Heoffed24e2013-11-28 14:54:32 -0500323 * @sb: super block
Tejun Heo324a56e2013-12-11 14:11:53 -0500324 * @kn: kernfs_node to allocate inode for
Tejun Heoffed24e2013-11-28 14:54:32 -0500325 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500326 * 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.
Tejun Heoffed24e2013-11-28 14:54:32 -0500329 *
330 * LOCKING:
331 * Kernel thread context (may sleep).
332 *
333 * RETURNS:
334 * Pointer to allocated inode on success, NULL on failure.
335 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500336struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
Tejun Heoffed24e2013-11-28 14:54:32 -0500337{
338 struct inode *inode;
339
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500340 inode = iget_locked(sb, kn->ino);
Tejun Heoffed24e2013-11-28 14:54:32 -0500341 if (inode && (inode->i_state & I_NEW))
Tejun Heoc637b8a2013-12-11 14:11:58 -0500342 kernfs_init_inode(kn, inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500343
344 return inode;
345}
346
347/*
Tejun Heoc637b8a2013-12-11 14:11:58 -0500348 * 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
Tejun Heoffed24e2013-11-28 14:54:32 -0500351 * super_operations.evict_inode() implementation is needed to drop that
352 * reference upon inode destruction.
353 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500354void kernfs_evict_inode(struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500355{
Tejun Heo324a56e2013-12-11 14:11:53 -0500356 struct kernfs_node *kn = inode->i_private;
Tejun Heoffed24e2013-11-28 14:54:32 -0500357
358 truncate_inode_pages(&inode->i_data, 0);
359 clear_inode(inode);
Tejun Heo324a56e2013-12-11 14:11:53 -0500360 kernfs_put(kn);
Tejun Heoffed24e2013-11-28 14:54:32 -0500361}
362
Tejun Heoc637b8a2013-12-11 14:11:58 -0500363int kernfs_iop_permission(struct inode *inode, int mask)
Tejun Heoffed24e2013-11-28 14:54:32 -0500364{
Tejun Heo324a56e2013-12-11 14:11:53 -0500365 struct kernfs_node *kn;
Tejun Heoffed24e2013-11-28 14:54:32 -0500366
367 if (mask & MAY_NOT_BLOCK)
368 return -ECHILD;
369
Tejun Heo324a56e2013-12-11 14:11:53 -0500370 kn = inode->i_private;
Tejun Heoffed24e2013-11-28 14:54:32 -0500371
Tejun Heoa797bfc2013-12-11 14:11:57 -0500372 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500373 kernfs_refresh_inode(kn, inode);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500374 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500375
376 return generic_permission(inode, mask);
377}