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] = &posix_acl_access_xattr_handler, 196 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler, 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 struct xattr_handler *f2fs_xattr_handler(int index) 216 { 217 const struct xattr_handler *handler = NULL; 218 219 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map)) 220 handler = f2fs_xattr_handler_map[index]; 221 return handler; 222 } 223 224 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, 225 void *last_base_addr, void **last_addr, 226 int index, size_t len, const char *name) 227 { 228 struct f2fs_xattr_entry *entry; 229 230 list_for_each_xattr(entry, base_addr) { 231 if ((void *)(entry) + sizeof(__u32) > last_base_addr || 232 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) { 233 if (last_addr) 234 *last_addr = entry; 235 return NULL; 236 } 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 entry = __find_xattr(base_addr, max_addr, last_addr, index, len, name); 257 if (!entry) 258 return NULL; 259 260 /* inline xattr header or entry across max inline xattr size */ 261 if (IS_XATTR_LAST_ENTRY(entry) && 262 (void *)entry + sizeof(__u32) > max_addr) { 263 *last_addr = entry; 264 return NULL; 265 } 266 return entry; 267 } 268 269 static int read_inline_xattr(struct inode *inode, struct page *ipage, 270 void *txattr_addr) 271 { 272 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 273 unsigned int inline_size = inline_xattr_size(inode); 274 struct page *page = NULL; 275 void *inline_addr; 276 277 if (ipage) { 278 inline_addr = inline_xattr_addr(inode, ipage); 279 } else { 280 page = f2fs_get_node_page(sbi, inode->i_ino); 281 if (IS_ERR(page)) 282 return PTR_ERR(page); 283 284 inline_addr = inline_xattr_addr(inode, page); 285 } 286 memcpy(txattr_addr, inline_addr, inline_size); 287 f2fs_put_page(page, 1); 288 289 return 0; 290 } 291 292 static int read_xattr_block(struct inode *inode, void *txattr_addr) 293 { 294 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 295 nid_t xnid = F2FS_I(inode)->i_xattr_nid; 296 unsigned int inline_size = inline_xattr_size(inode); 297 struct page *xpage; 298 void *xattr_addr; 299 300 /* The inode already has an extended attribute block. */ 301 xpage = f2fs_get_node_page(sbi, xnid); 302 if (IS_ERR(xpage)) 303 return PTR_ERR(xpage); 304 305 xattr_addr = page_address(xpage); 306 memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE); 307 f2fs_put_page(xpage, 1); 308 309 return 0; 310 } 311 312 static int lookup_all_xattrs(struct inode *inode, struct page *ipage, 313 unsigned int index, unsigned int len, 314 const char *name, struct f2fs_xattr_entry **xe, 315 void **base_addr, int *base_size, 316 bool *is_inline) 317 { 318 void *cur_addr, *txattr_addr, *last_txattr_addr; 319 void *last_addr = NULL; 320 nid_t xnid = F2FS_I(inode)->i_xattr_nid; 321 unsigned int inline_size = inline_xattr_size(inode); 322 int err; 323 324 if (!xnid && !inline_size) 325 return -ENODATA; 326 327 *base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE; 328 txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline); 329 if (!txattr_addr) 330 return -ENOMEM; 331 332 last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode); 333 334 /* read from inline xattr */ 335 if (inline_size) { 336 err = read_inline_xattr(inode, ipage, txattr_addr); 337 if (err) 338 goto out; 339 340 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr, 341 index, len, name); 342 if (*xe) { 343 *base_size = inline_size; 344 goto check; 345 } 346 } 347 348 /* read from xattr node block */ 349 if (xnid) { 350 err = read_xattr_block(inode, txattr_addr); 351 if (err) 352 goto out; 353 } 354 355 if (last_addr) 356 cur_addr = XATTR_HDR(last_addr) - 1; 357 else 358 cur_addr = txattr_addr; 359 360 *xe = __find_xattr(cur_addr, last_txattr_addr, NULL, index, len, name); 361 if (!*xe) { 362 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr", 363 inode->i_ino); 364 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); 365 err = -EFSCORRUPTED; 366 f2fs_handle_error(F2FS_I_SB(inode), 367 ERROR_CORRUPTED_XATTR); 368 goto out; 369 } 370 check: 371 if (IS_XATTR_LAST_ENTRY(*xe)) { 372 err = -ENODATA; 373 goto out; 374 } 375 376 *base_addr = txattr_addr; 377 return 0; 378 out: 379 xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline); 380 return err; 381 } 382 383 static int read_all_xattrs(struct inode *inode, struct page *ipage, 384 void **base_addr) 385 { 386 struct f2fs_xattr_header *header; 387 nid_t xnid = F2FS_I(inode)->i_xattr_nid; 388 unsigned int size = VALID_XATTR_BLOCK_SIZE; 389 unsigned int inline_size = inline_xattr_size(inode); 390 void *txattr_addr; 391 int err; 392 393 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), 394 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS); 395 if (!txattr_addr) 396 return -ENOMEM; 397 398 /* read from inline xattr */ 399 if (inline_size) { 400 err = read_inline_xattr(inode, ipage, txattr_addr); 401 if (err) 402 goto fail; 403 } 404 405 /* read from xattr node block */ 406 if (xnid) { 407 err = read_xattr_block(inode, txattr_addr); 408 if (err) 409 goto fail; 410 } 411 412 header = XATTR_HDR(txattr_addr); 413 414 /* never been allocated xattrs */ 415 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) { 416 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC); 417 header->h_refcount = cpu_to_le32(1); 418 } 419 *base_addr = txattr_addr; 420 return 0; 421 fail: 422 kfree(txattr_addr); 423 return err; 424 } 425 426 static inline int write_all_xattrs(struct inode *inode, __u32 hsize, 427 void *txattr_addr, struct page *ipage) 428 { 429 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 430 size_t inline_size = inline_xattr_size(inode); 431 struct page *in_page = NULL; 432 void *xattr_addr; 433 void *inline_addr = NULL; 434 struct page *xpage; 435 nid_t new_nid = 0; 436 int err = 0; 437 438 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid) 439 if (!f2fs_alloc_nid(sbi, &new_nid)) 440 return -ENOSPC; 441 442 /* write to inline xattr */ 443 if (inline_size) { 444 if (ipage) { 445 inline_addr = inline_xattr_addr(inode, ipage); 446 } else { 447 in_page = f2fs_get_node_page(sbi, inode->i_ino); 448 if (IS_ERR(in_page)) { 449 f2fs_alloc_nid_failed(sbi, new_nid); 450 return PTR_ERR(in_page); 451 } 452 inline_addr = inline_xattr_addr(inode, in_page); 453 } 454 455 f2fs_wait_on_page_writeback(ipage ? ipage : in_page, 456 NODE, true, true); 457 /* no need to use xattr node block */ 458 if (hsize <= inline_size) { 459 err = f2fs_truncate_xattr_node(inode); 460 f2fs_alloc_nid_failed(sbi, new_nid); 461 if (err) { 462 f2fs_put_page(in_page, 1); 463 return err; 464 } 465 memcpy(inline_addr, txattr_addr, inline_size); 466 set_page_dirty(ipage ? ipage : in_page); 467 goto in_page_out; 468 } 469 } 470 471 /* write to xattr node block */ 472 if (F2FS_I(inode)->i_xattr_nid) { 473 xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid); 474 if (IS_ERR(xpage)) { 475 err = PTR_ERR(xpage); 476 f2fs_alloc_nid_failed(sbi, new_nid); 477 goto in_page_out; 478 } 479 f2fs_bug_on(sbi, new_nid); 480 f2fs_wait_on_page_writeback(xpage, NODE, true, true); 481 } else { 482 struct dnode_of_data dn; 483 484 set_new_dnode(&dn, inode, NULL, NULL, new_nid); 485 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET); 486 if (IS_ERR(xpage)) { 487 err = PTR_ERR(xpage); 488 f2fs_alloc_nid_failed(sbi, new_nid); 489 goto in_page_out; 490 } 491 f2fs_alloc_nid_done(sbi, new_nid); 492 } 493 xattr_addr = page_address(xpage); 494 495 if (inline_size) 496 memcpy(inline_addr, txattr_addr, inline_size); 497 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE); 498 499 if (inline_size) 500 set_page_dirty(ipage ? ipage : in_page); 501 set_page_dirty(xpage); 502 503 f2fs_put_page(xpage, 1); 504 in_page_out: 505 f2fs_put_page(in_page, 1); 506 return err; 507 } 508 509 int f2fs_getxattr(struct inode *inode, int index, const char *name, 510 void *buffer, size_t buffer_size, struct page *ipage) 511 { 512 struct f2fs_xattr_entry *entry = NULL; 513 int error; 514 unsigned int size, len; 515 void *base_addr = NULL; 516 int base_size; 517 bool is_inline; 518 519 if (name == NULL) 520 return -EINVAL; 521 522 len = strlen(name); 523 if (len > F2FS_NAME_LEN) 524 return -ERANGE; 525 526 f2fs_down_read(&F2FS_I(inode)->i_xattr_sem); 527 error = lookup_all_xattrs(inode, ipage, index, len, name, 528 &entry, &base_addr, &base_size, &is_inline); 529 f2fs_up_read(&F2FS_I(inode)->i_xattr_sem); 530 if (error) 531 return error; 532 533 size = le16_to_cpu(entry->e_value_size); 534 535 if (buffer && size > buffer_size) { 536 error = -ERANGE; 537 goto out; 538 } 539 540 if (buffer) { 541 char *pval = entry->e_name + entry->e_name_len; 542 543 if (base_size - (pval - (char *)base_addr) < size) { 544 error = -ERANGE; 545 goto out; 546 } 547 memcpy(buffer, pval, size); 548 } 549 error = size; 550 out: 551 xattr_free(F2FS_I_SB(inode), base_addr, is_inline); 552 return error; 553 } 554 555 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) 556 { 557 struct inode *inode = d_inode(dentry); 558 struct f2fs_xattr_entry *entry; 559 void *base_addr, *last_base_addr; 560 int error; 561 size_t rest = buffer_size; 562 563 f2fs_down_read(&F2FS_I(inode)->i_xattr_sem); 564 error = read_all_xattrs(inode, NULL, &base_addr); 565 f2fs_up_read(&F2FS_I(inode)->i_xattr_sem); 566 if (error) 567 return error; 568 569 last_base_addr = (void *)base_addr + XATTR_SIZE(inode); 570 571 list_for_each_xattr(entry, base_addr) { 572 const struct xattr_handler *handler = 573 f2fs_xattr_handler(entry->e_name_index); 574 const char *prefix; 575 size_t prefix_len; 576 size_t size; 577 578 if ((void *)(entry) + sizeof(__u32) > last_base_addr || 579 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) { 580 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr", 581 inode->i_ino); 582 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); 583 error = -EFSCORRUPTED; 584 f2fs_handle_error(F2FS_I_SB(inode), 585 ERROR_CORRUPTED_XATTR); 586 goto cleanup; 587 } 588 589 if (!handler || (handler->list && !handler->list(dentry))) 590 continue; 591 592 prefix = xattr_prefix(handler); 593 prefix_len = strlen(prefix); 594 size = prefix_len + entry->e_name_len + 1; 595 if (buffer) { 596 if (size > rest) { 597 error = -ERANGE; 598 goto cleanup; 599 } 600 memcpy(buffer, prefix, prefix_len); 601 buffer += prefix_len; 602 memcpy(buffer, entry->e_name, entry->e_name_len); 603 buffer += entry->e_name_len; 604 *buffer++ = 0; 605 } 606 rest -= size; 607 } 608 error = buffer_size - rest; 609 cleanup: 610 kfree(base_addr); 611 return error; 612 } 613 614 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry, 615 const void *value, size_t size) 616 { 617 void *pval = entry->e_name + entry->e_name_len; 618 619 return (le16_to_cpu(entry->e_value_size) == size) && 620 !memcmp(pval, value, size); 621 } 622 623 static int __f2fs_setxattr(struct inode *inode, int index, 624 const char *name, const void *value, size_t size, 625 struct page *ipage, int flags) 626 { 627 struct f2fs_xattr_entry *here, *last; 628 void *base_addr, *last_base_addr; 629 int found, newsize; 630 size_t len; 631 __u32 new_hsize; 632 int error; 633 634 if (name == NULL) 635 return -EINVAL; 636 637 if (value == NULL) 638 size = 0; 639 640 len = strlen(name); 641 642 if (len > F2FS_NAME_LEN) 643 return -ERANGE; 644 645 if (size > MAX_VALUE_LEN(inode)) 646 return -E2BIG; 647 648 error = read_all_xattrs(inode, ipage, &base_addr); 649 if (error) 650 return error; 651 652 last_base_addr = (void *)base_addr + XATTR_SIZE(inode); 653 654 /* find entry with wanted name. */ 655 here = __find_xattr(base_addr, last_base_addr, NULL, index, len, name); 656 if (!here) { 657 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr", 658 inode->i_ino); 659 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); 660 error = -EFSCORRUPTED; 661 f2fs_handle_error(F2FS_I_SB(inode), 662 ERROR_CORRUPTED_XATTR); 663 goto exit; 664 } 665 666 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1; 667 668 if (found) { 669 if ((flags & XATTR_CREATE)) { 670 error = -EEXIST; 671 goto exit; 672 } 673 674 if (value && f2fs_xattr_value_same(here, value, size)) 675 goto same; 676 } else if ((flags & XATTR_REPLACE)) { 677 error = -ENODATA; 678 goto exit; 679 } 680 681 last = here; 682 while (!IS_XATTR_LAST_ENTRY(last)) { 683 if ((void *)(last) + sizeof(__u32) > last_base_addr || 684 (void *)XATTR_NEXT_ENTRY(last) > last_base_addr) { 685 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has invalid last xattr entry, entry_size: %zu", 686 inode->i_ino, ENTRY_SIZE(last)); 687 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); 688 error = -EFSCORRUPTED; 689 f2fs_handle_error(F2FS_I_SB(inode), 690 ERROR_CORRUPTED_XATTR); 691 goto exit; 692 } 693 last = XATTR_NEXT_ENTRY(last); 694 } 695 696 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size); 697 698 /* 1. Check space */ 699 if (value) { 700 int free; 701 /* 702 * If value is NULL, it is remove operation. 703 * In case of update operation, we calculate free. 704 */ 705 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr); 706 if (found) 707 free = free + ENTRY_SIZE(here); 708 709 if (unlikely(free < newsize)) { 710 error = -E2BIG; 711 goto exit; 712 } 713 } 714 715 /* 2. Remove old entry */ 716 if (found) { 717 /* 718 * If entry is found, remove old entry. 719 * If not found, remove operation is not needed. 720 */ 721 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here); 722 int oldsize = ENTRY_SIZE(here); 723 724 memmove(here, next, (char *)last - (char *)next); 725 last = (struct f2fs_xattr_entry *)((char *)last - oldsize); 726 memset(last, 0, oldsize); 727 } 728 729 new_hsize = (char *)last - (char *)base_addr; 730 731 /* 3. Write new entry */ 732 if (value) { 733 char *pval; 734 /* 735 * Before we come here, old entry is removed. 736 * We just write new entry. 737 */ 738 last->e_name_index = index; 739 last->e_name_len = len; 740 memcpy(last->e_name, name, len); 741 pval = last->e_name + len; 742 memcpy(pval, value, size); 743 last->e_value_size = cpu_to_le16(size); 744 new_hsize += newsize; 745 } 746 747 error = write_all_xattrs(inode, new_hsize, base_addr, ipage); 748 if (error) 749 goto exit; 750 751 if (index == F2FS_XATTR_INDEX_ENCRYPTION && 752 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT)) 753 f2fs_set_encrypted_inode(inode); 754 f2fs_mark_inode_dirty_sync(inode, true); 755 if (!error && S_ISDIR(inode->i_mode)) 756 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP); 757 758 same: 759 if (is_inode_flag_set(inode, FI_ACL_MODE)) { 760 inode->i_mode = F2FS_I(inode)->i_acl_mode; 761 inode->i_ctime = current_time(inode); 762 clear_inode_flag(inode, FI_ACL_MODE); 763 } 764 765 exit: 766 kfree(base_addr); 767 return error; 768 } 769 770 int f2fs_setxattr(struct inode *inode, int index, const char *name, 771 const void *value, size_t size, 772 struct page *ipage, int flags) 773 { 774 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 775 int err; 776 777 if (unlikely(f2fs_cp_error(sbi))) 778 return -EIO; 779 if (!f2fs_is_checkpoint_ready(sbi)) 780 return -ENOSPC; 781 782 err = f2fs_dquot_initialize(inode); 783 if (err) 784 return err; 785 786 /* this case is only from f2fs_init_inode_metadata */ 787 if (ipage) 788 return __f2fs_setxattr(inode, index, name, value, 789 size, ipage, flags); 790 f2fs_balance_fs(sbi, true); 791 792 f2fs_lock_op(sbi); 793 f2fs_down_write(&F2FS_I(inode)->i_xattr_sem); 794 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags); 795 f2fs_up_write(&F2FS_I(inode)->i_xattr_sem); 796 f2fs_unlock_op(sbi); 797 798 f2fs_update_time(sbi, REQ_TIME); 799 return err; 800 } 801 802 int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi) 803 { 804 dev_t dev = sbi->sb->s_bdev->bd_dev; 805 char slab_name[32]; 806 807 sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev)); 808 809 sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size * 810 sizeof(__le32) + XATTR_PADDING_SIZE; 811 812 sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name, 813 sbi->inline_xattr_slab_size); 814 if (!sbi->inline_xattr_slab) 815 return -ENOMEM; 816 817 return 0; 818 } 819 820 void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi) 821 { 822 kmem_cache_destroy(sbi->inline_xattr_slab); 823 } 824