2 * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
11 * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12 * and applies the following tests to each inode:
14 * - The mode field of the inode must be legal.
15 * - The size and block count fields of the inode are correct.
16 * - A data block must not be used by another inode
18 * Pass 1 also gathers the collects the following information:
20 * - A bitmap of which inodes are in use. (inode_used_map)
21 * - A bitmap of which inodes are directories. (inode_dir_map)
22 * - A bitmap of which inodes are regular files. (inode_reg_map)
23 * - A bitmap of which inodes have bad fields. (inode_bad_map)
24 * - A bitmap of which inodes are in bad blocks. (inode_bb_map)
25 * - A bitmap of which inodes are imagic inodes. (inode_imagic_map)
26 * - A bitmap of which inodes are casefolded. (inode_casefold_map)
27 * - A bitmap of which blocks are in use. (block_found_map)
28 * - A bitmap of which blocks are in use by two inodes (block_dup_map)
29 * - The data blocks of the directory inodes. (dir_map)
30 * - Ref counts for ea_inodes. (ea_inode_refs)
31 * - The encryption policy ID of each encrypted inode. (encrypted_files)
33 * Pass 1 is designed to stash away enough information so that the
34 * other passes should not need to read in the inode information
35 * during the normal course of a filesystem check. (Although if an
36 * inconsistency is detected, other passes may need to read in an
39 * Note that pass 1B will be invoked if there are any duplicate blocks
43 #define _GNU_SOURCE 1 /* get strnlen() */
56 #include <ext2fs/ext2_ext_attr.h>
57 /* todo remove this finally */
58 #include <ext2fs/ext2fsP.h>
63 #ifdef NO_INLINE_FUNCS
66 #define _INLINE_ inline
76 static int process_block(ext2_filsys fs, blk64_t *blocknr,
77 e2_blkcnt_t blockcnt, blk64_t ref_blk,
78 int ref_offset, void *priv_data);
79 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
80 e2_blkcnt_t blockcnt, blk64_t ref_blk,
81 int ref_offset, void *priv_data);
82 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
84 const struct ea_quota *ea_ibody_quota);
85 static void mark_table_blocks(e2fsck_t ctx);
86 static void alloc_bb_map(e2fsck_t ctx);
87 static void alloc_imagic_map(e2fsck_t ctx);
88 static void mark_inode_bad(e2fsck_t ctx, ext2_ino_t ino);
89 static void add_casefolded_dir(e2fsck_t ctx, ext2_ino_t ino);
90 static void handle_fs_bad_blocks(e2fsck_t ctx);
91 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
92 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
93 dgrp_t group, void * priv_data);
94 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
95 char *block_buf, int adjust_sign);
96 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
98 struct process_block_struct {
100 unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
101 fragmented:1, compressed:1, bbcheck:1,
106 e2_blkcnt_t last_init_lblock;
107 e2_blkcnt_t last_db_block;
108 int num_illegal_blocks;
109 blk64_t previous_block;
110 struct ext2_inode *inode;
111 struct problem_context *pctx;
112 ext2fs_block_bitmap fs_meta_blocks;
115 struct extent_tree_info eti;
118 struct process_inode_block {
120 struct ea_quota ea_ibody_quota;
121 struct ext2_inode_large inode;
124 struct scan_callback_struct {
127 struct process_inode_block *inodes_to_process;
128 int *process_inode_count;
131 static void process_inodes(e2fsck_t ctx, char *block_buf,
132 struct process_inode_block *inodes_to_process,
133 int *process_inode_count);
135 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
136 EXT2_MIN_BLOCK_LOG_SIZE + 1];
139 * Check to make sure a device inode is real. Returns 1 if the device
140 * checks out, 0 if not.
142 * Note: this routine is now also used to check FIFO's and Sockets,
143 * since they have the same requirement; the i_block fields should be
146 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
147 struct ext2_inode *inode)
152 * If the index or extents flag is set, then this is a bogus
155 if (inode->i_flags & (EXT2_INDEX_FL | EXT4_EXTENTS_FL))
159 * We should be able to do the test below all the time, but
160 * because the kernel doesn't forcibly clear the device
161 * inode's additional i_block fields, there are some rare
162 * occasions when a legitimate device inode will have non-zero
163 * additional i_block fields. So for now, we only complain
164 * when the immutable flag is set, which should never happen
165 * for devices. (And that's when the problem is caused, since
166 * you can't set or clear immutable flags for devices.) Once
167 * the kernel has been fixed we can change this...
169 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
170 for (i=4; i < EXT2_N_BLOCKS; i++)
171 if (inode->i_block[i])
178 * Check to make sure a symlink inode is real. Returns 1 if the symlink
179 * checks out, 0 if not.
181 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
182 struct ext2_inode *inode, char *buf)
187 if ((inode->i_size_high || inode->i_size == 0) ||
188 (inode->i_flags & EXT2_INDEX_FL))
191 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
194 if (inode->i_flags & EXT4_EXTENTS_FL)
196 if (ext2fs_inline_data_size(fs, ino, &inline_size))
198 if (inode->i_size != inline_size)
204 if (ext2fs_is_fast_symlink(inode)) {
205 if (inode->i_flags & EXT4_EXTENTS_FL)
207 buf = (char *)inode->i_block;
208 buflen = sizeof(inode->i_block);
210 ext2_extent_handle_t handle;
211 struct ext2_extent_info info;
212 struct ext2fs_extent extent;
216 if (inode->i_flags & EXT4_EXTENTS_FL) {
217 if (ext2fs_extent_open2(fs, ino, inode, &handle))
219 if (ext2fs_extent_get_info(handle, &info) ||
220 (info.num_entries != 1) ||
221 (info.max_depth != 0)) {
222 ext2fs_extent_free(handle);
225 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT,
227 (extent.e_lblk != 0) ||
228 (extent.e_len != 1)) {
229 ext2fs_extent_free(handle);
233 ext2fs_extent_free(handle);
235 blk = inode->i_block[0];
237 for (i = 1; i < EXT2_N_BLOCKS; i++)
238 if (inode->i_block[i])
242 if (blk < fs->super->s_first_data_block ||
243 blk >= ext2fs_blocks_count(fs->super))
246 if (io_channel_read_blk64(fs->io, blk, 1, buf))
249 buflen = fs->blocksize;
252 if (inode->i_flags & EXT4_ENCRYPT_FL)
253 len = ext2fs_le16_to_cpu(*(__u16 *)buf) + 2;
255 len = strnlen(buf, buflen);
260 if (len != inode->i_size)
266 * If the extents or inlinedata flags are set on the inode, offer to clear 'em.
268 #define BAD_SPECIAL_FLAGS (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)
269 static void check_extents_inlinedata(e2fsck_t ctx,
270 struct problem_context *pctx)
272 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
275 if (!fix_problem(ctx, PR_1_SPECIAL_EXTENTS_IDATA, pctx))
278 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
279 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
281 #undef BAD_SPECIAL_FLAGS
284 * If the immutable (or append-only) flag is set on the inode, offer
287 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
288 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
290 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
293 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
296 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
297 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
301 * If device, fifo or socket, check size is zero -- if not offer to
304 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
306 struct ext2_inode *inode = pctx->inode;
308 if (EXT2_I_SIZE(inode) == 0)
311 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
314 ext2fs_inode_size_set(ctx->fs, inode, 0);
315 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
319 * For a given size, calculate how many blocks would be charged towards quota.
321 static blk64_t size_to_quota_blocks(ext2_filsys fs, size_t size)
325 clusters = DIV_ROUND_UP(size, fs->blocksize << fs->cluster_ratio_bits);
326 return EXT2FS_C2B(fs, clusters);
330 * Check validity of EA inode. Return 0 if EA inode is valid, otherwise return
333 static problem_t check_large_ea_inode(e2fsck_t ctx,
334 struct ext2_ext_attr_entry *entry,
335 struct problem_context *pctx,
336 blk64_t *quota_blocks)
338 struct ext2_inode inode;
339 __u32 hash, signed_hash;
342 /* Check if inode is within valid range */
343 if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
344 (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
345 pctx->num = entry->e_value_inum;
346 return PR_1_ATTR_VALUE_EA_INODE;
349 e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
351 retval = ext2fs_ext_attr_hash_entry3(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) || (signed_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 int alloc_ea_inode_refs(e2fsck_t ctx, struct problem_context *pctx)
399 pctx->errcode = ea_refcount_create(0, &ctx->ea_inode_refs);
402 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
403 ctx->flags |= E2F_FLAG_ABORT;
409 static void inc_ea_inode_refs(e2fsck_t ctx, struct problem_context *pctx,
410 struct ext2_ext_attr_entry *first, void *end)
412 struct ext2_ext_attr_entry *entry = first;
413 struct ext2_ext_attr_entry *np = EXT2_EXT_ATTR_NEXT(entry);
416 while ((void *) entry < end && (void *) np < end &&
417 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
418 if (!entry->e_value_inum)
420 if (!ctx->ea_inode_refs && !alloc_ea_inode_refs(ctx, pctx))
422 ea_refcount_fetch(ctx->ea_inode_refs, entry->e_value_inum,
424 if (refs == EA_INODE_NO_REFS)
428 ea_refcount_store(ctx->ea_inode_refs, entry->e_value_inum, refs);
431 np = EXT2_EXT_ATTR_NEXT(entry);
436 * Make sure inode is tracked as EA inode. We use special EA_INODE_NO_REFS
437 * value if we didn't find any xattrs referencing this inode yet.
439 static int track_ea_inode(e2fsck_t ctx, struct problem_context *pctx,
444 if (!ctx->ea_inode_refs && !alloc_ea_inode_refs(ctx, pctx))
447 ea_refcount_fetch(ctx->ea_inode_refs, ino, &refs);
451 pctx->errcode = ea_refcount_store(ctx->ea_inode_refs, ino,
455 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
456 ctx->flags |= E2F_FLAG_ABORT;
462 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx,
463 struct ea_quota *ea_ibody_quota)
465 struct ext2_super_block *sb = ctx->fs->super;
466 struct ext2_inode_large *inode;
467 struct ext2_ext_attr_entry *entry;
468 char *start, *header, *end;
469 unsigned int storage_size, remain;
470 problem_t problem = 0;
473 ea_ibody_quota->blocks = 0;
474 ea_ibody_quota->inodes = 0;
476 inode = (struct ext2_inode_large *) pctx->inode;
477 storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
478 inode->i_extra_isize;
479 header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
480 inode->i_extra_isize;
481 end = header + storage_size;
482 start = header + sizeof(__u32);
483 entry = (struct ext2_ext_attr_entry *) start;
485 /* scan all entry's headers first */
487 /* take finish entry 0UL into account */
488 remain = storage_size - sizeof(__u32);
490 region = region_create(0, storage_size);
492 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
494 ctx->flags |= E2F_FLAG_ABORT;
497 if (region_allocate(region, 0, sizeof(__u32))) {
498 problem = PR_1_INODE_EA_ALLOC_COLLISION;
502 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
503 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
506 if (region_allocate(region, (char *)entry - (char *)header,
507 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
508 problem = PR_1_INODE_EA_ALLOC_COLLISION;
512 /* header eats this space */
513 remain -= sizeof(struct ext2_ext_attr_entry);
515 /* is attribute name valid? */
516 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
517 pctx->num = entry->e_name_len;
518 problem = PR_1_ATTR_NAME_LEN;
522 /* attribute len eats this space */
523 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
525 if (entry->e_value_inum == 0) {
526 /* check value size */
527 if (entry->e_value_size > remain) {
528 pctx->num = entry->e_value_size;
529 problem = PR_1_ATTR_VALUE_SIZE;
533 if (entry->e_value_size &&
534 region_allocate(region,
535 sizeof(__u32) + entry->e_value_offs,
537 entry->e_value_size))) {
538 problem = PR_1_INODE_EA_ALLOC_COLLISION;
542 hash = ext2fs_ext_attr_hash_entry(entry,
543 start + entry->e_value_offs);
544 if (entry->e_hash != 0 && entry->e_hash != hash)
545 hash = ext2fs_ext_attr_hash_entry_signed(entry,
546 start + entry->e_value_offs);
548 /* e_hash may be 0 in older inode's ea */
549 if (entry->e_hash != 0 && entry->e_hash != hash) {
550 pctx->num = entry->e_hash;
551 problem = PR_1_ATTR_HASH;
555 blk64_t quota_blocks;
557 if (!ext2fs_has_feature_ea_inode(sb) &&
558 fix_problem(ctx, PR_1_EA_INODE_FEATURE, pctx)) {
559 ext2fs_set_feature_ea_inode(sb);
560 ext2fs_mark_super_dirty(ctx->fs);
563 problem = check_large_ea_inode(ctx, entry, pctx,
568 ea_ibody_quota->blocks += quota_blocks;
569 ea_ibody_quota->inodes++;
572 /* If EA value is stored in external inode then it does not
573 * consume space here */
574 if (entry->e_value_inum == 0)
575 remain -= entry->e_value_size;
577 entry = EXT2_EXT_ATTR_NEXT(entry);
580 if (region_allocate(region, (char *)entry - (char *)header,
582 problem = PR_1_INODE_EA_ALLOC_COLLISION;
589 * it seems like a corruption. it's very unlikely we could repair
590 * EA(s) in automatic fashion -bzzz
592 if (problem == 0 || !fix_problem(ctx, problem, pctx)) {
593 inc_ea_inode_refs(ctx, pctx,
594 (struct ext2_ext_attr_entry *)start, end);
598 /* simply remove all possible EA(s) */
599 *((__u32 *)header) = 0UL;
600 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
601 EXT2_INODE_SIZE(sb), "pass1");
602 ea_ibody_quota->blocks = 0;
603 ea_ibody_quota->inodes = 0;
606 static int check_inode_extra_negative_epoch(__u32 xtime, __u32 extra) {
607 return (xtime & (1U << 31)) != 0 &&
608 (extra & EXT4_EPOCH_MASK) == EXT4_EPOCH_MASK;
611 #define CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, xtime) \
612 check_inode_extra_negative_epoch(inode->i_##xtime, \
613 inode->i_##xtime##_extra)
615 /* When today's date is earlier than 2242, we assume that atimes,
616 * ctimes, crtimes, and mtimes with years in the range 2310..2378 are
617 * actually pre-1970 dates mis-encoded.
619 #define EXT4_EXTRA_NEGATIVE_DATE_CUTOFF 2 * (1LL << 32)
621 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx,
622 struct ea_quota *ea_ibody_quota)
624 struct ext2_super_block *sb = ctx->fs->super;
625 struct ext2_inode_large *inode;
629 ea_ibody_quota->blocks = 0;
630 ea_ibody_quota->inodes = 0;
632 inode = (struct ext2_inode_large *) pctx->inode;
633 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
634 /* this isn't large inode. so, nothing to check */
639 printf("inode #%u, i_extra_size %d\n", pctx->ino,
640 inode->i_extra_isize);
642 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
643 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
644 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
646 * For now we will allow i_extra_isize to be 0, but really
647 * implementations should never allow i_extra_isize to be 0
649 if (inode->i_extra_isize &&
650 (inode->i_extra_isize < min || inode->i_extra_isize > max ||
651 inode->i_extra_isize & 3)) {
652 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
654 if (inode->i_extra_isize < min || inode->i_extra_isize > max)
655 inode->i_extra_isize = sb->s_want_extra_isize;
657 inode->i_extra_isize = (inode->i_extra_isize + 3) & ~3;
658 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
659 EXT2_INODE_SIZE(sb), "pass1");
662 /* check if there is no place for an EA header */
663 if (inode->i_extra_isize >= max - sizeof(__u32))
666 eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
667 inode->i_extra_isize);
668 if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
669 /* it seems inode has an extended attribute(s) in body */
670 check_ea_in_inode(ctx, pctx, ea_ibody_quota);
674 * If the inode's extended atime (ctime, crtime, mtime) is stored in
675 * the old, invalid format, repair it.
677 if (((sizeof(time_t) <= 4) ||
678 (((sizeof(time_t) > 4) &&
679 ctx->now < EXT4_EXTRA_NEGATIVE_DATE_CUTOFF))) &&
680 (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime) ||
681 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime) ||
682 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime) ||
683 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))) {
685 if (!fix_problem(ctx, PR_1_EA_TIME_OUT_OF_RANGE, pctx))
688 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime))
689 inode->i_atime_extra &= ~EXT4_EPOCH_MASK;
690 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime))
691 inode->i_ctime_extra &= ~EXT4_EPOCH_MASK;
692 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime))
693 inode->i_crtime_extra &= ~EXT4_EPOCH_MASK;
694 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))
695 inode->i_mtime_extra &= ~EXT4_EPOCH_MASK;
696 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
697 EXT2_INODE_SIZE(sb), "pass1");
702 static _INLINE_ int is_blocks_used(e2fsck_t ctx, blk64_t block,
707 /* used to avoid duplicate output from below */
708 retval = ext2fs_test_block_bitmap_range2_valid(ctx->block_found_map,
713 retval = ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num);
715 e2fsck_pass1_block_map_r_lock(ctx);
717 retval = ext2fs_test_block_bitmap_range2(
718 ctx->global_ctx->block_found_map, block, num);
719 e2fsck_pass1_block_map_r_unlock(ctx);
728 * Check to see if the inode might really be a directory, despite i_mode
730 * This is a lot of complexity for something for which I'm not really
731 * convinced happens frequently in the wild. If for any reason this
732 * causes any problems, take this code out.
733 * [tytso:20070331.0827EDT]
735 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
738 struct ext2_inode *inode = pctx->inode;
739 struct ext2_dir_entry *dirent;
742 unsigned int i, rec_len, not_device = 0;
747 * If the mode looks OK, we believe it. If the first block in
748 * the i_block array is 0, this cannot be a directory. If the
749 * inode is extent-mapped, it is still the case that the latter
750 * cannot be 0 - the magic number in the extent header would make
753 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
754 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
758 * Check the block numbers in the i_block array for validity:
759 * zero blocks are skipped (but the first one cannot be zero -
760 * see above), other blocks are checked against the first and
761 * max data blocks (from the the superblock) and against the
762 * block bitmap. Any invalid block found means this cannot be
765 * If there are non-zero blocks past the fourth entry, then
766 * this cannot be a device file: we remember that for the next
769 * For extent mapped files, we don't do any sanity checking:
770 * just try to get the phys block of logical block 0 and run
773 * For inline data files, we just try to get the size of inline
774 * data. If it's true, we will treat it as a directory.
777 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
778 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
779 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
782 unsigned int rec_len2;
783 struct ext2_dir_entry de;
785 if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
788 * If the size isn't a multiple of 4, it's probably not a
794 * If the first 10 bytes don't look like a directory entry,
795 * it's probably not a directory.
797 memcpy(&dotdot, inode->i_block, sizeof(dotdot));
798 memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
799 EXT2_DIR_REC_LEN(0));
800 dotdot = ext2fs_le32_to_cpu(dotdot);
801 de.inode = ext2fs_le32_to_cpu(de.inode);
802 de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
803 ext2fs_get_rec_len(ctx->fs, &de, &rec_len2);
804 if (dotdot >= ctx->fs->super->s_inodes_count ||
805 (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
806 dotdot != EXT2_ROOT_INO) ||
807 de.inode >= ctx->fs->super->s_inodes_count ||
808 (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
810 rec_len2 > EXT4_MIN_INLINE_DATA_SIZE -
811 EXT4_INLINE_DATA_DOTDOT_SIZE)
813 /* device files never have a "system.data" entry */
815 } else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
817 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
820 /* device files are never extent mapped */
823 for (i=0; i < EXT2_N_BLOCKS; i++) {
824 blk = inode->i_block[i];
830 if (blk < ctx->fs->super->s_first_data_block ||
831 blk >= ext2fs_blocks_count(ctx->fs->super) ||
832 is_blocks_used(ctx, blk, 1))
833 return; /* Invalid block, can't be dir */
835 blk = inode->i_block[0];
839 * If the mode says this is a device file and the i_links_count field
840 * is sane and we have not ruled it out as a device file previously,
841 * we declare it a device file, not a directory.
843 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
844 (inode->i_links_count == 1) && !not_device)
847 /* read the first block */
848 ehandler_operation(_("reading directory block"));
849 retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
850 ehandler_operation(0);
854 dirent = (struct ext2_dir_entry *) buf;
855 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
858 if ((ext2fs_dirent_name_len(dirent) != 1) ||
859 (dirent->name[0] != '.') ||
860 (dirent->inode != pctx->ino) ||
863 (rec_len >= ctx->fs->blocksize - 12))
866 dirent = (struct ext2_dir_entry *) (buf + rec_len);
867 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
870 if ((ext2fs_dirent_name_len(dirent) != 2) ||
871 (dirent->name[0] != '.') ||
872 (dirent->name[1] != '.') ||
878 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
879 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
880 e2fsck_write_inode_full(ctx, pctx->ino, inode,
881 EXT2_INODE_SIZE(ctx->fs->super),
882 "check_is_really_dir");
886 extern errcode_t e2fsck_setup_icount(e2fsck_t ctx, const char *icount_name,
887 int flags, ext2_icount_t hint,
890 unsigned int threshold;
891 unsigned int save_type;
899 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
901 profile_get_uint(ctx->profile, "scratch_files",
902 "numdirs_threshold", 0, 0, &threshold);
903 profile_get_boolean(ctx->profile, "scratch_files",
904 "icount", 0, 1, &enable);
906 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
908 num_dirs = 1024; /* Guess */
910 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
911 (!threshold || num_dirs > threshold)) {
912 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir,
917 e2fsck_set_bitmap_type(ctx->fs, EXT2FS_BMAP64_RBTREE, icount_name,
919 if (ctx->options & E2F_OPT_ICOUNT_FULLMAP)
920 flags |= EXT2_ICOUNT_OPT_FULLMAP;
921 retval = ext2fs_create_icount2(ctx->fs, flags, 0, hint, ret);
922 ctx->fs->default_bitmap_type = save_type;
926 static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
928 struct problem_context *pctx)
931 struct ext2_inode_large inode;
934 * Reread inode. If we don't see checksum error, then this inode
935 * has been fixed elsewhere.
937 ctx->stashed_ino = 0;
938 retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
940 if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
946 * Checksum still doesn't match. That implies that the inode passes
947 * all the sanity checks, so maybe the checksum is simply corrupt.
948 * See if the user will go for fixing that.
950 if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
954 e2fsck_pass1_fix_lock(ctx);
955 retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
957 e2fsck_pass1_fix_unlock(ctx);
961 static void reserve_block_for_root_repair(e2fsck_t ctx)
965 ext2_filsys fs = ctx->fs;
967 ctx->root_repair_block = 0;
968 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
971 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
974 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
975 ctx->root_repair_block = blk;
978 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
982 ext2_filsys fs = ctx->fs;
983 static const char name[] = "lost+found";
986 ctx->lnf_repair_block = 0;
987 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
990 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
993 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
994 ctx->lnf_repair_block = blk;
998 static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
999 struct ext2_inode *inode,
1003 struct ext2_xattr_handle *handle;
1006 retval = ext2fs_xattrs_open(fs, ino, &handle);
1010 retval = ext2fs_xattrs_read_inode(handle,
1011 (struct ext2_inode_large *)inode);
1015 retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
1018 ext2fs_free_mem(&p);
1020 (void) ext2fs_xattrs_close(&handle);
1024 static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
1025 struct problem_context *pctx,
1032 * If the inode failed the checksum and the user didn't
1033 * clear the inode, test the checksum again -- if it still
1034 * fails, ask the user if the checksum should be corrected.
1036 pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
1038 ctx->flags |= E2F_FLAG_ABORT;
1040 #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
1042 finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
1043 if ((ctx)->flags & E2F_FLAG_ABORT) { \
1044 e2fsck_pass1_check_unlock(ctx); \
1049 static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
1054 for (i = 0; i < EXT2_N_BLOCKS; i++) {
1055 x = inode->i_block[i];
1056 #ifdef WORDS_BIGENDIAN
1057 x = ext2fs_swab32(x);
1059 if (x >= ext2fs_blocks_count(fs->super))
1067 * Figure out what to do with an inode that has both extents and inline data
1068 * inode flags set. Returns -1 if we decide to erase the inode, 0 otherwise.
1070 static int fix_inline_data_extents_file(e2fsck_t ctx,
1072 struct ext2_inode *inode,
1074 struct problem_context *pctx)
1076 size_t max_inline_ea_size;
1077 ext2_filsys fs = ctx->fs;
1080 /* Both feature flags not set? Just run the regular checks */
1081 if (!ext2fs_has_feature_extents(fs->super) &&
1082 !ext2fs_has_feature_inline_data(fs->super))
1085 /* Clear both flags if it's a special file */
1086 if (LINUX_S_ISCHR(inode->i_mode) ||
1087 LINUX_S_ISBLK(inode->i_mode) ||
1088 LINUX_S_ISFIFO(inode->i_mode) ||
1089 LINUX_S_ISSOCK(inode->i_mode)) {
1090 check_extents_inlinedata(ctx, pctx);
1094 /* If it looks like an extent tree, try to clear inlinedata */
1095 if (ext2fs_extent_header_verify(inode->i_block,
1096 sizeof(inode->i_block)) == 0 &&
1097 fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
1098 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1103 /* If it looks short enough to be inline data, try to clear extents */
1104 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
1105 max_inline_ea_size = inode_size -
1106 (EXT2_GOOD_OLD_INODE_SIZE +
1107 ((struct ext2_inode_large *)inode)->i_extra_isize);
1109 max_inline_ea_size = 0;
1110 if (EXT2_I_SIZE(inode) <
1111 EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
1112 fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
1113 inode->i_flags &= ~EXT4_EXTENTS_FL;
1119 * Too big for inline data, but no evidence of extent tree -
1120 * maybe it's a block map file? If the mappings all look valid?
1122 if (could_be_block_map(fs, inode) &&
1123 fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
1124 #ifdef WORDS_BIGENDIAN
1127 for (i = 0; i < EXT2_N_BLOCKS; i++)
1128 inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
1131 inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
1136 /* Oh well, just clear the busted inode. */
1137 if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
1138 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1144 e2fsck_write_inode(ctx, ino, inode, "pass1");
1149 static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
1151 ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
1152 dgrp_t start = *group, grp, grp_end = ctx->fs->group_desc_count;
1153 blk64_t blocks_to_read = 0;
1154 errcode_t err = EXT2_ET_INVALID_ARGUMENT;
1157 if (ctx->fs->fs_num_threads > 1)
1158 grp_end = ctx->thread_info.et_group_end;
1160 if (ctx->readahead_kb == 0)
1163 /* Keep iterating groups until we have enough to readahead */
1164 inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
1165 for (grp = start; grp < grp_end; grp++) {
1166 if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
1168 inodes_in_group = ctx->fs->super->s_inodes_per_group -
1169 ext2fs_bg_itable_unused(ctx->fs, grp);
1170 blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
1172 if (blocks_to_read * ctx->fs->blocksize >
1173 ctx->readahead_kb * 1024)
1177 err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
1179 if (err == EAGAIN) {
1180 ctx->readahead_kb /= 2;
1186 /* Error; disable itable readahead */
1187 *group = ctx->fs->group_desc_count;
1188 *next_ino = ctx->fs->super->s_inodes_count;
1191 * Don't do more readahead until we've reached the first inode
1192 * of the last inode scan buffer block for the last group.
1195 inodes_per_buffer = (ctx->inode_buffer_blocks ?
1196 ctx->inode_buffer_blocks :
1197 EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
1198 ctx->fs->blocksize /
1199 EXT2_INODE_SIZE(ctx->fs->super);
1201 *next_ino = inodes_in_group -
1202 (inodes_in_group % inodes_per_buffer) + 1 +
1203 (grp * ctx->fs->super->s_inodes_per_group);
1208 * Check if the passed ino is one of the used superblock quota inodes.
1210 * Before the quota inodes were journaled, older superblock quota inodes
1211 * were just regular files in the filesystem and not reserved inodes. This
1212 * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1213 * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1215 static int quota_inum_is_super(struct ext2_super_block *sb, ext2_ino_t ino)
1217 enum quota_type qtype;
1219 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1220 if (*quota_sb_inump(sb, qtype) == ino)
1227 * Check if the passed ino is one of the reserved quota inodes.
1228 * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1229 * inodes. These inodes may or may not be in use by the quota feature.
1231 static int quota_inum_is_reserved(ext2_filsys fs, ext2_ino_t ino)
1233 enum quota_type qtype;
1235 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1236 if (quota_type2inum(qtype, fs->super) == ino)
1242 static int e2fsck_should_abort(e2fsck_t ctx)
1244 e2fsck_t global_ctx;
1246 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1249 if (ctx->global_ctx) {
1250 global_ctx = ctx->global_ctx;
1251 if (global_ctx->flags & E2F_FLAG_SIGNAL_MASK)
1257 static void init_ext2_max_sizes()
1263 * Init ext2_max_sizes which will be immutable and shared between
1266 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1268 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
1269 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
1270 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
1271 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
1272 max_sizes = (max_sizes * (1UL << i));
1273 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
1279 /* TODO: tdb needs to be handled properly for multiple threads*/
1280 static int multiple_threads_supported(e2fsck_t ctx)
1283 unsigned int threshold;
1284 ext2_ino_t num_dirs;
1289 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
1291 profile_get_uint(ctx->profile, "scratch_files",
1292 "numdirs_threshold", 0, 0, &threshold);
1293 profile_get_boolean(ctx->profile, "scratch_files",
1294 "icount", 0, 1, &enable);
1296 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
1298 num_dirs = 1024; /* Guess */
1300 /* tdb is unsupported now */
1301 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
1302 (!threshold || num_dirs > threshold))
1309 * Even though we could specify number of threads,
1310 * but it might be more than the whole filesystem
1311 * block groups, correct it here.
1313 static void e2fsck_pass1_set_thread_num(e2fsck_t ctx)
1315 unsigned flexbg_size = 1;
1316 ext2_filsys fs = ctx->fs;
1317 int num_threads = ctx->pfs_num_threads;
1320 if (num_threads < 1) {
1325 if (!multiple_threads_supported(ctx)) {
1327 fprintf(stderr, "Fall through single thread for pass1 "
1328 "because tdb could not handle properly\n");
1332 if (ext2fs_has_feature_flex_bg(fs->super))
1333 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1334 max_threads = fs->group_desc_count / flexbg_size;
1335 if (max_threads == 0)
1337 if (max_threads > E2FSCK_MAX_THREADS)
1338 max_threads = E2FSCK_MAX_THREADS;
1340 if (num_threads > max_threads) {
1341 fprintf(stderr, "Use max possible thread num: %d instead\n",
1343 num_threads = max_threads;
1346 ctx->pfs_num_threads = num_threads;
1347 ctx->fs->fs_num_threads = num_threads;
1352 * We need call mark_table_blocks() before multiple
1353 * thread start, since all known system blocks should be
1354 * marked and checked later.
1356 static errcode_t e2fsck_pass1_prepare(e2fsck_t ctx)
1358 struct problem_context pctx;
1359 ext2_filsys fs = ctx->fs;
1360 unsigned long long readahead_kb;
1362 init_ext2_max_sizes();
1364 e2fsck_pass1_set_thread_num(ctx);
1366 /* If we can do readahead, figure out how many groups to pull in. */
1367 if (!e2fsck_can_readahead(ctx->fs))
1368 ctx->readahead_kb = 0;
1369 else if (ctx->readahead_kb == ~0ULL)
1370 ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
1373 /* don't use more than 1/10 of memory for threads checking */
1374 readahead_kb = get_memory_size() / (10 * ctx->pfs_num_threads);
1375 /* maybe better disable RA if this is too small? */
1376 if (ctx->readahead_kb > readahead_kb)
1377 ctx->readahead_kb = readahead_kb;
1379 clear_problem_context(&pctx);
1380 if (!(ctx->options & E2F_OPT_PREEN))
1381 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1383 pctx.errcode = e2fsck_allocate_subcluster_bitmap(ctx->fs,
1384 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
1385 "block_found_map", &ctx->block_found_map);
1388 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1389 ctx->flags |= E2F_FLAG_ABORT;
1390 return pctx.errcode;
1392 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1393 _("metadata block map"), EXT2FS_BMAP64_RBTREE,
1394 "block_metadata_map", &ctx->block_metadata_map);
1397 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1398 ctx->flags |= E2F_FLAG_ABORT;
1399 return pctx.errcode;
1402 mark_table_blocks(ctx);
1403 pctx.errcode = ext2fs_convert_subcluster_bitmap(ctx->fs,
1404 &ctx->block_found_map);
1406 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1407 ctx->flags |= E2F_FLAG_ABORT;
1408 return pctx.errcode;
1411 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1412 _("multiply claimed block map"),
1413 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1414 &ctx->block_dup_map);
1417 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1419 /* Should never get here */
1420 ctx->flags |= E2F_FLAG_ABORT;
1421 return pctx.errcode;
1424 if (ext2fs_has_feature_mmp(fs->super) &&
1425 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1426 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1427 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1428 fs->super->s_mmp_block);
1430 pthread_rwlock_init(&ctx->fs_fix_rwlock, NULL);
1431 pthread_rwlock_init(&ctx->fs_block_map_rwlock, NULL);
1432 if (ctx->pfs_num_threads > 1)
1433 ctx->fs_need_locking = 1;
1439 static void e2fsck_pass1_post(e2fsck_t ctx)
1441 struct problem_context pctx;
1442 ext2_filsys fs = ctx->fs;
1445 if (e2fsck_should_abort(ctx))
1448 block_buf = (char *)e2fsck_allocate_memory(ctx, ctx->fs->blocksize * 3,
1449 "block interate buffer");
1450 reserve_block_for_root_repair(ctx);
1451 reserve_block_for_lnf_repair(ctx);
1454 * If any extended attribute blocks' reference counts need to
1455 * be adjusted, either up (ctx->refcount_extra), or down
1456 * (ctx->refcount), then fix them.
1458 if (ctx->refcount) {
1459 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1460 ea_refcount_free(ctx->refcount);
1463 if (ctx->refcount_extra) {
1464 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1466 ea_refcount_free(ctx->refcount_extra);
1467 ctx->refcount_extra = 0;
1470 if (ctx->invalid_bitmaps)
1471 handle_fs_bad_blocks(ctx);
1473 /* We don't need the block_ea_map any more */
1474 if (ctx->block_ea_map) {
1475 ext2fs_free_block_bitmap(ctx->block_ea_map);
1476 ctx->block_ea_map = 0;
1479 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1480 struct ext2_inode *inode;
1481 int inode_size = EXT2_INODE_SIZE(fs->super);
1482 inode = e2fsck_allocate_memory(ctx, inode_size,
1485 clear_problem_context(&pctx);
1486 pctx.errcode = ext2fs_create_resize_inode(fs);
1488 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1490 ctx->flags |= E2F_FLAG_ABORT;
1491 ext2fs_free_mem(&inode);
1492 ext2fs_free_mem(&block_buf);
1497 if (!pctx.errcode) {
1498 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1500 ext2fs_inode_xtime_set(inode, i_mtime, ctx->now);
1501 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1504 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1505 ext2fs_free_mem(&inode);
1508 if (ctx->flags & E2F_FLAG_RESTART) {
1509 ext2fs_free_mem(&block_buf);
1513 if (ctx->block_dup_map) {
1514 if (!(ctx->flags & E2F_FLAG_DUP_BLOCK)) {
1515 ext2fs_free_mem(&block_buf);
1518 if (ctx->options & E2F_OPT_PREEN) {
1519 clear_problem_context(&pctx);
1520 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1522 e2fsck_pass1_dupblocks(ctx, block_buf);
1523 ext2fs_free_mem(&block_buf);
1524 ctx->flags &= ~E2F_FLAG_DUP_BLOCK;
1527 ctx->flags |= E2F_FLAG_ALLOC_OK;
1531 void e2fsck_pass1_run(e2fsck_t ctx)
1534 ext2_filsys fs = ctx->fs;
1536 struct ext2_inode *inode = NULL;
1537 ext2_inode_scan scan = NULL;
1538 char *block_buf = NULL;
1539 #ifdef RESOURCE_TRACK
1540 struct resource_track rtrack;
1542 unsigned char frag, fsize;
1543 struct problem_context pctx;
1544 struct scan_callback_struct scan_struct;
1545 struct ext2_super_block *sb = ctx->fs->super;
1547 const char *eop_next_inode = _("getting next inode from scan");
1548 int imagic_fs, extent_fs, inlinedata_fs, casefold_fs;
1549 int low_dtime_check = 1;
1550 unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
1551 unsigned int bufsize;
1552 int failed_csum = 0;
1553 ext2_ino_t ino_threshold = 0;
1554 dgrp_t ra_group = 0;
1555 struct ea_quota ea_ibody_quota;
1556 struct process_inode_block *inodes_to_process;
1557 int process_inode_count, check_mmp;
1558 e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
1561 init_resource_track(&rtrack, ctx->fs->io);
1562 clear_problem_context(&pctx);
1564 pass1_readahead(ctx, &ra_group, &ino_threshold);
1565 if (ext2fs_has_feature_dir_index(fs->super) &&
1566 !(ctx->options & E2F_OPT_NO)) {
1567 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
1568 ctx->dirs_to_hash = 0;
1572 mtrace_print("Pass 1");
1575 imagic_fs = ext2fs_has_feature_imagic_inodes(sb);
1576 extent_fs = ext2fs_has_feature_extents(sb);
1577 inlinedata_fs = ext2fs_has_feature_inline_data(sb);
1578 casefold_fs = ext2fs_has_feature_casefold(sb);
1581 * Allocate bitmaps structures
1583 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1584 EXT2FS_BMAP64_RBTREE,
1586 &ctx->inode_used_map);
1589 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1590 ctx->flags |= E2F_FLAG_ABORT;
1593 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1594 _("directory inode map"),
1595 ctx->global_ctx ? EXT2FS_BMAP64_RBTREE :
1596 EXT2FS_BMAP64_AUTODIR,
1597 "inode_dir_map", &ctx->inode_dir_map);
1600 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1601 ctx->flags |= E2F_FLAG_ABORT;
1604 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1605 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1606 "inode_reg_map", &ctx->inode_reg_map);
1609 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1610 ctx->flags |= E2F_FLAG_ABORT;
1615 e2fsck_allocate_inode_bitmap(fs,
1616 _("inode casefold map"),
1617 EXT2FS_BMAP64_RBTREE,
1618 "inode_casefold_map",
1619 &ctx->inode_casefold_map);
1622 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1623 ctx->flags |= E2F_FLAG_ABORT;
1627 pctx.errcode = e2fsck_setup_icount(ctx, "inode_link_info", 0, NULL,
1628 &ctx->inode_link_info);
1630 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1631 ctx->flags |= E2F_FLAG_ABORT;
1634 bufsize = inode_size;
1635 if (bufsize < sizeof(struct ext2_inode_large))
1636 bufsize = sizeof(struct ext2_inode_large);
1637 inode = (struct ext2_inode *)
1638 e2fsck_allocate_memory(ctx, bufsize, "scratch inode");
1640 inodes_to_process = (struct process_inode_block *)
1641 e2fsck_allocate_memory(ctx,
1642 (ctx->process_inode_size *
1643 sizeof(struct process_inode_block)),
1644 "array of inodes to process");
1645 process_inode_count = 0;
1647 pctx.errcode = ext2fs_init_dblist(fs, 0);
1649 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1650 ctx->flags |= E2F_FLAG_ABORT;
1655 * If the last orphan field is set, clear it, since the pass1
1656 * processing will automatically find and clear the orphans.
1657 * In the future, we may want to try using the last_orphan
1658 * linked list ourselves, but for now, we clear it so that the
1659 * ext3 mount code won't get confused.
1661 if (!(ctx->options & E2F_OPT_READONLY)) {
1662 if (fs->super->s_last_orphan) {
1663 fs->super->s_last_orphan = 0;
1664 ext2fs_mark_super_dirty(fs);
1668 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1669 "block iterate buffer");
1670 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1671 e2fsck_use_inode_shortcuts(ctx, 1);
1672 e2fsck_intercept_block_allocations(ctx);
1673 old_op = ehandler_operation(_("opening inode scan"));
1674 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1676 ehandler_operation(old_op);
1678 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1679 ctx->flags |= E2F_FLAG_ABORT;
1682 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1683 EXT2_SF_WARN_GARBAGE_INODES, 0);
1684 ctx->stashed_inode = inode;
1685 scan_struct.ctx = ctx;
1686 scan_struct.block_buf = block_buf;
1687 scan_struct.inodes_to_process = inodes_to_process;
1688 scan_struct.process_inode_count = &process_inode_count;
1689 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1690 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1691 ctx->fs->group_desc_count)))
1694 if (((tm = ext2fs_get_tstamp(fs->super, s_wtime)) &&
1695 tm < fs->super->s_inodes_count) ||
1696 ((tm = ext2fs_get_tstamp(fs->super, s_mtime)) &&
1697 tm < fs->super->s_inodes_count) ||
1698 ((tm = ext2fs_get_tstamp(fs->super, s_mkfs_time)) &&
1699 tm < fs->super->s_inodes_count))
1700 low_dtime_check = 0;
1702 /* Set up ctx->lost_and_found if possible */
1703 (void) e2fsck_get_lost_and_found(ctx, 0);
1706 if (ctx->global_ctx) {
1707 if (ctx->options & E2F_OPT_DEBUG &&
1708 ctx->options & E2F_OPT_MULTITHREAD)
1709 fprintf(stderr, "thread %d jumping to group %u\n",
1710 ctx->thread_info.et_thread_index,
1711 ctx->thread_info.et_group_start);
1712 pctx.errcode = ext2fs_inode_scan_goto_blockgroup(scan,
1713 ctx->thread_info.et_group_start);
1715 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1716 ctx->flags |= E2F_FLAG_ABORT;
1724 e2fsck_pass1_check_lock(ctx);
1726 if (!global_ctx->mmp_update_thread) {
1727 e2fsck_pass1_block_map_w_lock(ctx);
1728 if (!global_ctx->mmp_update_thread) {
1729 global_ctx->mmp_update_thread =
1730 ctx->thread_info.et_thread_index + 1;
1733 e2fsck_pass1_block_map_w_unlock(ctx);
1736 /* only one active thread could update mmp block. */
1737 e2fsck_pass1_block_map_r_lock(ctx);
1738 if (global_ctx->mmp_update_thread ==
1739 ctx->thread_info.et_thread_index + 1)
1741 e2fsck_pass1_block_map_r_unlock(ctx);
1746 if (check_mmp && (ino % (fs->super->s_inodes_per_group * 4) == 1)) {
1747 if (e2fsck_mmp_update(fs))
1748 fatal_error(ctx, 0);
1750 old_op = ehandler_operation(eop_next_inode);
1751 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1753 if (ino > ino_threshold)
1754 pass1_readahead(ctx, &ra_group, &ino_threshold);
1755 ehandler_operation(old_op);
1756 if (e2fsck_should_abort(ctx)) {
1757 e2fsck_pass1_check_unlock(ctx);
1760 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1762 * If badblocks says badblocks is bad, offer to clear
1763 * the list, update the in-core bb list, and restart
1766 if (ino == EXT2_BAD_INO &&
1767 fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1771 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1772 ext2fs_badblocks_list_free(ctx->fs->badblocks);
1773 ctx->fs->badblocks = NULL;
1774 err = ext2fs_read_bb_inode(ctx->fs,
1775 &ctx->fs->badblocks);
1777 fix_problem(ctx, PR_1_ISCAN_ERROR,
1779 ctx->flags |= E2F_FLAG_ABORT;
1780 e2fsck_pass1_check_unlock(ctx);
1783 ctx->flags |= E2F_FLAG_RESTART;
1784 err = ext2fs_inode_scan_goto_blockgroup(scan,
1787 fix_problem(ctx, PR_1_ISCAN_ERROR,
1789 ctx->flags |= E2F_FLAG_ABORT;
1790 e2fsck_pass1_check_unlock(ctx);
1793 e2fsck_pass1_check_unlock(ctx);
1796 if (!ctx->inode_bb_map)
1798 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1799 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1800 e2fsck_pass1_check_unlock(ctx);
1803 if (pctx.errcode == EXT2_ET_SCAN_FINISHED) {
1804 e2fsck_pass1_check_unlock(ctx);
1808 pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1809 pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1810 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1811 ctx->flags |= E2F_FLAG_ABORT;
1812 e2fsck_pass1_check_unlock(ctx);
1816 e2fsck_pass1_check_unlock(ctx);
1820 if (ctx->global_ctx)
1821 ctx->thread_info.et_inode_number++;
1825 ctx->stashed_ino = ino;
1827 /* Clear trashed inode? */
1828 if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
1829 inode->i_links_count > 0 &&
1830 fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
1832 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1834 failed_csum = pctx.errcode != 0;
1837 * Check for inodes who might have been part of the
1838 * orphaned list linked list. They should have gotten
1839 * dealt with by now, unless the list had somehow been
1842 * FIXME: In the future, inodes which are still in use
1843 * (and which are therefore) pending truncation should
1844 * be handled specially. Right now we just clear the
1845 * dtime field, and the normal e2fsck handling of
1846 * inodes where i_size and the inode blocks are
1847 * inconsistent is to fix i_size, instead of releasing
1848 * the extra blocks. This won't catch the inodes that
1849 * was at the end of the orphan list, but it's better
1850 * than nothing. The right answer is that there
1851 * shouldn't be any bugs in the orphan list handling. :-)
1853 if (inode->i_dtime && low_dtime_check &&
1854 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1855 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1856 inode->i_dtime = inode->i_links_count ?
1858 e2fsck_write_inode(ctx, ino, inode,
1864 if (inode->i_links_count) {
1865 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1866 ino, inode->i_links_count);
1868 pctx.num = inode->i_links_count;
1869 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1870 ctx->flags |= E2F_FLAG_ABORT;
1871 e2fsck_pass1_check_unlock(ctx);
1874 } else if ((ino >= EXT2_FIRST_INODE(fs->super)) &&
1875 !quota_inum_is_reserved(fs, ino)) {
1876 if (!inode->i_dtime && inode->i_mode) {
1877 if (fix_problem(ctx,
1878 PR_1_ZERO_DTIME, &pctx)) {
1879 ext2fs_set_dtime(fs, inode);
1880 e2fsck_write_inode(ctx, ino, inode,
1885 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1886 e2fsck_pass1_check_unlock(ctx);
1890 if ((inode->i_flags & EXT4_CASEFOLD_FL) &&
1891 ((!LINUX_S_ISDIR(inode->i_mode) &&
1892 fix_problem(ctx, PR_1_CASEFOLD_NONDIR, &pctx)) ||
1894 fix_problem(ctx, PR_1_CASEFOLD_FEATURE, &pctx)))) {
1895 inode->i_flags &= ~EXT4_CASEFOLD_FL;
1896 e2fsck_write_inode(ctx, ino, inode, "pass1");
1899 if (inode->i_flags & EXT4_EA_INODE_FL) {
1900 if (!LINUX_S_ISREG(inode->i_mode) &&
1901 fix_problem(ctx, PR_1_EA_INODE_NONREG, &pctx)) {
1902 inode->i_flags &= ~EXT4_EA_INODE_FL;
1903 e2fsck_write_inode(ctx, ino, inode, "pass1");
1905 if (inode->i_flags & EXT4_EA_INODE_FL)
1906 if (!track_ea_inode(ctx, &pctx, ino))
1910 /* Conflicting inlinedata/extents inode flags? */
1911 if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
1912 (inode->i_flags & EXT4_EXTENTS_FL)) {
1913 int res = fix_inline_data_extents_file(ctx, ino, inode,
1917 /* skip FINISH_INODE_LOOP */
1918 e2fsck_pass1_check_unlock(ctx);
1923 /* Test for incorrect inline_data flags settings. */
1924 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
1925 (ino >= EXT2_FIRST_INODE(fs->super))) {
1928 pctx.errcode = get_inline_data_ea_size(fs, ino, inode,
1930 if (!pctx.errcode &&
1931 fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
1932 e2fsck_pass1_fix_lock(ctx);
1933 ext2fs_set_feature_inline_data(sb);
1934 ext2fs_mark_super_dirty(fs);
1935 e2fsck_pass1_fix_unlock(ctx);
1937 } else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
1938 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1939 /* skip FINISH_INODE_LOOP */
1940 e2fsck_pass1_check_unlock(ctx);
1945 /* Test for inline data flag but no attr */
1946 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
1947 (ino >= EXT2_FIRST_INODE(fs->super))) {
1954 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1955 err = get_inline_data_ea_size(fs, ino, inode, &size);
1956 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1957 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1961 /* Everything is awesome... */
1963 case EXT2_ET_BAD_EA_BLOCK_NUM:
1964 case EXT2_ET_BAD_EA_HASH:
1965 case EXT2_ET_BAD_EA_HEADER:
1966 case EXT2_ET_EA_BAD_NAME_LEN:
1967 case EXT2_ET_EA_BAD_VALUE_SIZE:
1968 case EXT2_ET_EA_KEY_NOT_FOUND:
1969 case EXT2_ET_EA_NO_SPACE:
1970 case EXT2_ET_MISSING_EA_FEATURE:
1971 case EXT2_ET_INLINE_DATA_CANT_ITERATE:
1972 case EXT2_ET_INLINE_DATA_NO_BLOCK:
1973 case EXT2_ET_INLINE_DATA_NO_SPACE:
1974 case EXT2_ET_NO_INLINE_DATA:
1975 case EXT2_ET_EXT_ATTR_CSUM_INVALID:
1976 case EXT2_ET_EA_BAD_VALUE_OFFSET:
1977 case EXT2_ET_EA_INODE_CORRUPTED:
1978 /* broken EA or no system.data EA; truncate */
1979 if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
1981 err = ext2fs_inode_size_set(fs, inode, 0);
1984 ctx->flags |= E2F_FLAG_ABORT;
1985 e2fsck_pass1_check_unlock(ctx);
1988 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1989 memset(&inode->i_block, 0,
1990 sizeof(inode->i_block));
1991 e2fsck_write_inode(ctx, ino, inode,
1997 /* Some other kind of non-xattr error? */
1999 ctx->flags |= E2F_FLAG_ABORT;
2000 e2fsck_pass1_check_unlock(ctx);
2006 * Test for incorrect extent flag settings.
2008 * On big-endian machines we must be careful:
2009 * When the inode is read, the i_block array is not swapped
2010 * if the extent flag is set. Therefore if we are testing
2011 * for or fixing a wrongly-set flag, we must potentially
2012 * (un)swap before testing, or after fixing.
2016 * In this case the extents flag was set when read, so
2017 * extent_header_verify is ok. If the inode is cleared,
2018 * no need to swap... so no extra swapping here.
2020 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
2021 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2022 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
2023 if ((ext2fs_extent_header_verify(inode->i_block,
2024 sizeof(inode->i_block)) == 0) &&
2025 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
2026 e2fsck_pass1_fix_lock(ctx);
2027 ext2fs_set_feature_extents(sb);
2028 ext2fs_mark_super_dirty(fs);
2030 e2fsck_pass1_fix_unlock(ctx);
2031 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
2033 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2034 if (ino == EXT2_BAD_INO)
2035 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
2037 /* skip FINISH_INODE_LOOP */
2038 e2fsck_pass1_check_unlock(ctx);
2044 * For big-endian machines:
2045 * If the inode didn't have the extents flag set when it
2046 * was read, then the i_blocks array was swapped. To test
2047 * as an extents header, we must swap it back first.
2048 * IF we then set the extents flag, the entire i_block
2049 * array must be un/re-swapped to make it proper extents data.
2051 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
2052 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2053 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
2054 (LINUX_S_ISREG(inode->i_mode) ||
2055 LINUX_S_ISDIR(inode->i_mode))) {
2057 #ifdef WORDS_BIGENDIAN
2058 __u32 tmp_block[EXT2_N_BLOCKS];
2060 for (i = 0; i < EXT2_N_BLOCKS; i++)
2061 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
2064 ehp = inode->i_block;
2066 if ((ext2fs_extent_header_verify(ehp,
2067 sizeof(inode->i_block)) == 0) &&
2068 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
2069 inode->i_flags |= EXT4_EXTENTS_FL;
2070 #ifdef WORDS_BIGENDIAN
2071 memcpy(inode->i_block, tmp_block,
2072 sizeof(inode->i_block));
2074 e2fsck_write_inode(ctx, ino, inode, "pass1");
2079 if (ino == EXT2_BAD_INO) {
2080 struct process_block_struct pb;
2082 if ((failed_csum || inode->i_mode || inode->i_uid ||
2083 inode->i_gid || inode->i_links_count ||
2084 (inode->i_flags & EXT4_INLINE_DATA_FL) ||
2085 inode->i_file_acl) &&
2086 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
2087 memset(inode, 0, sizeof(struct ext2_inode));
2088 e2fsck_write_inode(ctx, ino, inode,
2093 e2fsck_pass1_block_map_r_lock(ctx);
2094 pctx.errcode = ext2fs_copy_bitmap(ctx->global_ctx ?
2095 ctx->global_ctx->block_found_map :
2096 ctx->block_found_map, &pb.fs_meta_blocks);
2097 e2fsck_pass1_block_map_r_unlock(ctx);
2100 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
2101 ctx->flags |= E2F_FLAG_ABORT;
2102 e2fsck_pass1_check_unlock(ctx);
2105 pb.ino = EXT2_BAD_INO;
2106 pb.num_blocks = pb.last_block = 0;
2107 pb.last_db_block = -1;
2108 pb.num_illegal_blocks = 0;
2109 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
2110 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
2114 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
2115 block_buf, process_bad_block, &pb);
2116 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
2118 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
2119 ctx->flags |= E2F_FLAG_ABORT;
2120 e2fsck_pass1_check_unlock(ctx);
2124 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
2125 ctx->flags |= E2F_FLAG_ABORT;
2126 e2fsck_pass1_check_unlock(ctx);
2129 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2130 clear_problem_context(&pctx);
2131 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2132 e2fsck_pass1_check_unlock(ctx);
2134 } else if (ino == EXT2_ROOT_INO) {
2136 * Make sure the root inode is a directory; if
2137 * not, offer to clear it. It will be
2138 * regenerated in pass #3.
2140 if (!LINUX_S_ISDIR(inode->i_mode)) {
2141 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
2145 * If dtime is set, offer to clear it. mke2fs
2146 * version 0.2b created filesystems with the
2147 * dtime field set for the root and lost+found
2148 * directories. We won't worry about
2149 * /lost+found, since that can be regenerated
2150 * easily. But we will fix the root directory
2151 * as a special case.
2153 if (inode->i_dtime && inode->i_links_count) {
2154 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
2156 e2fsck_write_inode(ctx, ino, inode,
2161 } else if (ino == EXT2_JOURNAL_INO) {
2162 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2163 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
2164 if (!LINUX_S_ISREG(inode->i_mode) &&
2165 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
2167 inode->i_mode = LINUX_S_IFREG;
2168 e2fsck_write_inode(ctx, ino, inode,
2172 check_blocks(ctx, &pctx, block_buf, NULL);
2173 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2174 e2fsck_pass1_check_unlock(ctx);
2177 if ((inode->i_links_count ||
2178 inode->i_blocks || inode->i_block[0]) &&
2179 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
2181 memset(inode, 0, inode_size);
2182 ext2fs_icount_store(ctx->inode_link_info,
2184 e2fsck_write_inode_full(ctx, ino, inode,
2185 inode_size, "pass1");
2188 } else if (quota_inum_is_reserved(fs, ino)) {
2189 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2190 if (ext2fs_has_feature_quota(fs->super) &&
2191 quota_inum_is_super(fs->super, ino)) {
2192 if (!LINUX_S_ISREG(inode->i_mode) &&
2193 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
2195 inode->i_mode = LINUX_S_IFREG;
2196 e2fsck_write_inode(ctx, ino, inode,
2200 check_blocks(ctx, &pctx, block_buf, NULL);
2201 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2202 e2fsck_pass1_check_unlock(ctx);
2205 if ((inode->i_links_count ||
2206 inode->i_blocks || inode->i_block[0]) &&
2207 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
2209 memset(inode, 0, inode_size);
2210 ext2fs_icount_store(ctx->inode_link_info,
2212 e2fsck_write_inode_full(ctx, ino, inode,
2213 inode_size, "pass1");
2216 } else if (ino == fs->super->s_orphan_file_inum) {
2217 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2218 if (ext2fs_has_feature_orphan_file(fs->super)) {
2219 if (!LINUX_S_ISREG(inode->i_mode) &&
2220 fix_problem(ctx, PR_1_ORPHAN_FILE_BAD_MODE,
2222 inode->i_mode = LINUX_S_IFREG;
2223 e2fsck_write_inode(ctx, ino, inode,
2227 check_blocks(ctx, &pctx, block_buf, NULL);
2228 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2231 if ((inode->i_links_count ||
2232 inode->i_blocks || inode->i_block[0]) &&
2233 fix_problem(ctx, PR_1_ORPHAN_FILE_NOT_CLEAR,
2235 memset(inode, 0, inode_size);
2236 ext2fs_icount_store(ctx->inode_link_info, ino,
2238 e2fsck_write_inode_full(ctx, ino, inode,
2239 inode_size, "pass1");
2242 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
2243 problem_t problem = 0;
2245 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2246 if (ino == EXT2_BOOT_LOADER_INO) {
2247 if (LINUX_S_ISDIR(inode->i_mode))
2248 problem = PR_1_RESERVED_BAD_MODE;
2249 } else if (ino == EXT2_RESIZE_INO) {
2250 if (inode->i_mode &&
2251 !LINUX_S_ISREG(inode->i_mode))
2252 problem = PR_1_RESERVED_BAD_MODE;
2254 if (inode->i_mode != 0)
2255 problem = PR_1_RESERVED_BAD_MODE;
2258 if (fix_problem(ctx, problem, &pctx)) {
2260 e2fsck_write_inode(ctx, ino, inode,
2265 check_blocks(ctx, &pctx, block_buf, NULL);
2266 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2267 e2fsck_pass1_check_unlock(ctx);
2271 if (!inode->i_links_count) {
2272 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2273 e2fsck_pass1_check_unlock(ctx);
2277 * n.b. 0.3c ext2fs code didn't clear i_links_count for
2278 * deleted files. Oops.
2280 * Since all new ext2 implementations get this right,
2281 * we now assume that the case of non-zero
2282 * i_links_count and non-zero dtime means that we
2283 * should keep the file, not delete it.
2286 if (inode->i_dtime) {
2287 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
2289 e2fsck_write_inode(ctx, ino, inode, "pass1");
2294 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2295 switch (fs->super->s_creator_os) {
2297 frag = inode->osd2.hurd2.h_i_frag;
2298 fsize = inode->osd2.hurd2.h_i_fsize;
2304 if (inode->i_faddr || frag || fsize ||
2305 (!ext2fs_has_feature_largedir(fs->super) &&
2306 (LINUX_S_ISDIR(inode->i_mode) && inode->i_size_high)))
2307 mark_inode_bad(ctx, ino);
2308 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2309 !ext2fs_has_feature_64bit(fs->super) &&
2310 inode->osd2.linux2.l_i_file_acl_high != 0)
2311 mark_inode_bad(ctx, ino);
2312 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2313 !ext2fs_has_feature_huge_file(fs->super) &&
2314 (inode->osd2.linux2.l_i_blocks_hi != 0))
2315 mark_inode_bad(ctx, ino);
2316 if (inode->i_flags & EXT2_IMAGIC_FL) {
2318 if (!ctx->inode_imagic_map)
2319 alloc_imagic_map(ctx);
2320 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
2323 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
2324 inode->i_flags &= ~EXT2_IMAGIC_FL;
2325 e2fsck_write_inode(ctx, ino,
2332 check_inode_extra_space(ctx, &pctx, &ea_ibody_quota);
2333 check_is_really_dir(ctx, &pctx, block_buf);
2336 * ext2fs_inode_has_valid_blocks2 does not actually look
2337 * at i_block[] values, so not endian-sensitive here.
2339 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
2340 LINUX_S_ISLNK(inode->i_mode) &&
2341 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
2342 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
2343 inode->i_flags &= ~EXT4_EXTENTS_FL;
2344 e2fsck_write_inode(ctx, ino, inode, "pass1");
2348 if ((inode->i_flags & EXT4_ENCRYPT_FL) &&
2349 add_encrypted_file(ctx, &pctx) < 0)
2352 if (casefold_fs && inode->i_flags & EXT4_CASEFOLD_FL)
2353 ext2fs_mark_inode_bitmap2(ctx->inode_casefold_map, ino);
2355 if (LINUX_S_ISDIR(inode->i_mode)) {
2356 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
2357 e2fsck_add_dir_info(ctx, ino, 0);
2358 ctx->fs_directory_count++;
2359 if (inode->i_flags & EXT4_CASEFOLD_FL)
2360 add_casefolded_dir(ctx, ino);
2361 } else if (LINUX_S_ISREG (inode->i_mode)) {
2362 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
2363 ctx->fs_regular_count++;
2364 } else if (LINUX_S_ISCHR (inode->i_mode) &&
2365 e2fsck_pass1_check_device_inode(fs, inode)) {
2366 check_extents_inlinedata(ctx, &pctx);
2367 check_immutable(ctx, &pctx);
2368 check_size(ctx, &pctx);
2369 ctx->fs_chardev_count++;
2370 } else if (LINUX_S_ISBLK (inode->i_mode) &&
2371 e2fsck_pass1_check_device_inode(fs, inode)) {
2372 check_extents_inlinedata(ctx, &pctx);
2373 check_immutable(ctx, &pctx);
2374 check_size(ctx, &pctx);
2375 ctx->fs_blockdev_count++;
2376 } else if (LINUX_S_ISLNK (inode->i_mode) &&
2377 e2fsck_pass1_check_symlink(fs, ino, inode,
2379 check_immutable(ctx, &pctx);
2380 ctx->fs_symlinks_count++;
2381 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
2382 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2383 e2fsck_pass1_check_unlock(ctx);
2385 } else if (ext2fs_is_fast_symlink(inode)) {
2386 ctx->fs_fast_symlinks_count++;
2387 check_blocks(ctx, &pctx, block_buf,
2389 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2390 e2fsck_pass1_check_unlock(ctx);
2394 else if (LINUX_S_ISFIFO (inode->i_mode) &&
2395 e2fsck_pass1_check_device_inode(fs, inode)) {
2396 check_extents_inlinedata(ctx, &pctx);
2397 check_immutable(ctx, &pctx);
2398 check_size(ctx, &pctx);
2399 ctx->fs_fifo_count++;
2400 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
2401 e2fsck_pass1_check_device_inode(fs, inode)) {
2402 check_extents_inlinedata(ctx, &pctx);
2403 check_immutable(ctx, &pctx);
2404 check_size(ctx, &pctx);
2405 ctx->fs_sockets_count++;
2407 mark_inode_bad(ctx, ino);
2408 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2409 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
2410 if (inode->i_block[EXT2_IND_BLOCK])
2411 ctx->fs_ind_count++;
2412 if (inode->i_block[EXT2_DIND_BLOCK])
2413 ctx->fs_dind_count++;
2414 if (inode->i_block[EXT2_TIND_BLOCK])
2415 ctx->fs_tind_count++;
2417 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2418 !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
2419 (inode->i_block[EXT2_IND_BLOCK] ||
2420 inode->i_block[EXT2_DIND_BLOCK] ||
2421 inode->i_block[EXT2_TIND_BLOCK] ||
2422 ext2fs_file_acl_block(fs, inode))) {
2423 struct process_inode_block *itp;
2425 itp = &inodes_to_process[process_inode_count];
2427 itp->ea_ibody_quota = ea_ibody_quota;
2428 if (inode_size < sizeof(struct ext2_inode_large))
2429 memcpy(&itp->inode, inode, inode_size);
2431 memcpy(&itp->inode, inode, sizeof(itp->inode));
2432 process_inode_count++;
2434 check_blocks(ctx, &pctx, block_buf, &ea_ibody_quota);
2436 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2438 if (e2fsck_should_abort(ctx)) {
2439 e2fsck_pass1_check_unlock(ctx);
2443 if (process_inode_count >= ctx->process_inode_size) {
2444 process_inodes(ctx, block_buf, inodes_to_process,
2445 &process_inode_count);
2447 if (e2fsck_should_abort(ctx)) {
2448 e2fsck_pass1_check_unlock(ctx);
2452 e2fsck_pass1_check_unlock(ctx);
2454 process_inodes(ctx, block_buf, inodes_to_process,
2455 &process_inode_count);
2456 ext2fs_close_inode_scan(scan);
2459 if (ctx->ea_block_quota_blocks) {
2460 ea_refcount_free(ctx->ea_block_quota_blocks);
2461 ctx->ea_block_quota_blocks = 0;
2464 if (ctx->ea_block_quota_inodes) {
2465 ea_refcount_free(ctx->ea_block_quota_inodes);
2466 ctx->ea_block_quota_inodes = 0;
2469 /* We don't need the encryption policy => ID map any more */
2470 destroy_encryption_policy_map(ctx);
2472 if (ctx->flags & E2F_FLAG_RESTART) {
2474 * Only the master copy of the superblock and block
2475 * group descriptors are going to be written during a
2476 * restart, so set the superblock to be used to be the
2477 * master superblock.
2479 ctx->use_superblock = 0;
2483 if (ctx->large_dirs && !ext2fs_has_feature_largedir(fs->super)) {
2484 if (fix_problem(ctx, PR_2_FEATURE_LARGE_DIRS, &pctx)) {
2485 ext2fs_set_feature_largedir(fs->super);
2486 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2487 ext2fs_mark_super_dirty(fs);
2489 if (fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
2490 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
2491 ext2fs_update_dynamic_rev(fs);
2492 ext2fs_mark_super_dirty(fs);
2496 ctx->flags |= E2F_FLAG_ALLOC_OK;
2497 ext2fs_free_mem(&inodes_to_process);
2499 e2fsck_use_inode_shortcuts(ctx, 0);
2500 ext2fs_free_mem(&inodes_to_process);
2501 inodes_to_process = 0;
2504 ext2fs_close_inode_scan(scan);
2506 ext2fs_free_mem(&block_buf);
2508 ext2fs_free_mem(&inode);
2511 * The l+f inode may have been cleared, so zap it now and
2512 * later passes will recalculate it if necessary
2514 ctx->lost_and_found = 0;
2516 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
2517 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
2519 ctx->invalid_bitmaps++;
2521 /* reset update_thread after this thread exit */
2522 e2fsck_pass1_block_map_w_lock(ctx);
2524 global_ctx->mmp_update_thread = 0;
2525 e2fsck_pass1_block_map_w_unlock(ctx);
2530 static errcode_t e2fsck_pass1_copy_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2531 ext2fs_generic_bitmap *dest)
2535 ret = ext2fs_copy_bitmap(*src, dest);
2544 static void e2fsck_pass1_free_bitmap(ext2fs_generic_bitmap *bitmap)
2547 ext2fs_free_generic_bmap(*bitmap);
2553 static errcode_t e2fsck_pass1_merge_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2554 ext2fs_generic_bitmap *dest)
2559 if (*dest == NULL) {
2563 ret = ext2fs_merge_bitmap(*src, *dest, NULL, NULL);
2573 static errcode_t e2fsck_pass1_copy_fs(ext2_filsys dest, e2fsck_t src_context,
2578 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2579 dest->inode_map = NULL;
2580 dest->block_map = NULL;
2581 dest->badblocks = NULL;
2583 dest->dblist->fs = dest;
2584 if (src->block_map) {
2585 retval = e2fsck_pass1_copy_bitmap(dest, &src->block_map,
2590 if (src->inode_map) {
2591 retval = e2fsck_pass1_copy_bitmap(dest, &src->inode_map,
2597 if (src->badblocks) {
2598 retval = ext2fs_badblocks_copy(src->badblocks,
2604 /* disable it for now */
2605 src_context->openfs_flags &= ~EXT2_FLAG_EXCLUSIVE;
2606 retval = ext2fs_open_channel(dest, src_context->io_options,
2607 src_context->io_manager,
2608 src_context->openfs_flags,
2609 src->io->block_size);
2613 /* Block size might not be default */
2614 io_channel_set_blksize(dest->io, src->io->block_size);
2615 ehandler_init(dest->io);
2617 assert(dest->io->magic == src->io->magic);
2618 assert(dest->io->manager == src->io->manager);
2619 assert(strcmp(dest->io->name, src->io->name) == 0);
2620 assert(dest->io->block_size == src->io->block_size);
2621 assert(dest->io->read_error == src->io->read_error);
2622 assert(dest->io->write_error == src->io->write_error);
2623 assert(dest->io->refcount == src->io->refcount);
2624 assert(dest->io->flags == src->io->flags);
2625 assert(dest->io->app_data == dest);
2626 assert(src->io->app_data == src);
2627 assert(dest->io->align == src->io->align);
2629 /* The data should be written to disk immediately */
2630 dest->io->flags |= CHANNEL_FLAGS_WRITETHROUGH;
2631 /* icache will be rebuilt if needed, so do not copy from @src */
2636 static int e2fsck_pass1_merge_fs(ext2_filsys dest, ext2_filsys src)
2638 struct ext2_inode_cache *icache = dest->icache;
2639 errcode_t retval = 0;
2641 io_channel dest_image_io;
2642 ext2fs_inode_bitmap inode_map;
2643 ext2fs_block_bitmap block_map;
2644 ext2_badblocks_list badblocks;
2647 e2fsck_t dest_ctx = dest->priv_data;
2650 dest_image_io = dest->image_io;
2651 inode_map = dest->inode_map;
2652 block_map = dest->block_map;
2653 badblocks = dest->badblocks;
2654 dblist = dest->dblist;
2655 flags = dest->flags;
2657 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2659 dest->image_io = dest_image_io;
2660 dest->icache = icache;
2661 dest->inode_map = inode_map;
2662 dest->block_map = block_map;
2663 dest->badblocks = badblocks;
2664 dest->dblist = dblist;
2665 dest->priv_data = dest_ctx;
2667 dest->dblist->fs = dest;
2668 dest->flags = src->flags | flags;
2669 if (!(src->flags & EXT2_FLAG_VALID) || !(flags & EXT2_FLAG_VALID))
2670 ext2fs_unmark_valid(dest);
2673 ext2fs_free_inode_cache(src->icache);
2677 retval = e2fsck_pass1_merge_bitmap(dest, &src->inode_map,
2682 retval = e2fsck_pass1_merge_bitmap(dest, &src->block_map,
2689 retval = ext2fs_merge_dblist(src->dblist,
2694 dest->dblist = src->dblist;
2695 dest->dblist->fs = dest;
2700 if (src->badblocks) {
2701 if (dest->badblocks == NULL)
2702 retval = ext2fs_badblocks_copy(src->badblocks,
2705 retval = ext2fs_badblocks_merge(src->badblocks,
2709 io_channel_close(src->io);
2711 ext2fs_free_generic_bmap(src->inode_map);
2713 ext2fs_free_generic_bmap(src->block_map);
2715 ext2fs_badblocks_list_free(src->badblocks);
2717 ext2fs_free_dblist(src->dblist);
2722 static void e2fsck_pass1_copy_invalid_bitmaps(e2fsck_t global_ctx,
2723 e2fsck_t thread_ctx)
2726 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2727 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2728 dgrp_t total = grp_end - grp_start;
2730 thread_ctx->invalid_inode_bitmap_flag =
2731 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2732 "invalid_inode_bitmap");
2733 thread_ctx->invalid_block_bitmap_flag =
2734 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2735 "invalid_block_bitmap");
2736 thread_ctx->invalid_inode_table_flag =
2737 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2738 "invalid_inode_table");
2740 memcpy(thread_ctx->invalid_block_bitmap_flag,
2741 &global_ctx->invalid_block_bitmap_flag[grp_start],
2742 total * sizeof(int));
2743 memcpy(thread_ctx->invalid_inode_bitmap_flag,
2744 &global_ctx->invalid_inode_bitmap_flag[grp_start],
2745 total * sizeof(int));
2746 memcpy(thread_ctx->invalid_inode_table_flag,
2747 &global_ctx->invalid_inode_table_flag[grp_start],
2748 total * sizeof(int));
2750 thread_ctx->invalid_bitmaps = 0;
2751 for (i = grp_start, j = 0; i < grp_end; i++, j++) {
2752 if (thread_ctx->invalid_block_bitmap_flag[j])
2753 thread_ctx->invalid_bitmaps++;
2754 if (thread_ctx->invalid_inode_bitmap_flag[j])
2755 thread_ctx->invalid_bitmaps++;
2756 if (thread_ctx->invalid_inode_table_flag[j])
2757 thread_ctx->invalid_bitmaps++;
2761 static void e2fsck_pass1_merge_invalid_bitmaps(e2fsck_t global_ctx,
2762 e2fsck_t thread_ctx)
2764 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2765 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2766 dgrp_t total = grp_end - grp_start;
2768 memcpy(&global_ctx->invalid_block_bitmap_flag[grp_start],
2769 thread_ctx->invalid_block_bitmap_flag, total * sizeof(int));
2770 memcpy(&global_ctx->invalid_inode_bitmap_flag[grp_start],
2771 thread_ctx->invalid_inode_bitmap_flag, total * sizeof(int));
2772 memcpy(&global_ctx->invalid_inode_table_flag[grp_start],
2773 thread_ctx->invalid_inode_table_flag, total * sizeof(int));
2774 global_ctx->invalid_bitmaps += thread_ctx->invalid_bitmaps;
2777 static errcode_t e2fsck_pass1_thread_prepare(e2fsck_t global_ctx, e2fsck_t *thread_ctx,
2778 int thread_index, int num_threads,
2779 dgrp_t average_group)
2782 e2fsck_t thread_context;
2783 ext2_filsys thread_fs;
2784 ext2_filsys global_fs = global_ctx->fs;
2785 struct e2fsck_thread *tinfo;
2787 assert(global_ctx->inode_used_map == NULL);
2788 assert(global_ctx->inode_dir_map == NULL);
2789 assert(global_ctx->inode_bb_map == NULL);
2790 assert(global_ctx->inode_imagic_map == NULL);
2791 assert(global_ctx->inode_reg_map == NULL);
2792 assert(global_ctx->inodes_to_rebuild == NULL);
2794 assert(global_ctx->block_found_map != NULL);
2795 assert(global_ctx->block_metadata_map != NULL);
2796 assert(global_ctx->block_dup_map != NULL);
2797 assert(global_ctx->block_ea_map == NULL);
2798 assert(global_ctx->fs->dblist == NULL);
2800 retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &thread_context);
2802 com_err(global_ctx->program_name, retval, "while allocating memory");
2805 memcpy(thread_context, global_ctx, sizeof(struct e2fsck_struct));
2806 thread_context->block_dup_map = NULL;
2807 thread_context->casefolded_dirs = NULL;
2809 retval = e2fsck_allocate_block_bitmap(global_ctx->fs,
2810 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
2811 "block_found_map", &thread_context->block_found_map);
2815 thread_context->global_ctx = global_ctx;
2816 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &thread_fs);
2818 com_err(global_ctx->program_name, retval, "while allocating memory");
2822 io_channel_flush_cleanup(global_fs->io);
2823 retval = e2fsck_pass1_copy_fs(thread_fs, global_ctx, global_fs);
2825 com_err(global_ctx->program_name, retval, "while copying fs");
2828 thread_fs->priv_data = thread_context;
2830 thread_context->thread_info.et_thread_index = thread_index;
2831 set_up_logging(thread_context);
2833 tinfo = &thread_context->thread_info;
2834 tinfo->et_group_start = average_group * thread_index;
2835 if (thread_index == global_fs->fs_num_threads - 1)
2836 tinfo->et_group_end = thread_fs->group_desc_count;
2838 tinfo->et_group_end = average_group * (thread_index + 1);
2839 tinfo->et_group_next = tinfo->et_group_start;
2840 tinfo->et_inode_number = 0;
2841 tinfo->et_log_buf[0] = '\0';
2842 tinfo->et_log_length = 0;
2843 if (thread_context->options & E2F_OPT_MULTITHREAD)
2844 log_out(thread_context, _("Scan group range [%d, %d)\n"),
2845 tinfo->et_group_start, tinfo->et_group_end);
2846 thread_context->fs = thread_fs;
2847 retval = quota_init_context(&thread_context->qctx, thread_fs, 0);
2849 com_err(global_ctx->program_name, retval,
2850 "while init quota context");
2853 *thread_ctx = thread_context;
2854 e2fsck_pass1_copy_invalid_bitmaps(global_ctx, thread_context);
2857 ext2fs_free_mem(&thread_fs);
2859 if (thread_context->block_found_map)
2860 ext2fs_free_mem(&thread_context->block_found_map);
2861 ext2fs_free_mem(&thread_context);
2865 static void e2fsck_pass1_merge_dir_info(e2fsck_t global_ctx, e2fsck_t thread_ctx)
2867 if (thread_ctx->dir_info == NULL)
2870 if (global_ctx->dir_info == NULL) {
2871 global_ctx->dir_info = thread_ctx->dir_info;
2872 thread_ctx->dir_info = NULL;
2876 e2fsck_merge_dir_info(global_ctx, thread_ctx->dir_info,
2877 global_ctx->dir_info);
2880 static void e2fsck_pass1_merge_dx_dir(e2fsck_t global_ctx, e2fsck_t thread_ctx)
2882 if (thread_ctx->dx_dir_info == NULL)
2885 if (global_ctx->dx_dir_info == NULL) {
2886 global_ctx->dx_dir_info = thread_ctx->dx_dir_info;
2887 global_ctx->dx_dir_info_size = thread_ctx->dx_dir_info_size;
2888 global_ctx->dx_dir_info_count = thread_ctx->dx_dir_info_count;
2889 thread_ctx->dx_dir_info = NULL;
2893 e2fsck_merge_dx_dir(global_ctx, thread_ctx);
2896 static inline errcode_t
2897 e2fsck_pass1_merge_icount(ext2_icount_t *dest_icount,
2898 ext2_icount_t *src_icount)
2901 if (*dest_icount == NULL) {
2902 *dest_icount = *src_icount;
2907 ret = ext2fs_icount_merge(*src_icount,
2917 static errcode_t e2fsck_pass1_merge_icounts(e2fsck_t global_ctx, e2fsck_t thread_ctx)
2921 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_count,
2922 &thread_ctx->inode_count);
2925 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_link_info,
2926 &thread_ctx->inode_link_info);
2931 static errcode_t e2fsck_pass1_merge_dirs_to_hash(e2fsck_t global_ctx,
2932 e2fsck_t thread_ctx)
2934 errcode_t retval = 0;
2936 if (!thread_ctx->dirs_to_hash)
2939 if (!global_ctx->dirs_to_hash)
2940 retval = ext2fs_badblocks_copy(thread_ctx->dirs_to_hash,
2941 &global_ctx->dirs_to_hash);
2943 retval = ext2fs_badblocks_merge(thread_ctx->dirs_to_hash,
2944 global_ctx->dirs_to_hash);
2949 static errcode_t e2fsck_pass1_merge_ea_inode_refs(e2fsck_t global_ctx,
2950 e2fsck_t thread_ctx)
2952 ea_value_t thread_count, global_count;
2956 if (!thread_ctx->ea_inode_refs)
2959 if (!global_ctx->ea_inode_refs) {
2960 global_ctx->ea_inode_refs = thread_ctx->ea_inode_refs;
2961 thread_ctx->ea_inode_refs = NULL;
2965 ea_refcount_intr_begin(thread_ctx->ea_inode_refs);
2967 if ((ino = ea_refcount_intr_next(thread_ctx->ea_inode_refs,
2968 &thread_count)) == 0)
2970 ea_refcount_fetch(global_ctx->ea_inode_refs,
2971 ino, &global_count);
2972 retval = ea_refcount_store(global_ctx->ea_inode_refs,
2973 ino, thread_count + global_count);
2981 static ea_value_t ea_refcount_usage(e2fsck_t ctx, blk64_t blk,
2984 ea_value_t count_cur;
2985 ea_value_t count_extra = 0;
2986 ea_value_t count_orig;
2988 ea_refcount_fetch(ctx->refcount_orig, blk, &count_orig);
2989 ea_refcount_fetch(ctx->refcount, blk, &count_cur);
2990 /* most of time this is not needed */
2991 if (ctx->refcount_extra && count_cur == 0)
2992 ea_refcount_fetch(ctx->refcount_extra, blk, &count_extra);
2999 return count_orig + count_extra - count_cur;
3002 static errcode_t e2fsck_pass1_merge_ea_refcount(e2fsck_t global_ctx,
3003 e2fsck_t thread_ctx)
3007 errcode_t retval = 0;
3009 if (!thread_ctx->refcount)
3012 if (!global_ctx->refcount) {
3013 global_ctx->refcount = thread_ctx->refcount;
3014 thread_ctx->refcount = NULL;
3015 global_ctx->refcount_extra = thread_ctx->refcount;
3016 thread_ctx->refcount_extra = NULL;
3020 ea_refcount_intr_begin(thread_ctx->refcount);
3022 if ((blk = ea_refcount_intr_next(thread_ctx->refcount,
3026 * this EA has never seen before, so just store its
3027 * refcount and refcount_extra into global_ctx if needed.
3029 if (!global_ctx->block_ea_map ||
3030 !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
3034 retval = ea_refcount_store(global_ctx->refcount,
3039 if (count > 0 || !thread_ctx->refcount_extra)
3041 ea_refcount_fetch(thread_ctx->refcount_extra, blk,
3046 if (!global_ctx->refcount_extra) {
3047 retval = ea_refcount_create(0,
3048 &global_ctx->refcount_extra);
3052 retval = ea_refcount_store(global_ctx->refcount_extra,
3058 ea_value_t thread_usage;
3059 ea_value_t global_usage;
3062 thread_usage = ea_refcount_usage(thread_ctx,
3064 global_usage = ea_refcount_usage(global_ctx,
3066 if (thread_usage + global_usage <= orig) {
3067 new = orig - thread_usage - global_usage;
3068 retval = ea_refcount_store(global_ctx->refcount,
3074 /* update it is as zero */
3075 retval = ea_refcount_store(global_ctx->refcount,
3079 /* Ooops, this EA was referenced more than it stated */
3080 if (!global_ctx->refcount_extra) {
3081 retval = ea_refcount_create(0,
3082 &global_ctx->refcount_extra);
3086 new = global_usage + thread_usage - orig;
3087 retval = ea_refcount_store(global_ctx->refcount_extra,
3097 static errcode_t e2fsck_pass1_merge_casefolded_dirs(e2fsck_t global_ctx,
3098 e2fsck_t thread_ctx)
3100 errcode_t retval = 0;
3102 if (!thread_ctx->casefolded_dirs)
3105 if (!global_ctx->casefolded_dirs)
3106 retval = ext2fs_badblocks_copy(thread_ctx->casefolded_dirs,
3107 &global_ctx->casefolded_dirs);
3109 retval = ext2fs_badblocks_merge(thread_ctx->casefolded_dirs,
3110 global_ctx->casefolded_dirs);
3115 static errcode_t e2fsck_pass1_merge_context(e2fsck_t global_ctx,
3116 e2fsck_t thread_ctx)
3118 ext2_filsys global_fs = global_ctx->fs;
3122 global_ctx->fs_directory_count += thread_ctx->fs_directory_count;
3123 global_ctx->fs_regular_count += thread_ctx->fs_regular_count;
3124 global_ctx->fs_blockdev_count += thread_ctx->fs_blockdev_count;
3125 global_ctx->fs_chardev_count += thread_ctx->fs_chardev_count;
3126 global_ctx->fs_links_count += thread_ctx->fs_links_count;
3127 global_ctx->fs_symlinks_count += thread_ctx->fs_symlinks_count;
3128 global_ctx->fs_fast_symlinks_count += thread_ctx->fs_fast_symlinks_count;
3129 global_ctx->fs_fifo_count += thread_ctx->fs_fifo_count;
3130 global_ctx->fs_total_count += thread_ctx->fs_total_count;
3131 global_ctx->fs_badblocks_count += thread_ctx->fs_badblocks_count;
3132 global_ctx->fs_sockets_count += thread_ctx->fs_sockets_count;
3133 global_ctx->fs_ind_count += thread_ctx->fs_ind_count;
3134 global_ctx->fs_dind_count += thread_ctx->fs_dind_count;
3135 global_ctx->fs_tind_count += thread_ctx->fs_tind_count;
3136 global_ctx->fs_fragmented += thread_ctx->fs_fragmented;
3137 global_ctx->fs_fragmented_dir += thread_ctx->fs_fragmented_dir;
3138 global_ctx->large_files += thread_ctx->large_files;
3139 /* threads might enable E2F_OPT_YES */
3140 global_ctx->options |= thread_ctx->options;
3141 global_ctx->flags |= thread_ctx->flags;
3143 * The l+f inode may have been cleared, so zap it now and
3144 * later passes will recalculate it if necessary
3146 global_ctx->lost_and_found = 0;
3147 /* merge extent depth count */
3148 for (i = 0; i < MAX_EXTENT_DEPTH_COUNT; i++)
3149 global_ctx->extent_depth_count[i] +=
3150 thread_ctx->extent_depth_count[i];
3152 e2fsck_pass1_merge_dir_info(global_ctx, thread_ctx);
3153 e2fsck_pass1_merge_dx_dir(global_ctx, thread_ctx);
3155 retval = e2fsck_pass1_merge_fs(global_ctx->fs, thread_ctx->fs);
3157 com_err(global_ctx->program_name, 0, _("while merging fs\n"));
3160 retval = e2fsck_pass1_merge_icounts(global_ctx, thread_ctx);
3162 com_err(global_ctx->program_name, 0,
3163 _("while merging icounts\n"));
3167 retval = e2fsck_pass1_merge_dirs_to_hash(global_ctx, thread_ctx);
3169 com_err(global_ctx->program_name, 0,
3170 _("while merging dirs to hash\n"));
3174 e2fsck_pass1_merge_ea_inode_refs(global_ctx, thread_ctx);
3175 e2fsck_pass1_merge_ea_refcount(global_ctx, thread_ctx);
3176 retval = quota_merge_and_update_usage(global_ctx->qctx,
3181 retval = e2fsck_pass1_merge_casefolded_dirs(global_ctx, thread_ctx);
3183 com_err(global_ctx->program_name, 0,
3184 _("while merging casefolded dirs\n"));
3188 e2fsck_pass1_merge_invalid_bitmaps(global_ctx, thread_ctx);
3190 retval = e2fsck_pass1_merge_bitmap(global_fs,
3191 &thread_ctx->inode_used_map,
3192 &global_ctx->inode_used_map);
3196 retval = e2fsck_pass1_merge_bitmap(global_fs,
3197 &thread_ctx->inode_bad_map,
3198 &global_ctx->inode_bad_map);
3201 retval = e2fsck_pass1_merge_bitmap(global_fs,
3202 &thread_ctx->inode_dir_map,
3203 &global_ctx->inode_dir_map);
3206 retval = e2fsck_pass1_merge_bitmap(global_fs,
3207 &thread_ctx->inode_bb_map,
3208 &global_ctx->inode_bb_map);
3211 retval = e2fsck_pass1_merge_bitmap(global_fs,
3212 &thread_ctx->inode_imagic_map,
3213 &global_ctx->inode_imagic_map);
3216 retval = e2fsck_pass1_merge_bitmap(global_fs,
3217 &thread_ctx->inode_reg_map,
3218 &global_ctx->inode_reg_map);
3221 retval = e2fsck_pass1_merge_bitmap(global_fs,
3222 &thread_ctx->inodes_to_rebuild,
3223 &global_ctx->inodes_to_rebuild);
3226 retval = e2fsck_pass1_merge_bitmap(global_fs,
3227 &thread_ctx->block_ea_map,
3228 &global_ctx->block_ea_map);
3232 if (ext2fs_has_feature_shared_blocks(global_fs->super) &&
3233 !(global_ctx->options & E2F_OPT_UNSHARE_BLOCKS))
3236 * This need be done after merging block_ea_map
3237 * because ea block might be shared, we need exclude
3238 * them from dup blocks.
3240 e2fsck_pass1_block_map_w_lock(thread_ctx);
3241 retval = ext2fs_merge_bitmap(thread_ctx->block_found_map,
3242 global_ctx->block_found_map,
3243 global_ctx->block_dup_map,
3244 global_ctx->block_ea_map);
3245 e2fsck_pass1_block_map_w_unlock(thread_ctx);
3246 if (retval == EEXIST)
3247 global_ctx->flags |= E2F_FLAG_DUP_BLOCK;
3252 static int e2fsck_pass1_thread_join(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3256 retval = e2fsck_pass1_merge_context(global_ctx, thread_ctx);
3257 ext2fs_free_mem(&thread_ctx->fs);
3258 if (thread_ctx->logf)
3259 fclose(thread_ctx->logf);
3260 if (thread_ctx->problem_logf) {
3261 fputs("</problem_log>\n", thread_ctx->problem_logf);
3262 fclose(thread_ctx->problem_logf);
3265 quota_release_context(&thread_ctx->qctx);
3267 * @block_metadata_map and @block_dup_map are
3268 * shared, so we don't free them.
3270 thread_ctx->block_metadata_map = NULL;
3271 thread_ctx->block_dup_map = NULL;
3272 e2fsck_reset_context(thread_ctx);
3273 ext2fs_free_mem(&thread_ctx);
3278 static int e2fsck_pass1_threads_join(struct e2fsck_thread_info *infos,
3279 e2fsck_t global_ctx)
3283 struct e2fsck_thread_info *pinfo;
3284 int num_threads = global_ctx->pfs_num_threads;
3287 /* merge invalid bitmaps will recalculate it */
3288 global_ctx->invalid_bitmaps = 0;
3289 for (i = 0; i < num_threads; i++) {
3292 if (!pinfo->eti_started)
3295 rc = pthread_join(pinfo->eti_thread_id, NULL);
3297 com_err(global_ctx->program_name, rc,
3298 _("while joining thread\n"));
3302 rc = e2fsck_pass1_thread_join(global_ctx, infos[i].eti_thread_ctx);
3304 com_err(global_ctx->program_name, rc,
3305 _("while joining pass1 thread\n"));
3315 static void *e2fsck_pass1_thread(void *arg)
3317 struct e2fsck_thread_info *info = arg;
3318 e2fsck_t thread_ctx = info->eti_thread_ctx;
3319 #ifdef DEBUG_THREADS
3320 struct e2fsck_thread_debug *thread_debug = info->eti_debug;
3323 #ifdef DEBUG_THREADS
3324 pthread_mutex_lock(&thread_debug->etd_mutex);
3325 while (info->eti_thread_index > thread_debug->etd_finished_threads) {
3326 pthread_cond_wait(&thread_debug->etd_cond,
3327 &thread_debug->etd_mutex);
3329 pthread_mutex_unlock(&thread_debug->etd_mutex);
3332 #ifdef HAVE_SETJMP_H
3334 * When fatal_error() happens, jump to here. The thread
3335 * context's flags will be saved, but its abort_loc will
3336 * be overwritten by original jump buffer for the later
3339 if (setjmp(thread_ctx->abort_loc)) {
3340 thread_ctx->flags &= ~E2F_FLAG_SETJMP_OK;
3343 thread_ctx->flags |= E2F_FLAG_SETJMP_OK;
3346 e2fsck_pass1_run(thread_ctx);
3349 if (thread_ctx->options & E2F_OPT_MULTITHREAD)
3351 _("Scanned group range [%u, %u), inodes %u\n"),
3352 thread_ctx->thread_info.et_group_start,
3353 thread_ctx->thread_info.et_group_end,
3354 thread_ctx->thread_info.et_inode_number);
3356 #ifdef DEBUG_THREADS
3357 pthread_mutex_lock(&thread_debug->etd_mutex);
3358 thread_debug->etd_finished_threads++;
3359 pthread_cond_broadcast(&thread_debug->etd_cond);
3360 pthread_mutex_unlock(&thread_debug->etd_mutex);
3366 static dgrp_t ext2fs_get_avg_group(ext2_filsys fs)
3369 dgrp_t average_group;
3370 unsigned flexbg_size;
3372 if (fs->fs_num_threads <= 1)
3373 return fs->group_desc_count;
3375 average_group = fs->group_desc_count / fs->fs_num_threads;
3376 if (average_group <= 1)
3379 if (ext2fs_has_feature_flex_bg(fs->super)) {
3382 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3383 if (average_group % flexbg_size) {
3384 times = average_group / flexbg_size;
3385 average_group = times * flexbg_size;
3389 return average_group;
3391 return fs->group_desc_count;
3395 static int e2fsck_pass1_threads_start(struct e2fsck_thread_info **pinfo,
3396 e2fsck_t global_ctx)
3398 struct e2fsck_thread_info *infos;
3399 pthread_attr_t attr;
3402 struct e2fsck_thread_info *tmp_pinfo;
3404 e2fsck_t thread_ctx;
3405 dgrp_t average_group;
3406 int num_threads = global_ctx->pfs_num_threads;
3407 #ifdef DEBUG_THREADS
3408 struct e2fsck_thread_debug thread_debug =
3409 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0};
3411 thread_debug.etd_finished_threads = 0;
3414 retval = pthread_attr_init(&attr);
3416 com_err(global_ctx->program_name, retval,
3417 _("while setting pthread attribute\n"));
3421 infos = calloc(num_threads, sizeof(struct e2fsck_thread_info));
3422 if (infos == NULL) {
3424 com_err(global_ctx->program_name, retval,
3425 _("while allocating memory for threads\n"));
3426 pthread_attr_destroy(&attr);
3430 average_group = ext2fs_get_avg_group(global_ctx->fs);
3431 for (i = 0; i < num_threads; i++) {
3432 tmp_pinfo = &infos[i];
3433 tmp_pinfo->eti_thread_index = i;
3434 #ifdef DEBUG_THREADS
3435 tmp_pinfo->eti_debug = &thread_debug;
3437 retval = e2fsck_pass1_thread_prepare(global_ctx, &thread_ctx,
3441 com_err(global_ctx->program_name, retval,
3442 _("while preparing pass1 thread\n"));
3445 tmp_pinfo->eti_thread_ctx = thread_ctx;
3447 retval = pthread_create(&tmp_pinfo->eti_thread_id, &attr,
3448 &e2fsck_pass1_thread, tmp_pinfo);
3450 com_err(global_ctx->program_name, retval,
3451 _("while creating thread\n"));
3452 e2fsck_pass1_thread_join(global_ctx, thread_ctx);
3456 tmp_pinfo->eti_started = 1;
3459 /* destroy the thread attribute object, since it is no longer needed */
3460 ret = pthread_attr_destroy(&attr);
3462 com_err(global_ctx->program_name, ret,
3463 _("while destroying thread attribute\n"));
3469 e2fsck_pass1_threads_join(infos, global_ctx);
3476 static void e2fsck_pass1_multithread(e2fsck_t global_ctx)
3478 struct e2fsck_thread_info *infos = NULL;
3481 retval = e2fsck_pass1_threads_start(&infos, global_ctx);
3483 com_err(global_ctx->program_name, retval,
3484 _("while starting pass1 threads\n"));
3488 retval = e2fsck_pass1_threads_join(infos, global_ctx);
3490 com_err(global_ctx->program_name, retval,
3491 _("while joining pass1 threads\n"));
3496 global_ctx->flags |= E2F_FLAG_ABORT;
3501 void e2fsck_pass1(e2fsck_t ctx)
3504 int need_single = 1;
3506 retval = e2fsck_pass1_prepare(ctx);
3510 if (ctx->pfs_num_threads > 1 || ctx->options & E2F_OPT_MULTITHREAD) {
3512 e2fsck_pass1_multithread(ctx);
3514 /* No lock is needed at this time */
3515 ctx->fs_need_locking = 0;
3518 e2fsck_pass1_run(ctx);
3519 e2fsck_pass1_post(ctx);
3522 #undef FINISH_INODE_LOOP
3525 * When the inode_scan routines call this callback at the end of the
3526 * glock group, call process_inodes.
3528 static errcode_t scan_callback(ext2_filsys fs,
3529 ext2_inode_scan scan EXT2FS_ATTR((unused)),
3530 dgrp_t group, void * priv_data)
3532 struct scan_callback_struct *scan_struct;
3534 struct e2fsck_thread *tinfo;
3536 scan_struct = (struct scan_callback_struct *) priv_data;
3537 ctx = scan_struct->ctx;
3539 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf,
3540 scan_struct->inodes_to_process,
3541 scan_struct->process_inode_count);
3544 if ((ctx->progress)(ctx, 1, group+1,
3545 ctx->fs->group_desc_count))
3546 return EXT2_ET_CANCEL_REQUESTED;
3549 if (ctx->global_ctx) {
3550 tinfo = &ctx->thread_info;
3551 tinfo->et_group_next++;
3552 if (ctx->options & E2F_OPT_DEBUG &&
3553 ctx->options & E2F_OPT_MULTITHREAD)
3554 log_out(ctx, _("group %d finished\n"),
3555 tinfo->et_group_next);
3556 if (tinfo->et_group_next >= tinfo->et_group_end)
3557 return EXT2_ET_SCAN_FINISHED;
3565 * Process the inodes in the "inodes to process" list.
3567 static void process_inodes(e2fsck_t ctx, char *block_buf,
3568 struct process_inode_block *inodes_to_process,
3569 int *process_inode_count)
3572 struct ext2_inode *old_stashed_inode;
3573 ext2_ino_t old_stashed_ino;
3574 const char *old_operation;
3576 struct problem_context pctx;
3579 printf("begin process_inodes: ");
3581 if (*process_inode_count == 0)
3583 old_operation = ehandler_operation(0);
3584 old_stashed_inode = ctx->stashed_inode;
3585 old_stashed_ino = ctx->stashed_ino;
3586 qsort(inodes_to_process, *process_inode_count,
3587 sizeof(struct process_inode_block), process_inode_cmp);
3588 clear_problem_context(&pctx);
3589 for (i=0; i < *process_inode_count; i++) {
3590 pctx.inode = ctx->stashed_inode =
3591 (struct ext2_inode *) &inodes_to_process[i].inode;
3592 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
3595 printf("%u ", pctx.ino);
3597 sprintf(buf, _("reading indirect blocks of inode %u"),
3599 ehandler_operation(buf);
3600 check_blocks(ctx, &pctx, block_buf,
3601 &inodes_to_process[i].ea_ibody_quota);
3602 if (e2fsck_should_abort(ctx))
3605 ctx->stashed_inode = old_stashed_inode;
3606 ctx->stashed_ino = old_stashed_ino;
3607 *process_inode_count = 0;
3609 printf("end process inodes\n");
3611 ehandler_operation(old_operation);
3614 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
3616 const struct process_inode_block *ib_a =
3617 (const struct process_inode_block *) a;
3618 const struct process_inode_block *ib_b =
3619 (const struct process_inode_block *) b;
3622 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
3623 ib_b->inode.i_block[EXT2_IND_BLOCK]);
3626 * We only call process_inodes() for non-extent
3627 * inodes, so it's OK to pass NULL to
3628 * ext2fs_file_acl_block() here.
3630 ret = ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a->inode)) -
3631 ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b->inode));
3633 ret = ib_a->ino - ib_b->ino;
3638 * Mark an inode as being bad in some what
3640 static void mark_inode_bad(e2fsck_t ctx, ext2_ino_t ino)
3642 struct problem_context pctx;
3644 if (!ctx->inode_bad_map) {
3645 clear_problem_context(&pctx);
3647 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3648 _("bad inode map"), EXT2FS_BMAP64_RBTREE,
3649 "inode_bad_map", &ctx->inode_bad_map);
3652 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3653 /* Should never get here */
3654 ctx->flags |= E2F_FLAG_ABORT;
3658 ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
3661 static void add_casefolded_dir(e2fsck_t ctx, ext2_ino_t ino)
3663 struct problem_context pctx;
3665 if (!ctx->casefolded_dirs) {
3666 pctx.errcode = ext2fs_u32_list_create(&ctx->casefolded_dirs, 0);
3670 pctx.errcode = ext2fs_u32_list_add(ctx->casefolded_dirs, ino);
3671 if (pctx.errcode == 0)
3674 fix_problem(ctx, PR_1_ALLOCATE_CASEFOLDED_DIRLIST, &pctx);
3675 /* Should never get here */
3676 ctx->flags |= E2F_FLAG_ABORT;
3680 * This procedure will allocate the inode "bb" (badblock) map table
3682 static void alloc_bb_map(e2fsck_t ctx)
3684 struct problem_context pctx;
3686 clear_problem_context(&pctx);
3687 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3688 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
3689 "inode_bb_map", &ctx->inode_bb_map);
3692 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3693 /* Should never get here */
3694 ctx->flags |= E2F_FLAG_ABORT;
3700 * This procedure will allocate the inode imagic table
3702 static void alloc_imagic_map(e2fsck_t ctx)
3704 struct problem_context pctx;
3706 clear_problem_context(&pctx);
3707 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3708 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
3709 "inode_imagic_map", &ctx->inode_imagic_map);
3712 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3713 /* Should never get here */
3714 ctx->flags |= E2F_FLAG_ABORT;
3720 * Marks a block as in use, setting the dup_map if it's been set
3721 * already. Called by process_block and process_bad_block.
3723 * WARNING: Assumes checks have already been done to make sure block
3724 * is valid. This is true in both process_block and process_bad_block.
3726 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
3728 struct problem_context pctx;
3729 e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
3731 clear_problem_context(&pctx);
3733 if (is_blocks_used(ctx, block, 1)) {
3734 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
3735 !(ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
3738 ctx->flags |= E2F_FLAG_DUP_BLOCK;
3739 e2fsck_pass1_block_map_w_lock(ctx);
3740 ext2fs_fast_mark_block_bitmap2(global_ctx->block_dup_map, block);
3741 e2fsck_pass1_block_map_w_unlock(ctx);
3743 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
3748 * When cluster size is greater than one block, it is caller's responsibility
3749 * to make sure block parameter starts at a cluster boundary.
3751 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
3754 if (!is_blocks_used(ctx, block, num)) {
3755 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
3759 for (i = 0; i < num; i += EXT2FS_CLUSTER_RATIO(ctx->fs))
3760 mark_block_used(ctx, block + i);
3764 static errcode_t _INLINE_ e2fsck_write_ext_attr3(e2fsck_t ctx, blk64_t block,
3765 void *inbuf, ext2_ino_t inum)
3768 ext2_filsys fs = ctx->fs;
3770 e2fsck_pass1_fix_lock(ctx);
3771 retval = ext2fs_write_ext_attr3(fs, block, inbuf, inum);
3772 e2fsck_pass1_fix_unlock(ctx);
3777 * Adjust the extended attribute block's reference counts at the end
3778 * of pass 1, either by subtracting out references for EA blocks that
3779 * are still referenced in ctx->refcount, or by adding references for
3780 * EA blocks that had extra references as accounted for in
3781 * ctx->refcount_extra.
3783 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
3784 char *block_buf, int adjust_sign)
3786 struct ext2_ext_attr_header *header;
3787 struct problem_context pctx;
3788 ext2_filsys fs = ctx->fs;
3793 clear_problem_context(&pctx);
3795 ea_refcount_intr_begin(refcount);
3797 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
3800 pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
3803 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
3806 header = (struct ext2_ext_attr_header *) block_buf;
3807 pctx.blkcount = header->h_refcount;
3808 should_be = header->h_refcount + adjust_sign * (int)count;
3809 pctx.num = should_be;
3810 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
3811 header->h_refcount = should_be;
3812 pctx.errcode = e2fsck_write_ext_attr3(ctx, blk,
3816 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
3825 * Handle processing the extended attribute blocks
3827 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
3828 char *block_buf, struct ea_quota *ea_block_quota)
3830 ext2_filsys fs = ctx->fs;
3831 ext2_ino_t ino = pctx->ino;
3832 struct ext2_inode *inode = pctx->inode;
3835 struct ext2_ext_attr_header *header;
3836 struct ext2_ext_attr_entry *first, *entry;
3837 blk64_t quota_blocks = EXT2FS_C2B(fs, 1);
3838 __u64 quota_inodes = 0;
3839 region_t region = 0;
3840 int failed_csum = 0;
3842 ea_block_quota->blocks = 0;
3843 ea_block_quota->inodes = 0;
3845 blk = ext2fs_file_acl_block(fs, inode);
3850 * If the Extended attribute flag isn't set, then a non-zero
3851 * file acl means that the inode is corrupted.
3853 * Or if the extended attribute block is an invalid block,
3854 * then the inode is also corrupted.
3856 if (!ext2fs_has_feature_xattr(fs->super) ||
3857 (blk < fs->super->s_first_data_block) ||
3858 (blk >= ext2fs_blocks_count(fs->super))) {
3859 mark_inode_bad(ctx, ino);
3863 /* If ea bitmap hasn't been allocated, create it */
3864 if (!ctx->block_ea_map) {
3865 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
3866 _("ext attr block map"),
3867 EXT2FS_BMAP64_RBTREE, "block_ea_map",
3868 &ctx->block_ea_map);
3869 if (pctx->errcode) {
3871 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
3872 ctx->flags |= E2F_FLAG_ABORT;
3877 /* Create the EA refcount structure if necessary */
3878 if (!ctx->refcount) {
3879 pctx->errcode = ea_refcount_create(0,
3880 &ctx->refcount_orig);
3881 if (pctx->errcode) {
3883 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
3884 ctx->flags |= E2F_FLAG_ABORT;
3888 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
3889 if (pctx->errcode) {
3891 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
3892 ctx->flags |= E2F_FLAG_ABORT;
3898 /* Debugging text */
3899 printf("Inode %u has EA block %u\n", ino, blk);
3902 /* Have we seen this EA block before? */
3903 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
3904 ea_block_quota->blocks = EXT2FS_C2B(fs, 1);
3905 ea_block_quota->inodes = 0;
3907 if (ctx->ea_block_quota_blocks) {
3908 ea_refcount_fetch(ctx->ea_block_quota_blocks, blk,
3911 ea_block_quota->blocks = quota_blocks;
3914 if (ctx->ea_block_quota_inodes)
3915 ea_refcount_fetch(ctx->ea_block_quota_inodes, blk,
3916 &ea_block_quota->inodes);
3918 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
3920 /* Ooops, this EA was referenced more than it stated */
3921 if (!ctx->refcount_extra) {
3922 pctx->errcode = ea_refcount_create(0,
3923 &ctx->refcount_extra);
3924 if (pctx->errcode) {
3926 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
3927 ctx->flags |= E2F_FLAG_ABORT;
3931 ea_refcount_increment(ctx->refcount_extra, blk, 0);
3936 * OK, we haven't seen this EA block yet. So we need to
3940 pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
3941 if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
3944 } else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
3947 if (pctx->errcode &&
3948 fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
3952 header = (struct ext2_ext_attr_header *) block_buf;
3953 pctx->blk = ext2fs_file_acl_block(fs, inode);
3954 if (((ctx->ext_attr_ver == 1) &&
3955 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
3956 ((ctx->ext_attr_ver == 2) &&
3957 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
3958 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
3962 if (header->h_blocks != 1) {
3963 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
3967 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
3970 region = region_create(0, fs->blocksize);
3972 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
3973 ctx->flags |= E2F_FLAG_ABORT;
3976 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
3977 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
3981 first = (struct ext2_ext_attr_entry *)(header+1);
3982 end = block_buf + fs->blocksize;
3984 while ((char *)entry < end && *(__u32 *)entry) {
3987 if (region_allocate(region, (char *)entry - (char *)header,
3988 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
3989 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
3993 if ((ctx->ext_attr_ver == 1 &&
3994 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
3995 (ctx->ext_attr_ver == 2 &&
3996 entry->e_name_index == 0)) {
3997 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
4001 if (entry->e_value_inum == 0) {
4002 if (entry->e_value_size > EXT2_XATTR_SIZE_MAX ||
4003 (entry->e_value_offs + entry->e_value_size >
4005 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
4009 if (entry->e_value_size &&
4010 region_allocate(region, entry->e_value_offs,
4011 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
4012 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
4017 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
4018 entry->e_value_offs);
4019 if (entry->e_hash != hash)
4020 hash = ext2fs_ext_attr_hash_entry_signed(entry,
4021 block_buf + entry->e_value_offs);
4023 if (entry->e_hash != hash) {
4024 pctx->num = entry->e_hash;
4025 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
4027 entry->e_hash = hash;
4031 blk64_t entry_quota_blocks;
4033 if (!ext2fs_has_feature_ea_inode(fs->super) &&
4034 fix_problem(ctx, PR_1_EA_INODE_FEATURE, pctx)) {
4035 ext2fs_set_feature_ea_inode(fs->super);
4036 ext2fs_mark_super_dirty(fs);
4039 problem = check_large_ea_inode(ctx, entry, pctx,
4040 &entry_quota_blocks);
4041 if (problem && fix_problem(ctx, problem, pctx))
4044 quota_blocks += entry_quota_blocks;
4048 entry = EXT2_EXT_ATTR_NEXT(entry);
4050 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
4051 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4054 region_free(region);
4057 * We only get here if there was no other errors that were fixed.
4058 * If there was a checksum fail, ask to correct it.
4061 fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
4062 pctx->errcode = e2fsck_write_ext_attr3(ctx, blk, block_buf,
4068 if (quota_blocks != EXT2FS_C2B(fs, 1U)) {
4069 if (!ctx->ea_block_quota_blocks) {
4070 pctx->errcode = ea_refcount_create(0,
4071 &ctx->ea_block_quota_blocks);
4072 if (pctx->errcode) {
4077 ea_refcount_store(ctx->ea_block_quota_blocks, blk,
4082 if (!ctx->ea_block_quota_inodes) {
4083 pctx->errcode = ea_refcount_create(0,
4084 &ctx->ea_block_quota_inodes);
4085 if (pctx->errcode) {
4088 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4089 ctx->flags |= E2F_FLAG_ABORT;
4094 ea_refcount_store(ctx->ea_block_quota_inodes, blk,
4097 ea_block_quota->blocks = quota_blocks;
4098 ea_block_quota->inodes = quota_inodes;
4100 inc_ea_inode_refs(ctx, pctx, first, end);
4101 ea_refcount_store(ctx->refcount, blk, header->h_refcount - 1);
4102 ea_refcount_store(ctx->refcount_orig, blk, header->h_refcount);
4104 * It might be racy that this block has been merged in the
4107 if (!is_blocks_used(ctx, blk, 1))
4108 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, blk);
4109 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
4114 region_free(region);
4115 ext2fs_file_acl_block_set(fs, inode, 0);
4116 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
4120 /* Returns 1 if bad htree, 0 if OK */
4121 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
4122 ext2_ino_t ino, struct ext2_inode *inode,
4125 struct ext2_dx_root_info *root;
4126 ext2_filsys fs = ctx->fs;
4130 if ((!LINUX_S_ISDIR(inode->i_mode) &&
4131 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
4132 (!ext2fs_has_feature_dir_index(fs->super) &&
4133 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
4136 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
4138 if ((pctx->errcode) ||
4140 (blk < fs->super->s_first_data_block) ||
4141 (blk >= ext2fs_blocks_count(fs->super))) {
4142 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4148 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
4149 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4152 /* XXX should check that beginning matches a directory */
4153 root = (struct ext2_dx_root_info *) (block_buf + 24);
4155 if ((root->reserved_zero || root->info_length < 8) &&
4156 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4159 pctx->num = root->hash_version;
4160 if ((root->hash_version != EXT2_HASH_LEGACY) &&
4161 (root->hash_version != EXT2_HASH_HALF_MD4) &&
4162 (root->hash_version != EXT2_HASH_TEA) &&
4163 (root->hash_version != EXT2_HASH_SIPHASH) &&
4164 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
4167 if (ext4_hash_in_dirent(inode)) {
4168 if (root->hash_version != EXT2_HASH_SIPHASH &&
4169 fix_problem(ctx, PR_1_HTREE_NEEDS_SIPHASH, pctx))
4172 if (root->hash_version == EXT2_HASH_SIPHASH &&
4173 fix_problem(ctx, PR_1_HTREE_CANNOT_SIPHASH, pctx))
4177 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
4178 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
4181 pctx->num = root->indirect_levels;
4182 /* if htree level is clearly too high, consider it to be broken */
4183 if (root->indirect_levels > EXT4_HTREE_LEVEL &&
4184 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4187 /* if level is only maybe too high, LARGE_DIR feature could be unset */
4188 if (root->indirect_levels > ext2_dir_htree_level(fs) &&
4189 !ext2fs_has_feature_largedir(fs->super)) {
4190 int blockbits = EXT2_BLOCK_SIZE_BITS(fs->super) + 10;
4191 unsigned idx_pb = 1 << (blockbits - 3);
4193 /* compare inode size/blocks vs. max-sized 2-level htree */
4194 if (EXT2_I_SIZE(pctx->inode) <
4195 (idx_pb - 1) * (idx_pb - 2) << blockbits &&
4196 pctx->inode->i_blocks <
4197 (idx_pb - 1) * (idx_pb - 2) << (blockbits - 9) &&
4198 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4202 if (root->indirect_levels > EXT4_HTREE_LEVEL_COMPAT ||
4203 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
4209 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
4210 struct ext2_inode *inode, int restart_flag,
4214 inode->i_links_count = 0;
4215 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
4216 ext2fs_set_dtime(ctx->fs, inode);
4219 * If a special inode has such rotten block mappings that we
4220 * want to clear the whole inode, be sure to actually zap
4221 * the block maps because i_links_count isn't checked for
4222 * special inodes, and we'll end up right back here the next
4225 if (ino < EXT2_FIRST_INODE(ctx->fs->super))
4226 memset(inode->i_block, 0, sizeof(inode->i_block));
4228 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
4229 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
4230 if (ctx->inode_reg_map)
4231 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
4232 if (ctx->inode_bad_map)
4233 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
4236 * If the inode was partially accounted for before processing
4237 * was aborted, we need to restart the pass 1 scan.
4239 ctx->flags |= restart_flag;
4241 if (ino == EXT2_BAD_INO)
4242 memset(inode, 0, sizeof(struct ext2_inode));
4244 e2fsck_write_inode(ctx, ino, inode, source);
4248 * Use the multiple-blocks reclamation code to fix alignment problems in
4249 * a bigalloc filesystem. We want a logical cluster to map to *only* one
4250 * physical cluster, and we want the block offsets within that cluster to
4253 static int has_unaligned_cluster_map(e2fsck_t ctx,
4254 blk64_t last_pblk, blk64_t last_lblk,
4255 blk64_t pblk, blk64_t lblk)
4257 blk64_t cluster_mask;
4259 if (!ctx->fs->cluster_ratio_bits)
4261 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
4264 * If the block in the logical cluster doesn't align with the block in
4265 * the physical cluster...
4267 if ((lblk & cluster_mask) != (pblk & cluster_mask))
4271 * If we cross a physical cluster boundary within a logical cluster...
4273 if (last_pblk && (lblk & cluster_mask) != 0 &&
4274 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
4275 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
4281 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
4282 struct process_block_struct *pb,
4283 blk64_t start_block, blk64_t end_block,
4285 ext2_extent_handle_t ehandle,
4288 struct ext2fs_extent extent;
4289 blk64_t blk, last_lblk;
4291 int is_dir, is_leaf;
4293 struct ext2_extent_info info;
4294 int failed_csum = 0;
4296 if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
4299 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
4302 if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
4303 !pb->eti.force_rebuild &&
4304 info.curr_level < MAX_EXTENT_DEPTH_COUNT) {
4305 struct extent_tree_level *etl;
4307 etl = pb->eti.ext_info + info.curr_level;
4308 etl->num_extents += info.num_entries;
4309 etl->max_extents += info.max_entries;
4311 * Implementation wart: Splitting extent blocks when appending
4312 * will leave the old block with one free entry. Therefore
4313 * unless the node is totally full, pretend that a non-root
4314 * extent block can hold one fewer entry than it actually does,
4315 * so that we don't repeatedly rebuild the extent tree.
4317 if (info.curr_level && info.num_entries < info.max_entries)
4321 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
4323 while ((pctx->errcode == 0 ||
4324 pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
4325 info.num_entries-- > 0) {
4326 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
4327 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
4328 last_lblk = extent.e_lblk + extent.e_len - 1;
4331 pctx->blk = extent.e_pblk;
4332 pctx->blk2 = extent.e_lblk;
4333 pctx->num = extent.e_len;
4334 pctx->blkcount = extent.e_lblk + extent.e_len;
4336 if (extent.e_pblk == 0 ||
4337 extent.e_pblk < ctx->fs->super->s_first_data_block ||
4338 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
4339 problem = PR_1_EXTENT_BAD_START_BLK;
4340 else if (extent.e_lblk < start_block)
4341 problem = PR_1_OUT_OF_ORDER_EXTENTS;
4342 else if ((end_block && last_lblk > end_block) &&
4343 !(last_lblk > eof_block &&
4344 ((extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) ||
4345 (pctx->inode->i_flags & EXT4_VERITY_FL))))
4346 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
4347 else if (is_leaf && extent.e_len == 0)
4348 problem = PR_1_EXTENT_LENGTH_ZERO;
4350 (extent.e_pblk + extent.e_len) >
4351 ext2fs_blocks_count(ctx->fs->super))
4352 problem = PR_1_EXTENT_ENDS_BEYOND;
4353 else if (is_leaf && is_dir && !pctx->inode->i_size_high &&
4354 !ext2fs_has_feature_largedir(ctx->fs->super) &&
4355 ((extent.e_lblk + extent.e_len) >
4356 (1U << (21 - ctx->fs->super->s_log_block_size))))
4357 problem = PR_1_TOOBIG_DIR;
4359 if (is_leaf && problem == 0 && extent.e_len > 0) {
4361 printf("extent_region(ino=%u, expect=%llu, "
4362 "lblk=%llu, len=%u)\n", pb->ino,
4363 (unsigned long long) pb->next_lblock,
4364 (unsigned long long) extent.e_lblk,
4367 if (extent.e_lblk < pb->next_lblock)
4368 problem = PR_1_EXTENT_COLLISION;
4369 else if (extent.e_lblk + extent.e_len > pb->next_lblock)
4370 pb->next_lblock = extent.e_lblk + extent.e_len;
4374 * Uninitialized blocks in a directory? Clear the flag and
4375 * we'll interpret the blocks later.
4377 if (try_repairs && is_dir && problem == 0 &&
4378 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4379 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
4380 e2fsck_pass1_fix_lock(ctx);
4381 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4382 pb->inode_modified = 1;
4383 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4385 e2fsck_pass1_fix_unlock(ctx);
4390 #ifdef CONFIG_DEVELOPER_FEATURES
4391 if (try_repairs && !is_dir && problem == 0 &&
4392 (ctx->options & E2F_OPT_CLEAR_UNINIT) &&
4393 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4394 fix_problem(ctx, PR_1_CLEAR_UNINIT_EXTENT, pctx)) {
4395 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4396 pb->inode_modified = 1;
4397 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4404 if (try_repairs && problem) {
4406 if (fix_problem(ctx, problem, pctx)) {
4407 if (ctx->invalid_bitmaps) {
4409 * If fsck knows the bitmaps are bad,
4410 * skip to the next extent and
4411 * try to clear this extent again
4412 * after fixing the bitmaps, by
4415 pctx->errcode = ext2fs_extent_get(
4417 EXT2_EXTENT_NEXT_SIB,
4419 ctx->flags |= E2F_FLAG_RESTART_LATER;
4420 if (pctx->errcode ==
4421 EXT2_ET_NO_CURRENT_NODE) {
4427 e2fsck_pass1_fix_lock(ctx);
4428 e2fsck_read_bitmaps(ctx);
4429 pb->inode_modified = 1;
4431 ext2fs_extent_delete(ehandle, 0);
4432 e2fsck_pass1_fix_unlock(ctx);
4433 if (pctx->errcode) {
4434 pctx->str = "ext2fs_extent_delete";
4437 e2fsck_pass1_fix_lock(ctx);
4438 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4439 e2fsck_pass1_fix_unlock(ctx);
4440 if (pctx->errcode &&
4441 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
4442 pctx->str = "ext2fs_extent_fix_parents";
4445 pctx->errcode = ext2fs_extent_get(ehandle,
4446 EXT2_EXTENT_CURRENT,
4448 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
4459 blk64_t lblk = extent.e_lblk;
4460 int next_try_repairs = 1;
4462 blk = extent.e_pblk;
4465 * If this lower extent block collides with critical
4466 * metadata, don't try to repair the damage. Pass 1b
4467 * will reallocate the block; then we can try again.
4469 if (pb->ino != EXT2_RESIZE_INO &&
4470 extent.e_pblk < ctx->fs->super->s_blocks_count &&
4471 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
4473 next_try_repairs = 0;
4476 PR_1_CRITICAL_METADATA_COLLISION,
4478 if ((ctx->options & E2F_OPT_NO) == 0)
4479 ctx->flags |= E2F_FLAG_RESTART_LATER;
4481 pctx->errcode = ext2fs_extent_get(ehandle,
4482 EXT2_EXTENT_DOWN, &extent);
4483 if (pctx->errcode &&
4484 pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
4485 pctx->str = "EXT2_EXTENT_DOWN";
4486 problem = PR_1_EXTENT_HEADER_INVALID;
4487 if (!next_try_repairs)
4489 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
4490 goto report_problem;
4493 /* The next extent should match this index's logical start */
4494 if (extent.e_lblk != lblk) {
4495 struct ext2_extent_info e_info;
4497 pctx->errcode = ext2fs_extent_get_info(ehandle,
4499 if (pctx->errcode) {
4500 pctx->str = "ext2fs_extent_get_info";
4504 pctx->blk2 = extent.e_lblk;
4505 pctx->num = e_info.curr_level - 1;
4506 problem = PR_1_EXTENT_INDEX_START_INVALID;
4507 if (fix_problem(ctx, problem, pctx)) {
4508 e2fsck_pass1_fix_lock(ctx);
4509 pb->inode_modified = 1;
4511 ext2fs_extent_fix_parents(ehandle);
4512 e2fsck_pass1_fix_unlock(ctx);
4513 if (pctx->errcode) {
4514 pctx->str = "ext2fs_extent_fix_parents";
4519 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
4520 last_lblk, eof_block, ehandle,
4524 pctx->errcode = ext2fs_extent_get(ehandle,
4525 EXT2_EXTENT_UP, &extent);
4526 if (pctx->errcode) {
4527 pctx->str = "EXT2_EXTENT_UP";
4530 mark_block_used(ctx, blk);
4535 if ((pb->previous_block != 0) &&
4536 (pb->previous_block+1 != extent.e_pblk)) {
4537 if (ctx->options & E2F_OPT_FRAGCHECK) {
4542 else if (pb->is_reg)
4545 printf(("%6lu(%c): expecting %6lu "
4547 "phys %6lu log %lu len %lu\n"),
4548 (unsigned long) pctx->ino, type,
4549 (unsigned long) pb->previous_block+1,
4550 (unsigned long) extent.e_pblk,
4551 (unsigned long) extent.e_lblk,
4552 (unsigned long) extent.e_len);
4557 * If we notice a gap in the logical block mappings of an
4558 * extent-mapped directory, offer to close the hole by
4559 * moving the logical block down, otherwise we'll go mad in
4560 * pass 3 allocating empty directory blocks to fill the hole.
4562 if (try_repairs && is_dir &&
4563 pb->last_block + 1 < extent.e_lblk) {
4566 new_lblk = pb->last_block + 1;
4567 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
4568 new_lblk = ((new_lblk +
4569 EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
4570 ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
4572 EXT2FS_CLUSTER_MASK(ctx->fs));
4573 pctx->blk = extent.e_lblk;
4574 pctx->blk2 = new_lblk;
4575 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
4576 e2fsck_pass1_fix_lock(ctx);
4577 extent.e_lblk = new_lblk;
4578 pb->inode_modified = 1;
4579 pctx->errcode = ext2fs_extent_replace(ehandle,
4581 e2fsck_pass1_fix_unlock(ctx);
4582 if (pctx->errcode) {
4586 e2fsck_pass1_fix_lock(ctx);
4587 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4588 e2fsck_pass1_fix_unlock(ctx);
4590 goto failed_add_dir_block;
4591 pctx->errcode = ext2fs_extent_goto(ehandle,
4594 goto failed_add_dir_block;
4595 last_lblk = extent.e_lblk + extent.e_len - 1;
4601 while (++pb->last_db_block <
4602 (e2_blkcnt_t) extent.e_lblk) {
4603 pctx->errcode = ext2fs_add_dir_block2(
4607 if (pctx->errcode) {
4609 pctx->num = pb->last_db_block;
4610 goto failed_add_dir_block;
4614 for (i = 0; i < extent.e_len; i++) {
4615 pctx->errcode = ext2fs_add_dir_block2(
4620 if (pctx->errcode) {
4621 pctx->blk = extent.e_pblk + i;
4622 pctx->num = extent.e_lblk + i;
4623 failed_add_dir_block:
4624 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
4625 /* Should never get here */
4626 ctx->flags |= E2F_FLAG_ABORT;
4630 if (extent.e_len > 0)
4631 pb->last_db_block = extent.e_lblk + extent.e_len - 1;
4633 if (has_unaligned_cluster_map(ctx, pb->previous_block,
4637 for (i = 0; i < extent.e_len; i++) {
4638 pctx->blk = extent.e_lblk + i;
4639 pctx->blk2 = extent.e_pblk + i;
4640 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
4641 mark_block_used(ctx, extent.e_pblk + i);
4642 mark_block_used(ctx, extent.e_pblk + i);
4647 * Check whether first cluster got marked in previous iteration.
4649 if (ctx->fs->cluster_ratio_bits &&
4650 pb->previous_block &&
4651 (EXT2FS_B2C(ctx->fs, extent.e_pblk) ==
4652 EXT2FS_B2C(ctx->fs, pb->previous_block)))
4653 /* Set blk to the beginning of next cluster. */
4656 EXT2FS_B2C(ctx->fs, extent.e_pblk) + 1);
4658 /* Set blk to the beginning of current cluster. */
4659 blk = EXT2FS_C2B(ctx->fs,
4660 EXT2FS_B2C(ctx->fs, extent.e_pblk));
4662 if (blk < extent.e_pblk + extent.e_len) {
4663 mark_blocks_used(ctx, blk,
4664 extent.e_pblk + extent.e_len - blk);
4665 n = DIV_ROUND_UP(extent.e_pblk + extent.e_len - blk,
4666 EXT2FS_CLUSTER_RATIO(ctx->fs));
4667 pb->num_blocks += n;
4669 pb->last_block = extent.e_lblk + extent.e_len - 1;
4670 pb->previous_block = extent.e_pblk + extent.e_len - 1;
4671 start_block = pb->last_block = last_lblk;
4672 if (is_leaf && !is_dir &&
4673 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
4674 pb->last_init_lblock = last_lblk;
4676 pctx->errcode = ext2fs_extent_get(ehandle,
4677 EXT2_EXTENT_NEXT_SIB,
4681 /* Failed csum but passes checks? Ask to fix checksum. */
4683 fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
4684 e2fsck_pass1_fix_lock(ctx);
4685 pb->inode_modified = 1;
4686 pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
4687 e2fsck_pass1_fix_unlock(ctx);
4692 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
4696 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
4697 struct process_block_struct *pb)
4699 struct ext2_extent_info info;
4700 struct ext2_inode *inode = pctx->inode;
4701 ext2_extent_handle_t ehandle;
4702 ext2_filsys fs = ctx->fs;
4703 ext2_ino_t ino = pctx->ino;
4706 struct ext3_extent_header *eh;
4708 /* Check for a proper extent header... */
4709 eh = (struct ext3_extent_header *) &inode->i_block[0];
4710 retval = ext2fs_extent_header_verify(eh, sizeof(inode->i_block));
4712 if (fix_problem(ctx, PR_1_MISSING_EXTENT_HEADER, pctx))
4713 e2fsck_clear_inode(ctx, ino, inode, 0,
4714 "check_blocks_extents");
4719 /* ...since this function doesn't fail if i_block is zeroed. */
4720 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
4721 if (pctx->errcode) {
4722 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
4723 e2fsck_clear_inode(ctx, ino, inode, 0,
4724 "check_blocks_extents");
4729 retval = ext2fs_extent_get_info(ehandle, &info);
4731 int max_depth = info.max_depth;
4733 if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
4734 max_depth = MAX_EXTENT_DEPTH_COUNT-1;
4735 ctx->extent_depth_count[max_depth]++;
4738 /* Check maximum extent depth */
4739 pctx->blk = info.max_depth;
4740 pctx->blk2 = ext2fs_max_extent_depth(ehandle);
4741 if (pctx->blk2 < pctx->blk &&
4742 fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
4743 pb->eti.force_rebuild = 1;
4745 /* Can we collect extent tree level stats? */
4746 pctx->blk = MAX_EXTENT_DEPTH_COUNT;
4747 if (pctx->blk2 > pctx->blk)
4748 fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
4749 memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
4750 pb->eti.ino = pb->ino;
4752 pb->next_lblock = 0;
4754 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
4755 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
4756 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
4757 if (pctx->errcode &&
4758 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
4760 inode->i_blocks = 0;
4761 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
4762 "check_blocks_extents");
4765 ext2fs_extent_free(ehandle);
4767 /* Rebuild unless it's a dir and we're rehashing it */
4768 if (LINUX_S_ISDIR(inode->i_mode) &&
4769 e2fsck_dir_will_be_rehashed(ctx, ino))
4772 if (ctx->options & E2F_OPT_CONVERT_BMAP)
4773 e2fsck_rebuild_extents_later(ctx, ino);
4775 e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
4779 * In fact we don't need to check blocks for an inode with inline data
4780 * because this inode doesn't have any blocks. In this function all
4781 * we need to do is add this inode into dblist when it is a directory.
4783 static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
4784 struct process_block_struct *pb)
4787 size_t inline_data_size = 0;
4794 /* Process the dirents in i_block[] as the "first" block. */
4795 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
4799 /* Process the dirents in the EA as a "second" block. */
4800 flags = ctx->fs->flags;
4801 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
4802 pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
4804 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
4805 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
4806 if (pctx->errcode) {
4811 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
4814 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
4822 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
4823 ctx->flags |= E2F_FLAG_ABORT;
4827 * This subroutine is called on each inode to account for all of the
4828 * blocks used by that inode.
4830 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
4831 char *block_buf, const struct ea_quota *ea_ibody_quota)
4833 ext2_filsys fs = ctx->fs;
4834 struct process_block_struct pb;
4835 ext2_ino_t ino = pctx->ino;
4836 struct ext2_inode *inode = pctx->inode;
4837 unsigned bad_size = 0;
4838 int dirty_inode = 0;
4842 struct ea_quota ea_block_quota;
4845 pb.num_blocks = EXT2FS_B2C(ctx->fs,
4846 ea_ibody_quota ? ea_ibody_quota->blocks : 0);
4848 pb.last_init_lblock = -1;
4849 pb.last_db_block = -1;
4850 pb.num_illegal_blocks = 0;
4851 pb.suppress = 0; pb.clear = 0;
4854 pb.previous_block = 0;
4855 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
4856 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
4857 pb.max_blocks = 1U << (31 - fs->super->s_log_block_size);
4861 pb.inode_modified = 0;
4862 pb.eti.force_rebuild = 0;
4866 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
4867 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
4869 if (check_ext_attr(ctx, pctx, block_buf, &ea_block_quota)) {
4870 if (e2fsck_should_abort(ctx))
4872 pb.num_blocks += EXT2FS_B2C(ctx->fs, ea_block_quota.blocks);
4875 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
4876 check_blocks_inline_data(ctx, pctx, &pb);
4877 else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
4878 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
4879 check_blocks_extents(ctx, pctx, &pb);
4883 * If we've modified the inode, write it out before
4884 * iterate() tries to use it.
4887 e2fsck_write_inode(ctx, ino, inode,
4892 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
4893 pctx->errcode = ext2fs_block_iterate3(fs, ino,
4894 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
4895 block_buf, process_block, &pb);
4897 * We do not have uninitialized extents in non extent
4900 pb.last_init_lblock = pb.last_block;
4902 * If iterate() changed a block mapping, we have to
4903 * re-read the inode. If we decide to clear the
4904 * inode after clearing some stuff, we'll re-write the
4905 * bad mappings into the inode!
4907 if (pb.inode_modified)
4908 e2fsck_read_inode(ctx, ino, inode,
4910 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
4911 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
4913 if (ctx->options & E2F_OPT_CONVERT_BMAP) {
4915 printf("bmap rebuild ino=%d\n", ino);
4917 if (!LINUX_S_ISDIR(inode->i_mode) ||
4918 !e2fsck_dir_will_be_rehashed(ctx, ino))
4919 e2fsck_rebuild_extents_later(ctx, ino);
4923 end_problem_latch(ctx, PR_LATCH_BLOCK);
4924 end_problem_latch(ctx, PR_LATCH_TOOBIG);
4925 if (e2fsck_should_abort(ctx))
4928 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
4930 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
4931 if (LINUX_S_ISDIR(inode->i_mode))
4932 ctx->fs_fragmented_dir++;
4934 ctx->fs_fragmented++;
4938 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
4943 if (inode->i_flags & EXT2_INDEX_FL) {
4944 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
4945 inode->i_flags &= ~EXT2_INDEX_FL;
4948 e2fsck_add_dx_dir(ctx, ino, inode, pb.last_block+1);
4952 if (!pb.num_blocks && pb.is_dir &&
4953 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
4954 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
4955 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
4956 ctx->fs_directory_count--;
4961 if (ino != quota_type2inum(PRJQUOTA, fs->super) &&
4962 ino != fs->super->s_orphan_file_inum &&
4963 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) &&
4964 !(inode->i_flags & EXT4_EA_INODE_FL)) {
4965 quota_data_add(ctx->qctx, (struct ext2_inode_large *) inode,
4967 pb.num_blocks * EXT2_CLUSTER_SIZE(fs->super));
4968 quota_data_inodes(ctx->qctx, (struct ext2_inode_large *) inode,
4969 ino, (ea_ibody_quota ?
4970 ea_ibody_quota->inodes : 0) +
4971 ea_block_quota.inodes + 1);
4974 if (!ext2fs_has_feature_huge_file(fs->super) ||
4975 !(inode->i_flags & EXT4_HUGE_FILE_FL))
4976 pb.num_blocks *= (fs->blocksize / 512);
4977 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
4979 printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
4980 ino, inode->i_size, (unsigned long long) pb.last_block,
4981 (unsigned long long) ext2fs_inode_i_blocks(fs, inode),
4982 (unsigned long long) pb.num_blocks);
4984 size = EXT2_I_SIZE(inode);
4986 unsigned nblock = size >> EXT2_BLOCK_SIZE_BITS(fs->super);
4987 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
4992 flags = ctx->fs->flags;
4993 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
4994 err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
4996 ctx->fs->flags = (flags &
4997 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
4999 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5000 if (err || sz != size) {
5004 } else if (size & (fs->blocksize - 1))
5006 else if (nblock > (pb.last_block + 1))
5008 else if (nblock < (pb.last_block + 1)) {
5009 if (((pb.last_block + 1) - nblock) >
5010 fs->super->s_prealloc_dir_blocks)
5014 if ((pb.last_init_lblock >= 0) &&
5015 /* Do not allow initialized allocated blocks past i_size*/
5016 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
5017 !(inode->i_flags & EXT4_VERITY_FL))
5019 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5020 size > ext2_max_sizes[fs->super->s_log_block_size])
5021 /* too big for a direct/indirect-mapped file */
5023 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5025 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
5026 /* too big for an extent-based file - 32bit ee_block */
5029 /* i_size for symlinks is checked elsewhere */
5030 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
5031 /* Did inline_data set pctx->num earlier? */
5033 pctx->num = (pb.last_block + 1) * fs->blocksize;
5034 pctx->group = bad_size;
5035 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
5036 ext2fs_inode_size_set(fs, inode, pctx->num);
5037 if (EXT2_I_SIZE(inode) == 0 &&
5038 (inode->i_flags & EXT4_INLINE_DATA_FL)) {
5039 memset(inode->i_block, 0,
5040 sizeof(inode->i_block));
5041 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
5047 if (LINUX_S_ISREG(inode->i_mode) &&
5048 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
5050 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
5051 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
5052 (ext2fs_has_feature_huge_file(fs->super) &&
5053 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
5054 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
5055 pctx->num = pb.num_blocks;
5056 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
5057 inode->i_blocks = pb.num_blocks;
5058 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
5065 * The kernel gets mad if we ask it to allocate bigalloc clusters to
5066 * a block mapped file, so rebuild it as an extent file. We can skip
5067 * symlinks because they're never rewritten.
5069 if (ext2fs_has_feature_bigalloc(fs->super) &&
5070 (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
5071 ext2fs_inode_data_blocks2(fs, inode) > 0 &&
5072 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
5073 !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
5074 fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
5075 pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
5080 if (ctx->dirs_to_hash && pb.is_dir &&
5081 !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
5082 !(inode->i_flags & EXT2_INDEX_FL) &&
5083 ((inode->i_size / fs->blocksize) >= 3))
5084 e2fsck_rehash_dir_later(ctx, ino);
5088 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
5093 * Helper function called by process block when an illegal block is
5094 * found. It returns a description about why the block is illegal
5096 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
5100 static char problem[80];
5102 super = fs->super->s_first_data_block;
5103 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
5104 if (block < super) {
5105 sprintf(problem, "< FIRSTBLOCK (%u)", super);
5107 } else if (block >= ext2fs_blocks_count(fs->super)) {
5108 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
5111 for (i = 0; i < fs->group_desc_count; i++) {
5112 if (block == super) {
5113 sprintf(problem, "is the superblock in group %d", i);
5116 if (block > super &&
5117 block <= (super + fs->desc_blocks)) {
5118 sprintf(problem, "is in the group descriptors "
5122 if (block == ext2fs_block_bitmap_loc(fs, i)) {
5123 sprintf(problem, "is the block bitmap of group %d", i);
5126 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
5127 sprintf(problem, "is the inode bitmap of group %d", i);
5130 if (block >= ext2fs_inode_table_loc(fs, i) &&
5131 (block < ext2fs_inode_table_loc(fs, i)
5132 + fs->inode_blocks_per_group)) {
5133 sprintf(problem, "is in the inode table of group %d",
5137 super += fs->super->s_blocks_per_group;
5144 * This is a helper function for check_blocks().
5146 static int process_block(ext2_filsys fs,
5148 e2_blkcnt_t blockcnt,
5149 blk64_t ref_block EXT2FS_ATTR((unused)),
5150 int ref_offset EXT2FS_ATTR((unused)),
5153 struct process_block_struct *p;
5154 struct problem_context *pctx;
5155 blk64_t blk = *block_nr;
5157 problem_t problem = 0;
5160 p = (struct process_block_struct *) priv_data;
5165 * For a directory, add logical block zero for processing even if it's
5166 * not mapped or we'll be perennially stuck with broken "." and ".."
5169 if (p->is_dir && blockcnt == 0 && blk == 0) {
5170 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
5171 if (pctx->errcode) {
5173 pctx->num = blockcnt;
5174 goto failed_add_dir_block;
5183 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
5188 * Simplistic fragmentation check. We merely require that the
5189 * file be contiguous. (Which can never be true for really
5190 * big files that are greater than a block group.)
5192 if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
5193 if (p->previous_block+1 != blk) {
5194 if (ctx->options & E2F_OPT_FRAGCHECK) {
5202 printf(_("%6lu(%c): expecting %6lu "
5203 "got phys %6lu (blkcnt %lld)\n"),
5204 (unsigned long) pctx->ino, type,
5205 (unsigned long) p->previous_block+1,
5206 (unsigned long) blk,
5207 (long long) blockcnt);
5213 if (p->is_dir && !ext2fs_has_feature_largedir(fs->super) &&
5214 !pctx->inode->i_size_high &&
5215 blockcnt > (1 << (21 - fs->super->s_log_block_size)))
5216 problem = PR_1_TOOBIG_DIR;
5217 if (p->is_dir && p->num_blocks + 1 >= p->max_blocks)
5218 problem = PR_1_TOOBIG_DIR;
5219 if (p->is_reg && p->num_blocks + 1 >= p->max_blocks)
5220 problem = PR_1_TOOBIG_REG;
5221 if (!p->is_dir && !p->is_reg && blockcnt > 0)
5222 problem = PR_1_TOOBIG_SYMLINK;
5224 if (blk < fs->super->s_first_data_block ||
5225 blk >= ext2fs_blocks_count(fs->super))
5226 problem = PR_1_ILLEGAL_BLOCK_NUM;
5229 * If this IND/DIND/TIND block is squatting atop some critical metadata
5230 * (group descriptors, superblock, bitmap, inode table), any write to
5231 * "fix" mapping problems will destroy the metadata. We'll let pass 1b
5232 * fix that and restart fsck.
5235 p->ino != EXT2_RESIZE_INO &&
5236 blk < ctx->fs->super->s_blocks_count &&
5237 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
5239 fix_problem(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx);
5240 if ((ctx->options & E2F_OPT_NO) == 0)
5241 ctx->flags |= E2F_FLAG_RESTART_LATER;
5245 p->num_illegal_blocks++;
5247 * A bit of subterfuge here -- we're trying to fix a block
5248 * mapping, but the IND/DIND/TIND block could have collided
5249 * with some critical metadata. So, fix the in-core mapping so
5250 * iterate won't go insane, but return 0 instead of
5251 * BLOCK_CHANGED so that it won't write the remapping out to
5252 * our multiply linked block.
5254 * Even if we previously determined that an *IND block
5255 * conflicts with critical metadata, we must still try to
5256 * iterate the *IND block as if it is an *IND block to find and
5257 * mark the blocks it points to. Better to be overly cautious
5258 * with the used_blocks map so that we don't move the *IND
5259 * block to a block that's really in use!
5261 if (p->ino != EXT2_RESIZE_INO &&
5263 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
5268 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
5269 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
5273 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
5275 set_latch_flags(PR_LATCH_BLOCK,
5280 pctx->blkcount = blockcnt;
5281 if (fix_problem(ctx, problem, pctx)) {
5282 blk = *block_nr = 0;
5283 ret_code = BLOCK_CHANGED;
5284 p->inode_modified = 1;
5286 * If the directory block is too big and is beyond the
5287 * end of the FS, don't bother trying to add it for
5288 * processing -- the kernel would never have created a
5289 * directory this large, and we risk an ENOMEM abort.
5290 * In any case, the toobig handler for extent-based
5291 * directories also doesn't feed toobig blocks to
5294 if (problem == PR_1_TOOBIG_DIR)
5301 if (p->ino == EXT2_RESIZE_INO) {
5303 * The resize inode has already be sanity checked
5304 * during pass #0 (the superblock checks). All we
5305 * have to do is mark the double indirect block as
5306 * being in use; all of the other blocks are handled
5307 * by mark_table_blocks()).
5309 if (blockcnt == BLOCK_COUNT_DIND)
5310 mark_block_used(ctx, blk);
5312 } else if (!(ctx->fs->cluster_ratio_bits &&
5313 p->previous_block &&
5314 (EXT2FS_B2C(ctx->fs, blk) ==
5315 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
5316 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
5317 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
5318 mark_block_used(ctx, blk);
5320 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
5321 p->last_block, blk, blockcnt)) {
5322 pctx->blk = blockcnt;
5324 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
5325 mark_block_used(ctx, blk);
5326 mark_block_used(ctx, blk);
5329 p->last_block = blockcnt;
5330 p->previous_block = blk;
5332 if (p->is_dir && (blockcnt >= 0)) {
5333 while (++p->last_db_block < blockcnt) {
5334 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
5337 if (pctx->errcode) {
5339 pctx->num = p->last_db_block;
5340 goto failed_add_dir_block;
5343 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
5345 if (pctx->errcode) {
5347 pctx->num = blockcnt;
5348 failed_add_dir_block:
5349 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
5350 /* Should never get here */
5351 ctx->flags |= E2F_FLAG_ABORT;
5358 static int process_bad_block(ext2_filsys fs,
5360 e2_blkcnt_t blockcnt,
5361 blk64_t ref_block EXT2FS_ATTR((unused)),
5362 int ref_offset EXT2FS_ATTR((unused)),
5365 struct process_block_struct *p;
5366 blk64_t blk = *block_nr;
5367 blk64_t first_block;
5369 struct problem_context *pctx;
5375 p = (struct process_block_struct *) priv_data;
5379 pctx->ino = EXT2_BAD_INO;
5381 pctx->blkcount = blockcnt;
5383 if ((blk < fs->super->s_first_data_block) ||
5384 (blk >= ext2fs_blocks_count(fs->super))) {
5385 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
5387 return BLOCK_CHANGED;
5393 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
5395 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
5397 return BLOCK_CHANGED;
5399 } else if (is_blocks_used(ctx, blk, 1)) {
5401 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
5404 return BLOCK_CHANGED;
5406 if (e2fsck_should_abort(ctx))
5409 mark_block_used(ctx, blk);
5414 printf ("DEBUG: Marking %u as bad.\n", blk);
5416 ctx->fs_badblocks_count++;
5418 * If the block is not used, then mark it as used and return.
5419 * If it is already marked as found, this must mean that
5420 * there's an overlap between the filesystem table blocks
5421 * (bitmaps and inode table) and the bad block list.
5423 if (!is_blocks_used(ctx, blk, 1)) {
5424 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
5428 * Try to find the where the filesystem block was used...
5430 first_block = fs->super->s_first_data_block;
5432 for (i = 0; i < fs->group_desc_count; i++ ) {
5435 if (!ext2fs_bg_has_super(fs, i))
5437 if (blk == first_block) {
5439 if (fix_problem(ctx,
5440 PR_1_BAD_PRIMARY_SUPERBLOCK,
5443 return BLOCK_CHANGED;
5447 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
5450 if ((blk > first_block) &&
5451 (blk <= first_block + fs->desc_blocks)) {
5453 pctx->blk = *block_nr;
5454 if (fix_problem(ctx,
5455 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
5457 return BLOCK_CHANGED;
5461 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
5465 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
5466 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
5467 ctx->invalid_block_bitmap_flag[i]++;
5468 ctx->invalid_bitmaps++;
5472 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
5473 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
5474 ctx->invalid_inode_bitmap_flag[i]++;
5475 ctx->invalid_bitmaps++;
5479 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
5480 (blk < (ext2fs_inode_table_loc(fs, i) +
5481 fs->inode_blocks_per_group))) {
5483 * If there are bad blocks in the inode table,
5484 * the inode scan code will try to do
5485 * something reasonable automatically.
5489 first_block += fs->super->s_blocks_per_group;
5492 * If we've gotten to this point, then the only
5493 * possibility is that the bad block inode meta data
5494 * is using a bad block.
5496 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
5497 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
5498 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
5500 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
5502 return BLOCK_CHANGED;
5504 if (e2fsck_should_abort(ctx))
5511 /* Warn user that the block wasn't claimed */
5512 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
5517 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
5518 const char *name, int num, blk64_t *new_block)
5520 ext2_filsys fs = ctx->fs;
5522 blk64_t old_block = *new_block;
5525 unsigned flexbg_size;
5528 struct problem_context pctx;
5530 clear_problem_context(&pctx);
5533 pctx.blk = old_block;
5537 * For flex_bg filesystems, first try to allocate the metadata
5538 * within the flex_bg, and if that fails then try finding the
5539 * space anywhere in the filesystem.
5541 is_flexbg = ext2fs_has_feature_flex_bg(fs->super);
5543 flexbg_size = 1U << fs->super->s_log_groups_per_flex;
5544 flexbg = group / flexbg_size;
5545 first_block = ext2fs_group_first_block2(fs,
5546 flexbg_size * flexbg);
5547 last_grp = group | (flexbg_size - 1);
5548 if (last_grp >= fs->group_desc_count)
5549 last_grp = fs->group_desc_count - 1;
5550 last_block = ext2fs_group_last_block2(fs, last_grp);
5552 last_block = ext2fs_group_last_block2(fs, group);
5553 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
5554 num, ctx->block_found_map,
5556 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
5557 pctx.errcode = ext2fs_get_free_blocks2(fs,
5558 fs->super->s_first_data_block,
5559 ext2fs_blocks_count(fs->super),
5560 num, ctx->block_found_map, new_block);
5563 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
5564 ext2fs_unmark_valid(fs);
5565 ctx->flags |= E2F_FLAG_ABORT;
5568 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
5570 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
5571 ext2fs_unmark_valid(fs);
5572 ctx->flags |= E2F_FLAG_ABORT;
5575 ext2fs_mark_super_dirty(fs);
5576 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
5577 pctx.blk2 = *new_block;
5578 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
5579 PR_1_RELOC_TO), &pctx);
5581 for (i = 0; i < num; i++) {
5583 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
5585 pctx.errcode = io_channel_read_blk64(fs->io,
5586 old_block + i, 1, buf);
5588 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
5589 pctx.blk = (*new_block) + i;
5590 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
5593 pctx.blk = (*new_block) + i;
5594 pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
5599 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
5601 ext2fs_free_mem(&buf);
5605 * This routine gets called at the end of pass 1 if bad blocks are
5606 * detected in the superblock, group descriptors, inode_bitmaps, or
5607 * block bitmaps. At this point, all of the blocks have been mapped
5608 * out, so we can try to allocate new block(s) to replace the bad
5611 static void handle_fs_bad_blocks(e2fsck_t ctx)
5613 ext2_filsys fs = ctx->fs;
5615 blk64_t first_block;
5618 for (i = 0; i < fs->group_desc_count; i++) {
5619 first_block = ext2fs_group_first_block2(fs, i);
5621 if (ctx->invalid_block_bitmap_flag[i]) {
5622 new_blk = ext2fs_block_bitmap_loc(fs, i);
5623 new_table_block(ctx, first_block, i, _("block bitmap"),
5625 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
5627 if (ctx->invalid_inode_bitmap_flag[i]) {
5628 new_blk = ext2fs_inode_bitmap_loc(fs, i);
5629 new_table_block(ctx, first_block, i, _("inode bitmap"),
5631 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
5633 if (ctx->invalid_inode_table_flag[i]) {
5634 new_blk = ext2fs_inode_table_loc(fs, i);
5635 new_table_block(ctx, first_block, i, _("inode table"),
5636 fs->inode_blocks_per_group,
5638 ext2fs_inode_table_loc_set(fs, i, new_blk);
5639 ctx->flags |= E2F_FLAG_RESTART;
5642 ctx->invalid_bitmaps = 0;
5646 * This routine marks all blocks which are used by the superblock,
5647 * group descriptors, inode bitmaps, and block bitmaps.
5649 static void mark_table_blocks(e2fsck_t ctx)
5651 ext2_filsys fs = ctx->fs;
5655 struct problem_context pctx;
5657 clear_problem_context(&pctx);
5659 for (i = 0; i < fs->group_desc_count; i++) {
5662 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
5663 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
5666 * Mark the blocks used for the inode table
5668 if (ext2fs_inode_table_loc(fs, i)) {
5669 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
5670 j < fs->inode_blocks_per_group;
5672 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5675 if (!ctx->invalid_inode_table_flag[i] &&
5677 PR_1_ITABLE_CONFLICT, &pctx)) {
5678 ctx->invalid_inode_table_flag[i]++;
5679 ctx->invalid_bitmaps++;
5682 ext2fs_mark_block_bitmap2(
5683 ctx->block_found_map, b);
5684 ext2fs_mark_block_bitmap2(
5685 ctx->block_metadata_map, b);
5691 * Mark block used for the block bitmap
5693 if (ext2fs_block_bitmap_loc(fs, i)) {
5694 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5695 ext2fs_block_bitmap_loc(fs, i))) {
5696 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
5697 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
5698 ctx->invalid_block_bitmap_flag[i]++;
5699 ctx->invalid_bitmaps++;
5702 ext2fs_mark_block_bitmap2(ctx->block_found_map,
5703 ext2fs_block_bitmap_loc(fs, i));
5704 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5705 ext2fs_block_bitmap_loc(fs, i));
5709 * Mark block used for the inode bitmap
5711 if (ext2fs_inode_bitmap_loc(fs, i)) {
5712 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5713 ext2fs_inode_bitmap_loc(fs, i))) {
5714 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
5715 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
5716 ctx->invalid_inode_bitmap_flag[i]++;
5717 ctx->invalid_bitmaps++;
5720 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5721 ext2fs_inode_bitmap_loc(fs, i));
5722 ext2fs_mark_block_bitmap2(ctx->block_found_map,
5723 ext2fs_inode_bitmap_loc(fs, i));
5730 * These subroutines short circuits ext2fs_get_blocks and
5731 * ext2fs_check_directory; we use them since we already have the inode
5732 * structure, so there's no point in letting the ext2fs library read
5735 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
5738 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5741 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5742 return EXT2_ET_CALLBACK_NOTHANDLED;
5744 for (i=0; i < EXT2_N_BLOCKS; i++)
5745 blocks[i] = ctx->stashed_inode->i_block[i];
5749 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
5750 struct ext2_inode *inode)
5752 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5754 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5755 return EXT2_ET_CALLBACK_NOTHANDLED;
5756 *inode = *ctx->stashed_inode;
5760 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
5761 struct ext2_inode *inode)
5763 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5765 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
5766 (inode != ctx->stashed_inode))
5767 *ctx->stashed_inode = *inode;
5768 return EXT2_ET_CALLBACK_NOTHANDLED;
5771 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
5773 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5775 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5776 return EXT2_ET_CALLBACK_NOTHANDLED;
5778 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
5779 return EXT2_ET_NO_DIRECTORY;
5783 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
5786 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5790 if (ctx->block_found_map) {
5791 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
5795 if (fs->block_map) {
5796 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
5797 ext2fs_mark_bb_dirty(fs);
5800 if (!fs->block_map) {
5801 retval = ext2fs_read_block_bitmap(fs);
5806 retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
5815 static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
5816 blk64_t len, blk64_t *pblk, blk64_t *plen)
5818 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5821 if (ctx->block_found_map)
5822 return ext2fs_new_range(fs, flags, goal, len,
5823 ctx->block_found_map, pblk, plen);
5825 if (!fs->block_map) {
5826 retval = ext2fs_read_block_bitmap(fs);
5831 return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
5835 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
5837 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5839 /* Never free a critical metadata block */
5840 if (ctx->block_found_map &&
5841 ctx->block_metadata_map &&
5843 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
5846 if (ctx->block_found_map) {
5848 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
5850 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
5854 static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
5855 blk_t num, int inuse)
5857 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5859 /* Never free a critical metadata block */
5860 if (ctx->block_found_map &&
5861 ctx->block_metadata_map &&
5863 ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
5866 if (ctx->block_found_map) {
5868 ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
5871 ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
5876 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
5878 ext2_filsys fs = ctx->fs;
5880 if (use_shortcuts) {
5881 fs->get_blocks = pass1_get_blocks;
5882 fs->check_directory = pass1_check_directory;
5883 fs->read_inode = pass1_read_inode;
5884 fs->write_inode = pass1_write_inode;
5885 ctx->stashed_ino = 0;
5888 fs->check_directory = 0;
5890 fs->write_inode = 0;
5894 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
5896 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
5897 ext2fs_set_block_alloc_stats_callback(ctx->fs,
5898 e2fsck_block_alloc_stats, 0);
5899 ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
5900 ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
5901 e2fsck_block_alloc_stats_range, NULL);