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