Whamcloud - gitweb
e2fsck: read-ahead metadata during passes 1, 2, and 4
[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_block2(ext2_filsys fs,
65                            struct ext2_db_entry2 *dir_blocks_info,
66                            void *priv_data);
67 static int check_dir_block(ext2_filsys fs,
68                            struct ext2_db_entry2 *dir_blocks_info,
69                            void *priv_data);
70 static int allocate_dir_block(e2fsck_t ctx,
71                               struct ext2_db_entry2 *dir_blocks_info,
72                               char *buf, struct problem_context *pctx);
73 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
74 static int htree_depth(struct dx_dir_info *dx_dir,
75                        struct dx_dirblock_info *dx_db);
76 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
77
78 struct check_dir_struct {
79         char *buf;
80         struct problem_context  pctx;
81         int     count, max;
82         e2fsck_t ctx;
83         unsigned long long list_offset;
84         unsigned long long ra_entries;
85         unsigned long long next_ra_off;
86 };
87
88 void e2fsck_pass2(e2fsck_t ctx)
89 {
90         struct ext2_super_block *sb = ctx->fs->super;
91         struct problem_context  pctx;
92         ext2_filsys             fs = ctx->fs;
93         char                    *buf;
94 #ifdef RESOURCE_TRACK
95         struct resource_track   rtrack;
96 #endif
97         struct check_dir_struct cd;
98         struct dx_dir_info      *dx_dir;
99         struct dx_dirblock_info *dx_db, *dx_parent;
100         unsigned int            save_type;
101         int                     b;
102         int                     i, depth;
103         problem_t               code;
104         int                     bad_dir;
105         int (*check_dir_func)(ext2_filsys fs,
106                               struct ext2_db_entry2 *dir_blocks_info,
107                               void *priv_data);
108
109         init_resource_track(&rtrack, ctx->fs->io);
110         clear_problem_context(&cd.pctx);
111
112 #ifdef MTRACE
113         mtrace_print("Pass 2");
114 #endif
115
116         if (!(ctx->options & E2F_OPT_PREEN))
117                 fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
118
119         e2fsck_setup_tdb_icount(ctx, EXT2_ICOUNT_OPT_INCREMENT,
120                                 &ctx->inode_count);
121         if (ctx->inode_count)
122                 cd.pctx.errcode = 0;
123         else {
124                 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
125                                        "inode_count", &save_type);
126                 cd.pctx.errcode = ext2fs_create_icount2(fs,
127                                                 EXT2_ICOUNT_OPT_INCREMENT,
128                                                 0, ctx->inode_link_info,
129                                                 &ctx->inode_count);
130                 fs->default_bitmap_type = save_type;
131         }
132         if (cd.pctx.errcode) {
133                 fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
134                 ctx->flags |= E2F_FLAG_ABORT;
135                 return;
136         }
137         buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
138                                               "directory scan buffer");
139
140         /*
141          * Set up the parent pointer for the root directory, if
142          * present.  (If the root directory is not present, we will
143          * create it in pass 3.)
144          */
145         (void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
146
147         cd.buf = buf;
148         cd.ctx = ctx;
149         cd.count = 1;
150         cd.max = ext2fs_dblist_count2(fs->dblist);
151         cd.list_offset = 0;
152         cd.ra_entries = ctx->readahead_kb * 1024 / ctx->fs->blocksize;
153         cd.next_ra_off = 0;
154
155         if (ctx->progress)
156                 (void) (ctx->progress)(ctx, 2, 0, cd.max);
157
158         if (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX)
159                 ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp);
160
161         check_dir_func = cd.ra_entries ? check_dir_block2 : check_dir_block;
162         cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_func,
163                                                  &cd);
164         if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
165                 return;
166
167         if (ctx->flags & E2F_FLAG_RESTART_LATER) {
168                 ctx->flags |= E2F_FLAG_RESTART;
169                 return;
170         }
171
172         if (cd.pctx.errcode) {
173                 fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
174                 ctx->flags |= E2F_FLAG_ABORT;
175                 return;
176         }
177
178 #ifdef ENABLE_HTREE
179         for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
180                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
181                         return;
182                 if (e2fsck_dir_will_be_rehashed(ctx, dx_dir->ino) ||
183                     dx_dir->numblocks == 0)
184                         continue;
185                 clear_problem_context(&pctx);
186                 bad_dir = 0;
187                 pctx.dir = dx_dir->ino;
188                 dx_db = dx_dir->dx_block;
189                 if (dx_db->flags & DX_FLAG_REFERENCED)
190                         dx_db->flags |= DX_FLAG_DUP_REF;
191                 else
192                         dx_db->flags |= DX_FLAG_REFERENCED;
193                 /*
194                  * Find all of the first and last leaf blocks, and
195                  * update their parent's min and max hash values
196                  */
197                 for (b=0, dx_db = dx_dir->dx_block;
198                      b < dx_dir->numblocks;
199                      b++, dx_db++) {
200                         if ((dx_db->type != DX_DIRBLOCK_LEAF) ||
201                             !(dx_db->flags & (DX_FLAG_FIRST | DX_FLAG_LAST)))
202                                 continue;
203                         dx_parent = &dx_dir->dx_block[dx_db->parent];
204                         /*
205                          * XXX Make sure dx_parent->min_hash > dx_db->min_hash
206                          */
207                         if (dx_db->flags & DX_FLAG_FIRST)
208                                 dx_parent->min_hash = dx_db->min_hash;
209                         /*
210                          * XXX Make sure dx_parent->max_hash < dx_db->max_hash
211                          */
212                         if (dx_db->flags & DX_FLAG_LAST)
213                                 dx_parent->max_hash = dx_db->max_hash;
214                 }
215
216                 for (b=0, dx_db = dx_dir->dx_block;
217                      b < dx_dir->numblocks;
218                      b++, dx_db++) {
219                         pctx.blkcount = b;
220                         pctx.group = dx_db->parent;
221                         code = 0;
222                         if (!(dx_db->flags & DX_FLAG_FIRST) &&
223                             (dx_db->min_hash < dx_db->node_min_hash)) {
224                                 pctx.blk = dx_db->min_hash;
225                                 pctx.blk2 = dx_db->node_min_hash;
226                                 code = PR_2_HTREE_MIN_HASH;
227                                 fix_problem(ctx, code, &pctx);
228                                 bad_dir++;
229                         }
230                         if (dx_db->type == DX_DIRBLOCK_LEAF) {
231                                 depth = htree_depth(dx_dir, dx_db);
232                                 if (depth != dx_dir->depth) {
233                                         pctx.num = dx_dir->depth;
234                                         code = PR_2_HTREE_BAD_DEPTH;
235                                         fix_problem(ctx, code, &pctx);
236                                         bad_dir++;
237                                 }
238                         }
239                         /*
240                          * This test doesn't apply for the root block
241                          * at block #0
242                          */
243                         if (b &&
244                             (dx_db->max_hash > dx_db->node_max_hash)) {
245                                 pctx.blk = dx_db->max_hash;
246                                 pctx.blk2 = dx_db->node_max_hash;
247                                 code = PR_2_HTREE_MAX_HASH;
248                                 fix_problem(ctx, code, &pctx);
249                                 bad_dir++;
250                         }
251                         if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
252                                 code = PR_2_HTREE_NOTREF;
253                                 fix_problem(ctx, code, &pctx);
254                                 bad_dir++;
255                         } else if (dx_db->flags & DX_FLAG_DUP_REF) {
256                                 code = PR_2_HTREE_DUPREF;
257                                 fix_problem(ctx, code, &pctx);
258                                 bad_dir++;
259                         }
260                 }
261                 if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
262                         clear_htree(ctx, dx_dir->ino);
263                         dx_dir->numblocks = 0;
264                 }
265         }
266         e2fsck_free_dx_dir_info(ctx);
267 #endif
268         ext2fs_free_mem(&buf);
269         ext2fs_free_dblist(fs->dblist);
270
271         if (ctx->inode_bad_map) {
272                 ext2fs_free_inode_bitmap(ctx->inode_bad_map);
273                 ctx->inode_bad_map = 0;
274         }
275         if (ctx->inode_reg_map) {
276                 ext2fs_free_inode_bitmap(ctx->inode_reg_map);
277                 ctx->inode_reg_map = 0;
278         }
279         if (ctx->encrypted_dirs) {
280                 ext2fs_u32_list_free(ctx->encrypted_dirs);
281                 ctx->encrypted_dirs = 0;
282         }
283
284         clear_problem_context(&pctx);
285         if (ctx->large_files) {
286                 if (!(sb->s_feature_ro_compat &
287                       EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
288                     fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
289                         sb->s_feature_ro_compat |=
290                                 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
291                         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
292                         ext2fs_mark_super_dirty(fs);
293                 }
294                 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
295                     fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
296                         ext2fs_update_dynamic_rev(fs);
297                         ext2fs_mark_super_dirty(fs);
298                 }
299         }
300
301         print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io);
302 }
303
304 #define MAX_DEPTH 32000
305 static int htree_depth(struct dx_dir_info *dx_dir,
306                        struct dx_dirblock_info *dx_db)
307 {
308         int     depth = 0;
309
310         while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
311                 dx_db = &dx_dir->dx_block[dx_db->parent];
312                 depth++;
313         }
314         return depth;
315 }
316
317 static int dict_de_cmp(const void *a, const void *b)
318 {
319         const struct ext2_dir_entry *de_a, *de_b;
320         int     a_len, b_len;
321
322         de_a = (const struct ext2_dir_entry *) a;
323         a_len = ext2fs_dirent_name_len(de_a);
324         de_b = (const struct ext2_dir_entry *) b;
325         b_len = ext2fs_dirent_name_len(de_b);
326
327         if (a_len != b_len)
328                 return (a_len - b_len);
329
330         return memcmp(de_a->name, de_b->name, a_len);
331 }
332
333 /*
334  * This is special sort function that makes sure that directory blocks
335  * with a dirblock of zero are sorted to the beginning of the list.
336  * This guarantees that the root node of the htree directories are
337  * processed first, so we know what hash version to use.
338  */
339 static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
340 {
341         const struct ext2_db_entry2 *db_a =
342                 (const struct ext2_db_entry2 *) a;
343         const struct ext2_db_entry2 *db_b =
344                 (const struct ext2_db_entry2 *) b;
345
346         if (db_a->blockcnt && !db_b->blockcnt)
347                 return 1;
348
349         if (!db_a->blockcnt && db_b->blockcnt)
350                 return -1;
351
352         if (db_a->blk != db_b->blk)
353                 return (int) (db_a->blk - db_b->blk);
354
355         if (db_a->ino != db_b->ino)
356                 return (int) (db_a->ino - db_b->ino);
357
358         return (int) (db_a->blockcnt - db_b->blockcnt);
359 }
360
361
362 /*
363  * Make sure the first entry in the directory is '.', and that the
364  * directory entry is sane.
365  */
366 static int check_dot(e2fsck_t ctx,
367                      struct ext2_dir_entry *dirent,
368                      ext2_ino_t ino, struct problem_context *pctx)
369 {
370         struct ext2_dir_entry *nextdir;
371         unsigned int    rec_len, new_len;
372         int             status = 0;
373         int             created = 0;
374         problem_t       problem = 0;
375
376         if (!dirent->inode)
377                 problem = PR_2_MISSING_DOT;
378         else if ((ext2fs_dirent_name_len(dirent) != 1) ||
379                  (dirent->name[0] != '.'))
380                 problem = PR_2_1ST_NOT_DOT;
381         else if (dirent->name[1] != '\0')
382                 problem = PR_2_DOT_NULL_TERM;
383
384         (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
385         if (problem) {
386                 if (fix_problem(ctx, problem, pctx)) {
387                         if (rec_len < 12)
388                                 rec_len = dirent->rec_len = 12;
389                         dirent->inode = ino;
390                         ext2fs_dirent_set_name_len(dirent, 1);
391                         ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
392                         dirent->name[0] = '.';
393                         dirent->name[1] = '\0';
394                         status = 1;
395                         created = 1;
396                 }
397         }
398         if (dirent->inode != ino) {
399                 if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
400                         dirent->inode = ino;
401                         status = 1;
402                 }
403         }
404         if (rec_len > 12) {
405                 new_len = rec_len - 12;
406                 if (new_len > 12) {
407                         if (created ||
408                             fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
409                                 nextdir = (struct ext2_dir_entry *)
410                                         ((char *) dirent + 12);
411                                 dirent->rec_len = 12;
412                                 (void) ext2fs_set_rec_len(ctx->fs, new_len,
413                                                           nextdir);
414                                 nextdir->inode = 0;
415                                 ext2fs_dirent_set_name_len(nextdir, 0);
416                                 ext2fs_dirent_set_file_type(nextdir,
417                                                             EXT2_FT_UNKNOWN);
418                                 status = 1;
419                         }
420                 }
421         }
422         return status;
423 }
424
425 /*
426  * Make sure the second entry in the directory is '..', and that the
427  * directory entry is sane.  We do not check the inode number of '..'
428  * here; this gets done in pass 3.
429  */
430 static int check_dotdot(e2fsck_t ctx,
431                         struct ext2_dir_entry *dirent,
432                         ext2_ino_t ino, struct problem_context *pctx)
433 {
434         problem_t       problem = 0;
435         unsigned int    rec_len;
436
437         if (!dirent->inode)
438                 problem = PR_2_MISSING_DOT_DOT;
439         else if ((ext2fs_dirent_name_len(dirent) != 2) ||
440                  (dirent->name[0] != '.') ||
441                  (dirent->name[1] != '.'))
442                 problem = PR_2_2ND_NOT_DOT_DOT;
443         else if (dirent->name[2] != '\0')
444                 problem = PR_2_DOT_DOT_NULL_TERM;
445
446         (void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
447         if (problem) {
448                 if (fix_problem(ctx, problem, pctx)) {
449                         if (rec_len < 12)
450                                 dirent->rec_len = 12;
451                         /*
452                          * Note: we don't have the parent inode just
453                          * yet, so we will fill it in with the root
454                          * inode.  This will get fixed in pass 3.
455                          */
456                         dirent->inode = EXT2_ROOT_INO;
457                         ext2fs_dirent_set_name_len(dirent, 2);
458                         ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
459                         dirent->name[0] = '.';
460                         dirent->name[1] = '.';
461                         dirent->name[2] = '\0';
462                         return 1;
463                 }
464                 return 0;
465         }
466         if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
467                 fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
468                 return -1;
469         }
470         return 0;
471 }
472
473 /*
474  * Check to make sure a directory entry doesn't contain any illegal
475  * characters.
476  */
477 static int check_name(e2fsck_t ctx,
478                       struct ext2_dir_entry *dirent,
479                       ext2_ino_t dir_ino,
480                       struct problem_context *pctx)
481 {
482         int     i;
483         int     fixup = -1;
484         int     ret = 0;
485
486         for ( i = 0; i < ext2fs_dirent_name_len(dirent); i++) {
487                 if (dirent->name[i] != '/' && dirent->name[i] != '\0')
488                         continue;
489                 if (fixup < 0)
490                         fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
491                 if (fixup == 0)
492                         return 0;
493                 dirent->name[i] = '.';
494                 ret = 1;
495         }
496         return ret;
497 }
498
499 /*
500  * Check the directory filetype (if present)
501  */
502 static _INLINE_ int check_filetype(e2fsck_t ctx,
503                                    struct ext2_dir_entry *dirent,
504                                    ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
505                                    struct problem_context *pctx)
506 {
507         int     filetype = ext2fs_dirent_file_type(dirent);
508         int     should_be = EXT2_FT_UNKNOWN;
509         struct ext2_inode       inode;
510
511         if (!(ctx->fs->super->s_feature_incompat &
512               EXT2_FEATURE_INCOMPAT_FILETYPE)) {
513                 if (filetype == 0 ||
514                     !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
515                         return 0;
516                 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
517                 return 1;
518         }
519
520         if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) {
521                 should_be = EXT2_FT_DIR;
522         } else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map,
523                                             dirent->inode)) {
524                 should_be = EXT2_FT_REG_FILE;
525         } else if (ctx->inode_bad_map &&
526                    ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
527                                             dirent->inode))
528                 should_be = 0;
529         else {
530                 e2fsck_read_inode(ctx, dirent->inode, &inode,
531                                   "check_filetype");
532                 should_be = ext2_file_type(inode.i_mode);
533         }
534         if (filetype == should_be)
535                 return 0;
536         pctx->num = should_be;
537
538         if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
539                         pctx) == 0)
540                 return 0;
541
542         ext2fs_dirent_set_file_type(dirent, should_be);
543         return 1;
544 }
545
546 #ifdef ENABLE_HTREE
547 static void parse_int_node(ext2_filsys fs,
548                            struct ext2_db_entry2 *db,
549                            struct check_dir_struct *cd,
550                            struct dx_dir_info   *dx_dir,
551                            char *block_buf, int failed_csum)
552 {
553         struct          ext2_dx_root_info  *root;
554         struct          ext2_dx_entry *ent;
555         struct          ext2_dx_countlimit *limit;
556         struct dx_dirblock_info *dx_db;
557         int             i, expect_limit, count;
558         blk_t           blk;
559         ext2_dirhash_t  min_hash = 0xffffffff;
560         ext2_dirhash_t  max_hash = 0;
561         ext2_dirhash_t  hash = 0, prev_hash;
562         int             csum_size = 0;
563
564         if (db->blockcnt == 0) {
565                 root = (struct ext2_dx_root_info *) (block_buf + 24);
566
567 #ifdef DX_DEBUG
568                 printf("Root node dump:\n");
569                 printf("\t Reserved zero: %u\n", root->reserved_zero);
570                 printf("\t Hash Version: %d\n", root->hash_version);
571                 printf("\t Info length: %d\n", root->info_length);
572                 printf("\t Indirect levels: %d\n", root->indirect_levels);
573                 printf("\t Flags: %d\n", root->unused_flags);
574 #endif
575
576                 ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
577
578                 if (failed_csum &&
579                     (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
580                      fix_problem(cd->ctx, PR_2_HTREE_ROOT_CSUM_INVALID,
581                                 &cd->pctx)))
582                         goto clear_and_exit;
583         } else {
584                 ent = (struct ext2_dx_entry *) (block_buf+8);
585
586                 if (failed_csum &&
587                     (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
588                      fix_problem(cd->ctx, PR_2_HTREE_NODE_CSUM_INVALID,
589                                 &cd->pctx)))
590                         goto clear_and_exit;
591         }
592
593         limit = (struct ext2_dx_countlimit *) ent;
594
595 #ifdef DX_DEBUG
596         printf("Number of entries (count): %d\n",
597                ext2fs_le16_to_cpu(limit->count));
598         printf("Number of entries (limit): %d\n",
599                ext2fs_le16_to_cpu(limit->limit));
600 #endif
601
602         count = ext2fs_le16_to_cpu(limit->count);
603         if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
604                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
605                 csum_size = sizeof(struct ext2_dx_tail);
606         expect_limit = (fs->blocksize -
607                         (csum_size + ((char *) ent - block_buf))) /
608                        sizeof(struct ext2_dx_entry);
609         if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
610                 cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
611                 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
612                         goto clear_and_exit;
613         }
614         if (count > expect_limit) {
615                 cd->pctx.num = count;
616                 if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
617                         goto clear_and_exit;
618                 count = expect_limit;
619         }
620
621         for (i=0; i < count; i++) {
622                 prev_hash = hash;
623                 hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
624 #ifdef DX_DEBUG
625                 printf("Entry #%d: Hash 0x%08x, block %u\n", i,
626                        hash, ext2fs_le32_to_cpu(ent[i].block));
627 #endif
628                 blk = ext2fs_le32_to_cpu(ent[i].block) & 0x0ffffff;
629                 /* Check to make sure the block is valid */
630                 if (blk >= (blk_t) dx_dir->numblocks) {
631                         cd->pctx.blk = blk;
632                         if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
633                                         &cd->pctx))
634                                 goto clear_and_exit;
635                         continue;
636                 }
637                 if (hash < prev_hash &&
638                     fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
639                         goto clear_and_exit;
640                 dx_db = &dx_dir->dx_block[blk];
641                 if (dx_db->flags & DX_FLAG_REFERENCED) {
642                         dx_db->flags |= DX_FLAG_DUP_REF;
643                 } else {
644                         dx_db->flags |= DX_FLAG_REFERENCED;
645                         dx_db->parent = db->blockcnt;
646                 }
647                 if (hash < min_hash)
648                         min_hash = hash;
649                 if (hash > max_hash)
650                         max_hash = hash;
651                 dx_db->node_min_hash = hash;
652                 if ((i+1) < count)
653                         dx_db->node_max_hash =
654                           ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
655                 else {
656                         dx_db->node_max_hash = 0xfffffffe;
657                         dx_db->flags |= DX_FLAG_LAST;
658                 }
659                 if (i == 0)
660                         dx_db->flags |= DX_FLAG_FIRST;
661         }
662 #ifdef DX_DEBUG
663         printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
664                db->blockcnt, min_hash, max_hash);
665 #endif
666         dx_db = &dx_dir->dx_block[db->blockcnt];
667         dx_db->min_hash = min_hash;
668         dx_db->max_hash = max_hash;
669         return;
670
671 clear_and_exit:
672         clear_htree(cd->ctx, cd->pctx.ino);
673         dx_dir->numblocks = 0;
674         e2fsck_rehash_dir_later(cd->ctx, cd->pctx.ino);
675 }
676 #endif /* ENABLE_HTREE */
677
678 /*
679  * Given a busted directory, try to salvage it somehow.
680  *
681  */
682 static void salvage_directory(ext2_filsys fs,
683                               struct ext2_dir_entry *dirent,
684                               struct ext2_dir_entry *prev,
685                               unsigned int *offset,
686                               unsigned int block_len)
687 {
688         char    *cp = (char *) dirent;
689         int left;
690         unsigned int rec_len, prev_rec_len;
691         unsigned int name_len;
692
693         /*
694          * If the space left for the entry is too small to be an entry,
695          * we can't access dirent's fields, so plumb in the values needed
696          * so that the previous entry absorbs this one.
697          */
698         if (block_len - *offset < EXT2_DIR_ENTRY_HEADER_LEN) {
699                 name_len = 0;
700                 rec_len = block_len - *offset;
701         } else {
702                 name_len = ext2fs_dirent_name_len(dirent);
703                 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
704         }
705         left = block_len - *offset - rec_len;
706
707         /*
708          * Special case of directory entry of size 8: copy what's left
709          * of the directory block up to cover up the invalid hole.
710          */
711         if ((left >= 12) && (rec_len == EXT2_DIR_ENTRY_HEADER_LEN)) {
712                 memmove(cp, cp+EXT2_DIR_ENTRY_HEADER_LEN, left);
713                 memset(cp + left, 0, EXT2_DIR_ENTRY_HEADER_LEN);
714                 return;
715         }
716         /*
717          * If the directory entry overruns the end of the directory
718          * block, and the name is small enough to fit, then adjust the
719          * record length.
720          */
721         if ((left < 0) &&
722             ((int) rec_len + left > EXT2_DIR_ENTRY_HEADER_LEN) &&
723             ((int) name_len + EXT2_DIR_ENTRY_HEADER_LEN <= (int) rec_len + left) &&
724             dirent->inode <= fs->super->s_inodes_count &&
725             strnlen(dirent->name, name_len) == name_len) {
726                 (void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
727                 return;
728         }
729         /*
730          * If the record length of the directory entry is a multiple
731          * of four, and not too big, such that it is valid, let the
732          * previous directory entry absorb the invalid one.
733          */
734         if (prev && rec_len && (rec_len % 4) == 0 &&
735             (*offset + rec_len <= block_len)) {
736                 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
737                 prev_rec_len += rec_len;
738                 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
739                 *offset += rec_len;
740                 return;
741         }
742         /*
743          * Default salvage method --- kill all of the directory
744          * entries for the rest of the block.  We will either try to
745          * absorb it into the previous directory entry, or create a
746          * new empty directory entry the rest of the directory block.
747          */
748         if (prev) {
749                 (void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
750                 prev_rec_len += block_len - *offset;
751                 (void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
752                 *offset = fs->blocksize;
753         } else {
754                 rec_len = block_len - *offset;
755                 (void) ext2fs_set_rec_len(fs, rec_len, dirent);
756                 ext2fs_dirent_set_name_len(dirent, 0);
757                 ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
758                 dirent->inode = 0;
759         }
760 }
761
762 #define NEXT_DIRENT(d)  ((void *)((char *)(d) + (d)->rec_len))
763 static errcode_t insert_dirent_tail(ext2_filsys fs, void *dirbuf)
764 {
765         struct ext2_dir_entry *d;
766         void *top;
767         struct ext2_dir_entry_tail *t;
768
769         d = dirbuf;
770         top = EXT2_DIRENT_TAIL(dirbuf, fs->blocksize);
771
772         while (d->rec_len && !(d->rec_len & 0x3) && NEXT_DIRENT(d) <= top)
773                 d = NEXT_DIRENT(d);
774
775         if (d != top) {
776                 size_t min_size = EXT2_DIR_REC_LEN(
777                                 ext2fs_dirent_name_len(dirbuf));
778                 if (min_size > top - (void *)d)
779                         return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
780                 d->rec_len = top - (void *)d;
781         }
782
783         t = (struct ext2_dir_entry_tail *)top;
784         if (t->det_reserved_zero1 ||
785             t->det_rec_len != sizeof(struct ext2_dir_entry_tail) ||
786             t->det_reserved_name_len != EXT2_DIR_NAME_LEN_CSUM)
787                 ext2fs_initialize_dirent_tail(fs, t);
788
789         return 0;
790 }
791 #undef NEXT_DIRENT
792
793 static errcode_t fix_inline_dir_size(e2fsck_t ctx, ext2_ino_t ino,
794                                      size_t *inline_data_size,
795                                      struct problem_context *pctx,
796                                      char *buf)
797 {
798         ext2_filsys fs = ctx->fs;
799         struct ext2_inode inode;
800         size_t new_size, old_size;
801         errcode_t retval;
802
803         old_size = *inline_data_size;
804         /*
805          * If there's not enough bytes to start the "second" dir block
806          * (in the EA space) then truncate everything to the first block.
807          */
808         if (old_size > EXT4_MIN_INLINE_DATA_SIZE &&
809             old_size < EXT4_MIN_INLINE_DATA_SIZE +
810                        EXT2_DIR_REC_LEN(1)) {
811                 old_size = EXT4_MIN_INLINE_DATA_SIZE;
812                 new_size = old_size;
813         } else
814                 /* Increase to the next four-byte boundary for salvaging */
815                 new_size = old_size + (4 - (old_size & 3));
816         memset(buf + old_size, 0, new_size - old_size);
817         retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
818         if (retval == EXT2_ET_INLINE_DATA_NO_SPACE) {
819                 /* Or we can't, so truncate. */
820                 new_size -= 4;
821                 retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
822                 if (retval) {
823                         if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
824                                         pctx)) {
825                                 new_size = 0;
826                                 goto write_inode;
827                         }
828                         goto err;
829                 }
830         } else if (retval) {
831                 if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
832                                 pctx)) {
833                         new_size = 0;
834                         goto write_inode;
835                 }
836                 goto err;
837         }
838
839 write_inode:
840         retval = ext2fs_read_inode(fs, ino, &inode);
841         if (retval)
842                 goto err;
843
844         retval = ext2fs_inode_size_set(fs, &inode, new_size);
845         if (retval)
846                 goto err;
847         if (new_size == 0)
848                 inode.i_flags &= ~EXT4_INLINE_DATA_FL;
849         retval = ext2fs_write_inode(fs, ino, &inode);
850         if (retval)
851                 goto err;
852         *inline_data_size = new_size;
853
854 err:
855         return retval;
856 }
857
858 int get_filename_hash(ext2_filsys fs, int encrypted, int version,
859                       const char *name, int len, ext2_dirhash_t *ret_hash,
860                       ext2_dirhash_t *ret_minor_hash)
861 {
862         char    buf[2*EXT2FS_DIGEST_SIZE];
863         int     buf_len;
864
865         if (!encrypted)
866                 return ext2fs_dirhash(version, name, len,
867                                       fs->super->s_hash_seed,
868                                       ret_hash, ret_minor_hash);
869
870         if (len <= EXT2FS_DIGEST_SIZE)
871                 buf_len = ext2fs_digest_encode(name, len, buf);
872         else {
873                 ext2fs_sha256(name, len, buf + EXT2FS_DIGEST_SIZE);
874                 buf[0] = 'I';
875                 buf_len = ext2fs_digest_encode(buf + EXT2FS_DIGEST_SIZE,
876                                                EXT2FS_DIGEST_SIZE, buf + 1);
877                 buf_len++;
878         }
879         return ext2fs_dirhash(version, buf, buf_len,
880                               fs->super->s_hash_seed,
881                               ret_hash, ret_minor_hash);
882 }
883
884 static int check_dir_block2(ext2_filsys fs,
885                            struct ext2_db_entry2 *db,
886                            void *priv_data)
887 {
888         int err;
889         struct check_dir_struct *cd = priv_data;
890
891         if (cd->ra_entries && cd->list_offset >= cd->next_ra_off) {
892                 err = e2fsck_readahead_dblist(fs,
893                                         E2FSCK_RA_DBLIST_IGNORE_BLOCKCNT,
894                                         fs->dblist,
895                                         cd->list_offset + cd->ra_entries / 8,
896                                         cd->ra_entries);
897                 if (err)
898                         cd->ra_entries = 0;
899                 cd->next_ra_off = cd->list_offset + (cd->ra_entries * 7 / 8);
900         }
901
902         err = check_dir_block(fs, db, priv_data);
903         cd->list_offset++;
904         return err;
905 }
906
907 static int check_dir_block(ext2_filsys fs,
908                            struct ext2_db_entry2 *db,
909                            void *priv_data)
910 {
911         struct dx_dir_info      *dx_dir;
912 #ifdef ENABLE_HTREE
913         struct dx_dirblock_info *dx_db = 0;
914 #endif /* ENABLE_HTREE */
915         struct ext2_dir_entry   *dirent, *prev, dot, dotdot;
916         ext2_dirhash_t          hash;
917         unsigned int            offset = 0;
918         int                     dir_modified = 0;
919         int                     dot_state;
920         unsigned int            rec_len;
921         blk64_t                 block_nr = db->blk;
922         ext2_ino_t              ino = db->ino;
923         ext2_ino_t              subdir_parent;
924         __u16                   links;
925         struct check_dir_struct *cd;
926         char                    *buf, *ibuf;
927         e2fsck_t                ctx;
928         problem_t               problem;
929         struct ext2_dx_root_info *root;
930         struct ext2_dx_countlimit *limit;
931         static dict_t de_dict;
932         struct problem_context  pctx;
933         int     dups_found = 0;
934         int     ret;
935         int     dx_csum_size = 0, de_csum_size = 0;
936         int     failed_csum = 0;
937         int     is_leaf = 1;
938         size_t  inline_data_size = 0;
939         int     filetype = 0;
940         int     encrypted = 0;
941         size_t  max_block_size;
942
943         cd = (struct check_dir_struct *) priv_data;
944         ibuf = buf = cd->buf;
945         ctx = cd->ctx;
946
947         if (ctx->flags & E2F_FLAG_SIGNAL_MASK || ctx->flags & E2F_FLAG_RESTART)
948                 return DIRENT_ABORT;
949
950         if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
951                 return DIRENT_ABORT;
952
953         if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
954                                        EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
955                 dx_csum_size = sizeof(struct ext2_dx_tail);
956                 de_csum_size = sizeof(struct ext2_dir_entry_tail);
957         }
958
959         if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
960                                       EXT2_FEATURE_INCOMPAT_FILETYPE))
961                 filetype = EXT2_FT_DIR << 8;
962
963         /*
964          * Make sure the inode is still in use (could have been
965          * deleted in the duplicate/bad blocks pass.
966          */
967         if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
968                 return 0;
969
970         cd->pctx.ino = ino;
971         cd->pctx.blk = block_nr;
972         cd->pctx.blkcount = db->blockcnt;
973         cd->pctx.ino2 = 0;
974         cd->pctx.dirent = 0;
975         cd->pctx.num = 0;
976
977         if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
978                                       EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
979                 errcode_t ec;
980
981                 ec = ext2fs_inline_data_size(fs, ino, &inline_data_size);
982                 if (ec && ec != EXT2_ET_NO_INLINE_DATA)
983                         return DIRENT_ABORT;
984         }
985
986         if (db->blk == 0 && !inline_data_size) {
987                 if (allocate_dir_block(ctx, db, buf, &cd->pctx))
988                         return 0;
989                 block_nr = db->blk;
990         }
991
992         if (db->blockcnt)
993                 dot_state = 2;
994         else
995                 dot_state = 0;
996
997         if (ctx->dirs_to_hash &&
998             ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
999                 dups_found++;
1000
1001 #if 0
1002         printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
1003                db->blockcnt, ino);
1004 #endif
1005
1006         ehandler_operation(_("reading directory block"));
1007         if (inline_data_size) {
1008                 memset(buf, 0, fs->blocksize - inline_data_size);
1009                 cd->pctx.errcode = ext2fs_inline_data_get(fs, ino, 0, buf, 0);
1010                 if (cd->pctx.errcode)
1011                         goto inline_read_fail;
1012 #ifdef WORDS_BIGENDIAN
1013                 if (db->blockcnt)
1014                         goto skip_first_read_swab;
1015                 *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
1016                 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
1017                                 buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
1018                                 EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DATA_DOTDOT_SIZE,
1019                                 0);
1020                 if (cd->pctx.errcode)
1021                         goto inline_read_fail;
1022 skip_first_read_swab:
1023                 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
1024                     !db->blockcnt)
1025                         goto inline_read_fail;
1026                 cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
1027                                 buf + EXT4_MIN_INLINE_DATA_SIZE,
1028                                 inline_data_size - EXT4_MIN_INLINE_DATA_SIZE,
1029                                 0);
1030 #endif
1031         } else
1032                 cd->pctx.errcode = ext2fs_read_dir_block4(fs, block_nr,
1033                                                           buf, 0, ino);
1034 inline_read_fail:
1035         pctx.ino = ino;
1036         pctx.num = inline_data_size;
1037         if (((inline_data_size & 3) ||
1038              (inline_data_size > EXT4_MIN_INLINE_DATA_SIZE &&
1039               inline_data_size < EXT4_MIN_INLINE_DATA_SIZE +
1040                                  EXT2_DIR_REC_LEN(1))) &&
1041             fix_problem(ctx, PR_2_BAD_INLINE_DIR_SIZE, &pctx)) {
1042                 errcode_t err = fix_inline_dir_size(ctx, ino,
1043                                                     &inline_data_size, &pctx,
1044                                                     buf);
1045                 if (err)
1046                         return DIRENT_ABORT;
1047
1048         }
1049         ehandler_operation(0);
1050         if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
1051                 cd->pctx.errcode = 0; /* We'll handle this ourselves */
1052         else if (cd->pctx.errcode == EXT2_ET_DIR_CSUM_INVALID) {
1053                 cd->pctx.errcode = 0; /* We'll handle this ourselves */
1054                 failed_csum = 1;
1055         }
1056         if (cd->pctx.errcode) {
1057                 char *buf2;
1058                 if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
1059                         ctx->flags |= E2F_FLAG_ABORT;
1060                         return DIRENT_ABORT;
1061                 }
1062                 ext2fs_new_dir_block(fs, db->blockcnt == 0 ? ino : 0,
1063                                      EXT2_ROOT_INO, &buf2);
1064                 memcpy(buf, buf2, fs->blocksize);
1065                 ext2fs_free_mem(&buf2);
1066         }
1067 #ifdef ENABLE_HTREE
1068         dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
1069         if (dx_dir && dx_dir->numblocks) {
1070                 if (db->blockcnt >= dx_dir->numblocks) {
1071                         pctx.dir = ino;
1072                         if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
1073                                         &pctx)) {
1074                                 clear_htree(ctx, ino);
1075                                 dx_dir->numblocks = 0;
1076                                 dx_db = 0;
1077                                 goto out_htree;
1078                         }
1079                         fatal_error(ctx, _("Can not continue."));
1080                 }
1081                 dx_db = &dx_dir->dx_block[db->blockcnt];
1082                 dx_db->type = DX_DIRBLOCK_LEAF;
1083                 dx_db->phys = block_nr;
1084                 dx_db->min_hash = ~0;
1085                 dx_db->max_hash = 0;
1086
1087                 dirent = (struct ext2_dir_entry *) buf;
1088                 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1089                 limit = (struct ext2_dx_countlimit *) (buf+8);
1090                 if (db->blockcnt == 0) {
1091                         root = (struct ext2_dx_root_info *) (buf + 24);
1092                         dx_db->type = DX_DIRBLOCK_ROOT;
1093                         dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
1094                         if ((root->reserved_zero ||
1095                              root->info_length < 8 ||
1096                              root->indirect_levels > 1) &&
1097                             fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
1098                                 clear_htree(ctx, ino);
1099                                 dx_dir->numblocks = 0;
1100                                 dx_db = 0;
1101                         }
1102                         dx_dir->hashversion = root->hash_version;
1103                         if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
1104                             (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
1105                                 dx_dir->hashversion += 3;
1106                         dx_dir->depth = root->indirect_levels + 1;
1107                 } else if ((dirent->inode == 0) &&
1108                            (rec_len == fs->blocksize) &&
1109                            (ext2fs_dirent_name_len(dirent) == 0) &&
1110                            (ext2fs_le16_to_cpu(limit->limit) ==
1111                             ((fs->blocksize - (8 + dx_csum_size)) /
1112                              sizeof(struct ext2_dx_entry))))
1113                         dx_db->type = DX_DIRBLOCK_NODE;
1114                 is_leaf = 0;
1115         }
1116 out_htree:
1117 #endif /* ENABLE_HTREE */
1118
1119         /* Leaf node with no space for csum?  Rebuild dirs in pass 3A. */
1120         if (is_leaf && !inline_data_size && failed_csum &&
1121             !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1122                 de_csum_size = 0;
1123                 if (e2fsck_dir_will_be_rehashed(ctx, ino)) {
1124                         failed_csum = 0;
1125                         goto skip_checksum;
1126                 }
1127                 if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_MISSING_CSUM,
1128                                  &cd->pctx))
1129                         goto skip_checksum;
1130                 e2fsck_rehash_dir_later(ctx, ino);
1131                 failed_csum = 0;
1132                 goto skip_checksum;
1133         }
1134         /* htree nodes don't use fake dirents to store checksums */
1135         if (!is_leaf)
1136                 de_csum_size = 0;
1137
1138 skip_checksum:
1139         if (inline_data_size) {
1140                 if (db->blockcnt) {
1141                         buf += EXT4_MIN_INLINE_DATA_SIZE;
1142                         max_block_size = inline_data_size - EXT4_MIN_INLINE_DATA_SIZE;
1143                         /* Zero-length second block, just exit */
1144                         if (max_block_size == 0)
1145                                 return 0;
1146                 } else {
1147                         max_block_size = EXT4_MIN_INLINE_DATA_SIZE;
1148                 }
1149         } else
1150                 max_block_size = fs->blocksize - de_csum_size;
1151
1152         if (ctx->encrypted_dirs)
1153                 encrypted = ext2fs_u32_list_test(ctx->encrypted_dirs, ino);
1154
1155         dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
1156         prev = 0;
1157         do {
1158                 dgrp_t group;
1159                 ext2_ino_t first_unused_inode;
1160                 unsigned int name_len;
1161
1162                 problem = 0;
1163                 if (!inline_data_size || dot_state > 1) {
1164                         dirent = (struct ext2_dir_entry *) (buf + offset);
1165                         /*
1166                          * If there's not even space for the entry header,
1167                          * force salvaging this dir.
1168                          */
1169                         if (max_block_size - offset < EXT2_DIR_ENTRY_HEADER_LEN)
1170                                 rec_len = EXT2_DIR_REC_LEN(1);
1171                         else
1172                                 (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1173                         cd->pctx.dirent = dirent;
1174                         cd->pctx.num = offset;
1175                         if ((offset + rec_len > max_block_size) ||
1176                             (rec_len < 12) ||
1177                             ((rec_len % 4) != 0) ||
1178                             ((ext2fs_dirent_name_len(dirent) + EXT2_DIR_ENTRY_HEADER_LEN) > rec_len)) {
1179                                 if (fix_problem(ctx, PR_2_DIR_CORRUPTED,
1180                                                 &cd->pctx)) {
1181 #ifdef WORDS_BIGENDIAN
1182                                         /*
1183                                          * On big-endian systems, if the dirent
1184                                          * swap routine finds a rec_len that it
1185                                          * doesn't like, it continues
1186                                          * processing the block as if rec_len
1187                                          * == EXT2_DIR_ENTRY_HEADER_LEN.  This means that the name
1188                                          * field gets byte swapped, which means
1189                                          * that salvage will not detect the
1190                                          * correct name length (unless the name
1191                                          * has a length that's an exact
1192                                          * multiple of four bytes), and it'll
1193                                          * discard the entry (unnecessarily)
1194                                          * and the rest of the dirent block.
1195                                          * Therefore, swap the rest of the
1196                                          * block back to disk order, run
1197                                          * salvage, and re-swap anything after
1198                                          * the salvaged dirent.
1199                                          */
1200                                         int need_reswab = 0;
1201                                         if (rec_len < EXT2_DIR_ENTRY_HEADER_LEN || rec_len % 4) {
1202                                                 need_reswab = 1;
1203                                                 ext2fs_dirent_swab_in2(fs,
1204                                                         ((char *)dirent) + EXT2_DIR_ENTRY_HEADER_LEN,
1205                                                         max_block_size - offset - EXT2_DIR_ENTRY_HEADER_LEN,
1206                                                         0);
1207                                         }
1208 #endif
1209                                         salvage_directory(fs, dirent, prev,
1210                                                           &offset,
1211                                                           max_block_size);
1212 #ifdef WORDS_BIGENDIAN
1213                                         if (need_reswab) {
1214                                                 (void) ext2fs_get_rec_len(fs,
1215                                                         dirent, &rec_len);
1216                                                 ext2fs_dirent_swab_in2(fs,
1217                                                         ((char *)dirent) + offset + rec_len,
1218                                                         max_block_size - offset - rec_len,
1219                                                         0);
1220                                         }
1221 #endif
1222                                         dir_modified++;
1223                                         continue;
1224                                 } else
1225                                         goto abort_free_dict;
1226                         }
1227                 } else {
1228                         if (dot_state == 0) {
1229                                 memset(&dot, 0, sizeof(dot));
1230                                 dirent = &dot;
1231                                 dirent->inode = ino;
1232                                 dirent->rec_len = EXT2_DIR_REC_LEN(1);
1233                                 dirent->name_len = 1 | filetype;
1234                                 dirent->name[0] = '.';
1235                         } else if (dot_state == 1) {
1236                                 memset(&dotdot, 0, sizeof(dotdot));
1237                                 dirent = &dotdot;
1238                                 dirent->inode =
1239                                         ((struct ext2_dir_entry *)buf)->inode;
1240                                 dirent->rec_len = EXT2_DIR_REC_LEN(2);
1241                                 dirent->name_len = 2 | filetype;
1242                                 dirent->name[0] = '.';
1243                                 dirent->name[1] = '.';
1244                         } else {
1245                                 fatal_error(ctx, _("Can not continue."));
1246                         }
1247                         cd->pctx.dirent = dirent;
1248                         cd->pctx.num = offset;
1249                 }
1250
1251                 if (dot_state == 0) {
1252                         if (check_dot(ctx, dirent, ino, &cd->pctx))
1253                                 dir_modified++;
1254                 } else if (dot_state == 1) {
1255                         ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
1256                         if (ret < 0)
1257                                 goto abort_free_dict;
1258                         if (ret)
1259                                 dir_modified++;
1260                 } else if (dirent->inode == ino) {
1261                         problem = PR_2_LINK_DOT;
1262                         if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
1263                                 dirent->inode = 0;
1264                                 dir_modified++;
1265                                 goto next;
1266                         }
1267                 }
1268                 if (!dirent->inode)
1269                         goto next;
1270
1271                 /*
1272                  * Make sure the inode listed is a legal one.
1273                  */
1274                 name_len = ext2fs_dirent_name_len(dirent);
1275                 if (((dirent->inode != EXT2_ROOT_INO) &&
1276                      (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
1277                     (dirent->inode > fs->super->s_inodes_count)) {
1278                         problem = PR_2_BAD_INO;
1279                 } else if (ctx->inode_bb_map &&
1280                            (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
1281                                                      dirent->inode))) {
1282                         /*
1283                          * If the inode is in a bad block, offer to
1284                          * clear it.
1285                          */
1286                         problem = PR_2_BB_INODE;
1287                 } else if ((dot_state > 1) && (name_len == 1) &&
1288                            (dirent->name[0] == '.')) {
1289                         /*
1290                          * If there's a '.' entry in anything other
1291                          * than the first directory entry, it's a
1292                          * duplicate entry that should be removed.
1293                          */
1294                         problem = PR_2_DUP_DOT;
1295                 } else if ((dot_state > 1) && (name_len == 2) &&
1296                            (dirent->name[0] == '.') &&
1297                            (dirent->name[1] == '.')) {
1298                         /*
1299                          * If there's a '..' entry in anything other
1300                          * than the second directory entry, it's a
1301                          * duplicate entry that should be removed.
1302                          */
1303                         problem = PR_2_DUP_DOT_DOT;
1304                 } else if ((dot_state > 1) &&
1305                            (dirent->inode == EXT2_ROOT_INO)) {
1306                         /*
1307                          * Don't allow links to the root directory.
1308                          * We check this specially to make sure we
1309                          * catch this error case even if the root
1310                          * directory hasn't been created yet.
1311                          */
1312                         problem = PR_2_LINK_ROOT;
1313                 } else if ((dot_state > 1) && (name_len == 0)) {
1314                         /*
1315                          * Don't allow zero-length directory names.
1316                          */
1317                         problem = PR_2_NULL_NAME;
1318                 }
1319
1320                 if (problem) {
1321                         if (fix_problem(ctx, problem, &cd->pctx)) {
1322                                 dirent->inode = 0;
1323                                 dir_modified++;
1324                                 goto next;
1325                         } else {
1326                                 ext2fs_unmark_valid(fs);
1327                                 if (problem == PR_2_BAD_INO)
1328                                         goto next;
1329                         }
1330                 }
1331
1332                 /*
1333                  * If the inode was marked as having bad fields in
1334                  * pass1, process it and offer to fix/clear it.
1335                  * (We wait until now so that we can display the
1336                  * pathname to the user.)
1337                  */
1338                 if (ctx->inode_bad_map &&
1339                     ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
1340                                              dirent->inode)) {
1341                         if (e2fsck_process_bad_inode(ctx, ino,
1342                                                      dirent->inode,
1343                                                      buf + fs->blocksize)) {
1344                                 dirent->inode = 0;
1345                                 dir_modified++;
1346                                 goto next;
1347                         }
1348                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1349                                 return DIRENT_ABORT;
1350                 }
1351
1352                 group = ext2fs_group_of_ino(fs, dirent->inode);
1353                 first_unused_inode = group * fs->super->s_inodes_per_group +
1354                                         1 + fs->super->s_inodes_per_group -
1355                                         ext2fs_bg_itable_unused(fs, group);
1356                 cd->pctx.group = group;
1357
1358                 /*
1359                  * Check if the inode was missed out because
1360                  * _INODE_UNINIT flag was set or bg_itable_unused was
1361                  * incorrect.  If so, clear the _INODE_UNINIT flag and
1362                  * restart e2fsck.  In the future it would be nice if
1363                  * we could call a function in pass1.c that checks the
1364                  * newly visible inodes.
1365                  */
1366                 if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
1367                         pctx.num = dirent->inode;
1368                         if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
1369                                         &cd->pctx)){
1370                                 ext2fs_bg_flags_clear(fs, group,
1371                                                       EXT2_BG_INODE_UNINIT);
1372                                 ext2fs_mark_super_dirty(fs);
1373                                 ctx->flags |= E2F_FLAG_RESTART_LATER;
1374                         } else {
1375                                 ext2fs_unmark_valid(fs);
1376                                 if (problem == PR_2_BAD_INO)
1377                                         goto next;
1378                         }
1379                 } else if (dirent->inode >= first_unused_inode) {
1380                         pctx.num = dirent->inode;
1381                         if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
1382                                 ext2fs_bg_itable_unused_set(fs, group, 0);
1383                                 ext2fs_mark_super_dirty(fs);
1384                                 ctx->flags |= E2F_FLAG_RESTART_LATER;
1385                         } else {
1386                                 ext2fs_unmark_valid(fs);
1387                                 if (problem == PR_2_BAD_INO)
1388                                         goto next;
1389                         }
1390                 }
1391
1392                 /* 
1393                  * Offer to clear unused inodes; if we are going to be
1394                  * restarting the scan due to bg_itable_unused being
1395                  * wrong, then don't clear any inodes to avoid zapping
1396                  * inodes that were skipped during pass1 due to an
1397                  * incorrect bg_itable_unused; we'll get any real
1398                  * problems after we restart.
1399                  */
1400                 if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
1401                     !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1402                                                 dirent->inode)))
1403                         problem = PR_2_UNUSED_INODE;
1404
1405                 if (problem) {
1406                         if (fix_problem(ctx, problem, &cd->pctx)) {
1407                                 dirent->inode = 0;
1408                                 dir_modified++;
1409                                 goto next;
1410                         } else {
1411                                 ext2fs_unmark_valid(fs);
1412                                 if (problem == PR_2_BAD_INO)
1413                                         goto next;
1414                         }
1415                 }
1416
1417                 if (!encrypted && check_name(ctx, dirent, ino, &cd->pctx))
1418                         dir_modified++;
1419
1420                 if (check_filetype(ctx, dirent, ino, &cd->pctx))
1421                         dir_modified++;
1422
1423 #ifdef ENABLE_HTREE
1424                 if (dx_db) {
1425                         get_filename_hash(fs, encrypted, dx_dir->hashversion,
1426                                           dirent->name,
1427                                           ext2fs_dirent_name_len(dirent),
1428                                           &hash, 0);
1429                         if (hash < dx_db->min_hash)
1430                                 dx_db->min_hash = hash;
1431                         if (hash > dx_db->max_hash)
1432                                 dx_db->max_hash = hash;
1433                 }
1434 #endif
1435
1436                 /*
1437                  * If this is a directory, then mark its parent in its
1438                  * dir_info structure.  If the parent field is already
1439                  * filled in, then this directory has more than one
1440                  * hard link.  We assume the first link is correct,
1441                  * and ask the user if he/she wants to clear this one.
1442                  */
1443                 if ((dot_state > 1) &&
1444                     (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
1445                                               dirent->inode))) {
1446                         if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1447                                                        &subdir_parent)) {
1448                                 cd->pctx.ino = dirent->inode;
1449                                 fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
1450                                 goto abort_free_dict;
1451                         }
1452                         if (subdir_parent) {
1453                                 cd->pctx.ino2 = subdir_parent;
1454                                 if (fix_problem(ctx, PR_2_LINK_DIR,
1455                                                 &cd->pctx)) {
1456                                         dirent->inode = 0;
1457                                         dir_modified++;
1458                                         goto next;
1459                                 }
1460                                 cd->pctx.ino2 = 0;
1461                         } else {
1462                                 (void) e2fsck_dir_info_set_parent(ctx,
1463                                                   dirent->inode, ino);
1464                         }
1465                 }
1466
1467                 if (dups_found) {
1468                         ;
1469                 } else if (dict_lookup(&de_dict, dirent)) {
1470                         clear_problem_context(&pctx);
1471                         pctx.ino = ino;
1472                         pctx.dirent = dirent;
1473                         fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1474                         e2fsck_rehash_dir_later(ctx, ino);
1475                         dups_found++;
1476                 } else
1477                         dict_alloc_insert(&de_dict, dirent, dirent);
1478
1479                 ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1480                                         &links);
1481                 if (links > 1)
1482                         ctx->fs_links_count++;
1483                 ctx->fs_total_count++;
1484         next:
1485                 prev = dirent;
1486                 if (dir_modified)
1487                         (void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1488                 if (!inline_data_size || dot_state > 1) {
1489                         offset += rec_len;
1490                 } else {
1491                         if (dot_state == 1) {
1492                                 offset = 4;
1493                                 /*
1494                                  * If we get here, we're checking an inline
1495                                  * directory and we've just checked a (fake)
1496                                  * dotdot entry that we created on the stack.
1497                                  * Therefore set 'prev' to NULL so that if we
1498                                  * call salvage_directory on the next entry,
1499                                  * it won't try to absorb the next entry into
1500                                  * the on-stack dotdot entry.
1501                                  */
1502                                 prev = NULL;
1503                         }
1504                 }
1505                 dot_state++;
1506         } while (offset < max_block_size);
1507 #if 0
1508         printf("\n");
1509 #endif
1510 #ifdef ENABLE_HTREE
1511         if (dx_db) {
1512 #ifdef DX_DEBUG
1513                 printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1514                        db->blockcnt, dx_db->type,
1515                        dx_db->min_hash, dx_db->max_hash);
1516 #endif
1517                 cd->pctx.dir = cd->pctx.ino;
1518                 if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1519                     (dx_db->type == DX_DIRBLOCK_NODE))
1520                         parse_int_node(fs, db, cd, dx_dir, buf, failed_csum);
1521         }
1522 #endif /* ENABLE_HTREE */
1523
1524         if (offset != max_block_size) {
1525                 cd->pctx.num = rec_len + offset - max_block_size;
1526                 if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1527                         dirent->rec_len = cd->pctx.num;
1528                         dir_modified++;
1529                 }
1530         }
1531         if (dir_modified) {
1532                 int     flags, will_rehash;
1533                 /* leaf block with no tail?  Rehash dirs later. */
1534                 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
1535                                 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
1536                     is_leaf &&
1537                     !inline_data_size &&
1538                     !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1539                         if (insert_dirent_tail(fs, buf) == 0)
1540                                 goto write_and_fix;
1541                         e2fsck_rehash_dir_later(ctx, ino);
1542                 }
1543
1544 write_and_fix:
1545                 will_rehash = e2fsck_dir_will_be_rehashed(ctx, ino);
1546                 if (will_rehash) {
1547                         flags = ctx->fs->flags;
1548                         ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1549                 }
1550                 if (inline_data_size) {
1551                         buf = ibuf;
1552 #ifdef WORDS_BIGENDIAN
1553                         if (db->blockcnt)
1554                                 goto skip_first_write_swab;
1555                         *((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
1556                         cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1557                                         buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
1558                                         EXT4_MIN_INLINE_DATA_SIZE -
1559                                         EXT4_INLINE_DATA_DOTDOT_SIZE,
1560                                         0);
1561                         if (cd->pctx.errcode)
1562                                 goto skip_second_write_swab;
1563 skip_first_write_swab:
1564                         if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
1565                             !db->blockcnt)
1566                                 goto skip_second_write_swab;
1567                         cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1568                                         buf + EXT4_MIN_INLINE_DATA_SIZE,
1569                                         inline_data_size -
1570                                         EXT4_MIN_INLINE_DATA_SIZE,
1571                                         0);
1572 skip_second_write_swab:
1573                         if (cd->pctx.errcode &&
1574                             !fix_problem(ctx, PR_2_WRITE_DIRBLOCK, &cd->pctx))
1575                                 goto abort_free_dict;
1576 #endif
1577                         cd->pctx.errcode =
1578                                 ext2fs_inline_data_set(fs, ino, 0, buf,
1579                                                        inline_data_size);
1580                 } else
1581                         cd->pctx.errcode = ext2fs_write_dir_block4(fs, block_nr,
1582                                                                    buf, 0, ino);
1583                 if (will_rehash)
1584                         ctx->fs->flags = (flags &
1585                                           EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1586                                          (ctx->fs->flags &
1587                                           ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1588                 if (cd->pctx.errcode) {
1589                         if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
1590                                          &cd->pctx))
1591                                 goto abort_free_dict;
1592                 }
1593                 ext2fs_mark_changed(fs);
1594         } else if (is_leaf && failed_csum && !dir_modified) {
1595                 /*
1596                  * If a leaf node that fails csum makes it this far without
1597                  * alteration, ask the user if the checksum should be fixed.
1598                  */
1599                 if (fix_problem(ctx, PR_2_LEAF_NODE_ONLY_CSUM_INVALID,
1600                                 &cd->pctx))
1601                         goto write_and_fix;
1602         }
1603         dict_free_nodes(&de_dict);
1604         return 0;
1605 abort_free_dict:
1606         ctx->flags |= E2F_FLAG_ABORT;
1607         dict_free_nodes(&de_dict);
1608         return DIRENT_ABORT;
1609 }
1610
1611 struct del_block {
1612         e2fsck_t        ctx;
1613         e2_blkcnt_t     num;
1614 };
1615
1616 /*
1617  * This function is called to deallocate a block, and is an interator
1618  * functioned called by deallocate inode via ext2fs_iterate_block().
1619  */
1620 static int deallocate_inode_block(ext2_filsys fs,
1621                                   blk64_t       *block_nr,
1622                                   e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1623                                   blk64_t ref_block EXT2FS_ATTR((unused)),
1624                                   int ref_offset EXT2FS_ATTR((unused)),
1625                                   void *priv_data)
1626 {
1627         struct del_block *p = priv_data;
1628
1629         if (*block_nr == 0)
1630                 return 0;
1631         if ((*block_nr < fs->super->s_first_data_block) ||
1632             (*block_nr >= ext2fs_blocks_count(fs->super)))
1633                 return 0;
1634         if ((*block_nr % EXT2FS_CLUSTER_RATIO(fs)) == 0)
1635                 ext2fs_block_alloc_stats2(fs, *block_nr, -1);
1636         p->num++;
1637         return 0;
1638 }
1639
1640 /*
1641  * This fuction deallocates an inode
1642  */
1643 static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
1644 {
1645         ext2_filsys fs = ctx->fs;
1646         struct ext2_inode       inode;
1647         struct problem_context  pctx;
1648         __u32                   count;
1649         struct del_block        del_block;
1650
1651         e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1652         clear_problem_context(&pctx);
1653         pctx.ino = ino;
1654
1655         /*
1656          * Fix up the bitmaps...
1657          */
1658         e2fsck_read_bitmaps(ctx);
1659         ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1660
1661         if (ext2fs_file_acl_block(fs, &inode) &&
1662             (fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1663                 pctx.errcode = ext2fs_adjust_ea_refcount3(fs,
1664                                 ext2fs_file_acl_block(fs, &inode),
1665                                 block_buf, -1, &count, ino);
1666                 if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1667                         pctx.errcode = 0;
1668                         count = 1;
1669                 }
1670                 if (pctx.errcode) {
1671                         pctx.blk = ext2fs_file_acl_block(fs, &inode);
1672                         fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1673                         ctx->flags |= E2F_FLAG_ABORT;
1674                         return;
1675                 }
1676                 if (count == 0) {
1677                         ext2fs_block_alloc_stats2(fs,
1678                                   ext2fs_file_acl_block(fs, &inode), -1);
1679                 }
1680                 ext2fs_file_acl_block_set(fs, &inode, 0);
1681         }
1682
1683         if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1684                 goto clear_inode;
1685
1686         /* Inline data inodes don't have blocks to iterate */
1687         if (inode.i_flags & EXT4_INLINE_DATA_FL)
1688                 goto clear_inode;
1689
1690         if (LINUX_S_ISREG(inode.i_mode) &&
1691             ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode)))
1692                 ctx->large_files--;
1693
1694         del_block.ctx = ctx;
1695         del_block.num = 0;
1696         pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1697                                              deallocate_inode_block,
1698                                              &del_block);
1699         if (pctx.errcode) {
1700                 fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
1701                 ctx->flags |= E2F_FLAG_ABORT;
1702                 return;
1703         }
1704 clear_inode:
1705         /* Inode may have changed by block_iterate, so reread it */
1706         e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1707         e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
1708 }
1709
1710 /*
1711  * This fuction clears the htree flag on an inode
1712  */
1713 static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1714 {
1715         struct ext2_inode       inode;
1716
1717         e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1718         inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1719         e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
1720         if (ctx->dirs_to_hash)
1721                 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1722 }
1723
1724
1725 int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1726                              ext2_ino_t ino, char *buf)
1727 {
1728         ext2_filsys fs = ctx->fs;
1729         struct ext2_inode       inode;
1730         int                     inode_modified = 0;
1731         int                     not_fixed = 0;
1732         unsigned char           *frag, *fsize;
1733         struct problem_context  pctx;
1734         problem_t               problem = 0;
1735
1736         e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
1737
1738         clear_problem_context(&pctx);
1739         pctx.ino = ino;
1740         pctx.dir = dir;
1741         pctx.inode = &inode;
1742
1743         if (ext2fs_file_acl_block(fs, &inode) &&
1744             !(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR)) {
1745                 if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1746                         ext2fs_file_acl_block_set(fs, &inode, 0);
1747                         inode_modified++;
1748                 } else
1749                         not_fixed++;
1750         }
1751
1752         if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1753             !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1754             !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
1755             !(LINUX_S_ISSOCK(inode.i_mode)))
1756                 problem = PR_2_BAD_MODE;
1757         else if (LINUX_S_ISCHR(inode.i_mode)
1758                  && !e2fsck_pass1_check_device_inode(fs, &inode))
1759                 problem = PR_2_BAD_CHAR_DEV;
1760         else if (LINUX_S_ISBLK(inode.i_mode)
1761                  && !e2fsck_pass1_check_device_inode(fs, &inode))
1762                 problem = PR_2_BAD_BLOCK_DEV;
1763         else if (LINUX_S_ISFIFO(inode.i_mode)
1764                  && !e2fsck_pass1_check_device_inode(fs, &inode))
1765                 problem = PR_2_BAD_FIFO;
1766         else if (LINUX_S_ISSOCK(inode.i_mode)
1767                  && !e2fsck_pass1_check_device_inode(fs, &inode))
1768                 problem = PR_2_BAD_SOCKET;
1769         else if (LINUX_S_ISLNK(inode.i_mode)
1770                  && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
1771                 problem = PR_2_INVALID_SYMLINK;
1772         }
1773
1774         if (problem) {
1775                 if (fix_problem(ctx, problem, &pctx)) {
1776                         deallocate_inode(ctx, ino, 0);
1777                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1778                                 return 0;
1779                         return 1;
1780                 } else
1781                         not_fixed++;
1782                 problem = 0;
1783         }
1784
1785         if (inode.i_faddr) {
1786                 if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1787                         inode.i_faddr = 0;
1788                         inode_modified++;
1789                 } else
1790                         not_fixed++;
1791         }
1792
1793         switch (fs->super->s_creator_os) {
1794             case EXT2_OS_HURD:
1795                 frag = &inode.osd2.hurd2.h_i_frag;
1796                 fsize = &inode.osd2.hurd2.h_i_fsize;
1797                 break;
1798             default:
1799                 frag = fsize = 0;
1800         }
1801         if (frag && *frag) {
1802                 pctx.num = *frag;
1803                 if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
1804                         *frag = 0;
1805                         inode_modified++;
1806                 } else
1807                         not_fixed++;
1808                 pctx.num = 0;
1809         }
1810         if (fsize && *fsize) {
1811                 pctx.num = *fsize;
1812                 if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
1813                         *fsize = 0;
1814                         inode_modified++;
1815                 } else
1816                         not_fixed++;
1817                 pctx.num = 0;
1818         }
1819
1820         if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1821             !(fs->super->s_feature_ro_compat &
1822               EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1823             (inode.osd2.linux2.l_i_blocks_hi != 0)) {
1824                 pctx.num = inode.osd2.linux2.l_i_blocks_hi;
1825                 if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
1826                         inode.osd2.linux2.l_i_blocks_hi = 0;
1827                         inode_modified++;
1828                 }
1829         }
1830
1831         if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1832             !(fs->super->s_feature_incompat &
1833              EXT4_FEATURE_INCOMPAT_64BIT) &&
1834             inode.osd2.linux2.l_i_file_acl_high != 0) {
1835                 pctx.num = inode.osd2.linux2.l_i_file_acl_high;
1836                 if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
1837                         inode.osd2.linux2.l_i_file_acl_high = 0;
1838                         inode_modified++;
1839                 } else
1840                         not_fixed++;
1841         }
1842
1843         if (ext2fs_file_acl_block(fs, &inode) &&
1844             ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
1845              (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
1846                 if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
1847                         ext2fs_file_acl_block_set(fs, &inode, 0);
1848                         inode_modified++;
1849                 } else
1850                         not_fixed++;
1851         }
1852         if (inode.i_dir_acl &&
1853             LINUX_S_ISDIR(inode.i_mode)) {
1854                 if (fix_problem(ctx, PR_2_DIR_ACL_ZERO, &pctx)) {
1855                         inode.i_dir_acl = 0;
1856                         inode_modified++;
1857                 } else
1858                         not_fixed++;
1859         }
1860
1861         if (inode_modified)
1862                 e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
1863         if (!not_fixed && ctx->inode_bad_map)
1864                 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
1865         return 0;
1866 }
1867
1868 /*
1869  * allocate_dir_block --- this function allocates a new directory
1870  *      block for a particular inode; this is done if a directory has
1871  *      a "hole" in it, or if a directory has a illegal block number
1872  *      that was zeroed out and now needs to be replaced.
1873  */
1874 static int allocate_dir_block(e2fsck_t ctx,
1875                               struct ext2_db_entry2 *db,
1876                               char *buf EXT2FS_ATTR((unused)),
1877                               struct problem_context *pctx)
1878 {
1879         ext2_filsys fs = ctx->fs;
1880         blk64_t                 blk = 0;
1881         char                    *block;
1882         struct ext2_inode       inode;
1883
1884         if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
1885                 return 1;
1886
1887         /*
1888          * Read the inode and block bitmaps in; we'll be messing with
1889          * them.
1890          */
1891         e2fsck_read_bitmaps(ctx);
1892
1893         /*
1894          * First, find a free block
1895          */
1896         e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
1897         pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode,
1898                                                  db->blockcnt, &blk);
1899         if (pctx->errcode || blk == 0) {
1900                 blk = ext2fs_find_inode_goal(fs, db->ino, &inode, db->blockcnt);
1901                 pctx->errcode = ext2fs_new_block2(fs, blk,
1902                                                   ctx->block_found_map, &blk);
1903                 if (pctx->errcode) {
1904                         pctx->str = "ext2fs_new_block";
1905                         fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1906                         return 1;
1907                 }
1908         }
1909         ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1910         ext2fs_mark_block_bitmap2(fs->block_map, blk);
1911         ext2fs_mark_bb_dirty(fs);
1912
1913         /*
1914          * Now let's create the actual data block for the inode
1915          */
1916         if (db->blockcnt)
1917                 pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
1918         else
1919                 pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
1920                                                      EXT2_ROOT_INO, &block);
1921
1922         if (pctx->errcode) {
1923                 pctx->str = "ext2fs_new_dir_block";
1924                 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1925                 return 1;
1926         }
1927
1928         pctx->errcode = ext2fs_write_dir_block4(fs, blk, block, 0, db->ino);
1929         ext2fs_free_mem(&block);
1930         if (pctx->errcode) {
1931                 pctx->str = "ext2fs_write_dir_block";
1932                 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1933                 return 1;
1934         }
1935
1936         /*
1937          * Update the inode block count
1938          */
1939         ext2fs_iblk_add_blocks(fs, &inode, 1);
1940         if (EXT2_I_SIZE(&inode) < (db->blockcnt+1) * fs->blocksize) {
1941                 pctx->errcode = ext2fs_inode_size_set(fs, &inode,
1942                                         (db->blockcnt+1) * fs->blocksize);
1943                 if (pctx->errcode) {
1944                         pctx->str = "ext2fs_inode_size_set";
1945                         fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1946                         return 1;
1947                 }
1948         }
1949         e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
1950
1951         /*
1952          * Finally, update the block pointers for the inode
1953          */
1954         db->blk = blk;
1955         pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
1956                                      db->blockcnt, 0, &blk);
1957         if (pctx->errcode) {
1958                 pctx->str = "ext2fs_block_iterate";
1959                 fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
1960                 return 1;
1961         }
1962
1963         return 0;
1964 }