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