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 struct fill_dir_struct {
57 struct ext2_inode *inode;
60 struct hash_entry *harray;
61 int max_array, num_array;
62 unsigned int dir_size;
69 ext2_dirhash_t minor_hash;
71 struct ext2_dir_entry *dir;
78 ext2_dirhash_t *hashes;
81 static int fill_dir_block(ext2_filsys fs,
84 blk64_t ref_block EXT2FS_ATTR((unused)),
85 int ref_offset EXT2FS_ATTR((unused)),
88 struct fill_dir_struct *fd = (struct fill_dir_struct *) priv_data;
89 struct hash_entry *new_array, *ent;
90 struct ext2_dir_entry *dirent;
92 unsigned int offset, dir_offset, rec_len;
98 offset = blockcnt * fs->blocksize;
99 if (offset + fs->blocksize > fd->inode->i_size) {
100 fd->err = EXT2_ET_DIR_CORRUPTED;
103 dir = (fd->buf+offset);
104 if (HOLE_BLKADDR(*block_nr)) {
105 memset(dir, 0, fs->blocksize);
106 dirent = (struct ext2_dir_entry *) dir;
107 (void) ext2fs_set_rec_len(fs, fs->blocksize, dirent);
109 fd->err = ext2fs_read_dir_block3(fs, *block_nr, dir, 0);
113 hash_alg = fs->super->s_def_hash_version;
114 if ((hash_alg <= EXT2_HASH_TEA) &&
115 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
117 /* While the directory block is "hot", index it. */
119 while (dir_offset < fs->blocksize) {
120 dirent = (struct ext2_dir_entry *) (dir + dir_offset);
121 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
122 if (((dir_offset + rec_len) > fs->blocksize) ||
124 ((rec_len % 4) != 0) ||
125 (((dirent->name_len & 0xFF)+8U) > rec_len)) {
126 fd->err = EXT2_ET_DIR_CORRUPTED;
129 dir_offset += rec_len;
130 if (dirent->inode == 0)
132 if (!fd->compress && ((dirent->name_len&0xFF) == 1) &&
133 (dirent->name[0] == '.'))
135 if (!fd->compress && ((dirent->name_len&0xFF) == 2) &&
136 (dirent->name[0] == '.') && (dirent->name[1] == '.')) {
137 fd->parent = dirent->inode;
140 if (fd->num_array >= fd->max_array) {
141 new_array = realloc(fd->harray,
142 sizeof(struct hash_entry) * (fd->max_array+500));
147 fd->harray = new_array;
148 fd->max_array += 500;
150 ent = fd->harray + fd->num_array++;
152 fd->dir_size += EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
153 ent->ino = dirent->inode;
155 ent->hash = ent->minor_hash = 0;
157 fd->err = ext2fs_dirhash(hash_alg, dirent->name,
158 dirent->name_len & 0xFF,
159 fs->super->s_hash_seed,
160 &ent->hash, &ent->minor_hash);
169 /* Used for sorting the hash entry */
170 static EXT2_QSORT_TYPE ino_cmp(const void *a, const void *b)
172 const struct hash_entry *he_a = (const struct hash_entry *) a;
173 const struct hash_entry *he_b = (const struct hash_entry *) b;
175 return (he_a->ino - he_b->ino);
178 /* Used for sorting the hash entry */
179 static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
181 const struct hash_entry *he_a = (const struct hash_entry *) a;
182 const struct hash_entry *he_b = (const struct hash_entry *) b;
186 min_len = he_a->dir->name_len;
187 if (min_len > he_b->dir->name_len)
188 min_len = he_b->dir->name_len;
190 ret = strncmp(he_a->dir->name, he_b->dir->name, min_len);
192 if (he_a->dir->name_len > he_b->dir->name_len)
194 else if (he_a->dir->name_len < he_b->dir->name_len)
197 ret = he_b->dir->inode - he_a->dir->inode;
202 /* Used for sorting the hash entry */
203 static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
205 const struct hash_entry *he_a = (const struct hash_entry *) a;
206 const struct hash_entry *he_b = (const struct hash_entry *) b;
209 if (he_a->hash > he_b->hash)
211 else if (he_a->hash < he_b->hash)
214 if (he_a->minor_hash > he_b->minor_hash)
216 else if (he_a->minor_hash < he_b->minor_hash)
219 ret = name_cmp(a, b);
224 static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,
230 new_mem = realloc(outdir->buf, blocks * fs->blocksize);
233 outdir->buf = new_mem;
234 new_mem = realloc(outdir->hashes,
235 blocks * sizeof(ext2_dirhash_t));
238 outdir->hashes = new_mem;
240 outdir->buf = malloc(blocks * fs->blocksize);
241 outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t));
244 outdir->max = blocks;
248 static void free_out_dir(struct out_dir *outdir)
251 free(outdir->hashes);
256 static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
261 if (outdir->num >= outdir->max) {
262 retval = alloc_size_dir(fs, outdir, outdir->max + 50);
266 *ret = outdir->buf + (outdir->num++ * fs->blocksize);
267 memset(*ret, 0, fs->blocksize);
272 * This function is used to make a unique filename. We do this by
273 * appending ~0, and then incrementing the number. However, we cannot
274 * expand the length of the filename beyond the padding available in
275 * the directory entry.
277 static void mutate_name(char *str, __u16 *len)
280 __u16 l = *len & 0xFF, h = *len & 0xff00;
283 * First check to see if it looks the name has been mutated
286 for (i = l-1; i > 0; i--) {
287 if (!isdigit(str[i]))
290 if ((i == l-1) || (str[i] != '~')) {
300 for (i = l-1; i >= 0; i--) {
301 if (isdigit(str[i])) {
313 else if (str[0] == 'Z') {
331 static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
333 struct fill_dir_struct *fd)
335 struct problem_context pctx;
336 struct hash_entry *ent, *prev;
343 clear_problem_context(&pctx);
346 hash_alg = fs->super->s_def_hash_version;
347 if ((hash_alg <= EXT2_HASH_TEA) &&
348 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
351 for (i=1; i < fd->num_array; i++) {
352 ent = fd->harray + i;
354 if (!ent->dir->inode ||
355 ((ent->dir->name_len & 0xFF) !=
356 (prev->dir->name_len & 0xFF)) ||
357 (strncmp(ent->dir->name, prev->dir->name,
358 ent->dir->name_len & 0xFF)))
360 pctx.dirent = ent->dir;
361 if ((ent->dir->inode == prev->dir->inode) &&
362 fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) {
363 e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
368 memcpy(new_name, ent->dir->name, ent->dir->name_len & 0xFF);
369 new_len = ent->dir->name_len;
370 mutate_name(new_name, &new_len);
371 for (j=0; j < fd->num_array; j++) {
374 (fd->harray[j].dir->name_len & 0xFF)) ||
375 (strncmp(new_name, fd->harray[j].dir->name,
378 mutate_name(new_name, &new_len);
382 new_name[new_len & 0xFF] = 0;
384 if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) {
385 memcpy(ent->dir->name, new_name, new_len & 0xFF);
386 ent->dir->name_len = new_len;
387 ext2fs_dirhash(hash_alg, ent->dir->name,
388 ent->dir->name_len & 0xFF,
389 fs->super->s_hash_seed,
390 &ent->hash, &ent->minor_hash);
398 static errcode_t copy_dir_entries(e2fsck_t ctx,
399 struct fill_dir_struct *fd,
400 struct out_dir *outdir)
402 ext2_filsys fs = ctx->fs;
405 struct hash_entry *ent;
406 struct ext2_dir_entry *dirent;
407 unsigned int rec_len, prev_rec_len, left, slack, offset;
409 ext2_dirhash_t prev_hash;
411 if (ctx->htree_slack_percentage == 255) {
412 profile_get_uint(ctx->profile, "options",
413 "indexed_dir_slack_percentage",
415 &ctx->htree_slack_percentage);
416 if (ctx->htree_slack_percentage > 100)
417 ctx->htree_slack_percentage = 20;
421 retval = alloc_size_dir(fs, outdir,
422 (fd->dir_size / fs->blocksize) + 2);
425 outdir->num = fd->compress ? 0 : 1;
427 outdir->hashes[0] = 0;
429 if ((retval = get_next_block(fs, outdir, &block_start)))
431 dirent = (struct ext2_dir_entry *) block_start;
434 left = fs->blocksize;
435 slack = fd->compress ? 12 :
436 (fs->blocksize * ctx->htree_slack_percentage)/100;
439 for (i = 0; i < fd->num_array; i++) {
440 ent = fd->harray + i;
441 if (ent->dir->inode == 0)
443 rec_len = EXT2_DIR_REC_LEN(ent->dir->name_len & 0xFF);
444 if (rec_len > left) {
446 left += prev_rec_len;
447 retval = ext2fs_set_rec_len(fs, left, dirent);
451 if ((retval = get_next_block(fs, outdir,
456 left = fs->blocksize - offset;
457 dirent = (struct ext2_dir_entry *) (block_start + offset);
459 if (ent->hash == prev_hash)
460 outdir->hashes[outdir->num-1] = ent->hash | 1;
462 outdir->hashes[outdir->num-1] = ent->hash;
464 dirent->inode = ent->dir->inode;
465 dirent->name_len = ent->dir->name_len;
466 retval = ext2fs_set_rec_len(fs, rec_len, dirent);
469 prev_rec_len = rec_len;
470 memcpy(dirent->name, ent->dir->name, dirent->name_len & 0xFF);
474 prev_rec_len += left;
475 retval = ext2fs_set_rec_len(fs, prev_rec_len, dirent);
481 prev_hash = ent->hash;
484 retval = ext2fs_set_rec_len(fs, rec_len + left, dirent);
490 static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
491 ext2_ino_t ino, ext2_ino_t parent)
493 struct ext2_dir_entry *dir;
494 struct ext2_dx_root_info *root;
495 struct ext2_dx_countlimit *limits;
498 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
499 filetype = EXT2_FT_DIR << 8;
501 memset(buf, 0, fs->blocksize);
502 dir = (struct ext2_dir_entry *) buf;
505 dir->name_len = 1 | filetype;
507 dir = (struct ext2_dir_entry *) (buf + 12);
511 dir->name_len = 2 | filetype;
512 dir->rec_len = fs->blocksize - 12;
514 root = (struct ext2_dx_root_info *) (buf+24);
515 root->reserved_zero = 0;
516 root->hash_version = fs->super->s_def_hash_version;
517 root->info_length = 8;
518 root->indirect_levels = 0;
519 root->unused_flags = 0;
521 limits = (struct ext2_dx_countlimit *) (buf+32);
522 limits->limit = (fs->blocksize - 32) / sizeof(struct ext2_dx_entry);
529 static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
531 struct ext2_dir_entry *dir;
532 struct ext2_dx_countlimit *limits;
534 memset(buf, 0, fs->blocksize);
535 dir = (struct ext2_dir_entry *) buf;
537 (void) ext2fs_set_rec_len(fs, fs->blocksize, dir);
539 limits = (struct ext2_dx_countlimit *) (buf+8);
540 limits->limit = (fs->blocksize - 8) / sizeof(struct ext2_dx_entry);
543 return (struct ext2_dx_entry *) limits;
547 * This function takes the leaf nodes which have been written in
548 * outdir, and populates the root node and any necessary interior nodes.
550 static errcode_t calculate_tree(ext2_filsys fs,
551 struct out_dir *outdir,
555 struct ext2_dx_root_info *root_info;
556 struct ext2_dx_entry *root, *dx_ent = 0;
557 struct ext2_dx_countlimit *root_limit, *limit;
560 int i, c1, c2, nblks;
561 int limit_offset, root_offset;
563 root_info = set_root_node(fs, outdir->buf, ino, parent);
564 root_offset = limit_offset = ((char *) root_info - outdir->buf) +
565 root_info->info_length;
566 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
567 c1 = root_limit->limit;
570 /* Write out the pointer blocks */
572 /* Just write out the root block, and we're done */
573 root = (struct ext2_dx_entry *) (outdir->buf + root_offset);
574 for (i=1; i < nblks; i++) {
575 root->block = ext2fs_cpu_to_le32(i);
578 ext2fs_cpu_to_le32(outdir->hashes[i]);
585 root_info->indirect_levels = 1;
586 for (i=1; i < nblks; i++) {
591 limit->limit = limit->count =
592 ext2fs_cpu_to_le16(limit->limit);
593 root = (struct ext2_dx_entry *)
594 (outdir->buf + root_offset);
595 root->block = ext2fs_cpu_to_le32(outdir->num);
598 ext2fs_cpu_to_le32(outdir->hashes[i]);
599 if ((retval = get_next_block(fs, outdir,
602 dx_ent = set_int_node(fs, block_start);
603 limit = (struct ext2_dx_countlimit *) dx_ent;
605 root_offset += sizeof(struct ext2_dx_entry);
608 dx_ent->block = ext2fs_cpu_to_le32(i);
609 if (c2 != limit->limit)
611 ext2fs_cpu_to_le32(outdir->hashes[i]);
615 limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
616 limit->limit = ext2fs_cpu_to_le16(limit->limit);
618 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
619 root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1);
620 root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit);
625 struct write_dir_struct {
626 struct out_dir *outdir;
633 * Helper function which writes out a directory block.
635 static int write_dir_block(ext2_filsys fs,
637 e2_blkcnt_t blockcnt,
638 blk64_t ref_block EXT2FS_ATTR((unused)),
639 int ref_offset EXT2FS_ATTR((unused)),
642 struct write_dir_struct *wd = (struct write_dir_struct *) priv_data;
648 if (blockcnt >= wd->outdir->num) {
649 e2fsck_read_bitmaps(wd->ctx);
652 * In theory, we only release blocks from the end of the
653 * directory file, so it's fine to clobber a whole cluster at
656 if (blk % EXT2FS_CLUSTER_RATIO(fs) == 0) {
657 ext2fs_block_alloc_stats2(fs, blk, -1);
661 return BLOCK_CHANGED;
666 dir = wd->outdir->buf + (blockcnt * fs->blocksize);
667 wd->err = ext2fs_write_dir_block3(fs, *block_nr, dir, 0);
673 static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
674 struct out_dir *outdir,
675 ext2_ino_t ino, int compress)
677 struct write_dir_struct wd;
679 struct ext2_inode inode;
681 retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num);
690 retval = ext2fs_block_iterate3(fs, ino, 0, 0,
691 write_dir_block, &wd);
697 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
699 inode.i_flags &= ~EXT2_INDEX_FL;
701 inode.i_flags |= EXT2_INDEX_FL;
702 inode.i_size = outdir->num * fs->blocksize;
703 ext2fs_iblk_sub_blocks(fs, &inode, wd.cleared);
704 e2fsck_write_inode(ctx, ino, &inode, "rehash_dir");
709 errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino)
711 ext2_filsys fs = ctx->fs;
713 struct ext2_inode inode;
715 struct fill_dir_struct fd;
716 struct out_dir outdir;
718 outdir.max = outdir.num = 0;
721 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
725 dir_buf = malloc(inode.i_size);
729 fd.max_array = inode.i_size / 32;
731 fd.harray = malloc(fd.max_array * sizeof(struct hash_entry));
741 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
742 (inode.i_size / fs->blocksize) < 2)
747 /* Read in the entire directory into memory */
748 retval = ext2fs_block_iterate3(fs, ino, 0, 0,
749 fill_dir_block, &fd);
756 * If the entries read are less than a block, then don't index
759 if (!fd.compress && (fd.dir_size < (fs->blocksize - 24))) {
767 printf("%d entries (%d bytes) found in inode %d\n",
768 fd.num_array, fd.dir_size, ino);
774 qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry),
777 qsort(fd.harray, fd.num_array, sizeof(struct hash_entry),
781 * Look for duplicates
783 if (duplicate_search_and_fix(ctx, fs, ino, &fd))
786 if (ctx->options & E2F_OPT_NO) {
791 /* Sort non-hashed directories by inode number */
793 qsort(fd.harray+2, fd.num_array-2,
794 sizeof(struct hash_entry), ino_cmp);
797 * Copy the directory entries. In a htree directory these
798 * will become the leaf nodes.
800 retval = copy_dir_entries(ctx, &fd, &outdir);
804 free(dir_buf); dir_buf = 0;
807 /* Calculate the interior nodes */
808 retval = calculate_tree(fs, &outdir, ino, fd.parent);
813 retval = write_directory(ctx, fs, &outdir, ino, fd.compress);
821 free_out_dir(&outdir);
825 void e2fsck_rehash_directories(e2fsck_t ctx)
827 struct problem_context pctx;
828 #ifdef RESOURCE_TRACK
829 struct resource_track rtrack;
831 struct dir_info *dir;
832 ext2_u32_iterate iter;
833 struct dir_info_iter * dirinfo_iter = 0;
836 int cur, max, all_dirs, first = 1;
838 init_resource_track(&rtrack, ctx->fs->io);
839 all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS;
841 if (!ctx->dirs_to_hash && !all_dirs)
844 e2fsck_get_lost_and_found(ctx, 0);
846 clear_problem_context(&pctx);
850 dirinfo_iter = e2fsck_dir_info_iter_begin(ctx);
851 max = e2fsck_get_num_dirinfo(ctx);
853 retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash,
856 pctx.errcode = retval;
857 fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx);
860 max = ext2fs_u32_list_count(ctx->dirs_to_hash);
864 if ((dir = e2fsck_dir_info_iter(ctx,
869 if (!ext2fs_u32_list_iterate(iter, &ino))
872 if (ino == ctx->lost_and_found)
876 fix_problem(ctx, PR_3A_PASS_HEADER, &pctx);
880 fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx);
882 pctx.errcode = e2fsck_rehash_dir(ctx, ino);
884 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
885 fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx);
887 if (ctx->progress && !ctx->progress_fd)
888 e2fsck_simple_progress(ctx, "Rebuilding directory",
889 100.0 * (float) (++cur) / (float) max, ino);
891 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
893 e2fsck_dir_info_iter_end(ctx, dirinfo_iter);
895 ext2fs_u32_list_iterate_end(iter);
897 if (ctx->dirs_to_hash)
898 ext2fs_u32_list_free(ctx->dirs_to_hash);
899 ctx->dirs_to_hash = 0;
901 print_resource_track(ctx, "Pass 3A", &rtrack, ctx->fs->io);