blob: 756dd56aaf60acd337fb251fb287cbd382d57740 [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 const struct inode_operations kernfs_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -050028 .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,
Tejun Heoffed24e2013-11-28 14:54:32 -050035};
36
Tejun Heoc525aad2013-12-11 14:11:55 -050037static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
Tejun Heoffed24e2013-11-28 14:54:32 -050038{
Tejun Heo4afddd62014-04-02 16:40:52 -040039 static DEFINE_MUTEX(iattr_mutex);
40 struct kernfs_iattrs *ret;
Tejun Heoffed24e2013-11-28 14:54:32 -050041 struct iattr *iattrs;
42
Tejun Heo4afddd62014-04-02 16:40:52 -040043 mutex_lock(&iattr_mutex);
44
Tejun Heoadc5e8b2013-12-11 14:11:54 -050045 if (kn->iattr)
Tejun Heo4afddd62014-04-02 16:40:52 -040046 goto out_unlock;
Tejun Heo9a8049a2013-11-23 17:40:01 -050047
Tejun Heoc525aad2013-12-11 14:11:55 -050048 kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL);
Tejun Heoadc5e8b2013-12-11 14:11:54 -050049 if (!kn->iattr)
Tejun Heo4afddd62014-04-02 16:40:52 -040050 goto out_unlock;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050051 iattrs = &kn->iattr->ia_iattr;
Tejun Heoffed24e2013-11-28 14:54:32 -050052
53 /* assign default attributes */
Tejun Heoadc5e8b2013-12-11 14:11:54 -050054 iattrs->ia_mode = kn->mode;
Tejun Heoffed24e2013-11-28 14:54:32 -050055 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;
58
Tejun Heoadc5e8b2013-12-11 14:11:54 -050059 simple_xattrs_init(&kn->iattr->xattrs);
Tejun Heo4afddd62014-04-02 16:40:52 -040060out_unlock:
61 ret = kn->iattr;
62 mutex_unlock(&iattr_mutex);
63 return ret;
Tejun Heoffed24e2013-11-28 14:54:32 -050064}
65
Tejun Heo324a56e2013-12-11 14:11:53 -050066static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -050067{
Tejun Heoc525aad2013-12-11 14:11:55 -050068 struct kernfs_iattrs *attrs;
Tejun Heoffed24e2013-11-28 14:54:32 -050069 struct iattr *iattrs;
70 unsigned int ia_valid = iattr->ia_valid;
71
Tejun Heoc525aad2013-12-11 14:11:55 -050072 attrs = kernfs_iattrs(kn);
Tejun Heo9a8049a2013-11-23 17:40:01 -050073 if (!attrs)
74 return -ENOMEM;
Tejun Heoffed24e2013-11-28 14:54:32 -050075
Tejun Heo9a8049a2013-11-23 17:40:01 -050076 iattrs = &attrs->ia_iattr;
Tejun Heoffed24e2013-11-28 14:54:32 -050077
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;
Tejun Heoadc5e8b2013-12-11 14:11:54 -050090 iattrs->ia_mode = kn->mode = mode;
Tejun Heoffed24e2013-11-28 14:54:32 -050091 }
92 return 0;
93}
94
95/**
96 * kernfs_setattr - set iattr on a node
Tejun Heo324a56e2013-12-11 14:11:53 -050097 * @kn: target node
Tejun Heoffed24e2013-11-28 14:54:32 -050098 * @iattr: iattr to set
99 *
100 * Returns 0 on success, -errno on failure.
101 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500102int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -0500103{
104 int ret;
105
Tejun Heoa797bfc2013-12-11 14:11:57 -0500106 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500107 ret = __kernfs_setattr(kn, iattr);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500108 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500109 return ret;
110}
111
Tejun Heoc637b8a2013-12-11 14:11:58 -0500112int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -0500113{
David Howells2b0143b2015-03-17 22:25:59 +0000114 struct inode *inode = d_inode(dentry);
Tejun Heo324a56e2013-12-11 14:11:53 -0500115 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoffed24e2013-11-28 14:54:32 -0500116 int error;
117
Tejun Heo324a56e2013-12-11 14:11:53 -0500118 if (!kn)
Tejun Heoffed24e2013-11-28 14:54:32 -0500119 return -EINVAL;
120
Tejun Heoa797bfc2013-12-11 14:11:57 -0500121 mutex_lock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500122 error = inode_change_ok(inode, iattr);
123 if (error)
124 goto out;
125
Tejun Heo324a56e2013-12-11 14:11:53 -0500126 error = __kernfs_setattr(kn, iattr);
Tejun Heoffed24e2013-11-28 14:54:32 -0500127 if (error)
128 goto out;
129
130 /* this ignores size changes */
131 setattr_copy(inode, iattr);
132
133out:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500134 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500135 return error;
136}
137
Tejun Heoc637b8a2013-12-11 14:11:58 -0500138static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata,
139 u32 *secdata_len)
Tejun Heoffed24e2013-11-28 14:54:32 -0500140{
Tejun Heoc525aad2013-12-11 14:11:55 -0500141 struct kernfs_iattrs *attrs;
Tejun Heoffed24e2013-11-28 14:54:32 -0500142 void *old_secdata;
143 size_t old_secdata_len;
144
Tejun Heoc525aad2013-12-11 14:11:55 -0500145 attrs = kernfs_iattrs(kn);
Tejun Heo9a8049a2013-11-23 17:40:01 -0500146 if (!attrs)
147 return -ENOMEM;
Tejun Heoffed24e2013-11-28 14:54:32 -0500148
Tejun Heo9a8049a2013-11-23 17:40:01 -0500149 old_secdata = attrs->ia_secdata;
150 old_secdata_len = attrs->ia_secdata_len;
Tejun Heoffed24e2013-11-28 14:54:32 -0500151
Tejun Heo9a8049a2013-11-23 17:40:01 -0500152 attrs->ia_secdata = *secdata;
153 attrs->ia_secdata_len = *secdata_len;
Tejun Heoffed24e2013-11-28 14:54:32 -0500154
155 *secdata = old_secdata;
156 *secdata_len = old_secdata_len;
157 return 0;
158}
159
Tejun Heoc637b8a2013-12-11 14:11:58 -0500160int kernfs_iop_setxattr(struct dentry *dentry, const char *name,
161 const void *value, size_t size, int flags)
Tejun Heoffed24e2013-11-28 14:54:32 -0500162{
Tejun Heo324a56e2013-12-11 14:11:53 -0500163 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoc525aad2013-12-11 14:11:55 -0500164 struct kernfs_iattrs *attrs;
Tejun Heoffed24e2013-11-28 14:54:32 -0500165 void *secdata;
166 int error;
167 u32 secdata_len = 0;
168
Tejun Heoc525aad2013-12-11 14:11:55 -0500169 attrs = kernfs_iattrs(kn);
Tejun Heo23223922013-11-23 17:40:02 -0500170 if (!attrs)
171 return -ENOMEM;
Tejun Heoffed24e2013-11-28 14:54:32 -0500172
173 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN)) {
174 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
David Howells2b0143b2015-03-17 22:25:59 +0000175 error = security_inode_setsecurity(d_inode(dentry), suffix,
Tejun Heoffed24e2013-11-28 14:54:32 -0500176 value, size, flags);
177 if (error)
Tejun Heo23223922013-11-23 17:40:02 -0500178 return error;
David Howells2b0143b2015-03-17 22:25:59 +0000179 error = security_inode_getsecctx(d_inode(dentry),
Tejun Heoffed24e2013-11-28 14:54:32 -0500180 &secdata, &secdata_len);
181 if (error)
Tejun Heo23223922013-11-23 17:40:02 -0500182 return error;
Tejun Heoffed24e2013-11-28 14:54:32 -0500183
Tejun Heoa797bfc2013-12-11 14:11:57 -0500184 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500185 error = kernfs_node_setsecdata(kn, &secdata, &secdata_len);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500186 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500187
188 if (secdata)
189 security_release_secctx(secdata, secdata_len);
Tejun Heo23223922013-11-23 17:40:02 -0500190 return error;
191 } else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
192 return simple_xattr_set(&attrs->xattrs, name, value, size,
193 flags);
194 }
195
196 return -EINVAL;
197}
198
Tejun Heoc637b8a2013-12-11 14:11:58 -0500199int kernfs_iop_removexattr(struct dentry *dentry, const char *name)
Tejun Heo23223922013-11-23 17:40:02 -0500200{
Tejun Heo324a56e2013-12-11 14:11:53 -0500201 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoc525aad2013-12-11 14:11:55 -0500202 struct kernfs_iattrs *attrs;
Tejun Heo23223922013-11-23 17:40:02 -0500203
Tejun Heoc525aad2013-12-11 14:11:55 -0500204 attrs = kernfs_iattrs(kn);
Tejun Heo23223922013-11-23 17:40:02 -0500205 if (!attrs)
206 return -ENOMEM;
207
208 return simple_xattr_remove(&attrs->xattrs, name);
209}
210
Tejun Heoc637b8a2013-12-11 14:11:58 -0500211ssize_t kernfs_iop_getxattr(struct dentry *dentry, const char *name, void *buf,
212 size_t size)
Tejun Heo23223922013-11-23 17:40:02 -0500213{
Tejun Heo324a56e2013-12-11 14:11:53 -0500214 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoc525aad2013-12-11 14:11:55 -0500215 struct kernfs_iattrs *attrs;
Tejun Heo23223922013-11-23 17:40:02 -0500216
Tejun Heoc525aad2013-12-11 14:11:55 -0500217 attrs = kernfs_iattrs(kn);
Tejun Heo23223922013-11-23 17:40:02 -0500218 if (!attrs)
219 return -ENOMEM;
220
221 return simple_xattr_get(&attrs->xattrs, name, buf, size);
222}
223
Tejun Heoc637b8a2013-12-11 14:11:58 -0500224ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
Tejun Heo23223922013-11-23 17:40:02 -0500225{
Tejun Heo324a56e2013-12-11 14:11:53 -0500226 struct kernfs_node *kn = dentry->d_fsdata;
Tejun Heoc525aad2013-12-11 14:11:55 -0500227 struct kernfs_iattrs *attrs;
Tejun Heo23223922013-11-23 17:40:02 -0500228
Tejun Heoc525aad2013-12-11 14:11:55 -0500229 attrs = kernfs_iattrs(kn);
Tejun Heo23223922013-11-23 17:40:02 -0500230 if (!attrs)
231 return -ENOMEM;
232
233 return simple_xattr_list(&attrs->xattrs, buf, size);
Tejun Heoffed24e2013-11-28 14:54:32 -0500234}
235
236static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
237{
238 inode->i_mode = mode;
239 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
240}
241
242static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
243{
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;
249}
250
Tejun Heoc637b8a2013-12-11 14:11:58 -0500251static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500252{
Tejun Heoc525aad2013-12-11 14:11:55 -0500253 struct kernfs_iattrs *attrs = kn->iattr;
Tejun Heoffed24e2013-11-28 14:54:32 -0500254
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500255 inode->i_mode = kn->mode;
Tejun Heo9a8049a2013-11-23 17:40:01 -0500256 if (attrs) {
Tejun Heo324a56e2013-12-11 14:11:53 -0500257 /*
258 * kernfs_node has non-default attributes get them from
259 * persistent copy in kernfs_node.
Tejun Heoffed24e2013-11-28 14:54:32 -0500260 */
Tejun Heo9a8049a2013-11-23 17:40:01 -0500261 set_inode_attr(inode, &attrs->ia_iattr);
262 security_inode_notifysecctx(inode, attrs->ia_secdata,
263 attrs->ia_secdata_len);
Tejun Heoffed24e2013-11-28 14:54:32 -0500264 }
265
Tejun Heodf23fc32013-12-11 14:11:56 -0500266 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500267 set_nlink(inode, kn->dir.subdirs + 2);
Tejun Heoffed24e2013-11-28 14:54:32 -0500268}
269
Tejun Heoc637b8a2013-12-11 14:11:58 -0500270int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
271 struct kstat *stat)
Tejun Heoffed24e2013-11-28 14:54:32 -0500272{
Tejun Heo324a56e2013-12-11 14:11:53 -0500273 struct kernfs_node *kn = dentry->d_fsdata;
David Howells2b0143b2015-03-17 22:25:59 +0000274 struct inode *inode = d_inode(dentry);
Tejun Heoffed24e2013-11-28 14:54:32 -0500275
Tejun Heoa797bfc2013-12-11 14:11:57 -0500276 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500277 kernfs_refresh_inode(kn, inode);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500278 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500279
280 generic_fillattr(inode, stat);
281 return 0;
282}
283
Tejun Heoc637b8a2013-12-11 14:11:58 -0500284static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500285{
Tejun Heo324a56e2013-12-11 14:11:53 -0500286 kernfs_get(kn);
287 inode->i_private = kn;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500288 inode->i_mapping->a_ops = &kernfs_aops;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500289 inode->i_op = &kernfs_iops;
Tejun Heoffed24e2013-11-28 14:54:32 -0500290
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500291 set_default_inode_attr(inode, kn->mode);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500292 kernfs_refresh_inode(kn, inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500293
294 /* initialize inode according to type */
Tejun Heodf23fc32013-12-11 14:11:56 -0500295 switch (kernfs_type(kn)) {
296 case KERNFS_DIR:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500297 inode->i_op = &kernfs_dir_iops;
298 inode->i_fop = &kernfs_dir_fops;
Eric W. Biedermanea015212015-05-13 16:09:29 -0500299 if (kn->flags & KERNFS_EMPTY_DIR)
300 make_empty_dir_inode(inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500301 break;
Tejun Heodf23fc32013-12-11 14:11:56 -0500302 case KERNFS_FILE:
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500303 inode->i_size = kn->attr.size;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500304 inode->i_fop = &kernfs_file_fops;
Tejun Heoffed24e2013-11-28 14:54:32 -0500305 break;
Tejun Heodf23fc32013-12-11 14:11:56 -0500306 case KERNFS_LINK:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500307 inode->i_op = &kernfs_symlink_iops;
Tejun Heoffed24e2013-11-28 14:54:32 -0500308 break;
309 default:
310 BUG();
311 }
312
313 unlock_new_inode(inode);
314}
315
316/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500317 * kernfs_get_inode - get inode for kernfs_node
Tejun Heoffed24e2013-11-28 14:54:32 -0500318 * @sb: super block
Tejun Heo324a56e2013-12-11 14:11:53 -0500319 * @kn: kernfs_node to allocate inode for
Tejun Heoffed24e2013-11-28 14:54:32 -0500320 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500321 * Get inode for @kn. If such inode doesn't exist, a new inode is
322 * allocated and basics are initialized. New inode is returned
323 * locked.
Tejun Heoffed24e2013-11-28 14:54:32 -0500324 *
325 * LOCKING:
326 * Kernel thread context (may sleep).
327 *
328 * RETURNS:
329 * Pointer to allocated inode on success, NULL on failure.
330 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500331struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
Tejun Heoffed24e2013-11-28 14:54:32 -0500332{
333 struct inode *inode;
334
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500335 inode = iget_locked(sb, kn->ino);
Tejun Heoffed24e2013-11-28 14:54:32 -0500336 if (inode && (inode->i_state & I_NEW))
Tejun Heoc637b8a2013-12-11 14:11:58 -0500337 kernfs_init_inode(kn, inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500338
339 return inode;
340}
341
342/*
Tejun Heoc637b8a2013-12-11 14:11:58 -0500343 * The kernfs_node serves as both an inode and a directory entry for
344 * kernfs. To prevent the kernfs inode numbers from being freed
345 * prematurely we take a reference to kernfs_node from the kernfs inode. A
Tejun Heoffed24e2013-11-28 14:54:32 -0500346 * super_operations.evict_inode() implementation is needed to drop that
347 * reference upon inode destruction.
348 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500349void kernfs_evict_inode(struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500350{
Tejun Heo324a56e2013-12-11 14:11:53 -0500351 struct kernfs_node *kn = inode->i_private;
Tejun Heoffed24e2013-11-28 14:54:32 -0500352
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700353 truncate_inode_pages_final(&inode->i_data);
Tejun Heoffed24e2013-11-28 14:54:32 -0500354 clear_inode(inode);
Tejun Heo324a56e2013-12-11 14:11:53 -0500355 kernfs_put(kn);
Tejun Heoffed24e2013-11-28 14:54:32 -0500356}
357
Tejun Heoc637b8a2013-12-11 14:11:58 -0500358int kernfs_iop_permission(struct inode *inode, int mask)
Tejun Heoffed24e2013-11-28 14:54:32 -0500359{
Tejun Heo324a56e2013-12-11 14:11:53 -0500360 struct kernfs_node *kn;
Tejun Heoffed24e2013-11-28 14:54:32 -0500361
362 if (mask & MAY_NOT_BLOCK)
363 return -ECHILD;
364
Tejun Heo324a56e2013-12-11 14:11:53 -0500365 kn = inode->i_private;
Tejun Heoffed24e2013-11-28 14:54:32 -0500366
Tejun Heoa797bfc2013-12-11 14:11:57 -0500367 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500368 kernfs_refresh_inode(kn, inode);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500369 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500370
371 return generic_permission(inode, mask);
372}