]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/f2fs/xattr.c
squashfs: xattr simplifications
[mirror_ubuntu-artful-kernel.git] / fs / f2fs / xattr.c
CommitLineData
0a8165d7 1/*
af48b85b
JK
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>
8ae8f162 23#include <linux/security.h>
a6dda0e6 24#include <linux/posix_acl_xattr.h>
af48b85b
JK
25#include "f2fs.h"
26#include "xattr.h"
27
d9a82a04
AG
28static size_t f2fs_xattr_generic_list(const struct xattr_handler *handler,
29 struct dentry *dentry, char *list, size_t list_size,
30 const char *name, size_t len)
af48b85b
JK
31{
32 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
33 int total_len, prefix_len = 0;
34 const char *prefix = NULL;
35
d9a82a04 36 switch (handler->flags) {
af48b85b
JK
37 case F2FS_XATTR_INDEX_USER:
38 if (!test_opt(sbi, XATTR_USER))
39 return -EOPNOTSUPP;
40 prefix = XATTR_USER_PREFIX;
41 prefix_len = XATTR_USER_PREFIX_LEN;
42 break;
43 case F2FS_XATTR_INDEX_TRUSTED:
44 if (!capable(CAP_SYS_ADMIN))
45 return -EPERM;
46 prefix = XATTR_TRUSTED_PREFIX;
47 prefix_len = XATTR_TRUSTED_PREFIX_LEN;
48 break;
8ae8f162
JK
49 case F2FS_XATTR_INDEX_SECURITY:
50 prefix = XATTR_SECURITY_PREFIX;
51 prefix_len = XATTR_SECURITY_PREFIX_LEN;
52 break;
af48b85b
JK
53 default:
54 return -EINVAL;
55 }
56
e1123268 57 total_len = prefix_len + len + 1;
af48b85b
JK
58 if (list && total_len <= list_size) {
59 memcpy(list, prefix, prefix_len);
e1123268
JK
60 memcpy(list + prefix_len, name, len);
61 list[prefix_len + len] = '\0';
af48b85b
JK
62 }
63 return total_len;
64}
65
d9a82a04
AG
66static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
67 struct dentry *dentry, const char *name, void *buffer,
68 size_t size)
af48b85b
JK
69{
70 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
71
d9a82a04 72 switch (handler->flags) {
af48b85b
JK
73 case F2FS_XATTR_INDEX_USER:
74 if (!test_opt(sbi, XATTR_USER))
75 return -EOPNOTSUPP;
76 break;
77 case F2FS_XATTR_INDEX_TRUSTED:
78 if (!capable(CAP_SYS_ADMIN))
79 return -EPERM;
80 break;
8ae8f162
JK
81 case F2FS_XATTR_INDEX_SECURITY:
82 break;
af48b85b
JK
83 default:
84 return -EINVAL;
85 }
86 if (strcmp(name, "") == 0)
87 return -EINVAL;
d9a82a04
AG
88 return f2fs_getxattr(d_inode(dentry), handler->flags, name,
89 buffer, size, NULL);
af48b85b
JK
90}
91
d9a82a04
AG
92static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
93 struct dentry *dentry, const char *name, const void *value,
94 size_t size, int flags)
af48b85b
JK
95{
96 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
97
d9a82a04 98 switch (handler->flags) {
af48b85b
JK
99 case F2FS_XATTR_INDEX_USER:
100 if (!test_opt(sbi, XATTR_USER))
101 return -EOPNOTSUPP;
102 break;
103 case F2FS_XATTR_INDEX_TRUSTED:
104 if (!capable(CAP_SYS_ADMIN))
105 return -EPERM;
106 break;
8ae8f162
JK
107 case F2FS_XATTR_INDEX_SECURITY:
108 break;
af48b85b
JK
109 default:
110 return -EINVAL;
111 }
112 if (strcmp(name, "") == 0)
113 return -EINVAL;
114
d9a82a04 115 return f2fs_setxattr(d_inode(dentry), handler->flags, name,
c02745ef 116 value, size, NULL, flags);
af48b85b
JK
117}
118
d9a82a04
AG
119static size_t f2fs_xattr_advise_list(const struct xattr_handler *handler,
120 struct dentry *dentry, char *list, size_t list_size,
121 const char *name, size_t len)
573ea5fc
JK
122{
123 const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
124 size_t size;
125
d9a82a04 126 if (handler->flags != F2FS_XATTR_INDEX_ADVISE)
573ea5fc
JK
127 return 0;
128
129 size = strlen(xname) + 1;
130 if (list && size <= list_size)
131 memcpy(list, xname, size);
132 return size;
133}
134
d9a82a04
AG
135static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
136 struct dentry *dentry, const char *name, void *buffer,
137 size_t size)
573ea5fc 138{
2b0143b5 139 struct inode *inode = d_inode(dentry);
573ea5fc
JK
140
141 if (strcmp(name, "") != 0)
142 return -EINVAL;
143
84e97c27
CY
144 if (buffer)
145 *((char *)buffer) = F2FS_I(inode)->i_advise;
573ea5fc
JK
146 return sizeof(char);
147}
148
d9a82a04
AG
149static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
150 struct dentry *dentry, const char *name, const void *value,
151 size_t size, int flags)
573ea5fc 152{
2b0143b5 153 struct inode *inode = d_inode(dentry);
573ea5fc
JK
154
155 if (strcmp(name, "") != 0)
156 return -EINVAL;
157 if (!inode_owner_or_capable(inode))
158 return -EPERM;
159 if (value == NULL)
160 return -EINVAL;
161
162 F2FS_I(inode)->i_advise |= *(char *)value;
30c62fdb 163 mark_inode_dirty(inode);
573ea5fc
JK
164 return 0;
165}
166
8ae8f162
JK
167#ifdef CONFIG_F2FS_FS_SECURITY
168static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
169 void *page)
170{
171 const struct xattr *xattr;
172 int err = 0;
173
174 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
d631abda 175 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
8ae8f162 176 xattr->name, xattr->value,
c02745ef 177 xattr->value_len, (struct page *)page, 0);
8ae8f162
JK
178 if (err < 0)
179 break;
180 }
181 return err;
182}
183
184int f2fs_init_security(struct inode *inode, struct inode *dir,
185 const struct qstr *qstr, struct page *ipage)
186{
187 return security_inode_init_security(inode, dir, qstr,
188 &f2fs_initxattrs, ipage);
189}
190#endif
191
af48b85b
JK
192const struct xattr_handler f2fs_xattr_user_handler = {
193 .prefix = XATTR_USER_PREFIX,
194 .flags = F2FS_XATTR_INDEX_USER,
195 .list = f2fs_xattr_generic_list,
196 .get = f2fs_xattr_generic_get,
197 .set = f2fs_xattr_generic_set,
198};
199
200const struct xattr_handler f2fs_xattr_trusted_handler = {
201 .prefix = XATTR_TRUSTED_PREFIX,
202 .flags = F2FS_XATTR_INDEX_TRUSTED,
203 .list = f2fs_xattr_generic_list,
204 .get = f2fs_xattr_generic_get,
205 .set = f2fs_xattr_generic_set,
206};
207
573ea5fc
JK
208const struct xattr_handler f2fs_xattr_advise_handler = {
209 .prefix = F2FS_SYSTEM_ADVISE_PREFIX,
210 .flags = F2FS_XATTR_INDEX_ADVISE,
211 .list = f2fs_xattr_advise_list,
212 .get = f2fs_xattr_advise_get,
213 .set = f2fs_xattr_advise_set,
214};
215
8ae8f162
JK
216const struct xattr_handler f2fs_xattr_security_handler = {
217 .prefix = XATTR_SECURITY_PREFIX,
218 .flags = F2FS_XATTR_INDEX_SECURITY,
219 .list = f2fs_xattr_generic_list,
220 .get = f2fs_xattr_generic_get,
221 .set = f2fs_xattr_generic_set,
222};
223
af48b85b
JK
224static const struct xattr_handler *f2fs_xattr_handler_map[] = {
225 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
226#ifdef CONFIG_F2FS_FS_POSIX_ACL
a6dda0e6
CH
227 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
228 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
af48b85b
JK
229#endif
230 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
8ae8f162
JK
231#ifdef CONFIG_F2FS_FS_SECURITY
232 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
233#endif
af48b85b
JK
234 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
235};
236
237const struct xattr_handler *f2fs_xattr_handlers[] = {
238 &f2fs_xattr_user_handler,
239#ifdef CONFIG_F2FS_FS_POSIX_ACL
a6dda0e6
CH
240 &posix_acl_access_xattr_handler,
241 &posix_acl_default_xattr_handler,
af48b85b
JK
242#endif
243 &f2fs_xattr_trusted_handler,
8ae8f162
JK
244#ifdef CONFIG_F2FS_FS_SECURITY
245 &f2fs_xattr_security_handler,
246#endif
af48b85b
JK
247 &f2fs_xattr_advise_handler,
248 NULL,
249};
250
e1123268 251static inline const struct xattr_handler *f2fs_xattr_handler(int index)
af48b85b
JK
252{
253 const struct xattr_handler *handler = NULL;
254
e1123268
JK
255 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
256 handler = f2fs_xattr_handler_map[index];
af48b85b
JK
257 return handler;
258}
259
e1123268
JK
260static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
261 size_t len, const char *name)
dd9cfe23
JK
262{
263 struct f2fs_xattr_entry *entry;
264
265 list_for_each_xattr(entry, base_addr) {
e1123268 266 if (entry->e_name_index != index)
dd9cfe23 267 continue;
e1123268 268 if (entry->e_name_len != len)
dd9cfe23 269 continue;
e1123268 270 if (!memcmp(entry->e_name, name, len))
dd9cfe23
JK
271 break;
272 }
273 return entry;
274}
275
65985d93
JK
276static void *read_all_xattrs(struct inode *inode, struct page *ipage)
277{
4081363f 278 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
65985d93
JK
279 struct f2fs_xattr_header *header;
280 size_t size = PAGE_SIZE, inline_size = 0;
281 void *txattr_addr;
282
283 inline_size = inline_xattr_size(inode);
284
808a1d74 285 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
65985d93
JK
286 if (!txattr_addr)
287 return NULL;
288
289 /* read from inline xattr */
290 if (inline_size) {
291 struct page *page = NULL;
292 void *inline_addr;
293
294 if (ipage) {
295 inline_addr = inline_xattr_addr(ipage);
296 } else {
297 page = get_node_page(sbi, inode->i_ino);
298 if (IS_ERR(page))
299 goto fail;
300 inline_addr = inline_xattr_addr(page);
301 }
302 memcpy(txattr_addr, inline_addr, inline_size);
303 f2fs_put_page(page, 1);
304 }
305
306 /* read from xattr node block */
307 if (F2FS_I(inode)->i_xattr_nid) {
308 struct page *xpage;
309 void *xattr_addr;
310
311 /* The inode already has an extended attribute block. */
312 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
313 if (IS_ERR(xpage))
314 goto fail;
315
316 xattr_addr = page_address(xpage);
317 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
318 f2fs_put_page(xpage, 1);
319 }
320
321 header = XATTR_HDR(txattr_addr);
322
323 /* never been allocated xattrs */
324 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
325 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
326 header->h_refcount = cpu_to_le32(1);
327 }
328 return txattr_addr;
329fail:
330 kzfree(txattr_addr);
331 return NULL;
332}
333
334static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
335 void *txattr_addr, struct page *ipage)
336{
4081363f 337 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
65985d93
JK
338 size_t inline_size = 0;
339 void *xattr_addr;
340 struct page *xpage;
341 nid_t new_nid = 0;
342 int err;
343
344 inline_size = inline_xattr_size(inode);
345
346 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
347 if (!alloc_nid(sbi, &new_nid))
348 return -ENOSPC;
349
350 /* write to inline xattr */
351 if (inline_size) {
352 struct page *page = NULL;
353 void *inline_addr;
354
355 if (ipage) {
356 inline_addr = inline_xattr_addr(ipage);
54b591df 357 f2fs_wait_on_page_writeback(ipage, NODE);
65985d93
JK
358 } else {
359 page = get_node_page(sbi, inode->i_ino);
360 if (IS_ERR(page)) {
361 alloc_nid_failed(sbi, new_nid);
362 return PTR_ERR(page);
363 }
364 inline_addr = inline_xattr_addr(page);
54b591df 365 f2fs_wait_on_page_writeback(page, NODE);
65985d93
JK
366 }
367 memcpy(inline_addr, txattr_addr, inline_size);
368 f2fs_put_page(page, 1);
369
370 /* no need to use xattr node block */
371 if (hsize <= inline_size) {
372 err = truncate_xattr_node(inode, ipage);
373 alloc_nid_failed(sbi, new_nid);
374 return err;
375 }
376 }
377
378 /* write to xattr node block */
379 if (F2FS_I(inode)->i_xattr_nid) {
380 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
381 if (IS_ERR(xpage)) {
382 alloc_nid_failed(sbi, new_nid);
383 return PTR_ERR(xpage);
384 }
9850cf4a 385 f2fs_bug_on(sbi, new_nid);
54b591df 386 f2fs_wait_on_page_writeback(xpage, NODE);
65985d93
JK
387 } else {
388 struct dnode_of_data dn;
389 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
390 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
391 if (IS_ERR(xpage)) {
392 alloc_nid_failed(sbi, new_nid);
393 return PTR_ERR(xpage);
394 }
395 alloc_nid_done(sbi, new_nid);
396 }
397
398 xattr_addr = page_address(xpage);
399 memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
400 sizeof(struct node_footer));
401 set_page_dirty(xpage);
402 f2fs_put_page(xpage, 1);
403
404 /* need to checkpoint during fsync */
405 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
406 return 0;
407}
408
e1123268 409int f2fs_getxattr(struct inode *inode, int index, const char *name,
bce8d112 410 void *buffer, size_t buffer_size, struct page *ipage)
af48b85b 411{
af48b85b 412 struct f2fs_xattr_entry *entry;
65985d93 413 void *base_addr;
dd9cfe23 414 int error = 0;
e1123268 415 size_t size, len;
af48b85b
JK
416
417 if (name == NULL)
418 return -EINVAL;
e1123268
JK
419
420 len = strlen(name);
421 if (len > F2FS_NAME_LEN)
6e452d69 422 return -ERANGE;
af48b85b 423
bce8d112 424 base_addr = read_all_xattrs(inode, ipage);
65985d93
JK
425 if (!base_addr)
426 return -ENOMEM;
af48b85b 427
e1123268 428 entry = __find_xattr(base_addr, index, len, name);
dd9cfe23 429 if (IS_XATTR_LAST_ENTRY(entry)) {
af48b85b
JK
430 error = -ENODATA;
431 goto cleanup;
432 }
433
e1123268 434 size = le16_to_cpu(entry->e_value_size);
af48b85b 435
e1123268 436 if (buffer && size > buffer_size) {
af48b85b
JK
437 error = -ERANGE;
438 goto cleanup;
439 }
440
441 if (buffer) {
442 char *pval = entry->e_name + entry->e_name_len;
e1123268 443 memcpy(buffer, pval, size);
af48b85b 444 }
e1123268 445 error = size;
af48b85b
JK
446
447cleanup:
65985d93 448 kzfree(base_addr);
af48b85b
JK
449 return error;
450}
451
452ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
453{
2b0143b5 454 struct inode *inode = d_inode(dentry);
af48b85b 455 struct f2fs_xattr_entry *entry;
af48b85b
JK
456 void *base_addr;
457 int error = 0;
458 size_t rest = buffer_size;
459
65985d93
JK
460 base_addr = read_all_xattrs(inode, NULL);
461 if (!base_addr)
462 return -ENOMEM;
af48b85b
JK
463
464 list_for_each_xattr(entry, base_addr) {
465 const struct xattr_handler *handler =
466 f2fs_xattr_handler(entry->e_name_index);
467 size_t size;
468
469 if (!handler)
470 continue;
471
d9a82a04
AG
472 size = handler->list(handler, dentry, buffer, rest,
473 entry->e_name, entry->e_name_len);
af48b85b
JK
474 if (buffer && size > rest) {
475 error = -ERANGE;
476 goto cleanup;
477 }
478
479 if (buffer)
480 buffer += size;
481 rest -= size;
482 }
483 error = buffer_size - rest;
484cleanup:
65985d93 485 kzfree(base_addr);
af48b85b
JK
486 return error;
487}
488
e1123268
JK
489static int __f2fs_setxattr(struct inode *inode, int index,
490 const char *name, const void *value, size_t size,
c02745ef 491 struct page *ipage, int flags)
af48b85b 492{
af48b85b 493 struct f2fs_inode_info *fi = F2FS_I(inode);
af48b85b 494 struct f2fs_xattr_entry *here, *last;
af48b85b 495 void *base_addr;
65985d93 496 int found, newsize;
e1123268 497 size_t len;
65985d93
JK
498 __u32 new_hsize;
499 int error = -ENOMEM;
af48b85b
JK
500
501 if (name == NULL)
502 return -EINVAL;
af48b85b
JK
503
504 if (value == NULL)
e1123268 505 size = 0;
af48b85b 506
e1123268 507 len = strlen(name);
7c909772 508
037fe70c 509 if (len > F2FS_NAME_LEN)
af48b85b
JK
510 return -ERANGE;
511
037fe70c
CY
512 if (size > MAX_VALUE_LEN(inode))
513 return -E2BIG;
514
65985d93
JK
515 base_addr = read_all_xattrs(inode, ipage);
516 if (!base_addr)
517 goto exit;
af48b85b
JK
518
519 /* find entry with wanted name. */
e1123268 520 here = __find_xattr(base_addr, index, len, name);
af48b85b 521
dd9cfe23 522 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
af48b85b 523
916decbf
JK
524 if ((flags & XATTR_REPLACE) && !found) {
525 error = -ENODATA;
526 goto exit;
527 } else if ((flags & XATTR_CREATE) && found) {
528 error = -EEXIST;
529 goto exit;
530 }
531
532 last = here;
af48b85b
JK
533 while (!IS_XATTR_LAST_ENTRY(last))
534 last = XATTR_NEXT_ENTRY(last);
535
e1123268 536 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
af48b85b
JK
537
538 /* 1. Check space */
539 if (value) {
65985d93
JK
540 int free;
541 /*
542 * If value is NULL, it is remove operation.
e1c42045 543 * In case of update operation, we calculate free.
af48b85b 544 */
65985d93 545 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
af48b85b 546 if (found)
cc3de6a3 547 free = free + ENTRY_SIZE(here);
af48b85b 548
6bacf52f 549 if (unlikely(free < newsize)) {
af48b85b 550 error = -ENOSPC;
65985d93 551 goto exit;
af48b85b
JK
552 }
553 }
554
555 /* 2. Remove old entry */
556 if (found) {
65985d93
JK
557 /*
558 * If entry is found, remove old entry.
af48b85b
JK
559 * If not found, remove operation is not needed.
560 */
561 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
562 int oldsize = ENTRY_SIZE(here);
563
564 memmove(here, next, (char *)last - (char *)next);
565 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
566 memset(last, 0, oldsize);
567 }
568
65985d93
JK
569 new_hsize = (char *)last - (char *)base_addr;
570
af48b85b
JK
571 /* 3. Write new entry */
572 if (value) {
65985d93
JK
573 char *pval;
574 /*
575 * Before we come here, old entry is removed.
576 * We just write new entry.
577 */
af48b85b 578 memset(last, 0, newsize);
e1123268
JK
579 last->e_name_index = index;
580 last->e_name_len = len;
581 memcpy(last->e_name, name, len);
582 pval = last->e_name + len;
583 memcpy(pval, value, size);
584 last->e_value_size = cpu_to_le16(size);
65985d93 585 new_hsize += newsize;
af48b85b
JK
586 }
587
65985d93
JK
588 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
589 if (error)
590 goto exit;
af48b85b
JK
591
592 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
593 inode->i_mode = fi->i_acl_mode;
594 inode->i_ctime = CURRENT_TIME;
595 clear_inode_flag(fi, FI_ACL_MODE);
596 }
f424f664
JK
597 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
598 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
599 f2fs_set_encrypted_inode(inode);
e518ff81 600
8ae8f162
JK
601 if (ipage)
602 update_inode(inode, ipage);
603 else
604 update_inode_page(inode);
7c909772 605exit:
65985d93 606 kzfree(base_addr);
af48b85b
JK
607 return error;
608}
52ab9560 609
e1123268
JK
610int f2fs_setxattr(struct inode *inode, int index, const char *name,
611 const void *value, size_t size,
c02745ef 612 struct page *ipage, int flags)
52ab9560 613{
4081363f 614 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
52ab9560
RK
615 int err;
616
d631abda
JK
617 /* this case is only from init_inode_metadata */
618 if (ipage)
619 return __f2fs_setxattr(inode, index, name, value,
620 size, ipage, flags);
52ab9560
RK
621 f2fs_balance_fs(sbi);
622
e479556b 623 f2fs_lock_op(sbi);
d928bfbf
JK
624 /* protect xattr_ver */
625 down_write(&F2FS_I(inode)->i_sem);
c02745ef 626 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
d928bfbf 627 up_write(&F2FS_I(inode)->i_sem);
e479556b 628 f2fs_unlock_op(sbi);
52ab9560
RK
629
630 return err;
631}