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;
65 struct ext2_dir_entry *dot_de;
66 struct ext2_dir_entry *dotdot_de;
71 ext2_dirhash_t minor_hash;
73 struct ext2_dir_entry *dir;
80 ext2_dirhash_t *hashes;
83 static int fill_dir_block(ext2_filsys fs,
86 blk64_t ref_block EXT2FS_ATTR((unused)),
87 int ref_offset EXT2FS_ATTR((unused)),
90 struct fill_dir_struct *fd = (struct fill_dir_struct *) priv_data;
91 struct hash_entry *new_array, *ent;
92 struct ext2_dir_entry *dirent;
94 unsigned int offset, dir_offset, rec_len;
100 offset = blockcnt * fs->blocksize;
101 if (offset + fs->blocksize > fd->inode->i_size) {
102 fd->err = EXT2_ET_DIR_CORRUPTED;
105 dir = (fd->buf+offset);
106 if (HOLE_BLKADDR(*block_nr)) {
107 memset(dir, 0, fs->blocksize);
108 dirent = (struct ext2_dir_entry *) dir;
109 (void) ext2fs_set_rec_len(fs, fs->blocksize, dirent);
111 fd->err = ext2fs_read_dir_block3(fs, *block_nr, dir, 0);
115 hash_alg = fs->super->s_def_hash_version;
116 if ((hash_alg <= EXT2_HASH_TEA) &&
117 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
119 /* While the directory block is "hot", index it. */
121 while (dir_offset < fs->blocksize) {
122 dirent = (struct ext2_dir_entry *) (dir + dir_offset);
123 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
124 if (((dir_offset + rec_len) > fs->blocksize) ||
126 ((rec_len % 4) != 0) ||
127 (((dirent->name_len & 0xFF)+8U) > rec_len)) {
128 fd->err = EXT2_ET_DIR_CORRUPTED;
131 dir_offset += rec_len;
132 if (dirent->inode == 0)
134 if (!fd->compress && ((dirent->name_len & 0xFF) == 1) &&
135 (dirent->name[0] == '.')) {
139 if (!fd->compress && ((dirent->name_len & 0xFF) == 2) &&
140 (dirent->name[0] == '.') && (dirent->name[1] == '.')) {
141 fd->parent = dirent->inode;
142 fd->dotdot_de = dirent;
145 if (fd->num_array >= fd->max_array) {
146 new_array = realloc(fd->harray,
147 sizeof(struct hash_entry) * (fd->max_array+500));
152 fd->harray = new_array;
153 fd->max_array += 500;
155 ent = fd->harray + fd->num_array++;
157 fd->dir_size += EXT2_DIR_REC_LEN(dirent);
158 ent->ino = dirent->inode;
160 ent->hash = ent->minor_hash = 0;
162 fd->err = ext2fs_dirhash(hash_alg, dirent->name,
163 dirent->name_len & 0xFF,
164 fs->super->s_hash_seed,
165 &ent->hash, &ent->minor_hash);
174 /* Used for sorting the hash entry */
175 static EXT2_QSORT_TYPE ino_cmp(const void *a, const void *b)
177 const struct hash_entry *he_a = (const struct hash_entry *) a;
178 const struct hash_entry *he_b = (const struct hash_entry *) b;
180 return (he_a->ino - he_b->ino);
183 /* Used for sorting the hash entry */
184 static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
186 const struct hash_entry *he_a = (const struct hash_entry *) a;
187 const struct hash_entry *he_b = (const struct hash_entry *) b;
191 min_len = he_a->dir->name_len;
192 if (min_len > he_b->dir->name_len)
193 min_len = he_b->dir->name_len;
195 ret = strncmp(he_a->dir->name, he_b->dir->name, min_len);
197 if (he_a->dir->name_len > he_b->dir->name_len)
199 else if (he_a->dir->name_len < he_b->dir->name_len)
202 ret = he_b->dir->inode - he_a->dir->inode;
207 /* Used for sorting the hash entry */
208 static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
210 const struct hash_entry *he_a = (const struct hash_entry *) a;
211 const struct hash_entry *he_b = (const struct hash_entry *) b;
214 if (he_a->hash > he_b->hash)
216 else if (he_a->hash < he_b->hash)
219 if (he_a->minor_hash > he_b->minor_hash)
221 else if (he_a->minor_hash < he_b->minor_hash)
224 ret = name_cmp(a, b);
229 static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,
235 new_mem = realloc(outdir->buf, blocks * fs->blocksize);
238 outdir->buf = new_mem;
239 new_mem = realloc(outdir->hashes,
240 blocks * sizeof(ext2_dirhash_t));
243 outdir->hashes = new_mem;
245 outdir->buf = malloc(blocks * fs->blocksize);
246 outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t));
249 outdir->max = blocks;
253 static void free_out_dir(struct out_dir *outdir)
256 free(outdir->hashes);
261 static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
266 if (outdir->num >= outdir->max) {
267 retval = alloc_size_dir(fs, outdir, outdir->max + 50);
271 *ret = outdir->buf + (outdir->num++ * fs->blocksize);
272 memset(*ret, 0, fs->blocksize);
277 * This function is used to make a unique filename. We do this by
278 * appending ~0, and then incrementing the number. However, we cannot
279 * expand the length of the filename beyond the padding available in
280 * the directory entry.
282 static void mutate_name(char *str, __u16 *len)
285 __u16 l = *len & 0xFF, h = *len & 0xff00;
288 * First check to see if it looks the name has been mutated
291 for (i = l-1; i > 0; i--) {
292 if (!isdigit(str[i]))
295 if ((i == l-1) || (str[i] != '~')) {
305 for (i = l-1; i >= 0; i--) {
306 if (isdigit(str[i])) {
318 else if (str[0] == 'Z') {
336 static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
338 struct fill_dir_struct *fd)
340 struct problem_context pctx;
341 struct hash_entry *ent, *prev;
348 clear_problem_context(&pctx);
351 hash_alg = fs->super->s_def_hash_version;
352 if ((hash_alg <= EXT2_HASH_TEA) &&
353 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
356 for (i=1; i < fd->num_array; i++) {
357 ent = fd->harray + i;
359 if (!ent->dir->inode ||
360 ((ent->dir->name_len & 0xFF) !=
361 (prev->dir->name_len & 0xFF)) ||
362 (strncmp(ent->dir->name, prev->dir->name,
363 ent->dir->name_len & 0xFF)))
365 pctx.dirent = ent->dir;
366 if ((ent->dir->inode == prev->dir->inode) &&
367 fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) {
368 e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
373 memcpy(new_name, ent->dir->name, ent->dir->name_len & 0xFF);
374 new_len = ent->dir->name_len;
375 mutate_name(new_name, &new_len);
376 for (j=0; j < fd->num_array; j++) {
379 (fd->harray[j].dir->name_len & 0xFF)) ||
380 (strncmp(new_name, fd->harray[j].dir->name,
383 mutate_name(new_name, &new_len);
387 new_name[new_len & 0xFF] = 0;
389 if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) {
390 memcpy(ent->dir->name, new_name, new_len & 0xFF);
391 ent->dir->name_len = new_len;
392 ext2fs_dirhash(hash_alg, ent->dir->name,
393 ent->dir->name_len & 0xFF,
394 fs->super->s_hash_seed,
395 &ent->hash, &ent->minor_hash);
403 static errcode_t copy_dir_entries(e2fsck_t ctx,
404 struct fill_dir_struct *fd,
405 struct out_dir *outdir)
407 ext2_filsys fs = ctx->fs;
410 struct hash_entry *ent;
411 struct ext2_dir_entry *dirent;
412 unsigned int rec_len, prev_rec_len, left, slack, offset;
414 ext2_dirhash_t prev_hash;
416 if (ctx->htree_slack_percentage == 255) {
417 profile_get_uint(ctx->profile, "options",
418 "indexed_dir_slack_percentage",
420 &ctx->htree_slack_percentage);
421 if (ctx->htree_slack_percentage > 100)
422 ctx->htree_slack_percentage = 20;
426 retval = alloc_size_dir(fs, outdir,
427 (fd->dir_size / fs->blocksize) + 2);
430 outdir->num = fd->compress ? 0 : 1;
432 outdir->hashes[0] = 0;
434 if ((retval = get_next_block(fs, outdir, &block_start)))
436 dirent = (struct ext2_dir_entry *) block_start;
439 left = fs->blocksize;
440 slack = fd->compress ? 12 :
441 (fs->blocksize * ctx->htree_slack_percentage)/100;
444 for (i = 0; i < fd->num_array; i++) {
445 ent = fd->harray + i;
446 if (ent->dir->inode == 0)
448 rec_len = EXT2_DIR_REC_LEN(ent->dir);
449 if (rec_len > left) {
451 left += prev_rec_len;
452 retval = ext2fs_set_rec_len(fs, left, dirent);
456 if ((retval = get_next_block(fs, outdir,
461 left = fs->blocksize - offset;
462 dirent = (struct ext2_dir_entry *) (block_start + offset);
464 if (ent->hash == prev_hash)
465 outdir->hashes[outdir->num-1] = ent->hash | 1;
467 outdir->hashes[outdir->num-1] = ent->hash;
469 dirent->inode = ent->dir->inode;
470 dirent->name_len = ent->dir->name_len;
471 retval = ext2fs_set_rec_len(fs, rec_len, dirent);
474 prev_rec_len = rec_len;
475 memcpy(dirent->name, ent->dir->name, rec_len);
479 prev_rec_len += left;
480 retval = ext2fs_set_rec_len(fs, prev_rec_len, dirent);
486 prev_hash = ent->hash;
489 retval = ext2fs_set_rec_len(fs, rec_len + left, dirent);
495 static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
496 ext2_ino_t ino, ext2_ino_t parent,
497 struct ext2_dir_entry *dot_de,
498 struct ext2_dir_entry *dotdot_de)
500 struct ext2_dir_entry *dir;
501 struct ext2_dx_root_info *root;
502 struct ext2_dx_countlimit *limits;
506 memset(buf, 0, fs->blocksize);
507 dir = (struct ext2_dir_entry *) buf;
509 dir->name_len = dot_de->name_len;
510 dir->rec_len = dot_de->rec_len;
511 rec_len = EXT2_DIR_REC_LEN(dot_de);
512 memcpy(dir->name, dot_de->name, rec_len);
515 dir = (struct ext2_dir_entry *) (buf + offset);
516 /* set to jump over the index block */
518 dir->name_len = dotdot_de->name_len;
519 dir->rec_len = fs->blocksize - rec_len;
520 rec_len = EXT2_DIR_REC_LEN(dotdot_de);
521 memcpy(dir->name, dotdot_de->name, rec_len);
524 root = (struct ext2_dx_root_info *) (buf + offset);
525 root->reserved_zero = 0;
526 root->hash_version = fs->super->s_def_hash_version;
527 root->info_length = sizeof(struct ext2_dx_root_info);
528 root->indirect_levels = 0;
529 root->unused_flags = 0;
530 offset += sizeof(struct ext2_dx_root_info);
532 limits = (struct ext2_dx_countlimit *) (buf + offset);
533 limits->limit = (fs->blocksize - offset) / sizeof(struct ext2_dx_entry);
540 static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
542 struct ext2_dir_entry *dir;
543 struct ext2_dx_countlimit *limits;
545 memset(buf, 0, fs->blocksize);
546 dir = (struct ext2_dir_entry *) buf;
548 (void) ext2fs_set_rec_len(fs, fs->blocksize, dir);
550 limits = (struct ext2_dx_countlimit *) (buf+8);
551 limits->limit = (fs->blocksize - 8) / sizeof(struct ext2_dx_entry);
554 return (struct ext2_dx_entry *) limits;
558 * This function takes the leaf nodes which have been written in
559 * outdir, and populates the root node and any necessary interior nodes.
561 static errcode_t calculate_tree(ext2_filsys fs,
562 struct out_dir *outdir,
565 struct ext2_dir_entry *dot_de,
566 struct ext2_dir_entry *dotdot_de)
568 struct ext2_dx_root_info *root_info;
569 struct ext2_dx_entry *root, *dx_ent = 0;
570 struct ext2_dx_countlimit *root_limit, *limit;
573 int i, c1, c2, nblks;
574 int limit_offset, root_offset;
576 root_info = set_root_node(fs, outdir->buf, ino, parent, dot_de,
578 root_offset = limit_offset = ((char *) root_info - outdir->buf) +
579 root_info->info_length;
580 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
581 c1 = root_limit->limit;
584 /* Write out the pointer blocks */
586 /* Just write out the root block, and we're done */
587 root = (struct ext2_dx_entry *) (outdir->buf + root_offset);
588 for (i=1; i < nblks; i++) {
589 root->block = ext2fs_cpu_to_le32(i);
592 ext2fs_cpu_to_le32(outdir->hashes[i]);
599 root_info->indirect_levels = 1;
600 for (i=1; i < nblks; i++) {
605 limit->limit = limit->count =
606 ext2fs_cpu_to_le16(limit->limit);
607 root = (struct ext2_dx_entry *)
608 (outdir->buf + root_offset);
609 root->block = ext2fs_cpu_to_le32(outdir->num);
612 ext2fs_cpu_to_le32(outdir->hashes[i]);
613 if ((retval = get_next_block(fs, outdir,
616 dx_ent = set_int_node(fs, block_start);
617 limit = (struct ext2_dx_countlimit *) dx_ent;
619 root_offset += sizeof(struct ext2_dx_entry);
622 dx_ent->block = ext2fs_cpu_to_le32(i);
623 if (c2 != limit->limit)
625 ext2fs_cpu_to_le32(outdir->hashes[i]);
629 limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
630 limit->limit = ext2fs_cpu_to_le16(limit->limit);
632 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
633 root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1);
634 root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit);
639 struct write_dir_struct {
640 struct out_dir *outdir;
647 * Helper function which writes out a directory block.
649 static int write_dir_block(ext2_filsys fs,
651 e2_blkcnt_t blockcnt,
652 blk64_t ref_block EXT2FS_ATTR((unused)),
653 int ref_offset EXT2FS_ATTR((unused)),
656 struct write_dir_struct *wd = (struct write_dir_struct *) priv_data;
662 if (blockcnt >= wd->outdir->num) {
663 e2fsck_read_bitmaps(wd->ctx);
666 * In theory, we only release blocks from the end of the
667 * directory file, so it's fine to clobber a whole cluster at
670 if (blk % EXT2FS_CLUSTER_RATIO(fs) == 0) {
671 ext2fs_block_alloc_stats2(fs, blk, -1);
675 return BLOCK_CHANGED;
680 dir = wd->outdir->buf + (blockcnt * fs->blocksize);
681 wd->err = ext2fs_write_dir_block3(fs, *block_nr, dir, 0);
687 static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
688 struct out_dir *outdir,
689 ext2_ino_t ino, int compress)
691 struct write_dir_struct wd;
693 struct ext2_inode inode;
695 retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num);
704 retval = ext2fs_block_iterate3(fs, ino, 0, 0,
705 write_dir_block, &wd);
711 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
713 inode.i_flags &= ~EXT2_INDEX_FL;
715 inode.i_flags |= EXT2_INDEX_FL;
716 retval = ext2fs_inode_size_set(fs, &inode,
717 outdir->num * fs->blocksize);
720 ext2fs_iblk_sub_blocks(fs, &inode, wd.cleared);
721 e2fsck_write_inode(ctx, ino, &inode, "rehash_dir");
726 errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino)
728 ext2_filsys fs = ctx->fs;
730 struct ext2_inode inode;
732 struct fill_dir_struct fd;
733 struct out_dir outdir;
735 outdir.max = outdir.num = 0;
738 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
742 dir_buf = malloc(inode.i_size);
746 fd.max_array = inode.i_size / 32;
748 fd.harray = malloc(fd.max_array * sizeof(struct hash_entry));
758 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
759 (inode.i_size / fs->blocksize) < 2)
764 /* Read in the entire directory into memory */
765 retval = ext2fs_block_iterate3(fs, ino, 0, 0,
766 fill_dir_block, &fd);
773 * If the entries read are less than a block, then don't index
776 if (!fd.compress && (fd.dir_size < (fs->blocksize - 24))) {
784 printf("%d entries (%d bytes) found in inode %d\n",
785 fd.num_array, fd.dir_size, ino);
790 if (fd.compress && fd.num_array > 1)
791 qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry),
794 qsort(fd.harray, fd.num_array, sizeof(struct hash_entry),
798 * Look for duplicates
800 if (duplicate_search_and_fix(ctx, fs, ino, &fd))
803 if (ctx->options & E2F_OPT_NO) {
808 /* Sort non-hashed directories by inode number */
809 if (fd.compress && fd.num_array > 1)
810 qsort(fd.harray+2, fd.num_array-2,
811 sizeof(struct hash_entry), ino_cmp);
814 * Copy the directory entries. In a htree directory these
815 * will become the leaf nodes.
817 retval = copy_dir_entries(ctx, &fd, &outdir);
822 /* Calculate the interior nodes */
823 retval = calculate_tree(fs, &outdir, ino, fd.parent,
824 fd.dot_de, fd.dotdot_de);
829 retval = write_directory(ctx, fs, &outdir, ino, fd.compress);
837 free_out_dir(&outdir);
841 void e2fsck_rehash_directories(e2fsck_t ctx)
843 struct problem_context pctx;
844 #ifdef RESOURCE_TRACK
845 struct resource_track rtrack;
847 struct dir_info *dir;
848 ext2_u32_iterate iter;
849 struct dir_info_iter * dirinfo_iter = 0;
852 int cur, max, all_dirs, first = 1;
854 init_resource_track(&rtrack, ctx->fs->io);
855 all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS;
857 if (!ctx->dirs_to_hash && !all_dirs)
860 e2fsck_get_lost_and_found(ctx, 0);
862 clear_problem_context(&pctx);
866 dirinfo_iter = e2fsck_dir_info_iter_begin(ctx);
867 max = e2fsck_get_num_dirinfo(ctx);
869 retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash,
872 pctx.errcode = retval;
873 fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx);
876 max = ext2fs_u32_list_count(ctx->dirs_to_hash);
880 if ((dir = e2fsck_dir_info_iter(ctx,
885 if (!ext2fs_u32_list_iterate(iter, &ino))
888 if (ino == ctx->lost_and_found)
892 fix_problem(ctx, PR_3A_PASS_HEADER, &pctx);
896 fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx);
898 pctx.errcode = e2fsck_rehash_dir(ctx, ino);
900 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
901 fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx);
903 if (ctx->progress && !ctx->progress_fd)
904 e2fsck_simple_progress(ctx, "Rebuilding directory",
905 100.0 * (float) (++cur) / (float) max, ino);
907 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
909 e2fsck_dir_info_iter_end(ctx, dirinfo_iter);
911 ext2fs_u32_list_iterate_end(iter);
913 if (ctx->dirs_to_hash)
914 ext2fs_u32_list_free(ctx->dirs_to_hash);
915 ctx->dirs_to_hash = 0;
917 print_resource_track(ctx, "Pass 3A", &rtrack, ctx->fs->io);