xref: /aosp_15_r20/external/e2fsprogs/e2fsck/pass2.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * pass2.c --- check directory structure
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  * Pass 2 of e2fsck iterates through all active directory inodes, and
12*6a54128fSAndroid Build Coastguard Worker  * applies to following tests to each directory entry in the directory
13*6a54128fSAndroid Build Coastguard Worker  * blocks in the inodes:
14*6a54128fSAndroid Build Coastguard Worker  *
15*6a54128fSAndroid Build Coastguard Worker  *	- The length of the directory entry (rec_len) should be at
16*6a54128fSAndroid Build Coastguard Worker  * 		least 8 bytes, and no more than the remaining space
17*6a54128fSAndroid Build Coastguard Worker  * 		left in the directory block.
18*6a54128fSAndroid Build Coastguard Worker  * 	- The length of the name in the directory entry (name_len)
19*6a54128fSAndroid Build Coastguard Worker  * 		should be less than (rec_len - 8).
20*6a54128fSAndroid Build Coastguard Worker  *	- The inode number in the directory entry should be within
21*6a54128fSAndroid Build Coastguard Worker  * 		legal bounds.
22*6a54128fSAndroid Build Coastguard Worker  * 	- The inode number should refer to a in-use inode.
23*6a54128fSAndroid Build Coastguard Worker  *	- The first entry should be '.', and its inode should be
24*6a54128fSAndroid Build Coastguard Worker  * 		the inode of the directory.
25*6a54128fSAndroid Build Coastguard Worker  * 	- The second entry should be '..'.
26*6a54128fSAndroid Build Coastguard Worker  *
27*6a54128fSAndroid Build Coastguard Worker  * To minimize disk seek time, the directory blocks are processed in
28*6a54128fSAndroid Build Coastguard Worker  * sorted order of block numbers.
29*6a54128fSAndroid Build Coastguard Worker  *
30*6a54128fSAndroid Build Coastguard Worker  * Pass 2 also collects the following information:
31*6a54128fSAndroid Build Coastguard Worker  * 	- The inode numbers of the subdirectories for each directory.
32*6a54128fSAndroid Build Coastguard Worker  *
33*6a54128fSAndroid Build Coastguard Worker  * Pass 2 relies on the following information from previous passes:
34*6a54128fSAndroid Build Coastguard Worker  * 	- The directory information collected in pass 1.
35*6a54128fSAndroid Build Coastguard Worker  * 	- The inode_used_map bitmap
36*6a54128fSAndroid Build Coastguard Worker  * 	- The inode_bad_map bitmap
37*6a54128fSAndroid Build Coastguard Worker  * 	- The inode_dir_map bitmap
38*6a54128fSAndroid Build Coastguard Worker  * 	- The encrypted_file_info
39*6a54128fSAndroid Build Coastguard Worker  *	- The inode_casefold_map bitmap
40*6a54128fSAndroid Build Coastguard Worker  *
41*6a54128fSAndroid Build Coastguard Worker  * Pass 2 frees the following data structures
42*6a54128fSAndroid Build Coastguard Worker  * 	- The inode_bad_map bitmap
43*6a54128fSAndroid Build Coastguard Worker  * 	- The inode_reg_map bitmap
44*6a54128fSAndroid Build Coastguard Worker  * 	- The encrypted_file_info
45*6a54128fSAndroid Build Coastguard Worker  *	- The inode_casefold_map bitmap
46*6a54128fSAndroid Build Coastguard Worker  */
47*6a54128fSAndroid Build Coastguard Worker 
48*6a54128fSAndroid Build Coastguard Worker #define _GNU_SOURCE 1 /* get strnlen() */
49*6a54128fSAndroid Build Coastguard Worker #include "config.h"
50*6a54128fSAndroid Build Coastguard Worker #include <string.h>
51*6a54128fSAndroid Build Coastguard Worker 
52*6a54128fSAndroid Build Coastguard Worker #include "e2fsck.h"
53*6a54128fSAndroid Build Coastguard Worker #include "problem.h"
54*6a54128fSAndroid Build Coastguard Worker #include "support/dict.h"
55*6a54128fSAndroid Build Coastguard Worker 
56*6a54128fSAndroid Build Coastguard Worker #ifdef NO_INLINE_FUNCS
57*6a54128fSAndroid Build Coastguard Worker #define _INLINE_
58*6a54128fSAndroid Build Coastguard Worker #else
59*6a54128fSAndroid Build Coastguard Worker #define _INLINE_ inline
60*6a54128fSAndroid Build Coastguard Worker #endif
61*6a54128fSAndroid Build Coastguard Worker 
62*6a54128fSAndroid Build Coastguard Worker /* #define DX_DEBUG */
63*6a54128fSAndroid Build Coastguard Worker 
64*6a54128fSAndroid Build Coastguard Worker /*
65*6a54128fSAndroid Build Coastguard Worker  * Keeps track of how many times an inode is referenced.
66*6a54128fSAndroid Build Coastguard Worker  */
67*6a54128fSAndroid Build Coastguard Worker static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf);
68*6a54128fSAndroid Build Coastguard Worker static int check_dir_block2(ext2_filsys fs,
69*6a54128fSAndroid Build Coastguard Worker 			   struct ext2_db_entry2 *dir_blocks_info,
70*6a54128fSAndroid Build Coastguard Worker 			   void *priv_data);
71*6a54128fSAndroid Build Coastguard Worker static int check_dir_block(ext2_filsys fs,
72*6a54128fSAndroid Build Coastguard Worker 			   struct ext2_db_entry2 *dir_blocks_info,
73*6a54128fSAndroid Build Coastguard Worker 			   void *priv_data);
74*6a54128fSAndroid Build Coastguard Worker static int allocate_dir_block(e2fsck_t ctx,
75*6a54128fSAndroid Build Coastguard Worker 			      struct ext2_db_entry2 *dir_blocks_info,
76*6a54128fSAndroid Build Coastguard Worker 			      char *buf, struct problem_context *pctx);
77*6a54128fSAndroid Build Coastguard Worker static void clear_htree(e2fsck_t ctx, ext2_ino_t ino);
78*6a54128fSAndroid Build Coastguard Worker static short htree_depth(struct dx_dir_info *dx_dir,
79*6a54128fSAndroid Build Coastguard Worker 			 struct dx_dirblock_info *dx_db);
80*6a54128fSAndroid Build Coastguard Worker static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b);
81*6a54128fSAndroid Build Coastguard Worker 
82*6a54128fSAndroid Build Coastguard Worker struct check_dir_struct {
83*6a54128fSAndroid Build Coastguard Worker 	char *buf;
84*6a54128fSAndroid Build Coastguard Worker 	struct problem_context	pctx;
85*6a54128fSAndroid Build Coastguard Worker 	int	count, max;
86*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t ctx;
87*6a54128fSAndroid Build Coastguard Worker 	unsigned long long list_offset;
88*6a54128fSAndroid Build Coastguard Worker 	unsigned long long ra_entries;
89*6a54128fSAndroid Build Coastguard Worker 	unsigned long long next_ra_off;
90*6a54128fSAndroid Build Coastguard Worker };
91*6a54128fSAndroid Build Coastguard Worker 
update_parents(struct dx_dir_info * dx_dir,int type)92*6a54128fSAndroid Build Coastguard Worker static void update_parents(struct dx_dir_info *dx_dir, int type)
93*6a54128fSAndroid Build Coastguard Worker {
94*6a54128fSAndroid Build Coastguard Worker 	struct dx_dirblock_info *dx_db, *dx_parent, *dx_previous;
95*6a54128fSAndroid Build Coastguard Worker 	blk_t b;
96*6a54128fSAndroid Build Coastguard Worker 
97*6a54128fSAndroid Build Coastguard Worker 	for (b = 0, dx_db = dx_dir->dx_block;
98*6a54128fSAndroid Build Coastguard Worker 	     b < dx_dir->numblocks;
99*6a54128fSAndroid Build Coastguard Worker 	     b++, dx_db++) {
100*6a54128fSAndroid Build Coastguard Worker 		dx_parent = &dx_dir->dx_block[dx_db->parent];
101*6a54128fSAndroid Build Coastguard Worker 		if (dx_db->type != type)
102*6a54128fSAndroid Build Coastguard Worker 			continue;
103*6a54128fSAndroid Build Coastguard Worker 
104*6a54128fSAndroid Build Coastguard Worker 		/*
105*6a54128fSAndroid Build Coastguard Worker 		 * XXX Make sure dx_parent->min_hash > dx_db->min_hash
106*6a54128fSAndroid Build Coastguard Worker 		*/
107*6a54128fSAndroid Build Coastguard Worker 		if (dx_db->flags & DX_FLAG_FIRST) {
108*6a54128fSAndroid Build Coastguard Worker 			dx_parent->min_hash = dx_db->min_hash;
109*6a54128fSAndroid Build Coastguard Worker 			if (dx_parent->previous) {
110*6a54128fSAndroid Build Coastguard Worker 				dx_previous =
111*6a54128fSAndroid Build Coastguard Worker 					&dx_dir->dx_block[dx_parent->previous];
112*6a54128fSAndroid Build Coastguard Worker 				dx_previous->node_max_hash =
113*6a54128fSAndroid Build Coastguard Worker 					dx_parent->min_hash;
114*6a54128fSAndroid Build Coastguard Worker 			}
115*6a54128fSAndroid Build Coastguard Worker 		}
116*6a54128fSAndroid Build Coastguard Worker 		/*
117*6a54128fSAndroid Build Coastguard Worker 		 * XXX Make sure dx_parent->max_hash < dx_db->max_hash
118*6a54128fSAndroid Build Coastguard Worker 		 */
119*6a54128fSAndroid Build Coastguard Worker 		if (dx_db->flags & DX_FLAG_LAST) {
120*6a54128fSAndroid Build Coastguard Worker 			dx_parent->max_hash = dx_db->max_hash;
121*6a54128fSAndroid Build Coastguard Worker 		}
122*6a54128fSAndroid Build Coastguard Worker 	}
123*6a54128fSAndroid Build Coastguard Worker }
124*6a54128fSAndroid Build Coastguard Worker 
e2fsck_pass2(e2fsck_t ctx)125*6a54128fSAndroid Build Coastguard Worker void e2fsck_pass2(e2fsck_t ctx)
126*6a54128fSAndroid Build Coastguard Worker {
127*6a54128fSAndroid Build Coastguard Worker 	struct ext2_super_block *sb = ctx->fs->super;
128*6a54128fSAndroid Build Coastguard Worker 	struct problem_context	pctx;
129*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys 		fs = ctx->fs;
130*6a54128fSAndroid Build Coastguard Worker 	char			*buf = NULL;
131*6a54128fSAndroid Build Coastguard Worker #ifdef RESOURCE_TRACK
132*6a54128fSAndroid Build Coastguard Worker 	struct resource_track	rtrack;
133*6a54128fSAndroid Build Coastguard Worker #endif
134*6a54128fSAndroid Build Coastguard Worker 	struct check_dir_struct cd;
135*6a54128fSAndroid Build Coastguard Worker 	struct dx_dir_info	*dx_dir;
136*6a54128fSAndroid Build Coastguard Worker 	struct dx_dirblock_info	*dx_db;
137*6a54128fSAndroid Build Coastguard Worker 	blk_t			b;
138*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t		i;
139*6a54128fSAndroid Build Coastguard Worker 	short			depth;
140*6a54128fSAndroid Build Coastguard Worker 	problem_t		code;
141*6a54128fSAndroid Build Coastguard Worker 	int			bad_dir;
142*6a54128fSAndroid Build Coastguard Worker 	int (*check_dir_func)(ext2_filsys fs,
143*6a54128fSAndroid Build Coastguard Worker 			      struct ext2_db_entry2 *dir_blocks_info,
144*6a54128fSAndroid Build Coastguard Worker 			      void *priv_data);
145*6a54128fSAndroid Build Coastguard Worker 
146*6a54128fSAndroid Build Coastguard Worker 	init_resource_track(&rtrack, ctx->fs->io);
147*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&cd.pctx);
148*6a54128fSAndroid Build Coastguard Worker 
149*6a54128fSAndroid Build Coastguard Worker #ifdef MTRACE
150*6a54128fSAndroid Build Coastguard Worker 	mtrace_print("Pass 2");
151*6a54128fSAndroid Build Coastguard Worker #endif
152*6a54128fSAndroid Build Coastguard Worker 
153*6a54128fSAndroid Build Coastguard Worker 	fs->flags |= EXT2_FLAG_IGNORE_SWAP_DIRENT;
154*6a54128fSAndroid Build Coastguard Worker 	if (!(ctx->options & E2F_OPT_PREEN))
155*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_2_PASS_HEADER, &cd.pctx);
156*6a54128fSAndroid Build Coastguard Worker 
157*6a54128fSAndroid Build Coastguard Worker 	cd.pctx.errcode = e2fsck_setup_icount(ctx, "inode_count",
158*6a54128fSAndroid Build Coastguard Worker 				EXT2_ICOUNT_OPT_INCREMENT,
159*6a54128fSAndroid Build Coastguard Worker 				ctx->inode_link_info, &ctx->inode_count);
160*6a54128fSAndroid Build Coastguard Worker 	if (cd.pctx.errcode) {
161*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_2_ALLOCATE_ICOUNT, &cd.pctx);
162*6a54128fSAndroid Build Coastguard Worker 		ctx->flags |= E2F_FLAG_ABORT;
163*6a54128fSAndroid Build Coastguard Worker 		goto cleanup;
164*6a54128fSAndroid Build Coastguard Worker 	}
165*6a54128fSAndroid Build Coastguard Worker 	buf = (char *) e2fsck_allocate_memory(ctx, 2*fs->blocksize,
166*6a54128fSAndroid Build Coastguard Worker 					      "directory scan buffer");
167*6a54128fSAndroid Build Coastguard Worker 
168*6a54128fSAndroid Build Coastguard Worker 	/*
169*6a54128fSAndroid Build Coastguard Worker 	 * Set up the parent pointer for the root directory, if
170*6a54128fSAndroid Build Coastguard Worker 	 * present.  (If the root directory is not present, we will
171*6a54128fSAndroid Build Coastguard Worker 	 * create it in pass 3.)
172*6a54128fSAndroid Build Coastguard Worker 	 */
173*6a54128fSAndroid Build Coastguard Worker 	(void) e2fsck_dir_info_set_parent(ctx, EXT2_ROOT_INO, EXT2_ROOT_INO);
174*6a54128fSAndroid Build Coastguard Worker 
175*6a54128fSAndroid Build Coastguard Worker 	cd.buf = buf;
176*6a54128fSAndroid Build Coastguard Worker 	cd.ctx = ctx;
177*6a54128fSAndroid Build Coastguard Worker 	cd.count = 1;
178*6a54128fSAndroid Build Coastguard Worker 	cd.max = ext2fs_dblist_count2(fs->dblist);
179*6a54128fSAndroid Build Coastguard Worker 	cd.list_offset = 0;
180*6a54128fSAndroid Build Coastguard Worker 	cd.ra_entries = ctx->readahead_kb * 1024 / ctx->fs->blocksize;
181*6a54128fSAndroid Build Coastguard Worker 	cd.next_ra_off = 0;
182*6a54128fSAndroid Build Coastguard Worker 
183*6a54128fSAndroid Build Coastguard Worker 	if (ctx->progress)
184*6a54128fSAndroid Build Coastguard Worker 		(void) (ctx->progress)(ctx, 2, 0, cd.max);
185*6a54128fSAndroid Build Coastguard Worker 
186*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_dir_index(fs->super))
187*6a54128fSAndroid Build Coastguard Worker 		ext2fs_dblist_sort2(fs->dblist, special_dir_block_cmp);
188*6a54128fSAndroid Build Coastguard Worker 
189*6a54128fSAndroid Build Coastguard Worker 	check_dir_func = cd.ra_entries ? check_dir_block2 : check_dir_block;
190*6a54128fSAndroid Build Coastguard Worker 	cd.pctx.errcode = ext2fs_dblist_iterate2(fs->dblist, check_dir_func,
191*6a54128fSAndroid Build Coastguard Worker 						 &cd);
192*6a54128fSAndroid Build Coastguard Worker 	if (ctx->flags & E2F_FLAG_RESTART_LATER) {
193*6a54128fSAndroid Build Coastguard Worker 		ctx->flags |= E2F_FLAG_RESTART;
194*6a54128fSAndroid Build Coastguard Worker 		ctx->flags &= ~E2F_FLAG_RESTART_LATER;
195*6a54128fSAndroid Build Coastguard Worker 	}
196*6a54128fSAndroid Build Coastguard Worker 
197*6a54128fSAndroid Build Coastguard Worker 	if (ctx->flags & E2F_FLAG_RUN_RETURN)
198*6a54128fSAndroid Build Coastguard Worker 		goto cleanup;
199*6a54128fSAndroid Build Coastguard Worker 
200*6a54128fSAndroid Build Coastguard Worker 	if (cd.pctx.errcode) {
201*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_2_DBLIST_ITERATE, &cd.pctx);
202*6a54128fSAndroid Build Coastguard Worker 		ctx->flags |= E2F_FLAG_ABORT;
203*6a54128fSAndroid Build Coastguard Worker 		goto cleanup;
204*6a54128fSAndroid Build Coastguard Worker 	}
205*6a54128fSAndroid Build Coastguard Worker 
206*6a54128fSAndroid Build Coastguard Worker 	for (i=0; (dx_dir = e2fsck_dx_dir_info_iter(ctx, &i)) != 0;) {
207*6a54128fSAndroid Build Coastguard Worker 		if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
208*6a54128fSAndroid Build Coastguard Worker 			goto cleanup;
209*6a54128fSAndroid Build Coastguard Worker 		if (e2fsck_dir_will_be_rehashed(ctx, dx_dir->ino) ||
210*6a54128fSAndroid Build Coastguard Worker 		    dx_dir->numblocks == 0)
211*6a54128fSAndroid Build Coastguard Worker 			continue;
212*6a54128fSAndroid Build Coastguard Worker 		clear_problem_context(&pctx);
213*6a54128fSAndroid Build Coastguard Worker 		bad_dir = 0;
214*6a54128fSAndroid Build Coastguard Worker 		pctx.dir = dx_dir->ino;
215*6a54128fSAndroid Build Coastguard Worker 		dx_db = dx_dir->dx_block;
216*6a54128fSAndroid Build Coastguard Worker 		if (dx_db->flags & DX_FLAG_REFERENCED)
217*6a54128fSAndroid Build Coastguard Worker 			dx_db->flags |= DX_FLAG_DUP_REF;
218*6a54128fSAndroid Build Coastguard Worker 		else
219*6a54128fSAndroid Build Coastguard Worker 			dx_db->flags |= DX_FLAG_REFERENCED;
220*6a54128fSAndroid Build Coastguard Worker 		/*
221*6a54128fSAndroid Build Coastguard Worker 		 * Find all of the first and last leaf blocks, and
222*6a54128fSAndroid Build Coastguard Worker 		 * update their parent's min and max hash values
223*6a54128fSAndroid Build Coastguard Worker 		 */
224*6a54128fSAndroid Build Coastguard Worker 		update_parents(dx_dir, DX_DIRBLOCK_LEAF);
225*6a54128fSAndroid Build Coastguard Worker 
226*6a54128fSAndroid Build Coastguard Worker 		/* for 3 level htree: update 2 level parent's min
227*6a54128fSAndroid Build Coastguard Worker 		 * and max hash values */
228*6a54128fSAndroid Build Coastguard Worker 		update_parents(dx_dir, DX_DIRBLOCK_NODE);
229*6a54128fSAndroid Build Coastguard Worker 
230*6a54128fSAndroid Build Coastguard Worker 		for (b=0, dx_db = dx_dir->dx_block;
231*6a54128fSAndroid Build Coastguard Worker 		     b < dx_dir->numblocks;
232*6a54128fSAndroid Build Coastguard Worker 		     b++, dx_db++) {
233*6a54128fSAndroid Build Coastguard Worker 			pctx.blkcount = b;
234*6a54128fSAndroid Build Coastguard Worker 			pctx.group = dx_db->parent;
235*6a54128fSAndroid Build Coastguard Worker 			code = 0;
236*6a54128fSAndroid Build Coastguard Worker 			if (!(dx_db->flags & DX_FLAG_FIRST) &&
237*6a54128fSAndroid Build Coastguard Worker 			    (dx_db->min_hash < dx_db->node_min_hash)) {
238*6a54128fSAndroid Build Coastguard Worker 				pctx.blk = dx_db->min_hash;
239*6a54128fSAndroid Build Coastguard Worker 				pctx.blk2 = dx_db->node_min_hash;
240*6a54128fSAndroid Build Coastguard Worker 				code = PR_2_HTREE_MIN_HASH;
241*6a54128fSAndroid Build Coastguard Worker 				fix_problem(ctx, code, &pctx);
242*6a54128fSAndroid Build Coastguard Worker 				bad_dir++;
243*6a54128fSAndroid Build Coastguard Worker 			}
244*6a54128fSAndroid Build Coastguard Worker 			if (dx_db->type == DX_DIRBLOCK_LEAF) {
245*6a54128fSAndroid Build Coastguard Worker 				depth = htree_depth(dx_dir, dx_db);
246*6a54128fSAndroid Build Coastguard Worker 				if (depth != dx_dir->depth) {
247*6a54128fSAndroid Build Coastguard Worker 					pctx.num = dx_dir->depth;
248*6a54128fSAndroid Build Coastguard Worker 					code = PR_2_HTREE_BAD_DEPTH;
249*6a54128fSAndroid Build Coastguard Worker 					fix_problem(ctx, code, &pctx);
250*6a54128fSAndroid Build Coastguard Worker 					bad_dir++;
251*6a54128fSAndroid Build Coastguard Worker 				}
252*6a54128fSAndroid Build Coastguard Worker 			}
253*6a54128fSAndroid Build Coastguard Worker 			/*
254*6a54128fSAndroid Build Coastguard Worker 			 * This test doesn't apply for the root block
255*6a54128fSAndroid Build Coastguard Worker 			 * at block #0
256*6a54128fSAndroid Build Coastguard Worker 			 */
257*6a54128fSAndroid Build Coastguard Worker 			if (b &&
258*6a54128fSAndroid Build Coastguard Worker 			    (dx_db->max_hash > dx_db->node_max_hash)) {
259*6a54128fSAndroid Build Coastguard Worker 				pctx.blk = dx_db->max_hash;
260*6a54128fSAndroid Build Coastguard Worker 				pctx.blk2 = dx_db->node_max_hash;
261*6a54128fSAndroid Build Coastguard Worker 				code = PR_2_HTREE_MAX_HASH;
262*6a54128fSAndroid Build Coastguard Worker 				fix_problem(ctx, code, &pctx);
263*6a54128fSAndroid Build Coastguard Worker 				bad_dir++;
264*6a54128fSAndroid Build Coastguard Worker 			}
265*6a54128fSAndroid Build Coastguard Worker 			if (!(dx_db->flags & DX_FLAG_REFERENCED)) {
266*6a54128fSAndroid Build Coastguard Worker 				code = PR_2_HTREE_NOTREF;
267*6a54128fSAndroid Build Coastguard Worker 				fix_problem(ctx, code, &pctx);
268*6a54128fSAndroid Build Coastguard Worker 				bad_dir++;
269*6a54128fSAndroid Build Coastguard Worker 			} else if (dx_db->flags & DX_FLAG_DUP_REF) {
270*6a54128fSAndroid Build Coastguard Worker 				code = PR_2_HTREE_DUPREF;
271*6a54128fSAndroid Build Coastguard Worker 				fix_problem(ctx, code, &pctx);
272*6a54128fSAndroid Build Coastguard Worker 				bad_dir++;
273*6a54128fSAndroid Build Coastguard Worker 			}
274*6a54128fSAndroid Build Coastguard Worker 		}
275*6a54128fSAndroid Build Coastguard Worker 		if (bad_dir && fix_problem(ctx, PR_2_HTREE_CLEAR, &pctx)) {
276*6a54128fSAndroid Build Coastguard Worker 			clear_htree(ctx, dx_dir->ino);
277*6a54128fSAndroid Build Coastguard Worker 			dx_dir->numblocks = 0;
278*6a54128fSAndroid Build Coastguard Worker 		}
279*6a54128fSAndroid Build Coastguard Worker 	}
280*6a54128fSAndroid Build Coastguard Worker 	e2fsck_free_dx_dir_info(ctx);
281*6a54128fSAndroid Build Coastguard Worker 
282*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&buf);
283*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_dblist(fs->dblist);
284*6a54128fSAndroid Build Coastguard Worker 
285*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_bad_map) {
286*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_inode_bitmap(ctx->inode_bad_map);
287*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_bad_map = 0;
288*6a54128fSAndroid Build Coastguard Worker 	}
289*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_reg_map) {
290*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_inode_bitmap(ctx->inode_reg_map);
291*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_reg_map = 0;
292*6a54128fSAndroid Build Coastguard Worker 	}
293*6a54128fSAndroid Build Coastguard Worker 	if (ctx->inode_casefold_map) {
294*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_inode_bitmap(ctx->inode_casefold_map);
295*6a54128fSAndroid Build Coastguard Worker 		ctx->inode_casefold_map = 0;
296*6a54128fSAndroid Build Coastguard Worker 	}
297*6a54128fSAndroid Build Coastguard Worker 	destroy_encrypted_file_info(ctx);
298*6a54128fSAndroid Build Coastguard Worker 	if (ctx->casefolded_dirs) {
299*6a54128fSAndroid Build Coastguard Worker 		ext2fs_u32_list_free(ctx->casefolded_dirs);
300*6a54128fSAndroid Build Coastguard Worker 		ctx->casefolded_dirs = 0;
301*6a54128fSAndroid Build Coastguard Worker 	}
302*6a54128fSAndroid Build Coastguard Worker 
303*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
304*6a54128fSAndroid Build Coastguard Worker 	if (ctx->large_files) {
305*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_large_file(sb) &&
306*6a54128fSAndroid Build Coastguard Worker 		    fix_problem(ctx, PR_2_FEATURE_LARGE_FILES, &pctx)) {
307*6a54128fSAndroid Build Coastguard Worker 			ext2fs_set_feature_large_file(sb);
308*6a54128fSAndroid Build Coastguard Worker 			fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
309*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
310*6a54128fSAndroid Build Coastguard Worker 		}
311*6a54128fSAndroid Build Coastguard Worker 		if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
312*6a54128fSAndroid Build Coastguard Worker 		    fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
313*6a54128fSAndroid Build Coastguard Worker 			ext2fs_update_dynamic_rev(fs);
314*6a54128fSAndroid Build Coastguard Worker 			ext2fs_mark_super_dirty(fs);
315*6a54128fSAndroid Build Coastguard Worker 		}
316*6a54128fSAndroid Build Coastguard Worker 	}
317*6a54128fSAndroid Build Coastguard Worker 
318*6a54128fSAndroid Build Coastguard Worker 	print_resource_track(ctx, _("Pass 2"), &rtrack, fs->io);
319*6a54128fSAndroid Build Coastguard Worker cleanup:
320*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&buf);
321*6a54128fSAndroid Build Coastguard Worker 	fs->flags &= ~EXT2_FLAG_IGNORE_SWAP_DIRENT;
322*6a54128fSAndroid Build Coastguard Worker }
323*6a54128fSAndroid Build Coastguard Worker 
324*6a54128fSAndroid Build Coastguard Worker #define MAX_DEPTH 32000
htree_depth(struct dx_dir_info * dx_dir,struct dx_dirblock_info * dx_db)325*6a54128fSAndroid Build Coastguard Worker static short htree_depth(struct dx_dir_info *dx_dir,
326*6a54128fSAndroid Build Coastguard Worker 			 struct dx_dirblock_info *dx_db)
327*6a54128fSAndroid Build Coastguard Worker {
328*6a54128fSAndroid Build Coastguard Worker 	short depth = 0;
329*6a54128fSAndroid Build Coastguard Worker 
330*6a54128fSAndroid Build Coastguard Worker 	while (dx_db->type != DX_DIRBLOCK_ROOT && depth < MAX_DEPTH) {
331*6a54128fSAndroid Build Coastguard Worker 		dx_db = &dx_dir->dx_block[dx_db->parent];
332*6a54128fSAndroid Build Coastguard Worker 		depth++;
333*6a54128fSAndroid Build Coastguard Worker 	}
334*6a54128fSAndroid Build Coastguard Worker 	return depth;
335*6a54128fSAndroid Build Coastguard Worker }
336*6a54128fSAndroid Build Coastguard Worker 
dict_de_cmp(const void * cmp_ctx EXT2FS_ATTR ((unused)),const void * a,const void * b)337*6a54128fSAndroid Build Coastguard Worker static int dict_de_cmp(const void *cmp_ctx EXT2FS_ATTR((unused)),
338*6a54128fSAndroid Build Coastguard Worker 		       const void *a, const void *b)
339*6a54128fSAndroid Build Coastguard Worker {
340*6a54128fSAndroid Build Coastguard Worker 	const struct ext2_dir_entry *de_a, *de_b;
341*6a54128fSAndroid Build Coastguard Worker 	int	a_len, b_len;
342*6a54128fSAndroid Build Coastguard Worker 
343*6a54128fSAndroid Build Coastguard Worker 	de_a = (const struct ext2_dir_entry *) a;
344*6a54128fSAndroid Build Coastguard Worker 	a_len = ext2fs_dirent_name_len(de_a);
345*6a54128fSAndroid Build Coastguard Worker 	de_b = (const struct ext2_dir_entry *) b;
346*6a54128fSAndroid Build Coastguard Worker 	b_len = ext2fs_dirent_name_len(de_b);
347*6a54128fSAndroid Build Coastguard Worker 
348*6a54128fSAndroid Build Coastguard Worker 	if (a_len != b_len)
349*6a54128fSAndroid Build Coastguard Worker 		return (a_len - b_len);
350*6a54128fSAndroid Build Coastguard Worker 
351*6a54128fSAndroid Build Coastguard Worker 	return memcmp(de_a->name, de_b->name, a_len);
352*6a54128fSAndroid Build Coastguard Worker }
353*6a54128fSAndroid Build Coastguard Worker 
dict_de_cf_cmp(const void * cmp_ctx,const void * a,const void * b)354*6a54128fSAndroid Build Coastguard Worker static int dict_de_cf_cmp(const void *cmp_ctx, const void *a, const void *b)
355*6a54128fSAndroid Build Coastguard Worker {
356*6a54128fSAndroid Build Coastguard Worker 	const struct ext2fs_nls_table *tbl = cmp_ctx;
357*6a54128fSAndroid Build Coastguard Worker 	const struct ext2_dir_entry *de_a, *de_b;
358*6a54128fSAndroid Build Coastguard Worker 	int	a_len, b_len;
359*6a54128fSAndroid Build Coastguard Worker 
360*6a54128fSAndroid Build Coastguard Worker 	de_a = (const struct ext2_dir_entry *) a;
361*6a54128fSAndroid Build Coastguard Worker 	a_len = ext2fs_dirent_name_len(de_a);
362*6a54128fSAndroid Build Coastguard Worker 	de_b = (const struct ext2_dir_entry *) b;
363*6a54128fSAndroid Build Coastguard Worker 	b_len = ext2fs_dirent_name_len(de_b);
364*6a54128fSAndroid Build Coastguard Worker 
365*6a54128fSAndroid Build Coastguard Worker 	return ext2fs_casefold_cmp(tbl,
366*6a54128fSAndroid Build Coastguard Worker 				   (const unsigned char *) de_a->name, a_len,
367*6a54128fSAndroid Build Coastguard Worker 				   (const unsigned char *) de_b->name, b_len);
368*6a54128fSAndroid Build Coastguard Worker }
369*6a54128fSAndroid Build Coastguard Worker 
370*6a54128fSAndroid Build Coastguard Worker /*
371*6a54128fSAndroid Build Coastguard Worker  * This is special sort function that makes sure that directory blocks
372*6a54128fSAndroid Build Coastguard Worker  * with a dirblock of zero are sorted to the beginning of the list.
373*6a54128fSAndroid Build Coastguard Worker  * This guarantees that the root node of the htree directories are
374*6a54128fSAndroid Build Coastguard Worker  * processed first, so we know what hash version to use.
375*6a54128fSAndroid Build Coastguard Worker  */
special_dir_block_cmp(const void * a,const void * b)376*6a54128fSAndroid Build Coastguard Worker static EXT2_QSORT_TYPE special_dir_block_cmp(const void *a, const void *b)
377*6a54128fSAndroid Build Coastguard Worker {
378*6a54128fSAndroid Build Coastguard Worker 	const struct ext2_db_entry2 *db_a =
379*6a54128fSAndroid Build Coastguard Worker 		(const struct ext2_db_entry2 *) a;
380*6a54128fSAndroid Build Coastguard Worker 	const struct ext2_db_entry2 *db_b =
381*6a54128fSAndroid Build Coastguard Worker 		(const struct ext2_db_entry2 *) b;
382*6a54128fSAndroid Build Coastguard Worker 
383*6a54128fSAndroid Build Coastguard Worker 	if (db_a->blockcnt && !db_b->blockcnt)
384*6a54128fSAndroid Build Coastguard Worker 		return 1;
385*6a54128fSAndroid Build Coastguard Worker 
386*6a54128fSAndroid Build Coastguard Worker 	if (!db_a->blockcnt && db_b->blockcnt)
387*6a54128fSAndroid Build Coastguard Worker 		return -1;
388*6a54128fSAndroid Build Coastguard Worker 
389*6a54128fSAndroid Build Coastguard Worker 	if (db_a->blk != db_b->blk)
390*6a54128fSAndroid Build Coastguard Worker 		return (int) (db_a->blk - db_b->blk);
391*6a54128fSAndroid Build Coastguard Worker 
392*6a54128fSAndroid Build Coastguard Worker 	if (db_a->ino != db_b->ino)
393*6a54128fSAndroid Build Coastguard Worker 		return (int) (db_a->ino - db_b->ino);
394*6a54128fSAndroid Build Coastguard Worker 
395*6a54128fSAndroid Build Coastguard Worker 	return (int) (db_a->blockcnt - db_b->blockcnt);
396*6a54128fSAndroid Build Coastguard Worker }
397*6a54128fSAndroid Build Coastguard Worker 
398*6a54128fSAndroid Build Coastguard Worker 
399*6a54128fSAndroid Build Coastguard Worker /*
400*6a54128fSAndroid Build Coastguard Worker  * Make sure the first entry in the directory is '.', and that the
401*6a54128fSAndroid Build Coastguard Worker  * directory entry is sane.
402*6a54128fSAndroid Build Coastguard Worker  */
check_dot(e2fsck_t ctx,struct ext2_dir_entry * dirent,ext2_ino_t ino,struct problem_context * pctx)403*6a54128fSAndroid Build Coastguard Worker static int check_dot(e2fsck_t ctx,
404*6a54128fSAndroid Build Coastguard Worker 		     struct ext2_dir_entry *dirent,
405*6a54128fSAndroid Build Coastguard Worker 		     ext2_ino_t ino, struct problem_context *pctx)
406*6a54128fSAndroid Build Coastguard Worker {
407*6a54128fSAndroid Build Coastguard Worker 	struct ext2_dir_entry *nextdir;
408*6a54128fSAndroid Build Coastguard Worker 	unsigned int	rec_len, new_len;
409*6a54128fSAndroid Build Coastguard Worker 	int		status = 0;
410*6a54128fSAndroid Build Coastguard Worker 	int		created = 0;
411*6a54128fSAndroid Build Coastguard Worker 	problem_t	problem = 0;
412*6a54128fSAndroid Build Coastguard Worker 	int		ftype = EXT2_FT_DIR;
413*6a54128fSAndroid Build Coastguard Worker 
414*6a54128fSAndroid Build Coastguard Worker 	if (!dirent->inode)
415*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_MISSING_DOT;
416*6a54128fSAndroid Build Coastguard Worker 	else if ((ext2fs_dirent_name_len(dirent) != 1) ||
417*6a54128fSAndroid Build Coastguard Worker 		 (dirent->name[0] != '.'))
418*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_1ST_NOT_DOT;
419*6a54128fSAndroid Build Coastguard Worker 	else if (dirent->name[1] != '\0')
420*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_DOT_NULL_TERM;
421*6a54128fSAndroid Build Coastguard Worker 
422*6a54128fSAndroid Build Coastguard Worker 	(void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
423*6a54128fSAndroid Build Coastguard Worker 	if (problem) {
424*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_filetype(ctx->fs->super))
425*6a54128fSAndroid Build Coastguard Worker 			ftype = EXT2_FT_UNKNOWN;
426*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, problem, pctx)) {
427*6a54128fSAndroid Build Coastguard Worker 			if (rec_len < 12)
428*6a54128fSAndroid Build Coastguard Worker 				rec_len = dirent->rec_len = 12;
429*6a54128fSAndroid Build Coastguard Worker 			dirent->inode = ino;
430*6a54128fSAndroid Build Coastguard Worker 			ext2fs_dirent_set_name_len(dirent, 1);
431*6a54128fSAndroid Build Coastguard Worker 			ext2fs_dirent_set_file_type(dirent, ftype);
432*6a54128fSAndroid Build Coastguard Worker 			dirent->name[0] = '.';
433*6a54128fSAndroid Build Coastguard Worker 			dirent->name[1] = '\0';
434*6a54128fSAndroid Build Coastguard Worker 			status = 1;
435*6a54128fSAndroid Build Coastguard Worker 			created = 1;
436*6a54128fSAndroid Build Coastguard Worker 		}
437*6a54128fSAndroid Build Coastguard Worker 	}
438*6a54128fSAndroid Build Coastguard Worker 	if (dirent->inode != ino) {
439*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_BAD_INODE_DOT, pctx)) {
440*6a54128fSAndroid Build Coastguard Worker 			dirent->inode = ino;
441*6a54128fSAndroid Build Coastguard Worker 			status = 1;
442*6a54128fSAndroid Build Coastguard Worker 		}
443*6a54128fSAndroid Build Coastguard Worker 	}
444*6a54128fSAndroid Build Coastguard Worker 	if (rec_len > 12) {
445*6a54128fSAndroid Build Coastguard Worker 		new_len = rec_len - 12;
446*6a54128fSAndroid Build Coastguard Worker 		if (new_len > 12) {
447*6a54128fSAndroid Build Coastguard Worker 			if (created ||
448*6a54128fSAndroid Build Coastguard Worker 			    fix_problem(ctx, PR_2_SPLIT_DOT, pctx)) {
449*6a54128fSAndroid Build Coastguard Worker 				nextdir = (struct ext2_dir_entry *)
450*6a54128fSAndroid Build Coastguard Worker 					((char *) dirent + 12);
451*6a54128fSAndroid Build Coastguard Worker 				dirent->rec_len = 12;
452*6a54128fSAndroid Build Coastguard Worker 				/* if the next entry looks like "..", leave it
453*6a54128fSAndroid Build Coastguard Worker 				 * and let check_dotdot() verify the dirent,
454*6a54128fSAndroid Build Coastguard Worker 				 * otherwise zap the following entry. */
455*6a54128fSAndroid Build Coastguard Worker 				if (strncmp(nextdir->name, "..", 3) != 0) {
456*6a54128fSAndroid Build Coastguard Worker 					(void)ext2fs_set_rec_len(ctx->fs,
457*6a54128fSAndroid Build Coastguard Worker 								 new_len,
458*6a54128fSAndroid Build Coastguard Worker 								 nextdir);
459*6a54128fSAndroid Build Coastguard Worker 					nextdir->inode = 0;
460*6a54128fSAndroid Build Coastguard Worker 					ext2fs_dirent_set_name_len(nextdir, 0);
461*6a54128fSAndroid Build Coastguard Worker 					ext2fs_dirent_set_file_type(nextdir,
462*6a54128fSAndroid Build Coastguard Worker 								    ftype);
463*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
464*6a54128fSAndroid Build Coastguard Worker 				} else {
465*6a54128fSAndroid Build Coastguard Worker 					(void) ext2fs_dirent_swab_in2(ctx->fs,
466*6a54128fSAndroid Build Coastguard Worker 						(char *) nextdir,
467*6a54128fSAndroid Build Coastguard Worker 						ctx->fs->blocksize - 12, 0);
468*6a54128fSAndroid Build Coastguard Worker #endif
469*6a54128fSAndroid Build Coastguard Worker 				}
470*6a54128fSAndroid Build Coastguard Worker 				status = 1;
471*6a54128fSAndroid Build Coastguard Worker 			}
472*6a54128fSAndroid Build Coastguard Worker 		}
473*6a54128fSAndroid Build Coastguard Worker 	}
474*6a54128fSAndroid Build Coastguard Worker 	return status;
475*6a54128fSAndroid Build Coastguard Worker }
476*6a54128fSAndroid Build Coastguard Worker 
477*6a54128fSAndroid Build Coastguard Worker /*
478*6a54128fSAndroid Build Coastguard Worker  * Make sure the second entry in the directory is '..', and that the
479*6a54128fSAndroid Build Coastguard Worker  * directory entry is sane.  We do not check the inode number of '..'
480*6a54128fSAndroid Build Coastguard Worker  * here; this gets done in pass 3.
481*6a54128fSAndroid Build Coastguard Worker  */
check_dotdot(e2fsck_t ctx,struct ext2_dir_entry * dirent,ext2_ino_t ino,struct problem_context * pctx)482*6a54128fSAndroid Build Coastguard Worker static int check_dotdot(e2fsck_t ctx,
483*6a54128fSAndroid Build Coastguard Worker 			struct ext2_dir_entry *dirent,
484*6a54128fSAndroid Build Coastguard Worker 			ext2_ino_t ino, struct problem_context *pctx)
485*6a54128fSAndroid Build Coastguard Worker {
486*6a54128fSAndroid Build Coastguard Worker 	problem_t	problem = 0;
487*6a54128fSAndroid Build Coastguard Worker 	unsigned int	rec_len;
488*6a54128fSAndroid Build Coastguard Worker 	int		ftype = EXT2_FT_DIR;
489*6a54128fSAndroid Build Coastguard Worker 
490*6a54128fSAndroid Build Coastguard Worker 	if (!dirent->inode)
491*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_MISSING_DOT_DOT;
492*6a54128fSAndroid Build Coastguard Worker 	else if ((ext2fs_dirent_name_len(dirent) != 2) ||
493*6a54128fSAndroid Build Coastguard Worker 		 (dirent->name[0] != '.') ||
494*6a54128fSAndroid Build Coastguard Worker 		 (dirent->name[1] != '.'))
495*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_2ND_NOT_DOT_DOT;
496*6a54128fSAndroid Build Coastguard Worker 	else if (dirent->name[2] != '\0')
497*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_DOT_DOT_NULL_TERM;
498*6a54128fSAndroid Build Coastguard Worker 
499*6a54128fSAndroid Build Coastguard Worker 	(void) ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
500*6a54128fSAndroid Build Coastguard Worker 	if (problem) {
501*6a54128fSAndroid Build Coastguard Worker 		if (!ext2fs_has_feature_filetype(ctx->fs->super))
502*6a54128fSAndroid Build Coastguard Worker 			ftype = EXT2_FT_UNKNOWN;
503*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, problem, pctx)) {
504*6a54128fSAndroid Build Coastguard Worker 			if (rec_len < 12)
505*6a54128fSAndroid Build Coastguard Worker 				dirent->rec_len = 12;
506*6a54128fSAndroid Build Coastguard Worker 			/*
507*6a54128fSAndroid Build Coastguard Worker 			 * Note: we don't have the parent inode just
508*6a54128fSAndroid Build Coastguard Worker 			 * yet, so we will fill it in with the root
509*6a54128fSAndroid Build Coastguard Worker 			 * inode.  This will get fixed in pass 3.
510*6a54128fSAndroid Build Coastguard Worker 			 */
511*6a54128fSAndroid Build Coastguard Worker 			dirent->inode = EXT2_ROOT_INO;
512*6a54128fSAndroid Build Coastguard Worker 			ext2fs_dirent_set_name_len(dirent, 2);
513*6a54128fSAndroid Build Coastguard Worker 			ext2fs_dirent_set_file_type(dirent, ftype);
514*6a54128fSAndroid Build Coastguard Worker 			dirent->name[0] = '.';
515*6a54128fSAndroid Build Coastguard Worker 			dirent->name[1] = '.';
516*6a54128fSAndroid Build Coastguard Worker 			dirent->name[2] = '\0';
517*6a54128fSAndroid Build Coastguard Worker 			return 1;
518*6a54128fSAndroid Build Coastguard Worker 		}
519*6a54128fSAndroid Build Coastguard Worker 		return 0;
520*6a54128fSAndroid Build Coastguard Worker 	}
521*6a54128fSAndroid Build Coastguard Worker 	if (e2fsck_dir_info_set_dotdot(ctx, ino, dirent->inode)) {
522*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_2_NO_DIRINFO, pctx);
523*6a54128fSAndroid Build Coastguard Worker 		return -1;
524*6a54128fSAndroid Build Coastguard Worker 	}
525*6a54128fSAndroid Build Coastguard Worker 	return 0;
526*6a54128fSAndroid Build Coastguard Worker }
527*6a54128fSAndroid Build Coastguard Worker 
528*6a54128fSAndroid Build Coastguard Worker /*
529*6a54128fSAndroid Build Coastguard Worker  * Check to make sure a directory entry doesn't contain any illegal
530*6a54128fSAndroid Build Coastguard Worker  * characters.
531*6a54128fSAndroid Build Coastguard Worker  */
check_name(e2fsck_t ctx,struct ext2_dir_entry * dirent,struct problem_context * pctx)532*6a54128fSAndroid Build Coastguard Worker static int check_name(e2fsck_t ctx,
533*6a54128fSAndroid Build Coastguard Worker 		      struct ext2_dir_entry *dirent,
534*6a54128fSAndroid Build Coastguard Worker 		      struct problem_context *pctx)
535*6a54128fSAndroid Build Coastguard Worker {
536*6a54128fSAndroid Build Coastguard Worker 	int	i;
537*6a54128fSAndroid Build Coastguard Worker 	int	fixup = -1;
538*6a54128fSAndroid Build Coastguard Worker 	int	ret = 0;
539*6a54128fSAndroid Build Coastguard Worker 
540*6a54128fSAndroid Build Coastguard Worker 	for ( i = 0; i < ext2fs_dirent_name_len(dirent); i++) {
541*6a54128fSAndroid Build Coastguard Worker 		if (dirent->name[i] != '/' && dirent->name[i] != '\0')
542*6a54128fSAndroid Build Coastguard Worker 			continue;
543*6a54128fSAndroid Build Coastguard Worker 		if (fixup < 0)
544*6a54128fSAndroid Build Coastguard Worker 			fixup = fix_problem(ctx, PR_2_BAD_NAME, pctx);
545*6a54128fSAndroid Build Coastguard Worker 		if (fixup == 0)
546*6a54128fSAndroid Build Coastguard Worker 			return 0;
547*6a54128fSAndroid Build Coastguard Worker 		dirent->name[i] = '.';
548*6a54128fSAndroid Build Coastguard Worker 		ret = 1;
549*6a54128fSAndroid Build Coastguard Worker 	}
550*6a54128fSAndroid Build Coastguard Worker 	return ret;
551*6a54128fSAndroid Build Coastguard Worker }
552*6a54128fSAndroid Build Coastguard Worker 
encrypted_check_name(e2fsck_t ctx,const struct ext2_dir_entry * dirent,struct problem_context * pctx)553*6a54128fSAndroid Build Coastguard Worker static int encrypted_check_name(e2fsck_t ctx,
554*6a54128fSAndroid Build Coastguard Worker 				const struct ext2_dir_entry *dirent,
555*6a54128fSAndroid Build Coastguard Worker 				struct problem_context *pctx)
556*6a54128fSAndroid Build Coastguard Worker {
557*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_dirent_name_len(dirent) < EXT4_CRYPTO_BLOCK_SIZE) {
558*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_BAD_ENCRYPTED_NAME, pctx))
559*6a54128fSAndroid Build Coastguard Worker 			return 1;
560*6a54128fSAndroid Build Coastguard Worker 		ext2fs_unmark_valid(ctx->fs);
561*6a54128fSAndroid Build Coastguard Worker 	}
562*6a54128fSAndroid Build Coastguard Worker 	return 0;
563*6a54128fSAndroid Build Coastguard Worker }
564*6a54128fSAndroid Build Coastguard Worker 
encoded_check_name(e2fsck_t ctx,struct ext2_dir_entry * dirent,struct problem_context * pctx)565*6a54128fSAndroid Build Coastguard Worker static int encoded_check_name(e2fsck_t ctx,
566*6a54128fSAndroid Build Coastguard Worker 			      struct ext2_dir_entry *dirent,
567*6a54128fSAndroid Build Coastguard Worker 			      struct problem_context *pctx)
568*6a54128fSAndroid Build Coastguard Worker {
569*6a54128fSAndroid Build Coastguard Worker 	const struct ext2fs_nls_table *tbl = ctx->fs->encoding;
570*6a54128fSAndroid Build Coastguard Worker 	int ret;
571*6a54128fSAndroid Build Coastguard Worker 	int len = ext2fs_dirent_name_len(dirent);
572*6a54128fSAndroid Build Coastguard Worker 	char *pos, *end;
573*6a54128fSAndroid Build Coastguard Worker 
574*6a54128fSAndroid Build Coastguard Worker 	ret = ext2fs_check_encoded_name(tbl, dirent->name, len, &pos);
575*6a54128fSAndroid Build Coastguard Worker 	if (ret < 0) {
576*6a54128fSAndroid Build Coastguard Worker 		fatal_error(ctx, _("NLS is broken."));
577*6a54128fSAndroid Build Coastguard Worker 	} else if(ret > 0) {
578*6a54128fSAndroid Build Coastguard Worker 		ret = fix_problem(ctx, PR_2_BAD_ENCODED_NAME, pctx);
579*6a54128fSAndroid Build Coastguard Worker 		if (ret) {
580*6a54128fSAndroid Build Coastguard Worker 			end = &dirent->name[len];
581*6a54128fSAndroid Build Coastguard Worker 			for (; *pos && pos != end; pos++)
582*6a54128fSAndroid Build Coastguard Worker 				*pos = '.';
583*6a54128fSAndroid Build Coastguard Worker 		}
584*6a54128fSAndroid Build Coastguard Worker 	}
585*6a54128fSAndroid Build Coastguard Worker 
586*6a54128fSAndroid Build Coastguard Worker 	return (ret || check_name(ctx, dirent, pctx));
587*6a54128fSAndroid Build Coastguard Worker }
588*6a54128fSAndroid Build Coastguard Worker 
589*6a54128fSAndroid Build Coastguard Worker /*
590*6a54128fSAndroid Build Coastguard Worker  * Check the directory filetype (if present)
591*6a54128fSAndroid Build Coastguard Worker  */
check_filetype(e2fsck_t ctx,struct ext2_dir_entry * dirent,ext2_ino_t dir_ino EXT2FS_ATTR ((unused)),struct problem_context * pctx)592*6a54128fSAndroid Build Coastguard Worker static _INLINE_ int check_filetype(e2fsck_t ctx,
593*6a54128fSAndroid Build Coastguard Worker 				   struct ext2_dir_entry *dirent,
594*6a54128fSAndroid Build Coastguard Worker 				   ext2_ino_t dir_ino EXT2FS_ATTR((unused)),
595*6a54128fSAndroid Build Coastguard Worker 				   struct problem_context *pctx)
596*6a54128fSAndroid Build Coastguard Worker {
597*6a54128fSAndroid Build Coastguard Worker 	int	filetype = ext2fs_dirent_file_type(dirent);
598*6a54128fSAndroid Build Coastguard Worker 	int	should_be = EXT2_FT_UNKNOWN;
599*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode	inode;
600*6a54128fSAndroid Build Coastguard Worker 
601*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_has_feature_filetype(ctx->fs->super)) {
602*6a54128fSAndroid Build Coastguard Worker 		if (filetype == 0 ||
603*6a54128fSAndroid Build Coastguard Worker 		    !fix_problem(ctx, PR_2_CLEAR_FILETYPE, pctx))
604*6a54128fSAndroid Build Coastguard Worker 			return 0;
605*6a54128fSAndroid Build Coastguard Worker 		ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
606*6a54128fSAndroid Build Coastguard Worker 		return 1;
607*6a54128fSAndroid Build Coastguard Worker 	}
608*6a54128fSAndroid Build Coastguard Worker 
609*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_test_inode_bitmap2(ctx->inode_dir_map, dirent->inode)) {
610*6a54128fSAndroid Build Coastguard Worker 		should_be = EXT2_FT_DIR;
611*6a54128fSAndroid Build Coastguard Worker 	} else if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map,
612*6a54128fSAndroid Build Coastguard Worker 					    dirent->inode)) {
613*6a54128fSAndroid Build Coastguard Worker 		should_be = EXT2_FT_REG_FILE;
614*6a54128fSAndroid Build Coastguard Worker 	} else if (ctx->inode_bad_map &&
615*6a54128fSAndroid Build Coastguard Worker 		   ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
616*6a54128fSAndroid Build Coastguard Worker 					    dirent->inode))
617*6a54128fSAndroid Build Coastguard Worker 		should_be = 0;
618*6a54128fSAndroid Build Coastguard Worker 	else {
619*6a54128fSAndroid Build Coastguard Worker 		e2fsck_read_inode(ctx, dirent->inode, &inode,
620*6a54128fSAndroid Build Coastguard Worker 				  "check_filetype");
621*6a54128fSAndroid Build Coastguard Worker 		should_be = ext2_file_type(inode.i_mode);
622*6a54128fSAndroid Build Coastguard Worker 	}
623*6a54128fSAndroid Build Coastguard Worker 	if (filetype == should_be)
624*6a54128fSAndroid Build Coastguard Worker 		return 0;
625*6a54128fSAndroid Build Coastguard Worker 	pctx->num = should_be;
626*6a54128fSAndroid Build Coastguard Worker 
627*6a54128fSAndroid Build Coastguard Worker 	if (fix_problem(ctx, filetype ? PR_2_BAD_FILETYPE : PR_2_SET_FILETYPE,
628*6a54128fSAndroid Build Coastguard Worker 			pctx) == 0)
629*6a54128fSAndroid Build Coastguard Worker 		return 0;
630*6a54128fSAndroid Build Coastguard Worker 
631*6a54128fSAndroid Build Coastguard Worker 	ext2fs_dirent_set_file_type(dirent, should_be);
632*6a54128fSAndroid Build Coastguard Worker 	return 1;
633*6a54128fSAndroid Build Coastguard Worker }
634*6a54128fSAndroid Build Coastguard Worker 
parse_int_node(ext2_filsys fs,struct ext2_db_entry2 * db,struct check_dir_struct * cd,struct dx_dir_info * dx_dir,char * block_buf,int failed_csum)635*6a54128fSAndroid Build Coastguard Worker static void parse_int_node(ext2_filsys fs,
636*6a54128fSAndroid Build Coastguard Worker 			   struct ext2_db_entry2 *db,
637*6a54128fSAndroid Build Coastguard Worker 			   struct check_dir_struct *cd,
638*6a54128fSAndroid Build Coastguard Worker 			   struct dx_dir_info	*dx_dir,
639*6a54128fSAndroid Build Coastguard Worker 			   char *block_buf, int failed_csum)
640*6a54128fSAndroid Build Coastguard Worker {
641*6a54128fSAndroid Build Coastguard Worker 	struct		ext2_dx_root_info  *root;
642*6a54128fSAndroid Build Coastguard Worker 	struct		ext2_dx_entry *ent;
643*6a54128fSAndroid Build Coastguard Worker 	struct		ext2_dx_countlimit *limit;
644*6a54128fSAndroid Build Coastguard Worker 	struct dx_dirblock_info	*dx_db;
645*6a54128fSAndroid Build Coastguard Worker 	int		i, expect_limit, count;
646*6a54128fSAndroid Build Coastguard Worker 	blk_t		blk;
647*6a54128fSAndroid Build Coastguard Worker 	ext2_dirhash_t	min_hash = 0xffffffff;
648*6a54128fSAndroid Build Coastguard Worker 	ext2_dirhash_t	max_hash = 0;
649*6a54128fSAndroid Build Coastguard Worker 	ext2_dirhash_t	hash = 0, prev_hash;
650*6a54128fSAndroid Build Coastguard Worker 	int		csum_size = 0;
651*6a54128fSAndroid Build Coastguard Worker 
652*6a54128fSAndroid Build Coastguard Worker 	if (db->blockcnt == 0) {
653*6a54128fSAndroid Build Coastguard Worker 		root = (struct ext2_dx_root_info *) (block_buf + 24);
654*6a54128fSAndroid Build Coastguard Worker 
655*6a54128fSAndroid Build Coastguard Worker #ifdef DX_DEBUG
656*6a54128fSAndroid Build Coastguard Worker 		printf("Root node dump:\n");
657*6a54128fSAndroid Build Coastguard Worker 		printf("\t Reserved zero: %u\n", root->reserved_zero);
658*6a54128fSAndroid Build Coastguard Worker 		printf("\t Hash Version: %u\n", root->hash_version);
659*6a54128fSAndroid Build Coastguard Worker 		printf("\t Info length: %u\n", root->info_length);
660*6a54128fSAndroid Build Coastguard Worker 		printf("\t Indirect levels: %u\n", root->indirect_levels);
661*6a54128fSAndroid Build Coastguard Worker 		printf("\t Flags: %x\n", root->unused_flags);
662*6a54128fSAndroid Build Coastguard Worker #endif
663*6a54128fSAndroid Build Coastguard Worker 
664*6a54128fSAndroid Build Coastguard Worker 		ent = (struct ext2_dx_entry *) (block_buf + 24 + root->info_length);
665*6a54128fSAndroid Build Coastguard Worker 
666*6a54128fSAndroid Build Coastguard Worker 		if (failed_csum &&
667*6a54128fSAndroid Build Coastguard Worker 		    (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
668*6a54128fSAndroid Build Coastguard Worker 		     fix_problem(cd->ctx, PR_2_HTREE_ROOT_CSUM_INVALID,
669*6a54128fSAndroid Build Coastguard Worker 				&cd->pctx)))
670*6a54128fSAndroid Build Coastguard Worker 			goto clear_and_exit;
671*6a54128fSAndroid Build Coastguard Worker 	} else {
672*6a54128fSAndroid Build Coastguard Worker 		ent = (struct ext2_dx_entry *) (block_buf+8);
673*6a54128fSAndroid Build Coastguard Worker 
674*6a54128fSAndroid Build Coastguard Worker 		if (failed_csum &&
675*6a54128fSAndroid Build Coastguard Worker 		    (e2fsck_dir_will_be_rehashed(cd->ctx, cd->pctx.ino) ||
676*6a54128fSAndroid Build Coastguard Worker 		     fix_problem(cd->ctx, PR_2_HTREE_NODE_CSUM_INVALID,
677*6a54128fSAndroid Build Coastguard Worker 				&cd->pctx)))
678*6a54128fSAndroid Build Coastguard Worker 			goto clear_and_exit;
679*6a54128fSAndroid Build Coastguard Worker 	}
680*6a54128fSAndroid Build Coastguard Worker 
681*6a54128fSAndroid Build Coastguard Worker 	limit = (struct ext2_dx_countlimit *) ent;
682*6a54128fSAndroid Build Coastguard Worker 
683*6a54128fSAndroid Build Coastguard Worker #ifdef DX_DEBUG
684*6a54128fSAndroid Build Coastguard Worker 	printf("Number of entries (count): %d\n",
685*6a54128fSAndroid Build Coastguard Worker 	       ext2fs_le16_to_cpu(limit->count));
686*6a54128fSAndroid Build Coastguard Worker 	printf("Number of entries (limit): %d\n",
687*6a54128fSAndroid Build Coastguard Worker 	       ext2fs_le16_to_cpu(limit->limit));
688*6a54128fSAndroid Build Coastguard Worker #endif
689*6a54128fSAndroid Build Coastguard Worker 
690*6a54128fSAndroid Build Coastguard Worker 	count = ext2fs_le16_to_cpu(limit->count);
691*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_metadata_csum(fs->super))
692*6a54128fSAndroid Build Coastguard Worker 		csum_size = sizeof(struct ext2_dx_tail);
693*6a54128fSAndroid Build Coastguard Worker 	expect_limit = (fs->blocksize -
694*6a54128fSAndroid Build Coastguard Worker 			(csum_size + ((char *) ent - block_buf))) /
695*6a54128fSAndroid Build Coastguard Worker 		       sizeof(struct ext2_dx_entry);
696*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_le16_to_cpu(limit->limit) != expect_limit) {
697*6a54128fSAndroid Build Coastguard Worker 		cd->pctx.num = ext2fs_le16_to_cpu(limit->limit);
698*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(cd->ctx, PR_2_HTREE_BAD_LIMIT, &cd->pctx))
699*6a54128fSAndroid Build Coastguard Worker 			goto clear_and_exit;
700*6a54128fSAndroid Build Coastguard Worker 	}
701*6a54128fSAndroid Build Coastguard Worker 	if (count > expect_limit) {
702*6a54128fSAndroid Build Coastguard Worker 		cd->pctx.num = count;
703*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(cd->ctx, PR_2_HTREE_BAD_COUNT, &cd->pctx))
704*6a54128fSAndroid Build Coastguard Worker 			goto clear_and_exit;
705*6a54128fSAndroid Build Coastguard Worker 		count = expect_limit;
706*6a54128fSAndroid Build Coastguard Worker 	}
707*6a54128fSAndroid Build Coastguard Worker 
708*6a54128fSAndroid Build Coastguard Worker 	for (i=0; i < count; i++) {
709*6a54128fSAndroid Build Coastguard Worker 		prev_hash = hash;
710*6a54128fSAndroid Build Coastguard Worker 		hash = i ? (ext2fs_le32_to_cpu(ent[i].hash) & ~1) : 0;
711*6a54128fSAndroid Build Coastguard Worker #ifdef DX_DEBUG
712*6a54128fSAndroid Build Coastguard Worker 		printf("Entry #%d: Hash 0x%08x, block %u\n", i,
713*6a54128fSAndroid Build Coastguard Worker 		       hash, ext2fs_le32_to_cpu(ent[i].block));
714*6a54128fSAndroid Build Coastguard Worker #endif
715*6a54128fSAndroid Build Coastguard Worker 		blk = ext2fs_le32_to_cpu(ent[i].block) & EXT4_DX_BLOCK_MASK;
716*6a54128fSAndroid Build Coastguard Worker 		/* Check to make sure the block is valid */
717*6a54128fSAndroid Build Coastguard Worker 		if (blk >= dx_dir->numblocks) {
718*6a54128fSAndroid Build Coastguard Worker 			cd->pctx.blk = blk;
719*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(cd->ctx, PR_2_HTREE_BADBLK,
720*6a54128fSAndroid Build Coastguard Worker 					&cd->pctx))
721*6a54128fSAndroid Build Coastguard Worker 				goto clear_and_exit;
722*6a54128fSAndroid Build Coastguard Worker 			continue;
723*6a54128fSAndroid Build Coastguard Worker 		}
724*6a54128fSAndroid Build Coastguard Worker 		if (hash < prev_hash &&
725*6a54128fSAndroid Build Coastguard Worker 		    fix_problem(cd->ctx, PR_2_HTREE_HASH_ORDER, &cd->pctx))
726*6a54128fSAndroid Build Coastguard Worker 			goto clear_and_exit;
727*6a54128fSAndroid Build Coastguard Worker 		dx_db = &dx_dir->dx_block[blk];
728*6a54128fSAndroid Build Coastguard Worker 		if (dx_db->flags & DX_FLAG_REFERENCED) {
729*6a54128fSAndroid Build Coastguard Worker 			dx_db->flags |= DX_FLAG_DUP_REF;
730*6a54128fSAndroid Build Coastguard Worker 		} else {
731*6a54128fSAndroid Build Coastguard Worker 			dx_db->flags |= DX_FLAG_REFERENCED;
732*6a54128fSAndroid Build Coastguard Worker 			dx_db->parent = db->blockcnt;
733*6a54128fSAndroid Build Coastguard Worker 		}
734*6a54128fSAndroid Build Coastguard Worker 
735*6a54128fSAndroid Build Coastguard Worker 		dx_db->previous =
736*6a54128fSAndroid Build Coastguard Worker 			i ? (ext2fs_le32_to_cpu(ent[i-1].block) &
737*6a54128fSAndroid Build Coastguard Worker 			     EXT4_DX_BLOCK_MASK) : 0;
738*6a54128fSAndroid Build Coastguard Worker 
739*6a54128fSAndroid Build Coastguard Worker 		if (hash < min_hash)
740*6a54128fSAndroid Build Coastguard Worker 			min_hash = hash;
741*6a54128fSAndroid Build Coastguard Worker 		if (hash > max_hash)
742*6a54128fSAndroid Build Coastguard Worker 			max_hash = hash;
743*6a54128fSAndroid Build Coastguard Worker 		dx_db->node_min_hash = hash;
744*6a54128fSAndroid Build Coastguard Worker 		if ((i+1) < count)
745*6a54128fSAndroid Build Coastguard Worker 			dx_db->node_max_hash =
746*6a54128fSAndroid Build Coastguard Worker 			  ext2fs_le32_to_cpu(ent[i+1].hash) & ~1;
747*6a54128fSAndroid Build Coastguard Worker 		else {
748*6a54128fSAndroid Build Coastguard Worker 			dx_db->node_max_hash = 0xfffffffe;
749*6a54128fSAndroid Build Coastguard Worker 			dx_db->flags |= DX_FLAG_LAST;
750*6a54128fSAndroid Build Coastguard Worker 		}
751*6a54128fSAndroid Build Coastguard Worker 		if (i == 0)
752*6a54128fSAndroid Build Coastguard Worker 			dx_db->flags |= DX_FLAG_FIRST;
753*6a54128fSAndroid Build Coastguard Worker 	}
754*6a54128fSAndroid Build Coastguard Worker #ifdef DX_DEBUG
755*6a54128fSAndroid Build Coastguard Worker 	printf("Blockcnt = %d, min hash 0x%08x, max hash 0x%08x\n",
756*6a54128fSAndroid Build Coastguard Worker 	       db->blockcnt, min_hash, max_hash);
757*6a54128fSAndroid Build Coastguard Worker #endif
758*6a54128fSAndroid Build Coastguard Worker 	dx_db = &dx_dir->dx_block[db->blockcnt];
759*6a54128fSAndroid Build Coastguard Worker 	dx_db->min_hash = min_hash;
760*6a54128fSAndroid Build Coastguard Worker 	dx_db->max_hash = max_hash;
761*6a54128fSAndroid Build Coastguard Worker 	return;
762*6a54128fSAndroid Build Coastguard Worker 
763*6a54128fSAndroid Build Coastguard Worker clear_and_exit:
764*6a54128fSAndroid Build Coastguard Worker 	clear_htree(cd->ctx, cd->pctx.ino);
765*6a54128fSAndroid Build Coastguard Worker 	dx_dir->numblocks = 0;
766*6a54128fSAndroid Build Coastguard Worker 	e2fsck_rehash_dir_later(cd->ctx, cd->pctx.ino);
767*6a54128fSAndroid Build Coastguard Worker }
768*6a54128fSAndroid Build Coastguard Worker 
769*6a54128fSAndroid Build Coastguard Worker /*
770*6a54128fSAndroid Build Coastguard Worker  * Given a busted directory, try to salvage it somehow.
771*6a54128fSAndroid Build Coastguard Worker  *
772*6a54128fSAndroid Build Coastguard Worker  */
salvage_directory(ext2_filsys fs,struct ext2_dir_entry * dirent,struct ext2_dir_entry * prev,unsigned int * offset,unsigned int block_len,int hash_in_dirent)773*6a54128fSAndroid Build Coastguard Worker static void salvage_directory(ext2_filsys fs,
774*6a54128fSAndroid Build Coastguard Worker 			      struct ext2_dir_entry *dirent,
775*6a54128fSAndroid Build Coastguard Worker 			      struct ext2_dir_entry *prev,
776*6a54128fSAndroid Build Coastguard Worker 			      unsigned int *offset,
777*6a54128fSAndroid Build Coastguard Worker 			      unsigned int block_len,
778*6a54128fSAndroid Build Coastguard Worker 			      int hash_in_dirent)
779*6a54128fSAndroid Build Coastguard Worker {
780*6a54128fSAndroid Build Coastguard Worker 	char	*cp = (char *) dirent;
781*6a54128fSAndroid Build Coastguard Worker 	int left;
782*6a54128fSAndroid Build Coastguard Worker 	unsigned int rec_len, prev_rec_len;
783*6a54128fSAndroid Build Coastguard Worker 	unsigned int name_len;
784*6a54128fSAndroid Build Coastguard Worker 
785*6a54128fSAndroid Build Coastguard Worker 	/*
786*6a54128fSAndroid Build Coastguard Worker 	 * If the space left for the entry is too small to be an entry,
787*6a54128fSAndroid Build Coastguard Worker 	 * we can't access dirent's fields, so plumb in the values needed
788*6a54128fSAndroid Build Coastguard Worker 	 * so that the previous entry absorbs this one.
789*6a54128fSAndroid Build Coastguard Worker 	 */
790*6a54128fSAndroid Build Coastguard Worker 	if (block_len - *offset < EXT2_DIR_ENTRY_HEADER_LEN) {
791*6a54128fSAndroid Build Coastguard Worker 		name_len = 0;
792*6a54128fSAndroid Build Coastguard Worker 		rec_len = block_len - *offset;
793*6a54128fSAndroid Build Coastguard Worker 	} else {
794*6a54128fSAndroid Build Coastguard Worker 		name_len = ext2fs_dirent_name_len(dirent);
795*6a54128fSAndroid Build Coastguard Worker 		(void) ext2fs_get_rec_len(fs, dirent, &rec_len);
796*6a54128fSAndroid Build Coastguard Worker 	}
797*6a54128fSAndroid Build Coastguard Worker 	left = block_len - *offset - rec_len;
798*6a54128fSAndroid Build Coastguard Worker 
799*6a54128fSAndroid Build Coastguard Worker 	/*
800*6a54128fSAndroid Build Coastguard Worker 	 * Special case of directory entry of size 8: copy what's left
801*6a54128fSAndroid Build Coastguard Worker 	 * of the directory block up to cover up the invalid hole.
802*6a54128fSAndroid Build Coastguard Worker 	 */
803*6a54128fSAndroid Build Coastguard Worker 	if ((left >= (int) ext2fs_dir_rec_len(1, hash_in_dirent)) &&
804*6a54128fSAndroid Build Coastguard Worker 	     (rec_len == EXT2_DIR_ENTRY_HEADER_LEN)) {
805*6a54128fSAndroid Build Coastguard Worker 		memmove(cp, cp+EXT2_DIR_ENTRY_HEADER_LEN, left);
806*6a54128fSAndroid Build Coastguard Worker 		memset(cp + left, 0, EXT2_DIR_ENTRY_HEADER_LEN);
807*6a54128fSAndroid Build Coastguard Worker 		return;
808*6a54128fSAndroid Build Coastguard Worker 	}
809*6a54128fSAndroid Build Coastguard Worker 	/*
810*6a54128fSAndroid Build Coastguard Worker 	 * If the directory entry overruns the end of the directory
811*6a54128fSAndroid Build Coastguard Worker 	 * block, and the name is small enough to fit, then adjust the
812*6a54128fSAndroid Build Coastguard Worker 	 * record length.
813*6a54128fSAndroid Build Coastguard Worker 	 */
814*6a54128fSAndroid Build Coastguard Worker 	if ((left < 0) &&
815*6a54128fSAndroid Build Coastguard Worker 	    ((int) rec_len + left > EXT2_DIR_ENTRY_HEADER_LEN) &&
816*6a54128fSAndroid Build Coastguard Worker 	    ((int) ext2fs_dir_rec_len(name_len, hash_in_dirent) <= (int) rec_len + left) &&
817*6a54128fSAndroid Build Coastguard Worker 	    dirent->inode <= fs->super->s_inodes_count &&
818*6a54128fSAndroid Build Coastguard Worker 	    strnlen(dirent->name, name_len) == name_len) {
819*6a54128fSAndroid Build Coastguard Worker 		(void) ext2fs_set_rec_len(fs, (int) rec_len + left, dirent);
820*6a54128fSAndroid Build Coastguard Worker 		return;
821*6a54128fSAndroid Build Coastguard Worker 	}
822*6a54128fSAndroid Build Coastguard Worker 	/*
823*6a54128fSAndroid Build Coastguard Worker 	 * If the record length of the directory entry is a multiple
824*6a54128fSAndroid Build Coastguard Worker 	 * of four, and not too big, such that it is valid, let the
825*6a54128fSAndroid Build Coastguard Worker 	 * previous directory entry absorb the invalid one.
826*6a54128fSAndroid Build Coastguard Worker 	 */
827*6a54128fSAndroid Build Coastguard Worker 	if (prev && rec_len && (rec_len % 4) == 0 &&
828*6a54128fSAndroid Build Coastguard Worker 	    (*offset + rec_len <= block_len)) {
829*6a54128fSAndroid Build Coastguard Worker 		(void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
830*6a54128fSAndroid Build Coastguard Worker 		prev_rec_len += rec_len;
831*6a54128fSAndroid Build Coastguard Worker 		(void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
832*6a54128fSAndroid Build Coastguard Worker 		*offset += rec_len;
833*6a54128fSAndroid Build Coastguard Worker 		return;
834*6a54128fSAndroid Build Coastguard Worker 	}
835*6a54128fSAndroid Build Coastguard Worker 	/*
836*6a54128fSAndroid Build Coastguard Worker 	 * Default salvage method --- kill all of the directory
837*6a54128fSAndroid Build Coastguard Worker 	 * entries for the rest of the block.  We will either try to
838*6a54128fSAndroid Build Coastguard Worker 	 * absorb it into the previous directory entry, or create a
839*6a54128fSAndroid Build Coastguard Worker 	 * new empty directory entry the rest of the directory block.
840*6a54128fSAndroid Build Coastguard Worker 	 */
841*6a54128fSAndroid Build Coastguard Worker 	if (prev) {
842*6a54128fSAndroid Build Coastguard Worker 		(void) ext2fs_get_rec_len(fs, prev, &prev_rec_len);
843*6a54128fSAndroid Build Coastguard Worker 		prev_rec_len += block_len - *offset;
844*6a54128fSAndroid Build Coastguard Worker 		(void) ext2fs_set_rec_len(fs, prev_rec_len, prev);
845*6a54128fSAndroid Build Coastguard Worker 		*offset = fs->blocksize;
846*6a54128fSAndroid Build Coastguard Worker 	} else {
847*6a54128fSAndroid Build Coastguard Worker 		rec_len = block_len - *offset;
848*6a54128fSAndroid Build Coastguard Worker 		(void) ext2fs_set_rec_len(fs, rec_len, dirent);
849*6a54128fSAndroid Build Coastguard Worker 		ext2fs_dirent_set_name_len(dirent, 0);
850*6a54128fSAndroid Build Coastguard Worker 		ext2fs_dirent_set_file_type(dirent, EXT2_FT_UNKNOWN);
851*6a54128fSAndroid Build Coastguard Worker 		dirent->inode = 0;
852*6a54128fSAndroid Build Coastguard Worker 	}
853*6a54128fSAndroid Build Coastguard Worker }
854*6a54128fSAndroid Build Coastguard Worker 
855*6a54128fSAndroid Build Coastguard Worker #define NEXT_DIRENT(d)	((void *)((char *)(d) + (d)->rec_len))
insert_dirent_tail(ext2_filsys fs,void * dirbuf)856*6a54128fSAndroid Build Coastguard Worker static errcode_t insert_dirent_tail(ext2_filsys fs, void *dirbuf)
857*6a54128fSAndroid Build Coastguard Worker {
858*6a54128fSAndroid Build Coastguard Worker 	struct ext2_dir_entry *d;
859*6a54128fSAndroid Build Coastguard Worker 	void *top;
860*6a54128fSAndroid Build Coastguard Worker 	struct ext2_dir_entry_tail *t;
861*6a54128fSAndroid Build Coastguard Worker 
862*6a54128fSAndroid Build Coastguard Worker 	d = dirbuf;
863*6a54128fSAndroid Build Coastguard Worker 	top = EXT2_DIRENT_TAIL(dirbuf, fs->blocksize);
864*6a54128fSAndroid Build Coastguard Worker 
865*6a54128fSAndroid Build Coastguard Worker 	while (d->rec_len && !(d->rec_len & 0x3) && NEXT_DIRENT(d) <= top)
866*6a54128fSAndroid Build Coastguard Worker 		d = NEXT_DIRENT(d);
867*6a54128fSAndroid Build Coastguard Worker 
868*6a54128fSAndroid Build Coastguard Worker 	if (d != top) {
869*6a54128fSAndroid Build Coastguard Worker 		unsigned int min_size = EXT2_DIR_REC_LEN(
870*6a54128fSAndroid Build Coastguard Worker 				ext2fs_dirent_name_len(dirbuf));
871*6a54128fSAndroid Build Coastguard Worker 		if (min_size > (char *)top - (char *)d)
872*6a54128fSAndroid Build Coastguard Worker 			return EXT2_ET_DIR_NO_SPACE_FOR_CSUM;
873*6a54128fSAndroid Build Coastguard Worker 		d->rec_len = (char *)top - (char *)d;
874*6a54128fSAndroid Build Coastguard Worker 	}
875*6a54128fSAndroid Build Coastguard Worker 
876*6a54128fSAndroid Build Coastguard Worker 	t = (struct ext2_dir_entry_tail *)top;
877*6a54128fSAndroid Build Coastguard Worker 	if (t->det_reserved_zero1 ||
878*6a54128fSAndroid Build Coastguard Worker 	    t->det_rec_len != sizeof(struct ext2_dir_entry_tail) ||
879*6a54128fSAndroid Build Coastguard Worker 	    t->det_reserved_name_len != EXT2_DIR_NAME_LEN_CSUM)
880*6a54128fSAndroid Build Coastguard Worker 		ext2fs_initialize_dirent_tail(fs, t);
881*6a54128fSAndroid Build Coastguard Worker 
882*6a54128fSAndroid Build Coastguard Worker 	return 0;
883*6a54128fSAndroid Build Coastguard Worker }
884*6a54128fSAndroid Build Coastguard Worker #undef NEXT_DIRENT
885*6a54128fSAndroid Build Coastguard Worker 
fix_inline_dir_size(e2fsck_t ctx,ext2_ino_t ino,size_t * inline_data_size,struct problem_context * pctx,char * buf)886*6a54128fSAndroid Build Coastguard Worker static errcode_t fix_inline_dir_size(e2fsck_t ctx, ext2_ino_t ino,
887*6a54128fSAndroid Build Coastguard Worker 				     size_t *inline_data_size,
888*6a54128fSAndroid Build Coastguard Worker 				     struct problem_context *pctx,
889*6a54128fSAndroid Build Coastguard Worker 				     char *buf)
890*6a54128fSAndroid Build Coastguard Worker {
891*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
892*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode inode;
893*6a54128fSAndroid Build Coastguard Worker 	size_t new_size, old_size;
894*6a54128fSAndroid Build Coastguard Worker 	errcode_t retval;
895*6a54128fSAndroid Build Coastguard Worker 
896*6a54128fSAndroid Build Coastguard Worker 	old_size = *inline_data_size;
897*6a54128fSAndroid Build Coastguard Worker 	/*
898*6a54128fSAndroid Build Coastguard Worker 	 * If there's not enough bytes to start the "second" dir block
899*6a54128fSAndroid Build Coastguard Worker 	 * (in the EA space) then truncate everything to the first block.
900*6a54128fSAndroid Build Coastguard Worker 	 */
901*6a54128fSAndroid Build Coastguard Worker 	if (old_size > EXT4_MIN_INLINE_DATA_SIZE &&
902*6a54128fSAndroid Build Coastguard Worker 	    old_size < EXT4_MIN_INLINE_DATA_SIZE +
903*6a54128fSAndroid Build Coastguard Worker 		       EXT2_DIR_REC_LEN(1)) {
904*6a54128fSAndroid Build Coastguard Worker 		old_size = EXT4_MIN_INLINE_DATA_SIZE;
905*6a54128fSAndroid Build Coastguard Worker 		new_size = old_size;
906*6a54128fSAndroid Build Coastguard Worker 	} else
907*6a54128fSAndroid Build Coastguard Worker 		/* Increase to the next four-byte boundary for salvaging */
908*6a54128fSAndroid Build Coastguard Worker 		new_size = old_size + (4 - (old_size & 3));
909*6a54128fSAndroid Build Coastguard Worker 	memset(buf + old_size, 0, new_size - old_size);
910*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
911*6a54128fSAndroid Build Coastguard Worker 	if (retval == EXT2_ET_INLINE_DATA_NO_SPACE) {
912*6a54128fSAndroid Build Coastguard Worker 		/* Or we can't, so truncate. */
913*6a54128fSAndroid Build Coastguard Worker 		new_size -= 4;
914*6a54128fSAndroid Build Coastguard Worker 		retval = ext2fs_inline_data_set(fs, ino, 0, buf, new_size);
915*6a54128fSAndroid Build Coastguard Worker 		if (retval) {
916*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
917*6a54128fSAndroid Build Coastguard Worker 					pctx)) {
918*6a54128fSAndroid Build Coastguard Worker 				new_size = 0;
919*6a54128fSAndroid Build Coastguard Worker 				goto write_inode;
920*6a54128fSAndroid Build Coastguard Worker 			}
921*6a54128fSAndroid Build Coastguard Worker 			goto err;
922*6a54128fSAndroid Build Coastguard Worker 		}
923*6a54128fSAndroid Build Coastguard Worker 	} else if (retval) {
924*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_FIX_INLINE_DIR_FAILED,
925*6a54128fSAndroid Build Coastguard Worker 				pctx)) {
926*6a54128fSAndroid Build Coastguard Worker 			new_size = 0;
927*6a54128fSAndroid Build Coastguard Worker 			goto write_inode;
928*6a54128fSAndroid Build Coastguard Worker 		}
929*6a54128fSAndroid Build Coastguard Worker 		goto err;
930*6a54128fSAndroid Build Coastguard Worker 	}
931*6a54128fSAndroid Build Coastguard Worker 
932*6a54128fSAndroid Build Coastguard Worker write_inode:
933*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_read_inode(fs, ino, &inode);
934*6a54128fSAndroid Build Coastguard Worker 	if (retval)
935*6a54128fSAndroid Build Coastguard Worker 		goto err;
936*6a54128fSAndroid Build Coastguard Worker 
937*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_inode_size_set(fs, &inode, new_size);
938*6a54128fSAndroid Build Coastguard Worker 	if (retval)
939*6a54128fSAndroid Build Coastguard Worker 		goto err;
940*6a54128fSAndroid Build Coastguard Worker 	if (new_size == 0)
941*6a54128fSAndroid Build Coastguard Worker 		inode.i_flags &= ~EXT4_INLINE_DATA_FL;
942*6a54128fSAndroid Build Coastguard Worker 	retval = ext2fs_write_inode(fs, ino, &inode);
943*6a54128fSAndroid Build Coastguard Worker 	if (retval)
944*6a54128fSAndroid Build Coastguard Worker 		goto err;
945*6a54128fSAndroid Build Coastguard Worker 	*inline_data_size = new_size;
946*6a54128fSAndroid Build Coastguard Worker 
947*6a54128fSAndroid Build Coastguard Worker err:
948*6a54128fSAndroid Build Coastguard Worker 	return retval;
949*6a54128fSAndroid Build Coastguard Worker }
950*6a54128fSAndroid Build Coastguard Worker 
951*6a54128fSAndroid Build Coastguard Worker /* Return true if this type of file needs encryption */
needs_encryption(e2fsck_t ctx,const struct ext2_dir_entry * dirent)952*6a54128fSAndroid Build Coastguard Worker static int needs_encryption(e2fsck_t ctx, const struct ext2_dir_entry *dirent)
953*6a54128fSAndroid Build Coastguard Worker {
954*6a54128fSAndroid Build Coastguard Worker 	int filetype = ext2fs_dirent_file_type(dirent);
955*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t ino = dirent->inode;
956*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode inode;
957*6a54128fSAndroid Build Coastguard Worker 
958*6a54128fSAndroid Build Coastguard Worker 	if (filetype != EXT2_FT_UNKNOWN)
959*6a54128fSAndroid Build Coastguard Worker 		return filetype == EXT2_FT_REG_FILE ||
960*6a54128fSAndroid Build Coastguard Worker 		       filetype == EXT2_FT_DIR ||
961*6a54128fSAndroid Build Coastguard Worker 		       filetype == EXT2_FT_SYMLINK;
962*6a54128fSAndroid Build Coastguard Worker 
963*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_test_inode_bitmap2(ctx->inode_reg_map, ino) ||
964*6a54128fSAndroid Build Coastguard Worker 	    ext2fs_test_inode_bitmap2(ctx->inode_dir_map, ino))
965*6a54128fSAndroid Build Coastguard Worker 		return 1;
966*6a54128fSAndroid Build Coastguard Worker 
967*6a54128fSAndroid Build Coastguard Worker 	e2fsck_read_inode(ctx, ino, &inode, "check_encryption_policy");
968*6a54128fSAndroid Build Coastguard Worker 	return LINUX_S_ISREG(inode.i_mode) ||
969*6a54128fSAndroid Build Coastguard Worker 	       LINUX_S_ISDIR(inode.i_mode) ||
970*6a54128fSAndroid Build Coastguard Worker 	       LINUX_S_ISLNK(inode.i_mode);
971*6a54128fSAndroid Build Coastguard Worker }
972*6a54128fSAndroid Build Coastguard Worker 
973*6a54128fSAndroid Build Coastguard Worker /*
974*6a54128fSAndroid Build Coastguard Worker  * All regular files, directories, and symlinks in encrypted directories must be
975*6a54128fSAndroid Build Coastguard Worker  * encrypted using the same encryption policy as their directory.
976*6a54128fSAndroid Build Coastguard Worker  *
977*6a54128fSAndroid Build Coastguard Worker  * Returns 1 if the dirent should be cleared, otherwise 0.
978*6a54128fSAndroid Build Coastguard Worker  */
check_encryption_policy(e2fsck_t ctx,const struct ext2_dir_entry * dirent,__u32 dir_encpolicy_id,struct problem_context * pctx)979*6a54128fSAndroid Build Coastguard Worker static int check_encryption_policy(e2fsck_t ctx,
980*6a54128fSAndroid Build Coastguard Worker 				   const struct ext2_dir_entry *dirent,
981*6a54128fSAndroid Build Coastguard Worker 				   __u32 dir_encpolicy_id,
982*6a54128fSAndroid Build Coastguard Worker 				   struct problem_context *pctx)
983*6a54128fSAndroid Build Coastguard Worker {
984*6a54128fSAndroid Build Coastguard Worker 	__u32 file_encpolicy_id = find_encryption_policy(ctx, dirent->inode);
985*6a54128fSAndroid Build Coastguard Worker 
986*6a54128fSAndroid Build Coastguard Worker 	/* Same policy or both UNRECOGNIZED_ENCRYPTION_POLICY? */
987*6a54128fSAndroid Build Coastguard Worker 	if (file_encpolicy_id == dir_encpolicy_id)
988*6a54128fSAndroid Build Coastguard Worker 		return 0;
989*6a54128fSAndroid Build Coastguard Worker 
990*6a54128fSAndroid Build Coastguard Worker 	if (file_encpolicy_id == NO_ENCRYPTION_POLICY) {
991*6a54128fSAndroid Build Coastguard Worker 		if (!needs_encryption(ctx, dirent))
992*6a54128fSAndroid Build Coastguard Worker 			return 0;
993*6a54128fSAndroid Build Coastguard Worker 		return fix_problem(ctx, PR_2_UNENCRYPTED_FILE, pctx);
994*6a54128fSAndroid Build Coastguard Worker 	}
995*6a54128fSAndroid Build Coastguard Worker 
996*6a54128fSAndroid Build Coastguard Worker 	return fix_problem(ctx, PR_2_INCONSISTENT_ENCRYPTION_POLICY, pctx);
997*6a54128fSAndroid Build Coastguard Worker }
998*6a54128fSAndroid Build Coastguard Worker 
999*6a54128fSAndroid Build Coastguard Worker /*
1000*6a54128fSAndroid Build Coastguard Worker  * Check an encrypted directory entry.
1001*6a54128fSAndroid Build Coastguard Worker  *
1002*6a54128fSAndroid Build Coastguard Worker  * Returns 1 if the dirent should be cleared, otherwise 0.
1003*6a54128fSAndroid Build Coastguard Worker  */
check_encrypted_dirent(e2fsck_t ctx,const struct ext2_dir_entry * dirent,__u32 dir_encpolicy_id,struct problem_context * pctx)1004*6a54128fSAndroid Build Coastguard Worker static int check_encrypted_dirent(e2fsck_t ctx,
1005*6a54128fSAndroid Build Coastguard Worker 				  const struct ext2_dir_entry *dirent,
1006*6a54128fSAndroid Build Coastguard Worker 				  __u32 dir_encpolicy_id,
1007*6a54128fSAndroid Build Coastguard Worker 				  struct problem_context *pctx)
1008*6a54128fSAndroid Build Coastguard Worker {
1009*6a54128fSAndroid Build Coastguard Worker 	if (encrypted_check_name(ctx, dirent, pctx))
1010*6a54128fSAndroid Build Coastguard Worker 		return 1;
1011*6a54128fSAndroid Build Coastguard Worker 	if (check_encryption_policy(ctx, dirent, dir_encpolicy_id, pctx))
1012*6a54128fSAndroid Build Coastguard Worker 		return 1;
1013*6a54128fSAndroid Build Coastguard Worker 	return 0;
1014*6a54128fSAndroid Build Coastguard Worker }
1015*6a54128fSAndroid Build Coastguard Worker 
check_dir_block2(ext2_filsys fs,struct ext2_db_entry2 * db,void * priv_data)1016*6a54128fSAndroid Build Coastguard Worker static int check_dir_block2(ext2_filsys fs,
1017*6a54128fSAndroid Build Coastguard Worker 			   struct ext2_db_entry2 *db,
1018*6a54128fSAndroid Build Coastguard Worker 			   void *priv_data)
1019*6a54128fSAndroid Build Coastguard Worker {
1020*6a54128fSAndroid Build Coastguard Worker 	int err;
1021*6a54128fSAndroid Build Coastguard Worker 	struct check_dir_struct *cd = priv_data;
1022*6a54128fSAndroid Build Coastguard Worker 
1023*6a54128fSAndroid Build Coastguard Worker 	if (cd->ra_entries && cd->list_offset >= cd->next_ra_off) {
1024*6a54128fSAndroid Build Coastguard Worker 		err = e2fsck_readahead_dblist(fs,
1025*6a54128fSAndroid Build Coastguard Worker 					E2FSCK_RA_DBLIST_IGNORE_BLOCKCNT,
1026*6a54128fSAndroid Build Coastguard Worker 					fs->dblist,
1027*6a54128fSAndroid Build Coastguard Worker 					cd->list_offset + cd->ra_entries / 8,
1028*6a54128fSAndroid Build Coastguard Worker 					cd->ra_entries);
1029*6a54128fSAndroid Build Coastguard Worker 		if (err)
1030*6a54128fSAndroid Build Coastguard Worker 			cd->ra_entries = 0;
1031*6a54128fSAndroid Build Coastguard Worker 		cd->next_ra_off = cd->list_offset + (cd->ra_entries * 7 / 8);
1032*6a54128fSAndroid Build Coastguard Worker 	}
1033*6a54128fSAndroid Build Coastguard Worker 
1034*6a54128fSAndroid Build Coastguard Worker 	err = check_dir_block(fs, db, priv_data);
1035*6a54128fSAndroid Build Coastguard Worker 	cd->list_offset++;
1036*6a54128fSAndroid Build Coastguard Worker 	return err;
1037*6a54128fSAndroid Build Coastguard Worker }
1038*6a54128fSAndroid Build Coastguard Worker 
check_dir_block(ext2_filsys fs,struct ext2_db_entry2 * db,void * priv_data)1039*6a54128fSAndroid Build Coastguard Worker static int check_dir_block(ext2_filsys fs,
1040*6a54128fSAndroid Build Coastguard Worker 			   struct ext2_db_entry2 *db,
1041*6a54128fSAndroid Build Coastguard Worker 			   void *priv_data)
1042*6a54128fSAndroid Build Coastguard Worker {
1043*6a54128fSAndroid Build Coastguard Worker  	struct dx_dir_info	*dx_dir;
1044*6a54128fSAndroid Build Coastguard Worker 	struct dx_dirblock_info	*dx_db = 0;
1045*6a54128fSAndroid Build Coastguard Worker 	struct ext2_dir_entry 	*dirent, *prev, dot, dotdot;
1046*6a54128fSAndroid Build Coastguard Worker 	ext2_dirhash_t		hash;
1047*6a54128fSAndroid Build Coastguard Worker 	unsigned int		offset = 0;
1048*6a54128fSAndroid Build Coastguard Worker 	int			dir_modified = 0;
1049*6a54128fSAndroid Build Coastguard Worker 	int			dot_state;
1050*6a54128fSAndroid Build Coastguard Worker 	unsigned int		rec_len;
1051*6a54128fSAndroid Build Coastguard Worker 	blk64_t			block_nr = db->blk;
1052*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t 		ino = db->ino;
1053*6a54128fSAndroid Build Coastguard Worker 	ext2_ino_t 		subdir_parent;
1054*6a54128fSAndroid Build Coastguard Worker 	__u16			links;
1055*6a54128fSAndroid Build Coastguard Worker 	struct check_dir_struct	*cd;
1056*6a54128fSAndroid Build Coastguard Worker 	char			*buf, *ibuf;
1057*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t		ctx;
1058*6a54128fSAndroid Build Coastguard Worker 	problem_t		problem;
1059*6a54128fSAndroid Build Coastguard Worker 	struct ext2_dx_root_info *root;
1060*6a54128fSAndroid Build Coastguard Worker 	struct ext2_dx_countlimit *limit;
1061*6a54128fSAndroid Build Coastguard Worker 	static dict_t de_dict;
1062*6a54128fSAndroid Build Coastguard Worker 	struct problem_context	pctx;
1063*6a54128fSAndroid Build Coastguard Worker 	int	dups_found = 0;
1064*6a54128fSAndroid Build Coastguard Worker 	int	ret;
1065*6a54128fSAndroid Build Coastguard Worker 	int	dx_csum_size = 0, de_csum_size = 0;
1066*6a54128fSAndroid Build Coastguard Worker 	int	failed_csum = 0;
1067*6a54128fSAndroid Build Coastguard Worker 	int	is_leaf = 1;
1068*6a54128fSAndroid Build Coastguard Worker 	size_t	inline_data_size = 0;
1069*6a54128fSAndroid Build Coastguard Worker 	int	filetype = 0;
1070*6a54128fSAndroid Build Coastguard Worker 	__u32   dir_encpolicy_id = NO_ENCRYPTION_POLICY;
1071*6a54128fSAndroid Build Coastguard Worker 	int	hash_in_dirent = 0;
1072*6a54128fSAndroid Build Coastguard Worker 	int	casefolded = 0;
1073*6a54128fSAndroid Build Coastguard Worker 	size_t	max_block_size;
1074*6a54128fSAndroid Build Coastguard Worker 	int	hash_flags = 0;
1075*6a54128fSAndroid Build Coastguard Worker 	static char *eop_read_dirblock = NULL;
1076*6a54128fSAndroid Build Coastguard Worker 	int cf_dir = 0;
1077*6a54128fSAndroid Build Coastguard Worker 
1078*6a54128fSAndroid Build Coastguard Worker 	cd = (struct check_dir_struct *) priv_data;
1079*6a54128fSAndroid Build Coastguard Worker 	ibuf = buf = cd->buf;
1080*6a54128fSAndroid Build Coastguard Worker 	ctx = cd->ctx;
1081*6a54128fSAndroid Build Coastguard Worker 
1082*6a54128fSAndroid Build Coastguard Worker 	/* We only want filename encoding verification on strict
1083*6a54128fSAndroid Build Coastguard Worker 	 * mode or if explicitly requested by user. */
1084*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_test_inode_bitmap2(ctx->inode_casefold_map, ino) &&
1085*6a54128fSAndroid Build Coastguard Worker 	    ((ctx->fs->super->s_encoding_flags & EXT4_ENC_STRICT_MODE_FL) ||
1086*6a54128fSAndroid Build Coastguard Worker 	     (ctx->options & E2F_OPT_CHECK_ENCODING)))
1087*6a54128fSAndroid Build Coastguard Worker 		cf_dir = 1;
1088*6a54128fSAndroid Build Coastguard Worker 
1089*6a54128fSAndroid Build Coastguard Worker 	if (ctx->flags & E2F_FLAG_RUN_RETURN)
1090*6a54128fSAndroid Build Coastguard Worker 		return DIRENT_ABORT;
1091*6a54128fSAndroid Build Coastguard Worker 
1092*6a54128fSAndroid Build Coastguard Worker 	if (ctx->progress && (ctx->progress)(ctx, 2, cd->count++, cd->max))
1093*6a54128fSAndroid Build Coastguard Worker 		return DIRENT_ABORT;
1094*6a54128fSAndroid Build Coastguard Worker 
1095*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_metadata_csum(fs->super)) {
1096*6a54128fSAndroid Build Coastguard Worker 		dx_csum_size = sizeof(struct ext2_dx_tail);
1097*6a54128fSAndroid Build Coastguard Worker 		de_csum_size = sizeof(struct ext2_dir_entry_tail);
1098*6a54128fSAndroid Build Coastguard Worker 	}
1099*6a54128fSAndroid Build Coastguard Worker 
1100*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_filetype(fs->super))
1101*6a54128fSAndroid Build Coastguard Worker 		filetype = EXT2_FT_DIR << 8;
1102*6a54128fSAndroid Build Coastguard Worker 
1103*6a54128fSAndroid Build Coastguard Worker 	/*
1104*6a54128fSAndroid Build Coastguard Worker 	 * Make sure the inode is still in use (could have been
1105*6a54128fSAndroid Build Coastguard Worker 	 * deleted in the duplicate/bad blocks pass.
1106*6a54128fSAndroid Build Coastguard Worker 	 */
1107*6a54128fSAndroid Build Coastguard Worker 	if (!(ext2fs_test_inode_bitmap2(ctx->inode_used_map, ino)))
1108*6a54128fSAndroid Build Coastguard Worker 		return 0;
1109*6a54128fSAndroid Build Coastguard Worker 
1110*6a54128fSAndroid Build Coastguard Worker 	cd->pctx.ino = ino;
1111*6a54128fSAndroid Build Coastguard Worker 	cd->pctx.blk = block_nr;
1112*6a54128fSAndroid Build Coastguard Worker 	cd->pctx.blkcount = db->blockcnt;
1113*6a54128fSAndroid Build Coastguard Worker 	cd->pctx.ino2 = 0;
1114*6a54128fSAndroid Build Coastguard Worker 	cd->pctx.dirent = 0;
1115*6a54128fSAndroid Build Coastguard Worker 	cd->pctx.num = 0;
1116*6a54128fSAndroid Build Coastguard Worker 
1117*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_has_feature_inline_data(fs->super)) {
1118*6a54128fSAndroid Build Coastguard Worker 		errcode_t ec;
1119*6a54128fSAndroid Build Coastguard Worker 
1120*6a54128fSAndroid Build Coastguard Worker 		ec = ext2fs_inline_data_size(fs, ino, &inline_data_size);
1121*6a54128fSAndroid Build Coastguard Worker 		if (ec && ec != EXT2_ET_NO_INLINE_DATA)
1122*6a54128fSAndroid Build Coastguard Worker 			return DIRENT_ABORT;
1123*6a54128fSAndroid Build Coastguard Worker 	}
1124*6a54128fSAndroid Build Coastguard Worker 
1125*6a54128fSAndroid Build Coastguard Worker 	/* This will allow (at some point in the future) to punch out empty
1126*6a54128fSAndroid Build Coastguard Worker 	 * directory blocks and reduce the space used by a directory that grows
1127*6a54128fSAndroid Build Coastguard Worker 	 * very large and then the files are deleted. For now, all that is
1128*6a54128fSAndroid Build Coastguard Worker 	 * needed is to avoid e2fsck filling in these holes as part of
1129*6a54128fSAndroid Build Coastguard Worker 	 * feature flag. */
1130*6a54128fSAndroid Build Coastguard Worker 	if (db->blk == 0 && ext2fs_has_feature_largedir(fs->super) &&
1131*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_has_feature_inline_data(fs->super))
1132*6a54128fSAndroid Build Coastguard Worker 		return 0;
1133*6a54128fSAndroid Build Coastguard Worker 
1134*6a54128fSAndroid Build Coastguard Worker 	if (db->blk == 0 && !inline_data_size) {
1135*6a54128fSAndroid Build Coastguard Worker 		if (allocate_dir_block(ctx, db, buf, &cd->pctx))
1136*6a54128fSAndroid Build Coastguard Worker 			return 0;
1137*6a54128fSAndroid Build Coastguard Worker 		block_nr = db->blk;
1138*6a54128fSAndroid Build Coastguard Worker 	}
1139*6a54128fSAndroid Build Coastguard Worker 
1140*6a54128fSAndroid Build Coastguard Worker 	if (db->blockcnt)
1141*6a54128fSAndroid Build Coastguard Worker 		dot_state = 2;
1142*6a54128fSAndroid Build Coastguard Worker 	else
1143*6a54128fSAndroid Build Coastguard Worker 		dot_state = 0;
1144*6a54128fSAndroid Build Coastguard Worker 
1145*6a54128fSAndroid Build Coastguard Worker 	if (ctx->dirs_to_hash &&
1146*6a54128fSAndroid Build Coastguard Worker 	    ext2fs_u32_list_test(ctx->dirs_to_hash, ino))
1147*6a54128fSAndroid Build Coastguard Worker 		dups_found++;
1148*6a54128fSAndroid Build Coastguard Worker 
1149*6a54128fSAndroid Build Coastguard Worker #if 0
1150*6a54128fSAndroid Build Coastguard Worker 	printf("In process_dir_block block %lu, #%d, inode %lu\n", block_nr,
1151*6a54128fSAndroid Build Coastguard Worker 	       db->blockcnt, ino);
1152*6a54128fSAndroid Build Coastguard Worker #endif
1153*6a54128fSAndroid Build Coastguard Worker 
1154*6a54128fSAndroid Build Coastguard Worker 	if (!eop_read_dirblock)
1155*6a54128fSAndroid Build Coastguard Worker 		eop_read_dirblock = (char *) _("reading directory block");
1156*6a54128fSAndroid Build Coastguard Worker 	ehandler_operation(eop_read_dirblock);
1157*6a54128fSAndroid Build Coastguard Worker 	if (inline_data_size) {
1158*6a54128fSAndroid Build Coastguard Worker 		memset(buf, 0, fs->blocksize - inline_data_size);
1159*6a54128fSAndroid Build Coastguard Worker 		cd->pctx.errcode = ext2fs_inline_data_get(fs, ino, 0, buf, 0);
1160*6a54128fSAndroid Build Coastguard Worker 		if (cd->pctx.errcode)
1161*6a54128fSAndroid Build Coastguard Worker 			goto inline_read_fail;
1162*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
1163*6a54128fSAndroid Build Coastguard Worker 		if (db->blockcnt)
1164*6a54128fSAndroid Build Coastguard Worker 			goto skip_first_read_swab;
1165*6a54128fSAndroid Build Coastguard Worker 		*((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
1166*6a54128fSAndroid Build Coastguard Worker 		cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
1167*6a54128fSAndroid Build Coastguard Worker 				buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
1168*6a54128fSAndroid Build Coastguard Worker 				EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DATA_DOTDOT_SIZE,
1169*6a54128fSAndroid Build Coastguard Worker 				0);
1170*6a54128fSAndroid Build Coastguard Worker 		if (cd->pctx.errcode)
1171*6a54128fSAndroid Build Coastguard Worker 			goto inline_read_fail;
1172*6a54128fSAndroid Build Coastguard Worker skip_first_read_swab:
1173*6a54128fSAndroid Build Coastguard Worker 		if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
1174*6a54128fSAndroid Build Coastguard Worker 		    !db->blockcnt)
1175*6a54128fSAndroid Build Coastguard Worker 			goto inline_read_fail;
1176*6a54128fSAndroid Build Coastguard Worker 		cd->pctx.errcode = ext2fs_dirent_swab_in2(fs,
1177*6a54128fSAndroid Build Coastguard Worker 				buf + EXT4_MIN_INLINE_DATA_SIZE,
1178*6a54128fSAndroid Build Coastguard Worker 				inline_data_size - EXT4_MIN_INLINE_DATA_SIZE,
1179*6a54128fSAndroid Build Coastguard Worker 				0);
1180*6a54128fSAndroid Build Coastguard Worker #endif
1181*6a54128fSAndroid Build Coastguard Worker 	} else
1182*6a54128fSAndroid Build Coastguard Worker 		cd->pctx.errcode = ext2fs_read_dir_block4(fs, block_nr,
1183*6a54128fSAndroid Build Coastguard Worker 							  buf, 0, ino);
1184*6a54128fSAndroid Build Coastguard Worker inline_read_fail:
1185*6a54128fSAndroid Build Coastguard Worker 	pctx.ino = ino;
1186*6a54128fSAndroid Build Coastguard Worker 	pctx.num = inline_data_size;
1187*6a54128fSAndroid Build Coastguard Worker 	if (((inline_data_size & 3) ||
1188*6a54128fSAndroid Build Coastguard Worker 	     (inline_data_size > EXT4_MIN_INLINE_DATA_SIZE &&
1189*6a54128fSAndroid Build Coastguard Worker 	      inline_data_size < EXT4_MIN_INLINE_DATA_SIZE +
1190*6a54128fSAndroid Build Coastguard Worker 				 EXT2_DIR_REC_LEN(1))) &&
1191*6a54128fSAndroid Build Coastguard Worker 	    fix_problem(ctx, PR_2_BAD_INLINE_DIR_SIZE, &pctx)) {
1192*6a54128fSAndroid Build Coastguard Worker 		errcode_t err = fix_inline_dir_size(ctx, ino,
1193*6a54128fSAndroid Build Coastguard Worker 						    &inline_data_size, &pctx,
1194*6a54128fSAndroid Build Coastguard Worker 						    buf);
1195*6a54128fSAndroid Build Coastguard Worker 		if (err)
1196*6a54128fSAndroid Build Coastguard Worker 			return DIRENT_ABORT;
1197*6a54128fSAndroid Build Coastguard Worker 
1198*6a54128fSAndroid Build Coastguard Worker 	}
1199*6a54128fSAndroid Build Coastguard Worker 	ehandler_operation(0);
1200*6a54128fSAndroid Build Coastguard Worker 	if (cd->pctx.errcode == EXT2_ET_DIR_CORRUPTED)
1201*6a54128fSAndroid Build Coastguard Worker 		cd->pctx.errcode = 0; /* We'll handle this ourselves */
1202*6a54128fSAndroid Build Coastguard Worker 	else if (cd->pctx.errcode == EXT2_ET_DIR_CSUM_INVALID) {
1203*6a54128fSAndroid Build Coastguard Worker 		cd->pctx.errcode = 0; /* We'll handle this ourselves */
1204*6a54128fSAndroid Build Coastguard Worker 		failed_csum = 1;
1205*6a54128fSAndroid Build Coastguard Worker 	}
1206*6a54128fSAndroid Build Coastguard Worker 	if (cd->pctx.errcode) {
1207*6a54128fSAndroid Build Coastguard Worker 		char *buf2;
1208*6a54128fSAndroid Build Coastguard Worker 		if (!fix_problem(ctx, PR_2_READ_DIRBLOCK, &cd->pctx)) {
1209*6a54128fSAndroid Build Coastguard Worker 			ctx->flags |= E2F_FLAG_ABORT;
1210*6a54128fSAndroid Build Coastguard Worker 			return DIRENT_ABORT;
1211*6a54128fSAndroid Build Coastguard Worker 		}
1212*6a54128fSAndroid Build Coastguard Worker 		ext2fs_new_dir_block(fs, db->blockcnt == 0 ? ino : 0,
1213*6a54128fSAndroid Build Coastguard Worker 				     EXT2_ROOT_INO, &buf2);
1214*6a54128fSAndroid Build Coastguard Worker 		memcpy(buf, buf2, fs->blocksize);
1215*6a54128fSAndroid Build Coastguard Worker 		ext2fs_free_mem(&buf2);
1216*6a54128fSAndroid Build Coastguard Worker 	}
1217*6a54128fSAndroid Build Coastguard Worker 	dx_dir = e2fsck_get_dx_dir_info(ctx, ino);
1218*6a54128fSAndroid Build Coastguard Worker 	if (dx_dir && dx_dir->numblocks) {
1219*6a54128fSAndroid Build Coastguard Worker 		if (db->blockcnt >= dx_dir->numblocks) {
1220*6a54128fSAndroid Build Coastguard Worker 			pctx.dir = ino;
1221*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_2_UNEXPECTED_HTREE_BLOCK,
1222*6a54128fSAndroid Build Coastguard Worker 					&pctx)) {
1223*6a54128fSAndroid Build Coastguard Worker 				clear_htree(ctx, ino);
1224*6a54128fSAndroid Build Coastguard Worker 				dx_dir->numblocks = 0;
1225*6a54128fSAndroid Build Coastguard Worker 				dx_db = 0;
1226*6a54128fSAndroid Build Coastguard Worker 				goto out_htree;
1227*6a54128fSAndroid Build Coastguard Worker 			}
1228*6a54128fSAndroid Build Coastguard Worker 			fatal_error(ctx, _("Can not continue."));
1229*6a54128fSAndroid Build Coastguard Worker 		}
1230*6a54128fSAndroid Build Coastguard Worker 		dx_db = &dx_dir->dx_block[db->blockcnt];
1231*6a54128fSAndroid Build Coastguard Worker 		dx_db->type = DX_DIRBLOCK_LEAF;
1232*6a54128fSAndroid Build Coastguard Worker 		dx_db->phys = block_nr;
1233*6a54128fSAndroid Build Coastguard Worker 		dx_db->min_hash = ~0;
1234*6a54128fSAndroid Build Coastguard Worker 		dx_db->max_hash = 0;
1235*6a54128fSAndroid Build Coastguard Worker 
1236*6a54128fSAndroid Build Coastguard Worker 		dirent = (struct ext2_dir_entry *) buf;
1237*6a54128fSAndroid Build Coastguard Worker 		(void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1238*6a54128fSAndroid Build Coastguard Worker 		limit = (struct ext2_dx_countlimit *) (buf+8);
1239*6a54128fSAndroid Build Coastguard Worker 		if (db->blockcnt == 0) {
1240*6a54128fSAndroid Build Coastguard Worker 			root = (struct ext2_dx_root_info *) (buf + 24);
1241*6a54128fSAndroid Build Coastguard Worker 			dx_db->type = DX_DIRBLOCK_ROOT;
1242*6a54128fSAndroid Build Coastguard Worker 			dx_db->flags |= DX_FLAG_FIRST | DX_FLAG_LAST;
1243*6a54128fSAndroid Build Coastguard Worker 
1244*6a54128fSAndroid Build Coastguard Worker 			/* large_dir was set in pass1 if large dirs were found,
1245*6a54128fSAndroid Build Coastguard Worker 			 * so ext2_dir_htree_level() should now be correct */
1246*6a54128fSAndroid Build Coastguard Worker 			if ((root->reserved_zero ||
1247*6a54128fSAndroid Build Coastguard Worker 			     root->info_length < 8 ||
1248*6a54128fSAndroid Build Coastguard Worker 			     root->indirect_levels >=
1249*6a54128fSAndroid Build Coastguard Worker 			     ext2_dir_htree_level(fs)) &&
1250*6a54128fSAndroid Build Coastguard Worker 			    fix_problem(ctx, PR_2_HTREE_BAD_ROOT, &cd->pctx)) {
1251*6a54128fSAndroid Build Coastguard Worker 				clear_htree(ctx, ino);
1252*6a54128fSAndroid Build Coastguard Worker 				dx_dir->numblocks = 0;
1253*6a54128fSAndroid Build Coastguard Worker 				dx_db = NULL;
1254*6a54128fSAndroid Build Coastguard Worker 			}
1255*6a54128fSAndroid Build Coastguard Worker 			dx_dir->hashversion = root->hash_version;
1256*6a54128fSAndroid Build Coastguard Worker 			if ((dx_dir->hashversion <= EXT2_HASH_TEA) &&
1257*6a54128fSAndroid Build Coastguard Worker 			    (fs->super->s_flags & EXT2_FLAGS_UNSIGNED_HASH))
1258*6a54128fSAndroid Build Coastguard Worker 				dx_dir->hashversion += 3;
1259*6a54128fSAndroid Build Coastguard Worker 			dx_dir->depth = root->indirect_levels + 1;
1260*6a54128fSAndroid Build Coastguard Worker 		} else if ((dirent->inode == 0) &&
1261*6a54128fSAndroid Build Coastguard Worker 			   (rec_len == fs->blocksize) &&
1262*6a54128fSAndroid Build Coastguard Worker 			   (ext2fs_dirent_name_len(dirent) == 0) &&
1263*6a54128fSAndroid Build Coastguard Worker 			   (ext2fs_le16_to_cpu(limit->limit) ==
1264*6a54128fSAndroid Build Coastguard Worker 			    ((fs->blocksize - (8 + dx_csum_size)) /
1265*6a54128fSAndroid Build Coastguard Worker 			     sizeof(struct ext2_dx_entry)))) {
1266*6a54128fSAndroid Build Coastguard Worker 			dx_db->type = DX_DIRBLOCK_NODE;
1267*6a54128fSAndroid Build Coastguard Worker 		}
1268*6a54128fSAndroid Build Coastguard Worker 		is_leaf = dx_db ? (dx_db->type == DX_DIRBLOCK_LEAF) : 0;
1269*6a54128fSAndroid Build Coastguard Worker 	}
1270*6a54128fSAndroid Build Coastguard Worker out_htree:
1271*6a54128fSAndroid Build Coastguard Worker 
1272*6a54128fSAndroid Build Coastguard Worker 	/* Leaf node with no space for csum?  Rebuild dirs in pass 3A. */
1273*6a54128fSAndroid Build Coastguard Worker 	if (is_leaf && !inline_data_size && failed_csum &&
1274*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1275*6a54128fSAndroid Build Coastguard Worker 		de_csum_size = 0;
1276*6a54128fSAndroid Build Coastguard Worker 		if (e2fsck_dir_will_be_rehashed(ctx, ino)) {
1277*6a54128fSAndroid Build Coastguard Worker 			failed_csum = 0;
1278*6a54128fSAndroid Build Coastguard Worker 			goto skip_checksum;
1279*6a54128fSAndroid Build Coastguard Worker 		}
1280*6a54128fSAndroid Build Coastguard Worker 		if (!fix_problem(cd->ctx, PR_2_LEAF_NODE_MISSING_CSUM,
1281*6a54128fSAndroid Build Coastguard Worker 				 &cd->pctx))
1282*6a54128fSAndroid Build Coastguard Worker 			goto skip_checksum;
1283*6a54128fSAndroid Build Coastguard Worker 		e2fsck_rehash_dir_later(ctx, ino);
1284*6a54128fSAndroid Build Coastguard Worker 		failed_csum = 0;
1285*6a54128fSAndroid Build Coastguard Worker 		goto skip_checksum;
1286*6a54128fSAndroid Build Coastguard Worker 	}
1287*6a54128fSAndroid Build Coastguard Worker 	/* htree nodes don't use fake dirents to store checksums */
1288*6a54128fSAndroid Build Coastguard Worker 	if (!is_leaf)
1289*6a54128fSAndroid Build Coastguard Worker 		de_csum_size = 0;
1290*6a54128fSAndroid Build Coastguard Worker 
1291*6a54128fSAndroid Build Coastguard Worker skip_checksum:
1292*6a54128fSAndroid Build Coastguard Worker 	if (inline_data_size) {
1293*6a54128fSAndroid Build Coastguard Worker 		if (db->blockcnt) {
1294*6a54128fSAndroid Build Coastguard Worker 			buf += EXT4_MIN_INLINE_DATA_SIZE;
1295*6a54128fSAndroid Build Coastguard Worker 			max_block_size = inline_data_size - EXT4_MIN_INLINE_DATA_SIZE;
1296*6a54128fSAndroid Build Coastguard Worker 			/* Zero-length second block, just exit */
1297*6a54128fSAndroid Build Coastguard Worker 			if (max_block_size == 0)
1298*6a54128fSAndroid Build Coastguard Worker 				return 0;
1299*6a54128fSAndroid Build Coastguard Worker 		} else {
1300*6a54128fSAndroid Build Coastguard Worker 			max_block_size = EXT4_MIN_INLINE_DATA_SIZE;
1301*6a54128fSAndroid Build Coastguard Worker 		}
1302*6a54128fSAndroid Build Coastguard Worker 	} else
1303*6a54128fSAndroid Build Coastguard Worker 		max_block_size = fs->blocksize - de_csum_size;
1304*6a54128fSAndroid Build Coastguard Worker 
1305*6a54128fSAndroid Build Coastguard Worker 	dir_encpolicy_id = find_encryption_policy(ctx, ino);
1306*6a54128fSAndroid Build Coastguard Worker 
1307*6a54128fSAndroid Build Coastguard Worker 	if (cf_dir) {
1308*6a54128fSAndroid Build Coastguard Worker 		dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cf_cmp);
1309*6a54128fSAndroid Build Coastguard Worker 		dict_set_cmp_context(&de_dict, (const void *)ctx->fs->encoding);
1310*6a54128fSAndroid Build Coastguard Worker 	} else {
1311*6a54128fSAndroid Build Coastguard Worker 		dict_init(&de_dict, DICTCOUNT_T_MAX, dict_de_cmp);
1312*6a54128fSAndroid Build Coastguard Worker 	}
1313*6a54128fSAndroid Build Coastguard Worker 	if (ctx->casefolded_dirs)
1314*6a54128fSAndroid Build Coastguard Worker 		casefolded = ext2fs_u32_list_test(ctx->casefolded_dirs, ino);
1315*6a54128fSAndroid Build Coastguard Worker 	hash_in_dirent = (casefolded &&
1316*6a54128fSAndroid Build Coastguard Worker 			  (dir_encpolicy_id != NO_ENCRYPTION_POLICY));
1317*6a54128fSAndroid Build Coastguard Worker 
1318*6a54128fSAndroid Build Coastguard Worker 	prev = 0;
1319*6a54128fSAndroid Build Coastguard Worker 	do {
1320*6a54128fSAndroid Build Coastguard Worker 		dgrp_t group;
1321*6a54128fSAndroid Build Coastguard Worker 		ext2_ino_t first_unused_inode;
1322*6a54128fSAndroid Build Coastguard Worker 		unsigned int name_len;
1323*6a54128fSAndroid Build Coastguard Worker 		/* csum entry is not checked here, so don't worry about it */
1324*6a54128fSAndroid Build Coastguard Worker 		int extended = (dot_state > 1) && hash_in_dirent;
1325*6a54128fSAndroid Build Coastguard Worker 		unsigned int min_dir_len = ext2fs_dir_rec_len(1, extended);
1326*6a54128fSAndroid Build Coastguard Worker 
1327*6a54128fSAndroid Build Coastguard Worker 		problem = 0;
1328*6a54128fSAndroid Build Coastguard Worker 		if (!inline_data_size || dot_state > 1) {
1329*6a54128fSAndroid Build Coastguard Worker 			dirent = (struct ext2_dir_entry *) (buf + offset);
1330*6a54128fSAndroid Build Coastguard Worker 			/*
1331*6a54128fSAndroid Build Coastguard Worker 			 * If there's not even space for the entry header,
1332*6a54128fSAndroid Build Coastguard Worker 			 * force salvaging this dir.
1333*6a54128fSAndroid Build Coastguard Worker 			 */
1334*6a54128fSAndroid Build Coastguard Worker 			if (max_block_size - offset < EXT2_DIR_ENTRY_HEADER_LEN)
1335*6a54128fSAndroid Build Coastguard Worker 				rec_len = ext2fs_dir_rec_len(1, extended);
1336*6a54128fSAndroid Build Coastguard Worker 			else
1337*6a54128fSAndroid Build Coastguard Worker 				(void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1338*6a54128fSAndroid Build Coastguard Worker 			cd->pctx.dirent = dirent;
1339*6a54128fSAndroid Build Coastguard Worker 			cd->pctx.num = offset;
1340*6a54128fSAndroid Build Coastguard Worker 			if ((offset + rec_len > max_block_size) ||
1341*6a54128fSAndroid Build Coastguard Worker 			    (rec_len < min_dir_len) ||
1342*6a54128fSAndroid Build Coastguard Worker 			    ((rec_len % 4) != 0) ||
1343*6a54128fSAndroid Build Coastguard Worker 			    ((ext2fs_dir_rec_len(ext2fs_dirent_name_len(dirent),
1344*6a54128fSAndroid Build Coastguard Worker 						 extended)) > rec_len)) {
1345*6a54128fSAndroid Build Coastguard Worker 				if (fix_problem(ctx, PR_2_DIR_CORRUPTED,
1346*6a54128fSAndroid Build Coastguard Worker 						&cd->pctx)) {
1347*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
1348*6a54128fSAndroid Build Coastguard Worker 					/*
1349*6a54128fSAndroid Build Coastguard Worker 					 * On big-endian systems, if the dirent
1350*6a54128fSAndroid Build Coastguard Worker 					 * swap routine finds a rec_len that it
1351*6a54128fSAndroid Build Coastguard Worker 					 * doesn't like, it continues
1352*6a54128fSAndroid Build Coastguard Worker 					 * processing the block as if rec_len
1353*6a54128fSAndroid Build Coastguard Worker 					 * == EXT2_DIR_ENTRY_HEADER_LEN.  This means that the name
1354*6a54128fSAndroid Build Coastguard Worker 					 * field gets byte swapped, which means
1355*6a54128fSAndroid Build Coastguard Worker 					 * that salvage will not detect the
1356*6a54128fSAndroid Build Coastguard Worker 					 * correct name length (unless the name
1357*6a54128fSAndroid Build Coastguard Worker 					 * has a length that's an exact
1358*6a54128fSAndroid Build Coastguard Worker 					 * multiple of four bytes), and it'll
1359*6a54128fSAndroid Build Coastguard Worker 					 * discard the entry (unnecessarily)
1360*6a54128fSAndroid Build Coastguard Worker 					 * and the rest of the dirent block.
1361*6a54128fSAndroid Build Coastguard Worker 					 * Therefore, swap the rest of the
1362*6a54128fSAndroid Build Coastguard Worker 					 * block back to disk order, run
1363*6a54128fSAndroid Build Coastguard Worker 					 * salvage, and re-swap anything after
1364*6a54128fSAndroid Build Coastguard Worker 					 * the salvaged dirent.
1365*6a54128fSAndroid Build Coastguard Worker 					 */
1366*6a54128fSAndroid Build Coastguard Worker 					int need_reswab = 0;
1367*6a54128fSAndroid Build Coastguard Worker 					if (rec_len < EXT2_DIR_ENTRY_HEADER_LEN || rec_len % 4) {
1368*6a54128fSAndroid Build Coastguard Worker 						need_reswab = 1;
1369*6a54128fSAndroid Build Coastguard Worker 						ext2fs_dirent_swab_in2(fs,
1370*6a54128fSAndroid Build Coastguard Worker 							((char *)dirent) + EXT2_DIR_ENTRY_HEADER_LEN,
1371*6a54128fSAndroid Build Coastguard Worker 							max_block_size - offset - EXT2_DIR_ENTRY_HEADER_LEN,
1372*6a54128fSAndroid Build Coastguard Worker 							0);
1373*6a54128fSAndroid Build Coastguard Worker 					}
1374*6a54128fSAndroid Build Coastguard Worker #endif
1375*6a54128fSAndroid Build Coastguard Worker 					salvage_directory(fs, dirent, prev,
1376*6a54128fSAndroid Build Coastguard Worker 							  &offset,
1377*6a54128fSAndroid Build Coastguard Worker 							  max_block_size,
1378*6a54128fSAndroid Build Coastguard Worker 							  hash_in_dirent);
1379*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
1380*6a54128fSAndroid Build Coastguard Worker 					if (need_reswab) {
1381*6a54128fSAndroid Build Coastguard Worker 						unsigned int len;
1382*6a54128fSAndroid Build Coastguard Worker 
1383*6a54128fSAndroid Build Coastguard Worker 						(void) ext2fs_get_rec_len(fs,
1384*6a54128fSAndroid Build Coastguard Worker 							dirent, &len);
1385*6a54128fSAndroid Build Coastguard Worker 						len += offset;
1386*6a54128fSAndroid Build Coastguard Worker 						if (max_block_size > len)
1387*6a54128fSAndroid Build Coastguard Worker 							ext2fs_dirent_swab_in2(fs,
1388*6a54128fSAndroid Build Coastguard Worker 				((char *)dirent) + len, max_block_size - len, 0);
1389*6a54128fSAndroid Build Coastguard Worker 					}
1390*6a54128fSAndroid Build Coastguard Worker #endif
1391*6a54128fSAndroid Build Coastguard Worker 					dir_modified++;
1392*6a54128fSAndroid Build Coastguard Worker 					continue;
1393*6a54128fSAndroid Build Coastguard Worker 				} else
1394*6a54128fSAndroid Build Coastguard Worker 					goto abort_free_dict;
1395*6a54128fSAndroid Build Coastguard Worker 			}
1396*6a54128fSAndroid Build Coastguard Worker 		} else {
1397*6a54128fSAndroid Build Coastguard Worker 			if (dot_state == 0) {
1398*6a54128fSAndroid Build Coastguard Worker 				memset(&dot, 0, sizeof(dot));
1399*6a54128fSAndroid Build Coastguard Worker 				dirent = &dot;
1400*6a54128fSAndroid Build Coastguard Worker 				dirent->inode = ino;
1401*6a54128fSAndroid Build Coastguard Worker 				dirent->rec_len = EXT2_DIR_REC_LEN(1);
1402*6a54128fSAndroid Build Coastguard Worker 				dirent->name_len = 1 | filetype;
1403*6a54128fSAndroid Build Coastguard Worker 				dirent->name[0] = '.';
1404*6a54128fSAndroid Build Coastguard Worker 			} else if (dot_state == 1) {
1405*6a54128fSAndroid Build Coastguard Worker 				memset(&dotdot, 0, sizeof(dotdot));
1406*6a54128fSAndroid Build Coastguard Worker 				dirent = &dotdot;
1407*6a54128fSAndroid Build Coastguard Worker 				dirent->inode =
1408*6a54128fSAndroid Build Coastguard Worker 					((struct ext2_dir_entry *)buf)->inode;
1409*6a54128fSAndroid Build Coastguard Worker 				dirent->rec_len = EXT2_DIR_REC_LEN(2);
1410*6a54128fSAndroid Build Coastguard Worker 				dirent->name_len = 2 | filetype;
1411*6a54128fSAndroid Build Coastguard Worker 				dirent->name[0] = '.';
1412*6a54128fSAndroid Build Coastguard Worker 				dirent->name[1] = '.';
1413*6a54128fSAndroid Build Coastguard Worker 			} else {
1414*6a54128fSAndroid Build Coastguard Worker 				fatal_error(ctx, _("Can not continue."));
1415*6a54128fSAndroid Build Coastguard Worker 			}
1416*6a54128fSAndroid Build Coastguard Worker 			cd->pctx.dirent = dirent;
1417*6a54128fSAndroid Build Coastguard Worker 			cd->pctx.num = offset;
1418*6a54128fSAndroid Build Coastguard Worker 		}
1419*6a54128fSAndroid Build Coastguard Worker 
1420*6a54128fSAndroid Build Coastguard Worker 		if (dot_state == 0) {
1421*6a54128fSAndroid Build Coastguard Worker 			if (check_dot(ctx, dirent, ino, &cd->pctx))
1422*6a54128fSAndroid Build Coastguard Worker 				dir_modified++;
1423*6a54128fSAndroid Build Coastguard Worker 		} else if (dot_state == 1) {
1424*6a54128fSAndroid Build Coastguard Worker 			ret = check_dotdot(ctx, dirent, ino, &cd->pctx);
1425*6a54128fSAndroid Build Coastguard Worker 			if (ret < 0)
1426*6a54128fSAndroid Build Coastguard Worker 				goto abort_free_dict;
1427*6a54128fSAndroid Build Coastguard Worker 			if (ret)
1428*6a54128fSAndroid Build Coastguard Worker 				dir_modified++;
1429*6a54128fSAndroid Build Coastguard Worker 		} else if (dirent->inode == ino) {
1430*6a54128fSAndroid Build Coastguard Worker 			problem = PR_2_LINK_DOT;
1431*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_2_LINK_DOT, &cd->pctx)) {
1432*6a54128fSAndroid Build Coastguard Worker 				dirent->inode = 0;
1433*6a54128fSAndroid Build Coastguard Worker 				dir_modified++;
1434*6a54128fSAndroid Build Coastguard Worker 				goto next;
1435*6a54128fSAndroid Build Coastguard Worker 			}
1436*6a54128fSAndroid Build Coastguard Worker 		}
1437*6a54128fSAndroid Build Coastguard Worker 		if (!dirent->inode)
1438*6a54128fSAndroid Build Coastguard Worker 			goto next;
1439*6a54128fSAndroid Build Coastguard Worker 
1440*6a54128fSAndroid Build Coastguard Worker 		/*
1441*6a54128fSAndroid Build Coastguard Worker 		 * Make sure the inode listed is a legal one.
1442*6a54128fSAndroid Build Coastguard Worker 		 */
1443*6a54128fSAndroid Build Coastguard Worker 		name_len = ext2fs_dirent_name_len(dirent);
1444*6a54128fSAndroid Build Coastguard Worker 		if (((dirent->inode != EXT2_ROOT_INO) &&
1445*6a54128fSAndroid Build Coastguard Worker 		     (dirent->inode < EXT2_FIRST_INODE(fs->super))) ||
1446*6a54128fSAndroid Build Coastguard Worker 		    (dirent->inode > fs->super->s_inodes_count) ||
1447*6a54128fSAndroid Build Coastguard Worker 		    (dirent->inode == fs->super->s_usr_quota_inum) ||
1448*6a54128fSAndroid Build Coastguard Worker 		    (dirent->inode == fs->super->s_grp_quota_inum) ||
1449*6a54128fSAndroid Build Coastguard Worker 		    (dirent->inode == fs->super->s_prj_quota_inum)) {
1450*6a54128fSAndroid Build Coastguard Worker 			problem = PR_2_BAD_INO;
1451*6a54128fSAndroid Build Coastguard Worker 		} else if (ctx->inode_bb_map &&
1452*6a54128fSAndroid Build Coastguard Worker 			   (ext2fs_test_inode_bitmap2(ctx->inode_bb_map,
1453*6a54128fSAndroid Build Coastguard Worker 						     dirent->inode))) {
1454*6a54128fSAndroid Build Coastguard Worker 			/*
1455*6a54128fSAndroid Build Coastguard Worker 			 * If the inode is in a bad block, offer to
1456*6a54128fSAndroid Build Coastguard Worker 			 * clear it.
1457*6a54128fSAndroid Build Coastguard Worker 			 */
1458*6a54128fSAndroid Build Coastguard Worker 			problem = PR_2_BB_INODE;
1459*6a54128fSAndroid Build Coastguard Worker 		} else if ((dot_state > 1) && (name_len == 1) &&
1460*6a54128fSAndroid Build Coastguard Worker 			   (dirent->name[0] == '.')) {
1461*6a54128fSAndroid Build Coastguard Worker 			/*
1462*6a54128fSAndroid Build Coastguard Worker 			 * If there's a '.' entry in anything other
1463*6a54128fSAndroid Build Coastguard Worker 			 * than the first directory entry, it's a
1464*6a54128fSAndroid Build Coastguard Worker 			 * duplicate entry that should be removed.
1465*6a54128fSAndroid Build Coastguard Worker 			 */
1466*6a54128fSAndroid Build Coastguard Worker 			problem = PR_2_DUP_DOT;
1467*6a54128fSAndroid Build Coastguard Worker 		} else if ((dot_state > 1) && (name_len == 2) &&
1468*6a54128fSAndroid Build Coastguard Worker 			   (dirent->name[0] == '.') &&
1469*6a54128fSAndroid Build Coastguard Worker 			   (dirent->name[1] == '.')) {
1470*6a54128fSAndroid Build Coastguard Worker 			/*
1471*6a54128fSAndroid Build Coastguard Worker 			 * If there's a '..' entry in anything other
1472*6a54128fSAndroid Build Coastguard Worker 			 * than the second directory entry, it's a
1473*6a54128fSAndroid Build Coastguard Worker 			 * duplicate entry that should be removed.
1474*6a54128fSAndroid Build Coastguard Worker 			 */
1475*6a54128fSAndroid Build Coastguard Worker 			problem = PR_2_DUP_DOT_DOT;
1476*6a54128fSAndroid Build Coastguard Worker 		} else if ((dot_state > 1) &&
1477*6a54128fSAndroid Build Coastguard Worker 			   (dirent->inode == EXT2_ROOT_INO)) {
1478*6a54128fSAndroid Build Coastguard Worker 			/*
1479*6a54128fSAndroid Build Coastguard Worker 			 * Don't allow links to the root directory.
1480*6a54128fSAndroid Build Coastguard Worker 			 * We check this specially to make sure we
1481*6a54128fSAndroid Build Coastguard Worker 			 * catch this error case even if the root
1482*6a54128fSAndroid Build Coastguard Worker 			 * directory hasn't been created yet.
1483*6a54128fSAndroid Build Coastguard Worker 			 */
1484*6a54128fSAndroid Build Coastguard Worker 			problem = PR_2_LINK_ROOT;
1485*6a54128fSAndroid Build Coastguard Worker 		} else if ((dot_state > 1) && (name_len == 0)) {
1486*6a54128fSAndroid Build Coastguard Worker 			/*
1487*6a54128fSAndroid Build Coastguard Worker 			 * Don't allow zero-length directory names.
1488*6a54128fSAndroid Build Coastguard Worker 			 */
1489*6a54128fSAndroid Build Coastguard Worker 			problem = PR_2_NULL_NAME;
1490*6a54128fSAndroid Build Coastguard Worker 		}
1491*6a54128fSAndroid Build Coastguard Worker 
1492*6a54128fSAndroid Build Coastguard Worker 		if (problem) {
1493*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, problem, &cd->pctx)) {
1494*6a54128fSAndroid Build Coastguard Worker 				dirent->inode = 0;
1495*6a54128fSAndroid Build Coastguard Worker 				dir_modified++;
1496*6a54128fSAndroid Build Coastguard Worker 				goto next;
1497*6a54128fSAndroid Build Coastguard Worker 			} else {
1498*6a54128fSAndroid Build Coastguard Worker 				ext2fs_unmark_valid(fs);
1499*6a54128fSAndroid Build Coastguard Worker 				if (problem == PR_2_BAD_INO)
1500*6a54128fSAndroid Build Coastguard Worker 					goto next;
1501*6a54128fSAndroid Build Coastguard Worker 			}
1502*6a54128fSAndroid Build Coastguard Worker 		}
1503*6a54128fSAndroid Build Coastguard Worker 
1504*6a54128fSAndroid Build Coastguard Worker 		/*
1505*6a54128fSAndroid Build Coastguard Worker 		 * If the inode was marked as having bad fields in
1506*6a54128fSAndroid Build Coastguard Worker 		 * pass1, process it and offer to fix/clear it.
1507*6a54128fSAndroid Build Coastguard Worker 		 * (We wait until now so that we can display the
1508*6a54128fSAndroid Build Coastguard Worker 		 * pathname to the user.)
1509*6a54128fSAndroid Build Coastguard Worker 		 */
1510*6a54128fSAndroid Build Coastguard Worker 		if (ctx->inode_bad_map &&
1511*6a54128fSAndroid Build Coastguard Worker 		    ext2fs_test_inode_bitmap2(ctx->inode_bad_map,
1512*6a54128fSAndroid Build Coastguard Worker 					     dirent->inode)) {
1513*6a54128fSAndroid Build Coastguard Worker 			if (e2fsck_process_bad_inode(ctx, ino,
1514*6a54128fSAndroid Build Coastguard Worker 						     dirent->inode,
1515*6a54128fSAndroid Build Coastguard Worker 						     buf + fs->blocksize)) {
1516*6a54128fSAndroid Build Coastguard Worker 				dirent->inode = 0;
1517*6a54128fSAndroid Build Coastguard Worker 				dir_modified++;
1518*6a54128fSAndroid Build Coastguard Worker 				goto next;
1519*6a54128fSAndroid Build Coastguard Worker 			}
1520*6a54128fSAndroid Build Coastguard Worker 			if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1521*6a54128fSAndroid Build Coastguard Worker 				return DIRENT_ABORT;
1522*6a54128fSAndroid Build Coastguard Worker 		}
1523*6a54128fSAndroid Build Coastguard Worker 
1524*6a54128fSAndroid Build Coastguard Worker 		group = ext2fs_group_of_ino(fs, dirent->inode);
1525*6a54128fSAndroid Build Coastguard Worker 		first_unused_inode = group * fs->super->s_inodes_per_group +
1526*6a54128fSAndroid Build Coastguard Worker 					1 + fs->super->s_inodes_per_group -
1527*6a54128fSAndroid Build Coastguard Worker 					ext2fs_bg_itable_unused(fs, group);
1528*6a54128fSAndroid Build Coastguard Worker 		cd->pctx.group = group;
1529*6a54128fSAndroid Build Coastguard Worker 
1530*6a54128fSAndroid Build Coastguard Worker 		/*
1531*6a54128fSAndroid Build Coastguard Worker 		 * Check if the inode was missed out because
1532*6a54128fSAndroid Build Coastguard Worker 		 * _INODE_UNINIT flag was set or bg_itable_unused was
1533*6a54128fSAndroid Build Coastguard Worker 		 * incorrect.  If so, clear the _INODE_UNINIT flag and
1534*6a54128fSAndroid Build Coastguard Worker 		 * restart e2fsck.  In the future it would be nice if
1535*6a54128fSAndroid Build Coastguard Worker 		 * we could call a function in pass1.c that checks the
1536*6a54128fSAndroid Build Coastguard Worker 		 * newly visible inodes.
1537*6a54128fSAndroid Build Coastguard Worker 		 */
1538*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_bg_flags_test(fs, group, EXT2_BG_INODE_UNINIT)) {
1539*6a54128fSAndroid Build Coastguard Worker 			pctx.num = dirent->inode;
1540*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_2_INOREF_BG_INO_UNINIT,
1541*6a54128fSAndroid Build Coastguard Worker 					&cd->pctx)){
1542*6a54128fSAndroid Build Coastguard Worker 				ext2fs_bg_flags_clear(fs, group,
1543*6a54128fSAndroid Build Coastguard Worker 						      EXT2_BG_INODE_UNINIT);
1544*6a54128fSAndroid Build Coastguard Worker 				ext2fs_group_desc_csum_set(fs, group);
1545*6a54128fSAndroid Build Coastguard Worker 				ext2fs_mark_super_dirty(fs);
1546*6a54128fSAndroid Build Coastguard Worker 				ctx->flags |= E2F_FLAG_RESTART_LATER;
1547*6a54128fSAndroid Build Coastguard Worker 			} else {
1548*6a54128fSAndroid Build Coastguard Worker 				ext2fs_unmark_valid(fs);
1549*6a54128fSAndroid Build Coastguard Worker 				if (problem == PR_2_BAD_INO)
1550*6a54128fSAndroid Build Coastguard Worker 					goto next;
1551*6a54128fSAndroid Build Coastguard Worker 			}
1552*6a54128fSAndroid Build Coastguard Worker 		} else if (dirent->inode >= first_unused_inode) {
1553*6a54128fSAndroid Build Coastguard Worker 			pctx.num = dirent->inode;
1554*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, PR_2_INOREF_IN_UNUSED, &cd->pctx)){
1555*6a54128fSAndroid Build Coastguard Worker 				ext2fs_bg_itable_unused_set(fs, group, 0);
1556*6a54128fSAndroid Build Coastguard Worker 				ext2fs_group_desc_csum_set(fs, group);
1557*6a54128fSAndroid Build Coastguard Worker 				ext2fs_mark_super_dirty(fs);
1558*6a54128fSAndroid Build Coastguard Worker 				ctx->flags |= E2F_FLAG_RESTART_LATER;
1559*6a54128fSAndroid Build Coastguard Worker 			} else {
1560*6a54128fSAndroid Build Coastguard Worker 				ext2fs_unmark_valid(fs);
1561*6a54128fSAndroid Build Coastguard Worker 				if (problem == PR_2_BAD_INO)
1562*6a54128fSAndroid Build Coastguard Worker 					goto next;
1563*6a54128fSAndroid Build Coastguard Worker 			}
1564*6a54128fSAndroid Build Coastguard Worker 		}
1565*6a54128fSAndroid Build Coastguard Worker 
1566*6a54128fSAndroid Build Coastguard Worker 		/*
1567*6a54128fSAndroid Build Coastguard Worker 		 * Offer to clear unused inodes; if we are going to be
1568*6a54128fSAndroid Build Coastguard Worker 		 * restarting the scan due to bg_itable_unused being
1569*6a54128fSAndroid Build Coastguard Worker 		 * wrong, then don't clear any inodes to avoid zapping
1570*6a54128fSAndroid Build Coastguard Worker 		 * inodes that were skipped during pass1 due to an
1571*6a54128fSAndroid Build Coastguard Worker 		 * incorrect bg_itable_unused; we'll get any real
1572*6a54128fSAndroid Build Coastguard Worker 		 * problems after we restart.
1573*6a54128fSAndroid Build Coastguard Worker 		 */
1574*6a54128fSAndroid Build Coastguard Worker 		if (!(ctx->flags & E2F_FLAG_RESTART_LATER) &&
1575*6a54128fSAndroid Build Coastguard Worker 		    !(ext2fs_test_inode_bitmap2(ctx->inode_used_map,
1576*6a54128fSAndroid Build Coastguard Worker 						dirent->inode)))
1577*6a54128fSAndroid Build Coastguard Worker 			problem = PR_2_UNUSED_INODE;
1578*6a54128fSAndroid Build Coastguard Worker 
1579*6a54128fSAndroid Build Coastguard Worker 		if (problem) {
1580*6a54128fSAndroid Build Coastguard Worker 			if (fix_problem(ctx, problem, &cd->pctx)) {
1581*6a54128fSAndroid Build Coastguard Worker 				dirent->inode = 0;
1582*6a54128fSAndroid Build Coastguard Worker 				dir_modified++;
1583*6a54128fSAndroid Build Coastguard Worker 				goto next;
1584*6a54128fSAndroid Build Coastguard Worker 			} else {
1585*6a54128fSAndroid Build Coastguard Worker 				ext2fs_unmark_valid(fs);
1586*6a54128fSAndroid Build Coastguard Worker 				if (problem == PR_2_BAD_INO)
1587*6a54128fSAndroid Build Coastguard Worker 					goto next;
1588*6a54128fSAndroid Build Coastguard Worker 			}
1589*6a54128fSAndroid Build Coastguard Worker 		}
1590*6a54128fSAndroid Build Coastguard Worker 
1591*6a54128fSAndroid Build Coastguard Worker 		if (check_filetype(ctx, dirent, ino, &cd->pctx))
1592*6a54128fSAndroid Build Coastguard Worker 			dir_modified++;
1593*6a54128fSAndroid Build Coastguard Worker 
1594*6a54128fSAndroid Build Coastguard Worker 		if (dir_encpolicy_id != NO_ENCRYPTION_POLICY) {
1595*6a54128fSAndroid Build Coastguard Worker 			/* Encrypted directory */
1596*6a54128fSAndroid Build Coastguard Worker 			if (dot_state > 1 &&
1597*6a54128fSAndroid Build Coastguard Worker 			    check_encrypted_dirent(ctx, dirent,
1598*6a54128fSAndroid Build Coastguard Worker 						   dir_encpolicy_id,
1599*6a54128fSAndroid Build Coastguard Worker 						   &cd->pctx)) {
1600*6a54128fSAndroid Build Coastguard Worker 				dirent->inode = 0;
1601*6a54128fSAndroid Build Coastguard Worker 				dir_modified++;
1602*6a54128fSAndroid Build Coastguard Worker 				goto next;
1603*6a54128fSAndroid Build Coastguard Worker 			}
1604*6a54128fSAndroid Build Coastguard Worker 		} else if (cf_dir) {
1605*6a54128fSAndroid Build Coastguard Worker 			/* Casefolded directory */
1606*6a54128fSAndroid Build Coastguard Worker 			if (encoded_check_name(ctx, dirent, &cd->pctx))
1607*6a54128fSAndroid Build Coastguard Worker 				dir_modified++;
1608*6a54128fSAndroid Build Coastguard Worker 		} else {
1609*6a54128fSAndroid Build Coastguard Worker 			/* Unencrypted and uncasefolded directory */
1610*6a54128fSAndroid Build Coastguard Worker 			if (check_name(ctx, dirent, &cd->pctx))
1611*6a54128fSAndroid Build Coastguard Worker 				dir_modified++;
1612*6a54128fSAndroid Build Coastguard Worker 		}
1613*6a54128fSAndroid Build Coastguard Worker 
1614*6a54128fSAndroid Build Coastguard Worker 		if (dx_db) {
1615*6a54128fSAndroid Build Coastguard Worker 			if (dx_dir->casefolded_hash)
1616*6a54128fSAndroid Build Coastguard Worker 				hash_flags = EXT4_CASEFOLD_FL;
1617*6a54128fSAndroid Build Coastguard Worker 
1618*6a54128fSAndroid Build Coastguard Worker 			if (dx_dir->hashversion == EXT2_HASH_SIPHASH) {
1619*6a54128fSAndroid Build Coastguard Worker 				if (dot_state > 1)
1620*6a54128fSAndroid Build Coastguard Worker 					hash = EXT2_DIRENT_HASH(dirent);
1621*6a54128fSAndroid Build Coastguard Worker 			} else {
1622*6a54128fSAndroid Build Coastguard Worker 				ext2fs_dirhash2(dx_dir->hashversion,
1623*6a54128fSAndroid Build Coastguard Worker 						dirent->name,
1624*6a54128fSAndroid Build Coastguard Worker 						ext2fs_dirent_name_len(dirent),
1625*6a54128fSAndroid Build Coastguard Worker 						fs->encoding, hash_flags,
1626*6a54128fSAndroid Build Coastguard Worker 						fs->super->s_hash_seed,
1627*6a54128fSAndroid Build Coastguard Worker 						&hash, 0);
1628*6a54128fSAndroid Build Coastguard Worker 			}
1629*6a54128fSAndroid Build Coastguard Worker 			if (hash < dx_db->min_hash)
1630*6a54128fSAndroid Build Coastguard Worker 				dx_db->min_hash = hash;
1631*6a54128fSAndroid Build Coastguard Worker 			if (hash > dx_db->max_hash)
1632*6a54128fSAndroid Build Coastguard Worker 				dx_db->max_hash = hash;
1633*6a54128fSAndroid Build Coastguard Worker 		}
1634*6a54128fSAndroid Build Coastguard Worker 
1635*6a54128fSAndroid Build Coastguard Worker 		/*
1636*6a54128fSAndroid Build Coastguard Worker 		 * If this is a directory, then mark its parent in its
1637*6a54128fSAndroid Build Coastguard Worker 		 * dir_info structure.  If the parent field is already
1638*6a54128fSAndroid Build Coastguard Worker 		 * filled in, then this directory has more than one
1639*6a54128fSAndroid Build Coastguard Worker 		 * hard link.  We assume the first link is correct,
1640*6a54128fSAndroid Build Coastguard Worker 		 * and ask the user if he/she wants to clear this one.
1641*6a54128fSAndroid Build Coastguard Worker 		 */
1642*6a54128fSAndroid Build Coastguard Worker 		if ((dot_state > 1) &&
1643*6a54128fSAndroid Build Coastguard Worker 		    (ext2fs_test_inode_bitmap2(ctx->inode_dir_map,
1644*6a54128fSAndroid Build Coastguard Worker 					      dirent->inode))) {
1645*6a54128fSAndroid Build Coastguard Worker 			if (e2fsck_dir_info_get_parent(ctx, dirent->inode,
1646*6a54128fSAndroid Build Coastguard Worker 						       &subdir_parent)) {
1647*6a54128fSAndroid Build Coastguard Worker 				cd->pctx.ino = dirent->inode;
1648*6a54128fSAndroid Build Coastguard Worker 				fix_problem(ctx, PR_2_NO_DIRINFO, &cd->pctx);
1649*6a54128fSAndroid Build Coastguard Worker 				goto abort_free_dict;
1650*6a54128fSAndroid Build Coastguard Worker 			}
1651*6a54128fSAndroid Build Coastguard Worker 			if (subdir_parent) {
1652*6a54128fSAndroid Build Coastguard Worker 				cd->pctx.ino2 = subdir_parent;
1653*6a54128fSAndroid Build Coastguard Worker 				if (fix_problem(ctx, PR_2_LINK_DIR,
1654*6a54128fSAndroid Build Coastguard Worker 						&cd->pctx)) {
1655*6a54128fSAndroid Build Coastguard Worker 					dirent->inode = 0;
1656*6a54128fSAndroid Build Coastguard Worker 					dir_modified++;
1657*6a54128fSAndroid Build Coastguard Worker 					goto next;
1658*6a54128fSAndroid Build Coastguard Worker 				}
1659*6a54128fSAndroid Build Coastguard Worker 				cd->pctx.ino2 = 0;
1660*6a54128fSAndroid Build Coastguard Worker 			} else {
1661*6a54128fSAndroid Build Coastguard Worker 				(void) e2fsck_dir_info_set_parent(ctx,
1662*6a54128fSAndroid Build Coastguard Worker 						  dirent->inode, ino);
1663*6a54128fSAndroid Build Coastguard Worker 			}
1664*6a54128fSAndroid Build Coastguard Worker 		}
1665*6a54128fSAndroid Build Coastguard Worker 
1666*6a54128fSAndroid Build Coastguard Worker 		if (dups_found) {
1667*6a54128fSAndroid Build Coastguard Worker 			;
1668*6a54128fSAndroid Build Coastguard Worker 		} else if (dict_lookup(&de_dict, dirent)) {
1669*6a54128fSAndroid Build Coastguard Worker 			clear_problem_context(&pctx);
1670*6a54128fSAndroid Build Coastguard Worker 			pctx.ino = ino;
1671*6a54128fSAndroid Build Coastguard Worker 			pctx.dirent = dirent;
1672*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_2_REPORT_DUP_DIRENT, &pctx);
1673*6a54128fSAndroid Build Coastguard Worker 			e2fsck_rehash_dir_later(ctx, ino);
1674*6a54128fSAndroid Build Coastguard Worker 			dups_found++;
1675*6a54128fSAndroid Build Coastguard Worker 		} else
1676*6a54128fSAndroid Build Coastguard Worker 			dict_alloc_insert(&de_dict, dirent, dirent);
1677*6a54128fSAndroid Build Coastguard Worker 
1678*6a54128fSAndroid Build Coastguard Worker 		ext2fs_icount_increment(ctx->inode_count, dirent->inode,
1679*6a54128fSAndroid Build Coastguard Worker 					&links);
1680*6a54128fSAndroid Build Coastguard Worker 		if (links > 1)
1681*6a54128fSAndroid Build Coastguard Worker 			ctx->fs_links_count++;
1682*6a54128fSAndroid Build Coastguard Worker 		ctx->fs_total_count++;
1683*6a54128fSAndroid Build Coastguard Worker 	next:
1684*6a54128fSAndroid Build Coastguard Worker 		prev = dirent;
1685*6a54128fSAndroid Build Coastguard Worker 		if (dir_modified)
1686*6a54128fSAndroid Build Coastguard Worker 			(void) ext2fs_get_rec_len(fs, dirent, &rec_len);
1687*6a54128fSAndroid Build Coastguard Worker 		if (!inline_data_size || dot_state > 1) {
1688*6a54128fSAndroid Build Coastguard Worker 			offset += rec_len;
1689*6a54128fSAndroid Build Coastguard Worker 		} else {
1690*6a54128fSAndroid Build Coastguard Worker 			if (dot_state == 1) {
1691*6a54128fSAndroid Build Coastguard Worker 				offset = 4;
1692*6a54128fSAndroid Build Coastguard Worker 				/*
1693*6a54128fSAndroid Build Coastguard Worker 				 * If we get here, we're checking an inline
1694*6a54128fSAndroid Build Coastguard Worker 				 * directory and we've just checked a (fake)
1695*6a54128fSAndroid Build Coastguard Worker 				 * dotdot entry that we created on the stack.
1696*6a54128fSAndroid Build Coastguard Worker 				 * Therefore set 'prev' to NULL so that if we
1697*6a54128fSAndroid Build Coastguard Worker 				 * call salvage_directory on the next entry,
1698*6a54128fSAndroid Build Coastguard Worker 				 * it won't try to absorb the next entry into
1699*6a54128fSAndroid Build Coastguard Worker 				 * the on-stack dotdot entry.
1700*6a54128fSAndroid Build Coastguard Worker 				 */
1701*6a54128fSAndroid Build Coastguard Worker 				prev = NULL;
1702*6a54128fSAndroid Build Coastguard Worker 			}
1703*6a54128fSAndroid Build Coastguard Worker 		}
1704*6a54128fSAndroid Build Coastguard Worker 		dot_state++;
1705*6a54128fSAndroid Build Coastguard Worker 	} while (offset < max_block_size);
1706*6a54128fSAndroid Build Coastguard Worker #if 0
1707*6a54128fSAndroid Build Coastguard Worker 	printf("\n");
1708*6a54128fSAndroid Build Coastguard Worker #endif
1709*6a54128fSAndroid Build Coastguard Worker 	if (dx_db) {
1710*6a54128fSAndroid Build Coastguard Worker #ifdef DX_DEBUG
1711*6a54128fSAndroid Build Coastguard Worker 		printf("db_block %d, type %d, min_hash 0x%0x, max_hash 0x%0x\n",
1712*6a54128fSAndroid Build Coastguard Worker 		       db->blockcnt, dx_db->type,
1713*6a54128fSAndroid Build Coastguard Worker 		       dx_db->min_hash, dx_db->max_hash);
1714*6a54128fSAndroid Build Coastguard Worker #endif
1715*6a54128fSAndroid Build Coastguard Worker 		cd->pctx.dir = cd->pctx.ino;
1716*6a54128fSAndroid Build Coastguard Worker 		if ((dx_db->type == DX_DIRBLOCK_ROOT) ||
1717*6a54128fSAndroid Build Coastguard Worker 		    (dx_db->type == DX_DIRBLOCK_NODE))
1718*6a54128fSAndroid Build Coastguard Worker 			parse_int_node(fs, db, cd, dx_dir, buf, failed_csum);
1719*6a54128fSAndroid Build Coastguard Worker 	}
1720*6a54128fSAndroid Build Coastguard Worker 
1721*6a54128fSAndroid Build Coastguard Worker 	if (offset != max_block_size) {
1722*6a54128fSAndroid Build Coastguard Worker 		cd->pctx.num = rec_len + offset - max_block_size;
1723*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_FINAL_RECLEN, &cd->pctx)) {
1724*6a54128fSAndroid Build Coastguard Worker 			dirent->rec_len = cd->pctx.num;
1725*6a54128fSAndroid Build Coastguard Worker 			dir_modified++;
1726*6a54128fSAndroid Build Coastguard Worker 		}
1727*6a54128fSAndroid Build Coastguard Worker 	}
1728*6a54128fSAndroid Build Coastguard Worker 	if (dir_modified) {
1729*6a54128fSAndroid Build Coastguard Worker 		int	flags, will_rehash;
1730*6a54128fSAndroid Build Coastguard Worker 		/* leaf block with no tail?  Rehash dirs later. */
1731*6a54128fSAndroid Build Coastguard Worker 		if (ext2fs_has_feature_metadata_csum(fs->super) &&
1732*6a54128fSAndroid Build Coastguard Worker 		    is_leaf &&
1733*6a54128fSAndroid Build Coastguard Worker 		    !inline_data_size &&
1734*6a54128fSAndroid Build Coastguard Worker 		    !ext2fs_dirent_has_tail(fs, (struct ext2_dir_entry *)buf)) {
1735*6a54128fSAndroid Build Coastguard Worker 			if (insert_dirent_tail(fs, buf) == 0)
1736*6a54128fSAndroid Build Coastguard Worker 				goto write_and_fix;
1737*6a54128fSAndroid Build Coastguard Worker 			e2fsck_rehash_dir_later(ctx, ino);
1738*6a54128fSAndroid Build Coastguard Worker 		}
1739*6a54128fSAndroid Build Coastguard Worker 
1740*6a54128fSAndroid Build Coastguard Worker write_and_fix:
1741*6a54128fSAndroid Build Coastguard Worker 		will_rehash = e2fsck_dir_will_be_rehashed(ctx, ino);
1742*6a54128fSAndroid Build Coastguard Worker 		if (will_rehash) {
1743*6a54128fSAndroid Build Coastguard Worker 			flags = ctx->fs->flags;
1744*6a54128fSAndroid Build Coastguard Worker 			ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1745*6a54128fSAndroid Build Coastguard Worker 		}
1746*6a54128fSAndroid Build Coastguard Worker 		if (inline_data_size) {
1747*6a54128fSAndroid Build Coastguard Worker 			buf = ibuf;
1748*6a54128fSAndroid Build Coastguard Worker #ifdef WORDS_BIGENDIAN
1749*6a54128fSAndroid Build Coastguard Worker 			if (db->blockcnt)
1750*6a54128fSAndroid Build Coastguard Worker 				goto skip_first_write_swab;
1751*6a54128fSAndroid Build Coastguard Worker 			*((__u32 *)buf) = ext2fs_le32_to_cpu(*((__u32 *)buf));
1752*6a54128fSAndroid Build Coastguard Worker 			cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1753*6a54128fSAndroid Build Coastguard Worker 					buf + EXT4_INLINE_DATA_DOTDOT_SIZE,
1754*6a54128fSAndroid Build Coastguard Worker 					EXT4_MIN_INLINE_DATA_SIZE -
1755*6a54128fSAndroid Build Coastguard Worker 					EXT4_INLINE_DATA_DOTDOT_SIZE,
1756*6a54128fSAndroid Build Coastguard Worker 					0);
1757*6a54128fSAndroid Build Coastguard Worker 			if (cd->pctx.errcode)
1758*6a54128fSAndroid Build Coastguard Worker 				goto skip_second_write_swab;
1759*6a54128fSAndroid Build Coastguard Worker skip_first_write_swab:
1760*6a54128fSAndroid Build Coastguard Worker 			if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE ||
1761*6a54128fSAndroid Build Coastguard Worker 			    !db->blockcnt)
1762*6a54128fSAndroid Build Coastguard Worker 				goto skip_second_write_swab;
1763*6a54128fSAndroid Build Coastguard Worker 			cd->pctx.errcode = ext2fs_dirent_swab_out2(fs,
1764*6a54128fSAndroid Build Coastguard Worker 					buf + EXT4_MIN_INLINE_DATA_SIZE,
1765*6a54128fSAndroid Build Coastguard Worker 					inline_data_size -
1766*6a54128fSAndroid Build Coastguard Worker 					EXT4_MIN_INLINE_DATA_SIZE,
1767*6a54128fSAndroid Build Coastguard Worker 					0);
1768*6a54128fSAndroid Build Coastguard Worker skip_second_write_swab:
1769*6a54128fSAndroid Build Coastguard Worker 			if (cd->pctx.errcode &&
1770*6a54128fSAndroid Build Coastguard Worker 			    !fix_problem(ctx, PR_2_WRITE_DIRBLOCK, &cd->pctx))
1771*6a54128fSAndroid Build Coastguard Worker 				goto abort_free_dict;
1772*6a54128fSAndroid Build Coastguard Worker #endif
1773*6a54128fSAndroid Build Coastguard Worker 			cd->pctx.errcode =
1774*6a54128fSAndroid Build Coastguard Worker 				ext2fs_inline_data_set(fs, ino, 0, buf,
1775*6a54128fSAndroid Build Coastguard Worker 						       inline_data_size);
1776*6a54128fSAndroid Build Coastguard Worker 		} else
1777*6a54128fSAndroid Build Coastguard Worker 			cd->pctx.errcode = ext2fs_write_dir_block4(fs, block_nr,
1778*6a54128fSAndroid Build Coastguard Worker 								   buf, 0, ino);
1779*6a54128fSAndroid Build Coastguard Worker 		if (will_rehash)
1780*6a54128fSAndroid Build Coastguard Worker 			ctx->fs->flags = (flags &
1781*6a54128fSAndroid Build Coastguard Worker 					  EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1782*6a54128fSAndroid Build Coastguard Worker 					 (ctx->fs->flags &
1783*6a54128fSAndroid Build Coastguard Worker 					  ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1784*6a54128fSAndroid Build Coastguard Worker 		if (cd->pctx.errcode) {
1785*6a54128fSAndroid Build Coastguard Worker 			if (!fix_problem(ctx, PR_2_WRITE_DIRBLOCK,
1786*6a54128fSAndroid Build Coastguard Worker 					 &cd->pctx))
1787*6a54128fSAndroid Build Coastguard Worker 				goto abort_free_dict;
1788*6a54128fSAndroid Build Coastguard Worker 		}
1789*6a54128fSAndroid Build Coastguard Worker 		ext2fs_mark_changed(fs);
1790*6a54128fSAndroid Build Coastguard Worker 	} else if (is_leaf && failed_csum && !dir_modified) {
1791*6a54128fSAndroid Build Coastguard Worker 		/*
1792*6a54128fSAndroid Build Coastguard Worker 		 * If a leaf node that fails csum makes it this far without
1793*6a54128fSAndroid Build Coastguard Worker 		 * alteration, ask the user if the checksum should be fixed.
1794*6a54128fSAndroid Build Coastguard Worker 		 */
1795*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_LEAF_NODE_ONLY_CSUM_INVALID,
1796*6a54128fSAndroid Build Coastguard Worker 				&cd->pctx))
1797*6a54128fSAndroid Build Coastguard Worker 			goto write_and_fix;
1798*6a54128fSAndroid Build Coastguard Worker 	}
1799*6a54128fSAndroid Build Coastguard Worker 	dict_free_nodes(&de_dict);
1800*6a54128fSAndroid Build Coastguard Worker 	return 0;
1801*6a54128fSAndroid Build Coastguard Worker abort_free_dict:
1802*6a54128fSAndroid Build Coastguard Worker 	ctx->flags |= E2F_FLAG_ABORT;
1803*6a54128fSAndroid Build Coastguard Worker 	dict_free_nodes(&de_dict);
1804*6a54128fSAndroid Build Coastguard Worker 	return DIRENT_ABORT;
1805*6a54128fSAndroid Build Coastguard Worker }
1806*6a54128fSAndroid Build Coastguard Worker 
1807*6a54128fSAndroid Build Coastguard Worker struct del_block {
1808*6a54128fSAndroid Build Coastguard Worker 	e2fsck_t	ctx;
1809*6a54128fSAndroid Build Coastguard Worker 	e2_blkcnt_t	num;
1810*6a54128fSAndroid Build Coastguard Worker 	blk64_t last_cluster;
1811*6a54128fSAndroid Build Coastguard Worker };
1812*6a54128fSAndroid Build Coastguard Worker 
1813*6a54128fSAndroid Build Coastguard Worker /*
1814*6a54128fSAndroid Build Coastguard Worker  * This function is called to deallocate a block, and is an iterator
1815*6a54128fSAndroid Build Coastguard Worker  * functioned called by deallocate inode via ext2fs_iterate_block().
1816*6a54128fSAndroid Build Coastguard Worker  */
deallocate_inode_block(ext2_filsys fs,blk64_t * block_nr,e2_blkcnt_t blockcnt EXT2FS_ATTR ((unused)),blk64_t ref_block EXT2FS_ATTR ((unused)),int ref_offset EXT2FS_ATTR ((unused)),void * priv_data)1817*6a54128fSAndroid Build Coastguard Worker static int deallocate_inode_block(ext2_filsys fs,
1818*6a54128fSAndroid Build Coastguard Worker 				  blk64_t	*block_nr,
1819*6a54128fSAndroid Build Coastguard Worker 				  e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1820*6a54128fSAndroid Build Coastguard Worker 				  blk64_t ref_block EXT2FS_ATTR((unused)),
1821*6a54128fSAndroid Build Coastguard Worker 				  int ref_offset EXT2FS_ATTR((unused)),
1822*6a54128fSAndroid Build Coastguard Worker 				  void *priv_data)
1823*6a54128fSAndroid Build Coastguard Worker {
1824*6a54128fSAndroid Build Coastguard Worker 	struct del_block *p = priv_data;
1825*6a54128fSAndroid Build Coastguard Worker 	blk64_t cluster = EXT2FS_B2C(fs, *block_nr);
1826*6a54128fSAndroid Build Coastguard Worker 
1827*6a54128fSAndroid Build Coastguard Worker 	if (*block_nr == 0)
1828*6a54128fSAndroid Build Coastguard Worker 		return 0;
1829*6a54128fSAndroid Build Coastguard Worker 
1830*6a54128fSAndroid Build Coastguard Worker 	if (cluster == p->last_cluster)
1831*6a54128fSAndroid Build Coastguard Worker 		return 0;
1832*6a54128fSAndroid Build Coastguard Worker 
1833*6a54128fSAndroid Build Coastguard Worker 	p->last_cluster = cluster;
1834*6a54128fSAndroid Build Coastguard Worker 	if ((*block_nr < fs->super->s_first_data_block) ||
1835*6a54128fSAndroid Build Coastguard Worker 	    (*block_nr >= ext2fs_blocks_count(fs->super)))
1836*6a54128fSAndroid Build Coastguard Worker 		return 0;
1837*6a54128fSAndroid Build Coastguard Worker 
1838*6a54128fSAndroid Build Coastguard Worker         ext2fs_block_alloc_stats2(fs, *block_nr, -1);
1839*6a54128fSAndroid Build Coastguard Worker 	p->num++;
1840*6a54128fSAndroid Build Coastguard Worker 	return 0;
1841*6a54128fSAndroid Build Coastguard Worker }
1842*6a54128fSAndroid Build Coastguard Worker 
1843*6a54128fSAndroid Build Coastguard Worker /*
1844*6a54128fSAndroid Build Coastguard Worker  * This function deallocates an inode
1845*6a54128fSAndroid Build Coastguard Worker  */
deallocate_inode(e2fsck_t ctx,ext2_ino_t ino,char * block_buf)1846*6a54128fSAndroid Build Coastguard Worker static void deallocate_inode(e2fsck_t ctx, ext2_ino_t ino, char* block_buf)
1847*6a54128fSAndroid Build Coastguard Worker {
1848*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
1849*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode	inode;
1850*6a54128fSAndroid Build Coastguard Worker 	struct problem_context	pctx;
1851*6a54128fSAndroid Build Coastguard Worker 	__u32			count;
1852*6a54128fSAndroid Build Coastguard Worker 	struct del_block	del_block;
1853*6a54128fSAndroid Build Coastguard Worker 
1854*6a54128fSAndroid Build Coastguard Worker 	e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1855*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
1856*6a54128fSAndroid Build Coastguard Worker 	pctx.ino = ino;
1857*6a54128fSAndroid Build Coastguard Worker 
1858*6a54128fSAndroid Build Coastguard Worker 	/*
1859*6a54128fSAndroid Build Coastguard Worker 	 * Fix up the bitmaps...
1860*6a54128fSAndroid Build Coastguard Worker 	 */
1861*6a54128fSAndroid Build Coastguard Worker 	e2fsck_read_bitmaps(ctx);
1862*6a54128fSAndroid Build Coastguard Worker 	ext2fs_inode_alloc_stats2(fs, ino, -1, LINUX_S_ISDIR(inode.i_mode));
1863*6a54128fSAndroid Build Coastguard Worker 
1864*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_file_acl_block(fs, &inode) &&
1865*6a54128fSAndroid Build Coastguard Worker 	    ext2fs_has_feature_xattr(fs->super)) {
1866*6a54128fSAndroid Build Coastguard Worker 		pctx.errcode = ext2fs_adjust_ea_refcount3(fs,
1867*6a54128fSAndroid Build Coastguard Worker 				ext2fs_file_acl_block(fs, &inode),
1868*6a54128fSAndroid Build Coastguard Worker 				block_buf, -1, &count, ino);
1869*6a54128fSAndroid Build Coastguard Worker 		if (pctx.errcode == EXT2_ET_BAD_EA_BLOCK_NUM) {
1870*6a54128fSAndroid Build Coastguard Worker 			pctx.errcode = 0;
1871*6a54128fSAndroid Build Coastguard Worker 			count = 1;
1872*6a54128fSAndroid Build Coastguard Worker 		}
1873*6a54128fSAndroid Build Coastguard Worker 		if (pctx.errcode) {
1874*6a54128fSAndroid Build Coastguard Worker 			pctx.blk = ext2fs_file_acl_block(fs, &inode);
1875*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_2_ADJ_EA_REFCOUNT, &pctx);
1876*6a54128fSAndroid Build Coastguard Worker 			ctx->flags |= E2F_FLAG_ABORT;
1877*6a54128fSAndroid Build Coastguard Worker 			return;
1878*6a54128fSAndroid Build Coastguard Worker 		}
1879*6a54128fSAndroid Build Coastguard Worker 		if (count == 0) {
1880*6a54128fSAndroid Build Coastguard Worker 			ext2fs_block_alloc_stats2(fs,
1881*6a54128fSAndroid Build Coastguard Worker 				  ext2fs_file_acl_block(fs, &inode), -1);
1882*6a54128fSAndroid Build Coastguard Worker 		}
1883*6a54128fSAndroid Build Coastguard Worker 		ext2fs_file_acl_block_set(fs, &inode, 0);
1884*6a54128fSAndroid Build Coastguard Worker 	}
1885*6a54128fSAndroid Build Coastguard Worker 
1886*6a54128fSAndroid Build Coastguard Worker 	if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1887*6a54128fSAndroid Build Coastguard Worker 		goto clear_inode;
1888*6a54128fSAndroid Build Coastguard Worker 
1889*6a54128fSAndroid Build Coastguard Worker 	/* Inline data inodes don't have blocks to iterate */
1890*6a54128fSAndroid Build Coastguard Worker 	if (inode.i_flags & EXT4_INLINE_DATA_FL)
1891*6a54128fSAndroid Build Coastguard Worker 		goto clear_inode;
1892*6a54128fSAndroid Build Coastguard Worker 
1893*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_needs_large_file_feature(EXT2_I_SIZE(&inode))) {
1894*6a54128fSAndroid Build Coastguard Worker 		if (LINUX_S_ISREG(inode.i_mode))
1895*6a54128fSAndroid Build Coastguard Worker 		    ctx->large_files--;
1896*6a54128fSAndroid Build Coastguard Worker 		else if (LINUX_S_ISDIR(inode.i_mode))
1897*6a54128fSAndroid Build Coastguard Worker 		    ctx->large_dirs--;
1898*6a54128fSAndroid Build Coastguard Worker 	}
1899*6a54128fSAndroid Build Coastguard Worker 
1900*6a54128fSAndroid Build Coastguard Worker 	del_block.ctx = ctx;
1901*6a54128fSAndroid Build Coastguard Worker 	del_block.num = 0;
1902*6a54128fSAndroid Build Coastguard Worker 	del_block.last_cluster = 0;
1903*6a54128fSAndroid Build Coastguard Worker 	pctx.errcode = ext2fs_block_iterate3(fs, ino, 0, block_buf,
1904*6a54128fSAndroid Build Coastguard Worker 					     deallocate_inode_block,
1905*6a54128fSAndroid Build Coastguard Worker 					     &del_block);
1906*6a54128fSAndroid Build Coastguard Worker 	if (pctx.errcode) {
1907*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_2_DEALLOC_INODE, &pctx);
1908*6a54128fSAndroid Build Coastguard Worker 		ctx->flags |= E2F_FLAG_ABORT;
1909*6a54128fSAndroid Build Coastguard Worker 		return;
1910*6a54128fSAndroid Build Coastguard Worker 	}
1911*6a54128fSAndroid Build Coastguard Worker clear_inode:
1912*6a54128fSAndroid Build Coastguard Worker 	/* Inode may have changed by block_iterate, so reread it */
1913*6a54128fSAndroid Build Coastguard Worker 	e2fsck_read_inode(ctx, ino, &inode, "deallocate_inode");
1914*6a54128fSAndroid Build Coastguard Worker 	e2fsck_clear_inode(ctx, ino, &inode, 0, "deallocate_inode");
1915*6a54128fSAndroid Build Coastguard Worker }
1916*6a54128fSAndroid Build Coastguard Worker 
1917*6a54128fSAndroid Build Coastguard Worker /*
1918*6a54128fSAndroid Build Coastguard Worker  * This function clears the htree flag on an inode
1919*6a54128fSAndroid Build Coastguard Worker  */
clear_htree(e2fsck_t ctx,ext2_ino_t ino)1920*6a54128fSAndroid Build Coastguard Worker static void clear_htree(e2fsck_t ctx, ext2_ino_t ino)
1921*6a54128fSAndroid Build Coastguard Worker {
1922*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode	inode;
1923*6a54128fSAndroid Build Coastguard Worker 
1924*6a54128fSAndroid Build Coastguard Worker 	e2fsck_read_inode(ctx, ino, &inode, "clear_htree");
1925*6a54128fSAndroid Build Coastguard Worker 	inode.i_flags = inode.i_flags & ~EXT2_INDEX_FL;
1926*6a54128fSAndroid Build Coastguard Worker 	e2fsck_write_inode(ctx, ino, &inode, "clear_htree");
1927*6a54128fSAndroid Build Coastguard Worker 	if (ctx->dirs_to_hash)
1928*6a54128fSAndroid Build Coastguard Worker 		ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1929*6a54128fSAndroid Build Coastguard Worker }
1930*6a54128fSAndroid Build Coastguard Worker 
1931*6a54128fSAndroid Build Coastguard Worker 
e2fsck_process_bad_inode(e2fsck_t ctx,ext2_ino_t dir,ext2_ino_t ino,char * buf)1932*6a54128fSAndroid Build Coastguard Worker int e2fsck_process_bad_inode(e2fsck_t ctx, ext2_ino_t dir,
1933*6a54128fSAndroid Build Coastguard Worker 			     ext2_ino_t ino, char *buf)
1934*6a54128fSAndroid Build Coastguard Worker {
1935*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
1936*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode	inode;
1937*6a54128fSAndroid Build Coastguard Worker 	int			inode_modified = 0;
1938*6a54128fSAndroid Build Coastguard Worker 	int			not_fixed = 0;
1939*6a54128fSAndroid Build Coastguard Worker 	unsigned char		*frag, *fsize;
1940*6a54128fSAndroid Build Coastguard Worker 	struct problem_context	pctx;
1941*6a54128fSAndroid Build Coastguard Worker 	problem_t		problem = 0;
1942*6a54128fSAndroid Build Coastguard Worker 
1943*6a54128fSAndroid Build Coastguard Worker 	e2fsck_read_inode(ctx, ino, &inode, "process_bad_inode");
1944*6a54128fSAndroid Build Coastguard Worker 
1945*6a54128fSAndroid Build Coastguard Worker 	clear_problem_context(&pctx);
1946*6a54128fSAndroid Build Coastguard Worker 	pctx.ino = ino;
1947*6a54128fSAndroid Build Coastguard Worker 	pctx.dir = dir;
1948*6a54128fSAndroid Build Coastguard Worker 	pctx.inode = &inode;
1949*6a54128fSAndroid Build Coastguard Worker 
1950*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_file_acl_block(fs, &inode) &&
1951*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_has_feature_xattr(fs->super)) {
1952*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_FILE_ACL_ZERO, &pctx)) {
1953*6a54128fSAndroid Build Coastguard Worker 			ext2fs_file_acl_block_set(fs, &inode, 0);
1954*6a54128fSAndroid Build Coastguard Worker 			inode_modified++;
1955*6a54128fSAndroid Build Coastguard Worker 		} else
1956*6a54128fSAndroid Build Coastguard Worker 			not_fixed++;
1957*6a54128fSAndroid Build Coastguard Worker 	}
1958*6a54128fSAndroid Build Coastguard Worker 
1959*6a54128fSAndroid Build Coastguard Worker 	if (!LINUX_S_ISDIR(inode.i_mode) && !LINUX_S_ISREG(inode.i_mode) &&
1960*6a54128fSAndroid Build Coastguard Worker 	    !LINUX_S_ISCHR(inode.i_mode) && !LINUX_S_ISBLK(inode.i_mode) &&
1961*6a54128fSAndroid Build Coastguard Worker 	    !LINUX_S_ISLNK(inode.i_mode) && !LINUX_S_ISFIFO(inode.i_mode) &&
1962*6a54128fSAndroid Build Coastguard Worker 	    !(LINUX_S_ISSOCK(inode.i_mode)))
1963*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_BAD_MODE;
1964*6a54128fSAndroid Build Coastguard Worker 	else if (LINUX_S_ISCHR(inode.i_mode)
1965*6a54128fSAndroid Build Coastguard Worker 		 && !e2fsck_pass1_check_device_inode(fs, &inode))
1966*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_BAD_CHAR_DEV;
1967*6a54128fSAndroid Build Coastguard Worker 	else if (LINUX_S_ISBLK(inode.i_mode)
1968*6a54128fSAndroid Build Coastguard Worker 		 && !e2fsck_pass1_check_device_inode(fs, &inode))
1969*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_BAD_BLOCK_DEV;
1970*6a54128fSAndroid Build Coastguard Worker 	else if (LINUX_S_ISFIFO(inode.i_mode)
1971*6a54128fSAndroid Build Coastguard Worker 		 && !e2fsck_pass1_check_device_inode(fs, &inode))
1972*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_BAD_FIFO;
1973*6a54128fSAndroid Build Coastguard Worker 	else if (LINUX_S_ISSOCK(inode.i_mode)
1974*6a54128fSAndroid Build Coastguard Worker 		 && !e2fsck_pass1_check_device_inode(fs, &inode))
1975*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_BAD_SOCKET;
1976*6a54128fSAndroid Build Coastguard Worker 	else if (LINUX_S_ISLNK(inode.i_mode)
1977*6a54128fSAndroid Build Coastguard Worker 		 && !e2fsck_pass1_check_symlink(fs, ino, &inode, buf)) {
1978*6a54128fSAndroid Build Coastguard Worker 		problem = PR_2_INVALID_SYMLINK;
1979*6a54128fSAndroid Build Coastguard Worker 	}
1980*6a54128fSAndroid Build Coastguard Worker 
1981*6a54128fSAndroid Build Coastguard Worker 	if (problem) {
1982*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, problem, &pctx)) {
1983*6a54128fSAndroid Build Coastguard Worker 			deallocate_inode(ctx, ino, 0);
1984*6a54128fSAndroid Build Coastguard Worker 			if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1985*6a54128fSAndroid Build Coastguard Worker 				return 0;
1986*6a54128fSAndroid Build Coastguard Worker 			return 1;
1987*6a54128fSAndroid Build Coastguard Worker 		} else
1988*6a54128fSAndroid Build Coastguard Worker 			not_fixed++;
1989*6a54128fSAndroid Build Coastguard Worker 		problem = 0;
1990*6a54128fSAndroid Build Coastguard Worker 	}
1991*6a54128fSAndroid Build Coastguard Worker 
1992*6a54128fSAndroid Build Coastguard Worker 	if (inode.i_faddr) {
1993*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_FADDR_ZERO, &pctx)) {
1994*6a54128fSAndroid Build Coastguard Worker 			inode.i_faddr = 0;
1995*6a54128fSAndroid Build Coastguard Worker 			inode_modified++;
1996*6a54128fSAndroid Build Coastguard Worker 		} else
1997*6a54128fSAndroid Build Coastguard Worker 			not_fixed++;
1998*6a54128fSAndroid Build Coastguard Worker 	}
1999*6a54128fSAndroid Build Coastguard Worker 
2000*6a54128fSAndroid Build Coastguard Worker 	switch (fs->super->s_creator_os) {
2001*6a54128fSAndroid Build Coastguard Worker 	    case EXT2_OS_HURD:
2002*6a54128fSAndroid Build Coastguard Worker 		frag = &inode.osd2.hurd2.h_i_frag;
2003*6a54128fSAndroid Build Coastguard Worker 		fsize = &inode.osd2.hurd2.h_i_fsize;
2004*6a54128fSAndroid Build Coastguard Worker 		break;
2005*6a54128fSAndroid Build Coastguard Worker 	    default:
2006*6a54128fSAndroid Build Coastguard Worker 		frag = fsize = 0;
2007*6a54128fSAndroid Build Coastguard Worker 	}
2008*6a54128fSAndroid Build Coastguard Worker 	if (frag && *frag) {
2009*6a54128fSAndroid Build Coastguard Worker 		pctx.num = *frag;
2010*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_FRAG_ZERO, &pctx)) {
2011*6a54128fSAndroid Build Coastguard Worker 			*frag = 0;
2012*6a54128fSAndroid Build Coastguard Worker 			inode_modified++;
2013*6a54128fSAndroid Build Coastguard Worker 		} else
2014*6a54128fSAndroid Build Coastguard Worker 			not_fixed++;
2015*6a54128fSAndroid Build Coastguard Worker 		pctx.num = 0;
2016*6a54128fSAndroid Build Coastguard Worker 	}
2017*6a54128fSAndroid Build Coastguard Worker 	if (fsize && *fsize) {
2018*6a54128fSAndroid Build Coastguard Worker 		pctx.num = *fsize;
2019*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_FSIZE_ZERO, &pctx)) {
2020*6a54128fSAndroid Build Coastguard Worker 			*fsize = 0;
2021*6a54128fSAndroid Build Coastguard Worker 			inode_modified++;
2022*6a54128fSAndroid Build Coastguard Worker 		} else
2023*6a54128fSAndroid Build Coastguard Worker 			not_fixed++;
2024*6a54128fSAndroid Build Coastguard Worker 		pctx.num = 0;
2025*6a54128fSAndroid Build Coastguard Worker 	}
2026*6a54128fSAndroid Build Coastguard Worker 
2027*6a54128fSAndroid Build Coastguard Worker 	if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
2028*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_has_feature_huge_file(fs->super) &&
2029*6a54128fSAndroid Build Coastguard Worker 	    (inode.osd2.linux2.l_i_blocks_hi != 0)) {
2030*6a54128fSAndroid Build Coastguard Worker 		pctx.num = inode.osd2.linux2.l_i_blocks_hi;
2031*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_BLOCKS_HI_ZERO, &pctx)) {
2032*6a54128fSAndroid Build Coastguard Worker 			inode.osd2.linux2.l_i_blocks_hi = 0;
2033*6a54128fSAndroid Build Coastguard Worker 			inode_modified++;
2034*6a54128fSAndroid Build Coastguard Worker 		}
2035*6a54128fSAndroid Build Coastguard Worker 	}
2036*6a54128fSAndroid Build Coastguard Worker 
2037*6a54128fSAndroid Build Coastguard Worker 	if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
2038*6a54128fSAndroid Build Coastguard Worker 	    !ext2fs_has_feature_64bit(fs->super) &&
2039*6a54128fSAndroid Build Coastguard Worker 	    inode.osd2.linux2.l_i_file_acl_high != 0) {
2040*6a54128fSAndroid Build Coastguard Worker 		pctx.num = inode.osd2.linux2.l_i_file_acl_high;
2041*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_I_FILE_ACL_HI_ZERO, &pctx)) {
2042*6a54128fSAndroid Build Coastguard Worker 			inode.osd2.linux2.l_i_file_acl_high = 0;
2043*6a54128fSAndroid Build Coastguard Worker 			inode_modified++;
2044*6a54128fSAndroid Build Coastguard Worker 		} else
2045*6a54128fSAndroid Build Coastguard Worker 			not_fixed++;
2046*6a54128fSAndroid Build Coastguard Worker 	}
2047*6a54128fSAndroid Build Coastguard Worker 
2048*6a54128fSAndroid Build Coastguard Worker 	if (ext2fs_file_acl_block(fs, &inode) &&
2049*6a54128fSAndroid Build Coastguard Worker 	    ((ext2fs_file_acl_block(fs, &inode) < fs->super->s_first_data_block) ||
2050*6a54128fSAndroid Build Coastguard Worker 	     (ext2fs_file_acl_block(fs, &inode) >= ext2fs_blocks_count(fs->super)))) {
2051*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_FILE_ACL_BAD, &pctx)) {
2052*6a54128fSAndroid Build Coastguard Worker 			ext2fs_file_acl_block_set(fs, &inode, 0);
2053*6a54128fSAndroid Build Coastguard Worker 			inode_modified++;
2054*6a54128fSAndroid Build Coastguard Worker 		} else
2055*6a54128fSAndroid Build Coastguard Worker 			not_fixed++;
2056*6a54128fSAndroid Build Coastguard Worker 	}
2057*6a54128fSAndroid Build Coastguard Worker 	if (inode.i_size_high && !ext2fs_has_feature_largedir(fs->super) &&
2058*6a54128fSAndroid Build Coastguard Worker 	    inode.i_blocks < 1ULL << (29 - EXT2_BLOCK_SIZE_BITS(fs->super)) &&
2059*6a54128fSAndroid Build Coastguard Worker 	    LINUX_S_ISDIR(inode.i_mode)) {
2060*6a54128fSAndroid Build Coastguard Worker 		if (fix_problem(ctx, PR_2_DIR_SIZE_HIGH_ZERO, &pctx)) {
2061*6a54128fSAndroid Build Coastguard Worker 			inode.i_size_high = 0;
2062*6a54128fSAndroid Build Coastguard Worker 			inode_modified++;
2063*6a54128fSAndroid Build Coastguard Worker 		} else
2064*6a54128fSAndroid Build Coastguard Worker 			not_fixed++;
2065*6a54128fSAndroid Build Coastguard Worker 	}
2066*6a54128fSAndroid Build Coastguard Worker 
2067*6a54128fSAndroid Build Coastguard Worker 	if (inode_modified)
2068*6a54128fSAndroid Build Coastguard Worker 		e2fsck_write_inode(ctx, ino, &inode, "process_bad_inode");
2069*6a54128fSAndroid Build Coastguard Worker 	if (!not_fixed && ctx->inode_bad_map)
2070*6a54128fSAndroid Build Coastguard Worker 		ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
2071*6a54128fSAndroid Build Coastguard Worker 	return 0;
2072*6a54128fSAndroid Build Coastguard Worker }
2073*6a54128fSAndroid Build Coastguard Worker 
2074*6a54128fSAndroid Build Coastguard Worker /*
2075*6a54128fSAndroid Build Coastguard Worker  * allocate_dir_block --- this function allocates a new directory
2076*6a54128fSAndroid Build Coastguard Worker  * 	block for a particular inode; this is done if a directory has
2077*6a54128fSAndroid Build Coastguard Worker  * 	a "hole" in it, or if a directory has a illegal block number
2078*6a54128fSAndroid Build Coastguard Worker  * 	that was zeroed out and now needs to be replaced.
2079*6a54128fSAndroid Build Coastguard Worker  */
allocate_dir_block(e2fsck_t ctx,struct ext2_db_entry2 * db,char * buf EXT2FS_ATTR ((unused)),struct problem_context * pctx)2080*6a54128fSAndroid Build Coastguard Worker static int allocate_dir_block(e2fsck_t ctx,
2081*6a54128fSAndroid Build Coastguard Worker 			      struct ext2_db_entry2 *db,
2082*6a54128fSAndroid Build Coastguard Worker 			      char *buf EXT2FS_ATTR((unused)),
2083*6a54128fSAndroid Build Coastguard Worker 			      struct problem_context *pctx)
2084*6a54128fSAndroid Build Coastguard Worker {
2085*6a54128fSAndroid Build Coastguard Worker 	ext2_filsys fs = ctx->fs;
2086*6a54128fSAndroid Build Coastguard Worker 	blk64_t			blk = 0;
2087*6a54128fSAndroid Build Coastguard Worker 	char			*block;
2088*6a54128fSAndroid Build Coastguard Worker 	struct ext2_inode	inode;
2089*6a54128fSAndroid Build Coastguard Worker 
2090*6a54128fSAndroid Build Coastguard Worker 	if (fix_problem(ctx, PR_2_DIRECTORY_HOLE, pctx) == 0)
2091*6a54128fSAndroid Build Coastguard Worker 		return 1;
2092*6a54128fSAndroid Build Coastguard Worker 
2093*6a54128fSAndroid Build Coastguard Worker 	/*
2094*6a54128fSAndroid Build Coastguard Worker 	 * Read the inode and block bitmaps in; we'll be messing with
2095*6a54128fSAndroid Build Coastguard Worker 	 * them.
2096*6a54128fSAndroid Build Coastguard Worker 	 */
2097*6a54128fSAndroid Build Coastguard Worker 	e2fsck_read_bitmaps(ctx);
2098*6a54128fSAndroid Build Coastguard Worker 
2099*6a54128fSAndroid Build Coastguard Worker 	/*
2100*6a54128fSAndroid Build Coastguard Worker 	 * First, find a free block
2101*6a54128fSAndroid Build Coastguard Worker 	 */
2102*6a54128fSAndroid Build Coastguard Worker 	e2fsck_read_inode(ctx, db->ino, &inode, "allocate_dir_block");
2103*6a54128fSAndroid Build Coastguard Worker 	pctx->errcode = ext2fs_map_cluster_block(fs, db->ino, &inode,
2104*6a54128fSAndroid Build Coastguard Worker 						 db->blockcnt, &blk);
2105*6a54128fSAndroid Build Coastguard Worker 	if (pctx->errcode || blk == 0) {
2106*6a54128fSAndroid Build Coastguard Worker 		blk = ext2fs_find_inode_goal(fs, db->ino, &inode, db->blockcnt);
2107*6a54128fSAndroid Build Coastguard Worker 		pctx->errcode = ext2fs_new_block2(fs, blk,
2108*6a54128fSAndroid Build Coastguard Worker 						  ctx->block_found_map, &blk);
2109*6a54128fSAndroid Build Coastguard Worker 		if (pctx->errcode) {
2110*6a54128fSAndroid Build Coastguard Worker 			pctx->str = "ext2fs_new_block";
2111*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
2112*6a54128fSAndroid Build Coastguard Worker 			return 1;
2113*6a54128fSAndroid Build Coastguard Worker 		}
2114*6a54128fSAndroid Build Coastguard Worker 	}
2115*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
2116*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_block_bitmap2(fs->block_map, blk);
2117*6a54128fSAndroid Build Coastguard Worker 	ext2fs_mark_bb_dirty(fs);
2118*6a54128fSAndroid Build Coastguard Worker 
2119*6a54128fSAndroid Build Coastguard Worker 	/*
2120*6a54128fSAndroid Build Coastguard Worker 	 * Now let's create the actual data block for the inode
2121*6a54128fSAndroid Build Coastguard Worker 	 */
2122*6a54128fSAndroid Build Coastguard Worker 	if (db->blockcnt)
2123*6a54128fSAndroid Build Coastguard Worker 		pctx->errcode = ext2fs_new_dir_block(fs, 0, 0, &block);
2124*6a54128fSAndroid Build Coastguard Worker 	else
2125*6a54128fSAndroid Build Coastguard Worker 		pctx->errcode = ext2fs_new_dir_block(fs, db->ino,
2126*6a54128fSAndroid Build Coastguard Worker 						     EXT2_ROOT_INO, &block);
2127*6a54128fSAndroid Build Coastguard Worker 
2128*6a54128fSAndroid Build Coastguard Worker 	if (pctx->errcode) {
2129*6a54128fSAndroid Build Coastguard Worker 		pctx->str = "ext2fs_new_dir_block";
2130*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
2131*6a54128fSAndroid Build Coastguard Worker 		return 1;
2132*6a54128fSAndroid Build Coastguard Worker 	}
2133*6a54128fSAndroid Build Coastguard Worker 
2134*6a54128fSAndroid Build Coastguard Worker 	pctx->errcode = ext2fs_write_dir_block4(fs, blk, block, 0, db->ino);
2135*6a54128fSAndroid Build Coastguard Worker 	ext2fs_free_mem(&block);
2136*6a54128fSAndroid Build Coastguard Worker 	if (pctx->errcode) {
2137*6a54128fSAndroid Build Coastguard Worker 		pctx->str = "ext2fs_write_dir_block";
2138*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
2139*6a54128fSAndroid Build Coastguard Worker 		return 1;
2140*6a54128fSAndroid Build Coastguard Worker 	}
2141*6a54128fSAndroid Build Coastguard Worker 
2142*6a54128fSAndroid Build Coastguard Worker 	/*
2143*6a54128fSAndroid Build Coastguard Worker 	 * Update the inode block count
2144*6a54128fSAndroid Build Coastguard Worker 	 */
2145*6a54128fSAndroid Build Coastguard Worker 	ext2fs_iblk_add_blocks(fs, &inode, 1);
2146*6a54128fSAndroid Build Coastguard Worker 	if (EXT2_I_SIZE(&inode) < ((__u64) db->blockcnt+1) * fs->blocksize) {
2147*6a54128fSAndroid Build Coastguard Worker 		pctx->errcode = ext2fs_inode_size_set(fs, &inode,
2148*6a54128fSAndroid Build Coastguard Worker 					(db->blockcnt+1) * fs->blocksize);
2149*6a54128fSAndroid Build Coastguard Worker 		if (pctx->errcode) {
2150*6a54128fSAndroid Build Coastguard Worker 			pctx->str = "ext2fs_inode_size_set";
2151*6a54128fSAndroid Build Coastguard Worker 			fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
2152*6a54128fSAndroid Build Coastguard Worker 			return 1;
2153*6a54128fSAndroid Build Coastguard Worker 		}
2154*6a54128fSAndroid Build Coastguard Worker 	}
2155*6a54128fSAndroid Build Coastguard Worker 	e2fsck_write_inode(ctx, db->ino, &inode, "allocate_dir_block");
2156*6a54128fSAndroid Build Coastguard Worker 
2157*6a54128fSAndroid Build Coastguard Worker 	/*
2158*6a54128fSAndroid Build Coastguard Worker 	 * Finally, update the block pointers for the inode
2159*6a54128fSAndroid Build Coastguard Worker 	 */
2160*6a54128fSAndroid Build Coastguard Worker 	db->blk = blk;
2161*6a54128fSAndroid Build Coastguard Worker 	pctx->errcode = ext2fs_bmap2(fs, db->ino, &inode, 0, BMAP_SET,
2162*6a54128fSAndroid Build Coastguard Worker 				     db->blockcnt, 0, &blk);
2163*6a54128fSAndroid Build Coastguard Worker 	if (pctx->errcode) {
2164*6a54128fSAndroid Build Coastguard Worker 		pctx->str = "ext2fs_block_iterate";
2165*6a54128fSAndroid Build Coastguard Worker 		fix_problem(ctx, PR_2_ALLOC_DIRBOCK, pctx);
2166*6a54128fSAndroid Build Coastguard Worker 		return 1;
2167*6a54128fSAndroid Build Coastguard Worker 	}
2168*6a54128fSAndroid Build Coastguard Worker 
2169*6a54128fSAndroid Build Coastguard Worker 	return 0;
2170*6a54128fSAndroid Build Coastguard Worker }
2171