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