Whamcloud - gitweb
b5d3f12265038e4845ccb2414cac6e545b8f879c
[tools/e2fsprogs.git] / e2fsck / pass1.c
1 /*
2  * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
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 1 of e2fsck iterates over all the inodes in the filesystems,
12  * and applies the following tests to each inode:
13  *
14  *      - The mode field of the inode must be legal.
15  *      - The size and block count fields of the inode are correct.
16  *      - A data block must not be used by another inode
17  *
18  * Pass 1 also gathers the collects the following information:
19  *
20  *      - A bitmap of which inodes are in use.          (inode_used_map)
21  *      - A bitmap of which inodes are directories.     (inode_dir_map)
22  *      - A bitmap of which inodes are regular files.   (inode_reg_map)
23  *      - A bitmap of which inodes have bad fields.     (inode_bad_map)
24  *      - A bitmap of which inodes are in bad blocks.   (inode_bb_map)
25  *      - A bitmap of which inodes are imagic inodes.   (inode_imagic_map)
26  *      - A bitmap of which blocks are in use.          (block_found_map)
27  *      - A bitmap of which blocks are in use by two inodes     (block_dup_map)
28  *      - The data blocks of the directory inodes.      (dir_map)
29  *
30  * Pass 1 is designed to stash away enough information so that the
31  * other passes should not need to read in the inode information
32  * during the normal course of a filesystem check.  (Althogh if an
33  * inconsistency is detected, other passes may need to read in an
34  * inode to fix it.)
35  *
36  * Note that pass 1B will be invoked if there are any duplicate blocks
37  * found.
38  */
39
40 #define _GNU_SOURCE 1 /* get strnlen() */
41 #include "config.h"
42 #include <string.h>
43 #include <time.h>
44 #ifdef HAVE_ERRNO_H
45 #include <errno.h>
46 #endif
47
48 #include "e2fsck.h"
49 #include <ext2fs/ext2_ext_attr.h>
50
51 #include "problem.h"
52
53 #ifdef NO_INLINE_FUNCS
54 #define _INLINE_
55 #else
56 #define _INLINE_ inline
57 #endif
58
59 static int process_block(ext2_filsys fs, blk64_t        *blocknr,
60                          e2_blkcnt_t blockcnt, blk64_t ref_blk,
61                          int ref_offset, void *priv_data);
62 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
63                              e2_blkcnt_t blockcnt, blk64_t ref_blk,
64                              int ref_offset, void *priv_data);
65 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
66                          char *block_buf);
67 static void mark_table_blocks(e2fsck_t ctx);
68 static void alloc_bb_map(e2fsck_t ctx);
69 static void alloc_imagic_map(e2fsck_t ctx);
70 static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
71 static void handle_fs_bad_blocks(e2fsck_t ctx);
72 static void process_inodes(e2fsck_t ctx, char *block_buf);
73 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
74 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
75                                   dgrp_t group, void * priv_data);
76 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
77                                     char *block_buf, int adjust_sign);
78 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
79
80 struct process_block_struct {
81         ext2_ino_t      ino;
82         unsigned        is_dir:1, is_reg:1, clear:1, suppress:1,
83                                 fragmented:1, compressed:1, bbcheck:1;
84         blk64_t         num_blocks;
85         blk64_t         max_blocks;
86         e2_blkcnt_t     last_block;
87         e2_blkcnt_t     last_init_lblock;
88         e2_blkcnt_t     last_db_block;
89         int             num_illegal_blocks;
90         blk64_t         previous_block;
91         struct ext2_inode *inode;
92         struct problem_context *pctx;
93         ext2fs_block_bitmap fs_meta_blocks;
94         e2fsck_t        ctx;
95 };
96
97 struct process_inode_block {
98         ext2_ino_t ino;
99         struct ext2_inode inode;
100 };
101
102 struct scan_callback_struct {
103         e2fsck_t        ctx;
104         char            *block_buf;
105 };
106
107 /*
108  * For the inodes to process list.
109  */
110 static struct process_inode_block *inodes_to_process;
111 static int process_inode_count;
112
113 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
114                             EXT2_MIN_BLOCK_LOG_SIZE + 1];
115
116 /*
117  * Free all memory allocated by pass1 in preparation for restarting
118  * things.
119  */
120 static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
121 {
122         ext2fs_free_mem(&inodes_to_process);
123         inodes_to_process = 0;
124 }
125
126 /*
127  * Check to make sure a device inode is real.  Returns 1 if the device
128  * checks out, 0 if not.
129  *
130  * Note: this routine is now also used to check FIFO's and Sockets,
131  * since they have the same requirement; the i_block fields should be
132  * zero.
133  */
134 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
135                                     struct ext2_inode *inode)
136 {
137         int     i;
138
139         /*
140          * If the index flag is set, then this is a bogus
141          * device/fifo/socket
142          */
143         if (inode->i_flags & EXT2_INDEX_FL)
144                 return 0;
145
146         /*
147          * We should be able to do the test below all the time, but
148          * because the kernel doesn't forcibly clear the device
149          * inode's additional i_block fields, there are some rare
150          * occasions when a legitimate device inode will have non-zero
151          * additional i_block fields.  So for now, we only complain
152          * when the immutable flag is set, which should never happen
153          * for devices.  (And that's when the problem is caused, since
154          * you can't set or clear immutable flags for devices.)  Once
155          * the kernel has been fixed we can change this...
156          */
157         if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
158                 for (i=4; i < EXT2_N_BLOCKS; i++)
159                         if (inode->i_block[i])
160                                 return 0;
161         }
162         return 1;
163 }
164
165 /*
166  * Check to make sure a symlink inode is real.  Returns 1 if the symlink
167  * checks out, 0 if not.
168  */
169 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
170                                struct ext2_inode *inode, char *buf)
171 {
172         unsigned int len;
173         int i;
174         blk64_t blocks;
175         ext2_extent_handle_t    handle;
176         struct ext2_extent_info info;
177         struct ext2fs_extent    extent;
178
179         if ((inode->i_size_high || inode->i_size == 0) ||
180             (inode->i_flags & EXT2_INDEX_FL))
181                 return 0;
182
183         if (inode->i_flags & EXT4_EXTENTS_FL) {
184                 if (inode->i_size > fs->blocksize)
185                         return 0;
186                 if (ext2fs_extent_open2(fs, ino, inode, &handle))
187                         return 0;
188                 i = 0;
189                 if (ext2fs_extent_get_info(handle, &info) ||
190                     (info.num_entries != 1) ||
191                     (info.max_depth != 0))
192                         goto exit_extent;
193                 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
194                     (extent.e_lblk != 0) ||
195                     (extent.e_len != 1) ||
196                     (extent.e_pblk < fs->super->s_first_data_block) ||
197                     (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
198                         goto exit_extent;
199                 i = 1;
200         exit_extent:
201                 ext2fs_extent_free(handle);
202                 return i;
203         }
204
205         blocks = ext2fs_inode_data_blocks2(fs, inode);
206         if (blocks) {
207                 if ((inode->i_size >= fs->blocksize) ||
208                     (blocks != fs->blocksize >> 9) ||
209                     (inode->i_block[0] < fs->super->s_first_data_block) ||
210                     (inode->i_block[0] >= ext2fs_blocks_count(fs->super)))
211                         return 0;
212
213                 for (i = 1; i < EXT2_N_BLOCKS; i++)
214                         if (inode->i_block[i])
215                                 return 0;
216
217                 if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf))
218                         return 0;
219
220                 len = strnlen(buf, fs->blocksize);
221                 if (len == fs->blocksize)
222                         return 0;
223         } else {
224                 if (inode->i_size >= sizeof(inode->i_block))
225                         return 0;
226
227                 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
228                 if (len == sizeof(inode->i_block))
229                         return 0;
230         }
231         if (len != inode->i_size)
232                 return 0;
233         return 1;
234 }
235
236 /*
237  * If the immutable (or append-only) flag is set on the inode, offer
238  * to clear it.
239  */
240 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
241 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
242 {
243         if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
244                 return;
245
246         if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
247                 return;
248
249         pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
250         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
251 }
252
253 /*
254  * If device, fifo or socket, check size is zero -- if not offer to
255  * clear it
256  */
257 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
258 {
259         struct ext2_inode *inode = pctx->inode;
260
261         if (EXT2_I_SIZE(inode) == 0)
262                 return;
263
264         if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
265                 return;
266
267         inode->i_size = 0;
268         inode->i_size_high = 0;
269         e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
270 }
271
272 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
273 {
274         struct ext2_super_block *sb = ctx->fs->super;
275         struct ext2_inode_large *inode;
276         struct ext2_ext_attr_entry *entry;
277         char *start, *end;
278         unsigned int storage_size, remain;
279         int problem = 0;
280
281         inode = (struct ext2_inode_large *) pctx->inode;
282         storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
283                 inode->i_extra_isize;
284         start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
285                 inode->i_extra_isize + sizeof(__u32);
286         end = (char *) inode + EXT2_INODE_SIZE(ctx->fs->super);
287         entry = (struct ext2_ext_attr_entry *) start;
288
289         /* scan all entry's headers first */
290
291         /* take finish entry 0UL into account */
292         remain = storage_size - sizeof(__u32);
293
294         while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
295                 __u32 hash;
296
297                 /* header eats this space */
298                 remain -= sizeof(struct ext2_ext_attr_entry);
299
300                 /* is attribute name valid? */
301                 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
302                         pctx->num = entry->e_name_len;
303                         problem = PR_1_ATTR_NAME_LEN;
304                         goto fix;
305                 }
306
307                 /* attribute len eats this space */
308                 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
309
310                 /* check value size */
311                 if (entry->e_value_size == 0 || entry->e_value_size > remain) {
312                         pctx->num = entry->e_value_size;
313                         problem = PR_1_ATTR_VALUE_SIZE;
314                         goto fix;
315                 }
316
317                 /* e_value_block must be 0 in inode's ea */
318                 if (entry->e_value_block != 0) {
319                         pctx->num = entry->e_value_block;
320                         problem = PR_1_ATTR_VALUE_BLOCK;
321                         goto fix;
322                 }
323
324                 hash = ext2fs_ext_attr_hash_entry(entry,
325                                                   start + entry->e_value_offs);
326
327                 /* e_hash may be 0 in older inode's ea */
328                 if (entry->e_hash != 0 && entry->e_hash != hash) {
329                         pctx->num = entry->e_hash;
330                         problem = PR_1_ATTR_HASH;
331                         goto fix;
332                 }
333
334                 remain -= entry->e_value_size;
335
336                 entry = EXT2_EXT_ATTR_NEXT(entry);
337         }
338 fix:
339         /*
340          * it seems like a corruption. it's very unlikely we could repair
341          * EA(s) in automatic fashion -bzzz
342          */
343         if (problem == 0 || !fix_problem(ctx, problem, pctx))
344                 return;
345
346         /* simply remove all possible EA(s) */
347         *((__u32 *)start) = 0UL;
348         e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
349                                 EXT2_INODE_SIZE(sb), "pass1");
350 }
351
352 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
353 {
354         struct ext2_super_block *sb = ctx->fs->super;
355         struct ext2_inode_large *inode;
356         __u32 *eamagic;
357         int min, max;
358
359         inode = (struct ext2_inode_large *) pctx->inode;
360         if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
361                 /* this isn't large inode. so, nothing to check */
362                 return;
363         }
364
365 #if 0
366         printf("inode #%u, i_extra_size %d\n", pctx->ino,
367                         inode->i_extra_isize);
368 #endif
369         /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
370         min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
371         max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
372         /*
373          * For now we will allow i_extra_isize to be 0, but really
374          * implementations should never allow i_extra_isize to be 0
375          */
376         if (inode->i_extra_isize &&
377             (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
378                 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
379                         return;
380                 inode->i_extra_isize = min;
381                 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
382                                         EXT2_INODE_SIZE(sb), "pass1");
383                 return;
384         }
385
386         eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
387                         inode->i_extra_isize);
388         if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
389                 /* it seems inode has an extended attribute(s) in body */
390                 check_ea_in_inode(ctx, pctx);
391         }
392 }
393
394 /*
395  * Check to see if the inode might really be a directory, despite i_mode
396  *
397  * This is a lot of complexity for something for which I'm not really
398  * convinced happens frequently in the wild.  If for any reason this
399  * causes any problems, take this code out.
400  * [tytso:20070331.0827EDT]
401  */
402 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
403                                 char *buf)
404 {
405         struct ext2_inode *inode = pctx->inode;
406         struct ext2_dir_entry   *dirent;
407         errcode_t               retval;
408         blk64_t                 blk;
409         unsigned int            i, rec_len, not_device = 0;
410         int                     extent_fs;
411
412         /*
413          * If the mode looks OK, we believe it.  If the first block in
414          * the i_block array is 0, this cannot be a directory. If the
415          * inode is extent-mapped, it is still the case that the latter
416          * cannot be 0 - the magic number in the extent header would make
417          * it nonzero.
418          */
419         if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
420             LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
421                 return;
422
423         /* 
424          * Check the block numbers in the i_block array for validity:
425          * zero blocks are skipped (but the first one cannot be zero -
426          * see above), other blocks are checked against the first and
427          * max data blocks (from the the superblock) and against the
428          * block bitmap. Any invalid block found means this cannot be
429          * a directory.
430          * 
431          * If there are non-zero blocks past the fourth entry, then
432          * this cannot be a device file: we remember that for the next
433          * check.
434          *
435          * For extent mapped files, we don't do any sanity checking:
436          * just try to get the phys block of logical block 0 and run
437          * with it.
438          */
439
440         extent_fs = (ctx->fs->super->s_feature_incompat &
441                      EXT3_FEATURE_INCOMPAT_EXTENTS);
442         if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
443                 /* extent mapped */
444                 if  (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
445                                  &blk))
446                         return;
447                 /* device files are never extent mapped */
448                 not_device++;
449         } else {
450                 for (i=0; i < EXT2_N_BLOCKS; i++) {
451                         blk = inode->i_block[i];
452                         if (!blk)
453                                 continue;
454                         if (i >= 4)
455                                 not_device++;
456
457                         if (blk < ctx->fs->super->s_first_data_block ||
458                             blk >= ext2fs_blocks_count(ctx->fs->super) ||
459                             ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
460                                                            blk))
461                                 return; /* Invalid block, can't be dir */
462                 }
463                 blk = inode->i_block[0];
464         }
465
466         /*
467          * If the mode says this is a device file and the i_links_count field
468          * is sane and we have not ruled it out as a device file previously,
469          * we declare it a device file, not a directory.
470          */
471         if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
472             (inode->i_links_count == 1) && !not_device)
473                 return;
474
475         /* read the first block */
476         ehandler_operation(_("reading directory block"));
477         retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
478         ehandler_operation(0);
479         if (retval)
480                 return;
481
482         dirent = (struct ext2_dir_entry *) buf;
483         retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
484         if (retval)
485                 return;
486         if (((dirent->name_len & 0xFF) != 1) ||
487             (dirent->name[0] != '.') ||
488             (dirent->inode != pctx->ino) ||
489             (rec_len < 12) ||
490             (rec_len % 4) ||
491             (rec_len >= ctx->fs->blocksize - 12))
492                 return;
493
494         dirent = (struct ext2_dir_entry *) (buf + rec_len);
495         retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
496         if (retval)
497                 return;
498         if (((dirent->name_len & 0xFF) != 2) ||
499             (dirent->name[0] != '.') ||
500             (dirent->name[1] != '.') ||
501             (rec_len < 12) ||
502             (rec_len % 4))
503                 return;
504
505         if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
506                 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
507                 e2fsck_write_inode_full(ctx, pctx->ino, inode,
508                                         EXT2_INODE_SIZE(ctx->fs->super),
509                                         "check_is_really_dir");
510         }
511 }
512
513 extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
514                                     ext2_icount_t *ret)
515 {
516         unsigned int            threshold;
517         ext2_ino_t              num_dirs;
518         errcode_t               retval;
519         char                    *tdb_dir;
520         int                     enable;
521
522         *ret = 0;
523
524         profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
525                            &tdb_dir);
526         profile_get_uint(ctx->profile, "scratch_files",
527                          "numdirs_threshold", 0, 0, &threshold);
528         profile_get_boolean(ctx->profile, "scratch_files",
529                             "icount", 0, 1, &enable);
530
531         retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
532         if (retval)
533                 num_dirs = 1024;        /* Guess */
534
535         if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
536             (threshold && num_dirs <= threshold))
537                 return;
538
539         retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
540         if (retval)
541                 *ret = 0;
542 }
543
544 static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
545                                             e2fsck_t ctx,
546                                             struct problem_context *pctx)
547 {
548         errcode_t retval;
549         struct ext2_inode_large inode;
550
551         /*
552          * Reread inode.  If we don't see checksum error, then this inode
553          * has been fixed elsewhere.
554          */
555         retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
556                                         sizeof(inode));
557         if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
558                 return retval;
559         if (!retval)
560                 return 0;
561
562         /*
563          * Checksum still doesn't match.  That implies that the inode passes
564          * all the sanity checks, so maybe the checksum is simply corrupt.
565          * See if the user will go for fixing that.
566          */
567         if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
568                 return 0;
569
570         retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
571                                          sizeof(inode));
572         if (retval)
573                 return retval;
574
575         return 0;
576 }
577
578 void e2fsck_pass1(e2fsck_t ctx)
579 {
580         int     i;
581         __u64   max_sizes;
582         ext2_filsys fs = ctx->fs;
583         ext2_ino_t      ino = 0;
584         struct ext2_inode *inode;
585         ext2_inode_scan scan;
586         char            *block_buf;
587 #ifdef RESOURCE_TRACK
588         struct resource_track   rtrack;
589 #endif
590         unsigned char   frag, fsize;
591         struct          problem_context pctx;
592         struct          scan_callback_struct scan_struct;
593         struct ext2_super_block *sb = ctx->fs->super;
594         const char      *old_op;
595         unsigned int    save_type;
596         int             imagic_fs, extent_fs;
597         int             busted_fs_time = 0;
598         int             inode_size;
599         int             failed_csum = 0;
600
601         init_resource_track(&rtrack, ctx->fs->io);
602         clear_problem_context(&pctx);
603
604         if (!(ctx->options & E2F_OPT_PREEN))
605                 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
606
607         if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
608             !(ctx->options & E2F_OPT_NO)) {
609                 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
610                         ctx->dirs_to_hash = 0;
611         }
612
613 #ifdef MTRACE
614         mtrace_print("Pass 1");
615 #endif
616
617 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
618
619         for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
620                 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
621                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
622                 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
623                 max_sizes = (max_sizes * (1UL << i));
624                 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
625         }
626 #undef EXT2_BPP
627
628         imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
629         extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
630
631         /*
632          * Allocate bitmaps structures
633          */
634         pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
635                                                     EXT2FS_BMAP64_RBTREE,
636                                                     "inode_used_map",
637                                                     &ctx->inode_used_map);
638         if (pctx.errcode) {
639                 pctx.num = 1;
640                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
641                 ctx->flags |= E2F_FLAG_ABORT;
642                 return;
643         }
644         pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
645                         _("directory inode map"),
646                         EXT2FS_BMAP64_AUTODIR,
647                         "inode_dir_map", &ctx->inode_dir_map);
648         if (pctx.errcode) {
649                 pctx.num = 2;
650                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
651                 ctx->flags |= E2F_FLAG_ABORT;
652                 return;
653         }
654         pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
655                         _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
656                         "inode_reg_map", &ctx->inode_reg_map);
657         if (pctx.errcode) {
658                 pctx.num = 6;
659                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
660                 ctx->flags |= E2F_FLAG_ABORT;
661                 return;
662         }
663         pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
664                         _("in-use block map"), EXT2FS_BMAP64_RBTREE,
665                         "block_found_map", &ctx->block_found_map);
666         if (pctx.errcode) {
667                 pctx.num = 1;
668                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
669                 ctx->flags |= E2F_FLAG_ABORT;
670                 return;
671         }
672         e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
673         if (!ctx->inode_link_info) {
674                 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
675                                        "inode_link_info", &save_type);
676                 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
677                                                      &ctx->inode_link_info);
678                 fs->default_bitmap_type = save_type;
679         }
680
681         if (pctx.errcode) {
682                 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
683                 ctx->flags |= E2F_FLAG_ABORT;
684                 return;
685         }
686         inode_size = EXT2_INODE_SIZE(fs->super);
687         inode = (struct ext2_inode *)
688                 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
689
690         inodes_to_process = (struct process_inode_block *)
691                 e2fsck_allocate_memory(ctx,
692                                        (ctx->process_inode_size *
693                                         sizeof(struct process_inode_block)),
694                                        "array of inodes to process");
695         process_inode_count = 0;
696
697         pctx.errcode = ext2fs_init_dblist(fs, 0);
698         if (pctx.errcode) {
699                 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
700                 ctx->flags |= E2F_FLAG_ABORT;
701                 ext2fs_free_mem(&inode);
702                 return;
703         }
704
705         /*
706          * If the last orphan field is set, clear it, since the pass1
707          * processing will automatically find and clear the orphans.
708          * In the future, we may want to try using the last_orphan
709          * linked list ourselves, but for now, we clear it so that the
710          * ext3 mount code won't get confused.
711          */
712         if (!(ctx->options & E2F_OPT_READONLY)) {
713                 if (fs->super->s_last_orphan) {
714                         fs->super->s_last_orphan = 0;
715                         ext2fs_mark_super_dirty(fs);
716                 }
717         }
718
719         mark_table_blocks(ctx);
720         pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
721                                                 &ctx->block_found_map);
722         if (pctx.errcode) {
723                 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
724                 ctx->flags |= E2F_FLAG_ABORT;
725                 ext2fs_free_mem(&inode);
726                 return;
727         }
728         block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
729                                                     "block interate buffer");
730         if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
731                 e2fsck_use_inode_shortcuts(ctx, 1);
732         old_op = ehandler_operation(_("opening inode scan"));
733         pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
734                                               &scan);
735         ehandler_operation(old_op);
736         if (pctx.errcode) {
737                 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
738                 ctx->flags |= E2F_FLAG_ABORT;
739                 ext2fs_free_mem(&block_buf);
740                 ext2fs_free_mem(&inode);
741                 return;
742         }
743         ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
744         ctx->stashed_inode = inode;
745         scan_struct.ctx = ctx;
746         scan_struct.block_buf = block_buf;
747         ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
748         if (ctx->progress)
749                 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
750                         return;
751         if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
752             (fs->super->s_mtime < fs->super->s_inodes_count))
753                 busted_fs_time = 1;
754
755         if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
756             !(fs->super->s_mmp_block <= fs->super->s_first_data_block ||
757               fs->super->s_mmp_block >= fs->super->s_blocks_count))
758                 ext2fs_mark_block_bitmap2(ctx->block_found_map,
759                                           fs->super->s_mmp_block);
760
761         /* Set up ctx->lost_and_found if possible */
762         (void) e2fsck_get_lost_and_found(ctx, 0);
763
764         while (1) {
765                 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
766                         if (e2fsck_mmp_update(fs))
767                                 fatal_error(ctx, 0);
768                 }
769                 old_op = ehandler_operation(_("getting next inode from scan"));
770                 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
771                                                           inode, inode_size);
772                 ehandler_operation(old_op);
773                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
774                         return;
775                 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
776                         if (!ctx->inode_bb_map)
777                                 alloc_bb_map(ctx);
778                         ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
779                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
780                         continue;
781                 }
782                 if (pctx.errcode &&
783                     pctx.errcode != EXT2_ET_INODE_CSUM_INVALID) {
784                         fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
785                         ctx->flags |= E2F_FLAG_ABORT;
786                         return;
787                 }
788                 if (!ino)
789                         break;
790                 pctx.ino = ino;
791                 pctx.inode = inode;
792                 ctx->stashed_ino = ino;
793
794                 /* Clear corrupt inode? */
795                 if (pctx.errcode == EXT2_ET_INODE_CSUM_INVALID) {
796                         if (fix_problem(ctx, PR_1_INODE_CSUM_INVALID, &pctx))
797                                 goto clear_inode;
798                         failed_csum = 1;
799                 }
800
801                 if (inode->i_links_count) {
802                         pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
803                                            ino, inode->i_links_count);
804                         if (pctx.errcode) {
805                                 pctx.num = inode->i_links_count;
806                                 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
807                                 ctx->flags |= E2F_FLAG_ABORT;
808                                 return;
809                         }
810                 }
811
812                 /*
813                  * Test for incorrect extent flag settings.
814                  *
815                  * On big-endian machines we must be careful:
816                  * When the inode is read, the i_block array is not swapped
817                  * if the extent flag is set.  Therefore if we are testing
818                  * for or fixing a wrongly-set flag, we must potentially
819                  * (un)swap before testing, or after fixing.
820                  */
821
822                 /*
823                  * In this case the extents flag was set when read, so
824                  * extent_header_verify is ok.  If the inode is cleared,
825                  * no need to swap... so no extra swapping here.
826                  */
827                 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
828                     (inode->i_links_count || (ino == EXT2_BAD_INO) ||
829                      (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
830                         if ((ext2fs_extent_header_verify(inode->i_block,
831                                                  sizeof(inode->i_block)) == 0) &&
832                             fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
833                                 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
834                                 ext2fs_mark_super_dirty(fs);
835                                 extent_fs = 1;
836                         } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
837                         clear_inode:
838                                 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
839                                 if (ino == EXT2_BAD_INO)
840                                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
841                                                                  ino);
842                                 continue;
843                         }
844                 }
845
846                 /*
847                  * For big-endian machines:
848                  * If the inode didn't have the extents flag set when it
849                  * was read, then the i_blocks array was swapped.  To test
850                  * as an extents header, we must swap it back first.
851                  * IF we then set the extents flag, the entire i_block
852                  * array must be un/re-swapped to make it proper extents data.
853                  */
854                 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
855                     (inode->i_links_count || (ino == EXT2_BAD_INO) ||
856                      (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
857                     (LINUX_S_ISREG(inode->i_mode) ||
858                      LINUX_S_ISDIR(inode->i_mode))) {
859                         void *ehp;
860 #ifdef WORDS_BIGENDIAN
861                         __u32 tmp_block[EXT2_N_BLOCKS];
862
863                         for (i = 0; i < EXT2_N_BLOCKS; i++)
864                                 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
865                         ehp = tmp_block;
866 #else
867                         ehp = inode->i_block;
868 #endif
869                         if ((ext2fs_extent_header_verify(ehp,
870                                          sizeof(inode->i_block)) == 0) &&
871                             (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
872                                 inode->i_flags |= EXT4_EXTENTS_FL;
873 #ifdef WORDS_BIGENDIAN
874                                 memcpy(inode->i_block, tmp_block,
875                                        sizeof(inode->i_block));
876 #endif
877                                 e2fsck_write_inode(ctx, ino, inode, "pass1");
878                         }
879                 }
880
881                 if (ino == EXT2_BAD_INO) {
882                         struct process_block_struct pb;
883
884                         if ((inode->i_mode || inode->i_uid || inode->i_gid ||
885                              inode->i_links_count || inode->i_file_acl) &&
886                             fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
887                                 memset(inode, 0, sizeof(struct ext2_inode));
888                                 e2fsck_write_inode(ctx, ino, inode,
889                                                    "clear bad inode");
890                         }
891
892                         pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
893                                                           &pb.fs_meta_blocks);
894                         if (pctx.errcode) {
895                                 pctx.num = 4;
896                                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
897                                 ctx->flags |= E2F_FLAG_ABORT;
898                                 return;
899                         }
900                         pb.ino = EXT2_BAD_INO;
901                         pb.num_blocks = pb.last_block = 0;
902                         pb.last_db_block = -1;
903                         pb.num_illegal_blocks = 0;
904                         pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
905                         pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
906                         pb.inode = inode;
907                         pb.pctx = &pctx;
908                         pb.ctx = ctx;
909                         pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
910                                      block_buf, process_bad_block, &pb);
911                         ext2fs_free_block_bitmap(pb.fs_meta_blocks);
912                         if (pctx.errcode) {
913                                 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
914                                 ctx->flags |= E2F_FLAG_ABORT;
915                                 return;
916                         }
917                         if (pb.bbcheck)
918                                 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
919                                 ctx->flags |= E2F_FLAG_ABORT;
920                                 return;
921                         }
922                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
923                         clear_problem_context(&pctx);
924                         continue;
925                 } else if (ino == EXT2_ROOT_INO) {
926                         /*
927                          * Make sure the root inode is a directory; if
928                          * not, offer to clear it.  It will be
929                          * regnerated in pass #3.
930                          */
931                         if (!LINUX_S_ISDIR(inode->i_mode)) {
932                                 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
933                                         goto clear_inode;
934                         }
935                         /*
936                          * If dtime is set, offer to clear it.  mke2fs
937                          * version 0.2b created filesystems with the
938                          * dtime field set for the root and lost+found
939                          * directories.  We won't worry about
940                          * /lost+found, since that can be regenerated
941                          * easily.  But we will fix the root directory
942                          * as a special case.
943                          */
944                         if (inode->i_dtime && inode->i_links_count) {
945                                 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
946                                         inode->i_dtime = 0;
947                                         e2fsck_write_inode(ctx, ino, inode,
948                                                            "pass1");
949                                 }
950                         }
951                 } else if (ino == EXT2_JOURNAL_INO) {
952                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
953                         if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
954                                 if (!LINUX_S_ISREG(inode->i_mode) &&
955                                     fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
956                                                 &pctx)) {
957                                         inode->i_mode = LINUX_S_IFREG;
958                                         e2fsck_write_inode(ctx, ino, inode,
959                                                            "pass1");
960                                 }
961                                 check_blocks(ctx, &pctx, block_buf);
962                                 continue;
963                         }
964                         if ((inode->i_links_count ||
965                              inode->i_blocks || inode->i_block[0]) &&
966                             fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
967                                         &pctx)) {
968                                 memset(inode, 0, inode_size);
969                                 ext2fs_icount_store(ctx->inode_link_info,
970                                                     ino, 0);
971                                 e2fsck_write_inode_full(ctx, ino, inode,
972                                                         inode_size, "pass1");
973                         }
974                 } else if ((ino == EXT4_USR_QUOTA_INO) ||
975                            (ino == EXT4_GRP_QUOTA_INO)) {
976                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
977                         if ((fs->super->s_feature_ro_compat &
978                                         EXT4_FEATURE_RO_COMPAT_QUOTA) &&
979                             ((fs->super->s_usr_quota_inum == ino) ||
980                              (fs->super->s_grp_quota_inum == ino))) {
981                                 if (!LINUX_S_ISREG(inode->i_mode) &&
982                                     fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
983                                                         &pctx)) {
984                                         inode->i_mode = LINUX_S_IFREG;
985                                         e2fsck_write_inode(ctx, ino, inode,
986                                                         "pass1");
987                                 }
988                                 check_blocks(ctx, &pctx, block_buf);
989                                 continue;
990                         }
991                         if ((inode->i_links_count ||
992                              inode->i_blocks || inode->i_block[0]) &&
993                             fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
994                                         &pctx)) {
995                                 memset(inode, 0, inode_size);
996                                 ext2fs_icount_store(ctx->inode_link_info,
997                                                     ino, 0);
998                                 e2fsck_write_inode_full(ctx, ino, inode,
999                                                         inode_size, "pass1");
1000                         }
1001                 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1002                         int     problem = 0;
1003
1004                         ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1005                         if (ino == EXT2_BOOT_LOADER_INO) {
1006                                 if (LINUX_S_ISDIR(inode->i_mode))
1007                                         problem = PR_1_RESERVED_BAD_MODE;
1008                         } else if (ino == EXT2_RESIZE_INO) {
1009                                 if (inode->i_mode &&
1010                                     !LINUX_S_ISREG(inode->i_mode))
1011                                         problem = PR_1_RESERVED_BAD_MODE;
1012                         } else {
1013                                 if (inode->i_mode != 0)
1014                                         problem = PR_1_RESERVED_BAD_MODE;
1015                         }
1016                         if (problem) {
1017                                 if (fix_problem(ctx, problem, &pctx)) {
1018                                         inode->i_mode = 0;
1019                                         e2fsck_write_inode(ctx, ino, inode,
1020                                                            "pass1");
1021                                 }
1022                         }
1023                         check_blocks(ctx, &pctx, block_buf);
1024                         continue;
1025                 }
1026
1027                 /*
1028                  * Check for inodes who might have been part of the
1029                  * orphaned list linked list.  They should have gotten
1030                  * dealt with by now, unless the list had somehow been
1031                  * corrupted.
1032                  *
1033                  * FIXME: In the future, inodes which are still in use
1034                  * (and which are therefore) pending truncation should
1035                  * be handled specially.  Right now we just clear the
1036                  * dtime field, and the normal e2fsck handling of
1037                  * inodes where i_size and the inode blocks are
1038                  * inconsistent is to fix i_size, instead of releasing
1039                  * the extra blocks.  This won't catch the inodes that
1040                  * was at the end of the orphan list, but it's better
1041                  * than nothing.  The right answer is that there
1042                  * shouldn't be any bugs in the orphan list handling.  :-)
1043                  */
1044                 if (inode->i_dtime && !busted_fs_time &&
1045                     inode->i_dtime < ctx->fs->super->s_inodes_count) {
1046                         if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1047                                 inode->i_dtime = inode->i_links_count ?
1048                                         0 : ctx->now;
1049                                 e2fsck_write_inode(ctx, ino, inode,
1050                                                    "pass1");
1051                         }
1052                 }
1053
1054                 /*
1055                  * This code assumes that deleted inodes have
1056                  * i_links_count set to 0.
1057                  */
1058                 if (!inode->i_links_count) {
1059                         if (!inode->i_dtime && inode->i_mode) {
1060                                 if (fix_problem(ctx,
1061                                             PR_1_ZERO_DTIME, &pctx)) {
1062                                         inode->i_dtime = ctx->now;
1063                                         e2fsck_write_inode(ctx, ino, inode,
1064                                                            "pass1");
1065                                 }
1066                         }
1067                         continue;
1068                 }
1069                 /*
1070                  * n.b.  0.3c ext2fs code didn't clear i_links_count for
1071                  * deleted files.  Oops.
1072                  *
1073                  * Since all new ext2 implementations get this right,
1074                  * we now assume that the case of non-zero
1075                  * i_links_count and non-zero dtime means that we
1076                  * should keep the file, not delete it.
1077                  *
1078                  */
1079                 if (inode->i_dtime) {
1080                         if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1081                                 inode->i_dtime = 0;
1082                                 e2fsck_write_inode(ctx, ino, inode, "pass1");
1083                         }
1084                 }
1085
1086                 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1087                 switch (fs->super->s_creator_os) {
1088                     case EXT2_OS_HURD:
1089                         frag = inode->osd2.hurd2.h_i_frag;
1090                         fsize = inode->osd2.hurd2.h_i_fsize;
1091                         break;
1092                     default:
1093                         frag = fsize = 0;
1094                 }
1095
1096                 if (inode->i_faddr || frag || fsize ||
1097                     (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
1098                         mark_inode_bad(ctx, ino);
1099                 if (!(fs->super->s_feature_incompat & 
1100                       EXT4_FEATURE_INCOMPAT_64BIT) &&
1101                     inode->osd2.linux2.l_i_file_acl_high != 0)
1102                         mark_inode_bad(ctx, ino);
1103                 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1104                     !(fs->super->s_feature_ro_compat &
1105                       EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1106                     (inode->osd2.linux2.l_i_blocks_hi != 0))
1107                         mark_inode_bad(ctx, ino);
1108                 if (inode->i_flags & EXT2_IMAGIC_FL) {
1109                         if (imagic_fs) {
1110                                 if (!ctx->inode_imagic_map)
1111                                         alloc_imagic_map(ctx);
1112                                 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1113                                                          ino);
1114                         } else {
1115                                 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1116                                         inode->i_flags &= ~EXT2_IMAGIC_FL;
1117                                         e2fsck_write_inode(ctx, ino,
1118                                                            inode, "pass1");
1119                                 }
1120                         }
1121                 }
1122
1123                 check_inode_extra_space(ctx, &pctx);
1124                 check_is_really_dir(ctx, &pctx, block_buf);
1125
1126                 /*
1127                  * ext2fs_inode_has_valid_blocks2 does not actually look
1128                  * at i_block[] values, so not endian-sensitive here.
1129                  */
1130                 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1131                     LINUX_S_ISLNK(inode->i_mode) &&
1132                     !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1133                     fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1134                         inode->i_flags &= ~EXT4_EXTENTS_FL;
1135                         e2fsck_write_inode(ctx, ino, inode, "pass1");
1136                 }
1137
1138                 if (LINUX_S_ISDIR(inode->i_mode)) {
1139                         ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1140                         e2fsck_add_dir_info(ctx, ino, 0);
1141                         ctx->fs_directory_count++;
1142                 } else if (LINUX_S_ISREG (inode->i_mode)) {
1143                         ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1144                         ctx->fs_regular_count++;
1145                 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1146                            e2fsck_pass1_check_device_inode(fs, inode)) {
1147                         check_immutable(ctx, &pctx);
1148                         check_size(ctx, &pctx);
1149                         ctx->fs_chardev_count++;
1150                 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1151                            e2fsck_pass1_check_device_inode(fs, inode)) {
1152                         check_immutable(ctx, &pctx);
1153                         check_size(ctx, &pctx);
1154                         ctx->fs_blockdev_count++;
1155                 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1156                            e2fsck_pass1_check_symlink(fs, ino, inode,
1157                                                       block_buf)) {
1158                         check_immutable(ctx, &pctx);
1159                         ctx->fs_symlinks_count++;
1160                         if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1161                                 ctx->fs_fast_symlinks_count++;
1162                                 check_blocks(ctx, &pctx, block_buf);
1163                                 continue;
1164                         }
1165                 }
1166                 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1167                          e2fsck_pass1_check_device_inode(fs, inode)) {
1168                         check_immutable(ctx, &pctx);
1169                         check_size(ctx, &pctx);
1170                         ctx->fs_fifo_count++;
1171                 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1172                            e2fsck_pass1_check_device_inode(fs, inode)) {
1173                         check_immutable(ctx, &pctx);
1174                         check_size(ctx, &pctx);
1175                         ctx->fs_sockets_count++;
1176                 } else
1177                         mark_inode_bad(ctx, ino);
1178                 if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
1179                         if (inode->i_block[EXT2_IND_BLOCK])
1180                                 ctx->fs_ind_count++;
1181                         if (inode->i_block[EXT2_DIND_BLOCK])
1182                                 ctx->fs_dind_count++;
1183                         if (inode->i_block[EXT2_TIND_BLOCK])
1184                                 ctx->fs_tind_count++;
1185                 }
1186                 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1187                     (inode->i_block[EXT2_IND_BLOCK] ||
1188                      inode->i_block[EXT2_DIND_BLOCK] ||
1189                      inode->i_block[EXT2_TIND_BLOCK] ||
1190                      ext2fs_file_acl_block(fs, inode))) {
1191                         inodes_to_process[process_inode_count].ino = ino;
1192                         inodes_to_process[process_inode_count].inode = *inode;
1193                         process_inode_count++;
1194                 } else
1195                         check_blocks(ctx, &pctx, block_buf);
1196
1197                 /*
1198                  * If the inode failed the checksum and the user didn't
1199                  * clear the inode, test the checksum again -- if it still
1200                  * fails, ask the user if the checksum should be corrected.
1201                  */
1202                 if (failed_csum) {
1203                         pctx.errcode = recheck_bad_inode_checksum(fs, ino, ctx,
1204                                                                   &pctx);
1205                         if (pctx.errcode) {
1206                                 ctx->flags |= E2F_FLAG_ABORT;
1207                                 return;
1208                         }
1209                 }
1210
1211                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1212                         return;
1213
1214                 if (process_inode_count >= ctx->process_inode_size) {
1215                         process_inodes(ctx, block_buf);
1216
1217                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1218                                 return;
1219                 }
1220         }
1221         process_inodes(ctx, block_buf);
1222         ext2fs_close_inode_scan(scan);
1223
1224         /*
1225          * If any extended attribute blocks' reference counts need to
1226          * be adjusted, either up (ctx->refcount_extra), or down
1227          * (ctx->refcount), then fix them.
1228          */
1229         if (ctx->refcount) {
1230                 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1231                 ea_refcount_free(ctx->refcount);
1232                 ctx->refcount = 0;
1233         }
1234         if (ctx->refcount_extra) {
1235                 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1236                                         block_buf, +1);
1237                 ea_refcount_free(ctx->refcount_extra);
1238                 ctx->refcount_extra = 0;
1239         }
1240
1241         if (ctx->invalid_bitmaps)
1242                 handle_fs_bad_blocks(ctx);
1243
1244         /* We don't need the block_ea_map any more */
1245         if (ctx->block_ea_map) {
1246                 ext2fs_free_block_bitmap(ctx->block_ea_map);
1247                 ctx->block_ea_map = 0;
1248         }
1249
1250         if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1251                 ext2fs_block_bitmap save_bmap;
1252
1253                 save_bmap = fs->block_map;
1254                 fs->block_map = ctx->block_found_map;
1255                 clear_problem_context(&pctx);
1256                 pctx.errcode = ext2fs_create_resize_inode(fs);
1257                 if (pctx.errcode) {
1258                         if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1259                                          &pctx)) {
1260                                 ctx->flags |= E2F_FLAG_ABORT;
1261                                 return;
1262                         }
1263                         pctx.errcode = 0;
1264                 }
1265                 if (!pctx.errcode) {
1266                         e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1267                                           "recreate inode");
1268                         inode->i_mtime = ctx->now;
1269                         e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1270                                            "recreate inode");
1271                 }
1272                 fs->block_map = save_bmap;
1273                 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1274         }
1275
1276         if (ctx->flags & E2F_FLAG_RESTART) {
1277                 /*
1278                  * Only the master copy of the superblock and block
1279                  * group descriptors are going to be written during a
1280                  * restart, so set the superblock to be used to be the
1281                  * master superblock.
1282                  */
1283                 ctx->use_superblock = 0;
1284                 unwind_pass1(fs);
1285                 goto endit;
1286         }
1287
1288         if (ctx->block_dup_map) {
1289                 if (ctx->options & E2F_OPT_PREEN) {
1290                         clear_problem_context(&pctx);
1291                         fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1292                 }
1293                 e2fsck_pass1_dupblocks(ctx, block_buf);
1294         }
1295         ext2fs_free_mem(&inodes_to_process);
1296 endit:
1297         e2fsck_use_inode_shortcuts(ctx, 0);
1298
1299         ext2fs_free_mem(&block_buf);
1300         ext2fs_free_mem(&inode);
1301
1302         /*
1303          * The l+f inode may have been cleared, so zap it now and
1304          * later passes will recalculate it if necessary
1305          */
1306         ctx->lost_and_found = 0;
1307
1308         print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1309 }
1310
1311 /*
1312  * When the inode_scan routines call this callback at the end of the
1313  * glock group, call process_inodes.
1314  */
1315 static errcode_t scan_callback(ext2_filsys fs,
1316                                ext2_inode_scan scan EXT2FS_ATTR((unused)),
1317                                dgrp_t group, void * priv_data)
1318 {
1319         struct scan_callback_struct *scan_struct;
1320         e2fsck_t ctx;
1321
1322         scan_struct = (struct scan_callback_struct *) priv_data;
1323         ctx = scan_struct->ctx;
1324
1325         process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1326
1327         if (ctx->progress)
1328                 if ((ctx->progress)(ctx, 1, group+1,
1329                                     ctx->fs->group_desc_count))
1330                         return EXT2_ET_CANCEL_REQUESTED;
1331
1332         return 0;
1333 }
1334
1335 /*
1336  * Process the inodes in the "inodes to process" list.
1337  */
1338 static void process_inodes(e2fsck_t ctx, char *block_buf)
1339 {
1340         int                     i;
1341         struct ext2_inode       *old_stashed_inode;
1342         ext2_ino_t              old_stashed_ino;
1343         const char              *old_operation;
1344         char                    buf[80];
1345         struct problem_context  pctx;
1346
1347 #if 0
1348         printf("begin process_inodes: ");
1349 #endif
1350         if (process_inode_count == 0)
1351                 return;
1352         old_operation = ehandler_operation(0);
1353         old_stashed_inode = ctx->stashed_inode;
1354         old_stashed_ino = ctx->stashed_ino;
1355         qsort(inodes_to_process, process_inode_count,
1356                       sizeof(struct process_inode_block), process_inode_cmp);
1357         clear_problem_context(&pctx);
1358         for (i=0; i < process_inode_count; i++) {
1359                 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1360                 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1361
1362 #if 0
1363                 printf("%u ", pctx.ino);
1364 #endif
1365                 sprintf(buf, _("reading indirect blocks of inode %u"),
1366                         pctx.ino);
1367                 ehandler_operation(buf);
1368                 check_blocks(ctx, &pctx, block_buf);
1369                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1370                         break;
1371         }
1372         ctx->stashed_inode = old_stashed_inode;
1373         ctx->stashed_ino = old_stashed_ino;
1374         process_inode_count = 0;
1375 #if 0
1376         printf("end process inodes\n");
1377 #endif
1378         ehandler_operation(old_operation);
1379 }
1380
1381 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1382 {
1383         const struct process_inode_block *ib_a =
1384                 (const struct process_inode_block *) a;
1385         const struct process_inode_block *ib_b =
1386                 (const struct process_inode_block *) b;
1387         int     ret;
1388
1389         ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1390                ib_b->inode.i_block[EXT2_IND_BLOCK]);
1391         if (ret == 0)
1392                 /*
1393                  * We only call process_inodes() for non-extent
1394                  * inodes, so it's OK to pass NULL to
1395                  * ext2fs_file_acl_block() here.
1396                  */
1397                 ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
1398                         ext2fs_file_acl_block(0, &(ib_b->inode));
1399         if (ret == 0)
1400                 ret = ib_a->ino - ib_b->ino;
1401         return ret;
1402 }
1403
1404 /*
1405  * Mark an inode as being bad in some what
1406  */
1407 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
1408 {
1409         struct          problem_context pctx;
1410
1411         if (!ctx->inode_bad_map) {
1412                 clear_problem_context(&pctx);
1413
1414                 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1415                                 _("bad inode map"), EXT2FS_BMAP64_RBTREE,
1416                                 "inode_bad_map", &ctx->inode_bad_map);
1417                 if (pctx.errcode) {
1418                         pctx.num = 3;
1419                         fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1420                         /* Should never get here */
1421                         ctx->flags |= E2F_FLAG_ABORT;
1422                         return;
1423                 }
1424         }
1425         ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
1426 }
1427
1428
1429 /*
1430  * This procedure will allocate the inode "bb" (badblock) map table
1431  */
1432 static void alloc_bb_map(e2fsck_t ctx)
1433 {
1434         struct          problem_context pctx;
1435
1436         clear_problem_context(&pctx);
1437         pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1438                         _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
1439                         "inode_bb_map", &ctx->inode_bb_map);
1440         if (pctx.errcode) {
1441                 pctx.num = 4;
1442                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1443                 /* Should never get here */
1444                 ctx->flags |= E2F_FLAG_ABORT;
1445                 return;
1446         }
1447 }
1448
1449 /*
1450  * This procedure will allocate the inode imagic table
1451  */
1452 static void alloc_imagic_map(e2fsck_t ctx)
1453 {
1454         struct          problem_context pctx;
1455
1456         clear_problem_context(&pctx);
1457         pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1458                         _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
1459                         "inode_imagic_map", &ctx->inode_imagic_map);
1460         if (pctx.errcode) {
1461                 pctx.num = 5;
1462                 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1463                 /* Should never get here */
1464                 ctx->flags |= E2F_FLAG_ABORT;
1465                 return;
1466         }
1467 }
1468
1469 /*
1470  * Marks a block as in use, setting the dup_map if it's been set
1471  * already.  Called by process_block and process_bad_block.
1472  *
1473  * WARNING: Assumes checks have already been done to make sure block
1474  * is valid.  This is true in both process_block and process_bad_block.
1475  */
1476 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
1477 {
1478         struct          problem_context pctx;
1479
1480         clear_problem_context(&pctx);
1481
1482         if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
1483                 if (!ctx->block_dup_map) {
1484                         pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1485                                         _("multiply claimed block map"),
1486                                         EXT2FS_BMAP64_RBTREE, "block_dup_map",
1487                                         &ctx->block_dup_map);
1488                         if (pctx.errcode) {
1489                                 pctx.num = 3;
1490                                 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1491                                             &pctx);
1492                                 /* Should never get here */
1493                                 ctx->flags |= E2F_FLAG_ABORT;
1494                                 return;
1495                         }
1496                 }
1497                 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
1498         } else {
1499                 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
1500         }
1501 }
1502
1503 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
1504                                       unsigned int num)
1505 {
1506         if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
1507                 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
1508         else
1509                 while (num--)
1510                         mark_block_used(ctx, block++);
1511 }
1512
1513 /*
1514  * Adjust the extended attribute block's reference counts at the end
1515  * of pass 1, either by subtracting out references for EA blocks that
1516  * are still referenced in ctx->refcount, or by adding references for
1517  * EA blocks that had extra references as accounted for in
1518  * ctx->refcount_extra.
1519  */
1520 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1521                                     char *block_buf, int adjust_sign)
1522 {
1523         struct ext2_ext_attr_header     *header;
1524         struct problem_context          pctx;
1525         ext2_filsys                     fs = ctx->fs;
1526         blk64_t                         blk;
1527         __u32                           should_be;
1528         int                             count;
1529
1530         clear_problem_context(&pctx);
1531
1532         ea_refcount_intr_begin(refcount);
1533         while (1) {
1534                 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1535                         break;
1536                 pctx.blk = blk;
1537                 pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
1538                                                      pctx.ino);
1539                 if (pctx.errcode) {
1540                         fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1541                         return;
1542                 }
1543                 header = (struct ext2_ext_attr_header *) block_buf;
1544                 pctx.blkcount = header->h_refcount;
1545                 should_be = header->h_refcount + adjust_sign * count;
1546                 pctx.num = should_be;
1547                 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1548                         header->h_refcount = should_be;
1549                         pctx.errcode = ext2fs_write_ext_attr3(fs, blk,
1550                                                              block_buf,
1551                                                              pctx.ino);
1552                         if (pctx.errcode) {
1553                                 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
1554                                             &pctx);
1555                                 continue;
1556                         }
1557                 }
1558         }
1559 }
1560
1561 /*
1562  * Handle processing the extended attribute blocks
1563  */
1564 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1565                            char *block_buf)
1566 {
1567         ext2_filsys fs = ctx->fs;
1568         ext2_ino_t      ino = pctx->ino;
1569         struct ext2_inode *inode = pctx->inode;
1570         blk64_t         blk;
1571         char *          end;
1572         struct ext2_ext_attr_header *header;
1573         struct ext2_ext_attr_entry *entry;
1574         int             count;
1575         region_t        region = 0;
1576         int             failed_csum = 0;
1577
1578         blk = ext2fs_file_acl_block(fs, inode);
1579         if (blk == 0)
1580                 return 0;
1581
1582         /*
1583          * If the Extended attribute flag isn't set, then a non-zero
1584          * file acl means that the inode is corrupted.
1585          *
1586          * Or if the extended attribute block is an invalid block,
1587          * then the inode is also corrupted.
1588          */
1589         if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1590             (blk < fs->super->s_first_data_block) ||
1591             (blk >= ext2fs_blocks_count(fs->super))) {
1592                 mark_inode_bad(ctx, ino);
1593                 return 0;
1594         }
1595
1596         /* If ea bitmap hasn't been allocated, create it */
1597         if (!ctx->block_ea_map) {
1598                 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
1599                                         _("ext attr block map"),
1600                                         EXT2FS_BMAP64_RBTREE, "block_ea_map",
1601                                         &ctx->block_ea_map);
1602                 if (pctx->errcode) {
1603                         pctx->num = 2;
1604                         fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1605                         ctx->flags |= E2F_FLAG_ABORT;
1606                         return 0;
1607                 }
1608         }
1609
1610         /* Create the EA refcount structure if necessary */
1611         if (!ctx->refcount) {
1612                 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1613                 if (pctx->errcode) {
1614                         pctx->num = 1;
1615                         fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1616                         ctx->flags |= E2F_FLAG_ABORT;
1617                         return 0;
1618                 }
1619         }
1620
1621 #if 0
1622         /* Debugging text */
1623         printf("Inode %u has EA block %u\n", ino, blk);
1624 #endif
1625
1626         /* Have we seen this EA block before? */
1627         if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
1628                 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1629                         return 1;
1630                 /* Ooops, this EA was referenced more than it stated */
1631                 if (!ctx->refcount_extra) {
1632                         pctx->errcode = ea_refcount_create(0,
1633                                            &ctx->refcount_extra);
1634                         if (pctx->errcode) {
1635                                 pctx->num = 2;
1636                                 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1637                                 ctx->flags |= E2F_FLAG_ABORT;
1638                                 return 0;
1639                         }
1640                 }
1641                 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1642                 return 1;
1643         }
1644
1645         /*
1646          * OK, we haven't seen this EA block yet.  So we need to
1647          * validate it
1648          */
1649         pctx->blk = blk;
1650         pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
1651         if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
1652                 if (fix_problem(ctx, PR_1_EA_BLOCK_CSUM_INVALID, pctx))
1653                         goto clear_extattr;
1654                 failed_csum = 1;
1655         }
1656         if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1657                 goto clear_extattr;
1658         header = (struct ext2_ext_attr_header *) block_buf;
1659         pctx->blk = ext2fs_file_acl_block(fs, inode);
1660         if (((ctx->ext_attr_ver == 1) &&
1661              (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1662             ((ctx->ext_attr_ver == 2) &&
1663              (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1664                 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1665                         goto clear_extattr;
1666         }
1667
1668         if (header->h_blocks != 1) {
1669                 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1670                         goto clear_extattr;
1671         }
1672
1673         region = region_create(0, fs->blocksize);
1674         if (!region) {
1675                 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
1676                 ctx->flags |= E2F_FLAG_ABORT;
1677                 return 0;
1678         }
1679         if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1680                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1681                         goto clear_extattr;
1682         }
1683
1684         entry = (struct ext2_ext_attr_entry *)(header+1);
1685         end = block_buf + fs->blocksize;
1686         while ((char *)entry < end && *(__u32 *)entry) {
1687                 __u32 hash;
1688
1689                 if (region_allocate(region, (char *)entry - (char *)header,
1690                                    EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1691                         if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1692                                 goto clear_extattr;
1693                         break;
1694                 }
1695                 if ((ctx->ext_attr_ver == 1 &&
1696                      (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1697                     (ctx->ext_attr_ver == 2 &&
1698                      entry->e_name_index == 0)) {
1699                         if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1700                                 goto clear_extattr;
1701                         break;
1702                 }
1703                 if (entry->e_value_block != 0) {
1704                         if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1705                                 goto clear_extattr;
1706                 }
1707                 if (entry->e_value_offs + entry->e_value_size > fs->blocksize) {
1708                         if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1709                                 goto clear_extattr;
1710                         break;
1711                 }
1712                 if (entry->e_value_size &&
1713                     region_allocate(region, entry->e_value_offs,
1714                                     EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1715                         if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1716                                 goto clear_extattr;
1717                 }
1718
1719                 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
1720                                                          entry->e_value_offs);
1721
1722                 if (entry->e_hash != hash) {
1723                         pctx->num = entry->e_hash;
1724                         if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
1725                                 goto clear_extattr;
1726                         entry->e_hash = hash;
1727                 }
1728
1729                 entry = EXT2_EXT_ATTR_NEXT(entry);
1730         }
1731         if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1732                 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1733                         goto clear_extattr;
1734         }
1735         region_free(region);
1736
1737         /*
1738          * We only get here if there was no other errors that were fixed.
1739          * If there was a checksum fail, ask to correct it.
1740          */
1741         if (failed_csum &&
1742             fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
1743                 pctx->errcode = ext2fs_write_ext_attr3(fs, blk, block_buf,
1744                                                        pctx->ino);
1745                 if (pctx->errcode)
1746                         return 0;
1747         }
1748
1749         count = header->h_refcount - 1;
1750         if (count)
1751                 ea_refcount_store(ctx->refcount, blk, count);
1752         mark_block_used(ctx, blk);
1753         ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
1754         return 1;
1755
1756 clear_extattr:
1757         if (region)
1758                 region_free(region);
1759         ext2fs_file_acl_block_set(fs, inode, 0);
1760         e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1761         return 0;
1762 }
1763
1764 /* Returns 1 if bad htree, 0 if OK */
1765 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1766                         ext2_ino_t ino, struct ext2_inode *inode,
1767                         char *block_buf)
1768 {
1769         struct ext2_dx_root_info        *root;
1770         ext2_filsys                     fs = ctx->fs;
1771         errcode_t                       retval;
1772         blk64_t                         blk;
1773
1774         if ((!LINUX_S_ISDIR(inode->i_mode) &&
1775              fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1776             (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1777              fix_problem(ctx, PR_1_HTREE_SET, pctx)))
1778                 return 1;
1779
1780         pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
1781
1782         if ((pctx->errcode) ||
1783             (blk == 0) ||
1784             (blk < fs->super->s_first_data_block) ||
1785             (blk >= ext2fs_blocks_count(fs->super))) {
1786                 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1787                         return 1;
1788                 else
1789                         return 0;
1790         }
1791
1792         retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
1793         if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1794                 return 1;
1795
1796         /* XXX should check that beginning matches a directory */
1797         root = (struct ext2_dx_root_info *) (block_buf + 24);
1798
1799         if ((root->reserved_zero || root->info_length < 8) &&
1800             fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1801                 return 1;
1802
1803         pctx->num = root->hash_version;
1804         if ((root->hash_version != EXT2_HASH_LEGACY) &&
1805             (root->hash_version != EXT2_HASH_HALF_MD4) &&
1806             (root->hash_version != EXT2_HASH_TEA) &&
1807             fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
1808                 return 1;
1809
1810         if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
1811             fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
1812                 return 1;
1813
1814         pctx->num = root->indirect_levels;
1815         if ((root->indirect_levels > 1) &&
1816             fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
1817                 return 1;
1818
1819         return 0;
1820 }
1821
1822 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
1823                         struct ext2_inode *inode, int restart_flag,
1824                         const char *source)
1825 {
1826         inode->i_flags = 0;
1827         inode->i_links_count = 0;
1828         ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1829         inode->i_dtime = ctx->now;
1830
1831         ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
1832         ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
1833         if (ctx->inode_reg_map)
1834                 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
1835         if (ctx->inode_bad_map)
1836                 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
1837
1838         /*
1839          * If the inode was partially accounted for before processing
1840          * was aborted, we need to restart the pass 1 scan.
1841          */
1842         ctx->flags |= restart_flag;
1843
1844         if (ino == EXT2_BAD_INO)
1845                 memset(inode, 0, sizeof(struct ext2_inode));
1846
1847         e2fsck_write_inode(ctx, ino, inode, source);
1848 }
1849
1850 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
1851                              struct process_block_struct *pb,
1852                              blk64_t start_block,
1853                              ext2_extent_handle_t ehandle)
1854 {
1855         struct ext2fs_extent    extent;
1856         blk64_t                 blk;
1857         e2_blkcnt_t             blockcnt;
1858         unsigned int            i;
1859         int                     is_dir, is_leaf;
1860         errcode_t               problem;
1861         struct ext2_extent_info info;
1862         int                     failed_csum;
1863
1864         pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
1865         if (pctx->errcode)
1866                 return;
1867
1868         pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
1869                                           &extent);
1870         while ((pctx->errcode == 0 ||
1871                 pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
1872                info.num_entries-- > 0) {
1873                 failed_csum = 0;
1874                 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
1875                 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
1876
1877                 problem = 0;
1878                 /* Ask to clear a corrupt extent block */
1879                 if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) {
1880                         pctx->blk = extent.e_pblk;
1881                         pctx->blk2 = extent.e_lblk;
1882                         pctx->num = extent.e_len;
1883                         problem = PR_1_EXTENT_CSUM_INVALID;
1884                         if (fix_problem(ctx, problem, pctx))
1885                                 goto fix_problem_now;
1886                         failed_csum = 1;
1887                 }
1888
1889                 if (extent.e_pblk == 0 ||
1890                     extent.e_pblk < ctx->fs->super->s_first_data_block ||
1891                     extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
1892                         problem = PR_1_EXTENT_BAD_START_BLK;
1893                 else if (extent.e_lblk < start_block)
1894                         problem = PR_1_OUT_OF_ORDER_EXTENTS;
1895                 else if (is_leaf && extent.e_len == 0)
1896                         problem = PR_1_EXTENT_LENGTH_ZERO;
1897                 else if (is_leaf &&
1898                          (extent.e_pblk + extent.e_len) >
1899                          ext2fs_blocks_count(ctx->fs->super))
1900                         problem = PR_1_EXTENT_ENDS_BEYOND;
1901
1902                 /* Corrupt but passes checks?  Ask to fix checksum. */
1903                 if (failed_csum) {
1904                         pctx->blk = extent.e_pblk;
1905                         pctx->blk2 = extent.e_lblk;
1906                         pctx->num = extent.e_len;
1907                         problem = 0;
1908                         if (fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID,
1909                                         pctx))
1910                                 ext2fs_extent_replace(ehandle, 0, &extent);
1911                 }
1912
1913                 if (problem) {
1914                 report_problem:
1915                         pctx->blk = extent.e_pblk;
1916                         pctx->blk2 = extent.e_lblk;
1917                         pctx->num = extent.e_len;
1918                         if (fix_problem(ctx, problem, pctx)) {
1919 fix_problem_now:
1920                                 e2fsck_read_bitmaps(ctx);
1921                                 pctx->errcode =
1922                                         ext2fs_extent_delete(ehandle, 0);
1923                                 if (pctx->errcode) {
1924                                         pctx->str = "ext2fs_extent_delete";
1925                                         return;
1926                                 }
1927                                 pctx->errcode = ext2fs_extent_get(ehandle,
1928                                                                   EXT2_EXTENT_CURRENT,
1929                                                                   &extent);
1930                                 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
1931                                         pctx->errcode = 0;
1932                                         break;
1933                                 }
1934                                 continue;
1935                         }
1936                         goto next;
1937                 }
1938
1939                 if (!is_leaf) {
1940                         blk = extent.e_pblk;
1941                         pctx->errcode = ext2fs_extent_get(ehandle,
1942                                                   EXT2_EXTENT_DOWN, &extent);
1943                         if (pctx->errcode) {
1944                                 pctx->str = "EXT2_EXTENT_DOWN";
1945                                 problem = PR_1_EXTENT_HEADER_INVALID;
1946                                 if (pctx->errcode ==
1947                                         EXT2_ET_EXTENT_HEADER_BAD ||
1948                                     pctx->errcode ==
1949                                         EXT2_ET_EXTENT_CSUM_INVALID)
1950                                         goto report_problem;
1951                                 return;
1952                         }
1953                         scan_extent_node(ctx, pctx, pb, extent.e_lblk, ehandle);
1954                         if (pctx->errcode)
1955                                 return;
1956                         pctx->errcode = ext2fs_extent_get(ehandle,
1957                                                   EXT2_EXTENT_UP, &extent);
1958                         if (pctx->errcode) {
1959                                 pctx->str = "EXT2_EXTENT_UP";
1960                                 return;
1961                         }
1962                         mark_block_used(ctx, blk);
1963                         pb->num_blocks++;
1964                         goto next;
1965                 }
1966
1967                 if ((pb->previous_block != 0) &&
1968                     (pb->previous_block+1 != extent.e_pblk)) {
1969                         if (ctx->options & E2F_OPT_FRAGCHECK) {
1970                                 char type = '?';
1971
1972                                 if (pb->is_dir)
1973                                         type = 'd';
1974                                 else if (pb->is_reg)
1975                                         type = 'f';
1976
1977                                 printf(("%6lu(%c): expecting %6lu "
1978                                         "actual extent "
1979                                         "phys %6lu log %lu len %lu\n"),
1980                                        (unsigned long) pctx->ino, type,
1981                                        (unsigned long) pb->previous_block+1,
1982                                        (unsigned long) extent.e_pblk,
1983                                        (unsigned long) extent.e_lblk,
1984                                        (unsigned long) extent.e_len);
1985                         }
1986                         pb->fragmented = 1;
1987                 }
1988                 while (is_dir && ++pb->last_db_block < extent.e_lblk) {
1989                         pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
1990                                                               pb->ino, 0,
1991                                                               pb->last_db_block);
1992                         if (pctx->errcode) {
1993                                 pctx->blk = 0;
1994                                 pctx->num = pb->last_db_block;
1995                                 goto failed_add_dir_block;
1996                         }
1997                 }
1998                 if (!ctx->fs->cluster_ratio_bits) {
1999                         mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
2000                         pb->num_blocks += extent.e_len;
2001                 }
2002                 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
2003                      i < extent.e_len;
2004                      blk++, blockcnt++, i++) {
2005                         if (ctx->fs->cluster_ratio_bits &&
2006                             !(pb->previous_block &&
2007                               (EXT2FS_B2C(ctx->fs, blk) ==
2008                                EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
2009                               (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2010                               (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2011                                 mark_block_used(ctx, blk);
2012                                 pb->num_blocks++;
2013                         }
2014
2015                         pb->previous_block = blk;
2016
2017                         if (is_dir) {
2018                                 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
2019                                 if (pctx->errcode) {
2020                                         pctx->blk = blk;
2021                                         pctx->num = blockcnt;
2022                                 failed_add_dir_block:
2023                                         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2024                                         /* Should never get here */
2025                                         ctx->flags |= E2F_FLAG_ABORT;
2026                                         return;
2027                                 }
2028                         }
2029                 }
2030                 if (is_dir && extent.e_len > 0)
2031                         pb->last_db_block = blockcnt - 1;
2032                 pb->previous_block = extent.e_pblk + extent.e_len - 1;
2033                 start_block = pb->last_block = extent.e_lblk + extent.e_len - 1;
2034                 if (is_leaf && !is_dir &&
2035                     !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
2036                         pb->last_init_lblock = extent.e_lblk + extent.e_len - 1;
2037         next:
2038                 pctx->errcode = ext2fs_extent_get(ehandle,
2039                                                   EXT2_EXTENT_NEXT_SIB,
2040                                                   &extent);
2041         }
2042         if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
2043                 pctx->errcode = 0;
2044 }
2045
2046 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
2047                                  struct process_block_struct *pb)
2048 {
2049         struct ext2_extent_info info;
2050         struct ext2_inode       *inode = pctx->inode;
2051         ext2_extent_handle_t    ehandle;
2052         ext2_filsys             fs = ctx->fs;
2053         ext2_ino_t              ino = pctx->ino;
2054         errcode_t               retval;
2055
2056         pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
2057         if (pctx->errcode) {
2058                 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
2059                         e2fsck_clear_inode(ctx, ino, inode, 0,
2060                                            "check_blocks_extents");
2061                 pctx->errcode = 0;
2062                 return;
2063         }
2064
2065         retval = ext2fs_extent_get_info(ehandle, &info);
2066         if (retval == 0) {
2067                 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
2068                         info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
2069                 ctx->extent_depth_count[info.max_depth]++;
2070         }
2071
2072         scan_extent_node(ctx, pctx, pb, 0, ehandle);
2073         if (pctx->errcode &&
2074             fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
2075                 pb->num_blocks = 0;
2076                 inode->i_blocks = 0;
2077                 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2078                                    "check_blocks_extents");
2079                 pctx->errcode = 0;
2080         }
2081         ext2fs_extent_free(ehandle);
2082 }
2083
2084 /*
2085  * This subroutine is called on each inode to account for all of the
2086  * blocks used by that inode.
2087  */
2088 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
2089                          char *block_buf)
2090 {
2091         ext2_filsys fs = ctx->fs;
2092         struct process_block_struct pb;
2093         ext2_ino_t      ino = pctx->ino;
2094         struct ext2_inode *inode = pctx->inode;
2095         int             bad_size = 0;
2096         int             dirty_inode = 0;
2097         int             extent_fs;
2098         __u64           size;
2099
2100         pb.ino = ino;
2101         pb.num_blocks = 0;
2102         pb.last_block = -1;
2103         pb.last_init_lblock = -1;
2104         pb.last_db_block = -1;
2105         pb.num_illegal_blocks = 0;
2106         pb.suppress = 0; pb.clear = 0;
2107         pb.fragmented = 0;
2108         pb.compressed = 0;
2109         pb.previous_block = 0;
2110         pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2111         pb.is_reg = LINUX_S_ISREG(inode->i_mode);
2112         pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
2113         pb.inode = inode;
2114         pb.pctx = pctx;
2115         pb.ctx = ctx;
2116         pctx->ino = ino;
2117         pctx->errcode = 0;
2118
2119         extent_fs = (ctx->fs->super->s_feature_incompat &
2120                      EXT3_FEATURE_INCOMPAT_EXTENTS);
2121
2122         if (inode->i_flags & EXT2_COMPRBLK_FL) {
2123                 if (fs->super->s_feature_incompat &
2124                     EXT2_FEATURE_INCOMPAT_COMPRESSION)
2125                         pb.compressed = 1;
2126                 else {
2127                         if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
2128                                 inode->i_flags &= ~EXT2_COMPRBLK_FL;
2129                                 dirty_inode++;
2130                         }
2131                 }
2132         }
2133
2134         if (ext2fs_file_acl_block(fs, inode) &&
2135             check_ext_attr(ctx, pctx, block_buf)) {
2136                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2137                         goto out;
2138                 pb.num_blocks++;
2139         }
2140
2141         if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
2142                 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
2143                         check_blocks_extents(ctx, pctx, &pb);
2144                 else {
2145                         pctx->errcode = ext2fs_block_iterate3(fs, ino,
2146                                                 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
2147                                                 block_buf, process_block, &pb);
2148                         /*
2149                          * We do not have uninitialized extents in non extent
2150                          * files.
2151                          */
2152                         pb.last_init_lblock = pb.last_block;
2153                 }
2154         }
2155         end_problem_latch(ctx, PR_LATCH_BLOCK);
2156         end_problem_latch(ctx, PR_LATCH_TOOBIG);
2157         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2158                 goto out;
2159         if (pctx->errcode)
2160                 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
2161
2162         if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
2163                 if (LINUX_S_ISDIR(inode->i_mode))
2164                         ctx->fs_fragmented_dir++;
2165                 else
2166                         ctx->fs_fragmented++;
2167         }
2168
2169         if (pb.clear) {
2170                 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2171                                    "check_blocks");
2172                 return;
2173         }
2174
2175         if (inode->i_flags & EXT2_INDEX_FL) {
2176                 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
2177                         inode->i_flags &= ~EXT2_INDEX_FL;
2178                         dirty_inode++;
2179                 } else {
2180 #ifdef ENABLE_HTREE
2181                         e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
2182 #endif
2183                 }
2184         }
2185
2186         if (!pb.num_blocks && pb.is_dir) {
2187                 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
2188                         e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
2189                         ctx->fs_directory_count--;
2190                         return;
2191                 }
2192         }
2193
2194         if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
2195                 quota_data_add(ctx->qctx, inode, ino,
2196                                pb.num_blocks * fs->blocksize);
2197                 quota_data_inodes(ctx->qctx, inode, ino, +1);
2198         }
2199
2200         if (!(fs->super->s_feature_ro_compat &
2201               EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
2202             !(inode->i_flags & EXT4_HUGE_FILE_FL))
2203                 pb.num_blocks *= (fs->blocksize / 512);
2204         pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
2205 #if 0
2206         printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
2207                ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
2208                pb.num_blocks);
2209 #endif
2210         if (pb.is_dir) {
2211                 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
2212                 if (inode->i_size & (fs->blocksize - 1))
2213                         bad_size = 5;
2214                 else if (nblock > (pb.last_block + 1))
2215                         bad_size = 1;
2216                 else if (nblock < (pb.last_block + 1)) {
2217                         if (((pb.last_block + 1) - nblock) >
2218                             fs->super->s_prealloc_dir_blocks)
2219                                 bad_size = 2;
2220                 }
2221         } else {
2222                 e2_blkcnt_t blkpg = ctx->blocks_per_page;
2223
2224                 size = EXT2_I_SIZE(inode);
2225                 if ((pb.last_init_lblock >= 0) &&
2226                     /* allow allocated blocks to end of PAGE_SIZE */
2227                     (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
2228                     (pb.last_init_lblock / blkpg * blkpg != pb.last_init_lblock ||
2229                      size < (__u64)(pb.last_init_lblock & ~(blkpg-1)) *
2230                      fs->blocksize))
2231                         bad_size = 3;
2232                 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2233                          size > ext2_max_sizes[fs->super->s_log_block_size])
2234                         /* too big for a direct/indirect-mapped file */
2235                         bad_size = 4;
2236                 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2237                          size >
2238                          ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
2239                         /* too big for an extent-based file - 32bit ee_block */
2240                         bad_size = 6;
2241         }
2242         /* i_size for symlinks is checked elsewhere */
2243         if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
2244                 pctx->num = (pb.last_block+1) * fs->blocksize;
2245                 pctx->group = bad_size;
2246                 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
2247                         inode->i_size = pctx->num;
2248                         if (!LINUX_S_ISDIR(inode->i_mode))
2249                                 inode->i_size_high = pctx->num >> 32;
2250                         dirty_inode++;
2251                 }
2252                 pctx->num = 0;
2253         }
2254         if (LINUX_S_ISREG(inode->i_mode) && EXT2_I_SIZE(inode) >= 0x80000000UL)
2255                 ctx->large_files++;
2256         if ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
2257             ((fs->super->s_feature_ro_compat &
2258               EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
2259              (inode->i_flags & EXT4_HUGE_FILE_FL) &&
2260              (inode->osd2.linux2.l_i_blocks_hi != 0))) {
2261                 pctx->num = pb.num_blocks;
2262                 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
2263                         inode->i_blocks = pb.num_blocks;
2264                         inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
2265                         dirty_inode++;
2266                 }
2267                 pctx->num = 0;
2268         }
2269
2270         if (ctx->dirs_to_hash && pb.is_dir &&
2271             !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
2272             !(inode->i_flags & EXT2_INDEX_FL) &&
2273             ((inode->i_size / fs->blocksize) >= 3))
2274                 e2fsck_rehash_dir_later(ctx, ino);
2275
2276 out:
2277         if (dirty_inode)
2278                 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
2279 }
2280
2281 #if 0
2282 /*
2283  * Helper function called by process block when an illegal block is
2284  * found.  It returns a description about why the block is illegal
2285  */
2286 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
2287 {
2288         blk64_t super;
2289         int     i;
2290         static char     problem[80];
2291
2292         super = fs->super->s_first_data_block;
2293         strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
2294         if (block < super) {
2295                 sprintf(problem, "< FIRSTBLOCK (%u)", super);
2296                 return(problem);
2297         } else if (block >= ext2fs_blocks_count(fs->super)) {
2298                 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
2299                 return(problem);
2300         }
2301         for (i = 0; i < fs->group_desc_count; i++) {
2302                 if (block == super) {
2303                         sprintf(problem, "is the superblock in group %d", i);
2304                         break;
2305                 }
2306                 if (block > super &&
2307                     block <= (super + fs->desc_blocks)) {
2308                         sprintf(problem, "is in the group descriptors "
2309                                 "of group %d", i);
2310                         break;
2311                 }
2312                 if (block == ext2fs_block_bitmap_loc(fs, i)) {
2313                         sprintf(problem, "is the block bitmap of group %d", i);
2314                         break;
2315                 }
2316                 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
2317                         sprintf(problem, "is the inode bitmap of group %d", i);
2318                         break;
2319                 }
2320                 if (block >= ext2fs_inode_table_loc(fs, i) &&
2321                     (block < ext2fs_inode_table_loc(fs, i)
2322                      + fs->inode_blocks_per_group)) {
2323                         sprintf(problem, "is in the inode table of group %d",
2324                                 i);
2325                         break;
2326                 }
2327                 super += fs->super->s_blocks_per_group;
2328         }
2329         return(problem);
2330 }
2331 #endif
2332
2333 /*
2334  * This is a helper function for check_blocks().
2335  */
2336 static int process_block(ext2_filsys fs,
2337                   blk64_t       *block_nr,
2338                   e2_blkcnt_t blockcnt,
2339                   blk64_t ref_block EXT2FS_ATTR((unused)),
2340                   int ref_offset EXT2FS_ATTR((unused)),
2341                   void *priv_data)
2342 {
2343         struct process_block_struct *p;
2344         struct problem_context *pctx;
2345         blk64_t blk = *block_nr;
2346         int     ret_code = 0;
2347         int     problem = 0;
2348         e2fsck_t        ctx;
2349
2350         p = (struct process_block_struct *) priv_data;
2351         pctx = p->pctx;
2352         ctx = p->ctx;
2353
2354         if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
2355                 /* todo: Check that the comprblk_fl is high, that the
2356                    blkaddr pattern looks right (all non-holes up to
2357                    first EXT2FS_COMPRESSED_BLKADDR, then all
2358                    EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2359                    that the feature_incompat bit is high, and that the
2360                    inode is a regular file.  If we're doing a "full
2361                    check" (a concept introduced to e2fsck by e2compr,
2362                    meaning that we look at data blocks as well as
2363                    metadata) then call some library routine that
2364                    checks the compressed data.  I'll have to think
2365                    about this, because one particularly important
2366                    problem to be able to fix is to recalculate the
2367                    cluster size if necessary.  I think that perhaps
2368                    we'd better do most/all e2compr-specific checks
2369                    separately, after the non-e2compr checks.  If not
2370                    doing a full check, it may be useful to test that
2371                    the personality is linux; e.g. if it isn't then
2372                    perhaps this really is just an illegal block. */
2373                 return 0;
2374         }
2375
2376         if (blk == 0)
2377                 return 0;
2378
2379 #if 0
2380         printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
2381                blockcnt);
2382 #endif
2383
2384         /*
2385          * Simplistic fragmentation check.  We merely require that the
2386          * file be contiguous.  (Which can never be true for really
2387          * big files that are greater than a block group.)
2388          */
2389         if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
2390                 if (p->previous_block+1 != blk) {
2391                         if (ctx->options & E2F_OPT_FRAGCHECK) {
2392                                 char type = '?';
2393
2394                                 if (p->is_dir)
2395                                         type = 'd';
2396                                 else if (p->is_reg)
2397                                         type = 'f';
2398
2399                                 printf(_("%6lu(%c): expecting %6lu "
2400                                          "got phys %6lu (blkcnt %lld)\n"),
2401                                        (unsigned long) pctx->ino, type,
2402                                        (unsigned long) p->previous_block+1,
2403                                        (unsigned long) blk,
2404                                        blockcnt);
2405                         }
2406                         p->fragmented = 1;
2407                 }
2408         }
2409
2410         if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
2411                 problem = PR_1_TOOBIG_DIR;
2412         if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
2413                 problem = PR_1_TOOBIG_REG;
2414         if (!p->is_dir && !p->is_reg && blockcnt > 0)
2415                 problem = PR_1_TOOBIG_SYMLINK;
2416
2417         if (blk < fs->super->s_first_data_block ||
2418             blk >= ext2fs_blocks_count(fs->super))
2419                 problem = PR_1_ILLEGAL_BLOCK_NUM;
2420
2421         if (problem) {
2422                 p->num_illegal_blocks++;
2423                 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
2424                         if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
2425                                 p->clear = 1;
2426                                 return BLOCK_ABORT;
2427                         }
2428                         if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
2429                                 p->suppress = 1;
2430                                 set_latch_flags(PR_LATCH_BLOCK,
2431                                                 PRL_SUPPRESS, 0);
2432                         }
2433                 }
2434                 pctx->blk = blk;
2435                 pctx->blkcount = blockcnt;
2436                 if (fix_problem(ctx, problem, pctx)) {
2437                         blk = *block_nr = 0;
2438                         ret_code = BLOCK_CHANGED;
2439                         goto mark_dir;
2440                 } else
2441                         return 0;
2442         }
2443
2444         if (p->ino == EXT2_RESIZE_INO) {
2445                 /*
2446                  * The resize inode has already be sanity checked
2447                  * during pass #0 (the superblock checks).  All we
2448                  * have to do is mark the double indirect block as
2449                  * being in use; all of the other blocks are handled
2450                  * by mark_table_blocks()).
2451                  */
2452                 if (blockcnt == BLOCK_COUNT_DIND)
2453                         mark_block_used(ctx, blk);
2454                 p->num_blocks++;
2455         } else if (!(ctx->fs->cluster_ratio_bits &&
2456                      p->previous_block &&
2457                      (EXT2FS_B2C(ctx->fs, blk) ==
2458                       EXT2FS_B2C(ctx->fs, p->previous_block)) &&
2459                      (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2460                      (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2461                 mark_block_used(ctx, blk);
2462                 p->num_blocks++;
2463         }
2464         if (blockcnt >= 0)
2465                 p->last_block = blockcnt;
2466         p->previous_block = blk;
2467 mark_dir:
2468         if (p->is_dir && (blockcnt >= 0)) {
2469                 while (++p->last_db_block < blockcnt) {
2470                         pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
2471                                                               p->ino, 0,
2472                                                               p->last_db_block);
2473                         if (pctx->errcode) {
2474                                 pctx->blk = 0;
2475                                 pctx->num = p->last_db_block;
2476                                 goto failed_add_dir_block;
2477                         }
2478                 }
2479                 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
2480                                                       blk, blockcnt);
2481                 if (pctx->errcode) {
2482                         pctx->blk = blk;
2483                         pctx->num = blockcnt;
2484                 failed_add_dir_block:
2485                         fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2486                         /* Should never get here */
2487                         ctx->flags |= E2F_FLAG_ABORT;
2488                         return BLOCK_ABORT;
2489                 }
2490         }
2491         return ret_code;
2492 }
2493
2494 static int process_bad_block(ext2_filsys fs,
2495                       blk64_t *block_nr,
2496                       e2_blkcnt_t blockcnt,
2497                       blk64_t ref_block EXT2FS_ATTR((unused)),
2498                       int ref_offset EXT2FS_ATTR((unused)),
2499                       void *priv_data)
2500 {
2501         struct process_block_struct *p;
2502         blk64_t         blk = *block_nr;
2503         blk64_t         first_block;
2504         dgrp_t          i;
2505         struct problem_context *pctx;
2506         e2fsck_t        ctx;
2507
2508         /*
2509          * Note: This function processes blocks for the bad blocks
2510          * inode, which is never compressed.  So we don't use HOLE_BLKADDR().
2511          */
2512
2513         if (!blk)
2514                 return 0;
2515
2516         p = (struct process_block_struct *) priv_data;
2517         ctx = p->ctx;
2518         pctx = p->pctx;
2519
2520         pctx->ino = EXT2_BAD_INO;
2521         pctx->blk = blk;
2522         pctx->blkcount = blockcnt;
2523
2524         if ((blk < fs->super->s_first_data_block) ||
2525             (blk >= ext2fs_blocks_count(fs->super))) {
2526                 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
2527                         *block_nr = 0;
2528                         return BLOCK_CHANGED;
2529                 } else
2530                         return 0;
2531         }
2532
2533         if (blockcnt < 0) {
2534                 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
2535                         p->bbcheck = 1;
2536                         if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
2537                                 *block_nr = 0;
2538                                 return BLOCK_CHANGED;
2539                         }
2540                 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2541                                                     blk)) {
2542                         p->bbcheck = 1;
2543                         if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
2544                                         pctx)) {
2545                                 *block_nr = 0;
2546                                 return BLOCK_CHANGED;
2547                         }
2548                         if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2549                                 return BLOCK_ABORT;
2550                 } else
2551                         mark_block_used(ctx, blk);
2552                 return 0;
2553         }
2554 #if 0
2555         printf ("DEBUG: Marking %u as bad.\n", blk);
2556 #endif
2557         ctx->fs_badblocks_count++;
2558         /*
2559          * If the block is not used, then mark it as used and return.
2560          * If it is already marked as found, this must mean that
2561          * there's an overlap between the filesystem table blocks
2562          * (bitmaps and inode table) and the bad block list.
2563          */
2564         if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
2565                 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
2566                 return 0;
2567         }
2568         /*
2569          * Try to find the where the filesystem block was used...
2570          */
2571         first_block = fs->super->s_first_data_block;
2572
2573         for (i = 0; i < fs->group_desc_count; i++ ) {
2574                 pctx->group = i;
2575                 pctx->blk = blk;
2576                 if (!ext2fs_bg_has_super(fs, i))
2577                         goto skip_super;
2578                 if (blk == first_block) {
2579                         if (i == 0) {
2580                                 if (fix_problem(ctx,
2581                                                 PR_1_BAD_PRIMARY_SUPERBLOCK,
2582                                                 pctx)) {
2583                                         *block_nr = 0;
2584                                         return BLOCK_CHANGED;
2585                                 }
2586                                 return 0;
2587                         }
2588                         fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
2589                         return 0;
2590                 }
2591                 if ((blk > first_block) &&
2592                     (blk <= first_block + fs->desc_blocks)) {
2593                         if (i == 0) {
2594                                 pctx->blk = *block_nr;
2595                                 if (fix_problem(ctx,
2596                         PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
2597                                         *block_nr = 0;
2598                                         return BLOCK_CHANGED;
2599                                 }
2600                                 return 0;
2601                         }
2602                         fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
2603                         return 0;
2604                 }
2605         skip_super:
2606                 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
2607                         if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
2608                                 ctx->invalid_block_bitmap_flag[i]++;
2609                                 ctx->invalid_bitmaps++;
2610                         }
2611                         return 0;
2612                 }
2613                 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
2614                         if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
2615                                 ctx->invalid_inode_bitmap_flag[i]++;
2616                                 ctx->invalid_bitmaps++;
2617                         }
2618                         return 0;
2619                 }
2620                 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
2621                     (blk < (ext2fs_inode_table_loc(fs, i) +
2622                             fs->inode_blocks_per_group))) {
2623                         /*
2624                          * If there are bad blocks in the inode table,
2625                          * the inode scan code will try to do
2626                          * something reasonable automatically.
2627                          */
2628                         return 0;
2629                 }
2630                 first_block += fs->super->s_blocks_per_group;
2631         }
2632         /*
2633          * If we've gotten to this point, then the only
2634          * possibility is that the bad block inode meta data
2635          * is using a bad block.
2636          */
2637         if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
2638             (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
2639             (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
2640                 p->bbcheck = 1;
2641                 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
2642                         *block_nr = 0;
2643                         return BLOCK_CHANGED;
2644                 }
2645                 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2646                         return BLOCK_ABORT;
2647                 return 0;
2648         }
2649
2650         pctx->group = -1;
2651
2652         /* Warn user that the block wasn't claimed */
2653         fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
2654
2655         return 0;
2656 }
2657
2658 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
2659                             const char *name, int num, blk64_t *new_block)
2660 {
2661         ext2_filsys fs = ctx->fs;
2662         dgrp_t          last_grp;
2663         blk64_t         old_block = *new_block;
2664         blk64_t         last_block;
2665         int             i, is_flexbg, flexbg, flexbg_size;
2666         char            *buf;
2667         struct problem_context  pctx;
2668
2669         clear_problem_context(&pctx);
2670
2671         pctx.group = group;
2672         pctx.blk = old_block;
2673         pctx.str = name;
2674
2675         /*
2676          * For flex_bg filesystems, first try to allocate the metadata
2677          * within the flex_bg, and if that fails then try finding the
2678          * space anywhere in the filesystem.
2679          */
2680         is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2681                                               EXT4_FEATURE_INCOMPAT_FLEX_BG);
2682         if (is_flexbg) {
2683                 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2684                 flexbg = group / flexbg_size;
2685                 first_block = ext2fs_group_first_block2(fs,
2686                                                         flexbg_size * flexbg);
2687                 last_grp = group | (flexbg_size - 1);
2688                 if (last_grp > fs->group_desc_count)
2689                         last_grp = fs->group_desc_count;
2690                 last_block = ext2fs_group_last_block2(fs, last_grp);
2691         } else
2692                 last_block = ext2fs_group_last_block2(fs, group);
2693         pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
2694                                                num, ctx->block_found_map,
2695                                                new_block);
2696         if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
2697                 pctx.errcode = ext2fs_get_free_blocks2(fs,
2698                                 fs->super->s_first_data_block,
2699                                 ext2fs_blocks_count(fs->super),
2700                                 num, ctx->block_found_map, new_block);
2701         if (pctx.errcode) {
2702                 pctx.num = num;
2703                 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
2704                 ext2fs_unmark_valid(fs);
2705                 ctx->flags |= E2F_FLAG_ABORT;
2706                 return;
2707         }
2708         pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
2709         if (pctx.errcode) {
2710                 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
2711                 ext2fs_unmark_valid(fs);
2712                 ctx->flags |= E2F_FLAG_ABORT;
2713                 return;
2714         }
2715         ext2fs_mark_super_dirty(fs);
2716         fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2717         pctx.blk2 = *new_block;
2718         fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
2719                           PR_1_RELOC_TO), &pctx);
2720         pctx.blk2 = 0;
2721         for (i = 0; i < num; i++) {
2722                 pctx.blk = i;
2723                 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
2724                 if (old_block) {
2725                         pctx.errcode = io_channel_read_blk64(fs->io,
2726                                    old_block + i, 1, buf);
2727                         if (pctx.errcode)
2728                                 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
2729                 } else
2730                         memset(buf, 0, fs->blocksize);
2731
2732                 pctx.blk = (*new_block) + i;
2733                 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
2734                                               1, buf);
2735                 if (pctx.errcode)
2736                         fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
2737         }
2738         ext2fs_free_mem(&buf);
2739 }
2740
2741 /*
2742  * This routine gets called at the end of pass 1 if bad blocks are
2743  * detected in the superblock, group descriptors, inode_bitmaps, or
2744  * block bitmaps.  At this point, all of the blocks have been mapped
2745  * out, so we can try to allocate new block(s) to replace the bad
2746  * blocks.
2747  */
2748 static void handle_fs_bad_blocks(e2fsck_t ctx)
2749 {
2750         ext2_filsys fs = ctx->fs;
2751         dgrp_t          i;
2752         blk64_t         first_block;
2753         blk64_t         new_blk;
2754
2755         for (i = 0; i < fs->group_desc_count; i++) {
2756                 first_block = ext2fs_group_first_block2(fs, i);
2757
2758                 if (ctx->invalid_block_bitmap_flag[i]) {
2759                         new_blk = ext2fs_block_bitmap_loc(fs, i);
2760                         new_table_block(ctx, first_block, i, _("block bitmap"),
2761                                         1, &new_blk);
2762                         ext2fs_block_bitmap_loc_set(fs, i, new_blk);
2763                 }
2764                 if (ctx->invalid_inode_bitmap_flag[i]) {
2765                         new_blk = ext2fs_inode_bitmap_loc(fs, i);
2766                         new_table_block(ctx, first_block, i, _("inode bitmap"),
2767                                         1, &new_blk);
2768                         ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
2769                 }
2770                 if (ctx->invalid_inode_table_flag[i]) {
2771                         new_blk = ext2fs_inode_table_loc(fs, i);
2772                         new_table_block(ctx, first_block, i, _("inode table"),
2773                                         fs->inode_blocks_per_group,
2774                                         &new_blk);
2775                         ext2fs_inode_table_loc_set(fs, i, new_blk);
2776                         ctx->flags |= E2F_FLAG_RESTART;
2777                 }
2778         }
2779         ctx->invalid_bitmaps = 0;
2780 }
2781
2782 /*
2783  * This routine marks all blocks which are used by the superblock,
2784  * group descriptors, inode bitmaps, and block bitmaps.
2785  */
2786 static void mark_table_blocks(e2fsck_t ctx)
2787 {
2788         ext2_filsys fs = ctx->fs;
2789         blk64_t b;
2790         dgrp_t  i;
2791         int     j;
2792         struct problem_context pctx;
2793
2794         clear_problem_context(&pctx);
2795
2796         for (i = 0; i < fs->group_desc_count; i++) {
2797                 pctx.group = i;
2798
2799                 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
2800
2801                 /*
2802                  * Mark the blocks used for the inode table
2803                  */
2804                 if (ext2fs_inode_table_loc(fs, i)) {
2805                         for (j = 0, b = ext2fs_inode_table_loc(fs, i);
2806                              j < fs->inode_blocks_per_group;
2807                              j++, b++) {
2808                                 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2809                                                              b)) {
2810                                         pctx.blk = b;
2811                                         if (!ctx->invalid_inode_table_flag[i] &&
2812                                             fix_problem(ctx,
2813                                                 PR_1_ITABLE_CONFLICT, &pctx)) {
2814                                                 ctx->invalid_inode_table_flag[i]++;
2815                                                 ctx->invalid_bitmaps++;
2816                                         }
2817                                 } else {
2818                                     ext2fs_mark_block_bitmap2(ctx->block_found_map,
2819                                                              b);
2820                                 }
2821                         }
2822                 }
2823
2824                 /*
2825                  * Mark block used for the block bitmap
2826                  */
2827                 if (ext2fs_block_bitmap_loc(fs, i)) {
2828                         if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2829                                      ext2fs_block_bitmap_loc(fs, i))) {
2830                                 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
2831                                 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
2832                                         ctx->invalid_block_bitmap_flag[i]++;
2833                                         ctx->invalid_bitmaps++;
2834                                 }
2835                         } else {
2836                             ext2fs_mark_block_bitmap2(ctx->block_found_map,
2837                                      ext2fs_block_bitmap_loc(fs, i));
2838                     }
2839
2840                 }
2841                 /*
2842                  * Mark block used for the inode bitmap
2843                  */
2844                 if (ext2fs_inode_bitmap_loc(fs, i)) {
2845                         if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2846                                      ext2fs_inode_bitmap_loc(fs, i))) {
2847                                 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
2848                                 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
2849                                         ctx->invalid_inode_bitmap_flag[i]++;
2850                                         ctx->invalid_bitmaps++;
2851                                 }
2852                         } else {
2853                             ext2fs_mark_block_bitmap2(ctx->block_found_map,
2854                                      ext2fs_inode_bitmap_loc(fs, i));
2855                         }
2856                 }
2857         }
2858 }
2859
2860 /*
2861  * Thes subroutines short circuits ext2fs_get_blocks and
2862  * ext2fs_check_directory; we use them since we already have the inode
2863  * structure, so there's no point in letting the ext2fs library read
2864  * the inode again.
2865  */
2866 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
2867                                   blk_t *blocks)
2868 {
2869         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2870         int     i;
2871
2872         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2873                 return EXT2_ET_CALLBACK_NOTHANDLED;
2874
2875         for (i=0; i < EXT2_N_BLOCKS; i++)
2876                 blocks[i] = ctx->stashed_inode->i_block[i];
2877         return 0;
2878 }
2879
2880 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
2881                                   struct ext2_inode *inode)
2882 {
2883         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2884
2885         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2886                 return EXT2_ET_CALLBACK_NOTHANDLED;
2887         *inode = *ctx->stashed_inode;
2888         return 0;
2889 }
2890
2891 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
2892                             struct ext2_inode *inode)
2893 {
2894         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2895
2896         if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
2897                 (inode != ctx->stashed_inode))
2898                 *ctx->stashed_inode = *inode;
2899         return EXT2_ET_CALLBACK_NOTHANDLED;
2900 }
2901
2902 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
2903 {
2904         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2905
2906         if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2907                 return EXT2_ET_CALLBACK_NOTHANDLED;
2908
2909         if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
2910                 return EXT2_ET_NO_DIRECTORY;
2911         return 0;
2912 }
2913
2914 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
2915                                         blk64_t *ret)
2916 {
2917         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2918         errcode_t       retval;
2919         blk64_t         new_block;
2920
2921         if (ctx->block_found_map) {
2922                 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
2923                                            &new_block);
2924                 if (retval)
2925                         return retval;
2926                 if (fs->block_map) {
2927                         ext2fs_mark_block_bitmap2(fs->block_map, new_block);
2928                         ext2fs_mark_bb_dirty(fs);
2929                 }
2930         } else {
2931                 if (!fs->block_map) {
2932                         retval = ext2fs_read_block_bitmap(fs);
2933                         if (retval)
2934                                 return retval;
2935                 }
2936
2937                 retval = ext2fs_new_block2(fs, goal, 0, &new_block);
2938                 if (retval)
2939                         return retval;
2940         }
2941
2942         *ret = new_block;
2943         return (0);
2944 }
2945
2946 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
2947 {
2948         e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2949
2950         if (ctx->block_found_map) {
2951                 if (inuse > 0)
2952                         ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
2953                 else
2954                         ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
2955         }
2956 }
2957
2958 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
2959 {
2960         ext2_filsys fs = ctx->fs;
2961
2962         if (use_shortcuts) {
2963                 fs->get_blocks = pass1_get_blocks;
2964                 fs->check_directory = pass1_check_directory;
2965                 fs->read_inode = pass1_read_inode;
2966                 fs->write_inode = pass1_write_inode;
2967                 ctx->stashed_ino = 0;
2968                 ext2fs_set_alloc_block_callback(fs, e2fsck_get_alloc_block,
2969                                                 0);
2970                 ext2fs_set_block_alloc_stats_callback(fs,
2971                                                       e2fsck_block_alloc_stats,
2972                                                       0);
2973         } else {
2974                 fs->get_blocks = 0;
2975                 fs->check_directory = 0;
2976                 fs->read_inode = 0;
2977                 fs->write_inode = 0;
2978         }
2979 }