xref: /openbmc/linux/fs/kernfs/inode.c (revision bc5aa3a0)
1 /*
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  */
10 
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 
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,
25 };
26 
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,
35 };
36 
37 static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
38 {
39 	static DEFINE_MUTEX(iattr_mutex);
40 	struct kernfs_iattrs *ret;
41 	struct iattr *iattrs;
42 
43 	mutex_lock(&iattr_mutex);
44 
45 	if (kn->iattr)
46 		goto out_unlock;
47 
48 	kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL);
49 	if (!kn->iattr)
50 		goto out_unlock;
51 	iattrs = &kn->iattr->ia_iattr;
52 
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 
58 	ktime_get_real_ts(&iattrs->ia_atime);
59 	iattrs->ia_mtime = iattrs->ia_atime;
60 	iattrs->ia_ctime = iattrs->ia_atime;
61 
62 	simple_xattrs_init(&kn->iattr->xattrs);
63 out_unlock:
64 	ret = kn->iattr;
65 	mutex_unlock(&iattr_mutex);
66 	return ret;
67 }
68 
69 static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
70 {
71 	struct kernfs_iattrs *attrs;
72 	struct iattr *iattrs;
73 	unsigned int ia_valid = iattr->ia_valid;
74 
75 	attrs = kernfs_iattrs(kn);
76 	if (!attrs)
77 		return -ENOMEM;
78 
79 	iattrs = &attrs->ia_iattr;
80 
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;
94 	}
95 	return 0;
96 }
97 
98 /**
99  * kernfs_setattr - set iattr on a node
100  * @kn: target node
101  * @iattr: iattr to set
102  *
103  * Returns 0 on success, -errno on failure.
104  */
105 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
106 {
107 	int ret;
108 
109 	mutex_lock(&kernfs_mutex);
110 	ret = __kernfs_setattr(kn, iattr);
111 	mutex_unlock(&kernfs_mutex);
112 	return ret;
113 }
114 
115 int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
116 {
117 	struct inode *inode = d_inode(dentry);
118 	struct kernfs_node *kn = dentry->d_fsdata;
119 	int error;
120 
121 	if (!kn)
122 		return -EINVAL;
123 
124 	mutex_lock(&kernfs_mutex);
125 	error = inode_change_ok(inode, iattr);
126 	if (error)
127 		goto out;
128 
129 	error = __kernfs_setattr(kn, iattr);
130 	if (error)
131 		goto out;
132 
133 	/* this ignores size changes */
134 	setattr_copy(inode, iattr);
135 
136 out:
137 	mutex_unlock(&kernfs_mutex);
138 	return error;
139 }
140 
141 static int kernfs_node_setsecdata(struct kernfs_node *kn, void **secdata,
142 				  u32 *secdata_len)
143 {
144 	struct kernfs_iattrs *attrs;
145 	void *old_secdata;
146 	size_t old_secdata_len;
147 
148 	attrs = kernfs_iattrs(kn);
149 	if (!attrs)
150 		return -ENOMEM;
151 
152 	old_secdata = attrs->ia_secdata;
153 	old_secdata_len = attrs->ia_secdata_len;
154 
155 	attrs->ia_secdata = *secdata;
156 	attrs->ia_secdata_len = *secdata_len;
157 
158 	*secdata = old_secdata;
159 	*secdata_len = old_secdata_len;
160 	return 0;
161 }
162 
163 int kernfs_iop_setxattr(struct dentry *unused, struct inode *inode,
164 			const char *name, const void *value,
165 			size_t size, int flags)
166 {
167 	struct kernfs_node *kn = inode->i_private;
168 	struct kernfs_iattrs *attrs;
169 	void *secdata;
170 	int error;
171 	u32 secdata_len = 0;
172 
173 	attrs = kernfs_iattrs(kn);
174 	if (!attrs)
175 		return -ENOMEM;
176 
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,
180 						value, size, flags);
181 		if (error)
182 			return error;
183 		error = security_inode_getsecctx(inode,
184 						&secdata, &secdata_len);
185 		if (error)
186 			return error;
187 
188 		mutex_lock(&kernfs_mutex);
189 		error = kernfs_node_setsecdata(kn, &secdata, &secdata_len);
190 		mutex_unlock(&kernfs_mutex);
191 
192 		if (secdata)
193 			security_release_secctx(secdata, secdata_len);
194 		return error;
195 	} else if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
196 		return simple_xattr_set(&attrs->xattrs, name, value, size,
197 					flags);
198 	}
199 
200 	return -EINVAL;
201 }
202 
203 int kernfs_iop_removexattr(struct dentry *dentry, const char *name)
204 {
205 	struct kernfs_node *kn = dentry->d_fsdata;
206 	struct kernfs_iattrs *attrs;
207 
208 	attrs = kernfs_iattrs(kn);
209 	if (!attrs)
210 		return -ENOMEM;
211 
212 	return simple_xattr_set(&attrs->xattrs, name, NULL, 0, XATTR_REPLACE);
213 }
214 
215 ssize_t kernfs_iop_getxattr(struct dentry *unused, struct inode *inode,
216 			    const char *name, void *buf, size_t size)
217 {
218 	struct kernfs_node *kn = inode->i_private;
219 	struct kernfs_iattrs *attrs;
220 
221 	attrs = kernfs_iattrs(kn);
222 	if (!attrs)
223 		return -ENOMEM;
224 
225 	return simple_xattr_get(&attrs->xattrs, name, buf, size);
226 }
227 
228 ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
229 {
230 	struct kernfs_node *kn = dentry->d_fsdata;
231 	struct kernfs_iattrs *attrs;
232 
233 	attrs = kernfs_iattrs(kn);
234 	if (!attrs)
235 		return -ENOMEM;
236 
237 	return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
238 }
239 
240 static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
241 {
242 	inode->i_mode = mode;
243 	inode->i_atime = inode->i_mtime =
244 		inode->i_ctime = current_fs_time(inode->i_sb);
245 }
246 
247 static inline void set_inode_attr(struct inode *inode, struct iattr *iattr)
248 {
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);
255 }
256 
257 static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
258 {
259 	struct kernfs_iattrs *attrs = kn->iattr;
260 
261 	inode->i_mode = kn->mode;
262 	if (attrs) {
263 		/*
264 		 * kernfs_node has non-default attributes get them from
265 		 * persistent copy in kernfs_node.
266 		 */
267 		set_inode_attr(inode, &attrs->ia_iattr);
268 		security_inode_notifysecctx(inode, attrs->ia_secdata,
269 					    attrs->ia_secdata_len);
270 	}
271 
272 	if (kernfs_type(kn) == KERNFS_DIR)
273 		set_nlink(inode, kn->dir.subdirs + 2);
274 }
275 
276 int kernfs_iop_getattr(struct vfsmount *mnt, struct dentry *dentry,
277 		   struct kstat *stat)
278 {
279 	struct kernfs_node *kn = dentry->d_fsdata;
280 	struct inode *inode = d_inode(dentry);
281 
282 	mutex_lock(&kernfs_mutex);
283 	kernfs_refresh_inode(kn, inode);
284 	mutex_unlock(&kernfs_mutex);
285 
286 	generic_fillattr(inode, stat);
287 	return 0;
288 }
289 
290 static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
291 {
292 	kernfs_get(kn);
293 	inode->i_private = kn;
294 	inode->i_mapping->a_ops = &kernfs_aops;
295 	inode->i_op = &kernfs_iops;
296 
297 	set_default_inode_attr(inode, kn->mode);
298 	kernfs_refresh_inode(kn, inode);
299 
300 	/* initialize inode according to type */
301 	switch (kernfs_type(kn)) {
302 	case KERNFS_DIR:
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);
307 		break;
308 	case KERNFS_FILE:
309 		inode->i_size = kn->attr.size;
310 		inode->i_fop = &kernfs_file_fops;
311 		break;
312 	case KERNFS_LINK:
313 		inode->i_op = &kernfs_symlink_iops;
314 		break;
315 	default:
316 		BUG();
317 	}
318 
319 	unlock_new_inode(inode);
320 }
321 
322 /**
323  *	kernfs_get_inode - get inode for kernfs_node
324  *	@sb: super block
325  *	@kn: kernfs_node to allocate inode for
326  *
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
329  *	locked.
330  *
331  *	LOCKING:
332  *	Kernel thread context (may sleep).
333  *
334  *	RETURNS:
335  *	Pointer to allocated inode on success, NULL on failure.
336  */
337 struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
338 {
339 	struct inode *inode;
340 
341 	inode = iget_locked(sb, kn->ino);
342 	if (inode && (inode->i_state & I_NEW))
343 		kernfs_init_inode(kn, inode);
344 
345 	return inode;
346 }
347 
348 /*
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.
354  */
355 void kernfs_evict_inode(struct inode *inode)
356 {
357 	struct kernfs_node *kn = inode->i_private;
358 
359 	truncate_inode_pages_final(&inode->i_data);
360 	clear_inode(inode);
361 	kernfs_put(kn);
362 }
363 
364 int kernfs_iop_permission(struct inode *inode, int mask)
365 {
366 	struct kernfs_node *kn;
367 
368 	if (mask & MAY_NOT_BLOCK)
369 		return -ECHILD;
370 
371 	kn = inode->i_private;
372 
373 	mutex_lock(&kernfs_mutex);
374 	kernfs_refresh_inode(kn, inode);
375 	mutex_unlock(&kernfs_mutex);
376 
377 	return generic_permission(inode, mask);
378 }
379