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
;
92 ext2_dirhash_t minor_hash
;
94 struct ext2_dir_entry
*dir
;
101 ext2_dirhash_t
*hashes
;
104 static int fill_dir_block(ext2_filsys fs
,
106 e2_blkcnt_t blockcnt
,
107 blk64_t ref_block
EXT2FS_ATTR((unused
)),
108 int ref_offset
EXT2FS_ATTR((unused
)),
111 struct fill_dir_struct
*fd
= (struct fill_dir_struct
*) priv_data
;
112 struct hash_entry
*new_array
, *ent
;
113 struct ext2_dir_entry
*dirent
;
115 unsigned int offset
, dir_offset
, rec_len
, name_len
;
121 offset
= blockcnt
* fs
->blocksize
;
122 if (offset
+ fs
->blocksize
> fd
->inode
->i_size
) {
123 fd
->err
= EXT2_ET_DIR_CORRUPTED
;
127 dir
= (fd
->buf
+offset
);
128 if (*block_nr
== 0) {
129 memset(dir
, 0, fs
->blocksize
);
130 dirent
= (struct ext2_dir_entry
*) dir
;
131 (void) ext2fs_set_rec_len(fs
, fs
->blocksize
, dirent
);
133 int flags
= fs
->flags
;
134 fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
135 fd
->err
= ext2fs_read_dir_block4(fs
, *block_nr
, dir
, 0,
137 fs
->flags
= (flags
& EXT2_FLAG_IGNORE_CSUM_ERRORS
) |
138 (fs
->flags
& ~EXT2_FLAG_IGNORE_CSUM_ERRORS
);
142 hash_alg
= fs
->super
->s_def_hash_version
;
143 if ((hash_alg
<= EXT2_HASH_TEA
) &&
144 (fs
->super
->s_flags
& EXT2_FLAGS_UNSIGNED_HASH
))
146 /* While the directory block is "hot", index it. */
148 while (dir_offset
< fs
->blocksize
) {
149 dirent
= (struct ext2_dir_entry
*) (dir
+ dir_offset
);
150 (void) ext2fs_get_rec_len(fs
, dirent
, &rec_len
);
151 name_len
= ext2fs_dirent_name_len(dirent
);
152 if (((dir_offset
+ rec_len
) > fs
->blocksize
) ||
154 ((rec_len
% 4) != 0) ||
155 (name_len
+ 8 > rec_len
)) {
156 fd
->err
= EXT2_ET_DIR_CORRUPTED
;
159 dir_offset
+= rec_len
;
160 if (dirent
->inode
== 0)
162 if (!fd
->compress
&& (name_len
== 1) &&
163 (dirent
->name
[0] == '.'))
165 if (!fd
->compress
&& (name_len
== 2) &&
166 (dirent
->name
[0] == '.') && (dirent
->name
[1] == '.')) {
167 fd
->parent
= dirent
->inode
;
170 if (fd
->num_array
>= fd
->max_array
) {
171 new_array
= realloc(fd
->harray
,
172 sizeof(struct hash_entry
) * (fd
->max_array
+500));
177 fd
->harray
= new_array
;
178 fd
->max_array
+= 500;
180 ent
= fd
->harray
+ fd
->num_array
++;
182 fd
->dir_size
+= EXT2_DIR_REC_LEN(name_len
);
183 ent
->ino
= dirent
->inode
;
185 ent
->hash
= ent
->minor_hash
= 0;
187 fd
->err
= ext2fs_dirhash(hash_alg
, dirent
->name
,
189 fs
->super
->s_hash_seed
,
190 &ent
->hash
, &ent
->minor_hash
);
199 /* Used for sorting the hash entry */
200 static EXT2_QSORT_TYPE
ino_cmp(const void *a
, const void *b
)
202 const struct hash_entry
*he_a
= (const struct hash_entry
*) a
;
203 const struct hash_entry
*he_b
= (const struct hash_entry
*) b
;
205 return (he_a
->ino
- he_b
->ino
);
208 /* Used for sorting the hash entry */
209 static EXT2_QSORT_TYPE
name_cmp(const void *a
, const void *b
)
211 const struct hash_entry
*he_a
= (const struct hash_entry
*) a
;
212 const struct hash_entry
*he_b
= (const struct hash_entry
*) b
;
213 unsigned int he_a_len
, he_b_len
, min_len
;
216 he_a_len
= ext2fs_dirent_name_len(he_a
->dir
);
217 he_b_len
= ext2fs_dirent_name_len(he_b
->dir
);
219 if (min_len
> he_b_len
)
222 ret
= memcmp(he_a
->dir
->name
, he_b
->dir
->name
, min_len
);
224 if (he_a_len
> he_b_len
)
226 else if (he_a_len
< he_b_len
)
229 ret
= he_b
->dir
->inode
- he_a
->dir
->inode
;
234 /* Used for sorting the hash entry */
235 static EXT2_QSORT_TYPE
hash_cmp(const void *a
, const void *b
)
237 const struct hash_entry
*he_a
= (const struct hash_entry
*) a
;
238 const struct hash_entry
*he_b
= (const struct hash_entry
*) b
;
241 if (he_a
->hash
> he_b
->hash
)
243 else if (he_a
->hash
< he_b
->hash
)
246 if (he_a
->minor_hash
> he_b
->minor_hash
)
248 else if (he_a
->minor_hash
< he_b
->minor_hash
)
251 ret
= name_cmp(a
, b
);
256 static errcode_t
alloc_size_dir(ext2_filsys fs
, struct out_dir
*outdir
,
262 new_mem
= realloc(outdir
->buf
, blocks
* fs
->blocksize
);
265 outdir
->buf
= new_mem
;
266 new_mem
= realloc(outdir
->hashes
,
267 blocks
* sizeof(ext2_dirhash_t
));
270 outdir
->hashes
= new_mem
;
272 outdir
->buf
= malloc(blocks
* fs
->blocksize
);
273 outdir
->hashes
= malloc(blocks
* sizeof(ext2_dirhash_t
));
276 outdir
->max
= blocks
;
280 static void free_out_dir(struct out_dir
*outdir
)
283 free(outdir
->hashes
);
288 static errcode_t
get_next_block(ext2_filsys fs
, struct out_dir
*outdir
,
293 if (outdir
->num
>= outdir
->max
) {
294 retval
= alloc_size_dir(fs
, outdir
, outdir
->max
+ 50);
298 *ret
= outdir
->buf
+ (outdir
->num
++ * fs
->blocksize
);
299 memset(*ret
, 0, fs
->blocksize
);
304 * This function is used to make a unique filename. We do this by
305 * appending ~0, and then incrementing the number. However, we cannot
306 * expand the length of the filename beyond the padding available in
307 * the directory entry.
309 static void mutate_name(char *str
, unsigned int *len
)
312 unsigned int l
= *len
;
315 * First check to see if it looks the name has been mutated
318 for (i
= l
-1; i
> 0; i
--) {
319 if (!isdigit(str
[i
]))
322 if ((i
== (int)l
- 1) || (str
[i
] != '~')) {
332 for (i
= l
-1; i
>= 0; i
--) {
333 if (isdigit(str
[i
])) {
345 else if (str
[0] == 'Z') {
363 static int duplicate_search_and_fix(e2fsck_t ctx
, ext2_filsys fs
,
365 struct fill_dir_struct
*fd
)
367 struct problem_context pctx
;
368 struct hash_entry
*ent
, *prev
;
372 unsigned int new_len
;
375 clear_problem_context(&pctx
);
378 hash_alg
= fs
->super
->s_def_hash_version
;
379 if ((hash_alg
<= EXT2_HASH_TEA
) &&
380 (fs
->super
->s_flags
& EXT2_FLAGS_UNSIGNED_HASH
))
383 for (i
=1; i
< fd
->num_array
; i
++) {
384 ent
= fd
->harray
+ i
;
386 if (!ent
->dir
->inode
||
387 (ext2fs_dirent_name_len(ent
->dir
) !=
388 ext2fs_dirent_name_len(prev
->dir
)) ||
389 memcmp(ent
->dir
->name
, prev
->dir
->name
,
390 ext2fs_dirent_name_len(ent
->dir
)))
392 pctx
.dirent
= ent
->dir
;
393 if ((ent
->dir
->inode
== prev
->dir
->inode
) &&
394 fix_problem(ctx
, PR_2_DUPLICATE_DIRENT
, &pctx
)) {
395 e2fsck_adjust_inode_count(ctx
, ent
->dir
->inode
, -1);
400 new_len
= ext2fs_dirent_name_len(ent
->dir
);
401 memcpy(new_name
, ent
->dir
->name
, new_len
);
402 mutate_name(new_name
, &new_len
);
403 for (j
=0; j
< fd
->num_array
; j
++) {
406 (unsigned) ext2fs_dirent_name_len(fd
->harray
[j
].dir
)) ||
407 memcmp(new_name
, fd
->harray
[j
].dir
->name
, new_len
))
409 mutate_name(new_name
, &new_len
);
413 new_name
[new_len
] = 0;
415 if (fix_problem(ctx
, PR_2_NON_UNIQUE_FILE
, &pctx
)) {
416 memcpy(ent
->dir
->name
, new_name
, new_len
);
417 ext2fs_dirent_set_name_len(ent
->dir
, new_len
);
418 ext2fs_dirhash(hash_alg
, new_name
, new_len
,
419 fs
->super
->s_hash_seed
,
420 &ent
->hash
, &ent
->minor_hash
);
428 static errcode_t
copy_dir_entries(e2fsck_t ctx
,
429 struct fill_dir_struct
*fd
,
430 struct out_dir
*outdir
)
432 ext2_filsys fs
= ctx
->fs
;
435 struct hash_entry
*ent
;
436 struct ext2_dir_entry
*dirent
;
437 unsigned int rec_len
, prev_rec_len
, left
, slack
, offset
;
439 ext2_dirhash_t prev_hash
;
441 struct ext2_dir_entry_tail
*t
;
443 if (ctx
->htree_slack_percentage
== 255) {
444 profile_get_uint(ctx
->profile
, "options",
445 "indexed_dir_slack_percentage",
447 &ctx
->htree_slack_percentage
);
448 if (ctx
->htree_slack_percentage
> 100)
449 ctx
->htree_slack_percentage
= 20;
452 if (ext2fs_has_feature_metadata_csum(fs
->super
))
453 csum_size
= sizeof(struct ext2_dir_entry_tail
);
456 retval
= alloc_size_dir(fs
, outdir
,
457 (fd
->dir_size
/ fs
->blocksize
) + 2);
460 outdir
->num
= fd
->compress
? 0 : 1;
462 outdir
->hashes
[0] = 0;
464 if ((retval
= get_next_block(fs
, outdir
, &block_start
)))
466 dirent
= (struct ext2_dir_entry
*) block_start
;
469 left
= fs
->blocksize
- csum_size
;
470 slack
= fd
->compress
? 12 :
471 ((fs
->blocksize
- csum_size
) * ctx
->htree_slack_percentage
)/100;
474 for (i
= 0; i
< fd
->num_array
; i
++) {
475 ent
= fd
->harray
+ i
;
476 if (ent
->dir
->inode
== 0)
478 rec_len
= EXT2_DIR_REC_LEN(ext2fs_dirent_name_len(ent
->dir
));
479 if (rec_len
> left
) {
481 left
+= prev_rec_len
;
482 retval
= ext2fs_set_rec_len(fs
, left
, dirent
);
487 t
= EXT2_DIRENT_TAIL(block_start
,
489 ext2fs_initialize_dirent_tail(fs
, t
);
491 if ((retval
= get_next_block(fs
, outdir
,
496 left
= (fs
->blocksize
- csum_size
) - offset
;
497 dirent
= (struct ext2_dir_entry
*) (block_start
+ offset
);
499 if (ent
->hash
== prev_hash
)
500 outdir
->hashes
[outdir
->num
-1] = ent
->hash
| 1;
502 outdir
->hashes
[outdir
->num
-1] = ent
->hash
;
504 dirent
->inode
= ent
->dir
->inode
;
505 ext2fs_dirent_set_name_len(dirent
,
506 ext2fs_dirent_name_len(ent
->dir
));
507 ext2fs_dirent_set_file_type(dirent
,
508 ext2fs_dirent_file_type(ent
->dir
));
509 retval
= ext2fs_set_rec_len(fs
, rec_len
, dirent
);
512 prev_rec_len
= rec_len
;
513 memcpy(dirent
->name
, ent
->dir
->name
,
514 ext2fs_dirent_name_len(dirent
));
518 prev_rec_len
+= left
;
519 retval
= ext2fs_set_rec_len(fs
, prev_rec_len
, dirent
);
525 prev_hash
= ent
->hash
;
528 retval
= ext2fs_set_rec_len(fs
, rec_len
+ left
, dirent
);
530 t
= EXT2_DIRENT_TAIL(block_start
, fs
->blocksize
);
531 ext2fs_initialize_dirent_tail(fs
, t
);
538 static struct ext2_dx_root_info
*set_root_node(ext2_filsys fs
, char *buf
,
539 ext2_ino_t ino
, ext2_ino_t parent
)
541 struct ext2_dir_entry
*dir
;
542 struct ext2_dx_root_info
*root
;
543 struct ext2_dx_countlimit
*limits
;
547 if (ext2fs_has_feature_filetype(fs
->super
))
548 filetype
= EXT2_FT_DIR
;
550 memset(buf
, 0, fs
->blocksize
);
551 dir
= (struct ext2_dir_entry
*) buf
;
554 ext2fs_dirent_set_name_len(dir
, 1);
555 ext2fs_dirent_set_file_type(dir
, filetype
);
557 dir
= (struct ext2_dir_entry
*) (buf
+ 12);
561 ext2fs_dirent_set_name_len(dir
, 2);
562 ext2fs_dirent_set_file_type(dir
, filetype
);
563 dir
->rec_len
= fs
->blocksize
- 12;
565 root
= (struct ext2_dx_root_info
*) (buf
+24);
566 root
->reserved_zero
= 0;
567 root
->hash_version
= fs
->super
->s_def_hash_version
;
568 root
->info_length
= 8;
569 root
->indirect_levels
= 0;
570 root
->unused_flags
= 0;
572 if (ext2fs_has_feature_metadata_csum(fs
->super
))
573 csum_size
= sizeof(struct ext2_dx_tail
);
575 limits
= (struct ext2_dx_countlimit
*) (buf
+32);
576 limits
->limit
= (fs
->blocksize
- (32 + csum_size
)) /
577 sizeof(struct ext2_dx_entry
);
584 static struct ext2_dx_entry
*set_int_node(ext2_filsys fs
, char *buf
)
586 struct ext2_dir_entry
*dir
;
587 struct ext2_dx_countlimit
*limits
;
590 memset(buf
, 0, fs
->blocksize
);
591 dir
= (struct ext2_dir_entry
*) buf
;
593 (void) ext2fs_set_rec_len(fs
, fs
->blocksize
, dir
);
595 if (ext2fs_has_feature_metadata_csum(fs
->super
))
596 csum_size
= sizeof(struct ext2_dx_tail
);
598 limits
= (struct ext2_dx_countlimit
*) (buf
+8);
599 limits
->limit
= (fs
->blocksize
- (8 + csum_size
)) /
600 sizeof(struct ext2_dx_entry
);
603 return (struct ext2_dx_entry
*) limits
;
606 static int alloc_blocks(ext2_filsys fs
,
607 struct ext2_dx_countlimit
**limit
,
608 struct ext2_dx_entry
**prev_ent
,
609 struct ext2_dx_entry
**next_ent
,
610 int *prev_offset
, int *next_offset
,
611 struct out_dir
*outdir
, int i
,
612 int *prev_count
, int *next_count
)
618 (*limit
)->limit
= (*limit
)->count
=
619 ext2fs_cpu_to_le16((*limit
)->limit
);
620 *prev_ent
= (struct ext2_dx_entry
*) (outdir
->buf
+ *prev_offset
);
621 (*prev_ent
)->block
= ext2fs_cpu_to_le32(outdir
->num
);
625 ext2fs_cpu_to_le32(outdir
->hashes
[i
]);
627 retval
= get_next_block(fs
, outdir
, &block_start
);
631 *next_ent
= set_int_node(fs
, block_start
);
632 *limit
= (struct ext2_dx_countlimit
*)(*next_ent
);
634 *next_offset
= ((char *) *next_ent
- outdir
->buf
);
636 *next_count
= (*limit
)->limit
;
637 (*prev_offset
) += sizeof(struct ext2_dx_entry
);
644 * This function takes the leaf nodes which have been written in
645 * outdir, and populates the root node and any necessary interior nodes.
647 static errcode_t
calculate_tree(ext2_filsys fs
,
648 struct out_dir
*outdir
,
652 struct ext2_dx_root_info
*root_info
;
653 struct ext2_dx_entry
*root
, *int_ent
, *dx_ent
= 0;
654 struct ext2_dx_countlimit
*root_limit
, *int_limit
, *limit
;
657 int i
, c1
, c2
, c3
, nblks
;
658 int limit_offset
, int_offset
, root_offset
;
660 root_info
= set_root_node(fs
, outdir
->buf
, ino
, parent
);
661 root_offset
= limit_offset
= ((char *) root_info
- outdir
->buf
) +
662 root_info
->info_length
;
663 root_limit
= (struct ext2_dx_countlimit
*) (outdir
->buf
+ limit_offset
);
664 c1
= root_limit
->limit
;
667 /* Write out the pointer blocks */
668 if (nblks
- 1 <= c1
) {
669 /* Just write out the root block, and we're done */
670 root
= (struct ext2_dx_entry
*) (outdir
->buf
+ root_offset
);
671 for (i
=1; i
< nblks
; i
++) {
672 root
->block
= ext2fs_cpu_to_le32(i
);
675 ext2fs_cpu_to_le32(outdir
->hashes
[i
]);
679 } else if (nblks
- 1 <= ext2fs_htree_intnode_maxrecs(fs
, c1
)) {
682 root_info
->indirect_levels
= 1;
683 for (i
=1; i
< nblks
; i
++) {
684 if (c2
== 0 && c1
== 0)
687 retval
= alloc_blocks(fs
, &limit
, &root
,
688 &dx_ent
, &root_offset
,
689 NULL
, outdir
, i
, &c1
,
694 dx_ent
->block
= ext2fs_cpu_to_le32(i
);
695 if (c2
!= limit
->limit
)
697 ext2fs_cpu_to_le32(outdir
->hashes
[i
]);
701 limit
->count
= ext2fs_cpu_to_le16(limit
->limit
- c2
);
702 limit
->limit
= ext2fs_cpu_to_le16(limit
->limit
);
708 root_info
->indirect_levels
= 2;
709 for (i
= 1; i
< nblks
; i
++) {
710 if (c3
== 0 && c2
== 0 && c1
== 0)
712 if (c3
== 0 && c2
== 0) {
713 retval
= alloc_blocks(fs
, &int_limit
, &root
,
714 &int_ent
, &root_offset
,
715 &int_offset
, outdir
, i
,
721 retval
= alloc_blocks(fs
, &limit
, &int_ent
,
722 &dx_ent
, &int_offset
,
723 NULL
, outdir
, i
, &c2
,
729 dx_ent
->block
= ext2fs_cpu_to_le32(i
);
730 if (c3
!= limit
->limit
)
732 ext2fs_cpu_to_le32(outdir
->hashes
[i
]);
736 int_limit
->count
= ext2fs_cpu_to_le16(limit
->limit
- c2
);
737 int_limit
->limit
= ext2fs_cpu_to_le16(limit
->limit
);
739 limit
->count
= ext2fs_cpu_to_le16(limit
->limit
- c3
);
740 limit
->limit
= ext2fs_cpu_to_le16(limit
->limit
);
743 root_limit
= (struct ext2_dx_countlimit
*) (outdir
->buf
+ limit_offset
);
744 root_limit
->count
= ext2fs_cpu_to_le16(root_limit
->limit
- c1
);
745 root_limit
->limit
= ext2fs_cpu_to_le16(root_limit
->limit
);
750 struct write_dir_struct
{
751 struct out_dir
*outdir
;
759 * Helper function which writes out a directory block.
761 static int write_dir_block(ext2_filsys fs
,
763 e2_blkcnt_t blockcnt
,
764 blk64_t ref_block
EXT2FS_ATTR((unused
)),
765 int ref_offset
EXT2FS_ATTR((unused
)),
768 struct write_dir_struct
*wd
= (struct write_dir_struct
*) priv_data
;
772 printf("%u: write_dir_block %lld:%lld", wd
->ino
, blockcnt
, *block_nr
);
774 if ((*block_nr
== 0) || (blockcnt
< 0)) {
780 if (blockcnt
< wd
->outdir
->num
)
781 dir
= wd
->outdir
->buf
+ (blockcnt
* fs
->blocksize
);
782 else if (wd
->ctx
->lost_and_found
== wd
->dir
) {
783 /* Don't release any extra directory blocks for lost+found */
784 wd
->err
= ext2fs_new_dir_block(fs
, 0, 0, &buf
);
790 /* Don't free blocks at the end of the directory, they
791 * will be truncated by the caller. */
793 printf(" - not freed\n");
797 wd
->err
= ext2fs_write_dir_block4(fs
, *block_nr
, dir
, 0, wd
->dir
);
799 ext2fs_free_mem(&buf
);
802 printf(" - write (%d)\n", wd
->err
);
809 static errcode_t
write_directory(e2fsck_t ctx
, ext2_filsys fs
,
810 struct out_dir
*outdir
,
811 ext2_ino_t ino
, struct ext2_inode
*inode
,
814 struct write_dir_struct wd
;
817 retval
= e2fsck_expand_directory(ctx
, ino
, -1, outdir
->num
);
827 retval
= ext2fs_block_iterate3(fs
, ino
, 0, NULL
,
828 write_dir_block
, &wd
);
834 e2fsck_read_inode(ctx
, ino
, inode
, "rehash_dir");
836 inode
->i_flags
&= ~EXT2_INDEX_FL
;
838 inode
->i_flags
|= EXT2_INDEX_FL
;
840 printf("%u: set inode size to %u blocks = %u bytes\n",
841 ino
, outdir
->num
, outdir
->num
* fs
->blocksize
);
843 retval
= ext2fs_inode_size_set(fs
, inode
, (ext2_off64_t
)outdir
->num
*
848 /* ext2fs_punch() calls ext2fs_write_inode() which writes the size */
849 return ext2fs_punch(fs
, ino
, inode
, NULL
, outdir
->num
, ~0ULL);
852 errcode_t
e2fsck_rehash_dir(e2fsck_t ctx
, ext2_ino_t ino
,
853 struct problem_context
*pctx
)
855 ext2_filsys fs
= ctx
->fs
;
857 struct ext2_inode inode
;
859 struct fill_dir_struct fd
= { NULL
, NULL
, 0, 0, 0, NULL
,
861 struct out_dir outdir
= { 0, 0, 0, 0 };
863 e2fsck_read_inode(ctx
, ino
, &inode
, "rehash_dir");
865 if (ext2fs_has_feature_inline_data(fs
->super
) &&
866 (inode
.i_flags
& EXT4_INLINE_DATA_FL
))
870 dir_buf
= malloc(inode
.i_size
);
874 fd
.max_array
= inode
.i_size
/ 32;
875 fd
.harray
= malloc(fd
.max_array
* sizeof(struct hash_entry
));
884 if (!ext2fs_has_feature_dir_index(fs
->super
) ||
885 (inode
.i_size
/ fs
->blocksize
) < 2)
890 /* Read in the entire directory into memory */
891 retval
= ext2fs_block_iterate3(fs
, ino
, 0, 0,
892 fill_dir_block
, &fd
);
899 * If the entries read are less than a block, then don't index
902 if (!fd
.compress
&& (fd
.dir_size
< (fs
->blocksize
- 24))) {
910 printf("%d entries (%d bytes) found in inode %d\n",
911 fd
.num_array
, fd
.dir_size
, ino
);
916 if (fd
.compress
&& fd
.num_array
> 1)
917 qsort(fd
.harray
+2, fd
.num_array
-2, sizeof(struct hash_entry
),
920 qsort(fd
.harray
, fd
.num_array
, sizeof(struct hash_entry
),
924 * Look for duplicates
926 if (duplicate_search_and_fix(ctx
, fs
, ino
, &fd
))
929 if (ctx
->options
& E2F_OPT_NO
) {
934 /* Sort non-hashed directories by inode number */
935 if (fd
.compress
&& fd
.num_array
> 1)
936 qsort(fd
.harray
+2, fd
.num_array
-2,
937 sizeof(struct hash_entry
), ino_cmp
);
940 * Copy the directory entries. In a htree directory these
941 * will become the leaf nodes.
943 retval
= copy_dir_entries(ctx
, &fd
, &outdir
);
947 free(dir_buf
); dir_buf
= 0;
950 /* Calculate the interior nodes */
951 retval
= calculate_tree(fs
, &outdir
, ino
, fd
.parent
);
956 retval
= write_directory(ctx
, fs
, &outdir
, ino
, &inode
, fd
.compress
);
960 if (ctx
->options
& E2F_OPT_CONVERT_BMAP
)
961 retval
= e2fsck_rebuild_extents_later(ctx
, ino
);
963 retval
= e2fsck_check_rebuild_extents(ctx
, ino
, &inode
, pctx
);
968 free_out_dir(&outdir
);
972 void e2fsck_rehash_directories(e2fsck_t ctx
)
974 struct problem_context pctx
;
975 #ifdef RESOURCE_TRACK
976 struct resource_track rtrack
;
978 struct dir_info
*dir
;
979 ext2_u32_iterate iter
;
980 struct dir_info_iter
* dirinfo_iter
= 0;
983 int cur
, max
, all_dirs
, first
= 1;
985 init_resource_track(&rtrack
, ctx
->fs
->io
);
986 all_dirs
= ctx
->options
& E2F_OPT_COMPRESS_DIRS
;
988 if (!ctx
->dirs_to_hash
&& !all_dirs
)
991 (void) e2fsck_get_lost_and_found(ctx
, 0);
993 clear_problem_context(&pctx
);
997 dirinfo_iter
= e2fsck_dir_info_iter_begin(ctx
);
998 max
= e2fsck_get_num_dirinfo(ctx
);
1000 retval
= ext2fs_u32_list_iterate_begin(ctx
->dirs_to_hash
,
1003 pctx
.errcode
= retval
;
1004 fix_problem(ctx
, PR_3A_OPTIMIZE_ITER
, &pctx
);
1007 max
= ext2fs_u32_list_count(ctx
->dirs_to_hash
);
1011 if ((dir
= e2fsck_dir_info_iter(ctx
,
1012 dirinfo_iter
)) == 0)
1016 if (!ext2fs_u32_list_iterate(iter
, &ino
))
1022 fix_problem(ctx
, PR_3A_PASS_HEADER
, &pctx
);
1026 fix_problem(ctx
, PR_3A_OPTIMIZE_DIR
, &pctx
);
1028 pctx
.errcode
= e2fsck_rehash_dir(ctx
, ino
, &pctx
);
1030 end_problem_latch(ctx
, PR_LATCH_OPTIMIZE_DIR
);
1031 fix_problem(ctx
, PR_3A_OPTIMIZE_DIR_ERR
, &pctx
);
1033 if (ctx
->progress
&& !ctx
->progress_fd
)
1034 e2fsck_simple_progress(ctx
, "Rebuilding directory",
1035 100.0 * (float) (++cur
) / (float) max
, ino
);
1037 end_problem_latch(ctx
, PR_LATCH_OPTIMIZE_DIR
);
1039 e2fsck_dir_info_iter_end(ctx
, dirinfo_iter
);
1041 ext2fs_u32_list_iterate_end(iter
);
1043 if (ctx
->dirs_to_hash
)
1044 ext2fs_u32_list_free(ctx
->dirs_to_hash
);
1045 ctx
->dirs_to_hash
= 0;
1047 print_resource_track(ctx
, "Pass 3A", &rtrack
, ctx
->fs
->io
);