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