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);
81 #define assert(test) J_ASSERT(test)
85 #define dxtrace(command) command
87 #define dxtrace(command)
111 * dx_root_info is laid out so that if it should somehow get overlaid by a
112 * dirent the two low bits of the hash version will be zero. Therefore, the
113 * hash version mod 4 should never be 0. Sincerely, the paranoia department.
118 struct fake_dirent dot;
120 struct fake_dirent dotdot;
124 __le32 reserved_zero;
126 u8 info_length; /* 8 */
131 struct dx_entry entries[0];
136 struct fake_dirent fake;
137 struct dx_entry entries[0];
143 struct buffer_head *bh;
144 struct dx_entry *entries;
156 * This goes at the end of each htree block.
160 __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */
163 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry);
164 static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value);
165 static inline unsigned dx_get_hash(struct dx_entry *entry);
166 static void dx_set_hash(struct dx_entry *entry, unsigned value);
167 static unsigned dx_get_count(struct dx_entry *entries);
168 static unsigned dx_get_limit(struct dx_entry *entries);
169 static void dx_set_count(struct dx_entry *entries, unsigned value);
170 static void dx_set_limit(struct dx_entry *entries, unsigned value);
171 static unsigned dx_root_limit(struct inode *dir, unsigned infosize);
172 static unsigned dx_node_limit(struct inode *dir);
173 static struct dx_frame *dx_probe(const struct qstr *d_name,
175 struct dx_hash_info *hinfo,
176 struct dx_frame *frame,
178 static void dx_release(struct dx_frame *frames);
179 static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
180 struct dx_hash_info *hinfo, struct dx_map_entry map[]);
181 static void dx_sort_map(struct dx_map_entry *map, unsigned count);
182 static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to,
183 struct dx_map_entry *offsets, int count, unsigned blocksize);
184 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize);
185 static void dx_insert_block(struct dx_frame *frame,
186 u32 hash, ext4_lblk_t block);
187 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
188 struct dx_frame *frame,
189 struct dx_frame *frames,
191 static struct buffer_head * ext4_dx_find_entry(struct inode *dir,
192 const struct qstr *d_name,
193 struct ext4_dir_entry_2 **res_dir,
195 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
196 struct inode *inode);
198 /* checksumming functions */
199 #define EXT4_DIRENT_TAIL(block, blocksize) \
200 ((struct ext4_dir_entry_tail *)(((void *)(block)) + \
202 sizeof(struct ext4_dir_entry_tail))))
204 static void initialize_dirent_tail(struct ext4_dir_entry_tail *t,
205 unsigned int blocksize)
207 memset(t, 0, sizeof(struct ext4_dir_entry_tail));
208 t->det_rec_len = ext4_rec_len_to_disk(
209 sizeof(struct ext4_dir_entry_tail), blocksize);
210 t->det_reserved_ft = EXT4_FT_DIR_CSUM;
213 /* Walk through a dirent block to find a checksum "dirent" at the tail */
214 static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode,
215 struct ext4_dir_entry *de)
217 struct ext4_dir_entry_tail *t;
220 struct ext4_dir_entry *d, *top;
223 top = (struct ext4_dir_entry *)(((void *)de) +
224 (EXT4_BLOCK_SIZE(inode->i_sb) -
225 sizeof(struct ext4_dir_entry_tail)));
226 while (d < top && d->rec_len)
227 d = (struct ext4_dir_entry *)(((void *)d) +
228 le16_to_cpu(d->rec_len));
233 t = (struct ext4_dir_entry_tail *)d;
235 t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb));
238 if (t->det_reserved_zero1 ||
239 le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) ||
240 t->det_reserved_zero2 ||
241 t->det_reserved_ft != EXT4_FT_DIR_CSUM)
247 static __le32 ext4_dirent_csum(struct inode *inode,
248 struct ext4_dir_entry *dirent, int size)
250 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
251 struct ext4_inode_info *ei = EXT4_I(inode);
254 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
255 return cpu_to_le32(csum);
258 int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent)
260 struct ext4_dir_entry_tail *t;
262 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
263 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
266 t = get_dirent_tail(inode, dirent);
268 EXT4_ERROR_INODE(inode, "metadata_csum set but no space in dir "
269 "leaf for checksum. Please run e2fsck -D.");
273 if (t->det_checksum != ext4_dirent_csum(inode, dirent,
274 (void *)t - (void *)dirent))
280 static void ext4_dirent_csum_set(struct inode *inode,
281 struct ext4_dir_entry *dirent)
283 struct ext4_dir_entry_tail *t;
285 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
286 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
289 t = get_dirent_tail(inode, dirent);
291 EXT4_ERROR_INODE(inode, "metadata_csum set but no space in dir "
292 "leaf for checksum. Please run e2fsck -D.");
296 t->det_checksum = ext4_dirent_csum(inode, dirent,
297 (void *)t - (void *)dirent);
300 static inline int ext4_handle_dirty_dirent_node(handle_t *handle,
302 struct buffer_head *bh)
304 ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
305 return ext4_handle_dirty_metadata(handle, inode, bh);
308 static struct dx_countlimit *get_dx_countlimit(struct inode *inode,
309 struct ext4_dir_entry *dirent,
312 struct ext4_dir_entry *dp;
313 struct dx_root_info *root;
316 if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb))
318 else if (le16_to_cpu(dirent->rec_len) == 12) {
319 dp = (struct ext4_dir_entry *)(((void *)dirent) + 12);
320 if (le16_to_cpu(dp->rec_len) !=
321 EXT4_BLOCK_SIZE(inode->i_sb) - 12)
323 root = (struct dx_root_info *)(((void *)dp + 12));
324 if (root->reserved_zero ||
325 root->info_length != sizeof(struct dx_root_info))
332 *offset = count_offset;
333 return (struct dx_countlimit *)(((void *)dirent) + count_offset);
336 static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent,
337 int count_offset, int count, struct dx_tail *t)
339 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
340 struct ext4_inode_info *ei = EXT4_I(inode);
341 __u32 csum, old_csum;
344 size = count_offset + (count * sizeof(struct dx_entry));
345 old_csum = t->dt_checksum;
347 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size);
348 csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail));
349 t->dt_checksum = old_csum;
351 return cpu_to_le32(csum);
354 static int ext4_dx_csum_verify(struct inode *inode,
355 struct ext4_dir_entry *dirent)
357 struct dx_countlimit *c;
359 int count_offset, limit, count;
361 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
362 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
365 c = get_dx_countlimit(inode, dirent, &count_offset);
367 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
370 limit = le16_to_cpu(c->limit);
371 count = le16_to_cpu(c->count);
372 if (count_offset + (limit * sizeof(struct dx_entry)) >
373 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
374 EXT4_ERROR_INODE(inode, "metadata_csum set but no space for "
375 "tree checksum found. Run e2fsck -D.");
378 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
380 if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset,
386 static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent)
388 struct dx_countlimit *c;
390 int count_offset, limit, count;
392 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
393 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
396 c = get_dx_countlimit(inode, dirent, &count_offset);
398 EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D.");
401 limit = le16_to_cpu(c->limit);
402 count = le16_to_cpu(c->count);
403 if (count_offset + (limit * sizeof(struct dx_entry)) >
404 EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) {
405 EXT4_ERROR_INODE(inode, "metadata_csum set but no space for "
406 "tree checksum. Run e2fsck -D.");
409 t = (struct dx_tail *)(((struct dx_entry *)c) + limit);
411 t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t);
414 static inline int ext4_handle_dirty_dx_node(handle_t *handle,
416 struct buffer_head *bh)
418 ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data);
419 return ext4_handle_dirty_metadata(handle, inode, bh);
423 * p is at least 6 bytes before the end of page
425 static inline struct ext4_dir_entry_2 *
426 ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize)
428 return (struct ext4_dir_entry_2 *)((char *)p +
429 ext4_rec_len_from_disk(p->rec_len, blocksize));
433 * Future: use high four bits of block for coalesce-on-delete flags
434 * Mask them off for now.
437 static inline ext4_lblk_t dx_get_block(struct dx_entry *entry)
439 return le32_to_cpu(entry->block) & 0x00ffffff;
442 static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value)
444 entry->block = cpu_to_le32(value);
447 static inline unsigned dx_get_hash(struct dx_entry *entry)
449 return le32_to_cpu(entry->hash);
452 static inline void dx_set_hash(struct dx_entry *entry, unsigned value)
454 entry->hash = cpu_to_le32(value);
457 static inline unsigned dx_get_count(struct dx_entry *entries)
459 return le16_to_cpu(((struct dx_countlimit *) entries)->count);
462 static inline unsigned dx_get_limit(struct dx_entry *entries)
464 return le16_to_cpu(((struct dx_countlimit *) entries)->limit);
467 static inline void dx_set_count(struct dx_entry *entries, unsigned value)
469 ((struct dx_countlimit *) entries)->count = cpu_to_le16(value);
472 static inline void dx_set_limit(struct dx_entry *entries, unsigned value)
474 ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value);
477 static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize)
479 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) -
480 EXT4_DIR_REC_LEN(2) - infosize;
482 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
483 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
484 entry_space -= sizeof(struct dx_tail);
485 return entry_space / sizeof(struct dx_entry);
488 static inline unsigned dx_node_limit(struct inode *dir)
490 unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0);
492 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
493 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
494 entry_space -= sizeof(struct dx_tail);
495 return entry_space / sizeof(struct dx_entry);
502 static void dx_show_index(char * label, struct dx_entry *entries)
504 int i, n = dx_get_count (entries);
505 printk(KERN_DEBUG "%s index ", label);
506 for (i = 0; i < n; i++) {
507 printk("%x->%lu ", i ? dx_get_hash(entries + i) :
508 0, (unsigned long)dx_get_block(entries + i));
520 static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de,
521 int size, int show_names)
523 unsigned names = 0, space = 0;
524 char *base = (char *) de;
525 struct dx_hash_info h = *hinfo;
528 while ((char *) de < base + size)
534 int len = de->name_len;
535 char *name = de->name;
536 while (len--) printk("%c", *name++);
537 ext4fs_dirhash(de->name, de->name_len, &h);
538 printk(":%x.%u ", h.hash,
539 (unsigned) ((char *) de - base));
541 space += EXT4_DIR_REC_LEN(de->name_len);
544 de = ext4_next_entry(de, size);
546 printk("(%i)\n", names);
547 return (struct stats) { names, space, 1 };
550 struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir,
551 struct dx_entry *entries, int levels)
553 unsigned blocksize = dir->i_sb->s_blocksize;
554 unsigned count = dx_get_count(entries), names = 0, space = 0, i;
556 struct buffer_head *bh;
558 printk("%i indexed blocks...\n", count);
559 for (i = 0; i < count; i++, entries++)
561 ext4_lblk_t block = dx_get_block(entries);
562 ext4_lblk_t hash = i ? dx_get_hash(entries): 0;
563 u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash;
565 printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range);
566 if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue;
568 dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1):
569 dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0);
570 names += stats.names;
571 space += stats.space;
572 bcount += stats.bcount;
576 printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n",
577 levels ? "" : " ", names, space/bcount,
578 (space/bcount)*100/blocksize);
579 return (struct stats) { names, space, bcount};
581 #endif /* DX_DEBUG */
584 * Probe for a directory leaf block to search.
586 * dx_probe can return ERR_BAD_DX_DIR, which means there was a format
587 * error in the directory index, and the caller should fall back to
588 * searching the directory normally. The callers of dx_probe **MUST**
589 * check for this error code, and make sure it never gets reflected
592 static struct dx_frame *
593 dx_probe(const struct qstr *d_name, struct inode *dir,
594 struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err)
596 unsigned count, indirect;
597 struct dx_entry *at, *entries, *p, *q, *m;
598 struct dx_root *root;
599 struct buffer_head *bh;
600 struct dx_frame *frame = frame_in;
604 if (!(bh = ext4_bread (NULL,dir, 0, 0, err)))
606 root = (struct dx_root *) bh->b_data;
607 if (root->info.hash_version != DX_HASH_TEA &&
608 root->info.hash_version != DX_HASH_HALF_MD4 &&
609 root->info.hash_version != DX_HASH_LEGACY) {
610 ext4_warning(dir->i_sb, "Unrecognised inode hash code %d",
611 root->info.hash_version);
613 *err = ERR_BAD_DX_DIR;
616 hinfo->hash_version = root->info.hash_version;
617 if (hinfo->hash_version <= DX_HASH_TEA)
618 hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
619 hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed;
621 ext4fs_dirhash(d_name->name, d_name->len, hinfo);
624 if (root->info.unused_flags & 1) {
625 ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x",
626 root->info.unused_flags);
628 *err = ERR_BAD_DX_DIR;
632 if ((indirect = root->info.indirect_levels) > 1) {
633 ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x",
634 root->info.indirect_levels);
636 *err = ERR_BAD_DX_DIR;
640 if (!buffer_verified(bh) &&
641 !ext4_dx_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) {
642 ext4_warning(dir->i_sb, "Root failed checksum");
644 *err = ERR_BAD_DX_DIR;
647 set_buffer_verified(bh);
649 entries = (struct dx_entry *) (((char *)&root->info) +
650 root->info.info_length);
652 if (dx_get_limit(entries) != dx_root_limit(dir,
653 root->info.info_length)) {
654 ext4_warning(dir->i_sb, "dx entry: limit != root limit");
656 *err = ERR_BAD_DX_DIR;
660 dxtrace(printk("Look up %x", hash));
663 count = dx_get_count(entries);
664 if (!count || count > dx_get_limit(entries)) {
665 ext4_warning(dir->i_sb,
666 "dx entry: no count or count > limit");
668 *err = ERR_BAD_DX_DIR;
673 q = entries + count - 1;
677 dxtrace(printk("."));
678 if (dx_get_hash(m) > hash)
684 if (0) // linear search cross check
686 unsigned n = count - 1;
690 dxtrace(printk(","));
691 if (dx_get_hash(++at) > hash)
697 assert (at == p - 1);
701 dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at)));
703 frame->entries = entries;
705 if (!indirect--) return frame;
706 if (!(bh = ext4_bread (NULL,dir, dx_get_block(at), 0, err)))
708 at = entries = ((struct dx_node *) bh->b_data)->entries;
710 if (!buffer_verified(bh) &&
711 !ext4_dx_csum_verify(dir,
712 (struct ext4_dir_entry *)bh->b_data)) {
713 ext4_warning(dir->i_sb, "Node failed checksum");
715 *err = ERR_BAD_DX_DIR;
718 set_buffer_verified(bh);
720 if (dx_get_limit(entries) != dx_node_limit (dir)) {
721 ext4_warning(dir->i_sb,
722 "dx entry: limit != node limit");
724 *err = ERR_BAD_DX_DIR;
731 while (frame >= frame_in) {
736 if (*err == ERR_BAD_DX_DIR)
737 ext4_warning(dir->i_sb,
738 "Corrupt dir inode %lu, running e2fsck is "
739 "recommended.", dir->i_ino);
743 static void dx_release (struct dx_frame *frames)
745 if (frames[0].bh == NULL)
748 if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels)
749 brelse(frames[1].bh);
750 brelse(frames[0].bh);
754 * This function increments the frame pointer to search the next leaf
755 * block, and reads in the necessary intervening nodes if the search
756 * should be necessary. Whether or not the search is necessary is
757 * controlled by the hash parameter. If the hash value is even, then
758 * the search is only continued if the next block starts with that
759 * hash value. This is used if we are searching for a specific file.
761 * If the hash value is HASH_NB_ALWAYS, then always go to the next block.
763 * This function returns 1 if the caller should continue to search,
764 * or 0 if it should not. If there is an error reading one of the
765 * index blocks, it will a negative error code.
767 * If start_hash is non-null, it will be filled in with the starting
768 * hash of the next page.
770 static int ext4_htree_next_block(struct inode *dir, __u32 hash,
771 struct dx_frame *frame,
772 struct dx_frame *frames,
776 struct buffer_head *bh;
777 int err, num_frames = 0;
782 * Find the next leaf page by incrementing the frame pointer.
783 * If we run out of entries in the interior node, loop around and
784 * increment pointer in the parent node. When we break out of
785 * this loop, num_frames indicates the number of interior
786 * nodes need to be read.
789 if (++(p->at) < p->entries + dx_get_count(p->entries))
798 * If the hash is 1, then continue only if the next page has a
799 * continuation hash of any value. This is used for readdir
800 * handling. Otherwise, check to see if the hash matches the
801 * desired contiuation hash. If it doesn't, return since
802 * there's no point to read in the successive index pages.
804 bhash = dx_get_hash(p->at);
807 if ((hash & 1) == 0) {
808 if ((bhash & ~1) != hash)
812 * If the hash is HASH_NB_ALWAYS, we always go to the next
813 * block so no check is necessary
815 while (num_frames--) {
816 if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at),
818 return err; /* Failure */
820 if (!buffer_verified(bh) &&
821 !ext4_dx_csum_verify(dir,
822 (struct ext4_dir_entry *)bh->b_data)) {
823 ext4_warning(dir->i_sb, "Node failed checksum");
826 set_buffer_verified(bh);
831 p->at = p->entries = ((struct dx_node *) bh->b_data)->entries;
838 * This function fills a red-black tree with information from a
839 * directory block. It returns the number directory entries loaded
840 * into the tree. If there is an error it is returned in err.
842 static int htree_dirblock_to_tree(struct file *dir_file,
843 struct inode *dir, ext4_lblk_t block,
844 struct dx_hash_info *hinfo,
845 __u32 start_hash, __u32 start_minor_hash)
847 struct buffer_head *bh;
848 struct ext4_dir_entry_2 *de, *top;
849 int err = 0, count = 0;
851 dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n",
852 (unsigned long)block));
853 if (!(bh = ext4_bread (NULL, dir, block, 0, &err)))
856 if (!buffer_verified(bh) &&
857 !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
859 set_buffer_verified(bh);
861 de = (struct ext4_dir_entry_2 *) bh->b_data;
862 top = (struct ext4_dir_entry_2 *) ((char *) de +
863 dir->i_sb->s_blocksize -
864 EXT4_DIR_REC_LEN(0));
865 for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) {
866 if (ext4_check_dir_entry(dir, NULL, de, bh,
867 (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb))
868 + ((char *)de - bh->b_data))) {
869 /* On error, skip the f_pos to the next block. */
870 dir_file->f_pos = (dir_file->f_pos |
871 (dir->i_sb->s_blocksize - 1)) + 1;
875 ext4fs_dirhash(de->name, de->name_len, hinfo);
876 if ((hinfo->hash < start_hash) ||
877 ((hinfo->hash == start_hash) &&
878 (hinfo->minor_hash < start_minor_hash)))
882 if ((err = ext4_htree_store_dirent(dir_file,
883 hinfo->hash, hinfo->minor_hash, de)) != 0) {
895 * This function fills a red-black tree with information from a
896 * directory. We start scanning the directory in hash order, starting
897 * at start_hash and start_minor_hash.
899 * This function returns the number of entries inserted into the tree,
900 * or a negative error code.
902 int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
903 __u32 start_minor_hash, __u32 *next_hash)
905 struct dx_hash_info hinfo;
906 struct ext4_dir_entry_2 *de;
907 struct dx_frame frames[2], *frame;
914 dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n",
915 start_hash, start_minor_hash));
916 dir = dir_file->f_path.dentry->d_inode;
917 if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) {
918 hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
919 if (hinfo.hash_version <= DX_HASH_TEA)
920 hinfo.hash_version +=
921 EXT4_SB(dir->i_sb)->s_hash_unsigned;
922 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
923 count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo,
924 start_hash, start_minor_hash);
928 hinfo.hash = start_hash;
929 hinfo.minor_hash = 0;
930 frame = dx_probe(NULL, dir, &hinfo, frames, &err);
934 /* Add '.' and '..' from the htree header */
935 if (!start_hash && !start_minor_hash) {
936 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
937 if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0)
941 if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
942 de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
943 de = ext4_next_entry(de, dir->i_sb->s_blocksize);
944 if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0)
950 block = dx_get_block(frame->at);
951 ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo,
952 start_hash, start_minor_hash);
959 ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS,
960 frame, frames, &hashval);
961 *next_hash = hashval;
967 * Stop if: (a) there are no more entries, or
968 * (b) we have inserted at least one entry and the
969 * next hash value is not a continuation
972 (count && ((hashval & 1) == 0)))
976 dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, "
977 "next hash: %x\n", count, *next_hash));
986 * Directory block splitting, compacting
990 * Create map of hash values, offsets, and sizes, stored at end of block.
991 * Returns number of entries mapped.
993 static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize,
994 struct dx_hash_info *hinfo,
995 struct dx_map_entry *map_tail)
998 char *base = (char *) de;
999 struct dx_hash_info h = *hinfo;
1001 while ((char *) de < base + blocksize) {
1002 if (de->name_len && de->inode) {
1003 ext4fs_dirhash(de->name, de->name_len, &h);
1005 map_tail->hash = h.hash;
1006 map_tail->offs = ((char *) de - base)>>2;
1007 map_tail->size = le16_to_cpu(de->rec_len);
1011 /* XXX: do we need to check rec_len == 0 case? -Chris */
1012 de = ext4_next_entry(de, blocksize);
1017 /* Sort map by hash value */
1018 static void dx_sort_map (struct dx_map_entry *map, unsigned count)
1020 struct dx_map_entry *p, *q, *top = map + count - 1;
1022 /* Combsort until bubble sort doesn't suck */
1024 count = count*10/13;
1025 if (count - 9 < 2) /* 9, 10 -> 11 */
1027 for (p = top, q = p - count; q >= map; p--, q--)
1028 if (p->hash < q->hash)
1031 /* Garden variety bubble sort */
1036 if (q[1].hash >= q[0].hash)
1044 static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block)
1046 struct dx_entry *entries = frame->entries;
1047 struct dx_entry *old = frame->at, *new = old + 1;
1048 int count = dx_get_count(entries);
1050 assert(count < dx_get_limit(entries));
1051 assert(old < entries + count);
1052 memmove(new + 1, new, (char *)(entries + count) - (char *)(new));
1053 dx_set_hash(new, hash);
1054 dx_set_block(new, block);
1055 dx_set_count(entries, count + 1);
1058 static void ext4_update_dx_flag(struct inode *inode)
1060 if (!EXT4_HAS_COMPAT_FEATURE(inode->i_sb,
1061 EXT4_FEATURE_COMPAT_DIR_INDEX))
1062 ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
1066 * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure.
1068 * `len <= EXT4_NAME_LEN' is guaranteed by caller.
1069 * `de != NULL' is guaranteed by caller.
1071 static inline int ext4_match (int len, const char * const name,
1072 struct ext4_dir_entry_2 * de)
1074 if (len != de->name_len)
1078 return !memcmp(name, de->name, len);
1082 * Returns 0 if not found, -1 on failure, and 1 on success
1084 static inline int search_dirblock(struct buffer_head *bh,
1086 const struct qstr *d_name,
1087 unsigned int offset,
1088 struct ext4_dir_entry_2 ** res_dir)
1090 struct ext4_dir_entry_2 * de;
1093 const char *name = d_name->name;
1094 int namelen = d_name->len;
1096 de = (struct ext4_dir_entry_2 *) bh->b_data;
1097 dlimit = bh->b_data + dir->i_sb->s_blocksize;
1098 while ((char *) de < dlimit) {
1099 /* this code is executed quadratically often */
1100 /* do minimal checking `by hand' */
1102 if ((char *) de + namelen <= dlimit &&
1103 ext4_match (namelen, name, de)) {
1104 /* found a match - just to be sure, do a full check */
1105 if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
1110 /* prevent looping on a bad block */
1111 de_len = ext4_rec_len_from_disk(de->rec_len,
1112 dir->i_sb->s_blocksize);
1116 de = (struct ext4_dir_entry_2 *) ((char *) de + de_len);
1125 * finds an entry in the specified directory with the wanted name. It
1126 * returns the cache buffer in which the entry was found, and the entry
1127 * itself (as a parameter - res_dir). It does NOT read the inode of the
1128 * entry - you'll have to do that yourself if you want to.
1130 * The returned buffer_head has ->b_count elevated. The caller is expected
1131 * to brelse() it when appropriate.
1133 static struct buffer_head * ext4_find_entry (struct inode *dir,
1134 const struct qstr *d_name,
1135 struct ext4_dir_entry_2 ** res_dir)
1137 struct super_block *sb;
1138 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1139 struct buffer_head *bh, *ret = NULL;
1140 ext4_lblk_t start, block, b;
1141 const u8 *name = d_name->name;
1142 int ra_max = 0; /* Number of bh's in the readahead
1144 int ra_ptr = 0; /* Current index into readahead
1147 ext4_lblk_t nblocks;
1153 namelen = d_name->len;
1154 if (namelen > EXT4_NAME_LEN)
1156 if ((namelen <= 2) && (name[0] == '.') &&
1157 (name[1] == '.' || name[1] == '\0')) {
1159 * "." or ".." will only be in the first block
1160 * NFS may look up ".."; "." should be handled by the VFS
1167 bh = ext4_dx_find_entry(dir, d_name, res_dir, &err);
1169 * On success, or if the error was file not found,
1170 * return. Otherwise, fall back to doing a search the
1171 * old fashioned way.
1173 if (bh || (err != ERR_BAD_DX_DIR))
1175 dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, "
1178 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1179 start = EXT4_I(dir)->i_dir_start_lookup;
1180 if (start >= nblocks)
1186 * We deal with the read-ahead logic here.
1188 if (ra_ptr >= ra_max) {
1189 /* Refill the readahead buffer */
1192 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
1194 * Terminate if we reach the end of the
1195 * directory and must wrap, or if our
1196 * search has finished at this block.
1198 if (b >= nblocks || (num && block == start)) {
1199 bh_use[ra_max] = NULL;
1203 bh = ext4_getblk(NULL, dir, b++, 0, &err);
1204 bh_use[ra_max] = bh;
1206 ll_rw_block(READ | REQ_META | REQ_PRIO,
1210 if ((bh = bh_use[ra_ptr++]) == NULL)
1213 if (!buffer_uptodate(bh)) {
1214 /* read error, skip block & hope for the best */
1215 EXT4_ERROR_INODE(dir, "reading directory lblock %lu",
1216 (unsigned long) block);
1220 if (!buffer_verified(bh) &&
1221 !ext4_dirent_csum_verify(dir,
1222 (struct ext4_dir_entry *)bh->b_data)) {
1223 EXT4_ERROR_INODE(dir, "checksumming directory "
1224 "block %lu", (unsigned long)block);
1228 set_buffer_verified(bh);
1229 i = search_dirblock(bh, dir, d_name,
1230 block << EXT4_BLOCK_SIZE_BITS(sb), res_dir);
1232 EXT4_I(dir)->i_dir_start_lookup = block;
1234 goto cleanup_and_exit;
1238 goto cleanup_and_exit;
1241 if (++block >= nblocks)
1243 } while (block != start);
1246 * If the directory has grown while we were searching, then
1247 * search the last part of the directory before giving up.
1250 nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb);
1251 if (block < nblocks) {
1257 /* Clean up the read-ahead blocks */
1258 for (; ra_ptr < ra_max; ra_ptr++)
1259 brelse(bh_use[ra_ptr]);
1263 static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name,
1264 struct ext4_dir_entry_2 **res_dir, int *err)
1266 struct super_block * sb = dir->i_sb;
1267 struct dx_hash_info hinfo;
1268 struct dx_frame frames[2], *frame;
1269 struct buffer_head *bh;
1273 if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err)))
1276 block = dx_get_block(frame->at);
1277 if (!(bh = ext4_bread(NULL, dir, block, 0, err)))
1280 if (!buffer_verified(bh) &&
1281 !ext4_dirent_csum_verify(dir,
1282 (struct ext4_dir_entry *)bh->b_data)) {
1283 EXT4_ERROR_INODE(dir, "checksumming directory "
1284 "block %lu", (unsigned long)block);
1289 set_buffer_verified(bh);
1290 retval = search_dirblock(bh, dir, d_name,
1291 block << EXT4_BLOCK_SIZE_BITS(sb),
1293 if (retval == 1) { /* Success! */
1299 *err = ERR_BAD_DX_DIR;
1303 /* Check to see if we should continue to search */
1304 retval = ext4_htree_next_block(dir, hinfo.hash, frame,
1308 "error reading index page in directory #%lu",
1313 } while (retval == 1);
1317 dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name));
1318 dx_release (frames);
1322 static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1324 struct inode *inode;
1325 struct ext4_dir_entry_2 *de;
1326 struct buffer_head *bh;
1328 if (dentry->d_name.len > EXT4_NAME_LEN)
1329 return ERR_PTR(-ENAMETOOLONG);
1331 bh = ext4_find_entry(dir, &dentry->d_name, &de);
1334 __u32 ino = le32_to_cpu(de->inode);
1336 if (!ext4_valid_inum(dir->i_sb, ino)) {
1337 EXT4_ERROR_INODE(dir, "bad inode number: %u", ino);
1338 return ERR_PTR(-EIO);
1340 if (unlikely(ino == dir->i_ino)) {
1341 EXT4_ERROR_INODE(dir, "'%.*s' linked to parent dir",
1343 dentry->d_name.name);
1344 return ERR_PTR(-EIO);
1346 inode = ext4_iget(dir->i_sb, ino);
1347 if (inode == ERR_PTR(-ESTALE)) {
1348 EXT4_ERROR_INODE(dir,
1349 "deleted inode referenced: %u",
1351 return ERR_PTR(-EIO);
1354 return d_splice_alias(inode, dentry);
1358 struct dentry *ext4_get_parent(struct dentry *child)
1361 static const struct qstr dotdot = QSTR_INIT("..", 2);
1362 struct ext4_dir_entry_2 * de;
1363 struct buffer_head *bh;
1365 bh = ext4_find_entry(child->d_inode, &dotdot, &de);
1367 return ERR_PTR(-ENOENT);
1368 ino = le32_to_cpu(de->inode);
1371 if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
1372 EXT4_ERROR_INODE(child->d_inode,
1373 "bad parent inode number: %u", ino);
1374 return ERR_PTR(-EIO);
1377 return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
1381 static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = {
1382 [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
1383 [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
1384 [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
1385 [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
1386 [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
1387 [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
1388 [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
1391 static inline void ext4_set_de_type(struct super_block *sb,
1392 struct ext4_dir_entry_2 *de,
1394 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE))
1395 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
1399 * Move count entries from end of map between two memory locations.
1400 * Returns pointer to last entry moved.
1402 static struct ext4_dir_entry_2 *
1403 dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count,
1406 unsigned rec_len = 0;
1409 struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)
1410 (from + (map->offs<<2));
1411 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1412 memcpy (to, de, rec_len);
1413 ((struct ext4_dir_entry_2 *) to)->rec_len =
1414 ext4_rec_len_to_disk(rec_len, blocksize);
1419 return (struct ext4_dir_entry_2 *) (to - rec_len);
1423 * Compact each dir entry in the range to the minimal rec_len.
1424 * Returns pointer to last entry in range.
1426 static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize)
1428 struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base;
1429 unsigned rec_len = 0;
1432 while ((char*)de < base + blocksize) {
1433 next = ext4_next_entry(de, blocksize);
1434 if (de->inode && de->name_len) {
1435 rec_len = EXT4_DIR_REC_LEN(de->name_len);
1437 memmove(to, de, rec_len);
1438 to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);
1440 to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len);
1448 * Split a full leaf block to make room for a new dir entry.
1449 * Allocate a new block, and move entries so that they are approx. equally full.
1450 * Returns pointer to de in block into which the new entry will be inserted.
1452 static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir,
1453 struct buffer_head **bh,struct dx_frame *frame,
1454 struct dx_hash_info *hinfo, int *error)
1456 unsigned blocksize = dir->i_sb->s_blocksize;
1457 unsigned count, continued;
1458 struct buffer_head *bh2;
1459 ext4_lblk_t newblock;
1461 struct dx_map_entry *map;
1462 char *data1 = (*bh)->b_data, *data2;
1463 unsigned split, move, size;
1464 struct ext4_dir_entry_2 *de = NULL, *de2;
1465 struct ext4_dir_entry_tail *t;
1469 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
1470 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1471 csum_size = sizeof(struct ext4_dir_entry_tail);
1473 bh2 = ext4_append (handle, dir, &newblock, &err);
1480 BUFFER_TRACE(*bh, "get_write_access");
1481 err = ext4_journal_get_write_access(handle, *bh);
1485 BUFFER_TRACE(frame->bh, "get_write_access");
1486 err = ext4_journal_get_write_access(handle, frame->bh);
1490 data2 = bh2->b_data;
1492 /* create map in the end of data2 block */
1493 map = (struct dx_map_entry *) (data2 + blocksize);
1494 count = dx_make_map((struct ext4_dir_entry_2 *) data1,
1495 blocksize, hinfo, map);
1497 dx_sort_map(map, count);
1498 /* Split the existing block in the middle, size-wise */
1501 for (i = count-1; i >= 0; i--) {
1502 /* is more than half of this entry in 2nd half of the block? */
1503 if (size + map[i].size/2 > blocksize/2)
1505 size += map[i].size;
1508 /* map index at which we will split */
1509 split = count - move;
1510 hash2 = map[split].hash;
1511 continued = hash2 == map[split - 1].hash;
1512 dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",
1513 (unsigned long)dx_get_block(frame->at),
1514 hash2, split, count-split));
1516 /* Fancy dance to stay within two buffers */
1517 de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize);
1518 de = dx_pack_dirents(data1, blocksize);
1519 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1522 de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) -
1526 t = EXT4_DIRENT_TAIL(data2, blocksize);
1527 initialize_dirent_tail(t, blocksize);
1529 t = EXT4_DIRENT_TAIL(data1, blocksize);
1530 initialize_dirent_tail(t, blocksize);
1533 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1));
1534 dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1));
1536 /* Which block gets the new entry? */
1537 if (hinfo->hash >= hash2)
1542 dx_insert_block(frame, hash2 + continued, newblock);
1543 err = ext4_handle_dirty_dirent_node(handle, dir, bh2);
1546 err = ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1550 dxtrace(dx_show_index("frame", frame->entries));
1557 ext4_std_error(dir->i_sb, err);
1564 * Add a new entry into a directory (leaf) block. If de is non-NULL,
1565 * it points to a directory entry which is guaranteed to be large
1566 * enough for new directory entry. If de is NULL, then
1567 * add_dirent_to_buf will attempt search the directory block for
1568 * space. It will return -ENOSPC if no space is available, and -EIO
1569 * and -EEXIST if directory entry already exists.
1571 static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry,
1572 struct inode *inode, struct ext4_dir_entry_2 *de,
1573 struct buffer_head *bh)
1575 struct inode *dir = dentry->d_parent->d_inode;
1576 const char *name = dentry->d_name.name;
1577 int namelen = dentry->d_name.len;
1578 unsigned int offset = 0;
1579 unsigned int blocksize = dir->i_sb->s_blocksize;
1580 unsigned short reclen;
1581 int nlen, rlen, err;
1585 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1586 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1587 csum_size = sizeof(struct ext4_dir_entry_tail);
1589 reclen = EXT4_DIR_REC_LEN(namelen);
1591 de = (struct ext4_dir_entry_2 *)bh->b_data;
1592 top = bh->b_data + (blocksize - csum_size) - reclen;
1593 while ((char *) de <= top) {
1594 if (ext4_check_dir_entry(dir, NULL, de, bh, offset))
1596 if (ext4_match(namelen, name, de))
1598 nlen = EXT4_DIR_REC_LEN(de->name_len);
1599 rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
1600 if ((de->inode? rlen - nlen: rlen) >= reclen)
1602 de = (struct ext4_dir_entry_2 *)((char *)de + rlen);
1605 if ((char *) de > top)
1608 BUFFER_TRACE(bh, "get_write_access");
1609 err = ext4_journal_get_write_access(handle, bh);
1611 ext4_std_error(dir->i_sb, err);
1615 /* By now the buffer is marked for journaling */
1616 nlen = EXT4_DIR_REC_LEN(de->name_len);
1617 rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
1619 struct ext4_dir_entry_2 *de1 = (struct ext4_dir_entry_2 *)((char *)de + nlen);
1620 de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, blocksize);
1621 de->rec_len = ext4_rec_len_to_disk(nlen, blocksize);
1624 de->file_type = EXT4_FT_UNKNOWN;
1625 de->inode = cpu_to_le32(inode->i_ino);
1626 ext4_set_de_type(dir->i_sb, de, inode->i_mode);
1627 de->name_len = namelen;
1628 memcpy(de->name, name, namelen);
1630 * XXX shouldn't update any times until successful
1631 * completion of syscall, but too many callers depend
1634 * XXX similarly, too many callers depend on
1635 * ext4_new_inode() setting the times, but error
1636 * recovery deletes the inode, so the worst that can
1637 * happen is that the times are slightly out of date
1638 * and/or different from the directory change time.
1640 dir->i_mtime = dir->i_ctime = ext4_current_time(dir);
1641 ext4_update_dx_flag(dir);
1643 ext4_mark_inode_dirty(handle, dir);
1644 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
1645 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
1647 ext4_std_error(dir->i_sb, err);
1652 * This converts a one block unindexed directory to a 3 block indexed
1653 * directory, and adds the dentry to the indexed directory.
1655 static int make_indexed_dir(handle_t *handle, struct dentry *dentry,
1656 struct inode *inode, struct buffer_head *bh)
1658 struct inode *dir = dentry->d_parent->d_inode;
1659 const char *name = dentry->d_name.name;
1660 int namelen = dentry->d_name.len;
1661 struct buffer_head *bh2;
1662 struct dx_root *root;
1663 struct dx_frame frames[2], *frame;
1664 struct dx_entry *entries;
1665 struct ext4_dir_entry_2 *de, *de2;
1666 struct ext4_dir_entry_tail *t;
1671 struct dx_hash_info hinfo;
1673 struct fake_dirent *fde;
1676 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1677 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1678 csum_size = sizeof(struct ext4_dir_entry_tail);
1680 blocksize = dir->i_sb->s_blocksize;
1681 dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino));
1682 retval = ext4_journal_get_write_access(handle, bh);
1684 ext4_std_error(dir->i_sb, retval);
1688 root = (struct dx_root *) bh->b_data;
1690 /* The 0th block becomes the root, move the dirents out */
1691 fde = &root->dotdot;
1692 de = (struct ext4_dir_entry_2 *)((char *)fde +
1693 ext4_rec_len_from_disk(fde->rec_len, blocksize));
1694 if ((char *) de >= (((char *) root) + blocksize)) {
1695 EXT4_ERROR_INODE(dir, "invalid rec_len for '..'");
1699 len = ((char *) root) + (blocksize - csum_size) - (char *) de;
1701 /* Allocate new block for the 0th block's dirents */
1702 bh2 = ext4_append(handle, dir, &block, &retval);
1707 ext4_set_inode_flag(dir, EXT4_INODE_INDEX);
1708 data1 = bh2->b_data;
1710 memcpy (data1, de, len);
1711 de = (struct ext4_dir_entry_2 *) data1;
1713 while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top)
1715 de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) -
1720 t = EXT4_DIRENT_TAIL(data1, blocksize);
1721 initialize_dirent_tail(t, blocksize);
1724 /* Initialize the root; the dot dirents already exist */
1725 de = (struct ext4_dir_entry_2 *) (&root->dotdot);
1726 de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2),
1728 memset (&root->info, 0, sizeof(root->info));
1729 root->info.info_length = sizeof(root->info);
1730 root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version;
1731 entries = root->entries;
1732 dx_set_block(entries, 1);
1733 dx_set_count(entries, 1);
1734 dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info)));
1736 /* Initialize as for dx_probe */
1737 hinfo.hash_version = root->info.hash_version;
1738 if (hinfo.hash_version <= DX_HASH_TEA)
1739 hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned;
1740 hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed;
1741 ext4fs_dirhash(name, namelen, &hinfo);
1743 frame->entries = entries;
1744 frame->at = entries;
1748 ext4_handle_dirty_dx_node(handle, dir, frame->bh);
1749 ext4_handle_dirty_dirent_node(handle, dir, bh);
1751 de = do_split(handle,dir, &bh, frame, &hinfo, &retval);
1754 * Even if the block split failed, we have to properly write
1755 * out all the changes we did so far. Otherwise we can end up
1756 * with corrupted filesystem.
1758 ext4_mark_inode_dirty(handle, dir);
1764 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1772 * adds a file entry to the specified directory, using the same
1773 * semantics as ext4_find_entry(). It returns NULL if it failed.
1775 * NOTE!! The inode part of 'de' is left at 0 - which means you
1776 * may not sleep between calling this and putting something into
1777 * the entry, as someone else might have used it while you slept.
1779 static int ext4_add_entry(handle_t *handle, struct dentry *dentry,
1780 struct inode *inode)
1782 struct inode *dir = dentry->d_parent->d_inode;
1783 struct buffer_head *bh;
1784 struct ext4_dir_entry_2 *de;
1785 struct ext4_dir_entry_tail *t;
1786 struct super_block *sb;
1790 ext4_lblk_t block, blocks;
1793 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
1794 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
1795 csum_size = sizeof(struct ext4_dir_entry_tail);
1798 blocksize = sb->s_blocksize;
1799 if (!dentry->d_name.len)
1802 retval = ext4_dx_add_entry(handle, dentry, inode);
1803 if (!retval || (retval != ERR_BAD_DX_DIR))
1805 ext4_clear_inode_flag(dir, EXT4_INODE_INDEX);
1807 ext4_mark_inode_dirty(handle, dir);
1809 blocks = dir->i_size >> sb->s_blocksize_bits;
1810 for (block = 0; block < blocks; block++) {
1811 bh = ext4_bread(handle, dir, block, 0, &retval);
1814 if (!buffer_verified(bh) &&
1815 !ext4_dirent_csum_verify(dir,
1816 (struct ext4_dir_entry *)bh->b_data))
1818 set_buffer_verified(bh);
1819 retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1820 if (retval != -ENOSPC) {
1825 if (blocks == 1 && !dx_fallback &&
1826 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX))
1827 return make_indexed_dir(handle, dentry, inode, bh);
1830 bh = ext4_append(handle, dir, &block, &retval);
1833 de = (struct ext4_dir_entry_2 *) bh->b_data;
1835 de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize);
1838 t = EXT4_DIRENT_TAIL(bh->b_data, blocksize);
1839 initialize_dirent_tail(t, blocksize);
1842 retval = add_dirent_to_buf(handle, dentry, inode, de, bh);
1845 ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY);
1850 * Returns 0 for success, or a negative error value
1852 static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry,
1853 struct inode *inode)
1855 struct dx_frame frames[2], *frame;
1856 struct dx_entry *entries, *at;
1857 struct dx_hash_info hinfo;
1858 struct buffer_head *bh;
1859 struct inode *dir = dentry->d_parent->d_inode;
1860 struct super_block *sb = dir->i_sb;
1861 struct ext4_dir_entry_2 *de;
1864 frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err);
1867 entries = frame->entries;
1870 if (!(bh = ext4_bread(handle,dir, dx_get_block(frame->at), 0, &err)))
1873 if (!buffer_verified(bh) &&
1874 !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data))
1876 set_buffer_verified(bh);
1878 BUFFER_TRACE(bh, "get_write_access");
1879 err = ext4_journal_get_write_access(handle, bh);
1883 err = add_dirent_to_buf(handle, dentry, inode, NULL, bh);
1887 /* Block full, should compress but for now just split */
1888 dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n",
1889 dx_get_count(entries), dx_get_limit(entries)));
1890 /* Need to split index? */
1891 if (dx_get_count(entries) == dx_get_limit(entries)) {
1892 ext4_lblk_t newblock;
1893 unsigned icount = dx_get_count(entries);
1894 int levels = frame - frames;
1895 struct dx_entry *entries2;
1896 struct dx_node *node2;
1897 struct buffer_head *bh2;
1899 if (levels && (dx_get_count(frames->entries) ==
1900 dx_get_limit(frames->entries))) {
1901 ext4_warning(sb, "Directory index full!");
1905 bh2 = ext4_append (handle, dir, &newblock, &err);
1908 node2 = (struct dx_node *)(bh2->b_data);
1909 entries2 = node2->entries;
1910 memset(&node2->fake, 0, sizeof(struct fake_dirent));
1911 node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize,
1913 BUFFER_TRACE(frame->bh, "get_write_access");
1914 err = ext4_journal_get_write_access(handle, frame->bh);
1918 unsigned icount1 = icount/2, icount2 = icount - icount1;
1919 unsigned hash2 = dx_get_hash(entries + icount1);
1920 dxtrace(printk(KERN_DEBUG "Split index %i/%i\n",
1923 BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */
1924 err = ext4_journal_get_write_access(handle,
1929 memcpy((char *) entries2, (char *) (entries + icount1),
1930 icount2 * sizeof(struct dx_entry));
1931 dx_set_count(entries, icount1);
1932 dx_set_count(entries2, icount2);
1933 dx_set_limit(entries2, dx_node_limit(dir));
1935 /* Which index block gets the new entry? */
1936 if (at - entries >= icount1) {
1937 frame->at = at = at - entries - icount1 + entries2;
1938 frame->entries = entries = entries2;
1939 swap(frame->bh, bh2);
1941 dx_insert_block(frames + 0, hash2, newblock);
1942 dxtrace(dx_show_index("node", frames[1].entries));
1943 dxtrace(dx_show_index("node",
1944 ((struct dx_node *) bh2->b_data)->entries));
1945 err = ext4_handle_dirty_dx_node(handle, dir, bh2);
1950 dxtrace(printk(KERN_DEBUG
1951 "Creating second level index...\n"));
1952 memcpy((char *) entries2, (char *) entries,
1953 icount * sizeof(struct dx_entry));
1954 dx_set_limit(entries2, dx_node_limit(dir));
1957 dx_set_count(entries, 1);
1958 dx_set_block(entries + 0, newblock);
1959 ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1;
1961 /* Add new access path frame */
1963 frame->at = at = at - entries + entries2;
1964 frame->entries = entries = entries2;
1966 err = ext4_journal_get_write_access(handle,
1971 err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh);
1973 ext4_std_error(inode->i_sb, err);
1977 de = do_split(handle, dir, &bh, frame, &hinfo, &err);
1980 err = add_dirent_to_buf(handle, dentry, inode, de, bh);
1984 ext4_std_error(dir->i_sb, err);
1993 * ext4_delete_entry deletes a directory entry by merging it with the
1996 static int ext4_delete_entry(handle_t *handle,
1998 struct ext4_dir_entry_2 *de_del,
1999 struct buffer_head *bh)
2001 struct ext4_dir_entry_2 *de, *pde;
2002 unsigned int blocksize = dir->i_sb->s_blocksize;
2006 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2007 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2008 csum_size = sizeof(struct ext4_dir_entry_tail);
2012 de = (struct ext4_dir_entry_2 *) bh->b_data;
2013 while (i < bh->b_size - csum_size) {
2014 if (ext4_check_dir_entry(dir, NULL, de, bh, i))
2017 BUFFER_TRACE(bh, "get_write_access");
2018 err = ext4_journal_get_write_access(handle, bh);
2019 if (unlikely(err)) {
2020 ext4_std_error(dir->i_sb, err);
2024 pde->rec_len = ext4_rec_len_to_disk(
2025 ext4_rec_len_from_disk(pde->rec_len,
2027 ext4_rec_len_from_disk(de->rec_len,
2033 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
2034 err = ext4_handle_dirty_dirent_node(handle, dir, bh);
2035 if (unlikely(err)) {
2036 ext4_std_error(dir->i_sb, err);
2041 i += ext4_rec_len_from_disk(de->rec_len, blocksize);
2043 de = ext4_next_entry(de, blocksize);
2049 * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2,
2050 * since this indicates that nlinks count was previously 1.
2052 static void ext4_inc_count(handle_t *handle, struct inode *inode)
2055 if (is_dx(inode) && inode->i_nlink > 1) {
2056 /* limit is 16-bit i_links_count */
2057 if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) {
2058 set_nlink(inode, 1);
2059 EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb,
2060 EXT4_FEATURE_RO_COMPAT_DIR_NLINK);
2066 * If a directory had nlink == 1, then we should let it be 1. This indicates
2067 * directory has >EXT4_LINK_MAX subdirs.
2069 static void ext4_dec_count(handle_t *handle, struct inode *inode)
2071 if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2)
2076 static int ext4_add_nondir(handle_t *handle,
2077 struct dentry *dentry, struct inode *inode)
2079 int err = ext4_add_entry(handle, dentry, inode);
2081 ext4_mark_inode_dirty(handle, inode);
2082 unlock_new_inode(inode);
2083 d_instantiate(dentry, inode);
2087 unlock_new_inode(inode);
2093 * By the time this is called, we already have created
2094 * the directory cache entry for the new file, but it
2095 * is so far negative - it has no inode.
2097 * If the create succeeds, we fill in the inode information
2098 * with d_instantiate().
2100 static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2104 struct inode *inode;
2105 int err, retries = 0;
2107 dquot_initialize(dir);
2110 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2111 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2112 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2114 return PTR_ERR(handle);
2116 if (IS_DIRSYNC(dir))
2117 ext4_handle_sync(handle);
2119 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
2120 err = PTR_ERR(inode);
2121 if (!IS_ERR(inode)) {
2122 inode->i_op = &ext4_file_inode_operations;
2123 inode->i_fop = &ext4_file_operations;
2124 ext4_set_aops(inode);
2125 err = ext4_add_nondir(handle, dentry, inode);
2127 ext4_journal_stop(handle);
2128 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2133 static int ext4_mknod(struct inode *dir, struct dentry *dentry,
2134 umode_t mode, dev_t rdev)
2137 struct inode *inode;
2138 int err, retries = 0;
2140 if (!new_valid_dev(rdev))
2143 dquot_initialize(dir);
2146 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2147 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2148 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2150 return PTR_ERR(handle);
2152 if (IS_DIRSYNC(dir))
2153 ext4_handle_sync(handle);
2155 inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL);
2156 err = PTR_ERR(inode);
2157 if (!IS_ERR(inode)) {
2158 init_special_inode(inode, inode->i_mode, rdev);
2159 #ifdef CONFIG_EXT4_FS_XATTR
2160 inode->i_op = &ext4_special_inode_operations;
2162 err = ext4_add_nondir(handle, dentry, inode);
2164 ext4_journal_stop(handle);
2165 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2170 static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
2173 struct inode *inode;
2174 struct buffer_head *dir_block = NULL;
2175 struct ext4_dir_entry_2 *de;
2176 struct ext4_dir_entry_tail *t;
2177 unsigned int blocksize = dir->i_sb->s_blocksize;
2179 int err, retries = 0;
2181 if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb,
2182 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
2183 csum_size = sizeof(struct ext4_dir_entry_tail);
2185 if (EXT4_DIR_LINK_MAX(dir))
2188 dquot_initialize(dir);
2191 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2192 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2193 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb));
2195 return PTR_ERR(handle);
2197 if (IS_DIRSYNC(dir))
2198 ext4_handle_sync(handle);
2200 inode = ext4_new_inode(handle, dir, S_IFDIR | mode,
2201 &dentry->d_name, 0, NULL);
2202 err = PTR_ERR(inode);
2206 inode->i_op = &ext4_dir_inode_operations;
2207 inode->i_fop = &ext4_dir_operations;
2208 inode->i_size = EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize;
2209 dir_block = ext4_bread(handle, inode, 0, 1, &err);
2211 goto out_clear_inode;
2212 BUFFER_TRACE(dir_block, "get_write_access");
2213 err = ext4_journal_get_write_access(handle, dir_block);
2215 goto out_clear_inode;
2216 de = (struct ext4_dir_entry_2 *) dir_block->b_data;
2217 de->inode = cpu_to_le32(inode->i_ino);
2219 de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len),
2221 strcpy(de->name, ".");
2222 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
2223 de = ext4_next_entry(de, blocksize);
2224 de->inode = cpu_to_le32(dir->i_ino);
2225 de->rec_len = ext4_rec_len_to_disk(blocksize -
2226 (csum_size + EXT4_DIR_REC_LEN(1)),
2229 strcpy(de->name, "..");
2230 ext4_set_de_type(dir->i_sb, de, S_IFDIR);
2231 set_nlink(inode, 2);
2234 t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize);
2235 initialize_dirent_tail(t, blocksize);
2238 BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata");
2239 err = ext4_handle_dirty_dirent_node(handle, inode, dir_block);
2241 goto out_clear_inode;
2242 set_buffer_verified(dir_block);
2243 err = ext4_mark_inode_dirty(handle, inode);
2245 err = ext4_add_entry(handle, dentry, inode);
2249 unlock_new_inode(inode);
2250 ext4_mark_inode_dirty(handle, inode);
2254 ext4_inc_count(handle, dir);
2255 ext4_update_dx_flag(dir);
2256 err = ext4_mark_inode_dirty(handle, dir);
2258 goto out_clear_inode;
2259 unlock_new_inode(inode);
2260 d_instantiate(dentry, inode);
2263 ext4_journal_stop(handle);
2264 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2270 * routine to check that the specified directory is empty (for rmdir)
2272 static int empty_dir(struct inode *inode)
2274 unsigned int offset;
2275 struct buffer_head *bh;
2276 struct ext4_dir_entry_2 *de, *de1;
2277 struct super_block *sb;
2281 if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) ||
2282 !(bh = ext4_bread(NULL, inode, 0, 0, &err))) {
2284 EXT4_ERROR_INODE(inode,
2285 "error %d reading directory lblock 0", err);
2287 ext4_warning(inode->i_sb,
2288 "bad directory (dir #%lu) - no data block",
2292 if (!buffer_verified(bh) &&
2293 !ext4_dirent_csum_verify(inode,
2294 (struct ext4_dir_entry *)bh->b_data)) {
2295 EXT4_ERROR_INODE(inode, "checksum error reading directory "
2299 set_buffer_verified(bh);
2300 de = (struct ext4_dir_entry_2 *) bh->b_data;
2301 de1 = ext4_next_entry(de, sb->s_blocksize);
2302 if (le32_to_cpu(de->inode) != inode->i_ino ||
2303 !le32_to_cpu(de1->inode) ||
2304 strcmp(".", de->name) ||
2305 strcmp("..", de1->name)) {
2306 ext4_warning(inode->i_sb,
2307 "bad directory (dir #%lu) - no `.' or `..'",
2312 offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) +
2313 ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize);
2314 de = ext4_next_entry(de1, sb->s_blocksize);
2315 while (offset < inode->i_size) {
2317 (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) {
2318 unsigned int lblock;
2321 lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb);
2322 bh = ext4_bread(NULL, inode, lblock, 0, &err);
2325 EXT4_ERROR_INODE(inode,
2326 "error %d reading directory "
2327 "lblock %u", err, lblock);
2328 offset += sb->s_blocksize;
2331 if (!buffer_verified(bh) &&
2332 !ext4_dirent_csum_verify(inode,
2333 (struct ext4_dir_entry *)bh->b_data)) {
2334 EXT4_ERROR_INODE(inode, "checksum error "
2335 "reading directory lblock 0");
2338 set_buffer_verified(bh);
2339 de = (struct ext4_dir_entry_2 *) bh->b_data;
2341 if (ext4_check_dir_entry(inode, NULL, de, bh, offset)) {
2342 de = (struct ext4_dir_entry_2 *)(bh->b_data +
2344 offset = (offset | (sb->s_blocksize - 1)) + 1;
2347 if (le32_to_cpu(de->inode)) {
2351 offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize);
2352 de = ext4_next_entry(de, sb->s_blocksize);
2358 /* ext4_orphan_add() links an unlinked or truncated inode into a list of
2359 * such inodes, starting at the superblock, in case we crash before the
2360 * file is closed/deleted, or in case the inode truncate spans multiple
2361 * transactions and the last transaction is not recovered after a crash.
2363 * At filesystem recovery time, we walk this list deleting unlinked
2364 * inodes and truncating linked inodes in ext4_orphan_cleanup().
2366 int ext4_orphan_add(handle_t *handle, struct inode *inode)
2368 struct super_block *sb = inode->i_sb;
2369 struct ext4_iloc iloc;
2372 if (!EXT4_SB(sb)->s_journal)
2375 mutex_lock(&EXT4_SB(sb)->s_orphan_lock);
2376 if (!list_empty(&EXT4_I(inode)->i_orphan))
2380 * Orphan handling is only valid for files with data blocks
2381 * being truncated, or files being unlinked. Note that we either
2382 * hold i_mutex, or the inode can not be referenced from outside,
2383 * so i_nlink should not be bumped due to race
2385 J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2386 S_ISLNK(inode->i_mode)) || inode->i_nlink == 0);
2388 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
2389 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
2393 err = ext4_reserve_inode_write(handle, inode, &iloc);
2397 * Due to previous errors inode may be already a part of on-disk
2398 * orphan list. If so skip on-disk list modification.
2400 if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
2401 (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count)))
2404 /* Insert this inode at the head of the on-disk orphan list... */
2405 NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan);
2406 EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
2407 err = ext4_handle_dirty_super(handle, sb);
2408 rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
2412 /* Only add to the head of the in-memory list if all the
2413 * previous operations succeeded. If the orphan_add is going to
2414 * fail (possibly taking the journal offline), we can't risk
2415 * leaving the inode on the orphan list: stray orphan-list
2416 * entries can cause panics at unmount time.
2418 * This is safe: on error we're going to ignore the orphan list
2419 * anyway on the next recovery. */
2422 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2424 jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
2425 jbd_debug(4, "orphan inode %lu will point to %d\n",
2426 inode->i_ino, NEXT_ORPHAN(inode));
2428 mutex_unlock(&EXT4_SB(sb)->s_orphan_lock);
2429 ext4_std_error(inode->i_sb, err);
2434 * ext4_orphan_del() removes an unlinked or truncated inode from the list
2435 * of such inodes stored on disk, because it is finally being cleaned up.
2437 int ext4_orphan_del(handle_t *handle, struct inode *inode)
2439 struct list_head *prev;
2440 struct ext4_inode_info *ei = EXT4_I(inode);
2441 struct ext4_sb_info *sbi;
2443 struct ext4_iloc iloc;
2446 if (!EXT4_SB(inode->i_sb)->s_journal)
2449 mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2450 if (list_empty(&ei->i_orphan))
2453 ino_next = NEXT_ORPHAN(inode);
2454 prev = ei->i_orphan.prev;
2455 sbi = EXT4_SB(inode->i_sb);
2457 jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
2459 list_del_init(&ei->i_orphan);
2461 /* If we're on an error path, we may not have a valid
2462 * transaction handle with which to update the orphan list on
2463 * disk, but we still need to remove the inode from the linked
2464 * list in memory. */
2468 err = ext4_reserve_inode_write(handle, inode, &iloc);
2472 if (prev == &sbi->s_orphan) {
2473 jbd_debug(4, "superblock will point to %u\n", ino_next);
2474 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
2475 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
2478 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
2479 err = ext4_handle_dirty_super(handle, inode->i_sb);
2481 struct ext4_iloc iloc2;
2482 struct inode *i_prev =
2483 &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode;
2485 jbd_debug(4, "orphan inode %lu will point to %u\n",
2486 i_prev->i_ino, ino_next);
2487 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
2490 NEXT_ORPHAN(i_prev) = ino_next;
2491 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
2495 NEXT_ORPHAN(inode) = 0;
2496 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
2499 ext4_std_error(inode->i_sb, err);
2501 mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock);
2509 static int ext4_rmdir(struct inode *dir, struct dentry *dentry)
2512 struct inode *inode;
2513 struct buffer_head *bh;
2514 struct ext4_dir_entry_2 *de;
2517 /* Initialize quotas before so that eventual writes go in
2518 * separate transaction */
2519 dquot_initialize(dir);
2520 dquot_initialize(dentry->d_inode);
2522 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2524 return PTR_ERR(handle);
2527 bh = ext4_find_entry(dir, &dentry->d_name, &de);
2531 if (IS_DIRSYNC(dir))
2532 ext4_handle_sync(handle);
2534 inode = dentry->d_inode;
2537 if (le32_to_cpu(de->inode) != inode->i_ino)
2540 retval = -ENOTEMPTY;
2541 if (!empty_dir(inode))
2544 retval = ext4_delete_entry(handle, dir, de, bh);
2547 if (!EXT4_DIR_LINK_EMPTY(inode))
2548 ext4_warning(inode->i_sb,
2549 "empty directory has too many links (%d)",
2553 /* There's no need to set i_disksize: the fact that i_nlink is
2554 * zero will ensure that the right thing happens during any
2557 ext4_orphan_add(handle, inode);
2558 inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode);
2559 ext4_mark_inode_dirty(handle, inode);
2560 ext4_dec_count(handle, dir);
2561 ext4_update_dx_flag(dir);
2562 ext4_mark_inode_dirty(handle, dir);
2565 ext4_journal_stop(handle);
2570 static int ext4_unlink(struct inode *dir, struct dentry *dentry)
2573 struct inode *inode;
2574 struct buffer_head *bh;
2575 struct ext4_dir_entry_2 *de;
2578 trace_ext4_unlink_enter(dir, dentry);
2579 /* Initialize quotas before so that eventual writes go
2580 * in separate transaction */
2581 dquot_initialize(dir);
2582 dquot_initialize(dentry->d_inode);
2584 handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb));
2586 return PTR_ERR(handle);
2588 if (IS_DIRSYNC(dir))
2589 ext4_handle_sync(handle);
2592 bh = ext4_find_entry(dir, &dentry->d_name, &de);
2596 inode = dentry->d_inode;
2599 if (le32_to_cpu(de->inode) != inode->i_ino)
2602 if (!inode->i_nlink) {
2603 ext4_warning(inode->i_sb,
2604 "Deleting nonexistent file (%lu), %d",
2605 inode->i_ino, inode->i_nlink);
2606 set_nlink(inode, 1);
2608 retval = ext4_delete_entry(handle, dir, de, bh);
2611 dir->i_ctime = dir->i_mtime = ext4_current_time(dir);
2612 ext4_update_dx_flag(dir);
2613 ext4_mark_inode_dirty(handle, dir);
2615 if (!inode->i_nlink)
2616 ext4_orphan_add(handle, inode);
2617 inode->i_ctime = ext4_current_time(inode);
2618 ext4_mark_inode_dirty(handle, inode);
2622 ext4_journal_stop(handle);
2624 trace_ext4_unlink_exit(dentry, retval);
2628 static int ext4_symlink(struct inode *dir,
2629 struct dentry *dentry, const char *symname)
2632 struct inode *inode;
2633 int l, err, retries = 0;
2636 l = strlen(symname)+1;
2637 if (l > dir->i_sb->s_blocksize)
2638 return -ENAMETOOLONG;
2640 dquot_initialize(dir);
2642 if (l > EXT4_N_BLOCKS * 4) {
2644 * For non-fast symlinks, we just allocate inode and put it on
2645 * orphan list in the first transaction => we need bitmap,
2646 * group descriptor, sb, inode block, quota blocks, and
2647 * possibly selinux xattr blocks.
2649 credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) +
2650 EXT4_XATTR_TRANS_BLOCKS;
2653 * Fast symlink. We have to add entry to directory
2654 * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS),
2655 * allocate new inode (bitmap, group descriptor, inode block,
2656 * quota blocks, sb is already counted in previous macros).
2658 credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2659 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
2660 EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb);
2663 handle = ext4_journal_start(dir, credits);
2665 return PTR_ERR(handle);
2667 if (IS_DIRSYNC(dir))
2668 ext4_handle_sync(handle);
2670 inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO,
2671 &dentry->d_name, 0, NULL);
2672 err = PTR_ERR(inode);
2676 if (l > EXT4_N_BLOCKS * 4) {
2677 inode->i_op = &ext4_symlink_inode_operations;
2678 ext4_set_aops(inode);
2680 * We cannot call page_symlink() with transaction started
2681 * because it calls into ext4_write_begin() which can wait
2682 * for transaction commit if we are running out of space
2683 * and thus we deadlock. So we have to stop transaction now
2684 * and restart it when symlink contents is written.
2686 * To keep fs consistent in case of crash, we have to put inode
2687 * to orphan list in the mean time.
2690 err = ext4_orphan_add(handle, inode);
2691 ext4_journal_stop(handle);
2693 goto err_drop_inode;
2694 err = __page_symlink(inode, symname, l, 1);
2696 goto err_drop_inode;
2698 * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS
2699 * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified
2701 handle = ext4_journal_start(dir,
2702 EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2703 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1);
2704 if (IS_ERR(handle)) {
2705 err = PTR_ERR(handle);
2706 goto err_drop_inode;
2708 set_nlink(inode, 1);
2709 err = ext4_orphan_del(handle, inode);
2711 ext4_journal_stop(handle);
2713 goto err_drop_inode;
2716 /* clear the extent format for fast symlink */
2717 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS);
2718 inode->i_op = &ext4_fast_symlink_inode_operations;
2719 memcpy((char *)&EXT4_I(inode)->i_data, symname, l);
2720 inode->i_size = l-1;
2722 EXT4_I(inode)->i_disksize = inode->i_size;
2723 err = ext4_add_nondir(handle, dentry, inode);
2725 ext4_journal_stop(handle);
2726 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2730 unlock_new_inode(inode);
2735 static int ext4_link(struct dentry *old_dentry,
2736 struct inode *dir, struct dentry *dentry)
2739 struct inode *inode = old_dentry->d_inode;
2740 int err, retries = 0;
2742 if (inode->i_nlink >= EXT4_LINK_MAX)
2745 dquot_initialize(dir);
2748 handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) +
2749 EXT4_INDEX_EXTRA_TRANS_BLOCKS);
2751 return PTR_ERR(handle);
2753 if (IS_DIRSYNC(dir))
2754 ext4_handle_sync(handle);
2756 inode->i_ctime = ext4_current_time(inode);
2757 ext4_inc_count(handle, inode);
2760 err = ext4_add_entry(handle, dentry, inode);
2762 ext4_mark_inode_dirty(handle, inode);
2763 d_instantiate(dentry, inode);
2768 ext4_journal_stop(handle);
2769 if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries))
2774 #define PARENT_INO(buffer, size) \
2775 (ext4_next_entry((struct ext4_dir_entry_2 *)(buffer), size)->inode)
2778 * Anybody can rename anything with this: the permission checks are left to the
2779 * higher-level routines.
2781 static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry,
2782 struct inode *new_dir, struct dentry *new_dentry)
2785 struct inode *old_inode, *new_inode;
2786 struct buffer_head *old_bh, *new_bh, *dir_bh;
2787 struct ext4_dir_entry_2 *old_de, *new_de;
2788 int retval, force_da_alloc = 0;
2790 dquot_initialize(old_dir);
2791 dquot_initialize(new_dir);
2793 old_bh = new_bh = dir_bh = NULL;
2795 /* Initialize quotas before so that eventual writes go
2796 * in separate transaction */
2797 if (new_dentry->d_inode)
2798 dquot_initialize(new_dentry->d_inode);
2799 handle = ext4_journal_start(old_dir, 2 *
2800 EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) +
2801 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2);
2803 return PTR_ERR(handle);
2805 if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir))
2806 ext4_handle_sync(handle);
2808 old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de);
2810 * Check for inode number is _not_ due to possible IO errors.
2811 * We might rmdir the source, keep it as pwd of some process
2812 * and merrily kill the link to whatever was created under the
2813 * same name. Goodbye sticky bit ;-<
2815 old_inode = old_dentry->d_inode;
2817 if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino)
2820 new_inode = new_dentry->d_inode;
2821 new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, &new_de);
2828 if (S_ISDIR(old_inode->i_mode)) {
2830 retval = -ENOTEMPTY;
2831 if (!empty_dir(new_inode))
2835 dir_bh = ext4_bread(handle, old_inode, 0, 0, &retval);
2838 if (!buffer_verified(dir_bh) &&
2839 !ext4_dirent_csum_verify(old_inode,
2840 (struct ext4_dir_entry *)dir_bh->b_data))
2842 set_buffer_verified(dir_bh);
2843 if (le32_to_cpu(PARENT_INO(dir_bh->b_data,
2844 old_dir->i_sb->s_blocksize)) != old_dir->i_ino)
2847 if (!new_inode && new_dir != old_dir &&
2848 EXT4_DIR_LINK_MAX(new_dir))
2850 BUFFER_TRACE(dir_bh, "get_write_access");
2851 retval = ext4_journal_get_write_access(handle, dir_bh);
2856 retval = ext4_add_entry(handle, new_dentry, old_inode);
2860 BUFFER_TRACE(new_bh, "get write access");
2861 retval = ext4_journal_get_write_access(handle, new_bh);
2864 new_de->inode = cpu_to_le32(old_inode->i_ino);
2865 if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb,
2866 EXT4_FEATURE_INCOMPAT_FILETYPE))
2867 new_de->file_type = old_de->file_type;
2868 new_dir->i_version++;
2869 new_dir->i_ctime = new_dir->i_mtime =
2870 ext4_current_time(new_dir);
2871 ext4_mark_inode_dirty(handle, new_dir);
2872 BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata");
2873 retval = ext4_handle_dirty_dirent_node(handle, new_dir, new_bh);
2874 if (unlikely(retval)) {
2875 ext4_std_error(new_dir->i_sb, retval);
2883 * Like most other Unix systems, set the ctime for inodes on a
2886 old_inode->i_ctime = ext4_current_time(old_inode);
2887 ext4_mark_inode_dirty(handle, old_inode);
2892 if (le32_to_cpu(old_de->inode) != old_inode->i_ino ||
2893 old_de->name_len != old_dentry->d_name.len ||
2894 strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) ||
2895 (retval = ext4_delete_entry(handle, old_dir,
2896 old_de, old_bh)) == -ENOENT) {
2897 /* old_de could have moved from under us during htree split, so
2898 * make sure that we are deleting the right entry. We might
2899 * also be pointing to a stale entry in the unused part of
2900 * old_bh so just checking inum and the name isn't enough. */
2901 struct buffer_head *old_bh2;
2902 struct ext4_dir_entry_2 *old_de2;
2904 old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de2);
2906 retval = ext4_delete_entry(handle, old_dir,
2912 ext4_warning(old_dir->i_sb,
2913 "Deleting old file (%lu), %d, error=%d",
2914 old_dir->i_ino, old_dir->i_nlink, retval);
2918 ext4_dec_count(handle, new_inode);
2919 new_inode->i_ctime = ext4_current_time(new_inode);
2921 old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir);
2922 ext4_update_dx_flag(old_dir);
2924 PARENT_INO(dir_bh->b_data, new_dir->i_sb->s_blocksize) =
2925 cpu_to_le32(new_dir->i_ino);
2926 BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata");
2927 if (is_dx(old_inode)) {
2928 retval = ext4_handle_dirty_dx_node(handle,
2932 retval = ext4_handle_dirty_dirent_node(handle,
2937 ext4_std_error(old_dir->i_sb, retval);
2940 ext4_dec_count(handle, old_dir);
2942 /* checked empty_dir above, can't have another parent,
2943 * ext4_dec_count() won't work for many-linked dirs */
2944 clear_nlink(new_inode);
2946 ext4_inc_count(handle, new_dir);
2947 ext4_update_dx_flag(new_dir);
2948 ext4_mark_inode_dirty(handle, new_dir);
2951 ext4_mark_inode_dirty(handle, old_dir);
2953 ext4_mark_inode_dirty(handle, new_inode);
2954 if (!new_inode->i_nlink)
2955 ext4_orphan_add(handle, new_inode);
2956 if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC))
2965 ext4_journal_stop(handle);
2966 if (retval == 0 && force_da_alloc)
2967 ext4_alloc_da_blocks(old_inode);
2972 * directories can handle most operations...
2974 const struct inode_operations ext4_dir_inode_operations = {
2975 .create = ext4_create,
2976 .lookup = ext4_lookup,
2978 .unlink = ext4_unlink,
2979 .symlink = ext4_symlink,
2980 .mkdir = ext4_mkdir,
2981 .rmdir = ext4_rmdir,
2982 .mknod = ext4_mknod,
2983 .rename = ext4_rename,
2984 .setattr = ext4_setattr,
2985 #ifdef CONFIG_EXT4_FS_XATTR
2986 .setxattr = generic_setxattr,
2987 .getxattr = generic_getxattr,
2988 .listxattr = ext4_listxattr,
2989 .removexattr = generic_removexattr,
2991 .get_acl = ext4_get_acl,
2992 .fiemap = ext4_fiemap,
2995 const struct inode_operations ext4_special_inode_operations = {
2996 .setattr = ext4_setattr,
2997 #ifdef CONFIG_EXT4_FS_XATTR
2998 .setxattr = generic_setxattr,
2999 .getxattr = generic_getxattr,
3000 .listxattr = ext4_listxattr,
3001 .removexattr = generic_removexattr,
3003 .get_acl = ext4_get_acl,