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