xref: /linux/fs/kernfs/inode.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * fs/kernfs/inode.c - kernfs inode implementation
4  *
5  * Copyright (c) 2001-3 Patrick Mochel
6  * Copyright (c) 2007 SUSE Linux Products GmbH
7  * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
8  */
9 
10 #include <linux/pagemap.h>
11 #include <linux/backing-dev.h>
12 #include <linux/capability.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/xattr.h>
16 #include <linux/security.h>
17 
18 #include "kernfs-internal.h"
19 
20 static const struct inode_operations kernfs_iops = {
21 	.permission	= kernfs_iop_permission,
22 	.setattr	= kernfs_iop_setattr,
23 	.getattr	= kernfs_iop_getattr,
24 	.listxattr	= kernfs_iop_listxattr,
25 };
26 
__kernfs_iattrs(struct kernfs_node * kn,int alloc)27 static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, int alloc)
28 {
29 	static DEFINE_MUTEX(iattr_mutex);
30 	struct kernfs_iattrs *ret;
31 
32 	mutex_lock(&iattr_mutex);
33 
34 	if (kn->iattr || !alloc)
35 		goto out_unlock;
36 
37 	kn->iattr = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL);
38 	if (!kn->iattr)
39 		goto out_unlock;
40 
41 	/* assign default attributes */
42 	kn->iattr->ia_uid = GLOBAL_ROOT_UID;
43 	kn->iattr->ia_gid = GLOBAL_ROOT_GID;
44 
45 	ktime_get_real_ts64(&kn->iattr->ia_atime);
46 	kn->iattr->ia_mtime = kn->iattr->ia_atime;
47 	kn->iattr->ia_ctime = kn->iattr->ia_atime;
48 
49 	simple_xattrs_init(&kn->iattr->xattrs);
50 	atomic_set(&kn->iattr->nr_user_xattrs, 0);
51 	atomic_set(&kn->iattr->user_xattr_size, 0);
52 out_unlock:
53 	ret = kn->iattr;
54 	mutex_unlock(&iattr_mutex);
55 	return ret;
56 }
57 
kernfs_iattrs(struct kernfs_node * kn)58 static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
59 {
60 	return __kernfs_iattrs(kn, 1);
61 }
62 
kernfs_iattrs_noalloc(struct kernfs_node * kn)63 static struct kernfs_iattrs *kernfs_iattrs_noalloc(struct kernfs_node *kn)
64 {
65 	return __kernfs_iattrs(kn, 0);
66 }
67 
__kernfs_setattr(struct kernfs_node * kn,const struct iattr * iattr)68 int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
69 {
70 	struct kernfs_iattrs *attrs;
71 	unsigned int ia_valid = iattr->ia_valid;
72 
73 	attrs = kernfs_iattrs(kn);
74 	if (!attrs)
75 		return -ENOMEM;
76 
77 	if (ia_valid & ATTR_UID)
78 		attrs->ia_uid = iattr->ia_uid;
79 	if (ia_valid & ATTR_GID)
80 		attrs->ia_gid = iattr->ia_gid;
81 	if (ia_valid & ATTR_ATIME)
82 		attrs->ia_atime = iattr->ia_atime;
83 	if (ia_valid & ATTR_MTIME)
84 		attrs->ia_mtime = iattr->ia_mtime;
85 	if (ia_valid & ATTR_CTIME)
86 		attrs->ia_ctime = iattr->ia_ctime;
87 	if (ia_valid & ATTR_MODE)
88 		kn->mode = iattr->ia_mode;
89 	return 0;
90 }
91 
92 /**
93  * kernfs_setattr - set iattr on a node
94  * @kn: target node
95  * @iattr: iattr to set
96  *
97  * Return: %0 on success, -errno on failure.
98  */
kernfs_setattr(struct kernfs_node * kn,const struct iattr * iattr)99 int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
100 {
101 	int ret;
102 	struct kernfs_root *root = kernfs_root(kn);
103 
104 	down_write(&root->kernfs_iattr_rwsem);
105 	ret = __kernfs_setattr(kn, iattr);
106 	up_write(&root->kernfs_iattr_rwsem);
107 	return ret;
108 }
109 
kernfs_iop_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)110 int kernfs_iop_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
111 		       struct iattr *iattr)
112 {
113 	struct inode *inode = d_inode(dentry);
114 	struct kernfs_node *kn = inode->i_private;
115 	struct kernfs_root *root;
116 	int error;
117 
118 	if (!kn)
119 		return -EINVAL;
120 
121 	root = kernfs_root(kn);
122 	down_write(&root->kernfs_iattr_rwsem);
123 	error = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
124 	if (error)
125 		goto out;
126 
127 	error = __kernfs_setattr(kn, iattr);
128 	if (error)
129 		goto out;
130 
131 	/* this ignores size changes */
132 	setattr_copy(&nop_mnt_idmap, inode, iattr);
133 
134 out:
135 	up_write(&root->kernfs_iattr_rwsem);
136 	return error;
137 }
138 
kernfs_iop_listxattr(struct dentry * dentry,char * buf,size_t size)139 ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
140 {
141 	struct kernfs_node *kn = kernfs_dentry_node(dentry);
142 	struct kernfs_iattrs *attrs;
143 
144 	attrs = kernfs_iattrs(kn);
145 	if (!attrs)
146 		return -ENOMEM;
147 
148 	return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
149 }
150 
set_default_inode_attr(struct inode * inode,umode_t mode)151 static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
152 {
153 	inode->i_mode = mode;
154 	simple_inode_init_ts(inode);
155 }
156 
set_inode_attr(struct inode * inode,struct kernfs_iattrs * attrs)157 static inline void set_inode_attr(struct inode *inode,
158 				  struct kernfs_iattrs *attrs)
159 {
160 	inode->i_uid = attrs->ia_uid;
161 	inode->i_gid = attrs->ia_gid;
162 	inode_set_atime_to_ts(inode, attrs->ia_atime);
163 	inode_set_mtime_to_ts(inode, attrs->ia_mtime);
164 	inode_set_ctime_to_ts(inode, attrs->ia_ctime);
165 }
166 
kernfs_refresh_inode(struct kernfs_node * kn,struct inode * inode)167 static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
168 {
169 	struct kernfs_iattrs *attrs = kn->iattr;
170 
171 	inode->i_mode = kn->mode;
172 	if (attrs)
173 		/*
174 		 * kernfs_node has non-default attributes get them from
175 		 * persistent copy in kernfs_node.
176 		 */
177 		set_inode_attr(inode, attrs);
178 
179 	if (kernfs_type(kn) == KERNFS_DIR)
180 		set_nlink(inode, kn->dir.subdirs + 2);
181 }
182 
kernfs_iop_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)183 int kernfs_iop_getattr(struct mnt_idmap *idmap,
184 		       const struct path *path, struct kstat *stat,
185 		       u32 request_mask, unsigned int query_flags)
186 {
187 	struct inode *inode = d_inode(path->dentry);
188 	struct kernfs_node *kn = inode->i_private;
189 	struct kernfs_root *root = kernfs_root(kn);
190 
191 	down_read(&root->kernfs_iattr_rwsem);
192 	kernfs_refresh_inode(kn, inode);
193 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
194 	up_read(&root->kernfs_iattr_rwsem);
195 
196 	return 0;
197 }
198 
kernfs_init_inode(struct kernfs_node * kn,struct inode * inode)199 static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
200 {
201 	kernfs_get(kn);
202 	inode->i_private = kn;
203 	inode->i_mapping->a_ops = &ram_aops;
204 	inode->i_op = &kernfs_iops;
205 	inode->i_generation = kernfs_gen(kn);
206 
207 	set_default_inode_attr(inode, kn->mode);
208 	kernfs_refresh_inode(kn, inode);
209 
210 	/* initialize inode according to type */
211 	switch (kernfs_type(kn)) {
212 	case KERNFS_DIR:
213 		inode->i_op = &kernfs_dir_iops;
214 		inode->i_fop = &kernfs_dir_fops;
215 		if (kn->flags & KERNFS_EMPTY_DIR)
216 			make_empty_dir_inode(inode);
217 		break;
218 	case KERNFS_FILE:
219 		inode->i_size = kn->attr.size;
220 		inode->i_fop = &kernfs_file_fops;
221 		break;
222 	case KERNFS_LINK:
223 		inode->i_op = &kernfs_symlink_iops;
224 		break;
225 	default:
226 		BUG();
227 	}
228 
229 	unlock_new_inode(inode);
230 }
231 
232 /**
233  *	kernfs_get_inode - get inode for kernfs_node
234  *	@sb: super block
235  *	@kn: kernfs_node to allocate inode for
236  *
237  *	Get inode for @kn.  If such inode doesn't exist, a new inode is
238  *	allocated and basics are initialized.  New inode is returned
239  *	locked.
240  *
241  *	Locking:
242  *	Kernel thread context (may sleep).
243  *
244  *	Return:
245  *	Pointer to allocated inode on success, %NULL on failure.
246  */
kernfs_get_inode(struct super_block * sb,struct kernfs_node * kn)247 struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
248 {
249 	struct inode *inode;
250 
251 	inode = iget_locked(sb, kernfs_ino(kn));
252 	if (inode && (inode->i_state & I_NEW))
253 		kernfs_init_inode(kn, inode);
254 
255 	return inode;
256 }
257 
258 /*
259  * The kernfs_node serves as both an inode and a directory entry for
260  * kernfs.  To prevent the kernfs inode numbers from being freed
261  * prematurely we take a reference to kernfs_node from the kernfs inode.  A
262  * super_operations.evict_inode() implementation is needed to drop that
263  * reference upon inode destruction.
264  */
kernfs_evict_inode(struct inode * inode)265 void kernfs_evict_inode(struct inode *inode)
266 {
267 	struct kernfs_node *kn = inode->i_private;
268 
269 	truncate_inode_pages_final(&inode->i_data);
270 	clear_inode(inode);
271 	kernfs_put(kn);
272 }
273 
kernfs_iop_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)274 int kernfs_iop_permission(struct mnt_idmap *idmap,
275 			  struct inode *inode, int mask)
276 {
277 	struct kernfs_node *kn;
278 	struct kernfs_root *root;
279 	int ret;
280 
281 	if (mask & MAY_NOT_BLOCK)
282 		return -ECHILD;
283 
284 	kn = inode->i_private;
285 	root = kernfs_root(kn);
286 
287 	down_read(&root->kernfs_iattr_rwsem);
288 	kernfs_refresh_inode(kn, inode);
289 	ret = generic_permission(&nop_mnt_idmap, inode, mask);
290 	up_read(&root->kernfs_iattr_rwsem);
291 
292 	return ret;
293 }
294 
kernfs_xattr_get(struct kernfs_node * kn,const char * name,void * value,size_t size)295 int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
296 		     void *value, size_t size)
297 {
298 	struct kernfs_iattrs *attrs = kernfs_iattrs_noalloc(kn);
299 	if (!attrs)
300 		return -ENODATA;
301 
302 	return simple_xattr_get(&attrs->xattrs, name, value, size);
303 }
304 
kernfs_xattr_set(struct kernfs_node * kn,const char * name,const void * value,size_t size,int flags)305 int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
306 		     const void *value, size_t size, int flags)
307 {
308 	struct simple_xattr *old_xattr;
309 	struct kernfs_iattrs *attrs = kernfs_iattrs(kn);
310 	if (!attrs)
311 		return -ENOMEM;
312 
313 	old_xattr = simple_xattr_set(&attrs->xattrs, name, value, size, flags);
314 	if (IS_ERR(old_xattr))
315 		return PTR_ERR(old_xattr);
316 
317 	simple_xattr_free(old_xattr);
318 	return 0;
319 }
320 
kernfs_vfs_xattr_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * suffix,void * value,size_t size)321 static int kernfs_vfs_xattr_get(const struct xattr_handler *handler,
322 				struct dentry *unused, struct inode *inode,
323 				const char *suffix, void *value, size_t size)
324 {
325 	const char *name = xattr_full_name(handler, suffix);
326 	struct kernfs_node *kn = inode->i_private;
327 
328 	return kernfs_xattr_get(kn, name, value, size);
329 }
330 
kernfs_vfs_xattr_set(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * suffix,const void * value,size_t size,int flags)331 static int kernfs_vfs_xattr_set(const struct xattr_handler *handler,
332 				struct mnt_idmap *idmap,
333 				struct dentry *unused, struct inode *inode,
334 				const char *suffix, const void *value,
335 				size_t size, int flags)
336 {
337 	const char *name = xattr_full_name(handler, suffix);
338 	struct kernfs_node *kn = inode->i_private;
339 
340 	return kernfs_xattr_set(kn, name, value, size, flags);
341 }
342 
kernfs_vfs_user_xattr_add(struct kernfs_node * kn,const char * full_name,struct simple_xattrs * xattrs,const void * value,size_t size,int flags)343 static int kernfs_vfs_user_xattr_add(struct kernfs_node *kn,
344 				     const char *full_name,
345 				     struct simple_xattrs *xattrs,
346 				     const void *value, size_t size, int flags)
347 {
348 	atomic_t *sz = &kn->iattr->user_xattr_size;
349 	atomic_t *nr = &kn->iattr->nr_user_xattrs;
350 	struct simple_xattr *old_xattr;
351 	int ret;
352 
353 	if (atomic_inc_return(nr) > KERNFS_MAX_USER_XATTRS) {
354 		ret = -ENOSPC;
355 		goto dec_count_out;
356 	}
357 
358 	if (atomic_add_return(size, sz) > KERNFS_USER_XATTR_SIZE_LIMIT) {
359 		ret = -ENOSPC;
360 		goto dec_size_out;
361 	}
362 
363 	old_xattr = simple_xattr_set(xattrs, full_name, value, size, flags);
364 	if (!old_xattr)
365 		return 0;
366 
367 	if (IS_ERR(old_xattr)) {
368 		ret = PTR_ERR(old_xattr);
369 		goto dec_size_out;
370 	}
371 
372 	ret = 0;
373 	size = old_xattr->size;
374 	simple_xattr_free(old_xattr);
375 dec_size_out:
376 	atomic_sub(size, sz);
377 dec_count_out:
378 	atomic_dec(nr);
379 	return ret;
380 }
381 
kernfs_vfs_user_xattr_rm(struct kernfs_node * kn,const char * full_name,struct simple_xattrs * xattrs,const void * value,size_t size,int flags)382 static int kernfs_vfs_user_xattr_rm(struct kernfs_node *kn,
383 				    const char *full_name,
384 				    struct simple_xattrs *xattrs,
385 				    const void *value, size_t size, int flags)
386 {
387 	atomic_t *sz = &kn->iattr->user_xattr_size;
388 	atomic_t *nr = &kn->iattr->nr_user_xattrs;
389 	struct simple_xattr *old_xattr;
390 
391 	old_xattr = simple_xattr_set(xattrs, full_name, value, size, flags);
392 	if (!old_xattr)
393 		return 0;
394 
395 	if (IS_ERR(old_xattr))
396 		return PTR_ERR(old_xattr);
397 
398 	atomic_sub(old_xattr->size, sz);
399 	atomic_dec(nr);
400 	simple_xattr_free(old_xattr);
401 	return 0;
402 }
403 
kernfs_vfs_user_xattr_set(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * suffix,const void * value,size_t size,int flags)404 static int kernfs_vfs_user_xattr_set(const struct xattr_handler *handler,
405 				     struct mnt_idmap *idmap,
406 				     struct dentry *unused, struct inode *inode,
407 				     const char *suffix, const void *value,
408 				     size_t size, int flags)
409 {
410 	const char *full_name = xattr_full_name(handler, suffix);
411 	struct kernfs_node *kn = inode->i_private;
412 	struct kernfs_iattrs *attrs;
413 
414 	if (!(kernfs_root(kn)->flags & KERNFS_ROOT_SUPPORT_USER_XATTR))
415 		return -EOPNOTSUPP;
416 
417 	attrs = kernfs_iattrs(kn);
418 	if (!attrs)
419 		return -ENOMEM;
420 
421 	if (value)
422 		return kernfs_vfs_user_xattr_add(kn, full_name, &attrs->xattrs,
423 						 value, size, flags);
424 	else
425 		return kernfs_vfs_user_xattr_rm(kn, full_name, &attrs->xattrs,
426 						value, size, flags);
427 
428 }
429 
430 static const struct xattr_handler kernfs_trusted_xattr_handler = {
431 	.prefix = XATTR_TRUSTED_PREFIX,
432 	.get = kernfs_vfs_xattr_get,
433 	.set = kernfs_vfs_xattr_set,
434 };
435 
436 static const struct xattr_handler kernfs_security_xattr_handler = {
437 	.prefix = XATTR_SECURITY_PREFIX,
438 	.get = kernfs_vfs_xattr_get,
439 	.set = kernfs_vfs_xattr_set,
440 };
441 
442 static const struct xattr_handler kernfs_user_xattr_handler = {
443 	.prefix = XATTR_USER_PREFIX,
444 	.get = kernfs_vfs_xattr_get,
445 	.set = kernfs_vfs_user_xattr_set,
446 };
447 
448 const struct xattr_handler * const kernfs_xattr_handlers[] = {
449 	&kernfs_trusted_xattr_handler,
450 	&kernfs_security_xattr_handler,
451 	&kernfs_user_xattr_handler,
452 	NULL
453 };
454