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