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