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