blob: fc2469a20fed078b7ddb777cae7cd2dee65aea88 [file] [log] [blame]
Thomas Gleixner55716d22019-06-01 10:08:42 +02001// SPDX-License-Identifier: GPL-2.0-only
Tejun Heob8441ed2013-11-24 09:54:58 -05002/*
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>
Tejun Heob8441ed2013-11-24 09:54:58 -05008 */
Tejun Heoffed24e2013-11-28 14:54:32 -05009
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
Tejun Heoa797bfc2013-12-11 14:11:57 -050020static const struct address_space_operations kernfs_aops = {
Tejun Heoffed24e2013-11-28 14:54:32 -050021 .readpage = simple_readpage,
22 .write_begin = simple_write_begin,
23 .write_end = simple_write_end,
24};
25
Tejun Heoa797bfc2013-12-11 14:11:57 -050026static const struct inode_operations kernfs_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -050027 .permission = kernfs_iop_permission,
28 .setattr = kernfs_iop_setattr,
29 .getattr = kernfs_iop_getattr,
Tejun Heoc637b8a2013-12-11 14:11:58 -050030 .listxattr = kernfs_iop_listxattr,
Tejun Heoffed24e2013-11-28 14:54:32 -050031};
32
Ondrej Mosnacekd0c9c152019-02-22 15:57:13 +010033static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, int alloc)
Tejun Heoffed24e2013-11-28 14:54:32 -050034{
Tejun Heo4afddd62014-04-02 16:40:52 -040035 static DEFINE_MUTEX(iattr_mutex);
36 struct kernfs_iattrs *ret;
Tejun Heoffed24e2013-11-28 14:54:32 -050037
Tejun Heo4afddd62014-04-02 16:40:52 -040038 mutex_lock(&iattr_mutex);
39
Ondrej Mosnacekd0c9c152019-02-22 15:57:13 +010040 if (kn->iattr || !alloc)
Tejun Heo4afddd62014-04-02 16:40:52 -040041 goto out_unlock;
Tejun Heo9a8049a2013-11-23 17:40:01 -050042
Ayush Mittal26e28d62019-02-06 10:25:42 +053043 kn->iattr = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL);
Tejun Heoadc5e8b2013-12-11 14:11:54 -050044 if (!kn->iattr)
Tejun Heo4afddd62014-04-02 16:40:52 -040045 goto out_unlock;
Tejun Heoffed24e2013-11-28 14:54:32 -050046
47 /* assign default attributes */
Ondrej Mosnacek05895212019-02-22 15:57:12 +010048 kn->iattr->ia_uid = GLOBAL_ROOT_UID;
49 kn->iattr->ia_gid = GLOBAL_ROOT_GID;
Deepa Dinamani3a3a5fe2016-02-22 07:17:53 -080050
Ondrej Mosnacek05895212019-02-22 15:57:12 +010051 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;
Tejun Heoffed24e2013-11-28 14:54:32 -050054
Tejun Heoadc5e8b2013-12-11 14:11:54 -050055 simple_xattrs_init(&kn->iattr->xattrs);
Daniel Xu0c473832020-03-12 13:03:16 -070056 atomic_set(&kn->iattr->nr_user_xattrs, 0);
57 atomic_set(&kn->iattr->user_xattr_size, 0);
Tejun Heo4afddd62014-04-02 16:40:52 -040058out_unlock:
59 ret = kn->iattr;
60 mutex_unlock(&iattr_mutex);
61 return ret;
Tejun Heoffed24e2013-11-28 14:54:32 -050062}
63
Ondrej Mosnacekd0c9c152019-02-22 15:57:13 +010064static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
65{
66 return __kernfs_iattrs(kn, 1);
67}
68
69static struct kernfs_iattrs *kernfs_iattrs_noalloc(struct kernfs_node *kn)
70{
71 return __kernfs_iattrs(kn, 0);
72}
73
Dmitry Torokhov488dee92018-07-20 21:56:47 +000074int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -050075{
Tejun Heoc525aad2013-12-11 14:11:55 -050076 struct kernfs_iattrs *attrs;
Tejun Heoffed24e2013-11-28 14:54:32 -050077 unsigned int ia_valid = iattr->ia_valid;
78
Tejun Heoc525aad2013-12-11 14:11:55 -050079 attrs = kernfs_iattrs(kn);
Tejun Heo9a8049a2013-11-23 17:40:01 -050080 if (!attrs)
81 return -ENOMEM;
Tejun Heoffed24e2013-11-28 14:54:32 -050082
Tejun Heoffed24e2013-11-28 14:54:32 -050083 if (ia_valid & ATTR_UID)
Ondrej Mosnacek05895212019-02-22 15:57:12 +010084 attrs->ia_uid = iattr->ia_uid;
Tejun Heoffed24e2013-11-28 14:54:32 -050085 if (ia_valid & ATTR_GID)
Ondrej Mosnacek05895212019-02-22 15:57:12 +010086 attrs->ia_gid = iattr->ia_gid;
Tejun Heoffed24e2013-11-28 14:54:32 -050087 if (ia_valid & ATTR_ATIME)
Ondrej Mosnacek05895212019-02-22 15:57:12 +010088 attrs->ia_atime = iattr->ia_atime;
Tejun Heoffed24e2013-11-28 14:54:32 -050089 if (ia_valid & ATTR_MTIME)
Ondrej Mosnacek05895212019-02-22 15:57:12 +010090 attrs->ia_mtime = iattr->ia_mtime;
Tejun Heoffed24e2013-11-28 14:54:32 -050091 if (ia_valid & ATTR_CTIME)
Ondrej Mosnacek05895212019-02-22 15:57:12 +010092 attrs->ia_ctime = iattr->ia_ctime;
93 if (ia_valid & ATTR_MODE)
94 kn->mode = iattr->ia_mode;
Tejun Heoffed24e2013-11-28 14:54:32 -050095 return 0;
96}
97
98/**
99 * kernfs_setattr - set iattr on a node
Tejun Heo324a56e2013-12-11 14:11:53 -0500100 * @kn: target node
Tejun Heoffed24e2013-11-28 14:54:32 -0500101 * @iattr: iattr to set
102 *
103 * Returns 0 on success, -errno on failure.
104 */
Tejun Heo324a56e2013-12-11 14:11:53 -0500105int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -0500106{
107 int ret;
108
Tejun Heoa797bfc2013-12-11 14:11:57 -0500109 mutex_lock(&kernfs_mutex);
Tejun Heo324a56e2013-12-11 14:11:53 -0500110 ret = __kernfs_setattr(kn, iattr);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500111 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500112 return ret;
113}
114
Tejun Heoc637b8a2013-12-11 14:11:58 -0500115int kernfs_iop_setattr(struct dentry *dentry, struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -0500116{
David Howells2b0143b2015-03-17 22:25:59 +0000117 struct inode *inode = d_inode(dentry);
Shaohua Li319ba912017-07-12 11:49:49 -0700118 struct kernfs_node *kn = inode->i_private;
Tejun Heoffed24e2013-11-28 14:54:32 -0500119 int error;
120
Tejun Heo324a56e2013-12-11 14:11:53 -0500121 if (!kn)
Tejun Heoffed24e2013-11-28 14:54:32 -0500122 return -EINVAL;
123
Tejun Heoa797bfc2013-12-11 14:11:57 -0500124 mutex_lock(&kernfs_mutex);
Jan Kara31051c82016-05-26 16:55:18 +0200125 error = setattr_prepare(dentry, iattr);
Tejun Heoffed24e2013-11-28 14:54:32 -0500126 if (error)
127 goto out;
128
Tejun Heo324a56e2013-12-11 14:11:53 -0500129 error = __kernfs_setattr(kn, iattr);
Tejun Heoffed24e2013-11-28 14:54:32 -0500130 if (error)
131 goto out;
132
133 /* this ignores size changes */
134 setattr_copy(inode, iattr);
135
136out:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500137 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500138 return error;
139}
140
Tejun Heoc637b8a2013-12-11 14:11:58 -0500141ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
Tejun Heo23223922013-11-23 17:40:02 -0500142{
Shaohua Li319ba912017-07-12 11:49:49 -0700143 struct kernfs_node *kn = kernfs_dentry_node(dentry);
Tejun Heoc525aad2013-12-11 14:11:55 -0500144 struct kernfs_iattrs *attrs;
Tejun Heo23223922013-11-23 17:40:02 -0500145
Tejun Heoc525aad2013-12-11 14:11:55 -0500146 attrs = kernfs_iattrs(kn);
Tejun Heo23223922013-11-23 17:40:02 -0500147 if (!attrs)
148 return -ENOMEM;
149
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100150 return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
Tejun Heoffed24e2013-11-28 14:54:32 -0500151}
152
153static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
154{
155 inode->i_mode = mode;
Deepa Dinamani3a3a5fe2016-02-22 07:17:53 -0800156 inode->i_atime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700157 inode->i_ctime = current_time(inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500158}
159
Ondrej Mosnacek05895212019-02-22 15:57:12 +0100160static inline void set_inode_attr(struct inode *inode,
161 struct kernfs_iattrs *attrs)
Tejun Heoffed24e2013-11-28 14:54:32 -0500162{
Ondrej Mosnacek05895212019-02-22 15:57:12 +0100163 inode->i_uid = attrs->ia_uid;
164 inode->i_gid = attrs->ia_gid;
Al Virof0f35882019-12-07 21:51:31 -0500165 inode->i_atime = attrs->ia_atime;
166 inode->i_mtime = attrs->ia_mtime;
167 inode->i_ctime = attrs->ia_ctime;
Tejun Heoffed24e2013-11-28 14:54:32 -0500168}
169
Tejun Heoc637b8a2013-12-11 14:11:58 -0500170static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500171{
Tejun Heoc525aad2013-12-11 14:11:55 -0500172 struct kernfs_iattrs *attrs = kn->iattr;
Tejun Heoffed24e2013-11-28 14:54:32 -0500173
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500174 inode->i_mode = kn->mode;
Ondrej Mosnacek0ac60752019-02-22 15:57:15 +0100175 if (attrs)
Tejun Heo324a56e2013-12-11 14:11:53 -0500176 /*
177 * kernfs_node has non-default attributes get them from
178 * persistent copy in kernfs_node.
Tejun Heoffed24e2013-11-28 14:54:32 -0500179 */
Ondrej Mosnacek05895212019-02-22 15:57:12 +0100180 set_inode_attr(inode, attrs);
Tejun Heoffed24e2013-11-28 14:54:32 -0500181
Tejun Heodf23fc32013-12-11 14:11:56 -0500182 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500183 set_nlink(inode, kn->dir.subdirs + 2);
Tejun Heoffed24e2013-11-28 14:54:32 -0500184}
185
David Howellsa528d352017-01-31 16:46:22 +0000186int kernfs_iop_getattr(const struct path *path, struct kstat *stat,
187 u32 request_mask, unsigned int query_flags)
Tejun Heoffed24e2013-11-28 14:54:32 -0500188{
David Howellsa528d352017-01-31 16:46:22 +0000189 struct inode *inode = d_inode(path->dentry);
Shaohua Li319ba912017-07-12 11:49:49 -0700190 struct kernfs_node *kn = inode->i_private;
Tejun Heoffed24e2013-11-28 14:54:32 -0500191
Tejun Heoa797bfc2013-12-11 14:11:57 -0500192 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500193 kernfs_refresh_inode(kn, inode);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500194 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500195
196 generic_fillattr(inode, stat);
197 return 0;
198}
199
Tejun Heoc637b8a2013-12-11 14:11:58 -0500200static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500201{
Tejun Heo324a56e2013-12-11 14:11:53 -0500202 kernfs_get(kn);
203 inode->i_private = kn;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500204 inode->i_mapping->a_ops = &kernfs_aops;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500205 inode->i_op = &kernfs_iops;
Tejun Heo67c04962019-11-04 15:54:30 -0800206 inode->i_generation = kernfs_gen(kn);
Tejun Heoffed24e2013-11-28 14:54:32 -0500207
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500208 set_default_inode_attr(inode, kn->mode);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500209 kernfs_refresh_inode(kn, inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500210
211 /* initialize inode according to type */
Tejun Heodf23fc32013-12-11 14:11:56 -0500212 switch (kernfs_type(kn)) {
213 case KERNFS_DIR:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500214 inode->i_op = &kernfs_dir_iops;
215 inode->i_fop = &kernfs_dir_fops;
Eric W. Biedermanea015212015-05-13 16:09:29 -0500216 if (kn->flags & KERNFS_EMPTY_DIR)
217 make_empty_dir_inode(inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500218 break;
Tejun Heodf23fc32013-12-11 14:11:56 -0500219 case KERNFS_FILE:
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500220 inode->i_size = kn->attr.size;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500221 inode->i_fop = &kernfs_file_fops;
Tejun Heoffed24e2013-11-28 14:54:32 -0500222 break;
Tejun Heodf23fc32013-12-11 14:11:56 -0500223 case KERNFS_LINK:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500224 inode->i_op = &kernfs_symlink_iops;
Tejun Heoffed24e2013-11-28 14:54:32 -0500225 break;
226 default:
227 BUG();
228 }
229
230 unlock_new_inode(inode);
231}
232
233/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500234 * kernfs_get_inode - get inode for kernfs_node
Tejun Heoffed24e2013-11-28 14:54:32 -0500235 * @sb: super block
Tejun Heo324a56e2013-12-11 14:11:53 -0500236 * @kn: kernfs_node to allocate inode for
Tejun Heoffed24e2013-11-28 14:54:32 -0500237 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500238 * Get inode for @kn. If such inode doesn't exist, a new inode is
239 * allocated and basics are initialized. New inode is returned
240 * locked.
Tejun Heoffed24e2013-11-28 14:54:32 -0500241 *
242 * LOCKING:
243 * Kernel thread context (may sleep).
244 *
245 * RETURNS:
246 * Pointer to allocated inode on success, NULL on failure.
247 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500248struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
Tejun Heoffed24e2013-11-28 14:54:32 -0500249{
250 struct inode *inode;
251
Tejun Heo67c04962019-11-04 15:54:30 -0800252 inode = iget_locked(sb, kernfs_ino(kn));
Tejun Heoffed24e2013-11-28 14:54:32 -0500253 if (inode && (inode->i_state & I_NEW))
Tejun Heoc637b8a2013-12-11 14:11:58 -0500254 kernfs_init_inode(kn, inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500255
256 return inode;
257}
258
259/*
Tejun Heoc637b8a2013-12-11 14:11:58 -0500260 * The kernfs_node serves as both an inode and a directory entry for
261 * kernfs. To prevent the kernfs inode numbers from being freed
262 * prematurely we take a reference to kernfs_node from the kernfs inode. A
Tejun Heoffed24e2013-11-28 14:54:32 -0500263 * super_operations.evict_inode() implementation is needed to drop that
264 * reference upon inode destruction.
265 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500266void kernfs_evict_inode(struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500267{
Tejun Heo324a56e2013-12-11 14:11:53 -0500268 struct kernfs_node *kn = inode->i_private;
Tejun Heoffed24e2013-11-28 14:54:32 -0500269
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700270 truncate_inode_pages_final(&inode->i_data);
Tejun Heoffed24e2013-11-28 14:54:32 -0500271 clear_inode(inode);
Tejun Heo324a56e2013-12-11 14:11:53 -0500272 kernfs_put(kn);
Tejun Heoffed24e2013-11-28 14:54:32 -0500273}
274
Tejun Heoc637b8a2013-12-11 14:11:58 -0500275int kernfs_iop_permission(struct inode *inode, int mask)
Tejun Heoffed24e2013-11-28 14:54:32 -0500276{
Tejun Heo324a56e2013-12-11 14:11:53 -0500277 struct kernfs_node *kn;
Tejun Heoffed24e2013-11-28 14:54:32 -0500278
279 if (mask & MAY_NOT_BLOCK)
280 return -ECHILD;
281
Tejun Heo324a56e2013-12-11 14:11:53 -0500282 kn = inode->i_private;
Tejun Heoffed24e2013-11-28 14:54:32 -0500283
Tejun Heoa797bfc2013-12-11 14:11:57 -0500284 mutex_lock(&kernfs_mutex);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500285 kernfs_refresh_inode(kn, inode);
Tejun Heoa797bfc2013-12-11 14:11:57 -0500286 mutex_unlock(&kernfs_mutex);
Tejun Heoffed24e2013-11-28 14:54:32 -0500287
288 return generic_permission(inode, mask);
289}
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200290
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200291int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
292 void *value, size_t size)
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200293{
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200294 struct kernfs_iattrs *attrs = kernfs_iattrs_noalloc(kn);
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200295 if (!attrs)
Ondrej Mosnacekd0c9c152019-02-22 15:57:13 +0100296 return -ENODATA;
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200297
298 return simple_xattr_get(&attrs->xattrs, name, value, size);
299}
300
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200301int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
302 const void *value, size_t size, int flags)
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200303{
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200304 struct kernfs_iattrs *attrs = kernfs_iattrs(kn);
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200305 if (!attrs)
306 return -ENOMEM;
307
Daniel Xua46a2292020-03-12 13:03:15 -0700308 return simple_xattr_set(&attrs->xattrs, name, value, size, flags, NULL);
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200309}
310
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200311static int kernfs_vfs_xattr_get(const struct xattr_handler *handler,
312 struct dentry *unused, struct inode *inode,
313 const char *suffix, void *value, size_t size)
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100314{
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200315 const char *name = xattr_full_name(handler, suffix);
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100316 struct kernfs_node *kn = inode->i_private;
317
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200318 return kernfs_xattr_get(kn, name, value, size);
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100319}
320
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200321static int kernfs_vfs_xattr_set(const struct xattr_handler *handler,
322 struct dentry *unused, struct inode *inode,
323 const char *suffix, const void *value,
324 size_t size, int flags)
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100325{
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200326 const char *name = xattr_full_name(handler, suffix);
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100327 struct kernfs_node *kn = inode->i_private;
328
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200329 return kernfs_xattr_set(kn, name, value, size, flags);
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100330}
331
Daniel Xu0c473832020-03-12 13:03:16 -0700332static int kernfs_vfs_user_xattr_add(struct kernfs_node *kn,
333 const char *full_name,
334 struct simple_xattrs *xattrs,
335 const void *value, size_t size, int flags)
336{
337 atomic_t *sz = &kn->iattr->user_xattr_size;
338 atomic_t *nr = &kn->iattr->nr_user_xattrs;
339 ssize_t removed_size;
340 int ret;
341
342 if (atomic_inc_return(nr) > KERNFS_MAX_USER_XATTRS) {
343 ret = -ENOSPC;
344 goto dec_count_out;
345 }
346
347 if (atomic_add_return(size, sz) > KERNFS_USER_XATTR_SIZE_LIMIT) {
348 ret = -ENOSPC;
349 goto dec_size_out;
350 }
351
352 ret = simple_xattr_set(xattrs, full_name, value, size, flags,
353 &removed_size);
354
355 if (!ret && removed_size >= 0)
356 size = removed_size;
357 else if (!ret)
358 return 0;
359dec_size_out:
360 atomic_sub(size, sz);
361dec_count_out:
362 atomic_dec(nr);
363 return ret;
364}
365
366static int kernfs_vfs_user_xattr_rm(struct kernfs_node *kn,
367 const char *full_name,
368 struct simple_xattrs *xattrs,
369 const void *value, size_t size, int flags)
370{
371 atomic_t *sz = &kn->iattr->user_xattr_size;
372 atomic_t *nr = &kn->iattr->nr_user_xattrs;
373 ssize_t removed_size;
374 int ret;
375
376 ret = simple_xattr_set(xattrs, full_name, value, size, flags,
377 &removed_size);
378
379 if (removed_size >= 0) {
380 atomic_sub(removed_size, sz);
381 atomic_dec(nr);
382 }
383
384 return ret;
385}
386
387static int kernfs_vfs_user_xattr_set(const struct xattr_handler *handler,
388 struct dentry *unused, struct inode *inode,
389 const char *suffix, const void *value,
390 size_t size, int flags)
391{
392 const char *full_name = xattr_full_name(handler, suffix);
393 struct kernfs_node *kn = inode->i_private;
394 struct kernfs_iattrs *attrs;
395
396 if (!(kernfs_root(kn)->flags & KERNFS_ROOT_SUPPORT_USER_XATTR))
397 return -EOPNOTSUPP;
398
399 attrs = kernfs_iattrs(kn);
400 if (!attrs)
401 return -ENOMEM;
402
403 if (value)
404 return kernfs_vfs_user_xattr_add(kn, full_name, &attrs->xattrs,
405 value, size, flags);
406 else
407 return kernfs_vfs_user_xattr_rm(kn, full_name, &attrs->xattrs,
408 value, size, flags);
409
410}
411
Bart Van Asscheb5a06232016-11-11 16:45:58 -0800412static const struct xattr_handler kernfs_trusted_xattr_handler = {
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200413 .prefix = XATTR_TRUSTED_PREFIX,
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200414 .get = kernfs_vfs_xattr_get,
415 .set = kernfs_vfs_xattr_set,
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200416};
417
Bart Van Asscheb5a06232016-11-11 16:45:58 -0800418static const struct xattr_handler kernfs_security_xattr_handler = {
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200419 .prefix = XATTR_SECURITY_PREFIX,
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200420 .get = kernfs_vfs_xattr_get,
421 .set = kernfs_vfs_xattr_set,
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200422};
423
Daniel Xu0c473832020-03-12 13:03:16 -0700424static const struct xattr_handler kernfs_user_xattr_handler = {
425 .prefix = XATTR_USER_PREFIX,
426 .get = kernfs_vfs_xattr_get,
427 .set = kernfs_vfs_user_xattr_set,
428};
429
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200430const struct xattr_handler *kernfs_xattr_handlers[] = {
431 &kernfs_trusted_xattr_handler,
432 &kernfs_security_xattr_handler,
Daniel Xu0c473832020-03-12 13:03:16 -0700433 &kernfs_user_xattr_handler,
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200434 NULL
435};