]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/f2fs/xattr.c
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma
[mirror_ubuntu-jammy-kernel.git] / fs / f2fs / xattr.c
CommitLineData
7c1a000d 1// SPDX-License-Identifier: GPL-2.0
0a8165d7 2/*
af48b85b
JK
3 * fs/f2fs/xattr.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 *
8 * Portions of this code from linux/fs/ext2/xattr.c
9 *
10 * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
11 *
12 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
13 * Extended attributes for symlinks and special files added per
14 * suggestion of Luka Renko <luka.renko@hermes.si>.
15 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
16 * Red Hat Inc.
af48b85b
JK
17 */
18#include <linux/rwsem.h>
19#include <linux/f2fs_fs.h>
8ae8f162 20#include <linux/security.h>
a6dda0e6 21#include <linux/posix_acl_xattr.h>
af48b85b
JK
22#include "f2fs.h"
23#include "xattr.h"
955ebcd3 24#include "segment.h"
af48b85b 25
a999150f
CY
26static void *xattr_alloc(struct f2fs_sb_info *sbi, int size, bool *is_inline)
27{
28 if (likely(size == sbi->inline_xattr_slab_size)) {
29 *is_inline = true;
32410577
CY
30 return f2fs_kmem_cache_alloc(sbi->inline_xattr_slab,
31 GFP_F2FS_ZERO, false, sbi);
a999150f
CY
32 }
33 *is_inline = false;
34 return f2fs_kzalloc(sbi, size, GFP_NOFS);
35}
36
37static void xattr_free(struct f2fs_sb_info *sbi, void *xattr_addr,
38 bool is_inline)
39{
40 if (is_inline)
41 kmem_cache_free(sbi->inline_xattr_slab, xattr_addr);
42 else
c8eb7024 43 kfree(xattr_addr);
a999150f
CY
44}
45
d9a82a04 46static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
b296821a
AV
47 struct dentry *unused, struct inode *inode,
48 const char *name, void *buffer, size_t size)
af48b85b 49{
b296821a 50 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
af48b85b 51
d9a82a04 52 switch (handler->flags) {
af48b85b
JK
53 case F2FS_XATTR_INDEX_USER:
54 if (!test_opt(sbi, XATTR_USER))
55 return -EOPNOTSUPP;
56 break;
57 case F2FS_XATTR_INDEX_TRUSTED:
8ae8f162
JK
58 case F2FS_XATTR_INDEX_SECURITY:
59 break;
af48b85b
JK
60 default:
61 return -EINVAL;
62 }
b296821a 63 return f2fs_getxattr(inode, handler->flags, name,
d9a82a04 64 buffer, size, NULL);
af48b85b
JK
65}
66
d9a82a04 67static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
e65ce2a5 68 struct user_namespace *mnt_userns,
59301226
AV
69 struct dentry *unused, struct inode *inode,
70 const char *name, const void *value,
d9a82a04 71 size_t size, int flags)
af48b85b 72{
59301226 73 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
af48b85b 74
d9a82a04 75 switch (handler->flags) {
af48b85b
JK
76 case F2FS_XATTR_INDEX_USER:
77 if (!test_opt(sbi, XATTR_USER))
78 return -EOPNOTSUPP;
79 break;
80 case F2FS_XATTR_INDEX_TRUSTED:
8ae8f162
JK
81 case F2FS_XATTR_INDEX_SECURITY:
82 break;
af48b85b
JK
83 default:
84 return -EINVAL;
85 }
59301226 86 return f2fs_setxattr(inode, handler->flags, name,
c02745ef 87 value, size, NULL, flags);
af48b85b
JK
88}
89
764a5c6b 90static bool f2fs_xattr_user_list(struct dentry *dentry)
573ea5fc 91{
764a5c6b
AG
92 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
93
94 return test_opt(sbi, XATTR_USER);
95}
573ea5fc 96
764a5c6b
AG
97static bool f2fs_xattr_trusted_list(struct dentry *dentry)
98{
99 return capable(CAP_SYS_ADMIN);
573ea5fc
JK
100}
101
d9a82a04 102static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
b296821a
AV
103 struct dentry *unused, struct inode *inode,
104 const char *name, void *buffer, size_t size)
573ea5fc 105{
84e97c27
CY
106 if (buffer)
107 *((char *)buffer) = F2FS_I(inode)->i_advise;
573ea5fc
JK
108 return sizeof(char);
109}
110
d9a82a04 111static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
e65ce2a5 112 struct user_namespace *mnt_userns,
59301226
AV
113 struct dentry *unused, struct inode *inode,
114 const char *name, const void *value,
d9a82a04 115 size_t size, int flags)
573ea5fc 116{
797c1cb5
CY
117 unsigned char old_advise = F2FS_I(inode)->i_advise;
118 unsigned char new_advise;
119
21cb47be 120 if (!inode_owner_or_capable(&init_user_ns, inode))
573ea5fc
JK
121 return -EPERM;
122 if (value == NULL)
123 return -EINVAL;
124
797c1cb5
CY
125 new_advise = *(char *)value;
126 if (new_advise & ~FADVISE_MODIFIABLE_BITS)
127 return -EINVAL;
128
129 new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
130 new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
131
132 F2FS_I(inode)->i_advise = new_advise;
7c45729a 133 f2fs_mark_inode_dirty_sync(inode, true);
573ea5fc
JK
134 return 0;
135}
136
8ae8f162
JK
137#ifdef CONFIG_F2FS_FS_SECURITY
138static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
139 void *page)
140{
141 const struct xattr *xattr;
142 int err = 0;
143
144 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
d631abda 145 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
8ae8f162 146 xattr->name, xattr->value,
c02745ef 147 xattr->value_len, (struct page *)page, 0);
8ae8f162
JK
148 if (err < 0)
149 break;
150 }
151 return err;
152}
153
154int f2fs_init_security(struct inode *inode, struct inode *dir,
155 const struct qstr *qstr, struct page *ipage)
156{
157 return security_inode_init_security(inode, dir, qstr,
158 &f2fs_initxattrs, ipage);
159}
160#endif
161
af48b85b
JK
162const struct xattr_handler f2fs_xattr_user_handler = {
163 .prefix = XATTR_USER_PREFIX,
164 .flags = F2FS_XATTR_INDEX_USER,
764a5c6b 165 .list = f2fs_xattr_user_list,
af48b85b
JK
166 .get = f2fs_xattr_generic_get,
167 .set = f2fs_xattr_generic_set,
168};
169
170const struct xattr_handler f2fs_xattr_trusted_handler = {
171 .prefix = XATTR_TRUSTED_PREFIX,
172 .flags = F2FS_XATTR_INDEX_TRUSTED,
764a5c6b 173 .list = f2fs_xattr_trusted_list,
af48b85b
JK
174 .get = f2fs_xattr_generic_get,
175 .set = f2fs_xattr_generic_set,
176};
177
573ea5fc 178const struct xattr_handler f2fs_xattr_advise_handler = {
98e9cb57 179 .name = F2FS_SYSTEM_ADVISE_NAME,
573ea5fc 180 .flags = F2FS_XATTR_INDEX_ADVISE,
a87aff1d
JQ
181 .get = f2fs_xattr_advise_get,
182 .set = f2fs_xattr_advise_set,
573ea5fc
JK
183};
184
8ae8f162
JK
185const struct xattr_handler f2fs_xattr_security_handler = {
186 .prefix = XATTR_SECURITY_PREFIX,
187 .flags = F2FS_XATTR_INDEX_SECURITY,
8ae8f162
JK
188 .get = f2fs_xattr_generic_get,
189 .set = f2fs_xattr_generic_set,
190};
191
af48b85b
JK
192static const struct xattr_handler *f2fs_xattr_handler_map[] = {
193 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
194#ifdef CONFIG_F2FS_FS_POSIX_ACL
a6dda0e6
CH
195 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
196 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
af48b85b
JK
197#endif
198 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
8ae8f162
JK
199#ifdef CONFIG_F2FS_FS_SECURITY
200 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
201#endif
af48b85b
JK
202 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
203};
204
205const struct xattr_handler *f2fs_xattr_handlers[] = {
206 &f2fs_xattr_user_handler,
207#ifdef CONFIG_F2FS_FS_POSIX_ACL
a6dda0e6
CH
208 &posix_acl_access_xattr_handler,
209 &posix_acl_default_xattr_handler,
af48b85b
JK
210#endif
211 &f2fs_xattr_trusted_handler,
8ae8f162
JK
212#ifdef CONFIG_F2FS_FS_SECURITY
213 &f2fs_xattr_security_handler,
214#endif
af48b85b
JK
215 &f2fs_xattr_advise_handler,
216 NULL,
217};
218
e1123268 219static inline const struct xattr_handler *f2fs_xattr_handler(int index)
af48b85b
JK
220{
221 const struct xattr_handler *handler = NULL;
222
e1123268
JK
223 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
224 handler = f2fs_xattr_handler_map[index];
af48b85b
JK
225 return handler;
226}
227
2777e654
RH
228static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
229 void *last_base_addr, int index,
230 size_t len, const char *name)
dd9cfe23
JK
231{
232 struct f2fs_xattr_entry *entry;
233
234 list_for_each_xattr(entry, base_addr) {
2777e654
RH
235 if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
236 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr)
237 return NULL;
238
e1123268 239 if (entry->e_name_index != index)
dd9cfe23 240 continue;
e1123268 241 if (entry->e_name_len != len)
dd9cfe23 242 continue;
e1123268 243 if (!memcmp(entry->e_name, name, len))
dd9cfe23
JK
244 break;
245 }
246 return entry;
247}
248
6afc662e
CY
249static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
250 void *base_addr, void **last_addr, int index,
251 size_t len, const char *name)
ba38c27e
CY
252{
253 struct f2fs_xattr_entry *entry;
6afc662e 254 unsigned int inline_size = inline_xattr_size(inode);
2c28aba8 255 void *max_addr = base_addr + inline_size;
ba38c27e
CY
256
257 list_for_each_xattr(entry, base_addr) {
2c28aba8
CY
258 if ((void *)entry + sizeof(__u32) > max_addr ||
259 (void *)XATTR_NEXT_ENTRY(entry) > max_addr) {
ba38c27e
CY
260 *last_addr = entry;
261 return NULL;
262 }
263 if (entry->e_name_index != index)
264 continue;
265 if (entry->e_name_len != len)
266 continue;
267 if (!memcmp(entry->e_name, name, len))
268 break;
269 }
2c28aba8
CY
270
271 /* inline xattr header or entry across max inline xattr size */
272 if (IS_XATTR_LAST_ENTRY(entry) &&
273 (void *)entry + sizeof(__u32) > max_addr) {
274 *last_addr = entry;
275 return NULL;
276 }
ba38c27e
CY
277 return entry;
278}
279
a5f433f7
CY
280static int read_inline_xattr(struct inode *inode, struct page *ipage,
281 void *txattr_addr)
282{
283 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
284 unsigned int inline_size = inline_xattr_size(inode);
285 struct page *page = NULL;
286 void *inline_addr;
287
288 if (ipage) {
6afc662e 289 inline_addr = inline_xattr_addr(inode, ipage);
a5f433f7 290 } else {
4d57b86d 291 page = f2fs_get_node_page(sbi, inode->i_ino);
a5f433f7
CY
292 if (IS_ERR(page))
293 return PTR_ERR(page);
294
6afc662e 295 inline_addr = inline_xattr_addr(inode, page);
a5f433f7
CY
296 }
297 memcpy(txattr_addr, inline_addr, inline_size);
298 f2fs_put_page(page, 1);
299
300 return 0;
301}
302
63840695
CY
303static int read_xattr_block(struct inode *inode, void *txattr_addr)
304{
305 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
306 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
307 unsigned int inline_size = inline_xattr_size(inode);
308 struct page *xpage;
309 void *xattr_addr;
310
311 /* The inode already has an extended attribute block. */
4d57b86d 312 xpage = f2fs_get_node_page(sbi, xnid);
63840695
CY
313 if (IS_ERR(xpage))
314 return PTR_ERR(xpage);
315
316 xattr_addr = page_address(xpage);
317 memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
318 f2fs_put_page(xpage, 1);
319
320 return 0;
321}
322
ba38c27e
CY
323static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
324 unsigned int index, unsigned int len,
325 const char *name, struct f2fs_xattr_entry **xe,
a999150f
CY
326 void **base_addr, int *base_size,
327 bool *is_inline)
ba38c27e 328{
2777e654
RH
329 void *cur_addr, *txattr_addr, *last_txattr_addr;
330 void *last_addr = NULL;
ba38c27e 331 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
89e9eabd 332 unsigned int inline_size = inline_xattr_size(inode);
2e0cd472 333 int err;
ba38c27e 334
2777e654 335 if (!xnid && !inline_size)
ba38c27e
CY
336 return -ENODATA;
337
ba3b583c 338 *base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE;
a999150f 339 txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline);
ba38c27e
CY
340 if (!txattr_addr)
341 return -ENOMEM;
342
ba3b583c 343 last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode);
2777e654 344
ba38c27e
CY
345 /* read from inline xattr */
346 if (inline_size) {
a5f433f7
CY
347 err = read_inline_xattr(inode, ipage, txattr_addr);
348 if (err)
349 goto out;
ba38c27e 350
6afc662e 351 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
ba38c27e 352 index, len, name);
64beba05
JK
353 if (*xe) {
354 *base_size = inline_size;
ba38c27e 355 goto check;
64beba05 356 }
ba38c27e
CY
357 }
358
359 /* read from xattr node block */
360 if (xnid) {
63840695
CY
361 err = read_xattr_block(inode, txattr_addr);
362 if (err)
ba38c27e 363 goto out;
ba38c27e
CY
364 }
365
366 if (last_addr)
367 cur_addr = XATTR_HDR(last_addr) - 1;
368 else
369 cur_addr = txattr_addr;
370
2777e654
RH
371 *xe = __find_xattr(cur_addr, last_txattr_addr, index, len, name);
372 if (!*xe) {
c83414ae
CY
373 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
374 inode->i_ino);
375 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
10f966bb 376 err = -EFSCORRUPTED;
2777e654
RH
377 goto out;
378 }
ba38c27e
CY
379check:
380 if (IS_XATTR_LAST_ENTRY(*xe)) {
381 err = -ENODATA;
382 goto out;
383 }
384
385 *base_addr = txattr_addr;
386 return 0;
387out:
a999150f 388 xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline);
ba38c27e
CY
389 return err;
390}
391
86696966
CY
392static int read_all_xattrs(struct inode *inode, struct page *ipage,
393 void **base_addr)
65985d93 394{
65985d93 395 struct f2fs_xattr_header *header;
89e9eabd
CY
396 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
397 unsigned int size = VALID_XATTR_BLOCK_SIZE;
398 unsigned int inline_size = inline_xattr_size(inode);
65985d93 399 void *txattr_addr;
86696966 400 int err;
65985d93 401
acbf054d
CY
402 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
403 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
65985d93 404 if (!txattr_addr)
86696966 405 return -ENOMEM;
65985d93
JK
406
407 /* read from inline xattr */
408 if (inline_size) {
a5f433f7
CY
409 err = read_inline_xattr(inode, ipage, txattr_addr);
410 if (err)
411 goto fail;
65985d93
JK
412 }
413
414 /* read from xattr node block */
89e9eabd 415 if (xnid) {
63840695
CY
416 err = read_xattr_block(inode, txattr_addr);
417 if (err)
65985d93 418 goto fail;
65985d93
JK
419 }
420
421 header = XATTR_HDR(txattr_addr);
422
423 /* never been allocated xattrs */
424 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
425 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
426 header->h_refcount = cpu_to_le32(1);
427 }
86696966
CY
428 *base_addr = txattr_addr;
429 return 0;
65985d93 430fail:
c8eb7024 431 kfree(txattr_addr);
86696966 432 return err;
65985d93
JK
433}
434
435static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
436 void *txattr_addr, struct page *ipage)
437{
4081363f 438 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
89e9eabd 439 size_t inline_size = inline_xattr_size(inode);
bf9c1427 440 struct page *in_page = NULL;
65985d93 441 void *xattr_addr;
bf9c1427 442 void *inline_addr = NULL;
65985d93
JK
443 struct page *xpage;
444 nid_t new_nid = 0;
bf9c1427 445 int err = 0;
65985d93 446
65985d93 447 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
4d57b86d 448 if (!f2fs_alloc_nid(sbi, &new_nid))
65985d93
JK
449 return -ENOSPC;
450
451 /* write to inline xattr */
452 if (inline_size) {
65985d93 453 if (ipage) {
6afc662e 454 inline_addr = inline_xattr_addr(inode, ipage);
65985d93 455 } else {
4d57b86d 456 in_page = f2fs_get_node_page(sbi, inode->i_ino);
bf9c1427 457 if (IS_ERR(in_page)) {
4d57b86d 458 f2fs_alloc_nid_failed(sbi, new_nid);
bf9c1427 459 return PTR_ERR(in_page);
65985d93 460 }
bf9c1427 461 inline_addr = inline_xattr_addr(inode, in_page);
65985d93 462 }
65985d93 463
bf9c1427 464 f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
bae0ee7a 465 NODE, true, true);
65985d93
JK
466 /* no need to use xattr node block */
467 if (hsize <= inline_size) {
4d57b86d
CY
468 err = f2fs_truncate_xattr_node(inode);
469 f2fs_alloc_nid_failed(sbi, new_nid);
bf9c1427
JK
470 if (err) {
471 f2fs_put_page(in_page, 1);
472 return err;
473 }
474 memcpy(inline_addr, txattr_addr, inline_size);
475 set_page_dirty(ipage ? ipage : in_page);
476 goto in_page_out;
65985d93
JK
477 }
478 }
479
480 /* write to xattr node block */
481 if (F2FS_I(inode)->i_xattr_nid) {
4d57b86d 482 xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
65985d93 483 if (IS_ERR(xpage)) {
d620439f 484 err = PTR_ERR(xpage);
4d57b86d 485 f2fs_alloc_nid_failed(sbi, new_nid);
bf9c1427 486 goto in_page_out;
65985d93 487 }
9850cf4a 488 f2fs_bug_on(sbi, new_nid);
bae0ee7a 489 f2fs_wait_on_page_writeback(xpage, NODE, true, true);
65985d93
JK
490 } else {
491 struct dnode_of_data dn;
5f029c04 492
65985d93 493 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
4d57b86d 494 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
65985d93 495 if (IS_ERR(xpage)) {
d620439f 496 err = PTR_ERR(xpage);
4d57b86d 497 f2fs_alloc_nid_failed(sbi, new_nid);
bf9c1427 498 goto in_page_out;
65985d93 499 }
4d57b86d 500 f2fs_alloc_nid_done(sbi, new_nid);
65985d93 501 }
65985d93 502 xattr_addr = page_address(xpage);
bf9c1427
JK
503
504 if (inline_size)
505 memcpy(inline_addr, txattr_addr, inline_size);
22588f87 506 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
bf9c1427
JK
507
508 if (inline_size)
509 set_page_dirty(ipage ? ipage : in_page);
65985d93 510 set_page_dirty(xpage);
65985d93 511
bf9c1427
JK
512 f2fs_put_page(xpage, 1);
513in_page_out:
514 f2fs_put_page(in_page, 1);
515 return err;
65985d93
JK
516}
517
e1123268 518int f2fs_getxattr(struct inode *inode, int index, const char *name,
bce8d112 519 void *buffer, size_t buffer_size, struct page *ipage)
af48b85b 520{
ba38c27e 521 struct f2fs_xattr_entry *entry = NULL;
2e0cd472 522 int error;
ba38c27e 523 unsigned int size, len;
ba38c27e 524 void *base_addr = NULL;
64beba05 525 int base_size;
a999150f 526 bool is_inline;
af48b85b
JK
527
528 if (name == NULL)
529 return -EINVAL;
e1123268
JK
530
531 len = strlen(name);
532 if (len > F2FS_NAME_LEN)
6e452d69 533 return -ERANGE;
af48b85b 534
27161f13 535 down_read(&F2FS_I(inode)->i_xattr_sem);
ba38c27e 536 error = lookup_all_xattrs(inode, ipage, index, len, name,
a999150f 537 &entry, &base_addr, &base_size, &is_inline);
27161f13 538 up_read(&F2FS_I(inode)->i_xattr_sem);
86696966
CY
539 if (error)
540 return error;
af48b85b 541
e1123268 542 size = le16_to_cpu(entry->e_value_size);
af48b85b 543
e1123268 544 if (buffer && size > buffer_size) {
af48b85b 545 error = -ERANGE;
ba38c27e 546 goto out;
af48b85b
JK
547 }
548
549 if (buffer) {
550 char *pval = entry->e_name + entry->e_name_len;
64beba05
JK
551
552 if (base_size - (pval - (char *)base_addr) < size) {
553 error = -ERANGE;
554 goto out;
555 }
e1123268 556 memcpy(buffer, pval, size);
af48b85b 557 }
e1123268 558 error = size;
ba38c27e 559out:
a999150f 560 xattr_free(F2FS_I_SB(inode), base_addr, is_inline);
af48b85b
JK
561 return error;
562}
563
564ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
565{
2b0143b5 566 struct inode *inode = d_inode(dentry);
af48b85b 567 struct f2fs_xattr_entry *entry;
688078e7 568 void *base_addr, *last_base_addr;
2e0cd472 569 int error;
af48b85b
JK
570 size_t rest = buffer_size;
571
27161f13 572 down_read(&F2FS_I(inode)->i_xattr_sem);
86696966 573 error = read_all_xattrs(inode, NULL, &base_addr);
27161f13 574 up_read(&F2FS_I(inode)->i_xattr_sem);
86696966
CY
575 if (error)
576 return error;
af48b85b 577
ba3b583c 578 last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
688078e7 579
af48b85b
JK
580 list_for_each_xattr(entry, base_addr) {
581 const struct xattr_handler *handler =
582 f2fs_xattr_handler(entry->e_name_index);
764a5c6b
AG
583 const char *prefix;
584 size_t prefix_len;
af48b85b
JK
585 size_t size;
586
688078e7
RH
587 if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
588 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
589 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
590 inode->i_ino);
591 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
592 error = -EFSCORRUPTED;
593 goto cleanup;
594 }
595
764a5c6b 596 if (!handler || (handler->list && !handler->list(dentry)))
af48b85b
JK
597 continue;
598
eecfa427 599 prefix = xattr_prefix(handler);
764a5c6b
AG
600 prefix_len = strlen(prefix);
601 size = prefix_len + entry->e_name_len + 1;
602 if (buffer) {
603 if (size > rest) {
604 error = -ERANGE;
605 goto cleanup;
606 }
607 memcpy(buffer, prefix, prefix_len);
608 buffer += prefix_len;
609 memcpy(buffer, entry->e_name, entry->e_name_len);
610 buffer += entry->e_name_len;
611 *buffer++ = 0;
af48b85b 612 }
af48b85b
JK
613 rest -= size;
614 }
615 error = buffer_size - rest;
616cleanup:
c8eb7024 617 kfree(base_addr);
af48b85b
JK
618 return error;
619}
620
5f35a2cd
KM
621static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
622 const void *value, size_t size)
623{
624 void *pval = entry->e_name + entry->e_name_len;
b71deadb
JK
625
626 return (le16_to_cpu(entry->e_value_size) == size) &&
627 !memcmp(pval, value, size);
5f35a2cd
KM
628}
629
e1123268
JK
630static int __f2fs_setxattr(struct inode *inode, int index,
631 const char *name, const void *value, size_t size,
c02745ef 632 struct page *ipage, int flags)
af48b85b 633{
af48b85b 634 struct f2fs_xattr_entry *here, *last;
2777e654 635 void *base_addr, *last_base_addr;
65985d93 636 int found, newsize;
e1123268 637 size_t len;
65985d93 638 __u32 new_hsize;
2e0cd472 639 int error;
af48b85b
JK
640
641 if (name == NULL)
642 return -EINVAL;
af48b85b
JK
643
644 if (value == NULL)
e1123268 645 size = 0;
af48b85b 646
e1123268 647 len = strlen(name);
7c909772 648
037fe70c 649 if (len > F2FS_NAME_LEN)
af48b85b
JK
650 return -ERANGE;
651
037fe70c
CY
652 if (size > MAX_VALUE_LEN(inode))
653 return -E2BIG;
654
86696966
CY
655 error = read_all_xattrs(inode, ipage, &base_addr);
656 if (error)
657 return error;
af48b85b 658
ba3b583c 659 last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
2777e654 660
af48b85b 661 /* find entry with wanted name. */
2777e654
RH
662 here = __find_xattr(base_addr, last_base_addr, index, len, name);
663 if (!here) {
c83414ae
CY
664 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
665 inode->i_ino);
666 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
10f966bb 667 error = -EFSCORRUPTED;
2777e654
RH
668 goto exit;
669 }
af48b85b 670
dd9cfe23 671 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
af48b85b 672
5f35a2cd
KM
673 if (found) {
674 if ((flags & XATTR_CREATE)) {
675 error = -EEXIST;
676 goto exit;
677 }
678
b2c4692b 679 if (value && f2fs_xattr_value_same(here, value, size))
17232e83 680 goto same;
5f35a2cd 681 } else if ((flags & XATTR_REPLACE)) {
916decbf
JK
682 error = -ENODATA;
683 goto exit;
916decbf
JK
684 }
685
686 last = here;
af48b85b
JK
687 while (!IS_XATTR_LAST_ENTRY(last))
688 last = XATTR_NEXT_ENTRY(last);
689
e1123268 690 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
af48b85b
JK
691
692 /* 1. Check space */
693 if (value) {
65985d93
JK
694 int free;
695 /*
696 * If value is NULL, it is remove operation.
e1c42045 697 * In case of update operation, we calculate free.
af48b85b 698 */
65985d93 699 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
af48b85b 700 if (found)
cc3de6a3 701 free = free + ENTRY_SIZE(here);
af48b85b 702
6bacf52f 703 if (unlikely(free < newsize)) {
58457f1c 704 error = -E2BIG;
65985d93 705 goto exit;
af48b85b
JK
706 }
707 }
708
709 /* 2. Remove old entry */
710 if (found) {
65985d93
JK
711 /*
712 * If entry is found, remove old entry.
af48b85b
JK
713 * If not found, remove operation is not needed.
714 */
715 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
716 int oldsize = ENTRY_SIZE(here);
717
718 memmove(here, next, (char *)last - (char *)next);
719 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
720 memset(last, 0, oldsize);
721 }
722
65985d93
JK
723 new_hsize = (char *)last - (char *)base_addr;
724
af48b85b
JK
725 /* 3. Write new entry */
726 if (value) {
65985d93
JK
727 char *pval;
728 /*
729 * Before we come here, old entry is removed.
730 * We just write new entry.
731 */
e1123268
JK
732 last->e_name_index = index;
733 last->e_name_len = len;
734 memcpy(last->e_name, name, len);
735 pval = last->e_name + len;
736 memcpy(pval, value, size);
737 last->e_value_size = cpu_to_le16(size);
65985d93 738 new_hsize += newsize;
af48b85b
JK
739 }
740
65985d93
JK
741 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
742 if (error)
743 goto exit;
af48b85b 744
f424f664
JK
745 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
746 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
747 f2fs_set_encrypted_inode(inode);
7c45729a 748 f2fs_mark_inode_dirty_sync(inode, true);
bbf156f7
JK
749 if (!error && S_ISDIR(inode->i_mode))
750 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
17232e83
CY
751
752same:
753 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
754 inode->i_mode = F2FS_I(inode)->i_acl_mode;
755 inode->i_ctime = current_time(inode);
756 clear_inode_flag(inode, FI_ACL_MODE);
757 }
758
7c909772 759exit:
c8eb7024 760 kfree(base_addr);
af48b85b
JK
761 return error;
762}
52ab9560 763
e1123268
JK
764int f2fs_setxattr(struct inode *inode, int index, const char *name,
765 const void *value, size_t size,
c02745ef 766 struct page *ipage, int flags)
52ab9560 767{
4081363f 768 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
52ab9560
RK
769 int err;
770
a25c2cdc
CY
771 if (unlikely(f2fs_cp_error(sbi)))
772 return -EIO;
00e09c0b
CY
773 if (!f2fs_is_checkpoint_ready(sbi))
774 return -ENOSPC;
955ebcd3 775
d8d1389e
JK
776 err = dquot_initialize(inode);
777 if (err)
778 return err;
779
4d57b86d 780 /* this case is only from f2fs_init_inode_metadata */
d631abda
JK
781 if (ipage)
782 return __f2fs_setxattr(inode, index, name, value,
783 size, ipage, flags);
2c4db1a6 784 f2fs_balance_fs(sbi, true);
52ab9560 785
e479556b 786 f2fs_lock_op(sbi);
27161f13 787 down_write(&F2FS_I(inode)->i_xattr_sem);
c02745ef 788 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
27161f13 789 up_write(&F2FS_I(inode)->i_xattr_sem);
e479556b 790 f2fs_unlock_op(sbi);
52ab9560 791
d0239e1b 792 f2fs_update_time(sbi, REQ_TIME);
52ab9560
RK
793 return err;
794}
a999150f
CY
795
796int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi)
797{
798 dev_t dev = sbi->sb->s_bdev->bd_dev;
799 char slab_name[32];
800
801 sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev));
802
803 sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size *
804 sizeof(__le32) + XATTR_PADDING_SIZE;
805
806 sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name,
807 sbi->inline_xattr_slab_size);
808 if (!sbi->inline_xattr_slab)
809 return -ENOMEM;
810
811 return 0;
812}
813
814void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi)
815{
816 kmem_cache_destroy(sbi->inline_xattr_slab);
817}