]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - e2fsck/rehash.c
e2fsck: fix e2fsck -fD directory truncation
[thirdparty/e2fsprogs.git] / e2fsck / rehash.c
1 /*
2 * rehash.c --- rebuild hash tree directories
3 *
4 * Copyright (C) 2002 Theodore Ts'o
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 *
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.
17 *
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
32 * the new tree:
33 *
34 * --------------------------------------------------------
35 * | new tree | | old tree |
36 * --------------------------------------------------------
37 * ^ ptr ^ptr
38 * tail new head old
39 *
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.
46 */
47
48 #include "config.h"
49 #include <string.h>
50 #include <ctype.h>
51 #include <errno.h>
52 #include "e2fsck.h"
53 #include "problem.h"
54
55 #undef REHASH_DEBUG
56
57 struct fill_dir_struct {
58 char *buf;
59 struct ext2_inode *inode;
60 errcode_t err;
61 ext2_ino_t ino;
62 e2fsck_t ctx;
63 struct hash_entry *harray;
64 int max_array, num_array;
65 unsigned int dir_size;
66 int compress;
67 ino_t parent;
68 };
69
70 struct hash_entry {
71 ext2_dirhash_t hash;
72 ext2_dirhash_t minor_hash;
73 ino_t ino;
74 struct ext2_dir_entry *dir;
75 };
76
77 struct out_dir {
78 int num;
79 int max;
80 char *buf;
81 ext2_dirhash_t *hashes;
82 };
83
84 static int fill_dir_block(ext2_filsys fs,
85 blk64_t *block_nr,
86 e2_blkcnt_t blockcnt,
87 blk64_t ref_block EXT2FS_ATTR((unused)),
88 int ref_offset EXT2FS_ATTR((unused)),
89 void *priv_data)
90 {
91 struct fill_dir_struct *fd = (struct fill_dir_struct *) priv_data;
92 struct hash_entry *new_array, *ent;
93 struct ext2_dir_entry *dirent;
94 char *dir;
95 unsigned int offset, dir_offset, rec_len;
96 int hash_alg;
97
98 if (blockcnt < 0)
99 return 0;
100
101 offset = blockcnt * fs->blocksize;
102 if (offset + fs->blocksize > fd->inode->i_size) {
103 fd->err = EXT2_ET_DIR_CORRUPTED;
104 return BLOCK_ABORT;
105 }
106 dir = (fd->buf+offset);
107 if (HOLE_BLKADDR(*block_nr)) {
108 memset(dir, 0, fs->blocksize);
109 dirent = (struct ext2_dir_entry *) dir;
110 (void) ext2fs_set_rec_len(fs, fs->blocksize, dirent);
111 } else {
112 fd->err = ext2fs_read_dir_block3(fs, *block_nr, dir, 0);
113 if (fd->err)
114 return BLOCK_ABORT;
115 }
116 hash_alg = fs->super->s_def_hash_version;
117 if ((hash_alg <= EXT2_HASH_TEA) &&
118 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
119 hash_alg += 3;
120 /* While the directory block is "hot", index it. */
121 dir_offset = 0;
122 while (dir_offset < fs->blocksize) {
123 dirent = (struct ext2_dir_entry *) (dir + dir_offset);
124 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
125 if (((dir_offset + rec_len) > fs->blocksize) ||
126 (rec_len < 8) ||
127 ((rec_len % 4) != 0) ||
128 (((dirent->name_len & 0xFF)+8U) > rec_len)) {
129 fd->err = EXT2_ET_DIR_CORRUPTED;
130 return BLOCK_ABORT;
131 }
132 dir_offset += rec_len;
133 if (dirent->inode == 0)
134 continue;
135 if (!fd->compress && ((dirent->name_len&0xFF) == 1) &&
136 (dirent->name[0] == '.'))
137 continue;
138 if (!fd->compress && ((dirent->name_len&0xFF) == 2) &&
139 (dirent->name[0] == '.') && (dirent->name[1] == '.')) {
140 fd->parent = dirent->inode;
141 continue;
142 }
143 if (fd->num_array >= fd->max_array) {
144 new_array = realloc(fd->harray,
145 sizeof(struct hash_entry) * (fd->max_array+500));
146 if (!new_array) {
147 fd->err = ENOMEM;
148 return BLOCK_ABORT;
149 }
150 fd->harray = new_array;
151 fd->max_array += 500;
152 }
153 ent = fd->harray + fd->num_array++;
154 ent->dir = dirent;
155 fd->dir_size += EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
156 ent->ino = dirent->inode;
157 if (fd->compress)
158 ent->hash = ent->minor_hash = 0;
159 else {
160 fd->err = ext2fs_dirhash(hash_alg, dirent->name,
161 dirent->name_len & 0xFF,
162 fs->super->s_hash_seed,
163 &ent->hash, &ent->minor_hash);
164 if (fd->err)
165 return BLOCK_ABORT;
166 }
167 }
168
169 return 0;
170 }
171
172 /* Used for sorting the hash entry */
173 static EXT2_QSORT_TYPE ino_cmp(const void *a, const void *b)
174 {
175 const struct hash_entry *he_a = (const struct hash_entry *) a;
176 const struct hash_entry *he_b = (const struct hash_entry *) b;
177
178 return (he_a->ino - he_b->ino);
179 }
180
181 /* Used for sorting the hash entry */
182 static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
183 {
184 const struct hash_entry *he_a = (const struct hash_entry *) a;
185 const struct hash_entry *he_b = (const struct hash_entry *) b;
186 int ret;
187 int min_len;
188
189 min_len = he_a->dir->name_len;
190 if (min_len > he_b->dir->name_len)
191 min_len = he_b->dir->name_len;
192
193 ret = strncmp(he_a->dir->name, he_b->dir->name, min_len);
194 if (ret == 0) {
195 if (he_a->dir->name_len > he_b->dir->name_len)
196 ret = 1;
197 else if (he_a->dir->name_len < he_b->dir->name_len)
198 ret = -1;
199 else
200 ret = he_b->dir->inode - he_a->dir->inode;
201 }
202 return ret;
203 }
204
205 /* Used for sorting the hash entry */
206 static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
207 {
208 const struct hash_entry *he_a = (const struct hash_entry *) a;
209 const struct hash_entry *he_b = (const struct hash_entry *) b;
210 int ret;
211
212 if (he_a->hash > he_b->hash)
213 ret = 1;
214 else if (he_a->hash < he_b->hash)
215 ret = -1;
216 else {
217 if (he_a->minor_hash > he_b->minor_hash)
218 ret = 1;
219 else if (he_a->minor_hash < he_b->minor_hash)
220 ret = -1;
221 else
222 ret = name_cmp(a, b);
223 }
224 return ret;
225 }
226
227 static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,
228 int blocks)
229 {
230 void *new_mem;
231
232 if (outdir->max) {
233 new_mem = realloc(outdir->buf, blocks * fs->blocksize);
234 if (!new_mem)
235 return ENOMEM;
236 outdir->buf = new_mem;
237 new_mem = realloc(outdir->hashes,
238 blocks * sizeof(ext2_dirhash_t));
239 if (!new_mem)
240 return ENOMEM;
241 outdir->hashes = new_mem;
242 } else {
243 outdir->buf = malloc(blocks * fs->blocksize);
244 outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t));
245 outdir->num = 0;
246 }
247 outdir->max = blocks;
248 return 0;
249 }
250
251 static void free_out_dir(struct out_dir *outdir)
252 {
253 free(outdir->buf);
254 free(outdir->hashes);
255 outdir->max = 0;
256 outdir->num =0;
257 }
258
259 static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
260 char ** ret)
261 {
262 errcode_t retval;
263
264 if (outdir->num >= outdir->max) {
265 retval = alloc_size_dir(fs, outdir, outdir->max + 50);
266 if (retval)
267 return retval;
268 }
269 *ret = outdir->buf + (outdir->num++ * fs->blocksize);
270 memset(*ret, 0, fs->blocksize);
271 return 0;
272 }
273
274 /*
275 * This function is used to make a unique filename. We do this by
276 * appending ~0, and then incrementing the number. However, we cannot
277 * expand the length of the filename beyond the padding available in
278 * the directory entry.
279 */
280 static void mutate_name(char *str, __u16 *len)
281 {
282 int i;
283 __u16 l = *len & 0xFF, h = *len & 0xff00;
284
285 /*
286 * First check to see if it looks the name has been mutated
287 * already
288 */
289 for (i = l-1; i > 0; i--) {
290 if (!isdigit(str[i]))
291 break;
292 }
293 if ((i == l-1) || (str[i] != '~')) {
294 if (((l-1) & 3) < 2)
295 l += 2;
296 else
297 l = (l+3) & ~3;
298 str[l-2] = '~';
299 str[l-1] = '0';
300 *len = l | h;
301 return;
302 }
303 for (i = l-1; i >= 0; i--) {
304 if (isdigit(str[i])) {
305 if (str[i] == '9')
306 str[i] = '0';
307 else {
308 str[i]++;
309 return;
310 }
311 continue;
312 }
313 if (i == 1) {
314 if (str[0] == 'z')
315 str[0] = 'A';
316 else if (str[0] == 'Z') {
317 str[0] = '~';
318 str[1] = '0';
319 } else
320 str[0]++;
321 } else if (i > 0) {
322 str[i] = '1';
323 str[i-1] = '~';
324 } else {
325 if (str[0] == '~')
326 str[0] = 'a';
327 else
328 str[0]++;
329 }
330 break;
331 }
332 }
333
334 static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
335 ext2_ino_t ino,
336 struct fill_dir_struct *fd)
337 {
338 struct problem_context pctx;
339 struct hash_entry *ent, *prev;
340 int i, j;
341 int fixed = 0;
342 char new_name[256];
343 __u16 new_len;
344 int hash_alg;
345
346 clear_problem_context(&pctx);
347 pctx.ino = ino;
348
349 hash_alg = fs->super->s_def_hash_version;
350 if ((hash_alg <= EXT2_HASH_TEA) &&
351 (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
352 hash_alg += 3;
353
354 for (i=1; i < fd->num_array; i++) {
355 ent = fd->harray + i;
356 prev = ent - 1;
357 if (!ent->dir->inode ||
358 ((ent->dir->name_len & 0xFF) !=
359 (prev->dir->name_len & 0xFF)) ||
360 (strncmp(ent->dir->name, prev->dir->name,
361 ent->dir->name_len & 0xFF)))
362 continue;
363 pctx.dirent = ent->dir;
364 if ((ent->dir->inode == prev->dir->inode) &&
365 fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) {
366 e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
367 ent->dir->inode = 0;
368 fixed++;
369 continue;
370 }
371 memcpy(new_name, ent->dir->name, ent->dir->name_len & 0xFF);
372 new_len = ent->dir->name_len;
373 mutate_name(new_name, &new_len);
374 for (j=0; j < fd->num_array; j++) {
375 if ((i==j) ||
376 ((new_len & 0xFF) !=
377 (fd->harray[j].dir->name_len & 0xFF)) ||
378 (strncmp(new_name, fd->harray[j].dir->name,
379 new_len & 0xFF)))
380 continue;
381 mutate_name(new_name, &new_len);
382
383 j = -1;
384 }
385 new_name[new_len & 0xFF] = 0;
386 pctx.str = new_name;
387 if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) {
388 memcpy(ent->dir->name, new_name, new_len & 0xFF);
389 ent->dir->name_len = new_len;
390 ext2fs_dirhash(hash_alg, ent->dir->name,
391 ent->dir->name_len & 0xFF,
392 fs->super->s_hash_seed,
393 &ent->hash, &ent->minor_hash);
394 fixed++;
395 }
396 }
397 return fixed;
398 }
399
400
401 static errcode_t copy_dir_entries(e2fsck_t ctx,
402 struct fill_dir_struct *fd,
403 struct out_dir *outdir)
404 {
405 ext2_filsys fs = ctx->fs;
406 errcode_t retval;
407 char *block_start;
408 struct hash_entry *ent;
409 struct ext2_dir_entry *dirent;
410 unsigned int rec_len, prev_rec_len, left, slack, offset;
411 int i;
412 ext2_dirhash_t prev_hash;
413
414 if (ctx->htree_slack_percentage == 255) {
415 profile_get_uint(ctx->profile, "options",
416 "indexed_dir_slack_percentage",
417 0, 20,
418 &ctx->htree_slack_percentage);
419 if (ctx->htree_slack_percentage > 100)
420 ctx->htree_slack_percentage = 20;
421 }
422
423 outdir->max = 0;
424 retval = alloc_size_dir(fs, outdir,
425 (fd->dir_size / fs->blocksize) + 2);
426 if (retval)
427 return retval;
428 outdir->num = fd->compress ? 0 : 1;
429 offset = 0;
430 outdir->hashes[0] = 0;
431 prev_hash = 1;
432 if ((retval = get_next_block(fs, outdir, &block_start)))
433 return retval;
434 dirent = (struct ext2_dir_entry *) block_start;
435 prev_rec_len = 0;
436 rec_len = 0;
437 left = fs->blocksize;
438 slack = fd->compress ? 12 :
439 (fs->blocksize * ctx->htree_slack_percentage)/100;
440 if (slack < 12)
441 slack = 12;
442 for (i = 0; i < fd->num_array; i++) {
443 ent = fd->harray + i;
444 if (ent->dir->inode == 0)
445 continue;
446 rec_len = EXT2_DIR_REC_LEN(ent->dir->name_len & 0xFF);
447 if (rec_len > left) {
448 if (left) {
449 left += prev_rec_len;
450 retval = ext2fs_set_rec_len(fs, left, dirent);
451 if (retval)
452 return retval;
453 }
454 if ((retval = get_next_block(fs, outdir,
455 &block_start)))
456 return retval;
457 offset = 0;
458 }
459 left = fs->blocksize - offset;
460 dirent = (struct ext2_dir_entry *) (block_start + offset);
461 if (offset == 0) {
462 if (ent->hash == prev_hash)
463 outdir->hashes[outdir->num-1] = ent->hash | 1;
464 else
465 outdir->hashes[outdir->num-1] = ent->hash;
466 }
467 dirent->inode = ent->dir->inode;
468 dirent->name_len = ent->dir->name_len;
469 retval = ext2fs_set_rec_len(fs, rec_len, dirent);
470 if (retval)
471 return retval;
472 prev_rec_len = rec_len;
473 memcpy(dirent->name, ent->dir->name, dirent->name_len & 0xFF);
474 offset += rec_len;
475 left -= rec_len;
476 if (left < slack) {
477 prev_rec_len += left;
478 retval = ext2fs_set_rec_len(fs, prev_rec_len, dirent);
479 if (retval)
480 return retval;
481 offset += left;
482 left = 0;
483 }
484 prev_hash = ent->hash;
485 }
486 if (left)
487 retval = ext2fs_set_rec_len(fs, rec_len + left, dirent);
488
489 return retval;
490 }
491
492
493 static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
494 ext2_ino_t ino, ext2_ino_t parent)
495 {
496 struct ext2_dir_entry *dir;
497 struct ext2_dx_root_info *root;
498 struct ext2_dx_countlimit *limits;
499 int filetype = 0;
500
501 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
502 filetype = EXT2_FT_DIR << 8;
503
504 memset(buf, 0, fs->blocksize);
505 dir = (struct ext2_dir_entry *) buf;
506 dir->inode = ino;
507 dir->name[0] = '.';
508 dir->name_len = 1 | filetype;
509 dir->rec_len = 12;
510 dir = (struct ext2_dir_entry *) (buf + 12);
511 dir->inode = parent;
512 dir->name[0] = '.';
513 dir->name[1] = '.';
514 dir->name_len = 2 | filetype;
515 dir->rec_len = fs->blocksize - 12;
516
517 root = (struct ext2_dx_root_info *) (buf+24);
518 root->reserved_zero = 0;
519 root->hash_version = fs->super->s_def_hash_version;
520 root->info_length = 8;
521 root->indirect_levels = 0;
522 root->unused_flags = 0;
523
524 limits = (struct ext2_dx_countlimit *) (buf+32);
525 limits->limit = (fs->blocksize - 32) / sizeof(struct ext2_dx_entry);
526 limits->count = 0;
527
528 return root;
529 }
530
531
532 static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
533 {
534 struct ext2_dir_entry *dir;
535 struct ext2_dx_countlimit *limits;
536
537 memset(buf, 0, fs->blocksize);
538 dir = (struct ext2_dir_entry *) buf;
539 dir->inode = 0;
540 (void) ext2fs_set_rec_len(fs, fs->blocksize, dir);
541
542 limits = (struct ext2_dx_countlimit *) (buf+8);
543 limits->limit = (fs->blocksize - 8) / sizeof(struct ext2_dx_entry);
544 limits->count = 0;
545
546 return (struct ext2_dx_entry *) limits;
547 }
548
549 /*
550 * This function takes the leaf nodes which have been written in
551 * outdir, and populates the root node and any necessary interior nodes.
552 */
553 static errcode_t calculate_tree(ext2_filsys fs,
554 struct out_dir *outdir,
555 ext2_ino_t ino,
556 ext2_ino_t parent)
557 {
558 struct ext2_dx_root_info *root_info;
559 struct ext2_dx_entry *root, *dx_ent = 0;
560 struct ext2_dx_countlimit *root_limit, *limit;
561 errcode_t retval;
562 char * block_start;
563 int i, c1, c2, nblks;
564 int limit_offset, root_offset;
565
566 root_info = set_root_node(fs, outdir->buf, ino, parent);
567 root_offset = limit_offset = ((char *) root_info - outdir->buf) +
568 root_info->info_length;
569 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
570 c1 = root_limit->limit;
571 nblks = outdir->num;
572
573 /* Write out the pointer blocks */
574 if (nblks-1 <= c1) {
575 /* Just write out the root block, and we're done */
576 root = (struct ext2_dx_entry *) (outdir->buf + root_offset);
577 for (i=1; i < nblks; i++) {
578 root->block = ext2fs_cpu_to_le32(i);
579 if (i != 1)
580 root->hash =
581 ext2fs_cpu_to_le32(outdir->hashes[i]);
582 root++;
583 c1--;
584 }
585 } else {
586 c2 = 0;
587 limit = 0;
588 root_info->indirect_levels = 1;
589 for (i=1; i < nblks; i++) {
590 if (c1 == 0)
591 return ENOSPC;
592 if (c2 == 0) {
593 if (limit)
594 limit->limit = limit->count =
595 ext2fs_cpu_to_le16(limit->limit);
596 root = (struct ext2_dx_entry *)
597 (outdir->buf + root_offset);
598 root->block = ext2fs_cpu_to_le32(outdir->num);
599 if (i != 1)
600 root->hash =
601 ext2fs_cpu_to_le32(outdir->hashes[i]);
602 if ((retval = get_next_block(fs, outdir,
603 &block_start)))
604 return retval;
605 dx_ent = set_int_node(fs, block_start);
606 limit = (struct ext2_dx_countlimit *) dx_ent;
607 c2 = limit->limit;
608 root_offset += sizeof(struct ext2_dx_entry);
609 c1--;
610 }
611 dx_ent->block = ext2fs_cpu_to_le32(i);
612 if (c2 != limit->limit)
613 dx_ent->hash =
614 ext2fs_cpu_to_le32(outdir->hashes[i]);
615 dx_ent++;
616 c2--;
617 }
618 limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
619 limit->limit = ext2fs_cpu_to_le16(limit->limit);
620 }
621 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
622 root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1);
623 root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit);
624
625 return 0;
626 }
627
628 struct write_dir_struct {
629 struct out_dir *outdir;
630 errcode_t err;
631 ext2_ino_t ino;
632 e2fsck_t ctx;
633 };
634
635 /*
636 * Helper function which writes out a directory block.
637 */
638 static int write_dir_block(ext2_filsys fs,
639 blk64_t *block_nr,
640 e2_blkcnt_t blockcnt,
641 blk64_t ref_block EXT2FS_ATTR((unused)),
642 int ref_offset EXT2FS_ATTR((unused)),
643 void *priv_data)
644 {
645 struct write_dir_struct *wd = (struct write_dir_struct *) priv_data;
646 blk64_t blk;
647 char *dir;
648
649 #ifdef REHASH_DEBUG
650 printf("%u: write_dir_block %lld:%lld", wd->ino, blockcnt, *block_nr);
651 #endif
652 if (*block_nr == 0) {
653 #ifdef REHASH_DEBUG
654 printf(" - skip\n");
655 #endif
656 return 0;
657 }
658 /* Don't free blocks at the end of the directory, they will be
659 * truncated by the caller. */
660 if (blockcnt >= wd->outdir->num) {
661 #ifdef REHASH_DEBUG
662 printf(" - not freed\n");
663 #endif
664 return 0;
665 }
666 if (blockcnt < 0) {
667 #ifdef REHASH_DEBUG
668 printf(" - skip\n");
669 #endif
670 return 0;
671 }
672
673 dir = wd->outdir->buf + (blockcnt * fs->blocksize);
674 wd->err = ext2fs_write_dir_block3(fs, *block_nr, dir, 0);
675 #ifdef REHASH_DEBUG
676 printf(" - write (%d)\n", wd->err);
677 #endif
678 if (wd->err)
679 return BLOCK_ABORT;
680 return 0;
681 }
682
683 static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
684 struct out_dir *outdir,
685 ext2_ino_t ino, int compress)
686 {
687 struct write_dir_struct wd;
688 errcode_t retval;
689 struct ext2_inode inode;
690
691 retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num);
692 if (retval)
693 return retval;
694
695 wd.outdir = outdir;
696 wd.err = 0;
697 wd.ino = ino;
698 wd.ctx = ctx;
699
700 retval = ext2fs_block_iterate3(fs, ino, 0, NULL,
701 write_dir_block, &wd);
702 if (retval)
703 return retval;
704 if (wd.err)
705 return wd.err;
706
707 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
708 if (compress)
709 inode.i_flags &= ~EXT2_INDEX_FL;
710 else
711 inode.i_flags |= EXT2_INDEX_FL;
712 #ifdef REHASH_DEBUG
713 printf("%u: set inode size to %u blocks = %u bytes\n",
714 ino, outdir->num, outdir->num * fs->blocksize);
715 #endif
716 retval = ext2fs_inode_size_set(fs, &inode, (ext2_off64_t)outdir->num *
717 fs->blocksize);
718 if (retval)
719 return retval;
720
721 /* ext2fs_punch() calls ext2fs_write_inode() which writes the size */
722 return ext2fs_punch(fs, ino, &inode, NULL, outdir->num, ~0ULL);
723 }
724
725 errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino)
726 {
727 ext2_filsys fs = ctx->fs;
728 errcode_t retval;
729 struct ext2_inode inode;
730 char *dir_buf = 0;
731 struct fill_dir_struct fd = { NULL };
732 struct out_dir outdir = { 0 };
733
734 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
735
736 retval = ENOMEM;
737 dir_buf = malloc(inode.i_size);
738 if (!dir_buf)
739 goto errout;
740
741 fd.max_array = inode.i_size / 32;
742 fd.harray = malloc(fd.max_array * sizeof(struct hash_entry));
743 if (!fd.harray)
744 goto errout;
745
746 fd.ino = ino;
747 fd.ctx = ctx;
748 fd.buf = dir_buf;
749 fd.inode = &inode;
750 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
751 (inode.i_size / fs->blocksize) < 2)
752 fd.compress = 1;
753 fd.parent = 0;
754
755 retry_nohash:
756 /* Read in the entire directory into memory */
757 retval = ext2fs_block_iterate3(fs, ino, 0, 0,
758 fill_dir_block, &fd);
759 if (fd.err) {
760 retval = fd.err;
761 goto errout;
762 }
763
764 /*
765 * If the entries read are less than a block, then don't index
766 * the directory
767 */
768 if (!fd.compress && (fd.dir_size < (fs->blocksize - 24))) {
769 fd.compress = 1;
770 fd.dir_size = 0;
771 fd.num_array = 0;
772 goto retry_nohash;
773 }
774
775 #if 0
776 printf("%d entries (%d bytes) found in inode %d\n",
777 fd.num_array, fd.dir_size, ino);
778 #endif
779
780 /* Sort the list */
781 resort:
782 if (fd.compress && fd.num_array > 1)
783 qsort(fd.harray+2, fd.num_array-2, sizeof(struct hash_entry),
784 hash_cmp);
785 else
786 qsort(fd.harray, fd.num_array, sizeof(struct hash_entry),
787 hash_cmp);
788
789 /*
790 * Look for duplicates
791 */
792 if (duplicate_search_and_fix(ctx, fs, ino, &fd))
793 goto resort;
794
795 if (ctx->options & E2F_OPT_NO) {
796 retval = 0;
797 goto errout;
798 }
799
800 /* Sort non-hashed directories by inode number */
801 if (fd.compress && fd.num_array > 1)
802 qsort(fd.harray+2, fd.num_array-2,
803 sizeof(struct hash_entry), ino_cmp);
804
805 /*
806 * Copy the directory entries. In a htree directory these
807 * will become the leaf nodes.
808 */
809 retval = copy_dir_entries(ctx, &fd, &outdir);
810 if (retval)
811 goto errout;
812
813 free(dir_buf); dir_buf = 0;
814
815 if (!fd.compress) {
816 /* Calculate the interior nodes */
817 retval = calculate_tree(fs, &outdir, ino, fd.parent);
818 if (retval)
819 goto errout;
820 }
821
822 retval = write_directory(ctx, fs, &outdir, ino, fd.compress);
823 if (retval)
824 goto errout;
825
826 errout:
827 free(dir_buf);
828 free(fd.harray);
829
830 free_out_dir(&outdir);
831 return retval;
832 }
833
834 void e2fsck_rehash_directories(e2fsck_t ctx)
835 {
836 struct problem_context pctx;
837 #ifdef RESOURCE_TRACK
838 struct resource_track rtrack;
839 #endif
840 struct dir_info *dir;
841 ext2_u32_iterate iter;
842 struct dir_info_iter * dirinfo_iter = 0;
843 ext2_ino_t ino;
844 errcode_t retval;
845 int cur, max, all_dirs, first = 1;
846
847 init_resource_track(&rtrack, ctx->fs->io);
848 all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS;
849
850 if (!ctx->dirs_to_hash && !all_dirs)
851 return;
852
853 e2fsck_get_lost_and_found(ctx, 0);
854
855 clear_problem_context(&pctx);
856
857 cur = 0;
858 if (all_dirs) {
859 dirinfo_iter = e2fsck_dir_info_iter_begin(ctx);
860 max = e2fsck_get_num_dirinfo(ctx);
861 } else {
862 retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash,
863 &iter);
864 if (retval) {
865 pctx.errcode = retval;
866 fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx);
867 return;
868 }
869 max = ext2fs_u32_list_count(ctx->dirs_to_hash);
870 }
871 while (1) {
872 if (all_dirs) {
873 if ((dir = e2fsck_dir_info_iter(ctx,
874 dirinfo_iter)) == 0)
875 break;
876 ino = dir->ino;
877 } else {
878 if (!ext2fs_u32_list_iterate(iter, &ino))
879 break;
880 }
881 if (ino == ctx->lost_and_found)
882 continue;
883 pctx.dir = ino;
884 if (first) {
885 fix_problem(ctx, PR_3A_PASS_HEADER, &pctx);
886 first = 0;
887 }
888 #if 0
889 fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx);
890 #endif
891 pctx.errcode = e2fsck_rehash_dir(ctx, ino);
892 if (pctx.errcode) {
893 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
894 fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx);
895 }
896 if (ctx->progress && !ctx->progress_fd)
897 e2fsck_simple_progress(ctx, "Rebuilding directory",
898 100.0 * (float) (++cur) / (float) max, ino);
899 }
900 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
901 if (all_dirs)
902 e2fsck_dir_info_iter_end(ctx, dirinfo_iter);
903 else
904 ext2fs_u32_list_iterate_end(iter);
905
906 if (ctx->dirs_to_hash)
907 ext2fs_u32_list_free(ctx->dirs_to_hash);
908 ctx->dirs_to_hash = 0;
909
910 print_resource_track(ctx, "Pass 3A", &rtrack, ctx->fs->io);
911 }