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