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