pass1.c revision cf491d3a64ac0d6c1349e9672a5edaca3901300c
1/*
2 * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
3 *
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 *
11 * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12 * and applies the following tests to each inode:
13 *
14 * 	- The mode field of the inode must be legal.
15 * 	- The size and block count fields of the inode are correct.
16 * 	- A data block must not be used by another inode
17 *
18 * Pass 1 also gathers the collects the following information:
19 *
20 * 	- A bitmap of which inodes are in use.		(inode_used_map)
21 * 	- A bitmap of which inodes are directories.	(inode_dir_map)
22 * 	- A bitmap of which inodes are regular files.	(inode_reg_map)
23 * 	- A bitmap of which inodes have bad fields.	(inode_bad_map)
24 * 	- A bitmap of which inodes are in bad blocks.	(inode_bb_map)
25 * 	- A bitmap of which inodes are imagic inodes.	(inode_imagic_map)
26 * 	- A bitmap of which blocks are in use.		(block_found_map)
27 * 	- A bitmap of which blocks are in use by two inodes	(block_dup_map)
28 * 	- The data blocks of the directory inodes.	(dir_map)
29 *
30 * Pass 1 is designed to stash away enough information so that the
31 * other passes should not need to read in the inode information
32 * during the normal course of a filesystem check.  (Althogh if an
33 * inconsistency is detected, other passes may need to read in an
34 * inode to fix it.)
35 *
36 * Note that pass 1B will be invoked if there are any duplicate blocks
37 * found.
38 */
39
40#define _GNU_SOURCE 1 /* get strnlen() */
41#include "config.h"
42#include <string.h>
43#include <time.h>
44#ifdef HAVE_ERRNO_H
45#include <errno.h>
46#endif
47
48#include "e2fsck.h"
49#include <ext2fs/ext2_ext_attr.h>
50
51#include "problem.h"
52
53#ifdef NO_INLINE_FUNCS
54#define _INLINE_
55#else
56#define _INLINE_ inline
57#endif
58
59#undef DEBUG
60
61static int process_block(ext2_filsys fs, blk64_t	*blocknr,
62			 e2_blkcnt_t blockcnt, blk64_t ref_blk,
63			 int ref_offset, void *priv_data);
64static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
65			     e2_blkcnt_t blockcnt, blk64_t ref_blk,
66			     int ref_offset, void *priv_data);
67static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
68			 char *block_buf);
69static void mark_table_blocks(e2fsck_t ctx);
70static void alloc_bb_map(e2fsck_t ctx);
71static void alloc_imagic_map(e2fsck_t ctx);
72static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
73static void add_encrypted_dir(e2fsck_t ctx, ino_t ino);
74static void handle_fs_bad_blocks(e2fsck_t ctx);
75static void process_inodes(e2fsck_t ctx, char *block_buf);
76static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
77static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
78				  dgrp_t group, void * priv_data);
79static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
80				    char *block_buf, int adjust_sign);
81/* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
82
83struct process_block_struct {
84	ext2_ino_t	ino;
85	unsigned	is_dir:1, is_reg:1, clear:1, suppress:1,
86				fragmented:1, compressed:1, bbcheck:1,
87				inode_modified:1;
88	blk64_t		num_blocks;
89	blk64_t		max_blocks;
90	e2_blkcnt_t	last_block;
91	e2_blkcnt_t	last_init_lblock;
92	e2_blkcnt_t	last_db_block;
93	int		num_illegal_blocks;
94	blk64_t		previous_block;
95	struct ext2_inode *inode;
96	struct problem_context *pctx;
97	ext2fs_block_bitmap fs_meta_blocks;
98	e2fsck_t	ctx;
99	region_t	region;
100	struct extent_tree_info	eti;
101};
102
103struct process_inode_block {
104	ext2_ino_t ino;
105	struct ext2_inode inode;
106};
107
108struct scan_callback_struct {
109	e2fsck_t	ctx;
110	char		*block_buf;
111};
112
113/*
114 * For the inodes to process list.
115 */
116static struct process_inode_block *inodes_to_process;
117static int process_inode_count;
118
119static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
120			    EXT2_MIN_BLOCK_LOG_SIZE + 1];
121
122/*
123 * Free all memory allocated by pass1 in preparation for restarting
124 * things.
125 */
126static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
127{
128	ext2fs_free_mem(&inodes_to_process);
129	inodes_to_process = 0;
130}
131
132/*
133 * Check to make sure a device inode is real.  Returns 1 if the device
134 * checks out, 0 if not.
135 *
136 * Note: this routine is now also used to check FIFO's and Sockets,
137 * since they have the same requirement; the i_block fields should be
138 * zero.
139 */
140int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
141				    struct ext2_inode *inode)
142{
143	int	i;
144
145	/*
146	 * If the index flag is set, then this is a bogus
147	 * device/fifo/socket
148	 */
149	if (inode->i_flags & EXT2_INDEX_FL)
150		return 0;
151
152	/*
153	 * We should be able to do the test below all the time, but
154	 * because the kernel doesn't forcibly clear the device
155	 * inode's additional i_block fields, there are some rare
156	 * occasions when a legitimate device inode will have non-zero
157	 * additional i_block fields.  So for now, we only complain
158	 * when the immutable flag is set, which should never happen
159	 * for devices.  (And that's when the problem is caused, since
160	 * you can't set or clear immutable flags for devices.)  Once
161	 * the kernel has been fixed we can change this...
162	 */
163	if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
164		for (i=4; i < EXT2_N_BLOCKS; i++)
165			if (inode->i_block[i])
166				return 0;
167	}
168	return 1;
169}
170
171/*
172 * Check to make sure a symlink inode is real.  Returns 1 if the symlink
173 * checks out, 0 if not.
174 */
175int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
176			       struct ext2_inode *inode, char *buf)
177{
178	unsigned int len;
179	int i;
180	blk64_t	blocks;
181	ext2_extent_handle_t	handle;
182	struct ext2_extent_info	info;
183	struct ext2fs_extent	extent;
184
185	if ((inode->i_size_high || inode->i_size == 0) ||
186	    (inode->i_flags & EXT2_INDEX_FL))
187		return 0;
188
189	if (inode->i_flags & EXT4_EXTENTS_FL) {
190		if (inode->i_flags & EXT4_INLINE_DATA_FL)
191			return 0;
192		if (inode->i_size > fs->blocksize)
193			return 0;
194		if (ext2fs_extent_open2(fs, ino, inode, &handle))
195			return 0;
196		i = 0;
197		if (ext2fs_extent_get_info(handle, &info) ||
198		    (info.num_entries != 1) ||
199		    (info.max_depth != 0))
200			goto exit_extent;
201		if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
202		    (extent.e_lblk != 0) ||
203		    (extent.e_len != 1) ||
204		    (extent.e_pblk < fs->super->s_first_data_block) ||
205		    (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
206			goto exit_extent;
207		i = 1;
208	exit_extent:
209		ext2fs_extent_free(handle);
210		return i;
211	}
212
213	if (inode->i_flags & EXT4_INLINE_DATA_FL) {
214		size_t inline_size;
215
216		if (ext2fs_inline_data_size(fs, ino, &inline_size))
217			return 0;
218		if (inode->i_size != inline_size)
219			return 0;
220
221		return 1;
222	}
223
224	blocks = ext2fs_inode_data_blocks2(fs, inode);
225	if (blocks) {
226		if (inode->i_flags & EXT4_INLINE_DATA_FL)
227			return 0;
228		if ((inode->i_size >= fs->blocksize) ||
229		    (blocks != fs->blocksize >> 9) ||
230		    (inode->i_block[0] < fs->super->s_first_data_block) ||
231		    (inode->i_block[0] >= ext2fs_blocks_count(fs->super)))
232			return 0;
233
234		for (i = 1; i < EXT2_N_BLOCKS; i++)
235			if (inode->i_block[i])
236				return 0;
237
238		if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf))
239			return 0;
240
241		if (inode->i_flags & EXT4_ENCRYPT_FL) {
242			len = ext2fs_le32_to_cpu(*((__u32 *)buf)) + 4;
243		} else {
244			len = strnlen(buf, fs->blocksize);
245		}
246		if (len == fs->blocksize)
247			return 0;
248	} else if (inode->i_flags & EXT4_INLINE_DATA_FL) {
249		char *inline_buf = NULL;
250		size_t inline_sz = 0;
251
252		if (ext2fs_inline_data_size(fs, ino, &inline_sz))
253			return 0;
254		if (inode->i_size != inline_sz)
255			return 0;
256		if (ext2fs_get_mem(inline_sz + 1, &inline_buf))
257			return 0;
258		i = 0;
259		if (ext2fs_inline_data_get(fs, ino, inode, inline_buf, NULL))
260			goto exit_inline;
261		inline_buf[inline_sz] = 0;
262		len = strnlen(inline_buf, inline_sz);
263		if (len != inline_sz)
264			goto exit_inline;
265		i = 1;
266exit_inline:
267		ext2fs_free_mem(&inline_buf);
268		return i;
269	} else {
270		if (inode->i_size >= sizeof(inode->i_block))
271			return 0;
272
273		len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
274		if (len == sizeof(inode->i_block))
275			return 0;
276	}
277	if (len != inode->i_size)
278		if ((inode->i_flags & EXT4_ENCRYPT_FL) == 0)
279			return 0;
280	return 1;
281}
282
283/*
284 * If the extents or inlinedata flags are set on the inode, offer to clear 'em.
285 */
286#define BAD_SPECIAL_FLAGS (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)
287static void check_extents_inlinedata(e2fsck_t ctx,
288				     struct problem_context *pctx)
289{
290	if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
291		return;
292
293	if (!fix_problem(ctx, PR_1_SPECIAL_EXTENTS_IDATA, pctx))
294		return;
295
296	pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
297	e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
298}
299#undef BAD_SPECIAL_FLAGS
300
301/*
302 * If the immutable (or append-only) flag is set on the inode, offer
303 * to clear it.
304 */
305#define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
306static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
307{
308	if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
309		return;
310
311	if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
312		return;
313
314	pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
315	e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
316}
317
318/*
319 * If device, fifo or socket, check size is zero -- if not offer to
320 * clear it
321 */
322static void check_size(e2fsck_t ctx, struct problem_context *pctx)
323{
324	struct ext2_inode *inode = pctx->inode;
325
326	if (EXT2_I_SIZE(inode) == 0)
327		return;
328
329	if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
330		return;
331
332	ext2fs_inode_size_set(ctx->fs, inode, 0);
333	e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
334}
335
336static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
337{
338	struct ext2_super_block *sb = ctx->fs->super;
339	struct ext2_inode_large *inode;
340	struct ext2_ext_attr_entry *entry;
341	char *start, *header;
342	unsigned int storage_size, remain;
343	problem_t problem = 0;
344	region_t region = 0;
345
346	inode = (struct ext2_inode_large *) pctx->inode;
347	storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
348		inode->i_extra_isize;
349	header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
350		 inode->i_extra_isize;
351	start = header + sizeof(__u32);
352	entry = (struct ext2_ext_attr_entry *) start;
353
354	/* scan all entry's headers first */
355
356	/* take finish entry 0UL into account */
357	remain = storage_size - sizeof(__u32);
358
359	region = region_create(0, storage_size);
360	if (!region) {
361		fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
362		problem = 0;
363		ctx->flags |= E2F_FLAG_ABORT;
364		return;
365	}
366	if (region_allocate(region, 0, sizeof(__u32))) {
367		problem = PR_1_INODE_EA_ALLOC_COLLISION;
368		goto fix;
369	}
370
371	while (remain >= sizeof(struct ext2_ext_attr_entry) &&
372	       !EXT2_EXT_IS_LAST_ENTRY(entry)) {
373		__u32 hash;
374
375		if (region_allocate(region, (char *)entry - (char *)header,
376				    EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
377			problem = PR_1_INODE_EA_ALLOC_COLLISION;
378			goto fix;
379		}
380
381		/* header eats this space */
382		remain -= sizeof(struct ext2_ext_attr_entry);
383
384		/* is attribute name valid? */
385		if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
386			pctx->num = entry->e_name_len;
387			problem = PR_1_ATTR_NAME_LEN;
388			goto fix;
389		}
390
391		/* attribute len eats this space */
392		remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
393
394		/* check value size */
395		if (entry->e_value_size > remain) {
396			pctx->num = entry->e_value_size;
397			problem = PR_1_ATTR_VALUE_SIZE;
398			goto fix;
399		}
400
401		/* e_value_block must be 0 in inode's ea */
402		if (entry->e_value_block != 0) {
403			pctx->num = entry->e_value_block;
404			problem = PR_1_ATTR_VALUE_BLOCK;
405			goto fix;
406		}
407
408		if (entry->e_value_size &&
409		    region_allocate(region, sizeof(__u32) + entry->e_value_offs,
410				    EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
411			problem = PR_1_INODE_EA_ALLOC_COLLISION;
412			goto fix;
413		}
414
415		hash = ext2fs_ext_attr_hash_entry(entry,
416						  start + entry->e_value_offs);
417
418		/* e_hash may be 0 in older inode's ea */
419		if (entry->e_hash != 0 && entry->e_hash != hash) {
420			pctx->num = entry->e_hash;
421			problem = PR_1_ATTR_HASH;
422			goto fix;
423		}
424
425		remain -= entry->e_value_size;
426
427		entry = EXT2_EXT_ATTR_NEXT(entry);
428	}
429
430	if (region_allocate(region, (char *)entry - (char *)header,
431			    sizeof(__u32))) {
432		problem = PR_1_INODE_EA_ALLOC_COLLISION;
433		goto fix;
434	}
435fix:
436	if (region)
437		region_free(region);
438	/*
439	 * it seems like a corruption. it's very unlikely we could repair
440	 * EA(s) in automatic fashion -bzzz
441	 */
442	if (problem == 0 || !fix_problem(ctx, problem, pctx))
443		return;
444
445	/* simply remove all possible EA(s) */
446	*((__u32 *)header) = 0UL;
447	e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
448				EXT2_INODE_SIZE(sb), "pass1");
449}
450
451static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
452{
453	struct ext2_super_block *sb = ctx->fs->super;
454	struct ext2_inode_large *inode;
455	__u32 *eamagic;
456	int min, max;
457
458	inode = (struct ext2_inode_large *) pctx->inode;
459	if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
460		/* this isn't large inode. so, nothing to check */
461		return;
462	}
463
464#if 0
465	printf("inode #%u, i_extra_size %d\n", pctx->ino,
466			inode->i_extra_isize);
467#endif
468	/* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
469	min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
470	max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
471	/*
472	 * For now we will allow i_extra_isize to be 0, but really
473	 * implementations should never allow i_extra_isize to be 0
474	 */
475	if (inode->i_extra_isize &&
476	    (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
477		if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
478			return;
479		inode->i_extra_isize = min;
480		e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
481					EXT2_INODE_SIZE(sb), "pass1");
482		return;
483	}
484
485	eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
486			inode->i_extra_isize);
487	if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
488		/* it seems inode has an extended attribute(s) in body */
489		check_ea_in_inode(ctx, pctx);
490	}
491}
492
493/*
494 * Check to see if the inode might really be a directory, despite i_mode
495 *
496 * This is a lot of complexity for something for which I'm not really
497 * convinced happens frequently in the wild.  If for any reason this
498 * causes any problems, take this code out.
499 * [tytso:20070331.0827EDT]
500 */
501static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
502				char *buf)
503{
504	struct ext2_inode *inode = pctx->inode;
505	struct ext2_dir_entry 	*dirent;
506	errcode_t		retval;
507	blk64_t			blk;
508	unsigned int		i, rec_len, not_device = 0;
509	int			extent_fs;
510	int			inlinedata_fs;
511
512	/*
513	 * If the mode looks OK, we believe it.  If the first block in
514	 * the i_block array is 0, this cannot be a directory. If the
515	 * inode is extent-mapped, it is still the case that the latter
516	 * cannot be 0 - the magic number in the extent header would make
517	 * it nonzero.
518	 */
519	if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
520	    LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
521		return;
522
523	/*
524	 * Check the block numbers in the i_block array for validity:
525	 * zero blocks are skipped (but the first one cannot be zero -
526	 * see above), other blocks are checked against the first and
527	 * max data blocks (from the the superblock) and against the
528	 * block bitmap. Any invalid block found means this cannot be
529	 * a directory.
530	 *
531	 * If there are non-zero blocks past the fourth entry, then
532	 * this cannot be a device file: we remember that for the next
533	 * check.
534	 *
535	 * For extent mapped files, we don't do any sanity checking:
536	 * just try to get the phys block of logical block 0 and run
537	 * with it.
538	 *
539	 * For inline data files, we just try to get the size of inline
540	 * data.  If it's true, we will treat it as a directory.
541	 */
542
543	extent_fs = (ctx->fs->super->s_feature_incompat &
544		     EXT3_FEATURE_INCOMPAT_EXTENTS);
545	inlinedata_fs = (ctx->fs->super->s_feature_incompat &
546			 EXT4_FEATURE_INCOMPAT_INLINE_DATA);
547	if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
548		size_t size;
549		__u32 dotdot;
550		unsigned int rec_len;
551		struct ext2_dir_entry de;
552
553		if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
554			return;
555		/*
556		 * If the size isn't a multiple of 4, it's probably not a
557		 * directory??
558		 */
559		if (size & 3)
560			return;
561		/*
562		 * If the first 10 bytes don't look like a directory entry,
563		 * it's probably not a directory.
564		 */
565		memcpy(&dotdot, inode->i_block, sizeof(dotdot));
566		memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
567		       EXT2_DIR_REC_LEN(0));
568		dotdot = ext2fs_le32_to_cpu(dotdot);
569		de.inode = ext2fs_le32_to_cpu(de.inode);
570		de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
571		ext2fs_get_rec_len(ctx->fs, &de, &rec_len);
572		if (dotdot >= ctx->fs->super->s_inodes_count ||
573		    (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
574		     dotdot != EXT2_ROOT_INO) ||
575		    de.inode >= ctx->fs->super->s_inodes_count ||
576		    (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
577		     de.inode != 0) ||
578		    rec_len > EXT4_MIN_INLINE_DATA_SIZE -
579			      EXT4_INLINE_DATA_DOTDOT_SIZE)
580			return;
581		/* device files never have a "system.data" entry */
582		goto isdir;
583	} else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
584		/* extent mapped */
585		if  (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
586				 &blk))
587			return;
588		/* device files are never extent mapped */
589		not_device++;
590	} else {
591		for (i=0; i < EXT2_N_BLOCKS; i++) {
592			blk = inode->i_block[i];
593			if (!blk)
594				continue;
595			if (i >= 4)
596				not_device++;
597
598			if (blk < ctx->fs->super->s_first_data_block ||
599			    blk >= ext2fs_blocks_count(ctx->fs->super) ||
600			    ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
601							   blk))
602				return;	/* Invalid block, can't be dir */
603		}
604		blk = inode->i_block[0];
605	}
606
607	/*
608	 * If the mode says this is a device file and the i_links_count field
609	 * is sane and we have not ruled it out as a device file previously,
610	 * we declare it a device file, not a directory.
611	 */
612	if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
613	    (inode->i_links_count == 1) && !not_device)
614		return;
615
616	/* read the first block */
617	ehandler_operation(_("reading directory block"));
618	retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
619	ehandler_operation(0);
620	if (retval)
621		return;
622
623	dirent = (struct ext2_dir_entry *) buf;
624	retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
625	if (retval)
626		return;
627	if ((ext2fs_dirent_name_len(dirent) != 1) ||
628	    (dirent->name[0] != '.') ||
629	    (dirent->inode != pctx->ino) ||
630	    (rec_len < 12) ||
631	    (rec_len % 4) ||
632	    (rec_len >= ctx->fs->blocksize - 12))
633		return;
634
635	dirent = (struct ext2_dir_entry *) (buf + rec_len);
636	retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
637	if (retval)
638		return;
639	if ((ext2fs_dirent_name_len(dirent) != 2) ||
640	    (dirent->name[0] != '.') ||
641	    (dirent->name[1] != '.') ||
642	    (rec_len < 12) ||
643	    (rec_len % 4))
644		return;
645
646isdir:
647	if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
648		inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
649		e2fsck_write_inode_full(ctx, pctx->ino, inode,
650					EXT2_INODE_SIZE(ctx->fs->super),
651					"check_is_really_dir");
652	}
653}
654
655void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
656			     ext2_icount_t *ret)
657{
658	unsigned int		threshold;
659	ext2_ino_t		num_dirs;
660	errcode_t		retval;
661	char			*tdb_dir;
662	int			enable;
663
664	*ret = 0;
665
666	profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
667			   &tdb_dir);
668	profile_get_uint(ctx->profile, "scratch_files",
669			 "numdirs_threshold", 0, 0, &threshold);
670	profile_get_boolean(ctx->profile, "scratch_files",
671			    "icount", 0, 1, &enable);
672
673	retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
674	if (retval)
675		num_dirs = 1024;	/* Guess */
676
677	if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
678	    (threshold && num_dirs <= threshold))
679		return;
680
681	retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
682	if (retval)
683		*ret = 0;
684}
685
686static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
687					    e2fsck_t ctx,
688					    struct problem_context *pctx)
689{
690	errcode_t retval;
691	struct ext2_inode_large inode;
692
693	/*
694	 * Reread inode.  If we don't see checksum error, then this inode
695	 * has been fixed elsewhere.
696	 */
697	ctx->stashed_ino = 0;
698	retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
699					sizeof(inode));
700	if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
701		return retval;
702	if (!retval)
703		return 0;
704
705	/*
706	 * Checksum still doesn't match.  That implies that the inode passes
707	 * all the sanity checks, so maybe the checksum is simply corrupt.
708	 * See if the user will go for fixing that.
709	 */
710	if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
711		return 0;
712
713	retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
714					 sizeof(inode));
715	return retval;
716}
717
718static void reserve_block_for_root_repair(e2fsck_t ctx)
719{
720	blk64_t		blk = 0;
721	errcode_t	err;
722	ext2_filsys	fs = ctx->fs;
723
724	ctx->root_repair_block = 0;
725	if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
726		return;
727
728	err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
729	if (err)
730		return;
731	ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
732	ctx->root_repair_block = blk;
733}
734
735static void reserve_block_for_lnf_repair(e2fsck_t ctx)
736{
737	blk64_t		blk = 0;
738	errcode_t	err;
739	ext2_filsys	fs = ctx->fs;
740	static const char name[] = "lost+found";
741	ext2_ino_t	ino;
742
743	ctx->lnf_repair_block = 0;
744	if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
745		return;
746
747	err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
748	if (err)
749		return;
750	ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
751	ctx->lnf_repair_block = blk;
752}
753
754static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
755					 size_t *sz)
756{
757	void *p;
758	struct ext2_xattr_handle *handle;
759	errcode_t retval;
760
761	retval = ext2fs_xattrs_open(fs, ino, &handle);
762	if (retval)
763		return retval;
764
765	retval = ext2fs_xattrs_read(handle);
766	if (retval)
767		goto err;
768
769	retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
770	if (retval)
771		goto err;
772	ext2fs_free_mem(&p);
773err:
774	(void) ext2fs_xattrs_close(&handle);
775	return retval;
776}
777
778static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
779				    struct problem_context *pctx,
780				    int failed_csum)
781{
782	if (!failed_csum)
783		return;
784
785	/*
786	 * If the inode failed the checksum and the user didn't
787	 * clear the inode, test the checksum again -- if it still
788	 * fails, ask the user if the checksum should be corrected.
789	 */
790	pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
791	if (pctx->errcode)
792		ctx->flags |= E2F_FLAG_ABORT;
793}
794#define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
795	do { \
796		finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
797		if ((ctx)->flags & E2F_FLAG_ABORT) \
798			return; \
799	} while (0)
800
801static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
802{
803	__u32 x;
804	int i;
805
806	for (i = 0; i < EXT2_N_BLOCKS; i++) {
807		x = inode->i_block[i];
808#ifdef WORDS_BIGENDIAN
809		x = ext2fs_swab32(x);
810#endif
811		if (x >= ext2fs_blocks_count(fs->super))
812			return 0;
813	}
814
815	return 1;
816}
817
818/*
819 * Figure out what to do with an inode that has both extents and inline data
820 * inode flags set.  Returns -1 if we decide to erase the inode, 0 otherwise.
821 */
822static int fix_inline_data_extents_file(e2fsck_t ctx,
823					ext2_ino_t ino,
824					struct ext2_inode *inode,
825					int inode_size,
826					struct problem_context *pctx)
827{
828	size_t max_inline_ea_size;
829	ext2_filsys fs = ctx->fs;
830	int dirty = 0;
831
832	/* Both feature flags not set?  Just run the regular checks */
833	if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super,
834				       EXT3_FEATURE_INCOMPAT_EXTENTS) &&
835	    !EXT2_HAS_INCOMPAT_FEATURE(fs->super,
836				       EXT4_FEATURE_INCOMPAT_INLINE_DATA))
837		return 0;
838
839	/* Clear both flags if it's a special file */
840	if (LINUX_S_ISCHR(inode->i_mode) ||
841	    LINUX_S_ISBLK(inode->i_mode) ||
842	    LINUX_S_ISFIFO(inode->i_mode) ||
843	    LINUX_S_ISSOCK(inode->i_mode)) {
844		check_extents_inlinedata(ctx, pctx);
845		return 0;
846	}
847
848	/* If it looks like an extent tree, try to clear inlinedata */
849	if (ext2fs_extent_header_verify(inode->i_block,
850				 sizeof(inode->i_block)) == 0 &&
851	    fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
852		inode->i_flags &= ~EXT4_INLINE_DATA_FL;
853		dirty = 1;
854		goto out;
855	}
856
857	/* If it looks short enough to be inline data, try to clear extents */
858	if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
859		max_inline_ea_size = inode_size -
860				     (EXT2_GOOD_OLD_INODE_SIZE +
861				      ((struct ext2_inode_large *)inode)->i_extra_isize);
862	else
863		max_inline_ea_size = 0;
864	if (EXT2_I_SIZE(inode) <
865	    EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
866	    fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
867		inode->i_flags &= ~EXT4_EXTENTS_FL;
868		dirty = 1;
869		goto out;
870	}
871
872	/*
873	 * Too big for inline data, but no evidence of extent tree -
874	 * maybe it's a block map file?  If the mappings all look valid?
875	 */
876	if (could_be_block_map(fs, inode) &&
877	    fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
878#ifdef WORDS_BIGENDIAN
879		int i;
880
881		for (i = 0; i < EXT2_N_BLOCKS; i++)
882			inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
883#endif
884
885		inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
886		dirty = 1;
887		goto out;
888	}
889
890	/* Oh well, just clear the busted inode. */
891	if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
892		e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
893		return -1;
894	}
895
896out:
897	if (dirty)
898		e2fsck_write_inode(ctx, ino, inode, "pass1");
899
900	return 0;
901}
902
903static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
904{
905	ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
906	dgrp_t start = *group, grp;
907	blk64_t blocks_to_read = 0;
908	errcode_t err = EXT2_ET_INVALID_ARGUMENT;
909
910	if (ctx->readahead_kb == 0)
911		goto out;
912
913	/* Keep iterating groups until we have enough to readahead */
914	inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
915	for (grp = start; grp < ctx->fs->group_desc_count; grp++) {
916		if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
917			continue;
918		inodes_in_group = ctx->fs->super->s_inodes_per_group -
919					ext2fs_bg_itable_unused(ctx->fs, grp);
920		blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
921					inodes_per_block;
922		if (blocks_to_read * ctx->fs->blocksize >
923		    ctx->readahead_kb * 1024)
924			break;
925	}
926
927	err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
928			       grp - start + 1);
929	if (err == EAGAIN) {
930		ctx->readahead_kb /= 2;
931		err = 0;
932	}
933
934out:
935	if (err) {
936		/* Error; disable itable readahead */
937		*group = ctx->fs->group_desc_count;
938		*next_ino = ctx->fs->super->s_inodes_count;
939	} else {
940		/*
941		 * Don't do more readahead until we've reached the first inode
942		 * of the last inode scan buffer block for the last group.
943		 */
944		*group = grp + 1;
945		inodes_per_buffer = (ctx->inode_buffer_blocks ?
946				     ctx->inode_buffer_blocks :
947				     EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
948				    ctx->fs->blocksize /
949				    EXT2_INODE_SIZE(ctx->fs->super);
950		inodes_in_group--;
951		*next_ino = inodes_in_group -
952			    (inodes_in_group % inodes_per_buffer) + 1 +
953			    (grp * ctx->fs->super->s_inodes_per_group);
954	}
955}
956
957void e2fsck_pass1(e2fsck_t ctx)
958{
959	int	i;
960	__u64	max_sizes;
961	ext2_filsys fs = ctx->fs;
962	ext2_ino_t	ino = 0;
963	struct ext2_inode *inode = NULL;
964	ext2_inode_scan	scan = NULL;
965	char		*block_buf = NULL;
966#ifdef RESOURCE_TRACK
967	struct resource_track	rtrack;
968#endif
969	unsigned char	frag, fsize;
970	struct		problem_context pctx;
971	struct		scan_callback_struct scan_struct;
972	struct ext2_super_block *sb = ctx->fs->super;
973	const char	*old_op;
974	unsigned int	save_type;
975	int		imagic_fs, extent_fs, inlinedata_fs;
976	int		low_dtime_check = 1;
977	int		inode_size = EXT2_INODE_SIZE(fs->super);
978	int		failed_csum = 0;
979	ext2_ino_t	ino_threshold = 0;
980	dgrp_t		ra_group = 0;
981
982	init_resource_track(&rtrack, ctx->fs->io);
983	clear_problem_context(&pctx);
984
985	/* If we can do readahead, figure out how many groups to pull in. */
986	if (!e2fsck_can_readahead(ctx->fs))
987		ctx->readahead_kb = 0;
988	else if (ctx->readahead_kb == ~0ULL)
989		ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
990	pass1_readahead(ctx, &ra_group, &ino_threshold);
991
992	if (!(ctx->options & E2F_OPT_PREEN))
993		fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
994
995	if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
996	    !(ctx->options & E2F_OPT_NO)) {
997		if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
998			ctx->dirs_to_hash = 0;
999	}
1000
1001#ifdef MTRACE
1002	mtrace_print("Pass 1");
1003#endif
1004
1005#define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1006
1007	for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
1008		max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
1009		max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
1010		max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
1011		max_sizes = (max_sizes * (1UL << i));
1012		ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
1013	}
1014#undef EXT2_BPP
1015
1016	imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
1017	extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
1018	inlinedata_fs = (sb->s_feature_incompat &
1019			EXT4_FEATURE_INCOMPAT_INLINE_DATA);
1020
1021	/*
1022	 * Allocate bitmaps structures
1023	 */
1024	pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1025						    EXT2FS_BMAP64_RBTREE,
1026						    "inode_used_map",
1027						    &ctx->inode_used_map);
1028	if (pctx.errcode) {
1029		pctx.num = 1;
1030		fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1031		ctx->flags |= E2F_FLAG_ABORT;
1032		return;
1033	}
1034	pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1035			_("directory inode map"),
1036			EXT2FS_BMAP64_AUTODIR,
1037			"inode_dir_map", &ctx->inode_dir_map);
1038	if (pctx.errcode) {
1039		pctx.num = 2;
1040		fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1041		ctx->flags |= E2F_FLAG_ABORT;
1042		return;
1043	}
1044	pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1045			_("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1046			"inode_reg_map", &ctx->inode_reg_map);
1047	if (pctx.errcode) {
1048		pctx.num = 6;
1049		fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1050		ctx->flags |= E2F_FLAG_ABORT;
1051		return;
1052	}
1053	pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
1054			_("in-use block map"), EXT2FS_BMAP64_RBTREE,
1055			"block_found_map", &ctx->block_found_map);
1056	if (pctx.errcode) {
1057		pctx.num = 1;
1058		fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1059		ctx->flags |= E2F_FLAG_ABORT;
1060		return;
1061	}
1062	pctx.errcode = e2fsck_allocate_block_bitmap(fs,
1063			_("metadata block map"), EXT2FS_BMAP64_RBTREE,
1064			"block_metadata_map", &ctx->block_metadata_map);
1065	if (pctx.errcode) {
1066		pctx.num = 1;
1067		fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1068		ctx->flags |= E2F_FLAG_ABORT;
1069		return;
1070	}
1071	e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
1072	if (!ctx->inode_link_info) {
1073		e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
1074				       "inode_link_info", &save_type);
1075		pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
1076						     &ctx->inode_link_info);
1077		fs->default_bitmap_type = save_type;
1078	}
1079
1080	if (pctx.errcode) {
1081		fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1082		ctx->flags |= E2F_FLAG_ABORT;
1083		return;
1084	}
1085	inode = (struct ext2_inode *)
1086		e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
1087
1088	inodes_to_process = (struct process_inode_block *)
1089		e2fsck_allocate_memory(ctx,
1090				       (ctx->process_inode_size *
1091					sizeof(struct process_inode_block)),
1092				       "array of inodes to process");
1093	process_inode_count = 0;
1094
1095	pctx.errcode = ext2fs_init_dblist(fs, 0);
1096	if (pctx.errcode) {
1097		fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1098		ctx->flags |= E2F_FLAG_ABORT;
1099		goto endit;
1100	}
1101
1102	/*
1103	 * If the last orphan field is set, clear it, since the pass1
1104	 * processing will automatically find and clear the orphans.
1105	 * In the future, we may want to try using the last_orphan
1106	 * linked list ourselves, but for now, we clear it so that the
1107	 * ext3 mount code won't get confused.
1108	 */
1109	if (!(ctx->options & E2F_OPT_READONLY)) {
1110		if (fs->super->s_last_orphan) {
1111			fs->super->s_last_orphan = 0;
1112			ext2fs_mark_super_dirty(fs);
1113		}
1114	}
1115
1116	mark_table_blocks(ctx);
1117	pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
1118						&ctx->block_found_map);
1119	if (pctx.errcode) {
1120		fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1121		ctx->flags |= E2F_FLAG_ABORT;
1122		goto endit;
1123	}
1124	block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1125						    "block interate buffer");
1126	if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1127		e2fsck_use_inode_shortcuts(ctx, 1);
1128	e2fsck_intercept_block_allocations(ctx);
1129	old_op = ehandler_operation(_("opening inode scan"));
1130	pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1131					      &scan);
1132	ehandler_operation(old_op);
1133	if (pctx.errcode) {
1134		fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1135		ctx->flags |= E2F_FLAG_ABORT;
1136		goto endit;
1137	}
1138	ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1139				      EXT2_SF_WARN_GARBAGE_INODES, 0);
1140	ctx->stashed_inode = inode;
1141	scan_struct.ctx = ctx;
1142	scan_struct.block_buf = block_buf;
1143	ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1144	if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1145					      ctx->fs->group_desc_count)))
1146		goto endit;
1147	if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
1148	    (fs->super->s_mtime < fs->super->s_inodes_count) ||
1149	    (fs->super->s_mkfs_time &&
1150	     fs->super->s_mkfs_time < fs->super->s_inodes_count))
1151		low_dtime_check = 0;
1152
1153	if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
1154	    fs->super->s_mmp_block > fs->super->s_first_data_block &&
1155	    fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1156		ext2fs_mark_block_bitmap2(ctx->block_found_map,
1157					  fs->super->s_mmp_block);
1158
1159	/* Set up ctx->lost_and_found if possible */
1160	(void) e2fsck_get_lost_and_found(ctx, 0);
1161
1162	while (1) {
1163		if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
1164			if (e2fsck_mmp_update(fs))
1165				fatal_error(ctx, 0);
1166		}
1167		old_op = ehandler_operation(_("getting next inode from scan"));
1168		pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1169							  inode, inode_size);
1170		if (ino > ino_threshold)
1171			pass1_readahead(ctx, &ra_group, &ino_threshold);
1172		ehandler_operation(old_op);
1173		if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1174			return;
1175		if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1176			/*
1177			 * If badblocks says badblocks is bad, offer to clear
1178			 * the list, update the in-core bb list, and restart
1179			 * the inode scan.
1180			 */
1181			if (ino == EXT2_BAD_INO &&
1182			    fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1183					&pctx)) {
1184				errcode_t err;
1185
1186				e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1187				ext2fs_badblocks_list_free(ctx->fs->badblocks);
1188				ctx->fs->badblocks = NULL;
1189				err = ext2fs_read_bb_inode(ctx->fs,
1190							&ctx->fs->badblocks);
1191				if (err) {
1192					fix_problem(ctx, PR_1_ISCAN_ERROR,
1193						    &pctx);
1194					ctx->flags |= E2F_FLAG_ABORT;
1195					goto endit;
1196				}
1197				err = ext2fs_inode_scan_goto_blockgroup(scan,
1198									0);
1199				if (err) {
1200					fix_problem(ctx, PR_1_ISCAN_ERROR,
1201						    &pctx);
1202					ctx->flags |= E2F_FLAG_ABORT;
1203					goto endit;
1204				}
1205				continue;
1206			}
1207			if (!ctx->inode_bb_map)
1208				alloc_bb_map(ctx);
1209			ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1210			ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1211			continue;
1212		}
1213		if (pctx.errcode &&
1214		    pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1215		    pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1216			fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1217			ctx->flags |= E2F_FLAG_ABORT;
1218			goto endit;
1219		}
1220		if (!ino)
1221			break;
1222		pctx.ino = ino;
1223		pctx.inode = inode;
1224		ctx->stashed_ino = ino;
1225
1226		/* Clear trashed inode? */
1227		if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
1228		    inode->i_links_count > 0 &&
1229		    fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
1230			pctx.errcode = 0;
1231			e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1232		}
1233		failed_csum = pctx.errcode != 0;
1234
1235		if (inode->i_links_count) {
1236			pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1237					   ino, inode->i_links_count);
1238			if (pctx.errcode) {
1239				pctx.num = inode->i_links_count;
1240				fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1241				ctx->flags |= E2F_FLAG_ABORT;
1242				goto endit;
1243			}
1244		}
1245
1246		/* Conflicting inlinedata/extents inode flags? */
1247		if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
1248		    (inode->i_flags & EXT4_EXTENTS_FL)) {
1249			int res = fix_inline_data_extents_file(ctx, ino, inode,
1250							       inode_size,
1251							       &pctx);
1252			if (res < 0) {
1253				/* skip FINISH_INODE_LOOP */
1254				continue;
1255			}
1256		}
1257
1258		/* Test for incorrect inline_data flags settings. */
1259		if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
1260		    (ino >= EXT2_FIRST_INODE(fs->super))) {
1261			size_t size = 0;
1262
1263			pctx.errcode = ext2fs_inline_data_size(fs, ino, &size);
1264			if (!pctx.errcode && size &&
1265			    fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
1266				sb->s_feature_incompat |=
1267					EXT4_FEATURE_INCOMPAT_INLINE_DATA;
1268				ext2fs_mark_super_dirty(fs);
1269				inlinedata_fs = 1;
1270			} else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
1271				e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1272				/* skip FINISH_INODE_LOOP */
1273				continue;
1274			}
1275		}
1276
1277		/* Test for inline data flag but no attr */
1278		if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
1279		    EXT2_I_SIZE(inode) > EXT4_MIN_INLINE_DATA_SIZE &&
1280		    (ino >= EXT2_FIRST_INODE(fs->super))) {
1281			size_t size = 0;
1282			errcode_t err;
1283			int flags;
1284
1285			flags = fs->flags;
1286			if (failed_csum)
1287				fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1288			err = get_inline_data_ea_size(fs, ino, &size);
1289			fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1290				    (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1291
1292			switch (err) {
1293			case 0:
1294				/* Everything is awesome... */
1295				break;
1296			case EXT2_ET_BAD_EA_BLOCK_NUM:
1297			case EXT2_ET_BAD_EA_HASH:
1298			case EXT2_ET_BAD_EA_HEADER:
1299			case EXT2_ET_EA_BAD_NAME_LEN:
1300			case EXT2_ET_EA_BAD_VALUE_SIZE:
1301			case EXT2_ET_EA_KEY_NOT_FOUND:
1302			case EXT2_ET_EA_NO_SPACE:
1303			case EXT2_ET_MISSING_EA_FEATURE:
1304			case EXT2_ET_INLINE_DATA_CANT_ITERATE:
1305			case EXT2_ET_INLINE_DATA_NO_BLOCK:
1306			case EXT2_ET_INLINE_DATA_NO_SPACE:
1307			case EXT2_ET_NO_INLINE_DATA:
1308			case EXT2_ET_EXT_ATTR_CSUM_INVALID:
1309			case EXT2_ET_EA_BAD_VALUE_OFFSET:
1310				/* broken EA or no system.data EA; truncate */
1311				if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
1312						&pctx)) {
1313					err = ext2fs_inode_size_set(fs, inode,
1314							sizeof(inode->i_block));
1315					if (err) {
1316						pctx.errcode = err;
1317						ctx->flags |= E2F_FLAG_ABORT;
1318						goto endit;
1319					}
1320					if (LINUX_S_ISLNK(inode->i_mode))
1321						inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1322					e2fsck_write_inode(ctx, ino, inode,
1323							   "pass1");
1324					failed_csum = 0;
1325				}
1326				break;
1327			default:
1328				/* Some other kind of non-xattr error? */
1329				pctx.errcode = err;
1330				ctx->flags |= E2F_FLAG_ABORT;
1331				goto endit;
1332			}
1333		}
1334
1335		/*
1336		 * Test for incorrect extent flag settings.
1337		 *
1338		 * On big-endian machines we must be careful:
1339		 * When the inode is read, the i_block array is not swapped
1340		 * if the extent flag is set.  Therefore if we are testing
1341		 * for or fixing a wrongly-set flag, we must potentially
1342		 * (un)swap before testing, or after fixing.
1343		 */
1344
1345		/*
1346		 * In this case the extents flag was set when read, so
1347		 * extent_header_verify is ok.  If the inode is cleared,
1348		 * no need to swap... so no extra swapping here.
1349		 */
1350		if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
1351		    (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1352		     (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1353			if ((ext2fs_extent_header_verify(inode->i_block,
1354						 sizeof(inode->i_block)) == 0) &&
1355			    fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1356				sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
1357				ext2fs_mark_super_dirty(fs);
1358				extent_fs = 1;
1359			} else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1360			clear_inode:
1361				e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1362				if (ino == EXT2_BAD_INO)
1363					ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1364								 ino);
1365				/* skip FINISH_INODE_LOOP */
1366				continue;
1367			}
1368		}
1369
1370		/*
1371		 * For big-endian machines:
1372		 * If the inode didn't have the extents flag set when it
1373		 * was read, then the i_blocks array was swapped.  To test
1374		 * as an extents header, we must swap it back first.
1375		 * IF we then set the extents flag, the entire i_block
1376		 * array must be un/re-swapped to make it proper extents data.
1377		 */
1378		if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1379		    (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1380		     (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1381		    (LINUX_S_ISREG(inode->i_mode) ||
1382		     LINUX_S_ISDIR(inode->i_mode))) {
1383			void *ehp;
1384#ifdef WORDS_BIGENDIAN
1385			__u32 tmp_block[EXT2_N_BLOCKS];
1386
1387			for (i = 0; i < EXT2_N_BLOCKS; i++)
1388				tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1389			ehp = tmp_block;
1390#else
1391			ehp = inode->i_block;
1392#endif
1393			if ((ext2fs_extent_header_verify(ehp,
1394					 sizeof(inode->i_block)) == 0) &&
1395			    (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
1396				inode->i_flags |= EXT4_EXTENTS_FL;
1397#ifdef WORDS_BIGENDIAN
1398				memcpy(inode->i_block, tmp_block,
1399				       sizeof(inode->i_block));
1400#endif
1401				e2fsck_write_inode(ctx, ino, inode, "pass1");
1402				failed_csum = 0;
1403			}
1404		}
1405
1406		if (ino == EXT2_BAD_INO) {
1407			struct process_block_struct pb;
1408
1409			if ((failed_csum || inode->i_mode || inode->i_uid ||
1410			     inode->i_gid || inode->i_links_count ||
1411			     (inode->i_flags & EXT4_INLINE_DATA_FL) ||
1412			     inode->i_file_acl) &&
1413			    fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1414				memset(inode, 0, sizeof(struct ext2_inode));
1415				e2fsck_write_inode(ctx, ino, inode,
1416						   "clear bad inode");
1417				failed_csum = 0;
1418			}
1419
1420			pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1421							  &pb.fs_meta_blocks);
1422			if (pctx.errcode) {
1423				pctx.num = 4;
1424				fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1425				ctx->flags |= E2F_FLAG_ABORT;
1426				goto endit;
1427			}
1428			pb.ino = EXT2_BAD_INO;
1429			pb.num_blocks = pb.last_block = 0;
1430			pb.last_db_block = -1;
1431			pb.num_illegal_blocks = 0;
1432			pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1433			pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1434			pb.inode = inode;
1435			pb.pctx = &pctx;
1436			pb.ctx = ctx;
1437			pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1438				     block_buf, process_bad_block, &pb);
1439			ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1440			if (pctx.errcode) {
1441				fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1442				ctx->flags |= E2F_FLAG_ABORT;
1443				goto endit;
1444			}
1445			if (pb.bbcheck)
1446				if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1447				ctx->flags |= E2F_FLAG_ABORT;
1448				goto endit;
1449			}
1450			ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1451			clear_problem_context(&pctx);
1452			FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1453			continue;
1454		} else if (ino == EXT2_ROOT_INO) {
1455			/*
1456			 * Make sure the root inode is a directory; if
1457			 * not, offer to clear it.  It will be
1458			 * regnerated in pass #3.
1459			 */
1460			if (!LINUX_S_ISDIR(inode->i_mode)) {
1461				if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1462					goto clear_inode;
1463			}
1464			/*
1465			 * If dtime is set, offer to clear it.  mke2fs
1466			 * version 0.2b created filesystems with the
1467			 * dtime field set for the root and lost+found
1468			 * directories.  We won't worry about
1469			 * /lost+found, since that can be regenerated
1470			 * easily.  But we will fix the root directory
1471			 * as a special case.
1472			 */
1473			if (inode->i_dtime && inode->i_links_count) {
1474				if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1475					inode->i_dtime = 0;
1476					e2fsck_write_inode(ctx, ino, inode,
1477							   "pass1");
1478					failed_csum = 0;
1479				}
1480			}
1481		} else if (ino == EXT2_JOURNAL_INO) {
1482			ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1483			if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1484				if (!LINUX_S_ISREG(inode->i_mode) &&
1485				    fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1486						&pctx)) {
1487					inode->i_mode = LINUX_S_IFREG;
1488					e2fsck_write_inode(ctx, ino, inode,
1489							   "pass1");
1490					failed_csum = 0;
1491				}
1492				check_blocks(ctx, &pctx, block_buf);
1493				FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1494				continue;
1495			}
1496			if ((inode->i_links_count ||
1497			     inode->i_blocks || inode->i_block[0]) &&
1498			    fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1499					&pctx)) {
1500				memset(inode, 0, inode_size);
1501				ext2fs_icount_store(ctx->inode_link_info,
1502						    ino, 0);
1503				e2fsck_write_inode_full(ctx, ino, inode,
1504							inode_size, "pass1");
1505				failed_csum = 0;
1506			}
1507		} else if ((ino == EXT4_USR_QUOTA_INO) ||
1508			   (ino == EXT4_GRP_QUOTA_INO)) {
1509			ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1510			if ((fs->super->s_feature_ro_compat &
1511					EXT4_FEATURE_RO_COMPAT_QUOTA) &&
1512			    ((fs->super->s_usr_quota_inum == ino) ||
1513			     (fs->super->s_grp_quota_inum == ino))) {
1514				if (!LINUX_S_ISREG(inode->i_mode) &&
1515				    fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1516							&pctx)) {
1517					inode->i_mode = LINUX_S_IFREG;
1518					e2fsck_write_inode(ctx, ino, inode,
1519							"pass1");
1520					failed_csum = 0;
1521				}
1522				check_blocks(ctx, &pctx, block_buf);
1523				FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1524				continue;
1525			}
1526			if ((inode->i_links_count ||
1527			     inode->i_blocks || inode->i_block[0]) &&
1528			    fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1529					&pctx)) {
1530				memset(inode, 0, inode_size);
1531				ext2fs_icount_store(ctx->inode_link_info,
1532						    ino, 0);
1533				e2fsck_write_inode_full(ctx, ino, inode,
1534							inode_size, "pass1");
1535				failed_csum = 0;
1536			}
1537		} else if (ino < EXT2_FIRST_INODE(fs->super)) {
1538			problem_t problem = 0;
1539
1540			ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1541			if (ino == EXT2_BOOT_LOADER_INO) {
1542				if (LINUX_S_ISDIR(inode->i_mode))
1543					problem = PR_1_RESERVED_BAD_MODE;
1544			} else if (ino == EXT2_RESIZE_INO) {
1545				if (inode->i_mode &&
1546				    !LINUX_S_ISREG(inode->i_mode))
1547					problem = PR_1_RESERVED_BAD_MODE;
1548			} else {
1549				if (inode->i_mode != 0)
1550					problem = PR_1_RESERVED_BAD_MODE;
1551			}
1552			if (problem) {
1553				if (fix_problem(ctx, problem, &pctx)) {
1554					inode->i_mode = 0;
1555					e2fsck_write_inode(ctx, ino, inode,
1556							   "pass1");
1557					failed_csum = 0;
1558				}
1559			}
1560			check_blocks(ctx, &pctx, block_buf);
1561			FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1562			continue;
1563		}
1564
1565		/*
1566		 * Check for inodes who might have been part of the
1567		 * orphaned list linked list.  They should have gotten
1568		 * dealt with by now, unless the list had somehow been
1569		 * corrupted.
1570		 *
1571		 * FIXME: In the future, inodes which are still in use
1572		 * (and which are therefore) pending truncation should
1573		 * be handled specially.  Right now we just clear the
1574		 * dtime field, and the normal e2fsck handling of
1575		 * inodes where i_size and the inode blocks are
1576		 * inconsistent is to fix i_size, instead of releasing
1577		 * the extra blocks.  This won't catch the inodes that
1578		 * was at the end of the orphan list, but it's better
1579		 * than nothing.  The right answer is that there
1580		 * shouldn't be any bugs in the orphan list handling.  :-)
1581		 */
1582		if (inode->i_dtime && low_dtime_check &&
1583		    inode->i_dtime < ctx->fs->super->s_inodes_count) {
1584			if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1585				inode->i_dtime = inode->i_links_count ?
1586					0 : ctx->now;
1587				e2fsck_write_inode(ctx, ino, inode,
1588						   "pass1");
1589				failed_csum = 0;
1590			}
1591		}
1592
1593		/*
1594		 * This code assumes that deleted inodes have
1595		 * i_links_count set to 0.
1596		 */
1597		if (!inode->i_links_count) {
1598			if (!inode->i_dtime && inode->i_mode) {
1599				if (fix_problem(ctx,
1600					    PR_1_ZERO_DTIME, &pctx)) {
1601					inode->i_dtime = ctx->now;
1602					e2fsck_write_inode(ctx, ino, inode,
1603							   "pass1");
1604					failed_csum = 0;
1605				}
1606			}
1607			FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1608			continue;
1609		}
1610		/*
1611		 * n.b.  0.3c ext2fs code didn't clear i_links_count for
1612		 * deleted files.  Oops.
1613		 *
1614		 * Since all new ext2 implementations get this right,
1615		 * we now assume that the case of non-zero
1616		 * i_links_count and non-zero dtime means that we
1617		 * should keep the file, not delete it.
1618		 *
1619		 */
1620		if (inode->i_dtime) {
1621			if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1622				inode->i_dtime = 0;
1623				e2fsck_write_inode(ctx, ino, inode, "pass1");
1624				failed_csum = 0;
1625			}
1626		}
1627
1628		ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1629		switch (fs->super->s_creator_os) {
1630		    case EXT2_OS_HURD:
1631			frag = inode->osd2.hurd2.h_i_frag;
1632			fsize = inode->osd2.hurd2.h_i_fsize;
1633			break;
1634		    default:
1635			frag = fsize = 0;
1636		}
1637
1638		if (inode->i_faddr || frag || fsize ||
1639		    (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
1640			mark_inode_bad(ctx, ino);
1641		if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1642		    !(fs->super->s_feature_incompat &
1643		      EXT4_FEATURE_INCOMPAT_64BIT) &&
1644		    inode->osd2.linux2.l_i_file_acl_high != 0)
1645			mark_inode_bad(ctx, ino);
1646		if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1647		    !(fs->super->s_feature_ro_compat &
1648		      EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1649		    (inode->osd2.linux2.l_i_blocks_hi != 0))
1650			mark_inode_bad(ctx, ino);
1651		if (inode->i_flags & EXT2_IMAGIC_FL) {
1652			if (imagic_fs) {
1653				if (!ctx->inode_imagic_map)
1654					alloc_imagic_map(ctx);
1655				ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1656							 ino);
1657			} else {
1658				if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1659					inode->i_flags &= ~EXT2_IMAGIC_FL;
1660					e2fsck_write_inode(ctx, ino,
1661							   inode, "pass1");
1662					failed_csum = 0;
1663				}
1664			}
1665		}
1666
1667		check_inode_extra_space(ctx, &pctx);
1668		check_is_really_dir(ctx, &pctx, block_buf);
1669
1670		/*
1671		 * ext2fs_inode_has_valid_blocks2 does not actually look
1672		 * at i_block[] values, so not endian-sensitive here.
1673		 */
1674		if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1675		    LINUX_S_ISLNK(inode->i_mode) &&
1676		    !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1677		    fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1678			inode->i_flags &= ~EXT4_EXTENTS_FL;
1679			e2fsck_write_inode(ctx, ino, inode, "pass1");
1680			failed_csum = 0;
1681		}
1682
1683		if (LINUX_S_ISDIR(inode->i_mode)) {
1684			ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1685			e2fsck_add_dir_info(ctx, ino, 0);
1686			ctx->fs_directory_count++;
1687			if (inode->i_flags & EXT4_ENCRYPT_FL)
1688				add_encrypted_dir(ctx, ino);
1689		} else if (LINUX_S_ISREG (inode->i_mode)) {
1690			ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1691			ctx->fs_regular_count++;
1692		} else if (LINUX_S_ISCHR (inode->i_mode) &&
1693			   e2fsck_pass1_check_device_inode(fs, inode)) {
1694			check_extents_inlinedata(ctx, &pctx);
1695			check_immutable(ctx, &pctx);
1696			check_size(ctx, &pctx);
1697			ctx->fs_chardev_count++;
1698		} else if (LINUX_S_ISBLK (inode->i_mode) &&
1699			   e2fsck_pass1_check_device_inode(fs, inode)) {
1700			check_extents_inlinedata(ctx, &pctx);
1701			check_immutable(ctx, &pctx);
1702			check_size(ctx, &pctx);
1703			ctx->fs_blockdev_count++;
1704		} else if (LINUX_S_ISLNK (inode->i_mode) &&
1705			   e2fsck_pass1_check_symlink(fs, ino, inode,
1706						      block_buf)) {
1707			check_immutable(ctx, &pctx);
1708			ctx->fs_symlinks_count++;
1709			if (inode->i_flags & EXT4_INLINE_DATA_FL) {
1710				FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1711				continue;
1712			} else if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1713				ctx->fs_fast_symlinks_count++;
1714				check_blocks(ctx, &pctx, block_buf);
1715				FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1716				continue;
1717			}
1718		}
1719		else if (LINUX_S_ISFIFO (inode->i_mode) &&
1720			 e2fsck_pass1_check_device_inode(fs, inode)) {
1721			check_extents_inlinedata(ctx, &pctx);
1722			check_immutable(ctx, &pctx);
1723			check_size(ctx, &pctx);
1724			ctx->fs_fifo_count++;
1725		} else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1726			   e2fsck_pass1_check_device_inode(fs, inode)) {
1727			check_extents_inlinedata(ctx, &pctx);
1728			check_immutable(ctx, &pctx);
1729			check_size(ctx, &pctx);
1730			ctx->fs_sockets_count++;
1731		} else
1732			mark_inode_bad(ctx, ino);
1733		if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1734		    !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
1735			if (inode->i_block[EXT2_IND_BLOCK])
1736				ctx->fs_ind_count++;
1737			if (inode->i_block[EXT2_DIND_BLOCK])
1738				ctx->fs_dind_count++;
1739			if (inode->i_block[EXT2_TIND_BLOCK])
1740				ctx->fs_tind_count++;
1741		}
1742		if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1743		    !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
1744		    (inode->i_block[EXT2_IND_BLOCK] ||
1745		     inode->i_block[EXT2_DIND_BLOCK] ||
1746		     inode->i_block[EXT2_TIND_BLOCK] ||
1747		     ext2fs_file_acl_block(fs, inode))) {
1748			inodes_to_process[process_inode_count].ino = ino;
1749			inodes_to_process[process_inode_count].inode = *inode;
1750			process_inode_count++;
1751		} else
1752			check_blocks(ctx, &pctx, block_buf);
1753
1754		FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1755
1756		if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1757			goto endit;
1758
1759		if (process_inode_count >= ctx->process_inode_size) {
1760			process_inodes(ctx, block_buf);
1761
1762			if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1763				goto endit;
1764		}
1765	}
1766	process_inodes(ctx, block_buf);
1767	ext2fs_close_inode_scan(scan);
1768	scan = NULL;
1769
1770	reserve_block_for_root_repair(ctx);
1771	reserve_block_for_lnf_repair(ctx);
1772
1773	/*
1774	 * If any extended attribute blocks' reference counts need to
1775	 * be adjusted, either up (ctx->refcount_extra), or down
1776	 * (ctx->refcount), then fix them.
1777	 */
1778	if (ctx->refcount) {
1779		adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1780		ea_refcount_free(ctx->refcount);
1781		ctx->refcount = 0;
1782	}
1783	if (ctx->refcount_extra) {
1784		adjust_extattr_refcount(ctx, ctx->refcount_extra,
1785					block_buf, +1);
1786		ea_refcount_free(ctx->refcount_extra);
1787		ctx->refcount_extra = 0;
1788	}
1789
1790	if (ctx->invalid_bitmaps)
1791		handle_fs_bad_blocks(ctx);
1792
1793	/* We don't need the block_ea_map any more */
1794	if (ctx->block_ea_map) {
1795		ext2fs_free_block_bitmap(ctx->block_ea_map);
1796		ctx->block_ea_map = 0;
1797	}
1798
1799	if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1800		clear_problem_context(&pctx);
1801		pctx.errcode = ext2fs_create_resize_inode(fs);
1802		if (pctx.errcode) {
1803			if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1804					 &pctx)) {
1805				ctx->flags |= E2F_FLAG_ABORT;
1806				goto endit;
1807			}
1808			pctx.errcode = 0;
1809		}
1810		if (!pctx.errcode) {
1811			e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1812					  "recreate inode");
1813			inode->i_mtime = ctx->now;
1814			e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1815					   "recreate inode");
1816		}
1817		ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1818	}
1819
1820	if (ctx->flags & E2F_FLAG_RESTART) {
1821		/*
1822		 * Only the master copy of the superblock and block
1823		 * group descriptors are going to be written during a
1824		 * restart, so set the superblock to be used to be the
1825		 * master superblock.
1826		 */
1827		ctx->use_superblock = 0;
1828		unwind_pass1(fs);
1829		goto endit;
1830	}
1831
1832	if (ctx->block_dup_map) {
1833		if (ctx->options & E2F_OPT_PREEN) {
1834			clear_problem_context(&pctx);
1835			fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1836		}
1837		e2fsck_pass1_dupblocks(ctx, block_buf);
1838	}
1839	ctx->flags |= E2F_FLAG_ALLOC_OK;
1840	ext2fs_free_mem(&inodes_to_process);
1841endit:
1842	e2fsck_use_inode_shortcuts(ctx, 0);
1843
1844	if (scan)
1845		ext2fs_close_inode_scan(scan);
1846	if (block_buf)
1847		ext2fs_free_mem(&block_buf);
1848	if (inode)
1849		ext2fs_free_mem(&inode);
1850
1851	/*
1852	 * The l+f inode may have been cleared, so zap it now and
1853	 * later passes will recalculate it if necessary
1854	 */
1855	ctx->lost_and_found = 0;
1856
1857	if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
1858		print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1859}
1860#undef FINISH_INODE_LOOP
1861
1862/*
1863 * When the inode_scan routines call this callback at the end of the
1864 * glock group, call process_inodes.
1865 */
1866static errcode_t scan_callback(ext2_filsys fs,
1867			       ext2_inode_scan scan EXT2FS_ATTR((unused)),
1868			       dgrp_t group, void * priv_data)
1869{
1870	struct scan_callback_struct *scan_struct;
1871	e2fsck_t ctx;
1872
1873	scan_struct = (struct scan_callback_struct *) priv_data;
1874	ctx = scan_struct->ctx;
1875
1876	process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1877
1878	if (ctx->progress)
1879		if ((ctx->progress)(ctx, 1, group+1,
1880				    ctx->fs->group_desc_count))
1881			return EXT2_ET_CANCEL_REQUESTED;
1882
1883	return 0;
1884}
1885
1886/*
1887 * Process the inodes in the "inodes to process" list.
1888 */
1889static void process_inodes(e2fsck_t ctx, char *block_buf)
1890{
1891	int			i;
1892	struct ext2_inode	*old_stashed_inode;
1893	ext2_ino_t		old_stashed_ino;
1894	const char		*old_operation;
1895	char			buf[80];
1896	struct problem_context	pctx;
1897
1898#if 0
1899	printf("begin process_inodes: ");
1900#endif
1901	if (process_inode_count == 0)
1902		return;
1903	old_operation = ehandler_operation(0);
1904	old_stashed_inode = ctx->stashed_inode;
1905	old_stashed_ino = ctx->stashed_ino;
1906	qsort(inodes_to_process, process_inode_count,
1907		      sizeof(struct process_inode_block), process_inode_cmp);
1908	clear_problem_context(&pctx);
1909	for (i=0; i < process_inode_count; i++) {
1910		pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1911		pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1912
1913#if 0
1914		printf("%u ", pctx.ino);
1915#endif
1916		sprintf(buf, _("reading indirect blocks of inode %u"),
1917			pctx.ino);
1918		ehandler_operation(buf);
1919		check_blocks(ctx, &pctx, block_buf);
1920		if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1921			break;
1922	}
1923	ctx->stashed_inode = old_stashed_inode;
1924	ctx->stashed_ino = old_stashed_ino;
1925	process_inode_count = 0;
1926#if 0
1927	printf("end process inodes\n");
1928#endif
1929	ehandler_operation(old_operation);
1930}
1931
1932static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1933{
1934	const struct process_inode_block *ib_a =
1935		(const struct process_inode_block *) a;
1936	const struct process_inode_block *ib_b =
1937		(const struct process_inode_block *) b;
1938	int	ret;
1939
1940	ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1941	       ib_b->inode.i_block[EXT2_IND_BLOCK]);
1942	if (ret == 0)
1943		/*
1944		 * We only call process_inodes() for non-extent
1945		 * inodes, so it's OK to pass NULL to
1946		 * ext2fs_file_acl_block() here.
1947		 */
1948		ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
1949			ext2fs_file_acl_block(0, &(ib_b->inode));
1950	if (ret == 0)
1951		ret = ib_a->ino - ib_b->ino;
1952	return ret;
1953}
1954
1955/*
1956 * Mark an inode as being bad in some what
1957 */
1958static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
1959{
1960	struct		problem_context pctx;
1961
1962	if (!ctx->inode_bad_map) {
1963		clear_problem_context(&pctx);
1964
1965		pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1966				_("bad inode map"), EXT2FS_BMAP64_RBTREE,
1967				"inode_bad_map", &ctx->inode_bad_map);
1968		if (pctx.errcode) {
1969			pctx.num = 3;
1970			fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1971			/* Should never get here */
1972			ctx->flags |= E2F_FLAG_ABORT;
1973			return;
1974		}
1975	}
1976	ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
1977}
1978
1979static void add_encrypted_dir(e2fsck_t ctx, ino_t ino)
1980{
1981	struct		problem_context pctx;
1982
1983	if (!ctx->encrypted_dirs) {
1984		pctx.errcode = ext2fs_u32_list_create(&ctx->encrypted_dirs, 0);
1985		if (pctx.errcode)
1986			goto error;
1987	}
1988	pctx.errcode = ext2fs_u32_list_add(ctx->encrypted_dirs, ino);
1989	if (pctx.errcode == 0)
1990		return;
1991error:
1992	fix_problem(ctx, PR_1_ALLOCATE_ENCRYPTED_DIRLIST, &pctx);
1993	/* Should never get here */
1994	ctx->flags |= E2F_FLAG_ABORT;
1995}
1996
1997/*
1998 * This procedure will allocate the inode "bb" (badblock) map table
1999 */
2000static void alloc_bb_map(e2fsck_t ctx)
2001{
2002	struct		problem_context pctx;
2003
2004	clear_problem_context(&pctx);
2005	pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2006			_("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
2007			"inode_bb_map", &ctx->inode_bb_map);
2008	if (pctx.errcode) {
2009		pctx.num = 4;
2010		fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2011		/* Should never get here */
2012		ctx->flags |= E2F_FLAG_ABORT;
2013		return;
2014	}
2015}
2016
2017/*
2018 * This procedure will allocate the inode imagic table
2019 */
2020static void alloc_imagic_map(e2fsck_t ctx)
2021{
2022	struct		problem_context pctx;
2023
2024	clear_problem_context(&pctx);
2025	pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2026			_("imagic inode map"), EXT2FS_BMAP64_RBTREE,
2027			"inode_imagic_map", &ctx->inode_imagic_map);
2028	if (pctx.errcode) {
2029		pctx.num = 5;
2030		fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2031		/* Should never get here */
2032		ctx->flags |= E2F_FLAG_ABORT;
2033		return;
2034	}
2035}
2036
2037/*
2038 * Marks a block as in use, setting the dup_map if it's been set
2039 * already.  Called by process_block and process_bad_block.
2040 *
2041 * WARNING: Assumes checks have already been done to make sure block
2042 * is valid.  This is true in both process_block and process_bad_block.
2043 */
2044static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
2045{
2046	struct		problem_context pctx;
2047
2048	clear_problem_context(&pctx);
2049
2050	if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
2051		if (!ctx->block_dup_map) {
2052			pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
2053					_("multiply claimed block map"),
2054					EXT2FS_BMAP64_RBTREE, "block_dup_map",
2055					&ctx->block_dup_map);
2056			if (pctx.errcode) {
2057				pctx.num = 3;
2058				fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
2059					    &pctx);
2060				/* Should never get here */
2061				ctx->flags |= E2F_FLAG_ABORT;
2062				return;
2063			}
2064		}
2065		ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
2066	} else {
2067		ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
2068	}
2069}
2070
2071static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
2072				      unsigned int num)
2073{
2074	if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
2075		ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
2076	else
2077		while (num--)
2078			mark_block_used(ctx, block++);
2079}
2080
2081/*
2082 * Adjust the extended attribute block's reference counts at the end
2083 * of pass 1, either by subtracting out references for EA blocks that
2084 * are still referenced in ctx->refcount, or by adding references for
2085 * EA blocks that had extra references as accounted for in
2086 * ctx->refcount_extra.
2087 */
2088static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
2089				    char *block_buf, int adjust_sign)
2090{
2091	struct ext2_ext_attr_header 	*header;
2092	struct problem_context		pctx;
2093	ext2_filsys			fs = ctx->fs;
2094	blk64_t				blk;
2095	__u32				should_be;
2096	int				count;
2097
2098	clear_problem_context(&pctx);
2099
2100	ea_refcount_intr_begin(refcount);
2101	while (1) {
2102		if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
2103			break;
2104		pctx.blk = blk;
2105		pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
2106						     pctx.ino);
2107		if (pctx.errcode) {
2108			fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
2109			return;
2110		}
2111		header = (struct ext2_ext_attr_header *) block_buf;
2112		pctx.blkcount = header->h_refcount;
2113		should_be = header->h_refcount + adjust_sign * count;
2114		pctx.num = should_be;
2115		if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
2116			header->h_refcount = should_be;
2117			pctx.errcode = ext2fs_write_ext_attr3(fs, blk,
2118							     block_buf,
2119							     pctx.ino);
2120			if (pctx.errcode) {
2121				fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
2122					    &pctx);
2123				continue;
2124			}
2125		}
2126	}
2127}
2128
2129/*
2130 * Handle processing the extended attribute blocks
2131 */
2132static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
2133			   char *block_buf)
2134{
2135	ext2_filsys fs = ctx->fs;
2136	ext2_ino_t	ino = pctx->ino;
2137	struct ext2_inode *inode = pctx->inode;
2138	blk64_t		blk;
2139	char *		end;
2140	struct ext2_ext_attr_header *header;
2141	struct ext2_ext_attr_entry *entry;
2142	int		count;
2143	region_t	region = 0;
2144	int		failed_csum = 0;
2145
2146	blk = ext2fs_file_acl_block(fs, inode);
2147	if (blk == 0)
2148		return 0;
2149
2150	/*
2151	 * If the Extended attribute flag isn't set, then a non-zero
2152	 * file acl means that the inode is corrupted.
2153	 *
2154	 * Or if the extended attribute block is an invalid block,
2155	 * then the inode is also corrupted.
2156	 */
2157	if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
2158	    (blk < fs->super->s_first_data_block) ||
2159	    (blk >= ext2fs_blocks_count(fs->super))) {
2160		mark_inode_bad(ctx, ino);
2161		return 0;
2162	}
2163
2164	/* If ea bitmap hasn't been allocated, create it */
2165	if (!ctx->block_ea_map) {
2166		pctx->errcode = e2fsck_allocate_block_bitmap(fs,
2167					_("ext attr block map"),
2168					EXT2FS_BMAP64_RBTREE, "block_ea_map",
2169					&ctx->block_ea_map);
2170		if (pctx->errcode) {
2171			pctx->num = 2;
2172			fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
2173			ctx->flags |= E2F_FLAG_ABORT;
2174			return 0;
2175		}
2176	}
2177
2178	/* Create the EA refcount structure if necessary */
2179	if (!ctx->refcount) {
2180		pctx->errcode = ea_refcount_create(0, &ctx->refcount);
2181		if (pctx->errcode) {
2182			pctx->num = 1;
2183			fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2184			ctx->flags |= E2F_FLAG_ABORT;
2185			return 0;
2186		}
2187	}
2188
2189#if 0
2190	/* Debugging text */
2191	printf("Inode %u has EA block %u\n", ino, blk);
2192#endif
2193
2194	/* Have we seen this EA block before? */
2195	if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
2196		if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
2197			return 1;
2198		/* Ooops, this EA was referenced more than it stated */
2199		if (!ctx->refcount_extra) {
2200			pctx->errcode = ea_refcount_create(0,
2201					   &ctx->refcount_extra);
2202			if (pctx->errcode) {
2203				pctx->num = 2;
2204				fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2205				ctx->flags |= E2F_FLAG_ABORT;
2206				return 0;
2207			}
2208		}
2209		ea_refcount_increment(ctx->refcount_extra, blk, 0);
2210		return 1;
2211	}
2212
2213	/*
2214	 * OK, we haven't seen this EA block yet.  So we need to
2215	 * validate it
2216	 */
2217	pctx->blk = blk;
2218	pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
2219	if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
2220		pctx->errcode = 0;
2221		failed_csum = 1;
2222	} else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
2223		pctx->errcode = 0;
2224
2225	if (pctx->errcode &&
2226	    fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
2227		pctx->errcode = 0;
2228		goto clear_extattr;
2229	}
2230	header = (struct ext2_ext_attr_header *) block_buf;
2231	pctx->blk = ext2fs_file_acl_block(fs, inode);
2232	if (((ctx->ext_attr_ver == 1) &&
2233	     (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
2234	    ((ctx->ext_attr_ver == 2) &&
2235	     (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
2236		if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
2237			goto clear_extattr;
2238	}
2239
2240	if (header->h_blocks != 1) {
2241		if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
2242			goto clear_extattr;
2243	}
2244
2245	if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
2246		goto clear_extattr;
2247
2248	region = region_create(0, fs->blocksize);
2249	if (!region) {
2250		fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
2251		ctx->flags |= E2F_FLAG_ABORT;
2252		return 0;
2253	}
2254	if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
2255		if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2256			goto clear_extattr;
2257	}
2258
2259	entry = (struct ext2_ext_attr_entry *)(header+1);
2260	end = block_buf + fs->blocksize;
2261	while ((char *)entry < end && *(__u32 *)entry) {
2262		__u32 hash;
2263
2264		if (region_allocate(region, (char *)entry - (char *)header,
2265			           EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
2266			if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2267				goto clear_extattr;
2268			break;
2269		}
2270		if ((ctx->ext_attr_ver == 1 &&
2271		     (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
2272		    (ctx->ext_attr_ver == 2 &&
2273		     entry->e_name_index == 0)) {
2274			if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
2275				goto clear_extattr;
2276			break;
2277		}
2278		if (entry->e_value_block != 0) {
2279			if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
2280				goto clear_extattr;
2281		}
2282		if (entry->e_value_offs + entry->e_value_size > fs->blocksize) {
2283			if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
2284				goto clear_extattr;
2285			break;
2286		}
2287		if (entry->e_value_size &&
2288		    region_allocate(region, entry->e_value_offs,
2289				    EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
2290			if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2291				goto clear_extattr;
2292		}
2293
2294		hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
2295							 entry->e_value_offs);
2296
2297		if (entry->e_hash != hash) {
2298			pctx->num = entry->e_hash;
2299			if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
2300				goto clear_extattr;
2301			entry->e_hash = hash;
2302		}
2303
2304		entry = EXT2_EXT_ATTR_NEXT(entry);
2305	}
2306	if (region_allocate(region, (char *)entry - (char *)header, 4)) {
2307		if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2308			goto clear_extattr;
2309	}
2310	region_free(region);
2311
2312	/*
2313	 * We only get here if there was no other errors that were fixed.
2314	 * If there was a checksum fail, ask to correct it.
2315	 */
2316	if (failed_csum &&
2317	    fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
2318		pctx->errcode = ext2fs_write_ext_attr3(fs, blk, block_buf,
2319						       pctx->ino);
2320		if (pctx->errcode)
2321			return 0;
2322	}
2323
2324	count = header->h_refcount - 1;
2325	if (count)
2326		ea_refcount_store(ctx->refcount, blk, count);
2327	mark_block_used(ctx, blk);
2328	ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
2329	return 1;
2330
2331clear_extattr:
2332	if (region)
2333		region_free(region);
2334	ext2fs_file_acl_block_set(fs, inode, 0);
2335	e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
2336	return 0;
2337}
2338
2339/* Returns 1 if bad htree, 0 if OK */
2340static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
2341			ext2_ino_t ino, struct ext2_inode *inode,
2342			char *block_buf)
2343{
2344	struct ext2_dx_root_info	*root;
2345	ext2_filsys			fs = ctx->fs;
2346	errcode_t			retval;
2347	blk64_t				blk;
2348
2349	if ((!LINUX_S_ISDIR(inode->i_mode) &&
2350	     fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
2351	    (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
2352	     fix_problem(ctx, PR_1_HTREE_SET, pctx)))
2353		return 1;
2354
2355	pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
2356
2357	if ((pctx->errcode) ||
2358	    (blk == 0) ||
2359	    (blk < fs->super->s_first_data_block) ||
2360	    (blk >= ext2fs_blocks_count(fs->super))) {
2361		if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2362			return 1;
2363		else
2364			return 0;
2365	}
2366
2367	retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
2368	if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2369		return 1;
2370
2371	/* XXX should check that beginning matches a directory */
2372	root = (struct ext2_dx_root_info *) (block_buf + 24);
2373
2374	if ((root->reserved_zero || root->info_length < 8) &&
2375	    fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2376		return 1;
2377
2378	pctx->num = root->hash_version;
2379	if ((root->hash_version != EXT2_HASH_LEGACY) &&
2380	    (root->hash_version != EXT2_HASH_HALF_MD4) &&
2381	    (root->hash_version != EXT2_HASH_TEA) &&
2382	    fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2383		return 1;
2384
2385	if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2386	    fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2387		return 1;
2388
2389	pctx->num = root->indirect_levels;
2390	if ((root->indirect_levels > 1) &&
2391	    fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2392		return 1;
2393
2394	return 0;
2395}
2396
2397void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2398			struct ext2_inode *inode, int restart_flag,
2399			const char *source)
2400{
2401	inode->i_flags = 0;
2402	inode->i_links_count = 0;
2403	ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2404	inode->i_dtime = ctx->now;
2405
2406	/*
2407	 * If a special inode has such rotten block mappings that we
2408	 * want to clear the whole inode, be sure to actually zap
2409	 * the block maps because i_links_count isn't checked for
2410	 * special inodes, and we'll end up right back here the next
2411	 * time we run fsck.
2412	 */
2413	if (ino < EXT2_FIRST_INODE(ctx->fs->super))
2414		memset(inode->i_block, 0, sizeof(inode->i_block));
2415
2416	ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2417	ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2418	if (ctx->inode_reg_map)
2419		ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2420	if (ctx->inode_bad_map)
2421		ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
2422
2423	/*
2424	 * If the inode was partially accounted for before processing
2425	 * was aborted, we need to restart the pass 1 scan.
2426	 */
2427	ctx->flags |= restart_flag;
2428
2429	if (ino == EXT2_BAD_INO)
2430		memset(inode, 0, sizeof(struct ext2_inode));
2431
2432	e2fsck_write_inode(ctx, ino, inode, source);
2433}
2434
2435/*
2436 * Use the multiple-blocks reclamation code to fix alignment problems in
2437 * a bigalloc filesystem.  We want a logical cluster to map to *only* one
2438 * physical cluster, and we want the block offsets within that cluster to
2439 * line up.
2440 */
2441static int has_unaligned_cluster_map(e2fsck_t ctx,
2442				     blk64_t last_pblk, e2_blkcnt_t last_lblk,
2443				     blk64_t pblk, blk64_t lblk)
2444{
2445	blk64_t cluster_mask;
2446
2447	if (!ctx->fs->cluster_ratio_bits)
2448		return 0;
2449	cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
2450
2451	/*
2452	 * If the block in the logical cluster doesn't align with the block in
2453	 * the physical cluster...
2454	 */
2455	if ((lblk & cluster_mask) != (pblk & cluster_mask))
2456		return 1;
2457
2458	/*
2459	 * If we cross a physical cluster boundary within a logical cluster...
2460	 */
2461	if (last_pblk && (lblk & cluster_mask) != 0 &&
2462	    EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
2463	    EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
2464		return 1;
2465
2466	return 0;
2467}
2468
2469static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2470			     struct process_block_struct *pb,
2471			     blk64_t start_block, blk64_t end_block,
2472			     blk64_t eof_block,
2473			     ext2_extent_handle_t ehandle,
2474			     int try_repairs)
2475{
2476	struct ext2fs_extent	extent;
2477	blk64_t			blk, last_lblk;
2478	e2_blkcnt_t		blockcnt;
2479	unsigned int		i;
2480	int			is_dir, is_leaf;
2481	problem_t		problem;
2482	struct ext2_extent_info	info;
2483	int			failed_csum = 0;
2484
2485	if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
2486		failed_csum = 1;
2487
2488	pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2489	if (pctx->errcode)
2490		return;
2491	if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
2492	    !pb->eti.force_rebuild) {
2493		struct extent_tree_level *etl;
2494
2495		etl = pb->eti.ext_info + info.curr_level;
2496		etl->num_extents += info.num_entries;
2497		etl->max_extents += info.max_entries;
2498		/*
2499		 * Implementation wart: Splitting extent blocks when appending
2500		 * will leave the old block with one free entry.  Therefore
2501		 * unless the node is totally full, pretend that a non-root
2502		 * extent block can hold one fewer entry than it actually does,
2503		 * so that we don't repeatedly rebuild the extent tree.
2504		 */
2505		if (info.curr_level && info.num_entries < info.max_entries)
2506			etl->max_extents--;
2507	}
2508
2509	pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2510					  &extent);
2511	while ((pctx->errcode == 0 ||
2512		pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
2513	       info.num_entries-- > 0) {
2514		is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2515		is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2516		last_lblk = extent.e_lblk + extent.e_len - 1;
2517
2518		problem = 0;
2519		pctx->blk = extent.e_pblk;
2520		pctx->blk2 = extent.e_lblk;
2521		pctx->num = extent.e_len;
2522		pctx->blkcount = extent.e_lblk + extent.e_len;
2523
2524		if (extent.e_pblk == 0 ||
2525		    extent.e_pblk < ctx->fs->super->s_first_data_block ||
2526		    extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2527			problem = PR_1_EXTENT_BAD_START_BLK;
2528		else if (extent.e_lblk < start_block)
2529			problem = PR_1_OUT_OF_ORDER_EXTENTS;
2530		else if ((end_block && last_lblk > end_block) &&
2531			 (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
2532				last_lblk > eof_block)))
2533			problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
2534		else if (is_leaf && extent.e_len == 0)
2535			problem = PR_1_EXTENT_LENGTH_ZERO;
2536		else if (is_leaf &&
2537			 (extent.e_pblk + extent.e_len) >
2538			 ext2fs_blocks_count(ctx->fs->super))
2539			problem = PR_1_EXTENT_ENDS_BEYOND;
2540		else if (is_leaf && is_dir &&
2541			 ((extent.e_lblk + extent.e_len) >
2542			  (1 << (21 - ctx->fs->super->s_log_block_size))))
2543			problem = PR_1_TOOBIG_DIR;
2544
2545		if (is_leaf && problem == 0 && extent.e_len > 0 &&
2546		    region_allocate(pb->region, extent.e_lblk, extent.e_len))
2547			problem = PR_1_EXTENT_COLLISION;
2548
2549		/*
2550		 * Uninitialized blocks in a directory?  Clear the flag and
2551		 * we'll interpret the blocks later.
2552		 */
2553		if (try_repairs && is_dir && problem == 0 &&
2554		    (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2555		    fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
2556			extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2557			pb->inode_modified = 1;
2558			pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2559							      &extent);
2560			if (pctx->errcode)
2561				return;
2562			failed_csum = 0;
2563		}
2564
2565		if (try_repairs && problem) {
2566report_problem:
2567			if (fix_problem(ctx, problem, pctx)) {
2568				if (ctx->invalid_bitmaps) {
2569					/*
2570					 * If fsck knows the bitmaps are bad,
2571					 * skip to the next extent and
2572					 * try to clear this extent again
2573					 * after fixing the bitmaps, by
2574					 * restarting fsck.
2575					 */
2576					pctx->errcode = ext2fs_extent_get(
2577							  ehandle,
2578							  EXT2_EXTENT_NEXT_SIB,
2579							  &extent);
2580					ctx->flags |= E2F_FLAG_RESTART_LATER;
2581					if (pctx->errcode ==
2582						    EXT2_ET_NO_CURRENT_NODE) {
2583						pctx->errcode = 0;
2584						break;
2585					}
2586					continue;
2587				}
2588				e2fsck_read_bitmaps(ctx);
2589				pb->inode_modified = 1;
2590				pctx->errcode =
2591					ext2fs_extent_delete(ehandle, 0);
2592				if (pctx->errcode) {
2593					pctx->str = "ext2fs_extent_delete";
2594					return;
2595				}
2596				pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2597				if (pctx->errcode &&
2598				    pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
2599					pctx->str = "ext2fs_extent_fix_parents";
2600					return;
2601				}
2602				pctx->errcode = ext2fs_extent_get(ehandle,
2603								  EXT2_EXTENT_CURRENT,
2604								  &extent);
2605				if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2606					pctx->errcode = 0;
2607					break;
2608				}
2609				failed_csum = 0;
2610				continue;
2611			}
2612			goto next;
2613		}
2614
2615		if (!is_leaf) {
2616			blk64_t lblk = extent.e_lblk;
2617			int next_try_repairs = 1;
2618
2619			blk = extent.e_pblk;
2620
2621			/*
2622			 * If this lower extent block collides with critical
2623			 * metadata, don't try to repair the damage.  Pass 1b
2624			 * will reallocate the block; then we can try again.
2625			 */
2626			if (pb->ino != EXT2_RESIZE_INO &&
2627			    ext2fs_test_block_bitmap2(ctx->block_metadata_map,
2628						      extent.e_pblk)) {
2629				next_try_repairs = 0;
2630				pctx->blk = blk;
2631				fix_problem(ctx,
2632					    PR_1_CRITICAL_METADATA_COLLISION,
2633					    pctx);
2634				ctx->flags |= E2F_FLAG_RESTART_LATER;
2635			}
2636			pctx->errcode = ext2fs_extent_get(ehandle,
2637						  EXT2_EXTENT_DOWN, &extent);
2638			if (pctx->errcode &&
2639			    pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
2640				pctx->str = "EXT2_EXTENT_DOWN";
2641				problem = PR_1_EXTENT_HEADER_INVALID;
2642				if (!next_try_repairs)
2643					return;
2644				if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
2645					goto report_problem;
2646				return;
2647			}
2648			/* The next extent should match this index's logical start */
2649			if (extent.e_lblk != lblk) {
2650				struct ext2_extent_info e_info;
2651
2652				ext2fs_extent_get_info(ehandle, &e_info);
2653				pctx->blk = lblk;
2654				pctx->blk2 = extent.e_lblk;
2655				pctx->num = e_info.curr_level - 1;
2656				problem = PR_1_EXTENT_INDEX_START_INVALID;
2657				if (fix_problem(ctx, problem, pctx)) {
2658					pb->inode_modified = 1;
2659					pctx->errcode =
2660						ext2fs_extent_fix_parents(ehandle);
2661					if (pctx->errcode) {
2662						pctx->str = "ext2fs_extent_fix_parents";
2663						return;
2664					}
2665				}
2666			}
2667			scan_extent_node(ctx, pctx, pb, extent.e_lblk,
2668					 last_lblk, eof_block, ehandle,
2669					 next_try_repairs);
2670			if (pctx->errcode)
2671				return;
2672			pctx->errcode = ext2fs_extent_get(ehandle,
2673						  EXT2_EXTENT_UP, &extent);
2674			if (pctx->errcode) {
2675				pctx->str = "EXT2_EXTENT_UP";
2676				return;
2677			}
2678			mark_block_used(ctx, blk);
2679			pb->num_blocks++;
2680			goto next;
2681		}
2682
2683		if ((pb->previous_block != 0) &&
2684		    (pb->previous_block+1 != extent.e_pblk)) {
2685			if (ctx->options & E2F_OPT_FRAGCHECK) {
2686				char type = '?';
2687
2688				if (pb->is_dir)
2689					type = 'd';
2690				else if (pb->is_reg)
2691					type = 'f';
2692
2693				printf(("%6lu(%c): expecting %6lu "
2694					"actual extent "
2695					"phys %6lu log %lu len %lu\n"),
2696				       (unsigned long) pctx->ino, type,
2697				       (unsigned long) pb->previous_block+1,
2698				       (unsigned long) extent.e_pblk,
2699				       (unsigned long) extent.e_lblk,
2700				       (unsigned long) extent.e_len);
2701			}
2702			pb->fragmented = 1;
2703		}
2704		/*
2705		 * If we notice a gap in the logical block mappings of an
2706		 * extent-mapped directory, offer to close the hole by
2707		 * moving the logical block down, otherwise we'll go mad in
2708		 * pass 3 allocating empty directory blocks to fill the hole.
2709		 */
2710		if (try_repairs && is_dir &&
2711		    pb->last_block + 1 < (e2_blkcnt_t)extent.e_lblk) {
2712			blk64_t new_lblk;
2713
2714			new_lblk = pb->last_block + 1;
2715			if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
2716				new_lblk = ((new_lblk +
2717					     EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
2718					    ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
2719					   (extent.e_pblk &
2720					    EXT2FS_CLUSTER_MASK(ctx->fs));
2721			pctx->blk = extent.e_lblk;
2722			pctx->blk2 = new_lblk;
2723			if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
2724				extent.e_lblk = new_lblk;
2725				pb->inode_modified = 1;
2726				pctx->errcode = ext2fs_extent_replace(ehandle,
2727								0, &extent);
2728				if (pctx->errcode) {
2729					pctx->errcode = 0;
2730					goto alloc_later;
2731				}
2732				pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2733				if (pctx->errcode)
2734					goto failed_add_dir_block;
2735				pctx->errcode = ext2fs_extent_goto(ehandle,
2736								extent.e_lblk);
2737				if (pctx->errcode)
2738					goto failed_add_dir_block;
2739				last_lblk = extent.e_lblk + extent.e_len - 1;
2740				failed_csum = 0;
2741			}
2742		}
2743alloc_later:
2744		while (is_dir && (++pb->last_db_block <
2745				  (e2_blkcnt_t) extent.e_lblk)) {
2746			pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
2747							      pb->ino, 0,
2748							      pb->last_db_block);
2749			if (pctx->errcode) {
2750				pctx->blk = 0;
2751				pctx->num = pb->last_db_block;
2752				goto failed_add_dir_block;
2753			}
2754		}
2755		if (!ctx->fs->cluster_ratio_bits) {
2756			mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
2757			pb->num_blocks += extent.e_len;
2758		}
2759		for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
2760		     i < extent.e_len;
2761		     blk++, blockcnt++, i++) {
2762			if (ctx->fs->cluster_ratio_bits &&
2763			    !(pb->previous_block &&
2764			      (EXT2FS_B2C(ctx->fs, blk) ==
2765			       EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
2766			      (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2767			      ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2768				mark_block_used(ctx, blk);
2769				pb->num_blocks++;
2770			}
2771			if (has_unaligned_cluster_map(ctx, pb->previous_block,
2772						      pb->last_block, blk,
2773						      blockcnt)) {
2774				pctx->blk = blockcnt;
2775				pctx->blk2 = blk;
2776				fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2777				mark_block_used(ctx, blk);
2778				mark_block_used(ctx, blk);
2779			}
2780			pb->last_block = blockcnt;
2781			pb->previous_block = blk;
2782
2783			if (is_dir) {
2784				pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
2785				if (pctx->errcode) {
2786					pctx->blk = blk;
2787					pctx->num = blockcnt;
2788				failed_add_dir_block:
2789					fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2790					/* Should never get here */
2791					ctx->flags |= E2F_FLAG_ABORT;
2792					return;
2793				}
2794			}
2795		}
2796		if (is_dir && extent.e_len > 0)
2797			pb->last_db_block = blockcnt - 1;
2798		pb->previous_block = extent.e_pblk + extent.e_len - 1;
2799		start_block = pb->last_block = last_lblk;
2800		if (is_leaf && !is_dir &&
2801		    !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
2802			pb->last_init_lblock = last_lblk;
2803	next:
2804		pctx->errcode = ext2fs_extent_get(ehandle,
2805						  EXT2_EXTENT_NEXT_SIB,
2806						  &extent);
2807	}
2808
2809	/* Failed csum but passes checks?  Ask to fix checksum. */
2810	if (failed_csum &&
2811	    fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
2812		pb->inode_modified = 1;
2813		pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
2814		if (pctx->errcode)
2815			return;
2816	}
2817
2818	if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
2819		pctx->errcode = 0;
2820}
2821
2822static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
2823				 struct process_block_struct *pb)
2824{
2825	struct ext2_extent_info info;
2826	struct ext2_inode	*inode = pctx->inode;
2827	ext2_extent_handle_t	ehandle;
2828	ext2_filsys		fs = ctx->fs;
2829	ext2_ino_t		ino = pctx->ino;
2830	errcode_t		retval;
2831	blk64_t                 eof_lblk;
2832
2833	pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
2834	if (pctx->errcode) {
2835		if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
2836			e2fsck_clear_inode(ctx, ino, inode, 0,
2837					   "check_blocks_extents");
2838		pctx->errcode = 0;
2839		return;
2840	}
2841
2842	retval = ext2fs_extent_get_info(ehandle, &info);
2843	if (retval == 0) {
2844		int max_depth = info.max_depth;
2845
2846		if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
2847			max_depth = MAX_EXTENT_DEPTH_COUNT-1;
2848		ctx->extent_depth_count[max_depth]++;
2849	}
2850
2851	/* Check maximum extent depth */
2852	pctx->blk = info.max_depth;
2853	pctx->blk2 = ext2fs_max_extent_depth(ehandle);
2854	if (pctx->blk2 < pctx->blk &&
2855	    fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
2856		pb->eti.force_rebuild = 1;
2857
2858	/* Can we collect extent tree level stats? */
2859	pctx->blk = MAX_EXTENT_DEPTH_COUNT;
2860	if (pctx->blk2 > pctx->blk)
2861		fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
2862	memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
2863	pb->eti.ino = pb->ino;
2864
2865	pb->region = region_create(0, info.max_lblk);
2866	if (!pb->region) {
2867		ext2fs_extent_free(ehandle);
2868		fix_problem(ctx, PR_1_EXTENT_ALLOC_REGION_ABORT, pctx);
2869		ctx->flags |= E2F_FLAG_ABORT;
2870		return;
2871	}
2872
2873	eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
2874		EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
2875	scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
2876	if (pctx->errcode &&
2877	    fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
2878		pb->num_blocks = 0;
2879		inode->i_blocks = 0;
2880		e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2881				   "check_blocks_extents");
2882		pctx->errcode = 0;
2883	}
2884	region_free(pb->region);
2885	pb->region = NULL;
2886	ext2fs_extent_free(ehandle);
2887
2888	/* Rebuild unless it's a dir and we're rehashing it */
2889	if (LINUX_S_ISDIR(inode->i_mode) &&
2890	    e2fsck_dir_will_be_rehashed(ctx, ino))
2891		return;
2892
2893	if (ctx->options & E2F_OPT_CONVERT_BMAP)
2894		e2fsck_rebuild_extents_later(ctx, ino);
2895	else
2896		e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
2897}
2898
2899/*
2900 * In fact we don't need to check blocks for an inode with inline data
2901 * because this inode doesn't have any blocks.  In this function all
2902 * we need to do is add this inode into dblist when it is a directory.
2903 */
2904static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
2905				     struct process_block_struct *pb)
2906{
2907	int	flags;
2908	size_t	inline_data_size = 0;
2909
2910	if (!pb->is_dir) {
2911		pctx->errcode = 0;
2912		return;
2913	}
2914
2915	/* Process the dirents in i_block[] as the "first" block. */
2916	pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
2917	if (pctx->errcode)
2918		goto err;
2919
2920	/* Process the dirents in the EA as a "second" block. */
2921	flags = ctx->fs->flags;
2922	ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2923	pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
2924						&inline_data_size);
2925	ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
2926			 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
2927	if (pctx->errcode) {
2928		pctx->errcode = 0;
2929		return;
2930	}
2931
2932	if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
2933		return;
2934
2935	pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
2936	if (pctx->errcode)
2937		goto err;
2938
2939	return;
2940err:
2941	pctx->blk = 0;
2942	pctx->num = 0;
2943	fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2944	ctx->flags |= E2F_FLAG_ABORT;
2945}
2946
2947/*
2948 * This subroutine is called on each inode to account for all of the
2949 * blocks used by that inode.
2950 */
2951static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
2952			 char *block_buf)
2953{
2954	ext2_filsys fs = ctx->fs;
2955	struct process_block_struct pb;
2956	ext2_ino_t	ino = pctx->ino;
2957	struct ext2_inode *inode = pctx->inode;
2958	unsigned	bad_size = 0;
2959	int		dirty_inode = 0;
2960	int		extent_fs;
2961	int		inlinedata_fs;
2962	__u64		size;
2963
2964	pb.ino = ino;
2965	pb.num_blocks = 0;
2966	pb.last_block = -1;
2967	pb.last_init_lblock = -1;
2968	pb.last_db_block = -1;
2969	pb.num_illegal_blocks = 0;
2970	pb.suppress = 0; pb.clear = 0;
2971	pb.fragmented = 0;
2972	pb.compressed = 0;
2973	pb.previous_block = 0;
2974	pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2975	pb.is_reg = LINUX_S_ISREG(inode->i_mode);
2976	pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
2977	pb.inode = inode;
2978	pb.pctx = pctx;
2979	pb.ctx = ctx;
2980	pb.inode_modified = 0;
2981	pb.eti.force_rebuild = 0;
2982	pctx->ino = ino;
2983	pctx->errcode = 0;
2984
2985	extent_fs = (ctx->fs->super->s_feature_incompat &
2986                     EXT3_FEATURE_INCOMPAT_EXTENTS);
2987	inlinedata_fs = (ctx->fs->super->s_feature_incompat &
2988			 EXT4_FEATURE_INCOMPAT_INLINE_DATA);
2989
2990	if (check_ext_attr(ctx, pctx, block_buf)) {
2991		if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2992			goto out;
2993		pb.num_blocks++;
2994	}
2995
2996	if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
2997		check_blocks_inline_data(ctx, pctx, &pb);
2998	else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
2999		if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
3000			check_blocks_extents(ctx, pctx, &pb);
3001		else {
3002			int flags;
3003			/*
3004			 * If we've modified the inode, write it out before
3005			 * iterate() tries to use it.
3006			 */
3007			if (dirty_inode) {
3008				e2fsck_write_inode(ctx, ino, inode,
3009						   "check_blocks");
3010				dirty_inode = 0;
3011			}
3012			flags = fs->flags;
3013			fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3014			pctx->errcode = ext2fs_block_iterate3(fs, ino,
3015						pb.is_dir ? BLOCK_FLAG_HOLE : 0,
3016						block_buf, process_block, &pb);
3017			/*
3018			 * We do not have uninitialized extents in non extent
3019			 * files.
3020			 */
3021			pb.last_init_lblock = pb.last_block;
3022			/*
3023			 * If iterate() changed a block mapping, we have to
3024			 * re-read the inode.  If we decide to clear the
3025			 * inode after clearing some stuff, we'll re-write the
3026			 * bad mappings into the inode!
3027			 */
3028			if (pb.inode_modified)
3029				e2fsck_read_inode(ctx, ino, inode,
3030						  "check_blocks");
3031			fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3032				    (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3033
3034			if (ctx->options & E2F_OPT_CONVERT_BMAP) {
3035#ifdef DEBUG
3036				printf("bmap rebuild ino=%d\n", ino);
3037#endif
3038				if (!LINUX_S_ISDIR(inode->i_mode) ||
3039				    !e2fsck_dir_will_be_rehashed(ctx, ino))
3040					e2fsck_rebuild_extents_later(ctx, ino);
3041			}
3042		}
3043	}
3044	end_problem_latch(ctx, PR_LATCH_BLOCK);
3045	end_problem_latch(ctx, PR_LATCH_TOOBIG);
3046	if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3047		goto out;
3048	if (pctx->errcode)
3049		fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
3050
3051	if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
3052		if (LINUX_S_ISDIR(inode->i_mode))
3053			ctx->fs_fragmented_dir++;
3054		else
3055			ctx->fs_fragmented++;
3056	}
3057
3058	if (pb.clear) {
3059		e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
3060				   "check_blocks");
3061		return;
3062	}
3063
3064	if (inode->i_flags & EXT2_INDEX_FL) {
3065		if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
3066			inode->i_flags &= ~EXT2_INDEX_FL;
3067			dirty_inode++;
3068		} else {
3069			e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
3070		}
3071	}
3072
3073	if (!pb.num_blocks && pb.is_dir &&
3074	    !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
3075		if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
3076			e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
3077			ctx->fs_directory_count--;
3078			return;
3079		}
3080	}
3081
3082	if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
3083		quota_data_add(ctx->qctx, inode, ino,
3084			       pb.num_blocks * fs->blocksize);
3085		quota_data_inodes(ctx->qctx, inode, ino, +1);
3086	}
3087
3088	if (!(fs->super->s_feature_ro_compat &
3089	      EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
3090	    !(inode->i_flags & EXT4_HUGE_FILE_FL))
3091		pb.num_blocks *= (fs->blocksize / 512);
3092	pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
3093#if 0
3094	printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
3095	       ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
3096	       pb.num_blocks);
3097#endif
3098	if (pb.is_dir) {
3099		int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
3100		if (inode->i_flags & EXT4_INLINE_DATA_FL) {
3101			int flags;
3102			size_t size;
3103			errcode_t err;
3104
3105			size = 0;
3106			flags = ctx->fs->flags;
3107			ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3108			err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
3109						      &size);
3110			ctx->fs->flags = (flags &
3111					  EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3112					 (ctx->fs->flags &
3113					  ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3114			if (err || size != inode->i_size) {
3115				bad_size = 7;
3116				pctx->num = size;
3117			}
3118		} else if (inode->i_size & (fs->blocksize - 1))
3119			bad_size = 5;
3120		else if (nblock > (pb.last_block + 1))
3121			bad_size = 1;
3122		else if (nblock < (pb.last_block + 1)) {
3123			if (((pb.last_block + 1) - nblock) >
3124			    fs->super->s_prealloc_dir_blocks)
3125				bad_size = 2;
3126		}
3127	} else {
3128		e2_blkcnt_t blkpg = ctx->blocks_per_page;
3129
3130		size = EXT2_I_SIZE(inode);
3131		if ((pb.last_init_lblock >= 0) &&
3132		    /* allow allocated blocks to end of PAGE_SIZE */
3133		    (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
3134		    (pb.last_init_lblock / blkpg * blkpg != pb.last_init_lblock ||
3135		     size < (__u64)(pb.last_init_lblock & ~(blkpg-1)) *
3136		     fs->blocksize))
3137			bad_size = 3;
3138		else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
3139			 size > ext2_max_sizes[fs->super->s_log_block_size])
3140			/* too big for a direct/indirect-mapped file */
3141			bad_size = 4;
3142		else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
3143			 size >
3144			 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
3145			/* too big for an extent-based file - 32bit ee_block */
3146			bad_size = 6;
3147	}
3148	/* i_size for symlinks is checked elsewhere */
3149	if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
3150		/* Did inline_data set pctx->num earlier? */
3151		if (bad_size != 7)
3152			pctx->num = (pb.last_block + 1) * fs->blocksize;
3153		pctx->group = bad_size;
3154		if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
3155			if (LINUX_S_ISDIR(inode->i_mode))
3156				pctx->num &= 0xFFFFFFFFULL;
3157			ext2fs_inode_size_set(fs, inode, pctx->num);
3158			if (EXT2_I_SIZE(inode) == 0 &&
3159			    (inode->i_flags & EXT4_INLINE_DATA_FL)) {
3160				memset(inode->i_block, 0,
3161				       sizeof(inode->i_block));
3162				inode->i_flags &= ~EXT4_INLINE_DATA_FL;
3163			}
3164			dirty_inode++;
3165		}
3166		pctx->num = 0;
3167	}
3168	if (LINUX_S_ISREG(inode->i_mode) &&
3169	    ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
3170		ctx->large_files++;
3171	if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
3172	    ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
3173	     ((fs->super->s_feature_ro_compat &
3174	       EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
3175	      (inode->i_flags & EXT4_HUGE_FILE_FL) &&
3176	      (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
3177		pctx->num = pb.num_blocks;
3178		if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
3179			inode->i_blocks = pb.num_blocks;
3180			inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
3181			dirty_inode++;
3182		}
3183		pctx->num = 0;
3184	}
3185
3186	/*
3187	 * The kernel gets mad if we ask it to allocate bigalloc clusters to
3188	 * a block mapped file, so rebuild it as an extent file.  We can skip
3189	 * symlinks because they're never rewritten.
3190	 */
3191	if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
3192			EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
3193	    (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
3194	    ext2fs_inode_data_blocks2(fs, inode) > 0 &&
3195	    (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
3196	    !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
3197	    fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
3198		pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
3199		if (pctx->errcode)
3200			goto out;
3201	}
3202
3203	if (ctx->dirs_to_hash && pb.is_dir &&
3204	    !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
3205	    !(inode->i_flags & EXT2_INDEX_FL) &&
3206	    ((inode->i_size / fs->blocksize) >= 3))
3207		e2fsck_rehash_dir_later(ctx, ino);
3208
3209out:
3210	if (dirty_inode)
3211		e2fsck_write_inode(ctx, ino, inode, "check_blocks");
3212}
3213
3214#if 0
3215/*
3216 * Helper function called by process block when an illegal block is
3217 * found.  It returns a description about why the block is illegal
3218 */
3219static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
3220{
3221	blk64_t	super;
3222	int	i;
3223	static char	problem[80];
3224
3225	super = fs->super->s_first_data_block;
3226	strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
3227	if (block < super) {
3228		sprintf(problem, "< FIRSTBLOCK (%u)", super);
3229		return(problem);
3230	} else if (block >= ext2fs_blocks_count(fs->super)) {
3231		sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
3232		return(problem);
3233	}
3234	for (i = 0; i < fs->group_desc_count; i++) {
3235		if (block == super) {
3236			sprintf(problem, "is the superblock in group %d", i);
3237			break;
3238		}
3239		if (block > super &&
3240		    block <= (super + fs->desc_blocks)) {
3241			sprintf(problem, "is in the group descriptors "
3242				"of group %d", i);
3243			break;
3244		}
3245		if (block == ext2fs_block_bitmap_loc(fs, i)) {
3246			sprintf(problem, "is the block bitmap of group %d", i);
3247			break;
3248		}
3249		if (block == ext2fs_inode_bitmap_loc(fs, i)) {
3250			sprintf(problem, "is the inode bitmap of group %d", i);
3251			break;
3252		}
3253		if (block >= ext2fs_inode_table_loc(fs, i) &&
3254		    (block < ext2fs_inode_table_loc(fs, i)
3255		     + fs->inode_blocks_per_group)) {
3256			sprintf(problem, "is in the inode table of group %d",
3257				i);
3258			break;
3259		}
3260		super += fs->super->s_blocks_per_group;
3261	}
3262	return(problem);
3263}
3264#endif
3265
3266/*
3267 * This is a helper function for check_blocks().
3268 */
3269static int process_block(ext2_filsys fs,
3270		  blk64_t	*block_nr,
3271		  e2_blkcnt_t blockcnt,
3272		  blk64_t ref_block EXT2FS_ATTR((unused)),
3273		  int ref_offset EXT2FS_ATTR((unused)),
3274		  void *priv_data)
3275{
3276	struct process_block_struct *p;
3277	struct problem_context *pctx;
3278	blk64_t	blk = *block_nr;
3279	int	ret_code = 0;
3280	problem_t	problem = 0;
3281	e2fsck_t	ctx;
3282
3283	p = (struct process_block_struct *) priv_data;
3284	pctx = p->pctx;
3285	ctx = p->ctx;
3286
3287	/*
3288	 * For a directory, add logical block zero for processing even if it's
3289	 * not mapped or we'll be perennially stuck with broken "." and ".."
3290	 * entries.
3291	 */
3292	if (p->is_dir && blockcnt == 0 && blk == 0) {
3293		pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
3294		if (pctx->errcode) {
3295			pctx->blk = blk;
3296			pctx->num = blockcnt;
3297			goto failed_add_dir_block;
3298		}
3299		p->last_db_block++;
3300	}
3301
3302	if (blk == 0)
3303		return 0;
3304
3305#if 0
3306	printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
3307	       blockcnt);
3308#endif
3309
3310	/*
3311	 * Simplistic fragmentation check.  We merely require that the
3312	 * file be contiguous.  (Which can never be true for really
3313	 * big files that are greater than a block group.)
3314	 */
3315	if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
3316		if (p->previous_block+1 != blk) {
3317			if (ctx->options & E2F_OPT_FRAGCHECK) {
3318				char type = '?';
3319
3320				if (p->is_dir)
3321					type = 'd';
3322				else if (p->is_reg)
3323					type = 'f';
3324
3325				printf(_("%6lu(%c): expecting %6lu "
3326					 "got phys %6lu (blkcnt %lld)\n"),
3327				       (unsigned long) pctx->ino, type,
3328				       (unsigned long) p->previous_block+1,
3329				       (unsigned long) blk,
3330				       blockcnt);
3331			}
3332			p->fragmented = 1;
3333		}
3334	}
3335
3336	if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
3337		problem = PR_1_TOOBIG_DIR;
3338	if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
3339		problem = PR_1_TOOBIG_REG;
3340	if (!p->is_dir && !p->is_reg && blockcnt > 0)
3341		problem = PR_1_TOOBIG_SYMLINK;
3342
3343	if (blk < fs->super->s_first_data_block ||
3344	    blk >= ext2fs_blocks_count(fs->super))
3345		problem = PR_1_ILLEGAL_BLOCK_NUM;
3346
3347	/*
3348	 * If this IND/DIND/TIND block is squatting atop some critical metadata
3349	 * (group descriptors, superblock, bitmap, inode table), any write to
3350	 * "fix" mapping problems will destroy the metadata.  We'll let pass 1b
3351	 * fix that and restart fsck.
3352	 */
3353	if (blockcnt < 0 &&
3354	    p->ino != EXT2_RESIZE_INO &&
3355	    ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
3356		pctx->blk = blk;
3357		fix_problem(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx);
3358		ctx->flags |= E2F_FLAG_RESTART_LATER;
3359	}
3360
3361	if (problem) {
3362		p->num_illegal_blocks++;
3363		/*
3364		 * A bit of subterfuge here -- we're trying to fix a block
3365		 * mapping, but the IND/DIND/TIND block could have collided
3366		 * with some critical metadata.  So, fix the in-core mapping so
3367		 * iterate won't go insane, but return 0 instead of
3368		 * BLOCK_CHANGED so that it won't write the remapping out to
3369		 * our multiply linked block.
3370		 *
3371		 * Even if we previously determined that an *IND block
3372		 * conflicts with critical metadata, we must still try to
3373		 * iterate the *IND block as if it is an *IND block to find and
3374		 * mark the blocks it points to.  Better to be overly cautious
3375		 * with the used_blocks map so that we don't move the *IND
3376		 * block to a block that's really in use!
3377		 */
3378		if (p->ino != EXT2_RESIZE_INO &&
3379		    ref_block != 0 &&
3380		    ext2fs_test_block_bitmap2(ctx->block_metadata_map,
3381					      ref_block)) {
3382			*block_nr = 0;
3383			return 0;
3384		}
3385		if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
3386			if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
3387				p->clear = 1;
3388				return BLOCK_ABORT;
3389			}
3390			if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
3391				p->suppress = 1;
3392				set_latch_flags(PR_LATCH_BLOCK,
3393						PRL_SUPPRESS, 0);
3394			}
3395		}
3396		pctx->blk = blk;
3397		pctx->blkcount = blockcnt;
3398		if (fix_problem(ctx, problem, pctx)) {
3399			blk = *block_nr = 0;
3400			ret_code = BLOCK_CHANGED;
3401			p->inode_modified = 1;
3402			/*
3403			 * If the directory block is too big and is beyond the
3404			 * end of the FS, don't bother trying to add it for
3405			 * processing -- the kernel would never have created a
3406			 * directory this large, and we risk an ENOMEM abort.
3407			 * In any case, the toobig handler for extent-based
3408			 * directories also doesn't feed toobig blocks to
3409			 * pass 2.
3410			 */
3411			if (problem == PR_1_TOOBIG_DIR)
3412				return ret_code;
3413			goto mark_dir;
3414		} else
3415			return 0;
3416	}
3417
3418	if (p->ino == EXT2_RESIZE_INO) {
3419		/*
3420		 * The resize inode has already be sanity checked
3421		 * during pass #0 (the superblock checks).  All we
3422		 * have to do is mark the double indirect block as
3423		 * being in use; all of the other blocks are handled
3424		 * by mark_table_blocks()).
3425		 */
3426		if (blockcnt == BLOCK_COUNT_DIND)
3427			mark_block_used(ctx, blk);
3428		p->num_blocks++;
3429	} else if (!(ctx->fs->cluster_ratio_bits &&
3430		     p->previous_block &&
3431		     (EXT2FS_B2C(ctx->fs, blk) ==
3432		      EXT2FS_B2C(ctx->fs, p->previous_block)) &&
3433		     (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
3434		     ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
3435		mark_block_used(ctx, blk);
3436		p->num_blocks++;
3437	} else if (has_unaligned_cluster_map(ctx, p->previous_block,
3438					     p->last_block, blk, blockcnt)) {
3439		pctx->blk = blockcnt;
3440		pctx->blk2 = blk;
3441		fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3442		mark_block_used(ctx, blk);
3443		mark_block_used(ctx, blk);
3444	}
3445	if (blockcnt >= 0)
3446		p->last_block = blockcnt;
3447	p->previous_block = blk;
3448mark_dir:
3449	if (p->is_dir && (blockcnt >= 0)) {
3450		while (++p->last_db_block < blockcnt) {
3451			pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
3452							      p->ino, 0,
3453							      p->last_db_block);
3454			if (pctx->errcode) {
3455				pctx->blk = 0;
3456				pctx->num = p->last_db_block;
3457				goto failed_add_dir_block;
3458			}
3459		}
3460		pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
3461						      blk, blockcnt);
3462		if (pctx->errcode) {
3463			pctx->blk = blk;
3464			pctx->num = blockcnt;
3465		failed_add_dir_block:
3466			fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3467			/* Should never get here */
3468			ctx->flags |= E2F_FLAG_ABORT;
3469			return BLOCK_ABORT;
3470		}
3471	}
3472	return ret_code;
3473}
3474
3475static int process_bad_block(ext2_filsys fs,
3476		      blk64_t *block_nr,
3477		      e2_blkcnt_t blockcnt,
3478		      blk64_t ref_block EXT2FS_ATTR((unused)),
3479		      int ref_offset EXT2FS_ATTR((unused)),
3480		      void *priv_data)
3481{
3482	struct process_block_struct *p;
3483	blk64_t		blk = *block_nr;
3484	blk64_t		first_block;
3485	dgrp_t		i;
3486	struct problem_context *pctx;
3487	e2fsck_t	ctx;
3488
3489	if (!blk)
3490		return 0;
3491
3492	p = (struct process_block_struct *) priv_data;
3493	ctx = p->ctx;
3494	pctx = p->pctx;
3495
3496	pctx->ino = EXT2_BAD_INO;
3497	pctx->blk = blk;
3498	pctx->blkcount = blockcnt;
3499
3500	if ((blk < fs->super->s_first_data_block) ||
3501	    (blk >= ext2fs_blocks_count(fs->super))) {
3502		if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
3503			*block_nr = 0;
3504			return BLOCK_CHANGED;
3505		} else
3506			return 0;
3507	}
3508
3509	if (blockcnt < 0) {
3510		if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
3511			p->bbcheck = 1;
3512			if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
3513				*block_nr = 0;
3514				return BLOCK_CHANGED;
3515			}
3516		} else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3517						    blk)) {
3518			p->bbcheck = 1;
3519			if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
3520					pctx)) {
3521				*block_nr = 0;
3522				return BLOCK_CHANGED;
3523			}
3524			if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3525				return BLOCK_ABORT;
3526		} else
3527			mark_block_used(ctx, blk);
3528		return 0;
3529	}
3530#if 0
3531	printf ("DEBUG: Marking %u as bad.\n", blk);
3532#endif
3533	ctx->fs_badblocks_count++;
3534	/*
3535	 * If the block is not used, then mark it as used and return.
3536	 * If it is already marked as found, this must mean that
3537	 * there's an overlap between the filesystem table blocks
3538	 * (bitmaps and inode table) and the bad block list.
3539	 */
3540	if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
3541		ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3542		return 0;
3543	}
3544	/*
3545	 * Try to find the where the filesystem block was used...
3546	 */
3547	first_block = fs->super->s_first_data_block;
3548
3549	for (i = 0; i < fs->group_desc_count; i++ ) {
3550		pctx->group = i;
3551		pctx->blk = blk;
3552		if (!ext2fs_bg_has_super(fs, i))
3553			goto skip_super;
3554		if (blk == first_block) {
3555			if (i == 0) {
3556				if (fix_problem(ctx,
3557						PR_1_BAD_PRIMARY_SUPERBLOCK,
3558						pctx)) {
3559					*block_nr = 0;
3560					return BLOCK_CHANGED;
3561				}
3562				return 0;
3563			}
3564			fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
3565			return 0;
3566		}
3567		if ((blk > first_block) &&
3568		    (blk <= first_block + fs->desc_blocks)) {
3569			if (i == 0) {
3570				pctx->blk = *block_nr;
3571				if (fix_problem(ctx,
3572			PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
3573					*block_nr = 0;
3574					return BLOCK_CHANGED;
3575				}
3576				return 0;
3577			}
3578			fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
3579			return 0;
3580		}
3581	skip_super:
3582		if (blk == ext2fs_block_bitmap_loc(fs, i)) {
3583			if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
3584				ctx->invalid_block_bitmap_flag[i]++;
3585				ctx->invalid_bitmaps++;
3586			}
3587			return 0;
3588		}
3589		if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
3590			if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
3591				ctx->invalid_inode_bitmap_flag[i]++;
3592				ctx->invalid_bitmaps++;
3593			}
3594			return 0;
3595		}
3596		if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
3597		    (blk < (ext2fs_inode_table_loc(fs, i) +
3598			    fs->inode_blocks_per_group))) {
3599			/*
3600			 * If there are bad blocks in the inode table,
3601			 * the inode scan code will try to do
3602			 * something reasonable automatically.
3603			 */
3604			return 0;
3605		}
3606		first_block += fs->super->s_blocks_per_group;
3607	}
3608	/*
3609	 * If we've gotten to this point, then the only
3610	 * possibility is that the bad block inode meta data
3611	 * is using a bad block.
3612	 */
3613	if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
3614	    (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
3615	    (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
3616		p->bbcheck = 1;
3617		if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
3618			*block_nr = 0;
3619			return BLOCK_CHANGED;
3620		}
3621		if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3622			return BLOCK_ABORT;
3623		return 0;
3624	}
3625
3626	pctx->group = -1;
3627
3628	/* Warn user that the block wasn't claimed */
3629	fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
3630
3631	return 0;
3632}
3633
3634static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
3635			    const char *name, int num, blk64_t *new_block)
3636{
3637	ext2_filsys fs = ctx->fs;
3638	dgrp_t		last_grp;
3639	blk64_t		old_block = *new_block;
3640	blk64_t		last_block;
3641	dgrp_t		flexbg;
3642	unsigned	flexbg_size;
3643	int		i, is_flexbg;
3644	char		*buf;
3645	struct problem_context	pctx;
3646
3647	clear_problem_context(&pctx);
3648
3649	pctx.group = group;
3650	pctx.blk = old_block;
3651	pctx.str = name;
3652
3653	/*
3654	 * For flex_bg filesystems, first try to allocate the metadata
3655	 * within the flex_bg, and if that fails then try finding the
3656	 * space anywhere in the filesystem.
3657	 */
3658	is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
3659					      EXT4_FEATURE_INCOMPAT_FLEX_BG);
3660	if (is_flexbg) {
3661		flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3662		flexbg = group / flexbg_size;
3663		first_block = ext2fs_group_first_block2(fs,
3664							flexbg_size * flexbg);
3665		last_grp = group | (flexbg_size - 1);
3666		if (last_grp >= fs->group_desc_count)
3667			last_grp = fs->group_desc_count - 1;
3668		last_block = ext2fs_group_last_block2(fs, last_grp);
3669	} else
3670		last_block = ext2fs_group_last_block2(fs, group);
3671	pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
3672					       num, ctx->block_found_map,
3673					       new_block);
3674	if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
3675		pctx.errcode = ext2fs_get_free_blocks2(fs,
3676				fs->super->s_first_data_block,
3677				ext2fs_blocks_count(fs->super),
3678				num, ctx->block_found_map, new_block);
3679	if (pctx.errcode) {
3680		pctx.num = num;
3681		fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
3682		ext2fs_unmark_valid(fs);
3683		ctx->flags |= E2F_FLAG_ABORT;
3684		return;
3685	}
3686	pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
3687	if (pctx.errcode) {
3688		fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
3689		ext2fs_unmark_valid(fs);
3690		ctx->flags |= E2F_FLAG_ABORT;
3691		return;
3692	}
3693	ext2fs_mark_super_dirty(fs);
3694	fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3695	pctx.blk2 = *new_block;
3696	fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
3697			  PR_1_RELOC_TO), &pctx);
3698	pctx.blk2 = 0;
3699	for (i = 0; i < num; i++) {
3700		pctx.blk = i;
3701		ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
3702		if (old_block) {
3703			pctx.errcode = io_channel_read_blk64(fs->io,
3704				   old_block + i, 1, buf);
3705			if (pctx.errcode)
3706				fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
3707			pctx.blk = (*new_block) + i;
3708			pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
3709							      1, buf);
3710		} else {
3711			pctx.blk = (*new_block) + i;
3712			pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
3713							   NULL, NULL);
3714		}
3715
3716		if (pctx.errcode)
3717			fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
3718	}
3719	ext2fs_free_mem(&buf);
3720}
3721
3722/*
3723 * This routine gets called at the end of pass 1 if bad blocks are
3724 * detected in the superblock, group descriptors, inode_bitmaps, or
3725 * block bitmaps.  At this point, all of the blocks have been mapped
3726 * out, so we can try to allocate new block(s) to replace the bad
3727 * blocks.
3728 */
3729static void handle_fs_bad_blocks(e2fsck_t ctx)
3730{
3731	ext2_filsys fs = ctx->fs;
3732	dgrp_t		i;
3733	blk64_t		first_block;
3734	blk64_t		new_blk;
3735
3736	for (i = 0; i < fs->group_desc_count; i++) {
3737		first_block = ext2fs_group_first_block2(fs, i);
3738
3739		if (ctx->invalid_block_bitmap_flag[i]) {
3740			new_blk = ext2fs_block_bitmap_loc(fs, i);
3741			new_table_block(ctx, first_block, i, _("block bitmap"),
3742					1, &new_blk);
3743			ext2fs_block_bitmap_loc_set(fs, i, new_blk);
3744		}
3745		if (ctx->invalid_inode_bitmap_flag[i]) {
3746			new_blk = ext2fs_inode_bitmap_loc(fs, i);
3747			new_table_block(ctx, first_block, i, _("inode bitmap"),
3748					1, &new_blk);
3749			ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
3750		}
3751		if (ctx->invalid_inode_table_flag[i]) {
3752			new_blk = ext2fs_inode_table_loc(fs, i);
3753			new_table_block(ctx, first_block, i, _("inode table"),
3754					fs->inode_blocks_per_group,
3755					&new_blk);
3756			ext2fs_inode_table_loc_set(fs, i, new_blk);
3757			ctx->flags |= E2F_FLAG_RESTART;
3758		}
3759	}
3760	ctx->invalid_bitmaps = 0;
3761}
3762
3763/*
3764 * This routine marks all blocks which are used by the superblock,
3765 * group descriptors, inode bitmaps, and block bitmaps.
3766 */
3767static void mark_table_blocks(e2fsck_t ctx)
3768{
3769	ext2_filsys fs = ctx->fs;
3770	blk64_t	b;
3771	dgrp_t	i;
3772	unsigned int	j;
3773	struct problem_context pctx;
3774
3775	clear_problem_context(&pctx);
3776
3777	for (i = 0; i < fs->group_desc_count; i++) {
3778		pctx.group = i;
3779
3780		ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
3781		ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
3782
3783		/*
3784		 * Mark the blocks used for the inode table
3785		 */
3786		if (ext2fs_inode_table_loc(fs, i)) {
3787			for (j = 0, b = ext2fs_inode_table_loc(fs, i);
3788			     j < fs->inode_blocks_per_group;
3789			     j++, b++) {
3790				if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3791							     b)) {
3792					pctx.blk = b;
3793					if (!ctx->invalid_inode_table_flag[i] &&
3794					    fix_problem(ctx,
3795						PR_1_ITABLE_CONFLICT, &pctx)) {
3796						ctx->invalid_inode_table_flag[i]++;
3797						ctx->invalid_bitmaps++;
3798					}
3799				} else {
3800				    ext2fs_mark_block_bitmap2(
3801						ctx->block_found_map, b);
3802				    ext2fs_mark_block_bitmap2(
3803						ctx->block_metadata_map, b);
3804			    	}
3805			}
3806		}
3807
3808		/*
3809		 * Mark block used for the block bitmap
3810		 */
3811		if (ext2fs_block_bitmap_loc(fs, i)) {
3812			if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3813				     ext2fs_block_bitmap_loc(fs, i))) {
3814				pctx.blk = ext2fs_block_bitmap_loc(fs, i);
3815				if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
3816					ctx->invalid_block_bitmap_flag[i]++;
3817					ctx->invalid_bitmaps++;
3818				}
3819			} else {
3820			    ext2fs_mark_block_bitmap2(ctx->block_found_map,
3821				     ext2fs_block_bitmap_loc(fs, i));
3822			    ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
3823				     ext2fs_block_bitmap_loc(fs, i));
3824			}
3825		}
3826		/*
3827		 * Mark block used for the inode bitmap
3828		 */
3829		if (ext2fs_inode_bitmap_loc(fs, i)) {
3830			if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3831				     ext2fs_inode_bitmap_loc(fs, i))) {
3832				pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
3833				if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
3834					ctx->invalid_inode_bitmap_flag[i]++;
3835					ctx->invalid_bitmaps++;
3836				}
3837			} else {
3838			    ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
3839				     ext2fs_inode_bitmap_loc(fs, i));
3840			    ext2fs_mark_block_bitmap2(ctx->block_found_map,
3841				     ext2fs_inode_bitmap_loc(fs, i));
3842			}
3843		}
3844	}
3845}
3846
3847/*
3848 * Thes subroutines short circuits ext2fs_get_blocks and
3849 * ext2fs_check_directory; we use them since we already have the inode
3850 * structure, so there's no point in letting the ext2fs library read
3851 * the inode again.
3852 */
3853static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
3854				  blk_t *blocks)
3855{
3856	e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3857	int	i;
3858
3859	if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3860		return EXT2_ET_CALLBACK_NOTHANDLED;
3861
3862	for (i=0; i < EXT2_N_BLOCKS; i++)
3863		blocks[i] = ctx->stashed_inode->i_block[i];
3864	return 0;
3865}
3866
3867static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
3868				  struct ext2_inode *inode)
3869{
3870	e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3871
3872	if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3873		return EXT2_ET_CALLBACK_NOTHANDLED;
3874	*inode = *ctx->stashed_inode;
3875	return 0;
3876}
3877
3878static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
3879			    struct ext2_inode *inode)
3880{
3881	e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3882
3883	if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
3884		(inode != ctx->stashed_inode))
3885		*ctx->stashed_inode = *inode;
3886	return EXT2_ET_CALLBACK_NOTHANDLED;
3887}
3888
3889static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
3890{
3891	e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3892
3893	if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3894		return EXT2_ET_CALLBACK_NOTHANDLED;
3895
3896	if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
3897		return EXT2_ET_NO_DIRECTORY;
3898	return 0;
3899}
3900
3901static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
3902					blk64_t *ret)
3903{
3904	e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3905	errcode_t	retval;
3906	blk64_t		new_block;
3907
3908	if (ctx->block_found_map) {
3909		retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
3910					   &new_block);
3911		if (retval)
3912			return retval;
3913		if (fs->block_map) {
3914			ext2fs_mark_block_bitmap2(fs->block_map, new_block);
3915			ext2fs_mark_bb_dirty(fs);
3916		}
3917	} else {
3918		if (!fs->block_map) {
3919			retval = ext2fs_read_block_bitmap(fs);
3920			if (retval)
3921				return retval;
3922		}
3923
3924		retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
3925		if (retval)
3926			return retval;
3927	}
3928
3929	*ret = new_block;
3930	return (0);
3931}
3932
3933static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
3934				  blk64_t len, blk64_t *pblk, blk64_t *plen)
3935{
3936	e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3937	errcode_t	retval;
3938
3939	if (ctx->block_found_map)
3940		return ext2fs_new_range(fs, flags, goal, len,
3941					ctx->block_found_map, pblk, plen);
3942
3943	if (!fs->block_map) {
3944		retval = ext2fs_read_block_bitmap(fs);
3945		if (retval)
3946			return retval;
3947	}
3948
3949	return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
3950				pblk, plen);
3951}
3952
3953static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
3954{
3955	e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3956
3957	/* Never free a critical metadata block */
3958	if (ctx->block_found_map &&
3959	    ctx->block_metadata_map &&
3960	    inuse < 0 &&
3961	    ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
3962		return;
3963
3964	if (ctx->block_found_map) {
3965		if (inuse > 0)
3966			ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3967		else
3968			ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
3969	}
3970}
3971
3972static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
3973					   blk_t num, int inuse)
3974{
3975	e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3976
3977	/* Never free a critical metadata block */
3978	if (ctx->block_found_map &&
3979	    ctx->block_metadata_map &&
3980	    inuse < 0 &&
3981	    ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
3982		return;
3983
3984	if (ctx->block_found_map) {
3985		if (inuse > 0)
3986			ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
3987							blk, num);
3988		else
3989			ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
3990							blk, num);
3991	}
3992}
3993
3994void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
3995{
3996	ext2_filsys fs = ctx->fs;
3997
3998	if (use_shortcuts) {
3999		fs->get_blocks = pass1_get_blocks;
4000		fs->check_directory = pass1_check_directory;
4001		fs->read_inode = pass1_read_inode;
4002		fs->write_inode = pass1_write_inode;
4003		ctx->stashed_ino = 0;
4004	} else {
4005		fs->get_blocks = 0;
4006		fs->check_directory = 0;
4007		fs->read_inode = 0;
4008		fs->write_inode = 0;
4009	}
4010}
4011
4012void e2fsck_intercept_block_allocations(e2fsck_t ctx)
4013{
4014	ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
4015	ext2fs_set_block_alloc_stats_callback(ctx->fs,
4016						e2fsck_block_alloc_stats, 0);
4017	ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
4018	ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
4019					e2fsck_block_alloc_stats_range, NULL);
4020}
4021