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 * - An icount mechanism is used to keep track of
24 * inodes with bad fields and its badness (ctx->inode_badness)
25 * - A bitmap of which inodes are in bad blocks. (inode_bb_map)
26 * - A bitmap of which inodes are imagic inodes. (inode_imagic_map)
27 * - A bitmap of which inodes are casefolded. (inode_casefold_map)
28 * - A bitmap of which inodes need to be expanded (expand_eisize_map)
29 * - A bitmap of which blocks are in use. (block_found_map)
30 * - A bitmap of which blocks are in use by two inodes (block_dup_map)
31 * - The data blocks of the directory inodes. (dir_map)
32 * - Ref counts for ea_inodes. (ea_inode_refs)
33 * - The encryption policy ID of each encrypted inode. (encrypted_files)
35 * Pass 1 is designed to stash away enough information so that the
36 * other passes should not need to read in the inode information
37 * during the normal course of a filesystem check. (Although if an
38 * inconsistency is detected, other passes may need to read in an
41 * Note that pass 1B will be invoked if there are any duplicate blocks
45 #define _GNU_SOURCE 1 /* get strnlen() */
58 #include <ext2fs/ext2_ext_attr.h>
59 /* todo remove this finally */
60 #include <ext2fs/ext2fsP.h>
65 #ifdef NO_INLINE_FUNCS
68 #define _INLINE_ inline
78 static int process_block(ext2_filsys fs, blk64_t *blocknr,
79 e2_blkcnt_t blockcnt, blk64_t ref_blk,
80 int ref_offset, void *priv_data);
81 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
82 e2_blkcnt_t blockcnt, blk64_t ref_blk,
83 int ref_offset, void *priv_data);
84 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
86 const struct ea_quota *ea_ibody_quota);
87 static void mark_table_blocks(e2fsck_t ctx);
88 static void alloc_bb_map(e2fsck_t ctx);
89 static void alloc_imagic_map(e2fsck_t ctx);
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) -
441 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize;
442 header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
443 inode->i_extra_isize;
444 end = header + storage_size;
445 entry = &IHDR(inode)->h_first_entry[0];
446 start = (char *)entry;
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;
581 int min, max, dirty = 0;
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;
617 /* check if there is no place for an EA header */
618 if (inode->i_extra_isize >= max - sizeof(__u32))
621 eamagic = &IHDR(inode)->h_magic;
622 if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
623 (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
624 (inode->i_extra_isize < ctx->want_extra_isize)) {
625 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
626 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
627 EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
628 inode->i_extra_isize = ctx->want_extra_isize;
630 if (inode->i_extra_isize < ctx->min_extra_isize)
631 ctx->min_extra_isize = inode->i_extra_isize;
634 if (*eamagic == EXT2_EXT_ATTR_MAGIC)
635 check_ea_in_inode(ctx, pctx, ea_ibody_quota);
637 /* Since crtime cannot be set directly from userspace, consider
638 * very old/future values worse than a bad atime/mtime. */
639 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, ctx->time_fudge))
640 e2fsck_mark_inode_badder(ctx, pctx, PR_1_CRTIME_BAD);
641 else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, ctx->time_fudge))
642 e2fsck_mark_inode_badder(ctx, pctx, PR_1_CRTIME_BAD);
644 * If the inode's extended atime (ctime, crtime, mtime) is stored in
645 * the old, invalid format, repair it.
647 if (((sizeof(time_t) <= 4) ||
648 (((sizeof(time_t) > 4) &&
649 ctx->now < EXT4_EXTRA_NEGATIVE_DATE_CUTOFF))) &&
650 (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime) ||
651 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime) ||
652 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime) ||
653 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))) {
655 if (!fix_problem_bad(ctx, PR_1_EA_TIME_OUT_OF_RANGE, pctx, 2))
658 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime))
659 inode->i_atime_extra &= ~EXT4_EPOCH_MASK;
660 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime))
661 inode->i_ctime_extra &= ~EXT4_EPOCH_MASK;
662 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime))
663 inode->i_crtime_extra &= ~EXT4_EPOCH_MASK;
664 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))
665 inode->i_mtime_extra &= ~EXT4_EPOCH_MASK;
671 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
672 EXT2_INODE_SIZE(sb), "pass1");
675 static _INLINE_ int is_blocks_used(e2fsck_t ctx, blk64_t block,
680 /* used to avoid duplicate output from below */
681 retval = ext2fs_test_block_bitmap_range2_valid(ctx->block_found_map,
686 retval = ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num);
688 e2fsck_pass1_block_map_r_lock(ctx);
690 retval = ext2fs_test_block_bitmap_range2(
691 ctx->global_ctx->block_found_map, block, num);
692 e2fsck_pass1_block_map_r_unlock(ctx);
701 * Check to see if the inode might really be a directory, despite i_mode
703 * This is a lot of complexity for something for which I'm not really
704 * convinced happens frequently in the wild. If for any reason this
705 * causes any problems, take this code out.
706 * [tytso:20070331.0827EDT]
708 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
711 struct ext2_inode *inode = pctx->inode;
712 struct ext2_dir_entry *dirent;
715 unsigned int i, rec_len, not_device = 0;
720 * If the mode looks OK, we believe it. If the first block in
721 * the i_block array is 0, this cannot be a directory. If the
722 * inode is extent-mapped, it is still the case that the latter
723 * cannot be 0 - the magic number in the extent header would make
726 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
727 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
731 * Check the block numbers in the i_block array for validity:
732 * zero blocks are skipped (but the first one cannot be zero -
733 * see above), other blocks are checked against the first and
734 * max data blocks (from the the superblock) and against the
735 * block bitmap. Any invalid block found means this cannot be
738 * If there are non-zero blocks past the fourth entry, then
739 * this cannot be a device file: we remember that for the next
742 * For extent mapped files, we don't do any sanity checking:
743 * just try to get the phys block of logical block 0 and run
746 * For inline data files, we just try to get the size of inline
747 * data. If it's true, we will treat it as a directory.
750 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
751 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
752 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
755 unsigned int rec_len2;
756 struct ext2_dir_entry de;
758 if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
761 * If the size isn't a multiple of 4, it's probably not a
767 * If the first 10 bytes don't look like a directory entry,
768 * it's probably not a directory.
770 memcpy(&dotdot, inode->i_block, sizeof(dotdot));
771 memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
772 EXT2_DIR_REC_LEN(0));
773 dotdot = ext2fs_le32_to_cpu(dotdot);
774 de.inode = ext2fs_le32_to_cpu(de.inode);
775 de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
776 ext2fs_get_rec_len(ctx->fs, &de, &rec_len2);
777 if (dotdot >= ctx->fs->super->s_inodes_count ||
778 (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
779 dotdot != EXT2_ROOT_INO) ||
780 de.inode >= ctx->fs->super->s_inodes_count ||
781 (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
783 rec_len2 > EXT4_MIN_INLINE_DATA_SIZE -
784 EXT4_INLINE_DATA_DOTDOT_SIZE)
786 /* device files never have a "system.data" entry */
788 } else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
790 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
793 /* device files are never extent mapped */
796 for (i=0; i < EXT2_N_BLOCKS; i++) {
797 blk = inode->i_block[i];
803 if (blk < ctx->fs->super->s_first_data_block ||
804 blk >= ext2fs_blocks_count(ctx->fs->super) ||
805 is_blocks_used(ctx, blk, 1))
806 return; /* Invalid block, can't be dir */
808 blk = inode->i_block[0];
812 * If the mode says this is a device file and the i_links_count field
813 * is sane and we have not ruled it out as a device file previously,
814 * we declare it a device file, not a directory.
816 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
817 (inode->i_links_count == 1) && !not_device)
820 /* read the first block */
821 ehandler_operation(_("reading directory block"));
822 retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
823 ehandler_operation(0);
827 dirent = (struct ext2_dir_entry *) buf;
828 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
831 if ((ext2fs_dirent_name_len(dirent) != 1) ||
832 (dirent->name[0] != '.') ||
833 (dirent->inode != pctx->ino) ||
836 (rec_len >= ctx->fs->blocksize - 12))
839 dirent = (struct ext2_dir_entry *) (buf + rec_len);
840 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
843 if ((ext2fs_dirent_name_len(dirent) != 2) ||
844 (dirent->name[0] != '.') ||
845 (dirent->name[1] != '.') ||
851 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
852 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
853 e2fsck_write_inode_full(ctx, pctx->ino, inode,
854 EXT2_INODE_SIZE(ctx->fs->super),
855 "check_is_really_dir");
859 extern errcode_t e2fsck_setup_icount(e2fsck_t ctx, const char *icount_name,
860 int flags, ext2_icount_t hint,
863 unsigned int threshold;
864 unsigned int save_type;
872 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
874 profile_get_uint(ctx->profile, "scratch_files",
875 "numdirs_threshold", 0, 0, &threshold);
876 profile_get_boolean(ctx->profile, "scratch_files",
877 "icount", 0, 1, &enable);
879 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
881 num_dirs = 1024; /* Guess */
883 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
884 (!threshold || num_dirs > threshold)) {
885 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir,
890 e2fsck_set_bitmap_type(ctx->fs, EXT2FS_BMAP64_RBTREE, icount_name,
892 if (ctx->options & E2F_OPT_ICOUNT_FULLMAP)
893 flags |= EXT2_ICOUNT_OPT_FULLMAP;
894 retval = ext2fs_create_icount2(ctx->fs, flags, 0, hint, ret);
895 ctx->fs->default_bitmap_type = save_type;
899 static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
901 struct problem_context *pctx)
904 struct ext2_inode_large inode;
907 * Reread inode. If we don't see checksum error, then this inode
908 * has been fixed elsewhere.
910 ctx->stashed_ino = 0;
911 retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
913 if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
919 * Checksum still doesn't match. That implies that the inode passes
920 * all the sanity checks, so maybe the checksum is simply corrupt.
921 * See if the user will go for fixing that.
923 if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
927 e2fsck_pass1_fix_lock(ctx);
928 retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
930 e2fsck_pass1_fix_unlock(ctx);
934 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
935 struct problem_context *pctx, int needed_size)
937 struct ext2_ext_attr_header *header;
938 struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
939 char *start, name[4096], block_buf[4096];
940 int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
941 int in_inode = 1, error;
942 unsigned int freed_bytes = inode->i_extra_isize;
944 entry_ino = &IHDR(inode)->h_first_entry[0];
945 start = (char *)entry_ino;
947 if (inode->i_file_acl) {
948 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
950 /* We have already checked this block, shouldn't happen */
952 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
955 header = BHDR(block_buf);
956 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
957 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
961 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
964 len = sizeof(entry->e_name);
965 entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
967 while (freed_bytes < needed_size) {
968 if (entry_size && name[0] != '\0') {
970 if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
971 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
972 EXT2_EXT_ATTR_SIZE(entry->e_value_size);
973 error = ext2fs_attr_set(ctx->fs, pctx->ino,
974 (struct ext2_inode *)inode,
975 index, name, 0, 0, 0);
977 freed_bytes += ea_size;
980 len = sizeof(entry->e_name);
981 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
982 entry = EXT2_EXT_ATTR_NEXT(entry);
983 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
986 len = sizeof(entry->e_name);
987 entry_size = ext2fs_attr_get_next_attr(entry,
988 index, name, len, 1);
993 if (!entry && index < EXT2_ATTR_INDEX_MAX)
994 entry = (struct ext2_ext_attr_entry *)start;
1004 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
1005 struct problem_context *pctx)
1007 int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
1011 retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
1012 ctx->want_extra_isize, &ret,
1014 if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
1015 goto mark_expand_eisize_map;
1017 e2fsck_write_inode_full(ctx, pctx->ino,
1018 (struct ext2_inode *)inode,
1019 EXT2_INODE_SIZE(ctx->fs->super),
1024 if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
1025 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
1026 fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
1027 ctx->flags |= E2F_FLAG_ABORT;
1032 pctx->num = ctx->fs->super->s_min_extra_isize;
1033 fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
1037 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
1039 if (retval >= ctx->want_extra_isize)
1042 needed_size -= retval;
1045 * We loop here until either the user deletes EA(s) or
1046 * EXTRA_ISIZE feature is disabled.
1048 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
1049 ctx->fs->super->s_feature_ro_compat &=
1050 ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
1051 ext2fs_mark_super_dirty(ctx->fs);
1055 ctx->fs_unexpanded_inodes++;
1057 /* No EA was deleted, inode cannot be expanded */
1061 mark_expand_eisize_map:
1062 if (!ctx->expand_eisize_map) {
1063 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1064 _("expand extrz isize map"),
1065 &ctx->expand_eisize_map);
1066 if (pctx->errcode) {
1067 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
1073 /* Add this inode to the expand_eisize_map */
1074 ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
1078 static void reserve_block_for_root_repair(e2fsck_t ctx)
1082 ext2_filsys fs = ctx->fs;
1084 ctx->root_repair_block = 0;
1085 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
1088 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1091 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1092 ctx->root_repair_block = blk;
1095 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
1099 ext2_filsys fs = ctx->fs;
1100 static const char name[] = "lost+found";
1103 ctx->lnf_repair_block = 0;
1104 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
1107 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1110 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1111 ctx->lnf_repair_block = blk;
1115 static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
1119 struct ext2_xattr_handle *handle;
1122 retval = ext2fs_xattrs_open(fs, ino, &handle);
1126 retval = ext2fs_xattrs_read(handle);
1130 retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
1133 ext2fs_free_mem(&p);
1135 (void) ext2fs_xattrs_close(&handle);
1139 int e2fsck_fix_bad_inode(e2fsck_t ctx, struct problem_context *pctx)
1144 if (!ctx->inode_badness)
1147 if (ext2fs_icount_fetch(ctx->inode_badness, pctx->ino, &badness))
1150 if (badness > ctx->inode_badness_threshold) {
1151 __u64 pctx_num_sav = pctx->num;
1153 pctx->num = badness;
1154 rc = fix_problem_notbad(ctx, PR_1B_INODE_TOOBAD, pctx);
1155 pctx->num = pctx_num_sav;
1161 static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
1162 struct problem_context *pctx,
1169 * If the inode failed the checksum and the user didn't
1170 * clear the inode, test the checksum again -- if it still
1171 * fails, ask the user if the checksum should be corrected.
1173 pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
1175 ctx->flags |= E2F_FLAG_ABORT;
1177 #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
1179 finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
1180 if (e2fsck_should_abort(ctx)) { \
1181 e2fsck_pass1_check_unlock(ctx); \
1186 static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
1191 for (i = 0; i < EXT2_N_BLOCKS; i++) {
1192 x = inode->i_block[i];
1193 #ifdef WORDS_BIGENDIAN
1194 x = ext2fs_swab32(x);
1196 if (x >= ext2fs_blocks_count(fs->super))
1204 * Figure out what to do with an inode that has both extents and inline data
1205 * inode flags set. Returns -1 if we decide to erase the inode, 0 otherwise.
1207 static int fix_inline_data_extents_file(e2fsck_t ctx,
1209 struct ext2_inode *inode,
1211 struct problem_context *pctx)
1213 size_t max_inline_ea_size;
1214 ext2_filsys fs = ctx->fs;
1217 /* Both feature flags not set? Just run the regular checks */
1218 if (!ext2fs_has_feature_extents(fs->super) &&
1219 !ext2fs_has_feature_inline_data(fs->super))
1222 /* Clear both flags if it's a special file */
1223 if (LINUX_S_ISCHR(inode->i_mode) ||
1224 LINUX_S_ISBLK(inode->i_mode) ||
1225 LINUX_S_ISFIFO(inode->i_mode) ||
1226 LINUX_S_ISSOCK(inode->i_mode)) {
1227 check_extents_inlinedata(ctx, pctx);
1231 /* If it looks like an extent tree, try to clear inlinedata */
1232 if (ext2fs_extent_header_verify(inode->i_block,
1233 sizeof(inode->i_block)) == 0 &&
1234 fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
1235 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1240 /* If it looks short enough to be inline data, try to clear extents */
1241 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
1242 max_inline_ea_size = inode_size -
1243 (EXT2_GOOD_OLD_INODE_SIZE +
1244 ((struct ext2_inode_large *)inode)->i_extra_isize);
1246 max_inline_ea_size = 0;
1247 if (EXT2_I_SIZE(inode) <
1248 EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
1249 fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
1250 inode->i_flags &= ~EXT4_EXTENTS_FL;
1256 * Too big for inline data, but no evidence of extent tree -
1257 * maybe it's a block map file? If the mappings all look valid?
1259 if (could_be_block_map(fs, inode) &&
1260 fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
1261 #ifdef WORDS_BIGENDIAN
1264 for (i = 0; i < EXT2_N_BLOCKS; i++)
1265 inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
1268 inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
1273 /* Oh well, just clear the busted inode. */
1274 if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
1275 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1281 e2fsck_write_inode(ctx, ino, inode, "pass1");
1286 static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
1288 ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
1289 dgrp_t start = *group, grp, grp_end = ctx->fs->group_desc_count;
1290 blk64_t blocks_to_read = 0;
1291 errcode_t err = EXT2_ET_INVALID_ARGUMENT;
1294 if (ctx->fs->fs_num_threads > 1)
1295 grp_end = ctx->thread_info.et_group_end;
1297 if (ctx->readahead_kb == 0)
1300 /* Keep iterating groups until we have enough to readahead */
1301 inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
1302 for (grp = start; grp < grp_end; grp++) {
1303 if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
1305 inodes_in_group = ctx->fs->super->s_inodes_per_group -
1306 ext2fs_bg_itable_unused(ctx->fs, grp);
1307 blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
1309 if (blocks_to_read * ctx->fs->blocksize >
1310 ctx->readahead_kb * 1024)
1314 err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
1316 if (err == EAGAIN) {
1317 ctx->readahead_kb /= 2;
1323 /* Error; disable itable readahead */
1324 *group = ctx->fs->group_desc_count;
1325 *next_ino = ctx->fs->super->s_inodes_count;
1328 * Don't do more readahead until we've reached the first inode
1329 * of the last inode scan buffer block for the last group.
1332 inodes_per_buffer = (ctx->inode_buffer_blocks ?
1333 ctx->inode_buffer_blocks :
1334 EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
1335 ctx->fs->blocksize /
1336 EXT2_INODE_SIZE(ctx->fs->super);
1338 *next_ino = inodes_in_group -
1339 (inodes_in_group % inodes_per_buffer) + 1 +
1340 (grp * ctx->fs->super->s_inodes_per_group);
1345 * Check if the passed ino is one of the used superblock quota inodes.
1347 * Before the quota inodes were journaled, older superblock quota inodes
1348 * were just regular files in the filesystem and not reserved inodes. This
1349 * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1350 * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1352 static int quota_inum_is_super(struct ext2_super_block *sb, ext2_ino_t ino)
1354 enum quota_type qtype;
1356 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1357 if (*quota_sb_inump(sb, qtype) == ino)
1364 * Check if the passed ino is one of the reserved quota inodes.
1365 * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1366 * inodes. These inodes may or may not be in use by the quota feature.
1368 static int quota_inum_is_reserved(ext2_filsys fs, ext2_ino_t ino)
1370 enum quota_type qtype;
1372 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1373 if (quota_type2inum(qtype, fs->super) == ino)
1379 static int e2fsck_should_abort(e2fsck_t ctx)
1381 e2fsck_t global_ctx;
1383 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1386 if (ctx->global_ctx) {
1387 global_ctx = ctx->global_ctx;
1388 if (global_ctx->flags & E2F_FLAG_SIGNAL_MASK)
1394 static void init_ext2_max_sizes()
1400 * Init ext2_max_sizes which will be immutable and shared between
1403 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1405 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
1406 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
1407 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
1408 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
1409 max_sizes = (max_sizes * (1UL << i));
1410 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
1416 /* TODO: tdb needs to be handled properly for multiple threads*/
1417 static int multiple_threads_supported(e2fsck_t ctx)
1420 unsigned int threshold;
1421 ext2_ino_t num_dirs;
1426 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
1428 profile_get_uint(ctx->profile, "scratch_files",
1429 "numdirs_threshold", 0, 0, &threshold);
1430 profile_get_boolean(ctx->profile, "scratch_files",
1431 "icount", 0, 1, &enable);
1433 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
1435 num_dirs = 1024; /* Guess */
1437 /* tdb is unsupported now */
1438 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
1439 (!threshold || num_dirs > threshold))
1446 * Even though we could specify number of threads,
1447 * but it might be more than the whole filesystem
1448 * block groups, correct it here.
1450 static void e2fsck_pass1_set_thread_num(e2fsck_t ctx)
1452 unsigned flexbg_size = 1;
1453 ext2_filsys fs = ctx->fs;
1454 int num_threads = ctx->pfs_num_threads;
1457 if (num_threads < 1) {
1462 if (!multiple_threads_supported(ctx)) {
1464 fprintf(stderr, "Fall through single thread for pass1 "
1465 "because tdb could not handle properly\n");
1469 if (ext2fs_has_feature_flex_bg(fs->super))
1470 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1471 max_threads = fs->group_desc_count / flexbg_size;
1472 if (max_threads == 0)
1474 if (max_threads > E2FSCK_MAX_THREADS)
1475 max_threads = E2FSCK_MAX_THREADS;
1477 if (num_threads > max_threads) {
1478 fprintf(stderr, "Use max possible thread num: %d instead\n",
1480 num_threads = max_threads;
1483 ctx->pfs_num_threads = num_threads;
1484 ctx->fs->fs_num_threads = num_threads;
1489 * We need call mark_table_blocks() before multiple
1490 * thread start, since all known system blocks should be
1491 * marked and checked later.
1493 static errcode_t e2fsck_pass1_prepare(e2fsck_t ctx)
1495 struct problem_context pctx;
1496 ext2_filsys fs = ctx->fs;
1497 unsigned long long readahead_kb;
1499 init_ext2_max_sizes();
1501 e2fsck_pass1_set_thread_num(ctx);
1503 /* If we can do readahead, figure out how many groups to pull in. */
1504 if (!e2fsck_can_readahead(ctx->fs))
1505 ctx->readahead_kb = 0;
1506 else if (ctx->readahead_kb == ~0ULL)
1507 ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
1510 /* don't use more than 1/10 of memory for threads checking */
1511 readahead_kb = get_memory_size() / (10 * ctx->pfs_num_threads);
1512 /* maybe better disable RA if this is too small? */
1513 if (ctx->readahead_kb > readahead_kb)
1514 ctx->readahead_kb = readahead_kb;
1516 clear_problem_context(&pctx);
1517 if (!(ctx->options & E2F_OPT_PREEN))
1518 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1520 pctx.errcode = e2fsck_allocate_subcluster_bitmap(ctx->fs,
1521 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
1522 "block_found_map", &ctx->block_found_map);
1525 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1526 ctx->flags |= E2F_FLAG_ABORT;
1527 return pctx.errcode;
1529 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1530 _("metadata block map"), EXT2FS_BMAP64_RBTREE,
1531 "block_metadata_map", &ctx->block_metadata_map);
1534 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1535 ctx->flags |= E2F_FLAG_ABORT;
1536 return pctx.errcode;
1539 mark_table_blocks(ctx);
1540 pctx.errcode = ext2fs_convert_subcluster_bitmap(ctx->fs,
1541 &ctx->block_found_map);
1543 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1544 ctx->flags |= E2F_FLAG_ABORT;
1545 return pctx.errcode;
1548 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1549 _("multiply claimed block map"),
1550 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1551 &ctx->block_dup_map);
1554 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1556 /* Should never get here */
1557 ctx->flags |= E2F_FLAG_ABORT;
1558 return pctx.errcode;
1561 if (ext2fs_has_feature_mmp(fs->super) &&
1562 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1563 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1564 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1565 fs->super->s_mmp_block);
1567 pthread_rwlock_init(&ctx->fs_fix_rwlock, NULL);
1568 pthread_rwlock_init(&ctx->fs_block_map_rwlock, NULL);
1569 if (ctx->pfs_num_threads > 1)
1570 ctx->fs_need_locking = 1;
1576 static void e2fsck_pass1_post(e2fsck_t ctx)
1578 struct problem_context pctx;
1579 ext2_filsys fs = ctx->fs;
1582 if (e2fsck_should_abort(ctx))
1585 block_buf = (char *)e2fsck_allocate_memory(ctx, ctx->fs->blocksize * 3,
1586 "block interate buffer");
1587 reserve_block_for_root_repair(ctx);
1588 reserve_block_for_lnf_repair(ctx);
1591 * If any extended attribute blocks' reference counts need to
1592 * be adjusted, either up (ctx->refcount_extra), or down
1593 * (ctx->refcount), then fix them.
1595 if (ctx->refcount) {
1596 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1597 ea_refcount_free(ctx->refcount);
1600 if (ctx->refcount_extra) {
1601 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1603 ea_refcount_free(ctx->refcount_extra);
1604 ctx->refcount_extra = 0;
1607 if (ctx->invalid_bitmaps)
1608 handle_fs_bad_blocks(ctx);
1610 /* We don't need the block_ea_map any more */
1611 if (ctx->block_ea_map) {
1612 ext2fs_free_block_bitmap(ctx->block_ea_map);
1613 ctx->block_ea_map = 0;
1616 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1617 struct ext2_inode *inode;
1618 int inode_size = EXT2_INODE_SIZE(fs->super);
1619 inode = e2fsck_allocate_memory(ctx, inode_size,
1622 clear_problem_context(&pctx);
1623 pctx.errcode = ext2fs_create_resize_inode(fs);
1625 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1627 ctx->flags |= E2F_FLAG_ABORT;
1628 ext2fs_free_mem(&inode);
1629 ext2fs_free_mem(&block_buf);
1634 if (!pctx.errcode) {
1635 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1637 inode->i_mtime = ctx->now;
1638 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1641 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1642 ext2fs_free_mem(&inode);
1645 if (ctx->flags & E2F_FLAG_RESTART) {
1646 ext2fs_free_mem(&block_buf);
1650 if (ctx->block_dup_map) {
1651 if (!(ctx->flags & E2F_FLAG_DUP_BLOCK)) {
1652 ext2fs_free_mem(&block_buf);
1655 if (ctx->options & E2F_OPT_PREEN) {
1656 clear_problem_context(&pctx);
1657 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1659 e2fsck_pass1_dupblocks(ctx, block_buf);
1660 ext2fs_free_mem(&block_buf);
1661 ctx->flags &= ~E2F_FLAG_DUP_BLOCK;
1664 ctx->flags |= E2F_FLAG_ALLOC_OK;
1669 * Lustre FS creates special inodes - precreated objects.
1670 * They are zero-sized and have special attributes:
1671 * mode |= S_ISUID | S_ISGID;
1672 * valid |= LA_ATIME | LA_MTIME | LA_CTIME;
1677 static int precreated_object(struct ext2_inode *inode)
1679 if (((inode->i_mode & (S_ISUID | S_ISGID)) == (S_ISUID | S_ISGID)) &&
1680 inode->i_ctime == 0)
1685 void e2fsck_pass1_run(e2fsck_t ctx)
1688 ext2_filsys fs = ctx->fs;
1690 struct ext2_inode *inode = NULL;
1691 ext2_inode_scan scan = NULL;
1692 char *block_buf = NULL;
1693 #ifdef RESOURCE_TRACK
1694 struct resource_track rtrack;
1696 unsigned char frag, fsize;
1697 struct problem_context pctx;
1698 struct scan_callback_struct scan_struct;
1699 struct ext2_super_block *sb = ctx->fs->super;
1701 const char *eop_next_inode = _("getting next inode from scan");
1702 int imagic_fs, extent_fs, inlinedata_fs, casefold_fs;
1703 int low_dtime_check = 1;
1704 unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
1705 unsigned int bufsize;
1706 int failed_csum = 0;
1707 ext2_ino_t ino_threshold = 0;
1708 dgrp_t ra_group = 0;
1709 struct ea_quota ea_ibody_quota;
1710 struct process_inode_block *inodes_to_process;
1711 int process_inode_count, check_mmp;
1712 e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
1715 init_resource_track(&rtrack, ctx->fs->io);
1716 clear_problem_context(&pctx);
1718 pass1_readahead(ctx, &ra_group, &ino_threshold);
1719 if (ext2fs_has_feature_dir_index(fs->super) &&
1720 !(ctx->options & E2F_OPT_NO)) {
1721 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
1722 ctx->dirs_to_hash = 0;
1726 mtrace_print("Pass 1");
1729 imagic_fs = ext2fs_has_feature_imagic_inodes(sb);
1730 extent_fs = ext2fs_has_feature_extents(sb);
1731 inlinedata_fs = ext2fs_has_feature_inline_data(sb);
1732 casefold_fs = ext2fs_has_feature_casefold(sb);
1735 * Allocate bitmaps structures
1737 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1738 EXT2FS_BMAP64_RBTREE,
1740 &ctx->inode_used_map);
1743 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1744 ctx->flags |= E2F_FLAG_ABORT;
1747 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1748 _("directory inode map"),
1749 ctx->global_ctx ? EXT2FS_BMAP64_RBTREE :
1750 EXT2FS_BMAP64_AUTODIR,
1751 "inode_dir_map", &ctx->inode_dir_map);
1754 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1755 ctx->flags |= E2F_FLAG_ABORT;
1758 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1759 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1760 "inode_reg_map", &ctx->inode_reg_map);
1763 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1764 ctx->flags |= E2F_FLAG_ABORT;
1769 e2fsck_allocate_inode_bitmap(fs,
1770 _("inode casefold map"),
1771 EXT2FS_BMAP64_RBTREE,
1772 "inode_casefold_map",
1773 &ctx->inode_casefold_map);
1776 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1777 ctx->flags |= E2F_FLAG_ABORT;
1781 pctx.errcode = e2fsck_setup_icount(ctx, "inode_link_info", 0, NULL,
1782 &ctx->inode_link_info);
1784 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1785 ctx->flags |= E2F_FLAG_ABORT;
1788 bufsize = inode_size;
1789 if (bufsize < sizeof(struct ext2_inode_large))
1790 bufsize = sizeof(struct ext2_inode_large);
1791 inode = (struct ext2_inode *)
1792 e2fsck_allocate_memory(ctx, bufsize, "scratch inode");
1794 inodes_to_process = (struct process_inode_block *)
1795 e2fsck_allocate_memory(ctx,
1796 (ctx->process_inode_size *
1797 sizeof(struct process_inode_block)),
1798 "array of inodes to process");
1799 process_inode_count = 0;
1801 pctx.errcode = ext2fs_init_dblist(fs, 0);
1803 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1804 ctx->flags |= E2F_FLAG_ABORT;
1809 * If the last orphan field is set, clear it, since the pass1
1810 * processing will automatically find and clear the orphans.
1811 * In the future, we may want to try using the last_orphan
1812 * linked list ourselves, but for now, we clear it so that the
1813 * ext3 mount code won't get confused.
1815 if (!(ctx->options & E2F_OPT_READONLY)) {
1816 if (fs->super->s_last_orphan) {
1817 fs->super->s_last_orphan = 0;
1818 ext2fs_mark_super_dirty(fs);
1822 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1823 "block interate buffer");
1824 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1825 e2fsck_use_inode_shortcuts(ctx, 1);
1826 e2fsck_intercept_block_allocations(ctx);
1827 old_op = ehandler_operation(_("opening inode scan"));
1828 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1830 ehandler_operation(old_op);
1832 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1833 ctx->flags |= E2F_FLAG_ABORT;
1836 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1837 EXT2_SF_WARN_GARBAGE_INODES, 0);
1838 ctx->stashed_inode = inode;
1839 scan_struct.ctx = ctx;
1840 scan_struct.block_buf = block_buf;
1841 scan_struct.inodes_to_process = inodes_to_process;
1842 scan_struct.process_inode_count = &process_inode_count;
1843 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1844 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1845 ctx->fs->group_desc_count)))
1847 if ((fs->super->s_wtime &&
1848 fs->super->s_wtime < fs->super->s_inodes_count) ||
1849 (fs->super->s_mtime &&
1850 fs->super->s_mtime < fs->super->s_inodes_count) ||
1851 (fs->super->s_mkfs_time &&
1852 fs->super->s_mkfs_time < fs->super->s_inodes_count))
1853 low_dtime_check = 0;
1855 /* Set up ctx->lost_and_found if possible */
1856 (void) e2fsck_get_lost_and_found(ctx, 0);
1859 if (ctx->global_ctx) {
1860 if (ctx->options & E2F_OPT_DEBUG &&
1861 ctx->options & E2F_OPT_MULTITHREAD)
1862 log_out(ctx, "jumping to group %u\n",
1863 ctx->thread_info.et_group_start);
1864 pctx.errcode = ext2fs_inode_scan_goto_blockgroup(scan,
1865 ctx->thread_info.et_group_start);
1867 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1868 ctx->flags |= E2F_FLAG_ABORT;
1876 e2fsck_pass1_check_lock(ctx);
1878 if (!global_ctx->mmp_update_thread) {
1879 e2fsck_pass1_block_map_w_lock(ctx);
1880 if (!global_ctx->mmp_update_thread) {
1881 global_ctx->mmp_update_thread =
1882 ctx->thread_info.et_thread_index + 1;
1885 e2fsck_pass1_block_map_w_unlock(ctx);
1888 /* only one active thread could update mmp block. */
1889 e2fsck_pass1_block_map_r_lock(ctx);
1890 if (global_ctx->mmp_update_thread ==
1891 ctx->thread_info.et_thread_index + 1)
1893 e2fsck_pass1_block_map_r_unlock(ctx);
1898 if (check_mmp && (ino % (fs->super->s_inodes_per_group * 4) == 1)) {
1899 if (e2fsck_mmp_update(fs))
1900 fatal_error(ctx, 0);
1902 old_op = ehandler_operation(eop_next_inode);
1903 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1905 if (ino > ino_threshold)
1906 pass1_readahead(ctx, &ra_group, &ino_threshold);
1907 ehandler_operation(old_op);
1908 if (e2fsck_should_abort(ctx)) {
1909 e2fsck_pass1_check_unlock(ctx);
1912 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1914 * If badblocks says badblocks is bad, offer to clear
1915 * the list, update the in-core bb list, and restart
1918 if (ino == EXT2_BAD_INO &&
1919 fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1923 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1924 ext2fs_badblocks_list_free(ctx->fs->badblocks);
1925 ctx->fs->badblocks = NULL;
1926 err = ext2fs_read_bb_inode(ctx->fs,
1927 &ctx->fs->badblocks);
1929 fix_problem(ctx, PR_1_ISCAN_ERROR,
1931 ctx->flags |= E2F_FLAG_ABORT;
1932 e2fsck_pass1_check_unlock(ctx);
1935 ctx->flags |= E2F_FLAG_RESTART;
1936 err = ext2fs_inode_scan_goto_blockgroup(scan,
1939 fix_problem(ctx, PR_1_ISCAN_ERROR,
1941 ctx->flags |= E2F_FLAG_ABORT;
1942 e2fsck_pass1_check_unlock(ctx);
1945 e2fsck_pass1_check_unlock(ctx);
1948 if (!ctx->inode_bb_map)
1950 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1951 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1952 e2fsck_pass1_check_unlock(ctx);
1955 if (pctx.errcode == EXT2_ET_SCAN_FINISHED) {
1956 e2fsck_pass1_check_unlock(ctx);
1960 pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1961 pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1962 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1963 ctx->flags |= E2F_FLAG_ABORT;
1964 e2fsck_pass1_check_unlock(ctx);
1968 e2fsck_pass1_check_unlock(ctx);
1972 if (ctx->global_ctx)
1973 ctx->thread_info.et_inode_number++;
1977 ctx->stashed_ino = ino;
1979 /* Clear trashed inode? */
1980 if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
1981 inode->i_links_count > 0 &&
1982 fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
1984 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1986 failed_csum = pctx.errcode != 0;
1989 * Check for inodes who might have been part of the
1990 * orphaned list linked list. They should have gotten
1991 * dealt with by now, unless the list had somehow been
1994 * FIXME: In the future, inodes which are still in use
1995 * (and which are therefore) pending truncation should
1996 * be handled specially. Right now we just clear the
1997 * dtime field, and the normal e2fsck handling of
1998 * inodes where i_size and the inode blocks are
1999 * inconsistent is to fix i_size, instead of releasing
2000 * the extra blocks. This won't catch the inodes that
2001 * was at the end of the orphan list, but it's better
2002 * than nothing. The right answer is that there
2003 * shouldn't be any bugs in the orphan list handling. :-)
2005 if (inode->i_dtime && low_dtime_check &&
2006 inode->i_dtime < ctx->fs->super->s_inodes_count) {
2007 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
2008 inode->i_dtime = inode->i_links_count ?
2010 e2fsck_write_inode(ctx, ino, inode,
2016 if (inode->i_links_count) {
2017 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
2018 ino, inode->i_links_count);
2020 pctx.num = inode->i_links_count;
2021 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
2022 ctx->flags |= E2F_FLAG_ABORT;
2023 e2fsck_pass1_check_unlock(ctx);
2026 } else if ((ino >= EXT2_FIRST_INODE(fs->super)) &&
2027 !quota_inum_is_reserved(fs, ino)) {
2028 if (!inode->i_dtime && inode->i_mode) {
2029 if (fix_problem(ctx,
2030 PR_1_ZERO_DTIME, &pctx)) {
2031 inode->i_dtime = ctx->now;
2032 e2fsck_write_inode(ctx, ino, inode,
2037 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2038 e2fsck_pass1_check_unlock(ctx);
2042 if ((inode->i_flags & EXT4_CASEFOLD_FL) &&
2043 ((!LINUX_S_ISDIR(inode->i_mode) &&
2044 fix_problem(ctx, PR_1_CASEFOLD_NONDIR, &pctx)) ||
2046 fix_problem(ctx, PR_1_CASEFOLD_FEATURE, &pctx)))) {
2047 inode->i_flags &= ~EXT4_CASEFOLD_FL;
2048 e2fsck_write_inode(ctx, ino, inode, "pass1");
2051 /* Conflicting inlinedata/extents inode flags? */
2052 if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
2053 (inode->i_flags & EXT4_EXTENTS_FL)) {
2054 int res = fix_inline_data_extents_file(ctx, ino, inode,
2058 /* skip FINISH_INODE_LOOP */
2059 e2fsck_pass1_check_unlock(ctx);
2064 /* Test for incorrect inline_data flags settings. */
2065 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
2066 (ino >= EXT2_FIRST_INODE(fs->super))) {
2069 pctx.errcode = get_inline_data_ea_size(fs, ino, &size);
2070 if (!pctx.errcode &&
2071 fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
2072 e2fsck_pass1_fix_lock(ctx);
2073 ext2fs_set_feature_inline_data(sb);
2074 ext2fs_mark_super_dirty(fs);
2075 e2fsck_pass1_fix_unlock(ctx);
2077 } else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
2078 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2079 /* skip FINISH_INODE_LOOP */
2080 e2fsck_pass1_check_unlock(ctx);
2085 /* Test for inline data flag but no attr */
2086 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
2087 (ino >= EXT2_FIRST_INODE(fs->super))) {
2094 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2095 err = get_inline_data_ea_size(fs, ino, &size);
2096 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
2097 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
2101 /* Everything is awesome... */
2103 case EXT2_ET_BAD_EA_BLOCK_NUM:
2104 case EXT2_ET_BAD_EA_HASH:
2105 case EXT2_ET_BAD_EA_HEADER:
2106 case EXT2_ET_EA_BAD_NAME_LEN:
2107 case EXT2_ET_EA_BAD_VALUE_SIZE:
2108 case EXT2_ET_EA_KEY_NOT_FOUND:
2109 case EXT2_ET_EA_NO_SPACE:
2110 case EXT2_ET_MISSING_EA_FEATURE:
2111 case EXT2_ET_INLINE_DATA_CANT_ITERATE:
2112 case EXT2_ET_INLINE_DATA_NO_BLOCK:
2113 case EXT2_ET_INLINE_DATA_NO_SPACE:
2114 case EXT2_ET_NO_INLINE_DATA:
2115 case EXT2_ET_EXT_ATTR_CSUM_INVALID:
2116 case EXT2_ET_EA_BAD_VALUE_OFFSET:
2117 case EXT2_ET_EA_INODE_CORRUPTED:
2118 /* broken EA or no system.data EA; truncate */
2119 if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
2121 err = ext2fs_inode_size_set(fs, inode, 0);
2124 ctx->flags |= E2F_FLAG_ABORT;
2125 e2fsck_pass1_check_unlock(ctx);
2128 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
2129 memset(&inode->i_block, 0,
2130 sizeof(inode->i_block));
2131 e2fsck_write_inode(ctx, ino, inode,
2137 /* Some other kind of non-xattr error? */
2139 ctx->flags |= E2F_FLAG_ABORT;
2140 e2fsck_pass1_check_unlock(ctx);
2146 * Test for incorrect extent flag settings.
2148 * On big-endian machines we must be careful:
2149 * When the inode is read, the i_block array is not swapped
2150 * if the extent flag is set. Therefore if we are testing
2151 * for or fixing a wrongly-set flag, we must potentially
2152 * (un)swap before testing, or after fixing.
2156 * In this case the extents flag was set when read, so
2157 * extent_header_verify is ok. If the inode is cleared,
2158 * no need to swap... so no extra swapping here.
2160 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
2161 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2162 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
2163 if ((ext2fs_extent_header_verify(inode->i_block,
2164 sizeof(inode->i_block)) == 0) &&
2165 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
2166 e2fsck_pass1_fix_lock(ctx);
2167 ext2fs_set_feature_extents(sb);
2168 ext2fs_mark_super_dirty(fs);
2170 e2fsck_pass1_fix_unlock(ctx);
2171 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
2173 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2174 if (ino == EXT2_BAD_INO)
2175 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
2177 /* skip FINISH_INODE_LOOP */
2178 e2fsck_pass1_check_unlock(ctx);
2184 * For big-endian machines:
2185 * If the inode didn't have the extents flag set when it
2186 * was read, then the i_blocks array was swapped. To test
2187 * as an extents header, we must swap it back first.
2188 * IF we then set the extents flag, the entire i_block
2189 * array must be un/re-swapped to make it proper extents data.
2191 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
2192 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2193 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
2194 (LINUX_S_ISREG(inode->i_mode) ||
2195 LINUX_S_ISDIR(inode->i_mode))) {
2197 #ifdef WORDS_BIGENDIAN
2198 __u32 tmp_block[EXT2_N_BLOCKS];
2200 for (i = 0; i < EXT2_N_BLOCKS; i++)
2201 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
2204 ehp = inode->i_block;
2206 if ((ext2fs_extent_header_verify(ehp,
2207 sizeof(inode->i_block)) == 0) &&
2208 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
2209 inode->i_flags |= EXT4_EXTENTS_FL;
2210 #ifdef WORDS_BIGENDIAN
2211 memcpy(inode->i_block, tmp_block,
2212 sizeof(inode->i_block));
2214 e2fsck_write_inode(ctx, ino, inode, "pass1");
2217 /* Consider an inode in extent fs w/o extents
2218 * at least a bit suspect. It only matters if
2219 * the inode has several other problems. */
2220 e2fsck_mark_inode_bad(ctx, &pctx,
2221 PR_1_UNSET_EXTENT_FL);
2225 if (ino == EXT2_BAD_INO) {
2226 struct process_block_struct pb;
2228 if ((failed_csum || inode->i_mode || inode->i_uid ||
2229 inode->i_gid || inode->i_links_count ||
2230 (inode->i_flags & EXT4_INLINE_DATA_FL) ||
2231 inode->i_file_acl) &&
2232 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
2233 memset(inode, 0, sizeof(struct ext2_inode));
2234 e2fsck_write_inode(ctx, ino, inode,
2239 e2fsck_pass1_block_map_r_lock(ctx);
2240 pctx.errcode = ext2fs_copy_bitmap(ctx->global_ctx ?
2241 ctx->global_ctx->block_found_map :
2242 ctx->block_found_map, &pb.fs_meta_blocks);
2243 e2fsck_pass1_block_map_r_unlock(ctx);
2246 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
2247 ctx->flags |= E2F_FLAG_ABORT;
2248 e2fsck_pass1_check_unlock(ctx);
2251 pb.ino = EXT2_BAD_INO;
2252 pb.num_blocks = pb.last_block = 0;
2253 pb.last_db_block = -1;
2254 pb.num_illegal_blocks = 0;
2255 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
2256 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
2260 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
2261 block_buf, process_bad_block, &pb);
2262 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
2264 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
2265 ctx->flags |= E2F_FLAG_ABORT;
2266 e2fsck_pass1_check_unlock(ctx);
2270 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
2271 ctx->flags |= E2F_FLAG_ABORT;
2272 e2fsck_pass1_check_unlock(ctx);
2275 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2276 clear_problem_context(&pctx);
2277 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2278 e2fsck_pass1_check_unlock(ctx);
2280 } else if (ino == EXT2_ROOT_INO) {
2282 * Make sure the root inode is a directory; if
2283 * not, offer to clear it. It will be
2284 * regenerated in pass #3.
2286 if (!LINUX_S_ISDIR(inode->i_mode)) {
2287 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
2291 * If dtime is set, offer to clear it. mke2fs
2292 * version 0.2b created filesystems with the
2293 * dtime field set for the root and lost+found
2294 * directories. We won't worry about
2295 * /lost+found, since that can be regenerated
2296 * easily. But we will fix the root directory
2297 * as a special case.
2299 if (inode->i_dtime && inode->i_links_count) {
2300 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
2302 e2fsck_write_inode(ctx, ino, inode,
2307 } else if (ino == EXT2_JOURNAL_INO) {
2308 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2309 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
2310 if (!LINUX_S_ISREG(inode->i_mode) &&
2311 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
2313 inode->i_mode = LINUX_S_IFREG;
2314 e2fsck_write_inode(ctx, ino, inode,
2318 check_blocks(ctx, &pctx, block_buf, NULL);
2319 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2320 e2fsck_pass1_check_unlock(ctx);
2323 if ((inode->i_links_count ||
2324 inode->i_blocks || inode->i_block[0]) &&
2325 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
2327 memset(inode, 0, inode_size);
2328 ext2fs_icount_store(ctx->inode_link_info,
2330 e2fsck_write_inode_full(ctx, ino, inode,
2331 inode_size, "pass1");
2334 } else if (quota_inum_is_reserved(fs, ino)) {
2335 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2336 if (ext2fs_has_feature_quota(fs->super) &&
2337 quota_inum_is_super(fs->super, ino)) {
2338 if (!LINUX_S_ISREG(inode->i_mode) &&
2339 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
2341 inode->i_mode = LINUX_S_IFREG;
2342 e2fsck_write_inode(ctx, ino, inode,
2346 check_blocks(ctx, &pctx, block_buf, NULL);
2347 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2348 e2fsck_pass1_check_unlock(ctx);
2351 if ((inode->i_links_count ||
2352 inode->i_blocks || inode->i_block[0]) &&
2353 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
2355 memset(inode, 0, inode_size);
2356 ext2fs_icount_store(ctx->inode_link_info,
2358 e2fsck_write_inode_full(ctx, ino, inode,
2359 inode_size, "pass1");
2362 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
2363 problem_t problem = 0;
2365 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2366 if (ino == EXT2_BOOT_LOADER_INO) {
2367 if (LINUX_S_ISDIR(inode->i_mode))
2368 problem = PR_1_RESERVED_BAD_MODE;
2369 } else if (ino == EXT2_RESIZE_INO) {
2370 if (inode->i_mode &&
2371 !LINUX_S_ISREG(inode->i_mode))
2372 problem = PR_1_RESERVED_BAD_MODE;
2374 if (inode->i_mode != 0)
2375 problem = PR_1_RESERVED_BAD_MODE;
2378 if (fix_problem(ctx, problem, &pctx)) {
2380 e2fsck_write_inode(ctx, ino, inode,
2385 check_blocks(ctx, &pctx, block_buf, NULL);
2386 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2387 e2fsck_pass1_check_unlock(ctx);
2391 if (!inode->i_links_count) {
2392 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2393 e2fsck_pass1_check_unlock(ctx);
2397 * n.b. 0.3c ext2fs code didn't clear i_links_count for
2398 * deleted files. Oops.
2400 * Since all new ext2 implementations get this right,
2401 * we now assume that the case of non-zero
2402 * i_links_count and non-zero dtime means that we
2403 * should keep the file, not delete it.
2406 if (inode->i_dtime) {
2407 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
2409 e2fsck_write_inode(ctx, ino, inode, "pass1");
2414 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2415 switch (fs->super->s_creator_os) {
2417 frag = inode->osd2.hurd2.h_i_frag;
2418 fsize = inode->osd2.hurd2.h_i_fsize;
2424 /* Fixed in pass2, e2fsck_process_bad_inode(). */
2425 if (inode->i_faddr || frag || fsize ||
2426 (!ext2fs_has_feature_largedir(fs->super) &&
2427 LINUX_S_ISDIR(inode->i_mode) && inode->i_size_high))
2428 e2fsck_mark_inode_bad(ctx, &pctx,
2429 PR_2_DIR_SIZE_HIGH_ZERO);
2430 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2431 !ext2fs_has_feature_64bit(fs->super) &&
2432 inode->osd2.linux2.l_i_file_acl_high != 0)
2433 e2fsck_mark_inode_bad(ctx, &pctx,
2434 PR_2_I_FILE_ACL_HI_ZERO);
2435 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2436 !ext2fs_has_feature_huge_file(fs->super) &&
2437 (inode->osd2.linux2.l_i_blocks_hi != 0))
2438 e2fsck_mark_inode_bad(ctx, &pctx, PR_2_BLOCKS_HI_ZERO);
2439 if (inode->i_flags & EXT2_IMAGIC_FL) {
2441 if (!ctx->inode_imagic_map)
2442 alloc_imagic_map(ctx);
2443 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
2446 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
2447 inode->i_flags &= ~EXT2_IMAGIC_FL;
2448 e2fsck_write_inode(ctx, ino,
2455 check_inode_extra_space(ctx, &pctx, &ea_ibody_quota);
2456 check_is_really_dir(ctx, &pctx, block_buf);
2459 * ext2fs_inode_has_valid_blocks2 does not actually look
2460 * at i_block[] values, so not endian-sensitive here.
2462 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
2463 LINUX_S_ISLNK(inode->i_mode) &&
2464 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
2465 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
2466 inode->i_flags &= ~EXT4_EXTENTS_FL;
2467 e2fsck_write_inode(ctx, ino, inode, "pass1");
2471 if ((inode->i_flags & EXT4_ENCRYPT_FL) &&
2472 add_encrypted_file(ctx, &pctx) < 0)
2475 if (casefold_fs && inode->i_flags & EXT4_CASEFOLD_FL)
2476 ext2fs_mark_inode_bitmap2(ctx->inode_casefold_map, ino);
2478 if (LINUX_S_ISDIR(inode->i_mode)) {
2479 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
2480 e2fsck_add_dir_info(ctx, ino, 0);
2481 ctx->fs_directory_count++;
2482 if (inode->i_flags & EXT4_CASEFOLD_FL)
2483 add_casefolded_dir(ctx, ino);
2484 } else if (LINUX_S_ISREG (inode->i_mode)) {
2485 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
2486 ctx->fs_regular_count++;
2487 } else if (LINUX_S_ISCHR (inode->i_mode) &&
2488 e2fsck_pass1_check_device_inode(fs, inode)) {
2489 check_extents_inlinedata(ctx, &pctx);
2490 check_immutable(ctx, &pctx);
2491 check_size(ctx, &pctx);
2492 ctx->fs_chardev_count++;
2493 } else if (LINUX_S_ISBLK (inode->i_mode) &&
2494 e2fsck_pass1_check_device_inode(fs, inode)) {
2495 check_extents_inlinedata(ctx, &pctx);
2496 check_immutable(ctx, &pctx);
2497 check_size(ctx, &pctx);
2498 ctx->fs_blockdev_count++;
2499 } else if (LINUX_S_ISLNK (inode->i_mode) &&
2500 e2fsck_pass1_check_symlink(fs, ino, inode,
2502 check_immutable(ctx, &pctx);
2503 ctx->fs_symlinks_count++;
2504 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
2505 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2506 e2fsck_pass1_check_unlock(ctx);
2508 } else if (ext2fs_is_fast_symlink(inode)) {
2509 ctx->fs_fast_symlinks_count++;
2510 check_blocks(ctx, &pctx, block_buf,
2512 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2513 e2fsck_pass1_check_unlock(ctx);
2517 else if (LINUX_S_ISFIFO (inode->i_mode) &&
2518 e2fsck_pass1_check_device_inode(fs, inode)) {
2519 check_extents_inlinedata(ctx, &pctx);
2520 check_immutable(ctx, &pctx);
2521 check_size(ctx, &pctx);
2522 ctx->fs_fifo_count++;
2523 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
2524 e2fsck_pass1_check_device_inode(fs, inode)) {
2525 check_extents_inlinedata(ctx, &pctx);
2526 check_immutable(ctx, &pctx);
2527 check_size(ctx, &pctx);
2528 ctx->fs_sockets_count++;
2530 e2fsck_mark_inode_bad(ctx, &pctx, PR_2_BAD_MODE);
2533 /* Future atime/mtime may be valid in rare cases, but are more
2534 * likely to indicate corruption. Don't try to fix timestamps,
2535 * but take into consideration whether inode is corrupted. If
2536 * no other problems with the inode, probably it is OK. */
2537 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
2538 e2fsck_mark_inode_bad(ctx, &pctx, PR_1_INODE_BAD_TIME);
2539 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime, ctx->time_fudge))
2540 e2fsck_mark_inode_bad(ctx, &pctx, PR_1_INODE_BAD_TIME);
2542 /* Since ctime cannot be set directly from userspace, consider
2543 * very old/future values worse than a bad atime/mtime. Same for
2544 * crtime, but it is checked in check_inode_extra_space(). */
2545 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
2546 e2fsck_mark_inode_badder(ctx, &pctx,
2547 PR_1_INODE_BAD_TIME);
2548 else if (!precreated_object(inode) &&
2549 EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
2551 e2fsck_mark_inode_badder(ctx, &pctx,
2552 PR_1_INODE_BAD_TIME);
2554 /* no restart if clearing bad inode before block processing */
2555 if (e2fsck_fix_bad_inode(ctx, &pctx)) {
2556 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2560 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2561 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
2562 if (inode->i_block[EXT2_IND_BLOCK])
2563 ctx->fs_ind_count++;
2564 if (inode->i_block[EXT2_DIND_BLOCK])
2565 ctx->fs_dind_count++;
2566 if (inode->i_block[EXT2_TIND_BLOCK])
2567 ctx->fs_tind_count++;
2569 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2570 !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
2571 (inode->i_block[EXT2_IND_BLOCK] ||
2572 inode->i_block[EXT2_DIND_BLOCK] ||
2573 inode->i_block[EXT2_TIND_BLOCK] ||
2574 ext2fs_file_acl_block(fs, inode))) {
2575 struct process_inode_block *itp;
2577 itp = &inodes_to_process[process_inode_count];
2579 itp->ea_ibody_quota = ea_ibody_quota;
2580 if (inode_size < sizeof(struct ext2_inode_large))
2581 memcpy(&itp->inode, inode, inode_size);
2583 memcpy(&itp->inode, inode, sizeof(itp->inode));
2584 process_inode_count++;
2586 check_blocks(ctx, &pctx, block_buf, &ea_ibody_quota);
2588 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2590 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
2591 struct ext2_inode_large *inode_l;
2593 inode_l = (struct ext2_inode_large *)inode;
2595 if (inode_l->i_extra_isize < ctx->want_extra_isize) {
2596 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
2597 inode_exp = e2fsck_pass1_expand_eisize(ctx,
2601 if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
2603 ctx->min_extra_isize = inode_l->i_extra_isize;
2606 if (e2fsck_should_abort(ctx)) {
2607 e2fsck_pass1_check_unlock(ctx);
2611 if (process_inode_count >= ctx->process_inode_size) {
2612 process_inodes(ctx, block_buf, inodes_to_process,
2613 &process_inode_count);
2615 if (e2fsck_should_abort(ctx)) {
2616 e2fsck_pass1_check_unlock(ctx);
2621 e2fsck_pass1_check_unlock(ctx);
2623 process_inodes(ctx, block_buf, inodes_to_process,
2624 &process_inode_count);
2625 ext2fs_close_inode_scan(scan);
2628 if (ctx->ea_block_quota_blocks) {
2629 ea_refcount_free(ctx->ea_block_quota_blocks);
2630 ctx->ea_block_quota_blocks = 0;
2633 if (ctx->ea_block_quota_inodes) {
2634 ea_refcount_free(ctx->ea_block_quota_inodes);
2635 ctx->ea_block_quota_inodes = 0;
2638 /* We don't need the encryption policy => ID map any more */
2639 destroy_encryption_policy_map(ctx);
2641 if (ctx->flags & E2F_FLAG_RESTART) {
2643 * Only the master copy of the superblock and block
2644 * group descriptors are going to be written during a
2645 * restart, so set the superblock to be used to be the
2646 * master superblock.
2648 ctx->use_superblock = 0;
2652 if (ctx->large_dirs && !ext2fs_has_feature_largedir(fs->super)) {
2653 if (fix_problem(ctx, PR_2_FEATURE_LARGE_DIRS, &pctx)) {
2654 ext2fs_set_feature_largedir(fs->super);
2655 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2656 ext2fs_mark_super_dirty(fs);
2658 if (fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
2659 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
2660 ext2fs_update_dynamic_rev(fs);
2661 ext2fs_mark_super_dirty(fs);
2665 ctx->flags |= E2F_FLAG_ALLOC_OK;
2666 ext2fs_free_mem(&inodes_to_process);
2668 e2fsck_use_inode_shortcuts(ctx, 0);
2669 ext2fs_free_mem(&inodes_to_process);
2670 inodes_to_process = 0;
2673 ext2fs_close_inode_scan(scan);
2675 ext2fs_free_mem(&block_buf);
2677 ext2fs_free_mem(&inode);
2680 * The l+f inode may have been cleared, so zap it now and
2681 * later passes will recalculate it if necessary
2683 ctx->lost_and_found = 0;
2685 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
2686 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
2688 ctx->invalid_bitmaps++;
2690 /* reset update_thread after this thread exit */
2691 e2fsck_pass1_block_map_w_lock(ctx);
2693 global_ctx->mmp_update_thread = 0;
2694 e2fsck_pass1_block_map_w_unlock(ctx);
2699 static errcode_t e2fsck_pass1_copy_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2700 ext2fs_generic_bitmap *dest)
2704 ret = ext2fs_copy_bitmap(*src, dest);
2713 static void e2fsck_pass1_free_bitmap(ext2fs_generic_bitmap *bitmap)
2716 ext2fs_free_generic_bmap(*bitmap);
2722 static errcode_t e2fsck_pass1_merge_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2723 ext2fs_generic_bitmap *dest)
2728 if (*dest == NULL) {
2732 ret = ext2fs_merge_bitmap(*src, *dest, NULL, NULL);
2742 static errcode_t e2fsck_pass1_copy_fs(ext2_filsys dest, e2fsck_t src_context,
2747 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2748 dest->inode_map = NULL;
2749 dest->block_map = NULL;
2750 dest->badblocks = NULL;
2752 dest->dblist->fs = dest;
2753 if (src->block_map) {
2754 retval = e2fsck_pass1_copy_bitmap(dest, &src->block_map,
2759 if (src->inode_map) {
2760 retval = e2fsck_pass1_copy_bitmap(dest, &src->inode_map,
2766 if (src->badblocks) {
2767 retval = ext2fs_badblocks_copy(src->badblocks,
2773 /* disable it for now */
2774 src_context->openfs_flags &= ~EXT2_FLAG_EXCLUSIVE;
2775 retval = ext2fs_open_channel(dest, src_context->io_options,
2776 src_context->io_manager,
2777 src_context->openfs_flags,
2778 src->io->block_size);
2782 /* Block size might not be default */
2783 io_channel_set_blksize(dest->io, src->io->block_size);
2784 ehandler_init(dest->io);
2786 assert(dest->io->magic == src->io->magic);
2787 assert(dest->io->manager == src->io->manager);
2788 assert(strcmp(dest->io->name, src->io->name) == 0);
2789 assert(dest->io->block_size == src->io->block_size);
2790 assert(dest->io->read_error == src->io->read_error);
2791 assert(dest->io->write_error == src->io->write_error);
2792 assert(dest->io->refcount == src->io->refcount);
2793 assert(dest->io->flags == src->io->flags);
2794 assert(dest->io->app_data == dest);
2795 assert(src->io->app_data == src);
2796 assert(dest->io->align == src->io->align);
2798 /* The data should be written to disk immediately */
2799 dest->io->flags |= CHANNEL_FLAGS_WRITETHROUGH;
2800 /* icache will be rebuilt if needed, so do not copy from @src */
2805 static int e2fsck_pass1_merge_fs(ext2_filsys dest, ext2_filsys src)
2807 struct ext2_inode_cache *icache = dest->icache;
2808 errcode_t retval = 0;
2810 io_channel dest_image_io;
2811 ext2fs_inode_bitmap inode_map;
2812 ext2fs_block_bitmap block_map;
2813 ext2_badblocks_list badblocks;
2816 e2fsck_t dest_ctx = dest->priv_data;
2819 dest_image_io = dest->image_io;
2820 inode_map = dest->inode_map;
2821 block_map = dest->block_map;
2822 badblocks = dest->badblocks;
2823 dblist = dest->dblist;
2824 flags = dest->flags;
2826 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2828 dest->image_io = dest_image_io;
2829 dest->icache = icache;
2830 dest->inode_map = inode_map;
2831 dest->block_map = block_map;
2832 dest->badblocks = badblocks;
2833 dest->dblist = dblist;
2834 dest->priv_data = dest_ctx;
2836 dest->dblist->fs = dest;
2837 dest->flags = src->flags | flags;
2838 if (!(src->flags & EXT2_FLAG_VALID) || !(flags & EXT2_FLAG_VALID))
2839 ext2fs_unmark_valid(dest);
2842 ext2fs_free_inode_cache(src->icache);
2846 retval = e2fsck_pass1_merge_bitmap(dest, &src->inode_map,
2851 retval = e2fsck_pass1_merge_bitmap(dest, &src->block_map,
2858 retval = ext2fs_merge_dblist(src->dblist,
2863 dest->dblist = src->dblist;
2864 dest->dblist->fs = dest;
2869 if (src->badblocks) {
2870 if (dest->badblocks == NULL)
2871 retval = ext2fs_badblocks_copy(src->badblocks,
2874 retval = ext2fs_badblocks_merge(src->badblocks,
2878 io_channel_close(src->io);
2880 ext2fs_free_generic_bmap(src->inode_map);
2882 ext2fs_free_generic_bmap(src->block_map);
2884 ext2fs_badblocks_list_free(src->badblocks);
2886 ext2fs_free_dblist(src->dblist);
2891 static void e2fsck_pass1_copy_invalid_bitmaps(e2fsck_t global_ctx,
2892 e2fsck_t thread_ctx)
2895 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2896 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2897 dgrp_t total = grp_end - grp_start;
2899 thread_ctx->invalid_inode_bitmap_flag =
2900 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2901 "invalid_inode_bitmap");
2902 thread_ctx->invalid_block_bitmap_flag =
2903 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2904 "invalid_block_bitmap");
2905 thread_ctx->invalid_inode_table_flag =
2906 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2907 "invalid_inode_table");
2909 memcpy(thread_ctx->invalid_block_bitmap_flag,
2910 &global_ctx->invalid_block_bitmap_flag[grp_start],
2911 total * sizeof(int));
2912 memcpy(thread_ctx->invalid_inode_bitmap_flag,
2913 &global_ctx->invalid_inode_bitmap_flag[grp_start],
2914 total * sizeof(int));
2915 memcpy(thread_ctx->invalid_inode_table_flag,
2916 &global_ctx->invalid_inode_table_flag[grp_start],
2917 total * sizeof(int));
2919 thread_ctx->invalid_bitmaps = 0;
2920 for (i = grp_start, j = 0; i < grp_end; i++, j++) {
2921 if (thread_ctx->invalid_block_bitmap_flag[j])
2922 thread_ctx->invalid_bitmaps++;
2923 if (thread_ctx->invalid_inode_bitmap_flag[j])
2924 thread_ctx->invalid_bitmaps++;
2925 if (thread_ctx->invalid_inode_table_flag[j])
2926 thread_ctx->invalid_bitmaps++;
2930 static void e2fsck_pass1_merge_invalid_bitmaps(e2fsck_t global_ctx,
2931 e2fsck_t thread_ctx)
2933 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2934 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2935 dgrp_t total = grp_end - grp_start;
2937 memcpy(&global_ctx->invalid_block_bitmap_flag[grp_start],
2938 thread_ctx->invalid_block_bitmap_flag, total * sizeof(int));
2939 memcpy(&global_ctx->invalid_inode_bitmap_flag[grp_start],
2940 thread_ctx->invalid_inode_bitmap_flag, total * sizeof(int));
2941 memcpy(&global_ctx->invalid_inode_table_flag[grp_start],
2942 thread_ctx->invalid_inode_table_flag, total * sizeof(int));
2943 global_ctx->invalid_bitmaps += thread_ctx->invalid_bitmaps;
2946 static errcode_t e2fsck_pass1_thread_prepare(e2fsck_t global_ctx, e2fsck_t *thread_ctx,
2947 int thread_index, int num_threads,
2948 dgrp_t average_group)
2951 e2fsck_t thread_context;
2952 ext2_filsys thread_fs;
2953 ext2_filsys global_fs = global_ctx->fs;
2954 struct e2fsck_thread *tinfo;
2956 assert(global_ctx->inode_used_map == NULL);
2957 assert(global_ctx->inode_dir_map == NULL);
2958 assert(global_ctx->inode_bb_map == NULL);
2959 assert(global_ctx->inode_imagic_map == NULL);
2960 assert(global_ctx->inode_reg_map == NULL);
2961 assert(global_ctx->inodes_to_rebuild == NULL);
2963 assert(global_ctx->block_found_map != NULL);
2964 assert(global_ctx->block_metadata_map != NULL);
2965 assert(global_ctx->block_dup_map != NULL);
2966 assert(global_ctx->block_ea_map == NULL);
2967 assert(global_ctx->fs->dblist == NULL);
2969 retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &thread_context);
2971 com_err(global_ctx->program_name, retval, "while allocating memory");
2974 memcpy(thread_context, global_ctx, sizeof(struct e2fsck_struct));
2975 thread_context->block_dup_map = NULL;
2976 thread_context->casefolded_dirs = NULL;
2977 thread_context->expand_eisize_map = NULL;
2978 thread_context->inode_badness = NULL;
2980 retval = e2fsck_allocate_block_bitmap(global_ctx->fs,
2981 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
2982 "block_found_map", &thread_context->block_found_map);
2986 thread_context->global_ctx = global_ctx;
2987 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &thread_fs);
2989 com_err(global_ctx->program_name, retval, "while allocating memory");
2993 io_channel_flush_cleanup(global_fs->io);
2994 retval = e2fsck_pass1_copy_fs(thread_fs, global_ctx, global_fs);
2996 com_err(global_ctx->program_name, retval, "while copying fs");
2999 thread_fs->priv_data = thread_context;
3001 thread_context->thread_info.et_thread_index = thread_index;
3002 set_up_logging(thread_context);
3004 tinfo = &thread_context->thread_info;
3005 tinfo->et_group_start = average_group * thread_index;
3006 if (thread_index == global_fs->fs_num_threads - 1)
3007 tinfo->et_group_end = thread_fs->group_desc_count;
3009 tinfo->et_group_end = average_group * (thread_index + 1);
3010 tinfo->et_group_next = tinfo->et_group_start;
3011 tinfo->et_inode_number = 0;
3012 tinfo->et_log_buf[0] = '\0';
3013 tinfo->et_log_length = 0;
3014 if (thread_context->options & E2F_OPT_MULTITHREAD)
3015 log_out(thread_context, _("Scan group range [%d, %d)\n"),
3016 tinfo->et_group_start, tinfo->et_group_end);
3017 thread_context->fs = thread_fs;
3018 retval = quota_init_context(&thread_context->qctx, thread_fs, 0);
3020 com_err(global_ctx->program_name, retval,
3021 "while init quota context");
3024 *thread_ctx = thread_context;
3025 e2fsck_pass1_copy_invalid_bitmaps(global_ctx, thread_context);
3028 ext2fs_free_mem(&thread_fs);
3030 if (thread_context->block_found_map)
3031 ext2fs_free_mem(&thread_context->block_found_map);
3032 ext2fs_free_mem(&thread_context);
3036 static void e2fsck_pass1_merge_dir_info(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3038 if (thread_ctx->dir_info == NULL)
3041 if (global_ctx->dir_info == NULL) {
3042 global_ctx->dir_info = thread_ctx->dir_info;
3043 thread_ctx->dir_info = NULL;
3047 e2fsck_merge_dir_info(global_ctx, thread_ctx->dir_info,
3048 global_ctx->dir_info);
3051 static void e2fsck_pass1_merge_dx_dir(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3053 if (thread_ctx->dx_dir_info == NULL)
3056 if (global_ctx->dx_dir_info == NULL) {
3057 global_ctx->dx_dir_info = thread_ctx->dx_dir_info;
3058 global_ctx->dx_dir_info_size = thread_ctx->dx_dir_info_size;
3059 global_ctx->dx_dir_info_count = thread_ctx->dx_dir_info_count;
3060 thread_ctx->dx_dir_info = NULL;
3064 e2fsck_merge_dx_dir(global_ctx, thread_ctx);
3067 static inline errcode_t
3068 e2fsck_pass1_merge_icount(ext2_icount_t *dest_icount,
3069 ext2_icount_t *src_icount)
3072 if (*dest_icount == NULL) {
3073 *dest_icount = *src_icount;
3078 ret = ext2fs_icount_merge(*src_icount,
3088 static errcode_t e2fsck_pass1_merge_icounts(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3092 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_count,
3093 &thread_ctx->inode_count);
3096 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_link_info,
3097 &thread_ctx->inode_link_info);
3101 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_badness,
3102 &thread_ctx->inode_badness);
3107 static errcode_t e2fsck_pass1_merge_dirs_to_hash(e2fsck_t global_ctx,
3108 e2fsck_t thread_ctx)
3110 errcode_t retval = 0;
3112 if (!thread_ctx->dirs_to_hash)
3115 if (!global_ctx->dirs_to_hash)
3116 retval = ext2fs_badblocks_copy(thread_ctx->dirs_to_hash,
3117 &global_ctx->dirs_to_hash);
3119 retval = ext2fs_badblocks_merge(thread_ctx->dirs_to_hash,
3120 global_ctx->dirs_to_hash);
3125 static errcode_t e2fsck_pass1_merge_ea_inode_refs(e2fsck_t global_ctx,
3126 e2fsck_t thread_ctx)
3132 if (!thread_ctx->ea_inode_refs)
3135 if (!global_ctx->ea_inode_refs) {
3136 global_ctx->ea_inode_refs = thread_ctx->ea_inode_refs;
3137 thread_ctx->ea_inode_refs = NULL;
3141 ea_refcount_intr_begin(thread_ctx->ea_inode_refs);
3143 if ((blk = ea_refcount_intr_next(thread_ctx->ea_inode_refs,
3146 if (!global_ctx->block_ea_map ||
3147 !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
3149 retval = ea_refcount_store(global_ctx->ea_inode_refs,
3159 static ea_value_t ea_refcount_usage(e2fsck_t ctx, blk64_t blk,
3162 ea_value_t count_cur;
3163 ea_value_t count_extra = 0;
3164 ea_value_t count_orig;
3166 ea_refcount_fetch(ctx->refcount_orig, blk, &count_orig);
3167 ea_refcount_fetch(ctx->refcount, blk, &count_cur);
3168 /* most of time this is not needed */
3169 if (ctx->refcount_extra && count_cur == 0)
3170 ea_refcount_fetch(ctx->refcount_extra, blk, &count_extra);
3177 return count_orig + count_extra - count_cur;
3180 static errcode_t e2fsck_pass1_merge_ea_refcount(e2fsck_t global_ctx,
3181 e2fsck_t thread_ctx)
3185 errcode_t retval = 0;
3187 if (!thread_ctx->refcount)
3190 if (!global_ctx->refcount) {
3191 global_ctx->refcount = thread_ctx->refcount;
3192 thread_ctx->refcount = NULL;
3193 global_ctx->refcount_extra = thread_ctx->refcount;
3194 thread_ctx->refcount_extra = NULL;
3198 ea_refcount_intr_begin(thread_ctx->refcount);
3200 if ((blk = ea_refcount_intr_next(thread_ctx->refcount,
3204 * this EA has never seen before, so just store its
3205 * refcount and refcount_extra into global_ctx if needed.
3207 if (!global_ctx->block_ea_map ||
3208 !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
3212 retval = ea_refcount_store(global_ctx->refcount,
3217 if (count > 0 || !thread_ctx->refcount_extra)
3219 ea_refcount_fetch(thread_ctx->refcount_extra, blk,
3224 if (!global_ctx->refcount_extra) {
3225 retval = ea_refcount_create(0,
3226 &global_ctx->refcount_extra);
3230 retval = ea_refcount_store(global_ctx->refcount_extra,
3236 ea_value_t thread_usage;
3237 ea_value_t global_usage;
3240 thread_usage = ea_refcount_usage(thread_ctx,
3242 global_usage = ea_refcount_usage(global_ctx,
3244 if (thread_usage + global_usage <= orig) {
3245 new = orig - thread_usage - global_usage;
3246 retval = ea_refcount_store(global_ctx->refcount,
3252 /* update it is as zero */
3253 retval = ea_refcount_store(global_ctx->refcount,
3257 /* Ooops, this EA was referenced more than it stated */
3258 if (!global_ctx->refcount_extra) {
3259 retval = ea_refcount_create(0,
3260 &global_ctx->refcount_extra);
3264 new = global_usage + thread_usage - orig;
3265 retval = ea_refcount_store(global_ctx->refcount_extra,
3275 static errcode_t e2fsck_pass1_merge_casefolded_dirs(e2fsck_t global_ctx,
3276 e2fsck_t thread_ctx)
3278 errcode_t retval = 0;
3280 if (!thread_ctx->casefolded_dirs)
3283 if (!global_ctx->casefolded_dirs)
3284 retval = ext2fs_badblocks_copy(thread_ctx->casefolded_dirs,
3285 &global_ctx->casefolded_dirs);
3287 retval = ext2fs_badblocks_merge(thread_ctx->casefolded_dirs,
3288 global_ctx->casefolded_dirs);
3293 static errcode_t e2fsck_pass1_merge_context(e2fsck_t global_ctx,
3294 e2fsck_t thread_ctx)
3296 ext2_filsys global_fs = global_ctx->fs;
3300 global_ctx->fs_directory_count += thread_ctx->fs_directory_count;
3301 global_ctx->fs_regular_count += thread_ctx->fs_regular_count;
3302 global_ctx->fs_blockdev_count += thread_ctx->fs_blockdev_count;
3303 global_ctx->fs_chardev_count += thread_ctx->fs_chardev_count;
3304 global_ctx->fs_links_count += thread_ctx->fs_links_count;
3305 global_ctx->fs_symlinks_count += thread_ctx->fs_symlinks_count;
3306 global_ctx->fs_fast_symlinks_count += thread_ctx->fs_fast_symlinks_count;
3307 global_ctx->fs_fifo_count += thread_ctx->fs_fifo_count;
3308 global_ctx->fs_total_count += thread_ctx->fs_total_count;
3309 global_ctx->fs_badblocks_count += thread_ctx->fs_badblocks_count;
3310 global_ctx->fs_sockets_count += thread_ctx->fs_sockets_count;
3311 global_ctx->fs_ind_count += thread_ctx->fs_ind_count;
3312 global_ctx->fs_dind_count += thread_ctx->fs_dind_count;
3313 global_ctx->fs_tind_count += thread_ctx->fs_tind_count;
3314 global_ctx->fs_fragmented += thread_ctx->fs_fragmented;
3315 global_ctx->fs_fragmented_dir += thread_ctx->fs_fragmented_dir;
3316 global_ctx->large_files += thread_ctx->large_files;
3317 /* threads might enable E2F_OPT_YES */
3318 global_ctx->options |= thread_ctx->options;
3319 global_ctx->flags |= thread_ctx->flags;
3321 * The l+f inode may have been cleared, so zap it now and
3322 * later passes will recalculate it if necessary
3324 global_ctx->lost_and_found = 0;
3325 /* merge extent depth count */
3326 for (i = 0; i < MAX_EXTENT_DEPTH_COUNT; i++)
3327 global_ctx->extent_depth_count[i] +=
3328 thread_ctx->extent_depth_count[i];
3330 e2fsck_pass1_merge_dir_info(global_ctx, thread_ctx);
3331 e2fsck_pass1_merge_dx_dir(global_ctx, thread_ctx);
3333 retval = e2fsck_pass1_merge_fs(global_ctx->fs, thread_ctx->fs);
3335 com_err(global_ctx->program_name, 0, _("while merging fs\n"));
3338 retval = e2fsck_pass1_merge_icounts(global_ctx, thread_ctx);
3340 com_err(global_ctx->program_name, 0,
3341 _("while merging icounts\n"));
3345 retval = e2fsck_pass1_merge_dirs_to_hash(global_ctx, thread_ctx);
3347 com_err(global_ctx->program_name, 0,
3348 _("while merging dirs to hash\n"));
3352 e2fsck_pass1_merge_ea_inode_refs(global_ctx, thread_ctx);
3353 e2fsck_pass1_merge_ea_refcount(global_ctx, thread_ctx);
3354 retval = quota_merge_and_update_usage(global_ctx->qctx,
3359 retval = e2fsck_pass1_merge_casefolded_dirs(global_ctx, thread_ctx);
3361 com_err(global_ctx->program_name, 0,
3362 _("while merging casefolded dirs\n"));
3366 e2fsck_pass1_merge_invalid_bitmaps(global_ctx, thread_ctx);
3368 if (thread_ctx->min_extra_isize < global_ctx->min_extra_isize)
3369 global_ctx->min_extra_isize = thread_ctx->min_extra_isize;
3371 retval = e2fsck_pass1_merge_bitmap(global_fs,
3372 &thread_ctx->inode_used_map,
3373 &global_ctx->inode_used_map);
3377 retval = e2fsck_pass1_merge_bitmap(global_fs,
3378 &thread_ctx->inode_dir_map,
3379 &global_ctx->inode_dir_map);
3382 retval = e2fsck_pass1_merge_bitmap(global_fs,
3383 &thread_ctx->inode_bb_map,
3384 &global_ctx->inode_bb_map);
3387 retval = e2fsck_pass1_merge_bitmap(global_fs,
3388 &thread_ctx->inode_imagic_map,
3389 &global_ctx->inode_imagic_map);
3392 retval = e2fsck_pass1_merge_bitmap(global_fs,
3393 &thread_ctx->inode_reg_map,
3394 &global_ctx->inode_reg_map);
3397 retval = e2fsck_pass1_merge_bitmap(global_fs,
3398 &thread_ctx->inodes_to_rebuild,
3399 &global_ctx->inodes_to_rebuild);
3402 retval = e2fsck_pass1_merge_bitmap(global_fs,
3403 &thread_ctx->block_ea_map,
3404 &global_ctx->block_ea_map);
3408 retval = e2fsck_pass1_merge_bitmap(global_fs,
3409 &thread_ctx->expand_eisize_map,
3410 &global_ctx->expand_eisize_map);
3414 if (ext2fs_has_feature_shared_blocks(global_fs->super) &&
3415 !(global_ctx->options & E2F_OPT_UNSHARE_BLOCKS))
3418 * This need be done after merging block_ea_map
3419 * because ea block might be shared, we need exclude
3420 * them from dup blocks.
3422 e2fsck_pass1_block_map_w_lock(thread_ctx);
3423 retval = ext2fs_merge_bitmap(thread_ctx->block_found_map,
3424 global_ctx->block_found_map,
3425 global_ctx->block_dup_map,
3426 global_ctx->block_ea_map);
3427 e2fsck_pass1_block_map_w_unlock(thread_ctx);
3428 if (retval == EEXIST)
3429 global_ctx->flags |= E2F_FLAG_DUP_BLOCK;
3434 static int e2fsck_pass1_thread_join(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3438 retval = e2fsck_pass1_merge_context(global_ctx, thread_ctx);
3439 ext2fs_free_mem(&thread_ctx->fs);
3440 if (thread_ctx->logf)
3441 fclose(thread_ctx->logf);
3442 if (thread_ctx->problem_logf) {
3443 fputs("</problem_log>\n", thread_ctx->problem_logf);
3444 fclose(thread_ctx->problem_logf);
3447 quota_release_context(&thread_ctx->qctx);
3449 * @block_metadata_map and @block_dup_map are
3450 * shared, so we don't free them.
3452 thread_ctx->block_metadata_map = NULL;
3453 thread_ctx->block_dup_map = NULL;
3454 e2fsck_reset_context(thread_ctx);
3455 ext2fs_free_mem(&thread_ctx);
3460 static int e2fsck_pass1_threads_join(e2fsck_t global_ctx)
3464 struct e2fsck_thread_info *infos = global_ctx->infos;
3465 struct e2fsck_thread_info *pinfo;
3466 int num_threads = global_ctx->pfs_num_threads;
3469 /* merge invalid bitmaps will recalculate it */
3470 global_ctx->invalid_bitmaps = 0;
3471 for (i = 0; i < num_threads; i++) {
3474 if (!pinfo->eti_started)
3477 rc = pthread_join(pinfo->eti_thread_id, NULL);
3479 com_err(global_ctx->program_name, rc,
3480 _("while joining thread\n"));
3484 rc = e2fsck_pass1_thread_join(global_ctx, infos[i].eti_thread_ctx);
3486 com_err(global_ctx->program_name, rc,
3487 _("while joining pass1 thread\n"));
3493 global_ctx->infos = NULL;
3498 static void *e2fsck_pass1_thread(void *arg)
3500 struct e2fsck_thread_info *info = arg;
3501 e2fsck_t thread_ctx = info->eti_thread_ctx;
3502 #ifdef DEBUG_THREADS
3503 struct e2fsck_thread_debug *thread_debug = info->eti_debug;
3506 #ifdef DEBUG_THREADS
3507 pthread_mutex_lock(&thread_debug->etd_mutex);
3508 while (info->eti_thread_index > thread_debug->etd_finished_threads) {
3509 pthread_cond_wait(&thread_debug->etd_cond,
3510 &thread_debug->etd_mutex);
3512 pthread_mutex_unlock(&thread_debug->etd_mutex);
3515 #ifdef HAVE_SETJMP_H
3517 * When fatal_error() happens, jump to here. The thread
3518 * context's flags will be saved, but its abort_loc will
3519 * be overwritten by original jump buffer for the later
3522 if (setjmp(thread_ctx->abort_loc)) {
3523 thread_ctx->flags &= ~E2F_FLAG_SETJMP_OK;
3526 thread_ctx->flags |= E2F_FLAG_SETJMP_OK;
3529 e2fsck_pass1_run(thread_ctx);
3532 if (thread_ctx->options & E2F_OPT_MULTITHREAD)
3534 _("Scanned group range [%u, %u), inodes %u\n"),
3535 thread_ctx->thread_info.et_group_start,
3536 thread_ctx->thread_info.et_group_end,
3537 thread_ctx->thread_info.et_inode_number);
3539 #ifdef DEBUG_THREADS
3540 pthread_mutex_lock(&thread_debug->etd_mutex);
3541 thread_debug->etd_finished_threads++;
3542 pthread_cond_broadcast(&thread_debug->etd_cond);
3543 pthread_mutex_unlock(&thread_debug->etd_mutex);
3549 static dgrp_t ext2fs_get_avg_group(ext2_filsys fs)
3552 dgrp_t average_group;
3553 unsigned flexbg_size;
3555 if (fs->fs_num_threads <= 1)
3556 return fs->group_desc_count;
3558 average_group = fs->group_desc_count / fs->fs_num_threads;
3559 if (average_group <= 1)
3562 if (ext2fs_has_feature_flex_bg(fs->super)) {
3565 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3566 if (average_group % flexbg_size) {
3567 times = average_group / flexbg_size;
3568 average_group = times * flexbg_size;
3572 return average_group;
3574 return fs->group_desc_count;
3578 static int e2fsck_pass1_threads_start(e2fsck_t global_ctx)
3580 struct e2fsck_thread_info *infos;
3581 pthread_attr_t attr;
3584 struct e2fsck_thread_info *tmp_pinfo;
3586 e2fsck_t thread_ctx;
3587 dgrp_t average_group;
3588 int num_threads = global_ctx->pfs_num_threads;
3589 #ifdef DEBUG_THREADS
3590 struct e2fsck_thread_debug thread_debug =
3591 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0};
3593 thread_debug.etd_finished_threads = 0;
3596 retval = pthread_attr_init(&attr);
3598 com_err(global_ctx->program_name, retval,
3599 _("while setting pthread attribute\n"));
3603 infos = calloc(num_threads, sizeof(struct e2fsck_thread_info));
3604 if (infos == NULL) {
3606 com_err(global_ctx->program_name, retval,
3607 _("while allocating memory for threads\n"));
3608 pthread_attr_destroy(&attr);
3611 global_ctx->infos = infos;
3613 average_group = ext2fs_get_avg_group(global_ctx->fs);
3614 for (i = 0; i < num_threads; i++) {
3615 tmp_pinfo = &infos[i];
3616 tmp_pinfo->eti_thread_index = i;
3617 #ifdef DEBUG_THREADS
3618 tmp_pinfo->eti_debug = &thread_debug;
3620 retval = e2fsck_pass1_thread_prepare(global_ctx, &thread_ctx,
3624 com_err(global_ctx->program_name, retval,
3625 _("while preparing pass1 thread\n"));
3628 tmp_pinfo->eti_thread_ctx = thread_ctx;
3630 retval = pthread_create(&tmp_pinfo->eti_thread_id, &attr,
3631 &e2fsck_pass1_thread, tmp_pinfo);
3633 com_err(global_ctx->program_name, retval,
3634 _("while creating thread\n"));
3635 e2fsck_pass1_thread_join(global_ctx, thread_ctx);
3639 tmp_pinfo->eti_started = 1;
3642 /* destroy the thread attribute object, since it is no longer needed */
3643 ret = pthread_attr_destroy(&attr);
3645 com_err(global_ctx->program_name, ret,
3646 _("while destroying thread attribute\n"));
3652 e2fsck_pass1_threads_join(global_ctx);
3658 static void e2fsck_pass1_multithread(e2fsck_t global_ctx)
3662 retval = e2fsck_pass1_threads_start(global_ctx);
3664 com_err(global_ctx->program_name, retval,
3665 _("while starting pass1 threads\n"));
3669 retval = e2fsck_pass1_threads_join(global_ctx);
3671 com_err(global_ctx->program_name, retval,
3672 _("while joining pass1 threads\n"));
3677 global_ctx->flags |= E2F_FLAG_ABORT;
3682 void e2fsck_pass1(e2fsck_t ctx)
3685 int need_single = 1;
3687 retval = e2fsck_pass1_prepare(ctx);
3691 if (ctx->pfs_num_threads > 1 || ctx->options & E2F_OPT_MULTITHREAD) {
3693 e2fsck_pass1_multithread(ctx);
3695 /* No lock is needed at this time */
3696 ctx->fs_need_locking = 0;
3699 e2fsck_pass1_run(ctx);
3700 e2fsck_pass1_post(ctx);
3703 #undef FINISH_INODE_LOOP
3706 * When the inode_scan routines call this callback at the end of the
3707 * glock group, call process_inodes.
3709 static errcode_t scan_callback(ext2_filsys fs,
3710 ext2_inode_scan scan EXT2FS_ATTR((unused)),
3711 dgrp_t group, void * priv_data)
3713 struct scan_callback_struct *scan_struct;
3715 dgrp_t cur = group + 1;
3716 struct e2fsck_thread *tinfo;
3717 struct e2fsck_thread_info *pinfo, *infos;
3720 scan_struct = (struct scan_callback_struct *) priv_data;
3721 ctx = scan_struct->ctx;
3723 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf,
3724 scan_struct->inodes_to_process,
3725 scan_struct->process_inode_count);
3728 if (ctx->global_ctx) {
3730 infos = ctx->global_ctx->infos;
3731 for (i = 0; i < ctx->global_ctx->pfs_num_threads; i++) {
3734 if (!pinfo->eti_started)
3737 tinfo = &pinfo->eti_thread_ctx->thread_info;
3738 if (ctx == pinfo->eti_thread_ctx)
3739 cur += group + 1 - tinfo->et_group_start;
3741 cur += tinfo->et_group_next -
3742 tinfo->et_group_start;
3748 if ((ctx->progress)(ctx, 1, cur,
3749 ctx->fs->group_desc_count))
3750 return EXT2_ET_CANCEL_REQUESTED;
3753 if (ctx->global_ctx) {
3754 tinfo = &ctx->thread_info;
3755 tinfo->et_group_next++;
3756 if (ctx->options & E2F_OPT_DEBUG &&
3757 ctx->options & E2F_OPT_MULTITHREAD)
3758 log_out(ctx, _("group %d finished\n"),
3759 tinfo->et_group_next);
3760 if (tinfo->et_group_next >= tinfo->et_group_end)
3761 return EXT2_ET_SCAN_FINISHED;
3769 * Process the inodes in the "inodes to process" list.
3771 static void process_inodes(e2fsck_t ctx, char *block_buf,
3772 struct process_inode_block *inodes_to_process,
3773 int *process_inode_count)
3776 struct ext2_inode *old_stashed_inode;
3777 ext2_ino_t old_stashed_ino;
3778 const char *old_operation;
3780 struct problem_context pctx;
3783 printf("begin process_inodes: ");
3785 if (*process_inode_count == 0)
3787 old_operation = ehandler_operation(0);
3788 old_stashed_inode = ctx->stashed_inode;
3789 old_stashed_ino = ctx->stashed_ino;
3790 qsort(inodes_to_process, *process_inode_count,
3791 sizeof(struct process_inode_block), process_inode_cmp);
3792 clear_problem_context(&pctx);
3793 for (i=0; i < *process_inode_count; i++) {
3794 pctx.inode = ctx->stashed_inode =
3795 (struct ext2_inode *) &inodes_to_process[i].inode;
3796 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
3799 printf("%u ", pctx.ino);
3801 sprintf(buf, _("reading indirect blocks of inode %u"),
3803 ehandler_operation(buf);
3804 check_blocks(ctx, &pctx, block_buf,
3805 &inodes_to_process[i].ea_ibody_quota);
3806 if (e2fsck_should_abort(ctx))
3809 ctx->stashed_inode = old_stashed_inode;
3810 ctx->stashed_ino = old_stashed_ino;
3811 *process_inode_count = 0;
3813 printf("end process inodes\n");
3815 ehandler_operation(old_operation);
3818 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
3820 const struct process_inode_block *ib_a =
3821 (const struct process_inode_block *) a;
3822 const struct process_inode_block *ib_b =
3823 (const struct process_inode_block *) b;
3826 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
3827 ib_b->inode.i_block[EXT2_IND_BLOCK]);
3830 * We only call process_inodes() for non-extent
3831 * inodes, so it's OK to pass NULL to
3832 * ext2fs_file_acl_block() here.
3834 ret = ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a->inode)) -
3835 ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b->inode));
3837 ret = ib_a->ino - ib_b->ino;
3842 * Mark an inode as being bad and increment its badness counter.
3844 void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, struct problem_context *pctx,
3845 __u32 code, int badness, const char *func,
3848 __u16 badness_before, badness_after;
3850 if (!ctx->inode_badness_threshold) /* badness is disabled */
3853 if (!ctx->inode_badness) {
3856 retval = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
3857 &ctx->inode_badness);
3859 pctx->errcode = retval;
3860 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, pctx);
3861 ctx->flags |= E2F_FLAG_ABORT;
3865 ext2fs_icount_fetch(ctx->inode_badness, pctx->ino, &badness_before);
3866 if (badness + badness_before > BADNESS_MAX)
3867 badness_after = BADNESS_MAX;
3868 else if (badness < 0 && badness_before < -badness)
3871 badness_after = badness_before + badness;
3872 ext2fs_icount_store(ctx->inode_badness, pctx->ino, badness_after);
3874 if (ctx->options & E2F_OPT_DEBUG)
3876 "%s:%d: increase inode %lu badness %u to %u for %x\n",
3877 func, line, (unsigned long)pctx->ino, badness_before,
3878 badness_after, code);
3881 static void add_casefolded_dir(e2fsck_t ctx, ino_t ino)
3883 struct problem_context pctx;
3885 if (!ctx->casefolded_dirs) {
3886 pctx.errcode = ext2fs_u32_list_create(&ctx->casefolded_dirs, 0);
3890 pctx.errcode = ext2fs_u32_list_add(ctx->casefolded_dirs, ino);
3891 if (pctx.errcode == 0)
3894 fix_problem(ctx, PR_1_ALLOCATE_CASEFOLDED_DIRLIST, &pctx);
3895 /* Should never get here */
3896 ctx->flags |= E2F_FLAG_ABORT;
3900 * This procedure will allocate the inode "bb" (badblock) map table
3902 static void alloc_bb_map(e2fsck_t ctx)
3904 struct problem_context pctx;
3906 clear_problem_context(&pctx);
3907 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3908 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
3909 "inode_bb_map", &ctx->inode_bb_map);
3912 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3913 /* Should never get here */
3914 ctx->flags |= E2F_FLAG_ABORT;
3920 * This procedure will allocate the inode imagic table
3922 static void alloc_imagic_map(e2fsck_t ctx)
3924 struct problem_context pctx;
3926 clear_problem_context(&pctx);
3927 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3928 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
3929 "inode_imagic_map", &ctx->inode_imagic_map);
3932 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3933 /* Should never get here */
3934 ctx->flags |= E2F_FLAG_ABORT;
3940 * Marks a block as in use, setting the dup_map if it's been set
3941 * already. Called by process_block and process_bad_block.
3943 * WARNING: Assumes checks have already been done to make sure block
3944 * is valid. This is true in both process_block and process_bad_block.
3946 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
3948 struct problem_context pctx;
3949 e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
3951 clear_problem_context(&pctx);
3953 if (is_blocks_used(ctx, block, 1)) {
3954 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
3955 !(ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
3958 ctx->flags |= E2F_FLAG_DUP_BLOCK;
3959 e2fsck_pass1_block_map_w_lock(ctx);
3960 ext2fs_fast_mark_block_bitmap2(global_ctx->block_dup_map, block);
3961 e2fsck_pass1_block_map_w_unlock(ctx);
3963 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
3968 * When cluster size is greater than one block, it is caller's responsibility
3969 * to make sure block parameter starts at a cluster boundary.
3971 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
3974 if (!is_blocks_used(ctx, block, num)) {
3975 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
3979 for (i = 0; i < num; i += EXT2FS_CLUSTER_RATIO(ctx->fs))
3980 mark_block_used(ctx, block + i);
3984 static errcode_t _INLINE_ e2fsck_write_ext_attr3(e2fsck_t ctx, blk64_t block,
3985 void *inbuf, ext2_ino_t inum)
3988 ext2_filsys fs = ctx->fs;
3990 e2fsck_pass1_fix_lock(ctx);
3991 retval = ext2fs_write_ext_attr3(fs, block, inbuf, inum);
3992 e2fsck_pass1_fix_unlock(ctx);
3997 * Adjust the extended attribute block's reference counts at the end
3998 * of pass 1, either by subtracting out references for EA blocks that
3999 * are still referenced in ctx->refcount, or by adding references for
4000 * EA blocks that had extra references as accounted for in
4001 * ctx->refcount_extra.
4003 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
4004 char *block_buf, int adjust_sign)
4006 struct ext2_ext_attr_header *header;
4007 struct problem_context pctx;
4008 ext2_filsys fs = ctx->fs;
4013 clear_problem_context(&pctx);
4015 ea_refcount_intr_begin(refcount);
4017 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
4020 pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
4022 /* We already checked this block, shouldn't happen */
4024 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
4027 header = BHDR(block_buf);
4028 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
4029 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
4033 pctx.blkcount = header->h_refcount;
4034 should_be = header->h_refcount + adjust_sign * (int)count;
4035 pctx.num = should_be;
4036 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
4037 header->h_refcount = should_be;
4038 pctx.errcode = e2fsck_write_ext_attr3(ctx, blk,
4042 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
4051 * Handle processing the extended attribute blocks
4053 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
4054 char *block_buf, struct ea_quota *ea_block_quota)
4056 ext2_filsys fs = ctx->fs;
4057 ext2_ino_t ino = pctx->ino;
4058 struct ext2_inode *inode = pctx->inode;
4061 struct ext2_ext_attr_header *header;
4062 struct ext2_ext_attr_entry *first, *entry;
4063 blk64_t quota_blocks = EXT2FS_C2B(fs, 1);
4064 __u64 quota_inodes = 0;
4065 region_t region = 0;
4066 int failed_csum = 0;
4068 ea_block_quota->blocks = 0;
4069 ea_block_quota->inodes = 0;
4071 blk = ext2fs_file_acl_block(fs, inode);
4076 * If the Extended attribute flag isn't set, then a non-zero
4077 * file acl means that the inode is corrupted.
4079 * Or if the extended attribute block is an invalid block,
4080 * then the inode is also corrupted.
4082 if (!ext2fs_has_feature_xattr(fs->super) ||
4083 (blk < fs->super->s_first_data_block) ||
4084 (blk >= ext2fs_blocks_count(fs->super))) {
4085 /* Fixed in pass2, e2fsck_process_bad_inode(). */
4086 e2fsck_mark_inode_bad(ctx, pctx, PR_2_FILE_ACL_ZERO);
4090 /* If ea bitmap hasn't been allocated, create it */
4091 if (!ctx->block_ea_map) {
4092 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
4093 _("ext attr block map"),
4094 EXT2FS_BMAP64_RBTREE, "block_ea_map",
4095 &ctx->block_ea_map);
4096 if (pctx->errcode) {
4098 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
4099 ctx->flags |= E2F_FLAG_ABORT;
4104 /* Create the EA refcount structure if necessary */
4105 if (!ctx->refcount) {
4106 pctx->errcode = ea_refcount_create(0,
4107 &ctx->refcount_orig);
4108 if (pctx->errcode) {
4110 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4111 ctx->flags |= E2F_FLAG_ABORT;
4115 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
4116 if (pctx->errcode) {
4118 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4119 ctx->flags |= E2F_FLAG_ABORT;
4125 /* Debugging text */
4126 printf("Inode %u has EA block %u\n", ino, blk);
4129 /* Have we seen this EA block before? */
4130 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
4131 ea_block_quota->blocks = EXT2FS_C2B(fs, 1);
4132 ea_block_quota->inodes = 0;
4134 if (ctx->ea_block_quota_blocks) {
4135 ea_refcount_fetch(ctx->ea_block_quota_blocks, blk,
4138 ea_block_quota->blocks = quota_blocks;
4141 if (ctx->ea_block_quota_inodes)
4142 ea_refcount_fetch(ctx->ea_block_quota_inodes, blk,
4143 &ea_block_quota->inodes);
4145 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
4147 /* Ooops, this EA was referenced more than it stated */
4148 if (!ctx->refcount_extra) {
4149 pctx->errcode = ea_refcount_create(0,
4150 &ctx->refcount_extra);
4151 if (pctx->errcode) {
4153 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4154 ctx->flags |= E2F_FLAG_ABORT;
4158 ea_refcount_increment(ctx->refcount_extra, blk, 0);
4163 * OK, we haven't seen this EA block yet. So we need to
4167 pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
4168 if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
4171 } else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
4174 if (pctx->errcode &&
4175 fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
4179 header = BHDR(block_buf);
4180 pctx->blk = ext2fs_file_acl_block(fs, inode);
4181 if (((ctx->ext_attr_ver == 1) &&
4182 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
4183 ((ctx->ext_attr_ver == 2) &&
4184 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
4185 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
4189 if (header->h_blocks != 1) {
4190 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
4194 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
4197 region = region_create(0, fs->blocksize);
4199 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
4200 ctx->flags |= E2F_FLAG_ABORT;
4203 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
4204 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4208 first = (struct ext2_ext_attr_entry *)(header+1);
4209 end = block_buf + fs->blocksize;
4211 while ((char *)entry < end && *(__u32 *)entry) {
4214 if (region_allocate(region, (char *)entry - (char *)header,
4215 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
4216 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4220 if ((ctx->ext_attr_ver == 1 &&
4221 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
4222 (ctx->ext_attr_ver == 2 &&
4223 entry->e_name_index == 0)) {
4224 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
4228 if (entry->e_value_inum == 0) {
4229 if (entry->e_value_size > EXT2_XATTR_SIZE_MAX ||
4230 (entry->e_value_offs + entry->e_value_size >
4232 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
4236 if (entry->e_value_size &&
4237 region_allocate(region, entry->e_value_offs,
4238 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
4239 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
4244 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
4245 entry->e_value_offs);
4247 if (entry->e_hash != hash) {
4248 pctx->num = entry->e_hash;
4249 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
4251 entry->e_hash = hash;
4255 blk64_t entry_quota_blocks;
4257 problem = check_large_ea_inode(ctx, entry, pctx,
4258 &entry_quota_blocks);
4259 if (problem && fix_problem(ctx, problem, pctx))
4262 quota_blocks += entry_quota_blocks;
4266 entry = EXT2_EXT_ATTR_NEXT(entry);
4268 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
4269 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4272 region_free(region);
4275 * We only get here if there was no other errors that were fixed.
4276 * If there was a checksum fail, ask to correct it.
4279 fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
4280 pctx->errcode = e2fsck_write_ext_attr3(ctx, blk, block_buf,
4286 if (quota_blocks != EXT2FS_C2B(fs, 1U)) {
4287 if (!ctx->ea_block_quota_blocks) {
4288 pctx->errcode = ea_refcount_create(0,
4289 &ctx->ea_block_quota_blocks);
4290 if (pctx->errcode) {
4295 ea_refcount_store(ctx->ea_block_quota_blocks, blk,
4300 if (!ctx->ea_block_quota_inodes) {
4301 pctx->errcode = ea_refcount_create(0,
4302 &ctx->ea_block_quota_inodes);
4303 if (pctx->errcode) {
4306 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4307 ctx->flags |= E2F_FLAG_ABORT;
4312 ea_refcount_store(ctx->ea_block_quota_inodes, blk,
4315 ea_block_quota->blocks = quota_blocks;
4316 ea_block_quota->inodes = quota_inodes;
4318 inc_ea_inode_refs(ctx, pctx, first, end);
4319 ea_refcount_store(ctx->refcount, blk, header->h_refcount - 1);
4320 ea_refcount_store(ctx->refcount_orig, blk, header->h_refcount);
4322 * It might be racy that this block has been merged in the
4325 if (!is_blocks_used(ctx, blk, 1))
4326 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, blk);
4327 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
4332 region_free(region);
4333 ext2fs_file_acl_block_set(fs, inode, 0);
4334 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
4338 /* Returns 1 if bad htree, 0 if OK */
4339 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
4340 ext2_ino_t ino, struct ext2_inode *inode,
4343 struct ext2_dx_root_info *root;
4344 ext2_filsys fs = ctx->fs;
4348 if ((!LINUX_S_ISDIR(inode->i_mode) &&
4349 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
4350 (!ext2fs_has_feature_dir_index(fs->super) &&
4351 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
4354 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
4356 if ((pctx->errcode) ||
4358 (blk < fs->super->s_first_data_block) ||
4359 (blk >= ext2fs_blocks_count(fs->super))) {
4360 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4366 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
4368 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4372 /* XXX should check that beginning matches a directory */
4373 root = (struct ext2_dx_root_info *) (block_buf + 24);
4375 if ((root->reserved_zero || root->info_length < 8) &&
4376 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4379 pctx->num = root->hash_version;
4380 if ((root->hash_version != EXT2_HASH_LEGACY) &&
4381 (root->hash_version != EXT2_HASH_HALF_MD4) &&
4382 (root->hash_version != EXT2_HASH_TEA) &&
4383 (root->hash_version != EXT2_HASH_SIPHASH) &&
4384 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
4387 if (ext4_hash_in_dirent(inode)) {
4388 if (root->hash_version != EXT2_HASH_SIPHASH &&
4389 fix_problem(ctx, PR_1_HTREE_NEEDS_SIPHASH, pctx))
4392 if (root->hash_version == EXT2_HASH_SIPHASH &&
4393 fix_problem(ctx, PR_1_HTREE_CANNOT_SIPHASH, pctx))
4397 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
4398 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
4401 pctx->num = root->indirect_levels;
4402 /* if htree level is clearly too high, consider it to be broken */
4403 if (root->indirect_levels > EXT4_HTREE_LEVEL &&
4404 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4407 /* if level is only maybe too high, LARGE_DIR feature could be unset */
4408 if (root->indirect_levels > ext2_dir_htree_level(fs) &&
4409 !ext2fs_has_feature_largedir(fs->super)) {
4410 int blockbits = EXT2_BLOCK_SIZE_BITS(fs->super) + 10;
4411 unsigned idx_pb = 1 << (blockbits - 3);
4413 /* compare inode size/blocks vs. max-sized 2-level htree */
4414 if (EXT2_I_SIZE(pctx->inode) <
4415 (idx_pb - 1) * (idx_pb - 2) << blockbits &&
4416 pctx->inode->i_blocks <
4417 (idx_pb - 1) * (idx_pb - 2) << (blockbits - 9) &&
4418 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4422 if (root->indirect_levels > EXT4_HTREE_LEVEL_COMPAT ||
4423 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
4429 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
4430 struct ext2_inode *inode, int restart_flag,
4434 inode->i_links_count = 0;
4435 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
4436 inode->i_dtime = ctx->now;
4439 * If a special inode has such rotten block mappings that we
4440 * want to clear the whole inode, be sure to actually zap
4441 * the block maps because i_links_count isn't checked for
4442 * special inodes, and we'll end up right back here the next
4445 if (ino < EXT2_FIRST_INODE(ctx->fs->super))
4446 memset(inode->i_block, 0, sizeof(inode->i_block));
4448 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
4449 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
4450 if (ctx->inode_reg_map)
4451 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
4452 if (ctx->inode_badness)
4453 ext2fs_icount_store(ctx->inode_badness, ino, 0);
4456 * If the inode was partially accounted for before processing
4457 * was aborted, we need to restart the pass 1 scan.
4459 ctx->flags |= restart_flag;
4461 if (ino == EXT2_BAD_INO)
4462 memset(inode, 0, sizeof(struct ext2_inode));
4464 e2fsck_write_inode(ctx, ino, inode, source);
4468 * Use the multiple-blocks reclamation code to fix alignment problems in
4469 * a bigalloc filesystem. We want a logical cluster to map to *only* one
4470 * physical cluster, and we want the block offsets within that cluster to
4473 static int has_unaligned_cluster_map(e2fsck_t ctx,
4474 blk64_t last_pblk, blk64_t last_lblk,
4475 blk64_t pblk, blk64_t lblk)
4477 blk64_t cluster_mask;
4479 if (!ctx->fs->cluster_ratio_bits)
4481 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
4484 * If the block in the logical cluster doesn't align with the block in
4485 * the physical cluster...
4487 if ((lblk & cluster_mask) != (pblk & cluster_mask))
4491 * If we cross a physical cluster boundary within a logical cluster...
4493 if (last_pblk && (lblk & cluster_mask) != 0 &&
4494 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
4495 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
4501 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
4502 struct process_block_struct *pb,
4503 blk64_t start_block, blk64_t end_block,
4505 ext2_extent_handle_t ehandle,
4508 struct ext2fs_extent extent;
4509 blk64_t blk, last_lblk;
4511 int is_dir, is_leaf;
4513 struct ext2_extent_info info;
4514 int failed_csum = 0;
4516 if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
4519 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
4522 if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
4523 !pb->eti.force_rebuild &&
4524 info.curr_level < MAX_EXTENT_DEPTH_COUNT) {
4525 struct extent_tree_level *etl;
4527 etl = pb->eti.ext_info + info.curr_level;
4528 etl->num_extents += info.num_entries;
4529 etl->max_extents += info.max_entries;
4531 * Implementation wart: Splitting extent blocks when appending
4532 * will leave the old block with one free entry. Therefore
4533 * unless the node is totally full, pretend that a non-root
4534 * extent block can hold one fewer entry than it actually does,
4535 * so that we don't repeatedly rebuild the extent tree.
4537 if (info.curr_level && info.num_entries < info.max_entries)
4541 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
4543 while ((pctx->errcode == 0 ||
4544 pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
4545 info.num_entries-- > 0) {
4546 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
4547 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
4548 last_lblk = extent.e_lblk + extent.e_len - 1;
4551 pctx->blk = extent.e_pblk;
4552 pctx->blk2 = extent.e_lblk;
4553 pctx->num = extent.e_len;
4554 pctx->blkcount = extent.e_lblk + extent.e_len;
4556 if (extent.e_pblk == 0 ||
4557 extent.e_pblk < ctx->fs->super->s_first_data_block ||
4558 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
4559 problem = PR_1_EXTENT_BAD_START_BLK;
4560 else if (extent.e_lblk < start_block)
4561 problem = PR_1_OUT_OF_ORDER_EXTENTS;
4562 else if ((end_block && last_lblk > end_block) &&
4563 !(last_lblk > eof_block &&
4564 ((extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) ||
4565 (pctx->inode->i_flags & EXT4_VERITY_FL))))
4566 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
4567 else if (is_leaf && extent.e_len == 0)
4568 problem = PR_1_EXTENT_LENGTH_ZERO;
4570 (extent.e_pblk + extent.e_len) >
4571 ext2fs_blocks_count(ctx->fs->super))
4572 problem = PR_1_EXTENT_ENDS_BEYOND;
4573 else if (is_leaf && is_dir && !pctx->inode->i_size_high &&
4574 !ext2fs_has_feature_largedir(ctx->fs->super) &&
4575 ((extent.e_lblk + extent.e_len) >
4576 (1U << (21 - ctx->fs->super->s_log_block_size))))
4577 problem = PR_1_TOOBIG_DIR;
4579 if (is_leaf && problem == 0 && extent.e_len > 0) {
4581 printf("extent_region(ino=%u, expect=%llu, "
4582 "lblk=%llu, len=%u)\n", pb->ino,
4583 (unsigned long long) pb->next_lblock,
4584 (unsigned long long) extent.e_lblk,
4587 if (extent.e_lblk < pb->next_lblock)
4588 problem = PR_1_EXTENT_COLLISION;
4589 else if (extent.e_lblk + extent.e_len > pb->next_lblock)
4590 pb->next_lblock = extent.e_lblk + extent.e_len;
4594 * Uninitialized blocks in a directory? Clear the flag and
4595 * we'll interpret the blocks later.
4597 if (try_repairs && is_dir && problem == 0 &&
4598 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4599 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
4600 e2fsck_pass1_fix_lock(ctx);
4601 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4602 pb->inode_modified = 1;
4603 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4605 e2fsck_pass1_fix_unlock(ctx);
4610 #ifdef CONFIG_DEVELOPER_FEATURES
4611 if (try_repairs && !is_dir && problem == 0 &&
4612 (ctx->options & E2F_OPT_CLEAR_UNINIT) &&
4613 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4614 fix_problem(ctx, PR_1_CLEAR_UNINIT_EXTENT, pctx)) {
4615 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4616 pb->inode_modified = 1;
4617 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4624 if (try_repairs && problem) {
4626 /* Record badness only if extent is within inode */
4627 if (fix_problem_bad(ctx, problem, pctx,
4628 info.curr_level == 0)) {
4629 if (ctx->invalid_bitmaps) {
4631 * If fsck knows the bitmaps are bad,
4632 * skip to the next extent and
4633 * try to clear this extent again
4634 * after fixing the bitmaps, by
4637 pctx->errcode = ext2fs_extent_get(
4639 EXT2_EXTENT_NEXT_SIB,
4641 ctx->flags |= E2F_FLAG_RESTART_LATER;
4642 if (pctx->errcode ==
4643 EXT2_ET_NO_CURRENT_NODE) {
4649 e2fsck_pass1_fix_lock(ctx);
4650 e2fsck_read_bitmaps(ctx);
4651 pb->inode_modified = 1;
4653 ext2fs_extent_delete(ehandle, 0);
4654 e2fsck_pass1_fix_unlock(ctx);
4655 if (pctx->errcode) {
4656 pctx->str = "ext2fs_extent_delete";
4659 e2fsck_pass1_fix_lock(ctx);
4660 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4661 e2fsck_pass1_fix_unlock(ctx);
4662 if (pctx->errcode &&
4663 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
4664 pctx->str = "ext2fs_extent_fix_parents";
4667 pctx->errcode = ext2fs_extent_get(ehandle,
4668 EXT2_EXTENT_CURRENT,
4670 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
4681 blk64_t lblk = extent.e_lblk;
4682 int next_try_repairs = 1;
4684 blk = extent.e_pblk;
4687 * If this lower extent block collides with critical
4688 * metadata, don't try to repair the damage. Pass 1b
4689 * will reallocate the block; then we can try again.
4691 if (pb->ino != EXT2_RESIZE_INO &&
4692 extent.e_pblk < ctx->fs->super->s_blocks_count &&
4693 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
4695 next_try_repairs = 0;
4697 fix_problem_bad(ctx,
4698 PR_1_CRITICAL_METADATA_COLLISION,
4700 if ((ctx->options & E2F_OPT_NO) == 0)
4701 ctx->flags |= E2F_FLAG_RESTART_LATER;
4703 pctx->errcode = ext2fs_extent_get(ehandle,
4704 EXT2_EXTENT_DOWN, &extent);
4705 if (pctx->errcode &&
4706 pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
4707 pctx->str = "EXT2_EXTENT_DOWN";
4708 problem = PR_1_EXTENT_HEADER_INVALID;
4709 if (!next_try_repairs)
4711 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
4712 goto report_problem;
4715 /* The next extent should match this index's logical start */
4716 if (extent.e_lblk != lblk) {
4717 struct ext2_extent_info e_info;
4719 pctx->errcode = ext2fs_extent_get_info(ehandle,
4721 if (pctx->errcode) {
4722 pctx->str = "ext2fs_extent_get_info";
4726 pctx->blk2 = extent.e_lblk;
4727 pctx->num = e_info.curr_level - 1;
4728 problem = PR_1_EXTENT_INDEX_START_INVALID;
4729 if (fix_problem(ctx, problem, pctx)) {
4730 e2fsck_pass1_fix_lock(ctx);
4731 pb->inode_modified = 1;
4733 ext2fs_extent_fix_parents(ehandle);
4734 e2fsck_pass1_fix_unlock(ctx);
4735 if (pctx->errcode) {
4736 pctx->str = "ext2fs_extent_fix_parents";
4741 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
4742 last_lblk, eof_block, ehandle,
4746 pctx->errcode = ext2fs_extent_get(ehandle,
4747 EXT2_EXTENT_UP, &extent);
4748 if (pctx->errcode) {
4749 pctx->str = "EXT2_EXTENT_UP";
4752 mark_block_used(ctx, blk);
4757 if ((pb->previous_block != 0) &&
4758 (pb->previous_block+1 != extent.e_pblk)) {
4759 if (ctx->options & E2F_OPT_FRAGCHECK) {
4764 else if (pb->is_reg)
4767 printf(("%6lu(%c): expecting %6lu "
4769 "phys %6lu log %lu len %lu\n"),
4770 (unsigned long) pctx->ino, type,
4771 (unsigned long) pb->previous_block+1,
4772 (unsigned long) extent.e_pblk,
4773 (unsigned long) extent.e_lblk,
4774 (unsigned long) extent.e_len);
4779 * If we notice a gap in the logical block mappings of an
4780 * extent-mapped directory, offer to close the hole by
4781 * moving the logical block down, otherwise we'll go mad in
4782 * pass 3 allocating empty directory blocks to fill the hole.
4784 if (try_repairs && is_dir &&
4785 pb->last_block + 1 < extent.e_lblk) {
4788 new_lblk = pb->last_block + 1;
4789 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
4790 new_lblk = ((new_lblk +
4791 EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
4792 ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
4794 EXT2FS_CLUSTER_MASK(ctx->fs));
4795 pctx->blk = extent.e_lblk;
4796 pctx->blk2 = new_lblk;
4797 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
4798 e2fsck_pass1_fix_lock(ctx);
4799 extent.e_lblk = new_lblk;
4800 pb->inode_modified = 1;
4801 pctx->errcode = ext2fs_extent_replace(ehandle,
4803 e2fsck_pass1_fix_unlock(ctx);
4804 if (pctx->errcode) {
4808 e2fsck_pass1_fix_lock(ctx);
4809 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4810 e2fsck_pass1_fix_unlock(ctx);
4812 goto failed_add_dir_block;
4813 pctx->errcode = ext2fs_extent_goto(ehandle,
4816 goto failed_add_dir_block;
4817 last_lblk = extent.e_lblk + extent.e_len - 1;
4823 while (++pb->last_db_block <
4824 (e2_blkcnt_t) extent.e_lblk) {
4825 pctx->errcode = ext2fs_add_dir_block2(
4829 if (pctx->errcode) {
4831 pctx->num = pb->last_db_block;
4832 goto failed_add_dir_block;
4836 for (i = 0; i < extent.e_len; i++) {
4837 pctx->errcode = ext2fs_add_dir_block2(
4842 if (pctx->errcode) {
4843 pctx->blk = extent.e_pblk + i;
4844 pctx->num = extent.e_lblk + i;
4845 failed_add_dir_block:
4846 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
4847 /* Should never get here */
4848 ctx->flags |= E2F_FLAG_ABORT;
4852 if (extent.e_len > 0)
4853 pb->last_db_block = extent.e_lblk + extent.e_len - 1;
4855 if (has_unaligned_cluster_map(ctx, pb->previous_block,
4859 for (i = 0; i < extent.e_len; i++) {
4860 pctx->blk = extent.e_lblk + i;
4861 pctx->blk2 = extent.e_pblk + i;
4862 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
4863 mark_block_used(ctx, extent.e_pblk + i);
4864 mark_block_used(ctx, extent.e_pblk + i);
4869 * Check whether first cluster got marked in previous iteration.
4871 if (ctx->fs->cluster_ratio_bits &&
4872 pb->previous_block &&
4873 (EXT2FS_B2C(ctx->fs, extent.e_pblk) ==
4874 EXT2FS_B2C(ctx->fs, pb->previous_block)))
4875 /* Set blk to the beginning of next cluster. */
4878 EXT2FS_B2C(ctx->fs, extent.e_pblk) + 1);
4880 /* Set blk to the beginning of current cluster. */
4881 blk = EXT2FS_C2B(ctx->fs,
4882 EXT2FS_B2C(ctx->fs, extent.e_pblk));
4884 if (blk < extent.e_pblk + extent.e_len) {
4885 mark_blocks_used(ctx, blk,
4886 extent.e_pblk + extent.e_len - blk);
4887 n = DIV_ROUND_UP(extent.e_pblk + extent.e_len - blk,
4888 EXT2FS_CLUSTER_RATIO(ctx->fs));
4889 pb->num_blocks += n;
4891 pb->last_block = extent.e_lblk + extent.e_len - 1;
4892 pb->previous_block = extent.e_pblk + extent.e_len - 1;
4893 start_block = pb->last_block = last_lblk;
4894 if (is_leaf && !is_dir &&
4895 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
4896 pb->last_init_lblock = last_lblk;
4898 pctx->errcode = ext2fs_extent_get(ehandle,
4899 EXT2_EXTENT_NEXT_SIB,
4903 /* Failed csum but passes checks? Ask to fix checksum. */
4905 fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
4906 e2fsck_pass1_fix_lock(ctx);
4907 pb->inode_modified = 1;
4908 pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
4909 e2fsck_pass1_fix_unlock(ctx);
4914 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
4918 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
4919 struct process_block_struct *pb)
4921 struct ext2_extent_info info;
4922 struct ext2_inode *inode = pctx->inode;
4923 ext2_extent_handle_t ehandle;
4924 ext2_filsys fs = ctx->fs;
4925 ext2_ino_t ino = pctx->ino;
4928 struct ext3_extent_header *eh;
4930 /* Check for a proper extent header... */
4931 eh = (struct ext3_extent_header *) &inode->i_block[0];
4932 retval = ext2fs_extent_header_verify(eh, sizeof(inode->i_block));
4934 if (fix_problem(ctx, PR_1_MISSING_EXTENT_HEADER, pctx))
4935 e2fsck_clear_inode(ctx, ino, inode, 0,
4936 "check_blocks_extents");
4941 /* ...since this function doesn't fail if i_block is zeroed. */
4942 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
4943 if (pctx->errcode) {
4944 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
4945 e2fsck_clear_inode(ctx, ino, inode, 0,
4946 "check_blocks_extents");
4951 retval = ext2fs_extent_get_info(ehandle, &info);
4953 int max_depth = info.max_depth;
4955 if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
4956 max_depth = MAX_EXTENT_DEPTH_COUNT-1;
4957 ctx->extent_depth_count[max_depth]++;
4960 /* Check maximum extent depth */
4961 pctx->blk = info.max_depth;
4962 pctx->blk2 = ext2fs_max_extent_depth(ehandle);
4963 if (pctx->blk2 < pctx->blk &&
4964 fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
4965 pb->eti.force_rebuild = 1;
4967 /* Can we collect extent tree level stats? */
4968 pctx->blk = MAX_EXTENT_DEPTH_COUNT;
4969 if (pctx->blk2 > pctx->blk)
4970 fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
4971 memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
4972 pb->eti.ino = pb->ino;
4974 pb->next_lblock = 0;
4976 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
4977 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
4978 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
4979 if (pctx->errcode &&
4980 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
4982 inode->i_blocks = 0;
4983 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
4984 "check_blocks_extents");
4987 ext2fs_extent_free(ehandle);
4989 /* Rebuild unless it's a dir and we're rehashing it */
4990 if (LINUX_S_ISDIR(inode->i_mode) &&
4991 e2fsck_dir_will_be_rehashed(ctx, ino))
4994 if (ctx->options & E2F_OPT_CONVERT_BMAP)
4995 e2fsck_rebuild_extents_later(ctx, ino);
4997 e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
5001 * In fact we don't need to check blocks for an inode with inline data
5002 * because this inode doesn't have any blocks. In this function all
5003 * we need to do is add this inode into dblist when it is a directory.
5005 static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
5006 struct process_block_struct *pb)
5009 size_t inline_data_size = 0;
5016 /* Process the dirents in i_block[] as the "first" block. */
5017 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
5021 /* Process the dirents in the EA as a "second" block. */
5022 flags = ctx->fs->flags;
5023 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5024 pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
5026 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5027 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5028 if (pctx->errcode) {
5033 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
5036 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
5044 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
5045 ctx->flags |= E2F_FLAG_ABORT;
5049 * This subroutine is called on each inode to account for all of the
5050 * blocks used by that inode.
5052 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
5053 char *block_buf, const struct ea_quota *ea_ibody_quota)
5055 ext2_filsys fs = ctx->fs;
5056 struct process_block_struct pb;
5057 ext2_ino_t ino = pctx->ino;
5058 struct ext2_inode *inode = pctx->inode;
5059 unsigned bad_size = 0;
5060 int dirty_inode = 0;
5064 struct ea_quota ea_block_quota;
5067 pb.num_blocks = EXT2FS_B2C(ctx->fs,
5068 ea_ibody_quota ? ea_ibody_quota->blocks : 0);
5070 pb.last_init_lblock = -1;
5071 pb.last_db_block = -1;
5072 pb.num_illegal_blocks = 0;
5073 pb.suppress = 0; pb.clear = 0;
5076 pb.previous_block = 0;
5077 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
5078 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
5079 pb.max_blocks = 1U << (31 - fs->super->s_log_block_size);
5083 pb.inode_modified = 0;
5084 pb.eti.force_rebuild = 0;
5088 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
5089 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
5091 if (check_ext_attr(ctx, pctx, block_buf, &ea_block_quota)) {
5092 if (e2fsck_should_abort(ctx))
5094 pb.num_blocks += EXT2FS_B2C(ctx->fs, ea_block_quota.blocks);
5097 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
5098 check_blocks_inline_data(ctx, pctx, &pb);
5099 else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
5100 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
5101 check_blocks_extents(ctx, pctx, &pb);
5105 * If we've modified the inode, write it out before
5106 * iterate() tries to use it.
5109 e2fsck_write_inode(ctx, ino, inode,
5114 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5115 pctx->errcode = ext2fs_block_iterate3(fs, ino,
5116 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
5117 block_buf, process_block, &pb);
5119 * We do not have uninitialized extents in non extent
5122 pb.last_init_lblock = pb.last_block;
5124 * If iterate() changed a block mapping, we have to
5125 * re-read the inode. If we decide to clear the
5126 * inode after clearing some stuff, we'll re-write the
5127 * bad mappings into the inode!
5129 if (pb.inode_modified)
5130 e2fsck_read_inode(ctx, ino, inode,
5132 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5133 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5135 if (ctx->options & E2F_OPT_CONVERT_BMAP) {
5137 printf("bmap rebuild ino=%d\n", ino);
5139 if (!LINUX_S_ISDIR(inode->i_mode) ||
5140 !e2fsck_dir_will_be_rehashed(ctx, ino))
5141 e2fsck_rebuild_extents_later(ctx, ino);
5145 end_problem_latch(ctx, PR_LATCH_BLOCK);
5146 end_problem_latch(ctx, PR_LATCH_TOOBIG);
5147 if (e2fsck_should_abort(ctx))
5150 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
5152 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
5153 if (LINUX_S_ISDIR(inode->i_mode))
5154 ctx->fs_fragmented_dir++;
5156 ctx->fs_fragmented++;
5160 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
5165 if (inode->i_flags & EXT2_INDEX_FL) {
5166 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
5167 inode->i_flags &= ~EXT2_INDEX_FL;
5170 e2fsck_add_dx_dir(ctx, ino, inode, pb.last_block+1);
5174 if (!pb.num_blocks && pb.is_dir &&
5175 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
5177 * The mode might be in-correct. Increasing the badness by
5178 * small amount won't hurt much.
5180 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
5181 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
5182 ctx->fs_directory_count--;
5187 if (ino != quota_type2inum(PRJQUOTA, fs->super) &&
5188 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) &&
5189 !(inode->i_flags & EXT4_EA_INODE_FL)) {
5190 quota_data_add(ctx->qctx, (struct ext2_inode_large *) inode,
5192 pb.num_blocks * EXT2_CLUSTER_SIZE(fs->super));
5193 quota_data_inodes(ctx->qctx, (struct ext2_inode_large *) inode,
5194 ino, (ea_ibody_quota ?
5195 ea_ibody_quota->inodes : 0) +
5196 ea_block_quota.inodes + 1);
5199 if (!ext2fs_has_feature_huge_file(fs->super) ||
5200 !(inode->i_flags & EXT4_HUGE_FILE_FL))
5201 pb.num_blocks *= (fs->blocksize / 512);
5202 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
5204 printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
5205 ino, inode->i_size, (unsigned long long) pb.last_block,
5206 (unsigned long long) ext2fs_inode_i_blocks(fs, inode),
5207 (unsigned long long) pb.num_blocks);
5209 size = EXT2_I_SIZE(inode);
5211 unsigned nblock = size >> EXT2_BLOCK_SIZE_BITS(fs->super);
5212 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
5217 flags = ctx->fs->flags;
5218 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5219 err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
5221 ctx->fs->flags = (flags &
5222 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5224 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5225 if (err || sz != size) {
5229 } else if (size & (fs->blocksize - 1))
5231 else if (nblock > (pb.last_block + 1))
5233 else if (nblock < (pb.last_block + 1)) {
5234 if (((pb.last_block + 1) - nblock) >
5235 fs->super->s_prealloc_dir_blocks)
5239 if ((pb.last_init_lblock >= 0) &&
5240 /* Do not allow initialized allocated blocks past i_size*/
5241 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
5242 !(inode->i_flags & EXT4_VERITY_FL))
5244 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5245 size > ext2_max_sizes[fs->super->s_log_block_size])
5246 /* too big for a direct/indirect-mapped file */
5248 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5250 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
5251 /* too big for an extent-based file - 32bit ee_block */
5254 /* i_size for symlinks is checked elsewhere */
5255 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
5256 /* Did inline_data set pctx->num earlier? */
5258 pctx->num = (pb.last_block + 1) * fs->blocksize;
5259 pctx->group = bad_size;
5260 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
5261 ext2fs_inode_size_set(fs, inode, pctx->num);
5262 if (EXT2_I_SIZE(inode) == 0 &&
5263 (inode->i_flags & EXT4_INLINE_DATA_FL)) {
5264 memset(inode->i_block, 0,
5265 sizeof(inode->i_block));
5266 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
5272 if (LINUX_S_ISREG(inode->i_mode) &&
5273 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
5275 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
5276 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
5277 (ext2fs_has_feature_huge_file(fs->super) &&
5278 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
5279 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
5280 pctx->num = pb.num_blocks;
5281 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
5282 inode->i_blocks = pb.num_blocks;
5283 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
5290 * The kernel gets mad if we ask it to allocate bigalloc clusters to
5291 * a block mapped file, so rebuild it as an extent file. We can skip
5292 * symlinks because they're never rewritten.
5294 if (ext2fs_has_feature_bigalloc(fs->super) &&
5295 (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
5296 ext2fs_inode_data_blocks2(fs, inode) > 0 &&
5297 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
5298 !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
5299 fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
5300 pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
5305 if (ctx->dirs_to_hash && pb.is_dir &&
5306 !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
5307 !(inode->i_flags & EXT2_INDEX_FL) &&
5308 ((inode->i_size / fs->blocksize) >= 3))
5309 e2fsck_rehash_dir_later(ctx, ino);
5312 /* need restart if clearing bad inode after block processing */
5313 if (e2fsck_fix_bad_inode(ctx, pctx))
5314 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
5315 "check_blocks_bad");
5316 else if (dirty_inode)
5317 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
5322 * Helper function called by process block when an illegal block is
5323 * found. It returns a description about why the block is illegal
5325 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
5329 static char problem[80];
5331 super = fs->super->s_first_data_block;
5332 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
5333 if (block < super) {
5334 sprintf(problem, "< FIRSTBLOCK (%u)", super);
5336 } else if (block >= ext2fs_blocks_count(fs->super)) {
5337 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
5340 for (i = 0; i < fs->group_desc_count; i++) {
5341 if (block == super) {
5342 sprintf(problem, "is the superblock in group %d", i);
5345 if (block > super &&
5346 block <= (super + fs->desc_blocks)) {
5347 sprintf(problem, "is in the group descriptors "
5351 if (block == ext2fs_block_bitmap_loc(fs, i)) {
5352 sprintf(problem, "is the block bitmap of group %d", i);
5355 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
5356 sprintf(problem, "is the inode bitmap of group %d", i);
5359 if (block >= ext2fs_inode_table_loc(fs, i) &&
5360 (block < ext2fs_inode_table_loc(fs, i)
5361 + fs->inode_blocks_per_group)) {
5362 sprintf(problem, "is in the inode table of group %d",
5366 super += fs->super->s_blocks_per_group;
5373 * This is a helper function for check_blocks().
5375 static int process_block(ext2_filsys fs,
5377 e2_blkcnt_t blockcnt,
5378 blk64_t ref_block EXT2FS_ATTR((unused)),
5379 int ref_offset EXT2FS_ATTR((unused)),
5382 struct process_block_struct *p;
5383 struct problem_context *pctx;
5384 blk64_t blk = *block_nr;
5386 problem_t problem = 0;
5389 p = (struct process_block_struct *) priv_data;
5394 * For a directory, add logical block zero for processing even if it's
5395 * not mapped or we'll be perennially stuck with broken "." and ".."
5398 if (p->is_dir && blockcnt == 0 && blk == 0) {
5399 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
5400 if (pctx->errcode) {
5402 pctx->num = blockcnt;
5403 goto failed_add_dir_block;
5412 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
5417 * Simplistic fragmentation check. We merely require that the
5418 * file be contiguous. (Which can never be true for really
5419 * big files that are greater than a block group.)
5421 if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
5422 if (p->previous_block+1 != blk) {
5423 if (ctx->options & E2F_OPT_FRAGCHECK) {
5431 printf(_("%6lu(%c): expecting %6lu "
5432 "got phys %6lu (blkcnt %lld)\n"),
5433 (unsigned long) pctx->ino, type,
5434 (unsigned long) p->previous_block+1,
5435 (unsigned long) blk,
5436 (long long) blockcnt);
5442 if (p->is_dir && !ext2fs_has_feature_largedir(fs->super) &&
5443 !pctx->inode->i_size_high &&
5444 blockcnt > (1 << (21 - fs->super->s_log_block_size)))
5445 problem = PR_1_TOOBIG_DIR;
5446 if (p->is_dir && p->num_blocks + 1 >= p->max_blocks)
5447 problem = PR_1_TOOBIG_DIR;
5448 if (p->is_reg && p->num_blocks + 1 >= p->max_blocks)
5449 problem = PR_1_TOOBIG_REG;
5450 if (!p->is_dir && !p->is_reg && blockcnt > 0)
5451 problem = PR_1_TOOBIG_SYMLINK;
5453 if (blk < fs->super->s_first_data_block ||
5454 blk >= ext2fs_blocks_count(fs->super))
5455 problem = PR_1_ILLEGAL_BLOCK_NUM;
5458 * If this IND/DIND/TIND block is squatting atop some critical metadata
5459 * (group descriptors, superblock, bitmap, inode table), any write to
5460 * "fix" mapping problems will destroy the metadata. We'll let pass 1b
5461 * fix that and restart fsck.
5464 p->ino != EXT2_RESIZE_INO &&
5465 blk < ctx->fs->super->s_blocks_count &&
5466 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
5468 fix_problem_bad(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx, 2);
5469 if ((ctx->options & E2F_OPT_NO) == 0)
5470 ctx->flags |= E2F_FLAG_RESTART_LATER;
5474 p->num_illegal_blocks++;
5476 * A bit of subterfuge here -- we're trying to fix a block
5477 * mapping, but the IND/DIND/TIND block could have collided
5478 * with some critical metadata. So, fix the in-core mapping so
5479 * iterate won't go insane, but return 0 instead of
5480 * BLOCK_CHANGED so that it won't write the remapping out to
5481 * our multiply linked block.
5483 * Even if we previously determined that an *IND block
5484 * conflicts with critical metadata, we must still try to
5485 * iterate the *IND block as if it is an *IND block to find and
5486 * mark the blocks it points to. Better to be overly cautious
5487 * with the used_blocks map so that we don't move the *IND
5488 * block to a block that's really in use!
5490 if (p->ino != EXT2_RESIZE_INO &&
5492 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
5497 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
5498 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
5502 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
5504 set_latch_flags(PR_LATCH_BLOCK,
5509 pctx->blkcount = blockcnt;
5510 if (fix_problem(ctx, problem, pctx)) {
5511 blk = *block_nr = 0;
5512 ret_code = BLOCK_CHANGED;
5513 p->inode_modified = 1;
5515 * If the directory block is too big and is beyond the
5516 * end of the FS, don't bother trying to add it for
5517 * processing -- the kernel would never have created a
5518 * directory this large, and we risk an ENOMEM abort.
5519 * In any case, the toobig handler for extent-based
5520 * directories also doesn't feed toobig blocks to
5523 if (problem == PR_1_TOOBIG_DIR)
5530 if (p->ino == EXT2_RESIZE_INO) {
5532 * The resize inode has already be sanity checked
5533 * during pass #0 (the superblock checks). All we
5534 * have to do is mark the double indirect block as
5535 * being in use; all of the other blocks are handled
5536 * by mark_table_blocks()).
5538 if (blockcnt == BLOCK_COUNT_DIND)
5539 mark_block_used(ctx, blk);
5541 } else if (!(ctx->fs->cluster_ratio_bits &&
5542 p->previous_block &&
5543 (EXT2FS_B2C(ctx->fs, blk) ==
5544 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
5545 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
5546 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
5547 mark_block_used(ctx, blk);
5549 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
5550 p->last_block, blk, blockcnt)) {
5551 pctx->blk = blockcnt;
5553 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
5554 mark_block_used(ctx, blk);
5555 mark_block_used(ctx, blk);
5558 p->last_block = blockcnt;
5559 p->previous_block = blk;
5561 if (p->is_dir && (blockcnt >= 0)) {
5562 while (++p->last_db_block < blockcnt) {
5563 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
5566 if (pctx->errcode) {
5568 pctx->num = p->last_db_block;
5569 goto failed_add_dir_block;
5572 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
5574 if (pctx->errcode) {
5576 pctx->num = blockcnt;
5577 failed_add_dir_block:
5578 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
5579 /* Should never get here */
5580 ctx->flags |= E2F_FLAG_ABORT;
5587 static int process_bad_block(ext2_filsys fs,
5589 e2_blkcnt_t blockcnt,
5590 blk64_t ref_block EXT2FS_ATTR((unused)),
5591 int ref_offset EXT2FS_ATTR((unused)),
5594 struct process_block_struct *p;
5595 blk64_t blk = *block_nr;
5596 blk64_t first_block;
5598 struct problem_context *pctx;
5604 p = (struct process_block_struct *) priv_data;
5608 pctx->ino = EXT2_BAD_INO;
5610 pctx->blkcount = blockcnt;
5612 if ((blk < fs->super->s_first_data_block) ||
5613 (blk >= ext2fs_blocks_count(fs->super))) {
5614 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
5616 return BLOCK_CHANGED;
5622 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
5624 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
5626 return BLOCK_CHANGED;
5628 } else if (is_blocks_used(ctx, blk, 1)) {
5630 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
5633 return BLOCK_CHANGED;
5635 if (e2fsck_should_abort(ctx))
5638 mark_block_used(ctx, blk);
5643 printf ("DEBUG: Marking %u as bad.\n", blk);
5645 ctx->fs_badblocks_count++;
5647 * If the block is not used, then mark it as used and return.
5648 * If it is already marked as found, this must mean that
5649 * there's an overlap between the filesystem table blocks
5650 * (bitmaps and inode table) and the bad block list.
5652 if (!is_blocks_used(ctx, blk, 1)) {
5653 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
5657 * Try to find the where the filesystem block was used...
5659 first_block = fs->super->s_first_data_block;
5661 for (i = 0; i < fs->group_desc_count; i++ ) {
5664 if (!ext2fs_bg_has_super(fs, i))
5666 if (blk == first_block) {
5668 if (fix_problem(ctx,
5669 PR_1_BAD_PRIMARY_SUPERBLOCK,
5672 return BLOCK_CHANGED;
5676 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
5679 if ((blk > first_block) &&
5680 (blk <= first_block + fs->desc_blocks)) {
5682 pctx->blk = *block_nr;
5683 if (fix_problem(ctx,
5684 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
5686 return BLOCK_CHANGED;
5690 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
5694 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
5695 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
5696 ctx->invalid_block_bitmap_flag[i]++;
5697 ctx->invalid_bitmaps++;
5701 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
5702 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
5703 ctx->invalid_inode_bitmap_flag[i]++;
5704 ctx->invalid_bitmaps++;
5708 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
5709 (blk < (ext2fs_inode_table_loc(fs, i) +
5710 fs->inode_blocks_per_group))) {
5712 * If there are bad blocks in the inode table,
5713 * the inode scan code will try to do
5714 * something reasonable automatically.
5718 first_block += fs->super->s_blocks_per_group;
5721 * If we've gotten to this point, then the only
5722 * possibility is that the bad block inode meta data
5723 * is using a bad block.
5725 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
5726 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
5727 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
5729 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
5731 return BLOCK_CHANGED;
5733 if (e2fsck_should_abort(ctx))
5740 /* Warn user that the block wasn't claimed */
5741 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
5746 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
5747 const char *name, int num, blk64_t *new_block)
5749 ext2_filsys fs = ctx->fs;
5751 blk64_t old_block = *new_block;
5754 unsigned flexbg_size;
5757 struct problem_context pctx;
5759 clear_problem_context(&pctx);
5762 pctx.blk = old_block;
5766 * For flex_bg filesystems, first try to allocate the metadata
5767 * within the flex_bg, and if that fails then try finding the
5768 * space anywhere in the filesystem.
5770 is_flexbg = ext2fs_has_feature_flex_bg(fs->super);
5772 flexbg_size = 1U << fs->super->s_log_groups_per_flex;
5773 flexbg = group / flexbg_size;
5774 first_block = ext2fs_group_first_block2(fs,
5775 flexbg_size * flexbg);
5776 last_grp = group | (flexbg_size - 1);
5777 if (last_grp >= fs->group_desc_count)
5778 last_grp = fs->group_desc_count - 1;
5779 last_block = ext2fs_group_last_block2(fs, last_grp);
5781 last_block = ext2fs_group_last_block2(fs, group);
5782 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
5783 num, ctx->block_found_map,
5785 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
5786 pctx.errcode = ext2fs_get_free_blocks2(fs,
5787 fs->super->s_first_data_block,
5788 ext2fs_blocks_count(fs->super),
5789 num, ctx->block_found_map, new_block);
5792 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
5793 ext2fs_unmark_valid(fs);
5794 ctx->flags |= E2F_FLAG_ABORT;
5797 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
5799 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
5800 ext2fs_unmark_valid(fs);
5801 ctx->flags |= E2F_FLAG_ABORT;
5804 ext2fs_mark_super_dirty(fs);
5805 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
5806 pctx.blk2 = *new_block;
5807 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
5808 PR_1_RELOC_TO), &pctx);
5810 for (i = 0; i < num; i++) {
5812 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
5814 pctx.errcode = io_channel_read_blk64(fs->io,
5815 old_block + i, 1, buf);
5817 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
5818 pctx.blk = (*new_block) + i;
5819 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
5822 pctx.blk = (*new_block) + i;
5823 pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
5828 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
5830 ext2fs_free_mem(&buf);
5834 * This routine gets called at the end of pass 1 if bad blocks are
5835 * detected in the superblock, group descriptors, inode_bitmaps, or
5836 * block bitmaps. At this point, all of the blocks have been mapped
5837 * out, so we can try to allocate new block(s) to replace the bad
5840 static void handle_fs_bad_blocks(e2fsck_t ctx)
5842 ext2_filsys fs = ctx->fs;
5844 blk64_t first_block;
5847 for (i = 0; i < fs->group_desc_count; i++) {
5848 first_block = ext2fs_group_first_block2(fs, i);
5850 if (ctx->invalid_block_bitmap_flag[i]) {
5851 new_blk = ext2fs_block_bitmap_loc(fs, i);
5852 new_table_block(ctx, first_block, i, _("block bitmap"),
5854 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
5856 if (ctx->invalid_inode_bitmap_flag[i]) {
5857 new_blk = ext2fs_inode_bitmap_loc(fs, i);
5858 new_table_block(ctx, first_block, i, _("inode bitmap"),
5860 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
5862 if (ctx->invalid_inode_table_flag[i]) {
5863 new_blk = ext2fs_inode_table_loc(fs, i);
5864 new_table_block(ctx, first_block, i, _("inode table"),
5865 fs->inode_blocks_per_group,
5867 ext2fs_inode_table_loc_set(fs, i, new_blk);
5868 ctx->flags |= E2F_FLAG_RESTART;
5871 ctx->invalid_bitmaps = 0;
5875 * This routine marks all blocks which are used by the superblock,
5876 * group descriptors, inode bitmaps, and block bitmaps.
5878 static void mark_table_blocks(e2fsck_t ctx)
5880 ext2_filsys fs = ctx->fs;
5884 struct problem_context pctx;
5886 clear_problem_context(&pctx);
5888 for (i = 0; i < fs->group_desc_count; i++) {
5891 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
5892 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
5895 * Mark the blocks used for the inode table
5897 if (ext2fs_inode_table_loc(fs, i)) {
5898 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
5899 j < fs->inode_blocks_per_group;
5901 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5904 if (!ctx->invalid_inode_table_flag[i] &&
5906 PR_1_ITABLE_CONFLICT, &pctx)) {
5907 ctx->invalid_inode_table_flag[i]++;
5908 ctx->invalid_bitmaps++;
5911 ext2fs_mark_block_bitmap2(
5912 ctx->block_found_map, b);
5913 ext2fs_mark_block_bitmap2(
5914 ctx->block_metadata_map, b);
5920 * Mark block used for the block bitmap
5922 if (ext2fs_block_bitmap_loc(fs, i)) {
5923 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5924 ext2fs_block_bitmap_loc(fs, i))) {
5925 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
5926 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
5927 ctx->invalid_block_bitmap_flag[i]++;
5928 ctx->invalid_bitmaps++;
5931 ext2fs_mark_block_bitmap2(ctx->block_found_map,
5932 ext2fs_block_bitmap_loc(fs, i));
5933 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5934 ext2fs_block_bitmap_loc(fs, i));
5938 * Mark block used for the inode bitmap
5940 if (ext2fs_inode_bitmap_loc(fs, i)) {
5941 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5942 ext2fs_inode_bitmap_loc(fs, i))) {
5943 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
5944 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
5945 ctx->invalid_inode_bitmap_flag[i]++;
5946 ctx->invalid_bitmaps++;
5949 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5950 ext2fs_inode_bitmap_loc(fs, i));
5951 ext2fs_mark_block_bitmap2(ctx->block_found_map,
5952 ext2fs_inode_bitmap_loc(fs, i));
5959 * These subroutines short circuits ext2fs_get_blocks and
5960 * ext2fs_check_directory; we use them since we already have the inode
5961 * structure, so there's no point in letting the ext2fs library read
5964 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
5967 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5970 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5971 return EXT2_ET_CALLBACK_NOTHANDLED;
5973 for (i=0; i < EXT2_N_BLOCKS; i++)
5974 blocks[i] = ctx->stashed_inode->i_block[i];
5978 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
5979 struct ext2_inode *inode)
5981 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5983 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5984 return EXT2_ET_CALLBACK_NOTHANDLED;
5985 *inode = *ctx->stashed_inode;
5989 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
5990 struct ext2_inode *inode)
5992 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5994 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
5995 (inode != ctx->stashed_inode))
5996 *ctx->stashed_inode = *inode;
5997 return EXT2_ET_CALLBACK_NOTHANDLED;
6000 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
6002 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6004 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
6005 return EXT2_ET_CALLBACK_NOTHANDLED;
6007 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
6008 return EXT2_ET_NO_DIRECTORY;
6012 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
6015 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6019 if (ctx->block_found_map) {
6020 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
6024 if (fs->block_map) {
6025 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
6026 ext2fs_mark_bb_dirty(fs);
6029 if (!fs->block_map) {
6030 retval = ext2fs_read_block_bitmap(fs);
6035 retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
6044 static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
6045 blk64_t len, blk64_t *pblk, blk64_t *plen)
6047 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6050 if (ctx->block_found_map)
6051 return ext2fs_new_range(fs, flags, goal, len,
6052 ctx->block_found_map, pblk, plen);
6054 if (!fs->block_map) {
6055 retval = ext2fs_read_block_bitmap(fs);
6060 return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
6064 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
6066 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6068 /* Never free a critical metadata block */
6069 if (ctx->block_found_map &&
6070 ctx->block_metadata_map &&
6072 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
6075 if (ctx->block_found_map) {
6077 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
6079 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
6083 static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
6084 blk_t num, int inuse)
6086 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6088 /* Never free a critical metadata block */
6089 if (ctx->block_found_map &&
6090 ctx->block_metadata_map &&
6092 ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
6095 if (ctx->block_found_map) {
6097 ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
6100 ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
6105 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
6107 ext2_filsys fs = ctx->fs;
6109 if (use_shortcuts) {
6110 fs->get_blocks = pass1_get_blocks;
6111 fs->check_directory = pass1_check_directory;
6112 fs->read_inode = pass1_read_inode;
6113 fs->write_inode = pass1_write_inode;
6114 ctx->stashed_ino = 0;
6117 fs->check_directory = 0;
6119 fs->write_inode = 0;
6123 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
6125 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
6126 ext2fs_set_block_alloc_stats_callback(ctx->fs,
6127 e2fsck_block_alloc_stats, 0);
6128 ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
6129 ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
6130 e2fsck_block_alloc_stats_range, NULL);