blob: 3d783d80f5daab2d1b8cb8681aa39ef5ca10d9fd [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 inode_operations kernfs_iops = {
Tejun Heoc637b8a2013-12-11 14:11:58 -050021 .permission = kernfs_iop_permission,
22 .setattr = kernfs_iop_setattr,
23 .getattr = kernfs_iop_getattr,
Tejun Heoc637b8a2013-12-11 14:11:58 -050024 .listxattr = kernfs_iop_listxattr,
Tejun Heoffed24e2013-11-28 14:54:32 -050025};
26
Ondrej Mosnacekd0c9c152019-02-22 15:57:13 +010027static struct kernfs_iattrs *__kernfs_iattrs(struct kernfs_node *kn, int alloc)
Tejun Heoffed24e2013-11-28 14:54:32 -050028{
Tejun Heo4afddd62014-04-02 16:40:52 -040029 static DEFINE_MUTEX(iattr_mutex);
30 struct kernfs_iattrs *ret;
Tejun Heoffed24e2013-11-28 14:54:32 -050031
Tejun Heo4afddd62014-04-02 16:40:52 -040032 mutex_lock(&iattr_mutex);
33
Ondrej Mosnacekd0c9c152019-02-22 15:57:13 +010034 if (kn->iattr || !alloc)
Tejun Heo4afddd62014-04-02 16:40:52 -040035 goto out_unlock;
Tejun Heo9a8049a2013-11-23 17:40:01 -050036
Ayush Mittal26e28d62019-02-06 10:25:42 +053037 kn->iattr = kmem_cache_zalloc(kernfs_iattrs_cache, GFP_KERNEL);
Tejun Heoadc5e8b2013-12-11 14:11:54 -050038 if (!kn->iattr)
Tejun Heo4afddd62014-04-02 16:40:52 -040039 goto out_unlock;
Tejun Heoffed24e2013-11-28 14:54:32 -050040
41 /* assign default attributes */
Ondrej Mosnacek05895212019-02-22 15:57:12 +010042 kn->iattr->ia_uid = GLOBAL_ROOT_UID;
43 kn->iattr->ia_gid = GLOBAL_ROOT_GID;
Deepa Dinamani3a3a5fe2016-02-22 07:17:53 -080044
Ondrej Mosnacek05895212019-02-22 15:57:12 +010045 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;
Tejun Heoffed24e2013-11-28 14:54:32 -050048
Tejun Heoadc5e8b2013-12-11 14:11:54 -050049 simple_xattrs_init(&kn->iattr->xattrs);
Daniel Xu0c473832020-03-12 13:03:16 -070050 atomic_set(&kn->iattr->nr_user_xattrs, 0);
51 atomic_set(&kn->iattr->user_xattr_size, 0);
Tejun Heo4afddd62014-04-02 16:40:52 -040052out_unlock:
53 ret = kn->iattr;
54 mutex_unlock(&iattr_mutex);
55 return ret;
Tejun Heoffed24e2013-11-28 14:54:32 -050056}
57
Ondrej Mosnacekd0c9c152019-02-22 15:57:13 +010058static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
59{
60 return __kernfs_iattrs(kn, 1);
61}
62
63static struct kernfs_iattrs *kernfs_iattrs_noalloc(struct kernfs_node *kn)
64{
65 return __kernfs_iattrs(kn, 0);
66}
67
Dmitry Torokhov488dee92018-07-20 21:56:47 +000068int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -050069{
Tejun Heoc525aad2013-12-11 14:11:55 -050070 struct kernfs_iattrs *attrs;
Tejun Heoffed24e2013-11-28 14:54:32 -050071 unsigned int ia_valid = iattr->ia_valid;
72
Tejun Heoc525aad2013-12-11 14:11:55 -050073 attrs = kernfs_iattrs(kn);
Tejun Heo9a8049a2013-11-23 17:40:01 -050074 if (!attrs)
75 return -ENOMEM;
Tejun Heoffed24e2013-11-28 14:54:32 -050076
Tejun Heoffed24e2013-11-28 14:54:32 -050077 if (ia_valid & ATTR_UID)
Ondrej Mosnacek05895212019-02-22 15:57:12 +010078 attrs->ia_uid = iattr->ia_uid;
Tejun Heoffed24e2013-11-28 14:54:32 -050079 if (ia_valid & ATTR_GID)
Ondrej Mosnacek05895212019-02-22 15:57:12 +010080 attrs->ia_gid = iattr->ia_gid;
Tejun Heoffed24e2013-11-28 14:54:32 -050081 if (ia_valid & ATTR_ATIME)
Ondrej Mosnacek05895212019-02-22 15:57:12 +010082 attrs->ia_atime = iattr->ia_atime;
Tejun Heoffed24e2013-11-28 14:54:32 -050083 if (ia_valid & ATTR_MTIME)
Ondrej Mosnacek05895212019-02-22 15:57:12 +010084 attrs->ia_mtime = iattr->ia_mtime;
Tejun Heoffed24e2013-11-28 14:54:32 -050085 if (ia_valid & ATTR_CTIME)
Ondrej Mosnacek05895212019-02-22 15:57:12 +010086 attrs->ia_ctime = iattr->ia_ctime;
87 if (ia_valid & ATTR_MODE)
88 kn->mode = iattr->ia_mode;
Tejun Heoffed24e2013-11-28 14:54:32 -050089 return 0;
90}
91
92/**
93 * kernfs_setattr - set iattr on a node
Tejun Heo324a56e2013-12-11 14:11:53 -050094 * @kn: target node
Tejun Heoffed24e2013-11-28 14:54:32 -050095 * @iattr: iattr to set
96 *
97 * Returns 0 on success, -errno on failure.
98 */
Tejun Heo324a56e2013-12-11 14:11:53 -050099int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -0500100{
101 int ret;
Minchan Kim393c3712021-11-18 15:00:08 -0800102 struct kernfs_root *root = kernfs_root(kn);
Tejun Heoffed24e2013-11-28 14:54:32 -0500103
Minchan Kim393c3712021-11-18 15:00:08 -0800104 down_write(&root->kernfs_rwsem);
Tejun Heo324a56e2013-12-11 14:11:53 -0500105 ret = __kernfs_setattr(kn, iattr);
Minchan Kim393c3712021-11-18 15:00:08 -0800106 up_write(&root->kernfs_rwsem);
Tejun Heoffed24e2013-11-28 14:54:32 -0500107 return ret;
108}
109
Christian Brauner549c7292021-01-21 14:19:43 +0100110int kernfs_iop_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
111 struct iattr *iattr)
Tejun Heoffed24e2013-11-28 14:54:32 -0500112{
David Howells2b0143b2015-03-17 22:25:59 +0000113 struct inode *inode = d_inode(dentry);
Shaohua Li319ba912017-07-12 11:49:49 -0700114 struct kernfs_node *kn = inode->i_private;
Minchan Kim393c3712021-11-18 15:00:08 -0800115 struct kernfs_root *root;
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
Minchan Kim393c3712021-11-18 15:00:08 -0800121 root = kernfs_root(kn);
122 down_write(&root->kernfs_rwsem);
Christian Brauner2f221d62021-01-21 14:19:26 +0100123 error = setattr_prepare(&init_user_ns, dentry, iattr);
Tejun Heoffed24e2013-11-28 14:54:32 -0500124 if (error)
125 goto out;
126
Tejun Heo324a56e2013-12-11 14:11:53 -0500127 error = __kernfs_setattr(kn, iattr);
Tejun Heoffed24e2013-11-28 14:54:32 -0500128 if (error)
129 goto out;
130
131 /* this ignores size changes */
Christian Brauner2f221d62021-01-21 14:19:26 +0100132 setattr_copy(&init_user_ns, inode, iattr);
Tejun Heoffed24e2013-11-28 14:54:32 -0500133
134out:
Minchan Kim393c3712021-11-18 15:00:08 -0800135 up_write(&root->kernfs_rwsem);
Tejun Heoffed24e2013-11-28 14:54:32 -0500136 return error;
137}
138
Tejun Heoc637b8a2013-12-11 14:11:58 -0500139ssize_t kernfs_iop_listxattr(struct dentry *dentry, char *buf, size_t size)
Tejun Heo23223922013-11-23 17:40:02 -0500140{
Shaohua Li319ba912017-07-12 11:49:49 -0700141 struct kernfs_node *kn = kernfs_dentry_node(dentry);
Tejun Heoc525aad2013-12-11 14:11:55 -0500142 struct kernfs_iattrs *attrs;
Tejun Heo23223922013-11-23 17:40:02 -0500143
Tejun Heoc525aad2013-12-11 14:11:55 -0500144 attrs = kernfs_iattrs(kn);
Tejun Heo23223922013-11-23 17:40:02 -0500145 if (!attrs)
146 return -ENOMEM;
147
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100148 return simple_xattr_list(d_inode(dentry), &attrs->xattrs, buf, size);
Tejun Heoffed24e2013-11-28 14:54:32 -0500149}
150
151static inline void set_default_inode_attr(struct inode *inode, umode_t mode)
152{
153 inode->i_mode = mode;
Deepa Dinamani3a3a5fe2016-02-22 07:17:53 -0800154 inode->i_atime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -0700155 inode->i_ctime = current_time(inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500156}
157
Ondrej Mosnacek05895212019-02-22 15:57:12 +0100158static inline void set_inode_attr(struct inode *inode,
159 struct kernfs_iattrs *attrs)
Tejun Heoffed24e2013-11-28 14:54:32 -0500160{
Ondrej Mosnacek05895212019-02-22 15:57:12 +0100161 inode->i_uid = attrs->ia_uid;
162 inode->i_gid = attrs->ia_gid;
Al Virof0f35882019-12-07 21:51:31 -0500163 inode->i_atime = attrs->ia_atime;
164 inode->i_mtime = attrs->ia_mtime;
165 inode->i_ctime = attrs->ia_ctime;
Tejun Heoffed24e2013-11-28 14:54:32 -0500166}
167
Tejun Heoc637b8a2013-12-11 14:11:58 -0500168static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500169{
Tejun Heoc525aad2013-12-11 14:11:55 -0500170 struct kernfs_iattrs *attrs = kn->iattr;
Tejun Heoffed24e2013-11-28 14:54:32 -0500171
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500172 inode->i_mode = kn->mode;
Ondrej Mosnacek0ac60752019-02-22 15:57:15 +0100173 if (attrs)
Tejun Heo324a56e2013-12-11 14:11:53 -0500174 /*
175 * kernfs_node has non-default attributes get them from
176 * persistent copy in kernfs_node.
Tejun Heoffed24e2013-11-28 14:54:32 -0500177 */
Ondrej Mosnacek05895212019-02-22 15:57:12 +0100178 set_inode_attr(inode, attrs);
Tejun Heoffed24e2013-11-28 14:54:32 -0500179
Tejun Heodf23fc32013-12-11 14:11:56 -0500180 if (kernfs_type(kn) == KERNFS_DIR)
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500181 set_nlink(inode, kn->dir.subdirs + 2);
Tejun Heoffed24e2013-11-28 14:54:32 -0500182}
183
Christian Brauner549c7292021-01-21 14:19:43 +0100184int kernfs_iop_getattr(struct user_namespace *mnt_userns,
185 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +0000186 u32 request_mask, unsigned int query_flags)
Tejun Heoffed24e2013-11-28 14:54:32 -0500187{
David Howellsa528d352017-01-31 16:46:22 +0000188 struct inode *inode = d_inode(path->dentry);
Shaohua Li319ba912017-07-12 11:49:49 -0700189 struct kernfs_node *kn = inode->i_private;
Minchan Kim393c3712021-11-18 15:00:08 -0800190 struct kernfs_root *root = kernfs_root(kn);
Tejun Heoffed24e2013-11-28 14:54:32 -0500191
Minchan Kim393c3712021-11-18 15:00:08 -0800192 down_read(&root->kernfs_rwsem);
Ian Kent47b5c642021-07-16 17:28:34 +0800193 spin_lock(&inode->i_lock);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500194 kernfs_refresh_inode(kn, inode);
Christian Brauner0d56a452021-01-21 14:19:30 +0100195 generic_fillattr(&init_user_ns, inode, stat);
Ian Kent47b5c642021-07-16 17:28:34 +0800196 spin_unlock(&inode->i_lock);
Minchan Kim393c3712021-11-18 15:00:08 -0800197 up_read(&root->kernfs_rwsem);
Ian Kent47b5c642021-07-16 17:28:34 +0800198
Tejun Heoffed24e2013-11-28 14:54:32 -0500199 return 0;
200}
201
Tejun Heoc637b8a2013-12-11 14:11:58 -0500202static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500203{
Tejun Heo324a56e2013-12-11 14:11:53 -0500204 kernfs_get(kn);
205 inode->i_private = kn;
Christoph Hellwigc1e3dbe2021-06-28 19:36:09 -0700206 inode->i_mapping->a_ops = &ram_aops;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500207 inode->i_op = &kernfs_iops;
Tejun Heo67c04962019-11-04 15:54:30 -0800208 inode->i_generation = kernfs_gen(kn);
Tejun Heoffed24e2013-11-28 14:54:32 -0500209
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500210 set_default_inode_attr(inode, kn->mode);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500211 kernfs_refresh_inode(kn, inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500212
213 /* initialize inode according to type */
Tejun Heodf23fc32013-12-11 14:11:56 -0500214 switch (kernfs_type(kn)) {
215 case KERNFS_DIR:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500216 inode->i_op = &kernfs_dir_iops;
217 inode->i_fop = &kernfs_dir_fops;
Eric W. Biedermanea015212015-05-13 16:09:29 -0500218 if (kn->flags & KERNFS_EMPTY_DIR)
219 make_empty_dir_inode(inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500220 break;
Tejun Heodf23fc32013-12-11 14:11:56 -0500221 case KERNFS_FILE:
Tejun Heoadc5e8b2013-12-11 14:11:54 -0500222 inode->i_size = kn->attr.size;
Tejun Heoa797bfc2013-12-11 14:11:57 -0500223 inode->i_fop = &kernfs_file_fops;
Tejun Heoffed24e2013-11-28 14:54:32 -0500224 break;
Tejun Heodf23fc32013-12-11 14:11:56 -0500225 case KERNFS_LINK:
Tejun Heoa797bfc2013-12-11 14:11:57 -0500226 inode->i_op = &kernfs_symlink_iops;
Tejun Heoffed24e2013-11-28 14:54:32 -0500227 break;
228 default:
229 BUG();
230 }
231
232 unlock_new_inode(inode);
233}
234
235/**
Tejun Heoc637b8a2013-12-11 14:11:58 -0500236 * kernfs_get_inode - get inode for kernfs_node
Tejun Heoffed24e2013-11-28 14:54:32 -0500237 * @sb: super block
Tejun Heo324a56e2013-12-11 14:11:53 -0500238 * @kn: kernfs_node to allocate inode for
Tejun Heoffed24e2013-11-28 14:54:32 -0500239 *
Tejun Heo324a56e2013-12-11 14:11:53 -0500240 * Get inode for @kn. If such inode doesn't exist, a new inode is
241 * allocated and basics are initialized. New inode is returned
242 * locked.
Tejun Heoffed24e2013-11-28 14:54:32 -0500243 *
244 * LOCKING:
245 * Kernel thread context (may sleep).
246 *
247 * RETURNS:
248 * Pointer to allocated inode on success, NULL on failure.
249 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500250struct inode *kernfs_get_inode(struct super_block *sb, struct kernfs_node *kn)
Tejun Heoffed24e2013-11-28 14:54:32 -0500251{
252 struct inode *inode;
253
Tejun Heo67c04962019-11-04 15:54:30 -0800254 inode = iget_locked(sb, kernfs_ino(kn));
Tejun Heoffed24e2013-11-28 14:54:32 -0500255 if (inode && (inode->i_state & I_NEW))
Tejun Heoc637b8a2013-12-11 14:11:58 -0500256 kernfs_init_inode(kn, inode);
Tejun Heoffed24e2013-11-28 14:54:32 -0500257
258 return inode;
259}
260
261/*
Tejun Heoc637b8a2013-12-11 14:11:58 -0500262 * The kernfs_node serves as both an inode and a directory entry for
263 * kernfs. To prevent the kernfs inode numbers from being freed
264 * prematurely we take a reference to kernfs_node from the kernfs inode. A
Tejun Heoffed24e2013-11-28 14:54:32 -0500265 * super_operations.evict_inode() implementation is needed to drop that
266 * reference upon inode destruction.
267 */
Tejun Heoc637b8a2013-12-11 14:11:58 -0500268void kernfs_evict_inode(struct inode *inode)
Tejun Heoffed24e2013-11-28 14:54:32 -0500269{
Tejun Heo324a56e2013-12-11 14:11:53 -0500270 struct kernfs_node *kn = inode->i_private;
Tejun Heoffed24e2013-11-28 14:54:32 -0500271
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700272 truncate_inode_pages_final(&inode->i_data);
Tejun Heoffed24e2013-11-28 14:54:32 -0500273 clear_inode(inode);
Tejun Heo324a56e2013-12-11 14:11:53 -0500274 kernfs_put(kn);
Tejun Heoffed24e2013-11-28 14:54:32 -0500275}
276
Christian Brauner549c7292021-01-21 14:19:43 +0100277int kernfs_iop_permission(struct user_namespace *mnt_userns,
278 struct inode *inode, int mask)
Tejun Heoffed24e2013-11-28 14:54:32 -0500279{
Tejun Heo324a56e2013-12-11 14:11:53 -0500280 struct kernfs_node *kn;
Minchan Kim393c3712021-11-18 15:00:08 -0800281 struct kernfs_root *root;
Ian Kent47b5c642021-07-16 17:28:34 +0800282 int ret;
Tejun Heoffed24e2013-11-28 14:54:32 -0500283
284 if (mask & MAY_NOT_BLOCK)
285 return -ECHILD;
286
Tejun Heo324a56e2013-12-11 14:11:53 -0500287 kn = inode->i_private;
Minchan Kim393c3712021-11-18 15:00:08 -0800288 root = kernfs_root(kn);
Tejun Heoffed24e2013-11-28 14:54:32 -0500289
Minchan Kim393c3712021-11-18 15:00:08 -0800290 down_read(&root->kernfs_rwsem);
Ian Kent47b5c642021-07-16 17:28:34 +0800291 spin_lock(&inode->i_lock);
Tejun Heoc637b8a2013-12-11 14:11:58 -0500292 kernfs_refresh_inode(kn, inode);
Ian Kent47b5c642021-07-16 17:28:34 +0800293 ret = generic_permission(&init_user_ns, inode, mask);
294 spin_unlock(&inode->i_lock);
Minchan Kim393c3712021-11-18 15:00:08 -0800295 up_read(&root->kernfs_rwsem);
Tejun Heoffed24e2013-11-28 14:54:32 -0500296
Ian Kent47b5c642021-07-16 17:28:34 +0800297 return ret;
Tejun Heoffed24e2013-11-28 14:54:32 -0500298}
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200299
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200300int kernfs_xattr_get(struct kernfs_node *kn, const char *name,
301 void *value, size_t size)
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200302{
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200303 struct kernfs_iattrs *attrs = kernfs_iattrs_noalloc(kn);
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200304 if (!attrs)
Ondrej Mosnacekd0c9c152019-02-22 15:57:13 +0100305 return -ENODATA;
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200306
307 return simple_xattr_get(&attrs->xattrs, name, value, size);
308}
309
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200310int kernfs_xattr_set(struct kernfs_node *kn, const char *name,
311 const void *value, size_t size, int flags)
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200312{
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200313 struct kernfs_iattrs *attrs = kernfs_iattrs(kn);
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200314 if (!attrs)
315 return -ENOMEM;
316
Daniel Xua46a2292020-03-12 13:03:15 -0700317 return simple_xattr_set(&attrs->xattrs, name, value, size, flags, NULL);
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200318}
319
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200320static int kernfs_vfs_xattr_get(const struct xattr_handler *handler,
321 struct dentry *unused, struct inode *inode,
322 const char *suffix, void *value, size_t size)
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100323{
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200324 const char *name = xattr_full_name(handler, suffix);
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100325 struct kernfs_node *kn = inode->i_private;
326
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200327 return kernfs_xattr_get(kn, name, value, size);
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100328}
329
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200330static int kernfs_vfs_xattr_set(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +0100331 struct user_namespace *mnt_userns,
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200332 struct dentry *unused, struct inode *inode,
333 const char *suffix, const void *value,
334 size_t size, int flags)
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100335{
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200336 const char *name = xattr_full_name(handler, suffix);
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100337 struct kernfs_node *kn = inode->i_private;
338
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200339 return kernfs_xattr_set(kn, name, value, size, flags);
Ondrej Mosnacekb230d5a2019-02-22 15:57:16 +0100340}
341
Daniel Xu0c473832020-03-12 13:03:16 -0700342static int kernfs_vfs_user_xattr_add(struct kernfs_node *kn,
343 const char *full_name,
344 struct simple_xattrs *xattrs,
345 const void *value, size_t size, int flags)
346{
347 atomic_t *sz = &kn->iattr->user_xattr_size;
348 atomic_t *nr = &kn->iattr->nr_user_xattrs;
349 ssize_t removed_size;
350 int ret;
351
352 if (atomic_inc_return(nr) > KERNFS_MAX_USER_XATTRS) {
353 ret = -ENOSPC;
354 goto dec_count_out;
355 }
356
357 if (atomic_add_return(size, sz) > KERNFS_USER_XATTR_SIZE_LIMIT) {
358 ret = -ENOSPC;
359 goto dec_size_out;
360 }
361
362 ret = simple_xattr_set(xattrs, full_name, value, size, flags,
363 &removed_size);
364
365 if (!ret && removed_size >= 0)
366 size = removed_size;
367 else if (!ret)
368 return 0;
369dec_size_out:
370 atomic_sub(size, sz);
371dec_count_out:
372 atomic_dec(nr);
373 return ret;
374}
375
376static int kernfs_vfs_user_xattr_rm(struct kernfs_node *kn,
377 const char *full_name,
378 struct simple_xattrs *xattrs,
379 const void *value, size_t size, int flags)
380{
381 atomic_t *sz = &kn->iattr->user_xattr_size;
382 atomic_t *nr = &kn->iattr->nr_user_xattrs;
383 ssize_t removed_size;
384 int ret;
385
386 ret = simple_xattr_set(xattrs, full_name, value, size, flags,
387 &removed_size);
388
389 if (removed_size >= 0) {
390 atomic_sub(removed_size, sz);
391 atomic_dec(nr);
392 }
393
394 return ret;
395}
396
397static int kernfs_vfs_user_xattr_set(const struct xattr_handler *handler,
Christian Braunere65ce2a2021-01-21 14:19:27 +0100398 struct user_namespace *mnt_userns,
Daniel Xu0c473832020-03-12 13:03:16 -0700399 struct dentry *unused, struct inode *inode,
400 const char *suffix, const void *value,
401 size_t size, int flags)
402{
403 const char *full_name = xattr_full_name(handler, suffix);
404 struct kernfs_node *kn = inode->i_private;
405 struct kernfs_iattrs *attrs;
406
407 if (!(kernfs_root(kn)->flags & KERNFS_ROOT_SUPPORT_USER_XATTR))
408 return -EOPNOTSUPP;
409
410 attrs = kernfs_iattrs(kn);
411 if (!attrs)
412 return -ENOMEM;
413
414 if (value)
415 return kernfs_vfs_user_xattr_add(kn, full_name, &attrs->xattrs,
416 value, size, flags);
417 else
418 return kernfs_vfs_user_xattr_rm(kn, full_name, &attrs->xattrs,
419 value, size, flags);
420
421}
422
Bart Van Asscheb5a06232016-11-11 16:45:58 -0800423static const struct xattr_handler kernfs_trusted_xattr_handler = {
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200424 .prefix = XATTR_TRUSTED_PREFIX,
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200425 .get = kernfs_vfs_xattr_get,
426 .set = kernfs_vfs_xattr_set,
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200427};
428
Bart Van Asscheb5a06232016-11-11 16:45:58 -0800429static const struct xattr_handler kernfs_security_xattr_handler = {
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200430 .prefix = XATTR_SECURITY_PREFIX,
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +0200431 .get = kernfs_vfs_xattr_get,
432 .set = kernfs_vfs_xattr_set,
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200433};
434
Daniel Xu0c473832020-03-12 13:03:16 -0700435static const struct xattr_handler kernfs_user_xattr_handler = {
436 .prefix = XATTR_USER_PREFIX,
437 .get = kernfs_vfs_xattr_get,
438 .set = kernfs_vfs_user_xattr_set,
439};
440
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200441const struct xattr_handler *kernfs_xattr_handlers[] = {
442 &kernfs_trusted_xattr_handler,
443 &kernfs_security_xattr_handler,
Daniel Xu0c473832020-03-12 13:03:16 -0700444 &kernfs_user_xattr_handler,
Andreas Gruenbachere72a1a82016-09-29 17:48:33 +0200445 NULL
446};