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