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, ino_t ino);
89 static void add_casefolded_dir(e2fsck_t ctx, 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;
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_entry2(ctx->fs, entry, NULL, &hash);
353 com_err("check_large_ea_inode", retval,
354 _("while hashing entry with e_value_inum = %u"),
355 entry->e_value_inum);
359 if (hash == entry->e_hash) {
360 *quota_blocks = size_to_quota_blocks(ctx->fs,
361 entry->e_value_size);
363 /* This might be an old Lustre-style ea_inode reference. */
364 if (inode.i_mtime == pctx->ino &&
365 inode.i_generation == pctx->inode->i_generation) {
368 /* If target inode is also missing EA_INODE flag,
369 * this is likely to be a bad reference.
371 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
372 pctx->num = entry->e_value_inum;
373 return PR_1_ATTR_VALUE_EA_INODE;
375 pctx->num = entry->e_hash;
376 return PR_1_ATTR_HASH;
381 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
382 pctx->num = entry->e_value_inum;
383 if (fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
384 inode.i_flags |= EXT4_EA_INODE_FL;
385 e2fsck_pass1_fix_lock(ctx);
386 ext2fs_write_inode(ctx->fs, entry->e_value_inum,
388 e2fsck_pass1_fix_unlock(ctx);
390 return PR_1_ATTR_NO_EA_INODE_FL;
396 static void inc_ea_inode_refs(e2fsck_t ctx, struct problem_context *pctx,
397 struct ext2_ext_attr_entry *first, void *end)
399 struct ext2_ext_attr_entry *entry = first;
400 struct ext2_ext_attr_entry *np = EXT2_EXT_ATTR_NEXT(entry);
402 while ((void *) entry < end && (void *) np < end &&
403 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
404 if (!entry->e_value_inum)
406 if (!ctx->ea_inode_refs) {
407 pctx->errcode = ea_refcount_create(0,
408 &ctx->ea_inode_refs);
411 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
412 ctx->flags |= E2F_FLAG_ABORT;
416 ea_refcount_increment(ctx->ea_inode_refs, entry->e_value_inum,
420 np = EXT2_EXT_ATTR_NEXT(entry);
424 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx,
425 struct ea_quota *ea_ibody_quota)
427 struct ext2_super_block *sb = ctx->fs->super;
428 struct ext2_inode_large *inode;
429 struct ext2_ext_attr_entry *entry;
430 char *start, *header, *end;
431 unsigned int storage_size, remain;
432 problem_t problem = 0;
435 ea_ibody_quota->blocks = 0;
436 ea_ibody_quota->inodes = 0;
438 inode = (struct ext2_inode_large *) pctx->inode;
439 storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
440 inode->i_extra_isize;
441 header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
442 inode->i_extra_isize;
443 end = header + storage_size;
444 start = header + sizeof(__u32);
445 entry = (struct ext2_ext_attr_entry *) start;
447 /* scan all entry's headers first */
449 /* take finish entry 0UL into account */
450 remain = storage_size - sizeof(__u32);
452 region = region_create(0, storage_size);
454 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
456 ctx->flags |= E2F_FLAG_ABORT;
459 if (region_allocate(region, 0, sizeof(__u32))) {
460 problem = PR_1_INODE_EA_ALLOC_COLLISION;
464 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
465 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
468 if (region_allocate(region, (char *)entry - (char *)header,
469 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
470 problem = PR_1_INODE_EA_ALLOC_COLLISION;
474 /* header eats this space */
475 remain -= sizeof(struct ext2_ext_attr_entry);
477 /* is attribute name valid? */
478 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
479 pctx->num = entry->e_name_len;
480 problem = PR_1_ATTR_NAME_LEN;
484 /* attribute len eats this space */
485 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
487 if (entry->e_value_inum == 0) {
488 /* check value size */
489 if (entry->e_value_size > remain) {
490 pctx->num = entry->e_value_size;
491 problem = PR_1_ATTR_VALUE_SIZE;
495 if (entry->e_value_size &&
496 region_allocate(region,
497 sizeof(__u32) + entry->e_value_offs,
499 entry->e_value_size))) {
500 problem = PR_1_INODE_EA_ALLOC_COLLISION;
504 hash = ext2fs_ext_attr_hash_entry(entry,
505 start + entry->e_value_offs);
507 /* e_hash may be 0 in older inode's ea */
508 if (entry->e_hash != 0 && entry->e_hash != hash) {
509 pctx->num = entry->e_hash;
510 problem = PR_1_ATTR_HASH;
514 blk64_t quota_blocks;
516 problem = check_large_ea_inode(ctx, entry, pctx,
521 ea_ibody_quota->blocks += quota_blocks;
522 ea_ibody_quota->inodes++;
525 /* If EA value is stored in external inode then it does not
526 * consume space here */
527 if (entry->e_value_inum == 0)
528 remain -= entry->e_value_size;
530 entry = EXT2_EXT_ATTR_NEXT(entry);
533 if (region_allocate(region, (char *)entry - (char *)header,
535 problem = PR_1_INODE_EA_ALLOC_COLLISION;
542 * it seems like a corruption. it's very unlikely we could repair
543 * EA(s) in automatic fashion -bzzz
545 if (problem == 0 || !fix_problem(ctx, problem, pctx)) {
546 inc_ea_inode_refs(ctx, pctx,
547 (struct ext2_ext_attr_entry *)start, end);
551 /* simply remove all possible EA(s) */
552 *((__u32 *)header) = 0UL;
553 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
554 EXT2_INODE_SIZE(sb), "pass1");
555 ea_ibody_quota->blocks = 0;
556 ea_ibody_quota->inodes = 0;
559 static int check_inode_extra_negative_epoch(__u32 xtime, __u32 extra) {
560 return (xtime & (1U << 31)) != 0 &&
561 (extra & EXT4_EPOCH_MASK) == EXT4_EPOCH_MASK;
564 #define CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, xtime) \
565 check_inode_extra_negative_epoch(inode->i_##xtime, \
566 inode->i_##xtime##_extra)
568 /* When today's date is earlier than 2242, we assume that atimes,
569 * ctimes, crtimes, and mtimes with years in the range 2310..2378 are
570 * actually pre-1970 dates mis-encoded.
572 #define EXT4_EXTRA_NEGATIVE_DATE_CUTOFF 2 * (1LL << 32)
574 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx,
575 struct ea_quota *ea_ibody_quota)
577 struct ext2_super_block *sb = ctx->fs->super;
578 struct ext2_inode_large *inode;
582 ea_ibody_quota->blocks = 0;
583 ea_ibody_quota->inodes = 0;
585 inode = (struct ext2_inode_large *) pctx->inode;
586 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
587 /* this isn't large inode. so, nothing to check */
592 printf("inode #%u, i_extra_size %d\n", pctx->ino,
593 inode->i_extra_isize);
595 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
596 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
597 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
599 * For now we will allow i_extra_isize to be 0, but really
600 * implementations should never allow i_extra_isize to be 0
602 if (inode->i_extra_isize &&
603 (inode->i_extra_isize < min || inode->i_extra_isize > max ||
604 inode->i_extra_isize & 3)) {
605 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
607 if (inode->i_extra_isize < min || inode->i_extra_isize > max)
608 inode->i_extra_isize = sb->s_want_extra_isize;
610 inode->i_extra_isize = (inode->i_extra_isize + 3) & ~3;
611 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
612 EXT2_INODE_SIZE(sb), "pass1");
615 /* check if there is no place for an EA header */
616 if (inode->i_extra_isize >= max - sizeof(__u32))
619 eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
620 inode->i_extra_isize);
621 if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
622 /* it seems inode has an extended attribute(s) in body */
623 check_ea_in_inode(ctx, pctx, ea_ibody_quota);
627 * If the inode's extended atime (ctime, crtime, mtime) is stored in
628 * the old, invalid format, repair it.
630 if (((sizeof(time_t) <= 4) ||
631 (((sizeof(time_t) > 4) &&
632 ctx->now < EXT4_EXTRA_NEGATIVE_DATE_CUTOFF))) &&
633 (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime) ||
634 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime) ||
635 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime) ||
636 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))) {
638 if (!fix_problem(ctx, PR_1_EA_TIME_OUT_OF_RANGE, pctx))
641 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime))
642 inode->i_atime_extra &= ~EXT4_EPOCH_MASK;
643 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime))
644 inode->i_ctime_extra &= ~EXT4_EPOCH_MASK;
645 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime))
646 inode->i_crtime_extra &= ~EXT4_EPOCH_MASK;
647 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))
648 inode->i_mtime_extra &= ~EXT4_EPOCH_MASK;
649 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
650 EXT2_INODE_SIZE(sb), "pass1");
655 static _INLINE_ int is_blocks_used(e2fsck_t ctx, blk64_t block,
660 /* used to avoid duplicate output from below */
661 retval = ext2fs_test_block_bitmap_range2_valid(ctx->block_found_map,
666 retval = ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num);
668 e2fsck_pass1_block_map_r_lock(ctx);
670 retval = ext2fs_test_block_bitmap_range2(
671 ctx->global_ctx->block_found_map, block, num);
672 e2fsck_pass1_block_map_r_unlock(ctx);
681 * Check to see if the inode might really be a directory, despite i_mode
683 * This is a lot of complexity for something for which I'm not really
684 * convinced happens frequently in the wild. If for any reason this
685 * causes any problems, take this code out.
686 * [tytso:20070331.0827EDT]
688 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
691 struct ext2_inode *inode = pctx->inode;
692 struct ext2_dir_entry *dirent;
695 unsigned int i, rec_len, not_device = 0;
700 * If the mode looks OK, we believe it. If the first block in
701 * the i_block array is 0, this cannot be a directory. If the
702 * inode is extent-mapped, it is still the case that the latter
703 * cannot be 0 - the magic number in the extent header would make
706 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
707 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
711 * Check the block numbers in the i_block array for validity:
712 * zero blocks are skipped (but the first one cannot be zero -
713 * see above), other blocks are checked against the first and
714 * max data blocks (from the the superblock) and against the
715 * block bitmap. Any invalid block found means this cannot be
718 * If there are non-zero blocks past the fourth entry, then
719 * this cannot be a device file: we remember that for the next
722 * For extent mapped files, we don't do any sanity checking:
723 * just try to get the phys block of logical block 0 and run
726 * For inline data files, we just try to get the size of inline
727 * data. If it's true, we will treat it as a directory.
730 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
731 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
732 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
735 unsigned int rec_len2;
736 struct ext2_dir_entry de;
738 if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
741 * If the size isn't a multiple of 4, it's probably not a
747 * If the first 10 bytes don't look like a directory entry,
748 * it's probably not a directory.
750 memcpy(&dotdot, inode->i_block, sizeof(dotdot));
751 memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
752 EXT2_DIR_REC_LEN(0));
753 dotdot = ext2fs_le32_to_cpu(dotdot);
754 de.inode = ext2fs_le32_to_cpu(de.inode);
755 de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
756 ext2fs_get_rec_len(ctx->fs, &de, &rec_len2);
757 if (dotdot >= ctx->fs->super->s_inodes_count ||
758 (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
759 dotdot != EXT2_ROOT_INO) ||
760 de.inode >= ctx->fs->super->s_inodes_count ||
761 (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
763 rec_len2 > EXT4_MIN_INLINE_DATA_SIZE -
764 EXT4_INLINE_DATA_DOTDOT_SIZE)
766 /* device files never have a "system.data" entry */
768 } else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
770 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
773 /* device files are never extent mapped */
776 for (i=0; i < EXT2_N_BLOCKS; i++) {
777 blk = inode->i_block[i];
783 if (blk < ctx->fs->super->s_first_data_block ||
784 blk >= ext2fs_blocks_count(ctx->fs->super) ||
785 is_blocks_used(ctx, blk, 1))
786 return; /* Invalid block, can't be dir */
788 blk = inode->i_block[0];
792 * If the mode says this is a device file and the i_links_count field
793 * is sane and we have not ruled it out as a device file previously,
794 * we declare it a device file, not a directory.
796 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
797 (inode->i_links_count == 1) && !not_device)
800 /* read the first block */
801 ehandler_operation(_("reading directory block"));
802 retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
803 ehandler_operation(0);
807 dirent = (struct ext2_dir_entry *) buf;
808 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
811 if ((ext2fs_dirent_name_len(dirent) != 1) ||
812 (dirent->name[0] != '.') ||
813 (dirent->inode != pctx->ino) ||
816 (rec_len >= ctx->fs->blocksize - 12))
819 dirent = (struct ext2_dir_entry *) (buf + rec_len);
820 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
823 if ((ext2fs_dirent_name_len(dirent) != 2) ||
824 (dirent->name[0] != '.') ||
825 (dirent->name[1] != '.') ||
831 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
832 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
833 e2fsck_write_inode_full(ctx, pctx->ino, inode,
834 EXT2_INODE_SIZE(ctx->fs->super),
835 "check_is_really_dir");
839 extern errcode_t e2fsck_setup_icount(e2fsck_t ctx, const char *icount_name,
840 int flags, ext2_icount_t hint,
843 unsigned int threshold;
844 unsigned int save_type;
852 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
854 profile_get_uint(ctx->profile, "scratch_files",
855 "numdirs_threshold", 0, 0, &threshold);
856 profile_get_boolean(ctx->profile, "scratch_files",
857 "icount", 0, 1, &enable);
859 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
861 num_dirs = 1024; /* Guess */
863 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
864 (!threshold || num_dirs > threshold)) {
865 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir,
870 e2fsck_set_bitmap_type(ctx->fs, EXT2FS_BMAP64_RBTREE, icount_name,
872 if (ctx->options & E2F_OPT_ICOUNT_FULLMAP)
873 flags |= EXT2_ICOUNT_OPT_FULLMAP;
874 retval = ext2fs_create_icount2(ctx->fs, flags, 0, hint, ret);
875 ctx->fs->default_bitmap_type = save_type;
879 static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
881 struct problem_context *pctx)
884 struct ext2_inode_large inode;
887 * Reread inode. If we don't see checksum error, then this inode
888 * has been fixed elsewhere.
890 ctx->stashed_ino = 0;
891 retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
893 if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
899 * Checksum still doesn't match. That implies that the inode passes
900 * all the sanity checks, so maybe the checksum is simply corrupt.
901 * See if the user will go for fixing that.
903 if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
907 e2fsck_pass1_fix_lock(ctx);
908 retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
910 e2fsck_pass1_fix_unlock(ctx);
914 static void reserve_block_for_root_repair(e2fsck_t ctx)
918 ext2_filsys fs = ctx->fs;
920 ctx->root_repair_block = 0;
921 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
924 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
927 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
928 ctx->root_repair_block = blk;
931 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
935 ext2_filsys fs = ctx->fs;
936 static const char name[] = "lost+found";
939 ctx->lnf_repair_block = 0;
940 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
943 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
946 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
947 ctx->lnf_repair_block = blk;
951 static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
955 struct ext2_xattr_handle *handle;
958 retval = ext2fs_xattrs_open(fs, ino, &handle);
962 retval = ext2fs_xattrs_read(handle);
966 retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
971 (void) ext2fs_xattrs_close(&handle);
975 static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
976 struct problem_context *pctx,
983 * If the inode failed the checksum and the user didn't
984 * clear the inode, test the checksum again -- if it still
985 * fails, ask the user if the checksum should be corrected.
987 pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
989 ctx->flags |= E2F_FLAG_ABORT;
991 #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
993 finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
994 if ((ctx)->flags & E2F_FLAG_ABORT) \
998 static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
1003 for (i = 0; i < EXT2_N_BLOCKS; i++) {
1004 x = inode->i_block[i];
1005 #ifdef WORDS_BIGENDIAN
1006 x = ext2fs_swab32(x);
1008 if (x >= ext2fs_blocks_count(fs->super))
1016 * Figure out what to do with an inode that has both extents and inline data
1017 * inode flags set. Returns -1 if we decide to erase the inode, 0 otherwise.
1019 static int fix_inline_data_extents_file(e2fsck_t ctx,
1021 struct ext2_inode *inode,
1023 struct problem_context *pctx)
1025 size_t max_inline_ea_size;
1026 ext2_filsys fs = ctx->fs;
1029 /* Both feature flags not set? Just run the regular checks */
1030 if (!ext2fs_has_feature_extents(fs->super) &&
1031 !ext2fs_has_feature_inline_data(fs->super))
1034 /* Clear both flags if it's a special file */
1035 if (LINUX_S_ISCHR(inode->i_mode) ||
1036 LINUX_S_ISBLK(inode->i_mode) ||
1037 LINUX_S_ISFIFO(inode->i_mode) ||
1038 LINUX_S_ISSOCK(inode->i_mode)) {
1039 check_extents_inlinedata(ctx, pctx);
1043 /* If it looks like an extent tree, try to clear inlinedata */
1044 if (ext2fs_extent_header_verify(inode->i_block,
1045 sizeof(inode->i_block)) == 0 &&
1046 fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
1047 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1052 /* If it looks short enough to be inline data, try to clear extents */
1053 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
1054 max_inline_ea_size = inode_size -
1055 (EXT2_GOOD_OLD_INODE_SIZE +
1056 ((struct ext2_inode_large *)inode)->i_extra_isize);
1058 max_inline_ea_size = 0;
1059 if (EXT2_I_SIZE(inode) <
1060 EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
1061 fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
1062 inode->i_flags &= ~EXT4_EXTENTS_FL;
1068 * Too big for inline data, but no evidence of extent tree -
1069 * maybe it's a block map file? If the mappings all look valid?
1071 if (could_be_block_map(fs, inode) &&
1072 fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
1073 #ifdef WORDS_BIGENDIAN
1076 for (i = 0; i < EXT2_N_BLOCKS; i++)
1077 inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
1080 inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
1085 /* Oh well, just clear the busted inode. */
1086 if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
1087 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1093 e2fsck_write_inode(ctx, ino, inode, "pass1");
1098 static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
1100 ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
1101 dgrp_t start = *group, grp;
1102 blk64_t blocks_to_read = 0;
1103 errcode_t err = EXT2_ET_INVALID_ARGUMENT;
1105 if (ctx->readahead_kb == 0)
1108 /* Keep iterating groups until we have enough to readahead */
1109 inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
1110 for (grp = start; grp < ctx->fs->group_desc_count; grp++) {
1111 if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
1113 inodes_in_group = ctx->fs->super->s_inodes_per_group -
1114 ext2fs_bg_itable_unused(ctx->fs, grp);
1115 blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
1117 if (blocks_to_read * ctx->fs->blocksize >
1118 ctx->readahead_kb * 1024)
1122 err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
1124 if (err == EAGAIN) {
1125 ctx->readahead_kb /= 2;
1131 /* Error; disable itable readahead */
1132 *group = ctx->fs->group_desc_count;
1133 *next_ino = ctx->fs->super->s_inodes_count;
1136 * Don't do more readahead until we've reached the first inode
1137 * of the last inode scan buffer block for the last group.
1140 inodes_per_buffer = (ctx->inode_buffer_blocks ?
1141 ctx->inode_buffer_blocks :
1142 EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
1143 ctx->fs->blocksize /
1144 EXT2_INODE_SIZE(ctx->fs->super);
1146 *next_ino = inodes_in_group -
1147 (inodes_in_group % inodes_per_buffer) + 1 +
1148 (grp * ctx->fs->super->s_inodes_per_group);
1153 * Check if the passed ino is one of the used superblock quota inodes.
1155 * Before the quota inodes were journaled, older superblock quota inodes
1156 * were just regular files in the filesystem and not reserved inodes. This
1157 * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1158 * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1160 static int quota_inum_is_super(struct ext2_super_block *sb, ext2_ino_t ino)
1162 enum quota_type qtype;
1164 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1165 if (*quota_sb_inump(sb, qtype) == ino)
1172 * Check if the passed ino is one of the reserved quota inodes.
1173 * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1174 * inodes. These inodes may or may not be in use by the quota feature.
1176 static int quota_inum_is_reserved(ext2_filsys fs, ext2_ino_t ino)
1178 enum quota_type qtype;
1180 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1181 if (quota_type2inum(qtype, fs->super) == ino)
1187 static int e2fsck_should_abort(e2fsck_t ctx)
1189 e2fsck_t global_ctx;
1191 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1194 if (ctx->global_ctx) {
1195 global_ctx = ctx->global_ctx;
1196 if (global_ctx->flags & E2F_FLAG_SIGNAL_MASK)
1203 * We need call mark_table_blocks() before multiple
1204 * thread start, since all known system blocks should be
1205 * marked and checked later.
1207 static errcode_t e2fsck_pass1_prepare(e2fsck_t ctx)
1209 struct problem_context pctx;
1210 ext2_filsys fs = ctx->fs;
1212 clear_problem_context(&pctx);
1213 if (!(ctx->options & E2F_OPT_PREEN))
1214 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1216 pctx.errcode = e2fsck_allocate_subcluster_bitmap(ctx->fs,
1217 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
1218 "block_found_map", &ctx->block_found_map);
1221 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1222 ctx->flags |= E2F_FLAG_ABORT;
1223 return pctx.errcode;
1225 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1226 _("metadata block map"), EXT2FS_BMAP64_RBTREE,
1227 "block_metadata_map", &ctx->block_metadata_map);
1230 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1231 ctx->flags |= E2F_FLAG_ABORT;
1232 return pctx.errcode;
1235 mark_table_blocks(ctx);
1236 pctx.errcode = ext2fs_convert_subcluster_bitmap(ctx->fs,
1237 &ctx->block_found_map);
1239 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1240 ctx->flags |= E2F_FLAG_ABORT;
1241 return pctx.errcode;
1244 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1245 _("multiply claimed block map"),
1246 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1247 &ctx->block_dup_map);
1250 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1252 /* Should never get here */
1253 ctx->flags |= E2F_FLAG_ABORT;
1254 return pctx.errcode;
1257 if (ext2fs_has_feature_mmp(fs->super) &&
1258 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1259 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1260 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1261 fs->super->s_mmp_block);
1263 pthread_mutex_init(&ctx->fs_fix_mutex, NULL);
1264 pthread_rwlock_init(&ctx->fs_block_map_rwlock, NULL);
1270 static void e2fsck_pass1_post(e2fsck_t ctx)
1272 struct problem_context pctx;
1273 ext2_filsys fs = ctx->fs;
1276 (char *)e2fsck_allocate_memory(ctx, ctx->fs->blocksize * 3,
1277 "block interate buffer");
1278 reserve_block_for_root_repair(ctx);
1279 reserve_block_for_lnf_repair(ctx);
1282 * If any extended attribute blocks' reference counts need to
1283 * be adjusted, either up (ctx->refcount_extra), or down
1284 * (ctx->refcount), then fix them.
1286 if (ctx->refcount) {
1287 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1288 ea_refcount_free(ctx->refcount);
1291 if (ctx->refcount_extra) {
1292 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1294 ea_refcount_free(ctx->refcount_extra);
1295 ctx->refcount_extra = 0;
1298 if (ctx->invalid_bitmaps)
1299 handle_fs_bad_blocks(ctx);
1301 /* We don't need the block_ea_map any more */
1302 if (ctx->block_ea_map) {
1303 ext2fs_free_block_bitmap(ctx->block_ea_map);
1304 ctx->block_ea_map = 0;
1307 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1308 struct ext2_inode *inode;
1309 int inode_size = EXT2_INODE_SIZE(fs->super);
1310 inode = e2fsck_allocate_memory(ctx, inode_size,
1313 clear_problem_context(&pctx);
1314 pctx.errcode = ext2fs_create_resize_inode(fs);
1316 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1318 ctx->flags |= E2F_FLAG_ABORT;
1319 ext2fs_free_mem(&inode);
1320 ext2fs_free_mem(&block_buf);
1325 if (!pctx.errcode) {
1326 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1328 inode->i_mtime = ctx->now;
1329 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1332 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1333 ext2fs_free_mem(&inode);
1336 if (ctx->flags & E2F_FLAG_RESTART) {
1337 ext2fs_free_mem(&block_buf);
1341 if (ctx->block_dup_map) {
1342 if (!(ctx->flags & E2F_FLAG_DUP_BLOCK)) {
1343 ext2fs_free_mem(&block_buf);
1346 if (ctx->options & E2F_OPT_PREEN) {
1347 clear_problem_context(&pctx);
1348 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1350 e2fsck_pass1_dupblocks(ctx, block_buf);
1351 ext2fs_free_mem(&block_buf);
1352 ctx->flags &= ~E2F_FLAG_DUP_BLOCK;
1357 void e2fsck_pass1_run(e2fsck_t ctx)
1360 ext2_filsys fs = ctx->fs;
1362 struct ext2_inode *inode = NULL;
1363 ext2_inode_scan scan = NULL;
1364 char *block_buf = NULL;
1365 #ifdef RESOURCE_TRACK
1366 struct resource_track rtrack;
1368 unsigned char frag, fsize;
1369 struct problem_context pctx;
1370 struct scan_callback_struct scan_struct;
1371 struct ext2_super_block *sb = ctx->fs->super;
1373 const char *eop_next_inode = _("getting next inode from scan");
1374 int imagic_fs, extent_fs, inlinedata_fs, casefold_fs;
1375 int low_dtime_check = 1;
1376 unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
1377 unsigned int bufsize;
1378 int failed_csum = 0;
1379 ext2_ino_t ino_threshold = 0;
1380 dgrp_t ra_group = 0;
1381 struct ea_quota ea_ibody_quota;
1382 struct process_inode_block *inodes_to_process;
1383 int process_inode_count;
1385 init_resource_track(&rtrack, ctx->fs->io);
1386 clear_problem_context(&pctx);
1388 /* If we can do readahead, figure out how many groups to pull in. */
1389 if (!e2fsck_can_readahead(ctx->fs))
1390 ctx->readahead_kb = 0;
1391 else if (ctx->readahead_kb == ~0ULL)
1392 ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
1393 pass1_readahead(ctx, &ra_group, &ino_threshold);
1395 if (ext2fs_has_feature_dir_index(fs->super) &&
1396 !(ctx->options & E2F_OPT_NO)) {
1397 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
1398 ctx->dirs_to_hash = 0;
1402 mtrace_print("Pass 1");
1405 imagic_fs = ext2fs_has_feature_imagic_inodes(sb);
1406 extent_fs = ext2fs_has_feature_extents(sb);
1407 inlinedata_fs = ext2fs_has_feature_inline_data(sb);
1408 casefold_fs = ext2fs_has_feature_casefold(sb);
1411 * Allocate bitmaps structures
1413 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1414 EXT2FS_BMAP64_RBTREE,
1416 &ctx->inode_used_map);
1419 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1420 ctx->flags |= E2F_FLAG_ABORT;
1423 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1424 _("directory inode map"),
1425 ctx->global_ctx ? EXT2FS_BMAP64_RBTREE :
1426 EXT2FS_BMAP64_AUTODIR,
1427 "inode_dir_map", &ctx->inode_dir_map);
1430 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1431 ctx->flags |= E2F_FLAG_ABORT;
1434 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1435 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1436 "inode_reg_map", &ctx->inode_reg_map);
1439 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1440 ctx->flags |= E2F_FLAG_ABORT;
1445 e2fsck_allocate_inode_bitmap(fs,
1446 _("inode casefold map"),
1447 EXT2FS_BMAP64_RBTREE,
1448 "inode_casefold_map",
1449 &ctx->inode_casefold_map);
1452 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1453 ctx->flags |= E2F_FLAG_ABORT;
1457 pctx.errcode = e2fsck_setup_icount(ctx, "inode_link_info", 0, NULL,
1458 &ctx->inode_link_info);
1460 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1461 ctx->flags |= E2F_FLAG_ABORT;
1464 bufsize = inode_size;
1465 if (bufsize < sizeof(struct ext2_inode_large))
1466 bufsize = sizeof(struct ext2_inode_large);
1467 inode = (struct ext2_inode *)
1468 e2fsck_allocate_memory(ctx, bufsize, "scratch inode");
1470 inodes_to_process = (struct process_inode_block *)
1471 e2fsck_allocate_memory(ctx,
1472 (ctx->process_inode_size *
1473 sizeof(struct process_inode_block)),
1474 "array of inodes to process");
1475 process_inode_count = 0;
1477 pctx.errcode = ext2fs_init_dblist(fs, 0);
1479 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1480 ctx->flags |= E2F_FLAG_ABORT;
1485 * If the last orphan field is set, clear it, since the pass1
1486 * processing will automatically find and clear the orphans.
1487 * In the future, we may want to try using the last_orphan
1488 * linked list ourselves, but for now, we clear it so that the
1489 * ext3 mount code won't get confused.
1491 if (!(ctx->options & E2F_OPT_READONLY)) {
1492 if (fs->super->s_last_orphan) {
1493 fs->super->s_last_orphan = 0;
1494 ext2fs_mark_super_dirty(fs);
1498 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1499 "block interate buffer");
1500 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1501 e2fsck_use_inode_shortcuts(ctx, 1);
1502 e2fsck_intercept_block_allocations(ctx);
1503 old_op = ehandler_operation(_("opening inode scan"));
1504 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1506 ehandler_operation(old_op);
1508 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1509 ctx->flags |= E2F_FLAG_ABORT;
1512 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1513 EXT2_SF_WARN_GARBAGE_INODES, 0);
1514 ctx->stashed_inode = inode;
1515 scan_struct.ctx = ctx;
1516 scan_struct.block_buf = block_buf;
1517 scan_struct.inodes_to_process = inodes_to_process;
1518 scan_struct.process_inode_count = &process_inode_count;
1519 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1520 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1521 ctx->fs->group_desc_count)))
1523 if ((fs->super->s_wtime &&
1524 fs->super->s_wtime < fs->super->s_inodes_count) ||
1525 (fs->super->s_mtime &&
1526 fs->super->s_mtime < fs->super->s_inodes_count) ||
1527 (fs->super->s_mkfs_time &&
1528 fs->super->s_mkfs_time < fs->super->s_inodes_count))
1529 low_dtime_check = 0;
1531 /* Set up ctx->lost_and_found if possible */
1532 (void) e2fsck_get_lost_and_found(ctx, 0);
1535 if (ctx->global_ctx) {
1536 if (ctx->options & E2F_OPT_DEBUG &&
1537 ctx->options & E2F_OPT_MULTITHREAD)
1538 fprintf(stderr, "thread %d jumping to group %d\n",
1539 ctx->thread_info.et_thread_index,
1540 ctx->thread_info.et_group_start);
1541 pctx.errcode = ext2fs_inode_scan_goto_blockgroup(scan,
1542 ctx->thread_info.et_group_start);
1544 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1545 ctx->flags |= E2F_FLAG_ABORT;
1552 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
1553 if (e2fsck_mmp_update(fs))
1554 fatal_error(ctx, 0);
1556 old_op = ehandler_operation(eop_next_inode);
1557 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1559 if (ino > ino_threshold)
1560 pass1_readahead(ctx, &ra_group, &ino_threshold);
1561 ehandler_operation(old_op);
1562 if (e2fsck_should_abort(ctx))
1564 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1566 * If badblocks says badblocks is bad, offer to clear
1567 * the list, update the in-core bb list, and restart
1570 if (ino == EXT2_BAD_INO &&
1571 fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1575 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1576 ext2fs_badblocks_list_free(ctx->fs->badblocks);
1577 ctx->fs->badblocks = NULL;
1578 err = ext2fs_read_bb_inode(ctx->fs,
1579 &ctx->fs->badblocks);
1581 fix_problem(ctx, PR_1_ISCAN_ERROR,
1583 ctx->flags |= E2F_FLAG_ABORT;
1585 ctx->flags |= E2F_FLAG_RESTART;
1588 if (!ctx->inode_bb_map)
1590 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1591 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1594 if (pctx.errcode == EXT2_ET_SCAN_FINISHED)
1597 pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1598 pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1599 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1600 ctx->flags |= E2F_FLAG_ABORT;
1606 if (ctx->global_ctx)
1607 ctx->thread_info.et_inode_number++;
1611 ctx->stashed_ino = ino;
1613 /* Clear trashed inode? */
1614 if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
1615 inode->i_links_count > 0 &&
1616 fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
1618 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1620 failed_csum = pctx.errcode != 0;
1623 * Check for inodes who might have been part of the
1624 * orphaned list linked list. They should have gotten
1625 * dealt with by now, unless the list had somehow been
1628 * FIXME: In the future, inodes which are still in use
1629 * (and which are therefore) pending truncation should
1630 * be handled specially. Right now we just clear the
1631 * dtime field, and the normal e2fsck handling of
1632 * inodes where i_size and the inode blocks are
1633 * inconsistent is to fix i_size, instead of releasing
1634 * the extra blocks. This won't catch the inodes that
1635 * was at the end of the orphan list, but it's better
1636 * than nothing. The right answer is that there
1637 * shouldn't be any bugs in the orphan list handling. :-)
1639 if (inode->i_dtime && low_dtime_check &&
1640 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1641 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1642 inode->i_dtime = inode->i_links_count ?
1644 e2fsck_write_inode(ctx, ino, inode,
1650 if (inode->i_links_count) {
1651 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1652 ino, inode->i_links_count);
1654 pctx.num = inode->i_links_count;
1655 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1656 ctx->flags |= E2F_FLAG_ABORT;
1659 } else if ((ino >= EXT2_FIRST_INODE(fs->super)) &&
1660 !quota_inum_is_reserved(fs, ino)) {
1661 if (!inode->i_dtime && inode->i_mode) {
1662 if (fix_problem(ctx,
1663 PR_1_ZERO_DTIME, &pctx)) {
1664 inode->i_dtime = ctx->now;
1665 e2fsck_write_inode(ctx, ino, inode,
1670 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1674 if ((inode->i_flags & EXT4_CASEFOLD_FL) &&
1675 ((!LINUX_S_ISDIR(inode->i_mode) &&
1676 fix_problem(ctx, PR_1_CASEFOLD_NONDIR, &pctx)) ||
1678 fix_problem(ctx, PR_1_CASEFOLD_FEATURE, &pctx)))) {
1679 inode->i_flags &= ~EXT4_CASEFOLD_FL;
1680 e2fsck_write_inode(ctx, ino, inode, "pass1");
1683 /* Conflicting inlinedata/extents inode flags? */
1684 if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
1685 (inode->i_flags & EXT4_EXTENTS_FL)) {
1686 int res = fix_inline_data_extents_file(ctx, ino, inode,
1690 /* skip FINISH_INODE_LOOP */
1695 /* Test for incorrect inline_data flags settings. */
1696 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
1697 (ino >= EXT2_FIRST_INODE(fs->super))) {
1700 pctx.errcode = get_inline_data_ea_size(fs, ino, &size);
1701 if (!pctx.errcode &&
1702 fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
1703 e2fsck_pass1_fix_lock(ctx);
1704 ext2fs_set_feature_inline_data(sb);
1705 ext2fs_mark_super_dirty(fs);
1706 e2fsck_pass1_fix_unlock(ctx);
1708 } else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
1709 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1710 /* skip FINISH_INODE_LOOP */
1715 /* Test for inline data flag but no attr */
1716 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
1717 (ino >= EXT2_FIRST_INODE(fs->super))) {
1724 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1725 err = get_inline_data_ea_size(fs, ino, &size);
1726 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1727 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1731 /* Everything is awesome... */
1733 case EXT2_ET_BAD_EA_BLOCK_NUM:
1734 case EXT2_ET_BAD_EA_HASH:
1735 case EXT2_ET_BAD_EA_HEADER:
1736 case EXT2_ET_EA_BAD_NAME_LEN:
1737 case EXT2_ET_EA_BAD_VALUE_SIZE:
1738 case EXT2_ET_EA_KEY_NOT_FOUND:
1739 case EXT2_ET_EA_NO_SPACE:
1740 case EXT2_ET_MISSING_EA_FEATURE:
1741 case EXT2_ET_INLINE_DATA_CANT_ITERATE:
1742 case EXT2_ET_INLINE_DATA_NO_BLOCK:
1743 case EXT2_ET_INLINE_DATA_NO_SPACE:
1744 case EXT2_ET_NO_INLINE_DATA:
1745 case EXT2_ET_EXT_ATTR_CSUM_INVALID:
1746 case EXT2_ET_EA_BAD_VALUE_OFFSET:
1747 case EXT2_ET_EA_INODE_CORRUPTED:
1748 /* broken EA or no system.data EA; truncate */
1749 if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
1751 err = ext2fs_inode_size_set(fs, inode, 0);
1754 ctx->flags |= E2F_FLAG_ABORT;
1757 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1758 memset(&inode->i_block, 0,
1759 sizeof(inode->i_block));
1760 e2fsck_write_inode(ctx, ino, inode,
1766 /* Some other kind of non-xattr error? */
1768 ctx->flags |= E2F_FLAG_ABORT;
1774 * Test for incorrect extent flag settings.
1776 * On big-endian machines we must be careful:
1777 * When the inode is read, the i_block array is not swapped
1778 * if the extent flag is set. Therefore if we are testing
1779 * for or fixing a wrongly-set flag, we must potentially
1780 * (un)swap before testing, or after fixing.
1784 * In this case the extents flag was set when read, so
1785 * extent_header_verify is ok. If the inode is cleared,
1786 * no need to swap... so no extra swapping here.
1788 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
1789 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1790 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1791 if ((ext2fs_extent_header_verify(inode->i_block,
1792 sizeof(inode->i_block)) == 0) &&
1793 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1794 e2fsck_pass1_fix_lock(ctx);
1795 ext2fs_set_feature_extents(sb);
1796 ext2fs_mark_super_dirty(fs);
1798 e2fsck_pass1_fix_unlock(ctx);
1799 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1801 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1802 if (ino == EXT2_BAD_INO)
1803 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1805 /* skip FINISH_INODE_LOOP */
1811 * For big-endian machines:
1812 * If the inode didn't have the extents flag set when it
1813 * was read, then the i_blocks array was swapped. To test
1814 * as an extents header, we must swap it back first.
1815 * IF we then set the extents flag, the entire i_block
1816 * array must be un/re-swapped to make it proper extents data.
1818 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1819 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1820 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1821 (LINUX_S_ISREG(inode->i_mode) ||
1822 LINUX_S_ISDIR(inode->i_mode))) {
1824 #ifdef WORDS_BIGENDIAN
1825 __u32 tmp_block[EXT2_N_BLOCKS];
1827 for (i = 0; i < EXT2_N_BLOCKS; i++)
1828 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1831 ehp = inode->i_block;
1833 if ((ext2fs_extent_header_verify(ehp,
1834 sizeof(inode->i_block)) == 0) &&
1835 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
1836 inode->i_flags |= EXT4_EXTENTS_FL;
1837 #ifdef WORDS_BIGENDIAN
1838 memcpy(inode->i_block, tmp_block,
1839 sizeof(inode->i_block));
1841 e2fsck_write_inode(ctx, ino, inode, "pass1");
1846 if (ino == EXT2_BAD_INO) {
1847 struct process_block_struct pb;
1849 if ((failed_csum || inode->i_mode || inode->i_uid ||
1850 inode->i_gid || inode->i_links_count ||
1851 (inode->i_flags & EXT4_INLINE_DATA_FL) ||
1852 inode->i_file_acl) &&
1853 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1854 memset(inode, 0, sizeof(struct ext2_inode));
1855 e2fsck_write_inode(ctx, ino, inode,
1860 e2fsck_pass1_block_map_r_lock(ctx);
1861 pctx.errcode = ext2fs_copy_bitmap(ctx->global_ctx ?
1862 ctx->global_ctx->block_found_map :
1863 ctx->block_found_map, &pb.fs_meta_blocks);
1864 e2fsck_pass1_block_map_r_unlock(ctx);
1867 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1868 ctx->flags |= E2F_FLAG_ABORT;
1871 pb.ino = EXT2_BAD_INO;
1872 pb.num_blocks = pb.last_block = 0;
1873 pb.last_db_block = -1;
1874 pb.num_illegal_blocks = 0;
1875 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1876 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1880 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1881 block_buf, process_bad_block, &pb);
1882 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1884 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1885 ctx->flags |= E2F_FLAG_ABORT;
1889 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1890 ctx->flags |= E2F_FLAG_ABORT;
1893 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1894 clear_problem_context(&pctx);
1895 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1897 } else if (ino == EXT2_ROOT_INO) {
1899 * Make sure the root inode is a directory; if
1900 * not, offer to clear it. It will be
1901 * regenerated in pass #3.
1903 if (!LINUX_S_ISDIR(inode->i_mode)) {
1904 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1908 * If dtime is set, offer to clear it. mke2fs
1909 * version 0.2b created filesystems with the
1910 * dtime field set for the root and lost+found
1911 * directories. We won't worry about
1912 * /lost+found, since that can be regenerated
1913 * easily. But we will fix the root directory
1914 * as a special case.
1916 if (inode->i_dtime && inode->i_links_count) {
1917 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1919 e2fsck_write_inode(ctx, ino, inode,
1924 } else if (ino == EXT2_JOURNAL_INO) {
1925 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1926 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1927 if (!LINUX_S_ISREG(inode->i_mode) &&
1928 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1930 inode->i_mode = LINUX_S_IFREG;
1931 e2fsck_write_inode(ctx, ino, inode,
1935 check_blocks(ctx, &pctx, block_buf, NULL);
1936 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1939 if ((inode->i_links_count ||
1940 inode->i_blocks || inode->i_block[0]) &&
1941 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1943 memset(inode, 0, inode_size);
1944 ext2fs_icount_store(ctx->inode_link_info,
1946 e2fsck_write_inode_full(ctx, ino, inode,
1947 inode_size, "pass1");
1950 } else if (quota_inum_is_reserved(fs, ino)) {
1951 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1952 if (ext2fs_has_feature_quota(fs->super) &&
1953 quota_inum_is_super(fs->super, ino)) {
1954 if (!LINUX_S_ISREG(inode->i_mode) &&
1955 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1957 inode->i_mode = LINUX_S_IFREG;
1958 e2fsck_write_inode(ctx, ino, inode,
1962 check_blocks(ctx, &pctx, block_buf, NULL);
1963 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1966 if ((inode->i_links_count ||
1967 inode->i_blocks || inode->i_block[0]) &&
1968 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1970 memset(inode, 0, inode_size);
1971 ext2fs_icount_store(ctx->inode_link_info,
1973 e2fsck_write_inode_full(ctx, ino, inode,
1974 inode_size, "pass1");
1977 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1978 problem_t problem = 0;
1980 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1981 if (ino == EXT2_BOOT_LOADER_INO) {
1982 if (LINUX_S_ISDIR(inode->i_mode))
1983 problem = PR_1_RESERVED_BAD_MODE;
1984 } else if (ino == EXT2_RESIZE_INO) {
1985 if (inode->i_mode &&
1986 !LINUX_S_ISREG(inode->i_mode))
1987 problem = PR_1_RESERVED_BAD_MODE;
1989 if (inode->i_mode != 0)
1990 problem = PR_1_RESERVED_BAD_MODE;
1993 if (fix_problem(ctx, problem, &pctx)) {
1995 e2fsck_write_inode(ctx, ino, inode,
2000 check_blocks(ctx, &pctx, block_buf, NULL);
2001 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2005 if (!inode->i_links_count) {
2006 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2010 * n.b. 0.3c ext2fs code didn't clear i_links_count for
2011 * deleted files. Oops.
2013 * Since all new ext2 implementations get this right,
2014 * we now assume that the case of non-zero
2015 * i_links_count and non-zero dtime means that we
2016 * should keep the file, not delete it.
2019 if (inode->i_dtime) {
2020 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
2022 e2fsck_write_inode(ctx, ino, inode, "pass1");
2027 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2028 switch (fs->super->s_creator_os) {
2030 frag = inode->osd2.hurd2.h_i_frag;
2031 fsize = inode->osd2.hurd2.h_i_fsize;
2037 if (inode->i_faddr || frag || fsize ||
2038 (!ext2fs_has_feature_largedir(fs->super) &&
2039 (LINUX_S_ISDIR(inode->i_mode) && inode->i_size_high)))
2040 mark_inode_bad(ctx, ino);
2041 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2042 !ext2fs_has_feature_64bit(fs->super) &&
2043 inode->osd2.linux2.l_i_file_acl_high != 0)
2044 mark_inode_bad(ctx, ino);
2045 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2046 !ext2fs_has_feature_huge_file(fs->super) &&
2047 (inode->osd2.linux2.l_i_blocks_hi != 0))
2048 mark_inode_bad(ctx, ino);
2049 if (inode->i_flags & EXT2_IMAGIC_FL) {
2051 if (!ctx->inode_imagic_map)
2052 alloc_imagic_map(ctx);
2053 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
2056 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
2057 inode->i_flags &= ~EXT2_IMAGIC_FL;
2058 e2fsck_write_inode(ctx, ino,
2065 check_inode_extra_space(ctx, &pctx, &ea_ibody_quota);
2066 check_is_really_dir(ctx, &pctx, block_buf);
2069 * ext2fs_inode_has_valid_blocks2 does not actually look
2070 * at i_block[] values, so not endian-sensitive here.
2072 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
2073 LINUX_S_ISLNK(inode->i_mode) &&
2074 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
2075 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
2076 inode->i_flags &= ~EXT4_EXTENTS_FL;
2077 e2fsck_write_inode(ctx, ino, inode, "pass1");
2081 if ((inode->i_flags & EXT4_ENCRYPT_FL) &&
2082 add_encrypted_file(ctx, &pctx) < 0)
2085 if (casefold_fs && inode->i_flags & EXT4_CASEFOLD_FL)
2086 ext2fs_mark_inode_bitmap2(ctx->inode_casefold_map, ino);
2088 if (LINUX_S_ISDIR(inode->i_mode)) {
2089 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
2090 e2fsck_add_dir_info(ctx, ino, 0);
2091 ctx->fs_directory_count++;
2092 if (inode->i_flags & EXT4_CASEFOLD_FL)
2093 add_casefolded_dir(ctx, ino);
2094 } else if (LINUX_S_ISREG (inode->i_mode)) {
2095 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
2096 ctx->fs_regular_count++;
2097 } else if (LINUX_S_ISCHR (inode->i_mode) &&
2098 e2fsck_pass1_check_device_inode(fs, inode)) {
2099 check_extents_inlinedata(ctx, &pctx);
2100 check_immutable(ctx, &pctx);
2101 check_size(ctx, &pctx);
2102 ctx->fs_chardev_count++;
2103 } else if (LINUX_S_ISBLK (inode->i_mode) &&
2104 e2fsck_pass1_check_device_inode(fs, inode)) {
2105 check_extents_inlinedata(ctx, &pctx);
2106 check_immutable(ctx, &pctx);
2107 check_size(ctx, &pctx);
2108 ctx->fs_blockdev_count++;
2109 } else if (LINUX_S_ISLNK (inode->i_mode) &&
2110 e2fsck_pass1_check_symlink(fs, ino, inode,
2112 check_immutable(ctx, &pctx);
2113 ctx->fs_symlinks_count++;
2114 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
2115 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2117 } else if (ext2fs_is_fast_symlink(inode)) {
2118 ctx->fs_fast_symlinks_count++;
2119 check_blocks(ctx, &pctx, block_buf,
2121 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2125 else if (LINUX_S_ISFIFO (inode->i_mode) &&
2126 e2fsck_pass1_check_device_inode(fs, inode)) {
2127 check_extents_inlinedata(ctx, &pctx);
2128 check_immutable(ctx, &pctx);
2129 check_size(ctx, &pctx);
2130 ctx->fs_fifo_count++;
2131 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
2132 e2fsck_pass1_check_device_inode(fs, inode)) {
2133 check_extents_inlinedata(ctx, &pctx);
2134 check_immutable(ctx, &pctx);
2135 check_size(ctx, &pctx);
2136 ctx->fs_sockets_count++;
2138 mark_inode_bad(ctx, ino);
2139 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2140 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
2141 if (inode->i_block[EXT2_IND_BLOCK])
2142 ctx->fs_ind_count++;
2143 if (inode->i_block[EXT2_DIND_BLOCK])
2144 ctx->fs_dind_count++;
2145 if (inode->i_block[EXT2_TIND_BLOCK])
2146 ctx->fs_tind_count++;
2148 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2149 !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
2150 (inode->i_block[EXT2_IND_BLOCK] ||
2151 inode->i_block[EXT2_DIND_BLOCK] ||
2152 inode->i_block[EXT2_TIND_BLOCK] ||
2153 ext2fs_file_acl_block(fs, inode))) {
2154 struct process_inode_block *itp;
2156 itp = &inodes_to_process[process_inode_count];
2158 itp->ea_ibody_quota = ea_ibody_quota;
2159 if (inode_size < sizeof(struct ext2_inode_large))
2160 memcpy(&itp->inode, inode, inode_size);
2162 memcpy(&itp->inode, inode, sizeof(itp->inode));
2163 process_inode_count++;
2165 check_blocks(ctx, &pctx, block_buf, &ea_ibody_quota);
2167 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2169 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2172 if (process_inode_count >= ctx->process_inode_size) {
2173 process_inodes(ctx, block_buf, inodes_to_process,
2174 &process_inode_count);
2176 if (e2fsck_should_abort(ctx))
2180 process_inodes(ctx, block_buf, inodes_to_process,
2181 &process_inode_count);
2182 ext2fs_close_inode_scan(scan);
2185 if (ctx->ea_block_quota_blocks) {
2186 ea_refcount_free(ctx->ea_block_quota_blocks);
2187 ctx->ea_block_quota_blocks = 0;
2190 if (ctx->ea_block_quota_inodes) {
2191 ea_refcount_free(ctx->ea_block_quota_inodes);
2192 ctx->ea_block_quota_inodes = 0;
2195 /* We don't need the encryption policy => ID map any more */
2196 destroy_encryption_policy_map(ctx);
2198 if (ctx->flags & E2F_FLAG_RESTART) {
2200 * Only the master copy of the superblock and block
2201 * group descriptors are going to be written during a
2202 * restart, so set the superblock to be used to be the
2203 * master superblock.
2205 ctx->use_superblock = 0;
2209 if (ctx->large_dirs && !ext2fs_has_feature_largedir(fs->super)) {
2210 if (fix_problem(ctx, PR_2_FEATURE_LARGE_DIRS, &pctx)) {
2211 ext2fs_set_feature_largedir(fs->super);
2212 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2213 ext2fs_mark_super_dirty(fs);
2215 if (fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
2216 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
2217 ext2fs_update_dynamic_rev(fs);
2218 ext2fs_mark_super_dirty(fs);
2222 ctx->flags |= E2F_FLAG_ALLOC_OK;
2224 e2fsck_use_inode_shortcuts(ctx, 0);
2225 ext2fs_free_mem(&inodes_to_process);
2226 inodes_to_process = 0;
2229 ext2fs_close_inode_scan(scan);
2231 ext2fs_free_mem(&block_buf);
2233 ext2fs_free_mem(&inode);
2236 * The l+f inode may have been cleared, so zap it now and
2237 * later passes will recalculate it if necessary
2239 ctx->lost_and_found = 0;
2241 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
2242 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
2244 ctx->invalid_bitmaps++;
2247 static void init_ext2_max_sizes()
2253 * Init ext2_max_sizes which will be immutable and shared between
2256 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
2258 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
2259 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
2260 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
2261 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
2262 max_sizes = (max_sizes * (1UL << i));
2263 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
2269 static errcode_t e2fsck_pass1_copy_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2270 ext2fs_generic_bitmap *dest)
2274 ret = ext2fs_copy_bitmap(*src, dest);
2283 static void e2fsck_pass1_free_bitmap(ext2fs_generic_bitmap *bitmap)
2286 ext2fs_free_generic_bmap(*bitmap);
2292 static errcode_t e2fsck_pass1_merge_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2293 ext2fs_generic_bitmap *dest)
2298 if (*dest == NULL) {
2302 ret = ext2fs_merge_bitmap(*src, *dest, NULL, NULL);
2312 static errcode_t e2fsck_pass1_copy_fs(ext2_filsys dest, e2fsck_t src_context,
2317 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2318 dest->inode_map = NULL;
2319 dest->block_map = NULL;
2320 dest->badblocks = NULL;
2322 dest->dblist->fs = dest;
2323 if (src->block_map) {
2324 retval = e2fsck_pass1_copy_bitmap(dest, &src->block_map,
2329 if (src->inode_map) {
2330 retval = e2fsck_pass1_copy_bitmap(dest, &src->inode_map,
2336 if (src->badblocks) {
2337 retval = ext2fs_badblocks_copy(src->badblocks,
2343 /* disable it for now */
2344 src_context->openfs_flags &= ~EXT2_FLAG_EXCLUSIVE;
2345 retval = ext2fs_open_channel(dest, src_context->io_options,
2346 src_context->io_manager,
2347 src_context->openfs_flags,
2348 src->io->block_size);
2352 /* Block size might not be default */
2353 io_channel_set_blksize(dest->io, src->io->block_size);
2354 ehandler_init(dest->io);
2356 assert(dest->io->magic == src->io->magic);
2357 assert(dest->io->manager == src->io->manager);
2358 assert(strcmp(dest->io->name, src->io->name) == 0);
2359 assert(dest->io->block_size == src->io->block_size);
2360 assert(dest->io->read_error == src->io->read_error);
2361 assert(dest->io->write_error == src->io->write_error);
2362 assert(dest->io->refcount == src->io->refcount);
2363 assert(dest->io->flags == src->io->flags);
2364 assert(dest->io->app_data == dest);
2365 assert(src->io->app_data == src);
2366 assert(dest->io->align == src->io->align);
2368 /* The data should be written to disk immediately */
2369 dest->io->flags |= CHANNEL_FLAGS_WRITETHROUGH;
2370 /* icache will be rebuilt if needed, so do not copy from @src */
2375 static int e2fsck_pass1_merge_fs(ext2_filsys dest, ext2_filsys src)
2377 struct ext2_inode_cache *icache = dest->icache;
2378 errcode_t retval = 0;
2380 io_channel dest_image_io;
2381 ext2fs_inode_bitmap inode_map;
2382 ext2fs_block_bitmap block_map;
2383 ext2_badblocks_list badblocks;
2388 dest_image_io = dest->image_io;
2389 inode_map = dest->inode_map;
2390 block_map = dest->block_map;
2391 badblocks = dest->badblocks;
2392 dblist = dest->dblist;
2393 flags = dest->flags;
2395 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2397 dest->image_io = dest_image_io;
2398 dest->icache = icache;
2399 dest->inode_map = inode_map;
2400 dest->block_map = block_map;
2401 dest->badblocks = badblocks;
2402 dest->dblist = dblist;
2404 dest->dblist->fs = dest;
2405 dest->flags = src->flags | flags;
2406 if (!(src->flags & EXT2_FLAG_VALID) || !(flags & EXT2_FLAG_VALID))
2407 ext2fs_unmark_valid(dest);
2410 ext2fs_free_inode_cache(src->icache);
2414 retval = e2fsck_pass1_merge_bitmap(dest, &src->inode_map,
2419 retval = e2fsck_pass1_merge_bitmap(dest, &src->block_map,
2426 retval = ext2fs_merge_dblist(src->dblist,
2431 dest->dblist = src->dblist;
2432 dest->dblist->fs = dest;
2437 if (src->badblocks) {
2438 if (dest->badblocks == NULL)
2439 retval = ext2fs_badblocks_copy(src->badblocks,
2442 retval = ext2fs_badblocks_merge(src->badblocks,
2446 io_channel_close(src->io);
2448 ext2fs_free_generic_bmap(src->inode_map);
2450 ext2fs_free_generic_bmap(src->block_map);
2452 ext2fs_badblocks_list_free(src->badblocks);
2454 ext2fs_free_dblist(src->dblist);
2459 static void e2fsck_pass1_copy_invalid_bitmaps(e2fsck_t global_ctx,
2460 e2fsck_t thread_ctx)
2463 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2464 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2465 dgrp_t total = grp_end - grp_start;
2467 thread_ctx->invalid_inode_bitmap_flag =
2468 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2469 "invalid_inode_bitmap");
2470 thread_ctx->invalid_block_bitmap_flag =
2471 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2472 "invalid_block_bitmap");
2473 thread_ctx->invalid_inode_table_flag =
2474 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2475 "invalid_inode_table");
2477 memcpy(thread_ctx->invalid_block_bitmap_flag,
2478 &global_ctx->invalid_block_bitmap_flag[grp_start],
2479 total * sizeof(int));
2480 memcpy(thread_ctx->invalid_inode_bitmap_flag,
2481 &global_ctx->invalid_inode_bitmap_flag[grp_start],
2482 total * sizeof(int));
2483 memcpy(thread_ctx->invalid_inode_table_flag,
2484 &global_ctx->invalid_inode_table_flag[grp_start],
2485 total * sizeof(int));
2487 thread_ctx->invalid_bitmaps = 0;
2488 for (i = grp_start, j = 0; i < grp_end; i++, j++) {
2489 if (thread_ctx->invalid_block_bitmap_flag[j])
2490 thread_ctx->invalid_bitmaps++;
2491 if (thread_ctx->invalid_inode_bitmap_flag[j])
2492 thread_ctx->invalid_bitmaps++;
2493 if (thread_ctx->invalid_inode_table_flag[j])
2494 thread_ctx->invalid_bitmaps++;
2498 static void e2fsck_pass1_merge_invalid_bitmaps(e2fsck_t global_ctx,
2499 e2fsck_t thread_ctx)
2501 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2502 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2503 dgrp_t total = grp_end - grp_start;
2505 memcpy(&global_ctx->invalid_block_bitmap_flag[grp_start],
2506 thread_ctx->invalid_block_bitmap_flag, total * sizeof(int));
2507 memcpy(&global_ctx->invalid_inode_bitmap_flag[grp_start],
2508 thread_ctx->invalid_inode_bitmap_flag, total * sizeof(int));
2509 memcpy(&global_ctx->invalid_inode_table_flag[grp_start],
2510 thread_ctx->invalid_inode_table_flag, total * sizeof(int));
2511 global_ctx->invalid_bitmaps += thread_ctx->invalid_bitmaps;
2514 static errcode_t e2fsck_pass1_thread_prepare(e2fsck_t global_ctx, e2fsck_t *thread_ctx,
2515 int thread_index, int num_threads)
2518 e2fsck_t thread_context;
2519 ext2_filsys thread_fs;
2520 ext2_filsys global_fs = global_ctx->fs;
2521 struct e2fsck_thread *tinfo;
2522 dgrp_t average_group;
2524 assert(global_ctx->inode_used_map == NULL);
2525 assert(global_ctx->inode_dir_map == NULL);
2526 assert(global_ctx->inode_bb_map == NULL);
2527 assert(global_ctx->inode_imagic_map == NULL);
2528 assert(global_ctx->inode_reg_map == NULL);
2529 assert(global_ctx->inodes_to_rebuild == NULL);
2531 assert(global_ctx->block_found_map != NULL);
2532 assert(global_ctx->block_metadata_map != NULL);
2533 assert(global_ctx->block_dup_map != NULL);
2534 assert(global_ctx->block_ea_map == NULL);
2535 assert(global_ctx->fs->dblist == NULL);
2537 retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &thread_context);
2539 com_err(global_ctx->program_name, retval, "while allocating memory");
2542 memcpy(thread_context, global_ctx, sizeof(struct e2fsck_struct));
2543 thread_context->block_dup_map = NULL;
2545 retval = e2fsck_allocate_block_bitmap(global_ctx->fs,
2546 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
2547 "block_found_map", &thread_context->block_found_map);
2551 thread_context->global_ctx = global_ctx;
2552 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &thread_fs);
2554 com_err(global_ctx->program_name, retval, "while allocating memory");
2558 io_channel_flush_cleanup(global_fs->io);
2559 retval = e2fsck_pass1_copy_fs(thread_fs, global_ctx, global_fs);
2561 com_err(global_ctx->program_name, retval, "while copying fs");
2564 thread_fs->priv_data = thread_context;
2566 thread_context->thread_info.et_thread_index = thread_index;
2567 set_up_logging(thread_context);
2570 * Distribute work to multiple threads:
2571 * Each thread work on fs->group_desc_count / nthread groups.
2573 tinfo = &thread_context->thread_info;
2574 average_group = thread_fs->group_desc_count / num_threads;
2575 if (average_group == 0)
2577 tinfo->et_group_start = average_group * thread_index;
2578 if (thread_index == num_threads - 1)
2579 tinfo->et_group_end = thread_fs->group_desc_count;
2581 tinfo->et_group_end = average_group * (thread_index + 1);
2582 tinfo->et_group_next = tinfo->et_group_start;
2583 tinfo->et_inode_number = 0;
2584 tinfo->et_log_buf[0] = '\0';
2585 tinfo->et_log_length = 0;
2586 if (thread_context->options & E2F_OPT_MULTITHREAD)
2587 log_out(thread_context, _("Scan group range [%d, %d)\n"),
2588 tinfo->et_group_start, tinfo->et_group_end);
2589 thread_context->fs = thread_fs;
2590 retval = quota_init_context(&thread_context->qctx, thread_fs, 0);
2592 com_err(global_ctx->program_name, retval,
2593 "while init quota context");
2596 *thread_ctx = thread_context;
2597 e2fsck_pass1_copy_invalid_bitmaps(global_ctx, thread_context);
2600 ext2fs_free_mem(&thread_fs);
2602 if (thread_context->block_found_map)
2603 ext2fs_free_mem(&thread_context->block_found_map);
2604 ext2fs_free_mem(&thread_context);
2608 static void e2fsck_pass1_merge_dir_info(e2fsck_t global_ctx, e2fsck_t thread_ctx)
2610 if (thread_ctx->dir_info == NULL)
2613 if (global_ctx->dir_info == NULL) {
2614 global_ctx->dir_info = thread_ctx->dir_info;
2615 thread_ctx->dir_info = NULL;
2619 e2fsck_merge_dir_info(global_ctx, thread_ctx->dir_info,
2620 global_ctx->dir_info);
2623 static void e2fsck_pass1_merge_dx_dir(e2fsck_t global_ctx, e2fsck_t thread_ctx)
2625 if (thread_ctx->dx_dir_info == NULL)
2628 if (global_ctx->dx_dir_info == NULL) {
2629 global_ctx->dx_dir_info = thread_ctx->dx_dir_info;
2630 global_ctx->dx_dir_info_size = thread_ctx->dx_dir_info_size;
2631 global_ctx->dx_dir_info_count = thread_ctx->dx_dir_info_count;
2632 thread_ctx->dx_dir_info = NULL;
2636 e2fsck_merge_dx_dir(global_ctx, thread_ctx);
2639 static inline errcode_t
2640 e2fsck_pass1_merge_icount(ext2_icount_t *dest_icount,
2641 ext2_icount_t *src_icount)
2644 if (*dest_icount == NULL) {
2645 *dest_icount = *src_icount;
2650 ret = ext2fs_icount_merge(*src_icount,
2660 static errcode_t e2fsck_pass1_merge_icounts(e2fsck_t global_ctx, e2fsck_t thread_ctx)
2664 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_count,
2665 &thread_ctx->inode_count);
2668 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_link_info,
2669 &thread_ctx->inode_link_info);
2674 static errcode_t e2fsck_pass1_merge_dirs_to_hash(e2fsck_t global_ctx,
2675 e2fsck_t thread_ctx)
2677 errcode_t retval = 0;
2679 if (!thread_ctx->dirs_to_hash)
2682 if (!global_ctx->dirs_to_hash)
2683 retval = ext2fs_badblocks_copy(thread_ctx->dirs_to_hash,
2684 &global_ctx->dirs_to_hash);
2686 retval = ext2fs_badblocks_merge(thread_ctx->dirs_to_hash,
2687 global_ctx->dirs_to_hash);
2692 static errcode_t e2fsck_pass1_merge_ea_inode_refs(e2fsck_t global_ctx,
2693 e2fsck_t thread_ctx)
2699 if (!thread_ctx->ea_inode_refs)
2702 if (!global_ctx->ea_inode_refs) {
2703 global_ctx->ea_inode_refs = thread_ctx->ea_inode_refs;
2704 thread_ctx->ea_inode_refs = NULL;
2708 ea_refcount_intr_begin(thread_ctx->ea_inode_refs);
2710 if ((blk = ea_refcount_intr_next(thread_ctx->ea_inode_refs,
2713 if (!global_ctx->block_ea_map ||
2714 !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
2716 retval = ea_refcount_store(global_ctx->ea_inode_refs,
2726 static ea_value_t ea_refcount_usage(e2fsck_t ctx, blk64_t blk,
2729 ea_value_t count_cur;
2730 ea_value_t count_extra = 0;
2731 ea_value_t count_orig;
2733 ea_refcount_fetch(ctx->refcount_orig, blk, &count_orig);
2734 ea_refcount_fetch(ctx->refcount, blk, &count_cur);
2735 /* most of time this is not needed */
2736 if (ctx->refcount_extra && count_cur == 0)
2737 ea_refcount_fetch(ctx->refcount_extra, blk, &count_extra);
2744 return count_orig + count_extra - count_cur;
2747 static errcode_t e2fsck_pass1_merge_ea_refcount(e2fsck_t global_ctx,
2748 e2fsck_t thread_ctx)
2752 errcode_t retval = 0;
2754 if (!thread_ctx->refcount)
2757 if (!global_ctx->refcount) {
2758 global_ctx->refcount = thread_ctx->refcount;
2759 thread_ctx->refcount = NULL;
2760 global_ctx->refcount_extra = thread_ctx->refcount;
2761 thread_ctx->refcount_extra = NULL;
2765 ea_refcount_intr_begin(thread_ctx->refcount);
2767 if ((blk = ea_refcount_intr_next(thread_ctx->refcount,
2771 * this EA has never seen before, so just store its
2772 * refcount and refcount_extra into global_ctx if needed.
2774 if (!global_ctx->block_ea_map ||
2775 !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
2779 retval = ea_refcount_store(global_ctx->refcount,
2784 if (count > 0 || !thread_ctx->refcount_extra)
2786 ea_refcount_fetch(thread_ctx->refcount_extra, blk,
2791 if (!global_ctx->refcount_extra) {
2792 retval = ea_refcount_create(0,
2793 &global_ctx->refcount_extra);
2797 retval = ea_refcount_store(global_ctx->refcount_extra,
2803 ea_value_t thread_usage;
2804 ea_value_t global_usage;
2807 thread_usage = ea_refcount_usage(thread_ctx,
2809 global_usage = ea_refcount_usage(global_ctx,
2811 if (thread_usage + global_usage <= orig) {
2812 new = orig - thread_usage - global_usage;
2813 retval = ea_refcount_store(global_ctx->refcount,
2819 /* update it is as zero */
2820 retval = ea_refcount_store(global_ctx->refcount,
2824 /* Ooops, this EA was referenced more than it stated */
2825 if (!global_ctx->refcount_extra) {
2826 retval = ea_refcount_create(0,
2827 &global_ctx->refcount_extra);
2831 new = global_usage + thread_usage - orig;
2832 retval = ea_refcount_store(global_ctx->refcount_extra,
2842 static int e2fsck_pass1_thread_join_one(e2fsck_t global_ctx, e2fsck_t thread_ctx)
2845 int flags = global_ctx->flags;
2846 ext2_filsys thread_fs = thread_ctx->fs;
2847 ext2_filsys global_fs = global_ctx->fs;
2848 FILE *global_logf = global_ctx->logf;
2849 FILE *global_problem_logf = global_ctx->problem_logf;
2850 ext2fs_inode_bitmap inode_bad_map = global_ctx->inode_bad_map;
2851 struct dir_info_db *dir_info = global_ctx->dir_info;
2852 struct dx_dir_info *dx_dir_info = global_ctx->dx_dir_info;
2853 ext2fs_inode_bitmap inode_used_map = global_ctx->inode_used_map;
2854 ext2fs_inode_bitmap inode_dir_map = global_ctx->inode_dir_map;
2855 ext2fs_inode_bitmap inode_bb_map = global_ctx->inode_bb_map;
2856 ext2fs_inode_bitmap inode_imagic_map = global_ctx->inode_imagic_map;
2857 ext2fs_inode_bitmap inode_reg_map = global_ctx->inode_reg_map;
2858 ext2fs_block_bitmap inodes_to_rebuild = global_ctx->inodes_to_rebuild;
2859 ext2_icount_t inode_count = global_ctx->inode_count;
2860 ext2_icount_t inode_link_info = global_ctx->inode_link_info;
2861 __u32 fs_directory_count = global_ctx->fs_directory_count;
2862 __u32 fs_regular_count = global_ctx->fs_regular_count;
2863 __u32 fs_blockdev_count = global_ctx->fs_blockdev_count;
2864 __u32 fs_chardev_count = global_ctx->fs_chardev_count;
2865 __u32 fs_links_count = global_ctx->fs_links_count;
2866 __u32 fs_symlinks_count = global_ctx->fs_symlinks_count;
2867 __u32 fs_fast_symlinks_count = global_ctx->fs_fast_symlinks_count;
2868 __u32 fs_fifo_count = global_ctx->fs_fifo_count;
2869 __u32 fs_total_count = global_ctx->fs_total_count;
2870 __u32 fs_badblocks_count = global_ctx->fs_badblocks_count;
2871 __u32 fs_sockets_count = global_ctx->fs_sockets_count;
2872 __u32 fs_ind_count = global_ctx->fs_ind_count;
2873 __u32 fs_dind_count = global_ctx->fs_dind_count;
2874 __u32 fs_tind_count = global_ctx->fs_tind_count;
2875 __u32 fs_fragmented = global_ctx->fs_fragmented;
2876 __u32 fs_fragmented_dir = global_ctx->fs_fragmented_dir;
2877 __u32 large_files = global_ctx->large_files;
2878 ext2_ino_t dx_dir_info_size = global_ctx->dx_dir_info_size;
2879 ext2_ino_t dx_dir_info_count = global_ctx->dx_dir_info_count;
2880 ext2_u32_list dirs_to_hash = global_ctx->dirs_to_hash;
2881 quota_ctx_t qctx = global_ctx->qctx;
2882 int *invalid_block_bitmap_flag = global_ctx->invalid_block_bitmap_flag;
2883 int *invalid_inode_bitmap_flag = global_ctx->invalid_inode_bitmap_flag;
2884 int *invalid_inode_table_flag = global_ctx->invalid_inode_table_flag;
2885 int invalid_bitmaps = global_ctx->invalid_bitmaps;
2886 ext2_refcount_t refcount = global_ctx->refcount;
2887 ext2_refcount_t refcount_extra = global_ctx->refcount_extra;
2888 ext2_refcount_t refcount_orig = global_ctx->refcount_orig;
2889 ext2_refcount_t ea_block_quota_blocks = global_ctx->ea_block_quota_blocks;
2890 ext2_refcount_t ea_block_quota_inodes = global_ctx->ea_block_quota_inodes;
2891 ext2fs_block_bitmap block_ea_map = global_ctx->block_ea_map;
2892 ext2_refcount_t ea_inode_refs = global_ctx->ea_inode_refs;
2893 ext2fs_block_bitmap block_found_map = global_ctx->block_found_map;
2894 ext2fs_block_bitmap block_dup_map = global_ctx->block_dup_map;
2896 #ifdef HAVE_SETJMP_H
2899 memcpy(old_jmp, global_ctx->abort_loc, sizeof(jmp_buf));
2901 memcpy(global_ctx, thread_ctx, sizeof(struct e2fsck_struct));
2902 #ifdef HAVE_SETJMP_H
2903 memcpy(global_ctx->abort_loc, old_jmp, sizeof(jmp_buf));
2906 global_ctx->inode_used_map = inode_used_map;
2907 global_ctx->inode_bad_map = inode_bad_map;
2908 global_ctx->inode_dir_map = inode_dir_map;
2909 global_ctx->inode_bb_map = inode_bb_map;
2910 global_ctx->inode_imagic_map = inode_imagic_map;
2911 global_ctx->inodes_to_rebuild = inodes_to_rebuild;
2912 global_ctx->inode_reg_map = inode_reg_map;
2913 global_ctx->block_dup_map = block_dup_map;
2914 global_ctx->block_found_map = block_found_map;
2915 global_ctx->dir_info = dir_info;
2916 e2fsck_pass1_merge_dir_info(global_ctx, thread_ctx);
2917 global_ctx->dx_dir_info = dx_dir_info;
2918 global_ctx->dx_dir_info_count = dx_dir_info_count;
2919 global_ctx->dx_dir_info_size = dx_dir_info_size;
2920 e2fsck_pass1_merge_dx_dir(global_ctx, thread_ctx);
2921 global_ctx->inode_count = inode_count;
2922 global_ctx->inode_link_info = inode_link_info;
2923 global_ctx->refcount = refcount;
2924 global_ctx->refcount_extra = refcount_extra;
2925 global_ctx->refcount_orig = refcount_orig;
2926 global_ctx->ea_block_quota_blocks = ea_block_quota_blocks;
2927 global_ctx->ea_block_quota_inodes = ea_block_quota_inodes;
2928 global_ctx->block_ea_map = block_ea_map;
2929 global_ctx->ea_inode_refs = ea_inode_refs;
2930 global_ctx->fs_directory_count += fs_directory_count;
2931 global_ctx->fs_regular_count += fs_regular_count;
2932 global_ctx->fs_blockdev_count += fs_blockdev_count;
2933 global_ctx->fs_chardev_count += fs_chardev_count;
2934 global_ctx->fs_links_count += fs_links_count;
2935 global_ctx->fs_symlinks_count += fs_symlinks_count;
2936 global_ctx->fs_fast_symlinks_count += fs_fast_symlinks_count;
2937 global_ctx->fs_fifo_count += fs_fifo_count;
2938 global_ctx->fs_total_count += fs_total_count;
2939 global_ctx->fs_badblocks_count += fs_badblocks_count;
2940 global_ctx->fs_sockets_count += fs_sockets_count;
2941 global_ctx->fs_ind_count += fs_ind_count;
2942 global_ctx->fs_dind_count += fs_dind_count;
2943 global_ctx->fs_tind_count += fs_tind_count;
2944 global_ctx->fs_fragmented += fs_fragmented;
2945 global_ctx->fs_fragmented_dir += fs_fragmented_dir;
2946 global_ctx->large_files += large_files;
2948 global_ctx->flags |= flags;
2950 retval = e2fsck_pass1_merge_fs(global_fs, thread_fs);
2952 com_err(global_ctx->program_name, 0, _("while merging fs\n"));
2955 global_fs->priv_data = global_ctx;
2956 global_ctx->fs = global_fs;
2957 global_ctx->logf = global_logf;
2958 global_ctx->problem_logf = global_problem_logf;
2959 global_ctx->global_ctx = NULL;
2960 retval = e2fsck_pass1_merge_icounts(global_ctx, thread_ctx);
2962 com_err(global_ctx->program_name, 0,
2963 _("while merging icounts\n"));
2967 global_ctx->dirs_to_hash = dirs_to_hash;
2968 retval = e2fsck_pass1_merge_dirs_to_hash(global_ctx, thread_ctx);
2970 com_err(global_ctx->program_name, 0,
2971 _("while merging dirs to hash\n"));
2975 e2fsck_pass1_merge_ea_inode_refs(global_ctx, thread_ctx);
2976 e2fsck_pass1_merge_ea_refcount(global_ctx, thread_ctx);
2977 global_ctx->qctx = qctx;
2978 retval = quota_merge_and_update_usage(global_ctx->qctx,
2982 global_ctx->invalid_block_bitmap_flag = invalid_block_bitmap_flag;
2983 global_ctx->invalid_inode_bitmap_flag = invalid_inode_bitmap_flag;
2984 global_ctx->invalid_inode_table_flag = invalid_inode_table_flag;
2985 global_ctx->invalid_bitmaps = invalid_bitmaps;
2986 e2fsck_pass1_merge_invalid_bitmaps(global_ctx, thread_ctx);
2988 retval = e2fsck_pass1_merge_bitmap(global_fs,
2989 &thread_ctx->inode_used_map,
2990 &global_ctx->inode_used_map);
2994 retval = e2fsck_pass1_merge_bitmap(global_fs,
2995 &thread_ctx->inode_bad_map,
2996 &global_ctx->inode_bad_map);
2999 retval = e2fsck_pass1_merge_bitmap(global_fs,
3000 &thread_ctx->inode_dir_map,
3001 &global_ctx->inode_dir_map);
3004 retval = e2fsck_pass1_merge_bitmap(global_fs,
3005 &thread_ctx->inode_bb_map,
3006 &global_ctx->inode_bb_map);
3009 retval = e2fsck_pass1_merge_bitmap(global_fs,
3010 &thread_ctx->inode_imagic_map,
3011 &global_ctx->inode_imagic_map);
3014 retval = e2fsck_pass1_merge_bitmap(global_fs,
3015 &thread_ctx->inode_reg_map,
3016 &global_ctx->inode_reg_map);
3019 retval = e2fsck_pass1_merge_bitmap(global_fs,
3020 &thread_ctx->inodes_to_rebuild,
3021 &global_ctx->inodes_to_rebuild);
3024 retval = e2fsck_pass1_merge_bitmap(global_fs,
3025 &thread_ctx->block_ea_map,
3026 &global_ctx->block_ea_map);
3030 if (ext2fs_has_feature_shared_blocks(global_fs->super) &&
3031 !(global_ctx->options & E2F_OPT_UNSHARE_BLOCKS))
3034 * This need be done after merging block_ea_map
3035 * because ea block might be shared, we need exclude
3036 * them from dup blocks.
3038 e2fsck_pass1_block_map_w_lock(thread_ctx);
3039 retval = ext2fs_merge_bitmap(thread_ctx->block_found_map,
3040 global_ctx->block_found_map,
3041 global_ctx->block_dup_map,
3042 global_ctx->block_ea_map);
3043 e2fsck_pass1_block_map_w_unlock(thread_ctx);
3044 if (retval == EEXIST)
3045 global_ctx->flags |= E2F_FLAG_DUP_BLOCK;
3050 static int e2fsck_pass1_thread_join(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3054 retval = e2fsck_pass1_thread_join_one(global_ctx, thread_ctx);
3055 ext2fs_free_mem(&thread_ctx->fs);
3056 if (thread_ctx->logf)
3057 fclose(thread_ctx->logf);
3058 if (thread_ctx->problem_logf) {
3059 fputs("</problem_log>\n", thread_ctx->problem_logf);
3060 fclose(thread_ctx->problem_logf);
3062 e2fsck_pass1_free_bitmap(&thread_ctx->inode_used_map);
3063 e2fsck_pass1_free_bitmap(&thread_ctx->inode_bad_map);
3064 e2fsck_pass1_free_bitmap(&thread_ctx->inode_dir_map);
3065 e2fsck_pass1_free_bitmap(&thread_ctx->inode_bb_map);
3066 e2fsck_pass1_free_bitmap(&thread_ctx->inode_imagic_map);
3067 e2fsck_pass1_free_bitmap(&thread_ctx->inode_reg_map);
3068 e2fsck_pass1_free_bitmap(&thread_ctx->inodes_to_rebuild);
3069 e2fsck_pass1_free_bitmap(&thread_ctx->block_found_map);
3070 e2fsck_pass1_free_bitmap(&thread_ctx->block_ea_map);
3071 if (thread_ctx->refcount)
3072 ea_refcount_free(thread_ctx->refcount);
3073 if (thread_ctx->refcount_extra)
3074 ea_refcount_free(thread_ctx->refcount_extra);
3075 if (thread_ctx->ea_inode_refs)
3076 ea_refcount_free(thread_ctx->ea_inode_refs);
3077 if (thread_ctx->refcount_orig)
3078 ea_refcount_free(thread_ctx->refcount_orig);
3079 e2fsck_free_dir_info(thread_ctx);
3080 ext2fs_free_icount(thread_ctx->inode_count);
3081 ext2fs_free_icount(thread_ctx->inode_link_info);
3082 if (thread_ctx->dirs_to_hash)
3083 ext2fs_badblocks_list_free(thread_ctx->dirs_to_hash);
3084 quota_release_context(&thread_ctx->qctx);
3085 ext2fs_free_mem(&thread_ctx->invalid_block_bitmap_flag);
3086 ext2fs_free_mem(&thread_ctx->invalid_inode_bitmap_flag);
3087 ext2fs_free_mem(&thread_ctx->invalid_inode_table_flag);
3088 ext2fs_free_mem(&thread_ctx);
3093 static int e2fsck_pass1_threads_join(struct e2fsck_thread_info *infos,
3094 int num_threads, e2fsck_t global_ctx)
3099 struct e2fsck_thread_info *pinfo;
3101 /* merge invalid bitmaps will recalculate it */
3102 global_ctx->invalid_bitmaps = 0;
3103 for (i = 0; i < num_threads; i++) {
3106 if (!pinfo->eti_started)
3109 rc = pthread_join(pinfo->eti_thread_id, NULL);
3111 com_err(global_ctx->program_name, rc,
3112 _("while joining thread\n"));
3116 rc = e2fsck_pass1_thread_join(global_ctx, infos[i].eti_thread_ctx);
3118 com_err(global_ctx->program_name, rc,
3119 _("while joining pass1 thread\n"));
3129 static void *e2fsck_pass1_thread(void *arg)
3131 struct e2fsck_thread_info *info = arg;
3132 e2fsck_t thread_ctx = info->eti_thread_ctx;
3133 #ifdef DEBUG_THREADS
3134 struct e2fsck_thread_debug *thread_debug = info->eti_debug;
3137 #ifdef DEBUG_THREADS
3138 pthread_mutex_lock(&thread_debug->etd_mutex);
3139 while (info->eti_thread_index > thread_debug->etd_finished_threads) {
3140 pthread_cond_wait(&thread_debug->etd_cond,
3141 &thread_debug->etd_mutex);
3143 pthread_mutex_unlock(&thread_debug->etd_mutex);
3146 #ifdef HAVE_SETJMP_H
3148 * When fatal_error() happens, jump to here. The thread
3149 * context's flags will be saved, but its abort_loc will
3150 * be overwritten by original jump buffer for the later
3153 if (setjmp(thread_ctx->abort_loc)) {
3154 thread_ctx->flags &= ~E2F_FLAG_SETJMP_OK;
3157 thread_ctx->flags |= E2F_FLAG_SETJMP_OK;
3160 e2fsck_pass1_run(thread_ctx);
3163 if (thread_ctx->options & E2F_OPT_MULTITHREAD)
3165 _("Scanned group range [%lu, %lu), inodes %lu\n"),
3166 thread_ctx->thread_info.et_group_start,
3167 thread_ctx->thread_info.et_group_end,
3168 thread_ctx->thread_info.et_inode_number);
3170 #ifdef DEBUG_THREADS
3171 pthread_mutex_lock(&thread_debug->etd_mutex);
3172 thread_debug->etd_finished_threads++;
3173 pthread_cond_broadcast(&thread_debug->etd_cond);
3174 pthread_mutex_unlock(&thread_debug->etd_mutex);
3180 static int e2fsck_pass1_threads_start(struct e2fsck_thread_info **pinfo,
3181 int num_threads, e2fsck_t global_ctx)
3183 struct e2fsck_thread_info *infos;
3184 pthread_attr_t attr;
3187 struct e2fsck_thread_info *tmp_pinfo;
3189 e2fsck_t thread_ctx;
3190 #ifdef DEBUG_THREADS
3191 struct e2fsck_thread_debug thread_debug =
3192 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0};
3194 thread_debug.etd_finished_threads = 0;
3197 retval = pthread_attr_init(&attr);
3199 com_err(global_ctx->program_name, retval,
3200 _("while setting pthread attribute\n"));
3204 infos = calloc(num_threads, sizeof(struct e2fsck_thread_info));
3205 if (infos == NULL) {
3207 com_err(global_ctx->program_name, retval,
3208 _("while allocating memory for threads\n"));
3209 pthread_attr_destroy(&attr);
3213 for (i = 0; i < num_threads; i++) {
3214 tmp_pinfo = &infos[i];
3215 tmp_pinfo->eti_thread_index = i;
3216 #ifdef DEBUG_THREADS
3217 tmp_pinfo->eti_debug = &thread_debug;
3219 retval = e2fsck_pass1_thread_prepare(global_ctx, &thread_ctx,
3222 com_err(global_ctx->program_name, retval,
3223 _("while preparing pass1 thread\n"));
3226 tmp_pinfo->eti_thread_ctx = thread_ctx;
3228 retval = pthread_create(&tmp_pinfo->eti_thread_id, &attr,
3229 &e2fsck_pass1_thread, tmp_pinfo);
3231 com_err(global_ctx->program_name, retval,
3232 _("while creating thread\n"));
3233 e2fsck_pass1_thread_join(global_ctx, thread_ctx);
3237 tmp_pinfo->eti_started = 1;
3240 /* destroy the thread attribute object, since it is no longer needed */
3241 ret = pthread_attr_destroy(&attr);
3243 com_err(global_ctx->program_name, ret,
3244 _("while destroying thread attribute\n"));
3250 e2fsck_pass1_threads_join(infos, num_threads, global_ctx);
3257 static void e2fsck_pass1_multithread(e2fsck_t global_ctx)
3259 struct e2fsck_thread_info *infos = NULL;
3260 int num_threads = 1;
3263 retval = e2fsck_pass1_threads_start(&infos, num_threads, global_ctx);
3265 com_err(global_ctx->program_name, retval,
3266 _("while starting pass1 threads\n"));
3270 retval = e2fsck_pass1_threads_join(infos, num_threads, global_ctx);
3272 com_err(global_ctx->program_name, retval,
3273 _("while joining pass1 threads\n"));
3278 global_ctx->flags |= E2F_FLAG_ABORT;
3283 /* TODO: tdb needs to be handled properly for multiple threads*/
3284 static int multiple_threads_supported(e2fsck_t ctx)
3287 unsigned int threshold;
3288 ext2_ino_t num_dirs;
3293 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
3295 profile_get_uint(ctx->profile, "scratch_files",
3296 "numdirs_threshold", 0, 0, &threshold);
3297 profile_get_boolean(ctx->profile, "scratch_files",
3298 "icount", 0, 1, &enable);
3300 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
3302 num_dirs = 1024; /* Guess */
3304 /* tdb is unsupported now */
3305 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
3306 (!threshold || num_dirs > threshold))
3312 void e2fsck_pass1(e2fsck_t ctx)
3317 init_ext2_max_sizes();
3318 retval = e2fsck_pass1_prepare(ctx);
3322 if (multiple_threads_supported(ctx)) {
3324 e2fsck_pass1_multithread(ctx);
3326 fprintf(stderr, "Fall through single thread for pass1 "
3327 "because tdb could not handle properly\n");
3331 e2fsck_pass1_run(ctx);
3332 e2fsck_pass1_post(ctx);
3335 #undef FINISH_INODE_LOOP
3338 * When the inode_scan routines call this callback at the end of the
3339 * glock group, call process_inodes.
3341 static errcode_t scan_callback(ext2_filsys fs,
3342 ext2_inode_scan scan EXT2FS_ATTR((unused)),
3343 dgrp_t group, void * priv_data)
3345 struct scan_callback_struct *scan_struct;
3347 struct e2fsck_thread *tinfo;
3349 scan_struct = (struct scan_callback_struct *) priv_data;
3350 ctx = scan_struct->ctx;
3352 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf,
3353 scan_struct->inodes_to_process,
3354 scan_struct->process_inode_count);
3357 if ((ctx->progress)(ctx, 1, group+1,
3358 ctx->fs->group_desc_count))
3359 return EXT2_ET_CANCEL_REQUESTED;
3362 if (ctx->global_ctx) {
3363 tinfo = &ctx->thread_info;
3364 tinfo->et_group_next++;
3365 if (ctx->options & E2F_OPT_DEBUG &&
3366 ctx->options & E2F_OPT_MULTITHREAD)
3367 log_out(ctx, _("group %d finished\n"),
3368 tinfo->et_group_next);
3369 if (tinfo->et_group_next >= tinfo->et_group_end)
3370 return EXT2_ET_SCAN_FINISHED;
3378 * Process the inodes in the "inodes to process" list.
3380 static void process_inodes(e2fsck_t ctx, char *block_buf,
3381 struct process_inode_block *inodes_to_process,
3382 int *process_inode_count)
3385 struct ext2_inode *old_stashed_inode;
3386 ext2_ino_t old_stashed_ino;
3387 const char *old_operation;
3389 struct problem_context pctx;
3392 printf("begin process_inodes: ");
3394 if (*process_inode_count == 0)
3396 old_operation = ehandler_operation(0);
3397 old_stashed_inode = ctx->stashed_inode;
3398 old_stashed_ino = ctx->stashed_ino;
3399 qsort(inodes_to_process, *process_inode_count,
3400 sizeof(struct process_inode_block), process_inode_cmp);
3401 clear_problem_context(&pctx);
3402 for (i=0; i < *process_inode_count; i++) {
3403 pctx.inode = ctx->stashed_inode =
3404 (struct ext2_inode *) &inodes_to_process[i].inode;
3405 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
3408 printf("%u ", pctx.ino);
3410 sprintf(buf, _("reading indirect blocks of inode %u"),
3412 ehandler_operation(buf);
3413 check_blocks(ctx, &pctx, block_buf,
3414 &inodes_to_process[i].ea_ibody_quota);
3415 if (e2fsck_should_abort(ctx))
3418 ctx->stashed_inode = old_stashed_inode;
3419 ctx->stashed_ino = old_stashed_ino;
3420 *process_inode_count = 0;
3422 printf("end process inodes\n");
3424 ehandler_operation(old_operation);
3427 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
3429 const struct process_inode_block *ib_a =
3430 (const struct process_inode_block *) a;
3431 const struct process_inode_block *ib_b =
3432 (const struct process_inode_block *) b;
3435 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
3436 ib_b->inode.i_block[EXT2_IND_BLOCK]);
3439 * We only call process_inodes() for non-extent
3440 * inodes, so it's OK to pass NULL to
3441 * ext2fs_file_acl_block() here.
3443 ret = ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a->inode)) -
3444 ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b->inode));
3446 ret = ib_a->ino - ib_b->ino;
3451 * Mark an inode as being bad in some what
3453 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
3455 struct problem_context pctx;
3457 if (!ctx->inode_bad_map) {
3458 clear_problem_context(&pctx);
3460 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3461 _("bad inode map"), EXT2FS_BMAP64_RBTREE,
3462 "inode_bad_map", &ctx->inode_bad_map);
3465 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3466 /* Should never get here */
3467 ctx->flags |= E2F_FLAG_ABORT;
3471 ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
3474 static void add_casefolded_dir(e2fsck_t ctx, ino_t ino)
3476 struct problem_context pctx;
3478 if (!ctx->casefolded_dirs) {
3479 pctx.errcode = ext2fs_u32_list_create(&ctx->casefolded_dirs, 0);
3483 pctx.errcode = ext2fs_u32_list_add(ctx->casefolded_dirs, ino);
3484 if (pctx.errcode == 0)
3487 fix_problem(ctx, PR_1_ALLOCATE_CASEFOLDED_DIRLIST, &pctx);
3488 /* Should never get here */
3489 ctx->flags |= E2F_FLAG_ABORT;
3493 * This procedure will allocate the inode "bb" (badblock) map table
3495 static void alloc_bb_map(e2fsck_t ctx)
3497 struct problem_context pctx;
3499 clear_problem_context(&pctx);
3500 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3501 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
3502 "inode_bb_map", &ctx->inode_bb_map);
3505 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3506 /* Should never get here */
3507 ctx->flags |= E2F_FLAG_ABORT;
3513 * This procedure will allocate the inode imagic table
3515 static void alloc_imagic_map(e2fsck_t ctx)
3517 struct problem_context pctx;
3519 clear_problem_context(&pctx);
3520 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
3521 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
3522 "inode_imagic_map", &ctx->inode_imagic_map);
3525 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
3526 /* Should never get here */
3527 ctx->flags |= E2F_FLAG_ABORT;
3533 * Marks a block as in use, setting the dup_map if it's been set
3534 * already. Called by process_block and process_bad_block.
3536 * WARNING: Assumes checks have already been done to make sure block
3537 * is valid. This is true in both process_block and process_bad_block.
3539 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
3541 struct problem_context pctx;
3542 e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
3544 clear_problem_context(&pctx);
3546 if (is_blocks_used(ctx, block, 1)) {
3547 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
3548 !(ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
3551 ctx->flags |= E2F_FLAG_DUP_BLOCK;
3552 e2fsck_pass1_block_map_w_lock(ctx);
3553 ext2fs_fast_mark_block_bitmap2(global_ctx->block_dup_map, block);
3554 e2fsck_pass1_block_map_w_unlock(ctx);
3556 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
3561 * When cluster size is greater than one block, it is caller's responsibility
3562 * to make sure block parameter starts at a cluster boundary.
3564 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
3567 if (!is_blocks_used(ctx, block, num)) {
3568 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
3572 for (i = 0; i < num; i += EXT2FS_CLUSTER_RATIO(ctx->fs))
3573 mark_block_used(ctx, block + i);
3577 static errcode_t _INLINE_ e2fsck_write_ext_attr3(e2fsck_t ctx, blk64_t block,
3578 void *inbuf, ext2_ino_t inum)
3581 ext2_filsys fs = ctx->fs;
3583 e2fsck_pass1_fix_lock(ctx);
3584 retval = ext2fs_write_ext_attr3(fs, block, inbuf, inum);
3585 e2fsck_pass1_fix_unlock(ctx);
3590 * Adjust the extended attribute block's reference counts at the end
3591 * of pass 1, either by subtracting out references for EA blocks that
3592 * are still referenced in ctx->refcount, or by adding references for
3593 * EA blocks that had extra references as accounted for in
3594 * ctx->refcount_extra.
3596 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
3597 char *block_buf, int adjust_sign)
3599 struct ext2_ext_attr_header *header;
3600 struct problem_context pctx;
3601 ext2_filsys fs = ctx->fs;
3606 clear_problem_context(&pctx);
3608 ea_refcount_intr_begin(refcount);
3610 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
3613 pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
3616 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
3619 header = (struct ext2_ext_attr_header *) block_buf;
3620 pctx.blkcount = header->h_refcount;
3621 should_be = header->h_refcount + adjust_sign * (int)count;
3622 pctx.num = should_be;
3623 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
3624 header->h_refcount = should_be;
3625 pctx.errcode = e2fsck_write_ext_attr3(ctx, blk,
3629 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
3638 * Handle processing the extended attribute blocks
3640 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
3641 char *block_buf, struct ea_quota *ea_block_quota)
3643 ext2_filsys fs = ctx->fs;
3644 ext2_ino_t ino = pctx->ino;
3645 struct ext2_inode *inode = pctx->inode;
3648 struct ext2_ext_attr_header *header;
3649 struct ext2_ext_attr_entry *first, *entry;
3650 blk64_t quota_blocks = EXT2FS_C2B(fs, 1);
3651 __u64 quota_inodes = 0;
3652 region_t region = 0;
3653 int failed_csum = 0;
3655 ea_block_quota->blocks = 0;
3656 ea_block_quota->inodes = 0;
3658 blk = ext2fs_file_acl_block(fs, inode);
3663 * If the Extended attribute flag isn't set, then a non-zero
3664 * file acl means that the inode is corrupted.
3666 * Or if the extended attribute block is an invalid block,
3667 * then the inode is also corrupted.
3669 if (!ext2fs_has_feature_xattr(fs->super) ||
3670 (blk < fs->super->s_first_data_block) ||
3671 (blk >= ext2fs_blocks_count(fs->super))) {
3672 mark_inode_bad(ctx, ino);
3676 /* If ea bitmap hasn't been allocated, create it */
3677 if (!ctx->block_ea_map) {
3678 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
3679 _("ext attr block map"),
3680 EXT2FS_BMAP64_RBTREE, "block_ea_map",
3681 &ctx->block_ea_map);
3682 if (pctx->errcode) {
3684 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
3685 ctx->flags |= E2F_FLAG_ABORT;
3690 /* Create the EA refcount structure if necessary */
3691 if (!ctx->refcount) {
3692 pctx->errcode = ea_refcount_create(0,
3693 &ctx->refcount_orig);
3694 if (pctx->errcode) {
3696 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
3697 ctx->flags |= E2F_FLAG_ABORT;
3701 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
3702 if (pctx->errcode) {
3704 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
3705 ctx->flags |= E2F_FLAG_ABORT;
3711 /* Debugging text */
3712 printf("Inode %u has EA block %u\n", ino, blk);
3715 /* Have we seen this EA block before? */
3716 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
3717 ea_block_quota->blocks = EXT2FS_C2B(fs, 1);
3718 ea_block_quota->inodes = 0;
3720 if (ctx->ea_block_quota_blocks) {
3721 ea_refcount_fetch(ctx->ea_block_quota_blocks, blk,
3724 ea_block_quota->blocks = quota_blocks;
3727 if (ctx->ea_block_quota_inodes)
3728 ea_refcount_fetch(ctx->ea_block_quota_inodes, blk,
3729 &ea_block_quota->inodes);
3731 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
3733 /* Ooops, this EA was referenced more than it stated */
3734 if (!ctx->refcount_extra) {
3735 pctx->errcode = ea_refcount_create(0,
3736 &ctx->refcount_extra);
3737 if (pctx->errcode) {
3739 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
3740 ctx->flags |= E2F_FLAG_ABORT;
3744 ea_refcount_increment(ctx->refcount_extra, blk, 0);
3749 * OK, we haven't seen this EA block yet. So we need to
3753 pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
3754 if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
3757 } else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
3760 if (pctx->errcode &&
3761 fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
3765 header = (struct ext2_ext_attr_header *) block_buf;
3766 pctx->blk = ext2fs_file_acl_block(fs, inode);
3767 if (((ctx->ext_attr_ver == 1) &&
3768 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
3769 ((ctx->ext_attr_ver == 2) &&
3770 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
3771 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
3775 if (header->h_blocks != 1) {
3776 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
3780 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
3783 region = region_create(0, fs->blocksize);
3785 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
3786 ctx->flags |= E2F_FLAG_ABORT;
3789 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
3790 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
3794 first = (struct ext2_ext_attr_entry *)(header+1);
3795 end = block_buf + fs->blocksize;
3797 while ((char *)entry < end && *(__u32 *)entry) {
3800 if (region_allocate(region, (char *)entry - (char *)header,
3801 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
3802 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
3806 if ((ctx->ext_attr_ver == 1 &&
3807 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
3808 (ctx->ext_attr_ver == 2 &&
3809 entry->e_name_index == 0)) {
3810 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
3814 if (entry->e_value_inum == 0) {
3815 if (entry->e_value_size > EXT2_XATTR_SIZE_MAX ||
3816 (entry->e_value_offs + entry->e_value_size >
3818 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
3822 if (entry->e_value_size &&
3823 region_allocate(region, entry->e_value_offs,
3824 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
3825 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
3830 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
3831 entry->e_value_offs);
3833 if (entry->e_hash != hash) {
3834 pctx->num = entry->e_hash;
3835 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
3837 entry->e_hash = hash;
3841 blk64_t entry_quota_blocks;
3843 problem = check_large_ea_inode(ctx, entry, pctx,
3844 &entry_quota_blocks);
3845 if (problem && fix_problem(ctx, problem, pctx))
3848 quota_blocks += entry_quota_blocks;
3852 entry = EXT2_EXT_ATTR_NEXT(entry);
3854 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
3855 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
3858 region_free(region);
3861 * We only get here if there was no other errors that were fixed.
3862 * If there was a checksum fail, ask to correct it.
3865 fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
3866 pctx->errcode = e2fsck_write_ext_attr3(ctx, blk, block_buf,
3872 if (quota_blocks != EXT2FS_C2B(fs, 1U)) {
3873 if (!ctx->ea_block_quota_blocks) {
3874 pctx->errcode = ea_refcount_create(0,
3875 &ctx->ea_block_quota_blocks);
3876 if (pctx->errcode) {
3881 ea_refcount_store(ctx->ea_block_quota_blocks, blk,
3886 if (!ctx->ea_block_quota_inodes) {
3887 pctx->errcode = ea_refcount_create(0,
3888 &ctx->ea_block_quota_inodes);
3889 if (pctx->errcode) {
3892 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
3893 ctx->flags |= E2F_FLAG_ABORT;
3898 ea_refcount_store(ctx->ea_block_quota_inodes, blk,
3901 ea_block_quota->blocks = quota_blocks;
3902 ea_block_quota->inodes = quota_inodes;
3904 inc_ea_inode_refs(ctx, pctx, first, end);
3905 ea_refcount_store(ctx->refcount, blk, header->h_refcount - 1);
3906 ea_refcount_store(ctx->refcount_orig, blk, header->h_refcount);
3908 * It might be racy that this block has been merged in the
3911 if (!is_blocks_used(ctx, blk, 1))
3912 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, blk);
3913 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
3918 region_free(region);
3919 ext2fs_file_acl_block_set(fs, inode, 0);
3920 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
3924 /* Returns 1 if bad htree, 0 if OK */
3925 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
3926 ext2_ino_t ino, struct ext2_inode *inode,
3929 struct ext2_dx_root_info *root;
3930 ext2_filsys fs = ctx->fs;
3934 if ((!LINUX_S_ISDIR(inode->i_mode) &&
3935 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
3936 (!ext2fs_has_feature_dir_index(fs->super) &&
3937 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
3940 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
3942 if ((pctx->errcode) ||
3944 (blk < fs->super->s_first_data_block) ||
3945 (blk >= ext2fs_blocks_count(fs->super))) {
3946 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
3952 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
3953 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
3956 /* XXX should check that beginning matches a directory */
3957 root = (struct ext2_dx_root_info *) (block_buf + 24);
3959 if ((root->reserved_zero || root->info_length < 8) &&
3960 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
3963 pctx->num = root->hash_version;
3964 if ((root->hash_version != EXT2_HASH_LEGACY) &&
3965 (root->hash_version != EXT2_HASH_HALF_MD4) &&
3966 (root->hash_version != EXT2_HASH_TEA) &&
3967 (root->hash_version != EXT2_HASH_SIPHASH) &&
3968 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
3971 if (ext4_hash_in_dirent(inode)) {
3972 if (root->hash_version != EXT2_HASH_SIPHASH &&
3973 fix_problem(ctx, PR_1_HTREE_NEEDS_SIPHASH, pctx))
3976 if (root->hash_version == EXT2_HASH_SIPHASH &&
3977 fix_problem(ctx, PR_1_HTREE_CANNOT_SIPHASH, pctx))
3981 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
3982 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
3985 pctx->num = root->indirect_levels;
3986 /* if htree level is clearly too high, consider it to be broken */
3987 if (root->indirect_levels > EXT4_HTREE_LEVEL &&
3988 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
3991 /* if level is only maybe too high, LARGE_DIR feature could be unset */
3992 if (root->indirect_levels > ext2_dir_htree_level(fs) &&
3993 !ext2fs_has_feature_largedir(fs->super)) {
3994 int blockbits = EXT2_BLOCK_SIZE_BITS(fs->super) + 10;
3995 unsigned idx_pb = 1 << (blockbits - 3);
3997 /* compare inode size/blocks vs. max-sized 2-level htree */
3998 if (EXT2_I_SIZE(pctx->inode) <
3999 (idx_pb - 1) * (idx_pb - 2) << blockbits &&
4000 pctx->inode->i_blocks <
4001 (idx_pb - 1) * (idx_pb - 2) << (blockbits - 9) &&
4002 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4006 if (root->indirect_levels > EXT4_HTREE_LEVEL_COMPAT ||
4007 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
4013 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
4014 struct ext2_inode *inode, int restart_flag,
4018 inode->i_links_count = 0;
4019 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
4020 inode->i_dtime = ctx->now;
4023 * If a special inode has such rotten block mappings that we
4024 * want to clear the whole inode, be sure to actually zap
4025 * the block maps because i_links_count isn't checked for
4026 * special inodes, and we'll end up right back here the next
4029 if (ino < EXT2_FIRST_INODE(ctx->fs->super))
4030 memset(inode->i_block, 0, sizeof(inode->i_block));
4032 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
4033 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
4034 if (ctx->inode_reg_map)
4035 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
4036 if (ctx->inode_bad_map)
4037 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
4040 * If the inode was partially accounted for before processing
4041 * was aborted, we need to restart the pass 1 scan.
4043 ctx->flags |= restart_flag;
4045 if (ino == EXT2_BAD_INO)
4046 memset(inode, 0, sizeof(struct ext2_inode));
4048 e2fsck_write_inode(ctx, ino, inode, source);
4052 * Use the multiple-blocks reclamation code to fix alignment problems in
4053 * a bigalloc filesystem. We want a logical cluster to map to *only* one
4054 * physical cluster, and we want the block offsets within that cluster to
4057 static int has_unaligned_cluster_map(e2fsck_t ctx,
4058 blk64_t last_pblk, blk64_t last_lblk,
4059 blk64_t pblk, blk64_t lblk)
4061 blk64_t cluster_mask;
4063 if (!ctx->fs->cluster_ratio_bits)
4065 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
4068 * If the block in the logical cluster doesn't align with the block in
4069 * the physical cluster...
4071 if ((lblk & cluster_mask) != (pblk & cluster_mask))
4075 * If we cross a physical cluster boundary within a logical cluster...
4077 if (last_pblk && (lblk & cluster_mask) != 0 &&
4078 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
4079 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
4085 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
4086 struct process_block_struct *pb,
4087 blk64_t start_block, blk64_t end_block,
4089 ext2_extent_handle_t ehandle,
4092 struct ext2fs_extent extent;
4093 blk64_t blk, last_lblk;
4095 int is_dir, is_leaf;
4097 struct ext2_extent_info info;
4098 int failed_csum = 0;
4100 if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
4103 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
4106 if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
4107 !pb->eti.force_rebuild &&
4108 info.curr_level < MAX_EXTENT_DEPTH_COUNT) {
4109 struct extent_tree_level *etl;
4111 etl = pb->eti.ext_info + info.curr_level;
4112 etl->num_extents += info.num_entries;
4113 etl->max_extents += info.max_entries;
4115 * Implementation wart: Splitting extent blocks when appending
4116 * will leave the old block with one free entry. Therefore
4117 * unless the node is totally full, pretend that a non-root
4118 * extent block can hold one fewer entry than it actually does,
4119 * so that we don't repeatedly rebuild the extent tree.
4121 if (info.curr_level && info.num_entries < info.max_entries)
4125 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
4127 while ((pctx->errcode == 0 ||
4128 pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
4129 info.num_entries-- > 0) {
4130 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
4131 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
4132 last_lblk = extent.e_lblk + extent.e_len - 1;
4135 pctx->blk = extent.e_pblk;
4136 pctx->blk2 = extent.e_lblk;
4137 pctx->num = extent.e_len;
4138 pctx->blkcount = extent.e_lblk + extent.e_len;
4140 if (extent.e_pblk == 0 ||
4141 extent.e_pblk < ctx->fs->super->s_first_data_block ||
4142 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
4143 problem = PR_1_EXTENT_BAD_START_BLK;
4144 else if (extent.e_lblk < start_block)
4145 problem = PR_1_OUT_OF_ORDER_EXTENTS;
4146 else if ((end_block && last_lblk > end_block) &&
4147 !(last_lblk > eof_block &&
4148 ((extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) ||
4149 (pctx->inode->i_flags & EXT4_VERITY_FL))))
4150 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
4151 else if (is_leaf && extent.e_len == 0)
4152 problem = PR_1_EXTENT_LENGTH_ZERO;
4154 (extent.e_pblk + extent.e_len) >
4155 ext2fs_blocks_count(ctx->fs->super))
4156 problem = PR_1_EXTENT_ENDS_BEYOND;
4157 else if (is_leaf && is_dir && !pctx->inode->i_size_high &&
4158 !ext2fs_has_feature_largedir(ctx->fs->super) &&
4159 ((extent.e_lblk + extent.e_len) >
4160 (1U << (21 - ctx->fs->super->s_log_block_size))))
4161 problem = PR_1_TOOBIG_DIR;
4163 if (is_leaf && problem == 0 && extent.e_len > 0) {
4165 printf("extent_region(ino=%u, expect=%llu, "
4166 "lblk=%llu, len=%u)\n", pb->ino,
4167 (unsigned long long) pb->next_lblock,
4168 (unsigned long long) extent.e_lblk,
4171 if (extent.e_lblk < pb->next_lblock)
4172 problem = PR_1_EXTENT_COLLISION;
4173 else if (extent.e_lblk + extent.e_len > pb->next_lblock)
4174 pb->next_lblock = extent.e_lblk + extent.e_len;
4178 * Uninitialized blocks in a directory? Clear the flag and
4179 * we'll interpret the blocks later.
4181 if (try_repairs && is_dir && problem == 0 &&
4182 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4183 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
4184 e2fsck_pass1_fix_lock(ctx);
4185 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4186 pb->inode_modified = 1;
4187 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4189 e2fsck_pass1_fix_unlock(ctx);
4194 #ifdef CONFIG_DEVELOPER_FEATURES
4195 if (try_repairs && !is_dir && problem == 0 &&
4196 (ctx->options & E2F_OPT_CLEAR_UNINIT) &&
4197 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4198 fix_problem(ctx, PR_1_CLEAR_UNINIT_EXTENT, pctx)) {
4199 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4200 pb->inode_modified = 1;
4201 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4208 if (try_repairs && problem) {
4210 if (fix_problem(ctx, problem, pctx)) {
4211 if (ctx->invalid_bitmaps) {
4213 * If fsck knows the bitmaps are bad,
4214 * skip to the next extent and
4215 * try to clear this extent again
4216 * after fixing the bitmaps, by
4219 pctx->errcode = ext2fs_extent_get(
4221 EXT2_EXTENT_NEXT_SIB,
4223 ctx->flags |= E2F_FLAG_RESTART_LATER;
4224 if (pctx->errcode ==
4225 EXT2_ET_NO_CURRENT_NODE) {
4231 e2fsck_pass1_fix_lock(ctx);
4232 e2fsck_read_bitmaps(ctx);
4233 pb->inode_modified = 1;
4235 ext2fs_extent_delete(ehandle, 0);
4236 e2fsck_pass1_fix_unlock(ctx);
4237 if (pctx->errcode) {
4238 pctx->str = "ext2fs_extent_delete";
4241 e2fsck_pass1_fix_lock(ctx);
4242 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4243 e2fsck_pass1_fix_unlock(ctx);
4244 if (pctx->errcode &&
4245 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
4246 pctx->str = "ext2fs_extent_fix_parents";
4249 pctx->errcode = ext2fs_extent_get(ehandle,
4250 EXT2_EXTENT_CURRENT,
4252 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
4263 blk64_t lblk = extent.e_lblk;
4264 int next_try_repairs = 1;
4266 blk = extent.e_pblk;
4269 * If this lower extent block collides with critical
4270 * metadata, don't try to repair the damage. Pass 1b
4271 * will reallocate the block; then we can try again.
4273 if (pb->ino != EXT2_RESIZE_INO &&
4274 extent.e_pblk < ctx->fs->super->s_blocks_count &&
4275 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
4277 next_try_repairs = 0;
4280 PR_1_CRITICAL_METADATA_COLLISION,
4282 if ((ctx->options & E2F_OPT_NO) == 0)
4283 ctx->flags |= E2F_FLAG_RESTART_LATER;
4285 pctx->errcode = ext2fs_extent_get(ehandle,
4286 EXT2_EXTENT_DOWN, &extent);
4287 if (pctx->errcode &&
4288 pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
4289 pctx->str = "EXT2_EXTENT_DOWN";
4290 problem = PR_1_EXTENT_HEADER_INVALID;
4291 if (!next_try_repairs)
4293 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
4294 goto report_problem;
4297 /* The next extent should match this index's logical start */
4298 if (extent.e_lblk != lblk) {
4299 struct ext2_extent_info e_info;
4301 pctx->errcode = ext2fs_extent_get_info(ehandle,
4303 if (pctx->errcode) {
4304 pctx->str = "ext2fs_extent_get_info";
4308 pctx->blk2 = extent.e_lblk;
4309 pctx->num = e_info.curr_level - 1;
4310 problem = PR_1_EXTENT_INDEX_START_INVALID;
4311 if (fix_problem(ctx, problem, pctx)) {
4312 e2fsck_pass1_fix_lock(ctx);
4313 pb->inode_modified = 1;
4315 ext2fs_extent_fix_parents(ehandle);
4316 e2fsck_pass1_fix_unlock(ctx);
4317 if (pctx->errcode) {
4318 pctx->str = "ext2fs_extent_fix_parents";
4323 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
4324 last_lblk, eof_block, ehandle,
4328 pctx->errcode = ext2fs_extent_get(ehandle,
4329 EXT2_EXTENT_UP, &extent);
4330 if (pctx->errcode) {
4331 pctx->str = "EXT2_EXTENT_UP";
4334 mark_block_used(ctx, blk);
4339 if ((pb->previous_block != 0) &&
4340 (pb->previous_block+1 != extent.e_pblk)) {
4341 if (ctx->options & E2F_OPT_FRAGCHECK) {
4346 else if (pb->is_reg)
4349 printf(("%6lu(%c): expecting %6lu "
4351 "phys %6lu log %lu len %lu\n"),
4352 (unsigned long) pctx->ino, type,
4353 (unsigned long) pb->previous_block+1,
4354 (unsigned long) extent.e_pblk,
4355 (unsigned long) extent.e_lblk,
4356 (unsigned long) extent.e_len);
4361 * If we notice a gap in the logical block mappings of an
4362 * extent-mapped directory, offer to close the hole by
4363 * moving the logical block down, otherwise we'll go mad in
4364 * pass 3 allocating empty directory blocks to fill the hole.
4366 if (try_repairs && is_dir &&
4367 pb->last_block + 1 < extent.e_lblk) {
4370 new_lblk = pb->last_block + 1;
4371 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
4372 new_lblk = ((new_lblk +
4373 EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
4374 ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
4376 EXT2FS_CLUSTER_MASK(ctx->fs));
4377 pctx->blk = extent.e_lblk;
4378 pctx->blk2 = new_lblk;
4379 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
4380 e2fsck_pass1_fix_lock(ctx);
4381 extent.e_lblk = new_lblk;
4382 pb->inode_modified = 1;
4383 pctx->errcode = ext2fs_extent_replace(ehandle,
4385 e2fsck_pass1_fix_unlock(ctx);
4386 if (pctx->errcode) {
4390 e2fsck_pass1_fix_lock(ctx);
4391 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4392 e2fsck_pass1_fix_unlock(ctx);
4394 goto failed_add_dir_block;
4395 pctx->errcode = ext2fs_extent_goto(ehandle,
4398 goto failed_add_dir_block;
4399 last_lblk = extent.e_lblk + extent.e_len - 1;
4405 while (++pb->last_db_block <
4406 (e2_blkcnt_t) extent.e_lblk) {
4407 pctx->errcode = ext2fs_add_dir_block2(
4411 if (pctx->errcode) {
4413 pctx->num = pb->last_db_block;
4414 goto failed_add_dir_block;
4418 for (i = 0; i < extent.e_len; i++) {
4419 pctx->errcode = ext2fs_add_dir_block2(
4424 if (pctx->errcode) {
4425 pctx->blk = extent.e_pblk + i;
4426 pctx->num = extent.e_lblk + i;
4427 failed_add_dir_block:
4428 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
4429 /* Should never get here */
4430 ctx->flags |= E2F_FLAG_ABORT;
4434 if (extent.e_len > 0)
4435 pb->last_db_block = extent.e_lblk + extent.e_len - 1;
4437 if (has_unaligned_cluster_map(ctx, pb->previous_block,
4441 for (i = 0; i < extent.e_len; i++) {
4442 pctx->blk = extent.e_lblk + i;
4443 pctx->blk2 = extent.e_pblk + i;
4444 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
4445 mark_block_used(ctx, extent.e_pblk + i);
4446 mark_block_used(ctx, extent.e_pblk + i);
4451 * Check whether first cluster got marked in previous iteration.
4453 if (ctx->fs->cluster_ratio_bits &&
4454 pb->previous_block &&
4455 (EXT2FS_B2C(ctx->fs, extent.e_pblk) ==
4456 EXT2FS_B2C(ctx->fs, pb->previous_block)))
4457 /* Set blk to the beginning of next cluster. */
4460 EXT2FS_B2C(ctx->fs, extent.e_pblk) + 1);
4462 /* Set blk to the beginning of current cluster. */
4463 blk = EXT2FS_C2B(ctx->fs,
4464 EXT2FS_B2C(ctx->fs, extent.e_pblk));
4466 if (blk < extent.e_pblk + extent.e_len) {
4467 mark_blocks_used(ctx, blk,
4468 extent.e_pblk + extent.e_len - blk);
4469 n = DIV_ROUND_UP(extent.e_pblk + extent.e_len - blk,
4470 EXT2FS_CLUSTER_RATIO(ctx->fs));
4471 pb->num_blocks += n;
4473 pb->last_block = extent.e_lblk + extent.e_len - 1;
4474 pb->previous_block = extent.e_pblk + extent.e_len - 1;
4475 start_block = pb->last_block = last_lblk;
4476 if (is_leaf && !is_dir &&
4477 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
4478 pb->last_init_lblock = last_lblk;
4480 pctx->errcode = ext2fs_extent_get(ehandle,
4481 EXT2_EXTENT_NEXT_SIB,
4485 /* Failed csum but passes checks? Ask to fix checksum. */
4487 fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
4488 e2fsck_pass1_fix_lock(ctx);
4489 pb->inode_modified = 1;
4490 pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
4491 e2fsck_pass1_fix_unlock(ctx);
4496 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
4500 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
4501 struct process_block_struct *pb)
4503 struct ext2_extent_info info;
4504 struct ext2_inode *inode = pctx->inode;
4505 ext2_extent_handle_t ehandle;
4506 ext2_filsys fs = ctx->fs;
4507 ext2_ino_t ino = pctx->ino;
4510 struct ext3_extent_header *eh;
4512 /* Check for a proper extent header... */
4513 eh = (struct ext3_extent_header *) &inode->i_block[0];
4514 retval = ext2fs_extent_header_verify(eh, sizeof(inode->i_block));
4516 if (fix_problem(ctx, PR_1_MISSING_EXTENT_HEADER, pctx))
4517 e2fsck_clear_inode(ctx, ino, inode, 0,
4518 "check_blocks_extents");
4523 /* ...since this function doesn't fail if i_block is zeroed. */
4524 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
4525 if (pctx->errcode) {
4526 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
4527 e2fsck_clear_inode(ctx, ino, inode, 0,
4528 "check_blocks_extents");
4533 retval = ext2fs_extent_get_info(ehandle, &info);
4535 int max_depth = info.max_depth;
4537 if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
4538 max_depth = MAX_EXTENT_DEPTH_COUNT-1;
4539 ctx->extent_depth_count[max_depth]++;
4542 /* Check maximum extent depth */
4543 pctx->blk = info.max_depth;
4544 pctx->blk2 = ext2fs_max_extent_depth(ehandle);
4545 if (pctx->blk2 < pctx->blk &&
4546 fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
4547 pb->eti.force_rebuild = 1;
4549 /* Can we collect extent tree level stats? */
4550 pctx->blk = MAX_EXTENT_DEPTH_COUNT;
4551 if (pctx->blk2 > pctx->blk)
4552 fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
4553 memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
4554 pb->eti.ino = pb->ino;
4556 pb->next_lblock = 0;
4558 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
4559 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
4560 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
4561 if (pctx->errcode &&
4562 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
4564 inode->i_blocks = 0;
4565 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
4566 "check_blocks_extents");
4569 ext2fs_extent_free(ehandle);
4571 /* Rebuild unless it's a dir and we're rehashing it */
4572 if (LINUX_S_ISDIR(inode->i_mode) &&
4573 e2fsck_dir_will_be_rehashed(ctx, ino))
4576 if (ctx->options & E2F_OPT_CONVERT_BMAP)
4577 e2fsck_rebuild_extents_later(ctx, ino);
4579 e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
4583 * In fact we don't need to check blocks for an inode with inline data
4584 * because this inode doesn't have any blocks. In this function all
4585 * we need to do is add this inode into dblist when it is a directory.
4587 static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
4588 struct process_block_struct *pb)
4591 size_t inline_data_size = 0;
4598 /* Process the dirents in i_block[] as the "first" block. */
4599 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
4603 /* Process the dirents in the EA as a "second" block. */
4604 flags = ctx->fs->flags;
4605 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
4606 pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
4608 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
4609 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
4610 if (pctx->errcode) {
4615 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
4618 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
4626 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
4627 ctx->flags |= E2F_FLAG_ABORT;
4631 * This subroutine is called on each inode to account for all of the
4632 * blocks used by that inode.
4634 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
4635 char *block_buf, const struct ea_quota *ea_ibody_quota)
4637 ext2_filsys fs = ctx->fs;
4638 struct process_block_struct pb;
4639 ext2_ino_t ino = pctx->ino;
4640 struct ext2_inode *inode = pctx->inode;
4641 unsigned bad_size = 0;
4642 int dirty_inode = 0;
4646 struct ea_quota ea_block_quota;
4649 pb.num_blocks = EXT2FS_B2C(ctx->fs,
4650 ea_ibody_quota ? ea_ibody_quota->blocks : 0);
4652 pb.last_init_lblock = -1;
4653 pb.last_db_block = -1;
4654 pb.num_illegal_blocks = 0;
4655 pb.suppress = 0; pb.clear = 0;
4658 pb.previous_block = 0;
4659 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
4660 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
4661 pb.max_blocks = 1U << (31 - fs->super->s_log_block_size);
4665 pb.inode_modified = 0;
4666 pb.eti.force_rebuild = 0;
4670 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
4671 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
4673 if (check_ext_attr(ctx, pctx, block_buf, &ea_block_quota)) {
4674 if (e2fsck_should_abort(ctx))
4676 pb.num_blocks += EXT2FS_B2C(ctx->fs, ea_block_quota.blocks);
4679 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
4680 check_blocks_inline_data(ctx, pctx, &pb);
4681 else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
4682 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
4683 check_blocks_extents(ctx, pctx, &pb);
4687 * If we've modified the inode, write it out before
4688 * iterate() tries to use it.
4691 e2fsck_write_inode(ctx, ino, inode,
4696 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
4697 pctx->errcode = ext2fs_block_iterate3(fs, ino,
4698 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
4699 block_buf, process_block, &pb);
4701 * We do not have uninitialized extents in non extent
4704 pb.last_init_lblock = pb.last_block;
4706 * If iterate() changed a block mapping, we have to
4707 * re-read the inode. If we decide to clear the
4708 * inode after clearing some stuff, we'll re-write the
4709 * bad mappings into the inode!
4711 if (pb.inode_modified)
4712 e2fsck_read_inode(ctx, ino, inode,
4714 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
4715 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
4717 if (ctx->options & E2F_OPT_CONVERT_BMAP) {
4719 printf("bmap rebuild ino=%d\n", ino);
4721 if (!LINUX_S_ISDIR(inode->i_mode) ||
4722 !e2fsck_dir_will_be_rehashed(ctx, ino))
4723 e2fsck_rebuild_extents_later(ctx, ino);
4727 end_problem_latch(ctx, PR_LATCH_BLOCK);
4728 end_problem_latch(ctx, PR_LATCH_TOOBIG);
4729 if (e2fsck_should_abort(ctx))
4732 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
4734 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
4735 if (LINUX_S_ISDIR(inode->i_mode))
4736 ctx->fs_fragmented_dir++;
4738 ctx->fs_fragmented++;
4742 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
4747 if (inode->i_flags & EXT2_INDEX_FL) {
4748 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
4749 inode->i_flags &= ~EXT2_INDEX_FL;
4752 e2fsck_add_dx_dir(ctx, ino, inode, pb.last_block+1);
4756 if (!pb.num_blocks && pb.is_dir &&
4757 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
4758 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
4759 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
4760 ctx->fs_directory_count--;
4765 if (ino != quota_type2inum(PRJQUOTA, fs->super) &&
4766 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) &&
4767 !(inode->i_flags & EXT4_EA_INODE_FL)) {
4768 quota_data_add(ctx->qctx, (struct ext2_inode_large *) inode,
4770 pb.num_blocks * EXT2_CLUSTER_SIZE(fs->super));
4771 quota_data_inodes(ctx->qctx, (struct ext2_inode_large *) inode,
4772 ino, (ea_ibody_quota ?
4773 ea_ibody_quota->inodes : 0) +
4774 ea_block_quota.inodes + 1);
4777 if (!ext2fs_has_feature_huge_file(fs->super) ||
4778 !(inode->i_flags & EXT4_HUGE_FILE_FL))
4779 pb.num_blocks *= (fs->blocksize / 512);
4780 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
4782 printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
4783 ino, inode->i_size, (unsigned long long) pb.last_block,
4784 (unsigned long long) ext2fs_inode_i_blocks(fs, inode),
4785 (unsigned long long) pb.num_blocks);
4787 size = EXT2_I_SIZE(inode);
4789 unsigned nblock = size >> EXT2_BLOCK_SIZE_BITS(fs->super);
4790 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
4795 flags = ctx->fs->flags;
4796 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
4797 err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
4799 ctx->fs->flags = (flags &
4800 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
4802 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
4803 if (err || sz != size) {
4807 } else if (size & (fs->blocksize - 1))
4809 else if (nblock > (pb.last_block + 1))
4811 else if (nblock < (pb.last_block + 1)) {
4812 if (((pb.last_block + 1) - nblock) >
4813 fs->super->s_prealloc_dir_blocks)
4817 if ((pb.last_init_lblock >= 0) &&
4818 /* Do not allow initialized allocated blocks past i_size*/
4819 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
4820 !(inode->i_flags & EXT4_VERITY_FL))
4822 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
4823 size > ext2_max_sizes[fs->super->s_log_block_size])
4824 /* too big for a direct/indirect-mapped file */
4826 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
4828 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
4829 /* too big for an extent-based file - 32bit ee_block */
4832 /* i_size for symlinks is checked elsewhere */
4833 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
4834 /* Did inline_data set pctx->num earlier? */
4836 pctx->num = (pb.last_block + 1) * fs->blocksize;
4837 pctx->group = bad_size;
4838 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
4839 ext2fs_inode_size_set(fs, inode, pctx->num);
4840 if (EXT2_I_SIZE(inode) == 0 &&
4841 (inode->i_flags & EXT4_INLINE_DATA_FL)) {
4842 memset(inode->i_block, 0,
4843 sizeof(inode->i_block));
4844 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
4850 if (LINUX_S_ISREG(inode->i_mode) &&
4851 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
4853 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
4854 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
4855 (ext2fs_has_feature_huge_file(fs->super) &&
4856 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
4857 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
4858 pctx->num = pb.num_blocks;
4859 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
4860 inode->i_blocks = pb.num_blocks;
4861 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
4868 * The kernel gets mad if we ask it to allocate bigalloc clusters to
4869 * a block mapped file, so rebuild it as an extent file. We can skip
4870 * symlinks because they're never rewritten.
4872 if (ext2fs_has_feature_bigalloc(fs->super) &&
4873 (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
4874 ext2fs_inode_data_blocks2(fs, inode) > 0 &&
4875 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
4876 !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
4877 fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
4878 pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
4883 if (ctx->dirs_to_hash && pb.is_dir &&
4884 !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
4885 !(inode->i_flags & EXT2_INDEX_FL) &&
4886 ((inode->i_size / fs->blocksize) >= 3))
4887 e2fsck_rehash_dir_later(ctx, ino);
4891 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
4896 * Helper function called by process block when an illegal block is
4897 * found. It returns a description about why the block is illegal
4899 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
4903 static char problem[80];
4905 super = fs->super->s_first_data_block;
4906 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
4907 if (block < super) {
4908 sprintf(problem, "< FIRSTBLOCK (%u)", super);
4910 } else if (block >= ext2fs_blocks_count(fs->super)) {
4911 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
4914 for (i = 0; i < fs->group_desc_count; i++) {
4915 if (block == super) {
4916 sprintf(problem, "is the superblock in group %d", i);
4919 if (block > super &&
4920 block <= (super + fs->desc_blocks)) {
4921 sprintf(problem, "is in the group descriptors "
4925 if (block == ext2fs_block_bitmap_loc(fs, i)) {
4926 sprintf(problem, "is the block bitmap of group %d", i);
4929 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
4930 sprintf(problem, "is the inode bitmap of group %d", i);
4933 if (block >= ext2fs_inode_table_loc(fs, i) &&
4934 (block < ext2fs_inode_table_loc(fs, i)
4935 + fs->inode_blocks_per_group)) {
4936 sprintf(problem, "is in the inode table of group %d",
4940 super += fs->super->s_blocks_per_group;
4947 * This is a helper function for check_blocks().
4949 static int process_block(ext2_filsys fs,
4951 e2_blkcnt_t blockcnt,
4952 blk64_t ref_block EXT2FS_ATTR((unused)),
4953 int ref_offset EXT2FS_ATTR((unused)),
4956 struct process_block_struct *p;
4957 struct problem_context *pctx;
4958 blk64_t blk = *block_nr;
4960 problem_t problem = 0;
4963 p = (struct process_block_struct *) priv_data;
4968 * For a directory, add logical block zero for processing even if it's
4969 * not mapped or we'll be perennially stuck with broken "." and ".."
4972 if (p->is_dir && blockcnt == 0 && blk == 0) {
4973 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
4974 if (pctx->errcode) {
4976 pctx->num = blockcnt;
4977 goto failed_add_dir_block;
4986 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
4991 * Simplistic fragmentation check. We merely require that the
4992 * file be contiguous. (Which can never be true for really
4993 * big files that are greater than a block group.)
4995 if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
4996 if (p->previous_block+1 != blk) {
4997 if (ctx->options & E2F_OPT_FRAGCHECK) {
5005 printf(_("%6lu(%c): expecting %6lu "
5006 "got phys %6lu (blkcnt %lld)\n"),
5007 (unsigned long) pctx->ino, type,
5008 (unsigned long) p->previous_block+1,
5009 (unsigned long) blk,
5010 (long long) blockcnt);
5016 if (p->is_dir && !ext2fs_has_feature_largedir(fs->super) &&
5017 !pctx->inode->i_size_high &&
5018 blockcnt > (1 << (21 - fs->super->s_log_block_size)))
5019 problem = PR_1_TOOBIG_DIR;
5020 if (p->is_dir && p->num_blocks + 1 >= p->max_blocks)
5021 problem = PR_1_TOOBIG_DIR;
5022 if (p->is_reg && p->num_blocks + 1 >= p->max_blocks)
5023 problem = PR_1_TOOBIG_REG;
5024 if (!p->is_dir && !p->is_reg && blockcnt > 0)
5025 problem = PR_1_TOOBIG_SYMLINK;
5027 if (blk < fs->super->s_first_data_block ||
5028 blk >= ext2fs_blocks_count(fs->super))
5029 problem = PR_1_ILLEGAL_BLOCK_NUM;
5032 * If this IND/DIND/TIND block is squatting atop some critical metadata
5033 * (group descriptors, superblock, bitmap, inode table), any write to
5034 * "fix" mapping problems will destroy the metadata. We'll let pass 1b
5035 * fix that and restart fsck.
5038 p->ino != EXT2_RESIZE_INO &&
5039 blk < ctx->fs->super->s_blocks_count &&
5040 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
5042 fix_problem(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx);
5043 if ((ctx->options & E2F_OPT_NO) == 0)
5044 ctx->flags |= E2F_FLAG_RESTART_LATER;
5048 p->num_illegal_blocks++;
5050 * A bit of subterfuge here -- we're trying to fix a block
5051 * mapping, but the IND/DIND/TIND block could have collided
5052 * with some critical metadata. So, fix the in-core mapping so
5053 * iterate won't go insane, but return 0 instead of
5054 * BLOCK_CHANGED so that it won't write the remapping out to
5055 * our multiply linked block.
5057 * Even if we previously determined that an *IND block
5058 * conflicts with critical metadata, we must still try to
5059 * iterate the *IND block as if it is an *IND block to find and
5060 * mark the blocks it points to. Better to be overly cautious
5061 * with the used_blocks map so that we don't move the *IND
5062 * block to a block that's really in use!
5064 if (p->ino != EXT2_RESIZE_INO &&
5066 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
5071 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
5072 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
5076 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
5078 set_latch_flags(PR_LATCH_BLOCK,
5083 pctx->blkcount = blockcnt;
5084 if (fix_problem(ctx, problem, pctx)) {
5085 blk = *block_nr = 0;
5086 ret_code = BLOCK_CHANGED;
5087 p->inode_modified = 1;
5089 * If the directory block is too big and is beyond the
5090 * end of the FS, don't bother trying to add it for
5091 * processing -- the kernel would never have created a
5092 * directory this large, and we risk an ENOMEM abort.
5093 * In any case, the toobig handler for extent-based
5094 * directories also doesn't feed toobig blocks to
5097 if (problem == PR_1_TOOBIG_DIR)
5104 if (p->ino == EXT2_RESIZE_INO) {
5106 * The resize inode has already be sanity checked
5107 * during pass #0 (the superblock checks). All we
5108 * have to do is mark the double indirect block as
5109 * being in use; all of the other blocks are handled
5110 * by mark_table_blocks()).
5112 if (blockcnt == BLOCK_COUNT_DIND)
5113 mark_block_used(ctx, blk);
5115 } else if (!(ctx->fs->cluster_ratio_bits &&
5116 p->previous_block &&
5117 (EXT2FS_B2C(ctx->fs, blk) ==
5118 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
5119 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
5120 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
5121 mark_block_used(ctx, blk);
5123 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
5124 p->last_block, blk, blockcnt)) {
5125 pctx->blk = blockcnt;
5127 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
5128 mark_block_used(ctx, blk);
5129 mark_block_used(ctx, blk);
5132 p->last_block = blockcnt;
5133 p->previous_block = blk;
5135 if (p->is_dir && (blockcnt >= 0)) {
5136 while (++p->last_db_block < blockcnt) {
5137 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
5140 if (pctx->errcode) {
5142 pctx->num = p->last_db_block;
5143 goto failed_add_dir_block;
5146 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
5148 if (pctx->errcode) {
5150 pctx->num = blockcnt;
5151 failed_add_dir_block:
5152 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
5153 /* Should never get here */
5154 ctx->flags |= E2F_FLAG_ABORT;
5161 static int process_bad_block(ext2_filsys fs,
5163 e2_blkcnt_t blockcnt,
5164 blk64_t ref_block EXT2FS_ATTR((unused)),
5165 int ref_offset EXT2FS_ATTR((unused)),
5168 struct process_block_struct *p;
5169 blk64_t blk = *block_nr;
5170 blk64_t first_block;
5172 struct problem_context *pctx;
5178 p = (struct process_block_struct *) priv_data;
5182 pctx->ino = EXT2_BAD_INO;
5184 pctx->blkcount = blockcnt;
5186 if ((blk < fs->super->s_first_data_block) ||
5187 (blk >= ext2fs_blocks_count(fs->super))) {
5188 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
5190 return BLOCK_CHANGED;
5196 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
5198 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
5200 return BLOCK_CHANGED;
5202 } else if (is_blocks_used(ctx, blk, 1)) {
5204 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
5207 return BLOCK_CHANGED;
5209 if (e2fsck_should_abort(ctx))
5212 mark_block_used(ctx, blk);
5217 printf ("DEBUG: Marking %u as bad.\n", blk);
5219 ctx->fs_badblocks_count++;
5221 * If the block is not used, then mark it as used and return.
5222 * If it is already marked as found, this must mean that
5223 * there's an overlap between the filesystem table blocks
5224 * (bitmaps and inode table) and the bad block list.
5226 if (!is_blocks_used(ctx, blk, 1)) {
5227 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
5231 * Try to find the where the filesystem block was used...
5233 first_block = fs->super->s_first_data_block;
5235 for (i = 0; i < fs->group_desc_count; i++ ) {
5238 if (!ext2fs_bg_has_super(fs, i))
5240 if (blk == first_block) {
5242 if (fix_problem(ctx,
5243 PR_1_BAD_PRIMARY_SUPERBLOCK,
5246 return BLOCK_CHANGED;
5250 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
5253 if ((blk > first_block) &&
5254 (blk <= first_block + fs->desc_blocks)) {
5256 pctx->blk = *block_nr;
5257 if (fix_problem(ctx,
5258 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
5260 return BLOCK_CHANGED;
5264 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
5268 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
5269 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
5270 ctx->invalid_block_bitmap_flag[i]++;
5271 ctx->invalid_bitmaps++;
5275 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
5276 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
5277 ctx->invalid_inode_bitmap_flag[i]++;
5278 ctx->invalid_bitmaps++;
5282 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
5283 (blk < (ext2fs_inode_table_loc(fs, i) +
5284 fs->inode_blocks_per_group))) {
5286 * If there are bad blocks in the inode table,
5287 * the inode scan code will try to do
5288 * something reasonable automatically.
5292 first_block += fs->super->s_blocks_per_group;
5295 * If we've gotten to this point, then the only
5296 * possibility is that the bad block inode meta data
5297 * is using a bad block.
5299 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
5300 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
5301 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
5303 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
5305 return BLOCK_CHANGED;
5307 if (e2fsck_should_abort(ctx))
5314 /* Warn user that the block wasn't claimed */
5315 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
5320 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
5321 const char *name, int num, blk64_t *new_block)
5323 ext2_filsys fs = ctx->fs;
5325 blk64_t old_block = *new_block;
5328 unsigned flexbg_size;
5331 struct problem_context pctx;
5333 clear_problem_context(&pctx);
5336 pctx.blk = old_block;
5340 * For flex_bg filesystems, first try to allocate the metadata
5341 * within the flex_bg, and if that fails then try finding the
5342 * space anywhere in the filesystem.
5344 is_flexbg = ext2fs_has_feature_flex_bg(fs->super);
5346 flexbg_size = 1U << fs->super->s_log_groups_per_flex;
5347 flexbg = group / flexbg_size;
5348 first_block = ext2fs_group_first_block2(fs,
5349 flexbg_size * flexbg);
5350 last_grp = group | (flexbg_size - 1);
5351 if (last_grp >= fs->group_desc_count)
5352 last_grp = fs->group_desc_count - 1;
5353 last_block = ext2fs_group_last_block2(fs, last_grp);
5355 last_block = ext2fs_group_last_block2(fs, group);
5356 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
5357 num, ctx->block_found_map,
5359 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
5360 pctx.errcode = ext2fs_get_free_blocks2(fs,
5361 fs->super->s_first_data_block,
5362 ext2fs_blocks_count(fs->super),
5363 num, ctx->block_found_map, new_block);
5366 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
5367 ext2fs_unmark_valid(fs);
5368 ctx->flags |= E2F_FLAG_ABORT;
5371 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
5373 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
5374 ext2fs_unmark_valid(fs);
5375 ctx->flags |= E2F_FLAG_ABORT;
5378 ext2fs_mark_super_dirty(fs);
5379 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
5380 pctx.blk2 = *new_block;
5381 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
5382 PR_1_RELOC_TO), &pctx);
5384 for (i = 0; i < num; i++) {
5386 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
5388 pctx.errcode = io_channel_read_blk64(fs->io,
5389 old_block + i, 1, buf);
5391 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
5392 pctx.blk = (*new_block) + i;
5393 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
5396 pctx.blk = (*new_block) + i;
5397 pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
5402 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
5404 ext2fs_free_mem(&buf);
5408 * This routine gets called at the end of pass 1 if bad blocks are
5409 * detected in the superblock, group descriptors, inode_bitmaps, or
5410 * block bitmaps. At this point, all of the blocks have been mapped
5411 * out, so we can try to allocate new block(s) to replace the bad
5414 static void handle_fs_bad_blocks(e2fsck_t ctx)
5416 ext2_filsys fs = ctx->fs;
5418 blk64_t first_block;
5421 for (i = 0; i < fs->group_desc_count; i++) {
5422 first_block = ext2fs_group_first_block2(fs, i);
5424 if (ctx->invalid_block_bitmap_flag[i]) {
5425 new_blk = ext2fs_block_bitmap_loc(fs, i);
5426 new_table_block(ctx, first_block, i, _("block bitmap"),
5428 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
5430 if (ctx->invalid_inode_bitmap_flag[i]) {
5431 new_blk = ext2fs_inode_bitmap_loc(fs, i);
5432 new_table_block(ctx, first_block, i, _("inode bitmap"),
5434 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
5436 if (ctx->invalid_inode_table_flag[i]) {
5437 new_blk = ext2fs_inode_table_loc(fs, i);
5438 new_table_block(ctx, first_block, i, _("inode table"),
5439 fs->inode_blocks_per_group,
5441 ext2fs_inode_table_loc_set(fs, i, new_blk);
5442 ctx->flags |= E2F_FLAG_RESTART;
5445 ctx->invalid_bitmaps = 0;
5449 * This routine marks all blocks which are used by the superblock,
5450 * group descriptors, inode bitmaps, and block bitmaps.
5452 static void mark_table_blocks(e2fsck_t ctx)
5454 ext2_filsys fs = ctx->fs;
5458 struct problem_context pctx;
5460 clear_problem_context(&pctx);
5462 for (i = 0; i < fs->group_desc_count; i++) {
5465 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
5466 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
5469 * Mark the blocks used for the inode table
5471 if (ext2fs_inode_table_loc(fs, i)) {
5472 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
5473 j < fs->inode_blocks_per_group;
5475 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5478 if (!ctx->invalid_inode_table_flag[i] &&
5480 PR_1_ITABLE_CONFLICT, &pctx)) {
5481 ctx->invalid_inode_table_flag[i]++;
5482 ctx->invalid_bitmaps++;
5485 ext2fs_mark_block_bitmap2(
5486 ctx->block_found_map, b);
5487 ext2fs_mark_block_bitmap2(
5488 ctx->block_metadata_map, b);
5494 * Mark block used for the block bitmap
5496 if (ext2fs_block_bitmap_loc(fs, i)) {
5497 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5498 ext2fs_block_bitmap_loc(fs, i))) {
5499 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
5500 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
5501 ctx->invalid_block_bitmap_flag[i]++;
5502 ctx->invalid_bitmaps++;
5505 ext2fs_mark_block_bitmap2(ctx->block_found_map,
5506 ext2fs_block_bitmap_loc(fs, i));
5507 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5508 ext2fs_block_bitmap_loc(fs, i));
5512 * Mark block used for the inode bitmap
5514 if (ext2fs_inode_bitmap_loc(fs, i)) {
5515 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
5516 ext2fs_inode_bitmap_loc(fs, i))) {
5517 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
5518 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
5519 ctx->invalid_inode_bitmap_flag[i]++;
5520 ctx->invalid_bitmaps++;
5523 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
5524 ext2fs_inode_bitmap_loc(fs, i));
5525 ext2fs_mark_block_bitmap2(ctx->block_found_map,
5526 ext2fs_inode_bitmap_loc(fs, i));
5533 * These subroutines short circuits ext2fs_get_blocks and
5534 * ext2fs_check_directory; we use them since we already have the inode
5535 * structure, so there's no point in letting the ext2fs library read
5538 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
5541 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5544 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5545 return EXT2_ET_CALLBACK_NOTHANDLED;
5547 for (i=0; i < EXT2_N_BLOCKS; i++)
5548 blocks[i] = ctx->stashed_inode->i_block[i];
5552 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
5553 struct ext2_inode *inode)
5555 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5557 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5558 return EXT2_ET_CALLBACK_NOTHANDLED;
5559 *inode = *ctx->stashed_inode;
5563 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
5564 struct ext2_inode *inode)
5566 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5568 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
5569 (inode != ctx->stashed_inode))
5570 *ctx->stashed_inode = *inode;
5571 return EXT2_ET_CALLBACK_NOTHANDLED;
5574 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
5576 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5578 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
5579 return EXT2_ET_CALLBACK_NOTHANDLED;
5581 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
5582 return EXT2_ET_NO_DIRECTORY;
5586 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
5589 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5593 if (ctx->block_found_map) {
5594 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
5598 if (fs->block_map) {
5599 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
5600 ext2fs_mark_bb_dirty(fs);
5603 if (!fs->block_map) {
5604 retval = ext2fs_read_block_bitmap(fs);
5609 retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
5618 static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
5619 blk64_t len, blk64_t *pblk, blk64_t *plen)
5621 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5624 if (ctx->block_found_map)
5625 return ext2fs_new_range(fs, flags, goal, len,
5626 ctx->block_found_map, pblk, plen);
5628 if (!fs->block_map) {
5629 retval = ext2fs_read_block_bitmap(fs);
5634 return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
5638 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
5640 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5642 /* Never free a critical metadata block */
5643 if (ctx->block_found_map &&
5644 ctx->block_metadata_map &&
5646 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
5649 if (ctx->block_found_map) {
5651 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
5653 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
5657 static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
5658 blk_t num, int inuse)
5660 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
5662 /* Never free a critical metadata block */
5663 if (ctx->block_found_map &&
5664 ctx->block_metadata_map &&
5666 ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
5669 if (ctx->block_found_map) {
5671 ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
5674 ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
5679 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
5681 ext2_filsys fs = ctx->fs;
5683 if (use_shortcuts) {
5684 fs->get_blocks = pass1_get_blocks;
5685 fs->check_directory = pass1_check_directory;
5686 fs->read_inode = pass1_read_inode;
5687 fs->write_inode = pass1_write_inode;
5688 ctx->stashed_ino = 0;
5691 fs->check_directory = 0;
5693 fs->write_inode = 0;
5697 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
5699 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
5700 ext2fs_set_block_alloc_stats_callback(ctx->fs,
5701 e2fsck_block_alloc_stats, 0);
5702 ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
5703 ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
5704 e2fsck_block_alloc_stats_range, NULL);