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