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