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