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;
403 (void *)entry < end && !EXT2_EXT_IS_LAST_ENTRY(entry);
404 entry = EXT2_EXT_ATTR_NEXT(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,
422 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx,
423 struct ea_quota *ea_ibody_quota)
425 struct ext2_super_block *sb = ctx->fs->super;
426 struct ext2_inode_large *inode;
427 struct ext2_ext_attr_entry *entry;
428 char *start, *header, *end;
429 unsigned int storage_size, remain;
430 problem_t problem = 0;
433 ea_ibody_quota->blocks = 0;
434 ea_ibody_quota->inodes = 0;
436 inode = (struct ext2_inode_large *) pctx->inode;
437 storage_size = EXT2_INODE_SIZE(ctx->fs->super) -
438 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize;
439 header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
440 inode->i_extra_isize;
441 end = header + storage_size;
442 entry = &IHDR(inode)->h_first_entry[0];
443 start = (char *)entry;
445 /* scan all entry's headers first */
447 /* take finish entry 0UL into account */
448 remain = storage_size - sizeof(__u32);
450 region = region_create(0, storage_size);
452 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
454 ctx->flags |= E2F_FLAG_ABORT;
457 if (region_allocate(region, 0, sizeof(__u32))) {
458 problem = PR_1_INODE_EA_ALLOC_COLLISION;
462 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
463 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
466 if (region_allocate(region, (char *)entry - (char *)header,
467 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
468 problem = PR_1_INODE_EA_ALLOC_COLLISION;
472 /* header eats this space */
473 remain -= sizeof(struct ext2_ext_attr_entry);
475 /* is attribute name valid? */
476 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
477 pctx->num = entry->e_name_len;
478 problem = PR_1_ATTR_NAME_LEN;
482 /* attribute len eats this space */
483 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
485 if (entry->e_value_inum == 0) {
486 /* check value size */
487 if (entry->e_value_size > remain) {
488 pctx->num = entry->e_value_size;
489 problem = PR_1_ATTR_VALUE_SIZE;
493 if (entry->e_value_size &&
494 region_allocate(region,
495 sizeof(__u32) + entry->e_value_offs,
497 entry->e_value_size))) {
498 problem = PR_1_INODE_EA_ALLOC_COLLISION;
502 hash = ext2fs_ext_attr_hash_entry(entry,
503 start + entry->e_value_offs);
505 /* e_hash may be 0 in older inode's ea */
506 if (entry->e_hash != 0 && entry->e_hash != hash) {
507 pctx->num = entry->e_hash;
508 problem = PR_1_ATTR_HASH;
512 blk64_t quota_blocks;
514 problem = check_large_ea_inode(ctx, entry, pctx,
519 ea_ibody_quota->blocks += quota_blocks;
520 ea_ibody_quota->inodes++;
523 /* If EA value is stored in external inode then it does not
524 * consume space here */
525 if (entry->e_value_inum == 0)
526 remain -= entry->e_value_size;
528 entry = EXT2_EXT_ATTR_NEXT(entry);
531 if (region_allocate(region, (char *)entry - (char *)header,
533 problem = PR_1_INODE_EA_ALLOC_COLLISION;
540 * it seems like a corruption. it's very unlikely we could repair
541 * EA(s) in automatic fashion -bzzz
543 if (problem == 0 || !fix_problem(ctx, problem, pctx)) {
544 inc_ea_inode_refs(ctx, pctx,
545 (struct ext2_ext_attr_entry *)start, end);
549 /* simply remove all possible EA(s) */
550 *((__u32 *)header) = 0UL;
551 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
552 EXT2_INODE_SIZE(sb), "pass1");
553 ea_ibody_quota->blocks = 0;
554 ea_ibody_quota->inodes = 0;
557 static int check_inode_extra_negative_epoch(__u32 xtime, __u32 extra) {
558 return (xtime & (1U << 31)) != 0 &&
559 (extra & EXT4_EPOCH_MASK) == EXT4_EPOCH_MASK;
562 #define CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, xtime) \
563 check_inode_extra_negative_epoch(inode->i_##xtime, \
564 inode->i_##xtime##_extra)
566 /* When today's date is earlier than 2242, we assume that atimes,
567 * ctimes, crtimes, and mtimes with years in the range 2310..2378 are
568 * actually pre-1970 dates mis-encoded.
570 #define EXT4_EXTRA_NEGATIVE_DATE_CUTOFF 2 * (1LL << 32)
572 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx,
573 struct ea_quota *ea_ibody_quota)
575 struct ext2_super_block *sb = ctx->fs->super;
576 struct ext2_inode_large *inode;
578 int min, max, dirty = 0;
580 ea_ibody_quota->blocks = 0;
581 ea_ibody_quota->inodes = 0;
583 inode = (struct ext2_inode_large *) pctx->inode;
584 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
585 /* this isn't large inode. so, nothing to check */
590 printf("inode #%u, i_extra_size %d\n", pctx->ino,
591 inode->i_extra_isize);
593 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
594 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
595 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
597 * For now we will allow i_extra_isize to be 0, but really
598 * implementations should never allow i_extra_isize to be 0
600 if (inode->i_extra_isize &&
601 (inode->i_extra_isize < min || inode->i_extra_isize > max ||
602 inode->i_extra_isize & 3)) {
603 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
605 if (inode->i_extra_isize < min || inode->i_extra_isize > max)
606 inode->i_extra_isize = ctx->want_extra_isize;
608 inode->i_extra_isize = (inode->i_extra_isize + 3) & ~3;
614 /* check if there is no place for an EA header */
615 if (inode->i_extra_isize >= max - sizeof(__u32))
618 eamagic = &IHDR(inode)->h_magic;
619 if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
620 (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
621 (inode->i_extra_isize < ctx->want_extra_isize)) {
622 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
623 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
624 EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
625 inode->i_extra_isize = ctx->want_extra_isize;
627 if (inode->i_extra_isize < ctx->min_extra_isize)
628 ctx->min_extra_isize = inode->i_extra_isize;
631 if (*eamagic == EXT2_EXT_ATTR_MAGIC)
632 check_ea_in_inode(ctx, pctx, ea_ibody_quota);
634 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, ctx->time_fudge))
635 e2fsck_mark_inode_bad(ctx, pctx, PR_1_CRTIME_BAD);
636 else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, ctx->time_fudge))
637 e2fsck_mark_inode_bad(ctx, pctx, PR_1_CRTIME_BAD);
639 * If the inode's extended atime (ctime, crtime, mtime) is stored in
640 * the old, invalid format, repair it.
642 if (((sizeof(time_t) <= 4) ||
643 (((sizeof(time_t) > 4) &&
644 ctx->now < EXT4_EXTRA_NEGATIVE_DATE_CUTOFF))) &&
645 (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime) ||
646 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime) ||
647 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime) ||
648 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))) {
650 if (!fix_problem_bad(ctx, PR_1_EA_TIME_OUT_OF_RANGE, pctx, 2))
653 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime))
654 inode->i_atime_extra &= ~EXT4_EPOCH_MASK;
655 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime))
656 inode->i_ctime_extra &= ~EXT4_EPOCH_MASK;
657 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime))
658 inode->i_crtime_extra &= ~EXT4_EPOCH_MASK;
659 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))
660 inode->i_mtime_extra &= ~EXT4_EPOCH_MASK;
666 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
667 EXT2_INODE_SIZE(sb), "pass1");
670 static _INLINE_ int is_blocks_used(e2fsck_t ctx, blk64_t block,
675 /* used to avoid duplicate output from below */
676 retval = ext2fs_test_block_bitmap_range2_valid(ctx->block_found_map,
681 retval = ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num);
683 e2fsck_pass1_block_map_r_lock(ctx);
685 retval = ext2fs_test_block_bitmap_range2(
686 ctx->global_ctx->block_found_map, block, num);
687 e2fsck_pass1_block_map_r_unlock(ctx);
696 * Check to see if the inode might really be a directory, despite i_mode
698 * This is a lot of complexity for something for which I'm not really
699 * convinced happens frequently in the wild. If for any reason this
700 * causes any problems, take this code out.
701 * [tytso:20070331.0827EDT]
703 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
706 struct ext2_inode *inode = pctx->inode;
707 struct ext2_dir_entry *dirent;
710 unsigned int i, rec_len, not_device = 0;
715 * If the mode looks OK, we believe it. If the first block in
716 * the i_block array is 0, this cannot be a directory. If the
717 * inode is extent-mapped, it is still the case that the latter
718 * cannot be 0 - the magic number in the extent header would make
721 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
722 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
726 * Check the block numbers in the i_block array for validity:
727 * zero blocks are skipped (but the first one cannot be zero -
728 * see above), other blocks are checked against the first and
729 * max data blocks (from the the superblock) and against the
730 * block bitmap. Any invalid block found means this cannot be
733 * If there are non-zero blocks past the fourth entry, then
734 * this cannot be a device file: we remember that for the next
737 * For extent mapped files, we don't do any sanity checking:
738 * just try to get the phys block of logical block 0 and run
741 * For inline data files, we just try to get the size of inline
742 * data. If it's true, we will treat it as a directory.
745 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
746 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
747 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
750 unsigned int rec_len2;
751 struct ext2_dir_entry de;
753 if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
756 * If the size isn't a multiple of 4, it's probably not a
762 * If the first 10 bytes don't look like a directory entry,
763 * it's probably not a directory.
765 memcpy(&dotdot, inode->i_block, sizeof(dotdot));
766 memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
767 EXT2_DIR_NAME_LEN(0));
768 dotdot = ext2fs_le32_to_cpu(dotdot);
769 de.inode = ext2fs_le32_to_cpu(de.inode);
770 de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
771 ext2fs_get_rec_len(ctx->fs, &de, &rec_len2);
772 if (dotdot >= ctx->fs->super->s_inodes_count ||
773 (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
774 dotdot != EXT2_ROOT_INO) ||
775 de.inode >= ctx->fs->super->s_inodes_count ||
776 (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
778 rec_len2 > EXT4_MIN_INLINE_DATA_SIZE -
779 EXT4_INLINE_DATA_DOTDOT_SIZE)
781 /* device files never have a "system.data" entry */
783 } else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
785 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
788 /* device files are never extent mapped */
791 for (i=0; i < EXT2_N_BLOCKS; i++) {
792 blk = inode->i_block[i];
798 if (blk < ctx->fs->super->s_first_data_block ||
799 blk >= ext2fs_blocks_count(ctx->fs->super) ||
800 is_blocks_used(ctx, blk, 1))
801 return; /* Invalid block, can't be dir */
803 blk = inode->i_block[0];
807 * If the mode says this is a device file and the i_links_count field
808 * is sane and we have not ruled it out as a device file previously,
809 * we declare it a device file, not a directory.
811 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
812 (inode->i_links_count == 1) && !not_device)
815 /* read the first block */
816 ehandler_operation(_("reading directory block"));
817 retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
818 ehandler_operation(0);
822 dirent = (struct ext2_dir_entry *) buf;
823 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
826 if ((ext2fs_dirent_name_len(dirent) != 1) ||
827 (dirent->name[0] != '.') ||
828 (dirent->inode != pctx->ino) ||
831 (rec_len >= ctx->fs->blocksize - 12))
834 dirent = (struct ext2_dir_entry *) (buf + rec_len);
835 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
838 if ((ext2fs_dirent_name_len(dirent) != 2) ||
839 (dirent->name[0] != '.') ||
840 (dirent->name[1] != '.') ||
846 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
847 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
848 e2fsck_write_inode_full(ctx, pctx->ino, inode,
849 EXT2_INODE_SIZE(ctx->fs->super),
850 "check_is_really_dir");
854 extern errcode_t e2fsck_setup_icount(e2fsck_t ctx, const char *icount_name,
855 int flags, ext2_icount_t hint,
858 unsigned int threshold;
859 unsigned int save_type;
867 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
869 profile_get_uint(ctx->profile, "scratch_files",
870 "numdirs_threshold", 0, 0, &threshold);
871 profile_get_boolean(ctx->profile, "scratch_files",
872 "icount", 0, 1, &enable);
874 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
876 num_dirs = 1024; /* Guess */
878 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
879 (!threshold || num_dirs > threshold)) {
880 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir,
885 e2fsck_set_bitmap_type(ctx->fs, EXT2FS_BMAP64_RBTREE, icount_name,
887 if (ctx->options & E2F_OPT_ICOUNT_FULLMAP)
888 flags |= EXT2_ICOUNT_OPT_FULLMAP;
889 retval = ext2fs_create_icount2(ctx->fs, flags, 0, hint, ret);
890 ctx->fs->default_bitmap_type = save_type;
894 static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
896 struct problem_context *pctx)
899 struct ext2_inode_large inode;
902 * Reread inode. If we don't see checksum error, then this inode
903 * has been fixed elsewhere.
905 ctx->stashed_ino = 0;
906 retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
908 if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
914 * Checksum still doesn't match. That implies that the inode passes
915 * all the sanity checks, so maybe the checksum is simply corrupt.
916 * See if the user will go for fixing that.
918 if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
922 e2fsck_pass1_fix_lock(ctx);
923 retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
925 e2fsck_pass1_fix_unlock(ctx);
929 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
930 struct problem_context *pctx, int needed_size)
932 struct ext2_ext_attr_header *header;
933 struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
934 char *start, name[4096], block_buf[4096];
935 int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
936 int in_inode = 1, error;
937 unsigned int freed_bytes = inode->i_extra_isize;
939 entry_ino = &IHDR(inode)->h_first_entry[0];
940 start = (char *)entry_ino;
942 if (inode->i_file_acl) {
943 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
945 /* We have already checked this block, shouldn't happen */
947 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
950 header = BHDR(block_buf);
951 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
952 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
956 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
959 len = sizeof(entry->e_name);
960 entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
962 while (freed_bytes < needed_size) {
963 if (entry_size && name[0] != '\0') {
965 if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
966 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
967 EXT2_EXT_ATTR_SIZE(entry->e_value_size);
968 error = ext2fs_attr_set(ctx->fs, pctx->ino,
969 (struct ext2_inode *)inode,
970 index, name, 0, 0, 0);
972 freed_bytes += ea_size;
975 len = sizeof(entry->e_name);
976 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
977 entry = EXT2_EXT_ATTR_NEXT(entry);
978 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
981 len = sizeof(entry->e_name);
982 entry_size = ext2fs_attr_get_next_attr(entry,
983 index, name, len, 1);
988 if (!entry && index < EXT2_ATTR_INDEX_MAX)
989 entry = (struct ext2_ext_attr_entry *)start;
999 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
1000 struct problem_context *pctx)
1002 int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
1006 retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
1007 ctx->want_extra_isize, &ret,
1009 if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
1010 goto mark_expand_eisize_map;
1012 e2fsck_write_inode_full(ctx, pctx->ino,
1013 (struct ext2_inode *)inode,
1014 EXT2_INODE_SIZE(ctx->fs->super),
1019 if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
1020 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
1021 fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
1022 ctx->flags |= E2F_FLAG_ABORT;
1027 pctx->num = ctx->fs->super->s_min_extra_isize;
1028 fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
1032 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
1034 if (retval >= ctx->want_extra_isize)
1037 needed_size -= retval;
1040 * We loop here until either the user deletes EA(s) or
1041 * EXTRA_ISIZE feature is disabled.
1043 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
1044 ctx->fs->super->s_feature_ro_compat &=
1045 ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
1046 ext2fs_mark_super_dirty(ctx->fs);
1050 ctx->fs_unexpanded_inodes++;
1052 /* No EA was deleted, inode cannot be expanded */
1056 mark_expand_eisize_map:
1057 if (!ctx->expand_eisize_map) {
1058 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1059 _("expand extrz isize map"),
1060 &ctx->expand_eisize_map);
1061 if (pctx->errcode) {
1062 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
1068 /* Add this inode to the expand_eisize_map */
1069 ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
1073 static void reserve_block_for_root_repair(e2fsck_t ctx)
1077 ext2_filsys fs = ctx->fs;
1079 ctx->root_repair_block = 0;
1080 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
1083 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1086 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1087 ctx->root_repair_block = blk;
1090 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
1094 ext2_filsys fs = ctx->fs;
1095 static const char name[] = "lost+found";
1098 ctx->lnf_repair_block = 0;
1099 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
1102 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1105 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1106 ctx->lnf_repair_block = blk;
1110 static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
1114 struct ext2_xattr_handle *handle;
1117 retval = ext2fs_xattrs_open(fs, ino, &handle);
1121 retval = ext2fs_xattrs_read(handle);
1125 retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
1128 ext2fs_free_mem(&p);
1130 (void) ext2fs_xattrs_close(&handle);
1134 int e2fsck_fix_bad_inode(e2fsck_t ctx, struct problem_context *pctx)
1139 if (!ctx->inode_badness)
1142 if (ext2fs_icount_fetch(ctx->inode_badness, pctx->ino, &badness))
1145 if ((badness & ~BADNESS_BAD_MODE) > ctx->inode_badness_threshold) {
1146 __u64 pctx_num_sav = pctx->num;
1148 pctx->num = badness;
1149 rc = fix_problem_bad(ctx, PR_1B_INODE_TOOBAD, pctx, 0);
1150 pctx->num = pctx_num_sav;
1156 static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
1157 struct problem_context *pctx,
1164 * If the inode failed the checksum and the user didn't
1165 * clear the inode, test the checksum again -- if it still
1166 * fails, ask the user if the checksum should be corrected.
1168 pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
1170 ctx->flags |= E2F_FLAG_ABORT;
1172 #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
1174 finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
1175 if (e2fsck_should_abort(ctx)) { \
1176 e2fsck_pass1_check_unlock(ctx); \
1181 static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
1186 for (i = 0; i < EXT2_N_BLOCKS; i++) {
1187 x = inode->i_block[i];
1188 #ifdef WORDS_BIGENDIAN
1189 x = ext2fs_swab32(x);
1191 if (x >= ext2fs_blocks_count(fs->super))
1199 * Figure out what to do with an inode that has both extents and inline data
1200 * inode flags set. Returns -1 if we decide to erase the inode, 0 otherwise.
1202 static int fix_inline_data_extents_file(e2fsck_t ctx,
1204 struct ext2_inode *inode,
1206 struct problem_context *pctx)
1208 size_t max_inline_ea_size;
1209 ext2_filsys fs = ctx->fs;
1212 /* Both feature flags not set? Just run the regular checks */
1213 if (!ext2fs_has_feature_extents(fs->super) &&
1214 !ext2fs_has_feature_inline_data(fs->super))
1217 /* Clear both flags if it's a special file */
1218 if (LINUX_S_ISCHR(inode->i_mode) ||
1219 LINUX_S_ISBLK(inode->i_mode) ||
1220 LINUX_S_ISFIFO(inode->i_mode) ||
1221 LINUX_S_ISSOCK(inode->i_mode)) {
1222 check_extents_inlinedata(ctx, pctx);
1226 /* If it looks like an extent tree, try to clear inlinedata */
1227 if (ext2fs_extent_header_verify(inode->i_block,
1228 sizeof(inode->i_block)) == 0 &&
1229 fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
1230 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1235 /* If it looks short enough to be inline data, try to clear extents */
1236 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
1237 max_inline_ea_size = inode_size -
1238 (EXT2_GOOD_OLD_INODE_SIZE +
1239 ((struct ext2_inode_large *)inode)->i_extra_isize);
1241 max_inline_ea_size = 0;
1242 if (EXT2_I_SIZE(inode) <
1243 EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
1244 fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
1245 inode->i_flags &= ~EXT4_EXTENTS_FL;
1251 * Too big for inline data, but no evidence of extent tree -
1252 * maybe it's a block map file? If the mappings all look valid?
1254 if (could_be_block_map(fs, inode) &&
1255 fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
1256 #ifdef WORDS_BIGENDIAN
1259 for (i = 0; i < EXT2_N_BLOCKS; i++)
1260 inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
1263 inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
1268 /* Oh well, just clear the busted inode. */
1269 if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
1270 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1276 e2fsck_write_inode(ctx, ino, inode, "pass1");
1281 static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
1283 ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
1284 dgrp_t start = *group, grp, grp_end = ctx->fs->group_desc_count;
1285 blk64_t blocks_to_read = 0;
1286 errcode_t err = EXT2_ET_INVALID_ARGUMENT;
1289 if (ctx->fs->fs_num_threads > 1)
1290 grp_end = ctx->thread_info.et_group_end;
1292 if (ctx->readahead_kb == 0)
1295 /* Keep iterating groups until we have enough to readahead */
1296 inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
1297 for (grp = start; grp < grp_end; grp++) {
1298 if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
1300 inodes_in_group = ctx->fs->super->s_inodes_per_group -
1301 ext2fs_bg_itable_unused(ctx->fs, grp);
1302 blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
1304 if (blocks_to_read * ctx->fs->blocksize >
1305 ctx->readahead_kb * 1024)
1309 err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
1311 if (err == EAGAIN) {
1312 ctx->readahead_kb /= 2;
1318 /* Error; disable itable readahead */
1319 *group = ctx->fs->group_desc_count;
1320 *next_ino = ctx->fs->super->s_inodes_count;
1323 * Don't do more readahead until we've reached the first inode
1324 * of the last inode scan buffer block for the last group.
1327 inodes_per_buffer = (ctx->inode_buffer_blocks ?
1328 ctx->inode_buffer_blocks :
1329 EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
1330 ctx->fs->blocksize /
1331 EXT2_INODE_SIZE(ctx->fs->super);
1333 *next_ino = inodes_in_group -
1334 (inodes_in_group % inodes_per_buffer) + 1 +
1335 (grp * ctx->fs->super->s_inodes_per_group);
1340 * Check if the passed ino is one of the used superblock quota inodes.
1342 * Before the quota inodes were journaled, older superblock quota inodes
1343 * were just regular files in the filesystem and not reserved inodes. This
1344 * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1345 * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1347 static int quota_inum_is_super(struct ext2_super_block *sb, ext2_ino_t ino)
1349 enum quota_type qtype;
1351 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1352 if (*quota_sb_inump(sb, qtype) == ino)
1359 * Check if the passed ino is one of the reserved quota inodes.
1360 * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1361 * inodes. These inodes may or may not be in use by the quota feature.
1363 static int quota_inum_is_reserved(ext2_filsys fs, ext2_ino_t ino)
1365 enum quota_type qtype;
1367 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1368 if (quota_type2inum(qtype, fs->super) == ino)
1374 static int e2fsck_should_abort(e2fsck_t ctx)
1376 e2fsck_t global_ctx;
1378 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1381 if (ctx->global_ctx) {
1382 global_ctx = ctx->global_ctx;
1383 if (global_ctx->flags & E2F_FLAG_SIGNAL_MASK)
1389 static void init_ext2_max_sizes()
1395 * Init ext2_max_sizes which will be immutable and shared between
1398 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1400 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
1401 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
1402 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
1403 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
1404 max_sizes = (max_sizes * (1UL << i));
1405 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
1411 /* TODO: tdb needs to be handled properly for multiple threads*/
1412 static int multiple_threads_supported(e2fsck_t ctx)
1415 unsigned int threshold;
1416 ext2_ino_t num_dirs;
1421 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
1423 profile_get_uint(ctx->profile, "scratch_files",
1424 "numdirs_threshold", 0, 0, &threshold);
1425 profile_get_boolean(ctx->profile, "scratch_files",
1426 "icount", 0, 1, &enable);
1428 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
1430 num_dirs = 1024; /* Guess */
1432 /* tdb is unsupported now */
1433 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
1434 (!threshold || num_dirs > threshold))
1441 * Even though we could specify number of threads,
1442 * but it might be more than the whole filesystem
1443 * block groups, correct it here.
1445 static void e2fsck_pass1_set_thread_num(e2fsck_t ctx)
1447 unsigned flexbg_size = 1;
1448 ext2_filsys fs = ctx->fs;
1449 int num_threads = ctx->pfs_num_threads;
1452 if (num_threads < 1) {
1457 if (!multiple_threads_supported(ctx)) {
1459 fprintf(stderr, "Fall through single thread for pass1 "
1460 "because tdb could not handle properly\n");
1464 if (ext2fs_has_feature_flex_bg(fs->super))
1465 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1466 max_threads = fs->group_desc_count / flexbg_size;
1467 if (max_threads == 0)
1469 if (max_threads > E2FSCK_MAX_THREADS)
1470 max_threads = E2FSCK_MAX_THREADS;
1472 if (num_threads > max_threads) {
1473 fprintf(stderr, "Use max possible thread num: %d instead\n",
1475 num_threads = max_threads;
1478 ctx->pfs_num_threads = num_threads;
1479 ctx->fs->fs_num_threads = num_threads;
1484 * We need call mark_table_blocks() before multiple
1485 * thread start, since all known system blocks should be
1486 * marked and checked later.
1488 static errcode_t e2fsck_pass1_prepare(e2fsck_t ctx)
1490 struct problem_context pctx;
1491 ext2_filsys fs = ctx->fs;
1492 unsigned long long readahead_kb;
1494 init_ext2_max_sizes();
1496 e2fsck_pass1_set_thread_num(ctx);
1498 /* If we can do readahead, figure out how many groups to pull in. */
1499 if (!e2fsck_can_readahead(ctx->fs))
1500 ctx->readahead_kb = 0;
1501 else if (ctx->readahead_kb == ~0ULL)
1502 ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
1505 /* don't use more than 1/10 of memory for threads checking */
1506 readahead_kb = get_memory_size() / (10 * ctx->pfs_num_threads);
1507 /* maybe better disable RA if this is too small? */
1508 if (ctx->readahead_kb > readahead_kb)
1509 ctx->readahead_kb = readahead_kb;
1511 clear_problem_context(&pctx);
1512 if (!(ctx->options & E2F_OPT_PREEN))
1513 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1515 pctx.errcode = e2fsck_allocate_subcluster_bitmap(ctx->fs,
1516 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
1517 "block_found_map", &ctx->block_found_map);
1520 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1521 ctx->flags |= E2F_FLAG_ABORT;
1522 return pctx.errcode;
1524 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1525 _("metadata block map"), EXT2FS_BMAP64_RBTREE,
1526 "block_metadata_map", &ctx->block_metadata_map);
1529 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1530 ctx->flags |= E2F_FLAG_ABORT;
1531 return pctx.errcode;
1534 mark_table_blocks(ctx);
1535 pctx.errcode = ext2fs_convert_subcluster_bitmap(ctx->fs,
1536 &ctx->block_found_map);
1538 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1539 ctx->flags |= E2F_FLAG_ABORT;
1540 return pctx.errcode;
1543 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1544 _("multiply claimed block map"),
1545 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1546 &ctx->block_dup_map);
1549 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1551 /* Should never get here */
1552 ctx->flags |= E2F_FLAG_ABORT;
1553 return pctx.errcode;
1556 if (ext2fs_has_feature_mmp(fs->super) &&
1557 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1558 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1559 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1560 fs->super->s_mmp_block);
1562 pthread_rwlock_init(&ctx->fs_fix_rwlock, NULL);
1563 pthread_rwlock_init(&ctx->fs_block_map_rwlock, NULL);
1564 if (ctx->pfs_num_threads > 1)
1565 ctx->fs_need_locking = 1;
1571 static void e2fsck_pass1_post(e2fsck_t ctx)
1573 struct problem_context pctx;
1574 ext2_filsys fs = ctx->fs;
1577 if (e2fsck_should_abort(ctx))
1580 block_buf = (char *)e2fsck_allocate_memory(ctx, ctx->fs->blocksize * 3,
1581 "block interate buffer");
1582 reserve_block_for_root_repair(ctx);
1583 reserve_block_for_lnf_repair(ctx);
1586 * If any extended attribute blocks' reference counts need to
1587 * be adjusted, either up (ctx->refcount_extra), or down
1588 * (ctx->refcount), then fix them.
1590 if (ctx->refcount) {
1591 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1592 ea_refcount_free(ctx->refcount);
1595 if (ctx->refcount_extra) {
1596 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1598 ea_refcount_free(ctx->refcount_extra);
1599 ctx->refcount_extra = 0;
1602 if (ctx->invalid_bitmaps)
1603 handle_fs_bad_blocks(ctx);
1605 /* We don't need the block_ea_map any more */
1606 if (ctx->block_ea_map) {
1607 ext2fs_free_block_bitmap(ctx->block_ea_map);
1608 ctx->block_ea_map = 0;
1611 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1612 struct ext2_inode *inode;
1613 int inode_size = EXT2_INODE_SIZE(fs->super);
1614 inode = e2fsck_allocate_memory(ctx, inode_size,
1617 clear_problem_context(&pctx);
1618 pctx.errcode = ext2fs_create_resize_inode(fs);
1620 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1622 ctx->flags |= E2F_FLAG_ABORT;
1623 ext2fs_free_mem(&inode);
1624 ext2fs_free_mem(&block_buf);
1629 if (!pctx.errcode) {
1630 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1632 inode->i_mtime = ctx->now;
1633 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1636 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1637 ext2fs_free_mem(&inode);
1640 if (ctx->flags & E2F_FLAG_RESTART) {
1641 ext2fs_free_mem(&block_buf);
1645 if (ctx->block_dup_map) {
1646 if (!(ctx->flags & E2F_FLAG_DUP_BLOCK)) {
1647 ext2fs_free_mem(&block_buf);
1650 if (ctx->options & E2F_OPT_PREEN) {
1651 clear_problem_context(&pctx);
1652 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1654 e2fsck_pass1_dupblocks(ctx, block_buf);
1655 ext2fs_free_mem(&block_buf);
1656 ctx->flags &= ~E2F_FLAG_DUP_BLOCK;
1659 ctx->flags |= E2F_FLAG_ALLOC_OK;
1664 * Lustre FS creates special inodes - precreated objects.
1665 * They are zero-sized and have special attributes:
1666 * mode |= S_ISUID | S_ISGID;
1667 * valid |= LA_ATIME | LA_MTIME | LA_CTIME;
1672 static int precreated_object(struct ext2_inode *inode)
1674 if (((inode->i_mode & (S_ISUID | S_ISGID)) == (S_ISUID | S_ISGID)) &&
1675 inode->i_ctime == 0)
1680 void e2fsck_pass1_run(e2fsck_t ctx)
1683 ext2_filsys fs = ctx->fs;
1685 struct ext2_inode *inode = NULL;
1686 ext2_inode_scan scan = NULL;
1687 char *block_buf = NULL;
1688 #ifdef RESOURCE_TRACK
1689 struct resource_track rtrack;
1691 unsigned char frag, fsize;
1692 struct problem_context pctx;
1693 struct scan_callback_struct scan_struct;
1694 struct ext2_super_block *sb = ctx->fs->super;
1696 const char *eop_next_inode = _("getting next inode from scan");
1697 int imagic_fs, extent_fs, inlinedata_fs, casefold_fs;
1698 int low_dtime_check = 1;
1699 unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
1700 unsigned int bufsize;
1701 int failed_csum = 0;
1702 ext2_ino_t ino_threshold = 0;
1703 dgrp_t ra_group = 0;
1704 struct ea_quota ea_ibody_quota;
1705 struct process_inode_block *inodes_to_process;
1706 int process_inode_count, check_mmp;
1707 e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
1710 init_resource_track(&rtrack, ctx->fs->io);
1711 clear_problem_context(&pctx);
1713 pass1_readahead(ctx, &ra_group, &ino_threshold);
1714 if (ext2fs_has_feature_dir_index(fs->super) &&
1715 !(ctx->options & E2F_OPT_NO)) {
1716 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
1717 ctx->dirs_to_hash = 0;
1721 mtrace_print("Pass 1");
1724 imagic_fs = ext2fs_has_feature_imagic_inodes(sb);
1725 extent_fs = ext2fs_has_feature_extents(sb);
1726 inlinedata_fs = ext2fs_has_feature_inline_data(sb);
1727 casefold_fs = ext2fs_has_feature_casefold(sb);
1730 * Allocate bitmaps structures
1732 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1733 EXT2FS_BMAP64_RBTREE,
1735 &ctx->inode_used_map);
1738 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1739 ctx->flags |= E2F_FLAG_ABORT;
1742 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1743 _("directory inode map"),
1744 ctx->global_ctx ? EXT2FS_BMAP64_RBTREE :
1745 EXT2FS_BMAP64_AUTODIR,
1746 "inode_dir_map", &ctx->inode_dir_map);
1749 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1750 ctx->flags |= E2F_FLAG_ABORT;
1753 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1754 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1755 "inode_reg_map", &ctx->inode_reg_map);
1758 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1759 ctx->flags |= E2F_FLAG_ABORT;
1764 e2fsck_allocate_inode_bitmap(fs,
1765 _("inode casefold map"),
1766 EXT2FS_BMAP64_RBTREE,
1767 "inode_casefold_map",
1768 &ctx->inode_casefold_map);
1771 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1772 ctx->flags |= E2F_FLAG_ABORT;
1776 pctx.errcode = e2fsck_setup_icount(ctx, "inode_link_info", 0, NULL,
1777 &ctx->inode_link_info);
1779 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1780 ctx->flags |= E2F_FLAG_ABORT;
1783 bufsize = inode_size;
1784 if (bufsize < sizeof(struct ext2_inode_large))
1785 bufsize = sizeof(struct ext2_inode_large);
1786 inode = (struct ext2_inode *)
1787 e2fsck_allocate_memory(ctx, bufsize, "scratch inode");
1789 inodes_to_process = (struct process_inode_block *)
1790 e2fsck_allocate_memory(ctx,
1791 (ctx->process_inode_size *
1792 sizeof(struct process_inode_block)),
1793 "array of inodes to process");
1794 process_inode_count = 0;
1796 pctx.errcode = ext2fs_init_dblist(fs, 0);
1798 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1799 ctx->flags |= E2F_FLAG_ABORT;
1804 * If the last orphan field is set, clear it, since the pass1
1805 * processing will automatically find and clear the orphans.
1806 * In the future, we may want to try using the last_orphan
1807 * linked list ourselves, but for now, we clear it so that the
1808 * ext3 mount code won't get confused.
1810 if (!(ctx->options & E2F_OPT_READONLY)) {
1811 if (fs->super->s_last_orphan) {
1812 fs->super->s_last_orphan = 0;
1813 ext2fs_mark_super_dirty(fs);
1817 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1818 "block interate buffer");
1819 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1820 e2fsck_use_inode_shortcuts(ctx, 1);
1821 e2fsck_intercept_block_allocations(ctx);
1822 old_op = ehandler_operation(_("opening inode scan"));
1823 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1825 ehandler_operation(old_op);
1827 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1828 ctx->flags |= E2F_FLAG_ABORT;
1831 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1832 EXT2_SF_WARN_GARBAGE_INODES, 0);
1833 ctx->stashed_inode = inode;
1834 scan_struct.ctx = ctx;
1835 scan_struct.block_buf = block_buf;
1836 scan_struct.inodes_to_process = inodes_to_process;
1837 scan_struct.process_inode_count = &process_inode_count;
1838 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1839 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1840 ctx->fs->group_desc_count)))
1842 if ((fs->super->s_wtime &&
1843 fs->super->s_wtime < fs->super->s_inodes_count) ||
1844 (fs->super->s_mtime &&
1845 fs->super->s_mtime < fs->super->s_inodes_count) ||
1846 (fs->super->s_mkfs_time &&
1847 fs->super->s_mkfs_time < fs->super->s_inodes_count))
1848 low_dtime_check = 0;
1850 /* Set up ctx->lost_and_found if possible */
1851 (void) e2fsck_get_lost_and_found(ctx, 0);
1854 if (ctx->global_ctx) {
1855 if (ctx->options & E2F_OPT_DEBUG &&
1856 ctx->options & E2F_OPT_MULTITHREAD)
1857 log_out(ctx, "jumping to group %u\n",
1858 ctx->thread_info.et_group_start);
1859 pctx.errcode = ext2fs_inode_scan_goto_blockgroup(scan,
1860 ctx->thread_info.et_group_start);
1862 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1863 ctx->flags |= E2F_FLAG_ABORT;
1871 e2fsck_pass1_check_lock(ctx);
1873 if (!global_ctx->mmp_update_thread) {
1874 e2fsck_pass1_block_map_w_lock(ctx);
1875 if (!global_ctx->mmp_update_thread) {
1876 global_ctx->mmp_update_thread =
1877 ctx->thread_info.et_thread_index + 1;
1880 e2fsck_pass1_block_map_w_unlock(ctx);
1883 /* only one active thread could update mmp block. */
1884 e2fsck_pass1_block_map_r_lock(ctx);
1885 if (global_ctx->mmp_update_thread ==
1886 ctx->thread_info.et_thread_index + 1)
1888 e2fsck_pass1_block_map_r_unlock(ctx);
1893 if (check_mmp && (ino % (fs->super->s_inodes_per_group * 4) == 1)) {
1894 if (e2fsck_mmp_update(fs))
1895 fatal_error(ctx, 0);
1897 old_op = ehandler_operation(eop_next_inode);
1898 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1900 if (ino > ino_threshold)
1901 pass1_readahead(ctx, &ra_group, &ino_threshold);
1902 ehandler_operation(old_op);
1903 if (e2fsck_should_abort(ctx)) {
1904 e2fsck_pass1_check_unlock(ctx);
1907 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1909 * If badblocks says badblocks is bad, offer to clear
1910 * the list, update the in-core bb list, and restart
1913 if (ino == EXT2_BAD_INO &&
1914 fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1918 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1919 ext2fs_badblocks_list_free(ctx->fs->badblocks);
1920 ctx->fs->badblocks = NULL;
1921 err = ext2fs_read_bb_inode(ctx->fs,
1922 &ctx->fs->badblocks);
1924 fix_problem(ctx, PR_1_ISCAN_ERROR,
1926 ctx->flags |= E2F_FLAG_ABORT;
1927 e2fsck_pass1_check_unlock(ctx);
1930 ctx->flags |= E2F_FLAG_RESTART;
1931 err = ext2fs_inode_scan_goto_blockgroup(scan,
1934 fix_problem(ctx, PR_1_ISCAN_ERROR,
1936 ctx->flags |= E2F_FLAG_ABORT;
1937 e2fsck_pass1_check_unlock(ctx);
1940 e2fsck_pass1_check_unlock(ctx);
1943 if (!ctx->inode_bb_map)
1945 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1946 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1947 e2fsck_pass1_check_unlock(ctx);
1950 if (pctx.errcode == EXT2_ET_SCAN_FINISHED) {
1951 e2fsck_pass1_check_unlock(ctx);
1955 pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1956 pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1957 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1958 ctx->flags |= E2F_FLAG_ABORT;
1959 e2fsck_pass1_check_unlock(ctx);
1963 e2fsck_pass1_check_unlock(ctx);
1967 if (ctx->global_ctx)
1968 ctx->thread_info.et_inode_number++;
1972 ctx->stashed_ino = ino;
1974 /* Clear trashed inode? */
1975 if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
1976 inode->i_links_count > 0 &&
1977 fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
1979 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1981 failed_csum = pctx.errcode != 0;
1984 * Check for inodes who might have been part of the
1985 * orphaned list linked list. They should have gotten
1986 * dealt with by now, unless the list had somehow been
1989 * FIXME: In the future, inodes which are still in use
1990 * (and which are therefore) pending truncation should
1991 * be handled specially. Right now we just clear the
1992 * dtime field, and the normal e2fsck handling of
1993 * inodes where i_size and the inode blocks are
1994 * inconsistent is to fix i_size, instead of releasing
1995 * the extra blocks. This won't catch the inodes that
1996 * was at the end of the orphan list, but it's better
1997 * than nothing. The right answer is that there
1998 * shouldn't be any bugs in the orphan list handling. :-)
2000 if (inode->i_dtime && low_dtime_check &&
2001 inode->i_dtime < ctx->fs->super->s_inodes_count) {
2002 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
2003 inode->i_dtime = inode->i_links_count ?
2005 e2fsck_write_inode(ctx, ino, inode,
2011 if (inode->i_links_count) {
2012 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
2013 ino, inode->i_links_count);
2015 pctx.num = inode->i_links_count;
2016 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
2017 ctx->flags |= E2F_FLAG_ABORT;
2018 e2fsck_pass1_check_unlock(ctx);
2021 } else if ((ino >= EXT2_FIRST_INODE(fs->super)) &&
2022 !quota_inum_is_reserved(fs, ino)) {
2023 if (!inode->i_dtime && inode->i_mode) {
2024 if (fix_problem(ctx,
2025 PR_1_ZERO_DTIME, &pctx)) {
2026 inode->i_dtime = ctx->now;
2027 e2fsck_write_inode(ctx, ino, inode,
2032 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2033 e2fsck_pass1_check_unlock(ctx);
2037 if ((inode->i_flags & EXT4_CASEFOLD_FL) &&
2038 ((!LINUX_S_ISDIR(inode->i_mode) &&
2039 fix_problem(ctx, PR_1_CASEFOLD_NONDIR, &pctx)) ||
2041 fix_problem(ctx, PR_1_CASEFOLD_FEATURE, &pctx)))) {
2042 inode->i_flags &= ~EXT4_CASEFOLD_FL;
2043 e2fsck_write_inode(ctx, ino, inode, "pass1");
2046 /* Conflicting inlinedata/extents inode flags? */
2047 if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
2048 (inode->i_flags & EXT4_EXTENTS_FL)) {
2049 int res = fix_inline_data_extents_file(ctx, ino, inode,
2053 /* skip FINISH_INODE_LOOP */
2054 e2fsck_pass1_check_unlock(ctx);
2059 /* Test for incorrect inline_data flags settings. */
2060 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
2061 (ino >= EXT2_FIRST_INODE(fs->super))) {
2064 pctx.errcode = get_inline_data_ea_size(fs, ino, &size);
2065 if (!pctx.errcode &&
2066 fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
2067 e2fsck_pass1_fix_lock(ctx);
2068 ext2fs_set_feature_inline_data(sb);
2069 ext2fs_mark_super_dirty(fs);
2070 e2fsck_pass1_fix_unlock(ctx);
2072 } else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
2073 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2074 /* skip FINISH_INODE_LOOP */
2075 e2fsck_pass1_check_unlock(ctx);
2080 /* Test for inline data flag but no attr */
2081 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
2082 (ino >= EXT2_FIRST_INODE(fs->super))) {
2089 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2090 err = get_inline_data_ea_size(fs, ino, &size);
2091 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
2092 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
2096 /* Everything is awesome... */
2098 case EXT2_ET_BAD_EA_BLOCK_NUM:
2099 case EXT2_ET_BAD_EA_HASH:
2100 case EXT2_ET_BAD_EA_HEADER:
2101 case EXT2_ET_EA_BAD_NAME_LEN:
2102 case EXT2_ET_EA_BAD_VALUE_SIZE:
2103 case EXT2_ET_EA_KEY_NOT_FOUND:
2104 case EXT2_ET_EA_NO_SPACE:
2105 case EXT2_ET_MISSING_EA_FEATURE:
2106 case EXT2_ET_INLINE_DATA_CANT_ITERATE:
2107 case EXT2_ET_INLINE_DATA_NO_BLOCK:
2108 case EXT2_ET_INLINE_DATA_NO_SPACE:
2109 case EXT2_ET_NO_INLINE_DATA:
2110 case EXT2_ET_EXT_ATTR_CSUM_INVALID:
2111 case EXT2_ET_EA_BAD_VALUE_OFFSET:
2112 case EXT2_ET_EA_INODE_CORRUPTED:
2113 /* broken EA or no system.data EA; truncate */
2114 if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
2116 err = ext2fs_inode_size_set(fs, inode, 0);
2119 ctx->flags |= E2F_FLAG_ABORT;
2120 e2fsck_pass1_check_unlock(ctx);
2123 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
2124 memset(&inode->i_block, 0,
2125 sizeof(inode->i_block));
2126 e2fsck_write_inode(ctx, ino, inode,
2132 /* Some other kind of non-xattr error? */
2134 ctx->flags |= E2F_FLAG_ABORT;
2135 e2fsck_pass1_check_unlock(ctx);
2141 * Test for incorrect extent flag settings.
2143 * On big-endian machines we must be careful:
2144 * When the inode is read, the i_block array is not swapped
2145 * if the extent flag is set. Therefore if we are testing
2146 * for or fixing a wrongly-set flag, we must potentially
2147 * (un)swap before testing, or after fixing.
2151 * In this case the extents flag was set when read, so
2152 * extent_header_verify is ok. If the inode is cleared,
2153 * no need to swap... so no extra swapping here.
2155 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
2156 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2157 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
2158 if ((ext2fs_extent_header_verify(inode->i_block,
2159 sizeof(inode->i_block)) == 0) &&
2160 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
2161 e2fsck_pass1_fix_lock(ctx);
2162 ext2fs_set_feature_extents(sb);
2163 ext2fs_mark_super_dirty(fs);
2165 e2fsck_pass1_fix_unlock(ctx);
2166 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
2168 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2169 if (ino == EXT2_BAD_INO)
2170 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
2172 /* skip FINISH_INODE_LOOP */
2173 e2fsck_pass1_check_unlock(ctx);
2179 * For big-endian machines:
2180 * If the inode didn't have the extents flag set when it
2181 * was read, then the i_blocks array was swapped. To test
2182 * as an extents header, we must swap it back first.
2183 * IF we then set the extents flag, the entire i_block
2184 * array must be un/re-swapped to make it proper extents data.
2186 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
2187 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2188 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
2189 (LINUX_S_ISREG(inode->i_mode) ||
2190 LINUX_S_ISDIR(inode->i_mode))) {
2192 #ifdef WORDS_BIGENDIAN
2193 __u32 tmp_block[EXT2_N_BLOCKS];
2195 for (i = 0; i < EXT2_N_BLOCKS; i++)
2196 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
2199 ehp = inode->i_block;
2201 if ((ext2fs_extent_header_verify(ehp,
2202 sizeof(inode->i_block)) == 0) &&
2203 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
2204 inode->i_flags |= EXT4_EXTENTS_FL;
2205 #ifdef WORDS_BIGENDIAN
2206 memcpy(inode->i_block, tmp_block,
2207 sizeof(inode->i_block));
2209 e2fsck_write_inode(ctx, ino, inode, "pass1");
2214 if (ino == EXT2_BAD_INO) {
2215 struct process_block_struct pb;
2217 if ((failed_csum || inode->i_mode || inode->i_uid ||
2218 inode->i_gid || inode->i_links_count ||
2219 (inode->i_flags & EXT4_INLINE_DATA_FL) ||
2220 inode->i_file_acl) &&
2221 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
2222 memset(inode, 0, sizeof(struct ext2_inode));
2223 e2fsck_write_inode(ctx, ino, inode,
2228 e2fsck_pass1_block_map_r_lock(ctx);
2229 pctx.errcode = ext2fs_copy_bitmap(ctx->global_ctx ?
2230 ctx->global_ctx->block_found_map :
2231 ctx->block_found_map, &pb.fs_meta_blocks);
2232 e2fsck_pass1_block_map_r_unlock(ctx);
2235 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
2236 ctx->flags |= E2F_FLAG_ABORT;
2237 e2fsck_pass1_check_unlock(ctx);
2240 pb.ino = EXT2_BAD_INO;
2241 pb.num_blocks = pb.last_block = 0;
2242 pb.last_db_block = -1;
2243 pb.num_illegal_blocks = 0;
2244 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
2245 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
2249 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
2250 block_buf, process_bad_block, &pb);
2251 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
2253 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
2254 ctx->flags |= E2F_FLAG_ABORT;
2255 e2fsck_pass1_check_unlock(ctx);
2259 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
2260 ctx->flags |= E2F_FLAG_ABORT;
2261 e2fsck_pass1_check_unlock(ctx);
2264 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2265 clear_problem_context(&pctx);
2266 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2267 e2fsck_pass1_check_unlock(ctx);
2269 } else if (ino == EXT2_ROOT_INO) {
2271 * Make sure the root inode is a directory; if
2272 * not, offer to clear it. It will be
2273 * regenerated in pass #3.
2275 if (!LINUX_S_ISDIR(inode->i_mode)) {
2276 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
2280 * If dtime is set, offer to clear it. mke2fs
2281 * version 0.2b created filesystems with the
2282 * dtime field set for the root and lost+found
2283 * directories. We won't worry about
2284 * /lost+found, since that can be regenerated
2285 * easily. But we will fix the root directory
2286 * as a special case.
2288 if (inode->i_dtime && inode->i_links_count) {
2289 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
2291 e2fsck_write_inode(ctx, ino, inode,
2296 } else if (ino == EXT2_JOURNAL_INO) {
2297 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2298 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
2299 if (!LINUX_S_ISREG(inode->i_mode) &&
2300 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
2302 inode->i_mode = LINUX_S_IFREG;
2303 e2fsck_write_inode(ctx, ino, inode,
2307 check_blocks(ctx, &pctx, block_buf, NULL);
2308 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2309 e2fsck_pass1_check_unlock(ctx);
2312 if ((inode->i_links_count ||
2313 inode->i_blocks || inode->i_block[0]) &&
2314 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
2316 memset(inode, 0, inode_size);
2317 ext2fs_icount_store(ctx->inode_link_info,
2319 e2fsck_write_inode_full(ctx, ino, inode,
2320 inode_size, "pass1");
2323 } else if (quota_inum_is_reserved(fs, ino)) {
2324 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2325 if (ext2fs_has_feature_quota(fs->super) &&
2326 quota_inum_is_super(fs->super, ino)) {
2327 if (!LINUX_S_ISREG(inode->i_mode) &&
2328 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
2330 inode->i_mode = LINUX_S_IFREG;
2331 e2fsck_write_inode(ctx, ino, inode,
2335 check_blocks(ctx, &pctx, block_buf, NULL);
2336 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2337 e2fsck_pass1_check_unlock(ctx);
2340 if ((inode->i_links_count ||
2341 inode->i_blocks || inode->i_block[0]) &&
2342 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
2344 memset(inode, 0, inode_size);
2345 ext2fs_icount_store(ctx->inode_link_info,
2347 e2fsck_write_inode_full(ctx, ino, inode,
2348 inode_size, "pass1");
2351 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
2352 problem_t problem = 0;
2354 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2355 if (ino == EXT2_BOOT_LOADER_INO) {
2356 if (LINUX_S_ISDIR(inode->i_mode))
2357 problem = PR_1_RESERVED_BAD_MODE;
2358 } else if (ino == EXT2_RESIZE_INO) {
2359 if (inode->i_mode &&
2360 !LINUX_S_ISREG(inode->i_mode))
2361 problem = PR_1_RESERVED_BAD_MODE;
2363 if (inode->i_mode != 0)
2364 problem = PR_1_RESERVED_BAD_MODE;
2367 if (fix_problem(ctx, problem, &pctx)) {
2369 e2fsck_write_inode(ctx, ino, inode,
2374 check_blocks(ctx, &pctx, block_buf, NULL);
2375 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2376 e2fsck_pass1_check_unlock(ctx);
2380 if (!inode->i_links_count) {
2381 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2382 e2fsck_pass1_check_unlock(ctx);
2386 * n.b. 0.3c ext2fs code didn't clear i_links_count for
2387 * deleted files. Oops.
2389 * Since all new ext2 implementations get this right,
2390 * we now assume that the case of non-zero
2391 * i_links_count and non-zero dtime means that we
2392 * should keep the file, not delete it.
2395 if (inode->i_dtime) {
2396 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
2398 e2fsck_write_inode(ctx, ino, inode, "pass1");
2403 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2404 switch (fs->super->s_creator_os) {
2406 frag = inode->osd2.hurd2.h_i_frag;
2407 fsize = inode->osd2.hurd2.h_i_fsize;
2413 /* Fixed in pass2, e2fsck_process_bad_inode(). */
2414 if (inode->i_faddr || frag || fsize ||
2415 (!ext2fs_has_feature_largedir(fs->super) &&
2416 LINUX_S_ISDIR(inode->i_mode) && inode->i_size_high))
2417 e2fsck_mark_inode_bad(ctx, &pctx,
2418 PR_2_DIR_SIZE_HIGH_ZERO);
2419 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2420 !ext2fs_has_feature_64bit(fs->super) &&
2421 inode->osd2.linux2.l_i_file_acl_high != 0)
2422 e2fsck_mark_inode_bad(ctx, &pctx,
2423 PR_2_I_FILE_ACL_HI_ZERO);
2424 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2425 !ext2fs_has_feature_huge_file(fs->super) &&
2426 (inode->osd2.linux2.l_i_blocks_hi != 0))
2427 e2fsck_mark_inode_bad(ctx, &pctx, PR_2_BLOCKS_HI_ZERO);
2428 if (inode->i_flags & EXT2_IMAGIC_FL) {
2430 if (!ctx->inode_imagic_map)
2431 alloc_imagic_map(ctx);
2432 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
2435 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
2436 inode->i_flags &= ~EXT2_IMAGIC_FL;
2437 e2fsck_write_inode(ctx, ino,
2444 check_inode_extra_space(ctx, &pctx, &ea_ibody_quota);
2445 check_is_really_dir(ctx, &pctx, block_buf);
2448 * ext2fs_inode_has_valid_blocks2 does not actually look
2449 * at i_block[] values, so not endian-sensitive here.
2451 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
2452 LINUX_S_ISLNK(inode->i_mode) &&
2453 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
2454 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
2455 inode->i_flags &= ~EXT4_EXTENTS_FL;
2456 e2fsck_write_inode(ctx, ino, inode, "pass1");
2460 if ((inode->i_flags & EXT4_ENCRYPT_FL) &&
2461 add_encrypted_file(ctx, &pctx) < 0)
2464 if (casefold_fs && inode->i_flags & EXT4_CASEFOLD_FL)
2465 ext2fs_mark_inode_bitmap2(ctx->inode_casefold_map, ino);
2467 if (LINUX_S_ISDIR(inode->i_mode)) {
2468 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
2469 e2fsck_add_dir_info(ctx, ino, 0);
2470 ctx->fs_directory_count++;
2471 if (inode->i_flags & EXT4_CASEFOLD_FL)
2472 add_casefolded_dir(ctx, ino);
2473 } else if (LINUX_S_ISREG (inode->i_mode)) {
2474 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
2475 ctx->fs_regular_count++;
2476 } else if (LINUX_S_ISCHR (inode->i_mode) &&
2477 e2fsck_pass1_check_device_inode(fs, inode)) {
2478 check_extents_inlinedata(ctx, &pctx);
2479 check_immutable(ctx, &pctx);
2480 check_size(ctx, &pctx);
2481 ctx->fs_chardev_count++;
2482 } else if (LINUX_S_ISBLK (inode->i_mode) &&
2483 e2fsck_pass1_check_device_inode(fs, inode)) {
2484 check_extents_inlinedata(ctx, &pctx);
2485 check_immutable(ctx, &pctx);
2486 check_size(ctx, &pctx);
2487 ctx->fs_blockdev_count++;
2488 } else if (LINUX_S_ISLNK (inode->i_mode) &&
2489 e2fsck_pass1_check_symlink(fs, ino, inode,
2491 check_immutable(ctx, &pctx);
2492 ctx->fs_symlinks_count++;
2493 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
2494 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2495 e2fsck_pass1_check_unlock(ctx);
2497 } else if (ext2fs_is_fast_symlink(inode)) {
2498 ctx->fs_fast_symlinks_count++;
2499 check_blocks(ctx, &pctx, block_buf,
2501 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2502 e2fsck_pass1_check_unlock(ctx);
2506 else if (LINUX_S_ISFIFO (inode->i_mode) &&
2507 e2fsck_pass1_check_device_inode(fs, inode)) {
2508 check_extents_inlinedata(ctx, &pctx);
2509 check_immutable(ctx, &pctx);
2510 check_size(ctx, &pctx);
2511 ctx->fs_fifo_count++;
2512 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
2513 e2fsck_pass1_check_device_inode(fs, inode)) {
2514 check_extents_inlinedata(ctx, &pctx);
2515 check_immutable(ctx, &pctx);
2516 check_size(ctx, &pctx);
2517 ctx->fs_sockets_count++;
2519 e2fsck_mark_inode_bad(ctx, &pctx, PR_2_BAD_MODE);
2522 /* Future atime/mtime may be valid in rare cases, but are more
2523 * likely to indicate corruption. Don't try to fix timestamps,
2524 * but take into consideration whether inode is corrupted. If
2525 * no other problems with the inode, probably it is OK. */
2526 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
2527 e2fsck_mark_inode_bad(ctx, &pctx, PR_1_INODE_BAD_TIME);
2528 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime, ctx->time_fudge))
2529 e2fsck_mark_inode_bad(ctx, &pctx, PR_1_INODE_BAD_TIME);
2531 /* Since ctime cannot be set directly from userspace, consider
2532 * very old/future values worse than a bad atime/mtime. Same for
2533 * crtime, but it is checked in check_inode_extra_space(). */
2534 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
2535 e2fsck_mark_inode_badder(ctx, &pctx,
2536 PR_1_INODE_BAD_TIME);
2537 else if (!precreated_object(inode) &&
2538 EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
2540 e2fsck_mark_inode_badder(ctx, &pctx,
2541 PR_1_INODE_BAD_TIME);
2543 /* no restart if clearing bad inode before block processing */
2544 if (e2fsck_fix_bad_inode(ctx, &pctx)) {
2545 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2549 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2550 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
2551 if (inode->i_block[EXT2_IND_BLOCK])
2552 ctx->fs_ind_count++;
2553 if (inode->i_block[EXT2_DIND_BLOCK])
2554 ctx->fs_dind_count++;
2555 if (inode->i_block[EXT2_TIND_BLOCK])
2556 ctx->fs_tind_count++;
2558 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2559 !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
2560 (inode->i_block[EXT2_IND_BLOCK] ||
2561 inode->i_block[EXT2_DIND_BLOCK] ||
2562 inode->i_block[EXT2_TIND_BLOCK] ||
2563 ext2fs_file_acl_block(fs, inode))) {
2564 struct process_inode_block *itp;
2566 itp = &inodes_to_process[process_inode_count];
2568 itp->ea_ibody_quota = ea_ibody_quota;
2569 if (inode_size < sizeof(struct ext2_inode_large))
2570 memcpy(&itp->inode, inode, inode_size);
2572 memcpy(&itp->inode, inode, sizeof(itp->inode));
2573 process_inode_count++;
2575 check_blocks(ctx, &pctx, block_buf, &ea_ibody_quota);
2577 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2579 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
2580 struct ext2_inode_large *inode_l;
2582 inode_l = (struct ext2_inode_large *)inode;
2584 if (inode_l->i_extra_isize < ctx->want_extra_isize) {
2585 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
2586 inode_exp = e2fsck_pass1_expand_eisize(ctx,
2590 if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
2592 ctx->min_extra_isize = inode_l->i_extra_isize;
2595 if (e2fsck_should_abort(ctx)) {
2596 e2fsck_pass1_check_unlock(ctx);
2600 if (process_inode_count >= ctx->process_inode_size) {
2601 process_inodes(ctx, block_buf, inodes_to_process,
2602 &process_inode_count);
2604 if (e2fsck_should_abort(ctx)) {
2605 e2fsck_pass1_check_unlock(ctx);
2610 e2fsck_pass1_check_unlock(ctx);
2612 process_inodes(ctx, block_buf, inodes_to_process,
2613 &process_inode_count);
2614 ext2fs_close_inode_scan(scan);
2617 if (ctx->ea_block_quota_blocks) {
2618 ea_refcount_free(ctx->ea_block_quota_blocks);
2619 ctx->ea_block_quota_blocks = 0;
2622 if (ctx->ea_block_quota_inodes) {
2623 ea_refcount_free(ctx->ea_block_quota_inodes);
2624 ctx->ea_block_quota_inodes = 0;
2627 /* We don't need the encryption policy => ID map any more */
2628 destroy_encryption_policy_map(ctx);
2630 if (ctx->flags & E2F_FLAG_RESTART) {
2632 * Only the master copy of the superblock and block
2633 * group descriptors are going to be written during a
2634 * restart, so set the superblock to be used to be the
2635 * master superblock.
2637 ctx->use_superblock = 0;
2641 if (ctx->large_dirs && !ext2fs_has_feature_largedir(ctx->fs->super)) {
2642 ext2_filsys fs = ctx->fs;
2644 if (fix_problem(ctx, PR_2_FEATURE_LARGE_DIRS, &pctx)) {
2645 ext2fs_set_feature_largedir(fs->super);
2646 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2647 ext2fs_mark_super_dirty(fs);
2649 if (fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
2650 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
2651 ext2fs_update_dynamic_rev(fs);
2652 ext2fs_mark_super_dirty(fs);
2656 ctx->flags |= E2F_FLAG_ALLOC_OK;
2657 ext2fs_free_mem(&inodes_to_process);
2659 e2fsck_use_inode_shortcuts(ctx, 0);
2660 ext2fs_free_mem(&inodes_to_process);
2661 inodes_to_process = 0;
2664 ext2fs_close_inode_scan(scan);
2666 ext2fs_free_mem(&block_buf);
2668 ext2fs_free_mem(&inode);
2671 * The l+f inode may have been cleared, so zap it now and
2672 * later passes will recalculate it if necessary
2674 ctx->lost_and_found = 0;
2676 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
2677 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
2679 ctx->invalid_bitmaps++;
2681 /* reset update_thread after this thread exit */
2682 e2fsck_pass1_block_map_w_lock(ctx);
2684 global_ctx->mmp_update_thread = 0;
2685 e2fsck_pass1_block_map_w_unlock(ctx);
2690 static errcode_t e2fsck_pass1_copy_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2691 ext2fs_generic_bitmap *dest)
2695 ret = ext2fs_copy_bitmap(*src, dest);
2704 static void e2fsck_pass1_free_bitmap(ext2fs_generic_bitmap *bitmap)
2707 ext2fs_free_generic_bmap(*bitmap);
2713 static errcode_t e2fsck_pass1_merge_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2714 ext2fs_generic_bitmap *dest)
2719 if (*dest == NULL) {
2723 ret = ext2fs_merge_bitmap(*src, *dest, NULL, NULL);
2733 static errcode_t e2fsck_pass1_copy_fs(ext2_filsys dest, e2fsck_t src_context,
2738 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2739 dest->inode_map = NULL;
2740 dest->block_map = NULL;
2741 dest->badblocks = NULL;
2743 dest->dblist->fs = dest;
2744 if (src->block_map) {
2745 retval = e2fsck_pass1_copy_bitmap(dest, &src->block_map,
2750 if (src->inode_map) {
2751 retval = e2fsck_pass1_copy_bitmap(dest, &src->inode_map,
2757 if (src->badblocks) {
2758 retval = ext2fs_badblocks_copy(src->badblocks,
2764 /* disable it for now */
2765 src_context->openfs_flags &= ~EXT2_FLAG_EXCLUSIVE;
2766 retval = ext2fs_open_channel(dest, src_context->io_options,
2767 src_context->io_manager,
2768 src_context->openfs_flags,
2769 src->io->block_size);
2773 /* Block size might not be default */
2774 io_channel_set_blksize(dest->io, src->io->block_size);
2775 ehandler_init(dest->io);
2777 assert(dest->io->magic == src->io->magic);
2778 assert(dest->io->manager == src->io->manager);
2779 assert(strcmp(dest->io->name, src->io->name) == 0);
2780 assert(dest->io->block_size == src->io->block_size);
2781 assert(dest->io->read_error == src->io->read_error);
2782 assert(dest->io->write_error == src->io->write_error);
2783 assert(dest->io->refcount == src->io->refcount);
2784 assert(dest->io->flags == src->io->flags);
2785 assert(dest->io->app_data == dest);
2786 assert(src->io->app_data == src);
2787 assert(dest->io->align == src->io->align);
2789 /* The data should be written to disk immediately */
2790 dest->io->flags |= CHANNEL_FLAGS_WRITETHROUGH;
2791 /* icache will be rebuilt if needed, so do not copy from @src */
2796 static int e2fsck_pass1_merge_fs(ext2_filsys dest, ext2_filsys src)
2798 struct ext2_inode_cache *icache = dest->icache;
2799 errcode_t retval = 0;
2801 io_channel dest_image_io;
2802 ext2fs_inode_bitmap inode_map;
2803 ext2fs_block_bitmap block_map;
2804 ext2_badblocks_list badblocks;
2807 e2fsck_t dest_ctx = dest->priv_data;
2810 dest_image_io = dest->image_io;
2811 inode_map = dest->inode_map;
2812 block_map = dest->block_map;
2813 badblocks = dest->badblocks;
2814 dblist = dest->dblist;
2815 flags = dest->flags;
2817 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2819 dest->image_io = dest_image_io;
2820 dest->icache = icache;
2821 dest->inode_map = inode_map;
2822 dest->block_map = block_map;
2823 dest->badblocks = badblocks;
2824 dest->dblist = dblist;
2825 dest->priv_data = dest_ctx;
2827 dest->dblist->fs = dest;
2828 dest->flags = src->flags | flags;
2829 if (!(src->flags & EXT2_FLAG_VALID) || !(flags & EXT2_FLAG_VALID))
2830 ext2fs_unmark_valid(dest);
2833 ext2fs_free_inode_cache(src->icache);
2837 retval = e2fsck_pass1_merge_bitmap(dest, &src->inode_map,
2842 retval = e2fsck_pass1_merge_bitmap(dest, &src->block_map,
2849 retval = ext2fs_merge_dblist(src->dblist,
2854 dest->dblist = src->dblist;
2855 dest->dblist->fs = dest;
2860 if (src->badblocks) {
2861 if (dest->badblocks == NULL)
2862 retval = ext2fs_badblocks_copy(src->badblocks,
2865 retval = ext2fs_badblocks_merge(src->badblocks,
2869 io_channel_close(src->io);
2871 ext2fs_free_generic_bmap(src->inode_map);
2873 ext2fs_free_generic_bmap(src->block_map);
2875 ext2fs_badblocks_list_free(src->badblocks);
2877 ext2fs_free_dblist(src->dblist);
2882 static void e2fsck_pass1_copy_invalid_bitmaps(e2fsck_t global_ctx,
2883 e2fsck_t thread_ctx)
2886 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2887 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2888 dgrp_t total = grp_end - grp_start;
2890 thread_ctx->invalid_inode_bitmap_flag =
2891 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2892 "invalid_inode_bitmap");
2893 thread_ctx->invalid_block_bitmap_flag =
2894 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2895 "invalid_block_bitmap");
2896 thread_ctx->invalid_inode_table_flag =
2897 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2898 "invalid_inode_table");
2900 memcpy(thread_ctx->invalid_block_bitmap_flag,
2901 &global_ctx->invalid_block_bitmap_flag[grp_start],
2902 total * sizeof(int));
2903 memcpy(thread_ctx->invalid_inode_bitmap_flag,
2904 &global_ctx->invalid_inode_bitmap_flag[grp_start],
2905 total * sizeof(int));
2906 memcpy(thread_ctx->invalid_inode_table_flag,
2907 &global_ctx->invalid_inode_table_flag[grp_start],
2908 total * sizeof(int));
2910 thread_ctx->invalid_bitmaps = 0;
2911 for (i = grp_start, j = 0; i < grp_end; i++, j++) {
2912 if (thread_ctx->invalid_block_bitmap_flag[j])
2913 thread_ctx->invalid_bitmaps++;
2914 if (thread_ctx->invalid_inode_bitmap_flag[j])
2915 thread_ctx->invalid_bitmaps++;
2916 if (thread_ctx->invalid_inode_table_flag[j])
2917 thread_ctx->invalid_bitmaps++;
2921 static void e2fsck_pass1_merge_invalid_bitmaps(e2fsck_t global_ctx,
2922 e2fsck_t thread_ctx)
2925 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2926 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2927 dgrp_t total = grp_end - grp_start;
2929 memcpy(&global_ctx->invalid_block_bitmap_flag[grp_start],
2930 thread_ctx->invalid_block_bitmap_flag, total * sizeof(int));
2931 memcpy(&global_ctx->invalid_inode_bitmap_flag[grp_start],
2932 thread_ctx->invalid_inode_bitmap_flag, total * sizeof(int));
2933 memcpy(&global_ctx->invalid_inode_table_flag[grp_start],
2934 thread_ctx->invalid_inode_table_flag, total * sizeof(int));
2935 global_ctx->invalid_bitmaps += thread_ctx->invalid_bitmaps;
2938 static errcode_t e2fsck_pass1_thread_prepare(e2fsck_t global_ctx, e2fsck_t *thread_ctx,
2939 int thread_index, int num_threads,
2940 dgrp_t average_group)
2943 e2fsck_t thread_context;
2944 ext2_filsys thread_fs;
2945 ext2_filsys global_fs = global_ctx->fs;
2946 struct e2fsck_thread *tinfo;
2948 assert(global_ctx->inode_used_map == NULL);
2949 assert(global_ctx->inode_dir_map == NULL);
2950 assert(global_ctx->inode_bb_map == NULL);
2951 assert(global_ctx->inode_imagic_map == NULL);
2952 assert(global_ctx->inode_reg_map == NULL);
2953 assert(global_ctx->inodes_to_rebuild == NULL);
2955 assert(global_ctx->block_found_map != NULL);
2956 assert(global_ctx->block_metadata_map != NULL);
2957 assert(global_ctx->block_dup_map != NULL);
2958 assert(global_ctx->block_ea_map == NULL);
2959 assert(global_ctx->fs->dblist == NULL);
2961 retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &thread_context);
2963 com_err(global_ctx->program_name, retval, "while allocating memory");
2966 memcpy(thread_context, global_ctx, sizeof(struct e2fsck_struct));
2967 thread_context->block_dup_map = NULL;
2969 retval = e2fsck_allocate_block_bitmap(global_ctx->fs,
2970 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
2971 "block_found_map", &thread_context->block_found_map);
2975 thread_context->global_ctx = global_ctx;
2976 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &thread_fs);
2978 com_err(global_ctx->program_name, retval, "while allocating memory");
2982 io_channel_flush_cleanup(global_fs->io);
2983 retval = e2fsck_pass1_copy_fs(thread_fs, global_ctx, global_fs);
2985 com_err(global_ctx->program_name, retval, "while copying fs");
2988 thread_fs->priv_data = thread_context;
2990 thread_context->thread_info.et_thread_index = thread_index;
2991 set_up_logging(thread_context);
2993 tinfo = &thread_context->thread_info;
2994 tinfo->et_group_start = average_group * thread_index;
2995 if (thread_index == global_fs->fs_num_threads - 1)
2996 tinfo->et_group_end = thread_fs->group_desc_count;
2998 tinfo->et_group_end = average_group * (thread_index + 1);
2999 tinfo->et_group_next = tinfo->et_group_start;
3000 tinfo->et_inode_number = 0;
3001 tinfo->et_log_buf[0] = '\0';
3002 tinfo->et_log_length = 0;
3003 if (thread_context->options & E2F_OPT_MULTITHREAD)
3004 log_out(thread_context, _("Scan group range [%d, %d)\n"),
3005 tinfo->et_group_start, tinfo->et_group_end);
3006 thread_context->fs = thread_fs;
3007 retval = quota_init_context(&thread_context->qctx, thread_fs, 0);
3009 com_err(global_ctx->program_name, retval,
3010 "while init quota context");
3013 *thread_ctx = thread_context;
3014 e2fsck_pass1_copy_invalid_bitmaps(global_ctx, thread_context);
3017 ext2fs_free_mem(&thread_fs);
3019 if (thread_context->block_found_map)
3020 ext2fs_free_mem(&thread_context->block_found_map);
3021 ext2fs_free_mem(&thread_context);
3025 static void e2fsck_pass1_merge_dir_info(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3027 if (thread_ctx->dir_info == NULL)
3030 if (global_ctx->dir_info == NULL) {
3031 global_ctx->dir_info = thread_ctx->dir_info;
3032 thread_ctx->dir_info = NULL;
3036 e2fsck_merge_dir_info(global_ctx, thread_ctx->dir_info,
3037 global_ctx->dir_info);
3040 static void e2fsck_pass1_merge_dx_dir(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3042 if (thread_ctx->dx_dir_info == NULL)
3045 if (global_ctx->dx_dir_info == NULL) {
3046 global_ctx->dx_dir_info = thread_ctx->dx_dir_info;
3047 global_ctx->dx_dir_info_size = thread_ctx->dx_dir_info_size;
3048 global_ctx->dx_dir_info_count = thread_ctx->dx_dir_info_count;
3049 thread_ctx->dx_dir_info = NULL;
3053 e2fsck_merge_dx_dir(global_ctx, thread_ctx);
3056 static inline errcode_t
3057 e2fsck_pass1_merge_icount(ext2_icount_t *dest_icount,
3058 ext2_icount_t *src_icount)
3061 if (*dest_icount == NULL) {
3062 *dest_icount = *src_icount;
3067 ret = ext2fs_icount_merge(*src_icount,
3077 static errcode_t e2fsck_pass1_merge_icounts(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3081 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_count,
3082 &thread_ctx->inode_count);
3085 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_link_info,
3086 &thread_ctx->inode_link_info);
3090 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_badness,
3091 &thread_ctx->inode_badness);
3096 static errcode_t e2fsck_pass1_merge_dirs_to_hash(e2fsck_t global_ctx,
3097 e2fsck_t thread_ctx)
3099 errcode_t retval = 0;
3101 if (!thread_ctx->dirs_to_hash)
3104 if (!global_ctx->dirs_to_hash)
3105 retval = ext2fs_badblocks_copy(thread_ctx->dirs_to_hash,
3106 &global_ctx->dirs_to_hash);
3108 retval = ext2fs_badblocks_merge(thread_ctx->dirs_to_hash,
3109 global_ctx->dirs_to_hash);
3114 static errcode_t e2fsck_pass1_merge_ea_inode_refs(e2fsck_t global_ctx,
3115 e2fsck_t thread_ctx)
3121 if (!thread_ctx->ea_inode_refs)
3124 if (!global_ctx->ea_inode_refs) {
3125 global_ctx->ea_inode_refs = thread_ctx->ea_inode_refs;
3126 thread_ctx->ea_inode_refs = NULL;
3130 ea_refcount_intr_begin(thread_ctx->ea_inode_refs);
3132 if ((blk = ea_refcount_intr_next(thread_ctx->ea_inode_refs,
3135 if (!global_ctx->block_ea_map ||
3136 !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
3138 retval = ea_refcount_store(global_ctx->ea_inode_refs,
3148 static ea_value_t ea_refcount_usage(e2fsck_t ctx, blk64_t blk,
3151 ea_value_t count_cur;
3152 ea_value_t count_extra = 0;
3153 ea_value_t count_orig;
3155 ea_refcount_fetch(ctx->refcount_orig, blk, &count_orig);
3156 ea_refcount_fetch(ctx->refcount, blk, &count_cur);
3157 /* most of time this is not needed */
3158 if (ctx->refcount_extra && count_cur == 0)
3159 ea_refcount_fetch(ctx->refcount_extra, blk, &count_extra);
3166 return count_orig + count_extra - count_cur;
3169 static errcode_t e2fsck_pass1_merge_ea_refcount(e2fsck_t global_ctx,
3170 e2fsck_t thread_ctx)
3174 errcode_t retval = 0;
3176 if (!thread_ctx->refcount)
3179 if (!global_ctx->refcount) {
3180 global_ctx->refcount = thread_ctx->refcount;
3181 thread_ctx->refcount = NULL;
3182 global_ctx->refcount_extra = thread_ctx->refcount;
3183 thread_ctx->refcount_extra = NULL;
3187 ea_refcount_intr_begin(thread_ctx->refcount);
3189 if ((blk = ea_refcount_intr_next(thread_ctx->refcount,
3193 * this EA has never seen before, so just store its
3194 * refcount and refcount_extra into global_ctx if needed.
3196 if (!global_ctx->block_ea_map ||
3197 !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
3201 retval = ea_refcount_store(global_ctx->refcount,
3206 if (count > 0 || !thread_ctx->refcount_extra)
3208 ea_refcount_fetch(thread_ctx->refcount_extra, blk,
3213 if (!global_ctx->refcount_extra) {
3214 retval = ea_refcount_create(0,
3215 &global_ctx->refcount_extra);
3219 retval = ea_refcount_store(global_ctx->refcount_extra,
3225 ea_value_t thread_usage;
3226 ea_value_t global_usage;
3229 thread_usage = ea_refcount_usage(thread_ctx,
3231 global_usage = ea_refcount_usage(global_ctx,
3233 if (thread_usage + global_usage <= orig) {
3234 new = orig - thread_usage - global_usage;
3235 retval = ea_refcount_store(global_ctx->refcount,
3241 /* update it is as zero */
3242 retval = ea_refcount_store(global_ctx->refcount,
3246 /* Ooops, this EA was referenced more than it stated */
3247 if (!global_ctx->refcount_extra) {
3248 retval = ea_refcount_create(0,
3249 &global_ctx->refcount_extra);
3253 new = global_usage + thread_usage - orig;
3254 retval = ea_refcount_store(global_ctx->refcount_extra,
3264 static errcode_t e2fsck_pass1_merge_context(e2fsck_t global_ctx,
3265 e2fsck_t thread_ctx)
3267 ext2_filsys global_fs = global_ctx->fs;
3271 global_ctx->fs_directory_count += thread_ctx->fs_directory_count;
3272 global_ctx->fs_regular_count += thread_ctx->fs_regular_count;
3273 global_ctx->fs_blockdev_count += thread_ctx->fs_blockdev_count;
3274 global_ctx->fs_chardev_count += thread_ctx->fs_chardev_count;
3275 global_ctx->fs_links_count += thread_ctx->fs_links_count;
3276 global_ctx->fs_symlinks_count += thread_ctx->fs_symlinks_count;
3277 global_ctx->fs_fast_symlinks_count += thread_ctx->fs_fast_symlinks_count;
3278 global_ctx->fs_fifo_count += thread_ctx->fs_fifo_count;
3279 global_ctx->fs_total_count += thread_ctx->fs_total_count;
3280 global_ctx->fs_badblocks_count += thread_ctx->fs_badblocks_count;
3281 global_ctx->fs_sockets_count += thread_ctx->fs_sockets_count;
3282 global_ctx->fs_ind_count += thread_ctx->fs_ind_count;
3283 global_ctx->fs_dind_count += thread_ctx->fs_dind_count;
3284 global_ctx->fs_tind_count += thread_ctx->fs_tind_count;
3285 global_ctx->fs_fragmented += thread_ctx->fs_fragmented;
3286 global_ctx->fs_fragmented_dir += thread_ctx->fs_fragmented_dir;
3287 global_ctx->large_files += thread_ctx->large_files;
3288 /* threads might enable E2F_OPT_YES */
3289 global_ctx->options |= thread_ctx->options;
3290 global_ctx->flags |= thread_ctx->flags;
3292 * The l+f inode may have been cleared, so zap it now and
3293 * later passes will recalculate it if necessary
3295 global_ctx->lost_and_found = 0;
3296 /* merge extent depth count */
3297 for (i = 0; i < MAX_EXTENT_DEPTH_COUNT; i++)
3298 global_ctx->extent_depth_count[i] +=
3299 thread_ctx->extent_depth_count[i];
3301 e2fsck_pass1_merge_dir_info(global_ctx, thread_ctx);
3302 e2fsck_pass1_merge_dx_dir(global_ctx, thread_ctx);
3304 retval = e2fsck_pass1_merge_fs(global_ctx->fs, thread_ctx->fs);
3306 com_err(global_ctx->program_name, 0, _("while merging fs\n"));
3309 retval = e2fsck_pass1_merge_icounts(global_ctx, thread_ctx);
3311 com_err(global_ctx->program_name, 0,
3312 _("while merging icounts\n"));
3316 retval = e2fsck_pass1_merge_dirs_to_hash(global_ctx, thread_ctx);
3318 com_err(global_ctx->program_name, 0,
3319 _("while merging dirs to hash\n"));
3323 e2fsck_pass1_merge_ea_inode_refs(global_ctx, thread_ctx);
3324 e2fsck_pass1_merge_ea_refcount(global_ctx, thread_ctx);
3325 retval = quota_merge_and_update_usage(global_ctx->qctx,
3330 e2fsck_pass1_merge_invalid_bitmaps(global_ctx, thread_ctx);
3332 retval = e2fsck_pass1_merge_bitmap(global_fs,
3333 &thread_ctx->inode_used_map,
3334 &global_ctx->inode_used_map);
3338 retval = e2fsck_pass1_merge_bitmap(global_fs,
3339 &thread_ctx->inode_dir_map,
3340 &global_ctx->inode_dir_map);
3343 retval = e2fsck_pass1_merge_bitmap(global_fs,
3344 &thread_ctx->inode_bb_map,
3345 &global_ctx->inode_bb_map);
3348 retval = e2fsck_pass1_merge_bitmap(global_fs,
3349 &thread_ctx->inode_imagic_map,
3350 &global_ctx->inode_imagic_map);
3353 retval = e2fsck_pass1_merge_bitmap(global_fs,
3354 &thread_ctx->inode_reg_map,
3355 &global_ctx->inode_reg_map);
3358 retval = e2fsck_pass1_merge_bitmap(global_fs,
3359 &thread_ctx->inodes_to_rebuild,
3360 &global_ctx->inodes_to_rebuild);
3363 retval = e2fsck_pass1_merge_bitmap(global_fs,
3364 &thread_ctx->block_ea_map,
3365 &global_ctx->block_ea_map);
3369 if (ext2fs_has_feature_shared_blocks(global_fs->super) &&
3370 !(global_ctx->options & E2F_OPT_UNSHARE_BLOCKS))
3373 * This need be done after merging block_ea_map
3374 * because ea block might be shared, we need exclude
3375 * them from dup blocks.
3377 e2fsck_pass1_block_map_w_lock(thread_ctx);
3378 retval = ext2fs_merge_bitmap(thread_ctx->block_found_map,
3379 global_ctx->block_found_map,
3380 global_ctx->block_dup_map,
3381 global_ctx->block_ea_map);
3382 e2fsck_pass1_block_map_w_unlock(thread_ctx);
3383 if (retval == EEXIST)
3384 global_ctx->flags |= E2F_FLAG_DUP_BLOCK;
3389 static int e2fsck_pass1_thread_join(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3393 retval = e2fsck_pass1_merge_context(global_ctx, thread_ctx);
3394 ext2fs_free_mem(&thread_ctx->fs);
3395 if (thread_ctx->logf)
3396 fclose(thread_ctx->logf);
3397 if (thread_ctx->problem_logf) {
3398 fputs("</problem_log>\n", thread_ctx->problem_logf);
3399 fclose(thread_ctx->problem_logf);
3402 quota_release_context(&thread_ctx->qctx);
3404 * @block_metadata_map and @block_dup_map are
3405 * shared, so we don't free them.
3407 thread_ctx->block_metadata_map = NULL;
3408 thread_ctx->block_dup_map = NULL;
3409 e2fsck_reset_context(thread_ctx);
3410 ext2fs_free_mem(&thread_ctx);
3415 static int e2fsck_pass1_threads_join(struct e2fsck_thread_info *infos,
3416 e2fsck_t global_ctx)
3420 struct e2fsck_thread_info *pinfo;
3421 int num_threads = global_ctx->pfs_num_threads;
3424 /* merge invalid bitmaps will recalculate it */
3425 global_ctx->invalid_bitmaps = 0;
3426 for (i = 0; i < num_threads; i++) {
3429 if (!pinfo->eti_started)
3432 rc = pthread_join(pinfo->eti_thread_id, NULL);
3434 com_err(global_ctx->program_name, rc,
3435 _("while joining thread\n"));
3439 rc = e2fsck_pass1_thread_join(global_ctx, infos[i].eti_thread_ctx);
3441 com_err(global_ctx->program_name, rc,
3442 _("while joining pass1 thread\n"));
3452 static void *e2fsck_pass1_thread(void *arg)
3454 struct e2fsck_thread_info *info = arg;
3455 e2fsck_t thread_ctx = info->eti_thread_ctx;
3456 #ifdef DEBUG_THREADS
3457 struct e2fsck_thread_debug *thread_debug = info->eti_debug;
3460 #ifdef DEBUG_THREADS
3461 pthread_mutex_lock(&thread_debug->etd_mutex);
3462 while (info->eti_thread_index > thread_debug->etd_finished_threads) {
3463 pthread_cond_wait(&thread_debug->etd_cond,
3464 &thread_debug->etd_mutex);
3466 pthread_mutex_unlock(&thread_debug->etd_mutex);
3469 #ifdef HAVE_SETJMP_H
3471 * When fatal_error() happens, jump to here. The thread
3472 * context's flags will be saved, but its abort_loc will
3473 * be overwritten by original jump buffer for the later
3476 if (setjmp(thread_ctx->abort_loc)) {
3477 thread_ctx->flags &= ~E2F_FLAG_SETJMP_OK;
3480 thread_ctx->flags |= E2F_FLAG_SETJMP_OK;
3483 e2fsck_pass1_run(thread_ctx);
3486 if (thread_ctx->options & E2F_OPT_MULTITHREAD)
3488 _("Scanned group range [%u, %u), inodes %u\n"),
3489 thread_ctx->thread_info.et_group_start,
3490 thread_ctx->thread_info.et_group_end,
3491 thread_ctx->thread_info.et_inode_number);
3493 #ifdef DEBUG_THREADS
3494 pthread_mutex_lock(&thread_debug->etd_mutex);
3495 thread_debug->etd_finished_threads++;
3496 pthread_cond_broadcast(&thread_debug->etd_cond);
3497 pthread_mutex_unlock(&thread_debug->etd_mutex);
3503 static int e2fsck_pass1_threads_start(struct e2fsck_thread_info **pinfo,
3504 e2fsck_t global_ctx)
3506 struct e2fsck_thread_info *infos;
3507 pthread_attr_t attr;
3510 struct e2fsck_thread_info *tmp_pinfo;
3512 e2fsck_t thread_ctx;
3513 dgrp_t average_group;
3514 int num_threads = global_ctx->pfs_num_threads;
3515 #ifdef DEBUG_THREADS
3516 struct e2fsck_thread_debug thread_debug =
3517 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0};
3519 thread_debug.etd_finished_threads = 0;
3522 retval = pthread_attr_init(&attr);
3524 com_err(global_ctx->program_name, retval,
3525 _("while setting pthread attribute\n"));
3529 infos = calloc(num_threads, sizeof(struct e2fsck_thread_info));
3530 if (infos == NULL) {
3532 com_err(global_ctx->program_name, retval,
3533 _("while allocating memory for threads\n"));
3534 pthread_attr_destroy(&attr);
3538 average_group = ext2fs_get_avg_group(global_ctx->fs);
3539 for (i = 0; i < num_threads; i++) {
3540 tmp_pinfo = &infos[i];
3541 tmp_pinfo->eti_thread_index = i;
3542 #ifdef DEBUG_THREADS
3543 tmp_pinfo->eti_debug = &thread_debug;
3545 retval = e2fsck_pass1_thread_prepare(global_ctx, &thread_ctx,
3549 com_err(global_ctx->program_name, retval,
3550 _("while preparing pass1 thread\n"));
3553 tmp_pinfo->eti_thread_ctx = thread_ctx;
3555 retval = pthread_create(&tmp_pinfo->eti_thread_id, &attr,
3556 &e2fsck_pass1_thread, tmp_pinfo);
3558 com_err(global_ctx->program_name, retval,
3559 _("while creating thread\n"));
3560 e2fsck_pass1_thread_join(global_ctx, thread_ctx);
3564 tmp_pinfo->eti_started = 1;
3567 /* destroy the thread attribute object, since it is no longer needed */
3568 ret = pthread_attr_destroy(&attr);
3570 com_err(global_ctx->program_name, ret,
3571 _("while destroying thread attribute\n"));
3577 e2fsck_pass1_threads_join(infos, global_ctx);
3584 static void e2fsck_pass1_multithread(e2fsck_t global_ctx)
3586 struct e2fsck_thread_info *infos = NULL;
3589 retval = e2fsck_pass1_threads_start(&infos, global_ctx);
3591 com_err(global_ctx->program_name, retval,
3592 _("while starting pass1 threads\n"));
3596 retval = e2fsck_pass1_threads_join(infos, global_ctx);
3598 com_err(global_ctx->program_name, retval,
3599 _("while joining pass1 threads\n"));
3604 global_ctx->flags |= E2F_FLAG_ABORT;
3609 void e2fsck_pass1(e2fsck_t ctx)
3612 int need_single = 1;
3614 retval = e2fsck_pass1_prepare(ctx);
3618 if (ctx->pfs_num_threads > 1 || ctx->options & E2F_OPT_MULTITHREAD) {
3620 e2fsck_pass1_multithread(ctx);
3622 /* No lock is needed at this time */
3623 ctx->fs_need_locking = 0;
3626 e2fsck_pass1_run(ctx);
3627 e2fsck_pass1_post(ctx);
3630 #undef FINISH_INODE_LOOP
3633 * When the inode_scan routines call this callback at the end of the
3634 * glock group, call process_inodes.
3636 static errcode_t scan_callback(ext2_filsys fs,
3637 ext2_inode_scan scan EXT2FS_ATTR((unused)),
3638 dgrp_t group, void * priv_data)
3640 struct scan_callback_struct *scan_struct;
3642 struct e2fsck_thread *tinfo;
3644 scan_struct = (struct scan_callback_struct *) priv_data;
3645 ctx = scan_struct->ctx;
3647 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf,
3648 scan_struct->inodes_to_process,
3649 scan_struct->process_inode_count);
3652 if ((ctx->progress)(ctx, 1, group+1,
3653 ctx->fs->group_desc_count))
3654 return EXT2_ET_CANCEL_REQUESTED;
3657 if (ctx->global_ctx) {
3658 tinfo = &ctx->thread_info;
3659 tinfo->et_group_next++;
3660 if (ctx->options & E2F_OPT_DEBUG &&
3661 ctx->options & E2F_OPT_MULTITHREAD)
3662 log_out(ctx, _("group %d finished\n"),
3663 tinfo->et_group_next);
3664 if (tinfo->et_group_next >= tinfo->et_group_end)
3665 return EXT2_ET_SCAN_FINISHED;
3673 * Process the inodes in the "inodes to process" list.
3675 static void process_inodes(e2fsck_t ctx, char *block_buf,
3676 struct process_inode_block *inodes_to_process,
3677 int *process_inode_count)
3680 struct ext2_inode *old_stashed_inode;
3681 ext2_ino_t old_stashed_ino;
3682 const char *old_operation;
3684 struct problem_context pctx;
3687 printf("begin process_inodes: ");
3689 if (*process_inode_count == 0)
3691 old_operation = ehandler_operation(0);
3692 old_stashed_inode = ctx->stashed_inode;
3693 old_stashed_ino = ctx->stashed_ino;
3694 qsort(inodes_to_process, *process_inode_count,
3695 sizeof(struct process_inode_block), process_inode_cmp);
3696 clear_problem_context(&pctx);
3697 for (i=0; i < *process_inode_count; i++) {
3698 pctx.inode = ctx->stashed_inode =
3699 (struct ext2_inode *) &inodes_to_process[i].inode;
3700 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
3703 printf("%u ", pctx.ino);
3705 sprintf(buf, _("reading indirect blocks of inode %u"),
3707 ehandler_operation(buf);
3708 check_blocks(ctx, &pctx, block_buf,
3709 &inodes_to_process[i].ea_ibody_quota);
3710 if (e2fsck_should_abort(ctx))
3713 ctx->stashed_inode = old_stashed_inode;
3714 ctx->stashed_ino = old_stashed_ino;
3715 *process_inode_count = 0;
3717 printf("end process inodes\n");
3719 ehandler_operation(old_operation);
3722 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
3724 const struct process_inode_block *ib_a =
3725 (const struct process_inode_block *) a;
3726 const struct process_inode_block *ib_b =
3727 (const struct process_inode_block *) b;
3730 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
3731 ib_b->inode.i_block[EXT2_IND_BLOCK]);
3734 * We only call process_inodes() for non-extent
3735 * inodes, so it's OK to pass NULL to
3736 * ext2fs_file_acl_block() here.
3738 ret = ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a->inode)) -
3739 ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b->inode));
3741 ret = ib_a->ino - ib_b->ino;
3746 * Mark an inode as being bad and increment its badness counter.
3748 void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, struct problem_context *pctx,
3749 __u32 code, int badness, const char *func,
3752 __u16 badness_before, badness_after;
3753 __u64 pctx_num_sav = pctx->num;
3755 if (!ctx->inode_badness_threshold) /* badness is disabled */
3758 if (!ctx->inode_badness) {
3761 retval = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
3762 &ctx->inode_badness);
3764 pctx->errcode = retval;
3765 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, pctx);
3766 ctx->flags |= E2F_FLAG_ABORT;
3770 ext2fs_icount_fetch(ctx->inode_badness, pctx->ino, &badness_before);
3771 if (badness + badness_before > BADNESS_MAX)
3772 badness_after = BADNESS_MAX;
3773 else if (badness < 0 && badness_before < -badness)
3776 badness_after = badness_before + badness;
3777 ext2fs_icount_store(ctx->inode_badness, pctx->ino, badness_after);
3779 if (ctx->options & E2F_OPT_DEBUG)
3781 "%s:%d: increase inode %lu badness %u to %u for %x\n",
3782 func, line, (unsigned long)pctx->ino, badness_before,
3783 badness_after, code);
3786 static void add_casefolded_dir(e2fsck_t ctx, ino_t ino)
3788 struct problem_context pctx;
3790 if (!ctx->casefolded_dirs) {
3791 pctx.errcode = ext2fs_u32_list_create(&ctx->casefolded_dirs, 0);
3795 pctx.errcode = ext2fs_u32_list_add(ctx->casefolded_dirs, ino);
3796 if (pctx.errcode == 0)
3799 fix_problem(ctx, PR_1_ALLOCATE_CASEFOLDED_DIRLIST, &pctx);
3800 /* Should never get here */
3801 ctx->flags |= E2F_FLAG_ABORT;
3805 * This procedure will allocate the inode "bb" (badblock) map table
3807 static void alloc_bb_map(e2fsck_t ctx)
3809 struct problem_context pctx;
3811 clear_problem_context(&pctx);
3812 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3813 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
3814 "inode_bb_map", &ctx->inode_bb_map);
3817 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3818 /* Should never get here */
3819 ctx->flags |= E2F_FLAG_ABORT;
3825 * This procedure will allocate the inode imagic table
3827 static void alloc_imagic_map(e2fsck_t ctx)
3829 struct problem_context pctx;
3831 clear_problem_context(&pctx);
3832 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3833 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
3834 "inode_imagic_map", &ctx->inode_imagic_map);
3837 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3838 /* Should never get here */
3839 ctx->flags |= E2F_FLAG_ABORT;
3845 * Marks a block as in use, setting the dup_map if it's been set
3846 * already. Called by process_block and process_bad_block.
3848 * WARNING: Assumes checks have already been done to make sure block
3849 * is valid. This is true in both process_block and process_bad_block.
3851 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
3853 struct problem_context pctx;
3854 e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
3856 clear_problem_context(&pctx);
3858 if (is_blocks_used(ctx, block, 1)) {
3859 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
3860 !(ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
3863 ctx->flags |= E2F_FLAG_DUP_BLOCK;
3864 e2fsck_pass1_block_map_w_lock(ctx);
3865 ext2fs_fast_mark_block_bitmap2(global_ctx->block_dup_map, block);
3866 e2fsck_pass1_block_map_w_unlock(ctx);
3868 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
3873 * When cluster size is greater than one block, it is caller's responsibility
3874 * to make sure block parameter starts at a cluster boundary.
3876 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
3879 if (!is_blocks_used(ctx, block, num)) {
3880 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
3884 for (i = 0; i < num; i += EXT2FS_CLUSTER_RATIO(ctx->fs))
3885 mark_block_used(ctx, block + i);
3889 static errcode_t _INLINE_ e2fsck_write_ext_attr3(e2fsck_t ctx, blk64_t block,
3890 void *inbuf, ext2_ino_t inum)
3893 ext2_filsys fs = ctx->fs;
3895 e2fsck_pass1_fix_lock(ctx);
3896 retval = ext2fs_write_ext_attr3(fs, block, inbuf, inum);
3897 e2fsck_pass1_fix_unlock(ctx);
3902 * Adjust the extended attribute block's reference counts at the end
3903 * of pass 1, either by subtracting out references for EA blocks that
3904 * are still referenced in ctx->refcount, or by adding references for
3905 * EA blocks that had extra references as accounted for in
3906 * ctx->refcount_extra.
3908 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
3909 char *block_buf, int adjust_sign)
3911 struct ext2_ext_attr_header *header;
3912 struct problem_context pctx;
3913 ext2_filsys fs = ctx->fs;
3918 clear_problem_context(&pctx);
3920 ea_refcount_intr_begin(refcount);
3922 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
3925 pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
3927 /* We already checked this block, shouldn't happen */
3929 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
3932 header = BHDR(block_buf);
3933 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
3934 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
3938 pctx.blkcount = header->h_refcount;
3939 should_be = header->h_refcount + adjust_sign * (int)count;
3940 pctx.num = should_be;
3941 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
3942 header->h_refcount = should_be;
3943 pctx.errcode = e2fsck_write_ext_attr3(ctx, blk,
3947 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
3956 * Handle processing the extended attribute blocks
3958 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
3959 char *block_buf, struct ea_quota *ea_block_quota)
3961 ext2_filsys fs = ctx->fs;
3962 ext2_ino_t ino = pctx->ino;
3963 struct ext2_inode *inode = pctx->inode;
3966 struct ext2_ext_attr_header *header;
3967 struct ext2_ext_attr_entry *first, *entry;
3968 blk64_t quota_blocks = EXT2FS_C2B(fs, 1);
3969 __u64 quota_inodes = 0;
3970 region_t region = 0;
3971 int failed_csum = 0;
3973 ea_block_quota->blocks = 0;
3974 ea_block_quota->inodes = 0;
3976 blk = ext2fs_file_acl_block(fs, inode);
3981 * If the Extended attribute flag isn't set, then a non-zero
3982 * file acl means that the inode is corrupted.
3984 * Or if the extended attribute block is an invalid block,
3985 * then the inode is also corrupted.
3987 if (!ext2fs_has_feature_xattr(fs->super) ||
3988 (blk < fs->super->s_first_data_block) ||
3989 (blk >= ext2fs_blocks_count(fs->super))) {
3990 /* Fixed in pass2, e2fsck_process_bad_inode(). */
3991 e2fsck_mark_inode_bad(ctx, pctx, PR_2_FILE_ACL_ZERO);
3995 /* If ea bitmap hasn't been allocated, create it */
3996 if (!ctx->block_ea_map) {
3997 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
3998 _("ext attr block map"),
3999 EXT2FS_BMAP64_RBTREE, "block_ea_map",
4000 &ctx->block_ea_map);
4001 if (pctx->errcode) {
4003 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
4004 ctx->flags |= E2F_FLAG_ABORT;
4009 /* Create the EA refcount structure if necessary */
4010 if (!ctx->refcount) {
4011 pctx->errcode = ea_refcount_create(0,
4012 &ctx->refcount_orig);
4013 if (pctx->errcode) {
4015 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4016 ctx->flags |= E2F_FLAG_ABORT;
4020 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
4021 if (pctx->errcode) {
4023 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4024 ctx->flags |= E2F_FLAG_ABORT;
4030 /* Debugging text */
4031 printf("Inode %u has EA block %u\n", ino, blk);
4034 /* Have we seen this EA block before? */
4035 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
4036 ea_block_quota->blocks = EXT2FS_C2B(fs, 1);
4037 ea_block_quota->inodes = 0;
4039 if (ctx->ea_block_quota_blocks) {
4040 ea_refcount_fetch(ctx->ea_block_quota_blocks, blk,
4043 ea_block_quota->blocks = quota_blocks;
4046 if (ctx->ea_block_quota_inodes)
4047 ea_refcount_fetch(ctx->ea_block_quota_inodes, blk,
4048 &ea_block_quota->inodes);
4050 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
4052 /* Ooops, this EA was referenced more than it stated */
4053 if (!ctx->refcount_extra) {
4054 pctx->errcode = ea_refcount_create(0,
4055 &ctx->refcount_extra);
4056 if (pctx->errcode) {
4058 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4059 ctx->flags |= E2F_FLAG_ABORT;
4063 ea_refcount_increment(ctx->refcount_extra, blk, 0);
4068 * OK, we haven't seen this EA block yet. So we need to
4072 pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
4073 if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
4076 } else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
4079 if (pctx->errcode &&
4080 fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
4084 header = BHDR(block_buf);
4085 pctx->blk = ext2fs_file_acl_block(fs, inode);
4086 if (((ctx->ext_attr_ver == 1) &&
4087 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
4088 ((ctx->ext_attr_ver == 2) &&
4089 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
4090 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
4094 if (header->h_blocks != 1) {
4095 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
4099 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
4102 region = region_create(0, fs->blocksize);
4104 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
4105 ctx->flags |= E2F_FLAG_ABORT;
4108 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
4109 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4113 first = (struct ext2_ext_attr_entry *)(header+1);
4114 end = block_buf + fs->blocksize;
4116 while ((char *)entry < end && *(__u32 *)entry) {
4119 if (region_allocate(region, (char *)entry - (char *)header,
4120 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
4121 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4125 if ((ctx->ext_attr_ver == 1 &&
4126 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
4127 (ctx->ext_attr_ver == 2 &&
4128 entry->e_name_index == 0)) {
4129 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
4133 if (entry->e_value_inum == 0) {
4134 if (entry->e_value_offs + entry->e_value_size >
4136 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
4140 if (entry->e_value_size &&
4141 region_allocate(region, entry->e_value_offs,
4142 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
4143 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
4148 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
4149 entry->e_value_offs);
4151 if (entry->e_hash != hash) {
4152 pctx->num = entry->e_hash;
4153 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
4155 entry->e_hash = hash;
4159 blk64_t entry_quota_blocks;
4161 problem = check_large_ea_inode(ctx, entry, pctx,
4162 &entry_quota_blocks);
4163 if (problem && fix_problem(ctx, problem, pctx))
4166 quota_blocks += entry_quota_blocks;
4170 entry = EXT2_EXT_ATTR_NEXT(entry);
4172 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
4173 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4176 region_free(region);
4179 * We only get here if there was no other errors that were fixed.
4180 * If there was a checksum fail, ask to correct it.
4183 fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
4184 pctx->errcode = e2fsck_write_ext_attr3(ctx, blk, block_buf,
4190 if (quota_blocks != EXT2FS_C2B(fs, 1U)) {
4191 if (!ctx->ea_block_quota_blocks) {
4192 pctx->errcode = ea_refcount_create(0,
4193 &ctx->ea_block_quota_blocks);
4194 if (pctx->errcode) {
4199 ea_refcount_store(ctx->ea_block_quota_blocks, blk,
4204 if (!ctx->ea_block_quota_inodes) {
4205 pctx->errcode = ea_refcount_create(0,
4206 &ctx->ea_block_quota_inodes);
4207 if (pctx->errcode) {
4210 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4211 ctx->flags |= E2F_FLAG_ABORT;
4216 ea_refcount_store(ctx->ea_block_quota_inodes, blk,
4219 ea_block_quota->blocks = quota_blocks;
4220 ea_block_quota->inodes = quota_inodes;
4222 inc_ea_inode_refs(ctx, pctx, first, end);
4223 ea_refcount_store(ctx->refcount, blk, header->h_refcount - 1);
4224 ea_refcount_store(ctx->refcount_orig, blk, header->h_refcount);
4226 * It might be racy that this block has been merged in the
4229 if (!is_blocks_used(ctx, blk, 1))
4230 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, blk);
4231 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
4236 region_free(region);
4237 ext2fs_file_acl_block_set(fs, inode, 0);
4238 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
4242 /* Returns 1 if bad htree, 0 if OK */
4243 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
4244 ext2_ino_t ino, struct ext2_inode *inode,
4247 struct ext2_dx_root_info *root;
4248 ext2_filsys fs = ctx->fs;
4252 if ((!LINUX_S_ISDIR(inode->i_mode) &&
4253 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
4254 (!ext2fs_has_feature_dir_index(fs->super) &&
4255 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
4258 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
4260 if ((pctx->errcode) ||
4262 (blk < fs->super->s_first_data_block) ||
4263 (blk >= ext2fs_blocks_count(fs->super))) {
4264 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4270 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
4272 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4276 /* XXX should check that beginning matches a directory */
4277 root = get_ext2_dx_root_info(fs, block_buf);
4279 if ((root->reserved_zero || root->info_length < 8) &&
4280 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4283 pctx->num = root->hash_version;
4284 if ((root->hash_version != EXT2_HASH_LEGACY) &&
4285 (root->hash_version != EXT2_HASH_HALF_MD4) &&
4286 (root->hash_version != EXT2_HASH_TEA) &&
4287 (root->hash_version != EXT2_HASH_SIPHASH) &&
4288 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
4291 if (ext4_hash_in_dirent(inode)) {
4292 if (root->hash_version != EXT2_HASH_SIPHASH &&
4293 fix_problem(ctx, PR_1_HTREE_NEEDS_SIPHASH, pctx))
4296 if (root->hash_version == EXT2_HASH_SIPHASH &&
4297 fix_problem(ctx, PR_1_HTREE_CANNOT_SIPHASH, pctx))
4301 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
4302 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
4305 pctx->num = root->indirect_levels;
4306 /* if htree level is clearly too high, consider it to be broken */
4307 if (root->indirect_levels > EXT4_HTREE_LEVEL &&
4308 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4311 /* if level is only maybe too high, LARGE_DIR feature could be unset */
4312 if (root->indirect_levels > ext2_dir_htree_level(fs) &&
4313 !ext2fs_has_feature_largedir(fs->super)) {
4314 int blockbits = EXT2_BLOCK_SIZE_BITS(fs->super) + 10;
4315 int idx_pb = 1 << (blockbits - 3);
4317 /* compare inode size/blocks vs. max-sized 2-level htree */
4318 if (EXT2_I_SIZE(pctx->inode) <
4319 (idx_pb - 1) * (idx_pb - 2) << blockbits &&
4320 pctx->inode->i_blocks <
4321 (idx_pb - 1) * (idx_pb - 2) << (blockbits - 9) &&
4322 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4326 if (root->indirect_levels > EXT4_HTREE_LEVEL_COMPAT ||
4327 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
4333 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
4334 struct ext2_inode *inode, int restart_flag,
4338 inode->i_links_count = 0;
4339 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
4340 inode->i_dtime = ctx->now;
4343 * If a special inode has such rotten block mappings that we
4344 * want to clear the whole inode, be sure to actually zap
4345 * the block maps because i_links_count isn't checked for
4346 * special inodes, and we'll end up right back here the next
4349 if (ino < EXT2_FIRST_INODE(ctx->fs->super))
4350 memset(inode->i_block, 0, sizeof(inode->i_block));
4352 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
4353 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
4354 if (ctx->inode_reg_map)
4355 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
4356 if (ctx->inode_badness)
4357 ext2fs_icount_store(ctx->inode_badness, ino, 0);
4360 * If the inode was partially accounted for before processing
4361 * was aborted, we need to restart the pass 1 scan.
4363 ctx->flags |= restart_flag;
4365 if (ino == EXT2_BAD_INO)
4366 memset(inode, 0, sizeof(struct ext2_inode));
4368 e2fsck_write_inode(ctx, ino, inode, source);
4372 * Use the multiple-blocks reclamation code to fix alignment problems in
4373 * a bigalloc filesystem. We want a logical cluster to map to *only* one
4374 * physical cluster, and we want the block offsets within that cluster to
4377 static int has_unaligned_cluster_map(e2fsck_t ctx,
4378 blk64_t last_pblk, blk64_t last_lblk,
4379 blk64_t pblk, blk64_t lblk)
4381 blk64_t cluster_mask;
4383 if (!ctx->fs->cluster_ratio_bits)
4385 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
4388 * If the block in the logical cluster doesn't align with the block in
4389 * the physical cluster...
4391 if ((lblk & cluster_mask) != (pblk & cluster_mask))
4395 * If we cross a physical cluster boundary within a logical cluster...
4397 if (last_pblk && (lblk & cluster_mask) != 0 &&
4398 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
4399 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
4405 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
4406 struct process_block_struct *pb,
4407 blk64_t start_block, blk64_t end_block,
4409 ext2_extent_handle_t ehandle,
4412 struct ext2fs_extent extent;
4413 blk64_t blk, last_lblk;
4415 int is_dir, is_leaf;
4417 struct ext2_extent_info info;
4418 int failed_csum = 0;
4420 if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
4423 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
4426 if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
4427 !pb->eti.force_rebuild) {
4428 struct extent_tree_level *etl;
4430 etl = pb->eti.ext_info + info.curr_level;
4431 etl->num_extents += info.num_entries;
4432 etl->max_extents += info.max_entries;
4434 * Implementation wart: Splitting extent blocks when appending
4435 * will leave the old block with one free entry. Therefore
4436 * unless the node is totally full, pretend that a non-root
4437 * extent block can hold one fewer entry than it actually does,
4438 * so that we don't repeatedly rebuild the extent tree.
4440 if (info.curr_level && info.num_entries < info.max_entries)
4444 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
4446 while ((pctx->errcode == 0 ||
4447 pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
4448 info.num_entries-- > 0) {
4449 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
4450 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
4451 last_lblk = extent.e_lblk + extent.e_len - 1;
4454 pctx->blk = extent.e_pblk;
4455 pctx->blk2 = extent.e_lblk;
4456 pctx->num = extent.e_len;
4457 pctx->blkcount = extent.e_lblk + extent.e_len;
4459 if (extent.e_pblk == 0 ||
4460 extent.e_pblk < ctx->fs->super->s_first_data_block ||
4461 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
4462 problem = PR_1_EXTENT_BAD_START_BLK;
4463 else if (extent.e_lblk < start_block)
4464 problem = PR_1_OUT_OF_ORDER_EXTENTS;
4465 else if ((end_block && last_lblk > end_block) &&
4466 !(last_lblk > eof_block &&
4467 ((extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) ||
4468 (pctx->inode->i_flags & EXT4_VERITY_FL))))
4469 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
4470 else if (is_leaf && extent.e_len == 0)
4471 problem = PR_1_EXTENT_LENGTH_ZERO;
4473 (extent.e_pblk + extent.e_len) >
4474 ext2fs_blocks_count(ctx->fs->super))
4475 problem = PR_1_EXTENT_ENDS_BEYOND;
4476 else if (is_leaf && is_dir && !pctx->inode->i_size_high &&
4477 !ext2fs_has_feature_largedir(ctx->fs->super) &&
4478 ((extent.e_lblk + extent.e_len) >
4479 (1U << (21 - ctx->fs->super->s_log_block_size))))
4480 problem = PR_1_TOOBIG_DIR;
4482 if (is_leaf && problem == 0 && extent.e_len > 0) {
4484 printf("extent_region(ino=%u, expect=%llu, "
4485 "lblk=%llu, len=%u)\n", pb->ino,
4486 (unsigned long long) pb->next_lblock,
4487 (unsigned long long) extent.e_lblk,
4490 if (extent.e_lblk < pb->next_lblock)
4491 problem = PR_1_EXTENT_COLLISION;
4492 else if (extent.e_lblk + extent.e_len > pb->next_lblock)
4493 pb->next_lblock = extent.e_lblk + extent.e_len;
4497 * Uninitialized blocks in a directory? Clear the flag and
4498 * we'll interpret the blocks later.
4500 if (try_repairs && is_dir && problem == 0 &&
4501 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4502 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
4503 e2fsck_pass1_fix_lock(ctx);
4504 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4505 pb->inode_modified = 1;
4506 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4508 e2fsck_pass1_fix_unlock(ctx);
4513 #ifdef CONFIG_DEVELOPER_FEATURES
4514 if (try_repairs && !is_dir && problem == 0 &&
4515 (ctx->options & E2F_OPT_CLEAR_UNINIT) &&
4516 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4517 fix_problem(ctx, PR_1_CLEAR_UNINIT_EXTENT, pctx)) {
4518 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4519 pb->inode_modified = 1;
4520 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4527 if (try_repairs && problem) {
4529 /* Record badness only if extent is within inode */
4530 if (fix_problem_bad(ctx, problem, pctx,
4531 info.curr_level == 0)) {
4532 if (ctx->invalid_bitmaps) {
4534 * If fsck knows the bitmaps are bad,
4535 * skip to the next extent and
4536 * try to clear this extent again
4537 * after fixing the bitmaps, by
4540 pctx->errcode = ext2fs_extent_get(
4542 EXT2_EXTENT_NEXT_SIB,
4544 ctx->flags |= E2F_FLAG_RESTART_LATER;
4545 if (pctx->errcode ==
4546 EXT2_ET_NO_CURRENT_NODE) {
4552 e2fsck_pass1_fix_lock(ctx);
4553 e2fsck_read_bitmaps(ctx);
4554 pb->inode_modified = 1;
4556 ext2fs_extent_delete(ehandle, 0);
4557 e2fsck_pass1_fix_unlock(ctx);
4558 if (pctx->errcode) {
4559 pctx->str = "ext2fs_extent_delete";
4562 e2fsck_pass1_fix_lock(ctx);
4563 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4564 e2fsck_pass1_fix_unlock(ctx);
4565 if (pctx->errcode &&
4566 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
4567 pctx->str = "ext2fs_extent_fix_parents";
4570 pctx->errcode = ext2fs_extent_get(ehandle,
4571 EXT2_EXTENT_CURRENT,
4573 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
4584 blk64_t lblk = extent.e_lblk;
4585 int next_try_repairs = 1;
4587 blk = extent.e_pblk;
4590 * If this lower extent block collides with critical
4591 * metadata, don't try to repair the damage. Pass 1b
4592 * will reallocate the block; then we can try again.
4594 if (pb->ino != EXT2_RESIZE_INO &&
4595 extent.e_pblk < ctx->fs->super->s_blocks_count &&
4596 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
4598 next_try_repairs = 0;
4600 fix_problem_bad(ctx,
4601 PR_1_CRITICAL_METADATA_COLLISION,
4603 if ((ctx->options & E2F_OPT_NO) == 0)
4604 ctx->flags |= E2F_FLAG_RESTART_LATER;
4606 pctx->errcode = ext2fs_extent_get(ehandle,
4607 EXT2_EXTENT_DOWN, &extent);
4608 if (pctx->errcode &&
4609 pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
4610 pctx->str = "EXT2_EXTENT_DOWN";
4611 problem = PR_1_EXTENT_HEADER_INVALID;
4612 if (!next_try_repairs)
4614 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
4615 goto report_problem;
4618 /* The next extent should match this index's logical start */
4619 if (extent.e_lblk != lblk) {
4620 struct ext2_extent_info e_info;
4622 pctx->errcode = ext2fs_extent_get_info(ehandle,
4624 if (pctx->errcode) {
4625 pctx->str = "ext2fs_extent_get_info";
4629 pctx->blk2 = extent.e_lblk;
4630 pctx->num = e_info.curr_level - 1;
4631 problem = PR_1_EXTENT_INDEX_START_INVALID;
4632 if (fix_problem(ctx, problem, pctx)) {
4633 e2fsck_pass1_fix_lock(ctx);
4634 pb->inode_modified = 1;
4636 ext2fs_extent_fix_parents(ehandle);
4637 e2fsck_pass1_fix_unlock(ctx);
4638 if (pctx->errcode) {
4639 pctx->str = "ext2fs_extent_fix_parents";
4644 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
4645 last_lblk, eof_block, ehandle,
4649 pctx->errcode = ext2fs_extent_get(ehandle,
4650 EXT2_EXTENT_UP, &extent);
4651 if (pctx->errcode) {
4652 pctx->str = "EXT2_EXTENT_UP";
4655 mark_block_used(ctx, blk);
4660 if ((pb->previous_block != 0) &&
4661 (pb->previous_block+1 != extent.e_pblk)) {
4662 if (ctx->options & E2F_OPT_FRAGCHECK) {
4667 else if (pb->is_reg)
4670 printf(("%6lu(%c): expecting %6lu "
4672 "phys %6lu log %lu len %lu\n"),
4673 (unsigned long) pctx->ino, type,
4674 (unsigned long) pb->previous_block+1,
4675 (unsigned long) extent.e_pblk,
4676 (unsigned long) extent.e_lblk,
4677 (unsigned long) extent.e_len);
4682 * If we notice a gap in the logical block mappings of an
4683 * extent-mapped directory, offer to close the hole by
4684 * moving the logical block down, otherwise we'll go mad in
4685 * pass 3 allocating empty directory blocks to fill the hole.
4687 if (try_repairs && is_dir &&
4688 pb->last_block + 1 < extent.e_lblk) {
4691 new_lblk = pb->last_block + 1;
4692 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
4693 new_lblk = ((new_lblk +
4694 EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
4695 ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
4697 EXT2FS_CLUSTER_MASK(ctx->fs));
4698 pctx->blk = extent.e_lblk;
4699 pctx->blk2 = new_lblk;
4700 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
4701 e2fsck_pass1_fix_lock(ctx);
4702 extent.e_lblk = new_lblk;
4703 pb->inode_modified = 1;
4704 pctx->errcode = ext2fs_extent_replace(ehandle,
4706 e2fsck_pass1_fix_unlock(ctx);
4707 if (pctx->errcode) {
4711 e2fsck_pass1_fix_lock(ctx);
4712 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4713 e2fsck_pass1_fix_unlock(ctx);
4715 goto failed_add_dir_block;
4716 pctx->errcode = ext2fs_extent_goto(ehandle,
4719 goto failed_add_dir_block;
4720 last_lblk = extent.e_lblk + extent.e_len - 1;
4726 while (++pb->last_db_block <
4727 (e2_blkcnt_t) extent.e_lblk) {
4728 pctx->errcode = ext2fs_add_dir_block2(
4732 if (pctx->errcode) {
4734 pctx->num = pb->last_db_block;
4735 goto failed_add_dir_block;
4739 for (i = 0; i < extent.e_len; i++) {
4740 pctx->errcode = ext2fs_add_dir_block2(
4745 if (pctx->errcode) {
4746 pctx->blk = extent.e_pblk + i;
4747 pctx->num = extent.e_lblk + i;
4748 failed_add_dir_block:
4749 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
4750 /* Should never get here */
4751 ctx->flags |= E2F_FLAG_ABORT;
4755 if (extent.e_len > 0)
4756 pb->last_db_block = extent.e_lblk + extent.e_len - 1;
4758 if (has_unaligned_cluster_map(ctx, pb->previous_block,
4762 for (i = 0; i < extent.e_len; i++) {
4763 pctx->blk = extent.e_lblk + i;
4764 pctx->blk2 = extent.e_pblk + i;
4765 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
4766 mark_block_used(ctx, extent.e_pblk + i);
4767 mark_block_used(ctx, extent.e_pblk + i);
4772 * Check whether first cluster got marked in previous iteration.
4774 if (ctx->fs->cluster_ratio_bits &&
4775 pb->previous_block &&
4776 (EXT2FS_B2C(ctx->fs, extent.e_pblk) ==
4777 EXT2FS_B2C(ctx->fs, pb->previous_block)))
4778 /* Set blk to the beginning of next cluster. */
4781 EXT2FS_B2C(ctx->fs, extent.e_pblk) + 1);
4783 /* Set blk to the beginning of current cluster. */
4784 blk = EXT2FS_C2B(ctx->fs,
4785 EXT2FS_B2C(ctx->fs, extent.e_pblk));
4787 if (blk < extent.e_pblk + extent.e_len) {
4788 mark_blocks_used(ctx, blk,
4789 extent.e_pblk + extent.e_len - blk);
4790 n = DIV_ROUND_UP(extent.e_pblk + extent.e_len - blk,
4791 EXT2FS_CLUSTER_RATIO(ctx->fs));
4792 pb->num_blocks += n;
4794 pb->last_block = extent.e_lblk + extent.e_len - 1;
4795 pb->previous_block = extent.e_pblk + extent.e_len - 1;
4796 start_block = pb->last_block = last_lblk;
4797 if (is_leaf && !is_dir &&
4798 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
4799 pb->last_init_lblock = last_lblk;
4801 pctx->errcode = ext2fs_extent_get(ehandle,
4802 EXT2_EXTENT_NEXT_SIB,
4806 /* Failed csum but passes checks? Ask to fix checksum. */
4808 fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
4809 e2fsck_pass1_fix_lock(ctx);
4810 pb->inode_modified = 1;
4811 pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
4812 e2fsck_pass1_fix_unlock(ctx);
4817 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
4821 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
4822 struct process_block_struct *pb)
4824 struct ext2_extent_info info;
4825 struct ext2_inode *inode = pctx->inode;
4826 ext2_extent_handle_t ehandle;
4827 ext2_filsys fs = ctx->fs;
4828 ext2_ino_t ino = pctx->ino;
4831 struct ext3_extent_header *eh;
4833 /* Check for a proper extent header... */
4834 eh = (struct ext3_extent_header *) &inode->i_block[0];
4835 retval = ext2fs_extent_header_verify(eh, sizeof(inode->i_block));
4837 if (fix_problem(ctx, PR_1_MISSING_EXTENT_HEADER, pctx))
4838 e2fsck_clear_inode(ctx, ino, inode, 0,
4839 "check_blocks_extents");
4844 /* ...since this function doesn't fail if i_block is zeroed. */
4845 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
4846 if (pctx->errcode) {
4847 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
4848 e2fsck_clear_inode(ctx, ino, inode, 0,
4849 "check_blocks_extents");
4854 retval = ext2fs_extent_get_info(ehandle, &info);
4856 int max_depth = info.max_depth;
4858 if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
4859 max_depth = MAX_EXTENT_DEPTH_COUNT-1;
4860 ctx->extent_depth_count[max_depth]++;
4863 /* Check maximum extent depth */
4864 pctx->blk = info.max_depth;
4865 pctx->blk2 = ext2fs_max_extent_depth(ehandle);
4866 if (pctx->blk2 < pctx->blk &&
4867 fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
4868 pb->eti.force_rebuild = 1;
4870 /* Can we collect extent tree level stats? */
4871 pctx->blk = MAX_EXTENT_DEPTH_COUNT;
4872 if (pctx->blk2 > pctx->blk)
4873 fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
4874 memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
4875 pb->eti.ino = pb->ino;
4877 pb->next_lblock = 0;
4879 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
4880 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
4881 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
4882 if (pctx->errcode &&
4883 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
4885 inode->i_blocks = 0;
4886 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
4887 "check_blocks_extents");
4890 ext2fs_extent_free(ehandle);
4892 /* Rebuild unless it's a dir and we're rehashing it */
4893 if (LINUX_S_ISDIR(inode->i_mode) &&
4894 e2fsck_dir_will_be_rehashed(ctx, ino))
4897 if (ctx->options & E2F_OPT_CONVERT_BMAP)
4898 e2fsck_rebuild_extents_later(ctx, ino);
4900 e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
4904 * In fact we don't need to check blocks for an inode with inline data
4905 * because this inode doesn't have any blocks. In this function all
4906 * we need to do is add this inode into dblist when it is a directory.
4908 static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
4909 struct process_block_struct *pb)
4912 size_t inline_data_size = 0;
4919 /* Process the dirents in i_block[] as the "first" block. */
4920 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
4924 /* Process the dirents in the EA as a "second" block. */
4925 flags = ctx->fs->flags;
4926 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
4927 pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
4929 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
4930 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
4931 if (pctx->errcode) {
4936 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
4939 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
4947 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
4948 ctx->flags |= E2F_FLAG_ABORT;
4952 * This subroutine is called on each inode to account for all of the
4953 * blocks used by that inode.
4955 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
4956 char *block_buf, const struct ea_quota *ea_ibody_quota)
4958 ext2_filsys fs = ctx->fs;
4959 struct process_block_struct pb;
4960 ext2_ino_t ino = pctx->ino;
4961 struct ext2_inode *inode = pctx->inode;
4962 unsigned bad_size = 0;
4963 int dirty_inode = 0;
4967 struct ea_quota ea_block_quota;
4970 pb.num_blocks = EXT2FS_B2C(ctx->fs,
4971 ea_ibody_quota ? ea_ibody_quota->blocks : 0);
4973 pb.last_init_lblock = -1;
4974 pb.last_db_block = -1;
4975 pb.num_illegal_blocks = 0;
4976 pb.suppress = 0; pb.clear = 0;
4979 pb.previous_block = 0;
4980 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
4981 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
4982 pb.max_blocks = 1U << (31 - fs->super->s_log_block_size);
4986 pb.inode_modified = 0;
4987 pb.eti.force_rebuild = 0;
4991 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
4992 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
4994 if (check_ext_attr(ctx, pctx, block_buf, &ea_block_quota)) {
4995 if (e2fsck_should_abort(ctx))
4997 pb.num_blocks += EXT2FS_B2C(ctx->fs, ea_block_quota.blocks);
5000 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
5001 check_blocks_inline_data(ctx, pctx, &pb);
5002 else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
5003 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
5004 check_blocks_extents(ctx, pctx, &pb);
5008 * If we've modified the inode, write it out before
5009 * iterate() tries to use it.
5012 e2fsck_write_inode(ctx, ino, inode,
5017 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5018 pctx->errcode = ext2fs_block_iterate3(fs, ino,
5019 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
5020 block_buf, process_block, &pb);
5022 * We do not have uninitialized extents in non extent
5025 pb.last_init_lblock = pb.last_block;
5027 * If iterate() changed a block mapping, we have to
5028 * re-read the inode. If we decide to clear the
5029 * inode after clearing some stuff, we'll re-write the
5030 * bad mappings into the inode!
5032 if (pb.inode_modified)
5033 e2fsck_read_inode(ctx, ino, inode,
5035 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5036 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5038 if (ctx->options & E2F_OPT_CONVERT_BMAP) {
5040 printf("bmap rebuild ino=%d\n", ino);
5042 if (!LINUX_S_ISDIR(inode->i_mode) ||
5043 !e2fsck_dir_will_be_rehashed(ctx, ino))
5044 e2fsck_rebuild_extents_later(ctx, ino);
5048 end_problem_latch(ctx, PR_LATCH_BLOCK);
5049 end_problem_latch(ctx, PR_LATCH_TOOBIG);
5050 if (e2fsck_should_abort(ctx))
5053 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
5055 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
5056 if (LINUX_S_ISDIR(inode->i_mode))
5057 ctx->fs_fragmented_dir++;
5059 ctx->fs_fragmented++;
5063 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
5068 if (inode->i_flags & EXT2_INDEX_FL) {
5069 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
5070 inode->i_flags &= ~EXT2_INDEX_FL;
5073 e2fsck_add_dx_dir(ctx, ino, inode, pb.last_block+1);
5077 if (!pb.num_blocks && pb.is_dir &&
5078 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
5080 * The mode might be in-correct. Increasing the badness by
5081 * small amount won't hurt much.
5083 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
5084 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
5085 ctx->fs_directory_count--;
5090 if (ino != quota_type2inum(PRJQUOTA, fs->super) &&
5091 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) &&
5092 !(inode->i_flags & EXT4_EA_INODE_FL)) {
5093 quota_data_add(ctx->qctx, (struct ext2_inode_large *) inode,
5095 pb.num_blocks * EXT2_CLUSTER_SIZE(fs->super));
5096 quota_data_inodes(ctx->qctx, (struct ext2_inode_large *) inode,
5097 ino, (ea_ibody_quota ?
5098 ea_ibody_quota->inodes : 0) +
5099 ea_block_quota.inodes + 1);
5102 if (!ext2fs_has_feature_huge_file(fs->super) ||
5103 !(inode->i_flags & EXT4_HUGE_FILE_FL))
5104 pb.num_blocks *= (fs->blocksize / 512);
5105 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
5107 printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
5108 ino, inode->i_size, (unsigned long long) pb.last_block,
5109 (unsigned long long) ext2fs_inode_i_blocks(fs, inode),
5110 (unsigned long long) pb.num_blocks);
5112 size = EXT2_I_SIZE(inode);
5114 unsigned nblock = size >> EXT2_BLOCK_SIZE_BITS(fs->super);
5115 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
5120 flags = ctx->fs->flags;
5121 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5122 err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
5124 ctx->fs->flags = (flags &
5125 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5127 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5128 if (err || sz != size) {
5132 } else if (size & (fs->blocksize - 1))
5134 else if (nblock > (pb.last_block + 1))
5136 else if (nblock < (pb.last_block + 1)) {
5137 if (((pb.last_block + 1) - nblock) >
5138 fs->super->s_prealloc_dir_blocks)
5142 if ((pb.last_init_lblock >= 0) &&
5143 /* Do not allow initialized allocated blocks past i_size*/
5144 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
5145 !(inode->i_flags & EXT4_VERITY_FL))
5147 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5148 size > ext2_max_sizes[fs->super->s_log_block_size])
5149 /* too big for a direct/indirect-mapped file */
5151 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5153 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
5154 /* too big for an extent-based file - 32bit ee_block */
5157 /* i_size for symlinks is checked elsewhere */
5158 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
5159 /* Did inline_data set pctx->num earlier? */
5161 pctx->num = (pb.last_block + 1) * fs->blocksize;
5162 pctx->group = bad_size;
5163 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
5164 ext2fs_inode_size_set(fs, inode, pctx->num);
5165 if (EXT2_I_SIZE(inode) == 0 &&
5166 (inode->i_flags & EXT4_INLINE_DATA_FL)) {
5167 memset(inode->i_block, 0,
5168 sizeof(inode->i_block));
5169 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
5175 if (LINUX_S_ISREG(inode->i_mode) &&
5176 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
5178 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
5179 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
5180 (ext2fs_has_feature_huge_file(fs->super) &&
5181 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
5182 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
5183 pctx->num = pb.num_blocks;
5184 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
5185 inode->i_blocks = pb.num_blocks;
5186 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
5193 * The kernel gets mad if we ask it to allocate bigalloc clusters to
5194 * a block mapped file, so rebuild it as an extent file. We can skip
5195 * symlinks because they're never rewritten.
5197 if (ext2fs_has_feature_bigalloc(fs->super) &&
5198 (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
5199 ext2fs_inode_data_blocks2(fs, inode) > 0 &&
5200 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
5201 !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
5202 fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
5203 pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
5208 if (ctx->dirs_to_hash && pb.is_dir &&
5209 !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
5210 !(inode->i_flags & EXT2_INDEX_FL) &&
5211 ((inode->i_size / fs->blocksize) >= 3))
5212 e2fsck_rehash_dir_later(ctx, ino);
5215 /* need restart if clearing bad inode after block processing */
5216 if (e2fsck_fix_bad_inode(ctx, pctx))
5217 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
5218 "check_blocks_bad");
5219 else if (dirty_inode)
5220 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
5225 * Helper function called by process block when an illegal block is
5226 * found. It returns a description about why the block is illegal
5228 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
5232 static char problem[80];
5234 super = fs->super->s_first_data_block;
5235 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
5236 if (block < super) {
5237 sprintf(problem, "< FIRSTBLOCK (%u)", super);
5239 } else if (block >= ext2fs_blocks_count(fs->super)) {
5240 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
5243 for (i = 0; i < fs->group_desc_count; i++) {
5244 if (block == super) {
5245 sprintf(problem, "is the superblock in group %d", i);
5248 if (block > super &&
5249 block <= (super + fs->desc_blocks)) {
5250 sprintf(problem, "is in the group descriptors "
5254 if (block == ext2fs_block_bitmap_loc(fs, i)) {
5255 sprintf(problem, "is the block bitmap of group %d", i);
5258 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
5259 sprintf(problem, "is the inode bitmap of group %d", i);
5262 if (block >= ext2fs_inode_table_loc(fs, i) &&
5263 (block < ext2fs_inode_table_loc(fs, i)
5264 + fs->inode_blocks_per_group)) {
5265 sprintf(problem, "is in the inode table of group %d",
5269 super += fs->super->s_blocks_per_group;
5276 * This is a helper function for check_blocks().
5278 static int process_block(ext2_filsys fs,
5280 e2_blkcnt_t blockcnt,
5281 blk64_t ref_block EXT2FS_ATTR((unused)),
5282 int ref_offset EXT2FS_ATTR((unused)),
5285 struct process_block_struct *p;
5286 struct problem_context *pctx;
5287 blk64_t blk = *block_nr;
5289 problem_t problem = 0;
5292 p = (struct process_block_struct *) priv_data;
5297 * For a directory, add logical block zero for processing even if it's
5298 * not mapped or we'll be perennially stuck with broken "." and ".."
5301 if (p->is_dir && blockcnt == 0 && blk == 0) {
5302 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
5303 if (pctx->errcode) {
5305 pctx->num = blockcnt;
5306 goto failed_add_dir_block;
5315 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
5320 * Simplistic fragmentation check. We merely require that the
5321 * file be contiguous. (Which can never be true for really
5322 * big files that are greater than a block group.)
5324 if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
5325 if (p->previous_block+1 != blk) {
5326 if (ctx->options & E2F_OPT_FRAGCHECK) {
5334 printf(_("%6lu(%c): expecting %6lu "
5335 "got phys %6lu (blkcnt %lld)\n"),
5336 (unsigned long) pctx->ino, type,
5337 (unsigned long) p->previous_block+1,
5338 (unsigned long) blk,
5339 (long long) blockcnt);
5345 if (p->is_dir && !ext2fs_has_feature_largedir(fs->super) &&
5346 !pctx->inode->i_size_high &&
5347 blockcnt > (1 << (21 - fs->super->s_log_block_size)))
5348 problem = PR_1_TOOBIG_DIR;
5349 if (p->is_dir && p->num_blocks + 1 >= p->max_blocks)
5350 problem = PR_1_TOOBIG_DIR;
5351 if (p->is_reg && p->num_blocks + 1 >= p->max_blocks)
5352 problem = PR_1_TOOBIG_REG;
5353 if (!p->is_dir && !p->is_reg && blockcnt > 0)
5354 problem = PR_1_TOOBIG_SYMLINK;
5356 if (blk < fs->super->s_first_data_block ||
5357 blk >= ext2fs_blocks_count(fs->super))
5358 problem = PR_1_ILLEGAL_BLOCK_NUM;
5361 * If this IND/DIND/TIND block is squatting atop some critical metadata
5362 * (group descriptors, superblock, bitmap, inode table), any write to
5363 * "fix" mapping problems will destroy the metadata. We'll let pass 1b
5364 * fix that and restart fsck.
5367 p->ino != EXT2_RESIZE_INO &&
5368 blk < ctx->fs->super->s_blocks_count &&
5369 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
5371 fix_problem_bad(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx, 2);
5372 if ((ctx->options & E2F_OPT_NO) == 0)
5373 ctx->flags |= E2F_FLAG_RESTART_LATER;
5377 p->num_illegal_blocks++;
5379 * A bit of subterfuge here -- we're trying to fix a block
5380 * mapping, but the IND/DIND/TIND block could have collided
5381 * with some critical metadata. So, fix the in-core mapping so
5382 * iterate won't go insane, but return 0 instead of
5383 * BLOCK_CHANGED so that it won't write the remapping out to
5384 * our multiply linked block.
5386 * Even if we previously determined that an *IND block
5387 * conflicts with critical metadata, we must still try to
5388 * iterate the *IND block as if it is an *IND block to find and
5389 * mark the blocks it points to. Better to be overly cautious
5390 * with the used_blocks map so that we don't move the *IND
5391 * block to a block that's really in use!
5393 if (p->ino != EXT2_RESIZE_INO &&
5395 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
5400 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
5401 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
5405 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
5407 set_latch_flags(PR_LATCH_BLOCK,
5412 pctx->blkcount = blockcnt;
5413 if (fix_problem(ctx, problem, pctx)) {
5414 blk = *block_nr = 0;
5415 ret_code = BLOCK_CHANGED;
5416 p->inode_modified = 1;
5418 * If the directory block is too big and is beyond the
5419 * end of the FS, don't bother trying to add it for
5420 * processing -- the kernel would never have created a
5421 * directory this large, and we risk an ENOMEM abort.
5422 * In any case, the toobig handler for extent-based
5423 * directories also doesn't feed toobig blocks to
5426 if (problem == PR_1_TOOBIG_DIR)
5433 if (p->ino == EXT2_RESIZE_INO) {
5435 * The resize inode has already be sanity checked
5436 * during pass #0 (the superblock checks). All we
5437 * have to do is mark the double indirect block as
5438 * being in use; all of the other blocks are handled
5439 * by mark_table_blocks()).
5441 if (blockcnt == BLOCK_COUNT_DIND)
5442 mark_block_used(ctx, blk);
5444 } else if (!(ctx->fs->cluster_ratio_bits &&
5445 p->previous_block &&
5446 (EXT2FS_B2C(ctx->fs, blk) ==
5447 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
5448 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
5449 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
5450 mark_block_used(ctx, blk);
5452 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
5453 p->last_block, blk, blockcnt)) {
5454 pctx->blk = blockcnt;
5456 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
5457 mark_block_used(ctx, blk);
5458 mark_block_used(ctx, blk);
5461 p->last_block = blockcnt;
5462 p->previous_block = blk;
5464 if (p->is_dir && (blockcnt >= 0)) {
5465 while (++p->last_db_block < blockcnt) {
5466 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
5469 if (pctx->errcode) {
5471 pctx->num = p->last_db_block;
5472 goto failed_add_dir_block;
5475 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
5477 if (pctx->errcode) {
5479 pctx->num = blockcnt;
5480 failed_add_dir_block:
5481 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
5482 /* Should never get here */
5483 ctx->flags |= E2F_FLAG_ABORT;
5490 static int process_bad_block(ext2_filsys fs,
5492 e2_blkcnt_t blockcnt,
5493 blk64_t ref_block EXT2FS_ATTR((unused)),
5494 int ref_offset EXT2FS_ATTR((unused)),
5497 struct process_block_struct *p;
5498 blk64_t blk = *block_nr;
5499 blk64_t first_block;
5501 struct problem_context *pctx;
5507 p = (struct process_block_struct *) priv_data;
5511 pctx->ino = EXT2_BAD_INO;
5513 pctx->blkcount = blockcnt;
5515 if ((blk < fs->super->s_first_data_block) ||
5516 (blk >= ext2fs_blocks_count(fs->super))) {
5517 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
5519 return BLOCK_CHANGED;
5525 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
5527 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
5529 return BLOCK_CHANGED;
5531 } else if (is_blocks_used(ctx, blk, 1)) {
5533 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
5536 return BLOCK_CHANGED;
5538 if (e2fsck_should_abort(ctx))
5541 mark_block_used(ctx, blk);
5546 printf ("DEBUG: Marking %u as bad.\n", blk);
5548 ctx->fs_badblocks_count++;
5550 * If the block is not used, then mark it as used and return.
5551 * If it is already marked as found, this must mean that
5552 * there's an overlap between the filesystem table blocks
5553 * (bitmaps and inode table) and the bad block list.
5555 if (!is_blocks_used(ctx, blk, 1)) {
5556 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
5560 * Try to find the where the filesystem block was used...
5562 first_block = fs->super->s_first_data_block;
5564 for (i = 0; i < fs->group_desc_count; i++ ) {
5567 if (!ext2fs_bg_has_super(fs, i))
5569 if (blk == first_block) {
5571 if (fix_problem(ctx,
5572 PR_1_BAD_PRIMARY_SUPERBLOCK,
5575 return BLOCK_CHANGED;
5579 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
5582 if ((blk > first_block) &&
5583 (blk <= first_block + fs->desc_blocks)) {
5585 pctx->blk = *block_nr;
5586 if (fix_problem(ctx,
5587 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
5589 return BLOCK_CHANGED;
5593 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
5597 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
5598 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
5599 ctx->invalid_block_bitmap_flag[i]++;
5600 ctx->invalid_bitmaps++;
5604 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
5605 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
5606 ctx->invalid_inode_bitmap_flag[i]++;
5607 ctx->invalid_bitmaps++;
5611 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
5612 (blk < (ext2fs_inode_table_loc(fs, i) +
5613 fs->inode_blocks_per_group))) {
5615 * If there are bad blocks in the inode table,
5616 * the inode scan code will try to do
5617 * something reasonable automatically.
5621 first_block += fs->super->s_blocks_per_group;
5624 * If we've gotten to this point, then the only
5625 * possibility is that the bad block inode meta data
5626 * is using a bad block.
5628 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
5629 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
5630 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
5632 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
5634 return BLOCK_CHANGED;
5636 if (e2fsck_should_abort(ctx))
5643 /* Warn user that the block wasn't claimed */
5644 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
5649 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
5650 const char *name, int num, blk64_t *new_block)
5652 ext2_filsys fs = ctx->fs;
5654 blk64_t old_block = *new_block;
5657 unsigned flexbg_size;
5660 struct problem_context pctx;
5662 clear_problem_context(&pctx);
5665 pctx.blk = old_block;
5669 * For flex_bg filesystems, first try to allocate the metadata
5670 * within the flex_bg, and if that fails then try finding the
5671 * space anywhere in the filesystem.
5673 is_flexbg = ext2fs_has_feature_flex_bg(fs->super);
5675 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
5676 flexbg = group / flexbg_size;
5677 first_block = ext2fs_group_first_block2(fs,
5678 flexbg_size * flexbg);
5679 last_grp = group | (flexbg_size - 1);
5680 if (last_grp >= fs->group_desc_count)
5681 last_grp = fs->group_desc_count - 1;
5682 last_block = ext2fs_group_last_block2(fs, last_grp);
5684 last_block = ext2fs_group_last_block2(fs, group);
5685 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
5686 num, ctx->block_found_map,
5688 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
5689 pctx.errcode = ext2fs_get_free_blocks2(fs,
5690 fs->super->s_first_data_block,
5691 ext2fs_blocks_count(fs->super),
5692 num, ctx->block_found_map, new_block);
5695 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
5696 ext2fs_unmark_valid(fs);
5697 ctx->flags |= E2F_FLAG_ABORT;
5700 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
5702 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
5703 ext2fs_unmark_valid(fs);
5704 ctx->flags |= E2F_FLAG_ABORT;
5707 ext2fs_mark_super_dirty(fs);
5708 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
5709 pctx.blk2 = *new_block;
5710 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
5711 PR_1_RELOC_TO), &pctx);
5713 for (i = 0; i < num; i++) {
5715 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
5717 pctx.errcode = io_channel_read_blk64(fs->io,
5718 old_block + i, 1, buf);
5720 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
5721 pctx.blk = (*new_block) + i;
5722 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
5725 pctx.blk = (*new_block) + i;
5726 pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
5731 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
5733 ext2fs_free_mem(&buf);
5737 * This routine gets called at the end of pass 1 if bad blocks are
5738 * detected in the superblock, group descriptors, inode_bitmaps, or
5739 * block bitmaps. At this point, all of the blocks have been mapped
5740 * out, so we can try to allocate new block(s) to replace the bad
5743 static void handle_fs_bad_blocks(e2fsck_t ctx)
5745 ext2_filsys fs = ctx->fs;
5747 blk64_t first_block;
5750 for (i = 0; i < fs->group_desc_count; i++) {
5751 first_block = ext2fs_group_first_block2(fs, i);
5753 if (ctx->invalid_block_bitmap_flag[i]) {
5754 new_blk = ext2fs_block_bitmap_loc(fs, i);
5755 new_table_block(ctx, first_block, i, _("block bitmap"),
5757 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
5759 if (ctx->invalid_inode_bitmap_flag[i]) {
5760 new_blk = ext2fs_inode_bitmap_loc(fs, i);
5761 new_table_block(ctx, first_block, i, _("inode bitmap"),
5763 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
5765 if (ctx->invalid_inode_table_flag[i]) {
5766 new_blk = ext2fs_inode_table_loc(fs, i);
5767 new_table_block(ctx, first_block, i, _("inode table"),
5768 fs->inode_blocks_per_group,
5770 ext2fs_inode_table_loc_set(fs, i, new_blk);
5771 ctx->flags |= E2F_FLAG_RESTART;
5774 ctx->invalid_bitmaps = 0;
5778 * This routine marks all blocks which are used by the superblock,
5779 * group descriptors, inode bitmaps, and block bitmaps.
5781 static void mark_table_blocks(e2fsck_t ctx)
5783 ext2_filsys fs = ctx->fs;
5787 struct problem_context pctx;
5789 clear_problem_context(&pctx);
5791 for (i = 0; i < fs->group_desc_count; i++) {
5794 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
5795 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
5798 * Mark the blocks used for the inode table
5800 if (ext2fs_inode_table_loc(fs, i)) {
5801 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
5802 j < fs->inode_blocks_per_group;
5804 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5807 if (!ctx->invalid_inode_table_flag[i] &&
5809 PR_1_ITABLE_CONFLICT, &pctx)) {
5810 ctx->invalid_inode_table_flag[i]++;
5811 ctx->invalid_bitmaps++;
5814 ext2fs_mark_block_bitmap2(
5815 ctx->block_found_map, b);
5816 ext2fs_mark_block_bitmap2(
5817 ctx->block_metadata_map, b);
5823 * Mark block used for the block bitmap
5825 if (ext2fs_block_bitmap_loc(fs, i)) {
5826 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5827 ext2fs_block_bitmap_loc(fs, i))) {
5828 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
5829 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
5830 ctx->invalid_block_bitmap_flag[i]++;
5831 ctx->invalid_bitmaps++;
5834 ext2fs_mark_block_bitmap2(ctx->block_found_map,
5835 ext2fs_block_bitmap_loc(fs, i));
5836 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5837 ext2fs_block_bitmap_loc(fs, i));
5841 * Mark block used for the inode bitmap
5843 if (ext2fs_inode_bitmap_loc(fs, i)) {
5844 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5845 ext2fs_inode_bitmap_loc(fs, i))) {
5846 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
5847 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
5848 ctx->invalid_inode_bitmap_flag[i]++;
5849 ctx->invalid_bitmaps++;
5852 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5853 ext2fs_inode_bitmap_loc(fs, i));
5854 ext2fs_mark_block_bitmap2(ctx->block_found_map,
5855 ext2fs_inode_bitmap_loc(fs, i));
5862 * These subroutines short circuits ext2fs_get_blocks and
5863 * ext2fs_check_directory; we use them since we already have the inode
5864 * structure, so there's no point in letting the ext2fs library read
5867 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
5870 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5873 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5874 return EXT2_ET_CALLBACK_NOTHANDLED;
5876 for (i=0; i < EXT2_N_BLOCKS; i++)
5877 blocks[i] = ctx->stashed_inode->i_block[i];
5881 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
5882 struct ext2_inode *inode)
5884 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5886 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5887 return EXT2_ET_CALLBACK_NOTHANDLED;
5888 *inode = *ctx->stashed_inode;
5892 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
5893 struct ext2_inode *inode)
5895 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5897 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
5898 (inode != ctx->stashed_inode))
5899 *ctx->stashed_inode = *inode;
5900 return EXT2_ET_CALLBACK_NOTHANDLED;
5903 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
5905 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5907 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5908 return EXT2_ET_CALLBACK_NOTHANDLED;
5910 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
5911 return EXT2_ET_NO_DIRECTORY;
5915 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
5918 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5922 if (ctx->block_found_map) {
5923 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
5927 if (fs->block_map) {
5928 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
5929 ext2fs_mark_bb_dirty(fs);
5932 if (!fs->block_map) {
5933 retval = ext2fs_read_block_bitmap(fs);
5938 retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
5947 static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
5948 blk64_t len, blk64_t *pblk, blk64_t *plen)
5950 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5953 if (ctx->block_found_map)
5954 return ext2fs_new_range(fs, flags, goal, len,
5955 ctx->block_found_map, pblk, plen);
5957 if (!fs->block_map) {
5958 retval = ext2fs_read_block_bitmap(fs);
5963 return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
5967 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
5969 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5971 /* Never free a critical metadata block */
5972 if (ctx->block_found_map &&
5973 ctx->block_metadata_map &&
5975 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
5978 if (ctx->block_found_map) {
5980 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
5982 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
5986 static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
5987 blk_t num, int inuse)
5989 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5991 /* Never free a critical metadata block */
5992 if (ctx->block_found_map &&
5993 ctx->block_metadata_map &&
5995 ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
5998 if (ctx->block_found_map) {
6000 ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
6003 ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
6008 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
6010 ext2_filsys fs = ctx->fs;
6012 if (use_shortcuts) {
6013 fs->get_blocks = pass1_get_blocks;
6014 fs->check_directory = pass1_check_directory;
6015 fs->read_inode = pass1_read_inode;
6016 fs->write_inode = pass1_write_inode;
6017 ctx->stashed_ino = 0;
6020 fs->check_directory = 0;
6022 fs->write_inode = 0;
6026 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
6028 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
6029 ext2fs_set_block_alloc_stats_callback(ctx->fs,
6030 e2fsck_block_alloc_stats, 0);
6031 ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
6032 ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
6033 e2fsck_block_alloc_stats_range, NULL);