1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/ext4/namei.c 4 * 5 * Copyright (C) 1992, 1993, 1994, 1995 6 * Remy Card (card@masi.ibp.fr) 7 * Laboratoire MASI - Institut Blaise Pascal 8 * Universite Pierre et Marie Curie (Paris VI) 9 * 10 * from 11 * 12 * linux/fs/minix/namei.c 13 * 14 * Copyright (C) 1991, 1992 Linus Torvalds 15 * 16 * Big-endian to little-endian byte-swapping/bitmaps by 17 * David S. Miller (davem@caip.rutgers.edu), 1995 18 * Directory entry file type support and forward compatibility hooks 19 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998 20 * Hash Tree Directory indexing (c) 21 * Daniel Phillips, 2001 22 * Hash Tree Directory indexing porting 23 * Christopher Li, 2002 24 * Hash Tree Directory indexing cleanup 25 * Theodore Ts'o, 2002 26 */ 27 28 #include <linux/fs.h> 29 #include <linux/pagemap.h> 30 #include <linux/time.h> 31 #include <linux/fcntl.h> 32 #include <linux/stat.h> 33 #include <linux/string.h> 34 #include <linux/quotaops.h> 35 #include <linux/buffer_head.h> 36 #include <linux/bio.h> 37 #include <linux/iversion.h> 38 #include <linux/unicode.h> 39 #include "ext4.h" 40 #include "ext4_jbd2.h" 41 42 #include "xattr.h" 43 #include "acl.h" 44 45 #include <trace/events/ext4.h> 46 /* 47 * define how far ahead to read directories while searching them. 48 */ 49 #define NAMEI_RA_CHUNKS 2 50 #define NAMEI_RA_BLOCKS 4 51 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) 52 53 static struct buffer_head *ext4_append(handle_t *handle, 54 struct inode *inode, 55 ext4_lblk_t *block) 56 { 57 struct buffer_head *bh; 58 int err; 59 60 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb && 61 ((inode->i_size >> 10) >= 62 EXT4_SB(inode->i_sb)->s_max_dir_size_kb))) 63 return ERR_PTR(-ENOSPC); 64 65 *block = inode->i_size >> inode->i_sb->s_blocksize_bits; 66 67 bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE); 68 if (IS_ERR(bh)) 69 return bh; 70 inode->i_size += inode->i_sb->s_blocksize; 71 EXT4_I(inode)->i_disksize = inode->i_size; 72 BUFFER_TRACE(bh, "get_write_access"); 73 err = ext4_journal_get_write_access(handle, bh); 74 if (err) { 75 brelse(bh); 76 ext4_std_error(inode->i_sb, err); 77 return ERR_PTR(err); 78 } 79 return bh; 80 } 81 82 static int ext4_dx_csum_verify(struct inode *inode, 83 struct ext4_dir_entry *dirent); 84 85 typedef enum { 86 EITHER, INDEX, DIRENT 87 } dirblock_type_t; 88 89 #define ext4_read_dirblock(inode, block, type) \ 90 __ext4_read_dirblock((inode), (block), (type), __func__, __LINE__) 91 92 static struct buffer_head *__ext4_read_dirblock(struct inode *inode, 93 ext4_lblk_t block, 94 dirblock_type_t type, 95 const char *func, 96 unsigned int line) 97 { 98 struct buffer_head *bh; 99 struct ext4_dir_entry *dirent; 100 int is_dx_block = 0; 101 102 bh = ext4_bread(NULL, inode, block, 0); 103 if (IS_ERR(bh)) { 104 __ext4_warning(inode->i_sb, func, line, 105 "inode #%lu: lblock %lu: comm %s: " 106 "error %ld reading directory block", 107 inode->i_ino, (unsigned long)block, 108 current->comm, PTR_ERR(bh)); 109 110 return bh; 111 } 112 if (!bh) { 113 ext4_error_inode(inode, func, line, block, 114 "Directory hole found"); 115 return ERR_PTR(-EFSCORRUPTED); 116 } 117 dirent = (struct ext4_dir_entry *) bh->b_data; 118 /* Determine whether or not we have an index block */ 119 if (is_dx(inode)) { 120 if (block == 0) 121 is_dx_block = 1; 122 else if (ext4_rec_len_from_disk(dirent->rec_len, 123 inode->i_sb->s_blocksize) == 124 inode->i_sb->s_blocksize) 125 is_dx_block = 1; 126 } 127 if (!is_dx_block && type == INDEX) { 128 ext4_error_inode(inode, func, line, block, 129 "directory leaf block found instead of index block"); 130 brelse(bh); 131 return ERR_PTR(-EFSCORRUPTED); 132 } 133 if (!ext4_has_metadata_csum(inode->i_sb) || 134 buffer_verified(bh)) 135 return bh; 136 137 /* 138 * An empty leaf block can get mistaken for a index block; for 139 * this reason, we can only check the index checksum when the 140 * caller is sure it should be an index block. 141 */ 142 if (is_dx_block && type == INDEX) { 143 if (ext4_dx_csum_verify(inode, dirent)) 144 set_buffer_verified(bh); 145 else { 146 ext4_error_inode(inode, func, line, block, 147 "Directory index failed checksum"); 148 brelse(bh); 149 return ERR_PTR(-EFSBADCRC); 150 } 151 } 152 if (!is_dx_block) { 153 if (ext4_dirent_csum_verify(inode, dirent)) 154 set_buffer_verified(bh); 155 else { 156 ext4_error_inode(inode, func, line, block, 157 "Directory block failed checksum"); 158 brelse(bh); 159 return ERR_PTR(-EFSBADCRC); 160 } 161 } 162 return bh; 163 } 164 165 #ifndef assert 166 #define assert(test) J_ASSERT(test) 167 #endif 168 169 #ifdef DX_DEBUG 170 #define dxtrace(command) command 171 #else 172 #define dxtrace(command) 173 #endif 174 175 struct fake_dirent 176 { 177 __le32 inode; 178 __le16 rec_len; 179 u8 name_len; 180 u8 file_type; 181 }; 182 183 struct dx_countlimit 184 { 185 __le16 limit; 186 __le16 count; 187 }; 188 189 struct dx_entry 190 { 191 __le32 hash; 192 __le32 block; 193 }; 194 195 /* 196 * dx_root_info is laid out so that if it should somehow get overlaid by a 197 * dirent the two low bits of the hash version will be zero. Therefore, the 198 * hash version mod 4 should never be 0. Sincerely, the paranoia department. 199 */ 200 201 struct dx_root 202 { 203 struct fake_dirent dot; 204 char dot_name[4]; 205 struct fake_dirent dotdot; 206 char dotdot_name[4]; 207 struct dx_root_info 208 { 209 __le32 reserved_zero; 210 u8 hash_version; 211 u8 info_length; /* 8 */ 212 u8 indirect_levels; 213 u8 unused_flags; 214 } 215 info; 216 struct dx_entry entries[0]; 217 }; 218 219 struct dx_node 220 { 221 struct fake_dirent fake; 222 struct dx_entry entries[0]; 223 }; 224 225 226 struct dx_frame 227 { 228 struct buffer_head *bh; 229 struct dx_entry *entries; 230 struct dx_entry *at; 231 }; 232 233 struct dx_map_entry 234 { 235 u32 hash; 236 u16 offs; 237 u16 size; 238 }; 239 240 /* 241 * This goes at the end of each htree block. 242 */ 243 struct dx_tail { 244 u32 dt_reserved; 245 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */ 246 }; 247 248 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry); 249 static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value); 250 static inline unsigned dx_get_hash(struct dx_entry *entry); 251 static void dx_set_hash(struct dx_entry *entry, unsigned value); 252 static unsigned dx_get_count(struct dx_entry *entries); 253 static unsigned dx_get_limit(struct dx_entry *entries); 254 static void dx_set_count(struct dx_entry *entries, unsigned value); 255 static void dx_set_limit(struct dx_entry *entries, unsigned value); 256 static unsigned dx_root_limit(struct inode *dir, unsigned infosize); 257 static unsigned dx_node_limit(struct inode *dir); 258 static struct dx_frame *dx_probe(struct ext4_filename *fname, 259 struct inode *dir, 260 struct dx_hash_info *hinfo, 261 struct dx_frame *frame); 262 static void dx_release(struct dx_frame *frames); 263 static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de, 264 unsigned blocksize, struct dx_hash_info *hinfo, 265 struct dx_map_entry map[]); 266 static void dx_sort_map(struct dx_map_entry *map, unsigned count); 267 static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to, 268 struct dx_map_entry *offsets, int count, unsigned blocksize); 269 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize); 270 static void dx_insert_block(struct dx_frame *frame, 271 u32 hash, ext4_lblk_t block); 272 static int ext4_htree_next_block(struct inode *dir, __u32 hash, 273 struct dx_frame *frame, 274 struct dx_frame *frames, 275 __u32 *start_hash); 276 static struct buffer_head * ext4_dx_find_entry(struct inode *dir, 277 struct ext4_filename *fname, 278 struct ext4_dir_entry_2 **res_dir); 279 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname, 280 struct inode *dir, struct inode *inode); 281 282 /* checksumming functions */ 283 void initialize_dirent_tail(struct ext4_dir_entry_tail *t, 284 unsigned int blocksize) 285 { 286 memset(t, 0, sizeof(struct ext4_dir_entry_tail)); 287 t->det_rec_len = ext4_rec_len_to_disk( 288 sizeof(struct ext4_dir_entry_tail), blocksize); 289 t->det_reserved_ft = EXT4_FT_DIR_CSUM; 290 } 291 292 /* Walk through a dirent block to find a checksum "dirent" at the tail */ 293 static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode, 294 struct ext4_dir_entry *de) 295 { 296 struct ext4_dir_entry_tail *t; 297 298 #ifdef PARANOID 299 struct ext4_dir_entry *d, *top; 300 301 d = de; 302 top = (struct ext4_dir_entry *)(((void *)de) + 303 (EXT4_BLOCK_SIZE(inode->i_sb) - 304 sizeof(struct ext4_dir_entry_tail))); 305 while (d < top && d->rec_len) 306 d = (struct ext4_dir_entry *)(((void *)d) + 307 le16_to_cpu(d->rec_len)); 308 309 if (d != top) 310 return NULL; 311 312 t = (struct ext4_dir_entry_tail *)d; 313 #else 314 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb)); 315 #endif 316 317 if (t->det_reserved_zero1 || 318 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) || 319 t->det_reserved_zero2 || 320 t->det_reserved_ft != EXT4_FT_DIR_CSUM) 321 return NULL; 322 323 return t; 324 } 325 326 static __le32 ext4_dirent_csum(struct inode *inode, 327 struct ext4_dir_entry *dirent, int size) 328 { 329 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 330 struct ext4_inode_info *ei = EXT4_I(inode); 331 __u32 csum; 332 333 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size); 334 return cpu_to_le32(csum); 335 } 336 337 #define warn_no_space_for_csum(inode) \ 338 __warn_no_space_for_csum((inode), __func__, __LINE__) 339 340 static void __warn_no_space_for_csum(struct inode *inode, const char *func, 341 unsigned int line) 342 { 343 __ext4_warning_inode(inode, func, line, 344 "No space for directory leaf checksum. Please run e2fsck -D."); 345 } 346 347 int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent) 348 { 349 struct ext4_dir_entry_tail *t; 350 351 if (!ext4_has_metadata_csum(inode->i_sb)) 352 return 1; 353 354 t = get_dirent_tail(inode, dirent); 355 if (!t) { 356 warn_no_space_for_csum(inode); 357 return 0; 358 } 359 360 if (t->det_checksum != ext4_dirent_csum(inode, dirent, 361 (void *)t - (void *)dirent)) 362 return 0; 363 364 return 1; 365 } 366 367 static void ext4_dirent_csum_set(struct inode *inode, 368 struct ext4_dir_entry *dirent) 369 { 370 struct ext4_dir_entry_tail *t; 371 372 if (!ext4_has_metadata_csum(inode->i_sb)) 373 return; 374 375 t = get_dirent_tail(inode, dirent); 376 if (!t) { 377 warn_no_space_for_csum(inode); 378 return; 379 } 380 381 t->det_checksum = ext4_dirent_csum(inode, dirent, 382 (void *)t - (void *)dirent); 383 } 384 385 int ext4_handle_dirty_dirent_node(handle_t *handle, 386 struct inode *inode, 387 struct buffer_head *bh) 388 { 389 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data); 390 return ext4_handle_dirty_metadata(handle, inode, bh); 391 } 392 393 static struct dx_countlimit *get_dx_countlimit(struct inode *inode, 394 struct ext4_dir_entry *dirent, 395 int *offset) 396 { 397 struct ext4_dir_entry *dp; 398 struct dx_root_info *root; 399 int count_offset; 400 401 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb)) 402 count_offset = 8; 403 else if (le16_to_cpu(dirent->rec_len) == 12) { 404 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12); 405 if (le16_to_cpu(dp->rec_len) != 406 EXT4_BLOCK_SIZE(inode->i_sb) - 12) 407 return NULL; 408 root = (struct dx_root_info *)(((void *)dp + 12)); 409 if (root->reserved_zero || 410 root->info_length != sizeof(struct dx_root_info)) 411 return NULL; 412 count_offset = 32; 413 } else 414 return NULL; 415 416 if (offset) 417 *offset = count_offset; 418 return (struct dx_countlimit *)(((void *)dirent) + count_offset); 419 } 420 421 static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent, 422 int count_offset, int count, struct dx_tail *t) 423 { 424 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 425 struct ext4_inode_info *ei = EXT4_I(inode); 426 __u32 csum; 427 int size; 428 __u32 dummy_csum = 0; 429 int offset = offsetof(struct dx_tail, dt_checksum); 430 431 size = count_offset + (count * sizeof(struct dx_entry)); 432 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size); 433 csum = ext4_chksum(sbi, csum, (__u8 *)t, offset); 434 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum)); 435 436 return cpu_to_le32(csum); 437 } 438 439 static int ext4_dx_csum_verify(struct inode *inode, 440 struct ext4_dir_entry *dirent) 441 { 442 struct dx_countlimit *c; 443 struct dx_tail *t; 444 int count_offset, limit, count; 445 446 if (!ext4_has_metadata_csum(inode->i_sb)) 447 return 1; 448 449 c = get_dx_countlimit(inode, dirent, &count_offset); 450 if (!c) { 451 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D."); 452 return 0; 453 } 454 limit = le16_to_cpu(c->limit); 455 count = le16_to_cpu(c->count); 456 if (count_offset + (limit * sizeof(struct dx_entry)) > 457 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) { 458 warn_no_space_for_csum(inode); 459 return 0; 460 } 461 t = (struct dx_tail *)(((struct dx_entry *)c) + limit); 462 463 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset, 464 count, t)) 465 return 0; 466 return 1; 467 } 468 469 static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent) 470 { 471 struct dx_countlimit *c; 472 struct dx_tail *t; 473 int count_offset, limit, count; 474 475 if (!ext4_has_metadata_csum(inode->i_sb)) 476 return; 477 478 c = get_dx_countlimit(inode, dirent, &count_offset); 479 if (!c) { 480 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D."); 481 return; 482 } 483 limit = le16_to_cpu(c->limit); 484 count = le16_to_cpu(c->count); 485 if (count_offset + (limit * sizeof(struct dx_entry)) > 486 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) { 487 warn_no_space_for_csum(inode); 488 return; 489 } 490 t = (struct dx_tail *)(((struct dx_entry *)c) + limit); 491 492 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t); 493 } 494 495 static inline int ext4_handle_dirty_dx_node(handle_t *handle, 496 struct inode *inode, 497 struct buffer_head *bh) 498 { 499 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data); 500 return ext4_handle_dirty_metadata(handle, inode, bh); 501 } 502 503 /* 504 * p is at least 6 bytes before the end of page 505 */ 506 static inline struct ext4_dir_entry_2 * 507 ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize) 508 { 509 return (struct ext4_dir_entry_2 *)((char *)p + 510 ext4_rec_len_from_disk(p->rec_len, blocksize)); 511 } 512 513 /* 514 * Future: use high four bits of block for coalesce-on-delete flags 515 * Mask them off for now. 516 */ 517 518 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry) 519 { 520 return le32_to_cpu(entry->block) & 0x0fffffff; 521 } 522 523 static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value) 524 { 525 entry->block = cpu_to_le32(value); 526 } 527 528 static inline unsigned dx_get_hash(struct dx_entry *entry) 529 { 530 return le32_to_cpu(entry->hash); 531 } 532 533 static inline void dx_set_hash(struct dx_entry *entry, unsigned value) 534 { 535 entry->hash = cpu_to_le32(value); 536 } 537 538 static inline unsigned dx_get_count(struct dx_entry *entries) 539 { 540 return le16_to_cpu(((struct dx_countlimit *) entries)->count); 541 } 542 543 static inline unsigned dx_get_limit(struct dx_entry *entries) 544 { 545 return le16_to_cpu(((struct dx_countlimit *) entries)->limit); 546 } 547 548 static inline void dx_set_count(struct dx_entry *entries, unsigned value) 549 { 550 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value); 551 } 552 553 static inline void dx_set_limit(struct dx_entry *entries, unsigned value) 554 { 555 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value); 556 } 557 558 static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize) 559 { 560 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) - 561 EXT4_DIR_REC_LEN(2) - infosize; 562 563 if (ext4_has_metadata_csum(dir->i_sb)) 564 entry_space -= sizeof(struct dx_tail); 565 return entry_space / sizeof(struct dx_entry); 566 } 567 568 static inline unsigned dx_node_limit(struct inode *dir) 569 { 570 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0); 571 572 if (ext4_has_metadata_csum(dir->i_sb)) 573 entry_space -= sizeof(struct dx_tail); 574 return entry_space / sizeof(struct dx_entry); 575 } 576 577 /* 578 * Debug 579 */ 580 #ifdef DX_DEBUG 581 static void dx_show_index(char * label, struct dx_entry *entries) 582 { 583 int i, n = dx_get_count (entries); 584 printk(KERN_DEBUG "%s index", label); 585 for (i = 0; i < n; i++) { 586 printk(KERN_CONT " %x->%lu", 587 i ? dx_get_hash(entries + i) : 0, 588 (unsigned long)dx_get_block(entries + i)); 589 } 590 printk(KERN_CONT "\n"); 591 } 592 593 struct stats 594 { 595 unsigned names; 596 unsigned space; 597 unsigned bcount; 598 }; 599 600 static struct stats dx_show_leaf(struct inode *dir, 601 struct dx_hash_info *hinfo, 602 struct ext4_dir_entry_2 *de, 603 int size, int show_names) 604 { 605 unsigned names = 0, space = 0; 606 char *base = (char *) de; 607 struct dx_hash_info h = *hinfo; 608 609 printk("names: "); 610 while ((char *) de < base + size) 611 { 612 if (de->inode) 613 { 614 if (show_names) 615 { 616 #ifdef CONFIG_FS_ENCRYPTION 617 int len; 618 char *name; 619 struct fscrypt_str fname_crypto_str = 620 FSTR_INIT(NULL, 0); 621 int res = 0; 622 623 name = de->name; 624 len = de->name_len; 625 if (IS_ENCRYPTED(dir)) 626 res = fscrypt_get_encryption_info(dir); 627 if (res) { 628 printk(KERN_WARNING "Error setting up" 629 " fname crypto: %d\n", res); 630 } 631 if (!fscrypt_has_encryption_key(dir)) { 632 /* Directory is not encrypted */ 633 ext4fs_dirhash(dir, de->name, 634 de->name_len, &h); 635 printk("%*.s:(U)%x.%u ", len, 636 name, h.hash, 637 (unsigned) ((char *) de 638 - base)); 639 } else { 640 struct fscrypt_str de_name = 641 FSTR_INIT(name, len); 642 643 /* Directory is encrypted */ 644 res = fscrypt_fname_alloc_buffer( 645 dir, len, 646 &fname_crypto_str); 647 if (res) 648 printk(KERN_WARNING "Error " 649 "allocating crypto " 650 "buffer--skipping " 651 "crypto\n"); 652 res = fscrypt_fname_disk_to_usr(dir, 653 0, 0, &de_name, 654 &fname_crypto_str); 655 if (res) { 656 printk(KERN_WARNING "Error " 657 "converting filename " 658 "from disk to usr" 659 "\n"); 660 name = "??"; 661 len = 2; 662 } else { 663 name = fname_crypto_str.name; 664 len = fname_crypto_str.len; 665 } 666 ext4fs_dirhash(dir, de->name, 667 de->name_len, &h); 668 printk("%*.s:(E)%x.%u ", len, name, 669 h.hash, (unsigned) ((char *) de 670 - base)); 671 fscrypt_fname_free_buffer( 672 &fname_crypto_str); 673 } 674 #else 675 int len = de->name_len; 676 char *name = de->name; 677 ext4fs_dirhash(dir, de->name, de->name_len, &h); 678 printk("%*.s:%x.%u ", len, name, h.hash, 679 (unsigned) ((char *) de - base)); 680 #endif 681 } 682 space += EXT4_DIR_REC_LEN(de->name_len); 683 names++; 684 } 685 de = ext4_next_entry(de, size); 686 } 687 printk(KERN_CONT "(%i)\n", names); 688 return (struct stats) { names, space, 1 }; 689 } 690 691 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir, 692 struct dx_entry *entries, int levels) 693 { 694 unsigned blocksize = dir->i_sb->s_blocksize; 695 unsigned count = dx_get_count(entries), names = 0, space = 0, i; 696 unsigned bcount = 0; 697 struct buffer_head *bh; 698 printk("%i indexed blocks...\n", count); 699 for (i = 0; i < count; i++, entries++) 700 { 701 ext4_lblk_t block = dx_get_block(entries); 702 ext4_lblk_t hash = i ? dx_get_hash(entries): 0; 703 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash; 704 struct stats stats; 705 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range); 706 bh = ext4_bread(NULL,dir, block, 0); 707 if (!bh || IS_ERR(bh)) 708 continue; 709 stats = levels? 710 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1): 711 dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) 712 bh->b_data, blocksize, 0); 713 names += stats.names; 714 space += stats.space; 715 bcount += stats.bcount; 716 brelse(bh); 717 } 718 if (bcount) 719 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n", 720 levels ? "" : " ", names, space/bcount, 721 (space/bcount)*100/blocksize); 722 return (struct stats) { names, space, bcount}; 723 } 724 #endif /* DX_DEBUG */ 725 726 /* 727 * Probe for a directory leaf block to search. 728 * 729 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format 730 * error in the directory index, and the caller should fall back to 731 * searching the directory normally. The callers of dx_probe **MUST** 732 * check for this error code, and make sure it never gets reflected 733 * back to userspace. 734 */ 735 static struct dx_frame * 736 dx_probe(struct ext4_filename *fname, struct inode *dir, 737 struct dx_hash_info *hinfo, struct dx_frame *frame_in) 738 { 739 unsigned count, indirect; 740 struct dx_entry *at, *entries, *p, *q, *m; 741 struct dx_root *root; 742 struct dx_frame *frame = frame_in; 743 struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR); 744 u32 hash; 745 746 memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0])); 747 frame->bh = ext4_read_dirblock(dir, 0, INDEX); 748 if (IS_ERR(frame->bh)) 749 return (struct dx_frame *) frame->bh; 750 751 root = (struct dx_root *) frame->bh->b_data; 752 if (root->info.hash_version != DX_HASH_TEA && 753 root->info.hash_version != DX_HASH_HALF_MD4 && 754 root->info.hash_version != DX_HASH_LEGACY) { 755 ext4_warning_inode(dir, "Unrecognised inode hash code %u", 756 root->info.hash_version); 757 goto fail; 758 } 759 if (fname) 760 hinfo = &fname->hinfo; 761 hinfo->hash_version = root->info.hash_version; 762 if (hinfo->hash_version <= DX_HASH_TEA) 763 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned; 764 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed; 765 if (fname && fname_name(fname)) 766 ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), hinfo); 767 hash = hinfo->hash; 768 769 if (root->info.unused_flags & 1) { 770 ext4_warning_inode(dir, "Unimplemented hash flags: %#06x", 771 root->info.unused_flags); 772 goto fail; 773 } 774 775 indirect = root->info.indirect_levels; 776 if (indirect >= ext4_dir_htree_level(dir->i_sb)) { 777 ext4_warning(dir->i_sb, 778 "Directory (ino: %lu) htree depth %#06x exceed" 779 "supported value", dir->i_ino, 780 ext4_dir_htree_level(dir->i_sb)); 781 if (ext4_dir_htree_level(dir->i_sb) < EXT4_HTREE_LEVEL) { 782 ext4_warning(dir->i_sb, "Enable large directory " 783 "feature to access it"); 784 } 785 goto fail; 786 } 787 788 entries = (struct dx_entry *)(((char *)&root->info) + 789 root->info.info_length); 790 791 if (dx_get_limit(entries) != dx_root_limit(dir, 792 root->info.info_length)) { 793 ext4_warning_inode(dir, "dx entry: limit %u != root limit %u", 794 dx_get_limit(entries), 795 dx_root_limit(dir, root->info.info_length)); 796 goto fail; 797 } 798 799 dxtrace(printk("Look up %x", hash)); 800 while (1) { 801 count = dx_get_count(entries); 802 if (!count || count > dx_get_limit(entries)) { 803 ext4_warning_inode(dir, 804 "dx entry: count %u beyond limit %u", 805 count, dx_get_limit(entries)); 806 goto fail; 807 } 808 809 p = entries + 1; 810 q = entries + count - 1; 811 while (p <= q) { 812 m = p + (q - p) / 2; 813 dxtrace(printk(KERN_CONT ".")); 814 if (dx_get_hash(m) > hash) 815 q = m - 1; 816 else 817 p = m + 1; 818 } 819 820 if (0) { // linear search cross check 821 unsigned n = count - 1; 822 at = entries; 823 while (n--) 824 { 825 dxtrace(printk(KERN_CONT ",")); 826 if (dx_get_hash(++at) > hash) 827 { 828 at--; 829 break; 830 } 831 } 832 assert (at == p - 1); 833 } 834 835 at = p - 1; 836 dxtrace(printk(KERN_CONT " %x->%u\n", 837 at == entries ? 0 : dx_get_hash(at), 838 dx_get_block(at))); 839 frame->entries = entries; 840 frame->at = at; 841 if (!indirect--) 842 return frame; 843 frame++; 844 frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX); 845 if (IS_ERR(frame->bh)) { 846 ret_err = (struct dx_frame *) frame->bh; 847 frame->bh = NULL; 848 goto fail; 849 } 850 entries = ((struct dx_node *) frame->bh->b_data)->entries; 851 852 if (dx_get_limit(entries) != dx_node_limit(dir)) { 853 ext4_warning_inode(dir, 854 "dx entry: limit %u != node limit %u", 855 dx_get_limit(entries), dx_node_limit(dir)); 856 goto fail; 857 } 858 } 859 fail: 860 while (frame >= frame_in) { 861 brelse(frame->bh); 862 frame--; 863 } 864 865 if (ret_err == ERR_PTR(ERR_BAD_DX_DIR)) 866 ext4_warning_inode(dir, 867 "Corrupt directory, running e2fsck is recommended"); 868 return ret_err; 869 } 870 871 static void dx_release(struct dx_frame *frames) 872 { 873 struct dx_root_info *info; 874 int i; 875 unsigned int indirect_levels; 876 877 if (frames[0].bh == NULL) 878 return; 879 880 info = &((struct dx_root *)frames[0].bh->b_data)->info; 881 /* save local copy, "info" may be freed after brelse() */ 882 indirect_levels = info->indirect_levels; 883 for (i = 0; i <= indirect_levels; i++) { 884 if (frames[i].bh == NULL) 885 break; 886 brelse(frames[i].bh); 887 frames[i].bh = NULL; 888 } 889 } 890 891 /* 892 * This function increments the frame pointer to search the next leaf 893 * block, and reads in the necessary intervening nodes if the search 894 * should be necessary. Whether or not the search is necessary is 895 * controlled by the hash parameter. If the hash value is even, then 896 * the search is only continued if the next block starts with that 897 * hash value. This is used if we are searching for a specific file. 898 * 899 * If the hash value is HASH_NB_ALWAYS, then always go to the next block. 900 * 901 * This function returns 1 if the caller should continue to search, 902 * or 0 if it should not. If there is an error reading one of the 903 * index blocks, it will a negative error code. 904 * 905 * If start_hash is non-null, it will be filled in with the starting 906 * hash of the next page. 907 */ 908 static int ext4_htree_next_block(struct inode *dir, __u32 hash, 909 struct dx_frame *frame, 910 struct dx_frame *frames, 911 __u32 *start_hash) 912 { 913 struct dx_frame *p; 914 struct buffer_head *bh; 915 int num_frames = 0; 916 __u32 bhash; 917 918 p = frame; 919 /* 920 * Find the next leaf page by incrementing the frame pointer. 921 * If we run out of entries in the interior node, loop around and 922 * increment pointer in the parent node. When we break out of 923 * this loop, num_frames indicates the number of interior 924 * nodes need to be read. 925 */ 926 while (1) { 927 if (++(p->at) < p->entries + dx_get_count(p->entries)) 928 break; 929 if (p == frames) 930 return 0; 931 num_frames++; 932 p--; 933 } 934 935 /* 936 * If the hash is 1, then continue only if the next page has a 937 * continuation hash of any value. This is used for readdir 938 * handling. Otherwise, check to see if the hash matches the 939 * desired contiuation hash. If it doesn't, return since 940 * there's no point to read in the successive index pages. 941 */ 942 bhash = dx_get_hash(p->at); 943 if (start_hash) 944 *start_hash = bhash; 945 if ((hash & 1) == 0) { 946 if ((bhash & ~1) != hash) 947 return 0; 948 } 949 /* 950 * If the hash is HASH_NB_ALWAYS, we always go to the next 951 * block so no check is necessary 952 */ 953 while (num_frames--) { 954 bh = ext4_read_dirblock(dir, dx_get_block(p->at), INDEX); 955 if (IS_ERR(bh)) 956 return PTR_ERR(bh); 957 p++; 958 brelse(p->bh); 959 p->bh = bh; 960 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries; 961 } 962 return 1; 963 } 964 965 966 /* 967 * This function fills a red-black tree with information from a 968 * directory block. It returns the number directory entries loaded 969 * into the tree. If there is an error it is returned in err. 970 */ 971 static int htree_dirblock_to_tree(struct file *dir_file, 972 struct inode *dir, ext4_lblk_t block, 973 struct dx_hash_info *hinfo, 974 __u32 start_hash, __u32 start_minor_hash) 975 { 976 struct buffer_head *bh; 977 struct ext4_dir_entry_2 *de, *top; 978 int err = 0, count = 0; 979 struct fscrypt_str fname_crypto_str = FSTR_INIT(NULL, 0), tmp_str; 980 981 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n", 982 (unsigned long)block)); 983 bh = ext4_read_dirblock(dir, block, DIRENT); 984 if (IS_ERR(bh)) 985 return PTR_ERR(bh); 986 987 de = (struct ext4_dir_entry_2 *) bh->b_data; 988 top = (struct ext4_dir_entry_2 *) ((char *) de + 989 dir->i_sb->s_blocksize - 990 EXT4_DIR_REC_LEN(0)); 991 #ifdef CONFIG_FS_ENCRYPTION 992 /* Check if the directory is encrypted */ 993 if (IS_ENCRYPTED(dir)) { 994 err = fscrypt_get_encryption_info(dir); 995 if (err < 0) { 996 brelse(bh); 997 return err; 998 } 999 err = fscrypt_fname_alloc_buffer(dir, EXT4_NAME_LEN, 1000 &fname_crypto_str); 1001 if (err < 0) { 1002 brelse(bh); 1003 return err; 1004 } 1005 } 1006 #endif 1007 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) { 1008 if (ext4_check_dir_entry(dir, NULL, de, bh, 1009 bh->b_data, bh->b_size, 1010 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb)) 1011 + ((char *)de - bh->b_data))) { 1012 /* silently ignore the rest of the block */ 1013 break; 1014 } 1015 ext4fs_dirhash(dir, de->name, de->name_len, hinfo); 1016 if ((hinfo->hash < start_hash) || 1017 ((hinfo->hash == start_hash) && 1018 (hinfo->minor_hash < start_minor_hash))) 1019 continue; 1020 if (de->inode == 0) 1021 continue; 1022 if (!IS_ENCRYPTED(dir)) { 1023 tmp_str.name = de->name; 1024 tmp_str.len = de->name_len; 1025 err = ext4_htree_store_dirent(dir_file, 1026 hinfo->hash, hinfo->minor_hash, de, 1027 &tmp_str); 1028 } else { 1029 int save_len = fname_crypto_str.len; 1030 struct fscrypt_str de_name = FSTR_INIT(de->name, 1031 de->name_len); 1032 1033 /* Directory is encrypted */ 1034 err = fscrypt_fname_disk_to_usr(dir, hinfo->hash, 1035 hinfo->minor_hash, &de_name, 1036 &fname_crypto_str); 1037 if (err) { 1038 count = err; 1039 goto errout; 1040 } 1041 err = ext4_htree_store_dirent(dir_file, 1042 hinfo->hash, hinfo->minor_hash, de, 1043 &fname_crypto_str); 1044 fname_crypto_str.len = save_len; 1045 } 1046 if (err != 0) { 1047 count = err; 1048 goto errout; 1049 } 1050 count++; 1051 } 1052 errout: 1053 brelse(bh); 1054 #ifdef CONFIG_FS_ENCRYPTION 1055 fscrypt_fname_free_buffer(&fname_crypto_str); 1056 #endif 1057 return count; 1058 } 1059 1060 1061 /* 1062 * This function fills a red-black tree with information from a 1063 * directory. We start scanning the directory in hash order, starting 1064 * at start_hash and start_minor_hash. 1065 * 1066 * This function returns the number of entries inserted into the tree, 1067 * or a negative error code. 1068 */ 1069 int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash, 1070 __u32 start_minor_hash, __u32 *next_hash) 1071 { 1072 struct dx_hash_info hinfo; 1073 struct ext4_dir_entry_2 *de; 1074 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame; 1075 struct inode *dir; 1076 ext4_lblk_t block; 1077 int count = 0; 1078 int ret, err; 1079 __u32 hashval; 1080 struct fscrypt_str tmp_str; 1081 1082 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n", 1083 start_hash, start_minor_hash)); 1084 dir = file_inode(dir_file); 1085 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) { 1086 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version; 1087 if (hinfo.hash_version <= DX_HASH_TEA) 1088 hinfo.hash_version += 1089 EXT4_SB(dir->i_sb)->s_hash_unsigned; 1090 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed; 1091 if (ext4_has_inline_data(dir)) { 1092 int has_inline_data = 1; 1093 count = htree_inlinedir_to_tree(dir_file, dir, 0, 1094 &hinfo, start_hash, 1095 start_minor_hash, 1096 &has_inline_data); 1097 if (has_inline_data) { 1098 *next_hash = ~0; 1099 return count; 1100 } 1101 } 1102 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo, 1103 start_hash, start_minor_hash); 1104 *next_hash = ~0; 1105 return count; 1106 } 1107 hinfo.hash = start_hash; 1108 hinfo.minor_hash = 0; 1109 frame = dx_probe(NULL, dir, &hinfo, frames); 1110 if (IS_ERR(frame)) 1111 return PTR_ERR(frame); 1112 1113 /* Add '.' and '..' from the htree header */ 1114 if (!start_hash && !start_minor_hash) { 1115 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; 1116 tmp_str.name = de->name; 1117 tmp_str.len = de->name_len; 1118 err = ext4_htree_store_dirent(dir_file, 0, 0, 1119 de, &tmp_str); 1120 if (err != 0) 1121 goto errout; 1122 count++; 1123 } 1124 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) { 1125 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; 1126 de = ext4_next_entry(de, dir->i_sb->s_blocksize); 1127 tmp_str.name = de->name; 1128 tmp_str.len = de->name_len; 1129 err = ext4_htree_store_dirent(dir_file, 2, 0, 1130 de, &tmp_str); 1131 if (err != 0) 1132 goto errout; 1133 count++; 1134 } 1135 1136 while (1) { 1137 if (fatal_signal_pending(current)) { 1138 err = -ERESTARTSYS; 1139 goto errout; 1140 } 1141 cond_resched(); 1142 block = dx_get_block(frame->at); 1143 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo, 1144 start_hash, start_minor_hash); 1145 if (ret < 0) { 1146 err = ret; 1147 goto errout; 1148 } 1149 count += ret; 1150 hashval = ~0; 1151 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS, 1152 frame, frames, &hashval); 1153 *next_hash = hashval; 1154 if (ret < 0) { 1155 err = ret; 1156 goto errout; 1157 } 1158 /* 1159 * Stop if: (a) there are no more entries, or 1160 * (b) we have inserted at least one entry and the 1161 * next hash value is not a continuation 1162 */ 1163 if ((ret == 0) || 1164 (count && ((hashval & 1) == 0))) 1165 break; 1166 } 1167 dx_release(frames); 1168 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, " 1169 "next hash: %x\n", count, *next_hash)); 1170 return count; 1171 errout: 1172 dx_release(frames); 1173 return (err); 1174 } 1175 1176 static inline int search_dirblock(struct buffer_head *bh, 1177 struct inode *dir, 1178 struct ext4_filename *fname, 1179 unsigned int offset, 1180 struct ext4_dir_entry_2 **res_dir) 1181 { 1182 return ext4_search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir, 1183 fname, offset, res_dir); 1184 } 1185 1186 /* 1187 * Directory block splitting, compacting 1188 */ 1189 1190 /* 1191 * Create map of hash values, offsets, and sizes, stored at end of block. 1192 * Returns number of entries mapped. 1193 */ 1194 static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de, 1195 unsigned blocksize, struct dx_hash_info *hinfo, 1196 struct dx_map_entry *map_tail) 1197 { 1198 int count = 0; 1199 char *base = (char *) de; 1200 struct dx_hash_info h = *hinfo; 1201 1202 while ((char *) de < base + blocksize) { 1203 if (de->name_len && de->inode) { 1204 ext4fs_dirhash(dir, de->name, de->name_len, &h); 1205 map_tail--; 1206 map_tail->hash = h.hash; 1207 map_tail->offs = ((char *) de - base)>>2; 1208 map_tail->size = le16_to_cpu(de->rec_len); 1209 count++; 1210 cond_resched(); 1211 } 1212 /* XXX: do we need to check rec_len == 0 case? -Chris */ 1213 de = ext4_next_entry(de, blocksize); 1214 } 1215 return count; 1216 } 1217 1218 /* Sort map by hash value */ 1219 static void dx_sort_map (struct dx_map_entry *map, unsigned count) 1220 { 1221 struct dx_map_entry *p, *q, *top = map + count - 1; 1222 int more; 1223 /* Combsort until bubble sort doesn't suck */ 1224 while (count > 2) { 1225 count = count*10/13; 1226 if (count - 9 < 2) /* 9, 10 -> 11 */ 1227 count = 11; 1228 for (p = top, q = p - count; q >= map; p--, q--) 1229 if (p->hash < q->hash) 1230 swap(*p, *q); 1231 } 1232 /* Garden variety bubble sort */ 1233 do { 1234 more = 0; 1235 q = top; 1236 while (q-- > map) { 1237 if (q[1].hash >= q[0].hash) 1238 continue; 1239 swap(*(q+1), *q); 1240 more = 1; 1241 } 1242 } while(more); 1243 } 1244 1245 static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block) 1246 { 1247 struct dx_entry *entries = frame->entries; 1248 struct dx_entry *old = frame->at, *new = old + 1; 1249 int count = dx_get_count(entries); 1250 1251 assert(count < dx_get_limit(entries)); 1252 assert(old < entries + count); 1253 memmove(new + 1, new, (char *)(entries + count) - (char *)(new)); 1254 dx_set_hash(new, hash); 1255 dx_set_block(new, block); 1256 dx_set_count(entries, count + 1); 1257 } 1258 1259 #ifdef CONFIG_UNICODE 1260 /* 1261 * Test whether a case-insensitive directory entry matches the filename 1262 * being searched for. 1263 * 1264 * Returns: 0 if the directory entry matches, more than 0 if it 1265 * doesn't match or less than zero on error. 1266 */ 1267 int ext4_ci_compare(const struct inode *parent, const struct qstr *name, 1268 const struct qstr *entry) 1269 { 1270 const struct ext4_sb_info *sbi = EXT4_SB(parent->i_sb); 1271 const struct unicode_map *um = sbi->s_encoding; 1272 int ret; 1273 1274 ret = utf8_strncasecmp(um, name, entry); 1275 if (ret < 0) { 1276 /* Handle invalid character sequence as either an error 1277 * or as an opaque byte sequence. 1278 */ 1279 if (ext4_has_strict_mode(sbi)) 1280 return -EINVAL; 1281 1282 if (name->len != entry->len) 1283 return 1; 1284 1285 return !!memcmp(name->name, entry->name, name->len); 1286 } 1287 1288 return ret; 1289 } 1290 #endif 1291 1292 /* 1293 * Test whether a directory entry matches the filename being searched for. 1294 * 1295 * Return: %true if the directory entry matches, otherwise %false. 1296 */ 1297 static inline bool ext4_match(const struct inode *parent, 1298 const struct ext4_filename *fname, 1299 const struct ext4_dir_entry_2 *de) 1300 { 1301 struct fscrypt_name f; 1302 #ifdef CONFIG_UNICODE 1303 const struct qstr entry = {.name = de->name, .len = de->name_len}; 1304 #endif 1305 1306 if (!de->inode) 1307 return false; 1308 1309 f.usr_fname = fname->usr_fname; 1310 f.disk_name = fname->disk_name; 1311 #ifdef CONFIG_FS_ENCRYPTION 1312 f.crypto_buf = fname->crypto_buf; 1313 #endif 1314 1315 #ifdef CONFIG_UNICODE 1316 if (EXT4_SB(parent->i_sb)->s_encoding && IS_CASEFOLDED(parent)) 1317 return (ext4_ci_compare(parent, fname->usr_fname, &entry) == 0); 1318 #endif 1319 1320 return fscrypt_match_name(&f, de->name, de->name_len); 1321 } 1322 1323 /* 1324 * Returns 0 if not found, -1 on failure, and 1 on success 1325 */ 1326 int ext4_search_dir(struct buffer_head *bh, char *search_buf, int buf_size, 1327 struct inode *dir, struct ext4_filename *fname, 1328 unsigned int offset, struct ext4_dir_entry_2 **res_dir) 1329 { 1330 struct ext4_dir_entry_2 * de; 1331 char * dlimit; 1332 int de_len; 1333 1334 de = (struct ext4_dir_entry_2 *)search_buf; 1335 dlimit = search_buf + buf_size; 1336 while ((char *) de < dlimit) { 1337 /* this code is executed quadratically often */ 1338 /* do minimal checking `by hand' */ 1339 if ((char *) de + de->name_len <= dlimit && 1340 ext4_match(dir, fname, de)) { 1341 /* found a match - just to be sure, do 1342 * a full check */ 1343 if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data, 1344 bh->b_size, offset)) 1345 return -1; 1346 *res_dir = de; 1347 return 1; 1348 } 1349 /* prevent looping on a bad block */ 1350 de_len = ext4_rec_len_from_disk(de->rec_len, 1351 dir->i_sb->s_blocksize); 1352 if (de_len <= 0) 1353 return -1; 1354 offset += de_len; 1355 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len); 1356 } 1357 return 0; 1358 } 1359 1360 static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block, 1361 struct ext4_dir_entry *de) 1362 { 1363 struct super_block *sb = dir->i_sb; 1364 1365 if (!is_dx(dir)) 1366 return 0; 1367 if (block == 0) 1368 return 1; 1369 if (de->inode == 0 && 1370 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) == 1371 sb->s_blocksize) 1372 return 1; 1373 return 0; 1374 } 1375 1376 /* 1377 * __ext4_find_entry() 1378 * 1379 * finds an entry in the specified directory with the wanted name. It 1380 * returns the cache buffer in which the entry was found, and the entry 1381 * itself (as a parameter - res_dir). It does NOT read the inode of the 1382 * entry - you'll have to do that yourself if you want to. 1383 * 1384 * The returned buffer_head has ->b_count elevated. The caller is expected 1385 * to brelse() it when appropriate. 1386 */ 1387 static struct buffer_head *__ext4_find_entry(struct inode *dir, 1388 struct ext4_filename *fname, 1389 struct ext4_dir_entry_2 **res_dir, 1390 int *inlined) 1391 { 1392 struct super_block *sb; 1393 struct buffer_head *bh_use[NAMEI_RA_SIZE]; 1394 struct buffer_head *bh, *ret = NULL; 1395 ext4_lblk_t start, block; 1396 const u8 *name = fname->usr_fname->name; 1397 size_t ra_max = 0; /* Number of bh's in the readahead 1398 buffer, bh_use[] */ 1399 size_t ra_ptr = 0; /* Current index into readahead 1400 buffer */ 1401 ext4_lblk_t nblocks; 1402 int i, namelen, retval; 1403 1404 *res_dir = NULL; 1405 sb = dir->i_sb; 1406 namelen = fname->usr_fname->len; 1407 if (namelen > EXT4_NAME_LEN) 1408 return NULL; 1409 1410 if (ext4_has_inline_data(dir)) { 1411 int has_inline_data = 1; 1412 ret = ext4_find_inline_entry(dir, fname, res_dir, 1413 &has_inline_data); 1414 if (has_inline_data) { 1415 if (inlined) 1416 *inlined = 1; 1417 goto cleanup_and_exit; 1418 } 1419 } 1420 1421 if ((namelen <= 2) && (name[0] == '.') && 1422 (name[1] == '.' || name[1] == '\0')) { 1423 /* 1424 * "." or ".." will only be in the first block 1425 * NFS may look up ".."; "." should be handled by the VFS 1426 */ 1427 block = start = 0; 1428 nblocks = 1; 1429 goto restart; 1430 } 1431 if (is_dx(dir)) { 1432 ret = ext4_dx_find_entry(dir, fname, res_dir); 1433 /* 1434 * On success, or if the error was file not found, 1435 * return. Otherwise, fall back to doing a search the 1436 * old fashioned way. 1437 */ 1438 if (!IS_ERR(ret) || PTR_ERR(ret) != ERR_BAD_DX_DIR) 1439 goto cleanup_and_exit; 1440 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, " 1441 "falling back\n")); 1442 ret = NULL; 1443 } 1444 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb); 1445 if (!nblocks) { 1446 ret = NULL; 1447 goto cleanup_and_exit; 1448 } 1449 start = EXT4_I(dir)->i_dir_start_lookup; 1450 if (start >= nblocks) 1451 start = 0; 1452 block = start; 1453 restart: 1454 do { 1455 /* 1456 * We deal with the read-ahead logic here. 1457 */ 1458 if (ra_ptr >= ra_max) { 1459 /* Refill the readahead buffer */ 1460 ra_ptr = 0; 1461 if (block < start) 1462 ra_max = start - block; 1463 else 1464 ra_max = nblocks - block; 1465 ra_max = min(ra_max, ARRAY_SIZE(bh_use)); 1466 retval = ext4_bread_batch(dir, block, ra_max, 1467 false /* wait */, bh_use); 1468 if (retval) { 1469 ret = ERR_PTR(retval); 1470 ra_max = 0; 1471 goto cleanup_and_exit; 1472 } 1473 } 1474 if ((bh = bh_use[ra_ptr++]) == NULL) 1475 goto next; 1476 wait_on_buffer(bh); 1477 if (!buffer_uptodate(bh)) { 1478 EXT4_ERROR_INODE(dir, "reading directory lblock %lu", 1479 (unsigned long) block); 1480 brelse(bh); 1481 ret = ERR_PTR(-EIO); 1482 goto cleanup_and_exit; 1483 } 1484 if (!buffer_verified(bh) && 1485 !is_dx_internal_node(dir, block, 1486 (struct ext4_dir_entry *)bh->b_data) && 1487 !ext4_dirent_csum_verify(dir, 1488 (struct ext4_dir_entry *)bh->b_data)) { 1489 EXT4_ERROR_INODE(dir, "checksumming directory " 1490 "block %lu", (unsigned long)block); 1491 brelse(bh); 1492 ret = ERR_PTR(-EFSBADCRC); 1493 goto cleanup_and_exit; 1494 } 1495 set_buffer_verified(bh); 1496 i = search_dirblock(bh, dir, fname, 1497 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir); 1498 if (i == 1) { 1499 EXT4_I(dir)->i_dir_start_lookup = block; 1500 ret = bh; 1501 goto cleanup_and_exit; 1502 } else { 1503 brelse(bh); 1504 if (i < 0) 1505 goto cleanup_and_exit; 1506 } 1507 next: 1508 if (++block >= nblocks) 1509 block = 0; 1510 } while (block != start); 1511 1512 /* 1513 * If the directory has grown while we were searching, then 1514 * search the last part of the directory before giving up. 1515 */ 1516 block = nblocks; 1517 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb); 1518 if (block < nblocks) { 1519 start = 0; 1520 goto restart; 1521 } 1522 1523 cleanup_and_exit: 1524 /* Clean up the read-ahead blocks */ 1525 for (; ra_ptr < ra_max; ra_ptr++) 1526 brelse(bh_use[ra_ptr]); 1527 return ret; 1528 } 1529 1530 static struct buffer_head *ext4_find_entry(struct inode *dir, 1531 const struct qstr *d_name, 1532 struct ext4_dir_entry_2 **res_dir, 1533 int *inlined) 1534 { 1535 int err; 1536 struct ext4_filename fname; 1537 struct buffer_head *bh; 1538 1539 err = ext4_fname_setup_filename(dir, d_name, 1, &fname); 1540 if (err == -ENOENT) 1541 return NULL; 1542 if (err) 1543 return ERR_PTR(err); 1544 1545 bh = __ext4_find_entry(dir, &fname, res_dir, inlined); 1546 1547 ext4_fname_free_filename(&fname); 1548 return bh; 1549 } 1550 1551 static struct buffer_head *ext4_lookup_entry(struct inode *dir, 1552 struct dentry *dentry, 1553 struct ext4_dir_entry_2 **res_dir) 1554 { 1555 int err; 1556 struct ext4_filename fname; 1557 struct buffer_head *bh; 1558 1559 err = ext4_fname_prepare_lookup(dir, dentry, &fname); 1560 if (err == -ENOENT) 1561 return NULL; 1562 if (err) 1563 return ERR_PTR(err); 1564 1565 bh = __ext4_find_entry(dir, &fname, res_dir, NULL); 1566 1567 ext4_fname_free_filename(&fname); 1568 return bh; 1569 } 1570 1571 static struct buffer_head * ext4_dx_find_entry(struct inode *dir, 1572 struct ext4_filename *fname, 1573 struct ext4_dir_entry_2 **res_dir) 1574 { 1575 struct super_block * sb = dir->i_sb; 1576 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame; 1577 struct buffer_head *bh; 1578 ext4_lblk_t block; 1579 int retval; 1580 1581 #ifdef CONFIG_FS_ENCRYPTION 1582 *res_dir = NULL; 1583 #endif 1584 frame = dx_probe(fname, dir, NULL, frames); 1585 if (IS_ERR(frame)) 1586 return (struct buffer_head *) frame; 1587 do { 1588 block = dx_get_block(frame->at); 1589 bh = ext4_read_dirblock(dir, block, DIRENT); 1590 if (IS_ERR(bh)) 1591 goto errout; 1592 1593 retval = search_dirblock(bh, dir, fname, 1594 block << EXT4_BLOCK_SIZE_BITS(sb), 1595 res_dir); 1596 if (retval == 1) 1597 goto success; 1598 brelse(bh); 1599 if (retval == -1) { 1600 bh = ERR_PTR(ERR_BAD_DX_DIR); 1601 goto errout; 1602 } 1603 1604 /* Check to see if we should continue to search */ 1605 retval = ext4_htree_next_block(dir, fname->hinfo.hash, frame, 1606 frames, NULL); 1607 if (retval < 0) { 1608 ext4_warning_inode(dir, 1609 "error %d reading directory index block", 1610 retval); 1611 bh = ERR_PTR(retval); 1612 goto errout; 1613 } 1614 } while (retval == 1); 1615 1616 bh = NULL; 1617 errout: 1618 dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name)); 1619 success: 1620 dx_release(frames); 1621 return bh; 1622 } 1623 1624 static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) 1625 { 1626 struct inode *inode; 1627 struct ext4_dir_entry_2 *de; 1628 struct buffer_head *bh; 1629 1630 if (dentry->d_name.len > EXT4_NAME_LEN) 1631 return ERR_PTR(-ENAMETOOLONG); 1632 1633 bh = ext4_lookup_entry(dir, dentry, &de); 1634 if (IS_ERR(bh)) 1635 return ERR_CAST(bh); 1636 inode = NULL; 1637 if (bh) { 1638 __u32 ino = le32_to_cpu(de->inode); 1639 brelse(bh); 1640 if (!ext4_valid_inum(dir->i_sb, ino)) { 1641 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino); 1642 return ERR_PTR(-EFSCORRUPTED); 1643 } 1644 if (unlikely(ino == dir->i_ino)) { 1645 EXT4_ERROR_INODE(dir, "'%pd' linked to parent dir", 1646 dentry); 1647 return ERR_PTR(-EFSCORRUPTED); 1648 } 1649 inode = ext4_iget(dir->i_sb, ino, EXT4_IGET_NORMAL); 1650 if (inode == ERR_PTR(-ESTALE)) { 1651 EXT4_ERROR_INODE(dir, 1652 "deleted inode referenced: %u", 1653 ino); 1654 return ERR_PTR(-EFSCORRUPTED); 1655 } 1656 if (!IS_ERR(inode) && IS_ENCRYPTED(dir) && 1657 (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) && 1658 !fscrypt_has_permitted_context(dir, inode)) { 1659 ext4_warning(inode->i_sb, 1660 "Inconsistent encryption contexts: %lu/%lu", 1661 dir->i_ino, inode->i_ino); 1662 iput(inode); 1663 return ERR_PTR(-EPERM); 1664 } 1665 } 1666 1667 #ifdef CONFIG_UNICODE 1668 if (!inode && IS_CASEFOLDED(dir)) { 1669 /* Eventually we want to call d_add_ci(dentry, NULL) 1670 * for negative dentries in the encoding case as 1671 * well. For now, prevent the negative dentry 1672 * from being cached. 1673 */ 1674 return NULL; 1675 } 1676 #endif 1677 return d_splice_alias(inode, dentry); 1678 } 1679 1680 1681 struct dentry *ext4_get_parent(struct dentry *child) 1682 { 1683 __u32 ino; 1684 static const struct qstr dotdot = QSTR_INIT("..", 2); 1685 struct ext4_dir_entry_2 * de; 1686 struct buffer_head *bh; 1687 1688 bh = ext4_find_entry(d_inode(child), &dotdot, &de, NULL); 1689 if (IS_ERR(bh)) 1690 return ERR_CAST(bh); 1691 if (!bh) 1692 return ERR_PTR(-ENOENT); 1693 ino = le32_to_cpu(de->inode); 1694 brelse(bh); 1695 1696 if (!ext4_valid_inum(child->d_sb, ino)) { 1697 EXT4_ERROR_INODE(d_inode(child), 1698 "bad parent inode number: %u", ino); 1699 return ERR_PTR(-EFSCORRUPTED); 1700 } 1701 1702 return d_obtain_alias(ext4_iget(child->d_sb, ino, EXT4_IGET_NORMAL)); 1703 } 1704 1705 /* 1706 * Move count entries from end of map between two memory locations. 1707 * Returns pointer to last entry moved. 1708 */ 1709 static struct ext4_dir_entry_2 * 1710 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count, 1711 unsigned blocksize) 1712 { 1713 unsigned rec_len = 0; 1714 1715 while (count--) { 1716 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) 1717 (from + (map->offs<<2)); 1718 rec_len = EXT4_DIR_REC_LEN(de->name_len); 1719 memcpy (to, de, rec_len); 1720 ((struct ext4_dir_entry_2 *) to)->rec_len = 1721 ext4_rec_len_to_disk(rec_len, blocksize); 1722 de->inode = 0; 1723 map++; 1724 to += rec_len; 1725 } 1726 return (struct ext4_dir_entry_2 *) (to - rec_len); 1727 } 1728 1729 /* 1730 * Compact each dir entry in the range to the minimal rec_len. 1731 * Returns pointer to last entry in range. 1732 */ 1733 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize) 1734 { 1735 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base; 1736 unsigned rec_len = 0; 1737 1738 prev = to = de; 1739 while ((char*)de < base + blocksize) { 1740 next = ext4_next_entry(de, blocksize); 1741 if (de->inode && de->name_len) { 1742 rec_len = EXT4_DIR_REC_LEN(de->name_len); 1743 if (de > to) 1744 memmove(to, de, rec_len); 1745 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize); 1746 prev = to; 1747 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len); 1748 } 1749 de = next; 1750 } 1751 return prev; 1752 } 1753 1754 /* 1755 * Split a full leaf block to make room for a new dir entry. 1756 * Allocate a new block, and move entries so that they are approx. equally full. 1757 * Returns pointer to de in block into which the new entry will be inserted. 1758 */ 1759 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, 1760 struct buffer_head **bh,struct dx_frame *frame, 1761 struct dx_hash_info *hinfo) 1762 { 1763 unsigned blocksize = dir->i_sb->s_blocksize; 1764 unsigned count, continued; 1765 struct buffer_head *bh2; 1766 ext4_lblk_t newblock; 1767 u32 hash2; 1768 struct dx_map_entry *map; 1769 char *data1 = (*bh)->b_data, *data2; 1770 unsigned split, move, size; 1771 struct ext4_dir_entry_2 *de = NULL, *de2; 1772 struct ext4_dir_entry_tail *t; 1773 int csum_size = 0; 1774 int err = 0, i; 1775 1776 if (ext4_has_metadata_csum(dir->i_sb)) 1777 csum_size = sizeof(struct ext4_dir_entry_tail); 1778 1779 bh2 = ext4_append(handle, dir, &newblock); 1780 if (IS_ERR(bh2)) { 1781 brelse(*bh); 1782 *bh = NULL; 1783 return (struct ext4_dir_entry_2 *) bh2; 1784 } 1785 1786 BUFFER_TRACE(*bh, "get_write_access"); 1787 err = ext4_journal_get_write_access(handle, *bh); 1788 if (err) 1789 goto journal_error; 1790 1791 BUFFER_TRACE(frame->bh, "get_write_access"); 1792 err = ext4_journal_get_write_access(handle, frame->bh); 1793 if (err) 1794 goto journal_error; 1795 1796 data2 = bh2->b_data; 1797 1798 /* create map in the end of data2 block */ 1799 map = (struct dx_map_entry *) (data2 + blocksize); 1800 count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1, 1801 blocksize, hinfo, map); 1802 map -= count; 1803 dx_sort_map(map, count); 1804 /* Split the existing block in the middle, size-wise */ 1805 size = 0; 1806 move = 0; 1807 for (i = count-1; i >= 0; i--) { 1808 /* is more than half of this entry in 2nd half of the block? */ 1809 if (size + map[i].size/2 > blocksize/2) 1810 break; 1811 size += map[i].size; 1812 move++; 1813 } 1814 /* map index at which we will split */ 1815 split = count - move; 1816 hash2 = map[split].hash; 1817 continued = hash2 == map[split - 1].hash; 1818 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n", 1819 (unsigned long)dx_get_block(frame->at), 1820 hash2, split, count-split)); 1821 1822 /* Fancy dance to stay within two buffers */ 1823 de2 = dx_move_dirents(data1, data2, map + split, count - split, 1824 blocksize); 1825 de = dx_pack_dirents(data1, blocksize); 1826 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) - 1827 (char *) de, 1828 blocksize); 1829 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) - 1830 (char *) de2, 1831 blocksize); 1832 if (csum_size) { 1833 t = EXT4_DIRENT_TAIL(data2, blocksize); 1834 initialize_dirent_tail(t, blocksize); 1835 1836 t = EXT4_DIRENT_TAIL(data1, blocksize); 1837 initialize_dirent_tail(t, blocksize); 1838 } 1839 1840 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data1, 1841 blocksize, 1)); 1842 dxtrace(dx_show_leaf(dir, hinfo, (struct ext4_dir_entry_2 *) data2, 1843 blocksize, 1)); 1844 1845 /* Which block gets the new entry? */ 1846 if (hinfo->hash >= hash2) { 1847 swap(*bh, bh2); 1848 de = de2; 1849 } 1850 dx_insert_block(frame, hash2 + continued, newblock); 1851 err = ext4_handle_dirty_dirent_node(handle, dir, bh2); 1852 if (err) 1853 goto journal_error; 1854 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh); 1855 if (err) 1856 goto journal_error; 1857 brelse(bh2); 1858 dxtrace(dx_show_index("frame", frame->entries)); 1859 return de; 1860 1861 journal_error: 1862 brelse(*bh); 1863 brelse(bh2); 1864 *bh = NULL; 1865 ext4_std_error(dir->i_sb, err); 1866 return ERR_PTR(err); 1867 } 1868 1869 int ext4_find_dest_de(struct inode *dir, struct inode *inode, 1870 struct buffer_head *bh, 1871 void *buf, int buf_size, 1872 struct ext4_filename *fname, 1873 struct ext4_dir_entry_2 **dest_de) 1874 { 1875 struct ext4_dir_entry_2 *de; 1876 unsigned short reclen = EXT4_DIR_REC_LEN(fname_len(fname)); 1877 int nlen, rlen; 1878 unsigned int offset = 0; 1879 char *top; 1880 1881 de = (struct ext4_dir_entry_2 *)buf; 1882 top = buf + buf_size - reclen; 1883 while ((char *) de <= top) { 1884 if (ext4_check_dir_entry(dir, NULL, de, bh, 1885 buf, buf_size, offset)) 1886 return -EFSCORRUPTED; 1887 if (ext4_match(dir, fname, de)) 1888 return -EEXIST; 1889 nlen = EXT4_DIR_REC_LEN(de->name_len); 1890 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size); 1891 if ((de->inode ? rlen - nlen : rlen) >= reclen) 1892 break; 1893 de = (struct ext4_dir_entry_2 *)((char *)de + rlen); 1894 offset += rlen; 1895 } 1896 if ((char *) de > top) 1897 return -ENOSPC; 1898 1899 *dest_de = de; 1900 return 0; 1901 } 1902 1903 void ext4_insert_dentry(struct inode *inode, 1904 struct ext4_dir_entry_2 *de, 1905 int buf_size, 1906 struct ext4_filename *fname) 1907 { 1908 1909 int nlen, rlen; 1910 1911 nlen = EXT4_DIR_REC_LEN(de->name_len); 1912 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size); 1913 if (de->inode) { 1914 struct ext4_dir_entry_2 *de1 = 1915 (struct ext4_dir_entry_2 *)((char *)de + nlen); 1916 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size); 1917 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size); 1918 de = de1; 1919 } 1920 de->file_type = EXT4_FT_UNKNOWN; 1921 de->inode = cpu_to_le32(inode->i_ino); 1922 ext4_set_de_type(inode->i_sb, de, inode->i_mode); 1923 de->name_len = fname_len(fname); 1924 memcpy(de->name, fname_name(fname), fname_len(fname)); 1925 } 1926 1927 /* 1928 * Add a new entry into a directory (leaf) block. If de is non-NULL, 1929 * it points to a directory entry which is guaranteed to be large 1930 * enough for new directory entry. If de is NULL, then 1931 * add_dirent_to_buf will attempt search the directory block for 1932 * space. It will return -ENOSPC if no space is available, and -EIO 1933 * and -EEXIST if directory entry already exists. 1934 */ 1935 static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname, 1936 struct inode *dir, 1937 struct inode *inode, struct ext4_dir_entry_2 *de, 1938 struct buffer_head *bh) 1939 { 1940 unsigned int blocksize = dir->i_sb->s_blocksize; 1941 int csum_size = 0; 1942 int err; 1943 1944 if (ext4_has_metadata_csum(inode->i_sb)) 1945 csum_size = sizeof(struct ext4_dir_entry_tail); 1946 1947 if (!de) { 1948 err = ext4_find_dest_de(dir, inode, bh, bh->b_data, 1949 blocksize - csum_size, fname, &de); 1950 if (err) 1951 return err; 1952 } 1953 BUFFER_TRACE(bh, "get_write_access"); 1954 err = ext4_journal_get_write_access(handle, bh); 1955 if (err) { 1956 ext4_std_error(dir->i_sb, err); 1957 return err; 1958 } 1959 1960 /* By now the buffer is marked for journaling */ 1961 ext4_insert_dentry(inode, de, blocksize, fname); 1962 1963 /* 1964 * XXX shouldn't update any times until successful 1965 * completion of syscall, but too many callers depend 1966 * on this. 1967 * 1968 * XXX similarly, too many callers depend on 1969 * ext4_new_inode() setting the times, but error 1970 * recovery deletes the inode, so the worst that can 1971 * happen is that the times are slightly out of date 1972 * and/or different from the directory change time. 1973 */ 1974 dir->i_mtime = dir->i_ctime = current_time(dir); 1975 ext4_update_dx_flag(dir); 1976 inode_inc_iversion(dir); 1977 ext4_mark_inode_dirty(handle, dir); 1978 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 1979 err = ext4_handle_dirty_dirent_node(handle, dir, bh); 1980 if (err) 1981 ext4_std_error(dir->i_sb, err); 1982 return 0; 1983 } 1984 1985 /* 1986 * This converts a one block unindexed directory to a 3 block indexed 1987 * directory, and adds the dentry to the indexed directory. 1988 */ 1989 static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname, 1990 struct inode *dir, 1991 struct inode *inode, struct buffer_head *bh) 1992 { 1993 struct buffer_head *bh2; 1994 struct dx_root *root; 1995 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame; 1996 struct dx_entry *entries; 1997 struct ext4_dir_entry_2 *de, *de2; 1998 struct ext4_dir_entry_tail *t; 1999 char *data1, *top; 2000 unsigned len; 2001 int retval; 2002 unsigned blocksize; 2003 ext4_lblk_t block; 2004 struct fake_dirent *fde; 2005 int csum_size = 0; 2006 2007 if (ext4_has_metadata_csum(inode->i_sb)) 2008 csum_size = sizeof(struct ext4_dir_entry_tail); 2009 2010 blocksize = dir->i_sb->s_blocksize; 2011 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino)); 2012 BUFFER_TRACE(bh, "get_write_access"); 2013 retval = ext4_journal_get_write_access(handle, bh); 2014 if (retval) { 2015 ext4_std_error(dir->i_sb, retval); 2016 brelse(bh); 2017 return retval; 2018 } 2019 root = (struct dx_root *) bh->b_data; 2020 2021 /* The 0th block becomes the root, move the dirents out */ 2022 fde = &root->dotdot; 2023 de = (struct ext4_dir_entry_2 *)((char *)fde + 2024 ext4_rec_len_from_disk(fde->rec_len, blocksize)); 2025 if ((char *) de >= (((char *) root) + blocksize)) { 2026 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'"); 2027 brelse(bh); 2028 return -EFSCORRUPTED; 2029 } 2030 len = ((char *) root) + (blocksize - csum_size) - (char *) de; 2031 2032 /* Allocate new block for the 0th block's dirents */ 2033 bh2 = ext4_append(handle, dir, &block); 2034 if (IS_ERR(bh2)) { 2035 brelse(bh); 2036 return PTR_ERR(bh2); 2037 } 2038 ext4_set_inode_flag(dir, EXT4_INODE_INDEX); 2039 data1 = bh2->b_data; 2040 2041 memcpy (data1, de, len); 2042 de = (struct ext4_dir_entry_2 *) data1; 2043 top = data1 + len; 2044 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) 2045 de = de2; 2046 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) - 2047 (char *) de, 2048 blocksize); 2049 2050 if (csum_size) { 2051 t = EXT4_DIRENT_TAIL(data1, blocksize); 2052 initialize_dirent_tail(t, blocksize); 2053 } 2054 2055 /* Initialize the root; the dot dirents already exist */ 2056 de = (struct ext4_dir_entry_2 *) (&root->dotdot); 2057 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2), 2058 blocksize); 2059 memset (&root->info, 0, sizeof(root->info)); 2060 root->info.info_length = sizeof(root->info); 2061 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version; 2062 entries = root->entries; 2063 dx_set_block(entries, 1); 2064 dx_set_count(entries, 1); 2065 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info))); 2066 2067 /* Initialize as for dx_probe */ 2068 fname->hinfo.hash_version = root->info.hash_version; 2069 if (fname->hinfo.hash_version <= DX_HASH_TEA) 2070 fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned; 2071 fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed; 2072 ext4fs_dirhash(dir, fname_name(fname), fname_len(fname), &fname->hinfo); 2073 2074 memset(frames, 0, sizeof(frames)); 2075 frame = frames; 2076 frame->entries = entries; 2077 frame->at = entries; 2078 frame->bh = bh; 2079 2080 retval = ext4_handle_dirty_dx_node(handle, dir, frame->bh); 2081 if (retval) 2082 goto out_frames; 2083 retval = ext4_handle_dirty_dirent_node(handle, dir, bh2); 2084 if (retval) 2085 goto out_frames; 2086 2087 de = do_split(handle,dir, &bh2, frame, &fname->hinfo); 2088 if (IS_ERR(de)) { 2089 retval = PTR_ERR(de); 2090 goto out_frames; 2091 } 2092 2093 retval = add_dirent_to_buf(handle, fname, dir, inode, de, bh2); 2094 out_frames: 2095 /* 2096 * Even if the block split failed, we have to properly write 2097 * out all the changes we did so far. Otherwise we can end up 2098 * with corrupted filesystem. 2099 */ 2100 if (retval) 2101 ext4_mark_inode_dirty(handle, dir); 2102 dx_release(frames); 2103 brelse(bh2); 2104 return retval; 2105 } 2106 2107 /* 2108 * ext4_add_entry() 2109 * 2110 * adds a file entry to the specified directory, using the same 2111 * semantics as ext4_find_entry(). It returns NULL if it failed. 2112 * 2113 * NOTE!! The inode part of 'de' is left at 0 - which means you 2114 * may not sleep between calling this and putting something into 2115 * the entry, as someone else might have used it while you slept. 2116 */ 2117 static int ext4_add_entry(handle_t *handle, struct dentry *dentry, 2118 struct inode *inode) 2119 { 2120 struct inode *dir = d_inode(dentry->d_parent); 2121 struct buffer_head *bh = NULL; 2122 struct ext4_dir_entry_2 *de; 2123 struct ext4_dir_entry_tail *t; 2124 struct super_block *sb; 2125 struct ext4_sb_info *sbi; 2126 struct ext4_filename fname; 2127 int retval; 2128 int dx_fallback=0; 2129 unsigned blocksize; 2130 ext4_lblk_t block, blocks; 2131 int csum_size = 0; 2132 2133 if (ext4_has_metadata_csum(inode->i_sb)) 2134 csum_size = sizeof(struct ext4_dir_entry_tail); 2135 2136 sb = dir->i_sb; 2137 sbi = EXT4_SB(sb); 2138 blocksize = sb->s_blocksize; 2139 if (!dentry->d_name.len) 2140 return -EINVAL; 2141 2142 #ifdef CONFIG_UNICODE 2143 if (ext4_has_strict_mode(sbi) && IS_CASEFOLDED(dir) && 2144 utf8_validate(sbi->s_encoding, &dentry->d_name)) 2145 return -EINVAL; 2146 #endif 2147 2148 retval = ext4_fname_setup_filename(dir, &dentry->d_name, 0, &fname); 2149 if (retval) 2150 return retval; 2151 2152 if (ext4_has_inline_data(dir)) { 2153 retval = ext4_try_add_inline_entry(handle, &fname, dir, inode); 2154 if (retval < 0) 2155 goto out; 2156 if (retval == 1) { 2157 retval = 0; 2158 goto out; 2159 } 2160 } 2161 2162 if (is_dx(dir)) { 2163 retval = ext4_dx_add_entry(handle, &fname, dir, inode); 2164 if (!retval || (retval != ERR_BAD_DX_DIR)) 2165 goto out; 2166 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX); 2167 dx_fallback++; 2168 ext4_mark_inode_dirty(handle, dir); 2169 } 2170 blocks = dir->i_size >> sb->s_blocksize_bits; 2171 for (block = 0; block < blocks; block++) { 2172 bh = ext4_read_dirblock(dir, block, DIRENT); 2173 if (IS_ERR(bh)) { 2174 retval = PTR_ERR(bh); 2175 bh = NULL; 2176 goto out; 2177 } 2178 retval = add_dirent_to_buf(handle, &fname, dir, inode, 2179 NULL, bh); 2180 if (retval != -ENOSPC) 2181 goto out; 2182 2183 if (blocks == 1 && !dx_fallback && 2184 ext4_has_feature_dir_index(sb)) { 2185 retval = make_indexed_dir(handle, &fname, dir, 2186 inode, bh); 2187 bh = NULL; /* make_indexed_dir releases bh */ 2188 goto out; 2189 } 2190 brelse(bh); 2191 } 2192 bh = ext4_append(handle, dir, &block); 2193 if (IS_ERR(bh)) { 2194 retval = PTR_ERR(bh); 2195 bh = NULL; 2196 goto out; 2197 } 2198 de = (struct ext4_dir_entry_2 *) bh->b_data; 2199 de->inode = 0; 2200 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize); 2201 2202 if (csum_size) { 2203 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize); 2204 initialize_dirent_tail(t, blocksize); 2205 } 2206 2207 retval = add_dirent_to_buf(handle, &fname, dir, inode, de, bh); 2208 out: 2209 ext4_fname_free_filename(&fname); 2210 brelse(bh); 2211 if (retval == 0) 2212 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY); 2213 return retval; 2214 } 2215 2216 /* 2217 * Returns 0 for success, or a negative error value 2218 */ 2219 static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname, 2220 struct inode *dir, struct inode *inode) 2221 { 2222 struct dx_frame frames[EXT4_HTREE_LEVEL], *frame; 2223 struct dx_entry *entries, *at; 2224 struct buffer_head *bh; 2225 struct super_block *sb = dir->i_sb; 2226 struct ext4_dir_entry_2 *de; 2227 int restart; 2228 int err; 2229 2230 again: 2231 restart = 0; 2232 frame = dx_probe(fname, dir, NULL, frames); 2233 if (IS_ERR(frame)) 2234 return PTR_ERR(frame); 2235 entries = frame->entries; 2236 at = frame->at; 2237 bh = ext4_read_dirblock(dir, dx_get_block(frame->at), DIRENT); 2238 if (IS_ERR(bh)) { 2239 err = PTR_ERR(bh); 2240 bh = NULL; 2241 goto cleanup; 2242 } 2243 2244 BUFFER_TRACE(bh, "get_write_access"); 2245 err = ext4_journal_get_write_access(handle, bh); 2246 if (err) 2247 goto journal_error; 2248 2249 err = add_dirent_to_buf(handle, fname, dir, inode, NULL, bh); 2250 if (err != -ENOSPC) 2251 goto cleanup; 2252 2253 err = 0; 2254 /* Block full, should compress but for now just split */ 2255 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n", 2256 dx_get_count(entries), dx_get_limit(entries))); 2257 /* Need to split index? */ 2258 if (dx_get_count(entries) == dx_get_limit(entries)) { 2259 ext4_lblk_t newblock; 2260 int levels = frame - frames + 1; 2261 unsigned int icount; 2262 int add_level = 1; 2263 struct dx_entry *entries2; 2264 struct dx_node *node2; 2265 struct buffer_head *bh2; 2266 2267 while (frame > frames) { 2268 if (dx_get_count((frame - 1)->entries) < 2269 dx_get_limit((frame - 1)->entries)) { 2270 add_level = 0; 2271 break; 2272 } 2273 frame--; /* split higher index block */ 2274 at = frame->at; 2275 entries = frame->entries; 2276 restart = 1; 2277 } 2278 if (add_level && levels == ext4_dir_htree_level(sb)) { 2279 ext4_warning(sb, "Directory (ino: %lu) index full, " 2280 "reach max htree level :%d", 2281 dir->i_ino, levels); 2282 if (ext4_dir_htree_level(sb) < EXT4_HTREE_LEVEL) { 2283 ext4_warning(sb, "Large directory feature is " 2284 "not enabled on this " 2285 "filesystem"); 2286 } 2287 err = -ENOSPC; 2288 goto cleanup; 2289 } 2290 icount = dx_get_count(entries); 2291 bh2 = ext4_append(handle, dir, &newblock); 2292 if (IS_ERR(bh2)) { 2293 err = PTR_ERR(bh2); 2294 goto cleanup; 2295 } 2296 node2 = (struct dx_node *)(bh2->b_data); 2297 entries2 = node2->entries; 2298 memset(&node2->fake, 0, sizeof(struct fake_dirent)); 2299 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize, 2300 sb->s_blocksize); 2301 BUFFER_TRACE(frame->bh, "get_write_access"); 2302 err = ext4_journal_get_write_access(handle, frame->bh); 2303 if (err) 2304 goto journal_error; 2305 if (!add_level) { 2306 unsigned icount1 = icount/2, icount2 = icount - icount1; 2307 unsigned hash2 = dx_get_hash(entries + icount1); 2308 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n", 2309 icount1, icount2)); 2310 2311 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */ 2312 err = ext4_journal_get_write_access(handle, 2313 (frame - 1)->bh); 2314 if (err) 2315 goto journal_error; 2316 2317 memcpy((char *) entries2, (char *) (entries + icount1), 2318 icount2 * sizeof(struct dx_entry)); 2319 dx_set_count(entries, icount1); 2320 dx_set_count(entries2, icount2); 2321 dx_set_limit(entries2, dx_node_limit(dir)); 2322 2323 /* Which index block gets the new entry? */ 2324 if (at - entries >= icount1) { 2325 frame->at = at = at - entries - icount1 + entries2; 2326 frame->entries = entries = entries2; 2327 swap(frame->bh, bh2); 2328 } 2329 dx_insert_block((frame - 1), hash2, newblock); 2330 dxtrace(dx_show_index("node", frame->entries)); 2331 dxtrace(dx_show_index("node", 2332 ((struct dx_node *) bh2->b_data)->entries)); 2333 err = ext4_handle_dirty_dx_node(handle, dir, bh2); 2334 if (err) 2335 goto journal_error; 2336 brelse (bh2); 2337 err = ext4_handle_dirty_dx_node(handle, dir, 2338 (frame - 1)->bh); 2339 if (err) 2340 goto journal_error; 2341 if (restart) { 2342 err = ext4_handle_dirty_dx_node(handle, dir, 2343 frame->bh); 2344 goto journal_error; 2345 } 2346 } else { 2347 struct dx_root *dxroot; 2348 memcpy((char *) entries2, (char *) entries, 2349 icount * sizeof(struct dx_entry)); 2350 dx_set_limit(entries2, dx_node_limit(dir)); 2351 2352 /* Set up root */ 2353 dx_set_count(entries, 1); 2354 dx_set_block(entries + 0, newblock); 2355 dxroot = (struct dx_root *)frames[0].bh->b_data; 2356 dxroot->info.indirect_levels += 1; 2357 dxtrace(printk(KERN_DEBUG 2358 "Creating %d level index...\n", 2359 dxroot->info.indirect_levels)); 2360 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh); 2361 if (err) 2362 goto journal_error; 2363 err = ext4_handle_dirty_dx_node(handle, dir, bh2); 2364 brelse(bh2); 2365 restart = 1; 2366 goto journal_error; 2367 } 2368 } 2369 de = do_split(handle, dir, &bh, frame, &fname->hinfo); 2370 if (IS_ERR(de)) { 2371 err = PTR_ERR(de); 2372 goto cleanup; 2373 } 2374 err = add_dirent_to_buf(handle, fname, dir, inode, de, bh); 2375 goto cleanup; 2376 2377 journal_error: 2378 ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */ 2379 cleanup: 2380 brelse(bh); 2381 dx_release(frames); 2382 /* @restart is true means htree-path has been changed, we need to 2383 * repeat dx_probe() to find out valid htree-path 2384 */ 2385 if (restart && err == 0) 2386 goto again; 2387 return err; 2388 } 2389 2390 /* 2391 * ext4_generic_delete_entry deletes a directory entry by merging it 2392 * with the previous entry 2393 */ 2394 int ext4_generic_delete_entry(handle_t *handle, 2395 struct inode *dir, 2396 struct ext4_dir_entry_2 *de_del, 2397 struct buffer_head *bh, 2398 void *entry_buf, 2399 int buf_size, 2400 int csum_size) 2401 { 2402 struct ext4_dir_entry_2 *de, *pde; 2403 unsigned int blocksize = dir->i_sb->s_blocksize; 2404 int i; 2405 2406 i = 0; 2407 pde = NULL; 2408 de = (struct ext4_dir_entry_2 *)entry_buf; 2409 while (i < buf_size - csum_size) { 2410 if (ext4_check_dir_entry(dir, NULL, de, bh, 2411 bh->b_data, bh->b_size, i)) 2412 return -EFSCORRUPTED; 2413 if (de == de_del) { 2414 if (pde) 2415 pde->rec_len = ext4_rec_len_to_disk( 2416 ext4_rec_len_from_disk(pde->rec_len, 2417 blocksize) + 2418 ext4_rec_len_from_disk(de->rec_len, 2419 blocksize), 2420 blocksize); 2421 else 2422 de->inode = 0; 2423 inode_inc_iversion(dir); 2424 return 0; 2425 } 2426 i += ext4_rec_len_from_disk(de->rec_len, blocksize); 2427 pde = de; 2428 de = ext4_next_entry(de, blocksize); 2429 } 2430 return -ENOENT; 2431 } 2432 2433 static int ext4_delete_entry(handle_t *handle, 2434 struct inode *dir, 2435 struct ext4_dir_entry_2 *de_del, 2436 struct buffer_head *bh) 2437 { 2438 int err, csum_size = 0; 2439 2440 if (ext4_has_inline_data(dir)) { 2441 int has_inline_data = 1; 2442 err = ext4_delete_inline_entry(handle, dir, de_del, bh, 2443 &has_inline_data); 2444 if (has_inline_data) 2445 return err; 2446 } 2447 2448 if (ext4_has_metadata_csum(dir->i_sb)) 2449 csum_size = sizeof(struct ext4_dir_entry_tail); 2450 2451 BUFFER_TRACE(bh, "get_write_access"); 2452 err = ext4_journal_get_write_access(handle, bh); 2453 if (unlikely(err)) 2454 goto out; 2455 2456 err = ext4_generic_delete_entry(handle, dir, de_del, 2457 bh, bh->b_data, 2458 dir->i_sb->s_blocksize, csum_size); 2459 if (err) 2460 goto out; 2461 2462 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); 2463 err = ext4_handle_dirty_dirent_node(handle, dir, bh); 2464 if (unlikely(err)) 2465 goto out; 2466 2467 return 0; 2468 out: 2469 if (err != -ENOENT) 2470 ext4_std_error(dir->i_sb, err); 2471 return err; 2472 } 2473 2474 /* 2475 * Set directory link count to 1 if nlinks > EXT4_LINK_MAX, or if nlinks == 2 2476 * since this indicates that nlinks count was previously 1 to avoid overflowing 2477 * the 16-bit i_links_count field on disk. Directories with i_nlink == 1 mean 2478 * that subdirectory link counts are not being maintained accurately. 2479 * 2480 * The caller has already checked for i_nlink overflow in case the DIR_LINK 2481 * feature is not enabled and returned -EMLINK. The is_dx() check is a proxy 2482 * for checking S_ISDIR(inode) (since the INODE_INDEX feature will not be set 2483 * on regular files) and to avoid creating huge/slow non-HTREE directories. 2484 */ 2485 static void ext4_inc_count(handle_t *handle, struct inode *inode) 2486 { 2487 inc_nlink(inode); 2488 if (is_dx(inode) && 2489 (inode->i_nlink > EXT4_LINK_MAX || inode->i_nlink == 2)) 2490 set_nlink(inode, 1); 2491 } 2492 2493 /* 2494 * If a directory had nlink == 1, then we should let it be 1. This indicates 2495 * directory has >EXT4_LINK_MAX subdirs. 2496 */ 2497 static void ext4_dec_count(handle_t *handle, struct inode *inode) 2498 { 2499 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2) 2500 drop_nlink(inode); 2501 } 2502 2503 2504 static int ext4_add_nondir(handle_t *handle, 2505 struct dentry *dentry, struct inode *inode) 2506 { 2507 int err = ext4_add_entry(handle, dentry, inode); 2508 if (!err) { 2509 ext4_mark_inode_dirty(handle, inode); 2510 d_instantiate_new(dentry, inode); 2511 return 0; 2512 } 2513 drop_nlink(inode); 2514 unlock_new_inode(inode); 2515 iput(inode); 2516 return err; 2517 } 2518 2519 /* 2520 * By the time this is called, we already have created 2521 * the directory cache entry for the new file, but it 2522 * is so far negative - it has no inode. 2523 * 2524 * If the create succeeds, we fill in the inode information 2525 * with d_instantiate(). 2526 */ 2527 static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2528 bool excl) 2529 { 2530 handle_t *handle; 2531 struct inode *inode; 2532 int err, credits, retries = 0; 2533 2534 err = dquot_initialize(dir); 2535 if (err) 2536 return err; 2537 2538 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + 2539 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3); 2540 retry: 2541 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0, 2542 NULL, EXT4_HT_DIR, credits); 2543 handle = ext4_journal_current_handle(); 2544 err = PTR_ERR(inode); 2545 if (!IS_ERR(inode)) { 2546 inode->i_op = &ext4_file_inode_operations; 2547 inode->i_fop = &ext4_file_operations; 2548 ext4_set_aops(inode); 2549 err = ext4_add_nondir(handle, dentry, inode); 2550 if (!err && IS_DIRSYNC(dir)) 2551 ext4_handle_sync(handle); 2552 } 2553 if (handle) 2554 ext4_journal_stop(handle); 2555 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) 2556 goto retry; 2557 return err; 2558 } 2559 2560 static int ext4_mknod(struct inode *dir, struct dentry *dentry, 2561 umode_t mode, dev_t rdev) 2562 { 2563 handle_t *handle; 2564 struct inode *inode; 2565 int err, credits, retries = 0; 2566 2567 err = dquot_initialize(dir); 2568 if (err) 2569 return err; 2570 2571 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + 2572 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3); 2573 retry: 2574 inode = ext4_new_inode_start_handle(dir, mode, &dentry->d_name, 0, 2575 NULL, EXT4_HT_DIR, credits); 2576 handle = ext4_journal_current_handle(); 2577 err = PTR_ERR(inode); 2578 if (!IS_ERR(inode)) { 2579 init_special_inode(inode, inode->i_mode, rdev); 2580 inode->i_op = &ext4_special_inode_operations; 2581 err = ext4_add_nondir(handle, dentry, inode); 2582 if (!err && IS_DIRSYNC(dir)) 2583 ext4_handle_sync(handle); 2584 } 2585 if (handle) 2586 ext4_journal_stop(handle); 2587 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) 2588 goto retry; 2589 return err; 2590 } 2591 2592 static int ext4_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 2593 { 2594 handle_t *handle; 2595 struct inode *inode; 2596 int err, retries = 0; 2597 2598 err = dquot_initialize(dir); 2599 if (err) 2600 return err; 2601 2602 retry: 2603 inode = ext4_new_inode_start_handle(dir, mode, 2604 NULL, 0, NULL, 2605 EXT4_HT_DIR, 2606 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) + 2607 4 + EXT4_XATTR_TRANS_BLOCKS); 2608 handle = ext4_journal_current_handle(); 2609 err = PTR_ERR(inode); 2610 if (!IS_ERR(inode)) { 2611 inode->i_op = &ext4_file_inode_operations; 2612 inode->i_fop = &ext4_file_operations; 2613 ext4_set_aops(inode); 2614 d_tmpfile(dentry, inode); 2615 err = ext4_orphan_add(handle, inode); 2616 if (err) 2617 goto err_unlock_inode; 2618 mark_inode_dirty(inode); 2619 unlock_new_inode(inode); 2620 } 2621 if (handle) 2622 ext4_journal_stop(handle); 2623 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) 2624 goto retry; 2625 return err; 2626 err_unlock_inode: 2627 ext4_journal_stop(handle); 2628 unlock_new_inode(inode); 2629 return err; 2630 } 2631 2632 struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode, 2633 struct ext4_dir_entry_2 *de, 2634 int blocksize, int csum_size, 2635 unsigned int parent_ino, int dotdot_real_len) 2636 { 2637 de->inode = cpu_to_le32(inode->i_ino); 2638 de->name_len = 1; 2639 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len), 2640 blocksize); 2641 strcpy(de->name, "."); 2642 ext4_set_de_type(inode->i_sb, de, S_IFDIR); 2643 2644 de = ext4_next_entry(de, blocksize); 2645 de->inode = cpu_to_le32(parent_ino); 2646 de->name_len = 2; 2647 if (!dotdot_real_len) 2648 de->rec_len = ext4_rec_len_to_disk(blocksize - 2649 (csum_size + EXT4_DIR_REC_LEN(1)), 2650 blocksize); 2651 else 2652 de->rec_len = ext4_rec_len_to_disk( 2653 EXT4_DIR_REC_LEN(de->name_len), blocksize); 2654 strcpy(de->name, ".."); 2655 ext4_set_de_type(inode->i_sb, de, S_IFDIR); 2656 2657 return ext4_next_entry(de, blocksize); 2658 } 2659 2660 static int ext4_init_new_dir(handle_t *handle, struct inode *dir, 2661 struct inode *inode) 2662 { 2663 struct buffer_head *dir_block = NULL; 2664 struct ext4_dir_entry_2 *de; 2665 struct ext4_dir_entry_tail *t; 2666 ext4_lblk_t block = 0; 2667 unsigned int blocksize = dir->i_sb->s_blocksize; 2668 int csum_size = 0; 2669 int err; 2670 2671 if (ext4_has_metadata_csum(dir->i_sb)) 2672 csum_size = sizeof(struct ext4_dir_entry_tail); 2673 2674 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { 2675 err = ext4_try_create_inline_dir(handle, dir, inode); 2676 if (err < 0 && err != -ENOSPC) 2677 goto out; 2678 if (!err) 2679 goto out; 2680 } 2681 2682 inode->i_size = 0; 2683 dir_block = ext4_append(handle, inode, &block); 2684 if (IS_ERR(dir_block)) 2685 return PTR_ERR(dir_block); 2686 de = (struct ext4_dir_entry_2 *)dir_block->b_data; 2687 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0); 2688 set_nlink(inode, 2); 2689 if (csum_size) { 2690 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize); 2691 initialize_dirent_tail(t, blocksize); 2692 } 2693 2694 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata"); 2695 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block); 2696 if (err) 2697 goto out; 2698 set_buffer_verified(dir_block); 2699 out: 2700 brelse(dir_block); 2701 return err; 2702 } 2703 2704 static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 2705 { 2706 handle_t *handle; 2707 struct inode *inode; 2708 int err, credits, retries = 0; 2709 2710 if (EXT4_DIR_LINK_MAX(dir)) 2711 return -EMLINK; 2712 2713 err = dquot_initialize(dir); 2714 if (err) 2715 return err; 2716 2717 credits = (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + 2718 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3); 2719 retry: 2720 inode = ext4_new_inode_start_handle(dir, S_IFDIR | mode, 2721 &dentry->d_name, 2722 0, NULL, EXT4_HT_DIR, credits); 2723 handle = ext4_journal_current_handle(); 2724 err = PTR_ERR(inode); 2725 if (IS_ERR(inode)) 2726 goto out_stop; 2727 2728 inode->i_op = &ext4_dir_inode_operations; 2729 inode->i_fop = &ext4_dir_operations; 2730 err = ext4_init_new_dir(handle, dir, inode); 2731 if (err) 2732 goto out_clear_inode; 2733 err = ext4_mark_inode_dirty(handle, inode); 2734 if (!err) 2735 err = ext4_add_entry(handle, dentry, inode); 2736 if (err) { 2737 out_clear_inode: 2738 clear_nlink(inode); 2739 unlock_new_inode(inode); 2740 ext4_mark_inode_dirty(handle, inode); 2741 iput(inode); 2742 goto out_stop; 2743 } 2744 ext4_inc_count(handle, dir); 2745 ext4_update_dx_flag(dir); 2746 err = ext4_mark_inode_dirty(handle, dir); 2747 if (err) 2748 goto out_clear_inode; 2749 d_instantiate_new(dentry, inode); 2750 if (IS_DIRSYNC(dir)) 2751 ext4_handle_sync(handle); 2752 2753 out_stop: 2754 if (handle) 2755 ext4_journal_stop(handle); 2756 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) 2757 goto retry; 2758 return err; 2759 } 2760 2761 /* 2762 * routine to check that the specified directory is empty (for rmdir) 2763 */ 2764 bool ext4_empty_dir(struct inode *inode) 2765 { 2766 unsigned int offset; 2767 struct buffer_head *bh; 2768 struct ext4_dir_entry_2 *de, *de1; 2769 struct super_block *sb; 2770 2771 if (ext4_has_inline_data(inode)) { 2772 int has_inline_data = 1; 2773 int ret; 2774 2775 ret = empty_inline_dir(inode, &has_inline_data); 2776 if (has_inline_data) 2777 return ret; 2778 } 2779 2780 sb = inode->i_sb; 2781 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2)) { 2782 EXT4_ERROR_INODE(inode, "invalid size"); 2783 return true; 2784 } 2785 bh = ext4_read_dirblock(inode, 0, EITHER); 2786 if (IS_ERR(bh)) 2787 return true; 2788 2789 de = (struct ext4_dir_entry_2 *) bh->b_data; 2790 de1 = ext4_next_entry(de, sb->s_blocksize); 2791 if (le32_to_cpu(de->inode) != inode->i_ino || 2792 le32_to_cpu(de1->inode) == 0 || 2793 strcmp(".", de->name) || strcmp("..", de1->name)) { 2794 ext4_warning_inode(inode, "directory missing '.' and/or '..'"); 2795 brelse(bh); 2796 return true; 2797 } 2798 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) + 2799 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize); 2800 de = ext4_next_entry(de1, sb->s_blocksize); 2801 while (offset < inode->i_size) { 2802 if ((void *) de >= (void *) (bh->b_data+sb->s_blocksize)) { 2803 unsigned int lblock; 2804 brelse(bh); 2805 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb); 2806 bh = ext4_read_dirblock(inode, lblock, EITHER); 2807 if (IS_ERR(bh)) 2808 return true; 2809 de = (struct ext4_dir_entry_2 *) bh->b_data; 2810 } 2811 if (ext4_check_dir_entry(inode, NULL, de, bh, 2812 bh->b_data, bh->b_size, offset)) { 2813 de = (struct ext4_dir_entry_2 *)(bh->b_data + 2814 sb->s_blocksize); 2815 offset = (offset | (sb->s_blocksize - 1)) + 1; 2816 continue; 2817 } 2818 if (le32_to_cpu(de->inode)) { 2819 brelse(bh); 2820 return false; 2821 } 2822 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize); 2823 de = ext4_next_entry(de, sb->s_blocksize); 2824 } 2825 brelse(bh); 2826 return true; 2827 } 2828 2829 /* 2830 * ext4_orphan_add() links an unlinked or truncated inode into a list of 2831 * such inodes, starting at the superblock, in case we crash before the 2832 * file is closed/deleted, or in case the inode truncate spans multiple 2833 * transactions and the last transaction is not recovered after a crash. 2834 * 2835 * At filesystem recovery time, we walk this list deleting unlinked 2836 * inodes and truncating linked inodes in ext4_orphan_cleanup(). 2837 * 2838 * Orphan list manipulation functions must be called under i_mutex unless 2839 * we are just creating the inode or deleting it. 2840 */ 2841 int ext4_orphan_add(handle_t *handle, struct inode *inode) 2842 { 2843 struct super_block *sb = inode->i_sb; 2844 struct ext4_sb_info *sbi = EXT4_SB(sb); 2845 struct ext4_iloc iloc; 2846 int err = 0, rc; 2847 bool dirty = false; 2848 2849 if (!sbi->s_journal || is_bad_inode(inode)) 2850 return 0; 2851 2852 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && 2853 !inode_is_locked(inode)); 2854 /* 2855 * Exit early if inode already is on orphan list. This is a big speedup 2856 * since we don't have to contend on the global s_orphan_lock. 2857 */ 2858 if (!list_empty(&EXT4_I(inode)->i_orphan)) 2859 return 0; 2860 2861 /* 2862 * Orphan handling is only valid for files with data blocks 2863 * being truncated, or files being unlinked. Note that we either 2864 * hold i_mutex, or the inode can not be referenced from outside, 2865 * so i_nlink should not be bumped due to race 2866 */ 2867 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 2868 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0); 2869 2870 BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 2871 err = ext4_journal_get_write_access(handle, sbi->s_sbh); 2872 if (err) 2873 goto out; 2874 2875 err = ext4_reserve_inode_write(handle, inode, &iloc); 2876 if (err) 2877 goto out; 2878 2879 mutex_lock(&sbi->s_orphan_lock); 2880 /* 2881 * Due to previous errors inode may be already a part of on-disk 2882 * orphan list. If so skip on-disk list modification. 2883 */ 2884 if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) > 2885 (le32_to_cpu(sbi->s_es->s_inodes_count))) { 2886 /* Insert this inode at the head of the on-disk orphan list */ 2887 NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan); 2888 sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino); 2889 dirty = true; 2890 } 2891 list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan); 2892 mutex_unlock(&sbi->s_orphan_lock); 2893 2894 if (dirty) { 2895 err = ext4_handle_dirty_super(handle, sb); 2896 rc = ext4_mark_iloc_dirty(handle, inode, &iloc); 2897 if (!err) 2898 err = rc; 2899 if (err) { 2900 /* 2901 * We have to remove inode from in-memory list if 2902 * addition to on disk orphan list failed. Stray orphan 2903 * list entries can cause panics at unmount time. 2904 */ 2905 mutex_lock(&sbi->s_orphan_lock); 2906 list_del_init(&EXT4_I(inode)->i_orphan); 2907 mutex_unlock(&sbi->s_orphan_lock); 2908 } 2909 } else 2910 brelse(iloc.bh); 2911 2912 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino); 2913 jbd_debug(4, "orphan inode %lu will point to %d\n", 2914 inode->i_ino, NEXT_ORPHAN(inode)); 2915 out: 2916 ext4_std_error(sb, err); 2917 return err; 2918 } 2919 2920 /* 2921 * ext4_orphan_del() removes an unlinked or truncated inode from the list 2922 * of such inodes stored on disk, because it is finally being cleaned up. 2923 */ 2924 int ext4_orphan_del(handle_t *handle, struct inode *inode) 2925 { 2926 struct list_head *prev; 2927 struct ext4_inode_info *ei = EXT4_I(inode); 2928 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2929 __u32 ino_next; 2930 struct ext4_iloc iloc; 2931 int err = 0; 2932 2933 if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS)) 2934 return 0; 2935 2936 WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) && 2937 !inode_is_locked(inode)); 2938 /* Do this quick check before taking global s_orphan_lock. */ 2939 if (list_empty(&ei->i_orphan)) 2940 return 0; 2941 2942 if (handle) { 2943 /* Grab inode buffer early before taking global s_orphan_lock */ 2944 err = ext4_reserve_inode_write(handle, inode, &iloc); 2945 } 2946 2947 mutex_lock(&sbi->s_orphan_lock); 2948 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino); 2949 2950 prev = ei->i_orphan.prev; 2951 list_del_init(&ei->i_orphan); 2952 2953 /* If we're on an error path, we may not have a valid 2954 * transaction handle with which to update the orphan list on 2955 * disk, but we still need to remove the inode from the linked 2956 * list in memory. */ 2957 if (!handle || err) { 2958 mutex_unlock(&sbi->s_orphan_lock); 2959 goto out_err; 2960 } 2961 2962 ino_next = NEXT_ORPHAN(inode); 2963 if (prev == &sbi->s_orphan) { 2964 jbd_debug(4, "superblock will point to %u\n", ino_next); 2965 BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 2966 err = ext4_journal_get_write_access(handle, sbi->s_sbh); 2967 if (err) { 2968 mutex_unlock(&sbi->s_orphan_lock); 2969 goto out_brelse; 2970 } 2971 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next); 2972 mutex_unlock(&sbi->s_orphan_lock); 2973 err = ext4_handle_dirty_super(handle, inode->i_sb); 2974 } else { 2975 struct ext4_iloc iloc2; 2976 struct inode *i_prev = 2977 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode; 2978 2979 jbd_debug(4, "orphan inode %lu will point to %u\n", 2980 i_prev->i_ino, ino_next); 2981 err = ext4_reserve_inode_write(handle, i_prev, &iloc2); 2982 if (err) { 2983 mutex_unlock(&sbi->s_orphan_lock); 2984 goto out_brelse; 2985 } 2986 NEXT_ORPHAN(i_prev) = ino_next; 2987 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2); 2988 mutex_unlock(&sbi->s_orphan_lock); 2989 } 2990 if (err) 2991 goto out_brelse; 2992 NEXT_ORPHAN(inode) = 0; 2993 err = ext4_mark_iloc_dirty(handle, inode, &iloc); 2994 out_err: 2995 ext4_std_error(inode->i_sb, err); 2996 return err; 2997 2998 out_brelse: 2999 brelse(iloc.bh); 3000 goto out_err; 3001 } 3002 3003 static int ext4_rmdir(struct inode *dir, struct dentry *dentry) 3004 { 3005 int retval; 3006 struct inode *inode; 3007 struct buffer_head *bh; 3008 struct ext4_dir_entry_2 *de; 3009 handle_t *handle = NULL; 3010 3011 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb)))) 3012 return -EIO; 3013 3014 /* Initialize quotas before so that eventual writes go in 3015 * separate transaction */ 3016 retval = dquot_initialize(dir); 3017 if (retval) 3018 return retval; 3019 retval = dquot_initialize(d_inode(dentry)); 3020 if (retval) 3021 return retval; 3022 3023 retval = -ENOENT; 3024 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL); 3025 if (IS_ERR(bh)) 3026 return PTR_ERR(bh); 3027 if (!bh) 3028 goto end_rmdir; 3029 3030 inode = d_inode(dentry); 3031 3032 retval = -EFSCORRUPTED; 3033 if (le32_to_cpu(de->inode) != inode->i_ino) 3034 goto end_rmdir; 3035 3036 retval = -ENOTEMPTY; 3037 if (!ext4_empty_dir(inode)) 3038 goto end_rmdir; 3039 3040 handle = ext4_journal_start(dir, EXT4_HT_DIR, 3041 EXT4_DATA_TRANS_BLOCKS(dir->i_sb)); 3042 if (IS_ERR(handle)) { 3043 retval = PTR_ERR(handle); 3044 handle = NULL; 3045 goto end_rmdir; 3046 } 3047 3048 if (IS_DIRSYNC(dir)) 3049 ext4_handle_sync(handle); 3050 3051 retval = ext4_delete_entry(handle, dir, de, bh); 3052 if (retval) 3053 goto end_rmdir; 3054 if (!EXT4_DIR_LINK_EMPTY(inode)) 3055 ext4_warning_inode(inode, 3056 "empty directory '%.*s' has too many links (%u)", 3057 dentry->d_name.len, dentry->d_name.name, 3058 inode->i_nlink); 3059 inode_inc_iversion(inode); 3060 clear_nlink(inode); 3061 /* There's no need to set i_disksize: the fact that i_nlink is 3062 * zero will ensure that the right thing happens during any 3063 * recovery. */ 3064 inode->i_size = 0; 3065 ext4_orphan_add(handle, inode); 3066 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 3067 ext4_mark_inode_dirty(handle, inode); 3068 ext4_dec_count(handle, dir); 3069 ext4_update_dx_flag(dir); 3070 ext4_mark_inode_dirty(handle, dir); 3071 3072 #ifdef CONFIG_UNICODE 3073 /* VFS negative dentries are incompatible with Encoding and 3074 * Case-insensitiveness. Eventually we'll want avoid 3075 * invalidating the dentries here, alongside with returning the 3076 * negative dentries at ext4_lookup(), when it is better 3077 * supported by the VFS for the CI case. 3078 */ 3079 if (IS_CASEFOLDED(dir)) 3080 d_invalidate(dentry); 3081 #endif 3082 3083 end_rmdir: 3084 brelse(bh); 3085 if (handle) 3086 ext4_journal_stop(handle); 3087 return retval; 3088 } 3089 3090 static int ext4_unlink(struct inode *dir, struct dentry *dentry) 3091 { 3092 int retval; 3093 struct inode *inode; 3094 struct buffer_head *bh; 3095 struct ext4_dir_entry_2 *de; 3096 handle_t *handle = NULL; 3097 3098 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb)))) 3099 return -EIO; 3100 3101 trace_ext4_unlink_enter(dir, dentry); 3102 /* Initialize quotas before so that eventual writes go 3103 * in separate transaction */ 3104 retval = dquot_initialize(dir); 3105 if (retval) 3106 return retval; 3107 retval = dquot_initialize(d_inode(dentry)); 3108 if (retval) 3109 return retval; 3110 3111 retval = -ENOENT; 3112 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL); 3113 if (IS_ERR(bh)) 3114 return PTR_ERR(bh); 3115 if (!bh) 3116 goto end_unlink; 3117 3118 inode = d_inode(dentry); 3119 3120 retval = -EFSCORRUPTED; 3121 if (le32_to_cpu(de->inode) != inode->i_ino) 3122 goto end_unlink; 3123 3124 handle = ext4_journal_start(dir, EXT4_HT_DIR, 3125 EXT4_DATA_TRANS_BLOCKS(dir->i_sb)); 3126 if (IS_ERR(handle)) { 3127 retval = PTR_ERR(handle); 3128 handle = NULL; 3129 goto end_unlink; 3130 } 3131 3132 if (IS_DIRSYNC(dir)) 3133 ext4_handle_sync(handle); 3134 3135 if (inode->i_nlink == 0) { 3136 ext4_warning_inode(inode, "Deleting file '%.*s' with no links", 3137 dentry->d_name.len, dentry->d_name.name); 3138 set_nlink(inode, 1); 3139 } 3140 retval = ext4_delete_entry(handle, dir, de, bh); 3141 if (retval) 3142 goto end_unlink; 3143 dir->i_ctime = dir->i_mtime = current_time(dir); 3144 ext4_update_dx_flag(dir); 3145 ext4_mark_inode_dirty(handle, dir); 3146 drop_nlink(inode); 3147 if (!inode->i_nlink) 3148 ext4_orphan_add(handle, inode); 3149 inode->i_ctime = current_time(inode); 3150 ext4_mark_inode_dirty(handle, inode); 3151 3152 #ifdef CONFIG_UNICODE 3153 /* VFS negative dentries are incompatible with Encoding and 3154 * Case-insensitiveness. Eventually we'll want avoid 3155 * invalidating the dentries here, alongside with returning the 3156 * negative dentries at ext4_lookup(), when it is better 3157 * supported by the VFS for the CI case. 3158 */ 3159 if (IS_CASEFOLDED(dir)) 3160 d_invalidate(dentry); 3161 #endif 3162 3163 end_unlink: 3164 brelse(bh); 3165 if (handle) 3166 ext4_journal_stop(handle); 3167 trace_ext4_unlink_exit(dentry, retval); 3168 return retval; 3169 } 3170 3171 static int ext4_symlink(struct inode *dir, 3172 struct dentry *dentry, const char *symname) 3173 { 3174 handle_t *handle; 3175 struct inode *inode; 3176 int err, len = strlen(symname); 3177 int credits; 3178 struct fscrypt_str disk_link; 3179 3180 if (unlikely(ext4_forced_shutdown(EXT4_SB(dir->i_sb)))) 3181 return -EIO; 3182 3183 err = fscrypt_prepare_symlink(dir, symname, len, dir->i_sb->s_blocksize, 3184 &disk_link); 3185 if (err) 3186 return err; 3187 3188 err = dquot_initialize(dir); 3189 if (err) 3190 return err; 3191 3192 if ((disk_link.len > EXT4_N_BLOCKS * 4)) { 3193 /* 3194 * For non-fast symlinks, we just allocate inode and put it on 3195 * orphan list in the first transaction => we need bitmap, 3196 * group descriptor, sb, inode block, quota blocks, and 3197 * possibly selinux xattr blocks. 3198 */ 3199 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) + 3200 EXT4_XATTR_TRANS_BLOCKS; 3201 } else { 3202 /* 3203 * Fast symlink. We have to add entry to directory 3204 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS), 3205 * allocate new inode (bitmap, group descriptor, inode block, 3206 * quota blocks, sb is already counted in previous macros). 3207 */ 3208 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + 3209 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3; 3210 } 3211 3212 inode = ext4_new_inode_start_handle(dir, S_IFLNK|S_IRWXUGO, 3213 &dentry->d_name, 0, NULL, 3214 EXT4_HT_DIR, credits); 3215 handle = ext4_journal_current_handle(); 3216 if (IS_ERR(inode)) { 3217 if (handle) 3218 ext4_journal_stop(handle); 3219 return PTR_ERR(inode); 3220 } 3221 3222 if (IS_ENCRYPTED(inode)) { 3223 err = fscrypt_encrypt_symlink(inode, symname, len, &disk_link); 3224 if (err) 3225 goto err_drop_inode; 3226 inode->i_op = &ext4_encrypted_symlink_inode_operations; 3227 } 3228 3229 if ((disk_link.len > EXT4_N_BLOCKS * 4)) { 3230 if (!IS_ENCRYPTED(inode)) 3231 inode->i_op = &ext4_symlink_inode_operations; 3232 inode_nohighmem(inode); 3233 ext4_set_aops(inode); 3234 /* 3235 * We cannot call page_symlink() with transaction started 3236 * because it calls into ext4_write_begin() which can wait 3237 * for transaction commit if we are running out of space 3238 * and thus we deadlock. So we have to stop transaction now 3239 * and restart it when symlink contents is written. 3240 * 3241 * To keep fs consistent in case of crash, we have to put inode 3242 * to orphan list in the mean time. 3243 */ 3244 drop_nlink(inode); 3245 err = ext4_orphan_add(handle, inode); 3246 ext4_journal_stop(handle); 3247 handle = NULL; 3248 if (err) 3249 goto err_drop_inode; 3250 err = __page_symlink(inode, disk_link.name, disk_link.len, 1); 3251 if (err) 3252 goto err_drop_inode; 3253 /* 3254 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS 3255 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified 3256 */ 3257 handle = ext4_journal_start(dir, EXT4_HT_DIR, 3258 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + 3259 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1); 3260 if (IS_ERR(handle)) { 3261 err = PTR_ERR(handle); 3262 handle = NULL; 3263 goto err_drop_inode; 3264 } 3265 set_nlink(inode, 1); 3266 err = ext4_orphan_del(handle, inode); 3267 if (err) 3268 goto err_drop_inode; 3269 } else { 3270 /* clear the extent format for fast symlink */ 3271 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS); 3272 if (!IS_ENCRYPTED(inode)) { 3273 inode->i_op = &ext4_fast_symlink_inode_operations; 3274 inode->i_link = (char *)&EXT4_I(inode)->i_data; 3275 } 3276 memcpy((char *)&EXT4_I(inode)->i_data, disk_link.name, 3277 disk_link.len); 3278 inode->i_size = disk_link.len - 1; 3279 } 3280 EXT4_I(inode)->i_disksize = inode->i_size; 3281 err = ext4_add_nondir(handle, dentry, inode); 3282 if (!err && IS_DIRSYNC(dir)) 3283 ext4_handle_sync(handle); 3284 3285 if (handle) 3286 ext4_journal_stop(handle); 3287 goto out_free_encrypted_link; 3288 3289 err_drop_inode: 3290 if (handle) 3291 ext4_journal_stop(handle); 3292 clear_nlink(inode); 3293 unlock_new_inode(inode); 3294 iput(inode); 3295 out_free_encrypted_link: 3296 if (disk_link.name != (unsigned char *)symname) 3297 kfree(disk_link.name); 3298 return err; 3299 } 3300 3301 static int ext4_link(struct dentry *old_dentry, 3302 struct inode *dir, struct dentry *dentry) 3303 { 3304 handle_t *handle; 3305 struct inode *inode = d_inode(old_dentry); 3306 int err, retries = 0; 3307 3308 if (inode->i_nlink >= EXT4_LINK_MAX) 3309 return -EMLINK; 3310 3311 err = fscrypt_prepare_link(old_dentry, dir, dentry); 3312 if (err) 3313 return err; 3314 3315 if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) && 3316 (!projid_eq(EXT4_I(dir)->i_projid, 3317 EXT4_I(old_dentry->d_inode)->i_projid))) 3318 return -EXDEV; 3319 3320 err = dquot_initialize(dir); 3321 if (err) 3322 return err; 3323 3324 retry: 3325 handle = ext4_journal_start(dir, EXT4_HT_DIR, 3326 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + 3327 EXT4_INDEX_EXTRA_TRANS_BLOCKS) + 1); 3328 if (IS_ERR(handle)) 3329 return PTR_ERR(handle); 3330 3331 if (IS_DIRSYNC(dir)) 3332 ext4_handle_sync(handle); 3333 3334 inode->i_ctime = current_time(inode); 3335 ext4_inc_count(handle, inode); 3336 ihold(inode); 3337 3338 err = ext4_add_entry(handle, dentry, inode); 3339 if (!err) { 3340 ext4_mark_inode_dirty(handle, inode); 3341 /* this can happen only for tmpfile being 3342 * linked the first time 3343 */ 3344 if (inode->i_nlink == 1) 3345 ext4_orphan_del(handle, inode); 3346 d_instantiate(dentry, inode); 3347 } else { 3348 drop_nlink(inode); 3349 iput(inode); 3350 } 3351 ext4_journal_stop(handle); 3352 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) 3353 goto retry; 3354 return err; 3355 } 3356 3357 3358 /* 3359 * Try to find buffer head where contains the parent block. 3360 * It should be the inode block if it is inlined or the 1st block 3361 * if it is a normal dir. 3362 */ 3363 static struct buffer_head *ext4_get_first_dir_block(handle_t *handle, 3364 struct inode *inode, 3365 int *retval, 3366 struct ext4_dir_entry_2 **parent_de, 3367 int *inlined) 3368 { 3369 struct buffer_head *bh; 3370 3371 if (!ext4_has_inline_data(inode)) { 3372 bh = ext4_read_dirblock(inode, 0, EITHER); 3373 if (IS_ERR(bh)) { 3374 *retval = PTR_ERR(bh); 3375 return NULL; 3376 } 3377 *parent_de = ext4_next_entry( 3378 (struct ext4_dir_entry_2 *)bh->b_data, 3379 inode->i_sb->s_blocksize); 3380 return bh; 3381 } 3382 3383 *inlined = 1; 3384 return ext4_get_first_inline_block(inode, parent_de, retval); 3385 } 3386 3387 struct ext4_renament { 3388 struct inode *dir; 3389 struct dentry *dentry; 3390 struct inode *inode; 3391 bool is_dir; 3392 int dir_nlink_delta; 3393 3394 /* entry for "dentry" */ 3395 struct buffer_head *bh; 3396 struct ext4_dir_entry_2 *de; 3397 int inlined; 3398 3399 /* entry for ".." in inode if it's a directory */ 3400 struct buffer_head *dir_bh; 3401 struct ext4_dir_entry_2 *parent_de; 3402 int dir_inlined; 3403 }; 3404 3405 static int ext4_rename_dir_prepare(handle_t *handle, struct ext4_renament *ent) 3406 { 3407 int retval; 3408 3409 ent->dir_bh = ext4_get_first_dir_block(handle, ent->inode, 3410 &retval, &ent->parent_de, 3411 &ent->dir_inlined); 3412 if (!ent->dir_bh) 3413 return retval; 3414 if (le32_to_cpu(ent->parent_de->inode) != ent->dir->i_ino) 3415 return -EFSCORRUPTED; 3416 BUFFER_TRACE(ent->dir_bh, "get_write_access"); 3417 return ext4_journal_get_write_access(handle, ent->dir_bh); 3418 } 3419 3420 static int ext4_rename_dir_finish(handle_t *handle, struct ext4_renament *ent, 3421 unsigned dir_ino) 3422 { 3423 int retval; 3424 3425 ent->parent_de->inode = cpu_to_le32(dir_ino); 3426 BUFFER_TRACE(ent->dir_bh, "call ext4_handle_dirty_metadata"); 3427 if (!ent->dir_inlined) { 3428 if (is_dx(ent->inode)) { 3429 retval = ext4_handle_dirty_dx_node(handle, 3430 ent->inode, 3431 ent->dir_bh); 3432 } else { 3433 retval = ext4_handle_dirty_dirent_node(handle, 3434 ent->inode, 3435 ent->dir_bh); 3436 } 3437 } else { 3438 retval = ext4_mark_inode_dirty(handle, ent->inode); 3439 } 3440 if (retval) { 3441 ext4_std_error(ent->dir->i_sb, retval); 3442 return retval; 3443 } 3444 return 0; 3445 } 3446 3447 static int ext4_setent(handle_t *handle, struct ext4_renament *ent, 3448 unsigned ino, unsigned file_type) 3449 { 3450 int retval; 3451 3452 BUFFER_TRACE(ent->bh, "get write access"); 3453 retval = ext4_journal_get_write_access(handle, ent->bh); 3454 if (retval) 3455 return retval; 3456 ent->de->inode = cpu_to_le32(ino); 3457 if (ext4_has_feature_filetype(ent->dir->i_sb)) 3458 ent->de->file_type = file_type; 3459 inode_inc_iversion(ent->dir); 3460 ent->dir->i_ctime = ent->dir->i_mtime = 3461 current_time(ent->dir); 3462 ext4_mark_inode_dirty(handle, ent->dir); 3463 BUFFER_TRACE(ent->bh, "call ext4_handle_dirty_metadata"); 3464 if (!ent->inlined) { 3465 retval = ext4_handle_dirty_dirent_node(handle, 3466 ent->dir, ent->bh); 3467 if (unlikely(retval)) { 3468 ext4_std_error(ent->dir->i_sb, retval); 3469 return retval; 3470 } 3471 } 3472 brelse(ent->bh); 3473 ent->bh = NULL; 3474 3475 return 0; 3476 } 3477 3478 static int ext4_find_delete_entry(handle_t *handle, struct inode *dir, 3479 const struct qstr *d_name) 3480 { 3481 int retval = -ENOENT; 3482 struct buffer_head *bh; 3483 struct ext4_dir_entry_2 *de; 3484 3485 bh = ext4_find_entry(dir, d_name, &de, NULL); 3486 if (IS_ERR(bh)) 3487 return PTR_ERR(bh); 3488 if (bh) { 3489 retval = ext4_delete_entry(handle, dir, de, bh); 3490 brelse(bh); 3491 } 3492 return retval; 3493 } 3494 3495 static void ext4_rename_delete(handle_t *handle, struct ext4_renament *ent, 3496 int force_reread) 3497 { 3498 int retval; 3499 /* 3500 * ent->de could have moved from under us during htree split, so make 3501 * sure that we are deleting the right entry. We might also be pointing 3502 * to a stale entry in the unused part of ent->bh so just checking inum 3503 * and the name isn't enough. 3504 */ 3505 if (le32_to_cpu(ent->de->inode) != ent->inode->i_ino || 3506 ent->de->name_len != ent->dentry->d_name.len || 3507 strncmp(ent->de->name, ent->dentry->d_name.name, 3508 ent->de->name_len) || 3509 force_reread) { 3510 retval = ext4_find_delete_entry(handle, ent->dir, 3511 &ent->dentry->d_name); 3512 } else { 3513 retval = ext4_delete_entry(handle, ent->dir, ent->de, ent->bh); 3514 if (retval == -ENOENT) { 3515 retval = ext4_find_delete_entry(handle, ent->dir, 3516 &ent->dentry->d_name); 3517 } 3518 } 3519 3520 if (retval) { 3521 ext4_warning_inode(ent->dir, 3522 "Deleting old file: nlink %d, error=%d", 3523 ent->dir->i_nlink, retval); 3524 } 3525 } 3526 3527 static void ext4_update_dir_count(handle_t *handle, struct ext4_renament *ent) 3528 { 3529 if (ent->dir_nlink_delta) { 3530 if (ent->dir_nlink_delta == -1) 3531 ext4_dec_count(handle, ent->dir); 3532 else 3533 ext4_inc_count(handle, ent->dir); 3534 ext4_mark_inode_dirty(handle, ent->dir); 3535 } 3536 } 3537 3538 static struct inode *ext4_whiteout_for_rename(struct ext4_renament *ent, 3539 int credits, handle_t **h) 3540 { 3541 struct inode *wh; 3542 handle_t *handle; 3543 int retries = 0; 3544 3545 /* 3546 * for inode block, sb block, group summaries, 3547 * and inode bitmap 3548 */ 3549 credits += (EXT4_MAXQUOTAS_TRANS_BLOCKS(ent->dir->i_sb) + 3550 EXT4_XATTR_TRANS_BLOCKS + 4); 3551 retry: 3552 wh = ext4_new_inode_start_handle(ent->dir, S_IFCHR | WHITEOUT_MODE, 3553 &ent->dentry->d_name, 0, NULL, 3554 EXT4_HT_DIR, credits); 3555 3556 handle = ext4_journal_current_handle(); 3557 if (IS_ERR(wh)) { 3558 if (handle) 3559 ext4_journal_stop(handle); 3560 if (PTR_ERR(wh) == -ENOSPC && 3561 ext4_should_retry_alloc(ent->dir->i_sb, &retries)) 3562 goto retry; 3563 } else { 3564 *h = handle; 3565 init_special_inode(wh, wh->i_mode, WHITEOUT_DEV); 3566 wh->i_op = &ext4_special_inode_operations; 3567 } 3568 return wh; 3569 } 3570 3571 /* 3572 * Anybody can rename anything with this: the permission checks are left to the 3573 * higher-level routines. 3574 * 3575 * n.b. old_{dentry,inode) refers to the source dentry/inode 3576 * while new_{dentry,inode) refers to the destination dentry/inode 3577 * This comes from rename(const char *oldpath, const char *newpath) 3578 */ 3579 static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry, 3580 struct inode *new_dir, struct dentry *new_dentry, 3581 unsigned int flags) 3582 { 3583 handle_t *handle = NULL; 3584 struct ext4_renament old = { 3585 .dir = old_dir, 3586 .dentry = old_dentry, 3587 .inode = d_inode(old_dentry), 3588 }; 3589 struct ext4_renament new = { 3590 .dir = new_dir, 3591 .dentry = new_dentry, 3592 .inode = d_inode(new_dentry), 3593 }; 3594 int force_reread; 3595 int retval; 3596 struct inode *whiteout = NULL; 3597 int credits; 3598 u8 old_file_type; 3599 3600 if (new.inode && new.inode->i_nlink == 0) { 3601 EXT4_ERROR_INODE(new.inode, 3602 "target of rename is already freed"); 3603 return -EFSCORRUPTED; 3604 } 3605 3606 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT)) && 3607 (!projid_eq(EXT4_I(new_dir)->i_projid, 3608 EXT4_I(old_dentry->d_inode)->i_projid))) 3609 return -EXDEV; 3610 3611 retval = dquot_initialize(old.dir); 3612 if (retval) 3613 return retval; 3614 retval = dquot_initialize(new.dir); 3615 if (retval) 3616 return retval; 3617 3618 /* Initialize quotas before so that eventual writes go 3619 * in separate transaction */ 3620 if (new.inode) { 3621 retval = dquot_initialize(new.inode); 3622 if (retval) 3623 return retval; 3624 } 3625 3626 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, &old.de, NULL); 3627 if (IS_ERR(old.bh)) 3628 return PTR_ERR(old.bh); 3629 /* 3630 * Check for inode number is _not_ due to possible IO errors. 3631 * We might rmdir the source, keep it as pwd of some process 3632 * and merrily kill the link to whatever was created under the 3633 * same name. Goodbye sticky bit ;-< 3634 */ 3635 retval = -ENOENT; 3636 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino) 3637 goto end_rename; 3638 3639 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name, 3640 &new.de, &new.inlined); 3641 if (IS_ERR(new.bh)) { 3642 retval = PTR_ERR(new.bh); 3643 new.bh = NULL; 3644 goto end_rename; 3645 } 3646 if (new.bh) { 3647 if (!new.inode) { 3648 brelse(new.bh); 3649 new.bh = NULL; 3650 } 3651 } 3652 if (new.inode && !test_opt(new.dir->i_sb, NO_AUTO_DA_ALLOC)) 3653 ext4_alloc_da_blocks(old.inode); 3654 3655 credits = (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) + 3656 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2); 3657 if (!(flags & RENAME_WHITEOUT)) { 3658 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, credits); 3659 if (IS_ERR(handle)) { 3660 retval = PTR_ERR(handle); 3661 handle = NULL; 3662 goto end_rename; 3663 } 3664 } else { 3665 whiteout = ext4_whiteout_for_rename(&old, credits, &handle); 3666 if (IS_ERR(whiteout)) { 3667 retval = PTR_ERR(whiteout); 3668 whiteout = NULL; 3669 goto end_rename; 3670 } 3671 } 3672 3673 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir)) 3674 ext4_handle_sync(handle); 3675 3676 if (S_ISDIR(old.inode->i_mode)) { 3677 if (new.inode) { 3678 retval = -ENOTEMPTY; 3679 if (!ext4_empty_dir(new.inode)) 3680 goto end_rename; 3681 } else { 3682 retval = -EMLINK; 3683 if (new.dir != old.dir && EXT4_DIR_LINK_MAX(new.dir)) 3684 goto end_rename; 3685 } 3686 retval = ext4_rename_dir_prepare(handle, &old); 3687 if (retval) 3688 goto end_rename; 3689 } 3690 /* 3691 * If we're renaming a file within an inline_data dir and adding or 3692 * setting the new dirent causes a conversion from inline_data to 3693 * extents/blockmap, we need to force the dirent delete code to 3694 * re-read the directory, or else we end up trying to delete a dirent 3695 * from what is now the extent tree root (or a block map). 3696 */ 3697 force_reread = (new.dir->i_ino == old.dir->i_ino && 3698 ext4_test_inode_flag(new.dir, EXT4_INODE_INLINE_DATA)); 3699 3700 old_file_type = old.de->file_type; 3701 if (whiteout) { 3702 /* 3703 * Do this before adding a new entry, so the old entry is sure 3704 * to be still pointing to the valid old entry. 3705 */ 3706 retval = ext4_setent(handle, &old, whiteout->i_ino, 3707 EXT4_FT_CHRDEV); 3708 if (retval) 3709 goto end_rename; 3710 ext4_mark_inode_dirty(handle, whiteout); 3711 } 3712 if (!new.bh) { 3713 retval = ext4_add_entry(handle, new.dentry, old.inode); 3714 if (retval) 3715 goto end_rename; 3716 } else { 3717 retval = ext4_setent(handle, &new, 3718 old.inode->i_ino, old_file_type); 3719 if (retval) 3720 goto end_rename; 3721 } 3722 if (force_reread) 3723 force_reread = !ext4_test_inode_flag(new.dir, 3724 EXT4_INODE_INLINE_DATA); 3725 3726 /* 3727 * Like most other Unix systems, set the ctime for inodes on a 3728 * rename. 3729 */ 3730 old.inode->i_ctime = current_time(old.inode); 3731 ext4_mark_inode_dirty(handle, old.inode); 3732 3733 if (!whiteout) { 3734 /* 3735 * ok, that's it 3736 */ 3737 ext4_rename_delete(handle, &old, force_reread); 3738 } 3739 3740 if (new.inode) { 3741 ext4_dec_count(handle, new.inode); 3742 new.inode->i_ctime = current_time(new.inode); 3743 } 3744 old.dir->i_ctime = old.dir->i_mtime = current_time(old.dir); 3745 ext4_update_dx_flag(old.dir); 3746 if (old.dir_bh) { 3747 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino); 3748 if (retval) 3749 goto end_rename; 3750 3751 ext4_dec_count(handle, old.dir); 3752 if (new.inode) { 3753 /* checked ext4_empty_dir above, can't have another 3754 * parent, ext4_dec_count() won't work for many-linked 3755 * dirs */ 3756 clear_nlink(new.inode); 3757 } else { 3758 ext4_inc_count(handle, new.dir); 3759 ext4_update_dx_flag(new.dir); 3760 ext4_mark_inode_dirty(handle, new.dir); 3761 } 3762 } 3763 ext4_mark_inode_dirty(handle, old.dir); 3764 if (new.inode) { 3765 ext4_mark_inode_dirty(handle, new.inode); 3766 if (!new.inode->i_nlink) 3767 ext4_orphan_add(handle, new.inode); 3768 } 3769 retval = 0; 3770 3771 end_rename: 3772 brelse(old.dir_bh); 3773 brelse(old.bh); 3774 brelse(new.bh); 3775 if (whiteout) { 3776 if (retval) 3777 drop_nlink(whiteout); 3778 unlock_new_inode(whiteout); 3779 iput(whiteout); 3780 } 3781 if (handle) 3782 ext4_journal_stop(handle); 3783 return retval; 3784 } 3785 3786 static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry, 3787 struct inode *new_dir, struct dentry *new_dentry) 3788 { 3789 handle_t *handle = NULL; 3790 struct ext4_renament old = { 3791 .dir = old_dir, 3792 .dentry = old_dentry, 3793 .inode = d_inode(old_dentry), 3794 }; 3795 struct ext4_renament new = { 3796 .dir = new_dir, 3797 .dentry = new_dentry, 3798 .inode = d_inode(new_dentry), 3799 }; 3800 u8 new_file_type; 3801 int retval; 3802 struct timespec64 ctime; 3803 3804 if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) && 3805 !projid_eq(EXT4_I(new_dir)->i_projid, 3806 EXT4_I(old_dentry->d_inode)->i_projid)) || 3807 (ext4_test_inode_flag(old_dir, EXT4_INODE_PROJINHERIT) && 3808 !projid_eq(EXT4_I(old_dir)->i_projid, 3809 EXT4_I(new_dentry->d_inode)->i_projid))) 3810 return -EXDEV; 3811 3812 retval = dquot_initialize(old.dir); 3813 if (retval) 3814 return retval; 3815 retval = dquot_initialize(new.dir); 3816 if (retval) 3817 return retval; 3818 3819 old.bh = ext4_find_entry(old.dir, &old.dentry->d_name, 3820 &old.de, &old.inlined); 3821 if (IS_ERR(old.bh)) 3822 return PTR_ERR(old.bh); 3823 /* 3824 * Check for inode number is _not_ due to possible IO errors. 3825 * We might rmdir the source, keep it as pwd of some process 3826 * and merrily kill the link to whatever was created under the 3827 * same name. Goodbye sticky bit ;-< 3828 */ 3829 retval = -ENOENT; 3830 if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino) 3831 goto end_rename; 3832 3833 new.bh = ext4_find_entry(new.dir, &new.dentry->d_name, 3834 &new.de, &new.inlined); 3835 if (IS_ERR(new.bh)) { 3836 retval = PTR_ERR(new.bh); 3837 new.bh = NULL; 3838 goto end_rename; 3839 } 3840 3841 /* RENAME_EXCHANGE case: old *and* new must both exist */ 3842 if (!new.bh || le32_to_cpu(new.de->inode) != new.inode->i_ino) 3843 goto end_rename; 3844 3845 handle = ext4_journal_start(old.dir, EXT4_HT_DIR, 3846 (2 * EXT4_DATA_TRANS_BLOCKS(old.dir->i_sb) + 3847 2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2)); 3848 if (IS_ERR(handle)) { 3849 retval = PTR_ERR(handle); 3850 handle = NULL; 3851 goto end_rename; 3852 } 3853 3854 if (IS_DIRSYNC(old.dir) || IS_DIRSYNC(new.dir)) 3855 ext4_handle_sync(handle); 3856 3857 if (S_ISDIR(old.inode->i_mode)) { 3858 old.is_dir = true; 3859 retval = ext4_rename_dir_prepare(handle, &old); 3860 if (retval) 3861 goto end_rename; 3862 } 3863 if (S_ISDIR(new.inode->i_mode)) { 3864 new.is_dir = true; 3865 retval = ext4_rename_dir_prepare(handle, &new); 3866 if (retval) 3867 goto end_rename; 3868 } 3869 3870 /* 3871 * Other than the special case of overwriting a directory, parents' 3872 * nlink only needs to be modified if this is a cross directory rename. 3873 */ 3874 if (old.dir != new.dir && old.is_dir != new.is_dir) { 3875 old.dir_nlink_delta = old.is_dir ? -1 : 1; 3876 new.dir_nlink_delta = -old.dir_nlink_delta; 3877 retval = -EMLINK; 3878 if ((old.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(old.dir)) || 3879 (new.dir_nlink_delta > 0 && EXT4_DIR_LINK_MAX(new.dir))) 3880 goto end_rename; 3881 } 3882 3883 new_file_type = new.de->file_type; 3884 retval = ext4_setent(handle, &new, old.inode->i_ino, old.de->file_type); 3885 if (retval) 3886 goto end_rename; 3887 3888 retval = ext4_setent(handle, &old, new.inode->i_ino, new_file_type); 3889 if (retval) 3890 goto end_rename; 3891 3892 /* 3893 * Like most other Unix systems, set the ctime for inodes on a 3894 * rename. 3895 */ 3896 ctime = current_time(old.inode); 3897 old.inode->i_ctime = ctime; 3898 new.inode->i_ctime = ctime; 3899 ext4_mark_inode_dirty(handle, old.inode); 3900 ext4_mark_inode_dirty(handle, new.inode); 3901 3902 if (old.dir_bh) { 3903 retval = ext4_rename_dir_finish(handle, &old, new.dir->i_ino); 3904 if (retval) 3905 goto end_rename; 3906 } 3907 if (new.dir_bh) { 3908 retval = ext4_rename_dir_finish(handle, &new, old.dir->i_ino); 3909 if (retval) 3910 goto end_rename; 3911 } 3912 ext4_update_dir_count(handle, &old); 3913 ext4_update_dir_count(handle, &new); 3914 retval = 0; 3915 3916 end_rename: 3917 brelse(old.dir_bh); 3918 brelse(new.dir_bh); 3919 brelse(old.bh); 3920 brelse(new.bh); 3921 if (handle) 3922 ext4_journal_stop(handle); 3923 return retval; 3924 } 3925 3926 static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry, 3927 struct inode *new_dir, struct dentry *new_dentry, 3928 unsigned int flags) 3929 { 3930 int err; 3931 3932 if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb)))) 3933 return -EIO; 3934 3935 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 3936 return -EINVAL; 3937 3938 err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry, 3939 flags); 3940 if (err) 3941 return err; 3942 3943 if (flags & RENAME_EXCHANGE) { 3944 return ext4_cross_rename(old_dir, old_dentry, 3945 new_dir, new_dentry); 3946 } 3947 3948 return ext4_rename(old_dir, old_dentry, new_dir, new_dentry, flags); 3949 } 3950 3951 /* 3952 * directories can handle most operations... 3953 */ 3954 const struct inode_operations ext4_dir_inode_operations = { 3955 .create = ext4_create, 3956 .lookup = ext4_lookup, 3957 .link = ext4_link, 3958 .unlink = ext4_unlink, 3959 .symlink = ext4_symlink, 3960 .mkdir = ext4_mkdir, 3961 .rmdir = ext4_rmdir, 3962 .mknod = ext4_mknod, 3963 .tmpfile = ext4_tmpfile, 3964 .rename = ext4_rename2, 3965 .setattr = ext4_setattr, 3966 .getattr = ext4_getattr, 3967 .listxattr = ext4_listxattr, 3968 .get_acl = ext4_get_acl, 3969 .set_acl = ext4_set_acl, 3970 .fiemap = ext4_fiemap, 3971 }; 3972 3973 const struct inode_operations ext4_special_inode_operations = { 3974 .setattr = ext4_setattr, 3975 .getattr = ext4_getattr, 3976 .listxattr = ext4_listxattr, 3977 .get_acl = ext4_get_acl, 3978 .set_acl = ext4_set_acl, 3979 }; 3980