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;
68 ext2_dirhash_t minor_hash;
69 struct ext2_dir_entry *dir;
76 ext2_dirhash_t *hashes;
79 static int fill_dir_block(ext2_filsys fs,
86 struct fill_dir_struct *fd = (struct fill_dir_struct *) priv_data;
87 struct hash_entry *new_array, *ent;
88 struct ext2_dir_entry *dirent;
90 int offset, dir_offset;
95 offset = blockcnt * fs->blocksize;
96 if (offset + fs->blocksize > fd->inode->i_size) {
97 fd->err = EXT2_ET_DIR_CORRUPTED;
100 dir = (fd->buf+offset);
101 if (HOLE_BLKADDR(*block_nr)) {
102 memset(dir, 0, fs->blocksize);
103 dirent = (struct ext2_dir_entry *) dir;
104 dirent->rec_len = fs->blocksize;
106 fd->err = ext2fs_read_dir_block(fs, *block_nr, dir);
110 /* While the directory block is "hot", index it. */
112 while (dir_offset < fs->blocksize) {
113 dirent = (struct ext2_dir_entry *) (dir + dir_offset);
114 if (((dir_offset + dirent->rec_len) > fs->blocksize) ||
115 (dirent->rec_len < 8) ||
116 ((dirent->rec_len % 4) != 0) ||
117 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
118 fd->err = EXT2_ET_DIR_CORRUPTED;
121 dir_offset += dirent->rec_len;
122 if (dirent->inode == 0)
124 if (!fd->compress && ((dirent->name_len&0xFF) == 1) &&
125 (dirent->name[0] == '.'))
127 if (!fd->compress && ((dirent->name_len&0xFF) == 2) &&
128 (dirent->name[0] == '.') && (dirent->name[1] == '.')) {
129 fd->parent = dirent->inode;
132 if (fd->num_array >= fd->max_array) {
133 new_array = realloc(fd->harray,
134 sizeof(struct hash_entry) * (fd->max_array+500));
139 fd->harray = new_array;
140 fd->max_array += 500;
142 ent = fd->harray + fd->num_array++;
144 fd->dir_size += EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
146 ent->hash = ent->minor_hash = 0;
148 fd->err = ext2fs_dirhash(fs->super->s_def_hash_version,
150 dirent->name_len & 0xFF,
151 fs->super->s_hash_seed,
152 &ent->hash, &ent->minor_hash);
161 /* Used for sorting the hash entry */
162 static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
164 const struct hash_entry *he_a = (const struct hash_entry *) a;
165 const struct hash_entry *he_b = (const struct hash_entry *) b;
169 min_len = he_a->dir->name_len;
170 if (min_len > he_b->dir->name_len)
171 min_len = he_b->dir->name_len;
173 ret = strncmp(he_a->dir->name, he_b->dir->name, min_len);
175 if (he_a->dir->name_len > he_b->dir->name_len)
177 else if (he_a->dir->name_len < he_b->dir->name_len)
180 ret = he_b->dir->inode - he_a->dir->inode;
185 /* Used for sorting the hash entry */
186 static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
188 const struct hash_entry *he_a = (const struct hash_entry *) a;
189 const struct hash_entry *he_b = (const struct hash_entry *) b;
192 if (he_a->hash > he_b->hash)
194 else if (he_a->hash < he_b->hash)
197 if (he_a->minor_hash > he_b->minor_hash)
199 else if (he_a->minor_hash < he_b->minor_hash)
202 ret = name_cmp(a, b);
207 static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,
213 new_mem = realloc(outdir->buf, blocks * fs->blocksize);
216 outdir->buf = new_mem;
217 new_mem = realloc(outdir->hashes,
218 blocks * sizeof(ext2_dirhash_t));
221 outdir->hashes = new_mem;
223 outdir->buf = malloc(blocks * fs->blocksize);
224 outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t));
227 outdir->max = blocks;
231 static void free_out_dir(struct out_dir *outdir)
236 free(outdir->hashes);
241 static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
246 if (outdir->num >= outdir->max) {
247 retval = alloc_size_dir(fs, outdir, outdir->max + 50);
251 *ret = outdir->buf + (outdir->num++ * fs->blocksize);
252 memset(*ret, 0, fs->blocksize);
257 * This function is used to make a unique filename. We do this by
258 * appending ~0, and then incrementing the number. However, we cannot
259 * expand the length of the filename beyond the padding available in
260 * the directory entry.
262 static void mutate_name(char *str, __u16 *len)
265 __u16 l = *len & 0xFF, h = *len & 0xff00;
268 * First check to see if it looks the name has been mutated
271 for (i = l-1; i > 0; i--) {
272 if (!isdigit(str[i]))
275 if ((i == l-1) || (str[i] != '~')) {
285 for (i = l-1; i >= 0; i--) {
286 if (isdigit(str[i])) {
298 else if (str[0] == 'Z') {
316 static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
318 struct fill_dir_struct *fd)
320 struct problem_context pctx;
321 struct hash_entry *ent, *prev;
327 clear_problem_context(&pctx);
330 for (i=1; i < fd->num_array; i++) {
331 ent = fd->harray + i;
333 if (!ent->dir->inode ||
334 ((ent->dir->name_len & 0xFF) !=
335 (prev->dir->name_len & 0xFF)) ||
336 (strncmp(ent->dir->name, prev->dir->name,
337 ent->dir->name_len & 0xFF)))
339 pctx.dirent = ent->dir;
340 if ((ent->dir->inode == prev->dir->inode) &&
341 fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) {
342 e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
347 memcpy(new_name, ent->dir->name, ent->dir->name_len & 0xFF);
348 new_len = ent->dir->name_len;
349 mutate_name(new_name, &new_len);
350 for (j=0; j < fd->num_array; j++) {
352 ((ent->dir->name_len & 0xFF) !=
353 (fd->harray[j].dir->name_len & 0xFF)) ||
354 (strncmp(new_name, fd->harray[j].dir->name,
357 mutate_name(new_name, &new_len);
361 new_name[new_len & 0xFF] = 0;
363 if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) {
364 memcpy(ent->dir->name, new_name, new_len & 0xFF);
365 ent->dir->name_len = new_len;
366 ext2fs_dirhash(fs->super->s_def_hash_version,
368 ent->dir->name_len & 0xFF,
369 fs->super->s_hash_seed,
370 &ent->hash, &ent->minor_hash);
378 static errcode_t copy_dir_entries(ext2_filsys fs,
379 struct fill_dir_struct *fd,
380 struct out_dir *outdir)
384 struct hash_entry *ent;
385 struct ext2_dir_entry *dirent;
386 int i, rec_len, left;
387 ext2_dirhash_t prev_hash;
391 retval = alloc_size_dir(fs, outdir,
392 (fd->dir_size / fs->blocksize) + 2);
395 outdir->num = fd->compress ? 0 : 1;
397 outdir->hashes[0] = 0;
399 if ((retval = get_next_block(fs, outdir, &block_start)))
401 dirent = (struct ext2_dir_entry *) block_start;
402 left = fs->blocksize;
403 for (i=0; i < fd->num_array; i++) {
404 ent = fd->harray + i;
405 if (ent->dir->inode == 0)
407 rec_len = EXT2_DIR_REC_LEN(ent->dir->name_len & 0xFF);
408 if (rec_len > left) {
410 dirent->rec_len += left;
411 if ((retval = get_next_block(fs, outdir,
416 left = fs->blocksize - offset;
417 dirent = (struct ext2_dir_entry *) (block_start + offset);
419 if (ent->hash == prev_hash)
420 outdir->hashes[outdir->num-1] = ent->hash | 1;
422 outdir->hashes[outdir->num-1] = ent->hash;
424 dirent->inode = ent->dir->inode;
425 dirent->name_len = ent->dir->name_len;
426 dirent->rec_len = rec_len;
427 memcpy(dirent->name, ent->dir->name, dirent->name_len & 0xFF);
431 dirent->rec_len += left;
435 prev_hash = ent->hash;
438 dirent->rec_len += left;
444 static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
445 ext2_ino_t ino, ext2_ino_t parent)
447 struct ext2_dir_entry *dir;
448 struct ext2_dx_root_info *root;
449 struct ext2_dx_countlimit *limits;
452 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
453 filetype = EXT2_FT_DIR << 8;
455 memset(buf, 0, fs->blocksize);
456 dir = (struct ext2_dir_entry *) buf;
459 dir->name_len = 1 | filetype;
461 dir = (struct ext2_dir_entry *) (buf + 12);
465 dir->name_len = 2 | filetype;
466 dir->rec_len = fs->blocksize - 12;
468 root = (struct ext2_dx_root_info *) (buf+24);
469 root->reserved_zero = 0;
470 root->hash_version = fs->super->s_def_hash_version;
471 root->info_length = 8;
472 root->indirect_levels = 0;
473 root->unused_flags = 0;
475 limits = (struct ext2_dx_countlimit *) (buf+32);
476 limits->limit = (fs->blocksize - 32) / sizeof(struct ext2_dx_entry);
483 static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
485 struct ext2_dir_entry *dir;
486 struct ext2_dx_countlimit *limits;
488 memset(buf, 0, fs->blocksize);
489 dir = (struct ext2_dir_entry *) buf;
491 dir->rec_len = fs->blocksize;
493 limits = (struct ext2_dx_countlimit *) (buf+8);
494 limits->limit = (fs->blocksize - 8) / sizeof(struct ext2_dx_entry);
497 return (struct ext2_dx_entry *) limits;
501 * This function takes the leaf nodes which have been written in
502 * outdir, and populates the root node and any necessary interior nodes.
504 static errcode_t calculate_tree(ext2_filsys fs,
505 struct out_dir *outdir,
509 struct ext2_dx_root_info *root_info;
510 struct ext2_dx_entry *root, *dx_ent = 0;
511 struct ext2_dx_countlimit *root_limit, *limit;
514 int i, c1, c2, nblks;
515 int limit_offset, root_offset;
517 root_info = set_root_node(fs, outdir->buf, ino, parent);
518 root_offset = limit_offset = ((char *) root_info - outdir->buf) +
519 root_info->info_length;
520 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
521 c1 = root_limit->limit;
524 /* Write out the pointer blocks */
526 /* Just write out the root block, and we're done */
527 root = (struct ext2_dx_entry *) (outdir->buf + root_offset);
528 for (i=1; i < nblks; i++) {
529 root->block = ext2fs_cpu_to_le32(i);
532 ext2fs_cpu_to_le32(outdir->hashes[i]);
539 root_info->indirect_levels = 1;
540 for (i=1; i < nblks; i++) {
545 limit->limit = limit->count =
546 ext2fs_cpu_to_le16(limit->limit);
547 root = (struct ext2_dx_entry *)
548 (outdir->buf + root_offset);
549 root->block = ext2fs_cpu_to_le32(outdir->num);
552 ext2fs_cpu_to_le32(outdir->hashes[i]);
553 if ((retval = get_next_block(fs, outdir,
556 dx_ent = set_int_node(fs, block_start);
557 limit = (struct ext2_dx_countlimit *) dx_ent;
559 root_offset += sizeof(struct ext2_dx_entry);
562 dx_ent->block = ext2fs_cpu_to_le32(i);
563 if (c2 != limit->limit)
565 ext2fs_cpu_to_le32(outdir->hashes[i]);
569 limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
570 limit->limit = ext2fs_cpu_to_le16(limit->limit);
572 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
573 root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1);
574 root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit);
579 struct write_dir_struct {
580 struct out_dir *outdir;
587 * Helper function which writes out a directory block.
589 static int write_dir_block(ext2_filsys fs,
591 e2_blkcnt_t blockcnt,
596 struct write_dir_struct *wd = (struct write_dir_struct *) priv_data;
602 if (blockcnt >= wd->outdir->num) {
603 e2fsck_read_bitmaps(wd->ctx);
605 ext2fs_unmark_block_bitmap(wd->ctx->block_found_map, blk);
606 ext2fs_block_alloc_stats(fs, blk, -1);
609 return BLOCK_CHANGED;
614 dir = wd->outdir->buf + (blockcnt * fs->blocksize);
615 wd->err = ext2fs_write_dir_block(fs, *block_nr, dir);
621 static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
622 struct out_dir *outdir,
623 ext2_ino_t ino, int compress)
625 struct write_dir_struct wd;
627 struct ext2_inode inode;
629 retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num);
638 retval = ext2fs_block_iterate2(fs, ino, 0, 0,
639 write_dir_block, &wd);
645 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
647 inode.i_flags &= ~EXT2_INDEX_FL;
649 inode.i_flags |= EXT2_INDEX_FL;
650 inode.i_size = outdir->num * fs->blocksize;
651 inode.i_blocks -= (fs->blocksize / 512) * wd.cleared;
652 e2fsck_write_inode(ctx, ino, &inode, "rehash_dir");
657 errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino)
659 ext2_filsys fs = ctx->fs;
661 struct ext2_inode inode;
663 struct fill_dir_struct fd;
664 struct out_dir outdir;
666 outdir.max = outdir.num = 0;
669 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
673 dir_buf = malloc(inode.i_size);
677 fd.max_array = inode.i_size / 32;
679 fd.harray = malloc(fd.max_array * sizeof(struct hash_entry));
689 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
690 (inode.i_size / fs->blocksize) < 2)
694 /* Read in the entire directory into memory */
695 retval = ext2fs_block_iterate2(fs, ino, 0, 0,
696 fill_dir_block, &fd);
703 printf("%d entries (%d bytes) found in inode %d\n",
704 fd.num_array, fd.dir_size, ino);
710 qsort(fd.harray+2, fd.num_array-2,
711 sizeof(struct hash_entry), name_cmp);
713 qsort(fd.harray, fd.num_array,
714 sizeof(struct hash_entry), hash_cmp);
717 * Look for duplicates
719 if (duplicate_search_and_fix(ctx, fs, ino, &fd))
722 if (ctx->options & E2F_OPT_NO) {
728 * Copy the directory entries. In a htree directory these
729 * will become the leaf nodes.
731 retval = copy_dir_entries(fs, &fd, &outdir);
735 free(dir_buf); dir_buf = 0;
738 /* Calculate the interior nodes */
739 retval = calculate_tree(fs, &outdir, ino, fd.parent);
744 retval = write_directory(ctx, fs, &outdir, ino, fd.compress);
754 free_out_dir(&outdir);
758 void e2fsck_rehash_directories(e2fsck_t ctx)
760 struct problem_context pctx;
761 #ifdef RESOURCE_TRACK
762 struct resource_track rtrack;
764 struct dir_info *dir;
765 ext2_u32_iterate iter;
768 int i, cur, max, all_dirs, dir_index, first = 1;
770 #ifdef RESOURCE_TRACK
771 init_resource_track(&rtrack);
774 all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS;
776 if (!ctx->dirs_to_hash && !all_dirs)
779 e2fsck_get_lost_and_found(ctx, 0);
781 clear_problem_context(&pctx);
783 dir_index = ctx->fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX;
787 max = e2fsck_get_num_dirinfo(ctx);
789 retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash,
792 pctx.errcode = retval;
793 fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx);
796 max = ext2fs_u32_list_count(ctx->dirs_to_hash);
800 if ((dir = e2fsck_dir_info_iter(ctx, &i)) == 0)
804 if (!ext2fs_u32_list_iterate(iter, &ino))
807 if (ino == ctx->lost_and_found)
811 fix_problem(ctx, PR_3A_PASS_HEADER, &pctx);
815 fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx);
817 pctx.errcode = e2fsck_rehash_dir(ctx, ino);
819 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
820 fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx);
822 if (ctx->progress && !ctx->progress_fd)
823 e2fsck_simple_progress(ctx, "Rebuilding directory",
824 100.0 * (float) (++cur) / (float) max, ino);
826 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
828 ext2fs_u32_list_iterate_end(iter);
830 if (ctx->dirs_to_hash)
831 ext2fs_u32_list_free(ctx->dirs_to_hash);
832 ctx->dirs_to_hash = 0;
834 #ifdef RESOURCE_TRACK
835 if (ctx->options & E2F_OPT_TIME2) {
836 e2fsck_clear_progbar(ctx);
837 print_resource_track("Pass 3A", &rtrack);