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.
54 struct fill_dir_struct {
56 struct ext2_inode *inode;
59 struct hash_entry *harray;
60 int max_array, num_array;
61 unsigned int dir_size;
68 ext2_dirhash_t minor_hash;
70 struct ext2_dir_entry *dir;
77 ext2_dirhash_t *hashes;
80 static int fill_dir_block(ext2_filsys fs,
83 blk64_t ref_block EXT2FS_ATTR((unused)),
84 int ref_offset EXT2FS_ATTR((unused)),
87 struct fill_dir_struct *fd = (struct fill_dir_struct *) priv_data;
88 struct hash_entry *new_array, *ent;
89 struct ext2_dir_entry *dirent;
91 unsigned int offset, dir_offset, rec_len;
97 offset = blockcnt * fs->blocksize;
98 if (offset + fs->blocksize > fd->inode->i_size) {
99 fd->err = EXT2_ET_DIR_CORRUPTED;
102 dir = (fd->buf+offset);
103 if (HOLE_BLKADDR(*block_nr)) {
104 memset(dir, 0, fs->blocksize);
105 dirent = (struct ext2_dir_entry *) dir;
106 (void) ext2fs_set_rec_len(fs, fs->blocksize, dirent);
108 fd->err = ext2fs_read_dir_block3(fs, *block_nr, dir, 0);
112 hash_alg = fs->super->s_def_hash_version;
113 if ((hash_alg <= EXT2_HASH_TEA) &&
114 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
116 /* While the directory block is "hot", index it. */
118 while (dir_offset < fs->blocksize) {
119 dirent = (struct ext2_dir_entry *) (dir + dir_offset);
120 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
121 if (((dir_offset + rec_len) > fs->blocksize) ||
123 ((rec_len % 4) != 0) ||
124 (((dirent->name_len & 0xFF)+8U) > rec_len)) {
125 fd->err = EXT2_ET_DIR_CORRUPTED;
128 dir_offset += rec_len;
129 if (dirent->inode == 0)
131 if (!fd->compress && ((dirent->name_len&0xFF) == 1) &&
132 (dirent->name[0] == '.'))
134 if (!fd->compress && ((dirent->name_len&0xFF) == 2) &&
135 (dirent->name[0] == '.') && (dirent->name[1] == '.')) {
136 fd->parent = dirent->inode;
139 if (fd->num_array >= fd->max_array) {
140 new_array = realloc(fd->harray,
141 sizeof(struct hash_entry) * (fd->max_array+500));
146 fd->harray = new_array;
147 fd->max_array += 500;
149 ent = fd->harray + fd->num_array++;
151 fd->dir_size += EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
152 ent->ino = dirent->inode;
154 ent->hash = ent->minor_hash = 0;
156 fd->err = ext2fs_dirhash(hash_alg, dirent->name,
157 dirent->name_len & 0xFF,
158 fs->super->s_hash_seed,
159 &ent->hash, &ent->minor_hash);
168 /* Used for sorting the hash entry */
169 static EXT2_QSORT_TYPE ino_cmp(const void *a, const void *b)
171 const struct hash_entry *he_a = (const struct hash_entry *) a;
172 const struct hash_entry *he_b = (const struct hash_entry *) b;
174 return (he_a->ino - he_b->ino);
177 /* Used for sorting the hash entry */
178 static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
180 const struct hash_entry *he_a = (const struct hash_entry *) a;
181 const struct hash_entry *he_b = (const struct hash_entry *) b;
185 min_len = he_a->dir->name_len;
186 if (min_len > he_b->dir->name_len)
187 min_len = he_b->dir->name_len;
189 ret = strncmp(he_a->dir->name, he_b->dir->name, min_len);
191 if (he_a->dir->name_len > he_b->dir->name_len)
193 else if (he_a->dir->name_len < he_b->dir->name_len)
196 ret = he_b->dir->inode - he_a->dir->inode;
201 /* Used for sorting the hash entry */
202 static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
204 const struct hash_entry *he_a = (const struct hash_entry *) a;
205 const struct hash_entry *he_b = (const struct hash_entry *) b;
208 if (he_a->hash > he_b->hash)
210 else if (he_a->hash < he_b->hash)
213 if (he_a->minor_hash > he_b->minor_hash)
215 else if (he_a->minor_hash < he_b->minor_hash)
218 ret = name_cmp(a, b);
223 static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,
229 new_mem = realloc(outdir->buf, blocks * fs->blocksize);
232 outdir->buf = new_mem;
233 new_mem = realloc(outdir->hashes,
234 blocks * sizeof(ext2_dirhash_t));
237 outdir->hashes = new_mem;
239 outdir->buf = malloc(blocks * fs->blocksize);
240 outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t));
243 outdir->max = blocks;
247 static void free_out_dir(struct out_dir *outdir)
250 free(outdir->hashes);
255 static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
260 if (outdir->num >= outdir->max) {
261 retval = alloc_size_dir(fs, outdir, outdir->max + 50);
265 *ret = outdir->buf + (outdir->num++ * fs->blocksize);
266 memset(*ret, 0, fs->blocksize);
271 * This function is used to make a unique filename. We do this by
272 * appending ~0, and then incrementing the number. However, we cannot
273 * expand the length of the filename beyond the padding available in
274 * the directory entry.
276 static void mutate_name(char *str, __u16 *len)
279 __u16 l = *len & 0xFF, h = *len & 0xff00;
282 * First check to see if it looks the name has been mutated
285 for (i = l-1; i > 0; i--) {
286 if (!isdigit(str[i]))
289 if ((i == l-1) || (str[i] != '~')) {
299 for (i = l-1; i >= 0; i--) {
300 if (isdigit(str[i])) {
312 else if (str[0] == 'Z') {
330 static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
332 struct fill_dir_struct *fd)
334 struct problem_context pctx;
335 struct hash_entry *ent, *prev;
342 clear_problem_context(&pctx);
345 hash_alg = fs->super->s_def_hash_version;
346 if ((hash_alg <= EXT2_HASH_TEA) &&
347 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
350 for (i=1; i < fd->num_array; i++) {
351 ent = fd->harray + i;
353 if (!ent->dir->inode ||
354 ((ent->dir->name_len & 0xFF) !=
355 (prev->dir->name_len & 0xFF)) ||
356 (strncmp(ent->dir->name, prev->dir->name,
357 ent->dir->name_len & 0xFF)))
359 pctx.dirent = ent->dir;
360 if ((ent->dir->inode == prev->dir->inode) &&
361 fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) {
362 e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
367 memcpy(new_name, ent->dir->name, ent->dir->name_len & 0xFF);
368 new_len = ent->dir->name_len;
369 mutate_name(new_name, &new_len);
370 for (j=0; j < fd->num_array; j++) {
373 (fd->harray[j].dir->name_len & 0xFF)) ||
374 (strncmp(new_name, fd->harray[j].dir->name,
377 mutate_name(new_name, &new_len);
381 new_name[new_len & 0xFF] = 0;
383 if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) {
384 memcpy(ent->dir->name, new_name, new_len & 0xFF);
385 ent->dir->name_len = new_len;
386 ext2fs_dirhash(hash_alg, ent->dir->name,
387 ent->dir->name_len & 0xFF,
388 fs->super->s_hash_seed,
389 &ent->hash, &ent->minor_hash);
397 static errcode_t copy_dir_entries(e2fsck_t ctx,
398 struct fill_dir_struct *fd,
399 struct out_dir *outdir)
401 ext2_filsys fs = ctx->fs;
404 struct hash_entry *ent;
405 struct ext2_dir_entry *dirent;
406 unsigned int rec_len, prev_rec_len, left, slack, offset;
408 ext2_dirhash_t prev_hash;
410 if (ctx->htree_slack_percentage == 255) {
411 profile_get_uint(ctx->profile, "options",
412 "indexed_dir_slack_percentage",
414 &ctx->htree_slack_percentage);
415 if (ctx->htree_slack_percentage > 100)
416 ctx->htree_slack_percentage = 20;
420 retval = alloc_size_dir(fs, outdir,
421 (fd->dir_size / fs->blocksize) + 2);
424 outdir->num = fd->compress ? 0 : 1;
426 outdir->hashes[0] = 0;
428 if ((retval = get_next_block(fs, outdir, &block_start)))
430 dirent = (struct ext2_dir_entry *) block_start;
433 left = fs->blocksize;
434 slack = fd->compress ? 12 :
435 (fs->blocksize * ctx->htree_slack_percentage)/100;
438 for (i = 0; i < fd->num_array; i++) {
439 ent = fd->harray + i;
440 if (ent->dir->inode == 0)
442 rec_len = EXT2_DIR_REC_LEN(ent->dir->name_len & 0xFF);
443 if (rec_len > left) {
445 left += prev_rec_len;
446 retval = ext2fs_set_rec_len(fs, left, dirent);
450 if ((retval = get_next_block(fs, outdir,
455 left = fs->blocksize - offset;
456 dirent = (struct ext2_dir_entry *) (block_start + offset);
458 if (ent->hash == prev_hash)
459 outdir->hashes[outdir->num-1] = ent->hash | 1;
461 outdir->hashes[outdir->num-1] = ent->hash;
463 dirent->inode = ent->dir->inode;
464 dirent->name_len = ent->dir->name_len;
465 retval = ext2fs_set_rec_len(fs, rec_len, dirent);
468 prev_rec_len = rec_len;
469 memcpy(dirent->name, ent->dir->name, dirent->name_len & 0xFF);
473 prev_rec_len += left;
474 retval = ext2fs_set_rec_len(fs, prev_rec_len, dirent);
480 prev_hash = ent->hash;
483 retval = ext2fs_set_rec_len(fs, rec_len + left, dirent);
489 static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
490 ext2_ino_t ino, ext2_ino_t parent)
492 struct ext2_dir_entry *dir;
493 struct ext2_dx_root_info *root;
494 struct ext2_dx_countlimit *limits;
497 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
498 filetype = EXT2_FT_DIR << 8;
500 memset(buf, 0, fs->blocksize);
501 dir = (struct ext2_dir_entry *) buf;
504 dir->name_len = 1 | filetype;
506 dir = (struct ext2_dir_entry *) (buf + 12);
510 dir->name_len = 2 | filetype;
511 dir->rec_len = fs->blocksize - 12;
513 root = (struct ext2_dx_root_info *) (buf+24);
514 root->reserved_zero = 0;
515 root->hash_version = fs->super->s_def_hash_version;
516 root->info_length = 8;
517 root->indirect_levels = 0;
518 root->unused_flags = 0;
520 limits = (struct ext2_dx_countlimit *) (buf+32);
521 limits->limit = (fs->blocksize - 32) / sizeof(struct ext2_dx_entry);
528 static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
530 struct ext2_dir_entry *dir;
531 struct ext2_dx_countlimit *limits;
533 memset(buf, 0, fs->blocksize);
534 dir = (struct ext2_dir_entry *) buf;
536 (void) ext2fs_set_rec_len(fs, fs->blocksize, dir);
538 limits = (struct ext2_dx_countlimit *) (buf+8);
539 limits->limit = (fs->blocksize - 8) / sizeof(struct ext2_dx_entry);
542 return (struct ext2_dx_entry *) limits;
546 * This function takes the leaf nodes which have been written in
547 * outdir, and populates the root node and any necessary interior nodes.
549 static errcode_t calculate_tree(ext2_filsys fs,
550 struct out_dir *outdir,
554 struct ext2_dx_root_info *root_info;
555 struct ext2_dx_entry *root, *dx_ent = 0;
556 struct ext2_dx_countlimit *root_limit, *limit;
559 int i, c1, c2, nblks;
560 int limit_offset, root_offset;
562 root_info = set_root_node(fs, outdir->buf, ino, parent);
563 root_offset = limit_offset = ((char *) root_info - outdir->buf) +
564 root_info->info_length;
565 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
566 c1 = root_limit->limit;
569 /* Write out the pointer blocks */
571 /* Just write out the root block, and we're done */
572 root = (struct ext2_dx_entry *) (outdir->buf + root_offset);
573 for (i=1; i < nblks; i++) {
574 root->block = ext2fs_cpu_to_le32(i);
577 ext2fs_cpu_to_le32(outdir->hashes[i]);
584 root_info->indirect_levels = 1;
585 for (i=1; i < nblks; i++) {
590 limit->limit = limit->count =
591 ext2fs_cpu_to_le16(limit->limit);
592 root = (struct ext2_dx_entry *)
593 (outdir->buf + root_offset);
594 root->block = ext2fs_cpu_to_le32(outdir->num);
597 ext2fs_cpu_to_le32(outdir->hashes[i]);
598 if ((retval = get_next_block(fs, outdir,
601 dx_ent = set_int_node(fs, block_start);
602 limit = (struct ext2_dx_countlimit *) dx_ent;
604 root_offset += sizeof(struct ext2_dx_entry);
607 dx_ent->block = ext2fs_cpu_to_le32(i);
608 if (c2 != limit->limit)
610 ext2fs_cpu_to_le32(outdir->hashes[i]);
614 limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
615 limit->limit = ext2fs_cpu_to_le16(limit->limit);
617 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
618 root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1);
619 root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit);
624 struct write_dir_struct {
625 struct out_dir *outdir;
632 * Helper function which writes out a directory block.
634 static int write_dir_block(ext2_filsys fs,
636 e2_blkcnt_t blockcnt,
637 blk64_t ref_block EXT2FS_ATTR((unused)),
638 int ref_offset EXT2FS_ATTR((unused)),
641 struct write_dir_struct *wd = (struct write_dir_struct *) priv_data;
647 if (blockcnt >= wd->outdir->num) {
648 e2fsck_read_bitmaps(wd->ctx);
651 * In theory, we only release blocks from the end of the
652 * directory file, so it's fine to clobber a whole cluster at
655 if (blk % EXT2FS_CLUSTER_RATIO(fs) == 0) {
656 ext2fs_unmark_block_bitmap2(wd->ctx->block_found_map,
658 ext2fs_block_alloc_stats2(fs, blk, -1);
662 return BLOCK_CHANGED;
667 dir = wd->outdir->buf + (blockcnt * fs->blocksize);
668 wd->err = ext2fs_write_dir_block3(fs, *block_nr, dir, 0);
674 static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
675 struct out_dir *outdir,
676 ext2_ino_t ino, int compress)
678 struct write_dir_struct wd;
680 struct ext2_inode inode;
682 retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num);
691 retval = ext2fs_block_iterate3(fs, ino, 0, 0,
692 write_dir_block, &wd);
698 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
700 inode.i_flags &= ~EXT2_INDEX_FL;
702 inode.i_flags |= EXT2_INDEX_FL;
703 inode.i_size = outdir->num * fs->blocksize;
704 ext2fs_iblk_sub_blocks(fs, &inode, wd.cleared);
705 e2fsck_write_inode(ctx, ino, &inode, "rehash_dir");
710 errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino)
712 ext2_filsys fs = ctx->fs;
714 struct ext2_inode inode;
716 struct fill_dir_struct fd;
717 struct out_dir outdir;
719 outdir.max = outdir.num = 0;
722 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
726 dir_buf = malloc(inode.i_size);
730 fd.max_array = inode.i_size / 32;
732 fd.harray = malloc(fd.max_array * sizeof(struct hash_entry));
742 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
743 (inode.i_size / fs->blocksize) < 2)
748 /* Read in the entire directory into memory */
749 retval = ext2fs_block_iterate3(fs, ino, 0, 0,
750 fill_dir_block, &fd);
757 * If the entries read are less than a block, then don't index
760 if (!fd.compress && (fd.dir_size < (fs->blocksize - 24))) {
768 printf("%d entries (%d bytes) found in inode %d\n",
769 fd.num_array, fd.dir_size, ino);
775 qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry),
778 qsort(fd.harray, fd.num_array, sizeof(struct hash_entry),
782 * Look for duplicates
784 if (duplicate_search_and_fix(ctx, fs, ino, &fd))
787 if (ctx->options & E2F_OPT_NO) {
792 /* Sort non-hashed directories by inode number */
794 qsort(fd.harray+2, fd.num_array-2,
795 sizeof(struct hash_entry), ino_cmp);
798 * Copy the directory entries. In a htree directory these
799 * will become the leaf nodes.
801 retval = copy_dir_entries(ctx, &fd, &outdir);
805 free(dir_buf); dir_buf = 0;
808 /* Calculate the interior nodes */
809 retval = calculate_tree(fs, &outdir, ino, fd.parent);
814 retval = write_directory(ctx, fs, &outdir, ino, fd.compress);
822 free_out_dir(&outdir);
826 void e2fsck_rehash_directories(e2fsck_t ctx)
828 struct problem_context pctx;
829 #ifdef RESOURCE_TRACK
830 struct resource_track rtrack;
832 struct dir_info *dir;
833 ext2_u32_iterate iter;
834 struct dir_info_iter * dirinfo_iter = 0;
837 int cur, max, all_dirs, first = 1;
839 init_resource_track(&rtrack, ctx->fs->io);
840 all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS;
842 if (!ctx->dirs_to_hash && !all_dirs)
845 e2fsck_get_lost_and_found(ctx, 0);
847 clear_problem_context(&pctx);
851 dirinfo_iter = e2fsck_dir_info_iter_begin(ctx);
852 max = e2fsck_get_num_dirinfo(ctx);
854 retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash,
857 pctx.errcode = retval;
858 fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx);
861 max = ext2fs_u32_list_count(ctx->dirs_to_hash);
865 if ((dir = e2fsck_dir_info_iter(ctx,
870 if (!ext2fs_u32_list_iterate(iter, &ino))
873 if (ino == ctx->lost_and_found)
877 fix_problem(ctx, PR_3A_PASS_HEADER, &pctx);
881 fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx);
883 pctx.errcode = e2fsck_rehash_dir(ctx, ino);
885 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
886 fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx);
888 if (ctx->progress && !ctx->progress_fd)
889 e2fsck_simple_progress(ctx, "Rebuilding directory",
890 100.0 * (float) (++cur) / (float) max, ino);
892 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
894 e2fsck_dir_info_iter_end(ctx, dirinfo_iter);
896 ext2fs_u32_list_iterate_end(iter);
898 if (ctx->dirs_to_hash)
899 ext2fs_u32_list_free(ctx->dirs_to_hash);
900 ctx->dirs_to_hash = 0;
902 print_resource_track(ctx, "Pass 3A", &rtrack, ctx->fs->io);