2 * rehash.c --- rebuild hash tree directories
4 * Copyright (C) 2002 Theodore Ts'o
7 * This file may be redistributed under the terms of the GNU Public
11 * This algorithm is designed for simplicity of implementation and to
12 * pack the directory as much as possible. It however requires twice
13 * as much memory as the size of the directory. The maximum size
14 * directory supported using a 4k blocksize is roughly a gigabyte, and
15 * so there may very well be problems with machines that don't have
16 * virtual memory, and obscenely large directories.
18 * An alternate algorithm which is much more disk intensive could be
19 * written, and probably will need to be written in the future. The
20 * design goals of such an algorithm are: (a) use (roughly) constant
21 * amounts of memory, no matter how large the directory, (b) the
22 * directory must be safe at all times, even if e2fsck is interrupted
23 * in the middle, (c) we must use minimal amounts of extra disk
24 * blocks. This pretty much requires an incremental approach, where
25 * we are reading from one part of the directory, and inserting into
26 * the front half. So the algorithm will have to keep track of a
27 * moving block boundary between the new tree and the old tree, and
28 * files will need to be moved from the old directory and inserted
29 * into the new tree. If the new directory requires space which isn't
30 * yet available, blocks from the beginning part of the old directory
31 * may need to be moved to the end of the directory to make room for
34 * --------------------------------------------------------
35 * | new tree | | old tree |
36 * --------------------------------------------------------
40 * This is going to be a pain in the tuckus to implement, and will
41 * require a lot more disk accesses. So I'm going to skip it for now;
42 * it's only really going to be an issue for really, really big
43 * filesystems (when we reach the level of tens of millions of files
44 * in a single directory). It will probably be easier to simply
45 * require that e2fsck use VM first.
55 /* Schedule a dir to be rebuilt during pass 3A. */
56 void e2fsck_rehash_dir_later(e2fsck_t ctx, ext2_ino_t ino)
58 if (!ctx->dirs_to_hash)
59 ext2fs_u32_list_create(&ctx->dirs_to_hash, 50);
60 if (ctx->dirs_to_hash)
61 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
64 /* Ask if a dir will be rebuilt during pass 3A. */
65 int e2fsck_dir_will_be_rehashed(e2fsck_t ctx, ext2_ino_t ino)
67 if (ctx->options & E2F_OPT_COMPRESS_DIRS)
69 if (!ctx->dirs_to_hash)
71 return ext2fs_u32_list_test(ctx->dirs_to_hash, ino);
76 struct fill_dir_struct {
78 struct ext2_inode *inode;
82 struct hash_entry *harray;
83 int max_array, num_array;
84 unsigned int dir_size;
88 struct ext2_dir_entry *dot_de;
89 struct ext2_dir_entry *dotdot_de;
94 ext2_dirhash_t minor_hash;
96 struct ext2_dir_entry *dir;
103 ext2_dirhash_t *hashes;
106 static int fill_dir_block(ext2_filsys fs,
108 e2_blkcnt_t blockcnt,
109 blk64_t ref_block EXT2FS_ATTR((unused)),
110 int ref_offset EXT2FS_ATTR((unused)),
113 struct fill_dir_struct *fd = (struct fill_dir_struct *) priv_data;
114 struct hash_entry *new_array, *ent;
115 struct ext2_dir_entry *dirent;
117 unsigned int offset, dir_offset, rec_len, name_len;
123 offset = blockcnt * fs->blocksize;
124 if (offset + fs->blocksize > fd->inode->i_size) {
125 fd->err = EXT2_ET_DIR_CORRUPTED;
129 dir = (fd->buf+offset);
130 if (*block_nr == 0) {
131 memset(dir, 0, fs->blocksize);
132 dirent = (struct ext2_dir_entry *) dir;
133 (void) ext2fs_set_rec_len(fs, fs->blocksize, dirent);
135 int flags = fs->flags;
136 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
137 fd->err = ext2fs_read_dir_block4(fs, *block_nr, dir, 0,
139 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
140 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
144 hash_alg = fs->super->s_def_hash_version;
145 if ((hash_alg <= EXT2_HASH_TEA) &&
146 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
148 /* While the directory block is "hot", index it. */
150 while (dir_offset < fs->blocksize) {
151 dirent = (struct ext2_dir_entry *) (dir + dir_offset);
152 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
153 name_len = ext2fs_dirent_name_len(dirent);
154 if (((dir_offset + rec_len) > fs->blocksize) ||
156 ((rec_len % 4) != 0) ||
157 (name_len + 8 > rec_len)) {
158 fd->err = EXT2_ET_DIR_CORRUPTED;
161 dir_offset += rec_len;
162 if (dirent->inode == 0)
164 if (!fd->compress && (name_len == 1) &&
165 (dirent->name[0] == '.')) {
169 if (!fd->compress && (name_len == 2) &&
170 (dirent->name[0] == '.') && (dirent->name[1] == '.')) {
171 fd->parent = dirent->inode;
172 fd->dotdot_de = dirent;
175 if (fd->num_array >= fd->max_array) {
176 new_array = realloc(fd->harray,
177 sizeof(struct hash_entry) * (fd->max_array+500));
182 fd->harray = new_array;
183 fd->max_array += 500;
185 ent = fd->harray + fd->num_array++;
187 fd->dir_size += EXT2_DIR_REC_LEN(dirent);
188 ent->ino = dirent->inode;
190 ent->hash = ent->minor_hash = 0;
192 fd->err = ext2fs_dirhash(hash_alg, dirent->name,
194 fs->super->s_hash_seed,
195 &ent->hash, &ent->minor_hash);
204 /* Used for sorting the hash entry */
205 static EXT2_QSORT_TYPE ino_cmp(const void *a, const void *b)
207 const struct hash_entry *he_a = (const struct hash_entry *) a;
208 const struct hash_entry *he_b = (const struct hash_entry *) b;
210 return (he_a->ino - he_b->ino);
213 /* Used for sorting the hash entry */
214 static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
216 const struct hash_entry *he_a = (const struct hash_entry *) a;
217 const struct hash_entry *he_b = (const struct hash_entry *) b;
218 unsigned int he_a_len, he_b_len, min_len;
221 he_a_len = ext2fs_dirent_name_len(he_a->dir);
222 he_b_len = ext2fs_dirent_name_len(he_b->dir);
224 if (min_len > he_b_len)
227 ret = memcmp(he_a->dir->name, he_b->dir->name, min_len);
229 if (he_a_len > he_b_len)
231 else if (he_a_len < he_b_len)
234 ret = he_b->dir->inode - he_a->dir->inode;
239 /* Used for sorting the hash entry */
240 static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
242 const struct hash_entry *he_a = (const struct hash_entry *) a;
243 const struct hash_entry *he_b = (const struct hash_entry *) b;
246 if (he_a->hash > he_b->hash)
248 else if (he_a->hash < he_b->hash)
251 if (he_a->minor_hash > he_b->minor_hash)
253 else if (he_a->minor_hash < he_b->minor_hash)
256 ret = name_cmp(a, b);
261 static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,
267 new_mem = realloc(outdir->buf, blocks * fs->blocksize);
270 outdir->buf = new_mem;
271 new_mem = realloc(outdir->hashes,
272 blocks * sizeof(ext2_dirhash_t));
275 outdir->hashes = new_mem;
277 outdir->buf = malloc(blocks * fs->blocksize);
278 outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t));
281 outdir->max = blocks;
285 static void free_out_dir(struct out_dir *outdir)
288 free(outdir->hashes);
293 static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
298 if (outdir->num >= outdir->max) {
299 retval = alloc_size_dir(fs, outdir, outdir->max + 50);
303 *ret = outdir->buf + (outdir->num++ * fs->blocksize);
304 memset(*ret, 0, fs->blocksize);
309 * This function is used to make a unique filename. We do this by
310 * appending ~0, and then incrementing the number. However, we cannot
311 * expand the length of the filename beyond the padding available in
312 * the directory entry.
314 static void mutate_name(char *str, unsigned int *len)
317 unsigned int l = *len;
320 * First check to see if it looks the name has been mutated
323 for (i = l-1; i > 0; i--) {
324 if (!isdigit(str[i]))
327 if ((i == (int)l - 1) || (str[i] != '~')) {
337 for (i = l-1; i >= 0; i--) {
338 if (isdigit(str[i])) {
350 else if (str[0] == 'Z') {
368 static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
370 struct fill_dir_struct *fd)
372 struct problem_context pctx;
373 struct hash_entry *ent, *prev;
377 unsigned int new_len;
380 clear_problem_context(&pctx);
383 hash_alg = fs->super->s_def_hash_version;
384 if ((hash_alg <= EXT2_HASH_TEA) &&
385 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
388 for (i=1; i < fd->num_array; i++) {
389 ent = fd->harray + i;
391 if (!ent->dir->inode ||
392 (ext2fs_dirent_name_len(ent->dir) !=
393 ext2fs_dirent_name_len(prev->dir)) ||
394 memcmp(ent->dir->name, prev->dir->name,
395 ext2fs_dirent_name_len(ent->dir)))
397 pctx.dirent = ent->dir;
398 if ((ent->dir->inode == prev->dir->inode) &&
399 fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) {
400 e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
405 new_len = ext2fs_dirent_name_len(ent->dir);
406 memcpy(new_name, ent->dir->name, new_len);
407 mutate_name(new_name, &new_len);
408 for (j=0; j < fd->num_array; j++) {
411 (unsigned) ext2fs_dirent_name_len(fd->harray[j].dir)) ||
412 memcmp(new_name, fd->harray[j].dir->name, new_len))
414 mutate_name(new_name, &new_len);
418 new_name[new_len] = 0;
420 if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) {
421 memcpy(ent->dir->name, new_name, new_len);
422 ext2fs_dirent_set_name_len(ent->dir, new_len);
423 ext2fs_dirhash(hash_alg, new_name, new_len,
424 fs->super->s_hash_seed,
425 &ent->hash, &ent->minor_hash);
433 static errcode_t copy_dir_entries(e2fsck_t ctx,
434 struct fill_dir_struct *fd,
435 struct out_dir *outdir)
437 ext2_filsys fs = ctx->fs;
440 struct hash_entry *ent;
441 struct ext2_dir_entry *dirent;
442 unsigned int rec_len, prev_rec_len, left, slack, offset;
444 ext2_dirhash_t prev_hash;
446 struct ext2_dir_entry_tail *t;
448 if (ctx->htree_slack_percentage == 255) {
449 profile_get_uint(ctx->profile, "options",
450 "indexed_dir_slack_percentage",
452 &ctx->htree_slack_percentage);
453 if (ctx->htree_slack_percentage > 100)
454 ctx->htree_slack_percentage = 20;
457 if (ext2fs_has_feature_metadata_csum(fs->super))
458 csum_size = sizeof(struct ext2_dir_entry_tail);
461 retval = alloc_size_dir(fs, outdir,
462 (fd->dir_size / fs->blocksize) + 2);
465 outdir->num = fd->compress ? 0 : 1;
467 outdir->hashes[0] = 0;
469 if ((retval = get_next_block(fs, outdir, &block_start)))
471 dirent = (struct ext2_dir_entry *) block_start;
474 left = fs->blocksize - csum_size;
475 slack = fd->compress ? 12 :
476 ((fs->blocksize - csum_size) * ctx->htree_slack_percentage)/100;
479 for (i = 0; i < fd->num_array; i++) {
480 ent = fd->harray + i;
481 if (ent->dir->inode == 0)
483 rec_len = EXT2_DIR_REC_LEN(ent->dir);
484 if (rec_len > left) {
486 left += prev_rec_len;
487 retval = ext2fs_set_rec_len(fs, left, dirent);
492 t = EXT2_DIRENT_TAIL(block_start,
494 ext2fs_initialize_dirent_tail(fs, t);
496 if ((retval = get_next_block(fs, outdir,
501 left = (fs->blocksize - csum_size) - offset;
502 dirent = (struct ext2_dir_entry *) (block_start + offset);
504 if (ent->hash == prev_hash)
505 outdir->hashes[outdir->num-1] = ent->hash | 1;
507 outdir->hashes[outdir->num-1] = ent->hash;
509 dirent->inode = ent->dir->inode;
510 ext2fs_dirent_set_name_len(dirent,
511 ext2fs_dirent_name_len(ent->dir));
512 ext2fs_dirent_set_file_type(dirent,
513 ext2fs_dirent_file_type(ent->dir));
514 retval = ext2fs_set_rec_len(fs, rec_len, dirent);
517 prev_rec_len = rec_len;
518 memcpy(dirent->name, ent->dir->name, rec_len);
522 prev_rec_len += left;
523 retval = ext2fs_set_rec_len(fs, prev_rec_len, dirent);
529 prev_hash = ent->hash;
532 retval = ext2fs_set_rec_len(fs, rec_len + left, dirent);
534 t = EXT2_DIRENT_TAIL(block_start, fs->blocksize);
535 ext2fs_initialize_dirent_tail(fs, t);
542 static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
543 ext2_ino_t ino, ext2_ino_t parent,
544 struct ext2_dir_entry *dot_de,
545 struct ext2_dir_entry *dotdot_de)
547 struct ext2_dir_entry *dirent;
548 struct ext2_dx_root_info *root;
549 struct ext2_dx_countlimit *limits;
554 memset(buf, 0, fs->blocksize);
555 dirent = (struct ext2_dir_entry *) buf;
558 dirent->name_len = dot_de->name_len;
559 offset = rec_len = dirent->rec_len = dot_de->rec_len;
560 memcpy(dirent->name, dot_de->name, rec_len);
562 dirent = EXT2_NEXT_DIRENT(dirent);
563 /* set to jump over the index block */
565 dirent->inode = parent;
567 dirent->name_len = dotdot_de->name_len;
568 dirent->rec_len = fs->blocksize - rec_len;
569 rec_len = EXT2_DIR_REC_LEN(dotdot_de);
570 memcpy(dirent->name, dotdot_de->name, rec_len);
573 root = (struct ext2_dx_root_info *)(buf + offset);
574 root->reserved_zero = 0;
575 root->hash_version = fs->super->s_def_hash_version;
576 root->info_length = sizeof(*root);
577 root->indirect_levels = 0;
578 root->unused_flags = 0;
579 offset += root->info_length;
581 if (ext2fs_has_feature_metadata_csum(fs->super))
582 csum_size = sizeof(struct ext2_dx_tail);
584 limits = (struct ext2_dx_countlimit *) (buf + offset);
585 limits->limit = (fs->blocksize - (offset + csum_size)) /
586 sizeof(struct ext2_dx_entry);
593 static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
595 struct ext2_dir_entry *dir;
596 struct ext2_dx_countlimit *limits;
599 memset(buf, 0, fs->blocksize);
600 dir = (struct ext2_dir_entry *) buf;
602 (void) ext2fs_set_rec_len(fs, fs->blocksize, dir);
604 if (ext2fs_has_feature_metadata_csum(fs->super))
605 csum_size = sizeof(struct ext2_dx_tail);
607 limits = (struct ext2_dx_countlimit *) (buf+8);
608 limits->limit = (fs->blocksize - (8 + csum_size)) /
609 sizeof(struct ext2_dx_entry);
612 return (struct ext2_dx_entry *) limits;
615 static int alloc_blocks(ext2_filsys fs,
616 struct ext2_dx_countlimit **limit,
617 struct ext2_dx_entry **prev_ent,
618 struct ext2_dx_entry **next_ent,
619 int *prev_offset, int *next_offset,
620 struct out_dir *outdir, int i,
621 int *prev_count, int *next_count)
627 (*limit)->limit = (*limit)->count =
628 ext2fs_cpu_to_le16((*limit)->limit);
629 *prev_ent = (struct ext2_dx_entry *) (outdir->buf + *prev_offset);
630 (*prev_ent)->block = ext2fs_cpu_to_le32(outdir->num);
634 ext2fs_cpu_to_le32(outdir->hashes[i]);
636 retval = get_next_block(fs, outdir, &block_start);
640 *next_ent = set_int_node(fs, block_start);
641 *limit = (struct ext2_dx_countlimit *)(*next_ent);
643 *next_offset = ((char *) *next_ent - outdir->buf);
645 *next_count = (*limit)->limit;
646 (*prev_offset) += sizeof(struct ext2_dx_entry);
653 * This function takes the leaf nodes which have been written in
654 * outdir, and populates the root node and any necessary interior nodes.
656 static errcode_t calculate_tree(ext2_filsys fs,
657 struct out_dir *outdir,
660 struct ext2_dir_entry *dot_de,
661 struct ext2_dir_entry *dotdot_de)
663 struct ext2_dx_root_info *root_info;
664 struct ext2_dx_entry *root, *int_ent, *dx_ent = 0;
665 struct ext2_dx_countlimit *root_limit, *int_limit, *limit;
667 int i, c1, c2, c3, nblks;
668 int limit_offset, int_offset, root_offset;
670 root_info = set_root_node(fs, outdir->buf, ino, parent, dot_de,
673 root_offset = limit_offset = ((char *) root_info - outdir->buf) +
674 root_info->info_length;
675 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
676 c1 = root_limit->limit;
679 /* Write out the pointer blocks */
680 if (nblks - 1 <= c1) {
681 /* Just write out the root block, and we're done */
682 root = (struct ext2_dx_entry *) (outdir->buf + root_offset);
683 for (i=1; i < nblks; i++) {
684 root->block = ext2fs_cpu_to_le32(i);
687 ext2fs_cpu_to_le32(outdir->hashes[i]);
691 } else if (nblks - 1 <= ext2fs_htree_intnode_maxrecs(fs, c1)) {
694 root_info->indirect_levels = 1;
695 for (i=1; i < nblks; i++) {
696 if (c2 == 0 && c1 == 0)
699 retval = alloc_blocks(fs, &limit, &root,
700 &dx_ent, &root_offset,
701 NULL, outdir, i, &c1,
706 dx_ent->block = ext2fs_cpu_to_le32(i);
707 if (c2 != limit->limit)
709 ext2fs_cpu_to_le32(outdir->hashes[i]);
713 limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
714 limit->limit = ext2fs_cpu_to_le16(limit->limit);
720 root_info->indirect_levels = 2;
721 for (i = 1; i < nblks; i++) {
722 if (c3 == 0 && c2 == 0 && c1 == 0)
724 if (c3 == 0 && c2 == 0) {
725 retval = alloc_blocks(fs, &int_limit, &root,
726 &int_ent, &root_offset,
727 &int_offset, outdir, i,
733 retval = alloc_blocks(fs, &limit, &int_ent,
734 &dx_ent, &int_offset,
735 NULL, outdir, i, &c2,
741 dx_ent->block = ext2fs_cpu_to_le32(i);
742 if (c3 != limit->limit)
744 ext2fs_cpu_to_le32(outdir->hashes[i]);
748 int_limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
749 int_limit->limit = ext2fs_cpu_to_le16(limit->limit);
751 limit->count = ext2fs_cpu_to_le16(limit->limit - c3);
752 limit->limit = ext2fs_cpu_to_le16(limit->limit);
755 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
756 root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1);
757 root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit);
762 struct write_dir_struct {
763 struct out_dir *outdir;
771 * Helper function which writes out a directory block.
773 static int write_dir_block(ext2_filsys fs,
775 e2_blkcnt_t blockcnt,
776 blk64_t ref_block EXT2FS_ATTR((unused)),
777 int ref_offset EXT2FS_ATTR((unused)),
780 struct write_dir_struct *wd = (struct write_dir_struct *) priv_data;
784 printf("%u: write_dir_block %lld:%lld", wd->ino, blockcnt, *block_nr);
786 if ((*block_nr == 0) || (blockcnt < 0)) {
792 if (blockcnt < wd->outdir->num)
793 dir = wd->outdir->buf + (blockcnt * fs->blocksize);
794 else if (wd->ctx->lost_and_found == wd->dir) {
795 /* Don't release any extra directory blocks for lost+found */
796 wd->err = ext2fs_new_dir_block(fs, 0, 0, &buf);
802 /* Don't free blocks at the end of the directory, they
803 * will be truncated by the caller. */
805 printf(" - not freed\n");
809 wd->err = ext2fs_write_dir_block4(fs, *block_nr, dir, 0, wd->dir);
811 ext2fs_free_mem(&buf);
814 printf(" - write (%d)\n", wd->err);
821 static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
822 struct out_dir *outdir,
823 ext2_ino_t ino, struct ext2_inode *inode,
826 struct write_dir_struct wd;
829 retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num);
839 retval = ext2fs_block_iterate3(fs, ino, 0, NULL,
840 write_dir_block, &wd);
846 e2fsck_read_inode(ctx, ino, inode, "rehash_dir");
848 inode->i_flags &= ~EXT2_INDEX_FL;
850 inode->i_flags |= EXT2_INDEX_FL;
852 printf("%u: set inode size to %u blocks = %u bytes\n",
853 ino, outdir->num, outdir->num * fs->blocksize);
855 retval = ext2fs_inode_size_set(fs, inode, (ext2_off64_t)outdir->num *
860 /* ext2fs_punch() calls ext2fs_write_inode() which writes the size */
861 return ext2fs_punch(fs, ino, inode, NULL, outdir->num, ~0ULL);
864 errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino,
865 struct problem_context *pctx)
867 ext2_filsys fs = ctx->fs;
869 struct ext2_inode inode;
871 struct fill_dir_struct fd = { NULL, NULL, 0, 0, 0, NULL,
873 struct out_dir outdir = { 0, 0, 0, 0 };
875 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
877 if (ext2fs_has_feature_inline_data(fs->super) &&
878 (inode.i_flags & EXT4_INLINE_DATA_FL))
882 dir_buf = malloc(inode.i_size);
886 fd.max_array = inode.i_size / 32;
887 fd.harray = malloc(fd.max_array * sizeof(struct hash_entry));
896 if (!ext2fs_has_feature_dir_index(fs->super) ||
897 (inode.i_size / fs->blocksize) < 2)
902 /* Read in the entire directory into memory */
903 retval = ext2fs_block_iterate3(fs, ino, 0, 0,
904 fill_dir_block, &fd);
911 * If the entries read are less than a block, then don't index
914 if (!fd.compress && (fd.dir_size < (fs->blocksize - 24))) {
922 printf("%d entries (%d bytes) found in inode %d\n",
923 fd.num_array, fd.dir_size, ino);
928 if (fd.compress && fd.num_array > 1)
929 qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry),
932 qsort(fd.harray, fd.num_array, sizeof(struct hash_entry),
936 * Look for duplicates
938 if (duplicate_search_and_fix(ctx, fs, ino, &fd))
941 if (ctx->options & E2F_OPT_NO) {
946 /* Sort non-hashed directories by inode number */
947 if (fd.compress && fd.num_array > 1)
948 qsort(fd.harray+2, fd.num_array-2,
949 sizeof(struct hash_entry), ino_cmp);
952 * Copy the directory entries. In a htree directory these
953 * will become the leaf nodes.
955 retval = copy_dir_entries(ctx, &fd, &outdir);
960 /* Calculate the interior nodes */
961 retval = calculate_tree(fs, &outdir, ino, fd.parent,
962 fd.dot_de, fd.dotdot_de);
967 retval = write_directory(ctx, fs, &outdir, ino, &inode, fd.compress);
971 if (ctx->options & E2F_OPT_CONVERT_BMAP)
972 retval = e2fsck_rebuild_extents_later(ctx, ino);
974 retval = e2fsck_check_rebuild_extents(ctx, ino, &inode, pctx);
979 free_out_dir(&outdir);
983 void e2fsck_rehash_directories(e2fsck_t ctx)
985 struct problem_context pctx;
986 #ifdef RESOURCE_TRACK
987 struct resource_track rtrack;
989 struct dir_info *dir;
990 ext2_u32_iterate iter;
991 struct dir_info_iter * dirinfo_iter = 0;
994 int cur, max, all_dirs, first = 1;
996 init_resource_track(&rtrack, ctx->fs->io);
997 all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS;
999 if (!ctx->dirs_to_hash && !all_dirs)
1002 (void) e2fsck_get_lost_and_found(ctx, 0);
1004 clear_problem_context(&pctx);
1008 dirinfo_iter = e2fsck_dir_info_iter_begin(ctx);
1009 max = e2fsck_get_num_dirinfo(ctx);
1011 retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash,
1014 pctx.errcode = retval;
1015 fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx);
1018 max = ext2fs_u32_list_count(ctx->dirs_to_hash);
1022 if ((dir = e2fsck_dir_info_iter(ctx,
1023 dirinfo_iter)) == 0)
1027 if (!ext2fs_u32_list_iterate(iter, &ino))
1033 fix_problem(ctx, PR_3A_PASS_HEADER, &pctx);
1037 fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx);
1039 pctx.errcode = e2fsck_rehash_dir(ctx, ino, &pctx);
1041 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
1042 fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx);
1044 if (ctx->progress && !ctx->progress_fd)
1045 e2fsck_simple_progress(ctx, "Rebuilding directory",
1046 100.0 * (float) (++cur) / (float) max, ino);
1048 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
1050 e2fsck_dir_info_iter_end(ctx, dirinfo_iter);
1052 ext2fs_u32_list_iterate_end(iter);
1054 if (ctx->dirs_to_hash)
1055 ext2fs_u32_list_free(ctx->dirs_to_hash);
1056 ctx->dirs_to_hash = 0;
1058 print_resource_track(ctx, "Pass 3A", &rtrack, ctx->fs->io);