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 void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
206 unsigned int blocksize)
208 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
209 t->det_rec_len = ext4_rec_len_to_disk(
210 sizeof(struct ext4_dir_entry_tail), blocksize);
211 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
214 /* Walk through a dirent block to find a checksum "dirent" at the tail */
215 static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
216 struct ext4_dir_entry *de)
218 struct ext4_dir_entry_tail *t;
221 struct ext4_dir_entry *d, *top;
224 top = (struct ext4_dir_entry *)(((void *)de) +
225 (EXT4_BLOCK_SIZE(inode->i_sb) -
226 sizeof(struct ext4_dir_entry_tail)));
227 while (d < top && d->rec_len)
228 d = (struct ext4_dir_entry *)(((void *)d) +
229 le16_to_cpu(d->rec_len));
234 t = (struct ext4_dir_entry_tail *)d;
236 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
239 if (t->det_reserved_zero1 ||
240 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
241 t->det_reserved_zero2 ||
242 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
248 static __le32 ext4_dirent_csum(struct inode *inode,
249 struct ext4_dir_entry *dirent, int size)
251 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
252 struct ext4_inode_info *ei = EXT4_I(inode);
255 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
256 return cpu_to_le32(csum);
259 static void warn_no_space_for_csum(struct inode *inode)
261 ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for "
262 "checksum. Please run e2fsck -D.", inode->i_ino);
265 int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
267 struct ext4_dir_entry_tail *t;
269 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
270 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
273 t = get_dirent_tail(inode, dirent);
275 warn_no_space_for_csum(inode);
279 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
280 (void *)t - (void *)dirent))
286 static void ext4_dirent_csum_set(struct inode *inode,
287 struct ext4_dir_entry *dirent)
289 struct ext4_dir_entry_tail *t;
291 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
292 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
295 t = get_dirent_tail(inode, dirent);
297 warn_no_space_for_csum(inode);
301 t->det_checksum = ext4_dirent_csum(inode, dirent,
302 (void *)t - (void *)dirent);
305 int ext4_handle_dirty_dirent_node(handle_t *handle,
307 struct buffer_head *bh)
309 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
310 return ext4_handle_dirty_metadata(handle, inode, bh);
313 static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
314 struct ext4_dir_entry *dirent,
317 struct ext4_dir_entry *dp;
318 struct dx_root_info *root;
321 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
323 else if (le16_to_cpu(dirent->rec_len) == 12) {
324 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
325 if (le16_to_cpu(dp->rec_len) !=
326 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
328 root = (struct dx_root_info *)(((void *)dp + 12));
329 if (root->reserved_zero ||
330 root->info_length != sizeof(struct dx_root_info))
337 *offset = count_offset;
338 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
341 static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
342 int count_offset, int count, struct dx_tail *t)
344 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
345 struct ext4_inode_info *ei = EXT4_I(inode);
346 __u32 csum, old_csum;
349 size = count_offset + (count * sizeof(struct dx_entry));
350 old_csum = t->dt_checksum;
352 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
353 csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
354 t->dt_checksum = old_csum;
356 return cpu_to_le32(csum);
359 static int ext4_dx_csum_verify(struct inode *inode,
360 struct ext4_dir_entry *dirent)
362 struct dx_countlimit *c;
364 int count_offset, limit, count;
366 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
367 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
370 c = get_dx_countlimit(inode, dirent, &count_offset);
372 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
375 limit = le16_to_cpu(c->limit);
376 count = le16_to_cpu(c->count);
377 if (count_offset + (limit * sizeof(struct dx_entry)) >
378 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
379 warn_no_space_for_csum(inode);
382 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
384 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
390 static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
392 struct dx_countlimit *c;
394 int count_offset, limit, count;
396 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
397 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
400 c = get_dx_countlimit(inode, dirent, &count_offset);
402 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
405 limit = le16_to_cpu(c->limit);
406 count = le16_to_cpu(c->count);
407 if (count_offset + (limit * sizeof(struct dx_entry)) >
408 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
409 warn_no_space_for_csum(inode);
412 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
414 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
417 static inline int ext4_handle_dirty_dx_node(handle_t *handle,
419 struct buffer_head *bh)
421 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
422 return ext4_handle_dirty_metadata(handle, inode, bh);
426 * p is at least 6 bytes before the end of page
428 static inline struct ext4_dir_entry_2 *
429 ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
431 return (struct ext4_dir_entry_2 *)((char *)p +
432 ext4_rec_len_from_disk(p->rec_len, blocksize));
436 * Future: use high four bits of block for coalesce-on-delete flags
437 * Mask them off for now.
440 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
442 return le32_to_cpu(entry->block) & 0x00ffffff;
445 static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
447 entry->block = cpu_to_le32(value);
450 static inline unsigned dx_get_hash(struct dx_entry *entry)
452 return le32_to_cpu(entry->hash);
455 static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
457 entry->hash = cpu_to_le32(value);
460 static inline unsigned dx_get_count(struct dx_entry *entries)
462 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
465 static inline unsigned dx_get_limit(struct dx_entry *entries)
467 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
470 static inline void dx_set_count(struct dx_entry *entries, unsigned value)
472 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
475 static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
477 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
480 static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
482 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
483 EXT4_DIR_REC_LEN(2) - infosize;
485 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
486 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
487 entry_space -= sizeof(struct dx_tail);
488 return entry_space / sizeof(struct dx_entry);
491 static inline unsigned dx_node_limit(struct inode *dir)
493 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
495 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
496 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
497 entry_space -= sizeof(struct dx_tail);
498 return entry_space / sizeof(struct dx_entry);
505 static void dx_show_index(char * label, struct dx_entry *entries)
507 int i, n = dx_get_count (entries);
508 printk(KERN_DEBUG "%s index ", label);
509 for (i = 0; i < n; i++) {
510 printk("%x->%lu ", i ? dx_get_hash(entries + i) :
511 0, (unsigned long)dx_get_block(entries + i));
523 static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
524 int size, int show_names)
526 unsigned names = 0, space = 0;
527 char *base = (char *) de;
528 struct dx_hash_info h = *hinfo;
531 while ((char *) de < base + size)
537 int len = de->name_len;
538 char *name = de->name;
539 while (len--) printk("%c", *name++);
540 ext4fs_dirhash(de->name, de->name_len, &h);
541 printk(":%x.%u ", h.hash,
542 (unsigned) ((char *) de - base));
544 space += EXT4_DIR_REC_LEN(de->name_len);
547 de = ext4_next_entry(de, size);
549 printk("(%i)\n", names);
550 return (struct stats) { names, space, 1 };
553 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
554 struct dx_entry *entries, int levels)
556 unsigned blocksize = dir->i_sb->s_blocksize;
557 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
559 struct buffer_head *bh;
561 printk("%i indexed blocks...\n", count);
562 for (i = 0; i < count; i++, entries++)
564 ext4_lblk_t block = dx_get_block(entries);
565 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
566 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
568 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
569 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
571 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
572 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
573 names += stats.names;
574 space += stats.space;
575 bcount += stats.bcount;
579 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
580 levels ? "" : " ", names, space/bcount,
581 (space/bcount)*100/blocksize);
582 return (struct stats) { names, space, bcount};
584 #endif /* DX_DEBUG */
587 * Probe for a directory leaf block to search.
589 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
590 * error in the directory index, and the caller should fall back to
591 * searching the directory normally. The callers of dx_probe **MUST**
592 * check for this error code, and make sure it never gets reflected
595 static struct dx_frame *
596 dx_probe(const struct qstr *d_name, struct inode *dir,
597 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
599 unsigned count, indirect;
600 struct dx_entry *at, *entries, *p, *q, *m;
601 struct dx_root *root;
602 struct buffer_head *bh;
603 struct dx_frame *frame = frame_in;
607 if (!(bh = ext4_bread(NULL, dir, 0, 0, err))) {
609 *err = ERR_BAD_DX_DIR;
612 root = (struct dx_root *) bh->b_data;
613 if (root->info.hash_version != DX_HASH_TEA &&
614 root->info.hash_version != DX_HASH_HALF_MD4 &&
615 root->info.hash_version != DX_HASH_LEGACY) {
616 ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
617 root->info.hash_version);
619 *err = ERR_BAD_DX_DIR;
622 hinfo->hash_version = root->info.hash_version;
623 if (hinfo->hash_version <= DX_HASH_TEA)
624 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
625 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
627 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
630 if (root->info.unused_flags & 1) {
631 ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
632 root->info.unused_flags);
634 *err = ERR_BAD_DX_DIR;
638 if ((indirect = root->info.indirect_levels) > 1) {
639 ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
640 root->info.indirect_levels);
642 *err = ERR_BAD_DX_DIR;
646 if (!buffer_verified(bh) &&
647 !ext4_dx_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) {
648 ext4_warning(dir->i_sb, "Root failed checksum");
650 *err = ERR_BAD_DX_DIR;
653 set_buffer_verified(bh);
655 entries = (struct dx_entry *) (((char *)&root->info) +
656 root->info.info_length);
658 if (dx_get_limit(entries) != dx_root_limit(dir,
659 root->info.info_length)) {
660 ext4_warning(dir->i_sb, "dx entry: limit != root limit");
662 *err = ERR_BAD_DX_DIR;
666 dxtrace(printk("Look up %x", hash));
669 count = dx_get_count(entries);
670 if (!count || count > dx_get_limit(entries)) {
671 ext4_warning(dir->i_sb,
672 "dx entry: no count or count > limit");
674 *err = ERR_BAD_DX_DIR;
679 q = entries + count - 1;
683 dxtrace(printk("."));
684 if (dx_get_hash(m) > hash)
690 if (0) // linear search cross check
692 unsigned n = count - 1;
696 dxtrace(printk(","));
697 if (dx_get_hash(++at) > hash)
703 assert (at == p - 1);
707 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
709 frame->entries = entries;
711 if (!indirect--) return frame;
712 if (!(bh = ext4_bread(NULL, dir, dx_get_block(at), 0, err))) {
714 *err = ERR_BAD_DX_DIR;
717 entries = ((struct dx_node *) bh->b_data)->entries;
719 if (!buffer_verified(bh) &&
720 !ext4_dx_csum_verify(dir,
721 (struct ext4_dir_entry *)bh->b_data)) {
722 ext4_warning(dir->i_sb, "Node failed checksum");
724 *err = ERR_BAD_DX_DIR;
727 set_buffer_verified(bh);
729 if (dx_get_limit(entries) != dx_node_limit (dir)) {
730 ext4_warning(dir->i_sb,
731 "dx entry: limit != node limit");
733 *err = ERR_BAD_DX_DIR;
740 while (frame >= frame_in) {
745 if (*err == ERR_BAD_DX_DIR)
746 ext4_warning(dir->i_sb,
747 "Corrupt dir inode %lu, running e2fsck is "
748 "recommended.", dir->i_ino);
752 static void dx_release (struct dx_frame *frames)
754 if (frames[0].bh == NULL)
757 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
758 brelse(frames[1].bh);
759 brelse(frames[0].bh);
763 * This function increments the frame pointer to search the next leaf
764 * block, and reads in the necessary intervening nodes if the search
765 * should be necessary. Whether or not the search is necessary is
766 * controlled by the hash parameter. If the hash value is even, then
767 * the search is only continued if the next block starts with that
768 * hash value. This is used if we are searching for a specific file.
770 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
772 * This function returns 1 if the caller should continue to search,
773 * or 0 if it should not. If there is an error reading one of the
774 * index blocks, it will a negative error code.
776 * If start_hash is non-null, it will be filled in with the starting
777 * hash of the next page.
779 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
780 struct dx_frame *frame,
781 struct dx_frame *frames,
785 struct buffer_head *bh;
786 int err, num_frames = 0;
791 * Find the next leaf page by incrementing the frame pointer.
792 * If we run out of entries in the interior node, loop around and
793 * increment pointer in the parent node. When we break out of
794 * this loop, num_frames indicates the number of interior
795 * nodes need to be read.
798 if (++(p->at) < p->entries + dx_get_count(p->entries))
807 * If the hash is 1, then continue only if the next page has a
808 * continuation hash of any value. This is used for readdir
809 * handling. Otherwise, check to see if the hash matches the
810 * desired contiuation hash. If it doesn't, return since
811 * there's no point to read in the successive index pages.
813 bhash = dx_get_hash(p->at);
816 if ((hash & 1) == 0) {
817 if ((bhash & ~1) != hash)
821 * If the hash is HASH_NB_ALWAYS, we always go to the next
822 * block so no check is necessary
824 while (num_frames--) {
825 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
828 ext4_error(dir->i_sb,
829 "Directory hole detected on inode %lu\n",
833 return err; /* Failure */
836 if (!buffer_verified(bh) &&
837 !ext4_dx_csum_verify(dir,
838 (struct ext4_dir_entry *)bh->b_data)) {
839 ext4_warning(dir->i_sb, "Node failed checksum");
843 set_buffer_verified(bh);
848 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
855 * This function fills a red-black tree with information from a
856 * directory block. It returns the number directory entries loaded
857 * into the tree. If there is an error it is returned in err.
859 static int htree_dirblock_to_tree(struct file *dir_file,
860 struct inode *dir, ext4_lblk_t block,
861 struct dx_hash_info *hinfo,
862 __u32 start_hash, __u32 start_minor_hash)
864 struct buffer_head *bh;
865 struct ext4_dir_entry_2 *de, *top;
866 int err = 0, count = 0;
868 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
869 (unsigned long)block));
870 if (!(bh = ext4_bread(NULL, dir, block, 0, &err))) {
873 ext4_error(dir->i_sb,
874 "Directory hole detected on inode %lu\n",
880 if (!buffer_verified(bh) &&
881 !ext4_dirent_csum_verify(dir,
882 (struct ext4_dir_entry *)bh->b_data)) {
886 set_buffer_verified(bh);
888 de = (struct ext4_dir_entry_2 *) bh->b_data;
889 top = (struct ext4_dir_entry_2 *) ((char *) de +
890 dir->i_sb->s_blocksize -
891 EXT4_DIR_REC_LEN(0));
892 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
893 if (ext4_check_dir_entry(dir, NULL, de, bh,
894 bh->b_data, bh->b_size,
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));
1012 static inline int search_dirblock(struct buffer_head *bh,
1014 const struct qstr *d_name,
1015 unsigned int offset,
1016 struct ext4_dir_entry_2 **res_dir)
1018 return search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir,
1019 d_name, offset, res_dir);
1023 * Directory block splitting, compacting
1027 * Create map of hash values, offsets, and sizes, stored at end of block.
1028 * Returns number of entries mapped.
1030 static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
1031 struct dx_hash_info *hinfo,
1032 struct dx_map_entry *map_tail)
1035 char *base = (char *) de;
1036 struct dx_hash_info h = *hinfo;
1038 while ((char *) de < base + blocksize) {
1039 if (de->name_len && de->inode) {
1040 ext4fs_dirhash(de->name, de->name_len, &h);
1042 map_tail->hash = h.hash;
1043 map_tail->offs = ((char *) de - base)>>2;
1044 map_tail->size = le16_to_cpu(de->rec_len);
1048 /* XXX: do we need to check rec_len == 0 case? -Chris */
1049 de = ext4_next_entry(de, blocksize);
1054 /* Sort map by hash value */
1055 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1057 struct dx_map_entry *p, *q, *top = map + count - 1;
1059 /* Combsort until bubble sort doesn't suck */
1061 count = count*10/13;
1062 if (count - 9 < 2) /* 9, 10 -> 11 */
1064 for (p = top, q = p - count; q >= map; p--, q--)
1065 if (p->hash < q->hash)
1068 /* Garden variety bubble sort */
1073 if (q[1].hash >= q[0].hash)
1081 static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
1083 struct dx_entry *entries = frame->entries;
1084 struct dx_entry *old = frame->at, *new = old + 1;
1085 int count = dx_get_count(entries);
1087 assert(count < dx_get_limit(entries));
1088 assert(old < entries + count);
1089 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1090 dx_set_hash(new, hash);
1091 dx_set_block(new, block);
1092 dx_set_count(entries, count + 1);
1096 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
1098 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
1099 * `de != NULL' is guaranteed by caller.
1101 static inline int ext4_match (int len, const char * const name,
1102 struct ext4_dir_entry_2 * de)
1104 if (len != de->name_len)
1108 return !memcmp(name, de->name, len);
1112 * Returns 0 if not found, -1 on failure, and 1 on success
1114 int search_dir(struct buffer_head *bh,
1118 const struct qstr *d_name,
1119 unsigned int offset,
1120 struct ext4_dir_entry_2 **res_dir)
1122 struct ext4_dir_entry_2 * de;
1125 const char *name = d_name->name;
1126 int namelen = d_name->len;
1128 de = (struct ext4_dir_entry_2 *)search_buf;
1129 dlimit = search_buf + buf_size;
1130 while ((char *) de < dlimit) {
1131 /* this code is executed quadratically often */
1132 /* do minimal checking `by hand' */
1134 if ((char *) de + namelen <= dlimit &&
1135 ext4_match (namelen, name, de)) {
1136 /* found a match - just to be sure, do a full check */
1137 if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
1138 bh->b_size, offset))
1143 /* prevent looping on a bad block */
1144 de_len = ext4_rec_len_from_disk(de->rec_len,
1145 dir->i_sb->s_blocksize);
1149 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1154 static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block,
1155 struct ext4_dir_entry *de)
1157 struct super_block *sb = dir->i_sb;
1163 if (de->inode == 0 &&
1164 ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) ==
1173 * finds an entry in the specified directory with the wanted name. It
1174 * returns the cache buffer in which the entry was found, and the entry
1175 * itself (as a parameter - res_dir). It does NOT read the inode of the
1176 * entry - you'll have to do that yourself if you want to.
1178 * The returned buffer_head has ->b_count elevated. The caller is expected
1179 * to brelse() it when appropriate.
1181 static struct buffer_head * ext4_find_entry (struct inode *dir,
1182 const struct qstr *d_name,
1183 struct ext4_dir_entry_2 **res_dir,
1186 struct super_block *sb;
1187 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1188 struct buffer_head *bh, *ret = NULL;
1189 ext4_lblk_t start, block, b;
1190 const u8 *name = d_name->name;
1191 int ra_max = 0; /* Number of bh's in the readahead
1193 int ra_ptr = 0; /* Current index into readahead
1196 ext4_lblk_t nblocks;
1202 namelen = d_name->len;
1203 if (namelen > EXT4_NAME_LEN)
1206 if (ext4_has_inline_data(dir)) {
1207 int has_inline_data = 1;
1208 ret = ext4_find_inline_entry(dir, d_name, res_dir,
1210 if (has_inline_data) {
1217 if ((namelen <= 2) && (name[0] == '.') &&
1218 (name[1] == '.' || name[1] == '\0')) {
1220 * "." or ".." will only be in the first block
1221 * NFS may look up ".."; "." should be handled by the VFS
1228 bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
1230 * On success, or if the error was file not found,
1231 * return. Otherwise, fall back to doing a search the
1232 * old fashioned way.
1234 if (bh || (err != ERR_BAD_DX_DIR))
1236 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1239 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1240 start = EXT4_I(dir)->i_dir_start_lookup;
1241 if (start >= nblocks)
1247 * We deal with the read-ahead logic here.
1249 if (ra_ptr >= ra_max) {
1250 /* Refill the readahead buffer */
1253 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1255 * Terminate if we reach the end of the
1256 * directory and must wrap, or if our
1257 * search has finished at this block.
1259 if (b >= nblocks || (num && block == start)) {
1260 bh_use[ra_max] = NULL;
1264 bh = ext4_getblk(NULL, dir, b++, 0, &err);
1265 bh_use[ra_max] = bh;
1267 ll_rw_block(READ | REQ_META | REQ_PRIO,
1271 if ((bh = bh_use[ra_ptr++]) == NULL)
1274 if (!buffer_uptodate(bh)) {
1275 /* read error, skip block & hope for the best */
1276 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1277 (unsigned long) block);
1281 if (!buffer_verified(bh) &&
1282 !is_dx_internal_node(dir, block,
1283 (struct ext4_dir_entry *)bh->b_data) &&
1284 !ext4_dirent_csum_verify(dir,
1285 (struct ext4_dir_entry *)bh->b_data)) {
1286 EXT4_ERROR_INODE(dir, "checksumming directory "
1287 "block %lu", (unsigned long)block);
1291 set_buffer_verified(bh);
1292 i = search_dirblock(bh, dir, d_name,
1293 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
1295 EXT4_I(dir)->i_dir_start_lookup = block;
1297 goto cleanup_and_exit;
1301 goto cleanup_and_exit;
1304 if (++block >= nblocks)
1306 } while (block != start);
1309 * If the directory has grown while we were searching, then
1310 * search the last part of the directory before giving up.
1313 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1314 if (block < nblocks) {
1320 /* Clean up the read-ahead blocks */
1321 for (; ra_ptr < ra_max; ra_ptr++)
1322 brelse(bh_use[ra_ptr]);
1326 static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
1327 struct ext4_dir_entry_2 **res_dir, int *err)
1329 struct super_block * sb = dir->i_sb;
1330 struct dx_hash_info hinfo;
1331 struct dx_frame frames[2], *frame;
1332 struct buffer_head *bh;
1336 if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
1339 block = dx_get_block(frame->at);
1340 if (!(bh = ext4_bread(NULL, dir, block, 0, err))) {
1343 ext4_error(dir->i_sb,
1344 "Directory hole detected on inode %lu\n",
1350 if (!buffer_verified(bh) &&
1351 !ext4_dirent_csum_verify(dir,
1352 (struct ext4_dir_entry *)bh->b_data)) {
1353 EXT4_ERROR_INODE(dir, "checksumming directory "
1354 "block %lu", (unsigned long)block);
1359 set_buffer_verified(bh);
1360 retval = search_dirblock(bh, dir, d_name,
1361 block << EXT4_BLOCK_SIZE_BITS(sb),
1363 if (retval == 1) { /* Success! */
1369 *err = ERR_BAD_DX_DIR;
1373 /* Check to see if we should continue to search */
1374 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
1378 "error reading index page in directory #%lu",
1383 } while (retval == 1);
1387 dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
1388 dx_release (frames);
1392 static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1394 struct inode *inode;
1395 struct ext4_dir_entry_2 *de;
1396 struct buffer_head *bh;
1398 if (dentry->d_name.len > EXT4_NAME_LEN)
1399 return ERR_PTR(-ENAMETOOLONG);
1401 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
1404 __u32 ino = le32_to_cpu(de->inode);
1406 if (!ext4_valid_inum(dir->i_sb, ino)) {
1407 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
1408 return ERR_PTR(-EIO);
1410 if (unlikely(ino == dir->i_ino)) {
1411 EXT4_ERROR_INODE(dir, "'%.*s' linked to parent dir",
1413 dentry->d_name.name);
1414 return ERR_PTR(-EIO);
1416 inode = ext4_iget(dir->i_sb, ino);
1417 if (inode == ERR_PTR(-ESTALE)) {
1418 EXT4_ERROR_INODE(dir,
1419 "deleted inode referenced: %u",
1421 return ERR_PTR(-EIO);
1424 return d_splice_alias(inode, dentry);
1428 struct dentry *ext4_get_parent(struct dentry *child)
1431 static const struct qstr dotdot = QSTR_INIT("..", 2);
1432 struct ext4_dir_entry_2 * de;
1433 struct buffer_head *bh;
1435 bh = ext4_find_entry(child->d_inode, &dotdot, &de, NULL);
1437 return ERR_PTR(-ENOENT);
1438 ino = le32_to_cpu(de->inode);
1441 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
1442 EXT4_ERROR_INODE(child->d_inode,
1443 "bad parent inode number: %u", ino);
1444 return ERR_PTR(-EIO);
1447 return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
1451 static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1452 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
1453 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
1454 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
1455 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
1456 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
1457 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
1458 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
1461 static inline void ext4_set_de_type(struct super_block *sb,
1462 struct ext4_dir_entry_2 *de,
1464 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1465 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1469 * Move count entries from end of map between two memory locations.
1470 * Returns pointer to last entry moved.
1472 static struct ext4_dir_entry_2 *
1473 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1476 unsigned rec_len = 0;
1479 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
1480 (from + (map->offs<<2));
1481 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1482 memcpy (to, de, rec_len);
1483 ((struct ext4_dir_entry_2 *) to)->rec_len =
1484 ext4_rec_len_to_disk(rec_len, blocksize);
1489 return (struct ext4_dir_entry_2 *) (to - rec_len);
1493 * Compact each dir entry in the range to the minimal rec_len.
1494 * Returns pointer to last entry in range.
1496 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
1498 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
1499 unsigned rec_len = 0;
1502 while ((char*)de < base + blocksize) {
1503 next = ext4_next_entry(de, blocksize);
1504 if (de->inode && de->name_len) {
1505 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1507 memmove(to, de, rec_len);
1508 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
1510 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
1518 * Split a full leaf block to make room for a new dir entry.
1519 * Allocate a new block, and move entries so that they are approx. equally full.
1520 * Returns pointer to de in block into which the new entry will be inserted.
1522 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1523 struct buffer_head **bh,struct dx_frame *frame,
1524 struct dx_hash_info *hinfo, int *error)
1526 unsigned blocksize = dir->i_sb->s_blocksize;
1527 unsigned count, continued;
1528 struct buffer_head *bh2;
1529 ext4_lblk_t newblock;
1531 struct dx_map_entry *map;
1532 char *data1 = (*bh)->b_data, *data2;
1533 unsigned split, move, size;
1534 struct ext4_dir_entry_2 *de = NULL, *de2;
1535 struct ext4_dir_entry_tail *t;
1539 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
1540 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1541 csum_size = sizeof(struct ext4_dir_entry_tail);
1543 bh2 = ext4_append (handle, dir, &newblock, &err);
1550 BUFFER_TRACE(*bh, "get_write_access");
1551 err = ext4_journal_get_write_access(handle, *bh);
1555 BUFFER_TRACE(frame->bh, "get_write_access");
1556 err = ext4_journal_get_write_access(handle, frame->bh);
1560 data2 = bh2->b_data;
1562 /* create map in the end of data2 block */
1563 map = (struct dx_map_entry *) (data2 + blocksize);
1564 count = dx_make_map((struct ext4_dir_entry_2 *) data1,
1565 blocksize, hinfo, map);
1567 dx_sort_map(map, count);
1568 /* Split the existing block in the middle, size-wise */
1571 for (i = count-1; i >= 0; i--) {
1572 /* is more than half of this entry in 2nd half of the block? */
1573 if (size + map[i].size/2 > blocksize/2)
1575 size += map[i].size;
1578 /* map index at which we will split */
1579 split = count - move;
1580 hash2 = map[split].hash;
1581 continued = hash2 == map[split - 1].hash;
1582 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1583 (unsigned long)dx_get_block(frame->at),
1584 hash2, split, count-split));
1586 /* Fancy dance to stay within two buffers */
1587 de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
1588 de = dx_pack_dirents(data1, blocksize);
1589 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1592 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1596 t = EXT4_DIRENT_TAIL(data2, blocksize);
1597 initialize_dirent_tail(t, blocksize);
1599 t = EXT4_DIRENT_TAIL(data1, blocksize);
1600 initialize_dirent_tail(t, blocksize);
1603 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1604 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
1606 /* Which block gets the new entry? */
1607 if (hinfo->hash >= hash2)
1612 dx_insert_block(frame, hash2 + continued, newblock);
1613 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
1616 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1620 dxtrace(dx_show_index("frame", frame->entries));
1627 ext4_std_error(dir->i_sb, err);
1633 int ext4_find_dest_de(struct inode *dir, struct inode *inode,
1634 struct buffer_head *bh,
1635 void *buf, int buf_size,
1636 const char *name, int namelen,
1637 struct ext4_dir_entry_2 **dest_de)
1639 struct ext4_dir_entry_2 *de;
1640 unsigned short reclen = EXT4_DIR_REC_LEN(namelen);
1642 unsigned int offset = 0;
1645 de = (struct ext4_dir_entry_2 *)buf;
1646 top = buf + buf_size - reclen;
1647 while ((char *) de <= top) {
1648 if (ext4_check_dir_entry(dir, NULL, de, bh,
1649 buf, buf_size, offset))
1651 if (ext4_match(namelen, name, de))
1653 nlen = EXT4_DIR_REC_LEN(de->name_len);
1654 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1655 if ((de->inode ? rlen - nlen : rlen) >= reclen)
1657 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1660 if ((char *) de > top)
1667 void ext4_insert_dentry(struct inode *inode,
1668 struct ext4_dir_entry_2 *de,
1670 const char *name, int namelen)
1675 nlen = EXT4_DIR_REC_LEN(de->name_len);
1676 rlen = ext4_rec_len_from_disk(de->rec_len, buf_size);
1678 struct ext4_dir_entry_2 *de1 =
1679 (struct ext4_dir_entry_2 *)((char *)de + nlen);
1680 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size);
1681 de->rec_len = ext4_rec_len_to_disk(nlen, buf_size);
1684 de->file_type = EXT4_FT_UNKNOWN;
1685 de->inode = cpu_to_le32(inode->i_ino);
1686 ext4_set_de_type(inode->i_sb, de, inode->i_mode);
1687 de->name_len = namelen;
1688 memcpy(de->name, name, namelen);
1691 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1692 * it points to a directory entry which is guaranteed to be large
1693 * enough for new directory entry. If de is NULL, then
1694 * add_dirent_to_buf will attempt search the directory block for
1695 * space. It will return -ENOSPC if no space is available, and -EIO
1696 * and -EEXIST if directory entry already exists.
1698 static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1699 struct inode *inode, struct ext4_dir_entry_2 *de,
1700 struct buffer_head *bh)
1702 struct inode *dir = dentry->d_parent->d_inode;
1703 const char *name = dentry->d_name.name;
1704 int namelen = dentry->d_name.len;
1705 unsigned int blocksize = dir->i_sb->s_blocksize;
1709 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1710 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1711 csum_size = sizeof(struct ext4_dir_entry_tail);
1714 err = ext4_find_dest_de(dir, inode,
1715 bh, bh->b_data, blocksize - csum_size,
1716 name, namelen, &de);
1720 BUFFER_TRACE(bh, "get_write_access");
1721 err = ext4_journal_get_write_access(handle, bh);
1723 ext4_std_error(dir->i_sb, err);
1727 /* By now the buffer is marked for journaling */
1728 ext4_insert_dentry(inode, de, blocksize, name, namelen);
1731 * XXX shouldn't update any times until successful
1732 * completion of syscall, but too many callers depend
1735 * XXX similarly, too many callers depend on
1736 * ext4_new_inode() setting the times, but error
1737 * recovery deletes the inode, so the worst that can
1738 * happen is that the times are slightly out of date
1739 * and/or different from the directory change time.
1741 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1742 ext4_update_dx_flag(dir);
1744 ext4_mark_inode_dirty(handle, dir);
1745 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1746 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
1748 ext4_std_error(dir->i_sb, err);
1753 * This converts a one block unindexed directory to a 3 block indexed
1754 * directory, and adds the dentry to the indexed directory.
1756 static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1757 struct inode *inode, struct buffer_head *bh)
1759 struct inode *dir = dentry->d_parent->d_inode;
1760 const char *name = dentry->d_name.name;
1761 int namelen = dentry->d_name.len;
1762 struct buffer_head *bh2;
1763 struct dx_root *root;
1764 struct dx_frame frames[2], *frame;
1765 struct dx_entry *entries;
1766 struct ext4_dir_entry_2 *de, *de2;
1767 struct ext4_dir_entry_tail *t;
1772 struct dx_hash_info hinfo;
1774 struct fake_dirent *fde;
1777 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1778 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1779 csum_size = sizeof(struct ext4_dir_entry_tail);
1781 blocksize = dir->i_sb->s_blocksize;
1782 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
1783 retval = ext4_journal_get_write_access(handle, bh);
1785 ext4_std_error(dir->i_sb, retval);
1789 root = (struct dx_root *) bh->b_data;
1791 /* The 0th block becomes the root, move the dirents out */
1792 fde = &root->dotdot;
1793 de = (struct ext4_dir_entry_2 *)((char *)fde +
1794 ext4_rec_len_from_disk(fde->rec_len, blocksize));
1795 if ((char *) de >= (((char *) root) + blocksize)) {
1796 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
1800 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
1802 /* Allocate new block for the 0th block's dirents */
1803 bh2 = ext4_append(handle, dir, &block, &retval);
1808 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
1809 data1 = bh2->b_data;
1811 memcpy (data1, de, len);
1812 de = (struct ext4_dir_entry_2 *) data1;
1814 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
1816 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1821 t = EXT4_DIRENT_TAIL(data1, blocksize);
1822 initialize_dirent_tail(t, blocksize);
1825 /* Initialize the root; the dot dirents already exist */
1826 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
1827 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
1829 memset (&root->info, 0, sizeof(root->info));
1830 root->info.info_length = sizeof(root->info);
1831 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
1832 entries = root->entries;
1833 dx_set_block(entries, 1);
1834 dx_set_count(entries, 1);
1835 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
1837 /* Initialize as for dx_probe */
1838 hinfo.hash_version = root->info.hash_version;
1839 if (hinfo.hash_version <= DX_HASH_TEA)
1840 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
1841 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1842 ext4fs_dirhash(name, namelen, &hinfo);
1844 frame->entries = entries;
1845 frame->at = entries;
1849 ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1850 ext4_handle_dirty_dirent_node(handle, dir, bh);
1852 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1855 * Even if the block split failed, we have to properly write
1856 * out all the changes we did so far. Otherwise we can end up
1857 * with corrupted filesystem.
1859 ext4_mark_inode_dirty(handle, dir);
1865 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1873 * adds a file entry to the specified directory, using the same
1874 * semantics as ext4_find_entry(). It returns NULL if it failed.
1876 * NOTE!! The inode part of 'de' is left at 0 - which means you
1877 * may not sleep between calling this and putting something into
1878 * the entry, as someone else might have used it while you slept.
1880 static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1881 struct inode *inode)
1883 struct inode *dir = dentry->d_parent->d_inode;
1884 struct buffer_head *bh;
1885 struct ext4_dir_entry_2 *de;
1886 struct ext4_dir_entry_tail *t;
1887 struct super_block *sb;
1891 ext4_lblk_t block, blocks;
1894 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1895 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1896 csum_size = sizeof(struct ext4_dir_entry_tail);
1899 blocksize = sb->s_blocksize;
1900 if (!dentry->d_name.len)
1903 if (ext4_has_inline_data(dir)) {
1904 retval = ext4_try_add_inline_entry(handle, dentry, inode);
1914 retval = ext4_dx_add_entry(handle, dentry, inode);
1915 if (!retval || (retval != ERR_BAD_DX_DIR))
1917 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
1919 ext4_mark_inode_dirty(handle, dir);
1921 blocks = dir->i_size >> sb->s_blocksize_bits;
1922 for (block = 0; block < blocks; block++) {
1923 if (!(bh = ext4_bread(handle, dir, block, 0, &retval))) {
1926 ext4_error(inode->i_sb,
1927 "Directory hole detected on inode %lu\n",
1932 if (!buffer_verified(bh) &&
1933 !ext4_dirent_csum_verify(dir,
1934 (struct ext4_dir_entry *)bh->b_data)) {
1938 set_buffer_verified(bh);
1939 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1940 if (retval != -ENOSPC) {
1945 if (blocks == 1 && !dx_fallback &&
1946 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
1947 return make_indexed_dir(handle, dentry, inode, bh);
1950 bh = ext4_append(handle, dir, &block, &retval);
1953 de = (struct ext4_dir_entry_2 *) bh->b_data;
1955 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
1958 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
1959 initialize_dirent_tail(t, blocksize);
1962 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1965 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
1970 * Returns 0 for success, or a negative error value
1972 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
1973 struct inode *inode)
1975 struct dx_frame frames[2], *frame;
1976 struct dx_entry *entries, *at;
1977 struct dx_hash_info hinfo;
1978 struct buffer_head *bh;
1979 struct inode *dir = dentry->d_parent->d_inode;
1980 struct super_block *sb = dir->i_sb;
1981 struct ext4_dir_entry_2 *de;
1984 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
1987 entries = frame->entries;
1990 if (!(bh = ext4_bread(handle, dir, dx_get_block(frame->at), 0, &err))) {
1993 ext4_error(dir->i_sb,
1994 "Directory hole detected on inode %lu\n",
2000 if (!buffer_verified(bh) &&
2001 !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
2003 set_buffer_verified(bh);
2005 BUFFER_TRACE(bh, "get_write_access");
2006 err = ext4_journal_get_write_access(handle, bh);
2010 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
2014 /* Block full, should compress but for now just split */
2015 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
2016 dx_get_count(entries), dx_get_limit(entries)));
2017 /* Need to split index? */
2018 if (dx_get_count(entries) == dx_get_limit(entries)) {
2019 ext4_lblk_t newblock;
2020 unsigned icount = dx_get_count(entries);
2021 int levels = frame - frames;
2022 struct dx_entry *entries2;
2023 struct dx_node *node2;
2024 struct buffer_head *bh2;
2026 if (levels && (dx_get_count(frames->entries) ==
2027 dx_get_limit(frames->entries))) {
2028 ext4_warning(sb, "Directory index full!");
2032 bh2 = ext4_append (handle, dir, &newblock, &err);
2035 node2 = (struct dx_node *)(bh2->b_data);
2036 entries2 = node2->entries;
2037 memset(&node2->fake, 0, sizeof(struct fake_dirent));
2038 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
2040 BUFFER_TRACE(frame->bh, "get_write_access");
2041 err = ext4_journal_get_write_access(handle, frame->bh);
2045 unsigned icount1 = icount/2, icount2 = icount - icount1;
2046 unsigned hash2 = dx_get_hash(entries + icount1);
2047 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
2050 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
2051 err = ext4_journal_get_write_access(handle,
2056 memcpy((char *) entries2, (char *) (entries + icount1),
2057 icount2 * sizeof(struct dx_entry));
2058 dx_set_count(entries, icount1);
2059 dx_set_count(entries2, icount2);
2060 dx_set_limit(entries2, dx_node_limit(dir));
2062 /* Which index block gets the new entry? */
2063 if (at - entries >= icount1) {
2064 frame->at = at = at - entries - icount1 + entries2;
2065 frame->entries = entries = entries2;
2066 swap(frame->bh, bh2);
2068 dx_insert_block(frames + 0, hash2, newblock);
2069 dxtrace(dx_show_index("node", frames[1].entries));
2070 dxtrace(dx_show_index("node",
2071 ((struct dx_node *) bh2->b_data)->entries));
2072 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
2077 dxtrace(printk(KERN_DEBUG
2078 "Creating second level index...\n"));
2079 memcpy((char *) entries2, (char *) entries,
2080 icount * sizeof(struct dx_entry));
2081 dx_set_limit(entries2, dx_node_limit(dir));
2084 dx_set_count(entries, 1);
2085 dx_set_block(entries + 0, newblock);
2086 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
2088 /* Add new access path frame */
2090 frame->at = at = at - entries + entries2;
2091 frame->entries = entries = entries2;
2093 err = ext4_journal_get_write_access(handle,
2098 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
2100 ext4_std_error(inode->i_sb, err);
2104 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
2107 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
2111 ext4_std_error(dir->i_sb, err);
2119 * ext4_generic_delete_entry deletes a directory entry by merging it
2120 * with the previous entry
2122 int ext4_generic_delete_entry(handle_t *handle,
2124 struct ext4_dir_entry_2 *de_del,
2125 struct buffer_head *bh,
2130 struct ext4_dir_entry_2 *de, *pde;
2131 unsigned int blocksize = dir->i_sb->s_blocksize;
2136 de = (struct ext4_dir_entry_2 *)entry_buf;
2137 while (i < buf_size - csum_size) {
2138 if (ext4_check_dir_entry(dir, NULL, de, bh,
2139 bh->b_data, bh->b_size, i))
2143 pde->rec_len = ext4_rec_len_to_disk(
2144 ext4_rec_len_from_disk(pde->rec_len,
2146 ext4_rec_len_from_disk(de->rec_len,
2154 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
2156 de = ext4_next_entry(de, blocksize);
2161 static int ext4_delete_entry(handle_t *handle,
2163 struct ext4_dir_entry_2 *de_del,
2164 struct buffer_head *bh)
2166 int err, csum_size = 0;
2168 if (ext4_has_inline_data(dir)) {
2169 int has_inline_data = 1;
2170 err = ext4_delete_inline_entry(handle, dir, de_del, bh,
2172 if (has_inline_data)
2176 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2177 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2178 csum_size = sizeof(struct ext4_dir_entry_tail);
2180 BUFFER_TRACE(bh, "get_write_access");
2181 err = ext4_journal_get_write_access(handle, bh);
2185 err = ext4_generic_delete_entry(handle, dir, de_del,
2187 dir->i_sb->s_blocksize, csum_size);
2191 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2192 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2199 ext4_std_error(dir->i_sb, err);
2204 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2205 * since this indicates that nlinks count was previously 1.
2207 static void ext4_inc_count(handle_t *handle, struct inode *inode)
2210 if (is_dx(inode) && inode->i_nlink > 1) {
2211 /* limit is 16-bit i_links_count */
2212 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
2213 set_nlink(inode, 1);
2214 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2215 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2221 * If a directory had nlink == 1, then we should let it be 1. This indicates
2222 * directory has >EXT4_LINK_MAX subdirs.
2224 static void ext4_dec_count(handle_t *handle, struct inode *inode)
2226 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2231 static int ext4_add_nondir(handle_t *handle,
2232 struct dentry *dentry, struct inode *inode)
2234 int err = ext4_add_entry(handle, dentry, inode);
2236 ext4_mark_inode_dirty(handle, inode);
2237 unlock_new_inode(inode);
2238 d_instantiate(dentry, inode);
2242 unlock_new_inode(inode);
2248 * By the time this is called, we already have created
2249 * the directory cache entry for the new file, but it
2250 * is so far negative - it has no inode.
2252 * If the create succeeds, we fill in the inode information
2253 * with d_instantiate().
2255 static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2259 struct inode *inode;
2260 int err, retries = 0;
2262 dquot_initialize(dir);
2265 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2266 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2267 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2268 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)));
2270 return PTR_ERR(handle);
2272 if (IS_DIRSYNC(dir))
2273 ext4_handle_sync(handle);
2275 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
2276 err = PTR_ERR(inode);
2277 if (!IS_ERR(inode)) {
2278 inode->i_op = &ext4_file_inode_operations;
2279 inode->i_fop = &ext4_file_operations;
2280 ext4_set_aops(inode);
2281 err = ext4_add_nondir(handle, dentry, inode);
2283 ext4_journal_stop(handle);
2284 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2289 static int ext4_mknod(struct inode *dir, struct dentry *dentry,
2290 umode_t mode, dev_t rdev)
2293 struct inode *inode;
2294 int err, retries = 0;
2296 if (!new_valid_dev(rdev))
2299 dquot_initialize(dir);
2302 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2303 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2304 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2305 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)));
2307 return PTR_ERR(handle);
2309 if (IS_DIRSYNC(dir))
2310 ext4_handle_sync(handle);
2312 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
2313 err = PTR_ERR(inode);
2314 if (!IS_ERR(inode)) {
2315 init_special_inode(inode, inode->i_mode, rdev);
2316 inode->i_op = &ext4_special_inode_operations;
2317 err = ext4_add_nondir(handle, dentry, inode);
2319 ext4_journal_stop(handle);
2320 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2325 struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
2326 struct ext4_dir_entry_2 *de,
2327 int blocksize, int csum_size,
2328 unsigned int parent_ino, int dotdot_real_len)
2330 de->inode = cpu_to_le32(inode->i_ino);
2332 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2334 strcpy(de->name, ".");
2335 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2337 de = ext4_next_entry(de, blocksize);
2338 de->inode = cpu_to_le32(parent_ino);
2340 if (!dotdot_real_len)
2341 de->rec_len = ext4_rec_len_to_disk(blocksize -
2342 (csum_size + EXT4_DIR_REC_LEN(1)),
2345 de->rec_len = ext4_rec_len_to_disk(
2346 EXT4_DIR_REC_LEN(de->name_len), blocksize);
2347 strcpy(de->name, "..");
2348 ext4_set_de_type(inode->i_sb, de, S_IFDIR);
2350 return ext4_next_entry(de, blocksize);
2353 static int ext4_init_new_dir(handle_t *handle, struct inode *dir,
2354 struct inode *inode)
2356 struct buffer_head *dir_block = NULL;
2357 struct ext4_dir_entry_2 *de;
2358 struct ext4_dir_entry_tail *t;
2359 unsigned int blocksize = dir->i_sb->s_blocksize;
2363 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2364 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2365 csum_size = sizeof(struct ext4_dir_entry_tail);
2367 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2368 err = ext4_try_create_inline_dir(handle, dir, inode);
2369 if (err < 0 && err != -ENOSPC)
2375 inode->i_size = EXT4_I(inode)->i_disksize = blocksize;
2376 if (!(dir_block = ext4_bread(handle, inode, 0, 1, &err))) {
2379 ext4_error(inode->i_sb,
2380 "Directory hole detected on inode %lu\n",
2385 BUFFER_TRACE(dir_block, "get_write_access");
2386 err = ext4_journal_get_write_access(handle, dir_block);
2389 de = (struct ext4_dir_entry_2 *)dir_block->b_data;
2390 ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0);
2391 set_nlink(inode, 2);
2393 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2394 initialize_dirent_tail(t, blocksize);
2397 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2398 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2401 set_buffer_verified(dir_block);
2407 static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2410 struct inode *inode;
2411 int err, retries = 0;
2413 if (EXT4_DIR_LINK_MAX(dir))
2416 dquot_initialize(dir);
2419 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2420 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2421 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2422 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)));
2424 return PTR_ERR(handle);
2426 if (IS_DIRSYNC(dir))
2427 ext4_handle_sync(handle);
2429 inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
2430 &dentry->d_name, 0, NULL);
2431 err = PTR_ERR(inode);
2435 inode->i_op = &ext4_dir_inode_operations;
2436 inode->i_fop = &ext4_dir_operations;
2437 err = ext4_init_new_dir(handle, dir, inode);
2439 goto out_clear_inode;
2440 err = ext4_mark_inode_dirty(handle, inode);
2442 err = ext4_add_entry(handle, dentry, inode);
2446 unlock_new_inode(inode);
2447 ext4_mark_inode_dirty(handle, inode);
2451 ext4_inc_count(handle, dir);
2452 ext4_update_dx_flag(dir);
2453 err = ext4_mark_inode_dirty(handle, dir);
2455 goto out_clear_inode;
2456 unlock_new_inode(inode);
2457 d_instantiate(dentry, inode);
2459 ext4_journal_stop(handle);
2460 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2466 * routine to check that the specified directory is empty (for rmdir)
2468 static int empty_dir(struct inode *inode)
2470 unsigned int offset;
2471 struct buffer_head *bh;
2472 struct ext4_dir_entry_2 *de, *de1;
2473 struct super_block *sb;
2476 if (ext4_has_inline_data(inode)) {
2477 int has_inline_data = 1;
2479 err = empty_inline_dir(inode, &has_inline_data);
2480 if (has_inline_data)
2485 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
2486 !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
2488 EXT4_ERROR_INODE(inode,
2489 "error %d reading directory lblock 0", err);
2491 ext4_warning(inode->i_sb,
2492 "bad directory (dir #%lu) - no data block",
2496 if (!buffer_verified(bh) &&
2497 !ext4_dirent_csum_verify(inode,
2498 (struct ext4_dir_entry *)bh->b_data)) {
2499 EXT4_ERROR_INODE(inode, "checksum error reading directory "
2504 set_buffer_verified(bh);
2505 de = (struct ext4_dir_entry_2 *) bh->b_data;
2506 de1 = ext4_next_entry(de, sb->s_blocksize);
2507 if (le32_to_cpu(de->inode) != inode->i_ino ||
2508 !le32_to_cpu(de1->inode) ||
2509 strcmp(".", de->name) ||
2510 strcmp("..", de1->name)) {
2511 ext4_warning(inode->i_sb,
2512 "bad directory (dir #%lu) - no `.' or `..'",
2517 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2518 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2519 de = ext4_next_entry(de1, sb->s_blocksize);
2520 while (offset < inode->i_size) {
2522 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
2523 unsigned int lblock;
2526 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2527 bh = ext4_bread(NULL, inode, lblock, 0, &err);
2530 EXT4_ERROR_INODE(inode,
2531 "error %d reading directory "
2532 "lblock %u", err, lblock);
2534 ext4_warning(inode->i_sb,
2535 "bad directory (dir #%lu) - no data block",
2538 offset += sb->s_blocksize;
2541 if (!buffer_verified(bh) &&
2542 !ext4_dirent_csum_verify(inode,
2543 (struct ext4_dir_entry *)bh->b_data)) {
2544 EXT4_ERROR_INODE(inode, "checksum error "
2545 "reading directory lblock 0");
2549 set_buffer_verified(bh);
2550 de = (struct ext4_dir_entry_2 *) bh->b_data;
2552 if (ext4_check_dir_entry(inode, NULL, de, bh,
2553 bh->b_data, bh->b_size, offset)) {
2554 de = (struct ext4_dir_entry_2 *)(bh->b_data +
2556 offset = (offset | (sb->s_blocksize - 1)) + 1;
2559 if (le32_to_cpu(de->inode)) {
2563 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2564 de = ext4_next_entry(de, sb->s_blocksize);
2570 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
2571 * such inodes, starting at the superblock, in case we crash before the
2572 * file is closed/deleted, or in case the inode truncate spans multiple
2573 * transactions and the last transaction is not recovered after a crash.
2575 * At filesystem recovery time, we walk this list deleting unlinked
2576 * inodes and truncating linked inodes in ext4_orphan_cleanup().
2578 int ext4_orphan_add(handle_t *handle, struct inode *inode)
2580 struct super_block *sb = inode->i_sb;
2581 struct ext4_iloc iloc;
2584 if (!EXT4_SB(sb)->s_journal)
2587 mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
2588 if (!list_empty(&EXT4_I(inode)->i_orphan))
2592 * Orphan handling is only valid for files with data blocks
2593 * being truncated, or files being unlinked. Note that we either
2594 * hold i_mutex, or the inode can not be referenced from outside,
2595 * so i_nlink should not be bumped due to race
2597 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2598 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
2600 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
2601 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
2605 err = ext4_reserve_inode_write(handle, inode, &iloc);
2609 * Due to previous errors inode may be already a part of on-disk
2610 * orphan list. If so skip on-disk list modification.
2612 if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
2613 (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
2616 /* Insert this inode at the head of the on-disk orphan list... */
2617 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
2618 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2619 err = ext4_handle_dirty_super(handle, sb);
2620 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2624 /* Only add to the head of the in-memory list if all the
2625 * previous operations succeeded. If the orphan_add is going to
2626 * fail (possibly taking the journal offline), we can't risk
2627 * leaving the inode on the orphan list: stray orphan-list
2628 * entries can cause panics at unmount time.
2630 * This is safe: on error we're going to ignore the orphan list
2631 * anyway on the next recovery. */
2634 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2636 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2637 jbd_debug(4, "orphan inode %lu will point to %d\n",
2638 inode->i_ino, NEXT_ORPHAN(inode));
2640 mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
2641 ext4_std_error(inode->i_sb, err);
2646 * ext4_orphan_del() removes an unlinked or truncated inode from the list
2647 * of such inodes stored on disk, because it is finally being cleaned up.
2649 int ext4_orphan_del(handle_t *handle, struct inode *inode)
2651 struct list_head *prev;
2652 struct ext4_inode_info *ei = EXT4_I(inode);
2653 struct ext4_sb_info *sbi;
2655 struct ext4_iloc iloc;
2658 if ((!EXT4_SB(inode->i_sb)->s_journal) &&
2659 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS))
2662 mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2663 if (list_empty(&ei->i_orphan))
2666 ino_next = NEXT_ORPHAN(inode);
2667 prev = ei->i_orphan.prev;
2668 sbi = EXT4_SB(inode->i_sb);
2670 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2672 list_del_init(&ei->i_orphan);
2674 /* If we're on an error path, we may not have a valid
2675 * transaction handle with which to update the orphan list on
2676 * disk, but we still need to remove the inode from the linked
2677 * list in memory. */
2681 err = ext4_reserve_inode_write(handle, inode, &iloc);
2685 if (prev == &sbi->s_orphan) {
2686 jbd_debug(4, "superblock will point to %u\n", ino_next);
2687 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2688 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
2691 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2692 err = ext4_handle_dirty_super(handle, inode->i_sb);
2694 struct ext4_iloc iloc2;
2695 struct inode *i_prev =
2696 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
2698 jbd_debug(4, "orphan inode %lu will point to %u\n",
2699 i_prev->i_ino, ino_next);
2700 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
2703 NEXT_ORPHAN(i_prev) = ino_next;
2704 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
2708 NEXT_ORPHAN(inode) = 0;
2709 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
2712 ext4_std_error(inode->i_sb, err);
2714 mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2722 static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
2725 struct inode *inode;
2726 struct buffer_head *bh;
2727 struct ext4_dir_entry_2 *de;
2730 /* Initialize quotas before so that eventual writes go in
2731 * separate transaction */
2732 dquot_initialize(dir);
2733 dquot_initialize(dentry->d_inode);
2735 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2736 EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2738 return PTR_ERR(handle);
2741 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
2745 if (IS_DIRSYNC(dir))
2746 ext4_handle_sync(handle);
2748 inode = dentry->d_inode;
2751 if (le32_to_cpu(de->inode) != inode->i_ino)
2754 retval = -ENOTEMPTY;
2755 if (!empty_dir(inode))
2758 retval = ext4_delete_entry(handle, dir, de, bh);
2761 if (!EXT4_DIR_LINK_EMPTY(inode))
2762 ext4_warning(inode->i_sb,
2763 "empty directory has too many links (%d)",
2767 /* There's no need to set i_disksize: the fact that i_nlink is
2768 * zero will ensure that the right thing happens during any
2771 ext4_orphan_add(handle, inode);
2772 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
2773 ext4_mark_inode_dirty(handle, inode);
2774 ext4_dec_count(handle, dir);
2775 ext4_update_dx_flag(dir);
2776 ext4_mark_inode_dirty(handle, dir);
2779 ext4_journal_stop(handle);
2784 static int ext4_unlink(struct inode *dir, struct dentry *dentry)
2787 struct inode *inode;
2788 struct buffer_head *bh;
2789 struct ext4_dir_entry_2 *de;
2790 handle_t *handle = NULL;
2792 trace_ext4_unlink_enter(dir, dentry);
2793 /* Initialize quotas before so that eventual writes go
2794 * in separate transaction */
2795 dquot_initialize(dir);
2796 dquot_initialize(dentry->d_inode);
2799 bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL);
2803 inode = dentry->d_inode;
2806 if (le32_to_cpu(de->inode) != inode->i_ino)
2809 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2810 EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2811 if (IS_ERR(handle)) {
2812 retval = PTR_ERR(handle);
2817 if (IS_DIRSYNC(dir))
2818 ext4_handle_sync(handle);
2820 if (!inode->i_nlink) {
2821 ext4_warning(inode->i_sb,
2822 "Deleting nonexistent file (%lu), %d",
2823 inode->i_ino, inode->i_nlink);
2824 set_nlink(inode, 1);
2826 retval = ext4_delete_entry(handle, dir, de, bh);
2829 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
2830 ext4_update_dx_flag(dir);
2831 ext4_mark_inode_dirty(handle, dir);
2833 if (!inode->i_nlink)
2834 ext4_orphan_add(handle, inode);
2835 inode->i_ctime = ext4_current_time(inode);
2836 ext4_mark_inode_dirty(handle, inode);
2842 ext4_journal_stop(handle);
2843 trace_ext4_unlink_exit(dentry, retval);
2847 static int ext4_symlink(struct inode *dir,
2848 struct dentry *dentry, const char *symname)
2851 struct inode *inode;
2852 int l, err, retries = 0;
2855 l = strlen(symname)+1;
2856 if (l > dir->i_sb->s_blocksize)
2857 return -ENAMETOOLONG;
2859 dquot_initialize(dir);
2861 if (l > EXT4_N_BLOCKS * 4) {
2863 * For non-fast symlinks, we just allocate inode and put it on
2864 * orphan list in the first transaction => we need bitmap,
2865 * group descriptor, sb, inode block, quota blocks, and
2866 * possibly selinux xattr blocks.
2868 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2869 EXT4_XATTR_TRANS_BLOCKS;
2872 * Fast symlink. We have to add entry to directory
2873 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2874 * allocate new inode (bitmap, group descriptor, inode block,
2875 * quota blocks, sb is already counted in previous macros).
2877 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2878 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2879 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
2882 handle = ext4_journal_start(dir, EXT4_HT_DIR, credits);
2884 return PTR_ERR(handle);
2886 if (IS_DIRSYNC(dir))
2887 ext4_handle_sync(handle);
2889 inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
2890 &dentry->d_name, 0, NULL);
2891 err = PTR_ERR(inode);
2895 if (l > EXT4_N_BLOCKS * 4) {
2896 inode->i_op = &ext4_symlink_inode_operations;
2897 ext4_set_aops(inode);
2899 * We cannot call page_symlink() with transaction started
2900 * because it calls into ext4_write_begin() which can wait
2901 * for transaction commit if we are running out of space
2902 * and thus we deadlock. So we have to stop transaction now
2903 * and restart it when symlink contents is written.
2905 * To keep fs consistent in case of crash, we have to put inode
2906 * to orphan list in the mean time.
2909 err = ext4_orphan_add(handle, inode);
2910 ext4_journal_stop(handle);
2912 goto err_drop_inode;
2913 err = __page_symlink(inode, symname, l, 1);
2915 goto err_drop_inode;
2917 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2918 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2920 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2921 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2922 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
2923 if (IS_ERR(handle)) {
2924 err = PTR_ERR(handle);
2925 goto err_drop_inode;
2927 set_nlink(inode, 1);
2928 err = ext4_orphan_del(handle, inode);
2930 ext4_journal_stop(handle);
2932 goto err_drop_inode;
2935 /* clear the extent format for fast symlink */
2936 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
2937 inode->i_op = &ext4_fast_symlink_inode_operations;
2938 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
2939 inode->i_size = l-1;
2941 EXT4_I(inode)->i_disksize = inode->i_size;
2942 err = ext4_add_nondir(handle, dentry, inode);
2944 ext4_journal_stop(handle);
2945 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2949 unlock_new_inode(inode);
2954 static int ext4_link(struct dentry *old_dentry,
2955 struct inode *dir, struct dentry *dentry)
2958 struct inode *inode = old_dentry->d_inode;
2959 int err, retries = 0;
2961 if (inode->i_nlink >= EXT4_LINK_MAX)
2964 dquot_initialize(dir);
2967 handle = ext4_journal_start(dir, EXT4_HT_DIR,
2968 (EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2969 EXT4_INDEX_EXTRA_TRANS_BLOCKS));
2971 return PTR_ERR(handle);
2973 if (IS_DIRSYNC(dir))
2974 ext4_handle_sync(handle);
2976 inode->i_ctime = ext4_current_time(inode);
2977 ext4_inc_count(handle, inode);
2980 err = ext4_add_entry(handle, dentry, inode);
2982 ext4_mark_inode_dirty(handle, inode);
2983 d_instantiate(dentry, inode);
2988 ext4_journal_stop(handle);
2989 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2996 * Try to find buffer head where contains the parent block.
2997 * It should be the inode block if it is inlined or the 1st block
2998 * if it is a normal dir.
3000 static struct buffer_head *ext4_get_first_dir_block(handle_t *handle,
3001 struct inode *inode,
3003 struct ext4_dir_entry_2 **parent_de,
3006 struct buffer_head *bh;
3008 if (!ext4_has_inline_data(inode)) {
3009 if (!(bh = ext4_bread(handle, inode, 0, 0, retval))) {
3012 ext4_error(inode->i_sb,
3013 "Directory hole detected on inode %lu\n",
3018 *parent_de = ext4_next_entry(
3019 (struct ext4_dir_entry_2 *)bh->b_data,
3020 inode->i_sb->s_blocksize);
3025 return ext4_get_first_inline_block(inode, parent_de, retval);
3029 * Anybody can rename anything with this: the permission checks are left to the
3030 * higher-level routines.
3032 static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
3033 struct inode *new_dir, struct dentry *new_dentry)
3036 struct inode *old_inode, *new_inode;
3037 struct buffer_head *old_bh, *new_bh, *dir_bh;
3038 struct ext4_dir_entry_2 *old_de, *new_de;
3039 int retval, force_da_alloc = 0;
3040 int inlined = 0, new_inlined = 0;
3041 struct ext4_dir_entry_2 *parent_de;
3043 dquot_initialize(old_dir);
3044 dquot_initialize(new_dir);
3046 old_bh = new_bh = dir_bh = NULL;
3048 /* Initialize quotas before so that eventual writes go
3049 * in separate transaction */
3050 if (new_dentry->d_inode)
3051 dquot_initialize(new_dentry->d_inode);
3052 handle = ext4_journal_start(old_dir, EXT4_HT_DIR,
3053 (2 * EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
3054 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2));
3056 return PTR_ERR(handle);
3058 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
3059 ext4_handle_sync(handle);
3061 old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de, NULL);
3063 * Check for inode number is _not_ due to possible IO errors.
3064 * We might rmdir the source, keep it as pwd of some process
3065 * and merrily kill the link to whatever was created under the
3066 * same name. Goodbye sticky bit ;-<
3068 old_inode = old_dentry->d_inode;
3070 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
3073 new_inode = new_dentry->d_inode;
3074 new_bh = ext4_find_entry(new_dir, &new_dentry->d_name,
3075 &new_de, &new_inlined);
3082 if (S_ISDIR(old_inode->i_mode)) {
3084 retval = -ENOTEMPTY;
3085 if (!empty_dir(new_inode))
3089 dir_bh = ext4_get_first_dir_block(handle, old_inode,
3090 &retval, &parent_de,
3094 if (!inlined && !buffer_verified(dir_bh) &&
3095 !ext4_dirent_csum_verify(old_inode,
3096 (struct ext4_dir_entry *)dir_bh->b_data))
3098 set_buffer_verified(dir_bh);
3099 if (le32_to_cpu(parent_de->inode) != old_dir->i_ino)
3102 if (!new_inode && new_dir != old_dir &&
3103 EXT4_DIR_LINK_MAX(new_dir))
3105 BUFFER_TRACE(dir_bh, "get_write_access");
3106 retval = ext4_journal_get_write_access(handle, dir_bh);
3111 retval = ext4_add_entry(handle, new_dentry, old_inode);
3115 BUFFER_TRACE(new_bh, "get write access");
3116 retval = ext4_journal_get_write_access(handle, new_bh);
3119 new_de->inode = cpu_to_le32(old_inode->i_ino);
3120 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
3121 EXT4_FEATURE_INCOMPAT_FILETYPE))
3122 new_de->file_type = old_de->file_type;
3123 new_dir->i_version++;
3124 new_dir->i_ctime = new_dir->i_mtime =
3125 ext4_current_time(new_dir);
3126 ext4_mark_inode_dirty(handle, new_dir);
3127 BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
3129 retval = ext4_handle_dirty_dirent_node(handle,
3131 if (unlikely(retval)) {
3132 ext4_std_error(new_dir->i_sb, retval);
3141 * Like most other Unix systems, set the ctime for inodes on a
3144 old_inode->i_ctime = ext4_current_time(old_inode);
3145 ext4_mark_inode_dirty(handle, old_inode);
3150 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
3151 old_de->name_len != old_dentry->d_name.len ||
3152 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
3153 (retval = ext4_delete_entry(handle, old_dir,
3154 old_de, old_bh)) == -ENOENT) {
3155 /* old_de could have moved from under us during htree split, so
3156 * make sure that we are deleting the right entry. We might
3157 * also be pointing to a stale entry in the unused part of
3158 * old_bh so just checking inum and the name isn't enough. */
3159 struct buffer_head *old_bh2;
3160 struct ext4_dir_entry_2 *old_de2;
3162 old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name,
3165 retval = ext4_delete_entry(handle, old_dir,
3171 ext4_warning(old_dir->i_sb,
3172 "Deleting old file (%lu), %d, error=%d",
3173 old_dir->i_ino, old_dir->i_nlink, retval);
3177 ext4_dec_count(handle, new_inode);
3178 new_inode->i_ctime = ext4_current_time(new_inode);
3180 old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
3181 ext4_update_dx_flag(old_dir);
3183 parent_de->inode = cpu_to_le32(new_dir->i_ino);
3184 BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
3186 if (is_dx(old_inode)) {
3187 retval = ext4_handle_dirty_dx_node(handle,
3191 retval = ext4_handle_dirty_dirent_node(handle,
3195 retval = ext4_mark_inode_dirty(handle, old_inode);
3198 ext4_std_error(old_dir->i_sb, retval);
3201 ext4_dec_count(handle, old_dir);
3203 /* checked empty_dir above, can't have another parent,
3204 * ext4_dec_count() won't work for many-linked dirs */
3205 clear_nlink(new_inode);
3207 ext4_inc_count(handle, new_dir);
3208 ext4_update_dx_flag(new_dir);
3209 ext4_mark_inode_dirty(handle, new_dir);
3212 ext4_mark_inode_dirty(handle, old_dir);
3214 ext4_mark_inode_dirty(handle, new_inode);
3215 if (!new_inode->i_nlink)
3216 ext4_orphan_add(handle, new_inode);
3217 if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC))
3226 ext4_journal_stop(handle);
3227 if (retval == 0 && force_da_alloc)
3228 ext4_alloc_da_blocks(old_inode);
3233 * directories can handle most operations...
3235 const struct inode_operations ext4_dir_inode_operations = {
3236 .create = ext4_create,
3237 .lookup = ext4_lookup,
3239 .unlink = ext4_unlink,
3240 .symlink = ext4_symlink,
3241 .mkdir = ext4_mkdir,
3242 .rmdir = ext4_rmdir,
3243 .mknod = ext4_mknod,
3244 .rename = ext4_rename,
3245 .setattr = ext4_setattr,
3246 .setxattr = generic_setxattr,
3247 .getxattr = generic_getxattr,
3248 .listxattr = ext4_listxattr,
3249 .removexattr = generic_removexattr,
3250 .get_acl = ext4_get_acl,
3251 .fiemap = ext4_fiemap,
3254 const struct inode_operations ext4_special_inode_operations = {
3255 .setattr = ext4_setattr,
3256 .setxattr = generic_setxattr,
3257 .getxattr = generic_getxattr,
3258 .listxattr = ext4_listxattr,
3259 .removexattr = generic_removexattr,
3260 .get_acl = ext4_get_acl,