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