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