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