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