1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/ext4/xattr.c 4 * 5 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> 6 * 7 * Fix by Harrison Xing <harrison@mountainviewdata.com>. 8 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>. 9 * Extended attributes for symlinks and special files added per 10 * suggestion of Luka Renko <luka.renko@hermes.si>. 11 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>, 12 * Red Hat Inc. 13 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz 14 * and Andreas Gruenbacher <agruen@suse.de>. 15 */ 16 17 /* 18 * Extended attributes are stored directly in inodes (on file systems with 19 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl 20 * field contains the block number if an inode uses an additional block. All 21 * attributes must fit in the inode and one additional block. Blocks that 22 * contain the identical set of attributes may be shared among several inodes. 23 * Identical blocks are detected by keeping a cache of blocks that have 24 * recently been accessed. 25 * 26 * The attributes in inodes and on blocks have a different header; the entries 27 * are stored in the same format: 28 * 29 * +------------------+ 30 * | header | 31 * | entry 1 | | 32 * | entry 2 | | growing downwards 33 * | entry 3 | v 34 * | four null bytes | 35 * | . . . | 36 * | value 1 | ^ 37 * | value 3 | | growing upwards 38 * | value 2 | | 39 * +------------------+ 40 * 41 * The header is followed by multiple entry descriptors. In disk blocks, the 42 * entry descriptors are kept sorted. In inodes, they are unsorted. The 43 * attribute values are aligned to the end of the block in no specific order. 44 * 45 * Locking strategy 46 * ---------------- 47 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem. 48 * EA blocks are only changed if they are exclusive to an inode, so 49 * holding xattr_sem also means that nothing but the EA block's reference 50 * count can change. Multiple writers to the same block are synchronized 51 * by the buffer lock. 52 */ 53 54 #include <linux/init.h> 55 #include <linux/fs.h> 56 #include <linux/slab.h> 57 #include <linux/mbcache.h> 58 #include <linux/quotaops.h> 59 #include "ext4_jbd2.h" 60 #include "ext4.h" 61 #include "xattr.h" 62 #include "acl.h" 63 64 #ifdef EXT4_XATTR_DEBUG 65 # define ea_idebug(inode, fmt, ...) \ 66 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \ 67 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__) 68 # define ea_bdebug(bh, fmt, ...) \ 69 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \ 70 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__) 71 #else 72 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__) 73 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__) 74 #endif 75 76 static void ext4_xattr_block_cache_insert(struct mb_cache *, 77 struct buffer_head *); 78 static struct buffer_head * 79 ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *, 80 struct mb_cache_entry **); 81 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value, 82 size_t value_count); 83 static void ext4_xattr_rehash(struct ext4_xattr_header *); 84 85 static const struct xattr_handler * const ext4_xattr_handler_map[] = { 86 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler, 87 #ifdef CONFIG_EXT4_FS_POSIX_ACL 88 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler, 89 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler, 90 #endif 91 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler, 92 #ifdef CONFIG_EXT4_FS_SECURITY 93 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler, 94 #endif 95 }; 96 97 const struct xattr_handler *ext4_xattr_handlers[] = { 98 &ext4_xattr_user_handler, 99 &ext4_xattr_trusted_handler, 100 #ifdef CONFIG_EXT4_FS_POSIX_ACL 101 &posix_acl_access_xattr_handler, 102 &posix_acl_default_xattr_handler, 103 #endif 104 #ifdef CONFIG_EXT4_FS_SECURITY 105 &ext4_xattr_security_handler, 106 #endif 107 NULL 108 }; 109 110 #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \ 111 inode->i_sb->s_fs_info)->s_ea_block_cache) 112 113 #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \ 114 inode->i_sb->s_fs_info)->s_ea_inode_cache) 115 116 static int 117 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, 118 struct inode *inode); 119 120 #ifdef CONFIG_LOCKDEP 121 void ext4_xattr_inode_set_class(struct inode *ea_inode) 122 { 123 lockdep_set_subclass(&ea_inode->i_rwsem, 1); 124 } 125 #endif 126 127 static __le32 ext4_xattr_block_csum(struct inode *inode, 128 sector_t block_nr, 129 struct ext4_xattr_header *hdr) 130 { 131 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 132 __u32 csum; 133 __le64 dsk_block_nr = cpu_to_le64(block_nr); 134 __u32 dummy_csum = 0; 135 int offset = offsetof(struct ext4_xattr_header, h_checksum); 136 137 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr, 138 sizeof(dsk_block_nr)); 139 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset); 140 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum)); 141 offset += sizeof(dummy_csum); 142 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset, 143 EXT4_BLOCK_SIZE(inode->i_sb) - offset); 144 145 return cpu_to_le32(csum); 146 } 147 148 static int ext4_xattr_block_csum_verify(struct inode *inode, 149 struct buffer_head *bh) 150 { 151 struct ext4_xattr_header *hdr = BHDR(bh); 152 int ret = 1; 153 154 if (ext4_has_metadata_csum(inode->i_sb)) { 155 lock_buffer(bh); 156 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode, 157 bh->b_blocknr, hdr)); 158 unlock_buffer(bh); 159 } 160 return ret; 161 } 162 163 static void ext4_xattr_block_csum_set(struct inode *inode, 164 struct buffer_head *bh) 165 { 166 if (ext4_has_metadata_csum(inode->i_sb)) 167 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode, 168 bh->b_blocknr, BHDR(bh)); 169 } 170 171 static inline const struct xattr_handler * 172 ext4_xattr_handler(int name_index) 173 { 174 const struct xattr_handler *handler = NULL; 175 176 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map)) 177 handler = ext4_xattr_handler_map[name_index]; 178 return handler; 179 } 180 181 static int 182 ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end, 183 void *value_start) 184 { 185 struct ext4_xattr_entry *e = entry; 186 187 /* Find the end of the names list */ 188 while (!IS_LAST_ENTRY(e)) { 189 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e); 190 if ((void *)next >= end) 191 return -EFSCORRUPTED; 192 e = next; 193 } 194 195 /* Check the values */ 196 while (!IS_LAST_ENTRY(entry)) { 197 if (entry->e_value_size != 0 && 198 entry->e_value_inum == 0) { 199 u16 offs = le16_to_cpu(entry->e_value_offs); 200 u32 size = le32_to_cpu(entry->e_value_size); 201 void *value; 202 203 /* 204 * The value cannot overlap the names, and the value 205 * with padding cannot extend beyond 'end'. Check both 206 * the padded and unpadded sizes, since the size may 207 * overflow to 0 when adding padding. 208 */ 209 if (offs > end - value_start) 210 return -EFSCORRUPTED; 211 value = value_start + offs; 212 if (value < (void *)e + sizeof(u32) || 213 size > end - value || 214 EXT4_XATTR_SIZE(size) > end - value) 215 return -EFSCORRUPTED; 216 } 217 entry = EXT4_XATTR_NEXT(entry); 218 } 219 220 return 0; 221 } 222 223 static inline int 224 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh) 225 { 226 int error; 227 228 if (buffer_verified(bh)) 229 return 0; 230 231 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) || 232 BHDR(bh)->h_blocks != cpu_to_le32(1)) 233 return -EFSCORRUPTED; 234 if (!ext4_xattr_block_csum_verify(inode, bh)) 235 return -EFSBADCRC; 236 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size, 237 bh->b_data); 238 if (!error) 239 set_buffer_verified(bh); 240 return error; 241 } 242 243 static int 244 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header, 245 void *end, const char *function, unsigned int line) 246 { 247 int error = -EFSCORRUPTED; 248 249 if (end - (void *)header < sizeof(*header) + sizeof(u32) || 250 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC))) 251 goto errout; 252 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header)); 253 errout: 254 if (error) 255 __ext4_error_inode(inode, function, line, 0, 256 "corrupted in-inode xattr"); 257 return error; 258 } 259 260 #define xattr_check_inode(inode, header, end) \ 261 __xattr_check_inode((inode), (header), (end), __func__, __LINE__) 262 263 static int 264 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index, 265 const char *name, int sorted) 266 { 267 struct ext4_xattr_entry *entry; 268 size_t name_len; 269 int cmp = 1; 270 271 if (name == NULL) 272 return -EINVAL; 273 name_len = strlen(name); 274 entry = *pentry; 275 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { 276 cmp = name_index - entry->e_name_index; 277 if (!cmp) 278 cmp = name_len - entry->e_name_len; 279 if (!cmp) 280 cmp = memcmp(name, entry->e_name, name_len); 281 if (cmp <= 0 && (sorted || cmp == 0)) 282 break; 283 } 284 *pentry = entry; 285 return cmp ? -ENODATA : 0; 286 } 287 288 static u32 289 ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size) 290 { 291 return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size); 292 } 293 294 static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode) 295 { 296 return ((u64)ea_inode->i_ctime.tv_sec << 32) | 297 ((u32)ea_inode->i_version); 298 } 299 300 static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count) 301 { 302 ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32); 303 ea_inode->i_version = (u32)ref_count; 304 } 305 306 static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode) 307 { 308 return (u32)ea_inode->i_atime.tv_sec; 309 } 310 311 static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash) 312 { 313 ea_inode->i_atime.tv_sec = hash; 314 } 315 316 /* 317 * Read the EA value from an inode. 318 */ 319 static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size) 320 { 321 int blocksize = 1 << ea_inode->i_blkbits; 322 int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits; 323 int tail_size = (size % blocksize) ?: blocksize; 324 struct buffer_head *bhs_inline[8]; 325 struct buffer_head **bhs = bhs_inline; 326 int i, ret; 327 328 if (bh_count > ARRAY_SIZE(bhs_inline)) { 329 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS); 330 if (!bhs) 331 return -ENOMEM; 332 } 333 334 ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count, 335 true /* wait */, bhs); 336 if (ret) 337 goto free_bhs; 338 339 for (i = 0; i < bh_count; i++) { 340 /* There shouldn't be any holes in ea_inode. */ 341 if (!bhs[i]) { 342 ret = -EFSCORRUPTED; 343 goto put_bhs; 344 } 345 memcpy((char *)buf + blocksize * i, bhs[i]->b_data, 346 i < bh_count - 1 ? blocksize : tail_size); 347 } 348 ret = 0; 349 put_bhs: 350 for (i = 0; i < bh_count; i++) 351 brelse(bhs[i]); 352 free_bhs: 353 if (bhs != bhs_inline) 354 kfree(bhs); 355 return ret; 356 } 357 358 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec) 359 360 static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, 361 u32 ea_inode_hash, struct inode **ea_inode) 362 { 363 struct inode *inode; 364 int err; 365 366 inode = ext4_iget(parent->i_sb, ea_ino); 367 if (IS_ERR(inode)) { 368 err = PTR_ERR(inode); 369 ext4_error(parent->i_sb, 370 "error while reading EA inode %lu err=%d", ea_ino, 371 err); 372 return err; 373 } 374 375 if (is_bad_inode(inode)) { 376 ext4_error(parent->i_sb, 377 "error while reading EA inode %lu is_bad_inode", 378 ea_ino); 379 err = -EIO; 380 goto error; 381 } 382 383 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) { 384 ext4_error(parent->i_sb, 385 "EA inode %lu does not have EXT4_EA_INODE_FL flag", 386 ea_ino); 387 err = -EINVAL; 388 goto error; 389 } 390 391 ext4_xattr_inode_set_class(inode); 392 393 /* 394 * Check whether this is an old Lustre-style xattr inode. Lustre 395 * implementation does not have hash validation, rather it has a 396 * backpointer from ea_inode to the parent inode. 397 */ 398 if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) && 399 EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino && 400 inode->i_generation == parent->i_generation) { 401 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE); 402 ext4_xattr_inode_set_ref(inode, 1); 403 } else { 404 inode_lock(inode); 405 inode->i_flags |= S_NOQUOTA; 406 inode_unlock(inode); 407 } 408 409 *ea_inode = inode; 410 return 0; 411 error: 412 iput(inode); 413 return err; 414 } 415 416 static int 417 ext4_xattr_inode_verify_hashes(struct inode *ea_inode, 418 struct ext4_xattr_entry *entry, void *buffer, 419 size_t size) 420 { 421 u32 hash; 422 423 /* Verify stored hash matches calculated hash. */ 424 hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size); 425 if (hash != ext4_xattr_inode_get_hash(ea_inode)) 426 return -EFSCORRUPTED; 427 428 if (entry) { 429 __le32 e_hash, tmp_data; 430 431 /* Verify entry hash. */ 432 tmp_data = cpu_to_le32(hash); 433 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len, 434 &tmp_data, 1); 435 if (e_hash != entry->e_hash) 436 return -EFSCORRUPTED; 437 } 438 return 0; 439 } 440 441 /* 442 * Read xattr value from the EA inode. 443 */ 444 static int 445 ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry, 446 void *buffer, size_t size) 447 { 448 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode); 449 struct inode *ea_inode; 450 int err; 451 452 err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum), 453 le32_to_cpu(entry->e_hash), &ea_inode); 454 if (err) { 455 ea_inode = NULL; 456 goto out; 457 } 458 459 if (i_size_read(ea_inode) != size) { 460 ext4_warning_inode(ea_inode, 461 "ea_inode file size=%llu entry size=%zu", 462 i_size_read(ea_inode), size); 463 err = -EFSCORRUPTED; 464 goto out; 465 } 466 467 err = ext4_xattr_inode_read(ea_inode, buffer, size); 468 if (err) 469 goto out; 470 471 if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) { 472 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer, 473 size); 474 if (err) { 475 ext4_warning_inode(ea_inode, 476 "EA inode hash validation failed"); 477 goto out; 478 } 479 480 if (ea_inode_cache) 481 mb_cache_entry_create(ea_inode_cache, GFP_NOFS, 482 ext4_xattr_inode_get_hash(ea_inode), 483 ea_inode->i_ino, true /* reusable */); 484 } 485 out: 486 iput(ea_inode); 487 return err; 488 } 489 490 static int 491 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name, 492 void *buffer, size_t buffer_size) 493 { 494 struct buffer_head *bh = NULL; 495 struct ext4_xattr_entry *entry; 496 size_t size; 497 int error; 498 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); 499 500 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld", 501 name_index, name, buffer, (long)buffer_size); 502 503 error = -ENODATA; 504 if (!EXT4_I(inode)->i_file_acl) 505 goto cleanup; 506 ea_idebug(inode, "reading block %llu", 507 (unsigned long long)EXT4_I(inode)->i_file_acl); 508 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); 509 if (!bh) 510 goto cleanup; 511 ea_bdebug(bh, "b_count=%d, refcount=%d", 512 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); 513 if (ext4_xattr_check_block(inode, bh)) { 514 EXT4_ERROR_INODE(inode, "bad block %llu", 515 EXT4_I(inode)->i_file_acl); 516 error = -EFSCORRUPTED; 517 goto cleanup; 518 } 519 ext4_xattr_block_cache_insert(ea_block_cache, bh); 520 entry = BFIRST(bh); 521 error = ext4_xattr_find_entry(&entry, name_index, name, 1); 522 if (error) 523 goto cleanup; 524 size = le32_to_cpu(entry->e_value_size); 525 if (buffer) { 526 error = -ERANGE; 527 if (size > buffer_size) 528 goto cleanup; 529 if (entry->e_value_inum) { 530 error = ext4_xattr_inode_get(inode, entry, buffer, 531 size); 532 if (error) 533 goto cleanup; 534 } else { 535 memcpy(buffer, bh->b_data + 536 le16_to_cpu(entry->e_value_offs), size); 537 } 538 } 539 error = size; 540 541 cleanup: 542 brelse(bh); 543 return error; 544 } 545 546 int 547 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name, 548 void *buffer, size_t buffer_size) 549 { 550 struct ext4_xattr_ibody_header *header; 551 struct ext4_xattr_entry *entry; 552 struct ext4_inode *raw_inode; 553 struct ext4_iloc iloc; 554 size_t size; 555 void *end; 556 int error; 557 558 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) 559 return -ENODATA; 560 error = ext4_get_inode_loc(inode, &iloc); 561 if (error) 562 return error; 563 raw_inode = ext4_raw_inode(&iloc); 564 header = IHDR(inode, raw_inode); 565 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; 566 error = xattr_check_inode(inode, header, end); 567 if (error) 568 goto cleanup; 569 entry = IFIRST(header); 570 error = ext4_xattr_find_entry(&entry, name_index, name, 0); 571 if (error) 572 goto cleanup; 573 size = le32_to_cpu(entry->e_value_size); 574 if (buffer) { 575 error = -ERANGE; 576 if (size > buffer_size) 577 goto cleanup; 578 if (entry->e_value_inum) { 579 error = ext4_xattr_inode_get(inode, entry, buffer, 580 size); 581 if (error) 582 goto cleanup; 583 } else { 584 memcpy(buffer, (void *)IFIRST(header) + 585 le16_to_cpu(entry->e_value_offs), size); 586 } 587 } 588 error = size; 589 590 cleanup: 591 brelse(iloc.bh); 592 return error; 593 } 594 595 /* 596 * ext4_xattr_get() 597 * 598 * Copy an extended attribute into the buffer 599 * provided, or compute the buffer size required. 600 * Buffer is NULL to compute the size of the buffer required. 601 * 602 * Returns a negative error number on failure, or the number of bytes 603 * used / required on success. 604 */ 605 int 606 ext4_xattr_get(struct inode *inode, int name_index, const char *name, 607 void *buffer, size_t buffer_size) 608 { 609 int error; 610 611 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) 612 return -EIO; 613 614 if (strlen(name) > 255) 615 return -ERANGE; 616 617 down_read(&EXT4_I(inode)->xattr_sem); 618 error = ext4_xattr_ibody_get(inode, name_index, name, buffer, 619 buffer_size); 620 if (error == -ENODATA) 621 error = ext4_xattr_block_get(inode, name_index, name, buffer, 622 buffer_size); 623 up_read(&EXT4_I(inode)->xattr_sem); 624 return error; 625 } 626 627 static int 628 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry, 629 char *buffer, size_t buffer_size) 630 { 631 size_t rest = buffer_size; 632 633 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { 634 const struct xattr_handler *handler = 635 ext4_xattr_handler(entry->e_name_index); 636 637 if (handler && (!handler->list || handler->list(dentry))) { 638 const char *prefix = handler->prefix ?: handler->name; 639 size_t prefix_len = strlen(prefix); 640 size_t size = prefix_len + entry->e_name_len + 1; 641 642 if (buffer) { 643 if (size > rest) 644 return -ERANGE; 645 memcpy(buffer, prefix, prefix_len); 646 buffer += prefix_len; 647 memcpy(buffer, entry->e_name, entry->e_name_len); 648 buffer += entry->e_name_len; 649 *buffer++ = 0; 650 } 651 rest -= size; 652 } 653 } 654 return buffer_size - rest; /* total size */ 655 } 656 657 static int 658 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size) 659 { 660 struct inode *inode = d_inode(dentry); 661 struct buffer_head *bh = NULL; 662 int error; 663 664 ea_idebug(inode, "buffer=%p, buffer_size=%ld", 665 buffer, (long)buffer_size); 666 667 error = 0; 668 if (!EXT4_I(inode)->i_file_acl) 669 goto cleanup; 670 ea_idebug(inode, "reading block %llu", 671 (unsigned long long)EXT4_I(inode)->i_file_acl); 672 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); 673 error = -EIO; 674 if (!bh) 675 goto cleanup; 676 ea_bdebug(bh, "b_count=%d, refcount=%d", 677 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount)); 678 if (ext4_xattr_check_block(inode, bh)) { 679 EXT4_ERROR_INODE(inode, "bad block %llu", 680 EXT4_I(inode)->i_file_acl); 681 error = -EFSCORRUPTED; 682 goto cleanup; 683 } 684 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh); 685 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size); 686 687 cleanup: 688 brelse(bh); 689 690 return error; 691 } 692 693 static int 694 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size) 695 { 696 struct inode *inode = d_inode(dentry); 697 struct ext4_xattr_ibody_header *header; 698 struct ext4_inode *raw_inode; 699 struct ext4_iloc iloc; 700 void *end; 701 int error; 702 703 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) 704 return 0; 705 error = ext4_get_inode_loc(inode, &iloc); 706 if (error) 707 return error; 708 raw_inode = ext4_raw_inode(&iloc); 709 header = IHDR(inode, raw_inode); 710 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; 711 error = xattr_check_inode(inode, header, end); 712 if (error) 713 goto cleanup; 714 error = ext4_xattr_list_entries(dentry, IFIRST(header), 715 buffer, buffer_size); 716 717 cleanup: 718 brelse(iloc.bh); 719 return error; 720 } 721 722 /* 723 * Inode operation listxattr() 724 * 725 * d_inode(dentry)->i_rwsem: don't care 726 * 727 * Copy a list of attribute names into the buffer 728 * provided, or compute the buffer size required. 729 * Buffer is NULL to compute the size of the buffer required. 730 * 731 * Returns a negative error number on failure, or the number of bytes 732 * used / required on success. 733 */ 734 ssize_t 735 ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) 736 { 737 int ret, ret2; 738 739 down_read(&EXT4_I(d_inode(dentry))->xattr_sem); 740 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size); 741 if (ret < 0) 742 goto errout; 743 if (buffer) { 744 buffer += ret; 745 buffer_size -= ret; 746 } 747 ret = ext4_xattr_block_list(dentry, buffer, buffer_size); 748 if (ret < 0) 749 goto errout; 750 ret += ret2; 751 errout: 752 up_read(&EXT4_I(d_inode(dentry))->xattr_sem); 753 return ret; 754 } 755 756 /* 757 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is 758 * not set, set it. 759 */ 760 static void ext4_xattr_update_super_block(handle_t *handle, 761 struct super_block *sb) 762 { 763 if (ext4_has_feature_xattr(sb)) 764 return; 765 766 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 767 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) { 768 ext4_set_feature_xattr(sb); 769 ext4_handle_dirty_super(handle, sb); 770 } 771 } 772 773 int ext4_get_inode_usage(struct inode *inode, qsize_t *usage) 774 { 775 struct ext4_iloc iloc = { .bh = NULL }; 776 struct buffer_head *bh = NULL; 777 struct ext4_inode *raw_inode; 778 struct ext4_xattr_ibody_header *header; 779 struct ext4_xattr_entry *entry; 780 qsize_t ea_inode_refs = 0; 781 void *end; 782 int ret; 783 784 lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem); 785 786 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 787 ret = ext4_get_inode_loc(inode, &iloc); 788 if (ret) 789 goto out; 790 raw_inode = ext4_raw_inode(&iloc); 791 header = IHDR(inode, raw_inode); 792 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; 793 ret = xattr_check_inode(inode, header, end); 794 if (ret) 795 goto out; 796 797 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry); 798 entry = EXT4_XATTR_NEXT(entry)) 799 if (entry->e_value_inum) 800 ea_inode_refs++; 801 } 802 803 if (EXT4_I(inode)->i_file_acl) { 804 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); 805 if (!bh) { 806 ret = -EIO; 807 goto out; 808 } 809 810 if (ext4_xattr_check_block(inode, bh)) { 811 ret = -EFSCORRUPTED; 812 goto out; 813 } 814 815 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry); 816 entry = EXT4_XATTR_NEXT(entry)) 817 if (entry->e_value_inum) 818 ea_inode_refs++; 819 } 820 *usage = ea_inode_refs + 1; 821 ret = 0; 822 out: 823 brelse(iloc.bh); 824 brelse(bh); 825 return ret; 826 } 827 828 static inline size_t round_up_cluster(struct inode *inode, size_t length) 829 { 830 struct super_block *sb = inode->i_sb; 831 size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits + 832 inode->i_blkbits); 833 size_t mask = ~(cluster_size - 1); 834 835 return (length + cluster_size - 1) & mask; 836 } 837 838 static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len) 839 { 840 int err; 841 842 err = dquot_alloc_inode(inode); 843 if (err) 844 return err; 845 err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len)); 846 if (err) 847 dquot_free_inode(inode); 848 return err; 849 } 850 851 static void ext4_xattr_inode_free_quota(struct inode *parent, 852 struct inode *ea_inode, 853 size_t len) 854 { 855 if (ea_inode && 856 ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) 857 return; 858 dquot_free_space_nodirty(parent, round_up_cluster(parent, len)); 859 dquot_free_inode(parent); 860 } 861 862 int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode, 863 struct buffer_head *block_bh, size_t value_len, 864 bool is_create) 865 { 866 int credits; 867 int blocks; 868 869 /* 870 * 1) Owner inode update 871 * 2) Ref count update on old xattr block 872 * 3) new xattr block 873 * 4) block bitmap update for new xattr block 874 * 5) group descriptor for new xattr block 875 * 6) block bitmap update for old xattr block 876 * 7) group descriptor for old block 877 * 878 * 6 & 7 can happen if we have two racing threads T_a and T_b 879 * which are each trying to set an xattr on inodes I_a and I_b 880 * which were both initially sharing an xattr block. 881 */ 882 credits = 7; 883 884 /* Quota updates. */ 885 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb); 886 887 /* 888 * In case of inline data, we may push out the data to a block, 889 * so we need to reserve credits for this eventuality 890 */ 891 if (inode && ext4_has_inline_data(inode)) 892 credits += ext4_writepage_trans_blocks(inode) + 1; 893 894 /* We are done if ea_inode feature is not enabled. */ 895 if (!ext4_has_feature_ea_inode(sb)) 896 return credits; 897 898 /* New ea_inode, inode map, block bitmap, group descriptor. */ 899 credits += 4; 900 901 /* Data blocks. */ 902 blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits; 903 904 /* Indirection block or one level of extent tree. */ 905 blocks += 1; 906 907 /* Block bitmap and group descriptor updates for each block. */ 908 credits += blocks * 2; 909 910 /* Blocks themselves. */ 911 credits += blocks; 912 913 if (!is_create) { 914 /* Dereference ea_inode holding old xattr value. 915 * Old ea_inode, inode map, block bitmap, group descriptor. 916 */ 917 credits += 4; 918 919 /* Data blocks for old ea_inode. */ 920 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits; 921 922 /* Indirection block or one level of extent tree for old 923 * ea_inode. 924 */ 925 blocks += 1; 926 927 /* Block bitmap and group descriptor updates for each block. */ 928 credits += blocks * 2; 929 } 930 931 /* We may need to clone the existing xattr block in which case we need 932 * to increment ref counts for existing ea_inodes referenced by it. 933 */ 934 if (block_bh) { 935 struct ext4_xattr_entry *entry = BFIRST(block_bh); 936 937 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) 938 if (entry->e_value_inum) 939 /* Ref count update on ea_inode. */ 940 credits += 1; 941 } 942 return credits; 943 } 944 945 static int ext4_xattr_ensure_credits(handle_t *handle, struct inode *inode, 946 int credits, struct buffer_head *bh, 947 bool dirty, bool block_csum) 948 { 949 int error; 950 951 if (!ext4_handle_valid(handle)) 952 return 0; 953 954 if (handle->h_buffer_credits >= credits) 955 return 0; 956 957 error = ext4_journal_extend(handle, credits - handle->h_buffer_credits); 958 if (!error) 959 return 0; 960 if (error < 0) { 961 ext4_warning(inode->i_sb, "Extend journal (error %d)", error); 962 return error; 963 } 964 965 if (bh && dirty) { 966 if (block_csum) 967 ext4_xattr_block_csum_set(inode, bh); 968 error = ext4_handle_dirty_metadata(handle, NULL, bh); 969 if (error) { 970 ext4_warning(inode->i_sb, "Handle metadata (error %d)", 971 error); 972 return error; 973 } 974 } 975 976 error = ext4_journal_restart(handle, credits); 977 if (error) { 978 ext4_warning(inode->i_sb, "Restart journal (error %d)", error); 979 return error; 980 } 981 982 if (bh) { 983 error = ext4_journal_get_write_access(handle, bh); 984 if (error) { 985 ext4_warning(inode->i_sb, 986 "Get write access failed (error %d)", 987 error); 988 return error; 989 } 990 } 991 return 0; 992 } 993 994 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode, 995 int ref_change) 996 { 997 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(ea_inode); 998 struct ext4_iloc iloc; 999 s64 ref_count; 1000 u32 hash; 1001 int ret; 1002 1003 inode_lock(ea_inode); 1004 1005 ret = ext4_reserve_inode_write(handle, ea_inode, &iloc); 1006 if (ret) { 1007 iloc.bh = NULL; 1008 goto out; 1009 } 1010 1011 ref_count = ext4_xattr_inode_get_ref(ea_inode); 1012 ref_count += ref_change; 1013 ext4_xattr_inode_set_ref(ea_inode, ref_count); 1014 1015 if (ref_change > 0) { 1016 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld", 1017 ea_inode->i_ino, ref_count); 1018 1019 if (ref_count == 1) { 1020 WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u", 1021 ea_inode->i_ino, ea_inode->i_nlink); 1022 1023 set_nlink(ea_inode, 1); 1024 ext4_orphan_del(handle, ea_inode); 1025 1026 if (ea_inode_cache) { 1027 hash = ext4_xattr_inode_get_hash(ea_inode); 1028 mb_cache_entry_create(ea_inode_cache, 1029 GFP_NOFS, hash, 1030 ea_inode->i_ino, 1031 true /* reusable */); 1032 } 1033 } 1034 } else { 1035 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld", 1036 ea_inode->i_ino, ref_count); 1037 1038 if (ref_count == 0) { 1039 WARN_ONCE(ea_inode->i_nlink != 1, 1040 "EA inode %lu i_nlink=%u", 1041 ea_inode->i_ino, ea_inode->i_nlink); 1042 1043 clear_nlink(ea_inode); 1044 ext4_orphan_add(handle, ea_inode); 1045 1046 if (ea_inode_cache) { 1047 hash = ext4_xattr_inode_get_hash(ea_inode); 1048 mb_cache_entry_delete(ea_inode_cache, hash, 1049 ea_inode->i_ino); 1050 } 1051 } 1052 } 1053 1054 ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc); 1055 iloc.bh = NULL; 1056 if (ret) 1057 ext4_warning_inode(ea_inode, 1058 "ext4_mark_iloc_dirty() failed ret=%d", ret); 1059 out: 1060 brelse(iloc.bh); 1061 inode_unlock(ea_inode); 1062 return ret; 1063 } 1064 1065 static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode) 1066 { 1067 return ext4_xattr_inode_update_ref(handle, ea_inode, 1); 1068 } 1069 1070 static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode) 1071 { 1072 return ext4_xattr_inode_update_ref(handle, ea_inode, -1); 1073 } 1074 1075 static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent, 1076 struct ext4_xattr_entry *first) 1077 { 1078 struct inode *ea_inode; 1079 struct ext4_xattr_entry *entry; 1080 struct ext4_xattr_entry *failed_entry; 1081 unsigned int ea_ino; 1082 int err, saved_err; 1083 1084 for (entry = first; !IS_LAST_ENTRY(entry); 1085 entry = EXT4_XATTR_NEXT(entry)) { 1086 if (!entry->e_value_inum) 1087 continue; 1088 ea_ino = le32_to_cpu(entry->e_value_inum); 1089 err = ext4_xattr_inode_iget(parent, ea_ino, 1090 le32_to_cpu(entry->e_hash), 1091 &ea_inode); 1092 if (err) 1093 goto cleanup; 1094 err = ext4_xattr_inode_inc_ref(handle, ea_inode); 1095 if (err) { 1096 ext4_warning_inode(ea_inode, "inc ref error %d", err); 1097 iput(ea_inode); 1098 goto cleanup; 1099 } 1100 iput(ea_inode); 1101 } 1102 return 0; 1103 1104 cleanup: 1105 saved_err = err; 1106 failed_entry = entry; 1107 1108 for (entry = first; entry != failed_entry; 1109 entry = EXT4_XATTR_NEXT(entry)) { 1110 if (!entry->e_value_inum) 1111 continue; 1112 ea_ino = le32_to_cpu(entry->e_value_inum); 1113 err = ext4_xattr_inode_iget(parent, ea_ino, 1114 le32_to_cpu(entry->e_hash), 1115 &ea_inode); 1116 if (err) { 1117 ext4_warning(parent->i_sb, 1118 "cleanup ea_ino %u iget error %d", ea_ino, 1119 err); 1120 continue; 1121 } 1122 err = ext4_xattr_inode_dec_ref(handle, ea_inode); 1123 if (err) 1124 ext4_warning_inode(ea_inode, "cleanup dec ref error %d", 1125 err); 1126 iput(ea_inode); 1127 } 1128 return saved_err; 1129 } 1130 1131 static void 1132 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent, 1133 struct buffer_head *bh, 1134 struct ext4_xattr_entry *first, bool block_csum, 1135 struct ext4_xattr_inode_array **ea_inode_array, 1136 int extra_credits, bool skip_quota) 1137 { 1138 struct inode *ea_inode; 1139 struct ext4_xattr_entry *entry; 1140 bool dirty = false; 1141 unsigned int ea_ino; 1142 int err; 1143 int credits; 1144 1145 /* One credit for dec ref on ea_inode, one for orphan list addition, */ 1146 credits = 2 + extra_credits; 1147 1148 for (entry = first; !IS_LAST_ENTRY(entry); 1149 entry = EXT4_XATTR_NEXT(entry)) { 1150 if (!entry->e_value_inum) 1151 continue; 1152 ea_ino = le32_to_cpu(entry->e_value_inum); 1153 err = ext4_xattr_inode_iget(parent, ea_ino, 1154 le32_to_cpu(entry->e_hash), 1155 &ea_inode); 1156 if (err) 1157 continue; 1158 1159 err = ext4_expand_inode_array(ea_inode_array, ea_inode); 1160 if (err) { 1161 ext4_warning_inode(ea_inode, 1162 "Expand inode array err=%d", err); 1163 iput(ea_inode); 1164 continue; 1165 } 1166 1167 err = ext4_xattr_ensure_credits(handle, parent, credits, bh, 1168 dirty, block_csum); 1169 if (err) { 1170 ext4_warning_inode(ea_inode, "Ensure credits err=%d", 1171 err); 1172 continue; 1173 } 1174 1175 err = ext4_xattr_inode_dec_ref(handle, ea_inode); 1176 if (err) { 1177 ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d", 1178 err); 1179 continue; 1180 } 1181 1182 if (!skip_quota) 1183 ext4_xattr_inode_free_quota(parent, ea_inode, 1184 le32_to_cpu(entry->e_value_size)); 1185 1186 /* 1187 * Forget about ea_inode within the same transaction that 1188 * decrements the ref count. This avoids duplicate decrements in 1189 * case the rest of the work spills over to subsequent 1190 * transactions. 1191 */ 1192 entry->e_value_inum = 0; 1193 entry->e_value_size = 0; 1194 1195 dirty = true; 1196 } 1197 1198 if (dirty) { 1199 /* 1200 * Note that we are deliberately skipping csum calculation for 1201 * the final update because we do not expect any journal 1202 * restarts until xattr block is freed. 1203 */ 1204 1205 err = ext4_handle_dirty_metadata(handle, NULL, bh); 1206 if (err) 1207 ext4_warning_inode(parent, 1208 "handle dirty metadata err=%d", err); 1209 } 1210 } 1211 1212 /* 1213 * Release the xattr block BH: If the reference count is > 1, decrement it; 1214 * otherwise free the block. 1215 */ 1216 static void 1217 ext4_xattr_release_block(handle_t *handle, struct inode *inode, 1218 struct buffer_head *bh, 1219 struct ext4_xattr_inode_array **ea_inode_array, 1220 int extra_credits) 1221 { 1222 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); 1223 u32 hash, ref; 1224 int error = 0; 1225 1226 BUFFER_TRACE(bh, "get_write_access"); 1227 error = ext4_journal_get_write_access(handle, bh); 1228 if (error) 1229 goto out; 1230 1231 lock_buffer(bh); 1232 hash = le32_to_cpu(BHDR(bh)->h_hash); 1233 ref = le32_to_cpu(BHDR(bh)->h_refcount); 1234 if (ref == 1) { 1235 ea_bdebug(bh, "refcount now=0; freeing"); 1236 /* 1237 * This must happen under buffer lock for 1238 * ext4_xattr_block_set() to reliably detect freed block 1239 */ 1240 if (ea_block_cache) 1241 mb_cache_entry_delete(ea_block_cache, hash, 1242 bh->b_blocknr); 1243 get_bh(bh); 1244 unlock_buffer(bh); 1245 1246 if (ext4_has_feature_ea_inode(inode->i_sb)) 1247 ext4_xattr_inode_dec_ref_all(handle, inode, bh, 1248 BFIRST(bh), 1249 true /* block_csum */, 1250 ea_inode_array, 1251 extra_credits, 1252 true /* skip_quota */); 1253 ext4_free_blocks(handle, inode, bh, 0, 1, 1254 EXT4_FREE_BLOCKS_METADATA | 1255 EXT4_FREE_BLOCKS_FORGET); 1256 } else { 1257 ref--; 1258 BHDR(bh)->h_refcount = cpu_to_le32(ref); 1259 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) { 1260 struct mb_cache_entry *ce; 1261 1262 if (ea_block_cache) { 1263 ce = mb_cache_entry_get(ea_block_cache, hash, 1264 bh->b_blocknr); 1265 if (ce) { 1266 ce->e_reusable = 1; 1267 mb_cache_entry_put(ea_block_cache, ce); 1268 } 1269 } 1270 } 1271 1272 ext4_xattr_block_csum_set(inode, bh); 1273 /* 1274 * Beware of this ugliness: Releasing of xattr block references 1275 * from different inodes can race and so we have to protect 1276 * from a race where someone else frees the block (and releases 1277 * its journal_head) before we are done dirtying the buffer. In 1278 * nojournal mode this race is harmless and we actually cannot 1279 * call ext4_handle_dirty_metadata() with locked buffer as 1280 * that function can call sync_dirty_buffer() so for that case 1281 * we handle the dirtying after unlocking the buffer. 1282 */ 1283 if (ext4_handle_valid(handle)) 1284 error = ext4_handle_dirty_metadata(handle, inode, bh); 1285 unlock_buffer(bh); 1286 if (!ext4_handle_valid(handle)) 1287 error = ext4_handle_dirty_metadata(handle, inode, bh); 1288 if (IS_SYNC(inode)) 1289 ext4_handle_sync(handle); 1290 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1)); 1291 ea_bdebug(bh, "refcount now=%d; releasing", 1292 le32_to_cpu(BHDR(bh)->h_refcount)); 1293 } 1294 out: 1295 ext4_std_error(inode->i_sb, error); 1296 return; 1297 } 1298 1299 /* 1300 * Find the available free space for EAs. This also returns the total number of 1301 * bytes used by EA entries. 1302 */ 1303 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last, 1304 size_t *min_offs, void *base, int *total) 1305 { 1306 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { 1307 if (!last->e_value_inum && last->e_value_size) { 1308 size_t offs = le16_to_cpu(last->e_value_offs); 1309 if (offs < *min_offs) 1310 *min_offs = offs; 1311 } 1312 if (total) 1313 *total += EXT4_XATTR_LEN(last->e_name_len); 1314 } 1315 return (*min_offs - ((void *)last - base) - sizeof(__u32)); 1316 } 1317 1318 /* 1319 * Write the value of the EA in an inode. 1320 */ 1321 static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode, 1322 const void *buf, int bufsize) 1323 { 1324 struct buffer_head *bh = NULL; 1325 unsigned long block = 0; 1326 int blocksize = ea_inode->i_sb->s_blocksize; 1327 int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits; 1328 int csize, wsize = 0; 1329 int ret = 0; 1330 int retries = 0; 1331 1332 retry: 1333 while (ret >= 0 && ret < max_blocks) { 1334 struct ext4_map_blocks map; 1335 map.m_lblk = block += ret; 1336 map.m_len = max_blocks -= ret; 1337 1338 ret = ext4_map_blocks(handle, ea_inode, &map, 1339 EXT4_GET_BLOCKS_CREATE); 1340 if (ret <= 0) { 1341 ext4_mark_inode_dirty(handle, ea_inode); 1342 if (ret == -ENOSPC && 1343 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) { 1344 ret = 0; 1345 goto retry; 1346 } 1347 break; 1348 } 1349 } 1350 1351 if (ret < 0) 1352 return ret; 1353 1354 block = 0; 1355 while (wsize < bufsize) { 1356 if (bh != NULL) 1357 brelse(bh); 1358 csize = (bufsize - wsize) > blocksize ? blocksize : 1359 bufsize - wsize; 1360 bh = ext4_getblk(handle, ea_inode, block, 0); 1361 if (IS_ERR(bh)) 1362 return PTR_ERR(bh); 1363 ret = ext4_journal_get_write_access(handle, bh); 1364 if (ret) 1365 goto out; 1366 1367 memcpy(bh->b_data, buf, csize); 1368 set_buffer_uptodate(bh); 1369 ext4_handle_dirty_metadata(handle, ea_inode, bh); 1370 1371 buf += csize; 1372 wsize += csize; 1373 block += 1; 1374 } 1375 1376 inode_lock(ea_inode); 1377 i_size_write(ea_inode, wsize); 1378 ext4_update_i_disksize(ea_inode, wsize); 1379 inode_unlock(ea_inode); 1380 1381 ext4_mark_inode_dirty(handle, ea_inode); 1382 1383 out: 1384 brelse(bh); 1385 1386 return ret; 1387 } 1388 1389 /* 1390 * Create an inode to store the value of a large EA. 1391 */ 1392 static struct inode *ext4_xattr_inode_create(handle_t *handle, 1393 struct inode *inode, u32 hash) 1394 { 1395 struct inode *ea_inode = NULL; 1396 uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) }; 1397 int err; 1398 1399 /* 1400 * Let the next inode be the goal, so we try and allocate the EA inode 1401 * in the same group, or nearby one. 1402 */ 1403 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode, 1404 S_IFREG | 0600, NULL, inode->i_ino + 1, owner, 1405 EXT4_EA_INODE_FL); 1406 if (!IS_ERR(ea_inode)) { 1407 ea_inode->i_op = &ext4_file_inode_operations; 1408 ea_inode->i_fop = &ext4_file_operations; 1409 ext4_set_aops(ea_inode); 1410 ext4_xattr_inode_set_class(ea_inode); 1411 unlock_new_inode(ea_inode); 1412 ext4_xattr_inode_set_ref(ea_inode, 1); 1413 ext4_xattr_inode_set_hash(ea_inode, hash); 1414 err = ext4_mark_inode_dirty(handle, ea_inode); 1415 if (!err) 1416 err = ext4_inode_attach_jinode(ea_inode); 1417 if (err) { 1418 iput(ea_inode); 1419 return ERR_PTR(err); 1420 } 1421 1422 /* 1423 * Xattr inodes are shared therefore quota charging is performed 1424 * at a higher level. 1425 */ 1426 dquot_free_inode(ea_inode); 1427 dquot_drop(ea_inode); 1428 inode_lock(ea_inode); 1429 ea_inode->i_flags |= S_NOQUOTA; 1430 inode_unlock(ea_inode); 1431 } 1432 1433 return ea_inode; 1434 } 1435 1436 static struct inode * 1437 ext4_xattr_inode_cache_find(struct inode *inode, const void *value, 1438 size_t value_len, u32 hash) 1439 { 1440 struct inode *ea_inode; 1441 struct mb_cache_entry *ce; 1442 struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode); 1443 void *ea_data; 1444 1445 if (!ea_inode_cache) 1446 return NULL; 1447 1448 ce = mb_cache_entry_find_first(ea_inode_cache, hash); 1449 if (!ce) 1450 return NULL; 1451 1452 ea_data = ext4_kvmalloc(value_len, GFP_NOFS); 1453 if (!ea_data) { 1454 mb_cache_entry_put(ea_inode_cache, ce); 1455 return NULL; 1456 } 1457 1458 while (ce) { 1459 ea_inode = ext4_iget(inode->i_sb, ce->e_value); 1460 if (!IS_ERR(ea_inode) && 1461 !is_bad_inode(ea_inode) && 1462 (EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL) && 1463 i_size_read(ea_inode) == value_len && 1464 !ext4_xattr_inode_read(ea_inode, ea_data, value_len) && 1465 !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data, 1466 value_len) && 1467 !memcmp(value, ea_data, value_len)) { 1468 mb_cache_entry_touch(ea_inode_cache, ce); 1469 mb_cache_entry_put(ea_inode_cache, ce); 1470 kvfree(ea_data); 1471 return ea_inode; 1472 } 1473 1474 if (!IS_ERR(ea_inode)) 1475 iput(ea_inode); 1476 ce = mb_cache_entry_find_next(ea_inode_cache, ce); 1477 } 1478 kvfree(ea_data); 1479 return NULL; 1480 } 1481 1482 /* 1483 * Add value of the EA in an inode. 1484 */ 1485 static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode, 1486 const void *value, size_t value_len, 1487 struct inode **ret_inode) 1488 { 1489 struct inode *ea_inode; 1490 u32 hash; 1491 int err; 1492 1493 hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len); 1494 ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash); 1495 if (ea_inode) { 1496 err = ext4_xattr_inode_inc_ref(handle, ea_inode); 1497 if (err) { 1498 iput(ea_inode); 1499 return err; 1500 } 1501 1502 *ret_inode = ea_inode; 1503 return 0; 1504 } 1505 1506 /* Create an inode for the EA value */ 1507 ea_inode = ext4_xattr_inode_create(handle, inode, hash); 1508 if (IS_ERR(ea_inode)) 1509 return PTR_ERR(ea_inode); 1510 1511 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len); 1512 if (err) { 1513 ext4_xattr_inode_dec_ref(handle, ea_inode); 1514 iput(ea_inode); 1515 return err; 1516 } 1517 1518 if (EA_INODE_CACHE(inode)) 1519 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash, 1520 ea_inode->i_ino, true /* reusable */); 1521 1522 *ret_inode = ea_inode; 1523 return 0; 1524 } 1525 1526 /* 1527 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode 1528 * feature is enabled. 1529 */ 1530 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U) 1531 1532 static int ext4_xattr_set_entry(struct ext4_xattr_info *i, 1533 struct ext4_xattr_search *s, 1534 handle_t *handle, struct inode *inode, 1535 bool is_block) 1536 { 1537 struct ext4_xattr_entry *last; 1538 struct ext4_xattr_entry *here = s->here; 1539 size_t min_offs = s->end - s->base, name_len = strlen(i->name); 1540 int in_inode = i->in_inode; 1541 struct inode *old_ea_inode = NULL; 1542 struct inode *new_ea_inode = NULL; 1543 size_t old_size, new_size; 1544 int ret; 1545 1546 /* Space used by old and new values. */ 1547 old_size = (!s->not_found && !here->e_value_inum) ? 1548 EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0; 1549 new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0; 1550 1551 /* 1552 * Optimization for the simple case when old and new values have the 1553 * same padded sizes. Not applicable if external inodes are involved. 1554 */ 1555 if (new_size && new_size == old_size) { 1556 size_t offs = le16_to_cpu(here->e_value_offs); 1557 void *val = s->base + offs; 1558 1559 here->e_value_size = cpu_to_le32(i->value_len); 1560 if (i->value == EXT4_ZERO_XATTR_VALUE) { 1561 memset(val, 0, new_size); 1562 } else { 1563 memcpy(val, i->value, i->value_len); 1564 /* Clear padding bytes. */ 1565 memset(val + i->value_len, 0, new_size - i->value_len); 1566 } 1567 goto update_hash; 1568 } 1569 1570 /* Compute min_offs and last. */ 1571 last = s->first; 1572 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { 1573 if (!last->e_value_inum && last->e_value_size) { 1574 size_t offs = le16_to_cpu(last->e_value_offs); 1575 if (offs < min_offs) 1576 min_offs = offs; 1577 } 1578 } 1579 1580 /* Check whether we have enough space. */ 1581 if (i->value) { 1582 size_t free; 1583 1584 free = min_offs - ((void *)last - s->base) - sizeof(__u32); 1585 if (!s->not_found) 1586 free += EXT4_XATTR_LEN(name_len) + old_size; 1587 1588 if (free < EXT4_XATTR_LEN(name_len) + new_size) { 1589 ret = -ENOSPC; 1590 goto out; 1591 } 1592 1593 /* 1594 * If storing the value in an external inode is an option, 1595 * reserve space for xattr entries/names in the external 1596 * attribute block so that a long value does not occupy the 1597 * whole space and prevent futher entries being added. 1598 */ 1599 if (ext4_has_feature_ea_inode(inode->i_sb) && 1600 new_size && is_block && 1601 (min_offs + old_size - new_size) < 1602 EXT4_XATTR_BLOCK_RESERVE(inode)) { 1603 ret = -ENOSPC; 1604 goto out; 1605 } 1606 } 1607 1608 /* 1609 * Getting access to old and new ea inodes is subject to failures. 1610 * Finish that work before doing any modifications to the xattr data. 1611 */ 1612 if (!s->not_found && here->e_value_inum) { 1613 ret = ext4_xattr_inode_iget(inode, 1614 le32_to_cpu(here->e_value_inum), 1615 le32_to_cpu(here->e_hash), 1616 &old_ea_inode); 1617 if (ret) { 1618 old_ea_inode = NULL; 1619 goto out; 1620 } 1621 } 1622 if (i->value && in_inode) { 1623 WARN_ON_ONCE(!i->value_len); 1624 1625 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len); 1626 if (ret) 1627 goto out; 1628 1629 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value, 1630 i->value_len, 1631 &new_ea_inode); 1632 if (ret) { 1633 new_ea_inode = NULL; 1634 ext4_xattr_inode_free_quota(inode, NULL, i->value_len); 1635 goto out; 1636 } 1637 } 1638 1639 if (old_ea_inode) { 1640 /* We are ready to release ref count on the old_ea_inode. */ 1641 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode); 1642 if (ret) { 1643 /* Release newly required ref count on new_ea_inode. */ 1644 if (new_ea_inode) { 1645 int err; 1646 1647 err = ext4_xattr_inode_dec_ref(handle, 1648 new_ea_inode); 1649 if (err) 1650 ext4_warning_inode(new_ea_inode, 1651 "dec ref new_ea_inode err=%d", 1652 err); 1653 ext4_xattr_inode_free_quota(inode, new_ea_inode, 1654 i->value_len); 1655 } 1656 goto out; 1657 } 1658 1659 ext4_xattr_inode_free_quota(inode, old_ea_inode, 1660 le32_to_cpu(here->e_value_size)); 1661 } 1662 1663 /* No failures allowed past this point. */ 1664 1665 if (!s->not_found && here->e_value_offs) { 1666 /* Remove the old value. */ 1667 void *first_val = s->base + min_offs; 1668 size_t offs = le16_to_cpu(here->e_value_offs); 1669 void *val = s->base + offs; 1670 1671 memmove(first_val + old_size, first_val, val - first_val); 1672 memset(first_val, 0, old_size); 1673 min_offs += old_size; 1674 1675 /* Adjust all value offsets. */ 1676 last = s->first; 1677 while (!IS_LAST_ENTRY(last)) { 1678 size_t o = le16_to_cpu(last->e_value_offs); 1679 1680 if (!last->e_value_inum && 1681 last->e_value_size && o < offs) 1682 last->e_value_offs = cpu_to_le16(o + old_size); 1683 last = EXT4_XATTR_NEXT(last); 1684 } 1685 } 1686 1687 if (!i->value) { 1688 /* Remove old name. */ 1689 size_t size = EXT4_XATTR_LEN(name_len); 1690 1691 last = ENTRY((void *)last - size); 1692 memmove(here, (void *)here + size, 1693 (void *)last - (void *)here + sizeof(__u32)); 1694 memset(last, 0, size); 1695 } else if (s->not_found) { 1696 /* Insert new name. */ 1697 size_t size = EXT4_XATTR_LEN(name_len); 1698 size_t rest = (void *)last - (void *)here + sizeof(__u32); 1699 1700 memmove((void *)here + size, here, rest); 1701 memset(here, 0, size); 1702 here->e_name_index = i->name_index; 1703 here->e_name_len = name_len; 1704 memcpy(here->e_name, i->name, name_len); 1705 } else { 1706 /* This is an update, reset value info. */ 1707 here->e_value_inum = 0; 1708 here->e_value_offs = 0; 1709 here->e_value_size = 0; 1710 } 1711 1712 if (i->value) { 1713 /* Insert new value. */ 1714 if (in_inode) { 1715 here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino); 1716 } else if (i->value_len) { 1717 void *val = s->base + min_offs - new_size; 1718 1719 here->e_value_offs = cpu_to_le16(min_offs - new_size); 1720 if (i->value == EXT4_ZERO_XATTR_VALUE) { 1721 memset(val, 0, new_size); 1722 } else { 1723 memcpy(val, i->value, i->value_len); 1724 /* Clear padding bytes. */ 1725 memset(val + i->value_len, 0, 1726 new_size - i->value_len); 1727 } 1728 } 1729 here->e_value_size = cpu_to_le32(i->value_len); 1730 } 1731 1732 update_hash: 1733 if (i->value) { 1734 __le32 hash = 0; 1735 1736 /* Entry hash calculation. */ 1737 if (in_inode) { 1738 __le32 crc32c_hash; 1739 1740 /* 1741 * Feed crc32c hash instead of the raw value for entry 1742 * hash calculation. This is to avoid walking 1743 * potentially long value buffer again. 1744 */ 1745 crc32c_hash = cpu_to_le32( 1746 ext4_xattr_inode_get_hash(new_ea_inode)); 1747 hash = ext4_xattr_hash_entry(here->e_name, 1748 here->e_name_len, 1749 &crc32c_hash, 1); 1750 } else if (is_block) { 1751 __le32 *value = s->base + le16_to_cpu( 1752 here->e_value_offs); 1753 1754 hash = ext4_xattr_hash_entry(here->e_name, 1755 here->e_name_len, value, 1756 new_size >> 2); 1757 } 1758 here->e_hash = hash; 1759 } 1760 1761 if (is_block) 1762 ext4_xattr_rehash((struct ext4_xattr_header *)s->base); 1763 1764 ret = 0; 1765 out: 1766 iput(old_ea_inode); 1767 iput(new_ea_inode); 1768 return ret; 1769 } 1770 1771 struct ext4_xattr_block_find { 1772 struct ext4_xattr_search s; 1773 struct buffer_head *bh; 1774 }; 1775 1776 static int 1777 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i, 1778 struct ext4_xattr_block_find *bs) 1779 { 1780 struct super_block *sb = inode->i_sb; 1781 int error; 1782 1783 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld", 1784 i->name_index, i->name, i->value, (long)i->value_len); 1785 1786 if (EXT4_I(inode)->i_file_acl) { 1787 /* The inode already has an extended attribute block. */ 1788 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl); 1789 error = -EIO; 1790 if (!bs->bh) 1791 goto cleanup; 1792 ea_bdebug(bs->bh, "b_count=%d, refcount=%d", 1793 atomic_read(&(bs->bh->b_count)), 1794 le32_to_cpu(BHDR(bs->bh)->h_refcount)); 1795 if (ext4_xattr_check_block(inode, bs->bh)) { 1796 EXT4_ERROR_INODE(inode, "bad block %llu", 1797 EXT4_I(inode)->i_file_acl); 1798 error = -EFSCORRUPTED; 1799 goto cleanup; 1800 } 1801 /* Find the named attribute. */ 1802 bs->s.base = BHDR(bs->bh); 1803 bs->s.first = BFIRST(bs->bh); 1804 bs->s.end = bs->bh->b_data + bs->bh->b_size; 1805 bs->s.here = bs->s.first; 1806 error = ext4_xattr_find_entry(&bs->s.here, i->name_index, 1807 i->name, 1); 1808 if (error && error != -ENODATA) 1809 goto cleanup; 1810 bs->s.not_found = error; 1811 } 1812 error = 0; 1813 1814 cleanup: 1815 return error; 1816 } 1817 1818 static int 1819 ext4_xattr_block_set(handle_t *handle, struct inode *inode, 1820 struct ext4_xattr_info *i, 1821 struct ext4_xattr_block_find *bs) 1822 { 1823 struct super_block *sb = inode->i_sb; 1824 struct buffer_head *new_bh = NULL; 1825 struct ext4_xattr_search s_copy = bs->s; 1826 struct ext4_xattr_search *s = &s_copy; 1827 struct mb_cache_entry *ce = NULL; 1828 int error = 0; 1829 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); 1830 struct inode *ea_inode = NULL, *tmp_inode; 1831 size_t old_ea_inode_quota = 0; 1832 unsigned int ea_ino; 1833 1834 1835 #define header(x) ((struct ext4_xattr_header *)(x)) 1836 1837 if (s->base) { 1838 BUFFER_TRACE(bs->bh, "get_write_access"); 1839 error = ext4_journal_get_write_access(handle, bs->bh); 1840 if (error) 1841 goto cleanup; 1842 lock_buffer(bs->bh); 1843 1844 if (header(s->base)->h_refcount == cpu_to_le32(1)) { 1845 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash); 1846 1847 /* 1848 * This must happen under buffer lock for 1849 * ext4_xattr_block_set() to reliably detect modified 1850 * block 1851 */ 1852 if (ea_block_cache) 1853 mb_cache_entry_delete(ea_block_cache, hash, 1854 bs->bh->b_blocknr); 1855 ea_bdebug(bs->bh, "modifying in-place"); 1856 error = ext4_xattr_set_entry(i, s, handle, inode, 1857 true /* is_block */); 1858 ext4_xattr_block_csum_set(inode, bs->bh); 1859 unlock_buffer(bs->bh); 1860 if (error == -EFSCORRUPTED) 1861 goto bad_block; 1862 if (!error) 1863 error = ext4_handle_dirty_metadata(handle, 1864 inode, 1865 bs->bh); 1866 if (error) 1867 goto cleanup; 1868 goto inserted; 1869 } else { 1870 int offset = (char *)s->here - bs->bh->b_data; 1871 1872 unlock_buffer(bs->bh); 1873 ea_bdebug(bs->bh, "cloning"); 1874 s->base = kmalloc(bs->bh->b_size, GFP_NOFS); 1875 error = -ENOMEM; 1876 if (s->base == NULL) 1877 goto cleanup; 1878 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size); 1879 s->first = ENTRY(header(s->base)+1); 1880 header(s->base)->h_refcount = cpu_to_le32(1); 1881 s->here = ENTRY(s->base + offset); 1882 s->end = s->base + bs->bh->b_size; 1883 1884 /* 1885 * If existing entry points to an xattr inode, we need 1886 * to prevent ext4_xattr_set_entry() from decrementing 1887 * ref count on it because the reference belongs to the 1888 * original block. In this case, make the entry look 1889 * like it has an empty value. 1890 */ 1891 if (!s->not_found && s->here->e_value_inum) { 1892 ea_ino = le32_to_cpu(s->here->e_value_inum); 1893 error = ext4_xattr_inode_iget(inode, ea_ino, 1894 le32_to_cpu(s->here->e_hash), 1895 &tmp_inode); 1896 if (error) 1897 goto cleanup; 1898 1899 if (!ext4_test_inode_state(tmp_inode, 1900 EXT4_STATE_LUSTRE_EA_INODE)) { 1901 /* 1902 * Defer quota free call for previous 1903 * inode until success is guaranteed. 1904 */ 1905 old_ea_inode_quota = le32_to_cpu( 1906 s->here->e_value_size); 1907 } 1908 iput(tmp_inode); 1909 1910 s->here->e_value_inum = 0; 1911 s->here->e_value_size = 0; 1912 } 1913 } 1914 } else { 1915 /* Allocate a buffer where we construct the new block. */ 1916 s->base = kzalloc(sb->s_blocksize, GFP_NOFS); 1917 /* assert(header == s->base) */ 1918 error = -ENOMEM; 1919 if (s->base == NULL) 1920 goto cleanup; 1921 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); 1922 header(s->base)->h_blocks = cpu_to_le32(1); 1923 header(s->base)->h_refcount = cpu_to_le32(1); 1924 s->first = ENTRY(header(s->base)+1); 1925 s->here = ENTRY(header(s->base)+1); 1926 s->end = s->base + sb->s_blocksize; 1927 } 1928 1929 error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */); 1930 if (error == -EFSCORRUPTED) 1931 goto bad_block; 1932 if (error) 1933 goto cleanup; 1934 1935 if (i->value && s->here->e_value_inum) { 1936 /* 1937 * A ref count on ea_inode has been taken as part of the call to 1938 * ext4_xattr_set_entry() above. We would like to drop this 1939 * extra ref but we have to wait until the xattr block is 1940 * initialized and has its own ref count on the ea_inode. 1941 */ 1942 ea_ino = le32_to_cpu(s->here->e_value_inum); 1943 error = ext4_xattr_inode_iget(inode, ea_ino, 1944 le32_to_cpu(s->here->e_hash), 1945 &ea_inode); 1946 if (error) { 1947 ea_inode = NULL; 1948 goto cleanup; 1949 } 1950 } 1951 1952 inserted: 1953 if (!IS_LAST_ENTRY(s->first)) { 1954 new_bh = ext4_xattr_block_cache_find(inode, header(s->base), 1955 &ce); 1956 if (new_bh) { 1957 /* We found an identical block in the cache. */ 1958 if (new_bh == bs->bh) 1959 ea_bdebug(new_bh, "keeping"); 1960 else { 1961 u32 ref; 1962 1963 WARN_ON_ONCE(dquot_initialize_needed(inode)); 1964 1965 /* The old block is released after updating 1966 the inode. */ 1967 error = dquot_alloc_block(inode, 1968 EXT4_C2B(EXT4_SB(sb), 1)); 1969 if (error) 1970 goto cleanup; 1971 BUFFER_TRACE(new_bh, "get_write_access"); 1972 error = ext4_journal_get_write_access(handle, 1973 new_bh); 1974 if (error) 1975 goto cleanup_dquot; 1976 lock_buffer(new_bh); 1977 /* 1978 * We have to be careful about races with 1979 * freeing, rehashing or adding references to 1980 * xattr block. Once we hold buffer lock xattr 1981 * block's state is stable so we can check 1982 * whether the block got freed / rehashed or 1983 * not. Since we unhash mbcache entry under 1984 * buffer lock when freeing / rehashing xattr 1985 * block, checking whether entry is still 1986 * hashed is reliable. Same rules hold for 1987 * e_reusable handling. 1988 */ 1989 if (hlist_bl_unhashed(&ce->e_hash_list) || 1990 !ce->e_reusable) { 1991 /* 1992 * Undo everything and check mbcache 1993 * again. 1994 */ 1995 unlock_buffer(new_bh); 1996 dquot_free_block(inode, 1997 EXT4_C2B(EXT4_SB(sb), 1998 1)); 1999 brelse(new_bh); 2000 mb_cache_entry_put(ea_block_cache, ce); 2001 ce = NULL; 2002 new_bh = NULL; 2003 goto inserted; 2004 } 2005 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1; 2006 BHDR(new_bh)->h_refcount = cpu_to_le32(ref); 2007 if (ref >= EXT4_XATTR_REFCOUNT_MAX) 2008 ce->e_reusable = 0; 2009 ea_bdebug(new_bh, "reusing; refcount now=%d", 2010 ref); 2011 ext4_xattr_block_csum_set(inode, new_bh); 2012 unlock_buffer(new_bh); 2013 error = ext4_handle_dirty_metadata(handle, 2014 inode, 2015 new_bh); 2016 if (error) 2017 goto cleanup_dquot; 2018 } 2019 mb_cache_entry_touch(ea_block_cache, ce); 2020 mb_cache_entry_put(ea_block_cache, ce); 2021 ce = NULL; 2022 } else if (bs->bh && s->base == bs->bh->b_data) { 2023 /* We were modifying this block in-place. */ 2024 ea_bdebug(bs->bh, "keeping this block"); 2025 ext4_xattr_block_cache_insert(ea_block_cache, bs->bh); 2026 new_bh = bs->bh; 2027 get_bh(new_bh); 2028 } else { 2029 /* We need to allocate a new block */ 2030 ext4_fsblk_t goal, block; 2031 2032 WARN_ON_ONCE(dquot_initialize_needed(inode)); 2033 2034 goal = ext4_group_first_block_no(sb, 2035 EXT4_I(inode)->i_block_group); 2036 2037 /* non-extent files can't have physical blocks past 2^32 */ 2038 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 2039 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS; 2040 2041 block = ext4_new_meta_blocks(handle, inode, goal, 0, 2042 NULL, &error); 2043 if (error) 2044 goto cleanup; 2045 2046 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 2047 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS); 2048 2049 ea_idebug(inode, "creating block %llu", 2050 (unsigned long long)block); 2051 2052 new_bh = sb_getblk(sb, block); 2053 if (unlikely(!new_bh)) { 2054 error = -ENOMEM; 2055 getblk_failed: 2056 ext4_free_blocks(handle, inode, NULL, block, 1, 2057 EXT4_FREE_BLOCKS_METADATA); 2058 goto cleanup; 2059 } 2060 error = ext4_xattr_inode_inc_ref_all(handle, inode, 2061 ENTRY(header(s->base)+1)); 2062 if (error) 2063 goto getblk_failed; 2064 if (ea_inode) { 2065 /* Drop the extra ref on ea_inode. */ 2066 error = ext4_xattr_inode_dec_ref(handle, 2067 ea_inode); 2068 if (error) 2069 ext4_warning_inode(ea_inode, 2070 "dec ref error=%d", 2071 error); 2072 iput(ea_inode); 2073 ea_inode = NULL; 2074 } 2075 2076 lock_buffer(new_bh); 2077 error = ext4_journal_get_create_access(handle, new_bh); 2078 if (error) { 2079 unlock_buffer(new_bh); 2080 error = -EIO; 2081 goto getblk_failed; 2082 } 2083 memcpy(new_bh->b_data, s->base, new_bh->b_size); 2084 ext4_xattr_block_csum_set(inode, new_bh); 2085 set_buffer_uptodate(new_bh); 2086 unlock_buffer(new_bh); 2087 ext4_xattr_block_cache_insert(ea_block_cache, new_bh); 2088 error = ext4_handle_dirty_metadata(handle, inode, 2089 new_bh); 2090 if (error) 2091 goto cleanup; 2092 } 2093 } 2094 2095 if (old_ea_inode_quota) 2096 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota); 2097 2098 /* Update the inode. */ 2099 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0; 2100 2101 /* Drop the previous xattr block. */ 2102 if (bs->bh && bs->bh != new_bh) { 2103 struct ext4_xattr_inode_array *ea_inode_array = NULL; 2104 2105 ext4_xattr_release_block(handle, inode, bs->bh, 2106 &ea_inode_array, 2107 0 /* extra_credits */); 2108 ext4_xattr_inode_array_free(ea_inode_array); 2109 } 2110 error = 0; 2111 2112 cleanup: 2113 if (ea_inode) { 2114 int error2; 2115 2116 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode); 2117 if (error2) 2118 ext4_warning_inode(ea_inode, "dec ref error=%d", 2119 error2); 2120 2121 /* If there was an error, revert the quota charge. */ 2122 if (error) 2123 ext4_xattr_inode_free_quota(inode, ea_inode, 2124 i_size_read(ea_inode)); 2125 iput(ea_inode); 2126 } 2127 if (ce) 2128 mb_cache_entry_put(ea_block_cache, ce); 2129 brelse(new_bh); 2130 if (!(bs->bh && s->base == bs->bh->b_data)) 2131 kfree(s->base); 2132 2133 return error; 2134 2135 cleanup_dquot: 2136 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1)); 2137 goto cleanup; 2138 2139 bad_block: 2140 EXT4_ERROR_INODE(inode, "bad block %llu", 2141 EXT4_I(inode)->i_file_acl); 2142 goto cleanup; 2143 2144 #undef header 2145 } 2146 2147 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i, 2148 struct ext4_xattr_ibody_find *is) 2149 { 2150 struct ext4_xattr_ibody_header *header; 2151 struct ext4_inode *raw_inode; 2152 int error; 2153 2154 if (EXT4_I(inode)->i_extra_isize == 0) 2155 return 0; 2156 raw_inode = ext4_raw_inode(&is->iloc); 2157 header = IHDR(inode, raw_inode); 2158 is->s.base = is->s.first = IFIRST(header); 2159 is->s.here = is->s.first; 2160 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; 2161 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 2162 error = xattr_check_inode(inode, header, is->s.end); 2163 if (error) 2164 return error; 2165 /* Find the named attribute. */ 2166 error = ext4_xattr_find_entry(&is->s.here, i->name_index, 2167 i->name, 0); 2168 if (error && error != -ENODATA) 2169 return error; 2170 is->s.not_found = error; 2171 } 2172 return 0; 2173 } 2174 2175 int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode, 2176 struct ext4_xattr_info *i, 2177 struct ext4_xattr_ibody_find *is) 2178 { 2179 struct ext4_xattr_ibody_header *header; 2180 struct ext4_xattr_search *s = &is->s; 2181 int error; 2182 2183 if (EXT4_I(inode)->i_extra_isize == 0) 2184 return -ENOSPC; 2185 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */); 2186 if (error) { 2187 if (error == -ENOSPC && 2188 ext4_has_inline_data(inode)) { 2189 error = ext4_try_to_evict_inline_data(handle, inode, 2190 EXT4_XATTR_LEN(strlen(i->name) + 2191 EXT4_XATTR_SIZE(i->value_len))); 2192 if (error) 2193 return error; 2194 error = ext4_xattr_ibody_find(inode, i, is); 2195 if (error) 2196 return error; 2197 error = ext4_xattr_set_entry(i, s, handle, inode, 2198 false /* is_block */); 2199 } 2200 if (error) 2201 return error; 2202 } 2203 header = IHDR(inode, ext4_raw_inode(&is->iloc)); 2204 if (!IS_LAST_ENTRY(s->first)) { 2205 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); 2206 ext4_set_inode_state(inode, EXT4_STATE_XATTR); 2207 } else { 2208 header->h_magic = cpu_to_le32(0); 2209 ext4_clear_inode_state(inode, EXT4_STATE_XATTR); 2210 } 2211 return 0; 2212 } 2213 2214 static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode, 2215 struct ext4_xattr_info *i, 2216 struct ext4_xattr_ibody_find *is) 2217 { 2218 struct ext4_xattr_ibody_header *header; 2219 struct ext4_xattr_search *s = &is->s; 2220 int error; 2221 2222 if (EXT4_I(inode)->i_extra_isize == 0) 2223 return -ENOSPC; 2224 error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */); 2225 if (error) 2226 return error; 2227 header = IHDR(inode, ext4_raw_inode(&is->iloc)); 2228 if (!IS_LAST_ENTRY(s->first)) { 2229 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC); 2230 ext4_set_inode_state(inode, EXT4_STATE_XATTR); 2231 } else { 2232 header->h_magic = cpu_to_le32(0); 2233 ext4_clear_inode_state(inode, EXT4_STATE_XATTR); 2234 } 2235 return 0; 2236 } 2237 2238 static int ext4_xattr_value_same(struct ext4_xattr_search *s, 2239 struct ext4_xattr_info *i) 2240 { 2241 void *value; 2242 2243 /* When e_value_inum is set the value is stored externally. */ 2244 if (s->here->e_value_inum) 2245 return 0; 2246 if (le32_to_cpu(s->here->e_value_size) != i->value_len) 2247 return 0; 2248 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs); 2249 return !memcmp(value, i->value, i->value_len); 2250 } 2251 2252 static struct buffer_head *ext4_xattr_get_block(struct inode *inode) 2253 { 2254 struct buffer_head *bh; 2255 int error; 2256 2257 if (!EXT4_I(inode)->i_file_acl) 2258 return NULL; 2259 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); 2260 if (!bh) 2261 return ERR_PTR(-EIO); 2262 error = ext4_xattr_check_block(inode, bh); 2263 if (error) 2264 return ERR_PTR(error); 2265 return bh; 2266 } 2267 2268 /* 2269 * ext4_xattr_set_handle() 2270 * 2271 * Create, replace or remove an extended attribute for this inode. Value 2272 * is NULL to remove an existing extended attribute, and non-NULL to 2273 * either replace an existing extended attribute, or create a new extended 2274 * attribute. The flags XATTR_REPLACE and XATTR_CREATE 2275 * specify that an extended attribute must exist and must not exist 2276 * previous to the call, respectively. 2277 * 2278 * Returns 0, or a negative error number on failure. 2279 */ 2280 int 2281 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index, 2282 const char *name, const void *value, size_t value_len, 2283 int flags) 2284 { 2285 struct ext4_xattr_info i = { 2286 .name_index = name_index, 2287 .name = name, 2288 .value = value, 2289 .value_len = value_len, 2290 .in_inode = 0, 2291 }; 2292 struct ext4_xattr_ibody_find is = { 2293 .s = { .not_found = -ENODATA, }, 2294 }; 2295 struct ext4_xattr_block_find bs = { 2296 .s = { .not_found = -ENODATA, }, 2297 }; 2298 int no_expand; 2299 int error; 2300 2301 if (!name) 2302 return -EINVAL; 2303 if (strlen(name) > 255) 2304 return -ERANGE; 2305 2306 ext4_write_lock_xattr(inode, &no_expand); 2307 2308 /* Check journal credits under write lock. */ 2309 if (ext4_handle_valid(handle)) { 2310 struct buffer_head *bh; 2311 int credits; 2312 2313 bh = ext4_xattr_get_block(inode); 2314 if (IS_ERR(bh)) { 2315 error = PTR_ERR(bh); 2316 goto cleanup; 2317 } 2318 2319 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh, 2320 value_len, 2321 flags & XATTR_CREATE); 2322 brelse(bh); 2323 2324 if (!ext4_handle_has_enough_credits(handle, credits)) { 2325 error = -ENOSPC; 2326 goto cleanup; 2327 } 2328 } 2329 2330 error = ext4_reserve_inode_write(handle, inode, &is.iloc); 2331 if (error) 2332 goto cleanup; 2333 2334 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) { 2335 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc); 2336 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); 2337 ext4_clear_inode_state(inode, EXT4_STATE_NEW); 2338 } 2339 2340 error = ext4_xattr_ibody_find(inode, &i, &is); 2341 if (error) 2342 goto cleanup; 2343 if (is.s.not_found) 2344 error = ext4_xattr_block_find(inode, &i, &bs); 2345 if (error) 2346 goto cleanup; 2347 if (is.s.not_found && bs.s.not_found) { 2348 error = -ENODATA; 2349 if (flags & XATTR_REPLACE) 2350 goto cleanup; 2351 error = 0; 2352 if (!value) 2353 goto cleanup; 2354 } else { 2355 error = -EEXIST; 2356 if (flags & XATTR_CREATE) 2357 goto cleanup; 2358 } 2359 2360 if (!value) { 2361 if (!is.s.not_found) 2362 error = ext4_xattr_ibody_set(handle, inode, &i, &is); 2363 else if (!bs.s.not_found) 2364 error = ext4_xattr_block_set(handle, inode, &i, &bs); 2365 } else { 2366 error = 0; 2367 /* Xattr value did not change? Save us some work and bail out */ 2368 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i)) 2369 goto cleanup; 2370 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i)) 2371 goto cleanup; 2372 2373 if (ext4_has_feature_ea_inode(inode->i_sb) && 2374 (EXT4_XATTR_SIZE(i.value_len) > 2375 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize))) 2376 i.in_inode = 1; 2377 retry_inode: 2378 error = ext4_xattr_ibody_set(handle, inode, &i, &is); 2379 if (!error && !bs.s.not_found) { 2380 i.value = NULL; 2381 error = ext4_xattr_block_set(handle, inode, &i, &bs); 2382 } else if (error == -ENOSPC) { 2383 if (EXT4_I(inode)->i_file_acl && !bs.s.base) { 2384 error = ext4_xattr_block_find(inode, &i, &bs); 2385 if (error) 2386 goto cleanup; 2387 } 2388 error = ext4_xattr_block_set(handle, inode, &i, &bs); 2389 if (!error && !is.s.not_found) { 2390 i.value = NULL; 2391 error = ext4_xattr_ibody_set(handle, inode, &i, 2392 &is); 2393 } else if (error == -ENOSPC) { 2394 /* 2395 * Xattr does not fit in the block, store at 2396 * external inode if possible. 2397 */ 2398 if (ext4_has_feature_ea_inode(inode->i_sb) && 2399 !i.in_inode) { 2400 i.in_inode = 1; 2401 goto retry_inode; 2402 } 2403 } 2404 } 2405 } 2406 if (!error) { 2407 ext4_xattr_update_super_block(handle, inode->i_sb); 2408 inode->i_ctime = current_time(inode); 2409 if (!value) 2410 no_expand = 0; 2411 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); 2412 /* 2413 * The bh is consumed by ext4_mark_iloc_dirty, even with 2414 * error != 0. 2415 */ 2416 is.iloc.bh = NULL; 2417 if (IS_SYNC(inode)) 2418 ext4_handle_sync(handle); 2419 } 2420 2421 cleanup: 2422 brelse(is.iloc.bh); 2423 brelse(bs.bh); 2424 ext4_write_unlock_xattr(inode, &no_expand); 2425 return error; 2426 } 2427 2428 int ext4_xattr_set_credits(struct inode *inode, size_t value_len, 2429 bool is_create, int *credits) 2430 { 2431 struct buffer_head *bh; 2432 int err; 2433 2434 *credits = 0; 2435 2436 if (!EXT4_SB(inode->i_sb)->s_journal) 2437 return 0; 2438 2439 down_read(&EXT4_I(inode)->xattr_sem); 2440 2441 bh = ext4_xattr_get_block(inode); 2442 if (IS_ERR(bh)) { 2443 err = PTR_ERR(bh); 2444 } else { 2445 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh, 2446 value_len, is_create); 2447 brelse(bh); 2448 err = 0; 2449 } 2450 2451 up_read(&EXT4_I(inode)->xattr_sem); 2452 return err; 2453 } 2454 2455 /* 2456 * ext4_xattr_set() 2457 * 2458 * Like ext4_xattr_set_handle, but start from an inode. This extended 2459 * attribute modification is a filesystem transaction by itself. 2460 * 2461 * Returns 0, or a negative error number on failure. 2462 */ 2463 int 2464 ext4_xattr_set(struct inode *inode, int name_index, const char *name, 2465 const void *value, size_t value_len, int flags) 2466 { 2467 handle_t *handle; 2468 struct super_block *sb = inode->i_sb; 2469 int error, retries = 0; 2470 int credits; 2471 2472 error = dquot_initialize(inode); 2473 if (error) 2474 return error; 2475 2476 retry: 2477 error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE, 2478 &credits); 2479 if (error) 2480 return error; 2481 2482 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits); 2483 if (IS_ERR(handle)) { 2484 error = PTR_ERR(handle); 2485 } else { 2486 int error2; 2487 2488 error = ext4_xattr_set_handle(handle, inode, name_index, name, 2489 value, value_len, flags); 2490 error2 = ext4_journal_stop(handle); 2491 if (error == -ENOSPC && 2492 ext4_should_retry_alloc(sb, &retries)) 2493 goto retry; 2494 if (error == 0) 2495 error = error2; 2496 } 2497 2498 return error; 2499 } 2500 2501 /* 2502 * Shift the EA entries in the inode to create space for the increased 2503 * i_extra_isize. 2504 */ 2505 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry, 2506 int value_offs_shift, void *to, 2507 void *from, size_t n) 2508 { 2509 struct ext4_xattr_entry *last = entry; 2510 int new_offs; 2511 2512 /* We always shift xattr headers further thus offsets get lower */ 2513 BUG_ON(value_offs_shift > 0); 2514 2515 /* Adjust the value offsets of the entries */ 2516 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { 2517 if (!last->e_value_inum && last->e_value_size) { 2518 new_offs = le16_to_cpu(last->e_value_offs) + 2519 value_offs_shift; 2520 last->e_value_offs = cpu_to_le16(new_offs); 2521 } 2522 } 2523 /* Shift the entries by n bytes */ 2524 memmove(to, from, n); 2525 } 2526 2527 /* 2528 * Move xattr pointed to by 'entry' from inode into external xattr block 2529 */ 2530 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode, 2531 struct ext4_inode *raw_inode, 2532 struct ext4_xattr_entry *entry) 2533 { 2534 struct ext4_xattr_ibody_find *is = NULL; 2535 struct ext4_xattr_block_find *bs = NULL; 2536 char *buffer = NULL, *b_entry_name = NULL; 2537 size_t value_size = le32_to_cpu(entry->e_value_size); 2538 struct ext4_xattr_info i = { 2539 .value = NULL, 2540 .value_len = 0, 2541 .name_index = entry->e_name_index, 2542 .in_inode = !!entry->e_value_inum, 2543 }; 2544 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); 2545 int error; 2546 2547 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS); 2548 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS); 2549 buffer = kmalloc(value_size, GFP_NOFS); 2550 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS); 2551 if (!is || !bs || !buffer || !b_entry_name) { 2552 error = -ENOMEM; 2553 goto out; 2554 } 2555 2556 is->s.not_found = -ENODATA; 2557 bs->s.not_found = -ENODATA; 2558 is->iloc.bh = NULL; 2559 bs->bh = NULL; 2560 2561 /* Save the entry name and the entry value */ 2562 if (entry->e_value_inum) { 2563 error = ext4_xattr_inode_get(inode, entry, buffer, value_size); 2564 if (error) 2565 goto out; 2566 } else { 2567 size_t value_offs = le16_to_cpu(entry->e_value_offs); 2568 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size); 2569 } 2570 2571 memcpy(b_entry_name, entry->e_name, entry->e_name_len); 2572 b_entry_name[entry->e_name_len] = '\0'; 2573 i.name = b_entry_name; 2574 2575 error = ext4_get_inode_loc(inode, &is->iloc); 2576 if (error) 2577 goto out; 2578 2579 error = ext4_xattr_ibody_find(inode, &i, is); 2580 if (error) 2581 goto out; 2582 2583 /* Remove the chosen entry from the inode */ 2584 error = ext4_xattr_ibody_set(handle, inode, &i, is); 2585 if (error) 2586 goto out; 2587 2588 i.value = buffer; 2589 i.value_len = value_size; 2590 error = ext4_xattr_block_find(inode, &i, bs); 2591 if (error) 2592 goto out; 2593 2594 /* Add entry which was removed from the inode into the block */ 2595 error = ext4_xattr_block_set(handle, inode, &i, bs); 2596 if (error) 2597 goto out; 2598 error = 0; 2599 out: 2600 kfree(b_entry_name); 2601 kfree(buffer); 2602 if (is) 2603 brelse(is->iloc.bh); 2604 kfree(is); 2605 kfree(bs); 2606 2607 return error; 2608 } 2609 2610 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode, 2611 struct ext4_inode *raw_inode, 2612 int isize_diff, size_t ifree, 2613 size_t bfree, int *total_ino) 2614 { 2615 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode); 2616 struct ext4_xattr_entry *small_entry; 2617 struct ext4_xattr_entry *entry; 2618 struct ext4_xattr_entry *last; 2619 unsigned int entry_size; /* EA entry size */ 2620 unsigned int total_size; /* EA entry size + value size */ 2621 unsigned int min_total_size; 2622 int error; 2623 2624 while (isize_diff > ifree) { 2625 entry = NULL; 2626 small_entry = NULL; 2627 min_total_size = ~0U; 2628 last = IFIRST(header); 2629 /* Find the entry best suited to be pushed into EA block */ 2630 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) { 2631 total_size = EXT4_XATTR_LEN(last->e_name_len); 2632 if (!last->e_value_inum) 2633 total_size += EXT4_XATTR_SIZE( 2634 le32_to_cpu(last->e_value_size)); 2635 if (total_size <= bfree && 2636 total_size < min_total_size) { 2637 if (total_size + ifree < isize_diff) { 2638 small_entry = last; 2639 } else { 2640 entry = last; 2641 min_total_size = total_size; 2642 } 2643 } 2644 } 2645 2646 if (entry == NULL) { 2647 if (small_entry == NULL) 2648 return -ENOSPC; 2649 entry = small_entry; 2650 } 2651 2652 entry_size = EXT4_XATTR_LEN(entry->e_name_len); 2653 total_size = entry_size; 2654 if (!entry->e_value_inum) 2655 total_size += EXT4_XATTR_SIZE( 2656 le32_to_cpu(entry->e_value_size)); 2657 error = ext4_xattr_move_to_block(handle, inode, raw_inode, 2658 entry); 2659 if (error) 2660 return error; 2661 2662 *total_ino -= entry_size; 2663 ifree += total_size; 2664 bfree -= total_size; 2665 } 2666 2667 return 0; 2668 } 2669 2670 /* 2671 * Expand an inode by new_extra_isize bytes when EAs are present. 2672 * Returns 0 on success or negative error number on failure. 2673 */ 2674 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize, 2675 struct ext4_inode *raw_inode, handle_t *handle) 2676 { 2677 struct ext4_xattr_ibody_header *header; 2678 struct buffer_head *bh; 2679 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2680 static unsigned int mnt_count; 2681 size_t min_offs; 2682 size_t ifree, bfree; 2683 int total_ino; 2684 void *base, *end; 2685 int error = 0, tried_min_extra_isize = 0; 2686 int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize); 2687 int isize_diff; /* How much do we need to grow i_extra_isize */ 2688 2689 retry: 2690 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize; 2691 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) 2692 return 0; 2693 2694 header = IHDR(inode, raw_inode); 2695 2696 /* 2697 * Check if enough free space is available in the inode to shift the 2698 * entries ahead by new_extra_isize. 2699 */ 2700 2701 base = IFIRST(header); 2702 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; 2703 min_offs = end - base; 2704 total_ino = sizeof(struct ext4_xattr_ibody_header); 2705 2706 error = xattr_check_inode(inode, header, end); 2707 if (error) 2708 goto cleanup; 2709 2710 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino); 2711 if (ifree >= isize_diff) 2712 goto shift; 2713 2714 /* 2715 * Enough free space isn't available in the inode, check if 2716 * EA block can hold new_extra_isize bytes. 2717 */ 2718 if (EXT4_I(inode)->i_file_acl) { 2719 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); 2720 error = -EIO; 2721 if (!bh) 2722 goto cleanup; 2723 if (ext4_xattr_check_block(inode, bh)) { 2724 EXT4_ERROR_INODE(inode, "bad block %llu", 2725 EXT4_I(inode)->i_file_acl); 2726 error = -EFSCORRUPTED; 2727 brelse(bh); 2728 goto cleanup; 2729 } 2730 base = BHDR(bh); 2731 end = bh->b_data + bh->b_size; 2732 min_offs = end - base; 2733 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base, 2734 NULL); 2735 brelse(bh); 2736 if (bfree + ifree < isize_diff) { 2737 if (!tried_min_extra_isize && s_min_extra_isize) { 2738 tried_min_extra_isize++; 2739 new_extra_isize = s_min_extra_isize; 2740 goto retry; 2741 } 2742 error = -ENOSPC; 2743 goto cleanup; 2744 } 2745 } else { 2746 bfree = inode->i_sb->s_blocksize; 2747 } 2748 2749 error = ext4_xattr_make_inode_space(handle, inode, raw_inode, 2750 isize_diff, ifree, bfree, 2751 &total_ino); 2752 if (error) { 2753 if (error == -ENOSPC && !tried_min_extra_isize && 2754 s_min_extra_isize) { 2755 tried_min_extra_isize++; 2756 new_extra_isize = s_min_extra_isize; 2757 goto retry; 2758 } 2759 goto cleanup; 2760 } 2761 shift: 2762 /* Adjust the offsets and shift the remaining entries ahead */ 2763 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize 2764 - new_extra_isize, (void *)raw_inode + 2765 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize, 2766 (void *)header, total_ino); 2767 EXT4_I(inode)->i_extra_isize = new_extra_isize; 2768 2769 cleanup: 2770 if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) { 2771 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.", 2772 inode->i_ino); 2773 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count); 2774 } 2775 return error; 2776 } 2777 2778 #define EIA_INCR 16 /* must be 2^n */ 2779 #define EIA_MASK (EIA_INCR - 1) 2780 2781 /* Add the large xattr @inode into @ea_inode_array for deferred iput(). 2782 * If @ea_inode_array is new or full it will be grown and the old 2783 * contents copied over. 2784 */ 2785 static int 2786 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array, 2787 struct inode *inode) 2788 { 2789 if (*ea_inode_array == NULL) { 2790 /* 2791 * Start with 15 inodes, so it fits into a power-of-two size. 2792 * If *ea_inode_array is NULL, this is essentially offsetof() 2793 */ 2794 (*ea_inode_array) = 2795 kmalloc(offsetof(struct ext4_xattr_inode_array, 2796 inodes[EIA_MASK]), 2797 GFP_NOFS); 2798 if (*ea_inode_array == NULL) 2799 return -ENOMEM; 2800 (*ea_inode_array)->count = 0; 2801 } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) { 2802 /* expand the array once all 15 + n * 16 slots are full */ 2803 struct ext4_xattr_inode_array *new_array = NULL; 2804 int count = (*ea_inode_array)->count; 2805 2806 /* if new_array is NULL, this is essentially offsetof() */ 2807 new_array = kmalloc( 2808 offsetof(struct ext4_xattr_inode_array, 2809 inodes[count + EIA_INCR]), 2810 GFP_NOFS); 2811 if (new_array == NULL) 2812 return -ENOMEM; 2813 memcpy(new_array, *ea_inode_array, 2814 offsetof(struct ext4_xattr_inode_array, inodes[count])); 2815 kfree(*ea_inode_array); 2816 *ea_inode_array = new_array; 2817 } 2818 (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode; 2819 return 0; 2820 } 2821 2822 /* 2823 * ext4_xattr_delete_inode() 2824 * 2825 * Free extended attribute resources associated with this inode. Traverse 2826 * all entries and decrement reference on any xattr inodes associated with this 2827 * inode. This is called immediately before an inode is freed. We have exclusive 2828 * access to the inode. If an orphan inode is deleted it will also release its 2829 * references on xattr block and xattr inodes. 2830 */ 2831 int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode, 2832 struct ext4_xattr_inode_array **ea_inode_array, 2833 int extra_credits) 2834 { 2835 struct buffer_head *bh = NULL; 2836 struct ext4_xattr_ibody_header *header; 2837 struct ext4_iloc iloc = { .bh = NULL }; 2838 struct ext4_xattr_entry *entry; 2839 struct inode *ea_inode; 2840 int error; 2841 2842 error = ext4_xattr_ensure_credits(handle, inode, extra_credits, 2843 NULL /* bh */, 2844 false /* dirty */, 2845 false /* block_csum */); 2846 if (error) { 2847 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error); 2848 goto cleanup; 2849 } 2850 2851 if (ext4_has_feature_ea_inode(inode->i_sb) && 2852 ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 2853 2854 error = ext4_get_inode_loc(inode, &iloc); 2855 if (error) { 2856 EXT4_ERROR_INODE(inode, "inode loc (error %d)", error); 2857 goto cleanup; 2858 } 2859 2860 error = ext4_journal_get_write_access(handle, iloc.bh); 2861 if (error) { 2862 EXT4_ERROR_INODE(inode, "write access (error %d)", 2863 error); 2864 goto cleanup; 2865 } 2866 2867 header = IHDR(inode, ext4_raw_inode(&iloc)); 2868 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC)) 2869 ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh, 2870 IFIRST(header), 2871 false /* block_csum */, 2872 ea_inode_array, 2873 extra_credits, 2874 false /* skip_quota */); 2875 } 2876 2877 if (EXT4_I(inode)->i_file_acl) { 2878 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl); 2879 if (!bh) { 2880 EXT4_ERROR_INODE(inode, "block %llu read error", 2881 EXT4_I(inode)->i_file_acl); 2882 error = -EIO; 2883 goto cleanup; 2884 } 2885 error = ext4_xattr_check_block(inode, bh); 2886 if (error) { 2887 EXT4_ERROR_INODE(inode, "bad block %llu (error %d)", 2888 EXT4_I(inode)->i_file_acl, error); 2889 goto cleanup; 2890 } 2891 2892 if (ext4_has_feature_ea_inode(inode->i_sb)) { 2893 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry); 2894 entry = EXT4_XATTR_NEXT(entry)) { 2895 if (!entry->e_value_inum) 2896 continue; 2897 error = ext4_xattr_inode_iget(inode, 2898 le32_to_cpu(entry->e_value_inum), 2899 le32_to_cpu(entry->e_hash), 2900 &ea_inode); 2901 if (error) 2902 continue; 2903 ext4_xattr_inode_free_quota(inode, ea_inode, 2904 le32_to_cpu(entry->e_value_size)); 2905 iput(ea_inode); 2906 } 2907 2908 } 2909 2910 ext4_xattr_release_block(handle, inode, bh, ea_inode_array, 2911 extra_credits); 2912 /* 2913 * Update i_file_acl value in the same transaction that releases 2914 * block. 2915 */ 2916 EXT4_I(inode)->i_file_acl = 0; 2917 error = ext4_mark_inode_dirty(handle, inode); 2918 if (error) { 2919 EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)", 2920 error); 2921 goto cleanup; 2922 } 2923 } 2924 error = 0; 2925 cleanup: 2926 brelse(iloc.bh); 2927 brelse(bh); 2928 return error; 2929 } 2930 2931 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array) 2932 { 2933 int idx; 2934 2935 if (ea_inode_array == NULL) 2936 return; 2937 2938 for (idx = 0; idx < ea_inode_array->count; ++idx) 2939 iput(ea_inode_array->inodes[idx]); 2940 kfree(ea_inode_array); 2941 } 2942 2943 /* 2944 * ext4_xattr_block_cache_insert() 2945 * 2946 * Create a new entry in the extended attribute block cache, and insert 2947 * it unless such an entry is already in the cache. 2948 * 2949 * Returns 0, or a negative error number on failure. 2950 */ 2951 static void 2952 ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache, 2953 struct buffer_head *bh) 2954 { 2955 struct ext4_xattr_header *header = BHDR(bh); 2956 __u32 hash = le32_to_cpu(header->h_hash); 2957 int reusable = le32_to_cpu(header->h_refcount) < 2958 EXT4_XATTR_REFCOUNT_MAX; 2959 int error; 2960 2961 if (!ea_block_cache) 2962 return; 2963 error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash, 2964 bh->b_blocknr, reusable); 2965 if (error) { 2966 if (error == -EBUSY) 2967 ea_bdebug(bh, "already in cache"); 2968 } else 2969 ea_bdebug(bh, "inserting [%x]", (int)hash); 2970 } 2971 2972 /* 2973 * ext4_xattr_cmp() 2974 * 2975 * Compare two extended attribute blocks for equality. 2976 * 2977 * Returns 0 if the blocks are equal, 1 if they differ, and 2978 * a negative error number on errors. 2979 */ 2980 static int 2981 ext4_xattr_cmp(struct ext4_xattr_header *header1, 2982 struct ext4_xattr_header *header2) 2983 { 2984 struct ext4_xattr_entry *entry1, *entry2; 2985 2986 entry1 = ENTRY(header1+1); 2987 entry2 = ENTRY(header2+1); 2988 while (!IS_LAST_ENTRY(entry1)) { 2989 if (IS_LAST_ENTRY(entry2)) 2990 return 1; 2991 if (entry1->e_hash != entry2->e_hash || 2992 entry1->e_name_index != entry2->e_name_index || 2993 entry1->e_name_len != entry2->e_name_len || 2994 entry1->e_value_size != entry2->e_value_size || 2995 entry1->e_value_inum != entry2->e_value_inum || 2996 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len)) 2997 return 1; 2998 if (!entry1->e_value_inum && 2999 memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs), 3000 (char *)header2 + le16_to_cpu(entry2->e_value_offs), 3001 le32_to_cpu(entry1->e_value_size))) 3002 return 1; 3003 3004 entry1 = EXT4_XATTR_NEXT(entry1); 3005 entry2 = EXT4_XATTR_NEXT(entry2); 3006 } 3007 if (!IS_LAST_ENTRY(entry2)) 3008 return 1; 3009 return 0; 3010 } 3011 3012 /* 3013 * ext4_xattr_block_cache_find() 3014 * 3015 * Find an identical extended attribute block. 3016 * 3017 * Returns a pointer to the block found, or NULL if such a block was 3018 * not found or an error occurred. 3019 */ 3020 static struct buffer_head * 3021 ext4_xattr_block_cache_find(struct inode *inode, 3022 struct ext4_xattr_header *header, 3023 struct mb_cache_entry **pce) 3024 { 3025 __u32 hash = le32_to_cpu(header->h_hash); 3026 struct mb_cache_entry *ce; 3027 struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode); 3028 3029 if (!ea_block_cache) 3030 return NULL; 3031 if (!header->h_hash) 3032 return NULL; /* never share */ 3033 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash); 3034 ce = mb_cache_entry_find_first(ea_block_cache, hash); 3035 while (ce) { 3036 struct buffer_head *bh; 3037 3038 bh = sb_bread(inode->i_sb, ce->e_value); 3039 if (!bh) { 3040 EXT4_ERROR_INODE(inode, "block %lu read error", 3041 (unsigned long)ce->e_value); 3042 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) { 3043 *pce = ce; 3044 return bh; 3045 } 3046 brelse(bh); 3047 ce = mb_cache_entry_find_next(ea_block_cache, ce); 3048 } 3049 return NULL; 3050 } 3051 3052 #define NAME_HASH_SHIFT 5 3053 #define VALUE_HASH_SHIFT 16 3054 3055 /* 3056 * ext4_xattr_hash_entry() 3057 * 3058 * Compute the hash of an extended attribute. 3059 */ 3060 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value, 3061 size_t value_count) 3062 { 3063 __u32 hash = 0; 3064 3065 while (name_len--) { 3066 hash = (hash << NAME_HASH_SHIFT) ^ 3067 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^ 3068 *name++; 3069 } 3070 while (value_count--) { 3071 hash = (hash << VALUE_HASH_SHIFT) ^ 3072 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^ 3073 le32_to_cpu(*value++); 3074 } 3075 return cpu_to_le32(hash); 3076 } 3077 3078 #undef NAME_HASH_SHIFT 3079 #undef VALUE_HASH_SHIFT 3080 3081 #define BLOCK_HASH_SHIFT 16 3082 3083 /* 3084 * ext4_xattr_rehash() 3085 * 3086 * Re-compute the extended attribute hash value after an entry has changed. 3087 */ 3088 static void ext4_xattr_rehash(struct ext4_xattr_header *header) 3089 { 3090 struct ext4_xattr_entry *here; 3091 __u32 hash = 0; 3092 3093 here = ENTRY(header+1); 3094 while (!IS_LAST_ENTRY(here)) { 3095 if (!here->e_hash) { 3096 /* Block is not shared if an entry's hash value == 0 */ 3097 hash = 0; 3098 break; 3099 } 3100 hash = (hash << BLOCK_HASH_SHIFT) ^ 3101 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^ 3102 le32_to_cpu(here->e_hash); 3103 here = EXT4_XATTR_NEXT(here); 3104 } 3105 header->h_hash = cpu_to_le32(hash); 3106 } 3107 3108 #undef BLOCK_HASH_SHIFT 3109 3110 #define HASH_BUCKET_BITS 10 3111 3112 struct mb_cache * 3113 ext4_xattr_create_cache(void) 3114 { 3115 return mb_cache_create(HASH_BUCKET_BITS); 3116 } 3117 3118 void ext4_xattr_destroy_cache(struct mb_cache *cache) 3119 { 3120 if (cache) 3121 mb_cache_destroy(cache); 3122 } 3123 3124