1 /* 2 * linux/fs/ext4/namei.c 3 * 4 * Copyright (C) 1992, 1993, 1994, 1995 5 * Remy Card (card@masi.ibp.fr) 6 * Laboratoire MASI - Institut Blaise Pascal 7 * Universite Pierre et Marie Curie (Paris VI) 8 * 9 * from 10 * 11 * linux/fs/minix/namei.c 12 * 13 * Copyright (C) 1991, 1992 Linus Torvalds 14 * 15 * Big-endian to little-endian byte-swapping/bitmaps by 16 * David S. Miller (davem@caip.rutgers.edu), 1995 17 * Directory entry file type support and forward compatibility hooks 18 * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998 19 * Hash Tree Directory indexing (c) 20 * Daniel Phillips, 2001 21 * Hash Tree Directory indexing porting 22 * Christopher Li, 2002 23 * Hash Tree Directory indexing cleanup 24 * Theodore Ts'o, 2002 25 */ 26 27 #include <linux/fs.h> 28 #include <linux/pagemap.h> 29 #include <linux/jbd2.h> 30 #include <linux/time.h> 31 #include <linux/ext4_fs.h> 32 #include <linux/ext4_jbd2.h> 33 #include <linux/fcntl.h> 34 #include <linux/stat.h> 35 #include <linux/string.h> 36 #include <linux/quotaops.h> 37 #include <linux/buffer_head.h> 38 #include <linux/bio.h> 39 40 #include "namei.h" 41 #include "xattr.h" 42 #include "acl.h" 43 44 /* 45 * define how far ahead to read directories while searching them. 46 */ 47 #define NAMEI_RA_CHUNKS 2 48 #define NAMEI_RA_BLOCKS 4 49 #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) 50 #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b)) 51 52 static struct buffer_head *ext4_append(handle_t *handle, 53 struct inode *inode, 54 u32 *block, int *err) 55 { 56 struct buffer_head *bh; 57 58 *block = inode->i_size >> inode->i_sb->s_blocksize_bits; 59 60 if ((bh = ext4_bread(handle, inode, *block, 1, err))) { 61 inode->i_size += inode->i_sb->s_blocksize; 62 EXT4_I(inode)->i_disksize = inode->i_size; 63 ext4_journal_get_write_access(handle,bh); 64 } 65 return bh; 66 } 67 68 #ifndef assert 69 #define assert(test) J_ASSERT(test) 70 #endif 71 72 #ifndef swap 73 #define swap(x, y) do { typeof(x) z = x; x = y; y = z; } while (0) 74 #endif 75 76 #ifdef DX_DEBUG 77 #define dxtrace(command) command 78 #else 79 #define dxtrace(command) 80 #endif 81 82 struct fake_dirent 83 { 84 __le32 inode; 85 __le16 rec_len; 86 u8 name_len; 87 u8 file_type; 88 }; 89 90 struct dx_countlimit 91 { 92 __le16 limit; 93 __le16 count; 94 }; 95 96 struct dx_entry 97 { 98 __le32 hash; 99 __le32 block; 100 }; 101 102 /* 103 * dx_root_info is laid out so that if it should somehow get overlaid by a 104 * dirent the two low bits of the hash version will be zero. Therefore, the 105 * hash version mod 4 should never be 0. Sincerely, the paranoia department. 106 */ 107 108 struct dx_root 109 { 110 struct fake_dirent dot; 111 char dot_name[4]; 112 struct fake_dirent dotdot; 113 char dotdot_name[4]; 114 struct dx_root_info 115 { 116 __le32 reserved_zero; 117 u8 hash_version; 118 u8 info_length; /* 8 */ 119 u8 indirect_levels; 120 u8 unused_flags; 121 } 122 info; 123 struct dx_entry entries[0]; 124 }; 125 126 struct dx_node 127 { 128 struct fake_dirent fake; 129 struct dx_entry entries[0]; 130 }; 131 132 133 struct dx_frame 134 { 135 struct buffer_head *bh; 136 struct dx_entry *entries; 137 struct dx_entry *at; 138 }; 139 140 struct dx_map_entry 141 { 142 u32 hash; 143 u16 offs; 144 u16 size; 145 }; 146 147 #ifdef CONFIG_EXT4_INDEX 148 static inline unsigned dx_get_block (struct dx_entry *entry); 149 static void dx_set_block (struct dx_entry *entry, unsigned value); 150 static inline unsigned dx_get_hash (struct dx_entry *entry); 151 static void dx_set_hash (struct dx_entry *entry, unsigned value); 152 static unsigned dx_get_count (struct dx_entry *entries); 153 static unsigned dx_get_limit (struct dx_entry *entries); 154 static void dx_set_count (struct dx_entry *entries, unsigned value); 155 static void dx_set_limit (struct dx_entry *entries, unsigned value); 156 static unsigned dx_root_limit (struct inode *dir, unsigned infosize); 157 static unsigned dx_node_limit (struct inode *dir); 158 static struct dx_frame *dx_probe(struct dentry *dentry, 159 struct inode *dir, 160 struct dx_hash_info *hinfo, 161 struct dx_frame *frame, 162 int *err); 163 static void dx_release (struct dx_frame *frames); 164 static int dx_make_map (struct ext4_dir_entry_2 *de, int size, 165 struct dx_hash_info *hinfo, struct dx_map_entry map[]); 166 static void dx_sort_map(struct dx_map_entry *map, unsigned count); 167 static struct ext4_dir_entry_2 *dx_move_dirents (char *from, char *to, 168 struct dx_map_entry *offsets, int count); 169 static struct ext4_dir_entry_2* dx_pack_dirents (char *base, int size); 170 static void dx_insert_block (struct dx_frame *frame, u32 hash, u32 block); 171 static int ext4_htree_next_block(struct inode *dir, __u32 hash, 172 struct dx_frame *frame, 173 struct dx_frame *frames, 174 __u32 *start_hash); 175 static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry, 176 struct ext4_dir_entry_2 **res_dir, int *err); 177 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry, 178 struct inode *inode); 179 180 /* 181 * Future: use high four bits of block for coalesce-on-delete flags 182 * Mask them off for now. 183 */ 184 185 static inline unsigned dx_get_block (struct dx_entry *entry) 186 { 187 return le32_to_cpu(entry->block) & 0x00ffffff; 188 } 189 190 static inline void dx_set_block (struct dx_entry *entry, unsigned value) 191 { 192 entry->block = cpu_to_le32(value); 193 } 194 195 static inline unsigned dx_get_hash (struct dx_entry *entry) 196 { 197 return le32_to_cpu(entry->hash); 198 } 199 200 static inline void dx_set_hash (struct dx_entry *entry, unsigned value) 201 { 202 entry->hash = cpu_to_le32(value); 203 } 204 205 static inline unsigned dx_get_count (struct dx_entry *entries) 206 { 207 return le16_to_cpu(((struct dx_countlimit *) entries)->count); 208 } 209 210 static inline unsigned dx_get_limit (struct dx_entry *entries) 211 { 212 return le16_to_cpu(((struct dx_countlimit *) entries)->limit); 213 } 214 215 static inline void dx_set_count (struct dx_entry *entries, unsigned value) 216 { 217 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value); 218 } 219 220 static inline void dx_set_limit (struct dx_entry *entries, unsigned value) 221 { 222 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value); 223 } 224 225 static inline unsigned dx_root_limit (struct inode *dir, unsigned infosize) 226 { 227 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) - 228 EXT4_DIR_REC_LEN(2) - infosize; 229 return 0? 20: entry_space / sizeof(struct dx_entry); 230 } 231 232 static inline unsigned dx_node_limit (struct inode *dir) 233 { 234 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0); 235 return 0? 22: entry_space / sizeof(struct dx_entry); 236 } 237 238 /* 239 * Debug 240 */ 241 #ifdef DX_DEBUG 242 static void dx_show_index (char * label, struct dx_entry *entries) 243 { 244 int i, n = dx_get_count (entries); 245 printk("%s index ", label); 246 for (i = 0; i < n; i++) { 247 printk("%x->%u ", i? dx_get_hash(entries + i) : 248 0, dx_get_block(entries + i)); 249 } 250 printk("\n"); 251 } 252 253 struct stats 254 { 255 unsigned names; 256 unsigned space; 257 unsigned bcount; 258 }; 259 260 static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de, 261 int size, int show_names) 262 { 263 unsigned names = 0, space = 0; 264 char *base = (char *) de; 265 struct dx_hash_info h = *hinfo; 266 267 printk("names: "); 268 while ((char *) de < base + size) 269 { 270 if (de->inode) 271 { 272 if (show_names) 273 { 274 int len = de->name_len; 275 char *name = de->name; 276 while (len--) printk("%c", *name++); 277 ext4fs_dirhash(de->name, de->name_len, &h); 278 printk(":%x.%u ", h.hash, 279 ((char *) de - base)); 280 } 281 space += EXT4_DIR_REC_LEN(de->name_len); 282 names++; 283 } 284 de = (struct ext4_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len)); 285 } 286 printk("(%i)\n", names); 287 return (struct stats) { names, space, 1 }; 288 } 289 290 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir, 291 struct dx_entry *entries, int levels) 292 { 293 unsigned blocksize = dir->i_sb->s_blocksize; 294 unsigned count = dx_get_count (entries), names = 0, space = 0, i; 295 unsigned bcount = 0; 296 struct buffer_head *bh; 297 int err; 298 printk("%i indexed blocks...\n", count); 299 for (i = 0; i < count; i++, entries++) 300 { 301 u32 block = dx_get_block(entries), hash = i? dx_get_hash(entries): 0; 302 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash; 303 struct stats stats; 304 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range); 305 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue; 306 stats = levels? 307 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1): 308 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0); 309 names += stats.names; 310 space += stats.space; 311 bcount += stats.bcount; 312 brelse (bh); 313 } 314 if (bcount) 315 printk("%snames %u, fullness %u (%u%%)\n", levels?"":" ", 316 names, space/bcount,(space/bcount)*100/blocksize); 317 return (struct stats) { names, space, bcount}; 318 } 319 #endif /* DX_DEBUG */ 320 321 /* 322 * Probe for a directory leaf block to search. 323 * 324 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format 325 * error in the directory index, and the caller should fall back to 326 * searching the directory normally. The callers of dx_probe **MUST** 327 * check for this error code, and make sure it never gets reflected 328 * back to userspace. 329 */ 330 static struct dx_frame * 331 dx_probe(struct dentry *dentry, struct inode *dir, 332 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err) 333 { 334 unsigned count, indirect; 335 struct dx_entry *at, *entries, *p, *q, *m; 336 struct dx_root *root; 337 struct buffer_head *bh; 338 struct dx_frame *frame = frame_in; 339 u32 hash; 340 341 frame->bh = NULL; 342 if (dentry) 343 dir = dentry->d_parent->d_inode; 344 if (!(bh = ext4_bread (NULL,dir, 0, 0, err))) 345 goto fail; 346 root = (struct dx_root *) bh->b_data; 347 if (root->info.hash_version != DX_HASH_TEA && 348 root->info.hash_version != DX_HASH_HALF_MD4 && 349 root->info.hash_version != DX_HASH_LEGACY) { 350 ext4_warning(dir->i_sb, __FUNCTION__, 351 "Unrecognised inode hash code %d", 352 root->info.hash_version); 353 brelse(bh); 354 *err = ERR_BAD_DX_DIR; 355 goto fail; 356 } 357 hinfo->hash_version = root->info.hash_version; 358 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed; 359 if (dentry) 360 ext4fs_dirhash(dentry->d_name.name, dentry->d_name.len, hinfo); 361 hash = hinfo->hash; 362 363 if (root->info.unused_flags & 1) { 364 ext4_warning(dir->i_sb, __FUNCTION__, 365 "Unimplemented inode hash flags: %#06x", 366 root->info.unused_flags); 367 brelse(bh); 368 *err = ERR_BAD_DX_DIR; 369 goto fail; 370 } 371 372 if ((indirect = root->info.indirect_levels) > 1) { 373 ext4_warning(dir->i_sb, __FUNCTION__, 374 "Unimplemented inode hash depth: %#06x", 375 root->info.indirect_levels); 376 brelse(bh); 377 *err = ERR_BAD_DX_DIR; 378 goto fail; 379 } 380 381 entries = (struct dx_entry *) (((char *)&root->info) + 382 root->info.info_length); 383 384 if (dx_get_limit(entries) != dx_root_limit(dir, 385 root->info.info_length)) { 386 ext4_warning(dir->i_sb, __FUNCTION__, 387 "dx entry: limit != root limit"); 388 brelse(bh); 389 *err = ERR_BAD_DX_DIR; 390 goto fail; 391 } 392 393 dxtrace (printk("Look up %x", hash)); 394 while (1) 395 { 396 count = dx_get_count(entries); 397 if (!count || count > dx_get_limit(entries)) { 398 ext4_warning(dir->i_sb, __FUNCTION__, 399 "dx entry: no count or count > limit"); 400 brelse(bh); 401 *err = ERR_BAD_DX_DIR; 402 goto fail2; 403 } 404 405 p = entries + 1; 406 q = entries + count - 1; 407 while (p <= q) 408 { 409 m = p + (q - p)/2; 410 dxtrace(printk(".")); 411 if (dx_get_hash(m) > hash) 412 q = m - 1; 413 else 414 p = m + 1; 415 } 416 417 if (0) // linear search cross check 418 { 419 unsigned n = count - 1; 420 at = entries; 421 while (n--) 422 { 423 dxtrace(printk(",")); 424 if (dx_get_hash(++at) > hash) 425 { 426 at--; 427 break; 428 } 429 } 430 assert (at == p - 1); 431 } 432 433 at = p - 1; 434 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at))); 435 frame->bh = bh; 436 frame->entries = entries; 437 frame->at = at; 438 if (!indirect--) return frame; 439 if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err))) 440 goto fail2; 441 at = entries = ((struct dx_node *) bh->b_data)->entries; 442 if (dx_get_limit(entries) != dx_node_limit (dir)) { 443 ext4_warning(dir->i_sb, __FUNCTION__, 444 "dx entry: limit != node limit"); 445 brelse(bh); 446 *err = ERR_BAD_DX_DIR; 447 goto fail2; 448 } 449 frame++; 450 frame->bh = NULL; 451 } 452 fail2: 453 while (frame >= frame_in) { 454 brelse(frame->bh); 455 frame--; 456 } 457 fail: 458 if (*err == ERR_BAD_DX_DIR) 459 ext4_warning(dir->i_sb, __FUNCTION__, 460 "Corrupt dir inode %ld, running e2fsck is " 461 "recommended.", dir->i_ino); 462 return NULL; 463 } 464 465 static void dx_release (struct dx_frame *frames) 466 { 467 if (frames[0].bh == NULL) 468 return; 469 470 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels) 471 brelse(frames[1].bh); 472 brelse(frames[0].bh); 473 } 474 475 /* 476 * This function increments the frame pointer to search the next leaf 477 * block, and reads in the necessary intervening nodes if the search 478 * should be necessary. Whether or not the search is necessary is 479 * controlled by the hash parameter. If the hash value is even, then 480 * the search is only continued if the next block starts with that 481 * hash value. This is used if we are searching for a specific file. 482 * 483 * If the hash value is HASH_NB_ALWAYS, then always go to the next block. 484 * 485 * This function returns 1 if the caller should continue to search, 486 * or 0 if it should not. If there is an error reading one of the 487 * index blocks, it will a negative error code. 488 * 489 * If start_hash is non-null, it will be filled in with the starting 490 * hash of the next page. 491 */ 492 static int ext4_htree_next_block(struct inode *dir, __u32 hash, 493 struct dx_frame *frame, 494 struct dx_frame *frames, 495 __u32 *start_hash) 496 { 497 struct dx_frame *p; 498 struct buffer_head *bh; 499 int err, num_frames = 0; 500 __u32 bhash; 501 502 p = frame; 503 /* 504 * Find the next leaf page by incrementing the frame pointer. 505 * If we run out of entries in the interior node, loop around and 506 * increment pointer in the parent node. When we break out of 507 * this loop, num_frames indicates the number of interior 508 * nodes need to be read. 509 */ 510 while (1) { 511 if (++(p->at) < p->entries + dx_get_count(p->entries)) 512 break; 513 if (p == frames) 514 return 0; 515 num_frames++; 516 p--; 517 } 518 519 /* 520 * If the hash is 1, then continue only if the next page has a 521 * continuation hash of any value. This is used for readdir 522 * handling. Otherwise, check to see if the hash matches the 523 * desired contiuation hash. If it doesn't, return since 524 * there's no point to read in the successive index pages. 525 */ 526 bhash = dx_get_hash(p->at); 527 if (start_hash) 528 *start_hash = bhash; 529 if ((hash & 1) == 0) { 530 if ((bhash & ~1) != hash) 531 return 0; 532 } 533 /* 534 * If the hash is HASH_NB_ALWAYS, we always go to the next 535 * block so no check is necessary 536 */ 537 while (num_frames--) { 538 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at), 539 0, &err))) 540 return err; /* Failure */ 541 p++; 542 brelse (p->bh); 543 p->bh = bh; 544 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries; 545 } 546 return 1; 547 } 548 549 550 /* 551 * p is at least 6 bytes before the end of page 552 */ 553 static inline struct ext4_dir_entry_2 *ext4_next_entry(struct ext4_dir_entry_2 *p) 554 { 555 return (struct ext4_dir_entry_2 *)((char*)p + le16_to_cpu(p->rec_len)); 556 } 557 558 /* 559 * This function fills a red-black tree with information from a 560 * directory block. It returns the number directory entries loaded 561 * into the tree. If there is an error it is returned in err. 562 */ 563 static int htree_dirblock_to_tree(struct file *dir_file, 564 struct inode *dir, int block, 565 struct dx_hash_info *hinfo, 566 __u32 start_hash, __u32 start_minor_hash) 567 { 568 struct buffer_head *bh; 569 struct ext4_dir_entry_2 *de, *top; 570 int err, count = 0; 571 572 dxtrace(printk("In htree dirblock_to_tree: block %d\n", block)); 573 if (!(bh = ext4_bread (NULL, dir, block, 0, &err))) 574 return err; 575 576 de = (struct ext4_dir_entry_2 *) bh->b_data; 577 top = (struct ext4_dir_entry_2 *) ((char *) de + 578 dir->i_sb->s_blocksize - 579 EXT4_DIR_REC_LEN(0)); 580 for (; de < top; de = ext4_next_entry(de)) { 581 if (!ext4_check_dir_entry("htree_dirblock_to_tree", dir, de, bh, 582 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb)) 583 +((char *)de - bh->b_data))) { 584 /* On error, skip the f_pos to the next block. */ 585 dir_file->f_pos = (dir_file->f_pos | 586 (dir->i_sb->s_blocksize - 1)) + 1; 587 brelse (bh); 588 return count; 589 } 590 ext4fs_dirhash(de->name, de->name_len, hinfo); 591 if ((hinfo->hash < start_hash) || 592 ((hinfo->hash == start_hash) && 593 (hinfo->minor_hash < start_minor_hash))) 594 continue; 595 if (de->inode == 0) 596 continue; 597 if ((err = ext4_htree_store_dirent(dir_file, 598 hinfo->hash, hinfo->minor_hash, de)) != 0) { 599 brelse(bh); 600 return err; 601 } 602 count++; 603 } 604 brelse(bh); 605 return count; 606 } 607 608 609 /* 610 * This function fills a red-black tree with information from a 611 * directory. We start scanning the directory in hash order, starting 612 * at start_hash and start_minor_hash. 613 * 614 * This function returns the number of entries inserted into the tree, 615 * or a negative error code. 616 */ 617 int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash, 618 __u32 start_minor_hash, __u32 *next_hash) 619 { 620 struct dx_hash_info hinfo; 621 struct ext4_dir_entry_2 *de; 622 struct dx_frame frames[2], *frame; 623 struct inode *dir; 624 int block, err; 625 int count = 0; 626 int ret; 627 __u32 hashval; 628 629 dxtrace(printk("In htree_fill_tree, start hash: %x:%x\n", start_hash, 630 start_minor_hash)); 631 dir = dir_file->f_path.dentry->d_inode; 632 if (!(EXT4_I(dir)->i_flags & EXT4_INDEX_FL)) { 633 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version; 634 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed; 635 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo, 636 start_hash, start_minor_hash); 637 *next_hash = ~0; 638 return count; 639 } 640 hinfo.hash = start_hash; 641 hinfo.minor_hash = 0; 642 frame = dx_probe(NULL, dir_file->f_path.dentry->d_inode, &hinfo, frames, &err); 643 if (!frame) 644 return err; 645 646 /* Add '.' and '..' from the htree header */ 647 if (!start_hash && !start_minor_hash) { 648 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; 649 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0) 650 goto errout; 651 count++; 652 } 653 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) { 654 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; 655 de = ext4_next_entry(de); 656 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0) 657 goto errout; 658 count++; 659 } 660 661 while (1) { 662 block = dx_get_block(frame->at); 663 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo, 664 start_hash, start_minor_hash); 665 if (ret < 0) { 666 err = ret; 667 goto errout; 668 } 669 count += ret; 670 hashval = ~0; 671 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS, 672 frame, frames, &hashval); 673 *next_hash = hashval; 674 if (ret < 0) { 675 err = ret; 676 goto errout; 677 } 678 /* 679 * Stop if: (a) there are no more entries, or 680 * (b) we have inserted at least one entry and the 681 * next hash value is not a continuation 682 */ 683 if ((ret == 0) || 684 (count && ((hashval & 1) == 0))) 685 break; 686 } 687 dx_release(frames); 688 dxtrace(printk("Fill tree: returned %d entries, next hash: %x\n", 689 count, *next_hash)); 690 return count; 691 errout: 692 dx_release(frames); 693 return (err); 694 } 695 696 697 /* 698 * Directory block splitting, compacting 699 */ 700 701 /* 702 * Create map of hash values, offsets, and sizes, stored at end of block. 703 * Returns number of entries mapped. 704 */ 705 static int dx_make_map (struct ext4_dir_entry_2 *de, int size, 706 struct dx_hash_info *hinfo, struct dx_map_entry *map_tail) 707 { 708 int count = 0; 709 char *base = (char *) de; 710 struct dx_hash_info h = *hinfo; 711 712 while ((char *) de < base + size) 713 { 714 if (de->name_len && de->inode) { 715 ext4fs_dirhash(de->name, de->name_len, &h); 716 map_tail--; 717 map_tail->hash = h.hash; 718 map_tail->offs = (u16) ((char *) de - base); 719 map_tail->size = le16_to_cpu(de->rec_len); 720 count++; 721 cond_resched(); 722 } 723 /* XXX: do we need to check rec_len == 0 case? -Chris */ 724 de = (struct ext4_dir_entry_2 *) ((char *) de + le16_to_cpu(de->rec_len)); 725 } 726 return count; 727 } 728 729 /* Sort map by hash value */ 730 static void dx_sort_map (struct dx_map_entry *map, unsigned count) 731 { 732 struct dx_map_entry *p, *q, *top = map + count - 1; 733 int more; 734 /* Combsort until bubble sort doesn't suck */ 735 while (count > 2) { 736 count = count*10/13; 737 if (count - 9 < 2) /* 9, 10 -> 11 */ 738 count = 11; 739 for (p = top, q = p - count; q >= map; p--, q--) 740 if (p->hash < q->hash) 741 swap(*p, *q); 742 } 743 /* Garden variety bubble sort */ 744 do { 745 more = 0; 746 q = top; 747 while (q-- > map) { 748 if (q[1].hash >= q[0].hash) 749 continue; 750 swap(*(q+1), *q); 751 more = 1; 752 } 753 } while(more); 754 } 755 756 static void dx_insert_block(struct dx_frame *frame, u32 hash, u32 block) 757 { 758 struct dx_entry *entries = frame->entries; 759 struct dx_entry *old = frame->at, *new = old + 1; 760 int count = dx_get_count(entries); 761 762 assert(count < dx_get_limit(entries)); 763 assert(old < entries + count); 764 memmove(new + 1, new, (char *)(entries + count) - (char *)(new)); 765 dx_set_hash(new, hash); 766 dx_set_block(new, block); 767 dx_set_count(entries, count + 1); 768 } 769 #endif 770 771 772 static void ext4_update_dx_flag(struct inode *inode) 773 { 774 if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb, 775 EXT4_FEATURE_COMPAT_DIR_INDEX)) 776 EXT4_I(inode)->i_flags &= ~EXT4_INDEX_FL; 777 } 778 779 /* 780 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure. 781 * 782 * `len <= EXT4_NAME_LEN' is guaranteed by caller. 783 * `de != NULL' is guaranteed by caller. 784 */ 785 static inline int ext4_match (int len, const char * const name, 786 struct ext4_dir_entry_2 * de) 787 { 788 if (len != de->name_len) 789 return 0; 790 if (!de->inode) 791 return 0; 792 return !memcmp(name, de->name, len); 793 } 794 795 /* 796 * Returns 0 if not found, -1 on failure, and 1 on success 797 */ 798 static inline int search_dirblock(struct buffer_head * bh, 799 struct inode *dir, 800 struct dentry *dentry, 801 unsigned long offset, 802 struct ext4_dir_entry_2 ** res_dir) 803 { 804 struct ext4_dir_entry_2 * de; 805 char * dlimit; 806 int de_len; 807 const char *name = dentry->d_name.name; 808 int namelen = dentry->d_name.len; 809 810 de = (struct ext4_dir_entry_2 *) bh->b_data; 811 dlimit = bh->b_data + dir->i_sb->s_blocksize; 812 while ((char *) de < dlimit) { 813 /* this code is executed quadratically often */ 814 /* do minimal checking `by hand' */ 815 816 if ((char *) de + namelen <= dlimit && 817 ext4_match (namelen, name, de)) { 818 /* found a match - just to be sure, do a full check */ 819 if (!ext4_check_dir_entry("ext4_find_entry", 820 dir, de, bh, offset)) 821 return -1; 822 *res_dir = de; 823 return 1; 824 } 825 /* prevent looping on a bad block */ 826 de_len = le16_to_cpu(de->rec_len); 827 if (de_len <= 0) 828 return -1; 829 offset += de_len; 830 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len); 831 } 832 return 0; 833 } 834 835 836 /* 837 * ext4_find_entry() 838 * 839 * finds an entry in the specified directory with the wanted name. It 840 * returns the cache buffer in which the entry was found, and the entry 841 * itself (as a parameter - res_dir). It does NOT read the inode of the 842 * entry - you'll have to do that yourself if you want to. 843 * 844 * The returned buffer_head has ->b_count elevated. The caller is expected 845 * to brelse() it when appropriate. 846 */ 847 static struct buffer_head * ext4_find_entry (struct dentry *dentry, 848 struct ext4_dir_entry_2 ** res_dir) 849 { 850 struct super_block * sb; 851 struct buffer_head * bh_use[NAMEI_RA_SIZE]; 852 struct buffer_head * bh, *ret = NULL; 853 unsigned long start, block, b; 854 int ra_max = 0; /* Number of bh's in the readahead 855 buffer, bh_use[] */ 856 int ra_ptr = 0; /* Current index into readahead 857 buffer */ 858 int num = 0; 859 int nblocks, i, err; 860 struct inode *dir = dentry->d_parent->d_inode; 861 int namelen; 862 const u8 *name; 863 unsigned blocksize; 864 865 *res_dir = NULL; 866 sb = dir->i_sb; 867 blocksize = sb->s_blocksize; 868 namelen = dentry->d_name.len; 869 name = dentry->d_name.name; 870 if (namelen > EXT4_NAME_LEN) 871 return NULL; 872 #ifdef CONFIG_EXT4_INDEX 873 if (is_dx(dir)) { 874 bh = ext4_dx_find_entry(dentry, res_dir, &err); 875 /* 876 * On success, or if the error was file not found, 877 * return. Otherwise, fall back to doing a search the 878 * old fashioned way. 879 */ 880 if (bh || (err != ERR_BAD_DX_DIR)) 881 return bh; 882 dxtrace(printk("ext4_find_entry: dx failed, falling back\n")); 883 } 884 #endif 885 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb); 886 start = EXT4_I(dir)->i_dir_start_lookup; 887 if (start >= nblocks) 888 start = 0; 889 block = start; 890 restart: 891 do { 892 /* 893 * We deal with the read-ahead logic here. 894 */ 895 if (ra_ptr >= ra_max) { 896 /* Refill the readahead buffer */ 897 ra_ptr = 0; 898 b = block; 899 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { 900 /* 901 * Terminate if we reach the end of the 902 * directory and must wrap, or if our 903 * search has finished at this block. 904 */ 905 if (b >= nblocks || (num && block == start)) { 906 bh_use[ra_max] = NULL; 907 break; 908 } 909 num++; 910 bh = ext4_getblk(NULL, dir, b++, 0, &err); 911 bh_use[ra_max] = bh; 912 if (bh) 913 ll_rw_block(READ_META, 1, &bh); 914 } 915 } 916 if ((bh = bh_use[ra_ptr++]) == NULL) 917 goto next; 918 wait_on_buffer(bh); 919 if (!buffer_uptodate(bh)) { 920 /* read error, skip block & hope for the best */ 921 ext4_error(sb, __FUNCTION__, "reading directory #%lu " 922 "offset %lu", dir->i_ino, block); 923 brelse(bh); 924 goto next; 925 } 926 i = search_dirblock(bh, dir, dentry, 927 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir); 928 if (i == 1) { 929 EXT4_I(dir)->i_dir_start_lookup = block; 930 ret = bh; 931 goto cleanup_and_exit; 932 } else { 933 brelse(bh); 934 if (i < 0) 935 goto cleanup_and_exit; 936 } 937 next: 938 if (++block >= nblocks) 939 block = 0; 940 } while (block != start); 941 942 /* 943 * If the directory has grown while we were searching, then 944 * search the last part of the directory before giving up. 945 */ 946 block = nblocks; 947 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb); 948 if (block < nblocks) { 949 start = 0; 950 goto restart; 951 } 952 953 cleanup_and_exit: 954 /* Clean up the read-ahead blocks */ 955 for (; ra_ptr < ra_max; ra_ptr++) 956 brelse (bh_use[ra_ptr]); 957 return ret; 958 } 959 960 #ifdef CONFIG_EXT4_INDEX 961 static struct buffer_head * ext4_dx_find_entry(struct dentry *dentry, 962 struct ext4_dir_entry_2 **res_dir, int *err) 963 { 964 struct super_block * sb; 965 struct dx_hash_info hinfo; 966 u32 hash; 967 struct dx_frame frames[2], *frame; 968 struct ext4_dir_entry_2 *de, *top; 969 struct buffer_head *bh; 970 unsigned long block; 971 int retval; 972 int namelen = dentry->d_name.len; 973 const u8 *name = dentry->d_name.name; 974 struct inode *dir = dentry->d_parent->d_inode; 975 976 sb = dir->i_sb; 977 /* NFS may look up ".." - look at dx_root directory block */ 978 if (namelen > 2 || name[0] != '.'||(name[1] != '.' && name[1] != '\0')){ 979 if (!(frame = dx_probe(dentry, NULL, &hinfo, frames, err))) 980 return NULL; 981 } else { 982 frame = frames; 983 frame->bh = NULL; /* for dx_release() */ 984 frame->at = (struct dx_entry *)frames; /* hack for zero entry*/ 985 dx_set_block(frame->at, 0); /* dx_root block is 0 */ 986 } 987 hash = hinfo.hash; 988 do { 989 block = dx_get_block(frame->at); 990 if (!(bh = ext4_bread (NULL,dir, block, 0, err))) 991 goto errout; 992 de = (struct ext4_dir_entry_2 *) bh->b_data; 993 top = (struct ext4_dir_entry_2 *) ((char *) de + sb->s_blocksize - 994 EXT4_DIR_REC_LEN(0)); 995 for (; de < top; de = ext4_next_entry(de)) 996 if (ext4_match (namelen, name, de)) { 997 if (!ext4_check_dir_entry("ext4_find_entry", 998 dir, de, bh, 999 (block<<EXT4_BLOCK_SIZE_BITS(sb)) 1000 +((char *)de - bh->b_data))) { 1001 brelse (bh); 1002 *err = ERR_BAD_DX_DIR; 1003 goto errout; 1004 } 1005 *res_dir = de; 1006 dx_release (frames); 1007 return bh; 1008 } 1009 brelse (bh); 1010 /* Check to see if we should continue to search */ 1011 retval = ext4_htree_next_block(dir, hash, frame, 1012 frames, NULL); 1013 if (retval < 0) { 1014 ext4_warning(sb, __FUNCTION__, 1015 "error reading index page in directory #%lu", 1016 dir->i_ino); 1017 *err = retval; 1018 goto errout; 1019 } 1020 } while (retval == 1); 1021 1022 *err = -ENOENT; 1023 errout: 1024 dxtrace(printk("%s not found\n", name)); 1025 dx_release (frames); 1026 return NULL; 1027 } 1028 #endif 1029 1030 static struct dentry *ext4_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd) 1031 { 1032 struct inode * inode; 1033 struct ext4_dir_entry_2 * de; 1034 struct buffer_head * bh; 1035 1036 if (dentry->d_name.len > EXT4_NAME_LEN) 1037 return ERR_PTR(-ENAMETOOLONG); 1038 1039 bh = ext4_find_entry(dentry, &de); 1040 inode = NULL; 1041 if (bh) { 1042 unsigned long ino = le32_to_cpu(de->inode); 1043 brelse (bh); 1044 if (!ext4_valid_inum(dir->i_sb, ino)) { 1045 ext4_error(dir->i_sb, "ext4_lookup", 1046 "bad inode number: %lu", ino); 1047 inode = NULL; 1048 } else 1049 inode = iget(dir->i_sb, ino); 1050 1051 if (!inode) 1052 return ERR_PTR(-EACCES); 1053 1054 if (is_bad_inode(inode)) { 1055 iput(inode); 1056 return ERR_PTR(-ENOENT); 1057 } 1058 } 1059 return d_splice_alias(inode, dentry); 1060 } 1061 1062 1063 struct dentry *ext4_get_parent(struct dentry *child) 1064 { 1065 unsigned long ino; 1066 struct dentry *parent; 1067 struct inode *inode; 1068 struct dentry dotdot; 1069 struct ext4_dir_entry_2 * de; 1070 struct buffer_head *bh; 1071 1072 dotdot.d_name.name = ".."; 1073 dotdot.d_name.len = 2; 1074 dotdot.d_parent = child; /* confusing, isn't it! */ 1075 1076 bh = ext4_find_entry(&dotdot, &de); 1077 inode = NULL; 1078 if (!bh) 1079 return ERR_PTR(-ENOENT); 1080 ino = le32_to_cpu(de->inode); 1081 brelse(bh); 1082 1083 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) { 1084 ext4_error(child->d_inode->i_sb, "ext4_get_parent", 1085 "bad inode number: %lu", ino); 1086 inode = NULL; 1087 } else 1088 inode = iget(child->d_inode->i_sb, ino); 1089 1090 if (!inode) 1091 return ERR_PTR(-EACCES); 1092 1093 if (is_bad_inode(inode)) { 1094 iput(inode); 1095 return ERR_PTR(-ENOENT); 1096 } 1097 1098 parent = d_alloc_anon(inode); 1099 if (!parent) { 1100 iput(inode); 1101 parent = ERR_PTR(-ENOMEM); 1102 } 1103 return parent; 1104 } 1105 1106 #define S_SHIFT 12 1107 static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = { 1108 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE, 1109 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR, 1110 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV, 1111 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV, 1112 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO, 1113 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK, 1114 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK, 1115 }; 1116 1117 static inline void ext4_set_de_type(struct super_block *sb, 1118 struct ext4_dir_entry_2 *de, 1119 umode_t mode) { 1120 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE)) 1121 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; 1122 } 1123 1124 #ifdef CONFIG_EXT4_INDEX 1125 /* 1126 * Move count entries from end of map between two memory locations. 1127 * Returns pointer to last entry moved. 1128 */ 1129 static struct ext4_dir_entry_2 * 1130 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count) 1131 { 1132 unsigned rec_len = 0; 1133 1134 while (count--) { 1135 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) (from + map->offs); 1136 rec_len = EXT4_DIR_REC_LEN(de->name_len); 1137 memcpy (to, de, rec_len); 1138 ((struct ext4_dir_entry_2 *) to)->rec_len = 1139 cpu_to_le16(rec_len); 1140 de->inode = 0; 1141 map++; 1142 to += rec_len; 1143 } 1144 return (struct ext4_dir_entry_2 *) (to - rec_len); 1145 } 1146 1147 /* 1148 * Compact each dir entry in the range to the minimal rec_len. 1149 * Returns pointer to last entry in range. 1150 */ 1151 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, int size) 1152 { 1153 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base; 1154 unsigned rec_len = 0; 1155 1156 prev = to = de; 1157 while ((char*)de < base + size) { 1158 next = (struct ext4_dir_entry_2 *) ((char *) de + 1159 le16_to_cpu(de->rec_len)); 1160 if (de->inode && de->name_len) { 1161 rec_len = EXT4_DIR_REC_LEN(de->name_len); 1162 if (de > to) 1163 memmove(to, de, rec_len); 1164 to->rec_len = cpu_to_le16(rec_len); 1165 prev = to; 1166 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len); 1167 } 1168 de = next; 1169 } 1170 return prev; 1171 } 1172 1173 /* 1174 * Split a full leaf block to make room for a new dir entry. 1175 * Allocate a new block, and move entries so that they are approx. equally full. 1176 * Returns pointer to de in block into which the new entry will be inserted. 1177 */ 1178 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, 1179 struct buffer_head **bh,struct dx_frame *frame, 1180 struct dx_hash_info *hinfo, int *error) 1181 { 1182 unsigned blocksize = dir->i_sb->s_blocksize; 1183 unsigned count, continued; 1184 struct buffer_head *bh2; 1185 u32 newblock; 1186 u32 hash2; 1187 struct dx_map_entry *map; 1188 char *data1 = (*bh)->b_data, *data2; 1189 unsigned split, move, size, i; 1190 struct ext4_dir_entry_2 *de = NULL, *de2; 1191 int err = 0; 1192 1193 bh2 = ext4_append (handle, dir, &newblock, &err); 1194 if (!(bh2)) { 1195 brelse(*bh); 1196 *bh = NULL; 1197 goto errout; 1198 } 1199 1200 BUFFER_TRACE(*bh, "get_write_access"); 1201 err = ext4_journal_get_write_access(handle, *bh); 1202 if (err) 1203 goto journal_error; 1204 1205 BUFFER_TRACE(frame->bh, "get_write_access"); 1206 err = ext4_journal_get_write_access(handle, frame->bh); 1207 if (err) 1208 goto journal_error; 1209 1210 data2 = bh2->b_data; 1211 1212 /* create map in the end of data2 block */ 1213 map = (struct dx_map_entry *) (data2 + blocksize); 1214 count = dx_make_map ((struct ext4_dir_entry_2 *) data1, 1215 blocksize, hinfo, map); 1216 map -= count; 1217 dx_sort_map (map, count); 1218 /* Split the existing block in the middle, size-wise */ 1219 size = 0; 1220 move = 0; 1221 for (i = count-1; i >= 0; i--) { 1222 /* is more than half of this entry in 2nd half of the block? */ 1223 if (size + map[i].size/2 > blocksize/2) 1224 break; 1225 size += map[i].size; 1226 move++; 1227 } 1228 /* map index at which we will split */ 1229 split = count - move; 1230 hash2 = map[split].hash; 1231 continued = hash2 == map[split - 1].hash; 1232 dxtrace(printk("Split block %i at %x, %i/%i\n", 1233 dx_get_block(frame->at), hash2, split, count-split)); 1234 1235 /* Fancy dance to stay within two buffers */ 1236 de2 = dx_move_dirents(data1, data2, map + split, count - split); 1237 de = dx_pack_dirents(data1,blocksize); 1238 de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de); 1239 de2->rec_len = cpu_to_le16(data2 + blocksize - (char *) de2); 1240 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1)); 1241 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1)); 1242 1243 /* Which block gets the new entry? */ 1244 if (hinfo->hash >= hash2) 1245 { 1246 swap(*bh, bh2); 1247 de = de2; 1248 } 1249 dx_insert_block (frame, hash2 + continued, newblock); 1250 err = ext4_journal_dirty_metadata (handle, bh2); 1251 if (err) 1252 goto journal_error; 1253 err = ext4_journal_dirty_metadata (handle, frame->bh); 1254 if (err) 1255 goto journal_error; 1256 brelse (bh2); 1257 dxtrace(dx_show_index ("frame", frame->entries)); 1258 return de; 1259 1260 journal_error: 1261 brelse(*bh); 1262 brelse(bh2); 1263 *bh = NULL; 1264 ext4_std_error(dir->i_sb, err); 1265 errout: 1266 *error = err; 1267 return NULL; 1268 } 1269 #endif 1270 1271 1272 /* 1273 * Add a new entry into a directory (leaf) block. If de is non-NULL, 1274 * it points to a directory entry which is guaranteed to be large 1275 * enough for new directory entry. If de is NULL, then 1276 * add_dirent_to_buf will attempt search the directory block for 1277 * space. It will return -ENOSPC if no space is available, and -EIO 1278 * and -EEXIST if directory entry already exists. 1279 * 1280 * NOTE! bh is NOT released in the case where ENOSPC is returned. In 1281 * all other cases bh is released. 1282 */ 1283 static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry, 1284 struct inode *inode, struct ext4_dir_entry_2 *de, 1285 struct buffer_head * bh) 1286 { 1287 struct inode *dir = dentry->d_parent->d_inode; 1288 const char *name = dentry->d_name.name; 1289 int namelen = dentry->d_name.len; 1290 unsigned long offset = 0; 1291 unsigned short reclen; 1292 int nlen, rlen, err; 1293 char *top; 1294 1295 reclen = EXT4_DIR_REC_LEN(namelen); 1296 if (!de) { 1297 de = (struct ext4_dir_entry_2 *)bh->b_data; 1298 top = bh->b_data + dir->i_sb->s_blocksize - reclen; 1299 while ((char *) de <= top) { 1300 if (!ext4_check_dir_entry("ext4_add_entry", dir, de, 1301 bh, offset)) { 1302 brelse (bh); 1303 return -EIO; 1304 } 1305 if (ext4_match (namelen, name, de)) { 1306 brelse (bh); 1307 return -EEXIST; 1308 } 1309 nlen = EXT4_DIR_REC_LEN(de->name_len); 1310 rlen = le16_to_cpu(de->rec_len); 1311 if ((de->inode? rlen - nlen: rlen) >= reclen) 1312 break; 1313 de = (struct ext4_dir_entry_2 *)((char *)de + rlen); 1314 offset += rlen; 1315 } 1316 if ((char *) de > top) 1317 return -ENOSPC; 1318 } 1319 BUFFER_TRACE(bh, "get_write_access"); 1320 err = ext4_journal_get_write_access(handle, bh); 1321 if (err) { 1322 ext4_std_error(dir->i_sb, err); 1323 brelse(bh); 1324 return err; 1325 } 1326 1327 /* By now the buffer is marked for journaling */ 1328 nlen = EXT4_DIR_REC_LEN(de->name_len); 1329 rlen = le16_to_cpu(de->rec_len); 1330 if (de->inode) { 1331 struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen); 1332 de1->rec_len = cpu_to_le16(rlen - nlen); 1333 de->rec_len = cpu_to_le16(nlen); 1334 de = de1; 1335 } 1336 de->file_type = EXT4_FT_UNKNOWN; 1337 if (inode) { 1338 de->inode = cpu_to_le32(inode->i_ino); 1339 ext4_set_de_type(dir->i_sb, de, inode->i_mode); 1340 } else 1341 de->inode = 0; 1342 de->name_len = namelen; 1343 memcpy (de->name, name, namelen); 1344 /* 1345 * XXX shouldn't update any times until successful 1346 * completion of syscall, but too many callers depend 1347 * on this. 1348 * 1349 * XXX similarly, too many callers depend on 1350 * ext4_new_inode() setting the times, but error 1351 * recovery deletes the inode, so the worst that can 1352 * happen is that the times are slightly out of date 1353 * and/or different from the directory change time. 1354 */ 1355 dir->i_mtime = dir->i_ctime = ext4_current_time(dir); 1356 ext4_update_dx_flag(dir); 1357 dir->i_version++; 1358 ext4_mark_inode_dirty(handle, dir); 1359 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata"); 1360 err = ext4_journal_dirty_metadata(handle, bh); 1361 if (err) 1362 ext4_std_error(dir->i_sb, err); 1363 brelse(bh); 1364 return 0; 1365 } 1366 1367 #ifdef CONFIG_EXT4_INDEX 1368 /* 1369 * This converts a one block unindexed directory to a 3 block indexed 1370 * directory, and adds the dentry to the indexed directory. 1371 */ 1372 static int make_indexed_dir(handle_t *handle, struct dentry *dentry, 1373 struct inode *inode, struct buffer_head *bh) 1374 { 1375 struct inode *dir = dentry->d_parent->d_inode; 1376 const char *name = dentry->d_name.name; 1377 int namelen = dentry->d_name.len; 1378 struct buffer_head *bh2; 1379 struct dx_root *root; 1380 struct dx_frame frames[2], *frame; 1381 struct dx_entry *entries; 1382 struct ext4_dir_entry_2 *de, *de2; 1383 char *data1, *top; 1384 unsigned len; 1385 int retval; 1386 unsigned blocksize; 1387 struct dx_hash_info hinfo; 1388 u32 block; 1389 struct fake_dirent *fde; 1390 1391 blocksize = dir->i_sb->s_blocksize; 1392 dxtrace(printk("Creating index\n")); 1393 retval = ext4_journal_get_write_access(handle, bh); 1394 if (retval) { 1395 ext4_std_error(dir->i_sb, retval); 1396 brelse(bh); 1397 return retval; 1398 } 1399 root = (struct dx_root *) bh->b_data; 1400 1401 bh2 = ext4_append (handle, dir, &block, &retval); 1402 if (!(bh2)) { 1403 brelse(bh); 1404 return retval; 1405 } 1406 EXT4_I(dir)->i_flags |= EXT4_INDEX_FL; 1407 data1 = bh2->b_data; 1408 1409 /* The 0th block becomes the root, move the dirents out */ 1410 fde = &root->dotdot; 1411 de = (struct ext4_dir_entry_2 *)((char *)fde + le16_to_cpu(fde->rec_len)); 1412 len = ((char *) root) + blocksize - (char *) de; 1413 memcpy (data1, de, len); 1414 de = (struct ext4_dir_entry_2 *) data1; 1415 top = data1 + len; 1416 while ((char *)(de2=(void*)de+le16_to_cpu(de->rec_len)) < top) 1417 de = de2; 1418 de->rec_len = cpu_to_le16(data1 + blocksize - (char *) de); 1419 /* Initialize the root; the dot dirents already exist */ 1420 de = (struct ext4_dir_entry_2 *) (&root->dotdot); 1421 de->rec_len = cpu_to_le16(blocksize - EXT4_DIR_REC_LEN(2)); 1422 memset (&root->info, 0, sizeof(root->info)); 1423 root->info.info_length = sizeof(root->info); 1424 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version; 1425 entries = root->entries; 1426 dx_set_block (entries, 1); 1427 dx_set_count (entries, 1); 1428 dx_set_limit (entries, dx_root_limit(dir, sizeof(root->info))); 1429 1430 /* Initialize as for dx_probe */ 1431 hinfo.hash_version = root->info.hash_version; 1432 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed; 1433 ext4fs_dirhash(name, namelen, &hinfo); 1434 frame = frames; 1435 frame->entries = entries; 1436 frame->at = entries; 1437 frame->bh = bh; 1438 bh = bh2; 1439 de = do_split(handle,dir, &bh, frame, &hinfo, &retval); 1440 dx_release (frames); 1441 if (!(de)) 1442 return retval; 1443 1444 return add_dirent_to_buf(handle, dentry, inode, de, bh); 1445 } 1446 #endif 1447 1448 /* 1449 * ext4_add_entry() 1450 * 1451 * adds a file entry to the specified directory, using the same 1452 * semantics as ext4_find_entry(). It returns NULL if it failed. 1453 * 1454 * NOTE!! The inode part of 'de' is left at 0 - which means you 1455 * may not sleep between calling this and putting something into 1456 * the entry, as someone else might have used it while you slept. 1457 */ 1458 static int ext4_add_entry (handle_t *handle, struct dentry *dentry, 1459 struct inode *inode) 1460 { 1461 struct inode *dir = dentry->d_parent->d_inode; 1462 unsigned long offset; 1463 struct buffer_head * bh; 1464 struct ext4_dir_entry_2 *de; 1465 struct super_block * sb; 1466 int retval; 1467 #ifdef CONFIG_EXT4_INDEX 1468 int dx_fallback=0; 1469 #endif 1470 unsigned blocksize; 1471 u32 block, blocks; 1472 1473 sb = dir->i_sb; 1474 blocksize = sb->s_blocksize; 1475 if (!dentry->d_name.len) 1476 return -EINVAL; 1477 #ifdef CONFIG_EXT4_INDEX 1478 if (is_dx(dir)) { 1479 retval = ext4_dx_add_entry(handle, dentry, inode); 1480 if (!retval || (retval != ERR_BAD_DX_DIR)) 1481 return retval; 1482 EXT4_I(dir)->i_flags &= ~EXT4_INDEX_FL; 1483 dx_fallback++; 1484 ext4_mark_inode_dirty(handle, dir); 1485 } 1486 #endif 1487 blocks = dir->i_size >> sb->s_blocksize_bits; 1488 for (block = 0, offset = 0; block < blocks; block++) { 1489 bh = ext4_bread(handle, dir, block, 0, &retval); 1490 if(!bh) 1491 return retval; 1492 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh); 1493 if (retval != -ENOSPC) 1494 return retval; 1495 1496 #ifdef CONFIG_EXT4_INDEX 1497 if (blocks == 1 && !dx_fallback && 1498 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) 1499 return make_indexed_dir(handle, dentry, inode, bh); 1500 #endif 1501 brelse(bh); 1502 } 1503 bh = ext4_append(handle, dir, &block, &retval); 1504 if (!bh) 1505 return retval; 1506 de = (struct ext4_dir_entry_2 *) bh->b_data; 1507 de->inode = 0; 1508 de->rec_len = cpu_to_le16(blocksize); 1509 return add_dirent_to_buf(handle, dentry, inode, de, bh); 1510 } 1511 1512 #ifdef CONFIG_EXT4_INDEX 1513 /* 1514 * Returns 0 for success, or a negative error value 1515 */ 1516 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry, 1517 struct inode *inode) 1518 { 1519 struct dx_frame frames[2], *frame; 1520 struct dx_entry *entries, *at; 1521 struct dx_hash_info hinfo; 1522 struct buffer_head * bh; 1523 struct inode *dir = dentry->d_parent->d_inode; 1524 struct super_block * sb = dir->i_sb; 1525 struct ext4_dir_entry_2 *de; 1526 int err; 1527 1528 frame = dx_probe(dentry, NULL, &hinfo, frames, &err); 1529 if (!frame) 1530 return err; 1531 entries = frame->entries; 1532 at = frame->at; 1533 1534 if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err))) 1535 goto cleanup; 1536 1537 BUFFER_TRACE(bh, "get_write_access"); 1538 err = ext4_journal_get_write_access(handle, bh); 1539 if (err) 1540 goto journal_error; 1541 1542 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh); 1543 if (err != -ENOSPC) { 1544 bh = NULL; 1545 goto cleanup; 1546 } 1547 1548 /* Block full, should compress but for now just split */ 1549 dxtrace(printk("using %u of %u node entries\n", 1550 dx_get_count(entries), dx_get_limit(entries))); 1551 /* Need to split index? */ 1552 if (dx_get_count(entries) == dx_get_limit(entries)) { 1553 u32 newblock; 1554 unsigned icount = dx_get_count(entries); 1555 int levels = frame - frames; 1556 struct dx_entry *entries2; 1557 struct dx_node *node2; 1558 struct buffer_head *bh2; 1559 1560 if (levels && (dx_get_count(frames->entries) == 1561 dx_get_limit(frames->entries))) { 1562 ext4_warning(sb, __FUNCTION__, 1563 "Directory index full!"); 1564 err = -ENOSPC; 1565 goto cleanup; 1566 } 1567 bh2 = ext4_append (handle, dir, &newblock, &err); 1568 if (!(bh2)) 1569 goto cleanup; 1570 node2 = (struct dx_node *)(bh2->b_data); 1571 entries2 = node2->entries; 1572 node2->fake.rec_len = cpu_to_le16(sb->s_blocksize); 1573 node2->fake.inode = 0; 1574 BUFFER_TRACE(frame->bh, "get_write_access"); 1575 err = ext4_journal_get_write_access(handle, frame->bh); 1576 if (err) 1577 goto journal_error; 1578 if (levels) { 1579 unsigned icount1 = icount/2, icount2 = icount - icount1; 1580 unsigned hash2 = dx_get_hash(entries + icount1); 1581 dxtrace(printk("Split index %i/%i\n", icount1, icount2)); 1582 1583 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */ 1584 err = ext4_journal_get_write_access(handle, 1585 frames[0].bh); 1586 if (err) 1587 goto journal_error; 1588 1589 memcpy ((char *) entries2, (char *) (entries + icount1), 1590 icount2 * sizeof(struct dx_entry)); 1591 dx_set_count (entries, icount1); 1592 dx_set_count (entries2, icount2); 1593 dx_set_limit (entries2, dx_node_limit(dir)); 1594 1595 /* Which index block gets the new entry? */ 1596 if (at - entries >= icount1) { 1597 frame->at = at = at - entries - icount1 + entries2; 1598 frame->entries = entries = entries2; 1599 swap(frame->bh, bh2); 1600 } 1601 dx_insert_block (frames + 0, hash2, newblock); 1602 dxtrace(dx_show_index ("node", frames[1].entries)); 1603 dxtrace(dx_show_index ("node", 1604 ((struct dx_node *) bh2->b_data)->entries)); 1605 err = ext4_journal_dirty_metadata(handle, bh2); 1606 if (err) 1607 goto journal_error; 1608 brelse (bh2); 1609 } else { 1610 dxtrace(printk("Creating second level index...\n")); 1611 memcpy((char *) entries2, (char *) entries, 1612 icount * sizeof(struct dx_entry)); 1613 dx_set_limit(entries2, dx_node_limit(dir)); 1614 1615 /* Set up root */ 1616 dx_set_count(entries, 1); 1617 dx_set_block(entries + 0, newblock); 1618 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1; 1619 1620 /* Add new access path frame */ 1621 frame = frames + 1; 1622 frame->at = at = at - entries + entries2; 1623 frame->entries = entries = entries2; 1624 frame->bh = bh2; 1625 err = ext4_journal_get_write_access(handle, 1626 frame->bh); 1627 if (err) 1628 goto journal_error; 1629 } 1630 ext4_journal_dirty_metadata(handle, frames[0].bh); 1631 } 1632 de = do_split(handle, dir, &bh, frame, &hinfo, &err); 1633 if (!de) 1634 goto cleanup; 1635 err = add_dirent_to_buf(handle, dentry, inode, de, bh); 1636 bh = NULL; 1637 goto cleanup; 1638 1639 journal_error: 1640 ext4_std_error(dir->i_sb, err); 1641 cleanup: 1642 if (bh) 1643 brelse(bh); 1644 dx_release(frames); 1645 return err; 1646 } 1647 #endif 1648 1649 /* 1650 * ext4_delete_entry deletes a directory entry by merging it with the 1651 * previous entry 1652 */ 1653 static int ext4_delete_entry (handle_t *handle, 1654 struct inode * dir, 1655 struct ext4_dir_entry_2 * de_del, 1656 struct buffer_head * bh) 1657 { 1658 struct ext4_dir_entry_2 * de, * pde; 1659 int i; 1660 1661 i = 0; 1662 pde = NULL; 1663 de = (struct ext4_dir_entry_2 *) bh->b_data; 1664 while (i < bh->b_size) { 1665 if (!ext4_check_dir_entry("ext4_delete_entry", dir, de, bh, i)) 1666 return -EIO; 1667 if (de == de_del) { 1668 BUFFER_TRACE(bh, "get_write_access"); 1669 ext4_journal_get_write_access(handle, bh); 1670 if (pde) 1671 pde->rec_len = 1672 cpu_to_le16(le16_to_cpu(pde->rec_len) + 1673 le16_to_cpu(de->rec_len)); 1674 else 1675 de->inode = 0; 1676 dir->i_version++; 1677 BUFFER_TRACE(bh, "call ext4_journal_dirty_metadata"); 1678 ext4_journal_dirty_metadata(handle, bh); 1679 return 0; 1680 } 1681 i += le16_to_cpu(de->rec_len); 1682 pde = de; 1683 de = (struct ext4_dir_entry_2 *) 1684 ((char *) de + le16_to_cpu(de->rec_len)); 1685 } 1686 return -ENOENT; 1687 } 1688 1689 /* 1690 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2, 1691 * since this indicates that nlinks count was previously 1. 1692 */ 1693 static void ext4_inc_count(handle_t *handle, struct inode *inode) 1694 { 1695 inc_nlink(inode); 1696 if (is_dx(inode) && inode->i_nlink > 1) { 1697 /* limit is 16-bit i_links_count */ 1698 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) { 1699 inode->i_nlink = 1; 1700 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb, 1701 EXT4_FEATURE_RO_COMPAT_DIR_NLINK); 1702 } 1703 } 1704 } 1705 1706 /* 1707 * If a directory had nlink == 1, then we should let it be 1. This indicates 1708 * directory has >EXT4_LINK_MAX subdirs. 1709 */ 1710 static void ext4_dec_count(handle_t *handle, struct inode *inode) 1711 { 1712 drop_nlink(inode); 1713 if (S_ISDIR(inode->i_mode) && inode->i_nlink == 0) 1714 inc_nlink(inode); 1715 } 1716 1717 1718 static int ext4_add_nondir(handle_t *handle, 1719 struct dentry *dentry, struct inode *inode) 1720 { 1721 int err = ext4_add_entry(handle, dentry, inode); 1722 if (!err) { 1723 ext4_mark_inode_dirty(handle, inode); 1724 d_instantiate(dentry, inode); 1725 return 0; 1726 } 1727 drop_nlink(inode); 1728 iput(inode); 1729 return err; 1730 } 1731 1732 /* 1733 * By the time this is called, we already have created 1734 * the directory cache entry for the new file, but it 1735 * is so far negative - it has no inode. 1736 * 1737 * If the create succeeds, we fill in the inode information 1738 * with d_instantiate(). 1739 */ 1740 static int ext4_create (struct inode * dir, struct dentry * dentry, int mode, 1741 struct nameidata *nd) 1742 { 1743 handle_t *handle; 1744 struct inode * inode; 1745 int err, retries = 0; 1746 1747 retry: 1748 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + 1749 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + 1750 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb)); 1751 if (IS_ERR(handle)) 1752 return PTR_ERR(handle); 1753 1754 if (IS_DIRSYNC(dir)) 1755 handle->h_sync = 1; 1756 1757 inode = ext4_new_inode (handle, dir, mode); 1758 err = PTR_ERR(inode); 1759 if (!IS_ERR(inode)) { 1760 inode->i_op = &ext4_file_inode_operations; 1761 inode->i_fop = &ext4_file_operations; 1762 ext4_set_aops(inode); 1763 err = ext4_add_nondir(handle, dentry, inode); 1764 } 1765 ext4_journal_stop(handle); 1766 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) 1767 goto retry; 1768 return err; 1769 } 1770 1771 static int ext4_mknod (struct inode * dir, struct dentry *dentry, 1772 int mode, dev_t rdev) 1773 { 1774 handle_t *handle; 1775 struct inode *inode; 1776 int err, retries = 0; 1777 1778 if (!new_valid_dev(rdev)) 1779 return -EINVAL; 1780 1781 retry: 1782 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + 1783 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + 1784 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb)); 1785 if (IS_ERR(handle)) 1786 return PTR_ERR(handle); 1787 1788 if (IS_DIRSYNC(dir)) 1789 handle->h_sync = 1; 1790 1791 inode = ext4_new_inode (handle, dir, mode); 1792 err = PTR_ERR(inode); 1793 if (!IS_ERR(inode)) { 1794 init_special_inode(inode, inode->i_mode, rdev); 1795 #ifdef CONFIG_EXT4DEV_FS_XATTR 1796 inode->i_op = &ext4_special_inode_operations; 1797 #endif 1798 err = ext4_add_nondir(handle, dentry, inode); 1799 } 1800 ext4_journal_stop(handle); 1801 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) 1802 goto retry; 1803 return err; 1804 } 1805 1806 static int ext4_mkdir(struct inode * dir, struct dentry * dentry, int mode) 1807 { 1808 handle_t *handle; 1809 struct inode * inode; 1810 struct buffer_head * dir_block; 1811 struct ext4_dir_entry_2 * de; 1812 int err, retries = 0; 1813 1814 if (EXT4_DIR_LINK_MAX(dir)) 1815 return -EMLINK; 1816 1817 retry: 1818 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + 1819 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + 1820 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb)); 1821 if (IS_ERR(handle)) 1822 return PTR_ERR(handle); 1823 1824 if (IS_DIRSYNC(dir)) 1825 handle->h_sync = 1; 1826 1827 inode = ext4_new_inode (handle, dir, S_IFDIR | mode); 1828 err = PTR_ERR(inode); 1829 if (IS_ERR(inode)) 1830 goto out_stop; 1831 1832 inode->i_op = &ext4_dir_inode_operations; 1833 inode->i_fop = &ext4_dir_operations; 1834 inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize; 1835 dir_block = ext4_bread (handle, inode, 0, 1, &err); 1836 if (!dir_block) { 1837 ext4_dec_count(handle, inode); /* is this nlink == 0? */ 1838 ext4_mark_inode_dirty(handle, inode); 1839 iput (inode); 1840 goto out_stop; 1841 } 1842 BUFFER_TRACE(dir_block, "get_write_access"); 1843 ext4_journal_get_write_access(handle, dir_block); 1844 de = (struct ext4_dir_entry_2 *) dir_block->b_data; 1845 de->inode = cpu_to_le32(inode->i_ino); 1846 de->name_len = 1; 1847 de->rec_len = cpu_to_le16(EXT4_DIR_REC_LEN(de->name_len)); 1848 strcpy (de->name, "."); 1849 ext4_set_de_type(dir->i_sb, de, S_IFDIR); 1850 de = (struct ext4_dir_entry_2 *) 1851 ((char *) de + le16_to_cpu(de->rec_len)); 1852 de->inode = cpu_to_le32(dir->i_ino); 1853 de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize-EXT4_DIR_REC_LEN(1)); 1854 de->name_len = 2; 1855 strcpy (de->name, ".."); 1856 ext4_set_de_type(dir->i_sb, de, S_IFDIR); 1857 inode->i_nlink = 2; 1858 BUFFER_TRACE(dir_block, "call ext4_journal_dirty_metadata"); 1859 ext4_journal_dirty_metadata(handle, dir_block); 1860 brelse (dir_block); 1861 ext4_mark_inode_dirty(handle, inode); 1862 err = ext4_add_entry (handle, dentry, inode); 1863 if (err) { 1864 inode->i_nlink = 0; 1865 ext4_mark_inode_dirty(handle, inode); 1866 iput (inode); 1867 goto out_stop; 1868 } 1869 ext4_inc_count(handle, dir); 1870 ext4_update_dx_flag(dir); 1871 ext4_mark_inode_dirty(handle, dir); 1872 d_instantiate(dentry, inode); 1873 out_stop: 1874 ext4_journal_stop(handle); 1875 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) 1876 goto retry; 1877 return err; 1878 } 1879 1880 /* 1881 * routine to check that the specified directory is empty (for rmdir) 1882 */ 1883 static int empty_dir (struct inode * inode) 1884 { 1885 unsigned long offset; 1886 struct buffer_head * bh; 1887 struct ext4_dir_entry_2 * de, * de1; 1888 struct super_block * sb; 1889 int err = 0; 1890 1891 sb = inode->i_sb; 1892 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) || 1893 !(bh = ext4_bread (NULL, inode, 0, 0, &err))) { 1894 if (err) 1895 ext4_error(inode->i_sb, __FUNCTION__, 1896 "error %d reading directory #%lu offset 0", 1897 err, inode->i_ino); 1898 else 1899 ext4_warning(inode->i_sb, __FUNCTION__, 1900 "bad directory (dir #%lu) - no data block", 1901 inode->i_ino); 1902 return 1; 1903 } 1904 de = (struct ext4_dir_entry_2 *) bh->b_data; 1905 de1 = (struct ext4_dir_entry_2 *) 1906 ((char *) de + le16_to_cpu(de->rec_len)); 1907 if (le32_to_cpu(de->inode) != inode->i_ino || 1908 !le32_to_cpu(de1->inode) || 1909 strcmp (".", de->name) || 1910 strcmp ("..", de1->name)) { 1911 ext4_warning (inode->i_sb, "empty_dir", 1912 "bad directory (dir #%lu) - no `.' or `..'", 1913 inode->i_ino); 1914 brelse (bh); 1915 return 1; 1916 } 1917 offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len); 1918 de = (struct ext4_dir_entry_2 *) 1919 ((char *) de1 + le16_to_cpu(de1->rec_len)); 1920 while (offset < inode->i_size ) { 1921 if (!bh || 1922 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) { 1923 err = 0; 1924 brelse (bh); 1925 bh = ext4_bread (NULL, inode, 1926 offset >> EXT4_BLOCK_SIZE_BITS(sb), 0, &err); 1927 if (!bh) { 1928 if (err) 1929 ext4_error(sb, __FUNCTION__, 1930 "error %d reading directory" 1931 " #%lu offset %lu", 1932 err, inode->i_ino, offset); 1933 offset += sb->s_blocksize; 1934 continue; 1935 } 1936 de = (struct ext4_dir_entry_2 *) bh->b_data; 1937 } 1938 if (!ext4_check_dir_entry("empty_dir", inode, de, bh, offset)) { 1939 de = (struct ext4_dir_entry_2 *)(bh->b_data + 1940 sb->s_blocksize); 1941 offset = (offset | (sb->s_blocksize - 1)) + 1; 1942 continue; 1943 } 1944 if (le32_to_cpu(de->inode)) { 1945 brelse (bh); 1946 return 0; 1947 } 1948 offset += le16_to_cpu(de->rec_len); 1949 de = (struct ext4_dir_entry_2 *) 1950 ((char *) de + le16_to_cpu(de->rec_len)); 1951 } 1952 brelse (bh); 1953 return 1; 1954 } 1955 1956 /* ext4_orphan_add() links an unlinked or truncated inode into a list of 1957 * such inodes, starting at the superblock, in case we crash before the 1958 * file is closed/deleted, or in case the inode truncate spans multiple 1959 * transactions and the last transaction is not recovered after a crash. 1960 * 1961 * At filesystem recovery time, we walk this list deleting unlinked 1962 * inodes and truncating linked inodes in ext4_orphan_cleanup(). 1963 */ 1964 int ext4_orphan_add(handle_t *handle, struct inode *inode) 1965 { 1966 struct super_block *sb = inode->i_sb; 1967 struct ext4_iloc iloc; 1968 int err = 0, rc; 1969 1970 lock_super(sb); 1971 if (!list_empty(&EXT4_I(inode)->i_orphan)) 1972 goto out_unlock; 1973 1974 /* Orphan handling is only valid for files with data blocks 1975 * being truncated, or files being unlinked. */ 1976 1977 /* @@@ FIXME: Observation from aviro: 1978 * I think I can trigger J_ASSERT in ext4_orphan_add(). We block 1979 * here (on lock_super()), so race with ext4_link() which might bump 1980 * ->i_nlink. For, say it, character device. Not a regular file, 1981 * not a directory, not a symlink and ->i_nlink > 0. 1982 */ 1983 J_ASSERT ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || 1984 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0); 1985 1986 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); 1987 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); 1988 if (err) 1989 goto out_unlock; 1990 1991 err = ext4_reserve_inode_write(handle, inode, &iloc); 1992 if (err) 1993 goto out_unlock; 1994 1995 /* Insert this inode at the head of the on-disk orphan list... */ 1996 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan); 1997 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino); 1998 err = ext4_journal_dirty_metadata(handle, EXT4_SB(sb)->s_sbh); 1999 rc = ext4_mark_iloc_dirty(handle, inode, &iloc); 2000 if (!err) 2001 err = rc; 2002 2003 /* Only add to the head of the in-memory list if all the 2004 * previous operations succeeded. If the orphan_add is going to 2005 * fail (possibly taking the journal offline), we can't risk 2006 * leaving the inode on the orphan list: stray orphan-list 2007 * entries can cause panics at unmount time. 2008 * 2009 * This is safe: on error we're going to ignore the orphan list 2010 * anyway on the next recovery. */ 2011 if (!err) 2012 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan); 2013 2014 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino); 2015 jbd_debug(4, "orphan inode %lu will point to %d\n", 2016 inode->i_ino, NEXT_ORPHAN(inode)); 2017 out_unlock: 2018 unlock_super(sb); 2019 ext4_std_error(inode->i_sb, err); 2020 return err; 2021 } 2022 2023 /* 2024 * ext4_orphan_del() removes an unlinked or truncated inode from the list 2025 * of such inodes stored on disk, because it is finally being cleaned up. 2026 */ 2027 int ext4_orphan_del(handle_t *handle, struct inode *inode) 2028 { 2029 struct list_head *prev; 2030 struct ext4_inode_info *ei = EXT4_I(inode); 2031 struct ext4_sb_info *sbi; 2032 unsigned long ino_next; 2033 struct ext4_iloc iloc; 2034 int err = 0; 2035 2036 lock_super(inode->i_sb); 2037 if (list_empty(&ei->i_orphan)) { 2038 unlock_super(inode->i_sb); 2039 return 0; 2040 } 2041 2042 ino_next = NEXT_ORPHAN(inode); 2043 prev = ei->i_orphan.prev; 2044 sbi = EXT4_SB(inode->i_sb); 2045 2046 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino); 2047 2048 list_del_init(&ei->i_orphan); 2049 2050 /* If we're on an error path, we may not have a valid 2051 * transaction handle with which to update the orphan list on 2052 * disk, but we still need to remove the inode from the linked 2053 * list in memory. */ 2054 if (!handle) 2055 goto out; 2056 2057 err = ext4_reserve_inode_write(handle, inode, &iloc); 2058 if (err) 2059 goto out_err; 2060 2061 if (prev == &sbi->s_orphan) { 2062 jbd_debug(4, "superblock will point to %lu\n", ino_next); 2063 BUFFER_TRACE(sbi->s_sbh, "get_write_access"); 2064 err = ext4_journal_get_write_access(handle, sbi->s_sbh); 2065 if (err) 2066 goto out_brelse; 2067 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next); 2068 err = ext4_journal_dirty_metadata(handle, sbi->s_sbh); 2069 } else { 2070 struct ext4_iloc iloc2; 2071 struct inode *i_prev = 2072 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode; 2073 2074 jbd_debug(4, "orphan inode %lu will point to %lu\n", 2075 i_prev->i_ino, ino_next); 2076 err = ext4_reserve_inode_write(handle, i_prev, &iloc2); 2077 if (err) 2078 goto out_brelse; 2079 NEXT_ORPHAN(i_prev) = ino_next; 2080 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2); 2081 } 2082 if (err) 2083 goto out_brelse; 2084 NEXT_ORPHAN(inode) = 0; 2085 err = ext4_mark_iloc_dirty(handle, inode, &iloc); 2086 2087 out_err: 2088 ext4_std_error(inode->i_sb, err); 2089 out: 2090 unlock_super(inode->i_sb); 2091 return err; 2092 2093 out_brelse: 2094 brelse(iloc.bh); 2095 goto out_err; 2096 } 2097 2098 static int ext4_rmdir (struct inode * dir, struct dentry *dentry) 2099 { 2100 int retval; 2101 struct inode * inode; 2102 struct buffer_head * bh; 2103 struct ext4_dir_entry_2 * de; 2104 handle_t *handle; 2105 2106 /* Initialize quotas before so that eventual writes go in 2107 * separate transaction */ 2108 DQUOT_INIT(dentry->d_inode); 2109 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb)); 2110 if (IS_ERR(handle)) 2111 return PTR_ERR(handle); 2112 2113 retval = -ENOENT; 2114 bh = ext4_find_entry (dentry, &de); 2115 if (!bh) 2116 goto end_rmdir; 2117 2118 if (IS_DIRSYNC(dir)) 2119 handle->h_sync = 1; 2120 2121 inode = dentry->d_inode; 2122 2123 retval = -EIO; 2124 if (le32_to_cpu(de->inode) != inode->i_ino) 2125 goto end_rmdir; 2126 2127 retval = -ENOTEMPTY; 2128 if (!empty_dir (inode)) 2129 goto end_rmdir; 2130 2131 retval = ext4_delete_entry(handle, dir, de, bh); 2132 if (retval) 2133 goto end_rmdir; 2134 if (!EXT4_DIR_LINK_EMPTY(inode)) 2135 ext4_warning (inode->i_sb, "ext4_rmdir", 2136 "empty directory has too many links (%d)", 2137 inode->i_nlink); 2138 inode->i_version++; 2139 clear_nlink(inode); 2140 /* There's no need to set i_disksize: the fact that i_nlink is 2141 * zero will ensure that the right thing happens during any 2142 * recovery. */ 2143 inode->i_size = 0; 2144 ext4_orphan_add(handle, inode); 2145 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode); 2146 ext4_mark_inode_dirty(handle, inode); 2147 ext4_dec_count(handle, dir); 2148 ext4_update_dx_flag(dir); 2149 ext4_mark_inode_dirty(handle, dir); 2150 2151 end_rmdir: 2152 ext4_journal_stop(handle); 2153 brelse (bh); 2154 return retval; 2155 } 2156 2157 static int ext4_unlink(struct inode * dir, struct dentry *dentry) 2158 { 2159 int retval; 2160 struct inode * inode; 2161 struct buffer_head * bh; 2162 struct ext4_dir_entry_2 * de; 2163 handle_t *handle; 2164 2165 /* Initialize quotas before so that eventual writes go 2166 * in separate transaction */ 2167 DQUOT_INIT(dentry->d_inode); 2168 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb)); 2169 if (IS_ERR(handle)) 2170 return PTR_ERR(handle); 2171 2172 if (IS_DIRSYNC(dir)) 2173 handle->h_sync = 1; 2174 2175 retval = -ENOENT; 2176 bh = ext4_find_entry (dentry, &de); 2177 if (!bh) 2178 goto end_unlink; 2179 2180 inode = dentry->d_inode; 2181 2182 retval = -EIO; 2183 if (le32_to_cpu(de->inode) != inode->i_ino) 2184 goto end_unlink; 2185 2186 if (!inode->i_nlink) { 2187 ext4_warning (inode->i_sb, "ext4_unlink", 2188 "Deleting nonexistent file (%lu), %d", 2189 inode->i_ino, inode->i_nlink); 2190 inode->i_nlink = 1; 2191 } 2192 retval = ext4_delete_entry(handle, dir, de, bh); 2193 if (retval) 2194 goto end_unlink; 2195 dir->i_ctime = dir->i_mtime = ext4_current_time(dir); 2196 ext4_update_dx_flag(dir); 2197 ext4_mark_inode_dirty(handle, dir); 2198 ext4_dec_count(handle, inode); 2199 if (!inode->i_nlink) 2200 ext4_orphan_add(handle, inode); 2201 inode->i_ctime = ext4_current_time(inode); 2202 ext4_mark_inode_dirty(handle, inode); 2203 retval = 0; 2204 2205 end_unlink: 2206 ext4_journal_stop(handle); 2207 brelse (bh); 2208 return retval; 2209 } 2210 2211 static int ext4_symlink (struct inode * dir, 2212 struct dentry *dentry, const char * symname) 2213 { 2214 handle_t *handle; 2215 struct inode * inode; 2216 int l, err, retries = 0; 2217 2218 l = strlen(symname)+1; 2219 if (l > dir->i_sb->s_blocksize) 2220 return -ENAMETOOLONG; 2221 2222 retry: 2223 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + 2224 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 5 + 2225 2*EXT4_QUOTA_INIT_BLOCKS(dir->i_sb)); 2226 if (IS_ERR(handle)) 2227 return PTR_ERR(handle); 2228 2229 if (IS_DIRSYNC(dir)) 2230 handle->h_sync = 1; 2231 2232 inode = ext4_new_inode (handle, dir, S_IFLNK|S_IRWXUGO); 2233 err = PTR_ERR(inode); 2234 if (IS_ERR(inode)) 2235 goto out_stop; 2236 2237 if (l > sizeof (EXT4_I(inode)->i_data)) { 2238 inode->i_op = &ext4_symlink_inode_operations; 2239 ext4_set_aops(inode); 2240 /* 2241 * page_symlink() calls into ext4_prepare/commit_write. 2242 * We have a transaction open. All is sweetness. It also sets 2243 * i_size in generic_commit_write(). 2244 */ 2245 err = __page_symlink(inode, symname, l, 2246 mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS); 2247 if (err) { 2248 ext4_dec_count(handle, inode); 2249 ext4_mark_inode_dirty(handle, inode); 2250 iput (inode); 2251 goto out_stop; 2252 } 2253 } else { 2254 inode->i_op = &ext4_fast_symlink_inode_operations; 2255 memcpy((char*)&EXT4_I(inode)->i_data,symname,l); 2256 inode->i_size = l-1; 2257 } 2258 EXT4_I(inode)->i_disksize = inode->i_size; 2259 err = ext4_add_nondir(handle, dentry, inode); 2260 out_stop: 2261 ext4_journal_stop(handle); 2262 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) 2263 goto retry; 2264 return err; 2265 } 2266 2267 static int ext4_link (struct dentry * old_dentry, 2268 struct inode * dir, struct dentry *dentry) 2269 { 2270 handle_t *handle; 2271 struct inode *inode = old_dentry->d_inode; 2272 int err, retries = 0; 2273 2274 if (EXT4_DIR_LINK_MAX(inode)) 2275 return -EMLINK; 2276 2277 /* 2278 * Return -ENOENT if we've raced with unlink and i_nlink is 0. Doing 2279 * otherwise has the potential to corrupt the orphan inode list. 2280 */ 2281 if (inode->i_nlink == 0) 2282 return -ENOENT; 2283 2284 retry: 2285 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + 2286 EXT4_INDEX_EXTRA_TRANS_BLOCKS); 2287 if (IS_ERR(handle)) 2288 return PTR_ERR(handle); 2289 2290 if (IS_DIRSYNC(dir)) 2291 handle->h_sync = 1; 2292 2293 inode->i_ctime = ext4_current_time(inode); 2294 ext4_inc_count(handle, inode); 2295 atomic_inc(&inode->i_count); 2296 2297 err = ext4_add_nondir(handle, dentry, inode); 2298 ext4_journal_stop(handle); 2299 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) 2300 goto retry; 2301 return err; 2302 } 2303 2304 #define PARENT_INO(buffer) \ 2305 ((struct ext4_dir_entry_2 *) ((char *) buffer + \ 2306 le16_to_cpu(((struct ext4_dir_entry_2 *) buffer)->rec_len)))->inode 2307 2308 /* 2309 * Anybody can rename anything with this: the permission checks are left to the 2310 * higher-level routines. 2311 */ 2312 static int ext4_rename (struct inode * old_dir, struct dentry *old_dentry, 2313 struct inode * new_dir,struct dentry *new_dentry) 2314 { 2315 handle_t *handle; 2316 struct inode * old_inode, * new_inode; 2317 struct buffer_head * old_bh, * new_bh, * dir_bh; 2318 struct ext4_dir_entry_2 * old_de, * new_de; 2319 int retval; 2320 2321 old_bh = new_bh = dir_bh = NULL; 2322 2323 /* Initialize quotas before so that eventual writes go 2324 * in separate transaction */ 2325 if (new_dentry->d_inode) 2326 DQUOT_INIT(new_dentry->d_inode); 2327 handle = ext4_journal_start(old_dir, 2 * 2328 EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) + 2329 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2); 2330 if (IS_ERR(handle)) 2331 return PTR_ERR(handle); 2332 2333 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) 2334 handle->h_sync = 1; 2335 2336 old_bh = ext4_find_entry (old_dentry, &old_de); 2337 /* 2338 * Check for inode number is _not_ due to possible IO errors. 2339 * We might rmdir the source, keep it as pwd of some process 2340 * and merrily kill the link to whatever was created under the 2341 * same name. Goodbye sticky bit ;-< 2342 */ 2343 old_inode = old_dentry->d_inode; 2344 retval = -ENOENT; 2345 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino) 2346 goto end_rename; 2347 2348 new_inode = new_dentry->d_inode; 2349 new_bh = ext4_find_entry (new_dentry, &new_de); 2350 if (new_bh) { 2351 if (!new_inode) { 2352 brelse (new_bh); 2353 new_bh = NULL; 2354 } 2355 } 2356 if (S_ISDIR(old_inode->i_mode)) { 2357 if (new_inode) { 2358 retval = -ENOTEMPTY; 2359 if (!empty_dir (new_inode)) 2360 goto end_rename; 2361 } 2362 retval = -EIO; 2363 dir_bh = ext4_bread (handle, old_inode, 0, 0, &retval); 2364 if (!dir_bh) 2365 goto end_rename; 2366 if (le32_to_cpu(PARENT_INO(dir_bh->b_data)) != old_dir->i_ino) 2367 goto end_rename; 2368 retval = -EMLINK; 2369 if (!new_inode && new_dir!=old_dir && 2370 new_dir->i_nlink >= EXT4_LINK_MAX) 2371 goto end_rename; 2372 } 2373 if (!new_bh) { 2374 retval = ext4_add_entry (handle, new_dentry, old_inode); 2375 if (retval) 2376 goto end_rename; 2377 } else { 2378 BUFFER_TRACE(new_bh, "get write access"); 2379 ext4_journal_get_write_access(handle, new_bh); 2380 new_de->inode = cpu_to_le32(old_inode->i_ino); 2381 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb, 2382 EXT4_FEATURE_INCOMPAT_FILETYPE)) 2383 new_de->file_type = old_de->file_type; 2384 new_dir->i_version++; 2385 BUFFER_TRACE(new_bh, "call ext4_journal_dirty_metadata"); 2386 ext4_journal_dirty_metadata(handle, new_bh); 2387 brelse(new_bh); 2388 new_bh = NULL; 2389 } 2390 2391 /* 2392 * Like most other Unix systems, set the ctime for inodes on a 2393 * rename. 2394 */ 2395 old_inode->i_ctime = ext4_current_time(old_inode); 2396 ext4_mark_inode_dirty(handle, old_inode); 2397 2398 /* 2399 * ok, that's it 2400 */ 2401 if (le32_to_cpu(old_de->inode) != old_inode->i_ino || 2402 old_de->name_len != old_dentry->d_name.len || 2403 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) || 2404 (retval = ext4_delete_entry(handle, old_dir, 2405 old_de, old_bh)) == -ENOENT) { 2406 /* old_de could have moved from under us during htree split, so 2407 * make sure that we are deleting the right entry. We might 2408 * also be pointing to a stale entry in the unused part of 2409 * old_bh so just checking inum and the name isn't enough. */ 2410 struct buffer_head *old_bh2; 2411 struct ext4_dir_entry_2 *old_de2; 2412 2413 old_bh2 = ext4_find_entry(old_dentry, &old_de2); 2414 if (old_bh2) { 2415 retval = ext4_delete_entry(handle, old_dir, 2416 old_de2, old_bh2); 2417 brelse(old_bh2); 2418 } 2419 } 2420 if (retval) { 2421 ext4_warning(old_dir->i_sb, "ext4_rename", 2422 "Deleting old file (%lu), %d, error=%d", 2423 old_dir->i_ino, old_dir->i_nlink, retval); 2424 } 2425 2426 if (new_inode) { 2427 ext4_dec_count(handle, new_inode); 2428 new_inode->i_ctime = ext4_current_time(new_inode); 2429 } 2430 old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir); 2431 ext4_update_dx_flag(old_dir); 2432 if (dir_bh) { 2433 BUFFER_TRACE(dir_bh, "get_write_access"); 2434 ext4_journal_get_write_access(handle, dir_bh); 2435 PARENT_INO(dir_bh->b_data) = cpu_to_le32(new_dir->i_ino); 2436 BUFFER_TRACE(dir_bh, "call ext4_journal_dirty_metadata"); 2437 ext4_journal_dirty_metadata(handle, dir_bh); 2438 ext4_dec_count(handle, old_dir); 2439 if (new_inode) { 2440 /* checked empty_dir above, can't have another parent, 2441 * ext3_dec_count() won't work for many-linked dirs */ 2442 new_inode->i_nlink = 0; 2443 } else { 2444 ext4_inc_count(handle, new_dir); 2445 ext4_update_dx_flag(new_dir); 2446 ext4_mark_inode_dirty(handle, new_dir); 2447 } 2448 } 2449 ext4_mark_inode_dirty(handle, old_dir); 2450 if (new_inode) { 2451 ext4_mark_inode_dirty(handle, new_inode); 2452 if (!new_inode->i_nlink) 2453 ext4_orphan_add(handle, new_inode); 2454 } 2455 retval = 0; 2456 2457 end_rename: 2458 brelse (dir_bh); 2459 brelse (old_bh); 2460 brelse (new_bh); 2461 ext4_journal_stop(handle); 2462 return retval; 2463 } 2464 2465 /* 2466 * directories can handle most operations... 2467 */ 2468 const struct inode_operations ext4_dir_inode_operations = { 2469 .create = ext4_create, 2470 .lookup = ext4_lookup, 2471 .link = ext4_link, 2472 .unlink = ext4_unlink, 2473 .symlink = ext4_symlink, 2474 .mkdir = ext4_mkdir, 2475 .rmdir = ext4_rmdir, 2476 .mknod = ext4_mknod, 2477 .rename = ext4_rename, 2478 .setattr = ext4_setattr, 2479 #ifdef CONFIG_EXT4DEV_FS_XATTR 2480 .setxattr = generic_setxattr, 2481 .getxattr = generic_getxattr, 2482 .listxattr = ext4_listxattr, 2483 .removexattr = generic_removexattr, 2484 #endif 2485 .permission = ext4_permission, 2486 }; 2487 2488 const struct inode_operations ext4_special_inode_operations = { 2489 .setattr = ext4_setattr, 2490 #ifdef CONFIG_EXT4DEV_FS_XATTR 2491 .setxattr = generic_setxattr, 2492 .getxattr = generic_getxattr, 2493 .listxattr = ext4_listxattr, 2494 .removexattr = generic_removexattr, 2495 #endif 2496 .permission = ext4_permission, 2497 }; 2498