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