Whamcloud - gitweb
Miscellaneous gcc -Wall cleanups. Use fixed rather than floating point
[tools/e2fsprogs.git] / e2fsck / pass2.c
1 /*
2  * pass2.c --- check directory structure
3  * 
4  * Copyright (C) 1993, 1994, 1995, 1996, 1997 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  * Pass 2 of e2fsck iterates through all active directory inodes, and
12  * applies to following tests to each directory entry in the directory
13  * blocks in the inodes:
14  *
15  *      - The length of the directory entry (rec_len) should be at
16  *              least 8 bytes, and no more than the remaining space
17  *              left in the directory block.
18  *      - The length of the name in the directory entry (name_len)
19  *              should be less than (rec_len - 8).  
20  *      - The inode number in the directory entry should be within
21  *              legal bounds.
22  *      - The inode number should refer to a in-use inode.
23  *      - The first entry should be '.', and its inode should be
24  *              the inode of the directory.
25  *      - The second entry should be '..'.
26  *
27  * To minimize disk seek time, the directory blocks are processed in
28  * sorted order of block numbers.
29  *
30  * Pass 2 also collects the following information:
31  *      - The inode numbers of the subdirectories for each directory.
32  *
33  * Pass 2 relies on the following information from previous passes:
34  *      - The directory information collected in pass 1.
35  *      - The inode_used_map bitmap
36  *      - The inode_bad_map bitmap
37  *      - The inode_dir_map bitmap
38  *
39  * Pass 2 frees the following data structures
40  *      - The inode_bad_map bitmap
41  *      - The inode_reg_map bitmap
42  */
43
44 #define _GNU_SOURCE 1 /* get strnlen() */
45 #include <string.h>
46
47 #include "e2fsck.h"
48 #include "problem.h"
49 #include "dict.h"
50
51 #ifdef NO_INLINE_FUNCS
52 #define _INLINE_
53 #else
54 #define _INLINE_ inline
55 #endif
56
57 /* #define DX_DEBUG */
58
59 /*
60  * Keeps track of how many times an inode is referenced.
61  */
62 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf);
63 static int check_dir_block(ext2_filsys fs,
64                            struct ext2_db_entry *dir_blocks_info,
65                            void *priv_data);
66 static int allocate_dir_block(e2fsck_t ctx,
67                               struct ext2_db_entry *dir_blocks_info,
68                               char *buf, struct problem_context *pctx);
69 static int update_dir_block(ext2_filsys fs,
70                             blk_t       *block_nr,
71                             e2_blkcnt_t blockcnt,
72                             blk_t       ref_block,
73                             int         ref_offset, 
74                             void        *priv_data);
75 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
76 static int htree_depth(struct dx_dir_info *dx_dir,
77                        struct dx_dirblock_info *dx_db);
78 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
79
80 struct check_dir_struct {
81         char *buf;
82         struct problem_context  pctx;
83         int     count, max;
84         e2fsck_t ctx;
85 };      
86
87 void e2fsck_pass2(e2fsck_t ctx)
88 {
89         struct ext2_super_block *sb = ctx->fs->super;
90         struct problem_context  pctx;
91         ext2_filsys             fs = ctx->fs;
92         char                    *buf;
93 #ifdef RESOURCE_TRACK
94         struct resource_track   rtrack;
95 #endif
96         struct dir_info         *dir;
97         struct check_dir_struct cd;
98         struct dx_dir_info      *dx_dir;
99         struct dx_dirblock_info *dx_db, *dx_parent;
100         int                     b;
101         int                     i, depth;
102         problem_t               code;
103         int                     bad_dir;
104
105 #ifdef RESOURCE_TRACK
106         init_resource_track(&rtrack);
107 #endif
108
109         clear_problem_context(&cd.pctx);
110
111 #ifdef MTRACE
112         mtrace_print("Pass 2");
113 #endif
114
115         if (!(ctx->options & E2F_OPT_PREEN))
116                 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
117
118         cd.pctx.errcode = ext2fs_create_icount2(fs, EXT2_ICOUNT_OPT_INCREMENT,
119                                                 0, ctx->inode_link_info,
120                                                 &ctx->inode_count);
121         if (cd.pctx.errcode) {
122                 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
123                 ctx->flags |= E2F_FLAG_ABORT;
124                 return;
125         }
126         buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
127                                               "directory scan buffer");
128
129         /*
130          * Set up the parent pointer for the root directory, if
131          * present.  (If the root directory is not present, we will
132          * create it in pass 3.)
133          */
134         dir = e2fsck_get_dir_info(ctx, EXT2_ROOT_INO);
135         if (dir)
136                 dir->parent = EXT2_ROOT_INO;
137
138         cd.buf = buf;
139         cd.ctx = ctx;
140         cd.count = 1;
141         cd.max = ext2fs_dblist_count(fs->dblist);
142
143         if (ctx->progress)
144                 (void) (ctx->progress)(ctx, 2, 0, cd.max);
145
146         if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
147                 ext2fs_dblist_sort(fs->dblist, special_dir_block_cmp);
148         
149         cd.pctx.errcode = ext2fs_dblist_iterate(fs->dblist, check_dir_block,
150                                                 &cd);
151         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
152                 return;
153         if (cd.pctx.errcode) {
154                 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
155                 ctx->flags |= E2F_FLAG_ABORT;
156                 return;
157         }
158
159 #ifdef ENABLE_HTREE
160         for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
161                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
162                         return;
163                 if (dx_dir->numblocks == 0)
164                         continue;
165                 clear_problem_context(&pctx);
166                 bad_dir = 0;
167                 pctx.dir = dx_dir->ino;
168                 dx_db = dx_dir->dx_block;
169                 if (dx_db->flags & DX_FLAG_REFERENCED)
170                         dx_db->flags |= DX_FLAG_DUP_REF;
171                 else
172                         dx_db->flags |= DX_FLAG_REFERENCED;
173                 /*
174                  * Find all of the first and last leaf blocks, and
175                  * update their parent's min and max hash values
176                  */
177                 for (b=0, dx_db = dx_dir->dx_block;
178                      b < dx_dir->numblocks;
179                      b++, dx_db++) {
180                         if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
181                             !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
182                                 continue;
183                         dx_parent = &dx_dir->dx_block[dx_db->parent];
184                         /*
185                          * XXX Make sure dx_parent->min_hash > dx_db->min_hash
186                          */
187                         if (dx_db->flags & DX_FLAG_FIRST)
188                                 dx_parent->min_hash = dx_db->min_hash;
189                         /*
190                          * XXX Make sure dx_parent->max_hash < dx_db->max_hash
191                          */
192                         if (dx_db->flags & DX_FLAG_LAST)
193                                 dx_parent->max_hash = dx_db->max_hash;
194                 }
195                                 
196                 for (b=0, dx_db = dx_dir->dx_block;
197                      b < dx_dir->numblocks;
198                      b++, dx_db++) {
199                         pctx.blkcount = b;
200                         pctx.group = dx_db->parent;
201                         code = 0;
202                         if (!(dx_db->flags & DX_FLAG_FIRST) &&
203                             (dx_db->min_hash < dx_db->node_min_hash)) {
204                                 pctx.blk = dx_db->min_hash;
205                                 pctx.blk2 = dx_db->node_min_hash;
206                                 code = PR_2_HTREE_MIN_HASH;
207                                 fix_problem(ctx, code, &pctx);
208                                 bad_dir++;
209                         }
210                         if (dx_db->type == DX_DIRBLOCK_LEAF) {
211                                 depth = htree_depth(dx_dir, dx_db);
212                                 if (depth != dx_dir->depth) {
213                                         code = PR_2_HTREE_BAD_DEPTH;
214                                         fix_problem(ctx, code, &pctx);
215                                         bad_dir++;
216                                 }
217                         }
218                         /*
219                          * This test doesn't apply for the root block 
220                          * at block #0
221                          */
222                         if (b &&
223                             (dx_db->max_hash > dx_db->node_max_hash)) {
224                                 pctx.blk = dx_db->max_hash;
225                                 pctx.blk2 = dx_db->node_max_hash;
226                                 code = PR_2_HTREE_MAX_HASH;
227                                 fix_problem(ctx, code, &pctx);
228                                 bad_dir++;
229                         }
230                         if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
231                                 code = PR_2_HTREE_NOTREF;
232                                 fix_problem(ctx, code, &pctx);
233                                 bad_dir++;
234                         } else if (dx_db->flags & DX_FLAG_DUP_REF) {
235                                 code = PR_2_HTREE_DUPREF;
236                                 fix_problem(ctx, code, &pctx);
237                                 bad_dir++;
238                         }
239                         if (code == 0)
240                                 continue;
241                 }
242                 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
243                         clear_htree(ctx, dx_dir->ino);
244                         dx_dir->numblocks = 0;
245                 }
246 #ifdef ENABLE_HTREE_CLEAR
247                 if (dx_dir->numblocks) {
248                         fix_problem(ctx, PR_2_HTREE_FCLR, &pctx);
249                         clear_htree(ctx, dx_dir->ino);
250                         dx_dir->numblocks = 0;
251                 }
252 #endif
253         }
254 #endif
255         ext2fs_free_mem(&buf);
256         ext2fs_free_dblist(fs->dblist);
257
258         if (ctx->inode_bad_map) {
259                 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
260                 ctx->inode_bad_map = 0;
261         }
262         if (ctx->inode_reg_map) {
263                 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
264                 ctx->inode_reg_map = 0;
265         }
266
267         clear_problem_context(&pctx);
268         if (ctx->large_files) {
269                 if (!(sb->s_feature_ro_compat &
270                       EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
271                     fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
272                         sb->s_feature_ro_compat |=
273                                 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
274                         ext2fs_mark_super_dirty(fs);
275                 }
276                 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
277                     fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
278                         ext2fs_update_dynamic_rev(fs);
279                         ext2fs_mark_super_dirty(fs);
280                 }
281         } else if (!ctx->large_files &&
282             (sb->s_feature_ro_compat &
283               EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
284                 if (fs->flags & EXT2_FLAG_RW) {
285                         sb->s_feature_ro_compat &= 
286                                 ~EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
287                         ext2fs_mark_super_dirty(fs);
288                 }
289         }
290         
291 #ifdef RESOURCE_TRACK
292         if (ctx->options & E2F_OPT_TIME2) {
293                 e2fsck_clear_progbar(ctx);
294                 print_resource_track(_("Pass 2"), &rtrack);
295         }
296 #endif
297 }
298
299 #define MAX_DEPTH 32000
300 static int htree_depth(struct dx_dir_info *dx_dir,
301                        struct dx_dirblock_info *dx_db)
302 {
303         int     depth = 0;
304
305         while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
306                 dx_db = &dx_dir->dx_block[dx_db->parent];
307                 depth++;
308         }
309         return depth;
310 }
311
312 static int dict_de_cmp(const void *a, const void *b)
313 {
314         const struct ext2_dir_entry *de_a, *de_b;
315         int     a_len, b_len;
316
317         de_a = (const struct ext2_dir_entry *) a;
318         a_len = de_a->name_len & 0xFF;
319         de_b = (const struct ext2_dir_entry *) b;
320         b_len = de_b->name_len & 0xFF;
321
322         if (a_len != b_len)
323                 return (a_len - b_len);
324
325         return strncmp(de_a->name, de_b->name, a_len);
326 }
327
328 /*
329  * This is special sort function that makes sure that directory blocks
330  * with a dirblock of zero are sorted to the beginning of the list.
331  * This guarantees that the root node of the htree directories are
332  * processed first, so we know what hash version to use.
333  */
334 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
335 {
336         const struct ext2_db_entry *db_a =
337                 (const struct ext2_db_entry *) a;
338         const struct ext2_db_entry *db_b =
339                 (const struct ext2_db_entry *) b;
340
341         if (db_a->blockcnt && !db_b->blockcnt)
342                 return 1;
343
344         if (!db_a->blockcnt && db_b->blockcnt)
345                 return -1;
346         
347         if (db_a->blk != db_b->blk)
348                 return (int) (db_a->blk - db_b->blk);
349         
350         if (db_a->ino != db_b->ino)
351                 return (int) (db_a->ino - db_b->ino);
352
353         return (int) (db_a->blockcnt - db_b->blockcnt);
354 }
355
356
357 /*
358  * Make sure the first entry in the directory is '.', and that the
359  * directory entry is sane.
360  */
361 static int check_dot(e2fsck_t ctx,
362                      struct ext2_dir_entry *dirent,
363                      ext2_ino_t ino, struct problem_context *pctx)
364 {
365         struct ext2_dir_entry *nextdir;
366         int     status = 0;
367         int     created = 0;
368         int     new_len;
369         int     problem = 0;
370         
371         if (!dirent->inode)
372                 problem = PR_2_MISSING_DOT;
373         else if (((dirent->name_len & 0xFF) != 1) ||
374                  (dirent->name[0] != '.'))
375                 problem = PR_2_1ST_NOT_DOT;
376         else if (dirent->name[1] != '\0')
377                 problem = PR_2_DOT_NULL_TERM;
378         
379         if (problem) {
380                 if (fix_problem(ctx, problem, pctx)) {
381                         if (dirent->rec_len < 12)
382                                 dirent->rec_len = 12;
383                         dirent->inode = ino;
384                         dirent->name_len = 1;
385                         dirent->name[0] = '.';
386                         dirent->name[1] = '\0';
387                         status = 1;
388                         created = 1;
389                 }
390         }
391         if (dirent->inode != ino) {
392                 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
393                         dirent->inode = ino;
394                         status = 1;
395                 }
396         }
397         if (dirent->rec_len > 12) {
398                 new_len = dirent->rec_len - 12;
399                 if (new_len > 12) {
400                         if (created ||
401                             fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
402                                 nextdir = (struct ext2_dir_entry *)
403                                         ((char *) dirent + 12);
404                                 dirent->rec_len = 12;
405                                 nextdir->rec_len = new_len;
406                                 nextdir->inode = 0;
407                                 nextdir->name_len = 0;
408                                 status = 1;
409                         }
410                 }
411         }
412         return status;
413 }
414
415 /*
416  * Make sure the second entry in the directory is '..', and that the
417  * directory entry is sane.  We do not check the inode number of '..'
418  * here; this gets done in pass 3.
419  */
420 static int check_dotdot(e2fsck_t ctx,
421                         struct ext2_dir_entry *dirent,
422                         struct dir_info *dir, struct problem_context *pctx)
423 {
424         int             problem = 0;
425         
426         if (!dirent->inode)
427                 problem = PR_2_MISSING_DOT_DOT;
428         else if (((dirent->name_len & 0xFF) != 2) ||
429                  (dirent->name[0] != '.') ||
430                  (dirent->name[1] != '.'))
431                 problem = PR_2_2ND_NOT_DOT_DOT;
432         else if (dirent->name[2] != '\0')
433                 problem = PR_2_DOT_DOT_NULL_TERM;
434
435         if (problem) {
436                 if (fix_problem(ctx, problem, pctx)) {
437                         if (dirent->rec_len < 12)
438                                 dirent->rec_len = 12;
439                         /*
440                          * Note: we don't have the parent inode just
441                          * yet, so we will fill it in with the root
442                          * inode.  This will get fixed in pass 3.
443                          */
444                         dirent->inode = EXT2_ROOT_INO;
445                         dirent->name_len = 2;
446                         dirent->name[0] = '.';
447                         dirent->name[1] = '.';
448                         dirent->name[2] = '\0';
449                         return 1;
450                 } 
451                 return 0;
452         }
453         dir->dotdot = dirent->inode;
454         return 0;
455 }
456
457 /*
458  * Check to make sure a directory entry doesn't contain any illegal
459  * characters.
460  */
461 static int check_name(e2fsck_t ctx,
462                       struct ext2_dir_entry *dirent,
463                       ext2_ino_t dir_ino EXT2FS_ATTR((unused)), 
464                       struct problem_context *pctx)
465 {
466         int     i;
467         int     fixup = -1;
468         int     ret = 0;
469         
470         for ( i = 0; i < (dirent->name_len & 0xFF); i++) {
471                 if (dirent->name[i] == '/' || dirent->name[i] == '\0') {
472                         if (fixup < 0) {
473                                 fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
474                         }
475                         if (fixup) {
476                                 dirent->name[i] = '.';
477                                 ret = 1;
478                         }
479                 }
480         }
481         return ret;
482 }
483
484 /*
485  * Check the directory filetype (if present)
486  */
487 static _INLINE_ int check_filetype(e2fsck_t ctx,
488                                    struct ext2_dir_entry *dirent,
489                                    ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
490                                    struct problem_context *pctx)
491 {
492         int     filetype = dirent->name_len >> 8;
493         int     should_be = EXT2_FT_UNKNOWN;
494         struct ext2_inode       inode;
495
496         if (!(ctx->fs->super->s_feature_incompat &
497               EXT2_FEATURE_INCOMPAT_FILETYPE)) {
498                 if (filetype == 0 ||
499                     !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
500                         return 0;
501                 dirent->name_len = dirent->name_len & 0xFF;
502                 return 1;
503         }
504
505         if (ext2fs_test_inode_bitmap(ctx->inode_dir_map, dirent->inode)) {
506                 should_be = EXT2_FT_DIR;
507         } else if (ext2fs_test_inode_bitmap(ctx->inode_reg_map,
508                                             dirent->inode)) {
509                 should_be = EXT2_FT_REG_FILE;
510         } else if (ctx->inode_bad_map &&
511                    ext2fs_test_inode_bitmap(ctx->inode_bad_map,
512                                             dirent->inode))
513                 should_be = 0;
514         else {
515                 e2fsck_read_inode(ctx, dirent->inode, &inode,
516                                   "check_filetype");
517                 should_be = ext2_file_type(inode.i_mode);
518         }
519         if (filetype == should_be)
520                 return 0;
521         pctx->num = should_be;
522
523         if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
524                         pctx) == 0)
525                 return 0;
526                         
527         dirent->name_len = (dirent->name_len & 0xFF) | should_be << 8;
528         return 1;
529 }
530
531 #ifdef ENABLE_HTREE
532 static void parse_int_node(ext2_filsys fs,
533                            struct ext2_db_entry *db,
534                            struct check_dir_struct *cd,
535                            struct dx_dir_info   *dx_dir,
536                            char *block_buf)
537 {
538         struct          ext2_dx_root_info  *root;
539         struct          ext2_dx_entry *ent;
540         struct          ext2_dx_countlimit *limit;
541         struct dx_dirblock_info *dx_db;
542         int             i, expect_limit, count;
543         blk_t           blk;
544         ext2_dirhash_t  min_hash = 0xffffffff;
545         ext2_dirhash_t  max_hash = 0;
546         ext2_dirhash_t  hash = 0, prev_hash;
547
548         if (db->blockcnt == 0) {
549                 root = (struct ext2_dx_root_info *) (block_buf + 24);
550                 
551 #ifdef DX_DEBUG
552                 printf("Root node dump:\n");
553                 printf("\t Reserved zero: %d\n", root->reserved_zero);
554                 printf("\t Hash Version: %d\n", root->hash_version);
555                 printf("\t Info length: %d\n", root->info_length);
556                 printf("\t Indirect levels: %d\n", root->indirect_levels);
557                 printf("\t Flags: %d\n", root->unused_flags);
558 #endif
559
560                 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
561         } else {
562                 ent = (struct ext2_dx_entry *) (block_buf+8);
563         }
564         limit = (struct ext2_dx_countlimit *) ent;
565
566 #ifdef DX_DEBUG
567         printf("Number of entries (count): %d\n", 
568                ext2fs_le16_to_cpu(limit->count));
569         printf("Number of entries (limit): %d\n", 
570                ext2fs_le16_to_cpu(limit->limit));
571 #endif
572
573         count = ext2fs_le16_to_cpu(limit->count);
574         expect_limit = (fs->blocksize - ((char *) ent - block_buf)) /
575                 sizeof(struct ext2_dx_entry);
576         if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
577                 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
578                 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
579                         goto clear_and_exit;
580         }
581         if (count > expect_limit) {
582                 cd->pctx.num = count;
583                 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
584                         goto clear_and_exit;
585                 count = expect_limit;
586         }
587         
588         for (i=0; i < count; i++) {
589                 prev_hash = hash;
590                 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
591 #ifdef DX_DEBUG
592                 printf("Entry #%d: Hash 0x%08x, block %d\n", i,
593                        hash, ext2fs_le32_to_cpu(ent[i].block));
594 #endif
595                 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
596                 /* Check to make sure the block is valid */
597                 if (blk > (blk_t) dx_dir->numblocks) {
598                         cd->pctx.blk = blk;
599                         if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
600                                         &cd->pctx))
601                                 goto clear_and_exit;
602                 }
603                 if (hash < prev_hash &&
604                     fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
605                         goto clear_and_exit;
606                 dx_db = &dx_dir->dx_block[blk];
607                 if (dx_db->flags & DX_FLAG_REFERENCED) {
608                         dx_db->flags |= DX_FLAG_DUP_REF;
609                 } else {
610                         dx_db->flags |= DX_FLAG_REFERENCED;
611                         dx_db->parent = db->blockcnt;
612                 }
613                 if (hash < min_hash)
614                         min_hash = hash;
615                 if (hash > max_hash)
616                         max_hash = hash;
617                 dx_db->node_min_hash = hash;
618                 if ((i+1) < count)
619                         dx_db->node_max_hash = 
620                           ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
621                 else {
622                         dx_db->node_max_hash = 0xfffffffe;
623                         dx_db->flags |= DX_FLAG_LAST;
624                 }
625                 if (i == 0)
626                         dx_db->flags |= DX_FLAG_FIRST;
627         }
628 #ifdef DX_DEBUG
629         printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
630                db->blockcnt, min_hash, max_hash);
631 #endif
632         dx_db = &dx_dir->dx_block[db->blockcnt];
633         dx_db->min_hash = min_hash;
634         dx_db->max_hash = max_hash;
635         return;
636
637 clear_and_exit:
638         clear_htree(cd->ctx, cd->pctx.ino);
639         dx_dir->numblocks = 0;
640 }
641 #endif /* ENABLE_HTREE */
642
643 /*
644  * Given a busted directory, try to salvage it somehow.
645  * 
646  */
647 static void salvage_directory(ext2_filsys fs,
648                               struct ext2_dir_entry *dirent,
649                               struct ext2_dir_entry *prev,
650                               unsigned int *offset)
651 {
652         char    *cp = (char *) dirent;
653         int left = fs->blocksize - *offset - dirent->rec_len;
654         int name_len = dirent->name_len & 0xFF;
655
656         /*
657          * Special case of directory entry of size 8: copy what's left
658          * of the directory block up to cover up the invalid hole.
659          */
660         if ((left >= 12) && (dirent->rec_len == 8)) {
661                 memmove(cp, cp+8, left);
662                 memset(cp + left, 0, 8);
663                 return;
664         }
665         /*
666          * If the directory entry overruns the end of the directory
667          * block, and the name is small enough to fit, then adjust the
668          * record length.
669          */
670         if ((left < 0) &&
671             (name_len + 8 <= dirent->rec_len + left) &&
672             dirent->inode <= fs->super->s_inodes_count &&
673             strnlen(dirent->name, name_len) == name_len) {
674                 dirent->rec_len += left;
675                 return;
676         }
677         /*
678          * If the directory entry is a multiple of four, so it is
679          * valid, let the previous directory entry absorb the invalid
680          * one. 
681          */
682         if (prev && dirent->rec_len && (dirent->rec_len % 4) == 0) {
683                 prev->rec_len += dirent->rec_len;
684                 *offset += dirent->rec_len;
685                 return;
686         }
687         /*
688          * Default salvage method --- kill all of the directory
689          * entries for the rest of the block.  We will either try to
690          * absorb it into the previous directory entry, or create a
691          * new empty directory entry the rest of the directory block.
692          */
693         if (prev) {
694                 prev->rec_len += fs->blocksize - *offset;
695                 *offset = fs->blocksize;
696         } else {
697                 dirent->rec_len = fs->blocksize - *offset;
698                 dirent->name_len = 0;
699                 dirent->inode = 0;
700         }
701 }
702
703 static int check_dir_block(ext2_filsys fs,
704                            struct ext2_db_entry *db,
705                            void *priv_data)
706 {
707         struct dir_info         *subdir, *dir;
708         struct dx_dir_info      *dx_dir;
709 #ifdef ENABLE_HTREE
710         struct dx_dirblock_info *dx_db = 0;
711 #endif /* ENABLE_HTREE */
712         struct ext2_dir_entry   *dirent, *prev;
713         ext2_dirhash_t          hash;
714         unsigned int            offset = 0;
715         int                     dir_modified = 0;
716         int                     dot_state;
717         blk_t                   block_nr = db->blk;
718         ext2_ino_t              ino = db->ino;
719         __u16                   links;
720         struct check_dir_struct *cd;
721         char                    *buf;
722         e2fsck_t                ctx;
723         int                     problem;
724         struct ext2_dx_root_info *root;
725         struct ext2_dx_countlimit *limit;
726         static dict_t de_dict;
727         struct problem_context  pctx;
728         int     dups_found = 0;
729
730         cd = (struct check_dir_struct *) priv_data;
731         buf = cd->buf;
732         ctx = cd->ctx;
733
734         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
735                 return DIRENT_ABORT;
736         
737         if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
738                 return DIRENT_ABORT;
739         
740         /*
741          * Make sure the inode is still in use (could have been 
742          * deleted in the duplicate/bad blocks pass.
743          */
744         if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map, ino))) 
745                 return 0;
746
747         cd->pctx.ino = ino;
748         cd->pctx.blk = block_nr;
749         cd->pctx.blkcount = db->blockcnt;
750         cd->pctx.ino2 = 0;
751         cd->pctx.dirent = 0;
752         cd->pctx.num = 0;
753
754         if (db->blk == 0) {
755                 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
756                         return 0;
757                 block_nr = db->blk;
758         }
759         
760         if (db->blockcnt)
761                 dot_state = 2;
762         else
763                 dot_state = 0;
764
765         if (ctx->dirs_to_hash &&
766             ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
767                 dups_found++;
768
769 #if 0
770         printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
771                db->blockcnt, ino);
772 #endif
773         
774         cd->pctx.errcode = ext2fs_read_dir_block(fs, block_nr, buf);
775         if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
776                 cd->pctx.errcode = 0; /* We'll handle this ourselves */
777         if (cd->pctx.errcode) {
778                 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
779                         ctx->flags |= E2F_FLAG_ABORT;
780                         return DIRENT_ABORT;
781                 }
782                 memset(buf, 0, fs->blocksize);
783         }
784 #ifdef ENABLE_HTREE
785         dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
786         if (dx_dir && dx_dir->numblocks) {
787                 if (db->blockcnt >= dx_dir->numblocks) {
788                         printf("XXX should never happen!!!\n");
789                         abort();
790                 }
791                 dx_db = &dx_dir->dx_block[db->blockcnt];
792                 dx_db->type = DX_DIRBLOCK_LEAF;
793                 dx_db->phys = block_nr;
794                 dx_db->min_hash = ~0;
795                 dx_db->max_hash = 0;
796                         
797                 dirent = (struct ext2_dir_entry *) buf;
798                 limit = (struct ext2_dx_countlimit *) (buf+8);
799                 if (db->blockcnt == 0) {
800                         root = (struct ext2_dx_root_info *) (buf + 24);
801                         dx_db->type = DX_DIRBLOCK_ROOT;
802                         dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
803                         if ((root->reserved_zero ||
804                              root->info_length < 8 ||
805                              root->indirect_levels > 1) &&
806                             fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
807                                 clear_htree(ctx, ino);
808                                 dx_dir->numblocks = 0;
809                                 dx_db = 0;
810                         } 
811                         dx_dir->hashversion = root->hash_version;
812                         dx_dir->depth = root->indirect_levels + 1;
813                 } else if ((dirent->inode == 0) &&
814                            (dirent->rec_len == fs->blocksize) &&
815                            (dirent->name_len == 0) &&
816                            (ext2fs_le16_to_cpu(limit->limit) == 
817                             ((fs->blocksize-8) / 
818                              sizeof(struct ext2_dx_entry))))
819                         dx_db->type = DX_DIRBLOCK_NODE;
820         }
821 #endif /* ENABLE_HTREE */
822
823         dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
824         prev = 0;
825         do {
826                 problem = 0;
827                 dirent = (struct ext2_dir_entry *) (buf + offset);
828                 cd->pctx.dirent = dirent;
829                 cd->pctx.num = offset;
830                 if (((offset + dirent->rec_len) > fs->blocksize) ||
831                     (dirent->rec_len < 12) ||
832                     ((dirent->rec_len % 4) != 0) ||
833                     (((dirent->name_len & 0xFF)+8) > dirent->rec_len)) {
834                         if (fix_problem(ctx, PR_2_DIR_CORRUPTED, &cd->pctx)) {
835                                 salvage_directory(fs, dirent, prev, &offset);
836                                 dir_modified++;
837                                 continue;
838                         } else
839                                 goto abort_free_dict;
840                 }
841                 if ((dirent->name_len & 0xFF) > EXT2_NAME_LEN) {
842                         if (fix_problem(ctx, PR_2_FILENAME_LONG, &cd->pctx)) {
843                                 dirent->name_len = EXT2_NAME_LEN;
844                                 dir_modified++;
845                         }
846                 }
847
848                 if (dot_state == 0) {
849                         if (check_dot(ctx, dirent, ino, &cd->pctx))
850                                 dir_modified++;
851                 } else if (dot_state == 1) {
852                         dir = e2fsck_get_dir_info(ctx, ino);
853                         if (!dir) {
854                                 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
855                                 goto abort_free_dict;
856                         }
857                         if (check_dotdot(ctx, dirent, dir, &cd->pctx))
858                                 dir_modified++;
859                 } else if (dirent->inode == ino) {
860                         problem = PR_2_LINK_DOT;
861                         if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
862                                 dirent->inode = 0;
863                                 dir_modified++;
864                                 goto next;
865                         }
866                 }
867                 if (!dirent->inode) 
868                         goto next;
869                 
870                 /*
871                  * Make sure the inode listed is a legal one.
872                  */ 
873                 if (((dirent->inode != EXT2_ROOT_INO) &&
874                      (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
875                     (dirent->inode > fs->super->s_inodes_count)) {
876                         problem = PR_2_BAD_INO;
877                 } else if (!(ext2fs_test_inode_bitmap(ctx->inode_used_map,
878                                                dirent->inode))) {
879                         /*
880                          * If the inode is unused, offer to clear it.
881                          */
882                         problem = PR_2_UNUSED_INODE;
883                 } else if (ctx->inode_bb_map &&
884                            (ext2fs_test_inode_bitmap(ctx->inode_bb_map,
885                                                      dirent->inode))) {
886                         /*
887                          * If the inode is in a bad block, offer to
888                          * clear it.
889                          */
890                         problem = PR_2_BB_INODE;
891                 } else if ((dot_state > 1) &&
892                            ((dirent->name_len & 0xFF) == 1) &&
893                            (dirent->name[0] == '.')) {
894                         /*
895                          * If there's a '.' entry in anything other
896                          * than the first directory entry, it's a
897                          * duplicate entry that should be removed.
898                          */
899                         problem = PR_2_DUP_DOT;
900                 } else if ((dot_state > 1) &&
901                            ((dirent->name_len & 0xFF) == 2) &&
902                            (dirent->name[0] == '.') && 
903                            (dirent->name[1] == '.')) {
904                         /*
905                          * If there's a '..' entry in anything other
906                          * than the second directory entry, it's a
907                          * duplicate entry that should be removed.
908                          */
909                         problem = PR_2_DUP_DOT_DOT;
910                 } else if ((dot_state > 1) &&
911                            (dirent->inode == EXT2_ROOT_INO)) {
912                         /*
913                          * Don't allow links to the root directory.
914                          * We check this specially to make sure we
915                          * catch this error case even if the root
916                          * directory hasn't been created yet.
917                          */
918                         problem = PR_2_LINK_ROOT;
919                 } else if ((dot_state > 1) &&
920                            (dirent->name_len & 0xFF) == 0) {
921                         /*
922                          * Don't allow zero-length directory names.
923                          */
924                         problem = PR_2_NULL_NAME;
925                 }
926
927                 if (problem) {
928                         if (fix_problem(ctx, problem, &cd->pctx)) {
929                                 dirent->inode = 0;
930                                 dir_modified++;
931                                 goto next;
932                         } else {
933                                 ext2fs_unmark_valid(fs);
934                                 if (problem == PR_2_BAD_INO)
935                                         goto next;
936                         }
937                 }
938
939                 /*
940                  * If the inode was marked as having bad fields in
941                  * pass1, process it and offer to fix/clear it.
942                  * (We wait until now so that we can display the
943                  * pathname to the user.)
944                  */
945                 if (ctx->inode_bad_map &&
946                     ext2fs_test_inode_bitmap(ctx->inode_bad_map,
947                                              dirent->inode)) {
948                         if (e2fsck_process_bad_inode(ctx, ino,
949                                                      dirent->inode,
950                                                      buf + fs->blocksize)) {
951                                 dirent->inode = 0;
952                                 dir_modified++;
953                                 goto next;
954                         }
955                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
956                                 return DIRENT_ABORT;
957                 }
958
959                 if (check_name(ctx, dirent, ino, &cd->pctx))
960                         dir_modified++;
961
962                 if (check_filetype(ctx, dirent, ino, &cd->pctx))
963                         dir_modified++;
964
965 #ifdef ENABLE_HTREE
966                 if (dx_db) {
967                         ext2fs_dirhash(dx_dir->hashversion, dirent->name,
968                                        (dirent->name_len & 0xFF),
969                                        fs->super->s_hash_seed, &hash, 0);
970                         if (hash < dx_db->min_hash)
971                                 dx_db->min_hash = hash;
972                         if (hash > dx_db->max_hash)
973                                 dx_db->max_hash = hash;
974                 }
975 #endif
976
977                 /*
978                  * If this is a directory, then mark its parent in its
979                  * dir_info structure.  If the parent field is already
980                  * filled in, then this directory has more than one
981                  * hard link.  We assume the first link is correct,
982                  * and ask the user if he/she wants to clear this one.
983                  */
984                 if ((dot_state > 1) &&
985                     (ext2fs_test_inode_bitmap(ctx->inode_dir_map,
986                                               dirent->inode))) {
987                         subdir = e2fsck_get_dir_info(ctx, dirent->inode);
988                         if (!subdir) {
989                                 cd->pctx.ino = dirent->inode;
990                                 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
991                                 goto abort_free_dict;
992                         }
993                         if (subdir->parent) {
994                                 cd->pctx.ino2 = subdir->parent;
995                                 if (fix_problem(ctx, PR_2_LINK_DIR,
996                                                 &cd->pctx)) {
997                                         dirent->inode = 0;
998                                         dir_modified++;
999                                         goto next;
1000                                 }
1001                                 cd->pctx.ino2 = 0;
1002                         } else
1003                                 subdir->parent = ino;
1004                 }
1005
1006                 if (dups_found) {
1007                         ;
1008                 } else if (dict_lookup(&de_dict, dirent)) {
1009                         clear_problem_context(&pctx);
1010                         pctx.ino = ino;
1011                         pctx.dirent = dirent;
1012                         fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1013                         if (!ctx->dirs_to_hash)
1014                                 ext2fs_u32_list_create(&ctx->dirs_to_hash, 50);
1015                         if (ctx->dirs_to_hash)
1016                                 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1017                         dups_found++;
1018                 } else
1019                         dict_alloc_insert(&de_dict, dirent, dirent);
1020                 
1021                 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1022                                         &links);
1023                 if (links > 1)
1024                         ctx->fs_links_count++;
1025                 ctx->fs_total_count++;
1026         next:
1027                 prev = dirent;
1028                 offset += dirent->rec_len;
1029                 dot_state++;
1030         } while (offset < fs->blocksize);
1031 #if 0
1032         printf("\n");
1033 #endif
1034 #ifdef ENABLE_HTREE
1035         if (dx_db) {
1036 #ifdef DX_DEBUG
1037                 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1038                        db->blockcnt, dx_db->type,
1039                        dx_db->min_hash, dx_db->max_hash);
1040 #endif
1041                 cd->pctx.dir = cd->pctx.ino;
1042                 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1043                     (dx_db->type == DX_DIRBLOCK_NODE))
1044                         parse_int_node(fs, db, cd, dx_dir, buf);
1045         }
1046 #endif /* ENABLE_HTREE */
1047         if (offset != fs->blocksize) {
1048                 cd->pctx.num = dirent->rec_len - fs->blocksize + offset;
1049                 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1050                         dirent->rec_len = cd->pctx.num;
1051                         dir_modified++;
1052                 }
1053         }
1054         if (dir_modified) {
1055                 cd->pctx.errcode = ext2fs_write_dir_block(fs, block_nr, buf);
1056                 if (cd->pctx.errcode) {
1057                         if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
1058                                          &cd->pctx))
1059                                 goto abort_free_dict;
1060                 }
1061                 ext2fs_mark_changed(fs);
1062         }
1063         dict_free_nodes(&de_dict);
1064         return 0;
1065 abort_free_dict:
1066         dict_free_nodes(&de_dict);
1067         ctx->flags |= E2F_FLAG_ABORT;
1068         return DIRENT_ABORT;
1069 }
1070
1071 /*
1072  * This function is called to deallocate a block, and is an interator
1073  * functioned called by deallocate inode via ext2fs_iterate_block().
1074  */
1075 static int deallocate_inode_block(ext2_filsys fs,
1076                                   blk_t *block_nr,
1077                                   e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1078                                   blk_t ref_block EXT2FS_ATTR((unused)),
1079                                   int ref_offset EXT2FS_ATTR((unused)),
1080                                   void *priv_data)
1081 {
1082         e2fsck_t        ctx = (e2fsck_t) priv_data;
1083         
1084         if (HOLE_BLKADDR(*block_nr))
1085                 return 0;
1086         if ((*block_nr < fs->super->s_first_data_block) ||
1087             (*block_nr >= fs->super->s_blocks_count))
1088                 return 0;
1089         ext2fs_unmark_block_bitmap(ctx->block_found_map, *block_nr);
1090         ext2fs_block_alloc_stats(fs, *block_nr, -1);
1091         return 0;
1092 }
1093                 
1094 /*
1095  * This fuction deallocates an inode
1096  */
1097 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
1098 {
1099         ext2_filsys fs = ctx->fs;
1100         struct ext2_inode       inode;
1101         struct problem_context  pctx;
1102         __u32                   count;
1103         
1104         ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1105         e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1106         inode.i_links_count = 0;
1107         inode.i_dtime = time(0);
1108         e2fsck_write_inode(ctx, ino, &inode, "deallocate_inode");
1109         clear_problem_context(&pctx);
1110         pctx.ino = ino;
1111
1112         /*
1113          * Fix up the bitmaps...
1114          */
1115         e2fsck_read_bitmaps(ctx);
1116         ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1117         ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1118         if (ctx->inode_bad_map)
1119                 ext2fs_unmark_inode_bitmap(ctx->inode_bad_map, ino);
1120         ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1121
1122         if (inode.i_file_acl &&
1123             (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1124                 pctx.errcode = ext2fs_adjust_ea_refcount(fs, inode.i_file_acl,
1125                                                    block_buf, -1, &count);
1126                 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1127                         pctx.errcode = 0;
1128                         count = 1;
1129                 }
1130                 if (pctx.errcode) {
1131                         pctx.blk = inode.i_file_acl;
1132                         fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1133                         ctx->flags |= E2F_FLAG_ABORT;
1134                         return;
1135                 }
1136                 if (count == 0) {
1137                         ext2fs_unmark_block_bitmap(ctx->block_found_map,
1138                                                    inode.i_file_acl);
1139                         ext2fs_block_alloc_stats(fs, inode.i_file_acl, -1);
1140                 }
1141                 inode.i_file_acl = 0;
1142         }
1143
1144         if (!ext2fs_inode_has_valid_blocks(&inode))
1145                 return;
1146
1147         if (LINUX_S_ISREG(inode.i_mode) &&
1148             (inode.i_size_high || inode.i_size & 0x80000000UL))
1149                 ctx->large_files--;
1150
1151         pctx.errcode = ext2fs_block_iterate2(fs, ino, 0, block_buf,
1152                                             deallocate_inode_block, ctx);
1153         if (pctx.errcode) {
1154                 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
1155                 ctx->flags |= E2F_FLAG_ABORT;
1156                 return;
1157         }
1158 }
1159
1160 /*
1161  * This fuction clears the htree flag on an inode
1162  */
1163 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1164 {
1165         struct ext2_inode       inode;
1166         
1167         e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1168         inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1169         e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
1170         if (ctx->dirs_to_hash)
1171                 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1172 }
1173
1174
1175 extern int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1176                                     ext2_ino_t ino, char *buf)
1177 {
1178         ext2_filsys fs = ctx->fs;
1179         struct ext2_inode       inode;
1180         int                     inode_modified = 0;
1181         unsigned char           *frag, *fsize;
1182         struct problem_context  pctx;
1183         int     problem = 0;
1184
1185         e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
1186
1187         clear_problem_context(&pctx);
1188         pctx.ino = ino;
1189         pctx.dir = dir;
1190         pctx.inode = &inode;
1191
1192         if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1193             !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1194             !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
1195             !(LINUX_S_ISSOCK(inode.i_mode)))
1196                 problem = PR_2_BAD_MODE;
1197         else if (LINUX_S_ISCHR(inode.i_mode)
1198                  && !e2fsck_pass1_check_device_inode(fs, &inode))
1199                 problem = PR_2_BAD_CHAR_DEV;
1200         else if (LINUX_S_ISBLK(inode.i_mode)
1201                  && !e2fsck_pass1_check_device_inode(fs, &inode))
1202                 problem = PR_2_BAD_BLOCK_DEV;
1203         else if (LINUX_S_ISFIFO(inode.i_mode)
1204                  && !e2fsck_pass1_check_device_inode(fs, &inode))
1205                 problem = PR_2_BAD_FIFO;
1206         else if (LINUX_S_ISSOCK(inode.i_mode)
1207                  && !e2fsck_pass1_check_device_inode(fs, &inode))
1208                 problem = PR_2_BAD_SOCKET;
1209         else if (LINUX_S_ISLNK(inode.i_mode)
1210                  && !e2fsck_pass1_check_symlink(fs, &inode, buf)) {
1211                 problem = PR_2_INVALID_SYMLINK;
1212         }
1213
1214         if (problem) {
1215                 if (fix_problem(ctx, problem, &pctx)) {
1216                         deallocate_inode(ctx, ino, 0);
1217                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1218                                 return 0;
1219                         return 1;
1220                 }
1221                 problem = 0;
1222         }
1223                 
1224         if (inode.i_faddr &&
1225             fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1226                 inode.i_faddr = 0;
1227                 inode_modified++;
1228         }
1229
1230         switch (fs->super->s_creator_os) {
1231             case EXT2_OS_LINUX:
1232                 frag = &inode.osd2.linux2.l_i_frag;
1233                 fsize = &inode.osd2.linux2.l_i_fsize;
1234                 break;
1235             case EXT2_OS_HURD:
1236                 frag = &inode.osd2.hurd2.h_i_frag;
1237                 fsize = &inode.osd2.hurd2.h_i_fsize;
1238                 break;
1239             case EXT2_OS_MASIX:
1240                 frag = &inode.osd2.masix2.m_i_frag;
1241                 fsize = &inode.osd2.masix2.m_i_fsize;
1242                 break;
1243             default:
1244                 frag = fsize = 0;
1245         }
1246         if (frag && *frag) {
1247                 pctx.num = *frag;
1248                 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
1249                         *frag = 0;
1250                         inode_modified++;
1251                 }
1252                 pctx.num = 0;
1253         }
1254         if (fsize && *fsize) {
1255                 pctx.num = *fsize;
1256                 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
1257                         *fsize = 0;
1258                         inode_modified++;
1259                 }
1260                 pctx.num = 0;
1261         }
1262
1263         if (inode.i_file_acl &&
1264             !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) &&
1265             fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1266                 inode.i_file_acl = 0;
1267                 inode_modified++;
1268         }
1269         if (inode.i_file_acl &&
1270             ((inode.i_file_acl < fs->super->s_first_data_block) ||
1271              (inode.i_file_acl >= fs->super->s_blocks_count)) &&
1272             fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
1273                 inode.i_file_acl = 0;
1274                 inode_modified++;
1275         }
1276         if (inode.i_dir_acl &&
1277             LINUX_S_ISDIR(inode.i_mode) &&
1278             fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1279                 inode.i_dir_acl = 0;
1280                 inode_modified++;
1281         }
1282         if (inode_modified)
1283                 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
1284         return 0;
1285 }
1286
1287
1288 /*
1289  * allocate_dir_block --- this function allocates a new directory
1290  *      block for a particular inode; this is done if a directory has
1291  *      a "hole" in it, or if a directory has a illegal block number
1292  *      that was zeroed out and now needs to be replaced.
1293  */
1294 static int allocate_dir_block(e2fsck_t ctx,
1295                               struct ext2_db_entry *db,
1296                               char *buf EXT2FS_ATTR((unused)), 
1297                               struct problem_context *pctx)
1298 {
1299         ext2_filsys fs = ctx->fs;
1300         blk_t                   blk;
1301         char                    *block;
1302         struct ext2_inode       inode;
1303
1304         if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
1305                 return 1;
1306
1307         /*
1308          * Read the inode and block bitmaps in; we'll be messing with
1309          * them.
1310          */
1311         e2fsck_read_bitmaps(ctx);
1312         
1313         /*
1314          * First, find a free block
1315          */
1316         pctx->errcode = ext2fs_new_block(fs, 0, ctx->block_found_map, &blk);
1317         if (pctx->errcode) {
1318                 pctx->str = "ext2fs_new_block";
1319                 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1320                 return 1;
1321         }
1322         ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1323         ext2fs_mark_block_bitmap(fs->block_map, blk);
1324         ext2fs_mark_bb_dirty(fs);
1325
1326         /*
1327          * Now let's create the actual data block for the inode
1328          */
1329         if (db->blockcnt)
1330                 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
1331         else
1332                 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1333                                                      EXT2_ROOT_INO, &block);
1334
1335         if (pctx->errcode) {
1336                 pctx->str = "ext2fs_new_dir_block";
1337                 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1338                 return 1;
1339         }
1340
1341         pctx->errcode = ext2fs_write_dir_block(fs, blk, block);
1342         ext2fs_free_mem(&block);
1343         if (pctx->errcode) {
1344                 pctx->str = "ext2fs_write_dir_block";
1345                 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1346                 return 1;
1347         }
1348
1349         /*
1350          * Update the inode block count
1351          */
1352         e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1353         inode.i_blocks += fs->blocksize / 512;
1354         if (inode.i_size < (db->blockcnt+1) * fs->blocksize)
1355                 inode.i_size = (db->blockcnt+1) * fs->blocksize;
1356         e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
1357
1358         /*
1359          * Finally, update the block pointers for the inode
1360          */
1361         db->blk = blk;
1362         pctx->errcode = ext2fs_block_iterate2(fs, db->ino, BLOCK_FLAG_HOLE,
1363                                       0, update_dir_block, db);
1364         if (pctx->errcode) {
1365                 pctx->str = "ext2fs_block_iterate";
1366                 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1367                 return 1;
1368         }
1369
1370         return 0;
1371 }
1372
1373 /*
1374  * This is a helper function for allocate_dir_block().
1375  */
1376 static int update_dir_block(ext2_filsys fs EXT2FS_ATTR((unused)),
1377                             blk_t       *block_nr,
1378                             e2_blkcnt_t blockcnt,
1379                             blk_t ref_block EXT2FS_ATTR((unused)),
1380                             int ref_offset EXT2FS_ATTR((unused)), 
1381                             void *priv_data)
1382 {
1383         struct ext2_db_entry *db;
1384
1385         db = (struct ext2_db_entry *) priv_data;
1386         if (db->blockcnt == (int) blockcnt) {
1387                 *block_nr = db->blk;
1388                 return BLOCK_CHANGED;
1389         }
1390         return 0;
1391 }