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