blob: d048003c2ffee834fe74bd83fd0742e116e3672f [file] [log] [blame]
Theodore Ts'ob7a00562002-07-20 00:28:07 -04001/*
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 <errno.h>
49#include "e2fsck.h"
50#include "problem.h"
51
52struct fill_dir_struct {
53 char *buf;
54 struct ext2_inode *inode;
55 int err;
56 e2fsck_t ctx;
57 struct hash_entry *harray;
58 int max_array, num_array;
59 int dir_size;
Theodore Ts'o850d05e2002-07-25 00:00:08 -040060 int compress;
Theodore Ts'ob7a00562002-07-20 00:28:07 -040061 ino_t parent;
62};
63
64struct hash_entry {
65 ext2_dirhash_t hash;
66 ext2_dirhash_t minor_hash;
67 struct ext2_dir_entry *dir;
68};
69
70struct out_dir {
71 int num;
72 int max;
73 char *buf;
74 ext2_dirhash_t *hashes;
75};
76
77static int fill_dir_block(ext2_filsys fs,
78 blk_t *block_nr,
79 e2_blkcnt_t blockcnt,
80 blk_t ref_block,
81 int ref_offset,
82 void *priv_data)
83{
84 struct fill_dir_struct *fd = (struct fill_dir_struct *) priv_data;
85 struct hash_entry *new_array, *ent;
86 struct ext2_dir_entry *dirent;
87 char *dir;
88 int offset, dir_offset;
89
90 if (blockcnt < 0)
91 return 0;
92
93 offset = blockcnt * fs->blocksize;
94 if (offset + fs->blocksize > fd->inode->i_size) {
95 fd->err = EXT2_ET_DIR_CORRUPTED;
96 return BLOCK_ABORT;
97 }
98 dir = (fd->buf+offset);
99 if (HOLE_BLKADDR(*block_nr)) {
100 memset(dir, 0, fs->blocksize);
101 dirent = (struct ext2_dir_entry *) dir;
102 dirent->rec_len = fs->blocksize;
103 } else {
104 fd->err = ext2fs_read_dir_block(fs, *block_nr, dir);
105 if (fd->err)
106 return BLOCK_ABORT;
107 }
108 /* While the directory block is "hot", index it. */
109 dir_offset = 0;
110 while (dir_offset < fs->blocksize) {
111 dirent = (struct ext2_dir_entry *) (dir + dir_offset);
112 if (((dir_offset + dirent->rec_len) > fs->blocksize) ||
113 (dirent->rec_len < 8) ||
114 ((dirent->rec_len % 4) != 0) ||
115 (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
116 fd->err = EXT2_ET_DIR_CORRUPTED;
117 return BLOCK_ABORT;
118 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400119 dir_offset += dirent->rec_len;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400120 if (dirent->inode == 0)
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400121 continue;
122 if (!fd->compress && ((dirent->name_len&0xFF) == 1) &&
123 (dirent->name[0] == '.'))
124 continue;
125 if (!fd->compress && ((dirent->name_len&0xFF) == 2) &&
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400126 (dirent->name[0] == '.') && (dirent->name[1] == '.')) {
127 fd->parent = dirent->inode;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400128 continue;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400129 }
130 if (fd->num_array >= fd->max_array) {
131 new_array = realloc(fd->harray,
132 sizeof(struct hash_entry) * (fd->max_array+500));
133 if (!new_array) {
134 fd->err = ENOMEM;
135 return BLOCK_ABORT;
136 }
137 fd->harray = new_array;
138 fd->max_array += 500;
139 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400140 ent = fd->harray + fd->num_array++;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400141 ent->dir = dirent;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400142 fd->dir_size += EXT2_DIR_REC_LEN(dirent->name_len & 0xFF);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400143 if (fd->compress)
144 ent->hash = ent->minor_hash = 0;
145 else {
146 fd->err = ext2fs_dirhash(fs->super->s_def_hash_version,
147 dirent->name,
148 dirent->name_len & 0xFF,
149 fs->super->s_hash_seed,
150 &ent->hash, &ent->minor_hash);
151 if (fd->err)
152 return BLOCK_ABORT;
153 }
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400154 }
155
156 return 0;
157}
158
159/* Used for sorting the hash entry */
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400160static EXT2_QSORT_TYPE name_cmp(const void *a, const void *b)
161{
162 const struct hash_entry *he_a = (const struct hash_entry *) a;
163 const struct hash_entry *he_b = (const struct hash_entry *) b;
164 int ret;
165 int min_len;
166
167 min_len = he_a->dir->name_len;
168 if (min_len > he_b->dir->name_len)
169 min_len = he_b->dir->name_len;
170
171 ret = strncmp(he_a->dir->name, he_b->dir->name, min_len);
172 if (ret == 0) {
173 if (he_a->dir->name_len > he_b->dir->name_len)
174 ret = 1;
175 else if (he_a->dir->name_len < he_b->dir->name_len)
176 ret = -1;
177 else
178 ret = 0;
179 }
180 return ret;
181}
182
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500183/* Used for sorting the hash entry */
184static EXT2_QSORT_TYPE hash_cmp(const void *a, const void *b)
185{
186 const struct hash_entry *he_a = (const struct hash_entry *) a;
187 const struct hash_entry *he_b = (const struct hash_entry *) b;
188 int ret;
189
190 if (he_a->hash > he_b->hash)
191 ret = 1;
192 else if (he_a->hash < he_b->hash)
193 ret = -1;
194 else {
195 if (he_a->minor_hash > he_b->minor_hash)
196 ret = 1;
197 else if (he_a->minor_hash < he_b->minor_hash)
198 ret = -1;
199 else
200 ret = name_cmp(a, b);
201 }
202 return ret;
203}
204
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400205static errcode_t alloc_size_dir(ext2_filsys fs, struct out_dir *outdir,
206 int blocks)
207{
208 void *new_mem;
209
210 if (outdir->max) {
211 new_mem = realloc(outdir->buf, blocks * fs->blocksize);
212 if (!new_mem)
213 return ENOMEM;
214 outdir->buf = new_mem;
215 new_mem = realloc(outdir->hashes,
216 blocks * sizeof(ext2_dirhash_t));
217 if (!new_mem)
218 return ENOMEM;
219 outdir->hashes = new_mem;
220 } else {
221 outdir->buf = malloc(blocks * fs->blocksize);
222 outdir->hashes = malloc(blocks * sizeof(ext2_dirhash_t));
223 outdir->num = 0;
224 }
225 outdir->max = blocks;
226 return 0;
227}
228
229static void free_out_dir(struct out_dir *outdir)
230{
231 free(outdir->buf);
232 free(outdir->hashes);
233 outdir->max = 0;
234 outdir->num =0;
235}
236
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400237static errcode_t get_next_block(ext2_filsys fs, struct out_dir *outdir,
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400238 char ** ret)
239{
240 errcode_t retval;
241
242 if (outdir->num >= outdir->max) {
243 retval = alloc_size_dir(fs, outdir, outdir->max + 50);
244 if (retval)
245 return retval;
246 }
247 *ret = outdir->buf + (outdir->num++ * fs->blocksize);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400248 memset(*ret, 0, fs->blocksize);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400249 return 0;
250}
251
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500252/*
253 * This function is used to make a unique filename. We do this by
254 * appending ~0, and then incrementing the number. However, we cannot
255 * expand the length of the filename beyond the padding available in
256 * the directory entry.
257 */
258static void mutate_name(char *str, __u16 *len)
259{
260 int i;
261 __u16 l = *len & 0xFF, h = *len & 0xff00;
262
263 /*
264 * First check to see if it looks the name has been mutated
265 * already
266 */
267 for (i = l-1; i > 0; i--) {
268 if (!isdigit(str[i]))
269 break;
270 }
271 if ((i == l-1) || (str[i] != '~')) {
272 if (((l-1) & 3) < 2)
273 l += 2;
274 else
275 l = (l+3) & ~3;
276 str[l-2] = '~';
277 str[l-1] = '0';
278 *len = l | h;
279 return;
280 }
281 for (i = l-1; i >= 0; i--) {
282 if (isdigit(str[i])) {
283 if (str[i] == '9')
284 str[i] = '0';
285 else {
286 str[i]++;
287 return;
288 }
289 continue;
290 }
291 if (i == 1) {
292 if (str[0] == 'z')
293 str[0] = 'A';
294 else if (str[0] == 'Z') {
295 str[0] = '~';
296 str[1] = '0';
297 } else
298 str[0]++;
299 } else if (i > 0) {
300 str[i] = '1';
301 str[i-1] = '~';
302 } else {
303 if (str[0] == '~')
304 str[0] = 'a';
305 else
306 str[0]++;
307 }
308 break;
309 }
310}
311
312static int duplicate_search_and_fix(e2fsck_t ctx, ext2_filsys fs,
313 ext2_ino_t ino,
314 struct fill_dir_struct *fd)
315{
316 struct problem_context pctx;
317 struct hash_entry *ent, *prev;
318 int i, j;
319 int fixed = 0;
320 char new_name[256];
321 __u16 new_len;
322
323 clear_problem_context(&pctx);
324 pctx.ino = ino;
325
326 for (i=1; i < fd->num_array; i++) {
327 ent = fd->harray + i;
328 prev = ent - 1;
329 if (!ent->dir->inode ||
330 ((ent->dir->name_len & 0xFF) !=
331 (prev->dir->name_len & 0xFF)) ||
332 (strncmp(ent->dir->name, prev->dir->name,
333 ent->dir->name_len & 0xFF)))
334 continue;
335 pctx.dirent = ent->dir;
336 if ((ent->dir->inode == prev->dir->inode) &&
337 fix_problem(ctx, PR_2_DUPLICATE_DIRENT, &pctx)) {
338 e2fsck_adjust_inode_count(ctx, ent->dir->inode, -1);
339 ent->dir->inode = 0;
340 fixed++;
341 continue;
342 }
343 memcpy(new_name, ent->dir->name, ent->dir->name_len & 0xFF);
344 new_len = ent->dir->name_len;
345 mutate_name(new_name, &new_len);
346 for (j=0; j < fd->num_array; j++) {
347 if ((i==j) ||
348 ((ent->dir->name_len & 0xFF) !=
349 (fd->harray[j].dir->name_len & 0xFF)) ||
350 (strncmp(new_name, fd->harray[j].dir->name,
351 new_len & 0xFF)))
352 continue;
353 mutate_name(new_name, &new_len);
354
355 j = -1;
356 }
357 new_name[new_len & 0xFF] = 0;
358 pctx.str = new_name;
359 if (fix_problem(ctx, PR_2_NON_UNIQUE_FILE, &pctx)) {
360 memcpy(ent->dir->name, new_name, new_len & 0xFF);
361 ent->dir->name_len = new_len;
362 ext2fs_dirhash(fs->super->s_def_hash_version,
363 ent->dir->name,
364 ent->dir->name_len & 0xFF,
365 fs->super->s_hash_seed,
366 &ent->hash, &ent->minor_hash);
367 fixed++;
368 }
369 }
370 return fixed;
371}
372
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400373
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400374static errcode_t copy_dir_entries(ext2_filsys fs,
375 struct fill_dir_struct *fd,
376 struct out_dir *outdir)
377{
378 errcode_t retval;
379 char *block_start;
380 struct hash_entry *ent;
381 struct ext2_dir_entry *dirent;
382 int i, rec_len, left;
383 ext2_dirhash_t prev_hash;
384 int offset;
385
386 outdir->max = 0;
387 retval = alloc_size_dir(fs, outdir,
388 (fd->dir_size / fs->blocksize) + 2);
389 if (retval)
390 return retval;
391 outdir->num = fd->compress ? 0 : 1;
392 offset = 0;
393 outdir->hashes[0] = 0;
394 prev_hash = 1;
395 if ((retval = get_next_block(fs, outdir, &block_start)))
396 return retval;
397 dirent = (struct ext2_dir_entry *) block_start;
398 left = fs->blocksize;
399 for (i=0; i < fd->num_array; i++) {
400 ent = fd->harray + i;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500401 if (ent->dir->inode == 0)
402 continue;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400403 rec_len = EXT2_DIR_REC_LEN(ent->dir->name_len & 0xFF);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400404 if (rec_len > left) {
Theodore Ts'ofe5b72d2002-09-29 19:05:26 -0400405 if (left)
406 dirent->rec_len += left;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400407 if ((retval = get_next_block(fs, outdir,
408 &block_start)))
409 return retval;
Theodore Ts'ofe5b72d2002-09-29 19:05:26 -0400410 offset = 0;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400411 }
Theodore Ts'ofe5b72d2002-09-29 19:05:26 -0400412 left = fs->blocksize - offset;
413 dirent = (struct ext2_dir_entry *) (block_start + offset);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400414 if (offset == 0) {
415 if (ent->hash == prev_hash)
416 outdir->hashes[outdir->num-1] = ent->hash | 1;
417 else
418 outdir->hashes[outdir->num-1] = ent->hash;
419 }
420 dirent->inode = ent->dir->inode;
421 dirent->name_len = ent->dir->name_len;
422 dirent->rec_len = rec_len;
423 memcpy(dirent->name, ent->dir->name, dirent->name_len & 0xFF);
424 offset += rec_len;
425 left -= rec_len;
426 if (left < 12) {
427 dirent->rec_len += left;
428 offset += left;
Theodore Ts'ocf3909e2002-09-06 10:14:12 -0400429 left = 0;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400430 }
431 prev_hash = ent->hash;
432 }
433 if (left)
434 dirent->rec_len += left;
435
436 return 0;
437}
438
439
440static struct ext2_dx_root_info *set_root_node(ext2_filsys fs, char *buf,
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400441 ext2_ino_t ino, ext2_ino_t parent)
442{
443 struct ext2_dir_entry *dir;
444 struct ext2_dx_root_info *root;
445 struct ext2_dx_countlimit *limits;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400446 int filetype = 0;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400447
448 if (fs->super->s_feature_incompat & EXT2_FEATURE_INCOMPAT_FILETYPE)
449 filetype = EXT2_FT_DIR << 8;
450
451 memset(buf, 0, fs->blocksize);
452 dir = (struct ext2_dir_entry *) buf;
453 dir->inode = ino;
454 dir->name[0] = '.';
455 dir->name_len = 1 | filetype;
456 dir->rec_len = 12;
457 dir = (struct ext2_dir_entry *) (buf + 12);
458 dir->inode = parent;
459 dir->name[0] = '.';
460 dir->name[1] = '.';
461 dir->name_len = 2 | filetype;
462 dir->rec_len = fs->blocksize - 12;
463
464 root = (struct ext2_dx_root_info *) (buf+24);
465 root->reserved_zero = 0;
466 root->hash_version = fs->super->s_def_hash_version;
467 root->info_length = 8;
468 root->indirect_levels = 0;
469 root->unused_flags = 0;
470
471 limits = (struct ext2_dx_countlimit *) (buf+32);
472 limits->limit = (fs->blocksize - 32) / sizeof(struct ext2_dx_entry);
473 limits->count = 0;
474
475 return root;
476}
477
478
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400479static struct ext2_dx_entry *set_int_node(ext2_filsys fs, char *buf)
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400480{
481 struct ext2_dir_entry *dir;
482 struct ext2_dx_countlimit *limits;
483
484 memset(buf, 0, fs->blocksize);
485 dir = (struct ext2_dir_entry *) buf;
486 dir->inode = 0;
487 dir->rec_len = fs->blocksize;
488
489 limits = (struct ext2_dx_countlimit *) (buf+8);
490 limits->limit = (fs->blocksize - 8) / sizeof(struct ext2_dx_entry);
491 limits->count = 0;
492
493 return (struct ext2_dx_entry *) limits;
494}
495
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400496/*
497 * This function takes the leaf nodes which have been written in
498 * outdir, and populates the root node and any necessary interior nodes.
499 */
500static errcode_t calculate_tree(ext2_filsys fs,
501 struct out_dir *outdir,
502 ext2_ino_t ino,
503 ext2_ino_t parent)
504{
505 struct ext2_dx_root_info *root_info;
506 struct ext2_dx_entry *root, *dx_ent = 0;
507 struct ext2_dx_countlimit *root_limit, *limit;
508 errcode_t retval;
509 char * block_start;
510 int i, c1, c2, nblks;
511 int limit_offset, root_offset;
512
513 root_info = set_root_node(fs, outdir->buf, ino, parent);
514 root_offset = limit_offset = ((char *) root_info - outdir->buf) +
515 root_info->info_length;
516 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
517 c1 = root_limit->limit;
518 nblks = outdir->num;
519
520 /* Write out the pointer blocks */
521 if (nblks-1 <= c1) {
522 /* Just write out the root block, and we're done */
523 root = (struct ext2_dx_entry *) (outdir->buf + root_offset);
524 for (i=1; i < nblks; i++) {
525 root->block = ext2fs_cpu_to_le32(i);
526 if (i != 1)
527 root->hash =
528 ext2fs_cpu_to_le32(outdir->hashes[i]);
529 root++;
530 c1--;
531 }
532 } else {
533 c2 = 0;
534 limit = 0;
535 root_info->indirect_levels = 1;
536 for (i=1; i < nblks; i++) {
537 if (c1 == 0)
538 return ENOSPC;
539 if (c2 == 0) {
540 if (limit)
541 limit->limit = limit->count =
542 ext2fs_cpu_to_le16(limit->limit);
543 root = (struct ext2_dx_entry *)
544 (outdir->buf + root_offset);
545 root->block = ext2fs_cpu_to_le32(outdir->num);
546 if (i != 1)
547 root->hash =
548 ext2fs_cpu_to_le32(outdir->hashes[i]);
549 if ((retval = get_next_block(fs, outdir,
550 &block_start)))
551 return retval;
552 dx_ent = set_int_node(fs, block_start);
553 limit = (struct ext2_dx_countlimit *) dx_ent;
554 c2 = limit->limit;
555 root_offset += sizeof(struct ext2_dx_entry);
556 c1--;
557 }
558 dx_ent->block = ext2fs_cpu_to_le32(i);
559 if (c2 != limit->limit)
560 dx_ent->hash =
561 ext2fs_cpu_to_le32(outdir->hashes[i]);
562 dx_ent++;
563 c2--;
564 }
565 limit->count = ext2fs_cpu_to_le16(limit->limit - c2);
566 limit->limit = ext2fs_cpu_to_le16(limit->limit);
567 }
568 root_limit = (struct ext2_dx_countlimit *) (outdir->buf + limit_offset);
569 root_limit->count = ext2fs_cpu_to_le16(root_limit->limit - c1);
570 root_limit->limit = ext2fs_cpu_to_le16(root_limit->limit);
571
572 return 0;
573}
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400574
575struct write_dir_struct {
576 struct out_dir *outdir;
577 errcode_t err;
578 e2fsck_t ctx;
579 int cleared;
580};
581
582/*
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400583 * Helper function which writes out a directory block.
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400584 */
585static int write_dir_block(ext2_filsys fs,
586 blk_t *block_nr,
587 e2_blkcnt_t blockcnt,
588 blk_t ref_block,
589 int ref_offset,
590 void *priv_data)
591{
592 struct write_dir_struct *wd = (struct write_dir_struct *) priv_data;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400593 blk_t blk;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400594 char *dir;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400595
596 if (*block_nr == 0)
597 return 0;
598 if (blockcnt >= wd->outdir->num) {
599 e2fsck_read_bitmaps(wd->ctx);
600 blk = *block_nr;
601 ext2fs_unmark_block_bitmap(wd->ctx->block_found_map, blk);
602 ext2fs_block_alloc_stats(fs, blk, -1);
603 *block_nr = 0;
604 wd->cleared++;
605 return BLOCK_CHANGED;
606 }
607 if (blockcnt < 0)
608 return 0;
609
610 dir = wd->outdir->buf + (blockcnt * fs->blocksize);
611 wd->err = ext2fs_write_dir_block(fs, *block_nr, dir);
612 if (wd->err)
613 return BLOCK_ABORT;
614 return 0;
615}
616
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400617static errcode_t write_directory(e2fsck_t ctx, ext2_filsys fs,
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400618 struct out_dir *outdir,
619 ext2_ino_t ino, int compress)
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400620{
621 struct write_dir_struct wd;
622 errcode_t retval;
623 struct ext2_inode inode;
624
625 retval = e2fsck_expand_directory(ctx, ino, -1, outdir->num);
626 if (retval)
627 return retval;
628
629 wd.outdir = outdir;
630 wd.err = 0;
631 wd.ctx = ctx;
632 wd.cleared = 0;
633
634 retval = ext2fs_block_iterate2(fs, ino, 0, 0,
635 write_dir_block, &wd);
636 if (retval)
637 return retval;
638 if (wd.err)
639 return wd.err;
640
641 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400642 if (compress)
643 inode.i_flags &= ~EXT2_INDEX_FL;
644 else
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400645 inode.i_flags |= EXT2_INDEX_FL;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400646 inode.i_size = outdir->num * fs->blocksize;
647 inode.i_blocks -= (fs->blocksize / 512) * wd.cleared;
648 e2fsck_write_inode(ctx, ino, &inode, "rehash_dir");
649
650 return 0;
651}
652
653errcode_t e2fsck_rehash_dir(e2fsck_t ctx, ext2_ino_t ino)
654{
655 ext2_filsys fs = ctx->fs;
656 errcode_t retval;
657 struct ext2_inode inode;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400658 char *dir_buf = 0;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400659 struct fill_dir_struct fd;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400660 struct out_dir outdir;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400661
662 e2fsck_read_inode(ctx, ino, &inode, "rehash_dir");
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400663
664 retval = ENOMEM;
665 fd.harray = 0;
666 dir_buf = malloc(inode.i_size);
667 if (!dir_buf)
668 goto errout;
669
670 fd.max_array = inode.i_size / 32;
671 fd.num_array = 0;
672 fd.harray = malloc(fd.max_array * sizeof(struct hash_entry));
673 if (!fd.harray)
674 goto errout;
675
676 fd.ctx = ctx;
677 fd.buf = dir_buf;
678 fd.inode = &inode;
679 fd.err = 0;
680 fd.dir_size = 0;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400681 fd.compress = 0;
682 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
Theodore Ts'oe70ae992002-09-28 09:16:28 -0400683 (inode.i_size / fs->blocksize) < 2)
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400684 fd.compress = 1;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400685 fd.parent = 0;
686
687 /* Read in the entire directory into memory */
688 retval = ext2fs_block_iterate2(fs, ino, 0, 0,
689 fill_dir_block, &fd);
690 if (fd.err) {
691 retval = fd.err;
692 goto errout;
693 }
694
695#if 0
696 printf("%d entries (%d bytes) found in inode %d\n",
697 fd.num_array, fd.dir_size, ino);
698#endif
699
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400700 /* Sort the list */
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500701resort:
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400702 if (fd.compress)
703 qsort(fd.harray+2, fd.num_array-2,
704 sizeof(struct hash_entry), name_cmp);
705 else
706 qsort(fd.harray, fd.num_array,
707 sizeof(struct hash_entry), hash_cmp);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400708
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400709 /*
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500710 * Look for duplicates
711 */
712 if (duplicate_search_and_fix(ctx, fs, ino, &fd))
713 goto resort;
714
715 /*
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400716 * Copy the directory entries. In a htree directory these
717 * will become the leaf nodes.
718 */
719 retval = copy_dir_entries(fs, &fd, &outdir);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400720 if (retval)
721 goto errout;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400722
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400723 free(dir_buf); dir_buf = 0;
724
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400725 if (!fd.compress) {
726 /* Calculate the interior nodes */
727 retval = calculate_tree(fs, &outdir, ino, fd.parent);
728 if (retval)
729 goto errout;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400730 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400731
732 retval = write_directory(ctx, fs, &outdir, ino, fd.compress);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400733 if (retval)
734 goto errout;
735
736errout:
737 if (dir_buf)
738 free(dir_buf);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400739 if (fd.harray)
740 free(fd.harray);
741
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400742 free_out_dir(&outdir);
743 return retval;
744}
745
746void e2fsck_rehash_directories(e2fsck_t ctx)
747{
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400748 struct problem_context pctx;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400749#ifdef RESOURCE_TRACK
750 struct resource_track rtrack;
751#endif
752 struct dir_info *dir;
753 ext2_u32_iterate iter;
754 ext2_ino_t ino;
755 errcode_t retval;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500756 int i, cur, max, all_dirs, dir_index, first = 1;
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400757
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400758#ifdef RESOURCE_TRACK
759 init_resource_track(&rtrack);
760#endif
761
762 all_dirs = ctx->options & E2F_OPT_COMPRESS_DIRS;
763
764 if (!ctx->dirs_to_hash && !all_dirs)
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400765 return;
766
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400767 e2fsck_get_lost_and_found(ctx, 0);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400768
769 clear_problem_context(&pctx);
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400770
771 dir_index = ctx->fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500772 cur = 0;
773 if (all_dirs) {
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400774 i = 0;
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500775 max = e2fsck_get_num_dirinfo(ctx);
776 } else {
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400777 retval = ext2fs_u32_list_iterate_begin(ctx->dirs_to_hash,
778 &iter);
779 if (retval) {
780 pctx.errcode = retval;
781 fix_problem(ctx, PR_3A_OPTIMIZE_ITER, &pctx);
782 return;
783 }
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500784 max = ext2fs_u32_list_count(ctx->dirs_to_hash);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400785 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400786 while (1) {
787 if (all_dirs) {
788 if ((dir = e2fsck_dir_info_iter(ctx, &i)) == 0)
789 break;
790 ino = dir->ino;
791 } else {
792 if (!ext2fs_u32_list_iterate(iter, &ino))
793 break;
794 }
795 if (ino == ctx->lost_and_found)
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400796 continue;
797 pctx.dir = ino;
798 if (first) {
799 fix_problem(ctx, PR_3A_PASS_HEADER, &pctx);
800 first = 0;
801 }
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500802#if 0
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400803 fix_problem(ctx, PR_3A_OPTIMIZE_DIR, &pctx);
Theodore Ts'ob0700a12003-03-14 01:43:56 -0500804#endif
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400805 pctx.errcode = e2fsck_rehash_dir(ctx, ino);
806 if (pctx.errcode) {
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400807 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
808 fix_problem(ctx, PR_3A_OPTIMIZE_DIR_ERR, &pctx);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400809 }
Theodore Ts'o52734dc2003-03-15 04:03:43 -0500810 if (ctx->progress && !ctx->progress_fd)
811 e2fsck_simple_progress(ctx, "Rebuilding directory",
812 (float) (++cur) / (float) max, ino);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400813 }
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400814 end_problem_latch(ctx, PR_LATCH_OPTIMIZE_DIR);
815 if (!all_dirs)
816 ext2fs_u32_list_iterate_end(iter);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400817
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400818 if (ctx->dirs_to_hash)
819 ext2fs_u32_list_free(ctx->dirs_to_hash);
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400820 ctx->dirs_to_hash = 0;
Theodore Ts'o850d05e2002-07-25 00:00:08 -0400821
822#ifdef RESOURCE_TRACK
823 if (ctx->options & E2F_OPT_TIME2) {
824 e2fsck_clear_progbar(ctx);
825 print_resource_track("Pass 3A", &rtrack);
826 }
827#endif
Theodore Ts'ob7a00562002-07-20 00:28:07 -0400828}