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