xref: /aosp_15_r20/external/e2fsprogs/e2fsck/super.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * e2fsck.c - superblock checks
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker  *
6*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Public
8*6a54128fSAndroid Build Coastguard Worker  * License.
9*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker  */
11*6a54128fSAndroid Build Coastguard Worker 
12*6a54128fSAndroid Build Coastguard Worker #include "config.h"
13*6a54128fSAndroid Build Coastguard Worker #ifdef HAVE_ERRNO_H
14*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
15*6a54128fSAndroid Build Coastguard Worker #endif
16*6a54128fSAndroid Build Coastguard Worker 
17*6a54128fSAndroid Build Coastguard Worker #ifndef EXT2_SKIP_UUID
18*6a54128fSAndroid Build Coastguard Worker #include "uuid/uuid.h"
19*6a54128fSAndroid Build Coastguard Worker #endif
20*6a54128fSAndroid Build Coastguard Worker #include "e2fsck.h"
21*6a54128fSAndroid Build Coastguard Worker #include "problem.h"
22*6a54128fSAndroid Build Coastguard Worker 
23*6a54128fSAndroid Build Coastguard Worker #define MIN_CHECK 1
24*6a54128fSAndroid Build Coastguard Worker #define MAX_CHECK 2
25*6a54128fSAndroid Build Coastguard Worker #define LOG2_CHECK 4
26*6a54128fSAndroid Build Coastguard Worker 
check_super_value(e2fsck_t ctx,const char * descr,unsigned long value,int flags,unsigned long min_val,unsigned long max_val)27*6a54128fSAndroid Build Coastguard Worker static int check_super_value(e2fsck_t ctx, const char *descr,
28*6a54128fSAndroid Build Coastguard Worker 			      unsigned long value, int flags,
29*6a54128fSAndroid Build Coastguard Worker 			      unsigned long min_val, unsigned long max_val)
30*6a54128fSAndroid Build Coastguard Worker {
31*6a54128fSAndroid Build Coastguard Worker 	struct		problem_context pctx;
32*6a54128fSAndroid Build Coastguard Worker 
33*6a54128fSAndroid Build Coastguard Worker 	if ((flags & MIN_CHECK && value < min_val) ||
34*6a54128fSAndroid Build Coastguard Worker 	    (flags & MAX_CHECK && value > max_val) ||
35*6a54128fSAndroid Build Coastguard Worker 	    (flags & LOG2_CHECK && (value & (value - 1)) != 0)) {
36*6a54128fSAndroid Build Coastguard Worker 		clear_problem_context(&pctx);
37*6a54128fSAndroid Build Coastguard Worker 		pctx.num = value;
38*6a54128fSAndroid Build Coastguard Worker 		pctx.str = descr;
39*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
40*6a54128fSAndroid Build Coastguard Worker 		ctx->flags |= E2F_FLAG_ABORT;
41*6a54128fSAndroid Build Coastguard Worker 		return 0;
42*6a54128fSAndroid Build Coastguard Worker 	}
43*6a54128fSAndroid Build Coastguard Worker 	return 1;
44*6a54128fSAndroid Build Coastguard Worker }
45*6a54128fSAndroid Build Coastguard Worker 
check_super_value64(e2fsck_t ctx,const char * descr,__u64 value,int flags,__u64 min_val,__u64 max_val)46*6a54128fSAndroid Build Coastguard Worker static int check_super_value64(e2fsck_t ctx, const char *descr,
47*6a54128fSAndroid Build Coastguard Worker 				__u64 value, int flags,
48*6a54128fSAndroid Build Coastguard Worker 				__u64 min_val, __u64 max_val)
49*6a54128fSAndroid Build Coastguard Worker {
50*6a54128fSAndroid Build Coastguard Worker 	struct		problem_context pctx;
51*6a54128fSAndroid Build Coastguard Worker 
52*6a54128fSAndroid Build Coastguard Worker 	if ((flags & MIN_CHECK && value < min_val) ||
53*6a54128fSAndroid Build Coastguard Worker 	    (flags & MAX_CHECK && value > max_val) ||
54*6a54128fSAndroid Build Coastguard Worker 	    (flags & LOG2_CHECK && (value & (value - 1)) != 0)) {
55*6a54128fSAndroid Build Coastguard Worker 		clear_problem_context(&pctx);
56*6a54128fSAndroid Build Coastguard Worker 		pctx.num = value;
57*6a54128fSAndroid Build Coastguard Worker 		pctx.str = descr;
58*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
59*6a54128fSAndroid Build Coastguard Worker 		ctx->flags |= E2F_FLAG_ABORT;
60*6a54128fSAndroid Build Coastguard Worker 		return 0;
61*6a54128fSAndroid Build Coastguard Worker 	}
62*6a54128fSAndroid Build Coastguard Worker 	return 1;
63*6a54128fSAndroid Build Coastguard Worker }
64*6a54128fSAndroid Build Coastguard Worker 
65*6a54128fSAndroid Build Coastguard Worker /*
66*6a54128fSAndroid Build Coastguard Worker  * helper function to release an inode
67*6a54128fSAndroid Build Coastguard Worker  */
68*6a54128fSAndroid Build Coastguard Worker struct process_block_struct {
69*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t 	ctx;
70*6a54128fSAndroid Build Coastguard Worker 	char 		*buf;
71*6a54128fSAndroid Build Coastguard Worker 	struct problem_context *pctx;
72*6a54128fSAndroid Build Coastguard Worker 	int		truncating;
73*6a54128fSAndroid Build Coastguard Worker 	int		truncate_offset;
74*6a54128fSAndroid Build Coastguard Worker 	e2_blkcnt_t	truncate_block;
75*6a54128fSAndroid Build Coastguard Worker 	int		truncated_blocks;
76*6a54128fSAndroid Build Coastguard Worker 	int		abort;
77*6a54128fSAndroid Build Coastguard Worker 	errcode_t	errcode;
78*6a54128fSAndroid Build Coastguard Worker 	blk64_t last_cluster;
79*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode_large *inode;
80*6a54128fSAndroid Build Coastguard Worker };
81*6a54128fSAndroid Build Coastguard Worker 
release_inode_block(ext2_filsys fs,blk64_t * block_nr,e2_blkcnt_t blockcnt,blk64_t ref_blk EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)82*6a54128fSAndroid Build Coastguard Worker static int release_inode_block(ext2_filsys fs,
83*6a54128fSAndroid Build Coastguard Worker 			       blk64_t	*block_nr,
84*6a54128fSAndroid Build Coastguard Worker 			       e2_blkcnt_t blockcnt,
85*6a54128fSAndroid Build Coastguard Worker 			       blk64_t	ref_blk EXT2FS_ATTR((unused)),
86*6a54128fSAndroid Build Coastguard Worker 			       int	ref_offset EXT2FS_ATTR((unused)),
87*6a54128fSAndroid Build Coastguard Worker 			       void *priv_data)
88*6a54128fSAndroid Build Coastguard Worker {
89*6a54128fSAndroid Build Coastguard Worker 	struct process_block_struct *pb;
90*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t 		ctx;
91*6a54128fSAndroid Build Coastguard Worker 	struct problem_context	*pctx;
92*6a54128fSAndroid Build Coastguard Worker 	blk64_t			blk = *block_nr;
93*6a54128fSAndroid Build Coastguard Worker 	blk64_t			cluster = EXT2FS_B2C(fs, *block_nr);
94*6a54128fSAndroid Build Coastguard Worker 	int			retval = 0;
95*6a54128fSAndroid Build Coastguard Worker 
96*6a54128fSAndroid Build Coastguard Worker 	pb = (struct process_block_struct *) priv_data;
97*6a54128fSAndroid Build Coastguard Worker 	ctx = pb->ctx;
98*6a54128fSAndroid Build Coastguard Worker 	pctx = pb->pctx;
99*6a54128fSAndroid Build Coastguard Worker 
100*6a54128fSAndroid Build Coastguard Worker 	pctx->blk = blk;
101*6a54128fSAndroid Build Coastguard Worker 	pctx->blkcount = blockcnt;
102*6a54128fSAndroid Build Coastguard Worker 
103*6a54128fSAndroid Build Coastguard Worker 	if (blk == 0)
104*6a54128fSAndroid Build Coastguard Worker 		return 0;
105*6a54128fSAndroid Build Coastguard Worker 
106*6a54128fSAndroid Build Coastguard Worker 	if (pb->last_cluster == cluster)
107*6a54128fSAndroid Build Coastguard Worker 		return 0;
108*6a54128fSAndroid Build Coastguard Worker 
109*6a54128fSAndroid Build Coastguard Worker 	pb->last_cluster = cluster;
110*6a54128fSAndroid Build Coastguard Worker 
111*6a54128fSAndroid Build Coastguard Worker 	if ((blk < fs->super->s_first_data_block) ||
112*6a54128fSAndroid Build Coastguard Worker 	    (blk >= ext2fs_blocks_count(fs->super))) {
113*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx);
114*6a54128fSAndroid Build Coastguard Worker 	return_abort:
115*6a54128fSAndroid Build Coastguard Worker 		pb->abort = 1;
116*6a54128fSAndroid Build Coastguard Worker 		return BLOCK_ABORT;
117*6a54128fSAndroid Build Coastguard Worker 	}
118*6a54128fSAndroid Build Coastguard Worker 
119*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_test_block_bitmap2(fs->block_map, blk)) {
120*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx);
121*6a54128fSAndroid Build Coastguard Worker 		goto return_abort;
122*6a54128fSAndroid Build Coastguard Worker 	}
123*6a54128fSAndroid Build Coastguard Worker 
124*6a54128fSAndroid Build Coastguard Worker 	/*
125*6a54128fSAndroid Build Coastguard Worker 	 * If we are deleting an orphan, then we leave the fields alone.
126*6a54128fSAndroid Build Coastguard Worker 	 * If we are truncating an orphan, then update the inode fields
127*6a54128fSAndroid Build Coastguard Worker 	 * and clean up any partial block data.
128*6a54128fSAndroid Build Coastguard Worker 	 */
129*6a54128fSAndroid Build Coastguard Worker 	if (pb->truncating) {
130*6a54128fSAndroid Build Coastguard Worker 		/*
131*6a54128fSAndroid Build Coastguard Worker 		 * We only remove indirect blocks if they are
132*6a54128fSAndroid Build Coastguard Worker 		 * completely empty.
133*6a54128fSAndroid Build Coastguard Worker 		 */
134*6a54128fSAndroid Build Coastguard Worker 		if (blockcnt < 0) {
135*6a54128fSAndroid Build Coastguard Worker 			int	i, limit;
136*6a54128fSAndroid Build Coastguard Worker 			blk_t	*bp;
137*6a54128fSAndroid Build Coastguard Worker 
138*6a54128fSAndroid Build Coastguard Worker 			pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
139*6a54128fSAndroid Build Coastguard Worker 							pb->buf);
140*6a54128fSAndroid Build Coastguard Worker 			if (pb->errcode)
141*6a54128fSAndroid Build Coastguard Worker 				goto return_abort;
142*6a54128fSAndroid Build Coastguard Worker 
143*6a54128fSAndroid Build Coastguard Worker 			limit = fs->blocksize >> 2;
144*6a54128fSAndroid Build Coastguard Worker 			for (i = 0, bp = (blk_t *) pb->buf;
145*6a54128fSAndroid Build Coastguard Worker 			     i < limit;	 i++, bp++)
146*6a54128fSAndroid Build Coastguard Worker 				if (*bp)
147*6a54128fSAndroid Build Coastguard Worker 					return 0;
148*6a54128fSAndroid Build Coastguard Worker 		}
149*6a54128fSAndroid Build Coastguard Worker 		/*
150*6a54128fSAndroid Build Coastguard Worker 		 * We don't remove direct blocks until we've reached
151*6a54128fSAndroid Build Coastguard Worker 		 * the truncation block.
152*6a54128fSAndroid Build Coastguard Worker 		 */
153*6a54128fSAndroid Build Coastguard Worker 		if (blockcnt >= 0 && blockcnt < pb->truncate_block)
154*6a54128fSAndroid Build Coastguard Worker 			return 0;
155*6a54128fSAndroid Build Coastguard Worker 		/*
156*6a54128fSAndroid Build Coastguard Worker 		 * If part of the last block needs truncating, we do
157*6a54128fSAndroid Build Coastguard Worker 		 * it here.
158*6a54128fSAndroid Build Coastguard Worker 		 */
159*6a54128fSAndroid Build Coastguard Worker 		if ((blockcnt == pb->truncate_block) && pb->truncate_offset) {
160*6a54128fSAndroid Build Coastguard Worker 			pb->errcode = io_channel_read_blk64(fs->io, blk, 1,
161*6a54128fSAndroid Build Coastguard Worker 							pb->buf);
162*6a54128fSAndroid Build Coastguard Worker 			if (pb->errcode)
163*6a54128fSAndroid Build Coastguard Worker 				goto return_abort;
164*6a54128fSAndroid Build Coastguard Worker 			memset(pb->buf + pb->truncate_offset, 0,
165*6a54128fSAndroid Build Coastguard Worker 			       fs->blocksize - pb->truncate_offset);
166*6a54128fSAndroid Build Coastguard Worker 			pb->errcode = io_channel_write_blk64(fs->io, blk, 1,
167*6a54128fSAndroid Build Coastguard Worker 							 pb->buf);
168*6a54128fSAndroid Build Coastguard Worker 			if (pb->errcode)
169*6a54128fSAndroid Build Coastguard Worker 				goto return_abort;
170*6a54128fSAndroid Build Coastguard Worker 		}
171*6a54128fSAndroid Build Coastguard Worker 		pb->truncated_blocks++;
172*6a54128fSAndroid Build Coastguard Worker 		*block_nr = 0;
173*6a54128fSAndroid Build Coastguard Worker 		retval |= BLOCK_CHANGED;
174*6a54128fSAndroid Build Coastguard Worker 	}
175*6a54128fSAndroid Build Coastguard Worker 
176*6a54128fSAndroid Build Coastguard Worker 	if (ctx->qctx)
177*6a54128fSAndroid Build Coastguard Worker 		quota_data_sub(ctx->qctx, pb->inode, 0, ctx->fs->blocksize);
178*6a54128fSAndroid Build Coastguard Worker 	ext2fs_block_alloc_stats2(fs, blk, -1);
179*6a54128fSAndroid Build Coastguard Worker 	ctx->free_blocks++;
180*6a54128fSAndroid Build Coastguard Worker 	return retval;
181*6a54128fSAndroid Build Coastguard Worker }
182*6a54128fSAndroid Build Coastguard Worker 
183*6a54128fSAndroid Build Coastguard Worker /*
184*6a54128fSAndroid Build Coastguard Worker  * This function releases an inode.  Returns 1 if an inconsistency was
185*6a54128fSAndroid Build Coastguard Worker  * found.  If the inode has a link count, then it is being truncated and
186*6a54128fSAndroid Build Coastguard Worker  * not deleted.
187*6a54128fSAndroid Build Coastguard Worker  */
release_inode_blocks(e2fsck_t ctx,ext2_ino_t ino,struct ext2_inode_large * inode,char * block_buf,struct problem_context * pctx)188*6a54128fSAndroid Build Coastguard Worker static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino,
189*6a54128fSAndroid Build Coastguard Worker 				struct ext2_inode_large *inode, char *block_buf,
190*6a54128fSAndroid Build Coastguard Worker 				struct problem_context *pctx)
191*6a54128fSAndroid Build Coastguard Worker {
192*6a54128fSAndroid Build Coastguard Worker 	struct process_block_struct 	pb;
193*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys			fs = ctx->fs;
194*6a54128fSAndroid Build Coastguard Worker 	blk64_t				blk;
195*6a54128fSAndroid Build Coastguard Worker 	errcode_t			retval;
196*6a54128fSAndroid Build Coastguard Worker 	__u32				count;
197*6a54128fSAndroid Build Coastguard Worker 
198*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_inode_has_valid_blocks2(fs, EXT2_INODE(inode)))
199*6a54128fSAndroid Build Coastguard Worker 		return 0;
200*6a54128fSAndroid Build Coastguard Worker 
201*6a54128fSAndroid Build Coastguard Worker 	pb.buf = block_buf + 3 * ctx->fs->blocksize;
202*6a54128fSAndroid Build Coastguard Worker 	pb.ctx = ctx;
203*6a54128fSAndroid Build Coastguard Worker 	pb.abort = 0;
204*6a54128fSAndroid Build Coastguard Worker 	pb.errcode = 0;
205*6a54128fSAndroid Build Coastguard Worker 	pb.pctx = pctx;
206*6a54128fSAndroid Build Coastguard Worker 	pb.last_cluster = 0;
207*6a54128fSAndroid Build Coastguard Worker 	pb.inode = inode;
208*6a54128fSAndroid Build Coastguard Worker 	if (inode->i_links_count) {
209*6a54128fSAndroid Build Coastguard Worker 		pb.truncating = 1;
210*6a54128fSAndroid Build Coastguard Worker 		pb.truncate_block = (e2_blkcnt_t)
211*6a54128fSAndroid Build Coastguard Worker 			((EXT2_I_SIZE(inode) + fs->blocksize - 1) /
212*6a54128fSAndroid Build Coastguard Worker 			 fs->blocksize);
213*6a54128fSAndroid Build Coastguard Worker 		pb.truncate_offset = inode->i_size % fs->blocksize;
214*6a54128fSAndroid Build Coastguard Worker 	} else {
215*6a54128fSAndroid Build Coastguard Worker 		pb.truncating = 0;
216*6a54128fSAndroid Build Coastguard Worker 		pb.truncate_block = 0;
217*6a54128fSAndroid Build Coastguard Worker 		pb.truncate_offset = 0;
218*6a54128fSAndroid Build Coastguard Worker 	}
219*6a54128fSAndroid Build Coastguard Worker 	pb.truncated_blocks = 0;
220*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE,
221*6a54128fSAndroid Build Coastguard Worker 				      block_buf, release_inode_block, &pb);
222*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
223*6a54128fSAndroid Build Coastguard Worker 		com_err("release_inode_blocks", retval,
224*6a54128fSAndroid Build Coastguard Worker 			_("while calling ext2fs_block_iterate for inode %u"),
225*6a54128fSAndroid Build Coastguard Worker 			ino);
226*6a54128fSAndroid Build Coastguard Worker 		return 1;
227*6a54128fSAndroid Build Coastguard Worker 	}
228*6a54128fSAndroid Build Coastguard Worker 	if (pb.abort)
229*6a54128fSAndroid Build Coastguard Worker 		return 1;
230*6a54128fSAndroid Build Coastguard Worker 
231*6a54128fSAndroid Build Coastguard Worker 	/* Refresh the inode since ext2fs_block_iterate may have changed it */
232*6a54128fSAndroid Build Coastguard Worker 	e2fsck_read_inode_full(ctx, ino, EXT2_INODE(inode), sizeof(*inode),
233*6a54128fSAndroid Build Coastguard Worker 			"release_inode_blocks");
234*6a54128fSAndroid Build Coastguard Worker 
235*6a54128fSAndroid Build Coastguard Worker 	if (pb.truncated_blocks)
236*6a54128fSAndroid Build Coastguard Worker 		ext2fs_iblk_sub_blocks(fs, EXT2_INODE(inode),
237*6a54128fSAndroid Build Coastguard Worker 				pb.truncated_blocks);
238*6a54128fSAndroid Build Coastguard Worker 
239*6a54128fSAndroid Build Coastguard Worker 	blk = ext2fs_file_acl_block(fs, EXT2_INODE(inode));
240*6a54128fSAndroid Build Coastguard Worker 	if (blk) {
241*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_adjust_ea_refcount3(fs, blk, block_buf, -1,
242*6a54128fSAndroid Build Coastguard Worker 				&count, ino);
243*6a54128fSAndroid Build Coastguard Worker 		if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) {
244*6a54128fSAndroid Build Coastguard Worker 			retval = 0;
245*6a54128fSAndroid Build Coastguard Worker 			count = 1;
246*6a54128fSAndroid Build Coastguard Worker 		}
247*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
248*6a54128fSAndroid Build Coastguard Worker 			com_err("release_inode_blocks", retval,
249*6a54128fSAndroid Build Coastguard Worker 		_("while calling ext2fs_adjust_ea_refcount2 for inode %u"),
250*6a54128fSAndroid Build Coastguard Worker 				ino);
251*6a54128fSAndroid Build Coastguard Worker 			return 1;
252*6a54128fSAndroid Build Coastguard Worker 		}
253*6a54128fSAndroid Build Coastguard Worker 		if (count == 0) {
254*6a54128fSAndroid Build Coastguard Worker 			if (ctx->qctx)
255*6a54128fSAndroid Build Coastguard Worker 				quota_data_sub(ctx->qctx, inode, 0,
256*6a54128fSAndroid Build Coastguard Worker 						ctx->fs->blocksize);
257*6a54128fSAndroid Build Coastguard Worker 			ext2fs_block_alloc_stats2(fs, blk, -1);
258*6a54128fSAndroid Build Coastguard Worker 			ctx->free_blocks++;
259*6a54128fSAndroid Build Coastguard Worker 		}
260*6a54128fSAndroid Build Coastguard Worker 		ext2fs_file_acl_block_set(fs, EXT2_INODE(inode), 0);
261*6a54128fSAndroid Build Coastguard Worker 	}
262*6a54128fSAndroid Build Coastguard Worker 	return 0;
263*6a54128fSAndroid Build Coastguard Worker }
264*6a54128fSAndroid Build Coastguard Worker 
265*6a54128fSAndroid Build Coastguard Worker /* Load all quota data in preparation for orphan clearing. */
e2fsck_read_all_quotas(e2fsck_t ctx)266*6a54128fSAndroid Build Coastguard Worker static errcode_t e2fsck_read_all_quotas(e2fsck_t ctx)
267*6a54128fSAndroid Build Coastguard Worker {
268*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t qf_ino;
269*6a54128fSAndroid Build Coastguard Worker 	enum quota_type qtype;
270*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval = 0;
271*6a54128fSAndroid Build Coastguard Worker 
272*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_has_feature_quota(ctx->fs->super))
273*6a54128fSAndroid Build Coastguard Worker 		return retval;
274*6a54128fSAndroid Build Coastguard Worker 
275*6a54128fSAndroid Build Coastguard Worker 	retval = quota_init_context(&ctx->qctx, ctx->fs, 0);
276*6a54128fSAndroid Build Coastguard Worker 	if (retval)
277*6a54128fSAndroid Build Coastguard Worker 		return retval;
278*6a54128fSAndroid Build Coastguard Worker 
279*6a54128fSAndroid Build Coastguard Worker 	for (qtype = 0 ; qtype < MAXQUOTAS; qtype++) {
280*6a54128fSAndroid Build Coastguard Worker 		qf_ino = *quota_sb_inump(ctx->fs->super, qtype);
281*6a54128fSAndroid Build Coastguard Worker 		if (qf_ino == 0)
282*6a54128fSAndroid Build Coastguard Worker 			continue;
283*6a54128fSAndroid Build Coastguard Worker 
284*6a54128fSAndroid Build Coastguard Worker 		retval = quota_read_all_dquots(ctx->qctx, qf_ino, qtype,
285*6a54128fSAndroid Build Coastguard Worker 					       QREAD_USAGE | QREAD_LIMITS);
286*6a54128fSAndroid Build Coastguard Worker 		if (retval)
287*6a54128fSAndroid Build Coastguard Worker 			break;
288*6a54128fSAndroid Build Coastguard Worker 	}
289*6a54128fSAndroid Build Coastguard Worker 	if (retval)
290*6a54128fSAndroid Build Coastguard Worker 		quota_release_context(&ctx->qctx);
291*6a54128fSAndroid Build Coastguard Worker 	return retval;
292*6a54128fSAndroid Build Coastguard Worker }
293*6a54128fSAndroid Build Coastguard Worker 
294*6a54128fSAndroid Build Coastguard Worker /* Write all the quota info to disk. */
e2fsck_write_all_quotas(e2fsck_t ctx)295*6a54128fSAndroid Build Coastguard Worker static errcode_t e2fsck_write_all_quotas(e2fsck_t ctx)
296*6a54128fSAndroid Build Coastguard Worker {
297*6a54128fSAndroid Build Coastguard Worker 	struct problem_context pctx;
298*6a54128fSAndroid Build Coastguard Worker 	enum quota_type qtype;
299*6a54128fSAndroid Build Coastguard Worker 
300*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_has_feature_quota(ctx->fs->super))
301*6a54128fSAndroid Build Coastguard Worker 		return 0;
302*6a54128fSAndroid Build Coastguard Worker 
303*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
304*6a54128fSAndroid Build Coastguard Worker 	for (qtype = 0 ; qtype < MAXQUOTAS; qtype++) {
305*6a54128fSAndroid Build Coastguard Worker 		pctx.num = qtype;
306*6a54128fSAndroid Build Coastguard Worker 		pctx.errcode = quota_write_inode(ctx->qctx, 1 << qtype);
307*6a54128fSAndroid Build Coastguard Worker 		if (pctx.errcode) {
308*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_6_WRITE_QUOTAS, &pctx);
309*6a54128fSAndroid Build Coastguard Worker 			break;
310*6a54128fSAndroid Build Coastguard Worker 		}
311*6a54128fSAndroid Build Coastguard Worker 	}
312*6a54128fSAndroid Build Coastguard Worker 
313*6a54128fSAndroid Build Coastguard Worker 	quota_release_context(&ctx->qctx);
314*6a54128fSAndroid Build Coastguard Worker 	return pctx.errcode;
315*6a54128fSAndroid Build Coastguard Worker }
316*6a54128fSAndroid Build Coastguard Worker 
317*6a54128fSAndroid Build Coastguard Worker /*
318*6a54128fSAndroid Build Coastguard Worker  * This function releases all of the orphan inodes.  It returns 1 if
319*6a54128fSAndroid Build Coastguard Worker  * it hit some error, and 0 on success.
320*6a54128fSAndroid Build Coastguard Worker  */
release_orphan_inodes(e2fsck_t ctx)321*6a54128fSAndroid Build Coastguard Worker static int release_orphan_inodes(e2fsck_t ctx)
322*6a54128fSAndroid Build Coastguard Worker {
323*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
324*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t	ino, next_ino;
325*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode_large inode;
326*6a54128fSAndroid Build Coastguard Worker 	struct problem_context pctx;
327*6a54128fSAndroid Build Coastguard Worker 	char *block_buf;
328*6a54128fSAndroid Build Coastguard Worker 
329*6a54128fSAndroid Build Coastguard Worker 	if ((ino = fs->super->s_last_orphan) == 0)
330*6a54128fSAndroid Build Coastguard Worker 		return 0;
331*6a54128fSAndroid Build Coastguard Worker 
332*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
333*6a54128fSAndroid Build Coastguard Worker 	pctx.errcode = e2fsck_read_all_quotas(ctx);
334*6a54128fSAndroid Build Coastguard Worker 	if (pctx.errcode) {
335*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_0_QUOTA_INIT_CTX, &pctx);
336*6a54128fSAndroid Build Coastguard Worker 		return 1;
337*6a54128fSAndroid Build Coastguard Worker 	}
338*6a54128fSAndroid Build Coastguard Worker 
339*6a54128fSAndroid Build Coastguard Worker 	/*
340*6a54128fSAndroid Build Coastguard Worker 	 * Win or lose, we won't be using the head of the orphan inode
341*6a54128fSAndroid Build Coastguard Worker 	 * list again.
342*6a54128fSAndroid Build Coastguard Worker 	 */
343*6a54128fSAndroid Build Coastguard Worker 	fs->super->s_last_orphan = 0;
344*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_super_dirty(fs);
345*6a54128fSAndroid Build Coastguard Worker 
346*6a54128fSAndroid Build Coastguard Worker 	/*
347*6a54128fSAndroid Build Coastguard Worker 	 * If the filesystem contains errors, don't run the orphan
348*6a54128fSAndroid Build Coastguard Worker 	 * list, since the orphan list can't be trusted; and we're
349*6a54128fSAndroid Build Coastguard Worker 	 * going to be running a full e2fsck run anyway...
350*6a54128fSAndroid Build Coastguard Worker 	 */
351*6a54128fSAndroid Build Coastguard Worker 	if (fs->super->s_state & EXT2_ERROR_FS) {
352*6a54128fSAndroid Build Coastguard Worker 		if (ctx->qctx)
353*6a54128fSAndroid Build Coastguard Worker 			quota_release_context(&ctx->qctx);
354*6a54128fSAndroid Build Coastguard Worker 		return 0;
355*6a54128fSAndroid Build Coastguard Worker 	}
356*6a54128fSAndroid Build Coastguard Worker 
357*6a54128fSAndroid Build Coastguard Worker 	if ((ino < EXT2_FIRST_INODE(fs->super)) ||
358*6a54128fSAndroid Build Coastguard Worker 	    (ino > fs->super->s_inodes_count)) {
359*6a54128fSAndroid Build Coastguard Worker 		clear_problem_context(&pctx);
360*6a54128fSAndroid Build Coastguard Worker 		pctx.ino = ino;
361*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx);
362*6a54128fSAndroid Build Coastguard Worker 		goto err_qctx;
363*6a54128fSAndroid Build Coastguard Worker 	}
364*6a54128fSAndroid Build Coastguard Worker 
365*6a54128fSAndroid Build Coastguard Worker 	block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4,
366*6a54128fSAndroid Build Coastguard Worker 						    "block iterate buffer");
367*6a54128fSAndroid Build Coastguard Worker 	e2fsck_read_bitmaps(ctx);
368*6a54128fSAndroid Build Coastguard Worker 
369*6a54128fSAndroid Build Coastguard Worker 	while (ino) {
370*6a54128fSAndroid Build Coastguard Worker 		e2fsck_read_inode_full(ctx, ino, EXT2_INODE(&inode),
371*6a54128fSAndroid Build Coastguard Worker 				sizeof(inode), "release_orphan_inodes");
372*6a54128fSAndroid Build Coastguard Worker 		clear_problem_context(&pctx);
373*6a54128fSAndroid Build Coastguard Worker 		pctx.ino = ino;
374*6a54128fSAndroid Build Coastguard Worker 		pctx.inode = EXT2_INODE(&inode);
375*6a54128fSAndroid Build Coastguard Worker 		pctx.str = inode.i_links_count ? _("Truncating") :
376*6a54128fSAndroid Build Coastguard Worker 			_("Clearing");
377*6a54128fSAndroid Build Coastguard Worker 
378*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx);
379*6a54128fSAndroid Build Coastguard Worker 
380*6a54128fSAndroid Build Coastguard Worker 		next_ino = inode.i_dtime;
381*6a54128fSAndroid Build Coastguard Worker 		if (next_ino &&
382*6a54128fSAndroid Build Coastguard Worker 		    ((next_ino < EXT2_FIRST_INODE(fs->super)) ||
383*6a54128fSAndroid Build Coastguard Worker 		     (next_ino > fs->super->s_inodes_count))) {
384*6a54128fSAndroid Build Coastguard Worker 			pctx.ino = next_ino;
385*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx);
386*6a54128fSAndroid Build Coastguard Worker 			goto err_buf;
387*6a54128fSAndroid Build Coastguard Worker 		}
388*6a54128fSAndroid Build Coastguard Worker 
389*6a54128fSAndroid Build Coastguard Worker 		if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx))
390*6a54128fSAndroid Build Coastguard Worker 			goto err_buf;
391*6a54128fSAndroid Build Coastguard Worker 
392*6a54128fSAndroid Build Coastguard Worker 		if (!inode.i_links_count) {
393*6a54128fSAndroid Build Coastguard Worker 			if (ctx->qctx)
394*6a54128fSAndroid Build Coastguard Worker 				quota_data_inodes(ctx->qctx, &inode, ino, -1);
395*6a54128fSAndroid Build Coastguard Worker 			ext2fs_inode_alloc_stats2(fs, ino, -1,
396*6a54128fSAndroid Build Coastguard Worker 						  LINUX_S_ISDIR(inode.i_mode));
397*6a54128fSAndroid Build Coastguard Worker 			ctx->free_inodes++;
398*6a54128fSAndroid Build Coastguard Worker 			inode.i_dtime = ctx->now;
399*6a54128fSAndroid Build Coastguard Worker 		} else {
400*6a54128fSAndroid Build Coastguard Worker 			inode.i_dtime = 0;
401*6a54128fSAndroid Build Coastguard Worker 		}
402*6a54128fSAndroid Build Coastguard Worker 		e2fsck_write_inode_full(ctx, ino, EXT2_INODE(&inode),
403*6a54128fSAndroid Build Coastguard Worker 				sizeof(inode), "delete_file");
404*6a54128fSAndroid Build Coastguard Worker 		ino = next_ino;
405*6a54128fSAndroid Build Coastguard Worker 	}
406*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&block_buf);
407*6a54128fSAndroid Build Coastguard Worker 	pctx.errcode = e2fsck_write_all_quotas(ctx);
408*6a54128fSAndroid Build Coastguard Worker 	if (pctx.errcode)
409*6a54128fSAndroid Build Coastguard Worker 		goto err;
410*6a54128fSAndroid Build Coastguard Worker 	return 0;
411*6a54128fSAndroid Build Coastguard Worker err_buf:
412*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&block_buf);
413*6a54128fSAndroid Build Coastguard Worker err_qctx:
414*6a54128fSAndroid Build Coastguard Worker 	if (ctx->qctx)
415*6a54128fSAndroid Build Coastguard Worker 		quota_release_context(&ctx->qctx);
416*6a54128fSAndroid Build Coastguard Worker err:
417*6a54128fSAndroid Build Coastguard Worker 	return 1;
418*6a54128fSAndroid Build Coastguard Worker }
419*6a54128fSAndroid Build Coastguard Worker 
420*6a54128fSAndroid Build Coastguard Worker /*
421*6a54128fSAndroid Build Coastguard Worker  * Check the resize inode to make sure it is sane.  We check both for
422*6a54128fSAndroid Build Coastguard Worker  * the case where on-line resizing is not enabled (in which case the
423*6a54128fSAndroid Build Coastguard Worker  * resize inode should be cleared) as well as the case where on-line
424*6a54128fSAndroid Build Coastguard Worker  * resizing is enabled.
425*6a54128fSAndroid Build Coastguard Worker  */
check_resize_inode(e2fsck_t ctx)426*6a54128fSAndroid Build Coastguard Worker void check_resize_inode(e2fsck_t ctx)
427*6a54128fSAndroid Build Coastguard Worker {
428*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
429*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode inode;
430*6a54128fSAndroid Build Coastguard Worker 	struct problem_context	pctx;
431*6a54128fSAndroid Build Coastguard Worker 	int		i, gdt_off, ind_off;
432*6a54128fSAndroid Build Coastguard Worker 	dgrp_t		j;
433*6a54128fSAndroid Build Coastguard Worker 	blk_t		blk, pblk;
434*6a54128fSAndroid Build Coastguard Worker 	blk_t		expect;	/* for resize inode, which is 32-bit only */
435*6a54128fSAndroid Build Coastguard Worker 	__u32 		*dind_buf = 0, *ind_buf;
436*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
437*6a54128fSAndroid Build Coastguard Worker 
438*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
439*6a54128fSAndroid Build Coastguard Worker 
440*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_resize_inode(fs->super) &&
441*6a54128fSAndroid Build Coastguard Worker 	    ext2fs_has_feature_meta_bg(fs->super) &&
442*6a54128fSAndroid Build Coastguard Worker 	    fix_problem(ctx, PR_0_DISABLE_RESIZE_INODE, &pctx)) {
443*6a54128fSAndroid Build Coastguard Worker 		ext2fs_clear_feature_resize_inode(fs->super);
444*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_reserved_gdt_blocks = 0;
445*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
446*6a54128fSAndroid Build Coastguard Worker 	}
447*6a54128fSAndroid Build Coastguard Worker 
448*6a54128fSAndroid Build Coastguard Worker 	/*
449*6a54128fSAndroid Build Coastguard Worker 	 * If the resize inode feature isn't set, then
450*6a54128fSAndroid Build Coastguard Worker 	 * s_reserved_gdt_blocks must be zero.
451*6a54128fSAndroid Build Coastguard Worker 	 */
452*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_has_feature_resize_inode(fs->super)) {
453*6a54128fSAndroid Build Coastguard Worker 		if (fs->super->s_reserved_gdt_blocks) {
454*6a54128fSAndroid Build Coastguard Worker 			pctx.num = fs->super->s_reserved_gdt_blocks;
455*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS,
456*6a54128fSAndroid Build Coastguard Worker 					&pctx)) {
457*6a54128fSAndroid Build Coastguard Worker 				fs->super->s_reserved_gdt_blocks = 0;
458*6a54128fSAndroid Build Coastguard Worker 				ext2fs_mark_super_dirty(fs);
459*6a54128fSAndroid Build Coastguard Worker 			}
460*6a54128fSAndroid Build Coastguard Worker 		}
461*6a54128fSAndroid Build Coastguard Worker 	}
462*6a54128fSAndroid Build Coastguard Worker 
463*6a54128fSAndroid Build Coastguard Worker 	/* Read the resize inode */
464*6a54128fSAndroid Build Coastguard Worker 	pctx.ino = EXT2_RESIZE_INO;
465*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode);
466*6a54128fSAndroid Build Coastguard Worker 	if (retval) {
467*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_has_feature_resize_inode(fs->super))
468*6a54128fSAndroid Build Coastguard Worker 			ctx->flags |= E2F_FLAG_RESIZE_INODE;
469*6a54128fSAndroid Build Coastguard Worker 		return;
470*6a54128fSAndroid Build Coastguard Worker 	}
471*6a54128fSAndroid Build Coastguard Worker 
472*6a54128fSAndroid Build Coastguard Worker 	/*
473*6a54128fSAndroid Build Coastguard Worker 	 * If the resize inode feature isn't set, check to make sure
474*6a54128fSAndroid Build Coastguard Worker 	 * the resize inode is cleared; then we're done.
475*6a54128fSAndroid Build Coastguard Worker 	 */
476*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_has_feature_resize_inode(fs->super)) {
477*6a54128fSAndroid Build Coastguard Worker 		for (i=0; i < EXT2_N_BLOCKS; i++) {
478*6a54128fSAndroid Build Coastguard Worker 			if (inode.i_block[i])
479*6a54128fSAndroid Build Coastguard Worker 				break;
480*6a54128fSAndroid Build Coastguard Worker 		}
481*6a54128fSAndroid Build Coastguard Worker 		if ((i < EXT2_N_BLOCKS) &&
482*6a54128fSAndroid Build Coastguard Worker 		    fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) {
483*6a54128fSAndroid Build Coastguard Worker 			memset(&inode, 0, sizeof(inode));
484*6a54128fSAndroid Build Coastguard Worker 			e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
485*6a54128fSAndroid Build Coastguard Worker 					   "clear_resize");
486*6a54128fSAndroid Build Coastguard Worker 		}
487*6a54128fSAndroid Build Coastguard Worker 		return;
488*6a54128fSAndroid Build Coastguard Worker 	}
489*6a54128fSAndroid Build Coastguard Worker 
490*6a54128fSAndroid Build Coastguard Worker 	/*
491*6a54128fSAndroid Build Coastguard Worker 	 * The resize inode feature is enabled; check to make sure the
492*6a54128fSAndroid Build Coastguard Worker 	 * only block in use is the double indirect block
493*6a54128fSAndroid Build Coastguard Worker 	 */
494*6a54128fSAndroid Build Coastguard Worker 	blk = inode.i_block[EXT2_DIND_BLOCK];
495*6a54128fSAndroid Build Coastguard Worker 	for (i=0; i < EXT2_N_BLOCKS; i++) {
496*6a54128fSAndroid Build Coastguard Worker 		if (i != EXT2_DIND_BLOCK && inode.i_block[i])
497*6a54128fSAndroid Build Coastguard Worker 			break;
498*6a54128fSAndroid Build Coastguard Worker 	}
499*6a54128fSAndroid Build Coastguard Worker 	if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count ||
500*6a54128fSAndroid Build Coastguard Worker 	    !(inode.i_mode & LINUX_S_IFREG) ||
501*6a54128fSAndroid Build Coastguard Worker 	    (blk < fs->super->s_first_data_block ||
502*6a54128fSAndroid Build Coastguard Worker 	     blk >= ext2fs_blocks_count(fs->super))) {
503*6a54128fSAndroid Build Coastguard Worker 	resize_inode_invalid:
504*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) {
505*6a54128fSAndroid Build Coastguard Worker 			memset(&inode, 0, sizeof(inode));
506*6a54128fSAndroid Build Coastguard Worker 			e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode,
507*6a54128fSAndroid Build Coastguard Worker 					   "clear_resize");
508*6a54128fSAndroid Build Coastguard Worker 			ctx->flags |= E2F_FLAG_RESIZE_INODE;
509*6a54128fSAndroid Build Coastguard Worker 		}
510*6a54128fSAndroid Build Coastguard Worker 		if (!(ctx->options & E2F_OPT_READONLY)) {
511*6a54128fSAndroid Build Coastguard Worker 			fs->super->s_state &= ~EXT2_VALID_FS;
512*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
513*6a54128fSAndroid Build Coastguard Worker 		}
514*6a54128fSAndroid Build Coastguard Worker 		goto cleanup;
515*6a54128fSAndroid Build Coastguard Worker 	}
516*6a54128fSAndroid Build Coastguard Worker 	dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2,
517*6a54128fSAndroid Build Coastguard Worker 						    "resize dind buffer");
518*6a54128fSAndroid Build Coastguard Worker 	ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize);
519*6a54128fSAndroid Build Coastguard Worker 
520*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_ind_block(fs, blk, dind_buf);
521*6a54128fSAndroid Build Coastguard Worker 	if (retval)
522*6a54128fSAndroid Build Coastguard Worker 		goto resize_inode_invalid;
523*6a54128fSAndroid Build Coastguard Worker 
524*6a54128fSAndroid Build Coastguard Worker 	gdt_off = fs->desc_blocks;
525*6a54128fSAndroid Build Coastguard Worker 	pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks;
526*6a54128fSAndroid Build Coastguard Worker 	if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0)
527*6a54128fSAndroid Build Coastguard Worker 		pblk++;	/* Deal with 1024 blocksize bigalloc fs */
528*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4;
529*6a54128fSAndroid Build Coastguard Worker 	     i++, gdt_off++, pblk++) {
530*6a54128fSAndroid Build Coastguard Worker 		gdt_off %= fs->blocksize/4;
531*6a54128fSAndroid Build Coastguard Worker 		if (dind_buf[gdt_off] != pblk)
532*6a54128fSAndroid Build Coastguard Worker 			goto resize_inode_invalid;
533*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_read_ind_block(fs, pblk, ind_buf);
534*6a54128fSAndroid Build Coastguard Worker 		if (retval)
535*6a54128fSAndroid Build Coastguard Worker 			goto resize_inode_invalid;
536*6a54128fSAndroid Build Coastguard Worker 		ind_off = 0;
537*6a54128fSAndroid Build Coastguard Worker 		for (j = 1; j < fs->group_desc_count; j++) {
538*6a54128fSAndroid Build Coastguard Worker 			if (!ext2fs_bg_has_super(fs, j))
539*6a54128fSAndroid Build Coastguard Worker 				continue;
540*6a54128fSAndroid Build Coastguard Worker 			expect = pblk + EXT2_GROUPS_TO_BLOCKS(fs->super, j);
541*6a54128fSAndroid Build Coastguard Worker 			if (ind_buf[ind_off] != expect)
542*6a54128fSAndroid Build Coastguard Worker 				goto resize_inode_invalid;
543*6a54128fSAndroid Build Coastguard Worker 			ind_off++;
544*6a54128fSAndroid Build Coastguard Worker 		}
545*6a54128fSAndroid Build Coastguard Worker 	}
546*6a54128fSAndroid Build Coastguard Worker 
547*6a54128fSAndroid Build Coastguard Worker cleanup:
548*6a54128fSAndroid Build Coastguard Worker 	if (dind_buf)
549*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&dind_buf);
550*6a54128fSAndroid Build Coastguard Worker 
551*6a54128fSAndroid Build Coastguard Worker  }
552*6a54128fSAndroid Build Coastguard Worker 
553*6a54128fSAndroid Build Coastguard Worker /*
554*6a54128fSAndroid Build Coastguard Worker  * This function checks the dirhash signed/unsigned hint if necessary.
555*6a54128fSAndroid Build Coastguard Worker  */
e2fsck_fix_dirhash_hint(e2fsck_t ctx)556*6a54128fSAndroid Build Coastguard Worker static void e2fsck_fix_dirhash_hint(e2fsck_t ctx)
557*6a54128fSAndroid Build Coastguard Worker {
558*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *sb = ctx->fs->super;
559*6a54128fSAndroid Build Coastguard Worker 	struct problem_context pctx;
560*6a54128fSAndroid Build Coastguard Worker 	char	c;
561*6a54128fSAndroid Build Coastguard Worker 
562*6a54128fSAndroid Build Coastguard Worker 	if ((ctx->options & E2F_OPT_READONLY) ||
563*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_has_feature_dir_index(sb) ||
564*6a54128fSAndroid Build Coastguard Worker 	    (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH)))
565*6a54128fSAndroid Build Coastguard Worker 		return;
566*6a54128fSAndroid Build Coastguard Worker 
567*6a54128fSAndroid Build Coastguard Worker 	c = (char) 255;
568*6a54128fSAndroid Build Coastguard Worker 
569*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
570*6a54128fSAndroid Build Coastguard Worker 	if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) {
571*6a54128fSAndroid Build Coastguard Worker 		if (((int) c) == -1) {
572*6a54128fSAndroid Build Coastguard Worker 			sb->s_flags |= EXT2_FLAGS_SIGNED_HASH;
573*6a54128fSAndroid Build Coastguard Worker 		} else {
574*6a54128fSAndroid Build Coastguard Worker 			sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
575*6a54128fSAndroid Build Coastguard Worker 		}
576*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(ctx->fs);
577*6a54128fSAndroid Build Coastguard Worker 	}
578*6a54128fSAndroid Build Coastguard Worker }
579*6a54128fSAndroid Build Coastguard Worker 
580*6a54128fSAndroid Build Coastguard Worker 
check_super_block(e2fsck_t ctx)581*6a54128fSAndroid Build Coastguard Worker void check_super_block(e2fsck_t ctx)
582*6a54128fSAndroid Build Coastguard Worker {
583*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
584*6a54128fSAndroid Build Coastguard Worker 	blk64_t	first_block, last_block;
585*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *sb = fs->super;
586*6a54128fSAndroid Build Coastguard Worker 	unsigned int	ipg_max;
587*6a54128fSAndroid Build Coastguard Worker 	problem_t	problem;
588*6a54128fSAndroid Build Coastguard Worker 	blk64_t	blocks_per_group = fs->super->s_blocks_per_group;
589*6a54128fSAndroid Build Coastguard Worker 	__u32	bpg_max, cpg_max;
590*6a54128fSAndroid Build Coastguard Worker 	__u64	blks_max;
591*6a54128fSAndroid Build Coastguard Worker 	int	inodes_per_block;
592*6a54128fSAndroid Build Coastguard Worker 	int	inode_size;
593*6a54128fSAndroid Build Coastguard Worker 	int	accept_time_fudge;
594*6a54128fSAndroid Build Coastguard Worker 	int	broken_system_clock;
595*6a54128fSAndroid Build Coastguard Worker 	dgrp_t	i;
596*6a54128fSAndroid Build Coastguard Worker 	blk64_t	should_be;
597*6a54128fSAndroid Build Coastguard Worker 	struct problem_context	pctx;
598*6a54128fSAndroid Build Coastguard Worker 	blk64_t	free_blocks = 0;
599*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t free_inodes = 0;
600*6a54128fSAndroid Build Coastguard Worker 	int     csum_flag, clear_test_fs_flag;
601*6a54128fSAndroid Build Coastguard Worker 
602*6a54128fSAndroid Build Coastguard Worker 	inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super);
603*6a54128fSAndroid Build Coastguard Worker 	ipg_max = inodes_per_block * (blocks_per_group - 4);
604*6a54128fSAndroid Build Coastguard Worker 	if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb))
605*6a54128fSAndroid Build Coastguard Worker 		ipg_max = EXT2_MAX_INODES_PER_GROUP(sb);
606*6a54128fSAndroid Build Coastguard Worker 	cpg_max = 8 * EXT2_BLOCK_SIZE(sb);
607*6a54128fSAndroid Build Coastguard Worker 	if (cpg_max > EXT2_MAX_CLUSTERS_PER_GROUP(sb))
608*6a54128fSAndroid Build Coastguard Worker 		cpg_max = EXT2_MAX_CLUSTERS_PER_GROUP(sb);
609*6a54128fSAndroid Build Coastguard Worker 	bpg_max = 8 * EXT2_BLOCK_SIZE(sb) * EXT2FS_CLUSTER_RATIO(fs);
610*6a54128fSAndroid Build Coastguard Worker 	if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb))
611*6a54128fSAndroid Build Coastguard Worker 		bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb);
612*6a54128fSAndroid Build Coastguard Worker 
613*6a54128fSAndroid Build Coastguard Worker 	ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
614*6a54128fSAndroid Build Coastguard Worker 		 sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap");
615*6a54128fSAndroid Build Coastguard Worker 	ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx,
616*6a54128fSAndroid Build Coastguard Worker 		 sizeof(int) * fs->group_desc_count, "invalid_block_bitmap");
617*6a54128fSAndroid Build Coastguard Worker 	ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx,
618*6a54128fSAndroid Build Coastguard Worker 		sizeof(int) * fs->group_desc_count, "invalid_inode_table");
619*6a54128fSAndroid Build Coastguard Worker 
620*6a54128fSAndroid Build Coastguard Worker 	blks_max = (1ULL << 32) * EXT2_MAX_BLOCKS_PER_GROUP(fs->super);
621*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_64bit(fs->super)) {
622*6a54128fSAndroid Build Coastguard Worker 		if (blks_max > ((1ULL << 48) - 1))
623*6a54128fSAndroid Build Coastguard Worker 			blks_max = (1ULL << 48) - 1;
624*6a54128fSAndroid Build Coastguard Worker 	} else {
625*6a54128fSAndroid Build Coastguard Worker 		if (blks_max > ((1ULL << 32) - 1))
626*6a54128fSAndroid Build Coastguard Worker 			blks_max = (1ULL << 32) - 1;
627*6a54128fSAndroid Build Coastguard Worker 	}
628*6a54128fSAndroid Build Coastguard Worker 
629*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
630*6a54128fSAndroid Build Coastguard Worker 
631*6a54128fSAndroid Build Coastguard Worker 	/*
632*6a54128fSAndroid Build Coastguard Worker 	 * Verify the super block constants...
633*6a54128fSAndroid Build Coastguard Worker 	 */
634*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value(ctx, "inodes_count", sb->s_inodes_count,
635*6a54128fSAndroid Build Coastguard Worker 			       MIN_CHECK, 1, 0))
636*6a54128fSAndroid Build Coastguard Worker 		return;
637*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value64(ctx, "blocks_count", ext2fs_blocks_count(sb),
638*6a54128fSAndroid Build Coastguard Worker 				 MIN_CHECK | MAX_CHECK, 1, blks_max))
639*6a54128fSAndroid Build Coastguard Worker 		return;
640*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value(ctx, "first_data_block", sb->s_first_data_block,
641*6a54128fSAndroid Build Coastguard Worker 			       MAX_CHECK, 0, ext2fs_blocks_count(sb)))
642*6a54128fSAndroid Build Coastguard Worker 		return;
643*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value(ctx, "log_block_size", sb->s_log_block_size,
644*6a54128fSAndroid Build Coastguard Worker 			       MIN_CHECK | MAX_CHECK, 0,
645*6a54128fSAndroid Build Coastguard Worker 			       EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE))
646*6a54128fSAndroid Build Coastguard Worker 		return;
647*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value(ctx, "log_cluster_size",
648*6a54128fSAndroid Build Coastguard Worker 			       sb->s_log_cluster_size,
649*6a54128fSAndroid Build Coastguard Worker 			       MIN_CHECK | MAX_CHECK, sb->s_log_block_size,
650*6a54128fSAndroid Build Coastguard Worker 			       (EXT2_MAX_CLUSTER_LOG_SIZE -
651*6a54128fSAndroid Build Coastguard Worker 			        EXT2_MIN_CLUSTER_LOG_SIZE)))
652*6a54128fSAndroid Build Coastguard Worker 		return;
653*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value(ctx, "clusters_per_group",
654*6a54128fSAndroid Build Coastguard Worker 			       sb->s_clusters_per_group,
655*6a54128fSAndroid Build Coastguard Worker 			       MIN_CHECK | MAX_CHECK, 8, cpg_max))
656*6a54128fSAndroid Build Coastguard Worker 		return;
657*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group,
658*6a54128fSAndroid Build Coastguard Worker 			       MIN_CHECK | MAX_CHECK, 8, bpg_max))
659*6a54128fSAndroid Build Coastguard Worker 		return;
660*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group,
661*6a54128fSAndroid Build Coastguard Worker 			       MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max))
662*6a54128fSAndroid Build Coastguard Worker 		return;
663*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value(ctx, "r_blocks_count", ext2fs_r_blocks_count(sb),
664*6a54128fSAndroid Build Coastguard Worker 			       MAX_CHECK, 0, ext2fs_blocks_count(sb) / 2))
665*6a54128fSAndroid Build Coastguard Worker 		return;
666*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value(ctx, "reserved_gdt_blocks",
667*6a54128fSAndroid Build Coastguard Worker 			       sb->s_reserved_gdt_blocks, MAX_CHECK, 0,
668*6a54128fSAndroid Build Coastguard Worker 			       fs->blocksize / sizeof(__u32)))
669*6a54128fSAndroid Build Coastguard Worker 		return;
670*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value(ctx, "desc_size",
671*6a54128fSAndroid Build Coastguard Worker 			       sb->s_desc_size, MAX_CHECK | LOG2_CHECK, 0,
672*6a54128fSAndroid Build Coastguard Worker 			       EXT2_MAX_DESC_SIZE))
673*6a54128fSAndroid Build Coastguard Worker 		return;
674*6a54128fSAndroid Build Coastguard Worker 
675*6a54128fSAndroid Build Coastguard Worker 	should_be = (__u64)sb->s_inodes_per_group * fs->group_desc_count;
676*6a54128fSAndroid Build Coastguard Worker 	if (should_be > ~0U) {
677*6a54128fSAndroid Build Coastguard Worker 		pctx.num = should_be;
678*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_0_INODE_COUNT_BIG, &pctx);
679*6a54128fSAndroid Build Coastguard Worker 		ctx->flags |= E2F_FLAG_ABORT;
680*6a54128fSAndroid Build Coastguard Worker 		return;
681*6a54128fSAndroid Build Coastguard Worker 	}
682*6a54128fSAndroid Build Coastguard Worker 	if (sb->s_inodes_count != should_be) {
683*6a54128fSAndroid Build Coastguard Worker 		pctx.ino = sb->s_inodes_count;
684*6a54128fSAndroid Build Coastguard Worker 		pctx.ino2 = should_be;
685*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) {
686*6a54128fSAndroid Build Coastguard Worker 			sb->s_inodes_count = should_be;
687*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
688*6a54128fSAndroid Build Coastguard Worker 		} else {
689*6a54128fSAndroid Build Coastguard Worker 			pctx.num = sb->s_inodes_count;
690*6a54128fSAndroid Build Coastguard Worker 			pctx.str = "inodes_count";
691*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
692*6a54128fSAndroid Build Coastguard Worker 			ctx->flags |= E2F_FLAG_ABORT;
693*6a54128fSAndroid Build Coastguard Worker 			return;
694*6a54128fSAndroid Build Coastguard Worker 		}
695*6a54128fSAndroid Build Coastguard Worker 	}
696*6a54128fSAndroid Build Coastguard Worker 	if (sb->s_rev_level > EXT2_GOOD_OLD_REV &&
697*6a54128fSAndroid Build Coastguard Worker 	    !check_super_value(ctx, "first_ino", sb->s_first_ino,
698*6a54128fSAndroid Build Coastguard Worker 			       MIN_CHECK | MAX_CHECK,
699*6a54128fSAndroid Build Coastguard Worker 			       EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count))
700*6a54128fSAndroid Build Coastguard Worker 		return;
701*6a54128fSAndroid Build Coastguard Worker 	inode_size = EXT2_INODE_SIZE(sb);
702*6a54128fSAndroid Build Coastguard Worker 	if (!check_super_value(ctx, "inode_size",
703*6a54128fSAndroid Build Coastguard Worker 			       inode_size, MIN_CHECK | MAX_CHECK | LOG2_CHECK,
704*6a54128fSAndroid Build Coastguard Worker 			       EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize))
705*6a54128fSAndroid Build Coastguard Worker 		return;
706*6a54128fSAndroid Build Coastguard Worker 	if (sb->s_blocks_per_group != (sb->s_clusters_per_group *
707*6a54128fSAndroid Build Coastguard Worker 				       EXT2FS_CLUSTER_RATIO(fs))) {
708*6a54128fSAndroid Build Coastguard Worker 		pctx.num = sb->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs);
709*6a54128fSAndroid Build Coastguard Worker 		pctx.str = "block_size";
710*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx);
711*6a54128fSAndroid Build Coastguard Worker 		ctx->flags |= E2F_FLAG_ABORT;
712*6a54128fSAndroid Build Coastguard Worker 		return;
713*6a54128fSAndroid Build Coastguard Worker 	}
714*6a54128fSAndroid Build Coastguard Worker 
715*6a54128fSAndroid Build Coastguard Worker 	if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) &&
716*6a54128fSAndroid Build Coastguard Worker 	    (ctx->num_blocks < ext2fs_blocks_count(sb))) {
717*6a54128fSAndroid Build Coastguard Worker 		pctx.blk = ext2fs_blocks_count(sb);
718*6a54128fSAndroid Build Coastguard Worker 		pctx.blk2 = ctx->num_blocks;
719*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) {
720*6a54128fSAndroid Build Coastguard Worker 			ctx->flags |= E2F_FLAG_ABORT;
721*6a54128fSAndroid Build Coastguard Worker 			return;
722*6a54128fSAndroid Build Coastguard Worker 		}
723*6a54128fSAndroid Build Coastguard Worker 	}
724*6a54128fSAndroid Build Coastguard Worker 
725*6a54128fSAndroid Build Coastguard Worker 	should_be = (sb->s_log_block_size == 0 &&
726*6a54128fSAndroid Build Coastguard Worker 		     EXT2FS_CLUSTER_RATIO(fs) == 1) ? 1 : 0;
727*6a54128fSAndroid Build Coastguard Worker 	if (sb->s_first_data_block != should_be) {
728*6a54128fSAndroid Build Coastguard Worker 		pctx.blk = sb->s_first_data_block;
729*6a54128fSAndroid Build Coastguard Worker 		pctx.blk2 = should_be;
730*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx);
731*6a54128fSAndroid Build Coastguard Worker 		ctx->flags |= E2F_FLAG_ABORT;
732*6a54128fSAndroid Build Coastguard Worker 		return;
733*6a54128fSAndroid Build Coastguard Worker 	}
734*6a54128fSAndroid Build Coastguard Worker 
735*6a54128fSAndroid Build Coastguard Worker 	if (EXT2_INODE_SIZE(sb) > EXT2_GOOD_OLD_INODE_SIZE) {
736*6a54128fSAndroid Build Coastguard Worker 		unsigned min =
737*6a54128fSAndroid Build Coastguard Worker 			sizeof(((struct ext2_inode_large *) 0)->i_extra_isize) +
738*6a54128fSAndroid Build Coastguard Worker 			sizeof(((struct ext2_inode_large *) 0)->i_checksum_hi);
739*6a54128fSAndroid Build Coastguard Worker 		unsigned max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
740*6a54128fSAndroid Build Coastguard Worker 		pctx.num = sb->s_min_extra_isize;
741*6a54128fSAndroid Build Coastguard Worker 		if (sb->s_min_extra_isize &&
742*6a54128fSAndroid Build Coastguard Worker 		    (sb->s_min_extra_isize < min ||
743*6a54128fSAndroid Build Coastguard Worker 		     sb->s_min_extra_isize > max ||
744*6a54128fSAndroid Build Coastguard Worker 		     sb->s_min_extra_isize & 3) &&
745*6a54128fSAndroid Build Coastguard Worker 		    fix_problem(ctx, PR_0_BAD_MIN_EXTRA_ISIZE, &pctx)) {
746*6a54128fSAndroid Build Coastguard Worker 			sb->s_min_extra_isize =
747*6a54128fSAndroid Build Coastguard Worker 				(sizeof(struct ext2_inode_large) -
748*6a54128fSAndroid Build Coastguard Worker 				 EXT2_GOOD_OLD_INODE_SIZE);
749*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
750*6a54128fSAndroid Build Coastguard Worker 		}
751*6a54128fSAndroid Build Coastguard Worker 		pctx.num = sb->s_want_extra_isize;
752*6a54128fSAndroid Build Coastguard Worker 		if (sb->s_want_extra_isize &&
753*6a54128fSAndroid Build Coastguard Worker 		    (sb->s_want_extra_isize < min ||
754*6a54128fSAndroid Build Coastguard Worker 		     sb->s_want_extra_isize > max ||
755*6a54128fSAndroid Build Coastguard Worker 		     sb->s_want_extra_isize & 3) &&
756*6a54128fSAndroid Build Coastguard Worker 		    fix_problem(ctx, PR_0_BAD_WANT_EXTRA_ISIZE, &pctx)) {
757*6a54128fSAndroid Build Coastguard Worker 			sb->s_want_extra_isize =
758*6a54128fSAndroid Build Coastguard Worker 				(sizeof(struct ext2_inode_large) -
759*6a54128fSAndroid Build Coastguard Worker 				 EXT2_GOOD_OLD_INODE_SIZE);
760*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
761*6a54128fSAndroid Build Coastguard Worker 		}
762*6a54128fSAndroid Build Coastguard Worker 	}
763*6a54128fSAndroid Build Coastguard Worker 
764*6a54128fSAndroid Build Coastguard Worker 	/* Are metadata_csum and uninit_bg both set? */
765*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_metadata_csum(fs->super) &&
766*6a54128fSAndroid Build Coastguard Worker 	    ext2fs_has_feature_gdt_csum(fs->super) &&
767*6a54128fSAndroid Build Coastguard Worker 	    fix_problem(ctx, PR_0_META_AND_GDT_CSUM_SET, &pctx)) {
768*6a54128fSAndroid Build Coastguard Worker 		ext2fs_clear_feature_gdt_csum(fs->super);
769*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
770*6a54128fSAndroid Build Coastguard Worker 		for (i = 0; i < fs->group_desc_count; i++)
771*6a54128fSAndroid Build Coastguard Worker 			ext2fs_group_desc_csum_set(fs, i);
772*6a54128fSAndroid Build Coastguard Worker 	}
773*6a54128fSAndroid Build Coastguard Worker 
774*6a54128fSAndroid Build Coastguard Worker 	/* We can't have ^metadata_csum,metadata_csum_seed */
775*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_has_feature_metadata_csum(fs->super) &&
776*6a54128fSAndroid Build Coastguard Worker 	    ext2fs_has_feature_csum_seed(fs->super) &&
777*6a54128fSAndroid Build Coastguard Worker 	    fix_problem(ctx, PR_0_CSUM_SEED_WITHOUT_META_CSUM, &pctx)) {
778*6a54128fSAndroid Build Coastguard Worker 		ext2fs_clear_feature_csum_seed(fs->super);
779*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_checksum_seed = 0;
780*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
781*6a54128fSAndroid Build Coastguard Worker 	}
782*6a54128fSAndroid Build Coastguard Worker 
783*6a54128fSAndroid Build Coastguard Worker 	/* Is 64bit set and extents unset? */
784*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_64bit(fs->super) &&
785*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_has_feature_extents(fs->super) &&
786*6a54128fSAndroid Build Coastguard Worker 	    fix_problem(ctx, PR_0_64BIT_WITHOUT_EXTENTS, &pctx)) {
787*6a54128fSAndroid Build Coastguard Worker 		ext2fs_set_feature_extents(fs->super);
788*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
789*6a54128fSAndroid Build Coastguard Worker 	}
790*6a54128fSAndroid Build Coastguard Worker 
791*6a54128fSAndroid Build Coastguard Worker 	/* Did user ask us to convert files to extents? */
792*6a54128fSAndroid Build Coastguard Worker 	if (ctx->options & E2F_OPT_CONVERT_BMAP) {
793*6a54128fSAndroid Build Coastguard Worker 		ext2fs_set_feature_extents(fs->super);
794*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
795*6a54128fSAndroid Build Coastguard Worker 	}
796*6a54128fSAndroid Build Coastguard Worker 
797*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_meta_bg(fs->super) &&
798*6a54128fSAndroid Build Coastguard Worker 	    (fs->super->s_first_meta_bg > fs->desc_blocks)) {
799*6a54128fSAndroid Build Coastguard Worker 		pctx.group = fs->desc_blocks;
800*6a54128fSAndroid Build Coastguard Worker 		pctx.num = fs->super->s_first_meta_bg;
801*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_0_FIRST_META_BG_TOO_BIG, &pctx)) {
802*6a54128fSAndroid Build Coastguard Worker 			ext2fs_clear_feature_meta_bg(fs->super);
803*6a54128fSAndroid Build Coastguard Worker 			fs->super->s_first_meta_bg = 0;
804*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
805*6a54128fSAndroid Build Coastguard Worker 		}
806*6a54128fSAndroid Build Coastguard Worker 	}
807*6a54128fSAndroid Build Coastguard Worker 
808*6a54128fSAndroid Build Coastguard Worker 	/*
809*6a54128fSAndroid Build Coastguard Worker 	 * Verify the group descriptors....
810*6a54128fSAndroid Build Coastguard Worker 	 */
811*6a54128fSAndroid Build Coastguard Worker 	first_block = sb->s_first_data_block;
812*6a54128fSAndroid Build Coastguard Worker 	last_block = ext2fs_blocks_count(sb)-1;
813*6a54128fSAndroid Build Coastguard Worker 
814*6a54128fSAndroid Build Coastguard Worker 	csum_flag = ext2fs_has_group_desc_csum(fs);
815*6a54128fSAndroid Build Coastguard Worker 	for (i = 0; i < fs->group_desc_count; i++) {
816*6a54128fSAndroid Build Coastguard Worker 		pctx.group = i;
817*6a54128fSAndroid Build Coastguard Worker 
818*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_flex_bg(fs->super)) {
819*6a54128fSAndroid Build Coastguard Worker 			first_block = ext2fs_group_first_block2(fs, i);
820*6a54128fSAndroid Build Coastguard Worker 			last_block = ext2fs_group_last_block2(fs, i);
821*6a54128fSAndroid Build Coastguard Worker 		}
822*6a54128fSAndroid Build Coastguard Worker 
823*6a54128fSAndroid Build Coastguard Worker 		if ((ext2fs_block_bitmap_loc(fs, i) < first_block) ||
824*6a54128fSAndroid Build Coastguard Worker 		    (ext2fs_block_bitmap_loc(fs, i) > last_block)) {
825*6a54128fSAndroid Build Coastguard Worker 			pctx.blk = ext2fs_block_bitmap_loc(fs, i);
826*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx))
827*6a54128fSAndroid Build Coastguard Worker 				ext2fs_block_bitmap_loc_set(fs, i, 0);
828*6a54128fSAndroid Build Coastguard Worker 		}
829*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_block_bitmap_loc(fs, i) == 0) {
830*6a54128fSAndroid Build Coastguard Worker 			ctx->invalid_block_bitmap_flag[i]++;
831*6a54128fSAndroid Build Coastguard Worker 			ctx->invalid_bitmaps++;
832*6a54128fSAndroid Build Coastguard Worker 		}
833*6a54128fSAndroid Build Coastguard Worker 		if ((ext2fs_inode_bitmap_loc(fs, i) < first_block) ||
834*6a54128fSAndroid Build Coastguard Worker 		    (ext2fs_inode_bitmap_loc(fs, i) > last_block)) {
835*6a54128fSAndroid Build Coastguard Worker 			pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
836*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx))
837*6a54128fSAndroid Build Coastguard Worker 				ext2fs_inode_bitmap_loc_set(fs, i, 0);
838*6a54128fSAndroid Build Coastguard Worker 		}
839*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_inode_bitmap_loc(fs, i) == 0) {
840*6a54128fSAndroid Build Coastguard Worker 			ctx->invalid_inode_bitmap_flag[i]++;
841*6a54128fSAndroid Build Coastguard Worker 			ctx->invalid_bitmaps++;
842*6a54128fSAndroid Build Coastguard Worker 		}
843*6a54128fSAndroid Build Coastguard Worker 		if ((ext2fs_inode_table_loc(fs, i) < first_block) ||
844*6a54128fSAndroid Build Coastguard Worker 		    ((ext2fs_inode_table_loc(fs, i) +
845*6a54128fSAndroid Build Coastguard Worker 		      fs->inode_blocks_per_group - 1) > last_block)) {
846*6a54128fSAndroid Build Coastguard Worker 			pctx.blk = ext2fs_inode_table_loc(fs, i);
847*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx))
848*6a54128fSAndroid Build Coastguard Worker 				ext2fs_inode_table_loc_set(fs, i, 0);
849*6a54128fSAndroid Build Coastguard Worker 		}
850*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_inode_table_loc(fs, i) == 0) {
851*6a54128fSAndroid Build Coastguard Worker 			ctx->invalid_inode_table_flag[i]++;
852*6a54128fSAndroid Build Coastguard Worker 			ctx->invalid_bitmaps++;
853*6a54128fSAndroid Build Coastguard Worker 		}
854*6a54128fSAndroid Build Coastguard Worker 		free_blocks += ext2fs_bg_free_blocks_count(fs, i);
855*6a54128fSAndroid Build Coastguard Worker 		free_inodes += ext2fs_bg_free_inodes_count(fs, i);
856*6a54128fSAndroid Build Coastguard Worker 
857*6a54128fSAndroid Build Coastguard Worker 		if ((ext2fs_bg_free_blocks_count(fs, i) > sb->s_blocks_per_group) ||
858*6a54128fSAndroid Build Coastguard Worker 		    (ext2fs_bg_free_inodes_count(fs, i) > sb->s_inodes_per_group) ||
859*6a54128fSAndroid Build Coastguard Worker 		    (ext2fs_bg_used_dirs_count(fs, i) > sb->s_inodes_per_group))
860*6a54128fSAndroid Build Coastguard Worker 			ext2fs_unmark_valid(fs);
861*6a54128fSAndroid Build Coastguard Worker 
862*6a54128fSAndroid Build Coastguard Worker 		should_be = 0;
863*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_group_desc_csum_verify(fs, i)) {
864*6a54128fSAndroid Build Coastguard Worker 			pctx.csum1 = ext2fs_bg_checksum(fs, i);
865*6a54128fSAndroid Build Coastguard Worker 			pctx.csum2 = ext2fs_group_desc_csum(fs, i);
866*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) {
867*6a54128fSAndroid Build Coastguard Worker 				ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
868*6a54128fSAndroid Build Coastguard Worker 				ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
869*6a54128fSAndroid Build Coastguard Worker 				ext2fs_bg_itable_unused_set(fs, i, 0);
870*6a54128fSAndroid Build Coastguard Worker 				should_be = 1;
871*6a54128fSAndroid Build Coastguard Worker 			}
872*6a54128fSAndroid Build Coastguard Worker 			ext2fs_unmark_valid(fs);
873*6a54128fSAndroid Build Coastguard Worker 		}
874*6a54128fSAndroid Build Coastguard Worker 
875*6a54128fSAndroid Build Coastguard Worker 		if (!csum_flag &&
876*6a54128fSAndroid Build Coastguard Worker 		    (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) ||
877*6a54128fSAndroid Build Coastguard Worker 		     ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) ||
878*6a54128fSAndroid Build Coastguard Worker 		     ext2fs_bg_itable_unused(fs, i) != 0)) {
879*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) {
880*6a54128fSAndroid Build Coastguard Worker 				ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
881*6a54128fSAndroid Build Coastguard Worker 				ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
882*6a54128fSAndroid Build Coastguard Worker 				ext2fs_bg_itable_unused_set(fs, i, 0);
883*6a54128fSAndroid Build Coastguard Worker 				should_be = 1;
884*6a54128fSAndroid Build Coastguard Worker 			}
885*6a54128fSAndroid Build Coastguard Worker 			ext2fs_unmark_valid(fs);
886*6a54128fSAndroid Build Coastguard Worker 		}
887*6a54128fSAndroid Build Coastguard Worker 
888*6a54128fSAndroid Build Coastguard Worker 		if (i == fs->group_desc_count - 1 &&
889*6a54128fSAndroid Build Coastguard Worker 		    ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) {
890*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) {
891*6a54128fSAndroid Build Coastguard Worker 				ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT);
892*6a54128fSAndroid Build Coastguard Worker 				should_be = 1;
893*6a54128fSAndroid Build Coastguard Worker 			}
894*6a54128fSAndroid Build Coastguard Worker 			ext2fs_unmark_valid(fs);
895*6a54128fSAndroid Build Coastguard Worker 		}
896*6a54128fSAndroid Build Coastguard Worker 
897*6a54128fSAndroid Build Coastguard Worker 		if (csum_flag &&
898*6a54128fSAndroid Build Coastguard Worker 		    (ext2fs_bg_itable_unused(fs, i) > ext2fs_bg_free_inodes_count(fs, i) ||
899*6a54128fSAndroid Build Coastguard Worker 		     ext2fs_bg_itable_unused(fs, i) > sb->s_inodes_per_group)) {
900*6a54128fSAndroid Build Coastguard Worker 			pctx.blk = ext2fs_bg_itable_unused(fs, i);
901*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) {
902*6a54128fSAndroid Build Coastguard Worker 				ext2fs_bg_itable_unused_set(fs, i, 0);
903*6a54128fSAndroid Build Coastguard Worker 				should_be = 1;
904*6a54128fSAndroid Build Coastguard Worker 			}
905*6a54128fSAndroid Build Coastguard Worker 			ext2fs_unmark_valid(fs);
906*6a54128fSAndroid Build Coastguard Worker 		}
907*6a54128fSAndroid Build Coastguard Worker 
908*6a54128fSAndroid Build Coastguard Worker 		if (should_be)
909*6a54128fSAndroid Build Coastguard Worker 			ext2fs_group_desc_csum_set(fs, i);
910*6a54128fSAndroid Build Coastguard Worker 		/* If the user aborts e2fsck by typing ^C, stop right away */
911*6a54128fSAndroid Build Coastguard Worker 		if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
912*6a54128fSAndroid Build Coastguard Worker 			return;
913*6a54128fSAndroid Build Coastguard Worker 	}
914*6a54128fSAndroid Build Coastguard Worker 
915*6a54128fSAndroid Build Coastguard Worker 	ctx->free_blocks = EXT2FS_C2B(fs, free_blocks);
916*6a54128fSAndroid Build Coastguard Worker 	ctx->free_inodes = free_inodes;
917*6a54128fSAndroid Build Coastguard Worker 
918*6a54128fSAndroid Build Coastguard Worker 	if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) ||
919*6a54128fSAndroid Build Coastguard Worker 	    (sb->s_free_inodes_count > sb->s_inodes_count))
920*6a54128fSAndroid Build Coastguard Worker 		ext2fs_unmark_valid(fs);
921*6a54128fSAndroid Build Coastguard Worker 
922*6a54128fSAndroid Build Coastguard Worker 
923*6a54128fSAndroid Build Coastguard Worker 	/*
924*6a54128fSAndroid Build Coastguard Worker 	 * If we have invalid bitmaps, set the error state of the
925*6a54128fSAndroid Build Coastguard Worker 	 * filesystem.
926*6a54128fSAndroid Build Coastguard Worker 	 */
927*6a54128fSAndroid Build Coastguard Worker 	if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) {
928*6a54128fSAndroid Build Coastguard Worker 		sb->s_state &= ~EXT2_VALID_FS;
929*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
930*6a54128fSAndroid Build Coastguard Worker 	}
931*6a54128fSAndroid Build Coastguard Worker 
932*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
933*6a54128fSAndroid Build Coastguard Worker 
934*6a54128fSAndroid Build Coastguard Worker #ifndef EXT2_SKIP_UUID
935*6a54128fSAndroid Build Coastguard Worker 	/*
936*6a54128fSAndroid Build Coastguard Worker 	 * If the UUID field isn't assigned, assign it.
937*6a54128fSAndroid Build Coastguard Worker 	 * Skip if checksums are enabled and the filesystem is mounted,
938*6a54128fSAndroid Build Coastguard Worker 	 * if the id changes under the kernel remounting rw may fail.
939*6a54128fSAndroid Build Coastguard Worker 	 */
940*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid) &&
941*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_has_feature_metadata_csum(ctx->fs->super) &&
942*6a54128fSAndroid Build Coastguard Worker 	    (!csum_flag || !(ctx->mount_flags & EXT2_MF_MOUNTED))) {
943*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) {
944*6a54128fSAndroid Build Coastguard Worker 			uuid_generate(sb->s_uuid);
945*6a54128fSAndroid Build Coastguard Worker 			ext2fs_init_csum_seed(fs);
946*6a54128fSAndroid Build Coastguard Worker 			fs->flags |= EXT2_FLAG_DIRTY;
947*6a54128fSAndroid Build Coastguard Worker 			fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
948*6a54128fSAndroid Build Coastguard Worker 		}
949*6a54128fSAndroid Build Coastguard Worker 	}
950*6a54128fSAndroid Build Coastguard Worker #endif
951*6a54128fSAndroid Build Coastguard Worker 
952*6a54128fSAndroid Build Coastguard Worker 	/*
953*6a54128fSAndroid Build Coastguard Worker 	 * Check to see if we should disable the test_fs flag
954*6a54128fSAndroid Build Coastguard Worker 	 */
955*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options",
956*6a54128fSAndroid Build Coastguard Worker 			    "clear_test_fs_flag", 0, 1,
957*6a54128fSAndroid Build Coastguard Worker 			    &clear_test_fs_flag);
958*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->options & E2F_OPT_READONLY) &&
959*6a54128fSAndroid Build Coastguard Worker 	    clear_test_fs_flag &&
960*6a54128fSAndroid Build Coastguard Worker 	    (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) &&
961*6a54128fSAndroid Build Coastguard Worker 	    (fs_proc_check("ext4") || check_for_modules("ext4"))) {
962*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) {
963*6a54128fSAndroid Build Coastguard Worker 			fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS;
964*6a54128fSAndroid Build Coastguard Worker 			fs->flags |= EXT2_FLAG_DIRTY;
965*6a54128fSAndroid Build Coastguard Worker 			fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
966*6a54128fSAndroid Build Coastguard Worker 		}
967*6a54128fSAndroid Build Coastguard Worker 	}
968*6a54128fSAndroid Build Coastguard Worker 
969*6a54128fSAndroid Build Coastguard Worker 	/*
970*6a54128fSAndroid Build Coastguard Worker 	 * For the Hurd, check to see if the filetype option is set,
971*6a54128fSAndroid Build Coastguard Worker 	 * since it doesn't support it.
972*6a54128fSAndroid Build Coastguard Worker 	 */
973*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->options & E2F_OPT_READONLY) &&
974*6a54128fSAndroid Build Coastguard Worker 	    fs->super->s_creator_os == EXT2_OS_HURD &&
975*6a54128fSAndroid Build Coastguard Worker 	    ext2fs_has_feature_filetype(fs->super)) {
976*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) {
977*6a54128fSAndroid Build Coastguard Worker 			ext2fs_clear_feature_filetype(fs->super);
978*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
979*6a54128fSAndroid Build Coastguard Worker 			fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
980*6a54128fSAndroid Build Coastguard Worker 		}
981*6a54128fSAndroid Build Coastguard Worker 	}
982*6a54128fSAndroid Build Coastguard Worker 
983*6a54128fSAndroid Build Coastguard Worker 	/*
984*6a54128fSAndroid Build Coastguard Worker 	 * If we have any of the compatibility flags set, we need to have a
985*6a54128fSAndroid Build Coastguard Worker 	 * revision 1 filesystem.  Most kernels will not check the flags on
986*6a54128fSAndroid Build Coastguard Worker 	 * a rev 0 filesystem and we may have corruption issues because of
987*6a54128fSAndroid Build Coastguard Worker 	 * the incompatible changes to the filesystem.
988*6a54128fSAndroid Build Coastguard Worker 	 */
989*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->options & E2F_OPT_READONLY) &&
990*6a54128fSAndroid Build Coastguard Worker 	    fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
991*6a54128fSAndroid Build Coastguard Worker 	    (fs->super->s_feature_compat ||
992*6a54128fSAndroid Build Coastguard Worker 	     fs->super->s_feature_ro_compat ||
993*6a54128fSAndroid Build Coastguard Worker 	     fs->super->s_feature_incompat) &&
994*6a54128fSAndroid Build Coastguard Worker 	    fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) {
995*6a54128fSAndroid Build Coastguard Worker 		ext2fs_update_dynamic_rev(fs);
996*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
997*6a54128fSAndroid Build Coastguard Worker 		fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
998*6a54128fSAndroid Build Coastguard Worker 	}
999*6a54128fSAndroid Build Coastguard Worker 
1000*6a54128fSAndroid Build Coastguard Worker 	/*
1001*6a54128fSAndroid Build Coastguard Worker 	 * Clean up any orphan inodes, if present.
1002*6a54128fSAndroid Build Coastguard Worker 	 */
1003*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) {
1004*6a54128fSAndroid Build Coastguard Worker 		fs->super->s_state &= ~EXT2_VALID_FS;
1005*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_super_dirty(fs);
1006*6a54128fSAndroid Build Coastguard Worker 	}
1007*6a54128fSAndroid Build Coastguard Worker 
1008*6a54128fSAndroid Build Coastguard Worker 	/*
1009*6a54128fSAndroid Build Coastguard Worker 	 * Unfortunately, due to Windows' unfortunate design decision
1010*6a54128fSAndroid Build Coastguard Worker 	 * to configure the hardware clock to tick localtime, instead
1011*6a54128fSAndroid Build Coastguard Worker 	 * of the more proper and less error-prone UTC time, many
1012*6a54128fSAndroid Build Coastguard Worker 	 * users end up in the situation where the system clock is
1013*6a54128fSAndroid Build Coastguard Worker 	 * incorrectly set at the time when e2fsck is run.
1014*6a54128fSAndroid Build Coastguard Worker 	 *
1015*6a54128fSAndroid Build Coastguard Worker 	 * Historically this was usually due to some distributions
1016*6a54128fSAndroid Build Coastguard Worker 	 * having buggy init scripts and/or installers that didn't
1017*6a54128fSAndroid Build Coastguard Worker 	 * correctly detect this case and take appropriate
1018*6a54128fSAndroid Build Coastguard Worker 	 * countermeasures.  However, it's still possible, despite the
1019*6a54128fSAndroid Build Coastguard Worker 	 * best efforts of init script and installer authors to not be
1020*6a54128fSAndroid Build Coastguard Worker 	 * able to detect this misconfiguration, usually due to a
1021*6a54128fSAndroid Build Coastguard Worker 	 * buggy or misconfigured virtualization manager or the
1022*6a54128fSAndroid Build Coastguard Worker 	 * installer not having access to a network time server during
1023*6a54128fSAndroid Build Coastguard Worker 	 * the installation process.  So by default, we allow the
1024*6a54128fSAndroid Build Coastguard Worker 	 * superblock times to be fudged by up to 24 hours.  This can
1025*6a54128fSAndroid Build Coastguard Worker 	 * be disabled by setting options.accept_time_fudge to the
1026*6a54128fSAndroid Build Coastguard Worker 	 * boolean value of false in e2fsck.conf.  We also support
1027*6a54128fSAndroid Build Coastguard Worker 	 * options.buggy_init_scripts for backwards compatibility.
1028*6a54128fSAndroid Build Coastguard Worker 	 */
1029*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options", "accept_time_fudge",
1030*6a54128fSAndroid Build Coastguard Worker 			    0, 1, &accept_time_fudge);
1031*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options", "buggy_init_scripts",
1032*6a54128fSAndroid Build Coastguard Worker 			    0, accept_time_fudge, &accept_time_fudge);
1033*6a54128fSAndroid Build Coastguard Worker 	ctx->time_fudge = accept_time_fudge ? 86400 : 0;
1034*6a54128fSAndroid Build Coastguard Worker 
1035*6a54128fSAndroid Build Coastguard Worker 	profile_get_boolean(ctx->profile, "options", "broken_system_clock",
1036*6a54128fSAndroid Build Coastguard Worker 			    0, 0, &broken_system_clock);
1037*6a54128fSAndroid Build Coastguard Worker 
1038*6a54128fSAndroid Build Coastguard Worker 	/*
1039*6a54128fSAndroid Build Coastguard Worker 	 * Check to see if the superblock last mount time or last
1040*6a54128fSAndroid Build Coastguard Worker 	 * write time is in the future.
1041*6a54128fSAndroid Build Coastguard Worker 	 */
1042*6a54128fSAndroid Build Coastguard Worker 	if (((ctx->options & E2F_OPT_FORCE) || fs->super->s_checkinterval) &&
1043*6a54128fSAndroid Build Coastguard Worker 	    !broken_system_clock && !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
1044*6a54128fSAndroid Build Coastguard Worker 	    (fs->super->s_mtime > (__u32) ctx->now)) {
1045*6a54128fSAndroid Build Coastguard Worker 		pctx.num = fs->super->s_mtime;
1046*6a54128fSAndroid Build Coastguard Worker 		problem = PR_0_FUTURE_SB_LAST_MOUNT;
1047*6a54128fSAndroid Build Coastguard Worker 		if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge)
1048*6a54128fSAndroid Build Coastguard Worker 			problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED;
1049*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, problem, &pctx)) {
1050*6a54128fSAndroid Build Coastguard Worker 			fs->super->s_mtime = ctx->now;
1051*6a54128fSAndroid Build Coastguard Worker 			fs->flags |= EXT2_FLAG_DIRTY;
1052*6a54128fSAndroid Build Coastguard Worker 		}
1053*6a54128fSAndroid Build Coastguard Worker 	}
1054*6a54128fSAndroid Build Coastguard Worker 	if (((ctx->options & E2F_OPT_FORCE) || fs->super->s_checkinterval) &&
1055*6a54128fSAndroid Build Coastguard Worker 	    !broken_system_clock && !(ctx->flags & E2F_FLAG_TIME_INSANE) &&
1056*6a54128fSAndroid Build Coastguard Worker 	    (fs->super->s_wtime > (__u32) ctx->now)) {
1057*6a54128fSAndroid Build Coastguard Worker 		pctx.num = fs->super->s_wtime;
1058*6a54128fSAndroid Build Coastguard Worker 		problem = PR_0_FUTURE_SB_LAST_WRITE;
1059*6a54128fSAndroid Build Coastguard Worker 		if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge)
1060*6a54128fSAndroid Build Coastguard Worker 			problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED;
1061*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, problem, &pctx)) {
1062*6a54128fSAndroid Build Coastguard Worker 			fs->super->s_wtime = ctx->now;
1063*6a54128fSAndroid Build Coastguard Worker 			fs->flags |= EXT2_FLAG_DIRTY;
1064*6a54128fSAndroid Build Coastguard Worker 		}
1065*6a54128fSAndroid Build Coastguard Worker 	}
1066*6a54128fSAndroid Build Coastguard Worker 
1067*6a54128fSAndroid Build Coastguard Worker 	e2fsck_validate_quota_inodes(ctx);
1068*6a54128fSAndroid Build Coastguard Worker 
1069*6a54128fSAndroid Build Coastguard Worker 	/*
1070*6a54128fSAndroid Build Coastguard Worker 	 * Move the ext3 journal file, if necessary.
1071*6a54128fSAndroid Build Coastguard Worker 	 */
1072*6a54128fSAndroid Build Coastguard Worker 	e2fsck_move_ext3_journal(ctx);
1073*6a54128fSAndroid Build Coastguard Worker 
1074*6a54128fSAndroid Build Coastguard Worker 	/*
1075*6a54128fSAndroid Build Coastguard Worker 	 * Fix journal hint, if necessary
1076*6a54128fSAndroid Build Coastguard Worker 	 */
1077*6a54128fSAndroid Build Coastguard Worker 	e2fsck_fix_ext3_journal_hint(ctx);
1078*6a54128fSAndroid Build Coastguard Worker 
1079*6a54128fSAndroid Build Coastguard Worker 	/*
1080*6a54128fSAndroid Build Coastguard Worker 	 * Add dirhash hint if necessary
1081*6a54128fSAndroid Build Coastguard Worker 	 */
1082*6a54128fSAndroid Build Coastguard Worker 	e2fsck_fix_dirhash_hint(ctx);
1083*6a54128fSAndroid Build Coastguard Worker 
1084*6a54128fSAndroid Build Coastguard Worker 	/*
1085*6a54128fSAndroid Build Coastguard Worker 	 * Hide quota inodes if necessary.
1086*6a54128fSAndroid Build Coastguard Worker 	 */
1087*6a54128fSAndroid Build Coastguard Worker 	e2fsck_hide_quota(ctx);
1088*6a54128fSAndroid Build Coastguard Worker 
1089*6a54128fSAndroid Build Coastguard Worker 	return;
1090*6a54128fSAndroid Build Coastguard Worker }
1091*6a54128fSAndroid Build Coastguard Worker 
1092*6a54128fSAndroid Build Coastguard Worker /*
1093*6a54128fSAndroid Build Coastguard Worker  * Check to see if we should backup the master sb to the backup super
1094*6a54128fSAndroid Build Coastguard Worker  * blocks.  Returns non-zero if the sb should be backed up.
1095*6a54128fSAndroid Build Coastguard Worker  */
1096*6a54128fSAndroid Build Coastguard Worker 
1097*6a54128fSAndroid Build Coastguard Worker /*
1098*6a54128fSAndroid Build Coastguard Worker  * A few flags are set on the fly by the kernel, but only in the
1099*6a54128fSAndroid Build Coastguard Worker  * primary superblock.  This is actually a bad thing, and we should
1100*6a54128fSAndroid Build Coastguard Worker  * try to discourage it in the future.  In particular, for the newer
1101*6a54128fSAndroid Build Coastguard Worker  * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and
1102*6a54128fSAndroid Build Coastguard Worker  * EXT3_FEATURE_INCOMPAT_EXTENTS.  So some of these may go away in the
1103*6a54128fSAndroid Build Coastguard Worker  * future.  EXT3_FEATURE_INCOMPAT_RECOVER may also get set when
1104*6a54128fSAndroid Build Coastguard Worker  * copying the primary superblock during online resize.
1105*6a54128fSAndroid Build Coastguard Worker  *
1106*6a54128fSAndroid Build Coastguard Worker  * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but
1107*6a54128fSAndroid Build Coastguard Worker  * unfortunately, we shouldn't ignore it since if it's not set in the
1108*6a54128fSAndroid Build Coastguard Worker  * backup, the extended attributes in the filesystem will be stripped
1109*6a54128fSAndroid Build Coastguard Worker  * away.
1110*6a54128fSAndroid Build Coastguard Worker  */
1111*6a54128fSAndroid Build Coastguard Worker #define FEATURE_RO_COMPAT_IGNORE	(EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \
1112*6a54128fSAndroid Build Coastguard Worker 					 EXT4_FEATURE_RO_COMPAT_DIR_NLINK)
1113*6a54128fSAndroid Build Coastguard Worker #define FEATURE_INCOMPAT_IGNORE		(EXT3_FEATURE_INCOMPAT_EXTENTS| \
1114*6a54128fSAndroid Build Coastguard Worker 					 EXT3_FEATURE_INCOMPAT_RECOVER)
1115*6a54128fSAndroid Build Coastguard Worker 
check_backup_super_block(e2fsck_t ctx)1116*6a54128fSAndroid Build Coastguard Worker int check_backup_super_block(e2fsck_t ctx)
1117*6a54128fSAndroid Build Coastguard Worker {
1118*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys	fs = ctx->fs;
1119*6a54128fSAndroid Build Coastguard Worker 	errcode_t	retval;
1120*6a54128fSAndroid Build Coastguard Worker 	dgrp_t		g;
1121*6a54128fSAndroid Build Coastguard Worker 	blk64_t		sb;
1122*6a54128fSAndroid Build Coastguard Worker 	int		ret = 0;
1123*6a54128fSAndroid Build Coastguard Worker 	char		buf[SUPERBLOCK_SIZE];
1124*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block	*backup_sb;
1125*6a54128fSAndroid Build Coastguard Worker 
1126*6a54128fSAndroid Build Coastguard Worker 	/*
1127*6a54128fSAndroid Build Coastguard Worker 	 * If we are already writing out the backup blocks, then we
1128*6a54128fSAndroid Build Coastguard Worker 	 * don't need to test.  Also, if the filesystem is invalid, or
1129*6a54128fSAndroid Build Coastguard Worker 	 * the check was aborted or cancelled, we also don't want to
1130*6a54128fSAndroid Build Coastguard Worker 	 * do the backup.  If the filesystem was opened read-only then
1131*6a54128fSAndroid Build Coastguard Worker 	 * we can't do the backup.
1132*6a54128fSAndroid Build Coastguard Worker 	 */
1133*6a54128fSAndroid Build Coastguard Worker 	if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) ||
1134*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_test_valid(fs) ||
1135*6a54128fSAndroid Build Coastguard Worker 	    (fs->super->s_state & EXT2_ERROR_FS) ||
1136*6a54128fSAndroid Build Coastguard Worker 	    (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) ||
1137*6a54128fSAndroid Build Coastguard Worker 	    (ctx->options & E2F_OPT_READONLY))
1138*6a54128fSAndroid Build Coastguard Worker 		return 0;
1139*6a54128fSAndroid Build Coastguard Worker 
1140*6a54128fSAndroid Build Coastguard Worker 	for (g = 1; g < fs->group_desc_count; g++) {
1141*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_bg_has_super(fs, g))
1142*6a54128fSAndroid Build Coastguard Worker 			continue;
1143*6a54128fSAndroid Build Coastguard Worker 
1144*6a54128fSAndroid Build Coastguard Worker 		sb = ext2fs_group_first_block2(fs, g);
1145*6a54128fSAndroid Build Coastguard Worker 
1146*6a54128fSAndroid Build Coastguard Worker 		retval = io_channel_read_blk(fs->io, sb, -SUPERBLOCK_SIZE,
1147*6a54128fSAndroid Build Coastguard Worker 					     buf);
1148*6a54128fSAndroid Build Coastguard Worker 		if (retval)
1149*6a54128fSAndroid Build Coastguard Worker 			continue;
1150*6a54128fSAndroid Build Coastguard Worker 		backup_sb = (struct ext2_super_block *) buf;
1151*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
1152*6a54128fSAndroid Build Coastguard Worker 		ext2fs_swap_super(backup_sb);
1153*6a54128fSAndroid Build Coastguard Worker #endif
1154*6a54128fSAndroid Build Coastguard Worker 		if ((backup_sb->s_magic != EXT2_SUPER_MAGIC) ||
1155*6a54128fSAndroid Build Coastguard Worker 		    (backup_sb->s_rev_level > EXT2_LIB_CURRENT_REV) ||
1156*6a54128fSAndroid Build Coastguard Worker 		    ((backup_sb->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) >
1157*6a54128fSAndroid Build Coastguard Worker 		     EXT2_MAX_BLOCK_LOG_SIZE) ||
1158*6a54128fSAndroid Build Coastguard Worker 		    (EXT2_INODE_SIZE(backup_sb) < EXT2_GOOD_OLD_INODE_SIZE))
1159*6a54128fSAndroid Build Coastguard Worker 			continue;
1160*6a54128fSAndroid Build Coastguard Worker 
1161*6a54128fSAndroid Build Coastguard Worker #define SUPER_INCOMPAT_DIFFERENT(x)	\
1162*6a54128fSAndroid Build Coastguard Worker 	((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) !=	\
1163*6a54128fSAndroid Build Coastguard Worker 	 (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE))
1164*6a54128fSAndroid Build Coastguard Worker #define SUPER_RO_COMPAT_DIFFERENT(x)	\
1165*6a54128fSAndroid Build Coastguard Worker 	((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) !=	\
1166*6a54128fSAndroid Build Coastguard Worker 	 (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE))
1167*6a54128fSAndroid Build Coastguard Worker #define SUPER_DIFFERENT(x)		\
1168*6a54128fSAndroid Build Coastguard Worker 	(fs->super->x != backup_sb->x)
1169*6a54128fSAndroid Build Coastguard Worker 
1170*6a54128fSAndroid Build Coastguard Worker 		if (SUPER_DIFFERENT(s_feature_compat) ||
1171*6a54128fSAndroid Build Coastguard Worker 		    SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) ||
1172*6a54128fSAndroid Build Coastguard Worker 		    SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) ||
1173*6a54128fSAndroid Build Coastguard Worker 		    SUPER_DIFFERENT(s_blocks_count) ||
1174*6a54128fSAndroid Build Coastguard Worker 		    SUPER_DIFFERENT(s_blocks_count_hi) ||
1175*6a54128fSAndroid Build Coastguard Worker 		    SUPER_DIFFERENT(s_inodes_count) ||
1176*6a54128fSAndroid Build Coastguard Worker 		    memcmp(fs->super->s_uuid, backup_sb->s_uuid,
1177*6a54128fSAndroid Build Coastguard Worker 			   sizeof(fs->super->s_uuid)))
1178*6a54128fSAndroid Build Coastguard Worker 			ret = 1;
1179*6a54128fSAndroid Build Coastguard Worker 		break;
1180*6a54128fSAndroid Build Coastguard Worker 	}
1181*6a54128fSAndroid Build Coastguard Worker 	return ret;
1182*6a54128fSAndroid Build Coastguard Worker }
1183