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