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 492 set_new_dnode(&dn, inode, NULL, NULL, new_nid); 493 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET); 494 if (IS_ERR(xpage)) { 495 err = PTR_ERR(xpage); 496 f2fs_alloc_nid_failed(sbi, new_nid); 497 goto in_page_out; 498 } 499 f2fs_alloc_nid_done(sbi, new_nid); 500 } 501 xattr_addr = page_address(xpage); 502 503 if (inline_size) 504 memcpy(inline_addr, txattr_addr, inline_size); 505 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE); 506 507 if (inline_size) 508 set_page_dirty(ipage ? ipage : in_page); 509 set_page_dirty(xpage); 510 511 f2fs_put_page(xpage, 1); 512 in_page_out: 513 f2fs_put_page(in_page, 1); 514 return err; 515 } 516 517 int f2fs_getxattr(struct inode *inode, int index, const char *name, 518 void *buffer, size_t buffer_size, struct page *ipage) 519 { 520 struct f2fs_xattr_entry *entry = NULL; 521 int error; 522 unsigned int size, len; 523 void *base_addr = NULL; 524 int base_size; 525 bool is_inline; 526 527 if (name == NULL) 528 return -EINVAL; 529 530 len = strlen(name); 531 if (len > F2FS_NAME_LEN) 532 return -ERANGE; 533 534 down_read(&F2FS_I(inode)->i_xattr_sem); 535 error = lookup_all_xattrs(inode, ipage, index, len, name, 536 &entry, &base_addr, &base_size, &is_inline); 537 up_read(&F2FS_I(inode)->i_xattr_sem); 538 if (error) 539 return error; 540 541 size = le16_to_cpu(entry->e_value_size); 542 543 if (buffer && size > buffer_size) { 544 error = -ERANGE; 545 goto out; 546 } 547 548 if (buffer) { 549 char *pval = entry->e_name + entry->e_name_len; 550 551 if (base_size - (pval - (char *)base_addr) < size) { 552 error = -ERANGE; 553 goto out; 554 } 555 memcpy(buffer, pval, size); 556 } 557 error = size; 558 out: 559 xattr_free(F2FS_I_SB(inode), base_addr, is_inline); 560 return error; 561 } 562 563 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) 564 { 565 struct inode *inode = d_inode(dentry); 566 struct f2fs_xattr_entry *entry; 567 void *base_addr, *last_base_addr; 568 int error; 569 size_t rest = buffer_size; 570 571 down_read(&F2FS_I(inode)->i_xattr_sem); 572 error = read_all_xattrs(inode, NULL, &base_addr); 573 up_read(&F2FS_I(inode)->i_xattr_sem); 574 if (error) 575 return error; 576 577 last_base_addr = (void *)base_addr + XATTR_SIZE(inode); 578 579 list_for_each_xattr(entry, base_addr) { 580 const struct xattr_handler *handler = 581 f2fs_xattr_handler(entry->e_name_index); 582 const char *prefix; 583 size_t prefix_len; 584 size_t size; 585 586 if ((void *)(entry) + sizeof(__u32) > last_base_addr || 587 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) { 588 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr", 589 inode->i_ino); 590 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); 591 error = -EFSCORRUPTED; 592 goto cleanup; 593 } 594 595 if (!handler || (handler->list && !handler->list(dentry))) 596 continue; 597 598 prefix = xattr_prefix(handler); 599 prefix_len = strlen(prefix); 600 size = prefix_len + entry->e_name_len + 1; 601 if (buffer) { 602 if (size > rest) { 603 error = -ERANGE; 604 goto cleanup; 605 } 606 memcpy(buffer, prefix, prefix_len); 607 buffer += prefix_len; 608 memcpy(buffer, entry->e_name, entry->e_name_len); 609 buffer += entry->e_name_len; 610 *buffer++ = 0; 611 } 612 rest -= size; 613 } 614 error = buffer_size - rest; 615 cleanup: 616 kfree(base_addr); 617 return error; 618 } 619 620 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry, 621 const void *value, size_t size) 622 { 623 void *pval = entry->e_name + entry->e_name_len; 624 625 return (le16_to_cpu(entry->e_value_size) == size) && 626 !memcmp(pval, value, size); 627 } 628 629 static int __f2fs_setxattr(struct inode *inode, int index, 630 const char *name, const void *value, size_t size, 631 struct page *ipage, int flags) 632 { 633 struct f2fs_xattr_entry *here, *last; 634 void *base_addr, *last_base_addr; 635 int found, newsize; 636 size_t len; 637 __u32 new_hsize; 638 int error; 639 640 if (name == NULL) 641 return -EINVAL; 642 643 if (value == NULL) 644 size = 0; 645 646 len = strlen(name); 647 648 if (len > F2FS_NAME_LEN) 649 return -ERANGE; 650 651 if (size > MAX_VALUE_LEN(inode)) 652 return -E2BIG; 653 654 error = read_all_xattrs(inode, ipage, &base_addr); 655 if (error) 656 return error; 657 658 last_base_addr = (void *)base_addr + XATTR_SIZE(inode); 659 660 /* find entry with wanted name. */ 661 here = __find_xattr(base_addr, last_base_addr, index, len, name); 662 if (!here) { 663 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr", 664 inode->i_ino); 665 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK); 666 error = -EFSCORRUPTED; 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 last = XATTR_NEXT_ENTRY(last); 688 689 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size); 690 691 /* 1. Check space */ 692 if (value) { 693 int free; 694 /* 695 * If value is NULL, it is remove operation. 696 * In case of update operation, we calculate free. 697 */ 698 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr); 699 if (found) 700 free = free + ENTRY_SIZE(here); 701 702 if (unlikely(free < newsize)) { 703 error = -E2BIG; 704 goto exit; 705 } 706 } 707 708 /* 2. Remove old entry */ 709 if (found) { 710 /* 711 * If entry is found, remove old entry. 712 * If not found, remove operation is not needed. 713 */ 714 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here); 715 int oldsize = ENTRY_SIZE(here); 716 717 memmove(here, next, (char *)last - (char *)next); 718 last = (struct f2fs_xattr_entry *)((char *)last - oldsize); 719 memset(last, 0, oldsize); 720 } 721 722 new_hsize = (char *)last - (char *)base_addr; 723 724 /* 3. Write new entry */ 725 if (value) { 726 char *pval; 727 /* 728 * Before we come here, old entry is removed. 729 * We just write new entry. 730 */ 731 last->e_name_index = index; 732 last->e_name_len = len; 733 memcpy(last->e_name, name, len); 734 pval = last->e_name + len; 735 memcpy(pval, value, size); 736 last->e_value_size = cpu_to_le16(size); 737 new_hsize += newsize; 738 } 739 740 error = write_all_xattrs(inode, new_hsize, base_addr, ipage); 741 if (error) 742 goto exit; 743 744 if (index == F2FS_XATTR_INDEX_ENCRYPTION && 745 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT)) 746 f2fs_set_encrypted_inode(inode); 747 f2fs_mark_inode_dirty_sync(inode, true); 748 if (!error && S_ISDIR(inode->i_mode)) 749 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP); 750 751 same: 752 if (is_inode_flag_set(inode, FI_ACL_MODE)) { 753 inode->i_mode = F2FS_I(inode)->i_acl_mode; 754 inode->i_ctime = current_time(inode); 755 clear_inode_flag(inode, FI_ACL_MODE); 756 } 757 758 exit: 759 kfree(base_addr); 760 return error; 761 } 762 763 int f2fs_setxattr(struct inode *inode, int index, const char *name, 764 const void *value, size_t size, 765 struct page *ipage, int flags) 766 { 767 struct f2fs_sb_info *sbi = F2FS_I_SB(inode); 768 int err; 769 770 if (unlikely(f2fs_cp_error(sbi))) 771 return -EIO; 772 if (!f2fs_is_checkpoint_ready(sbi)) 773 return -ENOSPC; 774 775 err = dquot_initialize(inode); 776 if (err) 777 return err; 778 779 /* this case is only from f2fs_init_inode_metadata */ 780 if (ipage) 781 return __f2fs_setxattr(inode, index, name, value, 782 size, ipage, flags); 783 f2fs_balance_fs(sbi, true); 784 785 f2fs_lock_op(sbi); 786 down_write(&F2FS_I(inode)->i_xattr_sem); 787 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags); 788 up_write(&F2FS_I(inode)->i_xattr_sem); 789 f2fs_unlock_op(sbi); 790 791 f2fs_update_time(sbi, REQ_TIME); 792 return err; 793 } 794 795 int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi) 796 { 797 dev_t dev = sbi->sb->s_bdev->bd_dev; 798 char slab_name[32]; 799 800 sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev)); 801 802 sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size * 803 sizeof(__le32) + XATTR_PADDING_SIZE; 804 805 sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name, 806 sbi->inline_xattr_slab_size); 807 if (!sbi->inline_xattr_slab) 808 return -ENOMEM; 809 810 return 0; 811 } 812 813 void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi) 814 { 815 kmem_cache_destroy(sbi->inline_xattr_slab); 816 } 817