2 * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
11 * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12 * and applies the following tests to each inode:
14 * - The mode field of the inode must be legal.
15 * - The size and block count fields of the inode are correct.
16 * - A data block must not be used by another inode
18 * Pass 1 also gathers the collects the following information:
20 * - A bitmap of which inodes are in use. (inode_used_map)
21 * - A bitmap of which inodes are directories. (inode_dir_map)
22 * - A bitmap of which inodes are regular files. (inode_reg_map)
23 * - An icount mechanism is used to keep track of
24 * inodes with bad fields and its badness (ctx->inode_badness)
25 * - A bitmap of which inodes are in bad blocks. (inode_bb_map)
26 * - A bitmap of which inodes are imagic inodes. (inode_imagic_map)
27 * - A bitmap of which inodes are casefolded. (inode_casefold_map)
28 * - A bitmap of which inodes need to be expanded (expand_eisize_map)
29 * - A bitmap of which blocks are in use. (block_found_map)
30 * - A bitmap of which blocks are in use by two inodes (block_dup_map)
31 * - The data blocks of the directory inodes. (dir_map)
32 * - Ref counts for ea_inodes. (ea_inode_refs)
33 * - The encryption policy ID of each encrypted inode. (encrypted_files)
35 * Pass 1 is designed to stash away enough information so that the
36 * other passes should not need to read in the inode information
37 * during the normal course of a filesystem check. (Although if an
38 * inconsistency is detected, other passes may need to read in an
41 * Note that pass 1B will be invoked if there are any duplicate blocks
45 #define _GNU_SOURCE 1 /* get strnlen() */
58 #include <ext2fs/ext2_ext_attr.h>
59 /* todo remove this finally */
60 #include <ext2fs/ext2fsP.h>
65 #ifdef NO_INLINE_FUNCS
68 #define _INLINE_ inline
78 static int process_block(ext2_filsys fs, blk64_t *blocknr,
79 e2_blkcnt_t blockcnt, blk64_t ref_blk,
80 int ref_offset, void *priv_data);
81 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
82 e2_blkcnt_t blockcnt, blk64_t ref_blk,
83 int ref_offset, void *priv_data);
84 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
86 const struct ea_quota *ea_ibody_quota);
87 static void mark_table_blocks(e2fsck_t ctx);
88 static void alloc_bb_map(e2fsck_t ctx);
89 static void alloc_imagic_map(e2fsck_t ctx);
90 static void add_casefolded_dir(e2fsck_t ctx, ext2_ino_t ino);
91 static void handle_fs_bad_blocks(e2fsck_t ctx);
92 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
93 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
94 dgrp_t group, void * priv_data);
95 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
96 char *block_buf, int adjust_sign);
97 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
99 struct process_block_struct {
101 unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
102 fragmented:1, compressed:1, bbcheck:1,
107 e2_blkcnt_t last_init_lblock;
108 e2_blkcnt_t last_db_block;
109 int num_illegal_blocks;
110 blk64_t previous_block;
111 struct ext2_inode *inode;
112 struct problem_context *pctx;
113 ext2fs_block_bitmap fs_meta_blocks;
116 struct extent_tree_info eti;
119 struct process_inode_block {
121 struct ea_quota ea_ibody_quota;
122 struct ext2_inode_large inode;
125 struct scan_callback_struct {
128 struct process_inode_block *inodes_to_process;
129 int *process_inode_count;
132 static void process_inodes(e2fsck_t ctx, char *block_buf,
133 struct process_inode_block *inodes_to_process,
134 int *process_inode_count);
136 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
137 EXT2_MIN_BLOCK_LOG_SIZE + 1];
140 * Check to make sure a device inode is real. Returns 1 if the device
141 * checks out, 0 if not.
143 * Note: this routine is now also used to check FIFO's and Sockets,
144 * since they have the same requirement; the i_block fields should be
147 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
148 struct ext2_inode *inode)
153 * If the index or extents flag is set, then this is a bogus
156 if (inode->i_flags & (EXT2_INDEX_FL | EXT4_EXTENTS_FL))
160 * We should be able to do the test below all the time, but
161 * because the kernel doesn't forcibly clear the device
162 * inode's additional i_block fields, there are some rare
163 * occasions when a legitimate device inode will have non-zero
164 * additional i_block fields. So for now, we only complain
165 * when the immutable flag is set, which should never happen
166 * for devices. (And that's when the problem is caused, since
167 * you can't set or clear immutable flags for devices.) Once
168 * the kernel has been fixed we can change this...
170 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
171 for (i=4; i < EXT2_N_BLOCKS; i++)
172 if (inode->i_block[i])
179 * Check to make sure a symlink inode is real. Returns 1 if the symlink
180 * checks out, 0 if not.
182 static int check_symlink(e2fsck_t ctx, struct problem_context *pctx,
183 ext2_ino_t ino, struct ext2_inode *inode, char *buf)
189 if ((inode->i_size_high || inode->i_size == 0) ||
190 (inode->i_flags & EXT2_INDEX_FL))
193 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
196 if (inode->i_flags & EXT4_EXTENTS_FL)
198 if (ext2fs_inline_data_size(ctx->fs, ino, &inline_size))
200 if (inode->i_size != inline_size)
206 if (ext2fs_is_fast_symlink(inode)) {
207 if (inode->i_flags & EXT4_EXTENTS_FL)
209 buf = (char *)inode->i_block;
210 buflen = sizeof(inode->i_block);
212 ext2_extent_handle_t handle;
213 struct ext2_extent_info info;
214 struct ext2fs_extent extent;
217 if (inode->i_flags & EXT4_EXTENTS_FL) {
218 if (ext2fs_extent_open2(ctx->fs, ino, inode, &handle))
220 if (ext2fs_extent_get_info(handle, &info) ||
221 (info.num_entries != 1) ||
222 (info.max_depth != 0)) {
223 ext2fs_extent_free(handle);
226 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT,
228 (extent.e_lblk != 0) ||
229 (extent.e_len != 1)) {
230 ext2fs_extent_free(handle);
234 ext2fs_extent_free(handle);
236 blk = inode->i_block[0];
238 for (i = 1; i < EXT2_N_BLOCKS; i++)
239 if (inode->i_block[i])
243 if (blk < ctx->fs->super->s_first_data_block ||
244 blk >= ext2fs_blocks_count(ctx->fs->super))
247 if (io_channel_read_blk64(ctx->fs->io, blk, 1, buf))
250 buflen = ctx->fs->blocksize;
253 if (inode->i_flags & EXT4_ENCRYPT_FL)
254 len = ext2fs_le16_to_cpu(*(__u16 *)buf) + 2;
256 len = strnlen(buf, buflen);
258 /* Add missing NUL terminator at end of symlink (LU-1540),
259 * but only offer to fix this in pass1, not from pass2. */
260 if (len > inode->i_size && pctx != NULL &&
261 fix_problem(ctx, PR_1_SYMLINK_NUL, pctx)) {
262 buf[inode->i_size] = '\0';
263 if (ext2fs_is_fast_symlink(inode)) {
264 e2fsck_write_inode(ctx, ino,
265 inode, "check_ext_attr");
267 if (io_channel_write_blk64(ctx->fs->io,
278 if (len != inode->i_size)
284 int e2fsck_pass1_check_symlink(e2fsck_t ctx, ext2_ino_t ino,
285 struct ext2_inode *inode, char *buf)
287 return check_symlink(ctx, NULL, ino, inode, buf);
291 * If the extents or inlinedata flags are set on the inode, offer to clear 'em.
293 #define BAD_SPECIAL_FLAGS (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)
294 static void check_extents_inlinedata(e2fsck_t ctx,
295 struct problem_context *pctx)
297 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
300 if (!fix_problem(ctx, PR_1_SPECIAL_EXTENTS_IDATA, pctx))
303 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
304 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
306 #undef BAD_SPECIAL_FLAGS
309 * If the immutable (or append-only) flag is set on the inode, offer
312 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
313 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
315 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
318 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
321 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
322 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
326 * If device, fifo or socket, check size is zero -- if not offer to
329 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
331 struct ext2_inode *inode = pctx->inode;
333 if (EXT2_I_SIZE(inode) == 0)
336 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
339 ext2fs_inode_size_set(ctx->fs, inode, 0);
340 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
344 * For a given size, calculate how many blocks would be charged towards quota.
346 static blk64_t size_to_quota_blocks(ext2_filsys fs, size_t size)
350 clusters = DIV_ROUND_UP(size, fs->blocksize << fs->cluster_ratio_bits);
351 return EXT2FS_C2B(fs, clusters);
355 * Check validity of EA inode. Return 0 if EA inode is valid, otherwise return
358 static problem_t check_large_ea_inode(e2fsck_t ctx,
359 struct ext2_ext_attr_entry *entry,
360 struct problem_context *pctx,
361 blk64_t *quota_blocks)
363 struct ext2_inode inode;
364 __u32 hash, signed_hash;
367 /* Check if inode is within valid range */
368 if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
369 (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
370 pctx->num = entry->e_value_inum;
371 return PR_1_ATTR_VALUE_EA_INODE;
374 e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
376 retval = ext2fs_ext_attr_hash_entry3(ctx->fs, entry, NULL, &hash,
379 com_err("check_large_ea_inode", retval,
380 _("while hashing entry with e_value_inum = %u"),
381 entry->e_value_inum);
385 if ((hash == entry->e_hash) || (signed_hash == entry->e_hash)) {
386 *quota_blocks = size_to_quota_blocks(ctx->fs,
387 entry->e_value_size);
389 /* This might be an old Lustre-style ea_inode reference. */
390 if (inode.i_mtime == pctx->ino &&
391 inode.i_generation == pctx->inode->i_generation) {
394 /* If target inode is also missing EA_INODE flag,
395 * this is likely to be a bad reference.
397 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
398 pctx->num = entry->e_value_inum;
399 return PR_1_ATTR_VALUE_EA_INODE;
401 pctx->num = entry->e_hash;
402 return PR_1_ATTR_HASH;
407 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
408 pctx->num = entry->e_value_inum;
409 if (fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
410 inode.i_flags |= EXT4_EA_INODE_FL;
411 e2fsck_pass1_fix_lock(ctx);
412 ext2fs_write_inode(ctx->fs, entry->e_value_inum,
414 e2fsck_pass1_fix_unlock(ctx);
416 return PR_1_ATTR_NO_EA_INODE_FL;
422 static void inc_ea_inode_refs(e2fsck_t ctx, struct problem_context *pctx,
423 struct ext2_ext_attr_entry *first, void *end)
425 struct ext2_ext_attr_entry *entry = first;
426 struct ext2_ext_attr_entry *np = EXT2_EXT_ATTR_NEXT(entry);
428 while ((void *) entry < end && (void *) np < end &&
429 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
430 if (!entry->e_value_inum)
432 if (!ctx->ea_inode_refs) {
433 pctx->errcode = ea_refcount_create(&ctx->ea_inode_refs);
436 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
437 ctx->flags |= E2F_FLAG_ABORT;
441 ea_refcount_increment(ctx->ea_inode_refs, entry->e_value_inum,
445 np = EXT2_EXT_ATTR_NEXT(entry);
449 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx,
450 struct ea_quota *ea_ibody_quota)
452 struct ext2_super_block *sb = ctx->fs->super;
453 struct ext2_inode_large *inode;
454 struct ext2_ext_attr_entry *entry;
455 char *start, *header, *end;
456 unsigned int storage_size, remain;
457 problem_t problem = 0;
460 ea_ibody_quota->blocks = 0;
461 ea_ibody_quota->inodes = 0;
463 inode = (struct ext2_inode_large *) pctx->inode;
464 storage_size = EXT2_INODE_SIZE(ctx->fs->super) -
465 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize;
466 header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
467 inode->i_extra_isize;
468 end = header + storage_size;
469 entry = &IHDR(inode)->h_first_entry[0];
470 start = (char *)entry;
472 /* scan all entry's headers first */
474 /* take finish entry 0UL into account */
475 remain = storage_size - sizeof(__u32);
477 region = region_create(0, storage_size);
479 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
481 ctx->flags |= E2F_FLAG_ABORT;
484 if (region_allocate(region, 0, sizeof(__u32))) {
485 problem = PR_1_INODE_EA_ALLOC_COLLISION;
489 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
490 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
493 if (region_allocate(region, (char *)entry - (char *)header,
494 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
495 problem = PR_1_INODE_EA_ALLOC_COLLISION;
499 /* header eats this space */
500 remain -= sizeof(struct ext2_ext_attr_entry);
502 /* is attribute name valid? */
503 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
504 pctx->num = entry->e_name_len;
505 problem = PR_1_ATTR_NAME_LEN;
509 /* attribute len eats this space */
510 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
512 if (entry->e_value_inum == 0) {
513 /* check value size */
514 if (entry->e_value_size > remain) {
515 pctx->num = entry->e_value_size;
516 problem = PR_1_ATTR_VALUE_SIZE;
520 if (entry->e_value_size &&
521 region_allocate(region,
522 sizeof(__u32) + entry->e_value_offs,
524 entry->e_value_size))) {
525 problem = PR_1_INODE_EA_ALLOC_COLLISION;
529 hash = ext2fs_ext_attr_hash_entry(entry,
530 start + entry->e_value_offs);
531 if (entry->e_hash != 0 && entry->e_hash != hash)
532 hash = ext2fs_ext_attr_hash_entry_signed(entry,
533 start + entry->e_value_offs);
535 /* e_hash may be 0 in older inode's ea */
536 if (entry->e_hash != 0 && entry->e_hash != hash) {
537 pctx->num = entry->e_hash;
538 problem = PR_1_ATTR_HASH;
542 blk64_t quota_blocks;
544 problem = check_large_ea_inode(ctx, entry, pctx,
549 ea_ibody_quota->blocks += quota_blocks;
550 ea_ibody_quota->inodes++;
553 /* If EA value is stored in external inode then it does not
554 * consume space here */
555 if (entry->e_value_inum == 0)
556 remain -= entry->e_value_size;
558 entry = EXT2_EXT_ATTR_NEXT(entry);
561 if (region_allocate(region, (char *)entry - (char *)header,
563 problem = PR_1_INODE_EA_ALLOC_COLLISION;
570 * it seems like a corruption. it's very unlikely we could repair
571 * EA(s) in automatic fashion -bzzz
573 if (problem == 0 || !fix_problem(ctx, problem, pctx)) {
574 inc_ea_inode_refs(ctx, pctx,
575 (struct ext2_ext_attr_entry *)start, end);
579 /* simply remove all possible EA(s) */
580 *((__u32 *)header) = 0UL;
581 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
582 EXT2_INODE_SIZE(sb), "pass1");
583 ea_ibody_quota->blocks = 0;
584 ea_ibody_quota->inodes = 0;
587 static int check_inode_extra_negative_epoch(__u32 xtime, __u32 extra) {
588 return (xtime & (1U << 31)) != 0 &&
589 (extra & EXT4_EPOCH_MASK) == EXT4_EPOCH_MASK;
592 #define CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, xtime) \
593 check_inode_extra_negative_epoch(inode->i_##xtime, \
594 inode->i_##xtime##_extra)
596 /* When today's date is earlier than 2242, we assume that atimes,
597 * ctimes, crtimes, and mtimes with years in the range 2310..2378 are
598 * actually pre-1970 dates mis-encoded.
600 #define EXT4_EXTRA_NEGATIVE_DATE_CUTOFF 2 * (1LL << 32)
602 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx,
603 struct ea_quota *ea_ibody_quota)
605 struct ext2_super_block *sb = ctx->fs->super;
606 struct ext2_inode_large *inode;
608 int min, max, dirty = 0;
610 ea_ibody_quota->blocks = 0;
611 ea_ibody_quota->inodes = 0;
613 inode = (struct ext2_inode_large *) pctx->inode;
614 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
615 /* this isn't large inode. so, nothing to check */
620 printf("inode #%u, i_extra_size %d\n", pctx->ino,
621 inode->i_extra_isize);
623 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
624 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
625 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
627 * For now we will allow i_extra_isize to be 0, but really
628 * implementations should never allow i_extra_isize to be 0
630 if (inode->i_extra_isize &&
631 (inode->i_extra_isize < min || inode->i_extra_isize > max ||
632 inode->i_extra_isize & 3)) {
633 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
635 if (inode->i_extra_isize < min || inode->i_extra_isize > max)
636 inode->i_extra_isize = ctx->want_extra_isize;
638 inode->i_extra_isize = (inode->i_extra_isize + 3) & ~3;
644 /* check if there is no place for an EA header */
645 if (inode->i_extra_isize >= max - sizeof(__u32))
648 eamagic = &IHDR(inode)->h_magic;
649 if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
650 (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
651 (inode->i_extra_isize < ctx->want_extra_isize)) {
652 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
653 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
654 EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
655 inode->i_extra_isize = ctx->want_extra_isize;
657 if (inode->i_extra_isize < ctx->min_extra_isize)
658 ctx->min_extra_isize = inode->i_extra_isize;
661 if (*eamagic == EXT2_EXT_ATTR_MAGIC)
662 check_ea_in_inode(ctx, pctx, ea_ibody_quota);
664 /* Since crtime cannot be set directly from userspace, consider
665 * very old/future values worse than a bad atime/mtime. */
666 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, ctx->time_fudge))
667 e2fsck_mark_inode_badder(ctx, pctx, PR_1_CRTIME_BAD);
668 else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, ctx->time_fudge))
669 e2fsck_mark_inode_badder(ctx, pctx, PR_1_CRTIME_BAD);
671 * If the inode's extended atime (ctime, crtime, mtime) is stored in
672 * the old, invalid format, repair it.
674 if (((sizeof(time_t) <= 4) ||
675 (((sizeof(time_t) > 4) &&
676 ctx->now < EXT4_EXTRA_NEGATIVE_DATE_CUTOFF))) &&
677 (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime) ||
678 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime) ||
679 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime) ||
680 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))) {
682 if (!fix_problem_bad(ctx, PR_1_EA_TIME_OUT_OF_RANGE, pctx, 2))
685 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime))
686 inode->i_atime_extra &= ~EXT4_EPOCH_MASK;
687 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime))
688 inode->i_ctime_extra &= ~EXT4_EPOCH_MASK;
689 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime))
690 inode->i_crtime_extra &= ~EXT4_EPOCH_MASK;
691 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))
692 inode->i_mtime_extra &= ~EXT4_EPOCH_MASK;
698 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
699 EXT2_INODE_SIZE(sb), "pass1");
702 static _INLINE_ int is_blocks_used(e2fsck_t ctx, blk64_t block,
707 /* used to avoid duplicate output from below */
708 retval = ext2fs_test_block_bitmap_range2_valid(ctx->block_found_map,
713 retval = ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num);
715 e2fsck_pass1_block_map_r_lock(ctx);
717 retval = ext2fs_test_block_bitmap_range2(
718 ctx->global_ctx->block_found_map, block, num);
719 e2fsck_pass1_block_map_r_unlock(ctx);
728 * Check to see if the inode might really be a directory, despite i_mode
730 * This is a lot of complexity for something for which I'm not really
731 * convinced happens frequently in the wild. If for any reason this
732 * causes any problems, take this code out.
733 * [tytso:20070331.0827EDT]
735 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
738 struct ext2_inode *inode = pctx->inode;
739 struct ext2_dir_entry *dirent;
742 unsigned int i, rec_len, not_device = 0;
747 * If the mode looks OK, we believe it. If the first block in
748 * the i_block array is 0, this cannot be a directory. If the
749 * inode is extent-mapped, it is still the case that the latter
750 * cannot be 0 - the magic number in the extent header would make
753 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
754 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
758 * Check the block numbers in the i_block array for validity:
759 * zero blocks are skipped (but the first one cannot be zero -
760 * see above), other blocks are checked against the first and
761 * max data blocks (from the the superblock) and against the
762 * block bitmap. Any invalid block found means this cannot be
765 * If there are non-zero blocks past the fourth entry, then
766 * this cannot be a device file: we remember that for the next
769 * For extent mapped files, we don't do any sanity checking:
770 * just try to get the phys block of logical block 0 and run
773 * For inline data files, we just try to get the size of inline
774 * data. If it's true, we will treat it as a directory.
777 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
778 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
779 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
782 unsigned int rec_len2;
783 struct ext2_dir_entry de;
785 if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
788 * If the size isn't a multiple of 4, it's probably not a
794 * If the first 10 bytes don't look like a directory entry,
795 * it's probably not a directory.
797 memcpy(&dotdot, inode->i_block, sizeof(dotdot));
798 memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
799 EXT2_DIR_NAME_LEN(0));
800 dotdot = ext2fs_le32_to_cpu(dotdot);
801 de.inode = ext2fs_le32_to_cpu(de.inode);
802 de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
803 ext2fs_get_rec_len(ctx->fs, &de, &rec_len2);
804 if (dotdot >= ctx->fs->super->s_inodes_count ||
805 (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
806 dotdot != EXT2_ROOT_INO) ||
807 de.inode >= ctx->fs->super->s_inodes_count ||
808 (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
810 rec_len2 > EXT4_MIN_INLINE_DATA_SIZE -
811 EXT4_INLINE_DATA_DOTDOT_SIZE)
813 /* device files never have a "system.data" entry */
815 } else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
817 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
820 /* device files are never extent mapped */
823 for (i=0; i < EXT2_N_BLOCKS; i++) {
824 blk = inode->i_block[i];
830 if (blk < ctx->fs->super->s_first_data_block ||
831 blk >= ext2fs_blocks_count(ctx->fs->super) ||
832 is_blocks_used(ctx, blk, 1))
833 return; /* Invalid block, can't be dir */
835 blk = inode->i_block[0];
839 * If the mode says this is a device file and the i_links_count field
840 * is sane and we have not ruled it out as a device file previously,
841 * we declare it a device file, not a directory.
843 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
844 (inode->i_links_count == 1) && !not_device)
847 /* read the first block */
848 ehandler_operation(_("reading directory block"));
849 retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
850 ehandler_operation(0);
854 dirent = (struct ext2_dir_entry *) buf;
855 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
858 if ((ext2fs_dirent_name_len(dirent) != 1) ||
859 (dirent->name[0] != '.') ||
860 (dirent->inode != pctx->ino) ||
863 (rec_len >= ctx->fs->blocksize - 12))
866 dirent = (struct ext2_dir_entry *) (buf + rec_len);
867 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
870 if ((ext2fs_dirent_name_len(dirent) != 2) ||
871 (dirent->name[0] != '.') ||
872 (dirent->name[1] != '.') ||
878 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
879 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
880 e2fsck_write_inode_full(ctx, pctx->ino, inode,
881 EXT2_INODE_SIZE(ctx->fs->super),
882 "check_is_really_dir");
886 extern errcode_t e2fsck_setup_icount(e2fsck_t ctx, const char *icount_name,
887 int flags, ext2_icount_t hint,
890 unsigned int threshold;
891 unsigned int save_type;
899 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
901 profile_get_uint(ctx->profile, "scratch_files",
902 "numdirs_threshold", 0, 0, &threshold);
903 profile_get_boolean(ctx->profile, "scratch_files",
904 "icount", 0, 1, &enable);
906 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
908 num_dirs = 1024; /* Guess */
910 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
911 (!threshold || num_dirs > threshold)) {
912 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir,
917 e2fsck_set_bitmap_type(ctx->fs, EXT2FS_BMAP64_RBTREE, icount_name,
919 if (ctx->options & E2F_OPT_ICOUNT_FULLMAP)
920 flags |= EXT2_ICOUNT_OPT_FULLMAP;
921 retval = ext2fs_create_icount2(ctx->fs, flags, 0, hint, ret);
922 ctx->fs->default_bitmap_type = save_type;
926 static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
928 struct problem_context *pctx)
931 struct ext2_inode_large inode;
934 * Reread inode. If we don't see checksum error, then this inode
935 * has been fixed elsewhere.
937 ctx->stashed_ino = 0;
938 retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
940 if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
946 * Checksum still doesn't match. That implies that the inode passes
947 * all the sanity checks, so maybe the checksum is simply corrupt.
948 * See if the user will go for fixing that.
950 if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
954 e2fsck_pass1_fix_lock(ctx);
955 retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
957 e2fsck_pass1_fix_unlock(ctx);
961 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
962 struct problem_context *pctx, int needed_size)
964 struct ext2_ext_attr_header *header;
965 struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
966 char *start, name[4096], block_buf[4096];
967 int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
968 int in_inode = 1, error;
969 unsigned int freed_bytes = inode->i_extra_isize;
971 entry_ino = &IHDR(inode)->h_first_entry[0];
972 start = (char *)entry_ino;
974 if (inode->i_file_acl) {
975 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
977 /* We have already checked this block, shouldn't happen */
979 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
982 header = BHDR(block_buf);
983 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
984 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
988 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
991 len = sizeof(entry->e_name);
992 entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
994 while (freed_bytes < needed_size) {
995 if (entry_size && name[0] != '\0') {
997 if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
998 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
999 EXT2_EXT_ATTR_SIZE(entry->e_value_size);
1000 error = ext2fs_attr_set(ctx->fs, pctx->ino,
1001 (struct ext2_inode *)inode,
1002 index, name, 0, 0, 0);
1004 freed_bytes += ea_size;
1007 len = sizeof(entry->e_name);
1008 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
1009 entry = EXT2_EXT_ATTR_NEXT(entry);
1010 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
1013 len = sizeof(entry->e_name);
1014 entry_size = ext2fs_attr_get_next_attr(entry,
1015 index, name, len, 1);
1020 if (!entry && index < EXT2_ATTR_INDEX_MAX)
1021 entry = (struct ext2_ext_attr_entry *)start;
1031 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
1032 struct problem_context *pctx)
1034 int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
1038 retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
1039 ctx->want_extra_isize, &ret,
1041 if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
1042 goto mark_expand_eisize_map;
1044 e2fsck_write_inode_full(ctx, pctx->ino,
1045 (struct ext2_inode *)inode,
1046 EXT2_INODE_SIZE(ctx->fs->super),
1051 if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
1052 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
1053 fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
1054 ctx->flags |= E2F_FLAG_ABORT;
1059 pctx->num = ctx->fs->super->s_min_extra_isize;
1060 fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
1064 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
1066 if (retval >= ctx->want_extra_isize)
1069 needed_size -= retval;
1072 * We loop here until either the user deletes EA(s) or
1073 * EXTRA_ISIZE feature is disabled.
1075 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
1076 ctx->fs->super->s_feature_ro_compat &=
1077 ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
1078 ext2fs_mark_super_dirty(ctx->fs);
1082 ctx->fs_unexpanded_inodes++;
1084 /* No EA was deleted, inode cannot be expanded */
1088 mark_expand_eisize_map:
1089 if (!ctx->expand_eisize_map) {
1090 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1091 _("expand extrz isize map"),
1092 &ctx->expand_eisize_map);
1093 if (pctx->errcode) {
1094 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
1100 /* Add this inode to the expand_eisize_map */
1101 ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
1105 static void reserve_block_for_root_repair(e2fsck_t ctx)
1109 ext2_filsys fs = ctx->fs;
1111 ctx->root_repair_block = 0;
1112 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
1115 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1118 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1119 ctx->root_repair_block = blk;
1122 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
1126 ext2_filsys fs = ctx->fs;
1127 static const char name[] = "lost+found";
1130 ctx->lnf_repair_block = 0;
1131 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
1134 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
1137 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
1138 ctx->lnf_repair_block = blk;
1142 static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
1143 struct ext2_inode *inode,
1147 struct ext2_xattr_handle *handle;
1150 retval = ext2fs_xattrs_open(fs, ino, &handle);
1154 retval = ext2fs_xattrs_read_inode(handle,
1155 (struct ext2_inode_large *)inode);
1159 retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
1162 ext2fs_free_mem(&p);
1164 (void) ext2fs_xattrs_close(&handle);
1168 int e2fsck_fix_bad_inode(e2fsck_t ctx, struct problem_context *pctx)
1173 if (!ctx->inode_badness)
1176 if (ext2fs_icount_fetch(ctx->inode_badness, pctx->ino, &badness))
1179 if (badness > ctx->inode_badness_threshold) {
1180 __u64 pctx_num_sav = pctx->num;
1182 pctx->num = badness;
1183 rc = fix_problem_notbad(ctx, PR_1B_INODE_TOOBAD, pctx);
1184 pctx->num = pctx_num_sav;
1190 static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
1191 struct problem_context *pctx,
1198 * If the inode failed the checksum and the user didn't
1199 * clear the inode, test the checksum again -- if it still
1200 * fails, ask the user if the checksum should be corrected.
1202 pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
1204 ctx->flags |= E2F_FLAG_ABORT;
1206 #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
1208 finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
1209 if (e2fsck_should_abort(ctx)) { \
1210 e2fsck_pass1_check_unlock(ctx); \
1215 static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
1220 for (i = 0; i < EXT2_N_BLOCKS; i++) {
1221 x = inode->i_block[i];
1222 #ifdef WORDS_BIGENDIAN
1223 x = ext2fs_swab32(x);
1225 if (x >= ext2fs_blocks_count(fs->super))
1233 * Figure out what to do with an inode that has both extents and inline data
1234 * inode flags set. Returns -1 if we decide to erase the inode, 0 otherwise.
1236 static int fix_inline_data_extents_file(e2fsck_t ctx,
1238 struct ext2_inode *inode,
1240 struct problem_context *pctx)
1242 size_t max_inline_ea_size;
1243 ext2_filsys fs = ctx->fs;
1246 /* Both feature flags not set? Just run the regular checks */
1247 if (!ext2fs_has_feature_extents(fs->super) &&
1248 !ext2fs_has_feature_inline_data(fs->super))
1251 /* Clear both flags if it's a special file */
1252 if (LINUX_S_ISCHR(inode->i_mode) ||
1253 LINUX_S_ISBLK(inode->i_mode) ||
1254 LINUX_S_ISFIFO(inode->i_mode) ||
1255 LINUX_S_ISSOCK(inode->i_mode)) {
1256 check_extents_inlinedata(ctx, pctx);
1260 /* If it looks like an extent tree, try to clear inlinedata */
1261 if (ext2fs_extent_header_verify(inode->i_block,
1262 sizeof(inode->i_block)) == 0 &&
1263 fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
1264 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1269 /* If it looks short enough to be inline data, try to clear extents */
1270 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
1271 max_inline_ea_size = inode_size -
1272 (EXT2_GOOD_OLD_INODE_SIZE +
1273 ((struct ext2_inode_large *)inode)->i_extra_isize);
1275 max_inline_ea_size = 0;
1276 if (EXT2_I_SIZE(inode) <
1277 EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
1278 fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
1279 inode->i_flags &= ~EXT4_EXTENTS_FL;
1285 * Too big for inline data, but no evidence of extent tree -
1286 * maybe it's a block map file? If the mappings all look valid?
1288 if (could_be_block_map(fs, inode) &&
1289 fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
1290 #ifdef WORDS_BIGENDIAN
1293 for (i = 0; i < EXT2_N_BLOCKS; i++)
1294 inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
1297 inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
1302 /* Oh well, just clear the busted inode. */
1303 if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
1304 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1310 e2fsck_write_inode(ctx, ino, inode, "pass1");
1315 static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
1317 ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
1318 dgrp_t start = *group, grp, grp_end = ctx->fs->group_desc_count;
1319 blk64_t blocks_to_read = 0;
1320 errcode_t err = EXT2_ET_INVALID_ARGUMENT;
1323 if (ctx->fs->fs_num_threads > 1)
1324 grp_end = ctx->thread_info.et_group_end;
1326 if (ctx->readahead_kb == 0)
1329 /* Keep iterating groups until we have enough to readahead */
1330 inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
1331 for (grp = start; grp < grp_end; grp++) {
1332 if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
1334 inodes_in_group = ctx->fs->super->s_inodes_per_group -
1335 ext2fs_bg_itable_unused(ctx->fs, grp);
1336 blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
1338 if (blocks_to_read * ctx->fs->blocksize >
1339 ctx->readahead_kb * 1024)
1343 err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
1345 if (err == EAGAIN) {
1346 ctx->readahead_kb /= 2;
1352 /* Error; disable itable readahead */
1353 *group = ctx->fs->group_desc_count;
1354 *next_ino = ctx->fs->super->s_inodes_count;
1357 * Don't do more readahead until we've reached the first inode
1358 * of the last inode scan buffer block for the last group.
1361 inodes_per_buffer = (ctx->inode_buffer_blocks ?
1362 ctx->inode_buffer_blocks :
1363 EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
1364 ctx->fs->blocksize /
1365 EXT2_INODE_SIZE(ctx->fs->super);
1367 *next_ino = inodes_in_group -
1368 (inodes_in_group % inodes_per_buffer) + 1 +
1369 (grp * ctx->fs->super->s_inodes_per_group);
1374 * Check if the passed ino is one of the used superblock quota inodes.
1376 * Before the quota inodes were journaled, older superblock quota inodes
1377 * were just regular files in the filesystem and not reserved inodes. This
1378 * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1379 * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1381 static int quota_inum_is_super(struct ext2_super_block *sb, ext2_ino_t ino)
1383 enum quota_type qtype;
1385 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1386 if (*quota_sb_inump(sb, qtype) == ino)
1393 * Check if the passed ino is one of the reserved quota inodes.
1394 * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1395 * inodes. These inodes may or may not be in use by the quota feature.
1397 static int quota_inum_is_reserved(ext2_filsys fs, ext2_ino_t ino)
1399 enum quota_type qtype;
1401 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1402 if (quota_type2inum(qtype, fs->super) == ino)
1408 static int e2fsck_should_abort(e2fsck_t ctx)
1410 e2fsck_t global_ctx;
1412 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1415 if (ctx->global_ctx) {
1416 global_ctx = ctx->global_ctx;
1417 if (global_ctx->flags & E2F_FLAG_SIGNAL_MASK)
1423 static void init_ext2_max_sizes()
1429 * Init ext2_max_sizes which will be immutable and shared between
1432 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1434 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
1435 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
1436 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
1437 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
1438 max_sizes = (max_sizes * (1UL << i));
1439 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
1445 /* TODO: tdb needs to be handled properly for multiple threads*/
1446 static int multiple_threads_supported(e2fsck_t ctx)
1449 unsigned int threshold;
1450 ext2_ino_t num_dirs;
1455 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
1457 profile_get_uint(ctx->profile, "scratch_files",
1458 "numdirs_threshold", 0, 0, &threshold);
1459 profile_get_boolean(ctx->profile, "scratch_files",
1460 "icount", 0, 1, &enable);
1462 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
1464 num_dirs = 1024; /* Guess */
1466 /* tdb is unsupported now */
1467 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
1468 (!threshold || num_dirs > threshold))
1475 * Even though we could specify number of threads,
1476 * but it might be more than the whole filesystem
1477 * block groups, correct it here.
1479 static void e2fsck_pass1_set_thread_num(e2fsck_t ctx)
1481 unsigned flexbg_size = 1;
1482 ext2_filsys fs = ctx->fs;
1483 int num_threads = ctx->pfs_num_threads;
1486 if (num_threads < 1) {
1491 if (!multiple_threads_supported(ctx)) {
1493 fprintf(stderr, "Fall through single thread for pass1 "
1494 "because tdb could not handle properly\n");
1498 if (ext2fs_has_feature_flex_bg(fs->super))
1499 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
1500 max_threads = fs->group_desc_count / flexbg_size;
1501 if (max_threads == 0)
1503 if (max_threads > E2FSCK_MAX_THREADS)
1504 max_threads = E2FSCK_MAX_THREADS;
1506 if (num_threads > max_threads) {
1507 fprintf(stderr, "Use max possible thread num: %d instead\n",
1509 num_threads = max_threads;
1512 ctx->pfs_num_threads = num_threads;
1513 ctx->fs->fs_num_threads = num_threads;
1518 * We need call mark_table_blocks() before multiple
1519 * thread start, since all known system blocks should be
1520 * marked and checked later.
1522 static errcode_t e2fsck_pass1_prepare(e2fsck_t ctx)
1524 struct problem_context pctx;
1525 ext2_filsys fs = ctx->fs;
1526 unsigned long long readahead_kb;
1528 init_ext2_max_sizes();
1530 e2fsck_pass1_set_thread_num(ctx);
1532 /* If we can do readahead, figure out how many groups to pull in. */
1533 if (!e2fsck_can_readahead(ctx->fs))
1534 ctx->readahead_kb = 0;
1535 else if (ctx->readahead_kb == ~0ULL)
1536 ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
1539 /* don't use more than 1/10 of memory for threads checking */
1540 readahead_kb = get_memory_size() / (10 * ctx->pfs_num_threads);
1541 /* maybe better disable RA if this is too small? */
1542 if (ctx->readahead_kb > readahead_kb)
1543 ctx->readahead_kb = readahead_kb;
1545 clear_problem_context(&pctx);
1546 if (!(ctx->options & E2F_OPT_PREEN))
1547 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1549 pctx.errcode = e2fsck_allocate_subcluster_bitmap(ctx->fs,
1550 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
1551 "block_found_map", &ctx->block_found_map);
1554 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1555 ctx->flags |= E2F_FLAG_ABORT;
1556 return pctx.errcode;
1558 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1559 _("metadata block map"), EXT2FS_BMAP64_RBTREE,
1560 "block_metadata_map", &ctx->block_metadata_map);
1563 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1564 ctx->flags |= E2F_FLAG_ABORT;
1565 return pctx.errcode;
1568 mark_table_blocks(ctx);
1569 pctx.errcode = ext2fs_convert_subcluster_bitmap(ctx->fs,
1570 &ctx->block_found_map);
1572 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1573 ctx->flags |= E2F_FLAG_ABORT;
1574 return pctx.errcode;
1577 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1578 _("multiply claimed block map"),
1579 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1580 &ctx->block_dup_map);
1583 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1585 /* Should never get here */
1586 ctx->flags |= E2F_FLAG_ABORT;
1587 return pctx.errcode;
1590 if (ext2fs_has_feature_mmp(fs->super) &&
1591 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1592 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1593 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1594 fs->super->s_mmp_block);
1596 pthread_rwlock_init(&ctx->fs_fix_rwlock, NULL);
1597 pthread_rwlock_init(&ctx->fs_block_map_rwlock, NULL);
1598 if (ctx->pfs_num_threads > 1)
1599 ctx->fs_need_locking = 1;
1605 static void e2fsck_pass1_post(e2fsck_t ctx)
1607 struct problem_context pctx;
1608 ext2_filsys fs = ctx->fs;
1611 if (e2fsck_should_abort(ctx))
1614 block_buf = (char *)e2fsck_allocate_memory(ctx, ctx->fs->blocksize * 3,
1615 "block interate buffer");
1616 reserve_block_for_root_repair(ctx);
1617 reserve_block_for_lnf_repair(ctx);
1620 * If any extended attribute blocks' reference counts need to
1621 * be adjusted, either up (ctx->refcount_extra), or down
1622 * (ctx->refcount), then fix them.
1624 if (ctx->refcount) {
1625 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1626 ea_refcount_free(ctx->refcount);
1629 if (ctx->refcount_extra) {
1630 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1632 ea_refcount_free(ctx->refcount_extra);
1633 ctx->refcount_extra = 0;
1636 if (ctx->invalid_bitmaps)
1637 handle_fs_bad_blocks(ctx);
1639 /* We don't need the block_ea_map any more */
1640 if (ctx->block_ea_map) {
1641 ext2fs_free_block_bitmap(ctx->block_ea_map);
1642 ctx->block_ea_map = 0;
1645 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1646 struct ext2_inode *inode;
1647 int inode_size = EXT2_INODE_SIZE(fs->super);
1648 inode = e2fsck_allocate_memory(ctx, inode_size,
1651 clear_problem_context(&pctx);
1652 pctx.errcode = ext2fs_create_resize_inode(fs);
1654 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1656 ctx->flags |= E2F_FLAG_ABORT;
1657 ext2fs_free_mem(&inode);
1658 ext2fs_free_mem(&block_buf);
1663 if (!pctx.errcode) {
1664 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1666 inode->i_mtime = ctx->now;
1667 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1670 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1671 ext2fs_free_mem(&inode);
1674 if (ctx->flags & E2F_FLAG_RESTART) {
1675 ext2fs_free_mem(&block_buf);
1679 if (ctx->block_dup_map) {
1680 if (!(ctx->flags & E2F_FLAG_DUP_BLOCK)) {
1681 ext2fs_free_mem(&block_buf);
1684 if (ctx->options & E2F_OPT_PREEN) {
1685 clear_problem_context(&pctx);
1686 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1688 e2fsck_pass1_dupblocks(ctx, block_buf);
1689 ext2fs_free_mem(&block_buf);
1690 ctx->flags &= ~E2F_FLAG_DUP_BLOCK;
1693 ctx->flags |= E2F_FLAG_ALLOC_OK;
1698 * Lustre FS creates special inodes - precreated objects.
1699 * They are zero-sized and have special attributes:
1700 * mode |= S_ISUID | S_ISGID;
1701 * valid |= LA_ATIME | LA_MTIME | LA_CTIME;
1706 static int precreated_object(struct ext2_inode *inode)
1708 if (((inode->i_mode & (S_ISUID | S_ISGID)) == (S_ISUID | S_ISGID)) &&
1709 inode->i_ctime == 0)
1714 void e2fsck_pass1_run(e2fsck_t ctx)
1716 ext2_filsys fs = ctx->fs;
1718 struct ext2_inode *inode = NULL;
1719 ext2_inode_scan scan = NULL;
1720 char *block_buf = NULL;
1721 #ifdef RESOURCE_TRACK
1722 struct resource_track rtrack;
1724 unsigned char frag, fsize;
1725 struct problem_context pctx;
1726 struct scan_callback_struct scan_struct;
1727 struct ext2_super_block *sb = ctx->fs->super;
1729 const char *eop_next_inode = _("getting next inode from scan");
1730 int imagic_fs, extent_fs, inlinedata_fs, casefold_fs;
1731 int low_dtime_check = 1;
1732 unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
1733 unsigned int bufsize;
1734 int failed_csum = 0;
1735 ext2_ino_t ino_threshold = 0;
1736 dgrp_t ra_group = 0;
1737 struct ea_quota ea_ibody_quota;
1738 struct process_inode_block *inodes_to_process;
1739 int process_inode_count, check_mmp = 0;
1740 e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
1743 init_resource_track(&rtrack, ctx->fs->io);
1744 clear_problem_context(&pctx);
1746 pass1_readahead(ctx, &ra_group, &ino_threshold);
1747 if (ext2fs_has_feature_dir_index(fs->super) &&
1748 !(ctx->options & E2F_OPT_NO)) {
1749 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
1750 ctx->dirs_to_hash = 0;
1754 mtrace_print("Pass 1");
1757 imagic_fs = ext2fs_has_feature_imagic_inodes(sb);
1758 extent_fs = ext2fs_has_feature_extents(sb);
1759 inlinedata_fs = ext2fs_has_feature_inline_data(sb);
1760 casefold_fs = ext2fs_has_feature_casefold(sb);
1763 * Allocate bitmaps structures
1765 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1766 EXT2FS_BMAP64_RBTREE,
1768 &ctx->inode_used_map);
1771 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1772 ctx->flags |= E2F_FLAG_ABORT;
1775 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1776 _("directory inode map"),
1777 ctx->global_ctx ? EXT2FS_BMAP64_RBTREE :
1778 EXT2FS_BMAP64_AUTODIR,
1779 "inode_dir_map", &ctx->inode_dir_map);
1782 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1783 ctx->flags |= E2F_FLAG_ABORT;
1786 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1787 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1788 "inode_reg_map", &ctx->inode_reg_map);
1791 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1792 ctx->flags |= E2F_FLAG_ABORT;
1797 e2fsck_allocate_inode_bitmap(fs,
1798 _("inode casefold map"),
1799 EXT2FS_BMAP64_RBTREE,
1800 "inode_casefold_map",
1801 &ctx->inode_casefold_map);
1804 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1805 ctx->flags |= E2F_FLAG_ABORT;
1809 pctx.errcode = e2fsck_setup_icount(ctx, "inode_link_info", 0, NULL,
1810 &ctx->inode_link_info);
1812 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1813 ctx->flags |= E2F_FLAG_ABORT;
1816 bufsize = inode_size;
1817 if (bufsize < sizeof(struct ext2_inode_large))
1818 bufsize = sizeof(struct ext2_inode_large);
1819 inode = (struct ext2_inode *)
1820 e2fsck_allocate_memory(ctx, bufsize, "scratch inode");
1822 inodes_to_process = (struct process_inode_block *)
1823 e2fsck_allocate_memory(ctx,
1824 (ctx->process_inode_size *
1825 sizeof(struct process_inode_block)),
1826 "array of inodes to process");
1827 process_inode_count = 0;
1829 pctx.errcode = ext2fs_init_dblist(fs, 0);
1831 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1832 ctx->flags |= E2F_FLAG_ABORT;
1837 * If the last orphan field is set, clear it, since the pass1
1838 * processing will automatically find and clear the orphans.
1839 * In the future, we may want to try using the last_orphan
1840 * linked list ourselves, but for now, we clear it so that the
1841 * ext3 mount code won't get confused.
1843 if (!(ctx->options & E2F_OPT_READONLY)) {
1844 if (fs->super->s_last_orphan) {
1845 fs->super->s_last_orphan = 0;
1846 ext2fs_mark_super_dirty(fs);
1850 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1851 "block iterate buffer");
1852 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1853 e2fsck_use_inode_shortcuts(ctx, 1);
1854 e2fsck_intercept_block_allocations(ctx);
1855 old_op = ehandler_operation(_("opening inode scan"));
1856 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1858 ehandler_operation(old_op);
1860 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1861 ctx->flags |= E2F_FLAG_ABORT;
1864 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1865 EXT2_SF_WARN_GARBAGE_INODES, 0);
1866 ctx->stashed_inode = inode;
1867 scan_struct.ctx = ctx;
1868 scan_struct.block_buf = block_buf;
1869 scan_struct.inodes_to_process = inodes_to_process;
1870 scan_struct.process_inode_count = &process_inode_count;
1871 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1872 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1873 ctx->fs->group_desc_count)))
1875 if ((fs->super->s_wtime &&
1876 fs->super->s_wtime < fs->super->s_inodes_count) ||
1877 (fs->super->s_mtime &&
1878 fs->super->s_mtime < fs->super->s_inodes_count) ||
1879 (fs->super->s_mkfs_time &&
1880 fs->super->s_mkfs_time < fs->super->s_inodes_count))
1881 low_dtime_check = 0;
1883 /* Set up ctx->lost_and_found if possible */
1884 (void) e2fsck_get_lost_and_found(ctx, 0);
1887 if (ctx->global_ctx) {
1888 if (ctx->options & E2F_OPT_DEBUG &&
1889 ctx->options & E2F_OPT_MULTITHREAD)
1890 log_out(ctx, "jumping to group %u\n",
1891 ctx->thread_info.et_group_start);
1892 pctx.errcode = ext2fs_inode_scan_goto_blockgroup(scan,
1893 ctx->thread_info.et_group_start);
1895 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1896 ctx->flags |= E2F_FLAG_ABORT;
1904 e2fsck_pass1_check_lock(ctx);
1906 if (!global_ctx->mmp_update_thread) {
1907 e2fsck_pass1_block_map_w_lock(ctx);
1908 if (!global_ctx->mmp_update_thread) {
1909 global_ctx->mmp_update_thread =
1910 ctx->thread_info.et_thread_index + 1;
1913 e2fsck_pass1_block_map_w_unlock(ctx);
1916 /* only one active thread could update mmp block. */
1917 e2fsck_pass1_block_map_r_lock(ctx);
1918 if (global_ctx->mmp_update_thread ==
1919 ctx->thread_info.et_thread_index + 1)
1921 e2fsck_pass1_block_map_r_unlock(ctx);
1926 if (check_mmp && (ino % (fs->super->s_inodes_per_group * 4) == 1)) {
1927 if (e2fsck_mmp_update(fs))
1928 fatal_error(ctx, 0);
1930 old_op = ehandler_operation(eop_next_inode);
1931 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1933 if (ino > ino_threshold)
1934 pass1_readahead(ctx, &ra_group, &ino_threshold);
1935 ehandler_operation(old_op);
1936 if (e2fsck_should_abort(ctx)) {
1937 e2fsck_pass1_check_unlock(ctx);
1940 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1942 * If badblocks says badblocks is bad, offer to clear
1943 * the list, update the in-core bb list, and restart
1946 if (ino == EXT2_BAD_INO &&
1947 fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1951 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1952 ext2fs_badblocks_list_free(ctx->fs->badblocks);
1953 ctx->fs->badblocks = NULL;
1954 err = ext2fs_read_bb_inode(ctx->fs,
1955 &ctx->fs->badblocks);
1957 fix_problem(ctx, PR_1_ISCAN_ERROR,
1959 ctx->flags |= E2F_FLAG_ABORT;
1960 e2fsck_pass1_check_unlock(ctx);
1963 ctx->flags |= E2F_FLAG_RESTART;
1964 err = ext2fs_inode_scan_goto_blockgroup(scan,
1967 fix_problem(ctx, PR_1_ISCAN_ERROR,
1969 ctx->flags |= E2F_FLAG_ABORT;
1970 e2fsck_pass1_check_unlock(ctx);
1973 e2fsck_pass1_check_unlock(ctx);
1976 if (!ctx->inode_bb_map)
1978 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1979 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1980 e2fsck_pass1_check_unlock(ctx);
1983 if (pctx.errcode == EXT2_ET_SCAN_FINISHED) {
1984 e2fsck_pass1_check_unlock(ctx);
1988 pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1989 pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1990 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1991 ctx->flags |= E2F_FLAG_ABORT;
1992 e2fsck_pass1_check_unlock(ctx);
1996 e2fsck_pass1_check_unlock(ctx);
2000 if (ctx->global_ctx)
2001 ctx->thread_info.et_inode_number++;
2005 ctx->stashed_ino = ino;
2007 /* Clear trashed inode? */
2008 if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
2009 inode->i_links_count > 0 &&
2010 fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
2012 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2014 failed_csum = pctx.errcode != 0;
2017 * Check for inodes who might have been part of the
2018 * orphaned list linked list. They should have gotten
2019 * dealt with by now, unless the list had somehow been
2022 * FIXME: In the future, inodes which are still in use
2023 * (and which are therefore) pending truncation should
2024 * be handled specially. Right now we just clear the
2025 * dtime field, and the normal e2fsck handling of
2026 * inodes where i_size and the inode blocks are
2027 * inconsistent is to fix i_size, instead of releasing
2028 * the extra blocks. This won't catch the inodes that
2029 * was at the end of the orphan list, but it's better
2030 * than nothing. The right answer is that there
2031 * shouldn't be any bugs in the orphan list handling. :-)
2033 if (inode->i_dtime && low_dtime_check &&
2034 inode->i_dtime < ctx->fs->super->s_inodes_count) {
2035 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
2036 inode->i_dtime = inode->i_links_count ?
2038 e2fsck_write_inode(ctx, ino, inode,
2044 if (inode->i_links_count) {
2045 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
2046 ino, inode->i_links_count);
2048 pctx.num = inode->i_links_count;
2049 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
2050 ctx->flags |= E2F_FLAG_ABORT;
2051 e2fsck_pass1_check_unlock(ctx);
2054 } else if ((ino >= EXT2_FIRST_INODE(fs->super)) &&
2055 !quota_inum_is_reserved(fs, ino)) {
2056 if (!inode->i_dtime && inode->i_mode) {
2057 if (fix_problem(ctx,
2058 PR_1_ZERO_DTIME, &pctx)) {
2059 inode->i_dtime = ctx->now;
2060 e2fsck_write_inode(ctx, ino, inode,
2065 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2066 e2fsck_pass1_check_unlock(ctx);
2070 if ((inode->i_flags & EXT4_CASEFOLD_FL) &&
2071 ((!LINUX_S_ISDIR(inode->i_mode) &&
2072 fix_problem(ctx, PR_1_CASEFOLD_NONDIR, &pctx)) ||
2074 fix_problem(ctx, PR_1_CASEFOLD_FEATURE, &pctx)))) {
2075 inode->i_flags &= ~EXT4_CASEFOLD_FL;
2076 e2fsck_write_inode(ctx, ino, inode, "pass1");
2079 /* Conflicting inlinedata/extents inode flags? */
2080 if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
2081 (inode->i_flags & EXT4_EXTENTS_FL)) {
2082 int res = fix_inline_data_extents_file(ctx, ino, inode,
2086 /* skip FINISH_INODE_LOOP */
2087 e2fsck_pass1_check_unlock(ctx);
2092 /* Test for incorrect inline_data flags settings. */
2093 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
2094 (ino >= EXT2_FIRST_INODE(fs->super))) {
2097 pctx.errcode = get_inline_data_ea_size(fs, ino, inode,
2099 if (!pctx.errcode &&
2100 fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
2101 e2fsck_pass1_fix_lock(ctx);
2102 ext2fs_set_feature_inline_data(sb);
2103 ext2fs_mark_super_dirty(fs);
2104 e2fsck_pass1_fix_unlock(ctx);
2106 } else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
2107 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2108 /* skip FINISH_INODE_LOOP */
2109 e2fsck_pass1_check_unlock(ctx);
2114 /* Test for inline data flag but no attr */
2115 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
2116 (ino >= EXT2_FIRST_INODE(fs->super))) {
2123 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2124 err = get_inline_data_ea_size(fs, ino, inode, &size);
2125 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
2126 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
2130 /* Everything is awesome... */
2132 case EXT2_ET_BAD_EA_BLOCK_NUM:
2133 case EXT2_ET_BAD_EA_HASH:
2134 case EXT2_ET_BAD_EA_HEADER:
2135 case EXT2_ET_EA_BAD_NAME_LEN:
2136 case EXT2_ET_EA_BAD_VALUE_SIZE:
2137 case EXT2_ET_EA_KEY_NOT_FOUND:
2138 case EXT2_ET_EA_NO_SPACE:
2139 case EXT2_ET_MISSING_EA_FEATURE:
2140 case EXT2_ET_INLINE_DATA_CANT_ITERATE:
2141 case EXT2_ET_INLINE_DATA_NO_BLOCK:
2142 case EXT2_ET_INLINE_DATA_NO_SPACE:
2143 case EXT2_ET_NO_INLINE_DATA:
2144 case EXT2_ET_EXT_ATTR_CSUM_INVALID:
2145 case EXT2_ET_EA_BAD_VALUE_OFFSET:
2146 case EXT2_ET_EA_INODE_CORRUPTED:
2147 /* broken EA or no system.data EA; truncate */
2148 if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
2150 err = ext2fs_inode_size_set(fs, inode, 0);
2153 ctx->flags |= E2F_FLAG_ABORT;
2154 e2fsck_pass1_check_unlock(ctx);
2157 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
2158 memset(&inode->i_block, 0,
2159 sizeof(inode->i_block));
2160 e2fsck_write_inode(ctx, ino, inode,
2166 /* Some other kind of non-xattr error? */
2168 ctx->flags |= E2F_FLAG_ABORT;
2169 e2fsck_pass1_check_unlock(ctx);
2175 * Test for incorrect extent flag settings.
2177 * On big-endian machines we must be careful:
2178 * When the inode is read, the i_block array is not swapped
2179 * if the extent flag is set. Therefore if we are testing
2180 * for or fixing a wrongly-set flag, we must potentially
2181 * (un)swap before testing, or after fixing.
2185 * In this case the extents flag was set when read, so
2186 * extent_header_verify is ok. If the inode is cleared,
2187 * no need to swap... so no extra swapping here.
2189 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
2190 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2191 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
2192 if ((ext2fs_extent_header_verify(inode->i_block,
2193 sizeof(inode->i_block)) == 0) &&
2194 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
2195 e2fsck_pass1_fix_lock(ctx);
2196 ext2fs_set_feature_extents(sb);
2197 ext2fs_mark_super_dirty(fs);
2199 e2fsck_pass1_fix_unlock(ctx);
2200 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
2202 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2203 if (ino == EXT2_BAD_INO)
2204 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
2206 /* skip FINISH_INODE_LOOP */
2207 e2fsck_pass1_check_unlock(ctx);
2213 * For big-endian machines:
2214 * If the inode didn't have the extents flag set when it
2215 * was read, then the i_blocks array was swapped. To test
2216 * as an extents header, we must swap it back first.
2217 * IF we then set the extents flag, the entire i_block
2218 * array must be un/re-swapped to make it proper extents data.
2220 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
2221 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
2222 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
2223 (LINUX_S_ISREG(inode->i_mode) ||
2224 LINUX_S_ISDIR(inode->i_mode))) {
2226 #ifdef WORDS_BIGENDIAN
2227 __u32 tmp_block[EXT2_N_BLOCKS];
2230 for (i = 0; i < EXT2_N_BLOCKS; i++)
2231 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
2234 ehp = inode->i_block;
2236 if ((ext2fs_extent_header_verify(ehp,
2237 sizeof(inode->i_block)) == 0) &&
2238 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
2239 inode->i_flags |= EXT4_EXTENTS_FL;
2240 #ifdef WORDS_BIGENDIAN
2241 memcpy(inode->i_block, tmp_block,
2242 sizeof(inode->i_block));
2244 e2fsck_write_inode(ctx, ino, inode, "pass1");
2247 /* Consider an inode in extent fs w/o extents
2248 * at least a bit suspect. It only matters if
2249 * the inode has several other problems. */
2250 e2fsck_mark_inode_bad(ctx, &pctx,
2251 PR_1_UNSET_EXTENT_FL);
2255 if (ino == EXT2_BAD_INO) {
2256 struct process_block_struct pb;
2258 if ((failed_csum || inode->i_mode || inode->i_uid ||
2259 inode->i_gid || inode->i_links_count ||
2260 (inode->i_flags & EXT4_INLINE_DATA_FL) ||
2261 inode->i_file_acl) &&
2262 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
2263 memset(inode, 0, sizeof(struct ext2_inode));
2264 e2fsck_write_inode(ctx, ino, inode,
2269 e2fsck_pass1_block_map_r_lock(ctx);
2270 pctx.errcode = ext2fs_copy_bitmap(ctx->global_ctx ?
2271 ctx->global_ctx->block_found_map :
2272 ctx->block_found_map, &pb.fs_meta_blocks);
2273 e2fsck_pass1_block_map_r_unlock(ctx);
2276 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
2277 ctx->flags |= E2F_FLAG_ABORT;
2278 e2fsck_pass1_check_unlock(ctx);
2281 pb.ino = EXT2_BAD_INO;
2282 pb.num_blocks = pb.last_block = 0;
2283 pb.last_db_block = -1;
2284 pb.num_illegal_blocks = 0;
2285 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
2286 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
2290 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
2291 block_buf, process_bad_block, &pb);
2292 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
2294 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
2295 ctx->flags |= E2F_FLAG_ABORT;
2296 e2fsck_pass1_check_unlock(ctx);
2300 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
2301 ctx->flags |= E2F_FLAG_ABORT;
2302 e2fsck_pass1_check_unlock(ctx);
2305 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2306 clear_problem_context(&pctx);
2307 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2308 e2fsck_pass1_check_unlock(ctx);
2310 } else if (ino == EXT2_ROOT_INO) {
2312 * Make sure the root inode is a directory; if
2313 * not, offer to clear it. It will be
2314 * regenerated in pass #3.
2316 if (!LINUX_S_ISDIR(inode->i_mode)) {
2317 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
2321 * If dtime is set, offer to clear it. mke2fs
2322 * version 0.2b created filesystems with the
2323 * dtime field set for the root and lost+found
2324 * directories. We won't worry about
2325 * /lost+found, since that can be regenerated
2326 * easily. But we will fix the root directory
2327 * as a special case.
2329 if (inode->i_dtime && inode->i_links_count) {
2330 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
2332 e2fsck_write_inode(ctx, ino, inode,
2337 } else if (ino == EXT2_JOURNAL_INO) {
2338 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2339 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
2340 if (!LINUX_S_ISREG(inode->i_mode) &&
2341 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
2343 inode->i_mode = LINUX_S_IFREG;
2344 e2fsck_write_inode(ctx, ino, inode,
2348 check_blocks(ctx, &pctx, block_buf, NULL);
2349 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2350 e2fsck_pass1_check_unlock(ctx);
2353 if ((inode->i_links_count ||
2354 inode->i_blocks || inode->i_block[0]) &&
2355 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
2357 memset(inode, 0, inode_size);
2358 ext2fs_icount_store(ctx->inode_link_info,
2360 e2fsck_write_inode_full(ctx, ino, inode,
2361 inode_size, "pass1");
2364 } else if (quota_inum_is_reserved(fs, ino)) {
2365 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2366 if (ext2fs_has_feature_quota(fs->super) &&
2367 quota_inum_is_super(fs->super, ino)) {
2368 if (!LINUX_S_ISREG(inode->i_mode) &&
2369 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
2371 inode->i_mode = LINUX_S_IFREG;
2372 e2fsck_write_inode(ctx, ino, inode,
2376 check_blocks(ctx, &pctx, block_buf, NULL);
2377 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2378 e2fsck_pass1_check_unlock(ctx);
2381 if ((inode->i_links_count ||
2382 inode->i_blocks || inode->i_block[0]) &&
2383 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
2385 memset(inode, 0, inode_size);
2386 ext2fs_icount_store(ctx->inode_link_info,
2388 e2fsck_write_inode_full(ctx, ino, inode,
2389 inode_size, "pass1");
2392 } else if (ino == fs->super->s_orphan_file_inum) {
2393 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2394 if (ext2fs_has_feature_orphan_file(fs->super)) {
2395 if (!LINUX_S_ISREG(inode->i_mode) &&
2396 fix_problem(ctx, PR_1_ORPHAN_FILE_BAD_MODE,
2398 inode->i_mode = LINUX_S_IFREG;
2399 e2fsck_write_inode(ctx, ino, inode,
2403 check_blocks(ctx, &pctx, block_buf, NULL);
2404 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2407 if ((inode->i_links_count ||
2408 inode->i_blocks || inode->i_block[0]) &&
2409 fix_problem(ctx, PR_1_ORPHAN_FILE_NOT_CLEAR,
2411 memset(inode, 0, inode_size);
2412 ext2fs_icount_store(ctx->inode_link_info, ino,
2414 e2fsck_write_inode_full(ctx, ino, inode,
2415 inode_size, "pass1");
2418 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
2419 problem_t problem = 0;
2421 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2422 if (ino == EXT2_BOOT_LOADER_INO) {
2423 if (LINUX_S_ISDIR(inode->i_mode))
2424 problem = PR_1_RESERVED_BAD_MODE;
2425 } else if (ino == EXT2_RESIZE_INO) {
2426 if (inode->i_mode &&
2427 !LINUX_S_ISREG(inode->i_mode))
2428 problem = PR_1_RESERVED_BAD_MODE;
2430 if (inode->i_mode != 0)
2431 problem = PR_1_RESERVED_BAD_MODE;
2434 if (fix_problem(ctx, problem, &pctx)) {
2436 e2fsck_write_inode(ctx, ino, inode,
2441 check_blocks(ctx, &pctx, block_buf, NULL);
2442 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2443 e2fsck_pass1_check_unlock(ctx);
2447 if (!inode->i_links_count) {
2448 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2449 e2fsck_pass1_check_unlock(ctx);
2453 * n.b. 0.3c ext2fs code didn't clear i_links_count for
2454 * deleted files. Oops.
2456 * Since all new ext2 implementations get this right,
2457 * we now assume that the case of non-zero
2458 * i_links_count and non-zero dtime means that we
2459 * should keep the file, not delete it.
2462 if (inode->i_dtime) {
2463 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
2465 e2fsck_write_inode(ctx, ino, inode, "pass1");
2470 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
2471 switch (fs->super->s_creator_os) {
2473 frag = inode->osd2.hurd2.h_i_frag;
2474 fsize = inode->osd2.hurd2.h_i_fsize;
2480 /* Fixed in pass2, e2fsck_process_bad_inode(). */
2481 if (inode->i_faddr || frag || fsize ||
2482 (!ext2fs_has_feature_largedir(fs->super) &&
2483 LINUX_S_ISDIR(inode->i_mode) && inode->i_size_high))
2484 e2fsck_mark_inode_bad(ctx, &pctx,
2485 PR_2_DIR_SIZE_HIGH_ZERO);
2486 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2487 !ext2fs_has_feature_64bit(fs->super) &&
2488 inode->osd2.linux2.l_i_file_acl_high != 0)
2489 e2fsck_mark_inode_bad(ctx, &pctx,
2490 PR_2_I_FILE_ACL_HI_ZERO);
2491 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
2492 !ext2fs_has_feature_huge_file(fs->super) &&
2493 (inode->osd2.linux2.l_i_blocks_hi != 0))
2494 e2fsck_mark_inode_bad(ctx, &pctx, PR_2_BLOCKS_HI_ZERO);
2495 if (inode->i_flags & EXT2_IMAGIC_FL) {
2497 if (!ctx->inode_imagic_map)
2498 alloc_imagic_map(ctx);
2499 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
2502 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
2503 inode->i_flags &= ~EXT2_IMAGIC_FL;
2504 e2fsck_write_inode(ctx, ino,
2511 check_inode_extra_space(ctx, &pctx, &ea_ibody_quota);
2512 check_is_really_dir(ctx, &pctx, block_buf);
2515 * ext2fs_inode_has_valid_blocks2 does not actually look
2516 * at i_block[] values, so not endian-sensitive here.
2518 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
2519 LINUX_S_ISLNK(inode->i_mode) &&
2520 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
2521 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
2522 inode->i_flags &= ~EXT4_EXTENTS_FL;
2523 e2fsck_write_inode(ctx, ino, inode, "pass1");
2527 if ((inode->i_flags & EXT4_ENCRYPT_FL) &&
2528 add_encrypted_file(ctx, &pctx) < 0)
2531 if (casefold_fs && inode->i_flags & EXT4_CASEFOLD_FL)
2532 ext2fs_mark_inode_bitmap2(ctx->inode_casefold_map, ino);
2534 if (LINUX_S_ISDIR(inode->i_mode)) {
2535 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
2536 e2fsck_add_dir_info(ctx, ino, 0);
2537 ctx->fs_directory_count++;
2538 if (inode->i_flags & EXT4_CASEFOLD_FL)
2539 add_casefolded_dir(ctx, ino);
2540 } else if (LINUX_S_ISREG (inode->i_mode)) {
2541 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
2542 ctx->fs_regular_count++;
2543 } else if (LINUX_S_ISCHR (inode->i_mode) &&
2544 e2fsck_pass1_check_device_inode(fs, inode)) {
2545 check_extents_inlinedata(ctx, &pctx);
2546 check_immutable(ctx, &pctx);
2547 check_size(ctx, &pctx);
2548 ctx->fs_chardev_count++;
2549 } else if (LINUX_S_ISBLK (inode->i_mode) &&
2550 e2fsck_pass1_check_device_inode(fs, inode)) {
2551 check_extents_inlinedata(ctx, &pctx);
2552 check_immutable(ctx, &pctx);
2553 check_size(ctx, &pctx);
2554 ctx->fs_blockdev_count++;
2555 } else if (LINUX_S_ISLNK (inode->i_mode) &&
2556 check_symlink(ctx, &pctx, ino, inode, block_buf)) {
2557 check_immutable(ctx, &pctx);
2558 ctx->fs_symlinks_count++;
2559 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
2560 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2561 e2fsck_pass1_check_unlock(ctx);
2563 } else if (ext2fs_is_fast_symlink(inode)) {
2564 ctx->fs_fast_symlinks_count++;
2565 check_blocks(ctx, &pctx, block_buf,
2567 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2568 e2fsck_pass1_check_unlock(ctx);
2572 else if (LINUX_S_ISFIFO (inode->i_mode) &&
2573 e2fsck_pass1_check_device_inode(fs, inode)) {
2574 check_extents_inlinedata(ctx, &pctx);
2575 check_immutable(ctx, &pctx);
2576 check_size(ctx, &pctx);
2577 ctx->fs_fifo_count++;
2578 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
2579 e2fsck_pass1_check_device_inode(fs, inode)) {
2580 check_extents_inlinedata(ctx, &pctx);
2581 check_immutable(ctx, &pctx);
2582 check_size(ctx, &pctx);
2583 ctx->fs_sockets_count++;
2585 e2fsck_mark_inode_bad(ctx, &pctx, PR_2_BAD_MODE);
2588 /* Future atime/mtime may be valid in rare cases, but are more
2589 * likely to indicate corruption. Don't try to fix timestamps,
2590 * but take into consideration whether inode is corrupted. If
2591 * no other problems with the inode, probably it is OK. */
2592 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
2593 e2fsck_mark_inode_bad(ctx, &pctx, PR_1_INODE_BAD_TIME);
2594 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime, ctx->time_fudge))
2595 e2fsck_mark_inode_bad(ctx, &pctx, PR_1_INODE_BAD_TIME);
2597 /* Since ctime cannot be set directly from userspace, consider
2598 * very old/future values worse than a bad atime/mtime. Same for
2599 * crtime, but it is checked in check_inode_extra_space(). */
2600 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
2601 e2fsck_mark_inode_badder(ctx, &pctx,
2602 PR_1_INODE_BAD_TIME);
2603 else if (!precreated_object(inode) &&
2604 EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
2606 e2fsck_mark_inode_badder(ctx, &pctx,
2607 PR_1_INODE_BAD_TIME);
2609 /* no restart if clearing bad inode before block processing */
2610 if (e2fsck_fix_bad_inode(ctx, &pctx)) {
2611 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
2615 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2616 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
2617 if (inode->i_block[EXT2_IND_BLOCK])
2618 ctx->fs_ind_count++;
2619 if (inode->i_block[EXT2_DIND_BLOCK])
2620 ctx->fs_dind_count++;
2621 if (inode->i_block[EXT2_TIND_BLOCK])
2622 ctx->fs_tind_count++;
2624 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
2625 !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
2626 (inode->i_block[EXT2_IND_BLOCK] ||
2627 inode->i_block[EXT2_DIND_BLOCK] ||
2628 inode->i_block[EXT2_TIND_BLOCK] ||
2629 ext2fs_file_acl_block(fs, inode))) {
2630 struct process_inode_block *itp;
2632 itp = &inodes_to_process[process_inode_count];
2634 itp->ea_ibody_quota = ea_ibody_quota;
2635 if (inode_size < sizeof(struct ext2_inode_large))
2636 memcpy(&itp->inode, inode, inode_size);
2638 memcpy(&itp->inode, inode, sizeof(itp->inode));
2639 process_inode_count++;
2641 check_blocks(ctx, &pctx, block_buf, &ea_ibody_quota);
2643 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
2645 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
2646 struct ext2_inode_large *inode_l;
2648 inode_l = (struct ext2_inode_large *)inode;
2650 if (inode_l->i_extra_isize < ctx->want_extra_isize) {
2651 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
2652 inode_exp = e2fsck_pass1_expand_eisize(ctx,
2656 if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
2658 ctx->min_extra_isize = inode_l->i_extra_isize;
2661 if (e2fsck_should_abort(ctx)) {
2662 e2fsck_pass1_check_unlock(ctx);
2666 if (process_inode_count >= ctx->process_inode_size) {
2667 process_inodes(ctx, block_buf, inodes_to_process,
2668 &process_inode_count);
2670 if (e2fsck_should_abort(ctx)) {
2671 e2fsck_pass1_check_unlock(ctx);
2676 e2fsck_pass1_check_unlock(ctx);
2678 process_inodes(ctx, block_buf, inodes_to_process,
2679 &process_inode_count);
2680 ext2fs_close_inode_scan(scan);
2683 if (ctx->ea_block_quota_blocks) {
2684 ea_refcount_free(ctx->ea_block_quota_blocks);
2685 ctx->ea_block_quota_blocks = 0;
2688 if (ctx->ea_block_quota_inodes) {
2689 ea_refcount_free(ctx->ea_block_quota_inodes);
2690 ctx->ea_block_quota_inodes = 0;
2693 if (ctx->flags & E2F_FLAG_RESTART) {
2695 * Only the master copy of the superblock and block
2696 * group descriptors are going to be written during a
2697 * restart, so set the superblock to be used to be the
2698 * master superblock.
2700 ctx->use_superblock = 0;
2704 if (ctx->large_dirs && !ext2fs_has_feature_largedir(fs->super)) {
2705 if (fix_problem(ctx, PR_2_FEATURE_LARGE_DIRS, &pctx)) {
2706 ext2fs_set_feature_largedir(fs->super);
2707 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2708 ext2fs_mark_super_dirty(fs);
2710 if (fs->super->s_rev_level == EXT2_GOOD_OLD_REV &&
2711 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
2712 ext2fs_update_dynamic_rev(fs);
2713 ext2fs_mark_super_dirty(fs);
2717 ctx->flags |= E2F_FLAG_ALLOC_OK;
2718 ext2fs_free_mem(&inodes_to_process);
2720 e2fsck_use_inode_shortcuts(ctx, 0);
2721 ext2fs_free_mem(&inodes_to_process);
2722 inodes_to_process = 0;
2725 ext2fs_close_inode_scan(scan);
2727 ext2fs_free_mem(&block_buf);
2729 ext2fs_free_mem(&inode);
2732 * The l+f inode may have been cleared, so zap it now and
2733 * later passes will recalculate it if necessary
2735 ctx->lost_and_found = 0;
2737 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
2738 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
2740 ctx->invalid_bitmaps++;
2742 /* reset update_thread after this thread exit */
2743 e2fsck_pass1_block_map_w_lock(ctx);
2745 global_ctx->mmp_update_thread = 0;
2746 e2fsck_pass1_block_map_w_unlock(ctx);
2751 static errcode_t e2fsck_pass1_copy_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2752 ext2fs_generic_bitmap *dest)
2756 ret = ext2fs_copy_bitmap(*src, dest);
2765 static void e2fsck_pass1_free_bitmap(ext2fs_generic_bitmap *bitmap)
2768 ext2fs_free_generic_bmap(*bitmap);
2774 static errcode_t e2fsck_pass1_merge_bitmap(ext2_filsys fs, ext2fs_generic_bitmap *src,
2775 ext2fs_generic_bitmap *dest)
2780 if (*dest == NULL) {
2784 ret = ext2fs_merge_bitmap(*src, *dest, NULL, NULL);
2794 static errcode_t e2fsck_pass1_copy_fs(ext2_filsys dest, e2fsck_t src_context,
2799 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2800 dest->inode_map = NULL;
2801 dest->block_map = NULL;
2802 dest->badblocks = NULL;
2804 dest->dblist->fs = dest;
2805 if (src->block_map) {
2806 retval = e2fsck_pass1_copy_bitmap(dest, &src->block_map,
2811 if (src->inode_map) {
2812 retval = e2fsck_pass1_copy_bitmap(dest, &src->inode_map,
2818 if (src->badblocks) {
2819 retval = ext2fs_badblocks_copy(src->badblocks,
2825 /* disable it for now */
2826 src_context->openfs_flags &= ~EXT2_FLAG_EXCLUSIVE;
2827 retval = ext2fs_open_channel(dest, src_context->io_options,
2828 src_context->io_manager,
2829 src_context->openfs_flags,
2830 src->io->block_size);
2834 /* Block size might not be default */
2835 io_channel_set_blksize(dest->io, src->io->block_size);
2836 ehandler_init(dest->io);
2838 assert(dest->io->magic == src->io->magic);
2839 assert(dest->io->manager == src->io->manager);
2840 assert(strcmp(dest->io->name, src->io->name) == 0);
2841 assert(dest->io->block_size == src->io->block_size);
2842 assert(dest->io->read_error == src->io->read_error);
2843 assert(dest->io->write_error == src->io->write_error);
2844 assert(dest->io->refcount == src->io->refcount);
2845 assert(dest->io->flags == src->io->flags);
2846 assert(dest->io->app_data == dest);
2847 assert(src->io->app_data == src);
2848 assert(dest->io->align == src->io->align);
2850 /* The data should be written to disk immediately */
2851 dest->io->flags |= CHANNEL_FLAGS_WRITETHROUGH;
2852 /* icache will be rebuilt if needed, so do not copy from @src */
2857 static int e2fsck_pass1_merge_fs(ext2_filsys dest, ext2_filsys src)
2859 struct ext2_inode_cache *icache = dest->icache;
2860 errcode_t retval = 0;
2862 io_channel dest_image_io;
2863 ext2fs_inode_bitmap inode_map;
2864 ext2fs_block_bitmap block_map;
2865 ext2_badblocks_list badblocks;
2868 e2fsck_t dest_ctx = dest->priv_data;
2871 dest_image_io = dest->image_io;
2872 inode_map = dest->inode_map;
2873 block_map = dest->block_map;
2874 badblocks = dest->badblocks;
2875 dblist = dest->dblist;
2876 flags = dest->flags;
2878 memcpy(dest, src, sizeof(struct struct_ext2_filsys));
2880 dest->image_io = dest_image_io;
2881 dest->icache = icache;
2882 dest->inode_map = inode_map;
2883 dest->block_map = block_map;
2884 dest->badblocks = badblocks;
2885 dest->dblist = dblist;
2886 dest->priv_data = dest_ctx;
2888 dest->dblist->fs = dest;
2889 dest->flags = src->flags | flags;
2890 if (!(src->flags & EXT2_FLAG_VALID) || !(flags & EXT2_FLAG_VALID))
2891 ext2fs_unmark_valid(dest);
2894 ext2fs_free_inode_cache(src->icache);
2898 retval = e2fsck_pass1_merge_bitmap(dest, &src->inode_map,
2903 retval = e2fsck_pass1_merge_bitmap(dest, &src->block_map,
2910 retval = ext2fs_merge_dblist(src->dblist,
2915 dest->dblist = src->dblist;
2916 dest->dblist->fs = dest;
2921 if (src->badblocks) {
2922 if (dest->badblocks == NULL)
2923 retval = ext2fs_badblocks_copy(src->badblocks,
2926 retval = ext2fs_badblocks_merge(src->badblocks,
2930 io_channel_close(src->io);
2932 ext2fs_free_generic_bmap(src->inode_map);
2934 ext2fs_free_generic_bmap(src->block_map);
2936 ext2fs_badblocks_list_free(src->badblocks);
2938 ext2fs_free_dblist(src->dblist);
2943 static void e2fsck_pass1_copy_invalid_bitmaps(e2fsck_t global_ctx,
2944 e2fsck_t thread_ctx)
2947 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2948 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2949 dgrp_t total = grp_end - grp_start;
2951 thread_ctx->invalid_inode_bitmap_flag =
2952 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2953 "invalid_inode_bitmap");
2954 thread_ctx->invalid_block_bitmap_flag =
2955 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2956 "invalid_block_bitmap");
2957 thread_ctx->invalid_inode_table_flag =
2958 e2fsck_allocate_memory(global_ctx, sizeof(int) * total,
2959 "invalid_inode_table");
2961 memcpy(thread_ctx->invalid_block_bitmap_flag,
2962 &global_ctx->invalid_block_bitmap_flag[grp_start],
2963 total * sizeof(int));
2964 memcpy(thread_ctx->invalid_inode_bitmap_flag,
2965 &global_ctx->invalid_inode_bitmap_flag[grp_start],
2966 total * sizeof(int));
2967 memcpy(thread_ctx->invalid_inode_table_flag,
2968 &global_ctx->invalid_inode_table_flag[grp_start],
2969 total * sizeof(int));
2971 thread_ctx->invalid_bitmaps = 0;
2972 for (i = grp_start, j = 0; i < grp_end; i++, j++) {
2973 if (thread_ctx->invalid_block_bitmap_flag[j])
2974 thread_ctx->invalid_bitmaps++;
2975 if (thread_ctx->invalid_inode_bitmap_flag[j])
2976 thread_ctx->invalid_bitmaps++;
2977 if (thread_ctx->invalid_inode_table_flag[j])
2978 thread_ctx->invalid_bitmaps++;
2982 static void e2fsck_pass1_merge_invalid_bitmaps(e2fsck_t global_ctx,
2983 e2fsck_t thread_ctx)
2985 dgrp_t grp_start = thread_ctx->thread_info.et_group_start;
2986 dgrp_t grp_end = thread_ctx->thread_info.et_group_end;
2987 dgrp_t total = grp_end - grp_start;
2989 memcpy(&global_ctx->invalid_block_bitmap_flag[grp_start],
2990 thread_ctx->invalid_block_bitmap_flag, total * sizeof(int));
2991 memcpy(&global_ctx->invalid_inode_bitmap_flag[grp_start],
2992 thread_ctx->invalid_inode_bitmap_flag, total * sizeof(int));
2993 memcpy(&global_ctx->invalid_inode_table_flag[grp_start],
2994 thread_ctx->invalid_inode_table_flag, total * sizeof(int));
2995 global_ctx->invalid_bitmaps += thread_ctx->invalid_bitmaps;
2998 static errcode_t e2fsck_pass1_thread_prepare(e2fsck_t global_ctx,
2999 e2fsck_t *thread_ctx,
3001 dgrp_t average_group,
3002 ext2_ino_t average_inodes,
3003 dgrp_t *start_group,
3004 int *inode_count, int last_thread)
3007 e2fsck_t thread_context;
3008 ext2_filsys thread_fs;
3009 ext2_filsys global_fs = global_ctx->fs;
3010 struct e2fsck_thread *tinfo;
3013 assert(global_ctx->inode_used_map == NULL);
3014 assert(global_ctx->inode_dir_map == NULL);
3015 assert(global_ctx->inode_bb_map == NULL);
3016 assert(global_ctx->inode_imagic_map == NULL);
3017 assert(global_ctx->inode_reg_map == NULL);
3018 assert(global_ctx->inodes_to_rebuild == NULL);
3020 assert(global_ctx->block_found_map != NULL);
3021 assert(global_ctx->block_metadata_map != NULL);
3022 assert(global_ctx->block_dup_map != NULL);
3023 assert(global_ctx->block_ea_map == NULL);
3024 assert(global_ctx->fs->dblist == NULL);
3026 retval = ext2fs_get_mem(sizeof(struct e2fsck_struct), &thread_context);
3028 com_err(global_ctx->program_name, retval, "while allocating memory");
3031 memcpy(thread_context, global_ctx, sizeof(struct e2fsck_struct));
3032 thread_context->block_dup_map = NULL;
3033 thread_context->casefolded_dirs = NULL;
3034 thread_context->expand_eisize_map = NULL;
3035 thread_context->inode_badness = NULL;
3037 retval = e2fsck_allocate_block_bitmap(global_ctx->fs,
3038 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
3039 "block_found_map", &thread_context->block_found_map);
3043 thread_context->global_ctx = global_ctx;
3044 retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &thread_fs);
3046 com_err(global_ctx->program_name, retval, "while allocating memory");
3050 io_channel_flush_cleanup(global_fs->io);
3051 retval = e2fsck_pass1_copy_fs(thread_fs, global_ctx, global_fs);
3053 com_err(global_ctx->program_name, retval, "while copying fs");
3056 thread_fs->priv_data = thread_context;
3058 thread_context->thread_info.et_thread_index = thread_index;
3059 set_up_logging(thread_context);
3061 tinfo = &thread_context->thread_info;
3063 tinfo->et_group_start = *start_group;
3065 /* Try to allocate an equal number of in-use inodes to each thread,
3066 * rather than an equal number of block groups. Accumulate inodes
3067 * to each thread until approximately the average number is reached.
3069 * If the thread has least one group, and the inode count is closer
3070 * to the average *before* adding the next group, then stop before
3071 * adding it. Accumumlate the difference between ideal distribution
3072 * and actual number assigned to each thread to avoid to drifting too
3073 * far from the average, similar to Bresenham line drawing algorithm.
3075 * Limit groups per thread to 5x the average, in case the group
3076 * descriptor summaries are bad (e.g. read from backups with no info).
3078 * Assign all remaining groups to last thread if distribution was bad.
3080 for (grp = *start_group; grp < global_fs->group_desc_count; grp++) {
3081 ext2_ino_t grp_used = global_fs->super->s_inodes_per_group -
3082 ext2fs_bg_free_inodes_count(global_fs, grp);
3083 ext2_ino_t next_count = *inode_count + grp_used;
3085 if (((next_count >= average_inodes && grp > *start_group &&
3086 (long)next_count - average_inodes >
3087 (long)average_inodes - *inode_count) ||
3088 grp - *start_group > average_group * 5) && !last_thread) {
3089 *inode_count -= average_inodes;
3092 tinfo->et_inode_count += grp_used;
3093 *inode_count = next_count;
3095 tinfo->et_group_end = grp;
3097 tinfo->et_group_next = tinfo->et_group_start;
3098 tinfo->et_inode_number = 0;
3099 tinfo->et_log_buf[0] = '\0';
3100 tinfo->et_log_length = 0;
3101 if (thread_context->options & E2F_OPT_MULTITHREAD)
3102 log_out(thread_context,
3103 _("Scan group range [%d, %d), inode_count = %u/%u\n"),
3104 tinfo->et_group_start, tinfo->et_group_end,
3105 tinfo->et_inode_count, average_inodes);
3106 thread_context->fs = thread_fs;
3107 retval = quota_init_context(&thread_context->qctx, thread_fs, 0);
3109 com_err(global_ctx->program_name, retval,
3110 "while init quota context");
3113 *thread_ctx = thread_context;
3114 e2fsck_pass1_copy_invalid_bitmaps(global_ctx, thread_context);
3117 ext2fs_free_mem(&thread_fs);
3119 if (thread_context->block_found_map)
3120 ext2fs_free_mem(&thread_context->block_found_map);
3121 ext2fs_free_mem(&thread_context);
3125 static void e2fsck_pass1_merge_dir_info(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3127 if (thread_ctx->dir_info == NULL)
3130 if (global_ctx->dir_info == NULL) {
3131 global_ctx->dir_info = thread_ctx->dir_info;
3132 thread_ctx->dir_info = NULL;
3136 e2fsck_merge_dir_info(global_ctx, thread_ctx->dir_info,
3137 global_ctx->dir_info);
3140 static void e2fsck_pass1_merge_dx_dir(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3142 if (thread_ctx->dx_dir_info == NULL)
3145 if (global_ctx->dx_dir_info == NULL) {
3146 global_ctx->dx_dir_info = thread_ctx->dx_dir_info;
3147 global_ctx->dx_dir_info_size = thread_ctx->dx_dir_info_size;
3148 global_ctx->dx_dir_info_count = thread_ctx->dx_dir_info_count;
3149 thread_ctx->dx_dir_info = NULL;
3153 e2fsck_merge_dx_dir(global_ctx, thread_ctx);
3156 static int e2fsck_pass1_merge_encrypted_info(e2fsck_t global_ctx,
3157 e2fsck_t thread_ctx)
3159 if (thread_ctx->encrypted_files == NULL)
3162 if (global_ctx->encrypted_files == NULL) {
3163 global_ctx->encrypted_files = thread_ctx->encrypted_files;
3164 thread_ctx->encrypted_files = NULL;
3168 return e2fsck_merge_encrypted_info(global_ctx,
3169 thread_ctx->encrypted_files,
3170 global_ctx->encrypted_files);
3173 static inline errcode_t
3174 e2fsck_pass1_merge_icount(ext2_icount_t *dest_icount,
3175 ext2_icount_t *src_icount)
3178 if (*dest_icount == NULL) {
3179 *dest_icount = *src_icount;
3184 ret = ext2fs_icount_merge(*src_icount,
3194 static errcode_t e2fsck_pass1_merge_icounts(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3198 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_count,
3199 &thread_ctx->inode_count);
3202 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_link_info,
3203 &thread_ctx->inode_link_info);
3207 ret = e2fsck_pass1_merge_icount(&global_ctx->inode_badness,
3208 &thread_ctx->inode_badness);
3213 static errcode_t e2fsck_pass1_merge_dirs_to_hash(e2fsck_t global_ctx,
3214 e2fsck_t thread_ctx)
3216 errcode_t retval = 0;
3218 if (!thread_ctx->dirs_to_hash)
3221 if (!global_ctx->dirs_to_hash)
3222 retval = ext2fs_badblocks_copy(thread_ctx->dirs_to_hash,
3223 &global_ctx->dirs_to_hash);
3225 retval = ext2fs_badblocks_merge(thread_ctx->dirs_to_hash,
3226 global_ctx->dirs_to_hash);
3231 static errcode_t e2fsck_pass1_merge_ea_inode_refs(e2fsck_t global_ctx,
3232 e2fsck_t thread_ctx)
3234 ea_value_t thread_count, global_count;
3238 if (!thread_ctx->ea_inode_refs)
3241 if (!global_ctx->ea_inode_refs) {
3242 global_ctx->ea_inode_refs = thread_ctx->ea_inode_refs;
3243 thread_ctx->ea_inode_refs = NULL;
3247 ea_refcount_intr_begin(thread_ctx->ea_inode_refs);
3249 if ((ino = ea_refcount_intr_next(thread_ctx->ea_inode_refs,
3250 &thread_count)) == 0)
3252 ea_refcount_fetch(global_ctx->ea_inode_refs,
3253 ino, &global_count);
3254 retval = ea_refcount_store(global_ctx->ea_inode_refs,
3255 ino, thread_count + global_count);
3263 static ea_value_t ea_refcount_usage(e2fsck_t ctx, blk64_t blk,
3266 ea_value_t count_cur;
3267 ea_value_t count_extra = 0;
3268 ea_value_t count_orig;
3270 ea_refcount_fetch(ctx->refcount_orig, blk, &count_orig);
3271 ea_refcount_fetch(ctx->refcount, blk, &count_cur);
3272 /* most of time this is not needed */
3273 if (ctx->refcount_extra && count_cur == 0)
3274 ea_refcount_fetch(ctx->refcount_extra, blk, &count_extra);
3281 return count_orig + count_extra - count_cur;
3284 static errcode_t e2fsck_pass1_merge_ea_refcount(e2fsck_t global_ctx,
3285 e2fsck_t thread_ctx)
3289 errcode_t retval = 0;
3291 if (!thread_ctx->refcount)
3294 if (!global_ctx->refcount) {
3295 global_ctx->refcount = thread_ctx->refcount;
3296 thread_ctx->refcount = NULL;
3297 global_ctx->refcount_extra = thread_ctx->refcount;
3298 thread_ctx->refcount_extra = NULL;
3302 ea_refcount_intr_begin(thread_ctx->refcount);
3304 if ((blk = ea_refcount_intr_next(thread_ctx->refcount,
3308 * this EA has never seen before, so just store its
3309 * refcount and refcount_extra into global_ctx if needed.
3311 if (!global_ctx->block_ea_map ||
3312 !ext2fs_fast_test_block_bitmap2(global_ctx->block_ea_map,
3316 retval = ea_refcount_store(global_ctx->refcount,
3321 if (count > 0 || !thread_ctx->refcount_extra)
3323 ea_refcount_fetch(thread_ctx->refcount_extra, blk,
3328 if (!global_ctx->refcount_extra) {
3329 retval = ea_refcount_create(&global_ctx->refcount_extra);
3333 retval = ea_refcount_store(global_ctx->refcount_extra,
3339 ea_value_t thread_usage;
3340 ea_value_t global_usage;
3343 thread_usage = ea_refcount_usage(thread_ctx,
3345 global_usage = ea_refcount_usage(global_ctx,
3347 if (thread_usage + global_usage <= orig) {
3348 new = orig - thread_usage - global_usage;
3349 retval = ea_refcount_store(global_ctx->refcount,
3355 /* update it is as zero */
3356 retval = ea_refcount_store(global_ctx->refcount,
3360 /* Ooops, this EA was referenced more than it stated */
3361 if (!global_ctx->refcount_extra) {
3362 retval = ea_refcount_create(&global_ctx->refcount_extra);
3366 new = global_usage + thread_usage - orig;
3367 retval = ea_refcount_store(global_ctx->refcount_extra,
3377 static errcode_t e2fsck_pass1_merge_casefolded_dirs(e2fsck_t global_ctx,
3378 e2fsck_t thread_ctx)
3380 errcode_t retval = 0;
3382 if (!thread_ctx->casefolded_dirs)
3385 if (!global_ctx->casefolded_dirs)
3386 retval = ext2fs_badblocks_copy(thread_ctx->casefolded_dirs,
3387 &global_ctx->casefolded_dirs);
3389 retval = ext2fs_badblocks_merge(thread_ctx->casefolded_dirs,
3390 global_ctx->casefolded_dirs);
3395 static errcode_t e2fsck_pass1_merge_context(e2fsck_t global_ctx,
3396 e2fsck_t thread_ctx)
3398 ext2_filsys global_fs = global_ctx->fs;
3402 global_ctx->fs_directory_count += thread_ctx->fs_directory_count;
3403 global_ctx->fs_regular_count += thread_ctx->fs_regular_count;
3404 global_ctx->fs_blockdev_count += thread_ctx->fs_blockdev_count;
3405 global_ctx->fs_chardev_count += thread_ctx->fs_chardev_count;
3406 global_ctx->fs_links_count += thread_ctx->fs_links_count;
3407 global_ctx->fs_symlinks_count += thread_ctx->fs_symlinks_count;
3408 global_ctx->fs_fast_symlinks_count += thread_ctx->fs_fast_symlinks_count;
3409 global_ctx->fs_fifo_count += thread_ctx->fs_fifo_count;
3410 global_ctx->fs_total_count += thread_ctx->fs_total_count;
3411 global_ctx->fs_badblocks_count += thread_ctx->fs_badblocks_count;
3412 global_ctx->fs_sockets_count += thread_ctx->fs_sockets_count;
3413 global_ctx->fs_ind_count += thread_ctx->fs_ind_count;
3414 global_ctx->fs_dind_count += thread_ctx->fs_dind_count;
3415 global_ctx->fs_tind_count += thread_ctx->fs_tind_count;
3416 global_ctx->fs_fragmented += thread_ctx->fs_fragmented;
3417 global_ctx->fs_fragmented_dir += thread_ctx->fs_fragmented_dir;
3418 global_ctx->large_files += thread_ctx->large_files;
3419 /* threads might enable E2F_OPT_YES */
3420 global_ctx->options |= thread_ctx->options;
3421 global_ctx->flags |= thread_ctx->flags;
3423 * The l+f inode may have been cleared, so zap it now and
3424 * later passes will recalculate it if necessary
3426 global_ctx->lost_and_found = 0;
3427 /* merge extent depth count */
3428 for (i = 0; i < MAX_EXTENT_DEPTH_COUNT; i++)
3429 global_ctx->extent_depth_count[i] +=
3430 thread_ctx->extent_depth_count[i];
3432 e2fsck_pass1_merge_dir_info(global_ctx, thread_ctx);
3433 e2fsck_pass1_merge_dx_dir(global_ctx, thread_ctx);
3434 retval = e2fsck_pass1_merge_encrypted_info(global_ctx, thread_ctx);
3436 com_err(global_ctx->program_name, 0,
3437 _("while merging encrypted info\n"));
3441 retval = e2fsck_pass1_merge_fs(global_ctx->fs, thread_ctx->fs);
3443 com_err(global_ctx->program_name, 0, _("while merging fs\n"));
3446 retval = e2fsck_pass1_merge_icounts(global_ctx, thread_ctx);
3448 com_err(global_ctx->program_name, 0,
3449 _("while merging icounts\n"));
3453 retval = e2fsck_pass1_merge_dirs_to_hash(global_ctx, thread_ctx);
3455 com_err(global_ctx->program_name, 0,
3456 _("while merging dirs to hash\n"));
3460 e2fsck_pass1_merge_ea_inode_refs(global_ctx, thread_ctx);
3461 e2fsck_pass1_merge_ea_refcount(global_ctx, thread_ctx);
3462 retval = quota_merge_and_update_usage(global_ctx->qctx,
3467 retval = e2fsck_pass1_merge_casefolded_dirs(global_ctx, thread_ctx);
3469 com_err(global_ctx->program_name, 0,
3470 _("while merging casefolded dirs\n"));
3474 e2fsck_pass1_merge_invalid_bitmaps(global_ctx, thread_ctx);
3476 if (thread_ctx->min_extra_isize < global_ctx->min_extra_isize)
3477 global_ctx->min_extra_isize = thread_ctx->min_extra_isize;
3479 retval = e2fsck_pass1_merge_bitmap(global_fs,
3480 &thread_ctx->inode_used_map,
3481 &global_ctx->inode_used_map);
3485 retval = e2fsck_pass1_merge_bitmap(global_fs,
3486 &thread_ctx->inode_dir_map,
3487 &global_ctx->inode_dir_map);
3490 retval = e2fsck_pass1_merge_bitmap(global_fs,
3491 &thread_ctx->inode_bb_map,
3492 &global_ctx->inode_bb_map);
3495 retval = e2fsck_pass1_merge_bitmap(global_fs,
3496 &thread_ctx->inode_imagic_map,
3497 &global_ctx->inode_imagic_map);
3500 retval = e2fsck_pass1_merge_bitmap(global_fs,
3501 &thread_ctx->inode_reg_map,
3502 &global_ctx->inode_reg_map);
3505 retval = e2fsck_pass1_merge_bitmap(global_fs,
3506 &thread_ctx->inodes_to_rebuild,
3507 &global_ctx->inodes_to_rebuild);
3510 retval = e2fsck_pass1_merge_bitmap(global_fs,
3511 &thread_ctx->block_ea_map,
3512 &global_ctx->block_ea_map);
3516 retval = e2fsck_pass1_merge_bitmap(global_fs,
3517 &thread_ctx->expand_eisize_map,
3518 &global_ctx->expand_eisize_map);
3522 if (ext2fs_has_feature_shared_blocks(global_fs->super) &&
3523 !(global_ctx->options & E2F_OPT_UNSHARE_BLOCKS))
3526 * This need be done after merging block_ea_map
3527 * because ea block might be shared, we need exclude
3528 * them from dup blocks.
3530 e2fsck_pass1_block_map_w_lock(thread_ctx);
3531 retval = ext2fs_merge_bitmap(thread_ctx->block_found_map,
3532 global_ctx->block_found_map,
3533 global_ctx->block_dup_map,
3534 global_ctx->block_ea_map);
3535 e2fsck_pass1_block_map_w_unlock(thread_ctx);
3536 if (retval == EEXIST)
3537 global_ctx->flags |= E2F_FLAG_DUP_BLOCK;
3542 static int e2fsck_pass1_thread_join(e2fsck_t global_ctx, e2fsck_t thread_ctx)
3546 retval = e2fsck_pass1_merge_context(global_ctx, thread_ctx);
3547 ext2fs_free_mem(&thread_ctx->fs);
3548 if (thread_ctx->logf)
3549 fclose(thread_ctx->logf);
3550 if (thread_ctx->problem_logf) {
3551 fputs("</problem_log>\n", thread_ctx->problem_logf);
3552 fclose(thread_ctx->problem_logf);
3555 quota_release_context(&thread_ctx->qctx);
3557 * @block_metadata_map and @block_dup_map are
3558 * shared, so we don't free them.
3560 thread_ctx->block_metadata_map = NULL;
3561 thread_ctx->block_dup_map = NULL;
3562 e2fsck_reset_context(thread_ctx);
3563 ext2fs_free_mem(&thread_ctx);
3568 static int e2fsck_pass1_threads_join(e2fsck_t global_ctx)
3572 struct e2fsck_thread_info *infos = global_ctx->infos;
3573 struct e2fsck_thread_info *pinfo;
3574 int num_threads = global_ctx->pfs_num_threads;
3577 /* merge invalid bitmaps will recalculate it */
3578 global_ctx->invalid_bitmaps = 0;
3579 for (i = 0; i < num_threads; i++) {
3582 if (!pinfo->eti_started)
3585 rc = pthread_join(pinfo->eti_thread_id, NULL);
3587 com_err(global_ctx->program_name, rc,
3588 _("while joining thread\n"));
3592 rc = e2fsck_pass1_thread_join(global_ctx, infos[i].eti_thread_ctx);
3594 com_err(global_ctx->program_name, rc,
3595 _("while joining pass1 thread\n"));
3601 global_ctx->infos = NULL;
3606 static void *e2fsck_pass1_thread(void *arg)
3608 struct e2fsck_thread_info *info = arg;
3609 e2fsck_t thread_ctx = info->eti_thread_ctx;
3610 #ifdef DEBUG_THREADS
3611 struct e2fsck_thread_debug *thread_debug = info->eti_debug;
3614 #ifdef DEBUG_THREADS
3615 pthread_mutex_lock(&thread_debug->etd_mutex);
3616 while (info->eti_thread_index > thread_debug->etd_finished_threads) {
3617 pthread_cond_wait(&thread_debug->etd_cond,
3618 &thread_debug->etd_mutex);
3620 pthread_mutex_unlock(&thread_debug->etd_mutex);
3623 #ifdef HAVE_SETJMP_H
3625 * When fatal_error() happens, jump to here. The thread
3626 * context's flags will be saved, but its abort_loc will
3627 * be overwritten by original jump buffer for the later
3630 if (setjmp(thread_ctx->abort_loc)) {
3631 thread_ctx->flags &= ~E2F_FLAG_SETJMP_OK;
3634 thread_ctx->flags |= E2F_FLAG_SETJMP_OK;
3637 e2fsck_pass1_run(thread_ctx);
3640 if (thread_ctx->options & E2F_OPT_MULTITHREAD)
3642 _("Scanned group range [%u, %u), inodes %u/%u\n"),
3643 thread_ctx->thread_info.et_group_start,
3644 thread_ctx->thread_info.et_group_end,
3645 thread_ctx->thread_info.et_inode_count,
3646 thread_ctx->thread_info.et_inode_number);
3648 #ifdef DEBUG_THREADS
3649 pthread_mutex_lock(&thread_debug->etd_mutex);
3650 thread_debug->etd_finished_threads++;
3651 pthread_cond_broadcast(&thread_debug->etd_cond);
3652 pthread_mutex_unlock(&thread_debug->etd_mutex);
3658 static dgrp_t ext2fs_get_avg_group(ext2_filsys fs)
3660 dgrp_t average_group = fs->group_desc_count;
3662 unsigned flexbg_size;
3664 if (fs->fs_num_threads <= 1)
3667 average_group = fs->group_desc_count / fs->fs_num_threads;
3668 if (average_group <= 1)
3671 if (ext2fs_has_feature_flex_bg(fs->super)) {
3674 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3675 if (average_group % flexbg_size) {
3676 times = average_group / flexbg_size;
3677 average_group = times * flexbg_size;
3682 return average_group;
3685 static dgrp_t ext2fs_get_avg_inodes(ext2_filsys fs)
3687 ext2_ino_t average_inodes = fs->super->s_inodes_count;
3690 if (fs->fs_num_threads <= 1)
3693 average_inodes = fs->super->s_inodes_count / fs->fs_num_threads;
3694 if (average_inodes <= fs->super->s_inodes_per_group)
3695 average_inodes = fs->super->s_inodes_per_group;
3699 return average_inodes;
3702 static int e2fsck_pass1_threads_start(e2fsck_t global_ctx)
3704 struct e2fsck_thread_info *infos;
3705 pthread_attr_t attr;
3708 struct e2fsck_thread_info *tmp_pinfo;
3710 e2fsck_t thread_ctx;
3712 dgrp_t average_group;
3713 ext2_ino_t average_inodes;
3715 int num_threads = global_ctx->pfs_num_threads;
3716 #ifdef DEBUG_THREADS
3717 struct e2fsck_thread_debug thread_debug =
3718 {PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, 0};
3720 thread_debug.etd_finished_threads = 0;
3723 retval = pthread_attr_init(&attr);
3725 com_err(global_ctx->program_name, retval,
3726 _("while setting pthread attribute\n"));
3730 infos = calloc(num_threads, sizeof(struct e2fsck_thread_info));
3731 if (infos == NULL) {
3733 com_err(global_ctx->program_name, retval,
3734 _("while allocating memory for threads\n"));
3735 pthread_attr_destroy(&attr);
3738 global_ctx->infos = infos;
3740 average_group = ext2fs_get_avg_group(global_ctx->fs);
3741 average_inodes = ext2fs_get_avg_inodes(global_ctx->fs);
3742 for (thread = 0, start_group = 0, inode_count = 0;
3743 thread < num_threads; thread++) {
3744 tmp_pinfo = &infos[thread];
3745 tmp_pinfo->eti_thread_index = thread;
3746 #ifdef DEBUG_THREADS
3747 tmp_pinfo->eti_debug = &thread_debug;
3749 retval = e2fsck_pass1_thread_prepare(global_ctx, &thread_ctx,
3750 thread, average_group,
3752 &start_group, &inode_count,
3753 thread == num_threads - 1);
3755 com_err(global_ctx->program_name, retval,
3756 _("while preparing pass1 thread\n"));
3759 tmp_pinfo->eti_thread_ctx = thread_ctx;
3761 retval = pthread_create(&tmp_pinfo->eti_thread_id, &attr,
3762 &e2fsck_pass1_thread, tmp_pinfo);
3764 com_err(global_ctx->program_name, retval,
3765 _("while creating thread\n"));
3766 e2fsck_pass1_thread_join(global_ctx, thread_ctx);
3770 tmp_pinfo->eti_started = 1;
3773 /* destroy the thread attribute object, since it is no longer needed */
3774 ret = pthread_attr_destroy(&attr);
3776 com_err(global_ctx->program_name, ret,
3777 _("while destroying thread attribute\n"));
3783 e2fsck_pass1_threads_join(global_ctx);
3789 static void e2fsck_pass1_multithread(e2fsck_t global_ctx)
3793 retval = e2fsck_pass1_threads_start(global_ctx);
3795 com_err(global_ctx->program_name, retval,
3796 _("while starting pass1 threads\n"));
3800 retval = e2fsck_pass1_threads_join(global_ctx);
3802 com_err(global_ctx->program_name, retval,
3803 _("while joining pass1 threads\n"));
3808 global_ctx->flags |= E2F_FLAG_ABORT;
3813 void e2fsck_pass1(e2fsck_t ctx)
3816 int need_single = 1;
3818 retval = e2fsck_pass1_prepare(ctx);
3822 if (ctx->pfs_num_threads > 1 || ctx->options & E2F_OPT_MULTITHREAD) {
3824 e2fsck_pass1_multithread(ctx);
3826 /* No lock is needed at this time */
3827 ctx->fs_need_locking = 0;
3830 e2fsck_pass1_run(ctx);
3831 e2fsck_pass1_post(ctx);
3834 #undef FINISH_INODE_LOOP
3837 * When the inode_scan routines call this callback at the end of the
3838 * block group, call process_inodes.
3840 static errcode_t scan_callback(ext2_filsys fs,
3841 ext2_inode_scan scan EXT2FS_ATTR((unused)),
3842 dgrp_t group, void * priv_data)
3844 struct scan_callback_struct *scan_struct;
3846 dgrp_t cur = group + 1;
3847 struct e2fsck_thread *tinfo;
3848 struct e2fsck_thread_info *pinfo, *infos;
3851 scan_struct = (struct scan_callback_struct *) priv_data;
3852 ctx = scan_struct->ctx;
3854 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf,
3855 scan_struct->inodes_to_process,
3856 scan_struct->process_inode_count);
3859 if (ctx->global_ctx) {
3861 infos = ctx->global_ctx->infos;
3862 for (i = 0; i < ctx->global_ctx->pfs_num_threads; i++) {
3865 if (!pinfo->eti_started)
3868 tinfo = &pinfo->eti_thread_ctx->thread_info;
3869 if (ctx == pinfo->eti_thread_ctx)
3870 cur += group + 1 - tinfo->et_group_start;
3872 cur += tinfo->et_group_next -
3873 tinfo->et_group_start;
3879 if ((ctx->progress)(ctx, 1, cur,
3880 ctx->fs->group_desc_count))
3881 return EXT2_ET_CANCEL_REQUESTED;
3884 if (ctx->global_ctx) {
3885 tinfo = &ctx->thread_info;
3886 if (ctx->options & E2F_OPT_DEBUG &&
3887 ctx->options & E2F_OPT_MULTITHREAD)
3888 log_out(ctx, _("group %d finished\n"),
3889 tinfo->et_group_next);
3890 tinfo->et_group_next++;
3891 if (tinfo->et_group_next >= tinfo->et_group_end)
3892 return EXT2_ET_SCAN_FINISHED;
3900 * Process the inodes in the "inodes to process" list.
3902 static void process_inodes(e2fsck_t ctx, char *block_buf,
3903 struct process_inode_block *inodes_to_process,
3904 int *process_inode_count)
3907 struct ext2_inode *old_stashed_inode;
3908 ext2_ino_t old_stashed_ino;
3909 const char *old_operation;
3911 struct problem_context pctx;
3914 printf("begin process_inodes: ");
3916 if (*process_inode_count == 0)
3918 old_operation = ehandler_operation(0);
3919 old_stashed_inode = ctx->stashed_inode;
3920 old_stashed_ino = ctx->stashed_ino;
3921 qsort(inodes_to_process, *process_inode_count,
3922 sizeof(struct process_inode_block), process_inode_cmp);
3923 clear_problem_context(&pctx);
3924 for (i=0; i < *process_inode_count; i++) {
3925 pctx.inode = ctx->stashed_inode =
3926 (struct ext2_inode *) &inodes_to_process[i].inode;
3927 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
3930 printf("%u ", pctx.ino);
3932 sprintf(buf, _("reading indirect blocks of inode %u"),
3934 ehandler_operation(buf);
3935 check_blocks(ctx, &pctx, block_buf,
3936 &inodes_to_process[i].ea_ibody_quota);
3937 if (e2fsck_should_abort(ctx))
3940 ctx->stashed_inode = old_stashed_inode;
3941 ctx->stashed_ino = old_stashed_ino;
3942 *process_inode_count = 0;
3944 printf("end process inodes\n");
3946 ehandler_operation(old_operation);
3949 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
3951 const struct process_inode_block *ib_a =
3952 (const struct process_inode_block *) a;
3953 const struct process_inode_block *ib_b =
3954 (const struct process_inode_block *) b;
3957 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
3958 ib_b->inode.i_block[EXT2_IND_BLOCK]);
3961 * We only call process_inodes() for non-extent
3962 * inodes, so it's OK to pass NULL to
3963 * ext2fs_file_acl_block() here.
3965 ret = ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a->inode)) -
3966 ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b->inode));
3968 ret = ib_a->ino - ib_b->ino;
3973 * Mark an inode as being bad and increment its badness counter.
3975 void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, struct problem_context *pctx,
3976 __u32 code, int badness, const char *func,
3979 __u16 badness_before, badness_after;
3981 if (!ctx->inode_badness_threshold) /* badness is disabled */
3984 if (!ctx->inode_badness) {
3987 retval = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
3988 &ctx->inode_badness);
3990 pctx->errcode = retval;
3991 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, pctx);
3992 ctx->flags |= E2F_FLAG_ABORT;
3996 ext2fs_icount_fetch(ctx->inode_badness, pctx->ino, &badness_before);
3997 if (badness + badness_before > BADNESS_MAX)
3998 badness_after = BADNESS_MAX;
3999 else if (badness < 0 && badness_before < -badness)
4002 badness_after = badness_before + badness;
4003 ext2fs_icount_store(ctx->inode_badness, pctx->ino, badness_after);
4005 if (ctx->options & E2F_OPT_DEBUG)
4007 "%s:%d: increase inode %lu badness %u to %u for %x\n",
4008 func, line, (unsigned long)pctx->ino, badness_before,
4009 badness_after, code);
4012 static void add_casefolded_dir(e2fsck_t ctx, ext2_ino_t ino)
4014 struct problem_context pctx;
4016 if (!ctx->casefolded_dirs) {
4017 pctx.errcode = ext2fs_u32_list_create(&ctx->casefolded_dirs, 0);
4021 pctx.errcode = ext2fs_u32_list_add(ctx->casefolded_dirs, ino);
4022 if (pctx.errcode == 0)
4025 fix_problem(ctx, PR_1_ALLOCATE_CASEFOLDED_DIRLIST, &pctx);
4026 /* Should never get here */
4027 ctx->flags |= E2F_FLAG_ABORT;
4031 * This procedure will allocate the inode "bb" (badblock) map table
4033 static void alloc_bb_map(e2fsck_t ctx)
4035 struct problem_context pctx;
4037 clear_problem_context(&pctx);
4038 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
4039 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
4040 "inode_bb_map", &ctx->inode_bb_map);
4043 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
4044 /* Should never get here */
4045 ctx->flags |= E2F_FLAG_ABORT;
4051 * This procedure will allocate the inode imagic table
4053 static void alloc_imagic_map(e2fsck_t ctx)
4055 struct problem_context pctx;
4057 clear_problem_context(&pctx);
4058 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
4059 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
4060 "inode_imagic_map", &ctx->inode_imagic_map);
4063 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
4064 /* Should never get here */
4065 ctx->flags |= E2F_FLAG_ABORT;
4071 * Marks a block as in use, setting the dup_map if it's been set
4072 * already. Called by process_block and process_bad_block.
4074 * WARNING: Assumes checks have already been done to make sure block
4075 * is valid. This is true in both process_block and process_bad_block.
4077 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
4079 struct problem_context pctx;
4080 e2fsck_t global_ctx = ctx->global_ctx ? ctx->global_ctx : ctx;
4082 clear_problem_context(&pctx);
4084 if (is_blocks_used(ctx, block, 1)) {
4085 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
4086 !(ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
4089 ctx->flags |= E2F_FLAG_DUP_BLOCK;
4090 e2fsck_pass1_block_map_w_lock(ctx);
4091 ext2fs_fast_mark_block_bitmap2(global_ctx->block_dup_map, block);
4092 e2fsck_pass1_block_map_w_unlock(ctx);
4094 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
4099 * When cluster size is greater than one block, it is caller's responsibility
4100 * to make sure block parameter starts at a cluster boundary.
4102 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
4105 if (!is_blocks_used(ctx, block, num)) {
4106 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
4110 for (i = 0; i < num; i += EXT2FS_CLUSTER_RATIO(ctx->fs))
4111 mark_block_used(ctx, block + i);
4115 static errcode_t _INLINE_ e2fsck_write_ext_attr3(e2fsck_t ctx, blk64_t block,
4116 void *inbuf, ext2_ino_t inum)
4119 ext2_filsys fs = ctx->fs;
4121 e2fsck_pass1_fix_lock(ctx);
4122 retval = ext2fs_write_ext_attr3(fs, block, inbuf, inum);
4123 e2fsck_pass1_fix_unlock(ctx);
4128 * Adjust the extended attribute block's reference counts at the end
4129 * of pass 1, either by subtracting out references for EA blocks that
4130 * are still referenced in ctx->refcount, or by adding references for
4131 * EA blocks that had extra references as accounted for in
4132 * ctx->refcount_extra.
4134 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
4135 char *block_buf, int adjust_sign)
4137 struct ext2_ext_attr_header *header;
4138 struct problem_context pctx;
4139 ext2_filsys fs = ctx->fs;
4144 clear_problem_context(&pctx);
4146 ea_refcount_intr_begin(refcount);
4148 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
4151 pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
4153 /* We already checked this block, shouldn't happen */
4155 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
4158 header = BHDR(block_buf);
4159 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
4160 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
4164 pctx.blkcount = header->h_refcount;
4165 should_be = header->h_refcount + adjust_sign * (int)count;
4166 pctx.num = should_be;
4167 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
4168 header->h_refcount = should_be;
4169 pctx.errcode = e2fsck_write_ext_attr3(ctx, blk,
4173 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
4182 * Handle processing the extended attribute blocks
4184 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
4185 char *block_buf, struct ea_quota *ea_block_quota)
4187 ext2_filsys fs = ctx->fs;
4188 ext2_ino_t ino = pctx->ino;
4189 struct ext2_inode *inode = pctx->inode;
4192 struct ext2_ext_attr_header *header;
4193 struct ext2_ext_attr_entry *first, *entry;
4194 blk64_t quota_blocks = EXT2FS_C2B(fs, 1);
4195 __u64 quota_inodes = 0;
4196 region_t region = 0;
4197 int failed_csum = 0;
4199 ea_block_quota->blocks = 0;
4200 ea_block_quota->inodes = 0;
4202 blk = ext2fs_file_acl_block(fs, inode);
4207 * If the Extended attribute flag isn't set, then a non-zero
4208 * file acl means that the inode is corrupted.
4210 * Or if the extended attribute block is an invalid block,
4211 * then the inode is also corrupted.
4213 if (!ext2fs_has_feature_xattr(fs->super) ||
4214 (blk < fs->super->s_first_data_block) ||
4215 (blk >= ext2fs_blocks_count(fs->super))) {
4216 /* Fixed in pass2, e2fsck_process_bad_inode(). */
4217 e2fsck_mark_inode_bad(ctx, pctx, PR_2_FILE_ACL_ZERO);
4221 /* If ea bitmap hasn't been allocated, create it */
4222 if (!ctx->block_ea_map) {
4223 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
4224 _("ext attr block map"),
4225 EXT2FS_BMAP64_RBTREE, "block_ea_map",
4226 &ctx->block_ea_map);
4227 if (pctx->errcode) {
4229 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
4230 ctx->flags |= E2F_FLAG_ABORT;
4235 /* Create the EA refcount structure if necessary */
4236 if (!ctx->refcount) {
4237 pctx->errcode = ea_refcount_create(&ctx->refcount_orig);
4238 if (pctx->errcode) {
4240 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4241 ctx->flags |= E2F_FLAG_ABORT;
4245 pctx->errcode = ea_refcount_create(&ctx->refcount);
4246 if (pctx->errcode) {
4248 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4249 ctx->flags |= E2F_FLAG_ABORT;
4255 /* Debugging text */
4256 printf("Inode %u has EA block %u\n", ino, blk);
4259 /* Have we seen this EA block before? */
4260 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
4261 ea_block_quota->blocks = EXT2FS_C2B(fs, 1);
4262 ea_block_quota->inodes = 0;
4264 if (ctx->ea_block_quota_blocks) {
4265 ea_refcount_fetch(ctx->ea_block_quota_blocks, blk,
4268 ea_block_quota->blocks = quota_blocks;
4271 if (ctx->ea_block_quota_inodes)
4272 ea_refcount_fetch(ctx->ea_block_quota_inodes, blk,
4273 &ea_block_quota->inodes);
4275 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
4277 /* Ooops, this EA was referenced more than it stated */
4278 if (!ctx->refcount_extra) {
4279 pctx->errcode = ea_refcount_create(&ctx->refcount_extra);
4280 if (pctx->errcode) {
4282 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4283 ctx->flags |= E2F_FLAG_ABORT;
4287 ea_refcount_increment(ctx->refcount_extra, blk, 0);
4292 * OK, we haven't seen this EA block yet. So we need to
4296 pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
4297 if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
4300 } else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
4303 if (pctx->errcode &&
4304 fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
4308 header = BHDR(block_buf);
4309 pctx->blk = ext2fs_file_acl_block(fs, inode);
4310 if (((ctx->ext_attr_ver == 1) &&
4311 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
4312 ((ctx->ext_attr_ver == 2) &&
4313 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
4314 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
4318 if (header->h_blocks != 1) {
4319 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
4323 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
4326 region = region_create(0, fs->blocksize);
4328 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
4329 ctx->flags |= E2F_FLAG_ABORT;
4332 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
4333 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4337 first = (struct ext2_ext_attr_entry *)(header+1);
4338 end = block_buf + fs->blocksize;
4340 while ((char *)entry < end && *(__u32 *)entry) {
4343 if (region_allocate(region, (char *)entry - (char *)header,
4344 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
4345 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4349 if ((ctx->ext_attr_ver == 1 &&
4350 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
4351 (ctx->ext_attr_ver == 2 &&
4352 entry->e_name_index == 0)) {
4353 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
4357 if (entry->e_value_inum == 0) {
4358 if (entry->e_value_size > EXT2_XATTR_SIZE_MAX ||
4359 (entry->e_value_offs + entry->e_value_size >
4361 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
4365 if (entry->e_value_size &&
4366 region_allocate(region, entry->e_value_offs,
4367 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
4368 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
4373 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
4374 entry->e_value_offs);
4375 if (entry->e_hash != hash)
4376 hash = ext2fs_ext_attr_hash_entry_signed(entry,
4377 block_buf + entry->e_value_offs);
4379 if (entry->e_hash != hash) {
4380 pctx->num = entry->e_hash;
4381 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
4383 entry->e_hash = hash;
4387 blk64_t entry_quota_blocks;
4389 problem = check_large_ea_inode(ctx, entry, pctx,
4390 &entry_quota_blocks);
4391 if (problem && fix_problem(ctx, problem, pctx))
4394 quota_blocks += entry_quota_blocks;
4398 entry = EXT2_EXT_ATTR_NEXT(entry);
4400 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
4401 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
4404 region_free(region);
4407 * We only get here if there was no other errors that were fixed.
4408 * If there was a checksum fail, ask to correct it.
4411 fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
4412 pctx->errcode = e2fsck_write_ext_attr3(ctx, blk, block_buf,
4418 if (quota_blocks != EXT2FS_C2B(fs, 1U)) {
4419 if (!ctx->ea_block_quota_blocks) {
4420 pctx->errcode = ea_refcount_create(&ctx->ea_block_quota_blocks);
4421 if (pctx->errcode) {
4426 ea_refcount_store(ctx->ea_block_quota_blocks, blk,
4431 if (!ctx->ea_block_quota_inodes) {
4432 pctx->errcode = ea_refcount_create(&ctx->ea_block_quota_inodes);
4433 if (pctx->errcode) {
4436 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
4437 ctx->flags |= E2F_FLAG_ABORT;
4442 ea_refcount_store(ctx->ea_block_quota_inodes, blk,
4445 ea_block_quota->blocks = quota_blocks;
4446 ea_block_quota->inodes = quota_inodes;
4448 inc_ea_inode_refs(ctx, pctx, first, end);
4449 ea_refcount_store(ctx->refcount, blk, header->h_refcount - 1);
4450 ea_refcount_store(ctx->refcount_orig, blk, header->h_refcount);
4452 * It might be racy that this block has been merged in the
4455 if (!is_blocks_used(ctx, blk, 1))
4456 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, blk);
4457 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
4462 region_free(region);
4463 ext2fs_file_acl_block_set(fs, inode, 0);
4464 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
4468 /* Returns 1 if bad htree, 0 if OK */
4469 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
4470 ext2_ino_t ino, struct ext2_inode *inode,
4473 struct ext2_dx_root_info *root;
4474 ext2_filsys fs = ctx->fs;
4478 if ((!LINUX_S_ISDIR(inode->i_mode) &&
4479 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
4480 (!ext2fs_has_feature_dir_index(fs->super) &&
4481 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
4484 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
4486 if ((pctx->errcode) ||
4488 (blk < fs->super->s_first_data_block) ||
4489 (blk >= ext2fs_blocks_count(fs->super))) {
4490 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4496 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
4498 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4502 /* XXX should check that beginning matches a directory */
4503 root = get_ext2_dx_root_info(fs, block_buf);
4505 if ((root->reserved_zero || root->info_length < 8) &&
4506 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
4509 pctx->num = root->hash_version;
4510 if ((root->hash_version != EXT2_HASH_LEGACY) &&
4511 (root->hash_version != EXT2_HASH_HALF_MD4) &&
4512 (root->hash_version != EXT2_HASH_TEA) &&
4513 (root->hash_version != EXT2_HASH_SIPHASH) &&
4514 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
4517 if (ext4_hash_in_dirent(inode)) {
4518 if (root->hash_version != EXT2_HASH_SIPHASH &&
4519 fix_problem(ctx, PR_1_HTREE_NEEDS_SIPHASH, pctx))
4522 if (root->hash_version == EXT2_HASH_SIPHASH &&
4523 fix_problem(ctx, PR_1_HTREE_CANNOT_SIPHASH, pctx))
4527 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
4528 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
4531 pctx->num = root->indirect_levels;
4532 /* if htree level is clearly too high, consider it to be broken */
4533 if (root->indirect_levels > EXT4_HTREE_LEVEL &&
4534 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4537 /* if level is only maybe too high, LARGE_DIR feature could be unset */
4538 if (root->indirect_levels > ext2_dir_htree_level(fs) &&
4539 !ext2fs_has_feature_largedir(fs->super)) {
4540 int blockbits = EXT2_BLOCK_SIZE_BITS(fs->super) + 10;
4541 unsigned idx_pb = 1 << (blockbits - 3);
4543 /* compare inode size/blocks vs. max-sized 2-level htree */
4544 if (EXT2_I_SIZE(pctx->inode) <
4545 (idx_pb - 1) * (idx_pb - 2) << blockbits &&
4546 pctx->inode->i_blocks <
4547 (idx_pb - 1) * (idx_pb - 2) << (blockbits - 9) &&
4548 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
4552 if (root->indirect_levels > EXT4_HTREE_LEVEL_COMPAT ||
4553 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
4559 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
4560 struct ext2_inode *inode, int restart_flag,
4564 inode->i_links_count = 0;
4565 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
4566 inode->i_dtime = ctx->now;
4569 * If a special inode has such rotten block mappings that we
4570 * want to clear the whole inode, be sure to actually zap
4571 * the block maps because i_links_count isn't checked for
4572 * special inodes, and we'll end up right back here the next
4575 if (ino < EXT2_FIRST_INODE(ctx->fs->super))
4576 memset(inode->i_block, 0, sizeof(inode->i_block));
4578 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
4579 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
4580 if (ctx->inode_reg_map)
4581 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
4582 if (ctx->inode_badness)
4583 ext2fs_icount_store(ctx->inode_badness, ino, 0);
4586 * If the inode was partially accounted for before processing
4587 * was aborted, we need to restart the pass 1 scan.
4589 ctx->flags |= restart_flag;
4591 if (ino == EXT2_BAD_INO)
4592 memset(inode, 0, sizeof(struct ext2_inode));
4594 e2fsck_write_inode(ctx, ino, inode, source);
4598 * Use the multiple-blocks reclamation code to fix alignment problems in
4599 * a bigalloc filesystem. We want a logical cluster to map to *only* one
4600 * physical cluster, and we want the block offsets within that cluster to
4603 static int has_unaligned_cluster_map(e2fsck_t ctx,
4604 blk64_t last_pblk, blk64_t last_lblk,
4605 blk64_t pblk, blk64_t lblk)
4607 blk64_t cluster_mask;
4609 if (!ctx->fs->cluster_ratio_bits)
4611 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
4614 * If the block in the logical cluster doesn't align with the block in
4615 * the physical cluster...
4617 if ((lblk & cluster_mask) != (pblk & cluster_mask))
4621 * If we cross a physical cluster boundary within a logical cluster...
4623 if (last_pblk && (lblk & cluster_mask) != 0 &&
4624 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
4625 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
4631 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
4632 struct process_block_struct *pb,
4633 blk64_t start_block, blk64_t end_block,
4635 ext2_extent_handle_t ehandle,
4638 struct ext2fs_extent extent;
4639 blk64_t blk, last_lblk;
4641 int is_dir, is_leaf;
4643 struct ext2_extent_info info;
4644 int failed_csum = 0;
4646 if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
4649 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
4652 if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
4653 !pb->eti.force_rebuild &&
4654 info.curr_level < MAX_EXTENT_DEPTH_COUNT) {
4655 struct extent_tree_level *etl;
4657 etl = pb->eti.ext_info + info.curr_level;
4658 etl->num_extents += info.num_entries;
4659 etl->max_extents += info.max_entries;
4661 * Implementation wart: Splitting extent blocks when appending
4662 * will leave the old block with one free entry. Therefore
4663 * unless the node is totally full, pretend that a non-root
4664 * extent block can hold one fewer entry than it actually does,
4665 * so that we don't repeatedly rebuild the extent tree.
4667 if (info.curr_level && info.num_entries < info.max_entries)
4671 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
4673 while ((pctx->errcode == 0 ||
4674 pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
4675 info.num_entries-- > 0) {
4676 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
4677 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
4678 last_lblk = extent.e_lblk + extent.e_len - 1;
4681 pctx->blk = extent.e_pblk;
4682 pctx->blk2 = extent.e_lblk;
4683 pctx->num = extent.e_len;
4684 pctx->blkcount = extent.e_lblk + extent.e_len;
4686 if (extent.e_pblk == 0 ||
4687 extent.e_pblk < ctx->fs->super->s_first_data_block ||
4688 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
4689 problem = PR_1_EXTENT_BAD_START_BLK;
4690 else if (extent.e_lblk < start_block)
4691 problem = PR_1_OUT_OF_ORDER_EXTENTS;
4692 else if ((end_block && last_lblk > end_block) &&
4693 !(last_lblk > eof_block &&
4694 ((extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) ||
4695 (pctx->inode->i_flags & EXT4_VERITY_FL))))
4696 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
4697 else if (is_leaf && extent.e_len == 0)
4698 problem = PR_1_EXTENT_LENGTH_ZERO;
4700 (extent.e_pblk + extent.e_len) >
4701 ext2fs_blocks_count(ctx->fs->super))
4702 problem = PR_1_EXTENT_ENDS_BEYOND;
4703 else if (is_leaf && is_dir && !pctx->inode->i_size_high &&
4704 !ext2fs_has_feature_largedir(ctx->fs->super) &&
4705 ((extent.e_lblk + extent.e_len) >
4706 (1U << (21 - ctx->fs->super->s_log_block_size))))
4707 problem = PR_1_TOOBIG_DIR;
4709 if (is_leaf && problem == 0 && extent.e_len > 0) {
4711 printf("extent_region(ino=%u, expect=%llu, "
4712 "lblk=%llu, len=%u)\n", pb->ino,
4713 (unsigned long long) pb->next_lblock,
4714 (unsigned long long) extent.e_lblk,
4717 if (extent.e_lblk < pb->next_lblock)
4718 problem = PR_1_EXTENT_COLLISION;
4719 else if (extent.e_lblk + extent.e_len > pb->next_lblock)
4720 pb->next_lblock = extent.e_lblk + extent.e_len;
4724 * Uninitialized blocks in a directory? Clear the flag and
4725 * we'll interpret the blocks later.
4727 if (try_repairs && is_dir && problem == 0 &&
4728 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4729 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
4730 e2fsck_pass1_fix_lock(ctx);
4731 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4732 pb->inode_modified = 1;
4733 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4735 e2fsck_pass1_fix_unlock(ctx);
4740 #ifdef CONFIG_DEVELOPER_FEATURES
4741 if (try_repairs && !is_dir && problem == 0 &&
4742 (ctx->options & E2F_OPT_CLEAR_UNINIT) &&
4743 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
4744 fix_problem(ctx, PR_1_CLEAR_UNINIT_EXTENT, pctx)) {
4745 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
4746 pb->inode_modified = 1;
4747 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
4754 if (try_repairs && problem) {
4756 /* Record badness only if extent is within inode */
4757 if (fix_problem_bad(ctx, problem, pctx,
4758 info.curr_level == 0)) {
4759 if (ctx->invalid_bitmaps) {
4761 * If fsck knows the bitmaps are bad,
4762 * skip to the next extent and
4763 * try to clear this extent again
4764 * after fixing the bitmaps, by
4767 pctx->errcode = ext2fs_extent_get(
4769 EXT2_EXTENT_NEXT_SIB,
4771 ctx->flags |= E2F_FLAG_RESTART_LATER;
4772 if (pctx->errcode ==
4773 EXT2_ET_NO_CURRENT_NODE) {
4779 e2fsck_pass1_fix_lock(ctx);
4780 e2fsck_read_bitmaps(ctx);
4781 pb->inode_modified = 1;
4783 ext2fs_extent_delete(ehandle, 0);
4784 e2fsck_pass1_fix_unlock(ctx);
4785 if (pctx->errcode) {
4786 pctx->str = "ext2fs_extent_delete";
4789 e2fsck_pass1_fix_lock(ctx);
4790 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4791 e2fsck_pass1_fix_unlock(ctx);
4792 if (pctx->errcode &&
4793 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
4794 pctx->str = "ext2fs_extent_fix_parents";
4797 pctx->errcode = ext2fs_extent_get(ehandle,
4798 EXT2_EXTENT_CURRENT,
4800 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
4811 blk64_t lblk = extent.e_lblk;
4812 int next_try_repairs = 1;
4814 blk = extent.e_pblk;
4817 * If this lower extent block collides with critical
4818 * metadata, don't try to repair the damage. Pass 1b
4819 * will reallocate the block; then we can try again.
4821 if (pb->ino != EXT2_RESIZE_INO &&
4822 extent.e_pblk < ctx->fs->super->s_blocks_count &&
4823 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
4825 next_try_repairs = 0;
4827 fix_problem_bad(ctx,
4828 PR_1_CRITICAL_METADATA_COLLISION,
4830 if ((ctx->options & E2F_OPT_NO) == 0)
4831 ctx->flags |= E2F_FLAG_RESTART_LATER;
4833 pctx->errcode = ext2fs_extent_get(ehandle,
4834 EXT2_EXTENT_DOWN, &extent);
4835 if (pctx->errcode &&
4836 pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
4837 pctx->str = "EXT2_EXTENT_DOWN";
4838 problem = PR_1_EXTENT_HEADER_INVALID;
4839 if (!next_try_repairs)
4841 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
4842 goto report_problem;
4845 /* The next extent should match this index's logical start */
4846 if (extent.e_lblk != lblk) {
4847 struct ext2_extent_info e_info;
4849 pctx->errcode = ext2fs_extent_get_info(ehandle,
4851 if (pctx->errcode) {
4852 pctx->str = "ext2fs_extent_get_info";
4856 pctx->blk2 = extent.e_lblk;
4857 pctx->num = e_info.curr_level - 1;
4858 problem = PR_1_EXTENT_INDEX_START_INVALID;
4859 if (fix_problem(ctx, problem, pctx)) {
4860 e2fsck_pass1_fix_lock(ctx);
4861 pb->inode_modified = 1;
4863 ext2fs_extent_fix_parents(ehandle);
4864 e2fsck_pass1_fix_unlock(ctx);
4865 if (pctx->errcode) {
4866 pctx->str = "ext2fs_extent_fix_parents";
4871 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
4872 last_lblk, eof_block, ehandle,
4876 pctx->errcode = ext2fs_extent_get(ehandle,
4877 EXT2_EXTENT_UP, &extent);
4878 if (pctx->errcode) {
4879 pctx->str = "EXT2_EXTENT_UP";
4882 mark_block_used(ctx, blk);
4887 if ((pb->previous_block != 0) &&
4888 (pb->previous_block+1 != extent.e_pblk)) {
4889 if (ctx->options & E2F_OPT_FRAGCHECK) {
4894 else if (pb->is_reg)
4897 printf(("%6lu(%c): expecting %6lu "
4899 "phys %6lu log %lu len %lu\n"),
4900 (unsigned long) pctx->ino, type,
4901 (unsigned long) pb->previous_block+1,
4902 (unsigned long) extent.e_pblk,
4903 (unsigned long) extent.e_lblk,
4904 (unsigned long) extent.e_len);
4909 * If we notice a gap in the logical block mappings of an
4910 * extent-mapped directory, offer to close the hole by
4911 * moving the logical block down, otherwise we'll go mad in
4912 * pass 3 allocating empty directory blocks to fill the hole.
4914 if (try_repairs && is_dir &&
4915 pb->last_block + 1 < extent.e_lblk) {
4918 new_lblk = pb->last_block + 1;
4919 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
4920 new_lblk = ((new_lblk +
4921 EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
4922 ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
4924 EXT2FS_CLUSTER_MASK(ctx->fs));
4925 pctx->blk = extent.e_lblk;
4926 pctx->blk2 = new_lblk;
4927 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
4928 e2fsck_pass1_fix_lock(ctx);
4929 extent.e_lblk = new_lblk;
4930 pb->inode_modified = 1;
4931 pctx->errcode = ext2fs_extent_replace(ehandle,
4933 e2fsck_pass1_fix_unlock(ctx);
4934 if (pctx->errcode) {
4938 e2fsck_pass1_fix_lock(ctx);
4939 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
4940 e2fsck_pass1_fix_unlock(ctx);
4942 goto failed_add_dir_block;
4943 pctx->errcode = ext2fs_extent_goto(ehandle,
4946 goto failed_add_dir_block;
4947 last_lblk = extent.e_lblk + extent.e_len - 1;
4953 while (++pb->last_db_block <
4954 (e2_blkcnt_t) extent.e_lblk) {
4955 pctx->errcode = ext2fs_add_dir_block2(
4959 if (pctx->errcode) {
4961 pctx->num = pb->last_db_block;
4962 goto failed_add_dir_block;
4966 for (i = 0; i < extent.e_len; i++) {
4967 pctx->errcode = ext2fs_add_dir_block2(
4972 if (pctx->errcode) {
4973 pctx->blk = extent.e_pblk + i;
4974 pctx->num = extent.e_lblk + i;
4975 failed_add_dir_block:
4976 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
4977 /* Should never get here */
4978 ctx->flags |= E2F_FLAG_ABORT;
4982 if (extent.e_len > 0)
4983 pb->last_db_block = extent.e_lblk + extent.e_len - 1;
4985 if (has_unaligned_cluster_map(ctx, pb->previous_block,
4989 for (i = 0; i < extent.e_len; i++) {
4990 pctx->blk = extent.e_lblk + i;
4991 pctx->blk2 = extent.e_pblk + i;
4992 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
4993 mark_block_used(ctx, extent.e_pblk + i);
4994 mark_block_used(ctx, extent.e_pblk + i);
4999 * Check whether first cluster got marked in previous iteration.
5001 if (ctx->fs->cluster_ratio_bits &&
5002 pb->previous_block &&
5003 (EXT2FS_B2C(ctx->fs, extent.e_pblk) ==
5004 EXT2FS_B2C(ctx->fs, pb->previous_block)))
5005 /* Set blk to the beginning of next cluster. */
5008 EXT2FS_B2C(ctx->fs, extent.e_pblk) + 1);
5010 /* Set blk to the beginning of current cluster. */
5011 blk = EXT2FS_C2B(ctx->fs,
5012 EXT2FS_B2C(ctx->fs, extent.e_pblk));
5014 if (blk < extent.e_pblk + extent.e_len) {
5015 mark_blocks_used(ctx, blk,
5016 extent.e_pblk + extent.e_len - blk);
5017 n = DIV_ROUND_UP(extent.e_pblk + extent.e_len - blk,
5018 EXT2FS_CLUSTER_RATIO(ctx->fs));
5019 pb->num_blocks += n;
5021 pb->last_block = extent.e_lblk + extent.e_len - 1;
5022 pb->previous_block = extent.e_pblk + extent.e_len - 1;
5023 start_block = pb->last_block = last_lblk;
5024 if (is_leaf && !is_dir &&
5025 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
5026 pb->last_init_lblock = last_lblk;
5028 pctx->errcode = ext2fs_extent_get(ehandle,
5029 EXT2_EXTENT_NEXT_SIB,
5033 /* Failed csum but passes checks? Ask to fix checksum. */
5035 fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
5036 e2fsck_pass1_fix_lock(ctx);
5037 pb->inode_modified = 1;
5038 pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
5039 e2fsck_pass1_fix_unlock(ctx);
5044 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
5048 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
5049 struct process_block_struct *pb)
5051 struct ext2_extent_info info;
5052 struct ext2_inode *inode = pctx->inode;
5053 ext2_extent_handle_t ehandle;
5054 ext2_filsys fs = ctx->fs;
5055 ext2_ino_t ino = pctx->ino;
5058 struct ext3_extent_header *eh;
5060 /* Check for a proper extent header... */
5061 eh = (struct ext3_extent_header *) &inode->i_block[0];
5062 retval = ext2fs_extent_header_verify(eh, sizeof(inode->i_block));
5064 if (fix_problem(ctx, PR_1_MISSING_EXTENT_HEADER, pctx))
5065 e2fsck_clear_inode(ctx, ino, inode, 0,
5066 "check_blocks_extents");
5071 /* ...since this function doesn't fail if i_block is zeroed. */
5072 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
5073 if (pctx->errcode) {
5074 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
5075 e2fsck_clear_inode(ctx, ino, inode, 0,
5076 "check_blocks_extents");
5081 retval = ext2fs_extent_get_info(ehandle, &info);
5083 int max_depth = info.max_depth;
5085 if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
5086 max_depth = MAX_EXTENT_DEPTH_COUNT-1;
5087 ctx->extent_depth_count[max_depth]++;
5090 /* Check maximum extent depth */
5091 pctx->blk = info.max_depth;
5092 pctx->blk2 = ext2fs_max_extent_depth(ehandle);
5093 if (pctx->blk2 < pctx->blk &&
5094 fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
5095 pb->eti.force_rebuild = 1;
5097 /* Can we collect extent tree level stats? */
5098 pctx->blk = MAX_EXTENT_DEPTH_COUNT;
5099 if (pctx->blk2 > pctx->blk)
5100 fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
5101 memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
5102 pb->eti.ino = pb->ino;
5104 pb->next_lblock = 0;
5106 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
5107 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
5108 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
5109 if (pctx->errcode &&
5110 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
5112 inode->i_blocks = 0;
5113 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
5114 "check_blocks_extents");
5117 ext2fs_extent_free(ehandle);
5119 /* Rebuild unless it's a dir and we're rehashing it */
5120 if (LINUX_S_ISDIR(inode->i_mode) &&
5121 e2fsck_dir_will_be_rehashed(ctx, ino))
5124 if (ctx->options & E2F_OPT_CONVERT_BMAP)
5125 e2fsck_rebuild_extents_later(ctx, ino);
5127 e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
5131 * In fact we don't need to check blocks for an inode with inline data
5132 * because this inode doesn't have any blocks. In this function all
5133 * we need to do is add this inode into dblist when it is a directory.
5135 static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
5136 struct process_block_struct *pb)
5139 size_t inline_data_size = 0;
5146 /* Process the dirents in i_block[] as the "first" block. */
5147 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
5151 /* Process the dirents in the EA as a "second" block. */
5152 flags = ctx->fs->flags;
5153 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5154 pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
5156 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5157 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5158 if (pctx->errcode) {
5163 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
5166 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
5174 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
5175 ctx->flags |= E2F_FLAG_ABORT;
5179 * This subroutine is called on each inode to account for all of the
5180 * blocks used by that inode.
5182 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
5183 char *block_buf, const struct ea_quota *ea_ibody_quota)
5185 ext2_filsys fs = ctx->fs;
5186 struct process_block_struct pb;
5187 ext2_ino_t ino = pctx->ino;
5188 struct ext2_inode *inode = pctx->inode;
5189 unsigned bad_size = 0;
5190 int dirty_inode = 0;
5194 struct ea_quota ea_block_quota;
5197 pb.num_blocks = EXT2FS_B2C(ctx->fs,
5198 ea_ibody_quota ? ea_ibody_quota->blocks : 0);
5200 pb.last_init_lblock = -1;
5201 pb.last_db_block = -1;
5202 pb.num_illegal_blocks = 0;
5203 pb.suppress = 0; pb.clear = 0;
5206 pb.previous_block = 0;
5207 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
5208 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
5209 pb.max_blocks = 1U << (31 - fs->super->s_log_block_size);
5213 pb.inode_modified = 0;
5214 pb.eti.force_rebuild = 0;
5218 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
5219 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
5221 if (check_ext_attr(ctx, pctx, block_buf, &ea_block_quota)) {
5222 if (e2fsck_should_abort(ctx))
5224 pb.num_blocks += EXT2FS_B2C(ctx->fs, ea_block_quota.blocks);
5227 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
5228 check_blocks_inline_data(ctx, pctx, &pb);
5229 else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
5230 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
5231 check_blocks_extents(ctx, pctx, &pb);
5235 * If we've modified the inode, write it out before
5236 * iterate() tries to use it.
5239 e2fsck_write_inode(ctx, ino, inode,
5244 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5245 pctx->errcode = ext2fs_block_iterate3(fs, ino,
5246 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
5247 block_buf, process_block, &pb);
5249 * We do not have uninitialized extents in non extent
5252 pb.last_init_lblock = pb.last_block;
5254 * If iterate() changed a block mapping, we have to
5255 * re-read the inode. If we decide to clear the
5256 * inode after clearing some stuff, we'll re-write the
5257 * bad mappings into the inode!
5259 if (pb.inode_modified)
5260 e2fsck_read_inode(ctx, ino, inode,
5262 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5263 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5265 if (ctx->options & E2F_OPT_CONVERT_BMAP) {
5267 printf("bmap rebuild ino=%d\n", ino);
5269 if (!LINUX_S_ISDIR(inode->i_mode) ||
5270 !e2fsck_dir_will_be_rehashed(ctx, ino))
5271 e2fsck_rebuild_extents_later(ctx, ino);
5275 end_problem_latch(ctx, PR_LATCH_BLOCK);
5276 end_problem_latch(ctx, PR_LATCH_TOOBIG);
5277 if (e2fsck_should_abort(ctx))
5280 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
5282 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
5283 if (LINUX_S_ISDIR(inode->i_mode))
5284 ctx->fs_fragmented_dir++;
5286 ctx->fs_fragmented++;
5290 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
5295 if (inode->i_flags & EXT2_INDEX_FL) {
5296 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
5297 inode->i_flags &= ~EXT2_INDEX_FL;
5300 e2fsck_add_dx_dir(ctx, ino, inode, pb.last_block+1);
5304 if (!pb.num_blocks && pb.is_dir &&
5305 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
5307 * The mode might be in-correct. Increasing the badness by
5308 * small amount won't hurt much.
5310 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
5311 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
5312 ctx->fs_directory_count--;
5317 if (ino != quota_type2inum(PRJQUOTA, fs->super) &&
5318 ino != fs->super->s_orphan_file_inum &&
5319 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) &&
5320 !(inode->i_flags & EXT4_EA_INODE_FL)) {
5321 quota_data_add(ctx->qctx, (struct ext2_inode_large *) inode,
5323 pb.num_blocks * EXT2_CLUSTER_SIZE(fs->super));
5324 quota_data_inodes(ctx->qctx, (struct ext2_inode_large *) inode,
5325 ino, (ea_ibody_quota ?
5326 ea_ibody_quota->inodes : 0) +
5327 ea_block_quota.inodes + 1);
5330 if (!ext2fs_has_feature_huge_file(fs->super) ||
5331 !(inode->i_flags & EXT4_HUGE_FILE_FL))
5332 pb.num_blocks *= (fs->blocksize / 512);
5333 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
5335 printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
5336 ino, inode->i_size, (unsigned long long) pb.last_block,
5337 (unsigned long long) ext2fs_inode_i_blocks(fs, inode),
5338 (unsigned long long) pb.num_blocks);
5340 size = EXT2_I_SIZE(inode);
5342 unsigned nblock = size >> EXT2_BLOCK_SIZE_BITS(fs->super);
5343 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
5348 flags = ctx->fs->flags;
5349 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
5350 err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
5352 ctx->fs->flags = (flags &
5353 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
5355 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
5356 if (err || sz != size) {
5360 } else if (size & (fs->blocksize - 1))
5362 else if (nblock > (pb.last_block + 1))
5364 else if (nblock < (pb.last_block + 1)) {
5365 if (((pb.last_block + 1) - nblock) >
5366 fs->super->s_prealloc_dir_blocks)
5370 if ((pb.last_init_lblock >= 0) &&
5371 /* Do not allow initialized allocated blocks past i_size*/
5372 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
5373 !(inode->i_flags & EXT4_VERITY_FL))
5375 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5376 size > ext2_max_sizes[fs->super->s_log_block_size])
5377 /* too big for a direct/indirect-mapped file */
5379 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
5381 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
5382 /* too big for an extent-based file - 32bit ee_block */
5385 /* i_size for symlinks is checked elsewhere */
5386 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
5387 /* Did inline_data set pctx->num earlier? */
5389 pctx->num = (pb.last_block + 1) * fs->blocksize;
5390 pctx->group = bad_size;
5391 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
5392 ext2fs_inode_size_set(fs, inode, pctx->num);
5393 if (EXT2_I_SIZE(inode) == 0 &&
5394 (inode->i_flags & EXT4_INLINE_DATA_FL)) {
5395 memset(inode->i_block, 0,
5396 sizeof(inode->i_block));
5397 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
5403 if (LINUX_S_ISREG(inode->i_mode) &&
5404 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
5406 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
5407 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
5408 (ext2fs_has_feature_huge_file(fs->super) &&
5409 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
5410 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
5411 pctx->num = pb.num_blocks;
5412 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
5413 inode->i_blocks = pb.num_blocks;
5414 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
5421 * The kernel gets mad if we ask it to allocate bigalloc clusters to
5422 * a block mapped file, so rebuild it as an extent file. We can skip
5423 * symlinks because they're never rewritten.
5425 if (ext2fs_has_feature_bigalloc(fs->super) &&
5426 (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
5427 ext2fs_inode_data_blocks2(fs, inode) > 0 &&
5428 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
5429 !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
5430 fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
5431 pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
5436 if (ctx->dirs_to_hash && pb.is_dir &&
5437 !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
5438 !(inode->i_flags & EXT2_INDEX_FL) &&
5439 ((inode->i_size / fs->blocksize) >= 3))
5440 e2fsck_rehash_dir_later(ctx, ino);
5443 /* need restart if clearing bad inode after block processing */
5444 if (e2fsck_fix_bad_inode(ctx, pctx))
5445 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
5446 "check_blocks_bad");
5447 else if (dirty_inode)
5448 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
5453 * Helper function called by process block when an illegal block is
5454 * found. It returns a description about why the block is illegal
5456 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
5460 static char problem[80];
5462 super = fs->super->s_first_data_block;
5463 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
5464 if (block < super) {
5465 sprintf(problem, "< FIRSTBLOCK (%u)", super);
5467 } else if (block >= ext2fs_blocks_count(fs->super)) {
5468 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
5471 for (i = 0; i < fs->group_desc_count; i++) {
5472 if (block == super) {
5473 sprintf(problem, "is the superblock in group %d", i);
5476 if (block > super &&
5477 block <= (super + fs->desc_blocks)) {
5478 sprintf(problem, "is in the group descriptors "
5482 if (block == ext2fs_block_bitmap_loc(fs, i)) {
5483 sprintf(problem, "is the block bitmap of group %d", i);
5486 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
5487 sprintf(problem, "is the inode bitmap of group %d", i);
5490 if (block >= ext2fs_inode_table_loc(fs, i) &&
5491 (block < ext2fs_inode_table_loc(fs, i)
5492 + fs->inode_blocks_per_group)) {
5493 sprintf(problem, "is in the inode table of group %d",
5497 super += fs->super->s_blocks_per_group;
5504 * This is a helper function for check_blocks().
5506 static int process_block(ext2_filsys fs,
5508 e2_blkcnt_t blockcnt,
5509 blk64_t ref_block EXT2FS_ATTR((unused)),
5510 int ref_offset EXT2FS_ATTR((unused)),
5513 struct process_block_struct *p;
5514 struct problem_context *pctx;
5515 blk64_t blk = *block_nr;
5517 problem_t problem = 0;
5520 p = (struct process_block_struct *) priv_data;
5525 * For a directory, add logical block zero for processing even if it's
5526 * not mapped or we'll be perennially stuck with broken "." and ".."
5529 if (p->is_dir && blockcnt == 0 && blk == 0) {
5530 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
5531 if (pctx->errcode) {
5533 pctx->num = blockcnt;
5534 goto failed_add_dir_block;
5543 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
5548 * Simplistic fragmentation check. We merely require that the
5549 * file be contiguous. (Which can never be true for really
5550 * big files that are greater than a block group.)
5552 if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
5553 if (p->previous_block+1 != blk) {
5554 if (ctx->options & E2F_OPT_FRAGCHECK) {
5562 printf(_("%6lu(%c): expecting %6lu "
5563 "got phys %6lu (blkcnt %lld)\n"),
5564 (unsigned long) pctx->ino, type,
5565 (unsigned long) p->previous_block+1,
5566 (unsigned long) blk,
5567 (long long) blockcnt);
5573 if (p->is_dir && !ext2fs_has_feature_largedir(fs->super) &&
5574 !pctx->inode->i_size_high &&
5575 blockcnt > (1 << (21 - fs->super->s_log_block_size)))
5576 problem = PR_1_TOOBIG_DIR;
5577 if (p->is_dir && p->num_blocks + 1 >= p->max_blocks)
5578 problem = PR_1_TOOBIG_DIR;
5579 if (p->is_reg && p->num_blocks + 1 >= p->max_blocks)
5580 problem = PR_1_TOOBIG_REG;
5581 if (!p->is_dir && !p->is_reg && blockcnt > 0)
5582 problem = PR_1_TOOBIG_SYMLINK;
5584 if (blk < fs->super->s_first_data_block ||
5585 blk >= ext2fs_blocks_count(fs->super))
5586 problem = PR_1_ILLEGAL_BLOCK_NUM;
5589 * If this IND/DIND/TIND block is squatting atop some critical metadata
5590 * (group descriptors, superblock, bitmap, inode table), any write to
5591 * "fix" mapping problems will destroy the metadata. We'll let pass 1b
5592 * fix that and restart fsck.
5595 p->ino != EXT2_RESIZE_INO &&
5596 blk < ctx->fs->super->s_blocks_count &&
5597 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
5599 fix_problem_bad(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx, 2);
5600 if ((ctx->options & E2F_OPT_NO) == 0)
5601 ctx->flags |= E2F_FLAG_RESTART_LATER;
5605 p->num_illegal_blocks++;
5607 * A bit of subterfuge here -- we're trying to fix a block
5608 * mapping, but the IND/DIND/TIND block could have collided
5609 * with some critical metadata. So, fix the in-core mapping so
5610 * iterate won't go insane, but return 0 instead of
5611 * BLOCK_CHANGED so that it won't write the remapping out to
5612 * our multiply linked block.
5614 * Even if we previously determined that an *IND block
5615 * conflicts with critical metadata, we must still try to
5616 * iterate the *IND block as if it is an *IND block to find and
5617 * mark the blocks it points to. Better to be overly cautious
5618 * with the used_blocks map so that we don't move the *IND
5619 * block to a block that's really in use!
5621 if (p->ino != EXT2_RESIZE_INO &&
5623 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
5628 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
5629 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
5633 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
5635 set_latch_flags(PR_LATCH_BLOCK,
5640 pctx->blkcount = blockcnt;
5641 if (fix_problem(ctx, problem, pctx)) {
5642 blk = *block_nr = 0;
5643 ret_code = BLOCK_CHANGED;
5644 p->inode_modified = 1;
5646 * If the directory block is too big and is beyond the
5647 * end of the FS, don't bother trying to add it for
5648 * processing -- the kernel would never have created a
5649 * directory this large, and we risk an ENOMEM abort.
5650 * In any case, the toobig handler for extent-based
5651 * directories also doesn't feed toobig blocks to
5654 if (problem == PR_1_TOOBIG_DIR)
5661 if (p->ino == EXT2_RESIZE_INO) {
5663 * The resize inode has already be sanity checked
5664 * during pass #0 (the superblock checks). All we
5665 * have to do is mark the double indirect block as
5666 * being in use; all of the other blocks are handled
5667 * by mark_table_blocks()).
5669 if (blockcnt == BLOCK_COUNT_DIND)
5670 mark_block_used(ctx, blk);
5672 } else if (!(ctx->fs->cluster_ratio_bits &&
5673 p->previous_block &&
5674 (EXT2FS_B2C(ctx->fs, blk) ==
5675 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
5676 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
5677 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
5678 mark_block_used(ctx, blk);
5680 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
5681 p->last_block, blk, blockcnt)) {
5682 pctx->blk = blockcnt;
5684 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
5685 mark_block_used(ctx, blk);
5686 mark_block_used(ctx, blk);
5689 p->last_block = blockcnt;
5690 p->previous_block = blk;
5692 if (p->is_dir && (blockcnt >= 0)) {
5693 while (++p->last_db_block < blockcnt) {
5694 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
5697 if (pctx->errcode) {
5699 pctx->num = p->last_db_block;
5700 goto failed_add_dir_block;
5703 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
5705 if (pctx->errcode) {
5707 pctx->num = blockcnt;
5708 failed_add_dir_block:
5709 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
5710 /* Should never get here */
5711 ctx->flags |= E2F_FLAG_ABORT;
5718 static int process_bad_block(ext2_filsys fs,
5720 e2_blkcnt_t blockcnt,
5721 blk64_t ref_block EXT2FS_ATTR((unused)),
5722 int ref_offset EXT2FS_ATTR((unused)),
5725 struct process_block_struct *p;
5726 blk64_t blk = *block_nr;
5727 blk64_t first_block;
5729 struct problem_context *pctx;
5735 p = (struct process_block_struct *) priv_data;
5739 pctx->ino = EXT2_BAD_INO;
5741 pctx->blkcount = blockcnt;
5743 if ((blk < fs->super->s_first_data_block) ||
5744 (blk >= ext2fs_blocks_count(fs->super))) {
5745 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
5747 return BLOCK_CHANGED;
5753 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
5755 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
5757 return BLOCK_CHANGED;
5759 } else if (is_blocks_used(ctx, blk, 1)) {
5761 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
5764 return BLOCK_CHANGED;
5766 if (e2fsck_should_abort(ctx))
5769 mark_block_used(ctx, blk);
5774 printf ("DEBUG: Marking %u as bad.\n", blk);
5776 ctx->fs_badblocks_count++;
5778 * If the block is not used, then mark it as used and return.
5779 * If it is already marked as found, this must mean that
5780 * there's an overlap between the filesystem table blocks
5781 * (bitmaps and inode table) and the bad block list.
5783 if (!is_blocks_used(ctx, blk, 1)) {
5784 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
5788 * Try to find the where the filesystem block was used...
5790 first_block = fs->super->s_first_data_block;
5792 for (i = 0; i < fs->group_desc_count; i++ ) {
5795 if (!ext2fs_bg_has_super(fs, i))
5797 if (blk == first_block) {
5799 if (fix_problem(ctx,
5800 PR_1_BAD_PRIMARY_SUPERBLOCK,
5803 return BLOCK_CHANGED;
5807 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
5810 if ((blk > first_block) &&
5811 (blk <= first_block + fs->desc_blocks)) {
5813 pctx->blk = *block_nr;
5814 if (fix_problem(ctx,
5815 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
5817 return BLOCK_CHANGED;
5821 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
5825 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
5826 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
5827 ctx->invalid_block_bitmap_flag[i]++;
5828 ctx->invalid_bitmaps++;
5832 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
5833 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
5834 ctx->invalid_inode_bitmap_flag[i]++;
5835 ctx->invalid_bitmaps++;
5839 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
5840 (blk < (ext2fs_inode_table_loc(fs, i) +
5841 fs->inode_blocks_per_group))) {
5843 * If there are bad blocks in the inode table,
5844 * the inode scan code will try to do
5845 * something reasonable automatically.
5849 first_block += fs->super->s_blocks_per_group;
5852 * If we've gotten to this point, then the only
5853 * possibility is that the bad block inode meta data
5854 * is using a bad block.
5856 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
5857 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
5858 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
5860 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
5862 return BLOCK_CHANGED;
5864 if (e2fsck_should_abort(ctx))
5871 /* Warn user that the block wasn't claimed */
5872 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
5877 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
5878 const char *name, int num, blk64_t *new_block)
5880 ext2_filsys fs = ctx->fs;
5882 blk64_t old_block = *new_block;
5885 unsigned flexbg_size;
5888 struct problem_context pctx;
5890 clear_problem_context(&pctx);
5893 pctx.blk = old_block;
5897 * For flex_bg filesystems, first try to allocate the metadata
5898 * within the flex_bg, and if that fails then try finding the
5899 * space anywhere in the filesystem.
5901 is_flexbg = ext2fs_has_feature_flex_bg(fs->super);
5903 flexbg_size = 1U << fs->super->s_log_groups_per_flex;
5904 flexbg = group / flexbg_size;
5905 first_block = ext2fs_group_first_block2(fs,
5906 flexbg_size * flexbg);
5907 last_grp = group | (flexbg_size - 1);
5908 if (last_grp >= fs->group_desc_count)
5909 last_grp = fs->group_desc_count - 1;
5910 last_block = ext2fs_group_last_block2(fs, last_grp);
5912 last_block = ext2fs_group_last_block2(fs, group);
5913 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
5914 num, ctx->block_found_map,
5916 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
5917 pctx.errcode = ext2fs_get_free_blocks2(fs,
5918 fs->super->s_first_data_block,
5919 ext2fs_blocks_count(fs->super),
5920 num, ctx->block_found_map, new_block);
5923 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
5924 ext2fs_unmark_valid(fs);
5925 ctx->flags |= E2F_FLAG_ABORT;
5928 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
5930 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
5931 ext2fs_unmark_valid(fs);
5932 ctx->flags |= E2F_FLAG_ABORT;
5935 ext2fs_mark_super_dirty(fs);
5936 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
5937 pctx.blk2 = *new_block;
5938 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
5939 PR_1_RELOC_TO), &pctx);
5941 for (i = 0; i < num; i++) {
5943 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
5945 pctx.errcode = io_channel_read_blk64(fs->io,
5946 old_block + i, 1, buf);
5948 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
5949 pctx.blk = (*new_block) + i;
5950 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
5953 pctx.blk = (*new_block) + i;
5954 pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
5959 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
5961 ext2fs_free_mem(&buf);
5965 * This routine gets called at the end of pass 1 if bad blocks are
5966 * detected in the superblock, group descriptors, inode_bitmaps, or
5967 * block bitmaps. At this point, all of the blocks have been mapped
5968 * out, so we can try to allocate new block(s) to replace the bad
5971 static void handle_fs_bad_blocks(e2fsck_t ctx)
5973 ext2_filsys fs = ctx->fs;
5975 blk64_t first_block;
5978 for (i = 0; i < fs->group_desc_count; i++) {
5979 first_block = ext2fs_group_first_block2(fs, i);
5981 if (ctx->invalid_block_bitmap_flag[i]) {
5982 new_blk = ext2fs_block_bitmap_loc(fs, i);
5983 new_table_block(ctx, first_block, i, _("block bitmap"),
5985 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
5987 if (ctx->invalid_inode_bitmap_flag[i]) {
5988 new_blk = ext2fs_inode_bitmap_loc(fs, i);
5989 new_table_block(ctx, first_block, i, _("inode bitmap"),
5991 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
5993 if (ctx->invalid_inode_table_flag[i]) {
5994 new_blk = ext2fs_inode_table_loc(fs, i);
5995 new_table_block(ctx, first_block, i, _("inode table"),
5996 fs->inode_blocks_per_group,
5998 ext2fs_inode_table_loc_set(fs, i, new_blk);
5999 ctx->flags |= E2F_FLAG_RESTART;
6002 ctx->invalid_bitmaps = 0;
6006 * This routine marks all blocks which are used by the superblock,
6007 * group descriptors, inode bitmaps, and block bitmaps.
6009 static void mark_table_blocks(e2fsck_t ctx)
6011 ext2_filsys fs = ctx->fs;
6015 struct problem_context pctx;
6017 clear_problem_context(&pctx);
6019 for (i = 0; i < fs->group_desc_count; i++) {
6022 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
6023 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
6026 * Mark the blocks used for the inode table
6028 if (ext2fs_inode_table_loc(fs, i)) {
6029 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
6030 j < fs->inode_blocks_per_group;
6032 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
6035 if (!ctx->invalid_inode_table_flag[i] &&
6037 PR_1_ITABLE_CONFLICT, &pctx)) {
6038 ctx->invalid_inode_table_flag[i]++;
6039 ctx->invalid_bitmaps++;
6042 ext2fs_mark_block_bitmap2(
6043 ctx->block_found_map, b);
6044 ext2fs_mark_block_bitmap2(
6045 ctx->block_metadata_map, b);
6051 * Mark block used for the block bitmap
6053 if (ext2fs_block_bitmap_loc(fs, i)) {
6054 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
6055 ext2fs_block_bitmap_loc(fs, i))) {
6056 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
6057 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
6058 ctx->invalid_block_bitmap_flag[i]++;
6059 ctx->invalid_bitmaps++;
6062 ext2fs_mark_block_bitmap2(ctx->block_found_map,
6063 ext2fs_block_bitmap_loc(fs, i));
6064 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
6065 ext2fs_block_bitmap_loc(fs, i));
6069 * Mark block used for the inode bitmap
6071 if (ext2fs_inode_bitmap_loc(fs, i)) {
6072 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
6073 ext2fs_inode_bitmap_loc(fs, i))) {
6074 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
6075 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
6076 ctx->invalid_inode_bitmap_flag[i]++;
6077 ctx->invalid_bitmaps++;
6080 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
6081 ext2fs_inode_bitmap_loc(fs, i));
6082 ext2fs_mark_block_bitmap2(ctx->block_found_map,
6083 ext2fs_inode_bitmap_loc(fs, i));
6090 * These subroutines short circuits ext2fs_get_blocks and
6091 * ext2fs_check_directory; we use them since we already have the inode
6092 * structure, so there's no point in letting the ext2fs library read
6095 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
6098 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6101 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
6102 return EXT2_ET_CALLBACK_NOTHANDLED;
6104 for (i=0; i < EXT2_N_BLOCKS; i++)
6105 blocks[i] = ctx->stashed_inode->i_block[i];
6109 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
6110 struct ext2_inode *inode)
6112 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6114 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
6115 return EXT2_ET_CALLBACK_NOTHANDLED;
6116 *inode = *ctx->stashed_inode;
6120 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
6121 struct ext2_inode *inode)
6123 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6125 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
6126 (inode != ctx->stashed_inode))
6127 *ctx->stashed_inode = *inode;
6128 return EXT2_ET_CALLBACK_NOTHANDLED;
6131 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
6133 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6135 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
6136 return EXT2_ET_CALLBACK_NOTHANDLED;
6138 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
6139 return EXT2_ET_NO_DIRECTORY;
6143 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
6146 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6150 if (ctx->block_found_map) {
6151 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
6155 if (fs->block_map) {
6156 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
6157 ext2fs_mark_bb_dirty(fs);
6160 if (!fs->block_map) {
6161 retval = ext2fs_read_block_bitmap(fs);
6166 retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
6175 static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
6176 blk64_t len, blk64_t *pblk, blk64_t *plen)
6178 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6181 if (ctx->block_found_map)
6182 return ext2fs_new_range(fs, flags, goal, len,
6183 ctx->block_found_map, pblk, plen);
6185 if (!fs->block_map) {
6186 retval = ext2fs_read_block_bitmap(fs);
6191 return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
6195 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
6197 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6199 /* Never free a critical metadata block */
6200 if (ctx->block_found_map &&
6201 ctx->block_metadata_map &&
6203 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
6206 if (ctx->block_found_map) {
6208 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
6210 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
6214 static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
6215 blk_t num, int inuse)
6217 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
6219 /* Never free a critical metadata block */
6220 if (ctx->block_found_map &&
6221 ctx->block_metadata_map &&
6223 ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
6226 if (ctx->block_found_map) {
6228 ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
6231 ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
6236 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
6238 ext2_filsys fs = ctx->fs;
6240 if (use_shortcuts) {
6241 fs->get_blocks = pass1_get_blocks;
6242 fs->check_directory = pass1_check_directory;
6243 fs->read_inode = pass1_read_inode;
6244 fs->write_inode = pass1_write_inode;
6245 ctx->stashed_ino = 0;
6248 fs->check_directory = 0;
6250 fs->write_inode = 0;
6254 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
6256 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
6257 ext2fs_set_block_alloc_stats_callback(ctx->fs,
6258 e2fsck_block_alloc_stats, 0);
6259 ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
6260 ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
6261 e2fsck_block_alloc_stats_range, NULL);