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