2 * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
11 * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12 * and applies the following tests to each inode:
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
18 * Pass 1 also gathers the collects the following information:
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 inodes need to be expanded (expand_eisize_map)
28 * - A bitmap of which blocks are in use. (block_found_map)
29 * - A bitmap of which blocks are in use by two inodes (block_dup_map)
30 * - The data blocks of the directory inodes. (dir_map)
31 * - Ref counts for ea_inodes. (ea_inode_refs)
32 * - The encryption policy ID of each encrypted inode. (encrypted_files)
34 * Pass 1 is designed to stash away enough information so that the
35 * other passes should not need to read in the inode information
36 * during the normal course of a filesystem check. (Although if an
37 * inconsistency is detected, other passes may need to read in an
40 * Note that pass 1B will be invoked if there are any duplicate blocks
44 #define _GNU_SOURCE 1 /* get strnlen() */
57 #include <ext2fs/ext2_ext_attr.h>
58 /* todo remove this finally */
59 #include <ext2fs/ext2fsP.h>
64 #ifdef NO_INLINE_FUNCS
67 #define _INLINE_ inline
77 static int process_block(ext2_filsys fs, blk64_t *blocknr,
78 e2_blkcnt_t blockcnt, blk64_t ref_blk,
79 int ref_offset, void *priv_data);
80 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
81 e2_blkcnt_t blockcnt, blk64_t ref_blk,
82 int ref_offset, void *priv_data);
83 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
85 const struct ea_quota *ea_ibody_quota);
86 static void mark_table_blocks(e2fsck_t ctx);
87 static void alloc_bb_map(e2fsck_t ctx);
88 static void alloc_imagic_map(e2fsck_t ctx);
89 static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
90 static void add_casefolded_dir(e2fsck_t ctx, ino_t ino);
91 static void handle_fs_bad_blocks(e2fsck_t ctx);
92 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
93 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
94 dgrp_t group, void * priv_data);
95 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
96 char *block_buf, int adjust_sign);
97 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
99 struct process_block_struct {
101 unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
102 fragmented:1, compressed:1, bbcheck:1,
107 e2_blkcnt_t last_init_lblock;
108 e2_blkcnt_t last_db_block;
109 int num_illegal_blocks;
110 blk64_t previous_block;
111 struct ext2_inode *inode;
112 struct problem_context *pctx;
113 ext2fs_block_bitmap fs_meta_blocks;
116 struct extent_tree_info eti;
119 struct process_inode_block {
121 struct ea_quota ea_ibody_quota;
122 struct ext2_inode_large inode;
125 struct scan_callback_struct {
128 struct process_inode_block *inodes_to_process;
129 int *process_inode_count;
132 static void process_inodes(e2fsck_t ctx, char *block_buf,
133 struct process_inode_block *inodes_to_process,
134 int *process_inode_count);
136 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
137 EXT2_MIN_BLOCK_LOG_SIZE + 1];
140 * Check to make sure a device inode is real. Returns 1 if the device
141 * checks out, 0 if not.
143 * Note: this routine is now also used to check FIFO's and Sockets,
144 * since they have the same requirement; the i_block fields should be
147 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
148 struct ext2_inode *inode)
153 * If the index or extents flag is set, then this is a bogus
156 if (inode->i_flags & (EXT2_INDEX_FL | EXT4_EXTENTS_FL))
160 * We should be able to do the test below all the time, but
161 * because the kernel doesn't forcibly clear the device
162 * inode's additional i_block fields, there are some rare
163 * occasions when a legitimate device inode will have non-zero
164 * additional i_block fields. So for now, we only complain
165 * when the immutable flag is set, which should never happen
166 * for devices. (And that's when the problem is caused, since
167 * you can't set or clear immutable flags for devices.) Once
168 * the kernel has been fixed we can change this...
170 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
171 for (i=4; i < EXT2_N_BLOCKS; i++)
172 if (inode->i_block[i])
179 * Check to make sure a symlink inode is real. Returns 1 if the symlink
180 * checks out, 0 if not.
182 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
183 struct ext2_inode *inode, char *buf)
188 if ((inode->i_size_high || inode->i_size == 0) ||
189 (inode->i_flags & EXT2_INDEX_FL))
192 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
195 if (inode->i_flags & EXT4_EXTENTS_FL)
197 if (ext2fs_inline_data_size(fs, ino, &inline_size))
199 if (inode->i_size != inline_size)
205 if (ext2fs_is_fast_symlink(inode)) {
206 if (inode->i_flags & EXT4_EXTENTS_FL)
208 buf = (char *)inode->i_block;
209 buflen = sizeof(inode->i_block);
211 ext2_extent_handle_t handle;
212 struct ext2_extent_info info;
213 struct ext2fs_extent extent;
217 if (inode->i_flags & EXT4_EXTENTS_FL) {
218 if (ext2fs_extent_open2(fs, ino, inode, &handle))
220 if (ext2fs_extent_get_info(handle, &info) ||
221 (info.num_entries != 1) ||
222 (info.max_depth != 0)) {
223 ext2fs_extent_free(handle);
226 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT,
228 (extent.e_lblk != 0) ||
229 (extent.e_len != 1)) {
230 ext2fs_extent_free(handle);
234 ext2fs_extent_free(handle);
236 blk = inode->i_block[0];
238 for (i = 1; i < EXT2_N_BLOCKS; i++)
239 if (inode->i_block[i])
243 if (blk < fs->super->s_first_data_block ||
244 blk >= ext2fs_blocks_count(fs->super))
247 if (io_channel_read_blk64(fs->io, blk, 1, buf))
250 buflen = fs->blocksize;
253 if (inode->i_flags & EXT4_ENCRYPT_FL)
254 len = ext2fs_le16_to_cpu(*(__u16 *)buf) + 2;
256 len = strnlen(buf, buflen);
261 if (len != inode->i_size)
267 * If the extents or inlinedata flags are set on the inode, offer to clear 'em.
269 #define BAD_SPECIAL_FLAGS (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)
270 static void check_extents_inlinedata(e2fsck_t ctx,
271 struct problem_context *pctx)
273 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
276 if (!fix_problem(ctx, PR_1_SPECIAL_EXTENTS_IDATA, pctx))
279 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
280 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
282 #undef BAD_SPECIAL_FLAGS
285 * If the immutable (or append-only) flag is set on the inode, offer
288 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
289 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
291 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
294 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
297 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
298 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
302 * If device, fifo or socket, check size is zero -- if not offer to
305 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
307 struct ext2_inode *inode = pctx->inode;
309 if (EXT2_I_SIZE(inode) == 0)
312 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
315 ext2fs_inode_size_set(ctx->fs, inode, 0);
316 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
320 * For a given size, calculate how many blocks would be charged towards quota.
322 static blk64_t size_to_quota_blocks(ext2_filsys fs, size_t size)
326 clusters = DIV_ROUND_UP(size, fs->blocksize << fs->cluster_ratio_bits);
327 return EXT2FS_C2B(fs, clusters);
331 * Check validity of EA inode. Return 0 if EA inode is valid, otherwise return
334 static problem_t check_large_ea_inode(e2fsck_t ctx,
335 struct ext2_ext_attr_entry *entry,
336 struct problem_context *pctx,
337 blk64_t *quota_blocks)
339 struct ext2_inode inode;
343 /* Check if inode is within valid range */
344 if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
345 (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
346 pctx->num = entry->e_value_inum;
347 return PR_1_ATTR_VALUE_EA_INODE;
350 e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
352 retval = ext2fs_ext_attr_hash_entry2(ctx->fs, entry, NULL, &hash);
354 com_err("check_large_ea_inode", retval,
355 _("while hashing entry with e_value_inum = %u"),
356 entry->e_value_inum);
360 if (hash == entry->e_hash) {
361 *quota_blocks = size_to_quota_blocks(ctx->fs,
362 entry->e_value_size);
364 /* This might be an old Lustre-style ea_inode reference. */
365 if (inode.i_mtime == pctx->ino &&
366 inode.i_generation == pctx->inode->i_generation) {
369 /* If target inode is also missing EA_INODE flag,
370 * this is likely to be a bad reference.
372 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
373 pctx->num = entry->e_value_inum;
374 return PR_1_ATTR_VALUE_EA_INODE;
376 pctx->num = entry->e_hash;
377 return PR_1_ATTR_HASH;
382 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
383 pctx->num = entry->e_value_inum;
384 if (fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
385 inode.i_flags |= EXT4_EA_INODE_FL;
386 e2fsck_pass1_fix_lock(ctx);
387 ext2fs_write_inode(ctx->fs, entry->e_value_inum,
389 e2fsck_pass1_fix_unlock(ctx);
391 return PR_1_ATTR_NO_EA_INODE_FL;
397 static void inc_ea_inode_refs(e2fsck_t ctx, struct problem_context *pctx,
398 struct ext2_ext_attr_entry *first, void *end)
400 struct ext2_ext_attr_entry *entry = first;
401 struct ext2_ext_attr_entry *np = EXT2_EXT_ATTR_NEXT(entry);
403 while ((void *) entry < end && (void *) np < end &&
404 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
405 if (!entry->e_value_inum)
407 if (!ctx->ea_inode_refs) {
408 pctx->errcode = ea_refcount_create(0,
409 &ctx->ea_inode_refs);
412 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
413 ctx->flags |= E2F_FLAG_ABORT;
417 ea_refcount_increment(ctx->ea_inode_refs, entry->e_value_inum,
421 np = EXT2_EXT_ATTR_NEXT(entry);
425 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx,
426 struct ea_quota *ea_ibody_quota)
428 struct ext2_super_block *sb = ctx->fs->super;
429 struct ext2_inode_large *inode;
430 struct ext2_ext_attr_entry *entry;
431 char *start, *header, *end;
432 unsigned int storage_size, remain;
433 problem_t problem = 0;
436 ea_ibody_quota->blocks = 0;
437 ea_ibody_quota->inodes = 0;
439 inode = (struct ext2_inode_large *) pctx->inode;
440 storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
441 inode->i_extra_isize;
442 header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
443 inode->i_extra_isize;
444 end = header + storage_size;
445 start = header + sizeof(__u32);
446 entry = (struct ext2_ext_attr_entry *) start;
448 /* scan all entry's headers first */
450 /* take finish entry 0UL into account */
451 remain = storage_size - sizeof(__u32);
453 region = region_create(0, storage_size);
455 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
457 ctx->flags |= E2F_FLAG_ABORT;
460 if (region_allocate(region, 0, sizeof(__u32))) {
461 problem = PR_1_INODE_EA_ALLOC_COLLISION;
465 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
466 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
469 if (region_allocate(region, (char *)entry - (char *)header,
470 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
471 problem = PR_1_INODE_EA_ALLOC_COLLISION;
475 /* header eats this space */
476 remain -= sizeof(struct ext2_ext_attr_entry);
478 /* is attribute name valid? */
479 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
480 pctx->num = entry->e_name_len;
481 problem = PR_1_ATTR_NAME_LEN;
485 /* attribute len eats this space */
486 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
488 if (entry->e_value_inum == 0) {
489 /* check value size */
490 if (entry->e_value_size > remain) {
491 pctx->num = entry->e_value_size;
492 problem = PR_1_ATTR_VALUE_SIZE;
496 if (entry->e_value_size &&
497 region_allocate(region,
498 sizeof(__u32) + entry->e_value_offs,
500 entry->e_value_size))) {
501 problem = PR_1_INODE_EA_ALLOC_COLLISION;
505 hash = ext2fs_ext_attr_hash_entry(entry,
506 start + entry->e_value_offs);
508 /* e_hash may be 0 in older inode's ea */
509 if (entry->e_hash != 0 && entry->e_hash != hash) {
510 pctx->num = entry->e_hash;
511 problem = PR_1_ATTR_HASH;
515 blk64_t quota_blocks;
517 problem = check_large_ea_inode(ctx, entry, pctx,
522 ea_ibody_quota->blocks += quota_blocks;
523 ea_ibody_quota->inodes++;
526 /* If EA value is stored in external inode then it does not
527 * consume space here */
528 if (entry->e_value_inum == 0)
529 remain -= entry->e_value_size;
531 entry = EXT2_EXT_ATTR_NEXT(entry);
534 if (region_allocate(region, (char *)entry - (char *)header,
536 problem = PR_1_INODE_EA_ALLOC_COLLISION;
543 * it seems like a corruption. it's very unlikely we could repair
544 * EA(s) in automatic fashion -bzzz
546 if (problem == 0 || !fix_problem(ctx, problem, pctx)) {
547 inc_ea_inode_refs(ctx, pctx,
548 (struct ext2_ext_attr_entry *)start, end);
552 /* simply remove all possible EA(s) */
553 *((__u32 *)header) = 0UL;
554 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
555 EXT2_INODE_SIZE(sb), "pass1");
556 ea_ibody_quota->blocks = 0;
557 ea_ibody_quota->inodes = 0;
560 static int check_inode_extra_negative_epoch(__u32 xtime, __u32 extra) {
561 return (xtime & (1U << 31)) != 0 &&
562 (extra & EXT4_EPOCH_MASK) == EXT4_EPOCH_MASK;
565 #define CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, xtime) \
566 check_inode_extra_negative_epoch(inode->i_##xtime, \
567 inode->i_##xtime##_extra)
569 /* When today's date is earlier than 2242, we assume that atimes,
570 * ctimes, crtimes, and mtimes with years in the range 2310..2378 are
571 * actually pre-1970 dates mis-encoded.
573 #define EXT4_EXTRA_NEGATIVE_DATE_CUTOFF 2 * (1LL << 32)
575 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx,
576 struct ea_quota *ea_ibody_quota)
578 struct ext2_super_block *sb = ctx->fs->super;
579 struct ext2_inode_large *inode;
583 ea_ibody_quota->blocks = 0;
584 ea_ibody_quota->inodes = 0;
586 inode = (struct ext2_inode_large *) pctx->inode;
587 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
588 /* this isn't large inode. so, nothing to check */
593 printf("inode #%u, i_extra_size %d\n", pctx->ino,
594 inode->i_extra_isize);
596 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
597 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
598 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
600 * For now we will allow i_extra_isize to be 0, but really
601 * implementations should never allow i_extra_isize to be 0
603 if (inode->i_extra_isize &&
604 (inode->i_extra_isize < min || inode->i_extra_isize > max ||
605 inode->i_extra_isize & 3)) {
606 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
608 if (inode->i_extra_isize < min || inode->i_extra_isize > max)
609 inode->i_extra_isize = ctx->want_extra_isize;
611 inode->i_extra_isize = (inode->i_extra_isize + 3) & ~3;
612 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
613 EXT2_INODE_SIZE(sb), "pass1");
616 /* check if there is no place for an EA header */
617 if (inode->i_extra_isize >= max - sizeof(__u32))
620 eamagic = IHDR(inode);
621 if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
622 (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
623 (inode->i_extra_isize < ctx->want_extra_isize)) {
624 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
625 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
626 EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
627 inode->i_extra_isize = ctx->want_extra_isize;
628 e2fsck_write_inode_full(ctx, pctx->ino,
629 (struct ext2_inode *)inode,
631 "check_inode_extra_space");
632 if (inode->i_extra_isize < ctx->min_extra_isize)
633 ctx->min_extra_isize = inode->i_extra_isize;
636 if (*eamagic == EXT2_EXT_ATTR_MAGIC)
637 check_ea_in_inode(ctx, pctx, ea_ibody_quota);
640 * If the inode's extended atime (ctime, crtime, mtime) is stored in
641 * the old, invalid format, repair it.
643 if (((sizeof(time_t) <= 4) ||
644 (((sizeof(time_t) > 4) &&
645 ctx->now < EXT4_EXTRA_NEGATIVE_DATE_CUTOFF))) &&
646 (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime) ||
647 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime) ||
648 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime) ||
649 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))) {
651 if (!fix_problem(ctx, PR_1_EA_TIME_OUT_OF_RANGE, pctx))
654 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime))
655 inode->i_atime_extra &= ~EXT4_EPOCH_MASK;
656 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime))
657 inode->i_ctime_extra &= ~EXT4_EPOCH_MASK;
658 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime))
659 inode->i_crtime_extra &= ~EXT4_EPOCH_MASK;
660 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))
661 inode->i_mtime_extra &= ~EXT4_EPOCH_MASK;
662 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
663 EXT2_INODE_SIZE(sb), "pass1");
668 static _INLINE_ int is_blocks_used(e2fsck_t ctx, blk64_t block,
673 /* used to avoid duplicate output from below */
674 retval = ext2fs_test_block_bitmap_range2_valid(ctx->block_found_map,
679 retval = ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num);
681 e2fsck_pass1_block_map_r_lock(ctx);
683 retval = ext2fs_test_block_bitmap_range2(
684 ctx->global_ctx->block_found_map, block, num);
685 e2fsck_pass1_block_map_r_unlock(ctx);
694 * Check to see if the inode might really be a directory, despite i_mode
696 * This is a lot of complexity for something for which I'm not really
697 * convinced happens frequently in the wild. If for any reason this
698 * causes any problems, take this code out.
699 * [tytso:20070331.0827EDT]
701 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
704 struct ext2_inode *inode = pctx->inode;
705 struct ext2_dir_entry *dirent;
708 unsigned int i, rec_len, not_device = 0;
713 * If the mode looks OK, we believe it. If the first block in
714 * the i_block array is 0, this cannot be a directory. If the
715 * inode is extent-mapped, it is still the case that the latter
716 * cannot be 0 - the magic number in the extent header would make
719 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
720 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
724 * Check the block numbers in the i_block array for validity:
725 * zero blocks are skipped (but the first one cannot be zero -
726 * see above), other blocks are checked against the first and
727 * max data blocks (from the the superblock) and against the
728 * block bitmap. Any invalid block found means this cannot be
731 * If there are non-zero blocks past the fourth entry, then
732 * this cannot be a device file: we remember that for the next
735 * For extent mapped files, we don't do any sanity checking:
736 * just try to get the phys block of logical block 0 and run
739 * For inline data files, we just try to get the size of inline
740 * data. If it's true, we will treat it as a directory.
743 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
744 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
745 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
748 unsigned int rec_len2;
749 struct ext2_dir_entry de;
751 if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
754 * If the size isn't a multiple of 4, it's probably not a
760 * If the first 10 bytes don't look like a directory entry,
761 * it's probably not a directory.
763 memcpy(&dotdot, inode->i_block, sizeof(dotdot));
764 memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
765 EXT2_DIR_REC_LEN(0));
766 dotdot = ext2fs_le32_to_cpu(dotdot);
767 de.inode = ext2fs_le32_to_cpu(de.inode);
768 de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
769 ext2fs_get_rec_len(ctx->fs, &de, &rec_len2);
770 if (dotdot >= ctx->fs->super->s_inodes_count ||
771 (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
772 dotdot != EXT2_ROOT_INO) ||
773 de.inode >= ctx->fs->super->s_inodes_count ||
774 (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
776 rec_len2 > EXT4_MIN_INLINE_DATA_SIZE -
777 EXT4_INLINE_DATA_DOTDOT_SIZE)
779 /* device files never have a "system.data" entry */
781 } else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
783 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
786 /* device files are never extent mapped */
789 for (i=0; i < EXT2_N_BLOCKS; i++) {
790 blk = inode->i_block[i];
796 if (blk < ctx->fs->super->s_first_data_block ||
797 blk >= ext2fs_blocks_count(ctx->fs->super) ||
798 is_blocks_used(ctx, blk, 1))
799 return; /* Invalid block, can't be dir */
801 blk = inode->i_block[0];
805 * If the mode says this is a device file and the i_links_count field
806 * is sane and we have not ruled it out as a device file previously,
807 * we declare it a device file, not a directory.
809 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
810 (inode->i_links_count == 1) && !not_device)
813 /* read the first block */
814 ehandler_operation(_("reading directory block"));
815 retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
816 ehandler_operation(0);
820 dirent = (struct ext2_dir_entry *) buf;
821 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
824 if ((ext2fs_dirent_name_len(dirent) != 1) ||
825 (dirent->name[0] != '.') ||
826 (dirent->inode != pctx->ino) ||
829 (rec_len >= ctx->fs->blocksize - 12))
832 dirent = (struct ext2_dir_entry *) (buf + rec_len);
833 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
836 if ((ext2fs_dirent_name_len(dirent) != 2) ||
837 (dirent->name[0] != '.') ||
838 (dirent->name[1] != '.') ||
844 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
845 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
846 e2fsck_write_inode_full(ctx, pctx->ino, inode,
847 EXT2_INODE_SIZE(ctx->fs->super),
848 "check_is_really_dir");
852 extern errcode_t e2fsck_setup_icount(e2fsck_t ctx, const char *icount_name,
853 int flags, ext2_icount_t hint,
856 unsigned int threshold;
857 unsigned int save_type;
865 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
867 profile_get_uint(ctx->profile, "scratch_files",
868 "numdirs_threshold", 0, 0, &threshold);
869 profile_get_boolean(ctx->profile, "scratch_files",
870 "icount", 0, 1, &enable);
872 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
874 num_dirs = 1024; /* Guess */
876 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
877 (!threshold || num_dirs > threshold)) {
878 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir,
883 e2fsck_set_bitmap_type(ctx->fs, EXT2FS_BMAP64_RBTREE, icount_name,
885 if (ctx->options & E2F_OPT_ICOUNT_FULLMAP)
886 flags |= EXT2_ICOUNT_OPT_FULLMAP;
887 retval = ext2fs_create_icount2(ctx->fs, flags, 0, hint, ret);
888 ctx->fs->default_bitmap_type = save_type;
892 static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
894 struct problem_context *pctx)
897 struct ext2_inode_large inode;
900 * Reread inode. If we don't see checksum error, then this inode
901 * has been fixed elsewhere.
903 ctx->stashed_ino = 0;
904 retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
906 if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
912 * Checksum still doesn't match. That implies that the inode passes
913 * all the sanity checks, so maybe the checksum is simply corrupt.
914 * See if the user will go for fixing that.
916 if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
920 e2fsck_pass1_fix_lock(ctx);
921 retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
923 e2fsck_pass1_fix_unlock(ctx);
927 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
928 struct problem_context *pctx, int needed_size)
930 struct ext2_ext_attr_header *header;
931 struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
932 char *start, name[4096], block_buf[4096];
933 int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
934 int in_inode = 1, error;
935 unsigned int freed_bytes = inode->i_extra_isize;
937 start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
938 inode->i_extra_isize + sizeof(__u32);
939 entry_ino = (struct ext2_ext_attr_entry *)start;
941 if (inode->i_file_acl) {
942 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
944 /* We have already checked this block, shouldn't happen */
946 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
949 header = BHDR(block_buf);
950 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
951 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
955 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
958 len = sizeof(entry->e_name);
959 entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
961 while (freed_bytes < needed_size) {
962 if (entry_size && name[0] != '\0') {
964 if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
965 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
966 EXT2_EXT_ATTR_SIZE(entry->e_value_size);
967 error = ext2fs_attr_set(ctx->fs, pctx->ino,
968 (struct ext2_inode *)inode,
969 index, name, 0, 0, 0);
971 freed_bytes += ea_size;
974 len = sizeof(entry->e_name);
975 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
976 entry = EXT2_EXT_ATTR_NEXT(entry);
977 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
980 len = sizeof(entry->e_name);
981 entry_size = ext2fs_attr_get_next_attr(entry,
982 index, name, len, 1);
987 if (!entry && index < EXT2_ATTR_INDEX_MAX)
988 entry = (struct ext2_ext_attr_entry *)start;
998 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
999 struct problem_context *pctx)
1001 int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
1005 retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
1006 ctx->want_extra_isize, &ret,
1008 if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
1009 goto mark_expand_eisize_map;
1011 e2fsck_write_inode_full(ctx, pctx->ino,
1012 (struct ext2_inode *)inode,
1013 EXT2_INODE_SIZE(ctx->fs->super),
1018 if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
1019 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
1020 fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
1021 ctx->flags |= E2F_FLAG_ABORT;
1026 pctx->num = ctx->fs->super->s_min_extra_isize;
1027 fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
1031 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
1033 if (retval >= ctx->want_extra_isize)
1036 needed_size -= retval;
1039 * We loop here until either the user deletes EA(s) or
1040 * EXTRA_ISIZE feature is disabled.
1042 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
1043 ctx->fs->super->s_feature_ro_compat &=
1044 ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
1045 ext2fs_mark_super_dirty(ctx->fs);
1049 ctx->fs_unexpanded_inodes++;
1051 /* No EA was deleted, inode cannot be expanded */
1055 mark_expand_eisize_map:
1056 if (!ctx->expand_eisize_map) {
1057 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1058 _("expand extrz isize map"),
1059 &ctx->expand_eisize_map);
1060 if (pctx->errcode) {
1061 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
1067 /* Add this inode to the expand_eisize_map */
1068 ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
1072 static void reserve_block_for_root_repair(e2fsck_t ctx)
1076 ext2_filsys fs = ctx->fs;
1078 ctx->root_repair_block = 0;
1079 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
1082 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1085 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1086 ctx->root_repair_block = blk;
1089 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
1093 ext2_filsys fs = ctx->fs;
1094 static const char name[] = "lost+found";
1097 ctx->lnf_repair_block = 0;
1098 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
1101 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1104 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1105 ctx->lnf_repair_block = blk;
1109 static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
1113 struct ext2_xattr_handle *handle;
1116 retval = ext2fs_xattrs_open(fs, ino, &handle);
1120 retval = ext2fs_xattrs_read(handle);
1124 retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
1127 ext2fs_free_mem(&p);
1129 (void) ext2fs_xattrs_close(&handle);
1133 static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
1134 struct problem_context *pctx,
1141 * If the inode failed the checksum and the user didn't
1142 * clear the inode, test the checksum again -- if it still
1143 * fails, ask the user if the checksum should be corrected.
1145 pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
1147 ctx->flags |= E2F_FLAG_ABORT;
1149 #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
1151 finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
1152 if ((ctx)->flags & E2F_FLAG_ABORT) { \
1153 e2fsck_pass1_check_unlock(ctx); \
1158 static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
1163 for (i = 0; i < EXT2_N_BLOCKS; i++) {
1164 x = inode->i_block[i];
1165 #ifdef WORDS_BIGENDIAN
1166 x = ext2fs_swab32(x);
1168 if (x >= ext2fs_blocks_count(fs->super))
1176 * Figure out what to do with an inode that has both extents and inline data
1177 * inode flags set. Returns -1 if we decide to erase the inode, 0 otherwise.
1179 static int fix_inline_data_extents_file(e2fsck_t ctx,
1181 struct ext2_inode *inode,
1183 struct problem_context *pctx)
1185 size_t max_inline_ea_size;
1186 ext2_filsys fs = ctx->fs;
1189 /* Both feature flags not set? Just run the regular checks */
1190 if (!ext2fs_has_feature_extents(fs->super) &&
1191 !ext2fs_has_feature_inline_data(fs->super))
1194 /* Clear both flags if it's a special file */
1195 if (LINUX_S_ISCHR(inode->i_mode) ||
1196 LINUX_S_ISBLK(inode->i_mode) ||
1197 LINUX_S_ISFIFO(inode->i_mode) ||
1198 LINUX_S_ISSOCK(inode->i_mode)) {
1199 check_extents_inlinedata(ctx, pctx);
1203 /* If it looks like an extent tree, try to clear inlinedata */
1204 if (ext2fs_extent_header_verify(inode->i_block,
1205 sizeof(inode->i_block)) == 0 &&
1206 fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
1207 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1212 /* If it looks short enough to be inline data, try to clear extents */
1213 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
1214 max_inline_ea_size = inode_size -
1215 (EXT2_GOOD_OLD_INODE_SIZE +
1216 ((struct ext2_inode_large *)inode)->i_extra_isize);
1218 max_inline_ea_size = 0;
1219 if (EXT2_I_SIZE(inode) <
1220 EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
1221 fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
1222 inode->i_flags &= ~EXT4_EXTENTS_FL;
1228 * Too big for inline data, but no evidence of extent tree -
1229 * maybe it's a block map file? If the mappings all look valid?
1231 if (could_be_block_map(fs, inode) &&
1232 fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
1233 #ifdef WORDS_BIGENDIAN
1236 for (i = 0; i < EXT2_N_BLOCKS; i++)
1237 inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
1240 inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
1245 /* Oh well, just clear the busted inode. */
1246 if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
1247 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1253 e2fsck_write_inode(ctx, ino, inode, "pass1");
1258 static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
1260 ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
1261 dgrp_t start = *group, grp, grp_end = ctx->fs->group_desc_count;
1262 blk64_t blocks_to_read = 0;
1263 errcode_t err = EXT2_ET_INVALID_ARGUMENT;
1266 if (ctx->fs->fs_num_threads > 1)
1267 grp_end = ctx->thread_info.et_group_end;
1269 if (ctx->readahead_kb == 0)
1272 /* Keep iterating groups until we have enough to readahead */
1273 inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
1274 for (grp = start; grp < grp_end; grp++) {
1275 if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
1277 inodes_in_group = ctx->fs->super->s_inodes_per_group -
1278 ext2fs_bg_itable_unused(ctx->fs, grp);
1279 blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
1281 if (blocks_to_read * ctx->fs->blocksize >
1282 ctx->readahead_kb * 1024)
1286 err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
1288 if (err == EAGAIN) {
1289 ctx->readahead_kb /= 2;
1295 /* Error; disable itable readahead */
1296 *group = ctx->fs->group_desc_count;
1297 *next_ino = ctx->fs->super->s_inodes_count;
1300 * Don't do more readahead until we've reached the first inode
1301 * of the last inode scan buffer block for the last group.
1304 inodes_per_buffer = (ctx->inode_buffer_blocks ?
1305 ctx->inode_buffer_blocks :
1306 EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
1307 ctx->fs->blocksize /
1308 EXT2_INODE_SIZE(ctx->fs->super);
1310 *next_ino = inodes_in_group -
1311 (inodes_in_group % inodes_per_buffer) + 1 +
1312 (grp * ctx->fs->super->s_inodes_per_group);
1317 * Check if the passed ino is one of the used superblock quota inodes.
1319 * Before the quota inodes were journaled, older superblock quota inodes
1320 * were just regular files in the filesystem and not reserved inodes. This
1321 * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1322 * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1324 static int quota_inum_is_super(struct ext2_super_block *sb, ext2_ino_t ino)
1326 enum quota_type qtype;
1328 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1329 if (*quota_sb_inump(sb, qtype) == ino)
1336 * Check if the passed ino is one of the reserved quota inodes.
1337 * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1338 * inodes. These inodes may or may not be in use by the quota feature.
1340 static int quota_inum_is_reserved(ext2_filsys fs, ext2_ino_t ino)
1342 enum quota_type qtype;
1344 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1345 if (quota_type2inum(qtype, fs->super) == ino)
1351 static int e2fsck_should_abort(e2fsck_t ctx)
1353 e2fsck_t global_ctx;
1355 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1358 if (ctx->global_ctx) {
1359 global_ctx = ctx->global_ctx;
1360 if (global_ctx->flags & E2F_FLAG_SIGNAL_MASK)
1366 static void init_ext2_max_sizes()
1372 * Init ext2_max_sizes which will be immutable and shared between
1375 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1377 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
1378 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
1379 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
1380 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
1381 max_sizes = (max_sizes * (1UL << i));
1382 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
1388 /* TODO: tdb needs to be handled properly for multiple threads*/
1389 static int multiple_threads_supported(e2fsck_t ctx)
1392 unsigned int threshold;
1393 ext2_ino_t num_dirs;
1398 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
1400 profile_get_uint(ctx->profile, "scratch_files",
1401 "numdirs_threshold", 0, 0, &threshold);
1402 profile_get_boolean(ctx->profile, "scratch_files",
1403 "icount", 0, 1, &enable);
1405 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
1407 num_dirs = 1024; /* Guess */
1409 /* tdb is unsupported now */
1410 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
1411 (!threshold || num_dirs > threshold))
1418 * Even though we could specify number of threads,
1419 * but it might be more than the whole filesystem
1420 * block groups, correct it here.
1422 static void e2fsck_pass1_set_thread_num(e2fsck_t ctx)
1424 unsigned flexbg_size = 1;
1425 ext2_filsys fs = ctx->fs;
1426 int num_threads = ctx->pfs_num_threads;
1429 if (num_threads < 1) {
1434 if (!multiple_threads_supported(ctx)) {
1436 fprintf(stderr, "Fall through single thread for pass1 "
1437 "because tdb could not handle properly\n");
1441 if (ext2fs_has_feature_flex_bg(fs->super))
1442 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1443 max_threads = fs->group_desc_count / flexbg_size;
1444 if (max_threads == 0)
1446 if (max_threads > E2FSCK_MAX_THREADS)
1447 max_threads = E2FSCK_MAX_THREADS;
1449 if (num_threads > max_threads) {
1450 fprintf(stderr, "Use max possible thread num: %d instead\n",
1452 num_threads = max_threads;
1455 ctx->pfs_num_threads = num_threads;
1456 ctx->fs->fs_num_threads = num_threads;
1461 * We need call mark_table_blocks() before multiple
1462 * thread start, since all known system blocks should be
1463 * marked and checked later.
1465 static errcode_t e2fsck_pass1_prepare(e2fsck_t ctx)
1467 struct problem_context pctx;
1468 ext2_filsys fs = ctx->fs;
1469 unsigned long long readahead_kb;
1471 init_ext2_max_sizes();
1473 e2fsck_pass1_set_thread_num(ctx);
1475 /* If we can do readahead, figure out how many groups to pull in. */
1476 if (!e2fsck_can_readahead(ctx->fs))
1477 ctx->readahead_kb = 0;
1478 else if (ctx->readahead_kb == ~0ULL)
1479 ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
1482 /* don't use more than 1/10 of memory for threads checking */
1483 readahead_kb = get_memory_size() / (10 * ctx->pfs_num_threads);
1484 /* maybe better disable RA if this is too small? */
1485 if (ctx->readahead_kb > readahead_kb)
1486 ctx->readahead_kb = readahead_kb;
1488 clear_problem_context(&pctx);
1489 if (!(ctx->options & E2F_OPT_PREEN))
1490 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1492 pctx.errcode = e2fsck_allocate_subcluster_bitmap(ctx->fs,
1493 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
1494 "block_found_map", &ctx->block_found_map);
1497 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1498 ctx->flags |= E2F_FLAG_ABORT;
1499 return pctx.errcode;
1501 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1502 _("metadata block map"), EXT2FS_BMAP64_RBTREE,
1503 "block_metadata_map", &ctx->block_metadata_map);
1506 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1507 ctx->flags |= E2F_FLAG_ABORT;
1508 return pctx.errcode;
1511 mark_table_blocks(ctx);
1512 pctx.errcode = ext2fs_convert_subcluster_bitmap(ctx->fs,
1513 &ctx->block_found_map);
1515 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1516 ctx->flags |= E2F_FLAG_ABORT;
1517 return pctx.errcode;
1520 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1521 _("multiply claimed block map"),
1522 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1523 &ctx->block_dup_map);
1526 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1528 /* Should never get here */
1529 ctx->flags |= E2F_FLAG_ABORT;
1530 return pctx.errcode;
1533 if (ext2fs_has_feature_mmp(fs->super) &&
1534 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1535 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1536 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1537 fs->super->s_mmp_block);
1539 pthread_rwlock_init(&ctx->fs_fix_rwlock, NULL);
1540 pthread_rwlock_init(&ctx->fs_block_map_rwlock, NULL);
1541 if (ctx->pfs_num_threads > 1)
1542 ctx->fs_need_locking = 1;
1548 static void e2fsck_pass1_post(e2fsck_t ctx)
1550 struct problem_context pctx;
1551 ext2_filsys fs = ctx->fs;
1554 if (e2fsck_should_abort(ctx))
1557 block_buf = (char *)e2fsck_allocate_memory(ctx, ctx->fs->blocksize * 3,
1558 "block interate buffer");
1559 reserve_block_for_root_repair(ctx);
1560 reserve_block_for_lnf_repair(ctx);
1563 * If any extended attribute blocks' reference counts need to
1564 * be adjusted, either up (ctx->refcount_extra), or down
1565 * (ctx->refcount), then fix them.
1567 if (ctx->refcount) {
1568 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1569 ea_refcount_free(ctx->refcount);
1572 if (ctx->refcount_extra) {
1573 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1575 ea_refcount_free(ctx->refcount_extra);
1576 ctx->refcount_extra = 0;
1579 if (ctx->invalid_bitmaps)
1580 handle_fs_bad_blocks(ctx);
1582 /* We don't need the block_ea_map any more */
1583 if (ctx->block_ea_map) {
1584 ext2fs_free_block_bitmap(ctx->block_ea_map);
1585 ctx->block_ea_map = 0;
1588 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1589 struct ext2_inode *inode;
1590 int inode_size = EXT2_INODE_SIZE(fs->super);
1591 inode = e2fsck_allocate_memory(ctx, inode_size,
1594 clear_problem_context(&pctx);
1595 pctx.errcode = ext2fs_create_resize_inode(fs);
1597 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1599 ctx->flags |= E2F_FLAG_ABORT;
1600 ext2fs_free_mem(&inode);
1601 ext2fs_free_mem(&block_buf);
1606 if (!pctx.errcode) {
1607 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1609 inode->i_mtime = ctx->now;
1610 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1613 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1614 ext2fs_free_mem(&inode);
1617 if (ctx->flags & E2F_FLAG_RESTART) {
1618 ext2fs_free_mem(&block_buf);
1622 if (ctx->block_dup_map) {
1623 if (!(ctx->flags & E2F_FLAG_DUP_BLOCK)) {
1624 ext2fs_free_mem(&block_buf);
1627 if (ctx->options & E2F_OPT_PREEN) {
1628 clear_problem_context(&pctx);
1629 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1631 e2fsck_pass1_dupblocks(ctx, block_buf);
1632 ext2fs_free_mem(&block_buf);
1633 ctx->flags &= ~E2F_FLAG_DUP_BLOCK;
1636 ctx->flags |= E2F_FLAG_ALLOC_OK;
1640 void e2fsck_pass1_run(e2fsck_t ctx)
1643 ext2_filsys fs = ctx->fs;
1645 struct ext2_inode *inode = NULL;
1646 ext2_inode_scan scan = NULL;
1647 char *block_buf = NULL;
1648 #ifdef RESOURCE_TRACK
1649 struct resource_track rtrack;
1651 unsigned char frag, fsize;
1652 struct problem_context pctx;
1653 struct scan_callback_struct scan_struct;
1654 struct ext2_super_block *sb = ctx->fs->super;
1656 const char *eop_next_inode = _("getting next inode from scan");
1657 int imagic_fs, extent_fs, inlinedata_fs, casefold_fs;
1658 int low_dtime_check = 1;
1659 unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
1660 unsigned int bufsize;
1661 int failed_csum = 0;
1662 ext2_ino_t ino_threshold = 0;
1663 dgrp_t ra_group = 0;
1664 struct ea_quota ea_ibody_quota;
1665 struct process_inode_block *inodes_to_process;
1666 int process_inode_count, check_mmp;
1667 e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
1670 init_resource_track(&rtrack, ctx->fs->io);
1671 clear_problem_context(&pctx);
1673 pass1_readahead(ctx, &ra_group, &ino_threshold);
1674 if (ext2fs_has_feature_dir_index(fs->super) &&
1675 !(ctx->options & E2F_OPT_NO)) {
1676 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
1677 ctx->dirs_to_hash = 0;
1681 mtrace_print("Pass 1");
1684 imagic_fs = ext2fs_has_feature_imagic_inodes(sb);
1685 extent_fs = ext2fs_has_feature_extents(sb);
1686 inlinedata_fs = ext2fs_has_feature_inline_data(sb);
1687 casefold_fs = ext2fs_has_feature_casefold(sb);
1690 * Allocate bitmaps structures
1692 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1693 EXT2FS_BMAP64_RBTREE,
1695 &ctx->inode_used_map);
1698 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1699 ctx->flags |= E2F_FLAG_ABORT;
1702 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1703 _("directory inode map"),
1704 ctx->global_ctx ? EXT2FS_BMAP64_RBTREE :
1705 EXT2FS_BMAP64_AUTODIR,
1706 "inode_dir_map", &ctx->inode_dir_map);
1709 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1710 ctx->flags |= E2F_FLAG_ABORT;
1713 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1714 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1715 "inode_reg_map", &ctx->inode_reg_map);
1718 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1719 ctx->flags |= E2F_FLAG_ABORT;
1724 e2fsck_allocate_inode_bitmap(fs,
1725 _("inode casefold map"),
1726 EXT2FS_BMAP64_RBTREE,
1727 "inode_casefold_map",
1728 &ctx->inode_casefold_map);
1731 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1732 ctx->flags |= E2F_FLAG_ABORT;
1736 pctx.errcode = e2fsck_setup_icount(ctx, "inode_link_info", 0, NULL,
1737 &ctx->inode_link_info);
1739 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1740 ctx->flags |= E2F_FLAG_ABORT;
1743 bufsize = inode_size;
1744 if (bufsize < sizeof(struct ext2_inode_large))
1745 bufsize = sizeof(struct ext2_inode_large);
1746 inode = (struct ext2_inode *)
1747 e2fsck_allocate_memory(ctx, bufsize, "scratch inode");
1749 inodes_to_process = (struct process_inode_block *)
1750 e2fsck_allocate_memory(ctx,
1751 (ctx->process_inode_size *
1752 sizeof(struct process_inode_block)),
1753 "array of inodes to process");
1754 process_inode_count = 0;
1756 pctx.errcode = ext2fs_init_dblist(fs, 0);
1758 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1759 ctx->flags |= E2F_FLAG_ABORT;
1764 * If the last orphan field is set, clear it, since the pass1
1765 * processing will automatically find and clear the orphans.
1766 * In the future, we may want to try using the last_orphan
1767 * linked list ourselves, but for now, we clear it so that the
1768 * ext3 mount code won't get confused.
1770 if (!(ctx->options & E2F_OPT_READONLY)) {
1771 if (fs->super->s_last_orphan) {
1772 fs->super->s_last_orphan = 0;
1773 ext2fs_mark_super_dirty(fs);
1777 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1778 "block interate buffer");
1779 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1780 e2fsck_use_inode_shortcuts(ctx, 1);
1781 e2fsck_intercept_block_allocations(ctx);
1782 old_op = ehandler_operation(_("opening inode scan"));
1783 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1785 ehandler_operation(old_op);
1787 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1788 ctx->flags |= E2F_FLAG_ABORT;
1791 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1792 EXT2_SF_WARN_GARBAGE_INODES, 0);
1793 ctx->stashed_inode = inode;
1794 scan_struct.ctx = ctx;
1795 scan_struct.block_buf = block_buf;
1796 scan_struct.inodes_to_process = inodes_to_process;
1797 scan_struct.process_inode_count = &process_inode_count;
1798 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1799 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1800 ctx->fs->group_desc_count)))
1802 if ((fs->super->s_wtime &&
1803 fs->super->s_wtime < fs->super->s_inodes_count) ||
1804 (fs->super->s_mtime &&
1805 fs->super->s_mtime < fs->super->s_inodes_count) ||
1806 (fs->super->s_mkfs_time &&
1807 fs->super->s_mkfs_time < fs->super->s_inodes_count))
1808 low_dtime_check = 0;
1810 /* Set up ctx->lost_and_found if possible */
1811 (void) e2fsck_get_lost_and_found(ctx, 0);
1814 if (ctx->global_ctx) {
1815 if (ctx->options & E2F_OPT_DEBUG &&
1816 ctx->options & E2F_OPT_MULTITHREAD)
1817 fprintf(stderr, "thread %d jumping to group %u\n",
1818 ctx->thread_info.et_thread_index,
1819 ctx->thread_info.et_group_start);
1820 pctx.errcode = ext2fs_inode_scan_goto_blockgroup(scan,
1821 ctx->thread_info.et_group_start);
1823 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1824 ctx->flags |= E2F_FLAG_ABORT;
1832 e2fsck_pass1_check_lock(ctx);
1834 if (!global_ctx->mmp_update_thread) {
1835 e2fsck_pass1_block_map_w_lock(ctx);
1836 if (!global_ctx->mmp_update_thread) {
1837 global_ctx->mmp_update_thread =
1838 ctx->thread_info.et_thread_index + 1;
1841 e2fsck_pass1_block_map_w_unlock(ctx);
1844 /* only one active thread could update mmp block. */
1845 e2fsck_pass1_block_map_r_lock(ctx);
1846 if (global_ctx->mmp_update_thread ==
1847 ctx->thread_info.et_thread_index + 1)
1849 e2fsck_pass1_block_map_r_unlock(ctx);
1854 if (check_mmp && (ino % (fs->super->s_inodes_per_group * 4) == 1)) {
1855 if (e2fsck_mmp_update(fs))
1856 fatal_error(ctx, 0);
1858 old_op = ehandler_operation(eop_next_inode);
1859 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1861 if (ino > ino_threshold)
1862 pass1_readahead(ctx, &ra_group, &ino_threshold);
1863 ehandler_operation(old_op);
1864 if (e2fsck_should_abort(ctx)) {
1865 e2fsck_pass1_check_unlock(ctx);
1868 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1870 * If badblocks says badblocks is bad, offer to clear
1871 * the list, update the in-core bb list, and restart
1874 if (ino == EXT2_BAD_INO &&
1875 fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1879 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1880 ext2fs_badblocks_list_free(ctx->fs->badblocks);
1881 ctx->fs->badblocks = NULL;
1882 err = ext2fs_read_bb_inode(ctx->fs,
1883 &ctx->fs->badblocks);
1885 fix_problem(ctx, PR_1_ISCAN_ERROR,
1887 ctx->flags |= E2F_FLAG_ABORT;
1888 e2fsck_pass1_check_unlock(ctx);
1891 ctx->flags |= E2F_FLAG_RESTART;
1892 err = ext2fs_inode_scan_goto_blockgroup(scan,
1895 fix_problem(ctx, PR_1_ISCAN_ERROR,
1897 ctx->flags |= E2F_FLAG_ABORT;
1898 e2fsck_pass1_check_unlock(ctx);
1901 e2fsck_pass1_check_unlock(ctx);
1904 if (!ctx->inode_bb_map)
1906 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1907 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1908 e2fsck_pass1_check_unlock(ctx);
1911 if (pctx.errcode == EXT2_ET_SCAN_FINISHED) {
1912 e2fsck_pass1_check_unlock(ctx);
1916 pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1917 pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1918 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1919 ctx->flags |= E2F_FLAG_ABORT;
1920 e2fsck_pass1_check_unlock(ctx);
1924 e2fsck_pass1_check_unlock(ctx);
1928 if (ctx->global_ctx)
1929 ctx->thread_info.et_inode_number++;
1933 ctx->stashed_ino = ino;
1935 /* Clear trashed inode? */
1936 if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
1937 inode->i_links_count > 0 &&
1938 fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
1940 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1942 failed_csum = pctx.errcode != 0;
1945 * Check for inodes who might have been part of the
1946 * orphaned list linked list. They should have gotten
1947 * dealt with by now, unless the list had somehow been
1950 * FIXME: In the future, inodes which are still in use
1951 * (and which are therefore) pending truncation should
1952 * be handled specially. Right now we just clear the
1953 * dtime field, and the normal e2fsck handling of
1954 * inodes where i_size and the inode blocks are
1955 * inconsistent is to fix i_size, instead of releasing
1956 * the extra blocks. This won't catch the inodes that
1957 * was at the end of the orphan list, but it's better
1958 * than nothing. The right answer is that there
1959 * shouldn't be any bugs in the orphan list handling. :-)
1961 if (inode->i_dtime && low_dtime_check &&
1962 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1963 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1964 inode->i_dtime = inode->i_links_count ?
1966 e2fsck_write_inode(ctx, ino, inode,
1972 if (inode->i_links_count) {
1973 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1974 ino, inode->i_links_count);
1976 pctx.num = inode->i_links_count;
1977 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1978 ctx->flags |= E2F_FLAG_ABORT;
1979 e2fsck_pass1_check_unlock(ctx);
1982 } else if ((ino >= EXT2_FIRST_INODE(fs->super)) &&
1983 !quota_inum_is_reserved(fs, ino)) {
1984 if (!inode->i_dtime && inode->i_mode) {
1985 if (fix_problem(ctx,
1986 PR_1_ZERO_DTIME, &pctx)) {
1987 inode->i_dtime = ctx->now;
1988 e2fsck_write_inode(ctx, ino, inode,
1993 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1994 e2fsck_pass1_check_unlock(ctx);
1998 if ((inode->i_flags & EXT4_CASEFOLD_FL) &&
1999 ((!LINUX_S_ISDIR(inode->i_mode) &&
2000 fix_problem(ctx, PR_1_CASEFOLD_NONDIR, &pctx)) ||
2002 fix_problem(ctx, PR_1_CASEFOLD_FEATURE, &pctx)))) {
2003 inode->i_flags &= ~EXT4_CASEFOLD_FL;
2004 e2fsck_write_inode(ctx, ino, inode, "pass1");
2007 /* Conflicting inlinedata/extents inode flags? */
2008 if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
2009 (inode->i_flags & EXT4_EXTENTS_FL)) {
2010 int res = fix_inline_data_extents_file(ctx, ino, inode,
2014 /* skip FINISH_INODE_LOOP */
2015 e2fsck_pass1_check_unlock(ctx);
2020 /* Test for incorrect inline_data flags settings. */
2021 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
2022 (ino >= EXT2_FIRST_INODE(fs->super))) {
2025 pctx.errcode = get_inline_data_ea_size(fs, ino, &size);
2026 if (!pctx.errcode &&
2027 fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
2028 e2fsck_pass1_fix_lock(ctx);
2029 ext2fs_set_feature_inline_data(sb);
2030 ext2fs_mark_super_dirty(fs);
2031 e2fsck_pass1_fix_unlock(ctx);
2033 } else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
2034 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2035 /* skip FINISH_INODE_LOOP */
2036 e2fsck_pass1_check_unlock(ctx);
2041 /* Test for inline data flag but no attr */
2042 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
2043 (ino >= EXT2_FIRST_INODE(fs->super))) {
2050 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2051 err = get_inline_data_ea_size(fs, ino, &size);
2052 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
2053 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
2057 /* Everything is awesome... */
2059 case EXT2_ET_BAD_EA_BLOCK_NUM:
2060 case EXT2_ET_BAD_EA_HASH:
2061 case EXT2_ET_BAD_EA_HEADER:
2062 case EXT2_ET_EA_BAD_NAME_LEN:
2063 case EXT2_ET_EA_BAD_VALUE_SIZE:
2064 case EXT2_ET_EA_KEY_NOT_FOUND:
2065 case EXT2_ET_EA_NO_SPACE:
2066 case EXT2_ET_MISSING_EA_FEATURE:
2067 case EXT2_ET_INLINE_DATA_CANT_ITERATE:
2068 case EXT2_ET_INLINE_DATA_NO_BLOCK:
2069 case EXT2_ET_INLINE_DATA_NO_SPACE:
2070 case EXT2_ET_NO_INLINE_DATA:
2071 case EXT2_ET_EXT_ATTR_CSUM_INVALID:
2072 case EXT2_ET_EA_BAD_VALUE_OFFSET:
2073 case EXT2_ET_EA_INODE_CORRUPTED:
2074 /* broken EA or no system.data EA; truncate */
2075 if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
2077 err = ext2fs_inode_size_set(fs, inode, 0);
2080 ctx->flags |= E2F_FLAG_ABORT;
2081 e2fsck_pass1_check_unlock(ctx);
2084 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
2085 memset(&inode->i_block, 0,
2086 sizeof(inode->i_block));
2087 e2fsck_write_inode(ctx, ino, inode,
2093 /* Some other kind of non-xattr error? */
2095 ctx->flags |= E2F_FLAG_ABORT;
2096 e2fsck_pass1_check_unlock(ctx);
2102 * Test for incorrect extent flag settings.
2104 * On big-endian machines we must be careful:
2105 * When the inode is read, the i_block array is not swapped
2106 * if the extent flag is set. Therefore if we are testing
2107 * for or fixing a wrongly-set flag, we must potentially
2108 * (un)swap before testing, or after fixing.
2112 * In this case the extents flag was set when read, so
2113 * extent_header_verify is ok. If the inode is cleared,
2114 * no need to swap... so no extra swapping here.
2116 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
2117 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2118 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
2119 if ((ext2fs_extent_header_verify(inode->i_block,
2120 sizeof(inode->i_block)) == 0) &&
2121 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
2122 e2fsck_pass1_fix_lock(ctx);
2123 ext2fs_set_feature_extents(sb);
2124 ext2fs_mark_super_dirty(fs);
2126 e2fsck_pass1_fix_unlock(ctx);
2127 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
2129 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2130 if (ino == EXT2_BAD_INO)
2131 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
2133 /* skip FINISH_INODE_LOOP */
2134 e2fsck_pass1_check_unlock(ctx);
2140 * For big-endian machines:
2141 * If the inode didn't have the extents flag set when it
2142 * was read, then the i_blocks array was swapped. To test
2143 * as an extents header, we must swap it back first.
2144 * IF we then set the extents flag, the entire i_block
2145 * array must be un/re-swapped to make it proper extents data.
2147 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
2148 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2149 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
2150 (LINUX_S_ISREG(inode->i_mode) ||
2151 LINUX_S_ISDIR(inode->i_mode))) {
2153 #ifdef WORDS_BIGENDIAN
2154 __u32 tmp_block[EXT2_N_BLOCKS];
2156 for (i = 0; i < EXT2_N_BLOCKS; i++)
2157 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
2160 ehp = inode->i_block;
2162 if ((ext2fs_extent_header_verify(ehp,
2163 sizeof(inode->i_block)) == 0) &&
2164 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
2165 inode->i_flags |= EXT4_EXTENTS_FL;
2166 #ifdef WORDS_BIGENDIAN
2167 memcpy(inode->i_block, tmp_block,
2168 sizeof(inode->i_block));
2170 e2fsck_write_inode(ctx, ino, inode, "pass1");
2175 if (ino == EXT2_BAD_INO) {
2176 struct process_block_struct pb;
2178 if ((failed_csum || inode->i_mode || inode->i_uid ||
2179 inode->i_gid || inode->i_links_count ||
2180 (inode->i_flags & EXT4_INLINE_DATA_FL) ||
2181 inode->i_file_acl) &&
2182 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
2183 memset(inode, 0, sizeof(struct ext2_inode));
2184 e2fsck_write_inode(ctx, ino, inode,
2189 e2fsck_pass1_block_map_r_lock(ctx);
2190 pctx.errcode = ext2fs_copy_bitmap(ctx->global_ctx ?
2191 ctx->global_ctx->block_found_map :
2192 ctx->block_found_map, &pb.fs_meta_blocks);
2193 e2fsck_pass1_block_map_r_unlock(ctx);
2196 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
2197 ctx->flags |= E2F_FLAG_ABORT;
2198 e2fsck_pass1_check_unlock(ctx);
2201 pb.ino = EXT2_BAD_INO;
2202 pb.num_blocks = pb.last_block = 0;
2203 pb.last_db_block = -1;
2204 pb.num_illegal_blocks = 0;
2205 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
2206 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
2210 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
2211 block_buf, process_bad_block, &pb);
2212 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
2214 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
2215 ctx->flags |= E2F_FLAG_ABORT;
2216 e2fsck_pass1_check_unlock(ctx);
2220 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
2221 ctx->flags |= E2F_FLAG_ABORT;
2222 e2fsck_pass1_check_unlock(ctx);
2225 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2226 clear_problem_context(&pctx);
2227 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2228 e2fsck_pass1_check_unlock(ctx);
2230 } else if (ino == EXT2_ROOT_INO) {
2232 * Make sure the root inode is a directory; if
2233 * not, offer to clear it. It will be
2234 * regenerated in pass #3.
2236 if (!LINUX_S_ISDIR(inode->i_mode)) {
2237 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
2241 * If dtime is set, offer to clear it. mke2fs
2242 * version 0.2b created filesystems with the
2243 * dtime field set for the root and lost+found
2244 * directories. We won't worry about
2245 * /lost+found, since that can be regenerated
2246 * easily. But we will fix the root directory
2247 * as a special case.
2249 if (inode->i_dtime && inode->i_links_count) {
2250 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
2252 e2fsck_write_inode(ctx, ino, inode,
2257 } else if (ino == EXT2_JOURNAL_INO) {
2258 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2259 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
2260 if (!LINUX_S_ISREG(inode->i_mode) &&
2261 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
2263 inode->i_mode = LINUX_S_IFREG;
2264 e2fsck_write_inode(ctx, ino, inode,
2268 check_blocks(ctx, &pctx, block_buf, NULL);
2269 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2270 e2fsck_pass1_check_unlock(ctx);
2273 if ((inode->i_links_count ||
2274 inode->i_blocks || inode->i_block[0]) &&
2275 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
2277 memset(inode, 0, inode_size);
2278 ext2fs_icount_store(ctx->inode_link_info,
2280 e2fsck_write_inode_full(ctx, ino, inode,
2281 inode_size, "pass1");
2284 } else if (quota_inum_is_reserved(fs, ino)) {
2285 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2286 if (ext2fs_has_feature_quota(fs->super) &&
2287 quota_inum_is_super(fs->super, ino)) {
2288 if (!LINUX_S_ISREG(inode->i_mode) &&
2289 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
2291 inode->i_mode = LINUX_S_IFREG;
2292 e2fsck_write_inode(ctx, ino, inode,
2296 check_blocks(ctx, &pctx, block_buf, NULL);
2297 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2298 e2fsck_pass1_check_unlock(ctx);
2301 if ((inode->i_links_count ||
2302 inode->i_blocks || inode->i_block[0]) &&
2303 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
2305 memset(inode, 0, inode_size);
2306 ext2fs_icount_store(ctx->inode_link_info,
2308 e2fsck_write_inode_full(ctx, ino, inode,
2309 inode_size, "pass1");
2312 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
2313 problem_t problem = 0;
2315 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2316 if (ino == EXT2_BOOT_LOADER_INO) {
2317 if (LINUX_S_ISDIR(inode->i_mode))
2318 problem = PR_1_RESERVED_BAD_MODE;
2319 } else if (ino == EXT2_RESIZE_INO) {
2320 if (inode->i_mode &&
2321 !LINUX_S_ISREG(inode->i_mode))
2322 problem = PR_1_RESERVED_BAD_MODE;
2324 if (inode->i_mode != 0)
2325 problem = PR_1_RESERVED_BAD_MODE;
2328 if (fix_problem(ctx, problem, &pctx)) {
2330 e2fsck_write_inode(ctx, ino, inode,
2335 check_blocks(ctx, &pctx, block_buf, NULL);
2336 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2337 e2fsck_pass1_check_unlock(ctx);
2341 if (!inode->i_links_count) {
2342 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2343 e2fsck_pass1_check_unlock(ctx);
2347 * n.b. 0.3c ext2fs code didn't clear i_links_count for
2348 * deleted files. Oops.
2350 * Since all new ext2 implementations get this right,
2351 * we now assume that the case of non-zero
2352 * i_links_count and non-zero dtime means that we
2353 * should keep the file, not delete it.
2356 if (inode->i_dtime) {
2357 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
2359 e2fsck_write_inode(ctx, ino, inode, "pass1");
2364 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2365 switch (fs->super->s_creator_os) {
2367 frag = inode->osd2.hurd2.h_i_frag;
2368 fsize = inode->osd2.hurd2.h_i_fsize;
2374 if (inode->i_faddr || frag || fsize ||
2375 (!ext2fs_has_feature_largedir(fs->super) &&
2376 (LINUX_S_ISDIR(inode->i_mode) && inode->i_size_high)))
2377 mark_inode_bad(ctx, ino);
2378 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2379 !ext2fs_has_feature_64bit(fs->super) &&
2380 inode->osd2.linux2.l_i_file_acl_high != 0)
2381 mark_inode_bad(ctx, ino);
2382 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2383 !ext2fs_has_feature_huge_file(fs->super) &&
2384 (inode->osd2.linux2.l_i_blocks_hi != 0))
2385 mark_inode_bad(ctx, ino);
2386 if (inode->i_flags & EXT2_IMAGIC_FL) {
2388 if (!ctx->inode_imagic_map)
2389 alloc_imagic_map(ctx);
2390 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
2393 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
2394 inode->i_flags &= ~EXT2_IMAGIC_FL;
2395 e2fsck_write_inode(ctx, ino,
2402 check_inode_extra_space(ctx, &pctx, &ea_ibody_quota);
2403 check_is_really_dir(ctx, &pctx, block_buf);
2406 * ext2fs_inode_has_valid_blocks2 does not actually look
2407 * at i_block[] values, so not endian-sensitive here.
2409 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
2410 LINUX_S_ISLNK(inode->i_mode) &&
2411 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
2412 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
2413 inode->i_flags &= ~EXT4_EXTENTS_FL;
2414 e2fsck_write_inode(ctx, ino, inode, "pass1");
2418 if ((inode->i_flags & EXT4_ENCRYPT_FL) &&
2419 add_encrypted_file(ctx, &pctx) < 0)
2422 if (casefold_fs && inode->i_flags & EXT4_CASEFOLD_FL)
2423 ext2fs_mark_inode_bitmap2(ctx->inode_casefold_map, ino);
2425 if (LINUX_S_ISDIR(inode->i_mode)) {
2426 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
2427 e2fsck_add_dir_info(ctx, ino, 0);
2428 ctx->fs_directory_count++;
2429 if (inode->i_flags & EXT4_CASEFOLD_FL)
2430 add_casefolded_dir(ctx, ino);
2431 } else if (LINUX_S_ISREG (inode->i_mode)) {
2432 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
2433 ctx->fs_regular_count++;
2434 } else if (LINUX_S_ISCHR (inode->i_mode) &&
2435 e2fsck_pass1_check_device_inode(fs, inode)) {
2436 check_extents_inlinedata(ctx, &pctx);
2437 check_immutable(ctx, &pctx);
2438 check_size(ctx, &pctx);
2439 ctx->fs_chardev_count++;
2440 } else if (LINUX_S_ISBLK (inode->i_mode) &&
2441 e2fsck_pass1_check_device_inode(fs, inode)) {
2442 check_extents_inlinedata(ctx, &pctx);
2443 check_immutable(ctx, &pctx);
2444 check_size(ctx, &pctx);
2445 ctx->fs_blockdev_count++;
2446 } else if (LINUX_S_ISLNK (inode->i_mode) &&
2447 e2fsck_pass1_check_symlink(fs, ino, inode,
2449 check_immutable(ctx, &pctx);
2450 ctx->fs_symlinks_count++;
2451 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
2452 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2453 e2fsck_pass1_check_unlock(ctx);
2455 } else if (ext2fs_is_fast_symlink(inode)) {
2456 ctx->fs_fast_symlinks_count++;
2457 check_blocks(ctx, &pctx, block_buf,
2459 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2460 e2fsck_pass1_check_unlock(ctx);
2464 else if (LINUX_S_ISFIFO (inode->i_mode) &&
2465 e2fsck_pass1_check_device_inode(fs, inode)) {
2466 check_extents_inlinedata(ctx, &pctx);
2467 check_immutable(ctx, &pctx);
2468 check_size(ctx, &pctx);
2469 ctx->fs_fifo_count++;
2470 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
2471 e2fsck_pass1_check_device_inode(fs, inode)) {
2472 check_extents_inlinedata(ctx, &pctx);
2473 check_immutable(ctx, &pctx);
2474 check_size(ctx, &pctx);
2475 ctx->fs_sockets_count++;
2477 mark_inode_bad(ctx, ino);
2478 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2479 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
2480 if (inode->i_block[EXT2_IND_BLOCK])
2481 ctx->fs_ind_count++;
2482 if (inode->i_block[EXT2_DIND_BLOCK])
2483 ctx->fs_dind_count++;
2484 if (inode->i_block[EXT2_TIND_BLOCK])
2485 ctx->fs_tind_count++;
2487 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2488 !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
2489 (inode->i_block[EXT2_IND_BLOCK] ||
2490 inode->i_block[EXT2_DIND_BLOCK] ||
2491 inode->i_block[EXT2_TIND_BLOCK] ||
2492 ext2fs_file_acl_block(fs, inode))) {
2493 struct process_inode_block *itp;
2495 itp = &inodes_to_process[process_inode_count];
2497 itp->ea_ibody_quota = ea_ibody_quota;
2498 if (inode_size < sizeof(struct ext2_inode_large))
2499 memcpy(&itp->inode, inode, inode_size);
2501 memcpy(&itp->inode, inode, sizeof(itp->inode));
2502 process_inode_count++;
2504 check_blocks(ctx, &pctx, block_buf, &ea_ibody_quota);
2506 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2508 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
2509 struct ext2_inode_large *inode_l;
2511 inode_l = (struct ext2_inode_large *)inode;
2513 if (inode_l->i_extra_isize < ctx->want_extra_isize) {
2514 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
2515 inode_exp = e2fsck_pass1_expand_eisize(ctx,
2519 if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
2521 ctx->min_extra_isize = inode_l->i_extra_isize;
2524 if (e2fsck_should_abort(ctx)) {
2525 e2fsck_pass1_check_unlock(ctx);
2529 if (process_inode_count >= ctx->process_inode_size) {
2530 process_inodes(ctx, block_buf, inodes_to_process,
2531 &process_inode_count);
2533 if (e2fsck_should_abort(ctx)) {
2534 e2fsck_pass1_check_unlock(ctx);
2538 e2fsck_pass1_check_unlock(ctx);
2540 process_inodes(ctx, block_buf, inodes_to_process,
2541 &process_inode_count);
2542 ext2fs_close_inode_scan(scan);
2545 if (ctx->ea_block_quota_blocks) {
2546 ea_refcount_free(ctx->ea_block_quota_blocks);
2547 ctx->ea_block_quota_blocks = 0;
2550 if (ctx->ea_block_quota_inodes) {
2551 ea_refcount_free(ctx->ea_block_quota_inodes);
2552 ctx->ea_block_quota_inodes = 0;
2555 /* We don't need the encryption policy => ID map any more */
2556 destroy_encryption_policy_map(ctx);
2558 if (ctx->flags & E2F_FLAG_RESTART) {
2560 * Only the master copy of the superblock and block
2561 * group descriptors are going to be written during a
2562 * restart, so set the superblock to be used to be the
2563 * master superblock.
2565 ctx->use_superblock = 0;
2569 if (ctx->large_dirs && !ext2fs_has_feature_largedir(fs->super)) {
2570 if (fix_problem(ctx, PR_2_FEATURE_LARGE_DIRS, &pctx)) {
2571 ext2fs_set_feature_largedir(fs->super);
2572 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2573 ext2fs_mark_super_dirty(fs);
2575 if (fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
2576 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
2577 ext2fs_update_dynamic_rev(fs);
2578 ext2fs_mark_super_dirty(fs);
2582 ctx->flags |= E2F_FLAG_ALLOC_OK;
2583 ext2fs_free_mem(&inodes_to_process);
2585 e2fsck_use_inode_shortcuts(ctx, 0);
2586 ext2fs_free_mem(&inodes_to_process);
2587 inodes_to_process = 0;
2590 ext2fs_close_inode_scan(scan);
2592 ext2fs_free_mem(&block_buf);
2594 ext2fs_free_mem(&inode);
2597 * The l+f inode may have been cleared, so zap it now and
2598 * later passes will recalculate it if necessary
2600 ctx->lost_and_found = 0;
2602 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
2603 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
2605 ctx->invalid_bitmaps++;
2607 /* reset update_thread after this thread exit */
2608 e2fsck_pass1_block_map_w_lock(ctx);
2610 global_ctx->mmp_update_thread = 0;
2611 e2fsck_pass1_block_map_w_unlock(ctx);
2616 static errcode_t e2fsck_pass1_copy_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2617 ext2fs_generic_bitmap *dest)
2621 ret = ext2fs_copy_bitmap(*src, dest);
2630 static void e2fsck_pass1_free_bitmap(ext2fs_generic_bitmap *bitmap)
2633 ext2fs_free_generic_bmap(*bitmap);
2639 static errcode_t e2fsck_pass1_merge_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2640 ext2fs_generic_bitmap *dest)
2645 if (*dest == NULL) {
2649 ret = ext2fs_merge_bitmap(*src, *dest, NULL, NULL);
2659 static errcode_t e2fsck_pass1_copy_fs(ext2_filsys dest, e2fsck_t src_context,
2664 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2665 dest->inode_map = NULL;
2666 dest->block_map = NULL;
2667 dest->badblocks = NULL;
2669 dest->dblist->fs = dest;
2670 if (src->block_map) {
2671 retval = e2fsck_pass1_copy_bitmap(dest, &src->block_map,
2676 if (src->inode_map) {
2677 retval = e2fsck_pass1_copy_bitmap(dest, &src->inode_map,
2683 if (src->badblocks) {
2684 retval = ext2fs_badblocks_copy(src->badblocks,
2690 /* disable it for now */
2691 src_context->openfs_flags &= ~EXT2_FLAG_EXCLUSIVE;
2692 retval = ext2fs_open_channel(dest, src_context->io_options,
2693 src_context->io_manager,
2694 src_context->openfs_flags,
2695 src->io->block_size);
2699 /* Block size might not be default */
2700 io_channel_set_blksize(dest->io, src->io->block_size);
2701 ehandler_init(dest->io);
2703 assert(dest->io->magic == src->io->magic);
2704 assert(dest->io->manager == src->io->manager);
2705 assert(strcmp(dest->io->name, src->io->name) == 0);
2706 assert(dest->io->block_size == src->io->block_size);
2707 assert(dest->io->read_error == src->io->read_error);
2708 assert(dest->io->write_error == src->io->write_error);
2709 assert(dest->io->refcount == src->io->refcount);
2710 assert(dest->io->flags == src->io->flags);
2711 assert(dest->io->app_data == dest);
2712 assert(src->io->app_data == src);
2713 assert(dest->io->align == src->io->align);
2715 /* The data should be written to disk immediately */
2716 dest->io->flags |= CHANNEL_FLAGS_WRITETHROUGH;
2717 /* icache will be rebuilt if needed, so do not copy from @src */
2722 static int e2fsck_pass1_merge_fs(ext2_filsys dest, ext2_filsys src)
2724 struct ext2_inode_cache *icache = dest->icache;
2725 errcode_t retval = 0;
2727 io_channel dest_image_io;
2728 ext2fs_inode_bitmap inode_map;
2729 ext2fs_block_bitmap block_map;
2730 ext2_badblocks_list badblocks;
2733 e2fsck_t dest_ctx = dest->priv_data;
2736 dest_image_io = dest->image_io;
2737 inode_map = dest->inode_map;
2738 block_map = dest->block_map;
2739 badblocks = dest->badblocks;
2740 dblist = dest->dblist;
2741 flags = dest->flags;
2743 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2745 dest->image_io = dest_image_io;
2746 dest->icache = icache;
2747 dest->inode_map = inode_map;
2748 dest->block_map = block_map;
2749 dest->badblocks = badblocks;
2750 dest->dblist = dblist;
2751 dest->priv_data = dest_ctx;
2753 dest->dblist->fs = dest;
2754 dest->flags = src->flags | flags;
2755 if (!(src->flags & EXT2_FLAG_VALID) || !(flags & EXT2_FLAG_VALID))
2756 ext2fs_unmark_valid(dest);
2759 ext2fs_free_inode_cache(src->icache);
2763 retval = e2fsck_pass1_merge_bitmap(dest, &src->inode_map,
2768 retval = e2fsck_pass1_merge_bitmap(dest, &src->block_map,
2775 retval = ext2fs_merge_dblist(src->dblist,
2780 dest->dblist = src->dblist;
2781 dest->dblist->fs = dest;
2786 if (src->badblocks) {
2787 if (dest->badblocks == NULL)
2788 retval = ext2fs_badblocks_copy(src->badblocks,
2791 retval = ext2fs_badblocks_merge(src->badblocks,
2795 io_channel_close(src->io);
2797 ext2fs_free_generic_bmap(src->inode_map);
2799 ext2fs_free_generic_bmap(src->block_map);
2801 ext2fs_badblocks_list_free(src->badblocks);
2803 ext2fs_free_dblist(src->dblist);
2808 static void e2fsck_pass1_copy_invalid_bitmaps(e2fsck_t global_ctx,
2809 e2fsck_t thread_ctx)
2812 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2813 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2814 dgrp_t total = grp_end - grp_start;
2816 thread_ctx->invalid_inode_bitmap_flag =
2817 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2818 "invalid_inode_bitmap");
2819 thread_ctx->invalid_block_bitmap_flag =
2820 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2821 "invalid_block_bitmap");
2822 thread_ctx->invalid_inode_table_flag =
2823 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2824 "invalid_inode_table");
2826 memcpy(thread_ctx->invalid_block_bitmap_flag,
2827 &global_ctx->invalid_block_bitmap_flag[grp_start],
2828 total * sizeof(int));
2829 memcpy(thread_ctx->invalid_inode_bitmap_flag,
2830 &global_ctx->invalid_inode_bitmap_flag[grp_start],
2831 total * sizeof(int));
2832 memcpy(thread_ctx->invalid_inode_table_flag,
2833 &global_ctx->invalid_inode_table_flag[grp_start],
2834 total * sizeof(int));
2836 thread_ctx->invalid_bitmaps = 0;
2837 for (i = grp_start, j = 0; i < grp_end; i++, j++) {
2838 if (thread_ctx->invalid_block_bitmap_flag[j])
2839 thread_ctx->invalid_bitmaps++;
2840 if (thread_ctx->invalid_inode_bitmap_flag[j])
2841 thread_ctx->invalid_bitmaps++;
2842 if (thread_ctx->invalid_inode_table_flag[j])
2843 thread_ctx->invalid_bitmaps++;
2847 static void e2fsck_pass1_merge_invalid_bitmaps(e2fsck_t global_ctx,
2848 e2fsck_t thread_ctx)
2850 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2851 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2852 dgrp_t total = grp_end - grp_start;
2854 memcpy(&global_ctx->invalid_block_bitmap_flag[grp_start],
2855 thread_ctx->invalid_block_bitmap_flag, total * sizeof(int));
2856 memcpy(&global_ctx->invalid_inode_bitmap_flag[grp_start],
2857 thread_ctx->invalid_inode_bitmap_flag, total * sizeof(int));
2858 memcpy(&global_ctx->invalid_inode_table_flag[grp_start],
2859 thread_ctx->invalid_inode_table_flag, total * sizeof(int));
2860 global_ctx->invalid_bitmaps += thread_ctx->invalid_bitmaps;
2863 static errcode_t e2fsck_pass1_thread_prepare(e2fsck_t global_ctx, e2fsck_t *thread_ctx,
2864 int thread_index, int num_threads,
2865 dgrp_t average_group)
2868 e2fsck_t thread_context;
2869 ext2_filsys thread_fs;
2870 ext2_filsys global_fs = global_ctx->fs;
2871 struct e2fsck_thread *tinfo;
2873 assert(global_ctx->inode_used_map == NULL);
2874 assert(global_ctx->inode_dir_map == NULL);
2875 assert(global_ctx->inode_bb_map == NULL);
2876 assert(global_ctx->inode_imagic_map == NULL);
2877 assert(global_ctx->inode_reg_map == NULL);
2878 assert(global_ctx->inodes_to_rebuild == NULL);
2880 assert(global_ctx->block_found_map != NULL);
2881 assert(global_ctx->block_metadata_map != NULL);
2882 assert(global_ctx->block_dup_map != NULL);
2883 assert(global_ctx->block_ea_map == NULL);
2884 assert(global_ctx->fs->dblist == NULL);
2886 retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &thread_context);
2888 com_err(global_ctx->program_name, retval, "while allocating memory");
2891 memcpy(thread_context, global_ctx, sizeof(struct e2fsck_struct));
2892 thread_context->block_dup_map = NULL;
2893 thread_context->casefolded_dirs = NULL;
2895 retval = e2fsck_allocate_block_bitmap(global_ctx->fs,
2896 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
2897 "block_found_map", &thread_context->block_found_map);
2901 thread_context->global_ctx = global_ctx;
2902 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &thread_fs);
2904 com_err(global_ctx->program_name, retval, "while allocating memory");
2908 io_channel_flush_cleanup(global_fs->io);
2909 retval = e2fsck_pass1_copy_fs(thread_fs, global_ctx, global_fs);
2911 com_err(global_ctx->program_name, retval, "while copying fs");
2914 thread_fs->priv_data = thread_context;
2916 thread_context->thread_info.et_thread_index = thread_index;
2917 set_up_logging(thread_context);
2919 tinfo = &thread_context->thread_info;
2920 tinfo->et_group_start = average_group * thread_index;
2921 if (thread_index == global_fs->fs_num_threads - 1)
2922 tinfo->et_group_end = thread_fs->group_desc_count;
2924 tinfo->et_group_end = average_group * (thread_index + 1);
2925 tinfo->et_group_next = tinfo->et_group_start;
2926 tinfo->et_inode_number = 0;
2927 tinfo->et_log_buf[0] = '\0';
2928 tinfo->et_log_length = 0;
2929 if (thread_context->options & E2F_OPT_MULTITHREAD)
2930 log_out(thread_context, _("Scan group range [%d, %d)\n"),
2931 tinfo->et_group_start, tinfo->et_group_end);
2932 thread_context->fs = thread_fs;
2933 retval = quota_init_context(&thread_context->qctx, thread_fs, 0);
2935 com_err(global_ctx->program_name, retval,
2936 "while init quota context");
2939 *thread_ctx = thread_context;
2940 e2fsck_pass1_copy_invalid_bitmaps(global_ctx, thread_context);
2943 ext2fs_free_mem(&thread_fs);
2945 if (thread_context->block_found_map)
2946 ext2fs_free_mem(&thread_context->block_found_map);
2947 ext2fs_free_mem(&thread_context);
2951 static void e2fsck_pass1_merge_dir_info(e2fsck_t global_ctx, e2fsck_t thread_ctx)
2953 if (thread_ctx->dir_info == NULL)
2956 if (global_ctx->dir_info == NULL) {
2957 global_ctx->dir_info = thread_ctx->dir_info;
2958 thread_ctx->dir_info = NULL;
2962 e2fsck_merge_dir_info(global_ctx, thread_ctx->dir_info,
2963 global_ctx->dir_info);
2966 static void e2fsck_pass1_merge_dx_dir(e2fsck_t global_ctx, e2fsck_t thread_ctx)
2968 if (thread_ctx->dx_dir_info == NULL)
2971 if (global_ctx->dx_dir_info == NULL) {
2972 global_ctx->dx_dir_info = thread_ctx->dx_dir_info;
2973 global_ctx->dx_dir_info_size = thread_ctx->dx_dir_info_size;
2974 global_ctx->dx_dir_info_count = thread_ctx->dx_dir_info_count;
2975 thread_ctx->dx_dir_info = NULL;
2979 e2fsck_merge_dx_dir(global_ctx, thread_ctx);
2982 static inline errcode_t
2983 e2fsck_pass1_merge_icount(ext2_icount_t *dest_icount,
2984 ext2_icount_t *src_icount)
2987 if (*dest_icount == NULL) {
2988 *dest_icount = *src_icount;
2993 ret = ext2fs_icount_merge(*src_icount,
3003 static errcode_t e2fsck_pass1_merge_icounts(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3007 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_count,
3008 &thread_ctx->inode_count);
3011 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_link_info,
3012 &thread_ctx->inode_link_info);
3017 static errcode_t e2fsck_pass1_merge_dirs_to_hash(e2fsck_t global_ctx,
3018 e2fsck_t thread_ctx)
3020 errcode_t retval = 0;
3022 if (!thread_ctx->dirs_to_hash)
3025 if (!global_ctx->dirs_to_hash)
3026 retval = ext2fs_badblocks_copy(thread_ctx->dirs_to_hash,
3027 &global_ctx->dirs_to_hash);
3029 retval = ext2fs_badblocks_merge(thread_ctx->dirs_to_hash,
3030 global_ctx->dirs_to_hash);
3035 static errcode_t e2fsck_pass1_merge_ea_inode_refs(e2fsck_t global_ctx,
3036 e2fsck_t thread_ctx)
3042 if (!thread_ctx->ea_inode_refs)
3045 if (!global_ctx->ea_inode_refs) {
3046 global_ctx->ea_inode_refs = thread_ctx->ea_inode_refs;
3047 thread_ctx->ea_inode_refs = NULL;
3051 ea_refcount_intr_begin(thread_ctx->ea_inode_refs);
3053 if ((blk = ea_refcount_intr_next(thread_ctx->ea_inode_refs,
3056 if (!global_ctx->block_ea_map ||
3057 !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
3059 retval = ea_refcount_store(global_ctx->ea_inode_refs,
3069 static ea_value_t ea_refcount_usage(e2fsck_t ctx, blk64_t blk,
3072 ea_value_t count_cur;
3073 ea_value_t count_extra = 0;
3074 ea_value_t count_orig;
3076 ea_refcount_fetch(ctx->refcount_orig, blk, &count_orig);
3077 ea_refcount_fetch(ctx->refcount, blk, &count_cur);
3078 /* most of time this is not needed */
3079 if (ctx->refcount_extra && count_cur == 0)
3080 ea_refcount_fetch(ctx->refcount_extra, blk, &count_extra);
3087 return count_orig + count_extra - count_cur;
3090 static errcode_t e2fsck_pass1_merge_ea_refcount(e2fsck_t global_ctx,
3091 e2fsck_t thread_ctx)
3095 errcode_t retval = 0;
3097 if (!thread_ctx->refcount)
3100 if (!global_ctx->refcount) {
3101 global_ctx->refcount = thread_ctx->refcount;
3102 thread_ctx->refcount = NULL;
3103 global_ctx->refcount_extra = thread_ctx->refcount;
3104 thread_ctx->refcount_extra = NULL;
3108 ea_refcount_intr_begin(thread_ctx->refcount);
3110 if ((blk = ea_refcount_intr_next(thread_ctx->refcount,
3114 * this EA has never seen before, so just store its
3115 * refcount and refcount_extra into global_ctx if needed.
3117 if (!global_ctx->block_ea_map ||
3118 !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
3122 retval = ea_refcount_store(global_ctx->refcount,
3127 if (count > 0 || !thread_ctx->refcount_extra)
3129 ea_refcount_fetch(thread_ctx->refcount_extra, blk,
3134 if (!global_ctx->refcount_extra) {
3135 retval = ea_refcount_create(0,
3136 &global_ctx->refcount_extra);
3140 retval = ea_refcount_store(global_ctx->refcount_extra,
3146 ea_value_t thread_usage;
3147 ea_value_t global_usage;
3150 thread_usage = ea_refcount_usage(thread_ctx,
3152 global_usage = ea_refcount_usage(global_ctx,
3154 if (thread_usage + global_usage <= orig) {
3155 new = orig - thread_usage - global_usage;
3156 retval = ea_refcount_store(global_ctx->refcount,
3162 /* update it is as zero */
3163 retval = ea_refcount_store(global_ctx->refcount,
3167 /* Ooops, this EA was referenced more than it stated */
3168 if (!global_ctx->refcount_extra) {
3169 retval = ea_refcount_create(0,
3170 &global_ctx->refcount_extra);
3174 new = global_usage + thread_usage - orig;
3175 retval = ea_refcount_store(global_ctx->refcount_extra,
3185 static errcode_t e2fsck_pass1_merge_casefolded_dirs(e2fsck_t global_ctx,
3186 e2fsck_t thread_ctx)
3188 errcode_t retval = 0;
3190 if (!thread_ctx->casefolded_dirs)
3193 if (!global_ctx->casefolded_dirs)
3194 retval = ext2fs_badblocks_copy(thread_ctx->casefolded_dirs,
3195 &global_ctx->casefolded_dirs);
3197 retval = ext2fs_badblocks_merge(thread_ctx->casefolded_dirs,
3198 global_ctx->casefolded_dirs);
3203 static errcode_t e2fsck_pass1_merge_context(e2fsck_t global_ctx,
3204 e2fsck_t thread_ctx)
3206 ext2_filsys global_fs = global_ctx->fs;
3210 global_ctx->fs_directory_count += thread_ctx->fs_directory_count;
3211 global_ctx->fs_regular_count += thread_ctx->fs_regular_count;
3212 global_ctx->fs_blockdev_count += thread_ctx->fs_blockdev_count;
3213 global_ctx->fs_chardev_count += thread_ctx->fs_chardev_count;
3214 global_ctx->fs_links_count += thread_ctx->fs_links_count;
3215 global_ctx->fs_symlinks_count += thread_ctx->fs_symlinks_count;
3216 global_ctx->fs_fast_symlinks_count += thread_ctx->fs_fast_symlinks_count;
3217 global_ctx->fs_fifo_count += thread_ctx->fs_fifo_count;
3218 global_ctx->fs_total_count += thread_ctx->fs_total_count;
3219 global_ctx->fs_badblocks_count += thread_ctx->fs_badblocks_count;
3220 global_ctx->fs_sockets_count += thread_ctx->fs_sockets_count;
3221 global_ctx->fs_ind_count += thread_ctx->fs_ind_count;
3222 global_ctx->fs_dind_count += thread_ctx->fs_dind_count;
3223 global_ctx->fs_tind_count += thread_ctx->fs_tind_count;
3224 global_ctx->fs_fragmented += thread_ctx->fs_fragmented;
3225 global_ctx->fs_fragmented_dir += thread_ctx->fs_fragmented_dir;
3226 global_ctx->large_files += thread_ctx->large_files;
3227 /* threads might enable E2F_OPT_YES */
3228 global_ctx->options |= thread_ctx->options;
3229 global_ctx->flags |= thread_ctx->flags;
3231 * The l+f inode may have been cleared, so zap it now and
3232 * later passes will recalculate it if necessary
3234 global_ctx->lost_and_found = 0;
3235 /* merge extent depth count */
3236 for (i = 0; i < MAX_EXTENT_DEPTH_COUNT; i++)
3237 global_ctx->extent_depth_count[i] +=
3238 thread_ctx->extent_depth_count[i];
3240 e2fsck_pass1_merge_dir_info(global_ctx, thread_ctx);
3241 e2fsck_pass1_merge_dx_dir(global_ctx, thread_ctx);
3243 retval = e2fsck_pass1_merge_fs(global_ctx->fs, thread_ctx->fs);
3245 com_err(global_ctx->program_name, 0, _("while merging fs\n"));
3248 retval = e2fsck_pass1_merge_icounts(global_ctx, thread_ctx);
3250 com_err(global_ctx->program_name, 0,
3251 _("while merging icounts\n"));
3255 retval = e2fsck_pass1_merge_dirs_to_hash(global_ctx, thread_ctx);
3257 com_err(global_ctx->program_name, 0,
3258 _("while merging dirs to hash\n"));
3262 e2fsck_pass1_merge_ea_inode_refs(global_ctx, thread_ctx);
3263 e2fsck_pass1_merge_ea_refcount(global_ctx, thread_ctx);
3264 retval = quota_merge_and_update_usage(global_ctx->qctx,
3269 retval = e2fsck_pass1_merge_casefolded_dirs(global_ctx, thread_ctx);
3271 com_err(global_ctx->program_name, 0,
3272 _("while merging casefolded dirs\n"));
3276 e2fsck_pass1_merge_invalid_bitmaps(global_ctx, thread_ctx);
3278 retval = e2fsck_pass1_merge_bitmap(global_fs,
3279 &thread_ctx->inode_used_map,
3280 &global_ctx->inode_used_map);
3284 retval = e2fsck_pass1_merge_bitmap(global_fs,
3285 &thread_ctx->inode_bad_map,
3286 &global_ctx->inode_bad_map);
3289 retval = e2fsck_pass1_merge_bitmap(global_fs,
3290 &thread_ctx->inode_dir_map,
3291 &global_ctx->inode_dir_map);
3294 retval = e2fsck_pass1_merge_bitmap(global_fs,
3295 &thread_ctx->inode_bb_map,
3296 &global_ctx->inode_bb_map);
3299 retval = e2fsck_pass1_merge_bitmap(global_fs,
3300 &thread_ctx->inode_imagic_map,
3301 &global_ctx->inode_imagic_map);
3304 retval = e2fsck_pass1_merge_bitmap(global_fs,
3305 &thread_ctx->inode_reg_map,
3306 &global_ctx->inode_reg_map);
3309 retval = e2fsck_pass1_merge_bitmap(global_fs,
3310 &thread_ctx->inodes_to_rebuild,
3311 &global_ctx->inodes_to_rebuild);
3314 retval = e2fsck_pass1_merge_bitmap(global_fs,
3315 &thread_ctx->block_ea_map,
3316 &global_ctx->block_ea_map);
3320 if (ext2fs_has_feature_shared_blocks(global_fs->super) &&
3321 !(global_ctx->options & E2F_OPT_UNSHARE_BLOCKS))
3324 * This need be done after merging block_ea_map
3325 * because ea block might be shared, we need exclude
3326 * them from dup blocks.
3328 e2fsck_pass1_block_map_w_lock(thread_ctx);
3329 retval = ext2fs_merge_bitmap(thread_ctx->block_found_map,
3330 global_ctx->block_found_map,
3331 global_ctx->block_dup_map,
3332 global_ctx->block_ea_map);
3333 e2fsck_pass1_block_map_w_unlock(thread_ctx);
3334 if (retval == EEXIST)
3335 global_ctx->flags |= E2F_FLAG_DUP_BLOCK;
3340 static int e2fsck_pass1_thread_join(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3344 retval = e2fsck_pass1_merge_context(global_ctx, thread_ctx);
3345 ext2fs_free_mem(&thread_ctx->fs);
3346 if (thread_ctx->logf)
3347 fclose(thread_ctx->logf);
3348 if (thread_ctx->problem_logf) {
3349 fputs("</problem_log>\n", thread_ctx->problem_logf);
3350 fclose(thread_ctx->problem_logf);
3353 quota_release_context(&thread_ctx->qctx);
3355 * @block_metadata_map and @block_dup_map are
3356 * shared, so we don't free them.
3358 thread_ctx->block_metadata_map = NULL;
3359 thread_ctx->block_dup_map = NULL;
3360 e2fsck_reset_context(thread_ctx);
3361 ext2fs_free_mem(&thread_ctx);
3366 static int e2fsck_pass1_threads_join(e2fsck_t global_ctx)
3370 struct e2fsck_thread_info *infos = global_ctx->infos;
3371 struct e2fsck_thread_info *pinfo;
3372 int num_threads = global_ctx->pfs_num_threads;
3375 /* merge invalid bitmaps will recalculate it */
3376 global_ctx->invalid_bitmaps = 0;
3377 for (i = 0; i < num_threads; i++) {
3380 if (!pinfo->eti_started)
3383 rc = pthread_join(pinfo->eti_thread_id, NULL);
3385 com_err(global_ctx->program_name, rc,
3386 _("while joining thread\n"));
3390 rc = e2fsck_pass1_thread_join(global_ctx, infos[i].eti_thread_ctx);
3392 com_err(global_ctx->program_name, rc,
3393 _("while joining pass1 thread\n"));
3399 global_ctx->infos = NULL;
3404 static void *e2fsck_pass1_thread(void *arg)
3406 struct e2fsck_thread_info *info = arg;
3407 e2fsck_t thread_ctx = info->eti_thread_ctx;
3408 #ifdef DEBUG_THREADS
3409 struct e2fsck_thread_debug *thread_debug = info->eti_debug;
3412 #ifdef DEBUG_THREADS
3413 pthread_mutex_lock(&thread_debug->etd_mutex);
3414 while (info->eti_thread_index > thread_debug->etd_finished_threads) {
3415 pthread_cond_wait(&thread_debug->etd_cond,
3416 &thread_debug->etd_mutex);
3418 pthread_mutex_unlock(&thread_debug->etd_mutex);
3421 #ifdef HAVE_SETJMP_H
3423 * When fatal_error() happens, jump to here. The thread
3424 * context's flags will be saved, but its abort_loc will
3425 * be overwritten by original jump buffer for the later
3428 if (setjmp(thread_ctx->abort_loc)) {
3429 thread_ctx->flags &= ~E2F_FLAG_SETJMP_OK;
3432 thread_ctx->flags |= E2F_FLAG_SETJMP_OK;
3435 e2fsck_pass1_run(thread_ctx);
3438 if (thread_ctx->options & E2F_OPT_MULTITHREAD)
3440 _("Scanned group range [%u, %u), inodes %u\n"),
3441 thread_ctx->thread_info.et_group_start,
3442 thread_ctx->thread_info.et_group_end,
3443 thread_ctx->thread_info.et_inode_number);
3445 #ifdef DEBUG_THREADS
3446 pthread_mutex_lock(&thread_debug->etd_mutex);
3447 thread_debug->etd_finished_threads++;
3448 pthread_cond_broadcast(&thread_debug->etd_cond);
3449 pthread_mutex_unlock(&thread_debug->etd_mutex);
3455 static dgrp_t ext2fs_get_avg_group(ext2_filsys fs)
3458 dgrp_t average_group;
3459 unsigned flexbg_size;
3461 if (fs->fs_num_threads <= 1)
3462 return fs->group_desc_count;
3464 average_group = fs->group_desc_count / fs->fs_num_threads;
3465 if (average_group <= 1)
3468 if (ext2fs_has_feature_flex_bg(fs->super)) {
3471 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3472 if (average_group % flexbg_size) {
3473 times = average_group / flexbg_size;
3474 average_group = times * flexbg_size;
3478 return average_group;
3480 return fs->group_desc_count;
3484 static int e2fsck_pass1_threads_start(e2fsck_t global_ctx)
3486 struct e2fsck_thread_info *infos;
3487 pthread_attr_t attr;
3490 struct e2fsck_thread_info *tmp_pinfo;
3492 e2fsck_t thread_ctx;
3493 dgrp_t average_group;
3494 int num_threads = global_ctx->pfs_num_threads;
3495 #ifdef DEBUG_THREADS
3496 struct e2fsck_thread_debug thread_debug =
3497 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0};
3499 thread_debug.etd_finished_threads = 0;
3502 retval = pthread_attr_init(&attr);
3504 com_err(global_ctx->program_name, retval,
3505 _("while setting pthread attribute\n"));
3509 infos = calloc(num_threads, sizeof(struct e2fsck_thread_info));
3510 if (infos == NULL) {
3512 com_err(global_ctx->program_name, retval,
3513 _("while allocating memory for threads\n"));
3514 pthread_attr_destroy(&attr);
3517 global_ctx->infos = infos;
3519 average_group = ext2fs_get_avg_group(global_ctx->fs);
3520 for (i = 0; i < num_threads; i++) {
3521 tmp_pinfo = &infos[i];
3522 tmp_pinfo->eti_thread_index = i;
3523 #ifdef DEBUG_THREADS
3524 tmp_pinfo->eti_debug = &thread_debug;
3526 retval = e2fsck_pass1_thread_prepare(global_ctx, &thread_ctx,
3530 com_err(global_ctx->program_name, retval,
3531 _("while preparing pass1 thread\n"));
3534 tmp_pinfo->eti_thread_ctx = thread_ctx;
3536 retval = pthread_create(&tmp_pinfo->eti_thread_id, &attr,
3537 &e2fsck_pass1_thread, tmp_pinfo);
3539 com_err(global_ctx->program_name, retval,
3540 _("while creating thread\n"));
3541 e2fsck_pass1_thread_join(global_ctx, thread_ctx);
3545 tmp_pinfo->eti_started = 1;
3548 /* destroy the thread attribute object, since it is no longer needed */
3549 ret = pthread_attr_destroy(&attr);
3551 com_err(global_ctx->program_name, ret,
3552 _("while destroying thread attribute\n"));
3558 e2fsck_pass1_threads_join(global_ctx);
3564 static void e2fsck_pass1_multithread(e2fsck_t global_ctx)
3568 retval = e2fsck_pass1_threads_start(global_ctx);
3570 com_err(global_ctx->program_name, retval,
3571 _("while starting pass1 threads\n"));
3575 retval = e2fsck_pass1_threads_join(global_ctx);
3577 com_err(global_ctx->program_name, retval,
3578 _("while joining pass1 threads\n"));
3583 global_ctx->flags |= E2F_FLAG_ABORT;
3588 void e2fsck_pass1(e2fsck_t ctx)
3591 int need_single = 1;
3593 retval = e2fsck_pass1_prepare(ctx);
3597 if (ctx->pfs_num_threads > 1 || ctx->options & E2F_OPT_MULTITHREAD) {
3599 e2fsck_pass1_multithread(ctx);
3601 /* No lock is needed at this time */
3602 ctx->fs_need_locking = 0;
3605 e2fsck_pass1_run(ctx);
3606 e2fsck_pass1_post(ctx);
3609 #undef FINISH_INODE_LOOP
3612 * When the inode_scan routines call this callback at the end of the
3613 * glock group, call process_inodes.
3615 static errcode_t scan_callback(ext2_filsys fs,
3616 ext2_inode_scan scan EXT2FS_ATTR((unused)),
3617 dgrp_t group, void * priv_data)
3619 struct scan_callback_struct *scan_struct;
3621 dgrp_t cur = group + 1;
3622 struct e2fsck_thread *tinfo;
3623 struct e2fsck_thread_info *pinfo, *infos;
3626 scan_struct = (struct scan_callback_struct *) priv_data;
3627 ctx = scan_struct->ctx;
3629 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf,
3630 scan_struct->inodes_to_process,
3631 scan_struct->process_inode_count);
3634 if (ctx->global_ctx) {
3636 infos = ctx->global_ctx->infos;
3637 for (i = 0; i < ctx->global_ctx->pfs_num_threads; i++) {
3640 if (!pinfo->eti_started)
3643 tinfo = &pinfo->eti_thread_ctx->thread_info;
3644 if (ctx == pinfo->eti_thread_ctx)
3645 cur += group + 1 - tinfo->et_group_start;
3647 cur += tinfo->et_group_next -
3648 tinfo->et_group_start;
3654 if ((ctx->progress)(ctx, 1, cur,
3655 ctx->fs->group_desc_count))
3656 return EXT2_ET_CANCEL_REQUESTED;
3659 if (ctx->global_ctx) {
3660 tinfo = &ctx->thread_info;
3661 tinfo->et_group_next++;
3662 if (ctx->options & E2F_OPT_DEBUG &&
3663 ctx->options & E2F_OPT_MULTITHREAD)
3664 log_out(ctx, _("group %d finished\n"),
3665 tinfo->et_group_next);
3666 if (tinfo->et_group_next >= tinfo->et_group_end)
3667 return EXT2_ET_SCAN_FINISHED;
3675 * Process the inodes in the "inodes to process" list.
3677 static void process_inodes(e2fsck_t ctx, char *block_buf,
3678 struct process_inode_block *inodes_to_process,
3679 int *process_inode_count)
3682 struct ext2_inode *old_stashed_inode;
3683 ext2_ino_t old_stashed_ino;
3684 const char *old_operation;
3686 struct problem_context pctx;
3689 printf("begin process_inodes: ");
3691 if (*process_inode_count == 0)
3693 old_operation = ehandler_operation(0);
3694 old_stashed_inode = ctx->stashed_inode;
3695 old_stashed_ino = ctx->stashed_ino;
3696 qsort(inodes_to_process, *process_inode_count,
3697 sizeof(struct process_inode_block), process_inode_cmp);
3698 clear_problem_context(&pctx);
3699 for (i=0; i < *process_inode_count; i++) {
3700 pctx.inode = ctx->stashed_inode =
3701 (struct ext2_inode *) &inodes_to_process[i].inode;
3702 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
3705 printf("%u ", pctx.ino);
3707 sprintf(buf, _("reading indirect blocks of inode %u"),
3709 ehandler_operation(buf);
3710 check_blocks(ctx, &pctx, block_buf,
3711 &inodes_to_process[i].ea_ibody_quota);
3712 if (e2fsck_should_abort(ctx))
3715 ctx->stashed_inode = old_stashed_inode;
3716 ctx->stashed_ino = old_stashed_ino;
3717 *process_inode_count = 0;
3719 printf("end process inodes\n");
3721 ehandler_operation(old_operation);
3724 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
3726 const struct process_inode_block *ib_a =
3727 (const struct process_inode_block *) a;
3728 const struct process_inode_block *ib_b =
3729 (const struct process_inode_block *) b;
3732 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
3733 ib_b->inode.i_block[EXT2_IND_BLOCK]);
3736 * We only call process_inodes() for non-extent
3737 * inodes, so it's OK to pass NULL to
3738 * ext2fs_file_acl_block() here.
3740 ret = ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a->inode)) -
3741 ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b->inode));
3743 ret = ib_a->ino - ib_b->ino;
3748 * Mark an inode as being bad in some what
3750 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
3752 struct problem_context pctx;
3754 if (!ctx->inode_bad_map) {
3755 clear_problem_context(&pctx);
3757 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3758 _("bad inode map"), EXT2FS_BMAP64_RBTREE,
3759 "inode_bad_map", &ctx->inode_bad_map);
3762 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3763 /* Should never get here */
3764 ctx->flags |= E2F_FLAG_ABORT;
3768 ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
3771 static void add_casefolded_dir(e2fsck_t ctx, ino_t ino)
3773 struct problem_context pctx;
3775 if (!ctx->casefolded_dirs) {
3776 pctx.errcode = ext2fs_u32_list_create(&ctx->casefolded_dirs, 0);
3780 pctx.errcode = ext2fs_u32_list_add(ctx->casefolded_dirs, ino);
3781 if (pctx.errcode == 0)
3784 fix_problem(ctx, PR_1_ALLOCATE_CASEFOLDED_DIRLIST, &pctx);
3785 /* Should never get here */
3786 ctx->flags |= E2F_FLAG_ABORT;
3790 * This procedure will allocate the inode "bb" (badblock) map table
3792 static void alloc_bb_map(e2fsck_t ctx)
3794 struct problem_context pctx;
3796 clear_problem_context(&pctx);
3797 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3798 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
3799 "inode_bb_map", &ctx->inode_bb_map);
3802 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3803 /* Should never get here */
3804 ctx->flags |= E2F_FLAG_ABORT;
3810 * This procedure will allocate the inode imagic table
3812 static void alloc_imagic_map(e2fsck_t ctx)
3814 struct problem_context pctx;
3816 clear_problem_context(&pctx);
3817 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3818 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
3819 "inode_imagic_map", &ctx->inode_imagic_map);
3822 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3823 /* Should never get here */
3824 ctx->flags |= E2F_FLAG_ABORT;
3830 * Marks a block as in use, setting the dup_map if it's been set
3831 * already. Called by process_block and process_bad_block.
3833 * WARNING: Assumes checks have already been done to make sure block
3834 * is valid. This is true in both process_block and process_bad_block.
3836 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
3838 struct problem_context pctx;
3839 e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
3841 clear_problem_context(&pctx);
3843 if (is_blocks_used(ctx, block, 1)) {
3844 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
3845 !(ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
3848 ctx->flags |= E2F_FLAG_DUP_BLOCK;
3849 e2fsck_pass1_block_map_w_lock(ctx);
3850 ext2fs_fast_mark_block_bitmap2(global_ctx->block_dup_map, block);
3851 e2fsck_pass1_block_map_w_unlock(ctx);
3853 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
3858 * When cluster size is greater than one block, it is caller's responsibility
3859 * to make sure block parameter starts at a cluster boundary.
3861 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
3864 if (!is_blocks_used(ctx, block, num)) {
3865 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
3869 for (i = 0; i < num; i += EXT2FS_CLUSTER_RATIO(ctx->fs))
3870 mark_block_used(ctx, block + i);
3874 static errcode_t _INLINE_ e2fsck_write_ext_attr3(e2fsck_t ctx, blk64_t block,
3875 void *inbuf, ext2_ino_t inum)
3878 ext2_filsys fs = ctx->fs;
3880 e2fsck_pass1_fix_lock(ctx);
3881 retval = ext2fs_write_ext_attr3(fs, block, inbuf, inum);
3882 e2fsck_pass1_fix_unlock(ctx);
3887 * Adjust the extended attribute block's reference counts at the end
3888 * of pass 1, either by subtracting out references for EA blocks that
3889 * are still referenced in ctx->refcount, or by adding references for
3890 * EA blocks that had extra references as accounted for in
3891 * ctx->refcount_extra.
3893 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
3894 char *block_buf, int adjust_sign)
3896 struct ext2_ext_attr_header *header;
3897 struct problem_context pctx;
3898 ext2_filsys fs = ctx->fs;
3903 clear_problem_context(&pctx);
3905 ea_refcount_intr_begin(refcount);
3907 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
3910 pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
3912 /* We already checked this block, shouldn't happen */
3914 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
3917 header = BHDR(block_buf);
3918 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
3919 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
3923 pctx.blkcount = header->h_refcount;
3924 should_be = header->h_refcount + adjust_sign * (int)count;
3925 pctx.num = should_be;
3926 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
3927 header->h_refcount = should_be;
3928 pctx.errcode = e2fsck_write_ext_attr3(ctx, blk,
3932 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
3941 * Handle processing the extended attribute blocks
3943 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
3944 char *block_buf, struct ea_quota *ea_block_quota)
3946 ext2_filsys fs = ctx->fs;
3947 ext2_ino_t ino = pctx->ino;
3948 struct ext2_inode *inode = pctx->inode;
3951 struct ext2_ext_attr_header *header;
3952 struct ext2_ext_attr_entry *first, *entry;
3953 blk64_t quota_blocks = EXT2FS_C2B(fs, 1);
3954 __u64 quota_inodes = 0;
3955 region_t region = 0;
3956 int failed_csum = 0;
3958 ea_block_quota->blocks = 0;
3959 ea_block_quota->inodes = 0;
3961 blk = ext2fs_file_acl_block(fs, inode);
3966 * If the Extended attribute flag isn't set, then a non-zero
3967 * file acl means that the inode is corrupted.
3969 * Or if the extended attribute block is an invalid block,
3970 * then the inode is also corrupted.
3972 if (!ext2fs_has_feature_xattr(fs->super) ||
3973 (blk < fs->super->s_first_data_block) ||
3974 (blk >= ext2fs_blocks_count(fs->super))) {
3975 mark_inode_bad(ctx, ino);
3979 /* If ea bitmap hasn't been allocated, create it */
3980 if (!ctx->block_ea_map) {
3981 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
3982 _("ext attr block map"),
3983 EXT2FS_BMAP64_RBTREE, "block_ea_map",
3984 &ctx->block_ea_map);
3985 if (pctx->errcode) {
3987 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
3988 ctx->flags |= E2F_FLAG_ABORT;
3993 /* Create the EA refcount structure if necessary */
3994 if (!ctx->refcount) {
3995 pctx->errcode = ea_refcount_create(0,
3996 &ctx->refcount_orig);
3997 if (pctx->errcode) {
3999 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4000 ctx->flags |= E2F_FLAG_ABORT;
4004 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
4005 if (pctx->errcode) {
4007 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4008 ctx->flags |= E2F_FLAG_ABORT;
4014 /* Debugging text */
4015 printf("Inode %u has EA block %u\n", ino, blk);
4018 /* Have we seen this EA block before? */
4019 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
4020 ea_block_quota->blocks = EXT2FS_C2B(fs, 1);
4021 ea_block_quota->inodes = 0;
4023 if (ctx->ea_block_quota_blocks) {
4024 ea_refcount_fetch(ctx->ea_block_quota_blocks, blk,
4027 ea_block_quota->blocks = quota_blocks;
4030 if (ctx->ea_block_quota_inodes)
4031 ea_refcount_fetch(ctx->ea_block_quota_inodes, blk,
4032 &ea_block_quota->inodes);
4034 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
4036 /* Ooops, this EA was referenced more than it stated */
4037 if (!ctx->refcount_extra) {
4038 pctx->errcode = ea_refcount_create(0,
4039 &ctx->refcount_extra);
4040 if (pctx->errcode) {
4042 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4043 ctx->flags |= E2F_FLAG_ABORT;
4047 ea_refcount_increment(ctx->refcount_extra, blk, 0);
4052 * OK, we haven't seen this EA block yet. So we need to
4056 pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
4057 if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
4060 } else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
4063 if (pctx->errcode &&
4064 fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
4068 header = BHDR(block_buf);
4069 pctx->blk = ext2fs_file_acl_block(fs, inode);
4070 if (((ctx->ext_attr_ver == 1) &&
4071 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
4072 ((ctx->ext_attr_ver == 2) &&
4073 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
4074 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
4078 if (header->h_blocks != 1) {
4079 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
4083 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
4086 region = region_create(0, fs->blocksize);
4088 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
4089 ctx->flags |= E2F_FLAG_ABORT;
4092 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
4093 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4097 first = (struct ext2_ext_attr_entry *)(header+1);
4098 end = block_buf + fs->blocksize;
4100 while ((char *)entry < end && *(__u32 *)entry) {
4103 if (region_allocate(region, (char *)entry - (char *)header,
4104 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
4105 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4109 if ((ctx->ext_attr_ver == 1 &&
4110 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
4111 (ctx->ext_attr_ver == 2 &&
4112 entry->e_name_index == 0)) {
4113 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
4117 if (entry->e_value_inum == 0) {
4118 if (entry->e_value_size > EXT2_XATTR_SIZE_MAX ||
4119 (entry->e_value_offs + entry->e_value_size >
4121 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
4125 if (entry->e_value_size &&
4126 region_allocate(region, entry->e_value_offs,
4127 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
4128 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
4133 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
4134 entry->e_value_offs);
4136 if (entry->e_hash != hash) {
4137 pctx->num = entry->e_hash;
4138 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
4140 entry->e_hash = hash;
4144 blk64_t entry_quota_blocks;
4146 problem = check_large_ea_inode(ctx, entry, pctx,
4147 &entry_quota_blocks);
4148 if (problem && fix_problem(ctx, problem, pctx))
4151 quota_blocks += entry_quota_blocks;
4155 entry = EXT2_EXT_ATTR_NEXT(entry);
4157 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
4158 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4161 region_free(region);
4164 * We only get here if there was no other errors that were fixed.
4165 * If there was a checksum fail, ask to correct it.
4168 fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
4169 pctx->errcode = e2fsck_write_ext_attr3(ctx, blk, block_buf,
4175 if (quota_blocks != EXT2FS_C2B(fs, 1U)) {
4176 if (!ctx->ea_block_quota_blocks) {
4177 pctx->errcode = ea_refcount_create(0,
4178 &ctx->ea_block_quota_blocks);
4179 if (pctx->errcode) {
4184 ea_refcount_store(ctx->ea_block_quota_blocks, blk,
4189 if (!ctx->ea_block_quota_inodes) {
4190 pctx->errcode = ea_refcount_create(0,
4191 &ctx->ea_block_quota_inodes);
4192 if (pctx->errcode) {
4195 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4196 ctx->flags |= E2F_FLAG_ABORT;
4201 ea_refcount_store(ctx->ea_block_quota_inodes, blk,
4204 ea_block_quota->blocks = quota_blocks;
4205 ea_block_quota->inodes = quota_inodes;
4207 inc_ea_inode_refs(ctx, pctx, first, end);
4208 ea_refcount_store(ctx->refcount, blk, header->h_refcount - 1);
4209 ea_refcount_store(ctx->refcount_orig, blk, header->h_refcount);
4211 * It might be racy that this block has been merged in the
4214 if (!is_blocks_used(ctx, blk, 1))
4215 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, blk);
4216 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
4221 region_free(region);
4222 ext2fs_file_acl_block_set(fs, inode, 0);
4223 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
4227 /* Returns 1 if bad htree, 0 if OK */
4228 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
4229 ext2_ino_t ino, struct ext2_inode *inode,
4232 struct ext2_dx_root_info *root;
4233 ext2_filsys fs = ctx->fs;
4237 if ((!LINUX_S_ISDIR(inode->i_mode) &&
4238 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
4239 (!ext2fs_has_feature_dir_index(fs->super) &&
4240 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
4243 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
4245 if ((pctx->errcode) ||
4247 (blk < fs->super->s_first_data_block) ||
4248 (blk >= ext2fs_blocks_count(fs->super))) {
4249 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4255 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
4256 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4259 /* XXX should check that beginning matches a directory */
4260 root = (struct ext2_dx_root_info *) (block_buf + 24);
4262 if ((root->reserved_zero || root->info_length < 8) &&
4263 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4266 pctx->num = root->hash_version;
4267 if ((root->hash_version != EXT2_HASH_LEGACY) &&
4268 (root->hash_version != EXT2_HASH_HALF_MD4) &&
4269 (root->hash_version != EXT2_HASH_TEA) &&
4270 (root->hash_version != EXT2_HASH_SIPHASH) &&
4271 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
4274 if (ext4_hash_in_dirent(inode)) {
4275 if (root->hash_version != EXT2_HASH_SIPHASH &&
4276 fix_problem(ctx, PR_1_HTREE_NEEDS_SIPHASH, pctx))
4279 if (root->hash_version == EXT2_HASH_SIPHASH &&
4280 fix_problem(ctx, PR_1_HTREE_CANNOT_SIPHASH, pctx))
4284 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
4285 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
4288 pctx->num = root->indirect_levels;
4289 /* if htree level is clearly too high, consider it to be broken */
4290 if (root->indirect_levels > EXT4_HTREE_LEVEL &&
4291 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4294 /* if level is only maybe too high, LARGE_DIR feature could be unset */
4295 if (root->indirect_levels > ext2_dir_htree_level(fs) &&
4296 !ext2fs_has_feature_largedir(fs->super)) {
4297 int blockbits = EXT2_BLOCK_SIZE_BITS(fs->super) + 10;
4298 unsigned idx_pb = 1 << (blockbits - 3);
4300 /* compare inode size/blocks vs. max-sized 2-level htree */
4301 if (EXT2_I_SIZE(pctx->inode) <
4302 (idx_pb - 1) * (idx_pb - 2) << blockbits &&
4303 pctx->inode->i_blocks <
4304 (idx_pb - 1) * (idx_pb - 2) << (blockbits - 9) &&
4305 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4309 if (root->indirect_levels > EXT4_HTREE_LEVEL_COMPAT ||
4310 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
4316 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
4317 struct ext2_inode *inode, int restart_flag,
4321 inode->i_links_count = 0;
4322 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
4323 inode->i_dtime = ctx->now;
4326 * If a special inode has such rotten block mappings that we
4327 * want to clear the whole inode, be sure to actually zap
4328 * the block maps because i_links_count isn't checked for
4329 * special inodes, and we'll end up right back here the next
4332 if (ino < EXT2_FIRST_INODE(ctx->fs->super))
4333 memset(inode->i_block, 0, sizeof(inode->i_block));
4335 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
4336 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
4337 if (ctx->inode_reg_map)
4338 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
4339 if (ctx->inode_bad_map)
4340 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
4343 * If the inode was partially accounted for before processing
4344 * was aborted, we need to restart the pass 1 scan.
4346 ctx->flags |= restart_flag;
4348 if (ino == EXT2_BAD_INO)
4349 memset(inode, 0, sizeof(struct ext2_inode));
4351 e2fsck_write_inode(ctx, ino, inode, source);
4355 * Use the multiple-blocks reclamation code to fix alignment problems in
4356 * a bigalloc filesystem. We want a logical cluster to map to *only* one
4357 * physical cluster, and we want the block offsets within that cluster to
4360 static int has_unaligned_cluster_map(e2fsck_t ctx,
4361 blk64_t last_pblk, blk64_t last_lblk,
4362 blk64_t pblk, blk64_t lblk)
4364 blk64_t cluster_mask;
4366 if (!ctx->fs->cluster_ratio_bits)
4368 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
4371 * If the block in the logical cluster doesn't align with the block in
4372 * the physical cluster...
4374 if ((lblk & cluster_mask) != (pblk & cluster_mask))
4378 * If we cross a physical cluster boundary within a logical cluster...
4380 if (last_pblk && (lblk & cluster_mask) != 0 &&
4381 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
4382 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
4388 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
4389 struct process_block_struct *pb,
4390 blk64_t start_block, blk64_t end_block,
4392 ext2_extent_handle_t ehandle,
4395 struct ext2fs_extent extent;
4396 blk64_t blk, last_lblk;
4398 int is_dir, is_leaf;
4400 struct ext2_extent_info info;
4401 int failed_csum = 0;
4403 if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
4406 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
4409 if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
4410 !pb->eti.force_rebuild &&
4411 info.curr_level < MAX_EXTENT_DEPTH_COUNT) {
4412 struct extent_tree_level *etl;
4414 etl = pb->eti.ext_info + info.curr_level;
4415 etl->num_extents += info.num_entries;
4416 etl->max_extents += info.max_entries;
4418 * Implementation wart: Splitting extent blocks when appending
4419 * will leave the old block with one free entry. Therefore
4420 * unless the node is totally full, pretend that a non-root
4421 * extent block can hold one fewer entry than it actually does,
4422 * so that we don't repeatedly rebuild the extent tree.
4424 if (info.curr_level && info.num_entries < info.max_entries)
4428 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
4430 while ((pctx->errcode == 0 ||
4431 pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
4432 info.num_entries-- > 0) {
4433 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
4434 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
4435 last_lblk = extent.e_lblk + extent.e_len - 1;
4438 pctx->blk = extent.e_pblk;
4439 pctx->blk2 = extent.e_lblk;
4440 pctx->num = extent.e_len;
4441 pctx->blkcount = extent.e_lblk + extent.e_len;
4443 if (extent.e_pblk == 0 ||
4444 extent.e_pblk < ctx->fs->super->s_first_data_block ||
4445 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
4446 problem = PR_1_EXTENT_BAD_START_BLK;
4447 else if (extent.e_lblk < start_block)
4448 problem = PR_1_OUT_OF_ORDER_EXTENTS;
4449 else if ((end_block && last_lblk > end_block) &&
4450 !(last_lblk > eof_block &&
4451 ((extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) ||
4452 (pctx->inode->i_flags & EXT4_VERITY_FL))))
4453 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
4454 else if (is_leaf && extent.e_len == 0)
4455 problem = PR_1_EXTENT_LENGTH_ZERO;
4457 (extent.e_pblk + extent.e_len) >
4458 ext2fs_blocks_count(ctx->fs->super))
4459 problem = PR_1_EXTENT_ENDS_BEYOND;
4460 else if (is_leaf && is_dir && !pctx->inode->i_size_high &&
4461 !ext2fs_has_feature_largedir(ctx->fs->super) &&
4462 ((extent.e_lblk + extent.e_len) >
4463 (1U << (21 - ctx->fs->super->s_log_block_size))))
4464 problem = PR_1_TOOBIG_DIR;
4466 if (is_leaf && problem == 0 && extent.e_len > 0) {
4468 printf("extent_region(ino=%u, expect=%llu, "
4469 "lblk=%llu, len=%u)\n", pb->ino,
4470 (unsigned long long) pb->next_lblock,
4471 (unsigned long long) extent.e_lblk,
4474 if (extent.e_lblk < pb->next_lblock)
4475 problem = PR_1_EXTENT_COLLISION;
4476 else if (extent.e_lblk + extent.e_len > pb->next_lblock)
4477 pb->next_lblock = extent.e_lblk + extent.e_len;
4481 * Uninitialized blocks in a directory? Clear the flag and
4482 * we'll interpret the blocks later.
4484 if (try_repairs && is_dir && problem == 0 &&
4485 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4486 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
4487 e2fsck_pass1_fix_lock(ctx);
4488 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4489 pb->inode_modified = 1;
4490 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4492 e2fsck_pass1_fix_unlock(ctx);
4497 #ifdef CONFIG_DEVELOPER_FEATURES
4498 if (try_repairs && !is_dir && problem == 0 &&
4499 (ctx->options & E2F_OPT_CLEAR_UNINIT) &&
4500 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4501 fix_problem(ctx, PR_1_CLEAR_UNINIT_EXTENT, pctx)) {
4502 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4503 pb->inode_modified = 1;
4504 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4511 if (try_repairs && problem) {
4513 if (fix_problem(ctx, problem, pctx)) {
4514 if (ctx->invalid_bitmaps) {
4516 * If fsck knows the bitmaps are bad,
4517 * skip to the next extent and
4518 * try to clear this extent again
4519 * after fixing the bitmaps, by
4522 pctx->errcode = ext2fs_extent_get(
4524 EXT2_EXTENT_NEXT_SIB,
4526 ctx->flags |= E2F_FLAG_RESTART_LATER;
4527 if (pctx->errcode ==
4528 EXT2_ET_NO_CURRENT_NODE) {
4534 e2fsck_pass1_fix_lock(ctx);
4535 e2fsck_read_bitmaps(ctx);
4536 pb->inode_modified = 1;
4538 ext2fs_extent_delete(ehandle, 0);
4539 e2fsck_pass1_fix_unlock(ctx);
4540 if (pctx->errcode) {
4541 pctx->str = "ext2fs_extent_delete";
4544 e2fsck_pass1_fix_lock(ctx);
4545 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4546 e2fsck_pass1_fix_unlock(ctx);
4547 if (pctx->errcode &&
4548 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
4549 pctx->str = "ext2fs_extent_fix_parents";
4552 pctx->errcode = ext2fs_extent_get(ehandle,
4553 EXT2_EXTENT_CURRENT,
4555 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
4566 blk64_t lblk = extent.e_lblk;
4567 int next_try_repairs = 1;
4569 blk = extent.e_pblk;
4572 * If this lower extent block collides with critical
4573 * metadata, don't try to repair the damage. Pass 1b
4574 * will reallocate the block; then we can try again.
4576 if (pb->ino != EXT2_RESIZE_INO &&
4577 extent.e_pblk < ctx->fs->super->s_blocks_count &&
4578 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
4580 next_try_repairs = 0;
4583 PR_1_CRITICAL_METADATA_COLLISION,
4585 if ((ctx->options & E2F_OPT_NO) == 0)
4586 ctx->flags |= E2F_FLAG_RESTART_LATER;
4588 pctx->errcode = ext2fs_extent_get(ehandle,
4589 EXT2_EXTENT_DOWN, &extent);
4590 if (pctx->errcode &&
4591 pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
4592 pctx->str = "EXT2_EXTENT_DOWN";
4593 problem = PR_1_EXTENT_HEADER_INVALID;
4594 if (!next_try_repairs)
4596 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
4597 goto report_problem;
4600 /* The next extent should match this index's logical start */
4601 if (extent.e_lblk != lblk) {
4602 struct ext2_extent_info e_info;
4604 pctx->errcode = ext2fs_extent_get_info(ehandle,
4606 if (pctx->errcode) {
4607 pctx->str = "ext2fs_extent_get_info";
4611 pctx->blk2 = extent.e_lblk;
4612 pctx->num = e_info.curr_level - 1;
4613 problem = PR_1_EXTENT_INDEX_START_INVALID;
4614 if (fix_problem(ctx, problem, pctx)) {
4615 e2fsck_pass1_fix_lock(ctx);
4616 pb->inode_modified = 1;
4618 ext2fs_extent_fix_parents(ehandle);
4619 e2fsck_pass1_fix_unlock(ctx);
4620 if (pctx->errcode) {
4621 pctx->str = "ext2fs_extent_fix_parents";
4626 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
4627 last_lblk, eof_block, ehandle,
4631 pctx->errcode = ext2fs_extent_get(ehandle,
4632 EXT2_EXTENT_UP, &extent);
4633 if (pctx->errcode) {
4634 pctx->str = "EXT2_EXTENT_UP";
4637 mark_block_used(ctx, blk);
4642 if ((pb->previous_block != 0) &&
4643 (pb->previous_block+1 != extent.e_pblk)) {
4644 if (ctx->options & E2F_OPT_FRAGCHECK) {
4649 else if (pb->is_reg)
4652 printf(("%6lu(%c): expecting %6lu "
4654 "phys %6lu log %lu len %lu\n"),
4655 (unsigned long) pctx->ino, type,
4656 (unsigned long) pb->previous_block+1,
4657 (unsigned long) extent.e_pblk,
4658 (unsigned long) extent.e_lblk,
4659 (unsigned long) extent.e_len);
4664 * If we notice a gap in the logical block mappings of an
4665 * extent-mapped directory, offer to close the hole by
4666 * moving the logical block down, otherwise we'll go mad in
4667 * pass 3 allocating empty directory blocks to fill the hole.
4669 if (try_repairs && is_dir &&
4670 pb->last_block + 1 < extent.e_lblk) {
4673 new_lblk = pb->last_block + 1;
4674 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
4675 new_lblk = ((new_lblk +
4676 EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
4677 ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
4679 EXT2FS_CLUSTER_MASK(ctx->fs));
4680 pctx->blk = extent.e_lblk;
4681 pctx->blk2 = new_lblk;
4682 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
4683 e2fsck_pass1_fix_lock(ctx);
4684 extent.e_lblk = new_lblk;
4685 pb->inode_modified = 1;
4686 pctx->errcode = ext2fs_extent_replace(ehandle,
4688 e2fsck_pass1_fix_unlock(ctx);
4689 if (pctx->errcode) {
4693 e2fsck_pass1_fix_lock(ctx);
4694 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4695 e2fsck_pass1_fix_unlock(ctx);
4697 goto failed_add_dir_block;
4698 pctx->errcode = ext2fs_extent_goto(ehandle,
4701 goto failed_add_dir_block;
4702 last_lblk = extent.e_lblk + extent.e_len - 1;
4708 while (++pb->last_db_block <
4709 (e2_blkcnt_t) extent.e_lblk) {
4710 pctx->errcode = ext2fs_add_dir_block2(
4714 if (pctx->errcode) {
4716 pctx->num = pb->last_db_block;
4717 goto failed_add_dir_block;
4721 for (i = 0; i < extent.e_len; i++) {
4722 pctx->errcode = ext2fs_add_dir_block2(
4727 if (pctx->errcode) {
4728 pctx->blk = extent.e_pblk + i;
4729 pctx->num = extent.e_lblk + i;
4730 failed_add_dir_block:
4731 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
4732 /* Should never get here */
4733 ctx->flags |= E2F_FLAG_ABORT;
4737 if (extent.e_len > 0)
4738 pb->last_db_block = extent.e_lblk + extent.e_len - 1;
4740 if (has_unaligned_cluster_map(ctx, pb->previous_block,
4744 for (i = 0; i < extent.e_len; i++) {
4745 pctx->blk = extent.e_lblk + i;
4746 pctx->blk2 = extent.e_pblk + i;
4747 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
4748 mark_block_used(ctx, extent.e_pblk + i);
4749 mark_block_used(ctx, extent.e_pblk + i);
4754 * Check whether first cluster got marked in previous iteration.
4756 if (ctx->fs->cluster_ratio_bits &&
4757 pb->previous_block &&
4758 (EXT2FS_B2C(ctx->fs, extent.e_pblk) ==
4759 EXT2FS_B2C(ctx->fs, pb->previous_block)))
4760 /* Set blk to the beginning of next cluster. */
4763 EXT2FS_B2C(ctx->fs, extent.e_pblk) + 1);
4765 /* Set blk to the beginning of current cluster. */
4766 blk = EXT2FS_C2B(ctx->fs,
4767 EXT2FS_B2C(ctx->fs, extent.e_pblk));
4769 if (blk < extent.e_pblk + extent.e_len) {
4770 mark_blocks_used(ctx, blk,
4771 extent.e_pblk + extent.e_len - blk);
4772 n = DIV_ROUND_UP(extent.e_pblk + extent.e_len - blk,
4773 EXT2FS_CLUSTER_RATIO(ctx->fs));
4774 pb->num_blocks += n;
4776 pb->last_block = extent.e_lblk + extent.e_len - 1;
4777 pb->previous_block = extent.e_pblk + extent.e_len - 1;
4778 start_block = pb->last_block = last_lblk;
4779 if (is_leaf && !is_dir &&
4780 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
4781 pb->last_init_lblock = last_lblk;
4783 pctx->errcode = ext2fs_extent_get(ehandle,
4784 EXT2_EXTENT_NEXT_SIB,
4788 /* Failed csum but passes checks? Ask to fix checksum. */
4790 fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
4791 e2fsck_pass1_fix_lock(ctx);
4792 pb->inode_modified = 1;
4793 pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
4794 e2fsck_pass1_fix_unlock(ctx);
4799 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
4803 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
4804 struct process_block_struct *pb)
4806 struct ext2_extent_info info;
4807 struct ext2_inode *inode = pctx->inode;
4808 ext2_extent_handle_t ehandle;
4809 ext2_filsys fs = ctx->fs;
4810 ext2_ino_t ino = pctx->ino;
4813 struct ext3_extent_header *eh;
4815 /* Check for a proper extent header... */
4816 eh = (struct ext3_extent_header *) &inode->i_block[0];
4817 retval = ext2fs_extent_header_verify(eh, sizeof(inode->i_block));
4819 if (fix_problem(ctx, PR_1_MISSING_EXTENT_HEADER, pctx))
4820 e2fsck_clear_inode(ctx, ino, inode, 0,
4821 "check_blocks_extents");
4826 /* ...since this function doesn't fail if i_block is zeroed. */
4827 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
4828 if (pctx->errcode) {
4829 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
4830 e2fsck_clear_inode(ctx, ino, inode, 0,
4831 "check_blocks_extents");
4836 retval = ext2fs_extent_get_info(ehandle, &info);
4838 int max_depth = info.max_depth;
4840 if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
4841 max_depth = MAX_EXTENT_DEPTH_COUNT-1;
4842 ctx->extent_depth_count[max_depth]++;
4845 /* Check maximum extent depth */
4846 pctx->blk = info.max_depth;
4847 pctx->blk2 = ext2fs_max_extent_depth(ehandle);
4848 if (pctx->blk2 < pctx->blk &&
4849 fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
4850 pb->eti.force_rebuild = 1;
4852 /* Can we collect extent tree level stats? */
4853 pctx->blk = MAX_EXTENT_DEPTH_COUNT;
4854 if (pctx->blk2 > pctx->blk)
4855 fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
4856 memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
4857 pb->eti.ino = pb->ino;
4859 pb->next_lblock = 0;
4861 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
4862 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
4863 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
4864 if (pctx->errcode &&
4865 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
4867 inode->i_blocks = 0;
4868 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
4869 "check_blocks_extents");
4872 ext2fs_extent_free(ehandle);
4874 /* Rebuild unless it's a dir and we're rehashing it */
4875 if (LINUX_S_ISDIR(inode->i_mode) &&
4876 e2fsck_dir_will_be_rehashed(ctx, ino))
4879 if (ctx->options & E2F_OPT_CONVERT_BMAP)
4880 e2fsck_rebuild_extents_later(ctx, ino);
4882 e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
4886 * In fact we don't need to check blocks for an inode with inline data
4887 * because this inode doesn't have any blocks. In this function all
4888 * we need to do is add this inode into dblist when it is a directory.
4890 static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
4891 struct process_block_struct *pb)
4894 size_t inline_data_size = 0;
4901 /* Process the dirents in i_block[] as the "first" block. */
4902 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
4906 /* Process the dirents in the EA as a "second" block. */
4907 flags = ctx->fs->flags;
4908 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
4909 pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
4911 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
4912 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
4913 if (pctx->errcode) {
4918 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
4921 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
4929 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
4930 ctx->flags |= E2F_FLAG_ABORT;
4934 * This subroutine is called on each inode to account for all of the
4935 * blocks used by that inode.
4937 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
4938 char *block_buf, const struct ea_quota *ea_ibody_quota)
4940 ext2_filsys fs = ctx->fs;
4941 struct process_block_struct pb;
4942 ext2_ino_t ino = pctx->ino;
4943 struct ext2_inode *inode = pctx->inode;
4944 unsigned bad_size = 0;
4945 int dirty_inode = 0;
4949 struct ea_quota ea_block_quota;
4952 pb.num_blocks = EXT2FS_B2C(ctx->fs,
4953 ea_ibody_quota ? ea_ibody_quota->blocks : 0);
4955 pb.last_init_lblock = -1;
4956 pb.last_db_block = -1;
4957 pb.num_illegal_blocks = 0;
4958 pb.suppress = 0; pb.clear = 0;
4961 pb.previous_block = 0;
4962 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
4963 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
4964 pb.max_blocks = 1U << (31 - fs->super->s_log_block_size);
4968 pb.inode_modified = 0;
4969 pb.eti.force_rebuild = 0;
4973 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
4974 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
4976 if (check_ext_attr(ctx, pctx, block_buf, &ea_block_quota)) {
4977 if (e2fsck_should_abort(ctx))
4979 pb.num_blocks += EXT2FS_B2C(ctx->fs, ea_block_quota.blocks);
4982 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
4983 check_blocks_inline_data(ctx, pctx, &pb);
4984 else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
4985 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
4986 check_blocks_extents(ctx, pctx, &pb);
4990 * If we've modified the inode, write it out before
4991 * iterate() tries to use it.
4994 e2fsck_write_inode(ctx, ino, inode,
4999 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5000 pctx->errcode = ext2fs_block_iterate3(fs, ino,
5001 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
5002 block_buf, process_block, &pb);
5004 * We do not have uninitialized extents in non extent
5007 pb.last_init_lblock = pb.last_block;
5009 * If iterate() changed a block mapping, we have to
5010 * re-read the inode. If we decide to clear the
5011 * inode after clearing some stuff, we'll re-write the
5012 * bad mappings into the inode!
5014 if (pb.inode_modified)
5015 e2fsck_read_inode(ctx, ino, inode,
5017 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5018 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5020 if (ctx->options & E2F_OPT_CONVERT_BMAP) {
5022 printf("bmap rebuild ino=%d\n", ino);
5024 if (!LINUX_S_ISDIR(inode->i_mode) ||
5025 !e2fsck_dir_will_be_rehashed(ctx, ino))
5026 e2fsck_rebuild_extents_later(ctx, ino);
5030 end_problem_latch(ctx, PR_LATCH_BLOCK);
5031 end_problem_latch(ctx, PR_LATCH_TOOBIG);
5032 if (e2fsck_should_abort(ctx))
5035 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
5037 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
5038 if (LINUX_S_ISDIR(inode->i_mode))
5039 ctx->fs_fragmented_dir++;
5041 ctx->fs_fragmented++;
5045 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
5050 if (inode->i_flags & EXT2_INDEX_FL) {
5051 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
5052 inode->i_flags &= ~EXT2_INDEX_FL;
5055 e2fsck_add_dx_dir(ctx, ino, inode, pb.last_block+1);
5059 if (!pb.num_blocks && pb.is_dir &&
5060 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
5061 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
5062 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
5063 ctx->fs_directory_count--;
5068 if (ino != quota_type2inum(PRJQUOTA, fs->super) &&
5069 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) &&
5070 !(inode->i_flags & EXT4_EA_INODE_FL)) {
5071 quota_data_add(ctx->qctx, (struct ext2_inode_large *) inode,
5073 pb.num_blocks * EXT2_CLUSTER_SIZE(fs->super));
5074 quota_data_inodes(ctx->qctx, (struct ext2_inode_large *) inode,
5075 ino, (ea_ibody_quota ?
5076 ea_ibody_quota->inodes : 0) +
5077 ea_block_quota.inodes + 1);
5080 if (!ext2fs_has_feature_huge_file(fs->super) ||
5081 !(inode->i_flags & EXT4_HUGE_FILE_FL))
5082 pb.num_blocks *= (fs->blocksize / 512);
5083 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
5085 printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
5086 ino, inode->i_size, (unsigned long long) pb.last_block,
5087 (unsigned long long) ext2fs_inode_i_blocks(fs, inode),
5088 (unsigned long long) pb.num_blocks);
5090 size = EXT2_I_SIZE(inode);
5092 unsigned nblock = size >> EXT2_BLOCK_SIZE_BITS(fs->super);
5093 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
5098 flags = ctx->fs->flags;
5099 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5100 err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
5102 ctx->fs->flags = (flags &
5103 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5105 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5106 if (err || sz != size) {
5110 } else if (size & (fs->blocksize - 1))
5112 else if (nblock > (pb.last_block + 1))
5114 else if (nblock < (pb.last_block + 1)) {
5115 if (((pb.last_block + 1) - nblock) >
5116 fs->super->s_prealloc_dir_blocks)
5120 if ((pb.last_init_lblock >= 0) &&
5121 /* Do not allow initialized allocated blocks past i_size*/
5122 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
5123 !(inode->i_flags & EXT4_VERITY_FL))
5125 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5126 size > ext2_max_sizes[fs->super->s_log_block_size])
5127 /* too big for a direct/indirect-mapped file */
5129 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5131 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
5132 /* too big for an extent-based file - 32bit ee_block */
5135 /* i_size for symlinks is checked elsewhere */
5136 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
5137 /* Did inline_data set pctx->num earlier? */
5139 pctx->num = (pb.last_block + 1) * fs->blocksize;
5140 pctx->group = bad_size;
5141 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
5142 ext2fs_inode_size_set(fs, inode, pctx->num);
5143 if (EXT2_I_SIZE(inode) == 0 &&
5144 (inode->i_flags & EXT4_INLINE_DATA_FL)) {
5145 memset(inode->i_block, 0,
5146 sizeof(inode->i_block));
5147 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
5153 if (LINUX_S_ISREG(inode->i_mode) &&
5154 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
5156 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
5157 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
5158 (ext2fs_has_feature_huge_file(fs->super) &&
5159 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
5160 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
5161 pctx->num = pb.num_blocks;
5162 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
5163 inode->i_blocks = pb.num_blocks;
5164 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
5171 * The kernel gets mad if we ask it to allocate bigalloc clusters to
5172 * a block mapped file, so rebuild it as an extent file. We can skip
5173 * symlinks because they're never rewritten.
5175 if (ext2fs_has_feature_bigalloc(fs->super) &&
5176 (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
5177 ext2fs_inode_data_blocks2(fs, inode) > 0 &&
5178 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
5179 !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
5180 fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
5181 pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
5186 if (ctx->dirs_to_hash && pb.is_dir &&
5187 !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
5188 !(inode->i_flags & EXT2_INDEX_FL) &&
5189 ((inode->i_size / fs->blocksize) >= 3))
5190 e2fsck_rehash_dir_later(ctx, ino);
5194 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
5199 * Helper function called by process block when an illegal block is
5200 * found. It returns a description about why the block is illegal
5202 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
5206 static char problem[80];
5208 super = fs->super->s_first_data_block;
5209 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
5210 if (block < super) {
5211 sprintf(problem, "< FIRSTBLOCK (%u)", super);
5213 } else if (block >= ext2fs_blocks_count(fs->super)) {
5214 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
5217 for (i = 0; i < fs->group_desc_count; i++) {
5218 if (block == super) {
5219 sprintf(problem, "is the superblock in group %d", i);
5222 if (block > super &&
5223 block <= (super + fs->desc_blocks)) {
5224 sprintf(problem, "is in the group descriptors "
5228 if (block == ext2fs_block_bitmap_loc(fs, i)) {
5229 sprintf(problem, "is the block bitmap of group %d", i);
5232 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
5233 sprintf(problem, "is the inode bitmap of group %d", i);
5236 if (block >= ext2fs_inode_table_loc(fs, i) &&
5237 (block < ext2fs_inode_table_loc(fs, i)
5238 + fs->inode_blocks_per_group)) {
5239 sprintf(problem, "is in the inode table of group %d",
5243 super += fs->super->s_blocks_per_group;
5250 * This is a helper function for check_blocks().
5252 static int process_block(ext2_filsys fs,
5254 e2_blkcnt_t blockcnt,
5255 blk64_t ref_block EXT2FS_ATTR((unused)),
5256 int ref_offset EXT2FS_ATTR((unused)),
5259 struct process_block_struct *p;
5260 struct problem_context *pctx;
5261 blk64_t blk = *block_nr;
5263 problem_t problem = 0;
5266 p = (struct process_block_struct *) priv_data;
5271 * For a directory, add logical block zero for processing even if it's
5272 * not mapped or we'll be perennially stuck with broken "." and ".."
5275 if (p->is_dir && blockcnt == 0 && blk == 0) {
5276 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
5277 if (pctx->errcode) {
5279 pctx->num = blockcnt;
5280 goto failed_add_dir_block;
5289 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
5294 * Simplistic fragmentation check. We merely require that the
5295 * file be contiguous. (Which can never be true for really
5296 * big files that are greater than a block group.)
5298 if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
5299 if (p->previous_block+1 != blk) {
5300 if (ctx->options & E2F_OPT_FRAGCHECK) {
5308 printf(_("%6lu(%c): expecting %6lu "
5309 "got phys %6lu (blkcnt %lld)\n"),
5310 (unsigned long) pctx->ino, type,
5311 (unsigned long) p->previous_block+1,
5312 (unsigned long) blk,
5313 (long long) blockcnt);
5319 if (p->is_dir && !ext2fs_has_feature_largedir(fs->super) &&
5320 !pctx->inode->i_size_high &&
5321 blockcnt > (1 << (21 - fs->super->s_log_block_size)))
5322 problem = PR_1_TOOBIG_DIR;
5323 if (p->is_dir && p->num_blocks + 1 >= p->max_blocks)
5324 problem = PR_1_TOOBIG_DIR;
5325 if (p->is_reg && p->num_blocks + 1 >= p->max_blocks)
5326 problem = PR_1_TOOBIG_REG;
5327 if (!p->is_dir && !p->is_reg && blockcnt > 0)
5328 problem = PR_1_TOOBIG_SYMLINK;
5330 if (blk < fs->super->s_first_data_block ||
5331 blk >= ext2fs_blocks_count(fs->super))
5332 problem = PR_1_ILLEGAL_BLOCK_NUM;
5335 * If this IND/DIND/TIND block is squatting atop some critical metadata
5336 * (group descriptors, superblock, bitmap, inode table), any write to
5337 * "fix" mapping problems will destroy the metadata. We'll let pass 1b
5338 * fix that and restart fsck.
5341 p->ino != EXT2_RESIZE_INO &&
5342 blk < ctx->fs->super->s_blocks_count &&
5343 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
5345 fix_problem(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx);
5346 if ((ctx->options & E2F_OPT_NO) == 0)
5347 ctx->flags |= E2F_FLAG_RESTART_LATER;
5351 p->num_illegal_blocks++;
5353 * A bit of subterfuge here -- we're trying to fix a block
5354 * mapping, but the IND/DIND/TIND block could have collided
5355 * with some critical metadata. So, fix the in-core mapping so
5356 * iterate won't go insane, but return 0 instead of
5357 * BLOCK_CHANGED so that it won't write the remapping out to
5358 * our multiply linked block.
5360 * Even if we previously determined that an *IND block
5361 * conflicts with critical metadata, we must still try to
5362 * iterate the *IND block as if it is an *IND block to find and
5363 * mark the blocks it points to. Better to be overly cautious
5364 * with the used_blocks map so that we don't move the *IND
5365 * block to a block that's really in use!
5367 if (p->ino != EXT2_RESIZE_INO &&
5369 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
5374 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
5375 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
5379 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
5381 set_latch_flags(PR_LATCH_BLOCK,
5386 pctx->blkcount = blockcnt;
5387 if (fix_problem(ctx, problem, pctx)) {
5388 blk = *block_nr = 0;
5389 ret_code = BLOCK_CHANGED;
5390 p->inode_modified = 1;
5392 * If the directory block is too big and is beyond the
5393 * end of the FS, don't bother trying to add it for
5394 * processing -- the kernel would never have created a
5395 * directory this large, and we risk an ENOMEM abort.
5396 * In any case, the toobig handler for extent-based
5397 * directories also doesn't feed toobig blocks to
5400 if (problem == PR_1_TOOBIG_DIR)
5407 if (p->ino == EXT2_RESIZE_INO) {
5409 * The resize inode has already be sanity checked
5410 * during pass #0 (the superblock checks). All we
5411 * have to do is mark the double indirect block as
5412 * being in use; all of the other blocks are handled
5413 * by mark_table_blocks()).
5415 if (blockcnt == BLOCK_COUNT_DIND)
5416 mark_block_used(ctx, blk);
5418 } else if (!(ctx->fs->cluster_ratio_bits &&
5419 p->previous_block &&
5420 (EXT2FS_B2C(ctx->fs, blk) ==
5421 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
5422 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
5423 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
5424 mark_block_used(ctx, blk);
5426 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
5427 p->last_block, blk, blockcnt)) {
5428 pctx->blk = blockcnt;
5430 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
5431 mark_block_used(ctx, blk);
5432 mark_block_used(ctx, blk);
5435 p->last_block = blockcnt;
5436 p->previous_block = blk;
5438 if (p->is_dir && (blockcnt >= 0)) {
5439 while (++p->last_db_block < blockcnt) {
5440 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
5443 if (pctx->errcode) {
5445 pctx->num = p->last_db_block;
5446 goto failed_add_dir_block;
5449 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
5451 if (pctx->errcode) {
5453 pctx->num = blockcnt;
5454 failed_add_dir_block:
5455 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
5456 /* Should never get here */
5457 ctx->flags |= E2F_FLAG_ABORT;
5464 static int process_bad_block(ext2_filsys fs,
5466 e2_blkcnt_t blockcnt,
5467 blk64_t ref_block EXT2FS_ATTR((unused)),
5468 int ref_offset EXT2FS_ATTR((unused)),
5471 struct process_block_struct *p;
5472 blk64_t blk = *block_nr;
5473 blk64_t first_block;
5475 struct problem_context *pctx;
5481 p = (struct process_block_struct *) priv_data;
5485 pctx->ino = EXT2_BAD_INO;
5487 pctx->blkcount = blockcnt;
5489 if ((blk < fs->super->s_first_data_block) ||
5490 (blk >= ext2fs_blocks_count(fs->super))) {
5491 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
5493 return BLOCK_CHANGED;
5499 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
5501 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
5503 return BLOCK_CHANGED;
5505 } else if (is_blocks_used(ctx, blk, 1)) {
5507 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
5510 return BLOCK_CHANGED;
5512 if (e2fsck_should_abort(ctx))
5515 mark_block_used(ctx, blk);
5520 printf ("DEBUG: Marking %u as bad.\n", blk);
5522 ctx->fs_badblocks_count++;
5524 * If the block is not used, then mark it as used and return.
5525 * If it is already marked as found, this must mean that
5526 * there's an overlap between the filesystem table blocks
5527 * (bitmaps and inode table) and the bad block list.
5529 if (!is_blocks_used(ctx, blk, 1)) {
5530 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
5534 * Try to find the where the filesystem block was used...
5536 first_block = fs->super->s_first_data_block;
5538 for (i = 0; i < fs->group_desc_count; i++ ) {
5541 if (!ext2fs_bg_has_super(fs, i))
5543 if (blk == first_block) {
5545 if (fix_problem(ctx,
5546 PR_1_BAD_PRIMARY_SUPERBLOCK,
5549 return BLOCK_CHANGED;
5553 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
5556 if ((blk > first_block) &&
5557 (blk <= first_block + fs->desc_blocks)) {
5559 pctx->blk = *block_nr;
5560 if (fix_problem(ctx,
5561 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
5563 return BLOCK_CHANGED;
5567 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
5571 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
5572 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
5573 ctx->invalid_block_bitmap_flag[i]++;
5574 ctx->invalid_bitmaps++;
5578 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
5579 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
5580 ctx->invalid_inode_bitmap_flag[i]++;
5581 ctx->invalid_bitmaps++;
5585 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
5586 (blk < (ext2fs_inode_table_loc(fs, i) +
5587 fs->inode_blocks_per_group))) {
5589 * If there are bad blocks in the inode table,
5590 * the inode scan code will try to do
5591 * something reasonable automatically.
5595 first_block += fs->super->s_blocks_per_group;
5598 * If we've gotten to this point, then the only
5599 * possibility is that the bad block inode meta data
5600 * is using a bad block.
5602 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
5603 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
5604 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
5606 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
5608 return BLOCK_CHANGED;
5610 if (e2fsck_should_abort(ctx))
5617 /* Warn user that the block wasn't claimed */
5618 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
5623 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
5624 const char *name, int num, blk64_t *new_block)
5626 ext2_filsys fs = ctx->fs;
5628 blk64_t old_block = *new_block;
5631 unsigned flexbg_size;
5634 struct problem_context pctx;
5636 clear_problem_context(&pctx);
5639 pctx.blk = old_block;
5643 * For flex_bg filesystems, first try to allocate the metadata
5644 * within the flex_bg, and if that fails then try finding the
5645 * space anywhere in the filesystem.
5647 is_flexbg = ext2fs_has_feature_flex_bg(fs->super);
5649 flexbg_size = 1U << fs->super->s_log_groups_per_flex;
5650 flexbg = group / flexbg_size;
5651 first_block = ext2fs_group_first_block2(fs,
5652 flexbg_size * flexbg);
5653 last_grp = group | (flexbg_size - 1);
5654 if (last_grp >= fs->group_desc_count)
5655 last_grp = fs->group_desc_count - 1;
5656 last_block = ext2fs_group_last_block2(fs, last_grp);
5658 last_block = ext2fs_group_last_block2(fs, group);
5659 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
5660 num, ctx->block_found_map,
5662 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
5663 pctx.errcode = ext2fs_get_free_blocks2(fs,
5664 fs->super->s_first_data_block,
5665 ext2fs_blocks_count(fs->super),
5666 num, ctx->block_found_map, new_block);
5669 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
5670 ext2fs_unmark_valid(fs);
5671 ctx->flags |= E2F_FLAG_ABORT;
5674 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
5676 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
5677 ext2fs_unmark_valid(fs);
5678 ctx->flags |= E2F_FLAG_ABORT;
5681 ext2fs_mark_super_dirty(fs);
5682 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
5683 pctx.blk2 = *new_block;
5684 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
5685 PR_1_RELOC_TO), &pctx);
5687 for (i = 0; i < num; i++) {
5689 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
5691 pctx.errcode = io_channel_read_blk64(fs->io,
5692 old_block + i, 1, buf);
5694 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
5695 pctx.blk = (*new_block) + i;
5696 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
5699 pctx.blk = (*new_block) + i;
5700 pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
5705 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
5707 ext2fs_free_mem(&buf);
5711 * This routine gets called at the end of pass 1 if bad blocks are
5712 * detected in the superblock, group descriptors, inode_bitmaps, or
5713 * block bitmaps. At this point, all of the blocks have been mapped
5714 * out, so we can try to allocate new block(s) to replace the bad
5717 static void handle_fs_bad_blocks(e2fsck_t ctx)
5719 ext2_filsys fs = ctx->fs;
5721 blk64_t first_block;
5724 for (i = 0; i < fs->group_desc_count; i++) {
5725 first_block = ext2fs_group_first_block2(fs, i);
5727 if (ctx->invalid_block_bitmap_flag[i]) {
5728 new_blk = ext2fs_block_bitmap_loc(fs, i);
5729 new_table_block(ctx, first_block, i, _("block bitmap"),
5731 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
5733 if (ctx->invalid_inode_bitmap_flag[i]) {
5734 new_blk = ext2fs_inode_bitmap_loc(fs, i);
5735 new_table_block(ctx, first_block, i, _("inode bitmap"),
5737 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
5739 if (ctx->invalid_inode_table_flag[i]) {
5740 new_blk = ext2fs_inode_table_loc(fs, i);
5741 new_table_block(ctx, first_block, i, _("inode table"),
5742 fs->inode_blocks_per_group,
5744 ext2fs_inode_table_loc_set(fs, i, new_blk);
5745 ctx->flags |= E2F_FLAG_RESTART;
5748 ctx->invalid_bitmaps = 0;
5752 * This routine marks all blocks which are used by the superblock,
5753 * group descriptors, inode bitmaps, and block bitmaps.
5755 static void mark_table_blocks(e2fsck_t ctx)
5757 ext2_filsys fs = ctx->fs;
5761 struct problem_context pctx;
5763 clear_problem_context(&pctx);
5765 for (i = 0; i < fs->group_desc_count; i++) {
5768 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
5769 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
5772 * Mark the blocks used for the inode table
5774 if (ext2fs_inode_table_loc(fs, i)) {
5775 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
5776 j < fs->inode_blocks_per_group;
5778 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5781 if (!ctx->invalid_inode_table_flag[i] &&
5783 PR_1_ITABLE_CONFLICT, &pctx)) {
5784 ctx->invalid_inode_table_flag[i]++;
5785 ctx->invalid_bitmaps++;
5788 ext2fs_mark_block_bitmap2(
5789 ctx->block_found_map, b);
5790 ext2fs_mark_block_bitmap2(
5791 ctx->block_metadata_map, b);
5797 * Mark block used for the block bitmap
5799 if (ext2fs_block_bitmap_loc(fs, i)) {
5800 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5801 ext2fs_block_bitmap_loc(fs, i))) {
5802 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
5803 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
5804 ctx->invalid_block_bitmap_flag[i]++;
5805 ctx->invalid_bitmaps++;
5808 ext2fs_mark_block_bitmap2(ctx->block_found_map,
5809 ext2fs_block_bitmap_loc(fs, i));
5810 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5811 ext2fs_block_bitmap_loc(fs, i));
5815 * Mark block used for the inode bitmap
5817 if (ext2fs_inode_bitmap_loc(fs, i)) {
5818 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5819 ext2fs_inode_bitmap_loc(fs, i))) {
5820 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
5821 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
5822 ctx->invalid_inode_bitmap_flag[i]++;
5823 ctx->invalid_bitmaps++;
5826 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5827 ext2fs_inode_bitmap_loc(fs, i));
5828 ext2fs_mark_block_bitmap2(ctx->block_found_map,
5829 ext2fs_inode_bitmap_loc(fs, i));
5836 * These subroutines short circuits ext2fs_get_blocks and
5837 * ext2fs_check_directory; we use them since we already have the inode
5838 * structure, so there's no point in letting the ext2fs library read
5841 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
5844 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5847 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5848 return EXT2_ET_CALLBACK_NOTHANDLED;
5850 for (i=0; i < EXT2_N_BLOCKS; i++)
5851 blocks[i] = ctx->stashed_inode->i_block[i];
5855 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
5856 struct ext2_inode *inode)
5858 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5860 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5861 return EXT2_ET_CALLBACK_NOTHANDLED;
5862 *inode = *ctx->stashed_inode;
5866 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
5867 struct ext2_inode *inode)
5869 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5871 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
5872 (inode != ctx->stashed_inode))
5873 *ctx->stashed_inode = *inode;
5874 return EXT2_ET_CALLBACK_NOTHANDLED;
5877 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
5879 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5881 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5882 return EXT2_ET_CALLBACK_NOTHANDLED;
5884 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
5885 return EXT2_ET_NO_DIRECTORY;
5889 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
5892 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5896 if (ctx->block_found_map) {
5897 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
5901 if (fs->block_map) {
5902 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
5903 ext2fs_mark_bb_dirty(fs);
5906 if (!fs->block_map) {
5907 retval = ext2fs_read_block_bitmap(fs);
5912 retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
5921 static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
5922 blk64_t len, blk64_t *pblk, blk64_t *plen)
5924 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5927 if (ctx->block_found_map)
5928 return ext2fs_new_range(fs, flags, goal, len,
5929 ctx->block_found_map, pblk, plen);
5931 if (!fs->block_map) {
5932 retval = ext2fs_read_block_bitmap(fs);
5937 return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
5941 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
5943 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5945 /* Never free a critical metadata block */
5946 if (ctx->block_found_map &&
5947 ctx->block_metadata_map &&
5949 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
5952 if (ctx->block_found_map) {
5954 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
5956 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
5960 static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
5961 blk_t num, int inuse)
5963 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5965 /* Never free a critical metadata block */
5966 if (ctx->block_found_map &&
5967 ctx->block_metadata_map &&
5969 ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
5972 if (ctx->block_found_map) {
5974 ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
5977 ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
5982 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
5984 ext2_filsys fs = ctx->fs;
5986 if (use_shortcuts) {
5987 fs->get_blocks = pass1_get_blocks;
5988 fs->check_directory = pass1_check_directory;
5989 fs->read_inode = pass1_read_inode;
5990 fs->write_inode = pass1_write_inode;
5991 ctx->stashed_ino = 0;
5994 fs->check_directory = 0;
5996 fs->write_inode = 0;
6000 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
6002 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
6003 ext2fs_set_block_alloc_stats_callback(ctx->fs,
6004 e2fsck_block_alloc_stats, 0);
6005 ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
6006 ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
6007 e2fsck_block_alloc_stats_range, NULL);