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