2 * linux/fs/ext4/namei.c
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)
11 * linux/fs/minix/namei.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
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
28 #include <linux/pagemap.h>
29 #include <linux/jbd2.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>
38 #include "ext4_jbd2.h"
43 #include <trace/events/ext4.h>
45 * define how far ahead to read directories while searching them.
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))
52 static struct buffer_head *ext4_append(handle_t *handle,
54 ext4_lblk_t *block, int *err)
56 struct buffer_head *bh;
58 if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb &&
59 ((inode->i_size >> 10) >=
60 EXT4_SB(inode->i_sb)->s_max_dir_size_kb))) {
65 *block = inode->i_size >> inode->i_sb->s_blocksize_bits;
67 bh = ext4_bread(handle, inode, *block, 1, err);
69 inode->i_size += inode->i_sb->s_blocksize;
70 EXT4_I(inode)->i_disksize = inode->i_size;
71 *err = ext4_journal_get_write_access(handle, bh);
79 ext4_error(inode->i_sb,
80 "Directory hole detected on inode %lu\n",
87 #define assert(test) J_ASSERT(test)
91 #define dxtrace(command) command
93 #define dxtrace(command)
117 * dx_root_info is laid out so that if it should somehow get overlaid by a
118 * dirent the two low bits of the hash version will be zero. Therefore, the
119 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
124 struct fake_dirent dot;
126 struct fake_dirent dotdot;
130 __le32 reserved_zero;
132 u8 info_length; /* 8 */
137 struct dx_entry entries[0];
142 struct fake_dirent fake;
143 struct dx_entry entries[0];
149 struct buffer_head *bh;
150 struct dx_entry *entries;
162 * This goes at the end of each htree block.
166 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
169 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
170 static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
171 static inline unsigned dx_get_hash(struct dx_entry *entry);
172 static void dx_set_hash(struct dx_entry *entry, unsigned value);
173 static unsigned dx_get_count(struct dx_entry *entries);
174 static unsigned dx_get_limit(struct dx_entry *entries);
175 static void dx_set_count(struct dx_entry *entries, unsigned value);
176 static void dx_set_limit(struct dx_entry *entries, unsigned value);
177 static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
178 static unsigned dx_node_limit(struct inode *dir);
179 static struct dx_frame *dx_probe(const struct qstr *d_name,
181 struct dx_hash_info *hinfo,
182 struct dx_frame *frame,
184 static void dx_release(struct dx_frame *frames);
185 static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
186 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
187 static void dx_sort_map(struct dx_map_entry *map, unsigned count);
188 static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
189 struct dx_map_entry *offsets, int count, unsigned blocksize);
190 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
191 static void dx_insert_block(struct dx_frame *frame,
192 u32 hash, ext4_lblk_t block);
193 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
194 struct dx_frame *frame,
195 struct dx_frame *frames,
197 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
198 const struct qstr *d_name,
199 struct ext4_dir_entry_2 **res_dir,
201 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
202 struct inode *inode);
204 /* checksumming functions */
205 #define EXT4_DIRENT_TAIL(block, blocksize) \
206 ((struct ext4_dir_entry_tail *)(((void *)(block)) + \
208 sizeof(struct ext4_dir_entry_tail))))
210 static void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
211 unsigned int blocksize)
213 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
214 t->det_rec_len = ext4_rec_len_to_disk(
215 sizeof(struct ext4_dir_entry_tail), blocksize);
216 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
219 /* Walk through a dirent block to find a checksum "dirent" at the tail */
220 static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
221 struct ext4_dir_entry *de)
223 struct ext4_dir_entry_tail *t;
226 struct ext4_dir_entry *d, *top;
229 top = (struct ext4_dir_entry *)(((void *)de) +
230 (EXT4_BLOCK_SIZE(inode->i_sb) -
231 sizeof(struct ext4_dir_entry_tail)));
232 while (d < top && d->rec_len)
233 d = (struct ext4_dir_entry *)(((void *)d) +
234 le16_to_cpu(d->rec_len));
239 t = (struct ext4_dir_entry_tail *)d;
241 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
244 if (t->det_reserved_zero1 ||
245 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
246 t->det_reserved_zero2 ||
247 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
253 static __le32 ext4_dirent_csum(struct inode *inode,
254 struct ext4_dir_entry *dirent, int size)
256 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
257 struct ext4_inode_info *ei = EXT4_I(inode);
260 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
261 return cpu_to_le32(csum);
264 static void warn_no_space_for_csum(struct inode *inode)
266 ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for "
267 "checksum. Please run e2fsck -D.", inode->i_ino);
270 int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
272 struct ext4_dir_entry_tail *t;
274 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
275 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
278 t = get_dirent_tail(inode, dirent);
280 warn_no_space_for_csum(inode);
284 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
285 (void *)t - (void *)dirent))
291 static void ext4_dirent_csum_set(struct inode *inode,
292 struct ext4_dir_entry *dirent)
294 struct ext4_dir_entry_tail *t;
296 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
297 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
300 t = get_dirent_tail(inode, dirent);
302 warn_no_space_for_csum(inode);
306 t->det_checksum = ext4_dirent_csum(inode, dirent,
307 (void *)t - (void *)dirent);
310 static inline int ext4_handle_dirty_dirent_node(handle_t *handle,
312 struct buffer_head *bh)
314 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
315 return ext4_handle_dirty_metadata(handle, inode, bh);
318 static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
319 struct ext4_dir_entry *dirent,
322 struct ext4_dir_entry *dp;
323 struct dx_root_info *root;
326 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
328 else if (le16_to_cpu(dirent->rec_len) == 12) {
329 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
330 if (le16_to_cpu(dp->rec_len) !=
331 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
333 root = (struct dx_root_info *)(((void *)dp + 12));
334 if (root->reserved_zero ||
335 root->info_length != sizeof(struct dx_root_info))
342 *offset = count_offset;
343 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
346 static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
347 int count_offset, int count, struct dx_tail *t)
349 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
350 struct ext4_inode_info *ei = EXT4_I(inode);
351 __u32 csum, old_csum;
354 size = count_offset + (count * sizeof(struct dx_entry));
355 old_csum = t->dt_checksum;
357 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
358 csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
359 t->dt_checksum = old_csum;
361 return cpu_to_le32(csum);
364 static int ext4_dx_csum_verify(struct inode *inode,
365 struct ext4_dir_entry *dirent)
367 struct dx_countlimit *c;
369 int count_offset, limit, count;
371 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
372 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
375 c = get_dx_countlimit(inode, dirent, &count_offset);
377 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
380 limit = le16_to_cpu(c->limit);
381 count = le16_to_cpu(c->count);
382 if (count_offset + (limit * sizeof(struct dx_entry)) >
383 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
384 warn_no_space_for_csum(inode);
387 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
389 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
395 static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
397 struct dx_countlimit *c;
399 int count_offset, limit, count;
401 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
402 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
405 c = get_dx_countlimit(inode, dirent, &count_offset);
407 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
410 limit = le16_to_cpu(c->limit);
411 count = le16_to_cpu(c->count);
412 if (count_offset + (limit * sizeof(struct dx_entry)) >
413 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
414 warn_no_space_for_csum(inode);
417 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
419 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
422 static inline int ext4_handle_dirty_dx_node(handle_t *handle,
424 struct buffer_head *bh)
426 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
427 return ext4_handle_dirty_metadata(handle, inode, bh);
431 * p is at least 6 bytes before the end of page
433 static inline struct ext4_dir_entry_2 *
434 ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
436 return (struct ext4_dir_entry_2 *)((char *)p +
437 ext4_rec_len_from_disk(p->rec_len, blocksize));
441 * Future: use high four bits of block for coalesce-on-delete flags
442 * Mask them off for now.
445 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
447 return le32_to_cpu(entry->block) & 0x00ffffff;
450 static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
452 entry->block = cpu_to_le32(value);
455 static inline unsigned dx_get_hash(struct dx_entry *entry)
457 return le32_to_cpu(entry->hash);
460 static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
462 entry->hash = cpu_to_le32(value);
465 static inline unsigned dx_get_count(struct dx_entry *entries)
467 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
470 static inline unsigned dx_get_limit(struct dx_entry *entries)
472 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
475 static inline void dx_set_count(struct dx_entry *entries, unsigned value)
477 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
480 static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
482 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
485 static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
487 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
488 EXT4_DIR_REC_LEN(2) - infosize;
490 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
491 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
492 entry_space -= sizeof(struct dx_tail);
493 return entry_space / sizeof(struct dx_entry);
496 static inline unsigned dx_node_limit(struct inode *dir)
498 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
500 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
501 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
502 entry_space -= sizeof(struct dx_tail);
503 return entry_space / sizeof(struct dx_entry);
510 static void dx_show_index(char * label, struct dx_entry *entries)
512 int i, n = dx_get_count (entries);
513 printk(KERN_DEBUG "%s index ", label);
514 for (i = 0; i < n; i++) {
515 printk("%x->%lu ", i ? dx_get_hash(entries + i) :
516 0, (unsigned long)dx_get_block(entries + i));
528 static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
529 int size, int show_names)
531 unsigned names = 0, space = 0;
532 char *base = (char *) de;
533 struct dx_hash_info h = *hinfo;
536 while ((char *) de < base + size)
542 int len = de->name_len;
543 char *name = de->name;
544 while (len--) printk("%c", *name++);
545 ext4fs_dirhash(de->name, de->name_len, &h);
546 printk(":%x.%u ", h.hash,
547 (unsigned) ((char *) de - base));
549 space += EXT4_DIR_REC_LEN(de->name_len);
552 de = ext4_next_entry(de, size);
554 printk("(%i)\n", names);
555 return (struct stats) { names, space, 1 };
558 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
559 struct dx_entry *entries, int levels)
561 unsigned blocksize = dir->i_sb->s_blocksize;
562 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
564 struct buffer_head *bh;
566 printk("%i indexed blocks...\n", count);
567 for (i = 0; i < count; i++, entries++)
569 ext4_lblk_t block = dx_get_block(entries);
570 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
571 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
573 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
574 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
576 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
577 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
578 names += stats.names;
579 space += stats.space;
580 bcount += stats.bcount;
584 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
585 levels ? "" : " ", names, space/bcount,
586 (space/bcount)*100/blocksize);
587 return (struct stats) { names, space, bcount};
589 #endif /* DX_DEBUG */
592 * Probe for a directory leaf block to search.
594 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
595 * error in the directory index, and the caller should fall back to
596 * searching the directory normally. The callers of dx_probe **MUST**
597 * check for this error code, and make sure it never gets reflected
600 static struct dx_frame *
601 dx_probe(const struct qstr *d_name, struct inode *dir,
602 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
604 unsigned count, indirect;
605 struct dx_entry *at, *entries, *p, *q, *m;
606 struct dx_root *root;
607 struct buffer_head *bh;
608 struct dx_frame *frame = frame_in;
612 if (!(bh = ext4_bread(NULL, dir, 0, 0, err))) {
614 *err = ERR_BAD_DX_DIR;
617 root = (struct dx_root *) bh->b_data;
618 if (root->info.hash_version != DX_HASH_TEA &&
619 root->info.hash_version != DX_HASH_HALF_MD4 &&
620 root->info.hash_version != DX_HASH_LEGACY) {
621 ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
622 root->info.hash_version);
624 *err = ERR_BAD_DX_DIR;
627 hinfo->hash_version = root->info.hash_version;
628 if (hinfo->hash_version <= DX_HASH_TEA)
629 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
630 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
632 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
635 if (root->info.unused_flags & 1) {
636 ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
637 root->info.unused_flags);
639 *err = ERR_BAD_DX_DIR;
643 if ((indirect = root->info.indirect_levels) > 1) {
644 ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
645 root->info.indirect_levels);
647 *err = ERR_BAD_DX_DIR;
651 if (!buffer_verified(bh) &&
652 !ext4_dx_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) {
653 ext4_warning(dir->i_sb, "Root failed checksum");
655 *err = ERR_BAD_DX_DIR;
658 set_buffer_verified(bh);
660 entries = (struct dx_entry *) (((char *)&root->info) +
661 root->info.info_length);
663 if (dx_get_limit(entries) != dx_root_limit(dir,
664 root->info.info_length)) {
665 ext4_warning(dir->i_sb, "dx entry: limit != root limit");
667 *err = ERR_BAD_DX_DIR;
671 dxtrace(printk("Look up %x", hash));
674 count = dx_get_count(entries);
675 if (!count || count > dx_get_limit(entries)) {
676 ext4_warning(dir->i_sb,
677 "dx entry: no count or count > limit");
679 *err = ERR_BAD_DX_DIR;
684 q = entries + count - 1;
688 dxtrace(printk("."));
689 if (dx_get_hash(m) > hash)
695 if (0) // linear search cross check
697 unsigned n = count - 1;
701 dxtrace(printk(","));
702 if (dx_get_hash(++at) > hash)
708 assert (at == p - 1);
712 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
714 frame->entries = entries;
716 if (!indirect--) return frame;
717 if (!(bh = ext4_bread(NULL, dir, dx_get_block(at), 0, err))) {
719 *err = ERR_BAD_DX_DIR;
722 at = entries = ((struct dx_node *) bh->b_data)->entries;
724 if (!buffer_verified(bh) &&
725 !ext4_dx_csum_verify(dir,
726 (struct ext4_dir_entry *)bh->b_data)) {
727 ext4_warning(dir->i_sb, "Node failed checksum");
729 *err = ERR_BAD_DX_DIR;
732 set_buffer_verified(bh);
734 if (dx_get_limit(entries) != dx_node_limit (dir)) {
735 ext4_warning(dir->i_sb,
736 "dx entry: limit != node limit");
738 *err = ERR_BAD_DX_DIR;
745 while (frame >= frame_in) {
750 if (*err == ERR_BAD_DX_DIR)
751 ext4_warning(dir->i_sb,
752 "Corrupt dir inode %lu, running e2fsck is "
753 "recommended.", dir->i_ino);
757 static void dx_release (struct dx_frame *frames)
759 if (frames[0].bh == NULL)
762 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
763 brelse(frames[1].bh);
764 brelse(frames[0].bh);
768 * This function increments the frame pointer to search the next leaf
769 * block, and reads in the necessary intervening nodes if the search
770 * should be necessary. Whether or not the search is necessary is
771 * controlled by the hash parameter. If the hash value is even, then
772 * the search is only continued if the next block starts with that
773 * hash value. This is used if we are searching for a specific file.
775 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
777 * This function returns 1 if the caller should continue to search,
778 * or 0 if it should not. If there is an error reading one of the
779 * index blocks, it will a negative error code.
781 * If start_hash is non-null, it will be filled in with the starting
782 * hash of the next page.
784 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
785 struct dx_frame *frame,
786 struct dx_frame *frames,
790 struct buffer_head *bh;
791 int err, num_frames = 0;
796 * Find the next leaf page by incrementing the frame pointer.
797 * If we run out of entries in the interior node, loop around and
798 * increment pointer in the parent node. When we break out of
799 * this loop, num_frames indicates the number of interior
800 * nodes need to be read.
803 if (++(p->at) < p->entries + dx_get_count(p->entries))
812 * If the hash is 1, then continue only if the next page has a
813 * continuation hash of any value. This is used for readdir
814 * handling. Otherwise, check to see if the hash matches the
815 * desired contiuation hash. If it doesn't, return since
816 * there's no point to read in the successive index pages.
818 bhash = dx_get_hash(p->at);
821 if ((hash & 1) == 0) {
822 if ((bhash & ~1) != hash)
826 * If the hash is HASH_NB_ALWAYS, we always go to the next
827 * block so no check is necessary
829 while (num_frames--) {
830 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
833 ext4_error(dir->i_sb,
834 "Directory hole detected on inode %lu\n",
838 return err; /* Failure */
841 if (!buffer_verified(bh) &&
842 !ext4_dx_csum_verify(dir,
843 (struct ext4_dir_entry *)bh->b_data)) {
844 ext4_warning(dir->i_sb, "Node failed checksum");
847 set_buffer_verified(bh);
852 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
859 * This function fills a red-black tree with information from a
860 * directory block. It returns the number directory entries loaded
861 * into the tree. If there is an error it is returned in err.
863 static int htree_dirblock_to_tree(struct file *dir_file,
864 struct inode *dir, ext4_lblk_t block,
865 struct dx_hash_info *hinfo,
866 __u32 start_hash, __u32 start_minor_hash)
868 struct buffer_head *bh;
869 struct ext4_dir_entry_2 *de, *top;
870 int err = 0, count = 0;
872 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
873 (unsigned long)block));
874 if (!(bh = ext4_bread(NULL, dir, block, 0, &err))) {
877 ext4_error(dir->i_sb,
878 "Directory hole detected on inode %lu\n",
884 if (!buffer_verified(bh) &&
885 !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
887 set_buffer_verified(bh);
889 de = (struct ext4_dir_entry_2 *) bh->b_data;
890 top = (struct ext4_dir_entry_2 *) ((char *) de +
891 dir->i_sb->s_blocksize -
892 EXT4_DIR_REC_LEN(0));
893 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
894 if (ext4_check_dir_entry(dir, NULL, de, bh,
895 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
896 + ((char *)de - bh->b_data))) {
897 /* On error, skip the f_pos to the next block. */
898 dir_file->f_pos = (dir_file->f_pos |
899 (dir->i_sb->s_blocksize - 1)) + 1;
903 ext4fs_dirhash(de->name, de->name_len, hinfo);
904 if ((hinfo->hash < start_hash) ||
905 ((hinfo->hash == start_hash) &&
906 (hinfo->minor_hash < start_minor_hash)))
910 if ((err = ext4_htree_store_dirent(dir_file,
911 hinfo->hash, hinfo->minor_hash, de)) != 0) {
923 * This function fills a red-black tree with information from a
924 * directory. We start scanning the directory in hash order, starting
925 * at start_hash and start_minor_hash.
927 * This function returns the number of entries inserted into the tree,
928 * or a negative error code.
930 int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
931 __u32 start_minor_hash, __u32 *next_hash)
933 struct dx_hash_info hinfo;
934 struct ext4_dir_entry_2 *de;
935 struct dx_frame frames[2], *frame;
942 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
943 start_hash, start_minor_hash));
944 dir = dir_file->f_path.dentry->d_inode;
945 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
946 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
947 if (hinfo.hash_version <= DX_HASH_TEA)
948 hinfo.hash_version +=
949 EXT4_SB(dir->i_sb)->s_hash_unsigned;
950 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
951 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
952 start_hash, start_minor_hash);
956 hinfo.hash = start_hash;
957 hinfo.minor_hash = 0;
958 frame = dx_probe(NULL, dir, &hinfo, frames, &err);
962 /* Add '.' and '..' from the htree header */
963 if (!start_hash && !start_minor_hash) {
964 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
965 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
969 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
970 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
971 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
972 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
978 block = dx_get_block(frame->at);
979 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
980 start_hash, start_minor_hash);
987 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
988 frame, frames, &hashval);
989 *next_hash = hashval;
995 * Stop if: (a) there are no more entries, or
996 * (b) we have inserted at least one entry and the
997 * next hash value is not a continuation
1000 (count && ((hashval & 1) == 0)))
1004 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
1005 "next hash: %x\n", count, *next_hash));
1014 * Directory block splitting, compacting
1018 * Create map of hash values, offsets, and sizes, stored at end of block.
1019 * Returns number of entries mapped.
1021 static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
1022 struct dx_hash_info *hinfo,
1023 struct dx_map_entry *map_tail)
1026 char *base = (char *) de;
1027 struct dx_hash_info h = *hinfo;
1029 while ((char *) de < base + blocksize) {
1030 if (de->name_len && de->inode) {
1031 ext4fs_dirhash(de->name, de->name_len, &h);
1033 map_tail->hash = h.hash;
1034 map_tail->offs = ((char *) de - base)>>2;
1035 map_tail->size = le16_to_cpu(de->rec_len);
1039 /* XXX: do we need to check rec_len == 0 case? -Chris */
1040 de = ext4_next_entry(de, blocksize);
1045 /* Sort map by hash value */
1046 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1048 struct dx_map_entry *p, *q, *top = map + count - 1;
1050 /* Combsort until bubble sort doesn't suck */
1052 count = count*10/13;
1053 if (count - 9 < 2) /* 9, 10 -> 11 */
1055 for (p = top, q = p - count; q >= map; p--, q--)
1056 if (p->hash < q->hash)
1059 /* Garden variety bubble sort */
1064 if (q[1].hash >= q[0].hash)
1072 static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
1074 struct dx_entry *entries = frame->entries;
1075 struct dx_entry *old = frame->at, *new = old + 1;
1076 int count = dx_get_count(entries);
1078 assert(count < dx_get_limit(entries));
1079 assert(old < entries + count);
1080 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1081 dx_set_hash(new, hash);
1082 dx_set_block(new, block);
1083 dx_set_count(entries, count + 1);
1086 static void ext4_update_dx_flag(struct inode *inode)
1088 if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
1089 EXT4_FEATURE_COMPAT_DIR_INDEX))
1090 ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
1094 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
1096 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
1097 * `de != NULL' is guaranteed by caller.
1099 static inline int ext4_match (int len, const char * const name,
1100 struct ext4_dir_entry_2 * de)
1102 if (len != de->name_len)
1106 return !memcmp(name, de->name, len);
1110 * Returns 0 if not found, -1 on failure, and 1 on success
1112 static inline int search_dirblock(struct buffer_head *bh,
1114 const struct qstr *d_name,
1115 unsigned int offset,
1116 struct ext4_dir_entry_2 ** res_dir)
1118 struct ext4_dir_entry_2 * de;
1121 const char *name = d_name->name;
1122 int namelen = d_name->len;
1124 de = (struct ext4_dir_entry_2 *) bh->b_data;
1125 dlimit = bh->b_data + dir->i_sb->s_blocksize;
1126 while ((char *) de < dlimit) {
1127 /* this code is executed quadratically often */
1128 /* do minimal checking `by hand' */
1130 if ((char *) de + namelen <= dlimit &&
1131 ext4_match (namelen, name, de)) {
1132 /* found a match - just to be sure, do a full check */
1133 if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
1138 /* prevent looping on a bad block */
1139 de_len = ext4_rec_len_from_disk(de->rec_len,
1140 dir->i_sb->s_blocksize);
1144 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1149 static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1150 struct ext4_dir_entry *de)
1152 struct super_block *sb = dir->i_sb;
1158 if (de->inode == 0 &&
1159 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1168 * finds an entry in the specified directory with the wanted name. It
1169 * returns the cache buffer in which the entry was found, and the entry
1170 * itself (as a parameter - res_dir). It does NOT read the inode of the
1171 * entry - you'll have to do that yourself if you want to.
1173 * The returned buffer_head has ->b_count elevated. The caller is expected
1174 * to brelse() it when appropriate.
1176 static struct buffer_head * ext4_find_entry (struct inode *dir,
1177 const struct qstr *d_name,
1178 struct ext4_dir_entry_2 ** res_dir)
1180 struct super_block *sb;
1181 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1182 struct buffer_head *bh, *ret = NULL;
1183 ext4_lblk_t start, block, b;
1184 const u8 *name = d_name->name;
1185 int ra_max = 0; /* Number of bh's in the readahead
1187 int ra_ptr = 0; /* Current index into readahead
1190 ext4_lblk_t nblocks;
1196 namelen = d_name->len;
1197 if (namelen > EXT4_NAME_LEN)
1199 if ((namelen <= 2) && (name[0] == '.') &&
1200 (name[1] == '.' || name[1] == '\0')) {
1202 * "." or ".." will only be in the first block
1203 * NFS may look up ".."; "." should be handled by the VFS
1210 bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
1212 * On success, or if the error was file not found,
1213 * return. Otherwise, fall back to doing a search the
1214 * old fashioned way.
1216 if (bh || (err != ERR_BAD_DX_DIR))
1218 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1221 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1222 start = EXT4_I(dir)->i_dir_start_lookup;
1223 if (start >= nblocks)
1229 * We deal with the read-ahead logic here.
1231 if (ra_ptr >= ra_max) {
1232 /* Refill the readahead buffer */
1235 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1237 * Terminate if we reach the end of the
1238 * directory and must wrap, or if our
1239 * search has finished at this block.
1241 if (b >= nblocks || (num && block == start)) {
1242 bh_use[ra_max] = NULL;
1246 bh = ext4_getblk(NULL, dir, b++, 0, &err);
1247 bh_use[ra_max] = bh;
1249 ll_rw_block(READ | REQ_META | REQ_PRIO,
1253 if ((bh = bh_use[ra_ptr++]) == NULL)
1256 if (!buffer_uptodate(bh)) {
1257 /* read error, skip block & hope for the best */
1258 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1259 (unsigned long) block);
1263 if (!buffer_verified(bh) &&
1264 !is_dx_internal_node(dir, block,
1265 (struct ext4_dir_entry *)bh->b_data) &&
1266 !ext4_dirent_csum_verify(dir,
1267 (struct ext4_dir_entry *)bh->b_data)) {
1268 EXT4_ERROR_INODE(dir, "checksumming directory "
1269 "block %lu", (unsigned long)block);
1273 set_buffer_verified(bh);
1274 i = search_dirblock(bh, dir, d_name,
1275 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
1277 EXT4_I(dir)->i_dir_start_lookup = block;
1279 goto cleanup_and_exit;
1283 goto cleanup_and_exit;
1286 if (++block >= nblocks)
1288 } while (block != start);
1291 * If the directory has grown while we were searching, then
1292 * search the last part of the directory before giving up.
1295 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1296 if (block < nblocks) {
1302 /* Clean up the read-ahead blocks */
1303 for (; ra_ptr < ra_max; ra_ptr++)
1304 brelse(bh_use[ra_ptr]);
1308 static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
1309 struct ext4_dir_entry_2 **res_dir, int *err)
1311 struct super_block * sb = dir->i_sb;
1312 struct dx_hash_info hinfo;
1313 struct dx_frame frames[2], *frame;
1314 struct buffer_head *bh;
1318 if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
1321 block = dx_get_block(frame->at);
1322 if (!(bh = ext4_bread(NULL, dir, block, 0, err))) {
1325 ext4_error(dir->i_sb,
1326 "Directory hole detected on inode %lu\n",
1332 if (!buffer_verified(bh) &&
1333 !ext4_dirent_csum_verify(dir,
1334 (struct ext4_dir_entry *)bh->b_data)) {
1335 EXT4_ERROR_INODE(dir, "checksumming directory "
1336 "block %lu", (unsigned long)block);
1341 set_buffer_verified(bh);
1342 retval = search_dirblock(bh, dir, d_name,
1343 block << EXT4_BLOCK_SIZE_BITS(sb),
1345 if (retval == 1) { /* Success! */
1351 *err = ERR_BAD_DX_DIR;
1355 /* Check to see if we should continue to search */
1356 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
1360 "error reading index page in directory #%lu",
1365 } while (retval == 1);
1369 dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
1370 dx_release (frames);
1374 static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1376 struct inode *inode;
1377 struct ext4_dir_entry_2 *de;
1378 struct buffer_head *bh;
1380 if (dentry->d_name.len > EXT4_NAME_LEN)
1381 return ERR_PTR(-ENAMETOOLONG);
1383 bh = ext4_find_entry(dir, &dentry->d_name, &de);
1386 __u32 ino = le32_to_cpu(de->inode);
1388 if (!ext4_valid_inum(dir->i_sb, ino)) {
1389 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
1390 return ERR_PTR(-EIO);
1392 if (unlikely(ino == dir->i_ino)) {
1393 EXT4_ERROR_INODE(dir, "'%.*s' linked to parent dir",
1395 dentry->d_name.name);
1396 return ERR_PTR(-EIO);
1398 inode = ext4_iget(dir->i_sb, ino);
1399 if (inode == ERR_PTR(-ESTALE)) {
1400 EXT4_ERROR_INODE(dir,
1401 "deleted inode referenced: %u",
1403 return ERR_PTR(-EIO);
1406 return d_splice_alias(inode, dentry);
1410 struct dentry *ext4_get_parent(struct dentry *child)
1413 static const struct qstr dotdot = QSTR_INIT("..", 2);
1414 struct ext4_dir_entry_2 * de;
1415 struct buffer_head *bh;
1417 bh = ext4_find_entry(child->d_inode, &dotdot, &de);
1419 return ERR_PTR(-ENOENT);
1420 ino = le32_to_cpu(de->inode);
1423 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
1424 EXT4_ERROR_INODE(child->d_inode,
1425 "bad parent inode number: %u", ino);
1426 return ERR_PTR(-EIO);
1429 return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
1433 static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1434 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
1435 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
1436 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
1437 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
1438 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
1439 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
1440 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
1443 static inline void ext4_set_de_type(struct super_block *sb,
1444 struct ext4_dir_entry_2 *de,
1446 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1447 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1451 * Move count entries from end of map between two memory locations.
1452 * Returns pointer to last entry moved.
1454 static struct ext4_dir_entry_2 *
1455 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1458 unsigned rec_len = 0;
1461 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
1462 (from + (map->offs<<2));
1463 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1464 memcpy (to, de, rec_len);
1465 ((struct ext4_dir_entry_2 *) to)->rec_len =
1466 ext4_rec_len_to_disk(rec_len, blocksize);
1471 return (struct ext4_dir_entry_2 *) (to - rec_len);
1475 * Compact each dir entry in the range to the minimal rec_len.
1476 * Returns pointer to last entry in range.
1478 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
1480 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
1481 unsigned rec_len = 0;
1484 while ((char*)de < base + blocksize) {
1485 next = ext4_next_entry(de, blocksize);
1486 if (de->inode && de->name_len) {
1487 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1489 memmove(to, de, rec_len);
1490 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
1492 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
1500 * Split a full leaf block to make room for a new dir entry.
1501 * Allocate a new block, and move entries so that they are approx. equally full.
1502 * Returns pointer to de in block into which the new entry will be inserted.
1504 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1505 struct buffer_head **bh,struct dx_frame *frame,
1506 struct dx_hash_info *hinfo, int *error)
1508 unsigned blocksize = dir->i_sb->s_blocksize;
1509 unsigned count, continued;
1510 struct buffer_head *bh2;
1511 ext4_lblk_t newblock;
1513 struct dx_map_entry *map;
1514 char *data1 = (*bh)->b_data, *data2;
1515 unsigned split, move, size;
1516 struct ext4_dir_entry_2 *de = NULL, *de2;
1517 struct ext4_dir_entry_tail *t;
1521 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
1522 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1523 csum_size = sizeof(struct ext4_dir_entry_tail);
1525 bh2 = ext4_append (handle, dir, &newblock, &err);
1532 BUFFER_TRACE(*bh, "get_write_access");
1533 err = ext4_journal_get_write_access(handle, *bh);
1537 BUFFER_TRACE(frame->bh, "get_write_access");
1538 err = ext4_journal_get_write_access(handle, frame->bh);
1542 data2 = bh2->b_data;
1544 /* create map in the end of data2 block */
1545 map = (struct dx_map_entry *) (data2 + blocksize);
1546 count = dx_make_map((struct ext4_dir_entry_2 *) data1,
1547 blocksize, hinfo, map);
1549 dx_sort_map(map, count);
1550 /* Split the existing block in the middle, size-wise */
1553 for (i = count-1; i >= 0; i--) {
1554 /* is more than half of this entry in 2nd half of the block? */
1555 if (size + map[i].size/2 > blocksize/2)
1557 size += map[i].size;
1560 /* map index at which we will split */
1561 split = count - move;
1562 hash2 = map[split].hash;
1563 continued = hash2 == map[split - 1].hash;
1564 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1565 (unsigned long)dx_get_block(frame->at),
1566 hash2, split, count-split));
1568 /* Fancy dance to stay within two buffers */
1569 de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
1570 de = dx_pack_dirents(data1, blocksize);
1571 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1574 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1578 t = EXT4_DIRENT_TAIL(data2, blocksize);
1579 initialize_dirent_tail(t, blocksize);
1581 t = EXT4_DIRENT_TAIL(data1, blocksize);
1582 initialize_dirent_tail(t, blocksize);
1585 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1586 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
1588 /* Which block gets the new entry? */
1589 if (hinfo->hash >= hash2)
1594 dx_insert_block(frame, hash2 + continued, newblock);
1595 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
1598 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1602 dxtrace(dx_show_index("frame", frame->entries));
1609 ext4_std_error(dir->i_sb, err);
1616 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1617 * it points to a directory entry which is guaranteed to be large
1618 * enough for new directory entry. If de is NULL, then
1619 * add_dirent_to_buf will attempt search the directory block for
1620 * space. It will return -ENOSPC if no space is available, and -EIO
1621 * and -EEXIST if directory entry already exists.
1623 static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1624 struct inode *inode, struct ext4_dir_entry_2 *de,
1625 struct buffer_head *bh)
1627 struct inode *dir = dentry->d_parent->d_inode;
1628 const char *name = dentry->d_name.name;
1629 int namelen = dentry->d_name.len;
1630 unsigned int offset = 0;
1631 unsigned int blocksize = dir->i_sb->s_blocksize;
1632 unsigned short reclen;
1633 int nlen, rlen, err;
1637 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1638 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1639 csum_size = sizeof(struct ext4_dir_entry_tail);
1641 reclen = EXT4_DIR_REC_LEN(namelen);
1643 de = (struct ext4_dir_entry_2 *)bh->b_data;
1644 top = bh->b_data + (blocksize - csum_size) - reclen;
1645 while ((char *) de <= top) {
1646 if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
1648 if (ext4_match(namelen, name, de))
1650 nlen = EXT4_DIR_REC_LEN(de->name_len);
1651 rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
1652 if ((de->inode? rlen - nlen: rlen) >= reclen)
1654 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1657 if ((char *) de > top)
1660 BUFFER_TRACE(bh, "get_write_access");
1661 err = ext4_journal_get_write_access(handle, bh);
1663 ext4_std_error(dir->i_sb, err);
1667 /* By now the buffer is marked for journaling */
1668 nlen = EXT4_DIR_REC_LEN(de->name_len);
1669 rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
1671 struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
1672 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, blocksize);
1673 de->rec_len = ext4_rec_len_to_disk(nlen, blocksize);
1676 de->file_type = EXT4_FT_UNKNOWN;
1677 de->inode = cpu_to_le32(inode->i_ino);
1678 ext4_set_de_type(dir->i_sb, de, inode->i_mode);
1679 de->name_len = namelen;
1680 memcpy(de->name, name, namelen);
1682 * XXX shouldn't update any times until successful
1683 * completion of syscall, but too many callers depend
1686 * XXX similarly, too many callers depend on
1687 * ext4_new_inode() setting the times, but error
1688 * recovery deletes the inode, so the worst that can
1689 * happen is that the times are slightly out of date
1690 * and/or different from the directory change time.
1692 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1693 ext4_update_dx_flag(dir);
1695 ext4_mark_inode_dirty(handle, dir);
1696 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1697 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
1699 ext4_std_error(dir->i_sb, err);
1704 * This converts a one block unindexed directory to a 3 block indexed
1705 * directory, and adds the dentry to the indexed directory.
1707 static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1708 struct inode *inode, struct buffer_head *bh)
1710 struct inode *dir = dentry->d_parent->d_inode;
1711 const char *name = dentry->d_name.name;
1712 int namelen = dentry->d_name.len;
1713 struct buffer_head *bh2;
1714 struct dx_root *root;
1715 struct dx_frame frames[2], *frame;
1716 struct dx_entry *entries;
1717 struct ext4_dir_entry_2 *de, *de2;
1718 struct ext4_dir_entry_tail *t;
1723 struct dx_hash_info hinfo;
1725 struct fake_dirent *fde;
1728 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1729 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1730 csum_size = sizeof(struct ext4_dir_entry_tail);
1732 blocksize = dir->i_sb->s_blocksize;
1733 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
1734 retval = ext4_journal_get_write_access(handle, bh);
1736 ext4_std_error(dir->i_sb, retval);
1740 root = (struct dx_root *) bh->b_data;
1742 /* The 0th block becomes the root, move the dirents out */
1743 fde = &root->dotdot;
1744 de = (struct ext4_dir_entry_2 *)((char *)fde +
1745 ext4_rec_len_from_disk(fde->rec_len, blocksize));
1746 if ((char *) de >= (((char *) root) + blocksize)) {
1747 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
1751 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
1753 /* Allocate new block for the 0th block's dirents */
1754 bh2 = ext4_append(handle, dir, &block, &retval);
1759 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
1760 data1 = bh2->b_data;
1762 memcpy (data1, de, len);
1763 de = (struct ext4_dir_entry_2 *) data1;
1765 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
1767 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1772 t = EXT4_DIRENT_TAIL(data1, blocksize);
1773 initialize_dirent_tail(t, blocksize);
1776 /* Initialize the root; the dot dirents already exist */
1777 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
1778 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
1780 memset (&root->info, 0, sizeof(root->info));
1781 root->info.info_length = sizeof(root->info);
1782 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
1783 entries = root->entries;
1784 dx_set_block(entries, 1);
1785 dx_set_count(entries, 1);
1786 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
1788 /* Initialize as for dx_probe */
1789 hinfo.hash_version = root->info.hash_version;
1790 if (hinfo.hash_version <= DX_HASH_TEA)
1791 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
1792 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1793 ext4fs_dirhash(name, namelen, &hinfo);
1795 frame->entries = entries;
1796 frame->at = entries;
1800 ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1801 ext4_handle_dirty_dirent_node(handle, dir, bh);
1803 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1806 * Even if the block split failed, we have to properly write
1807 * out all the changes we did so far. Otherwise we can end up
1808 * with corrupted filesystem.
1810 ext4_mark_inode_dirty(handle, dir);
1816 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1824 * adds a file entry to the specified directory, using the same
1825 * semantics as ext4_find_entry(). It returns NULL if it failed.
1827 * NOTE!! The inode part of 'de' is left at 0 - which means you
1828 * may not sleep between calling this and putting something into
1829 * the entry, as someone else might have used it while you slept.
1831 static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1832 struct inode *inode)
1834 struct inode *dir = dentry->d_parent->d_inode;
1835 struct buffer_head *bh;
1836 struct ext4_dir_entry_2 *de;
1837 struct ext4_dir_entry_tail *t;
1838 struct super_block *sb;
1842 ext4_lblk_t block, blocks;
1845 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1846 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1847 csum_size = sizeof(struct ext4_dir_entry_tail);
1850 blocksize = sb->s_blocksize;
1851 if (!dentry->d_name.len)
1854 retval = ext4_dx_add_entry(handle, dentry, inode);
1855 if (!retval || (retval != ERR_BAD_DX_DIR))
1857 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
1859 ext4_mark_inode_dirty(handle, dir);
1861 blocks = dir->i_size >> sb->s_blocksize_bits;
1862 for (block = 0; block < blocks; block++) {
1863 if (!(bh = ext4_bread(handle, dir, block, 0, &retval))) {
1866 ext4_error(inode->i_sb,
1867 "Directory hole detected on inode %lu\n",
1872 if (!buffer_verified(bh) &&
1873 !ext4_dirent_csum_verify(dir,
1874 (struct ext4_dir_entry *)bh->b_data))
1876 set_buffer_verified(bh);
1877 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1878 if (retval != -ENOSPC) {
1883 if (blocks == 1 && !dx_fallback &&
1884 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
1885 return make_indexed_dir(handle, dentry, inode, bh);
1888 bh = ext4_append(handle, dir, &block, &retval);
1891 de = (struct ext4_dir_entry_2 *) bh->b_data;
1893 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
1896 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
1897 initialize_dirent_tail(t, blocksize);
1900 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1903 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
1908 * Returns 0 for success, or a negative error value
1910 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
1911 struct inode *inode)
1913 struct dx_frame frames[2], *frame;
1914 struct dx_entry *entries, *at;
1915 struct dx_hash_info hinfo;
1916 struct buffer_head *bh;
1917 struct inode *dir = dentry->d_parent->d_inode;
1918 struct super_block *sb = dir->i_sb;
1919 struct ext4_dir_entry_2 *de;
1922 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
1925 entries = frame->entries;
1928 if (!(bh = ext4_bread(handle, dir, dx_get_block(frame->at), 0, &err))) {
1931 ext4_error(dir->i_sb,
1932 "Directory hole detected on inode %lu\n",
1938 if (!buffer_verified(bh) &&
1939 !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
1941 set_buffer_verified(bh);
1943 BUFFER_TRACE(bh, "get_write_access");
1944 err = ext4_journal_get_write_access(handle, bh);
1948 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1952 /* Block full, should compress but for now just split */
1953 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
1954 dx_get_count(entries), dx_get_limit(entries)));
1955 /* Need to split index? */
1956 if (dx_get_count(entries) == dx_get_limit(entries)) {
1957 ext4_lblk_t newblock;
1958 unsigned icount = dx_get_count(entries);
1959 int levels = frame - frames;
1960 struct dx_entry *entries2;
1961 struct dx_node *node2;
1962 struct buffer_head *bh2;
1964 if (levels && (dx_get_count(frames->entries) ==
1965 dx_get_limit(frames->entries))) {
1966 ext4_warning(sb, "Directory index full!");
1970 bh2 = ext4_append (handle, dir, &newblock, &err);
1973 node2 = (struct dx_node *)(bh2->b_data);
1974 entries2 = node2->entries;
1975 memset(&node2->fake, 0, sizeof(struct fake_dirent));
1976 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
1978 BUFFER_TRACE(frame->bh, "get_write_access");
1979 err = ext4_journal_get_write_access(handle, frame->bh);
1983 unsigned icount1 = icount/2, icount2 = icount - icount1;
1984 unsigned hash2 = dx_get_hash(entries + icount1);
1985 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
1988 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1989 err = ext4_journal_get_write_access(handle,
1994 memcpy((char *) entries2, (char *) (entries + icount1),
1995 icount2 * sizeof(struct dx_entry));
1996 dx_set_count(entries, icount1);
1997 dx_set_count(entries2, icount2);
1998 dx_set_limit(entries2, dx_node_limit(dir));
2000 /* Which index block gets the new entry? */
2001 if (at - entries >= icount1) {
2002 frame->at = at = at - entries - icount1 + entries2;
2003 frame->entries = entries = entries2;
2004 swap(frame->bh, bh2);
2006 dx_insert_block(frames + 0, hash2, newblock);
2007 dxtrace(dx_show_index("node", frames[1].entries));
2008 dxtrace(dx_show_index("node",
2009 ((struct dx_node *) bh2->b_data)->entries));
2010 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2015 dxtrace(printk(KERN_DEBUG
2016 "Creating second level index...\n"));
2017 memcpy((char *) entries2, (char *) entries,
2018 icount * sizeof(struct dx_entry));
2019 dx_set_limit(entries2, dx_node_limit(dir));
2022 dx_set_count(entries, 1);
2023 dx_set_block(entries + 0, newblock);
2024 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2026 /* Add new access path frame */
2028 frame->at = at = at - entries + entries2;
2029 frame->entries = entries = entries2;
2031 err = ext4_journal_get_write_access(handle,
2036 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
2038 ext4_std_error(inode->i_sb, err);
2042 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
2045 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
2049 ext4_std_error(dir->i_sb, err);
2058 * ext4_delete_entry deletes a directory entry by merging it with the
2061 static int ext4_delete_entry(handle_t *handle,
2063 struct ext4_dir_entry_2 *de_del,
2064 struct buffer_head *bh)
2066 struct ext4_dir_entry_2 *de, *pde;
2067 unsigned int blocksize = dir->i_sb->s_blocksize;
2071 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2072 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2073 csum_size = sizeof(struct ext4_dir_entry_tail);
2077 de = (struct ext4_dir_entry_2 *) bh->b_data;
2078 while (i < bh->b_size - csum_size) {
2079 if (ext4_check_dir_entry(dir, NULL, de, bh, i))
2082 BUFFER_TRACE(bh, "get_write_access");
2083 err = ext4_journal_get_write_access(handle, bh);
2084 if (unlikely(err)) {
2085 ext4_std_error(dir->i_sb, err);
2089 pde->rec_len = ext4_rec_len_to_disk(
2090 ext4_rec_len_from_disk(pde->rec_len,
2092 ext4_rec_len_from_disk(de->rec_len,
2098 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2099 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2100 if (unlikely(err)) {
2101 ext4_std_error(dir->i_sb, err);
2106 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
2108 de = ext4_next_entry(de, blocksize);
2114 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2115 * since this indicates that nlinks count was previously 1.
2117 static void ext4_inc_count(handle_t *handle, struct inode *inode)
2120 if (is_dx(inode) && inode->i_nlink > 1) {
2121 /* limit is 16-bit i_links_count */
2122 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
2123 set_nlink(inode, 1);
2124 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2125 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2131 * If a directory had nlink == 1, then we should let it be 1. This indicates
2132 * directory has >EXT4_LINK_MAX subdirs.
2134 static void ext4_dec_count(handle_t *handle, struct inode *inode)
2136 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2141 static int ext4_add_nondir(handle_t *handle,
2142 struct dentry *dentry, struct inode *inode)
2144 int err = ext4_add_entry(handle, dentry, inode);
2146 ext4_mark_inode_dirty(handle, inode);
2147 unlock_new_inode(inode);
2148 d_instantiate(dentry, inode);
2152 unlock_new_inode(inode);
2158 * By the time this is called, we already have created
2159 * the directory cache entry for the new file, but it
2160 * is so far negative - it has no inode.
2162 * If the create succeeds, we fill in the inode information
2163 * with d_instantiate().
2165 static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2169 struct inode *inode;
2170 int err, retries = 0;
2172 dquot_initialize(dir);
2175 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2176 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2177 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2179 return PTR_ERR(handle);
2181 if (IS_DIRSYNC(dir))
2182 ext4_handle_sync(handle);
2184 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
2185 err = PTR_ERR(inode);
2186 if (!IS_ERR(inode)) {
2187 inode->i_op = &ext4_file_inode_operations;
2188 inode->i_fop = &ext4_file_operations;
2189 ext4_set_aops(inode);
2190 err = ext4_add_nondir(handle, dentry, inode);
2192 ext4_journal_stop(handle);
2193 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2198 static int ext4_mknod(struct inode *dir, struct dentry *dentry,
2199 umode_t mode, dev_t rdev)
2202 struct inode *inode;
2203 int err, retries = 0;
2205 if (!new_valid_dev(rdev))
2208 dquot_initialize(dir);
2211 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2212 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2213 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2215 return PTR_ERR(handle);
2217 if (IS_DIRSYNC(dir))
2218 ext4_handle_sync(handle);
2220 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
2221 err = PTR_ERR(inode);
2222 if (!IS_ERR(inode)) {
2223 init_special_inode(inode, inode->i_mode, rdev);
2224 inode->i_op = &ext4_special_inode_operations;
2225 err = ext4_add_nondir(handle, dentry, inode);
2227 ext4_journal_stop(handle);
2228 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2233 struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2234 struct ext4_dir_entry_2 *de,
2235 int blocksize, int csum_size,
2236 unsigned int parent_ino, int dotdot_real_len)
2238 de->inode = cpu_to_le32(inode->i_ino);
2240 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2242 strcpy(de->name, ".");
2243 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2245 de = ext4_next_entry(de, blocksize);
2246 de->inode = cpu_to_le32(parent_ino);
2248 if (!dotdot_real_len)
2249 de->rec_len = ext4_rec_len_to_disk(blocksize -
2250 (csum_size + EXT4_DIR_REC_LEN(1)),
2253 de->rec_len = ext4_rec_len_to_disk(
2254 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2255 strcpy(de->name, "..");
2256 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2258 return ext4_next_entry(de, blocksize);
2261 static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2262 struct inode *inode)
2264 struct buffer_head *dir_block = NULL;
2265 struct ext4_dir_entry_2 *de;
2266 struct ext4_dir_entry_tail *t;
2267 unsigned int blocksize = dir->i_sb->s_blocksize;
2271 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2272 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2273 csum_size = sizeof(struct ext4_dir_entry_tail);
2275 inode->i_size = EXT4_I(inode)->i_disksize = blocksize;
2276 dir_block = ext4_bread(handle, inode, 0, 1, &err);
2277 if (!(dir_block = ext4_bread(handle, inode, 0, 1, &err))) {
2280 ext4_error(inode->i_sb,
2281 "Directory hole detected on inode %lu\n",
2286 BUFFER_TRACE(dir_block, "get_write_access");
2287 err = ext4_journal_get_write_access(handle, dir_block);
2290 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2291 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2292 set_nlink(inode, 2);
2294 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2295 initialize_dirent_tail(t, blocksize);
2298 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2299 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2302 set_buffer_verified(dir_block);
2308 static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2311 struct inode *inode;
2312 int err, retries = 0;
2314 if (EXT4_DIR_LINK_MAX(dir))
2317 dquot_initialize(dir);
2320 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2321 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2322 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2324 return PTR_ERR(handle);
2326 if (IS_DIRSYNC(dir))
2327 ext4_handle_sync(handle);
2329 inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
2330 &dentry->d_name, 0, NULL);
2331 err = PTR_ERR(inode);
2335 inode->i_op = &ext4_dir_inode_operations;
2336 inode->i_fop = &ext4_dir_operations;
2337 err = ext4_init_new_dir(handle, dir, inode);
2339 goto out_clear_inode;
2340 err = ext4_mark_inode_dirty(handle, inode);
2342 err = ext4_add_entry(handle, dentry, inode);
2346 unlock_new_inode(inode);
2347 ext4_mark_inode_dirty(handle, inode);
2351 ext4_inc_count(handle, dir);
2352 ext4_update_dx_flag(dir);
2353 err = ext4_mark_inode_dirty(handle, dir);
2355 goto out_clear_inode;
2356 unlock_new_inode(inode);
2357 d_instantiate(dentry, inode);
2359 ext4_journal_stop(handle);
2360 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2366 * routine to check that the specified directory is empty (for rmdir)
2368 static int empty_dir(struct inode *inode)
2370 unsigned int offset;
2371 struct buffer_head *bh;
2372 struct ext4_dir_entry_2 *de, *de1;
2373 struct super_block *sb;
2377 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
2378 !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
2380 EXT4_ERROR_INODE(inode,
2381 "error %d reading directory lblock 0", err);
2383 ext4_warning(inode->i_sb,
2384 "bad directory (dir #%lu) - no data block",
2388 if (!buffer_verified(bh) &&
2389 !ext4_dirent_csum_verify(inode,
2390 (struct ext4_dir_entry *)bh->b_data)) {
2391 EXT4_ERROR_INODE(inode, "checksum error reading directory "
2395 set_buffer_verified(bh);
2396 de = (struct ext4_dir_entry_2 *) bh->b_data;
2397 de1 = ext4_next_entry(de, sb->s_blocksize);
2398 if (le32_to_cpu(de->inode) != inode->i_ino ||
2399 !le32_to_cpu(de1->inode) ||
2400 strcmp(".", de->name) ||
2401 strcmp("..", de1->name)) {
2402 ext4_warning(inode->i_sb,
2403 "bad directory (dir #%lu) - no `.' or `..'",
2408 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2409 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2410 de = ext4_next_entry(de1, sb->s_blocksize);
2411 while (offset < inode->i_size) {
2413 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
2414 unsigned int lblock;
2417 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2418 bh = ext4_bread(NULL, inode, lblock, 0, &err);
2421 EXT4_ERROR_INODE(inode,
2422 "error %d reading directory "
2423 "lblock %u", err, lblock);
2425 ext4_warning(inode->i_sb,
2426 "bad directory (dir #%lu) - no data block",
2429 offset += sb->s_blocksize;
2432 if (!buffer_verified(bh) &&
2433 !ext4_dirent_csum_verify(inode,
2434 (struct ext4_dir_entry *)bh->b_data)) {
2435 EXT4_ERROR_INODE(inode, "checksum error "
2436 "reading directory lblock 0");
2439 set_buffer_verified(bh);
2440 de = (struct ext4_dir_entry_2 *) bh->b_data;
2442 if (ext4_check_dir_entry(inode, NULL, de, bh, offset)) {
2443 de = (struct ext4_dir_entry_2 *)(bh->b_data +
2445 offset = (offset | (sb->s_blocksize - 1)) + 1;
2448 if (le32_to_cpu(de->inode)) {
2452 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2453 de = ext4_next_entry(de, sb->s_blocksize);
2459 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
2460 * such inodes, starting at the superblock, in case we crash before the
2461 * file is closed/deleted, or in case the inode truncate spans multiple
2462 * transactions and the last transaction is not recovered after a crash.
2464 * At filesystem recovery time, we walk this list deleting unlinked
2465 * inodes and truncating linked inodes in ext4_orphan_cleanup().
2467 int ext4_orphan_add(handle_t *handle, struct inode *inode)
2469 struct super_block *sb = inode->i_sb;
2470 struct ext4_iloc iloc;
2473 if (!EXT4_SB(sb)->s_journal)
2476 mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
2477 if (!list_empty(&EXT4_I(inode)->i_orphan))
2481 * Orphan handling is only valid for files with data blocks
2482 * being truncated, or files being unlinked. Note that we either
2483 * hold i_mutex, or the inode can not be referenced from outside,
2484 * so i_nlink should not be bumped due to race
2486 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2487 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
2489 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
2490 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
2494 err = ext4_reserve_inode_write(handle, inode, &iloc);
2498 * Due to previous errors inode may be already a part of on-disk
2499 * orphan list. If so skip on-disk list modification.
2501 if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
2502 (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
2505 /* Insert this inode at the head of the on-disk orphan list... */
2506 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
2507 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2508 err = ext4_handle_dirty_super(handle, sb);
2509 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2513 /* Only add to the head of the in-memory list if all the
2514 * previous operations succeeded. If the orphan_add is going to
2515 * fail (possibly taking the journal offline), we can't risk
2516 * leaving the inode on the orphan list: stray orphan-list
2517 * entries can cause panics at unmount time.
2519 * This is safe: on error we're going to ignore the orphan list
2520 * anyway on the next recovery. */
2523 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2525 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2526 jbd_debug(4, "orphan inode %lu will point to %d\n",
2527 inode->i_ino, NEXT_ORPHAN(inode));
2529 mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
2530 ext4_std_error(inode->i_sb, err);
2535 * ext4_orphan_del() removes an unlinked or truncated inode from the list
2536 * of such inodes stored on disk, because it is finally being cleaned up.
2538 int ext4_orphan_del(handle_t *handle, struct inode *inode)
2540 struct list_head *prev;
2541 struct ext4_inode_info *ei = EXT4_I(inode);
2542 struct ext4_sb_info *sbi;
2544 struct ext4_iloc iloc;
2547 if (!EXT4_SB(inode->i_sb)->s_journal)
2550 mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2551 if (list_empty(&ei->i_orphan))
2554 ino_next = NEXT_ORPHAN(inode);
2555 prev = ei->i_orphan.prev;
2556 sbi = EXT4_SB(inode->i_sb);
2558 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2560 list_del_init(&ei->i_orphan);
2562 /* If we're on an error path, we may not have a valid
2563 * transaction handle with which to update the orphan list on
2564 * disk, but we still need to remove the inode from the linked
2565 * list in memory. */
2569 err = ext4_reserve_inode_write(handle, inode, &iloc);
2573 if (prev == &sbi->s_orphan) {
2574 jbd_debug(4, "superblock will point to %u\n", ino_next);
2575 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2576 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
2579 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2580 err = ext4_handle_dirty_super(handle, inode->i_sb);
2582 struct ext4_iloc iloc2;
2583 struct inode *i_prev =
2584 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
2586 jbd_debug(4, "orphan inode %lu will point to %u\n",
2587 i_prev->i_ino, ino_next);
2588 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
2591 NEXT_ORPHAN(i_prev) = ino_next;
2592 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
2596 NEXT_ORPHAN(inode) = 0;
2597 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
2600 ext4_std_error(inode->i_sb, err);
2602 mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2610 static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
2613 struct inode *inode;
2614 struct buffer_head *bh;
2615 struct ext4_dir_entry_2 *de;
2618 /* Initialize quotas before so that eventual writes go in
2619 * separate transaction */
2620 dquot_initialize(dir);
2621 dquot_initialize(dentry->d_inode);
2623 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2625 return PTR_ERR(handle);
2628 bh = ext4_find_entry(dir, &dentry->d_name, &de);
2632 if (IS_DIRSYNC(dir))
2633 ext4_handle_sync(handle);
2635 inode = dentry->d_inode;
2638 if (le32_to_cpu(de->inode) != inode->i_ino)
2641 retval = -ENOTEMPTY;
2642 if (!empty_dir(inode))
2645 retval = ext4_delete_entry(handle, dir, de, bh);
2648 if (!EXT4_DIR_LINK_EMPTY(inode))
2649 ext4_warning(inode->i_sb,
2650 "empty directory has too many links (%d)",
2654 /* There's no need to set i_disksize: the fact that i_nlink is
2655 * zero will ensure that the right thing happens during any
2658 ext4_orphan_add(handle, inode);
2659 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
2660 ext4_mark_inode_dirty(handle, inode);
2661 ext4_dec_count(handle, dir);
2662 ext4_update_dx_flag(dir);
2663 ext4_mark_inode_dirty(handle, dir);
2666 ext4_journal_stop(handle);
2671 static int ext4_unlink(struct inode *dir, struct dentry *dentry)
2674 struct inode *inode;
2675 struct buffer_head *bh;
2676 struct ext4_dir_entry_2 *de;
2679 trace_ext4_unlink_enter(dir, dentry);
2680 /* Initialize quotas before so that eventual writes go
2681 * in separate transaction */
2682 dquot_initialize(dir);
2683 dquot_initialize(dentry->d_inode);
2685 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2687 return PTR_ERR(handle);
2689 if (IS_DIRSYNC(dir))
2690 ext4_handle_sync(handle);
2693 bh = ext4_find_entry(dir, &dentry->d_name, &de);
2697 inode = dentry->d_inode;
2700 if (le32_to_cpu(de->inode) != inode->i_ino)
2703 if (!inode->i_nlink) {
2704 ext4_warning(inode->i_sb,
2705 "Deleting nonexistent file (%lu), %d",
2706 inode->i_ino, inode->i_nlink);
2707 set_nlink(inode, 1);
2709 retval = ext4_delete_entry(handle, dir, de, bh);
2712 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
2713 ext4_update_dx_flag(dir);
2714 ext4_mark_inode_dirty(handle, dir);
2716 if (!inode->i_nlink)
2717 ext4_orphan_add(handle, inode);
2718 inode->i_ctime = ext4_current_time(inode);
2719 ext4_mark_inode_dirty(handle, inode);
2723 ext4_journal_stop(handle);
2725 trace_ext4_unlink_exit(dentry, retval);
2729 static int ext4_symlink(struct inode *dir,
2730 struct dentry *dentry, const char *symname)
2733 struct inode *inode;
2734 int l, err, retries = 0;
2737 l = strlen(symname)+1;
2738 if (l > dir->i_sb->s_blocksize)
2739 return -ENAMETOOLONG;
2741 dquot_initialize(dir);
2743 if (l > EXT4_N_BLOCKS * 4) {
2745 * For non-fast symlinks, we just allocate inode and put it on
2746 * orphan list in the first transaction => we need bitmap,
2747 * group descriptor, sb, inode block, quota blocks, and
2748 * possibly selinux xattr blocks.
2750 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2751 EXT4_XATTR_TRANS_BLOCKS;
2754 * Fast symlink. We have to add entry to directory
2755 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2756 * allocate new inode (bitmap, group descriptor, inode block,
2757 * quota blocks, sb is already counted in previous macros).
2759 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2760 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2761 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
2764 handle = ext4_journal_start(dir, credits);
2766 return PTR_ERR(handle);
2768 if (IS_DIRSYNC(dir))
2769 ext4_handle_sync(handle);
2771 inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
2772 &dentry->d_name, 0, NULL);
2773 err = PTR_ERR(inode);
2777 if (l > EXT4_N_BLOCKS * 4) {
2778 inode->i_op = &ext4_symlink_inode_operations;
2779 ext4_set_aops(inode);
2781 * We cannot call page_symlink() with transaction started
2782 * because it calls into ext4_write_begin() which can wait
2783 * for transaction commit if we are running out of space
2784 * and thus we deadlock. So we have to stop transaction now
2785 * and restart it when symlink contents is written.
2787 * To keep fs consistent in case of crash, we have to put inode
2788 * to orphan list in the mean time.
2791 err = ext4_orphan_add(handle, inode);
2792 ext4_journal_stop(handle);
2794 goto err_drop_inode;
2795 err = __page_symlink(inode, symname, l, 1);
2797 goto err_drop_inode;
2799 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2800 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2802 handle = ext4_journal_start(dir,
2803 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2804 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
2805 if (IS_ERR(handle)) {
2806 err = PTR_ERR(handle);
2807 goto err_drop_inode;
2809 set_nlink(inode, 1);
2810 err = ext4_orphan_del(handle, inode);
2812 ext4_journal_stop(handle);
2814 goto err_drop_inode;
2817 /* clear the extent format for fast symlink */
2818 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
2819 inode->i_op = &ext4_fast_symlink_inode_operations;
2820 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
2821 inode->i_size = l-1;
2823 EXT4_I(inode)->i_disksize = inode->i_size;
2824 err = ext4_add_nondir(handle, dentry, inode);
2826 ext4_journal_stop(handle);
2827 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2831 unlock_new_inode(inode);
2836 static int ext4_link(struct dentry *old_dentry,
2837 struct inode *dir, struct dentry *dentry)
2840 struct inode *inode = old_dentry->d_inode;
2841 int err, retries = 0;
2843 if (inode->i_nlink >= EXT4_LINK_MAX)
2846 dquot_initialize(dir);
2849 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2850 EXT4_INDEX_EXTRA_TRANS_BLOCKS);
2852 return PTR_ERR(handle);
2854 if (IS_DIRSYNC(dir))
2855 ext4_handle_sync(handle);
2857 inode->i_ctime = ext4_current_time(inode);
2858 ext4_inc_count(handle, inode);
2861 err = ext4_add_entry(handle, dentry, inode);
2863 ext4_mark_inode_dirty(handle, inode);
2864 d_instantiate(dentry, inode);
2869 ext4_journal_stop(handle);
2870 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2875 #define PARENT_INO(buffer, size) \
2876 (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
2879 * Anybody can rename anything with this: the permission checks are left to the
2880 * higher-level routines.
2882 static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
2883 struct inode *new_dir, struct dentry *new_dentry)
2886 struct inode *old_inode, *new_inode;
2887 struct buffer_head *old_bh, *new_bh, *dir_bh;
2888 struct ext4_dir_entry_2 *old_de, *new_de;
2889 int retval, force_da_alloc = 0;
2891 dquot_initialize(old_dir);
2892 dquot_initialize(new_dir);
2894 old_bh = new_bh = dir_bh = NULL;
2896 /* Initialize quotas before so that eventual writes go
2897 * in separate transaction */
2898 if (new_dentry->d_inode)
2899 dquot_initialize(new_dentry->d_inode);
2900 handle = ext4_journal_start(old_dir, 2 *
2901 EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2902 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
2904 return PTR_ERR(handle);
2906 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2907 ext4_handle_sync(handle);
2909 old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
2911 * Check for inode number is _not_ due to possible IO errors.
2912 * We might rmdir the source, keep it as pwd of some process
2913 * and merrily kill the link to whatever was created under the
2914 * same name. Goodbye sticky bit ;-<
2916 old_inode = old_dentry->d_inode;
2918 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2921 new_inode = new_dentry->d_inode;
2922 new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
2929 if (S_ISDIR(old_inode->i_mode)) {
2931 retval = -ENOTEMPTY;
2932 if (!empty_dir(new_inode))
2936 if (!(dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval))) {
2939 ext4_error(old_inode->i_sb,
2940 "Directory hole detected on inode %lu\n",
2945 if (!buffer_verified(dir_bh) &&
2946 !ext4_dirent_csum_verify(old_inode,
2947 (struct ext4_dir_entry *)dir_bh->b_data))
2949 set_buffer_verified(dir_bh);
2950 if (le32_to_cpu(PARENT_INO(dir_bh->b_data,
2951 old_dir->i_sb->s_blocksize)) != old_dir->i_ino)
2954 if (!new_inode && new_dir != old_dir &&
2955 EXT4_DIR_LINK_MAX(new_dir))
2957 BUFFER_TRACE(dir_bh, "get_write_access");
2958 retval = ext4_journal_get_write_access(handle, dir_bh);
2963 retval = ext4_add_entry(handle, new_dentry, old_inode);
2967 BUFFER_TRACE(new_bh, "get write access");
2968 retval = ext4_journal_get_write_access(handle, new_bh);
2971 new_de->inode = cpu_to_le32(old_inode->i_ino);
2972 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2973 EXT4_FEATURE_INCOMPAT_FILETYPE))
2974 new_de->file_type = old_de->file_type;
2975 new_dir->i_version++;
2976 new_dir->i_ctime = new_dir->i_mtime =
2977 ext4_current_time(new_dir);
2978 ext4_mark_inode_dirty(handle, new_dir);
2979 BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
2980 retval = ext4_handle_dirty_dirent_node(handle, new_dir, new_bh);
2981 if (unlikely(retval)) {
2982 ext4_std_error(new_dir->i_sb, retval);
2990 * Like most other Unix systems, set the ctime for inodes on a
2993 old_inode->i_ctime = ext4_current_time(old_inode);
2994 ext4_mark_inode_dirty(handle, old_inode);
2999 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
3000 old_de->name_len != old_dentry->d_name.len ||
3001 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
3002 (retval = ext4_delete_entry(handle, old_dir,
3003 old_de, old_bh)) == -ENOENT) {
3004 /* old_de could have moved from under us during htree split, so
3005 * make sure that we are deleting the right entry. We might
3006 * also be pointing to a stale entry in the unused part of
3007 * old_bh so just checking inum and the name isn't enough. */
3008 struct buffer_head *old_bh2;
3009 struct ext4_dir_entry_2 *old_de2;
3011 old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
3013 retval = ext4_delete_entry(handle, old_dir,
3019 ext4_warning(old_dir->i_sb,
3020 "Deleting old file (%lu), %d, error=%d",
3021 old_dir->i_ino, old_dir->i_nlink, retval);
3025 ext4_dec_count(handle, new_inode);
3026 new_inode->i_ctime = ext4_current_time(new_inode);
3028 old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
3029 ext4_update_dx_flag(old_dir);
3031 PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) =
3032 cpu_to_le32(new_dir->i_ino);
3033 BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
3034 if (is_dx(old_inode)) {
3035 retval = ext4_handle_dirty_dx_node(handle,
3039 retval = ext4_handle_dirty_dirent_node(handle,
3044 ext4_std_error(old_dir->i_sb, retval);
3047 ext4_dec_count(handle, old_dir);
3049 /* checked empty_dir above, can't have another parent,
3050 * ext4_dec_count() won't work for many-linked dirs */
3051 clear_nlink(new_inode);
3053 ext4_inc_count(handle, new_dir);
3054 ext4_update_dx_flag(new_dir);
3055 ext4_mark_inode_dirty(handle, new_dir);
3058 ext4_mark_inode_dirty(handle, old_dir);
3060 ext4_mark_inode_dirty(handle, new_inode);
3061 if (!new_inode->i_nlink)
3062 ext4_orphan_add(handle, new_inode);
3063 if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC))
3072 ext4_journal_stop(handle);
3073 if (retval == 0 && force_da_alloc)
3074 ext4_alloc_da_blocks(old_inode);
3079 * directories can handle most operations...
3081 const struct inode_operations ext4_dir_inode_operations = {
3082 .create = ext4_create,
3083 .lookup = ext4_lookup,
3085 .unlink = ext4_unlink,
3086 .symlink = ext4_symlink,
3087 .mkdir = ext4_mkdir,
3088 .rmdir = ext4_rmdir,
3089 .mknod = ext4_mknod,
3090 .rename = ext4_rename,
3091 .setattr = ext4_setattr,
3092 #ifdef CONFIG_EXT4_FS_XATTR
3093 .setxattr = generic_setxattr,
3094 .getxattr = generic_getxattr,
3095 .listxattr = ext4_listxattr,
3096 .removexattr = generic_removexattr,
3098 .get_acl = ext4_get_acl,
3099 .fiemap = ext4_fiemap,
3102 const struct inode_operations ext4_special_inode_operations = {
3103 .setattr = ext4_setattr,
3104 #ifdef CONFIG_EXT4_FS_XATTR
3105 .setxattr = generic_setxattr,
3106 .getxattr = generic_getxattr,
3107 .listxattr = ext4_listxattr,
3108 .removexattr = generic_removexattr,
3110 .get_acl = ext4_get_acl,