2 * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
11 * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12 * and applies the following tests to each inode:
14 * - The mode field of the inode must be legal.
15 * - The size and block count fields of the inode are correct.
16 * - A data block must not be used by another inode
18 * Pass 1 also gathers the collects the following information:
20 * - A bitmap of which inodes are in use. (inode_used_map)
21 * - A bitmap of which inodes are directories. (inode_dir_map)
22 * - A bitmap of which inodes are regular files. (inode_reg_map)
23 * - A bitmap of which inodes have bad fields. (inode_bad_map)
24 * - A bitmap of which inodes are in bad blocks. (inode_bb_map)
25 * - A bitmap of which inodes are imagic inodes. (inode_imagic_map)
26 * - A bitmap of which blocks are in use. (block_found_map)
27 * - A bitmap of which blocks are in use by two inodes (block_dup_map)
28 * - The data blocks of the directory inodes. (dir_map)
29 * - Ref counts for ea_inodes. (ea_inode_refs)
30 * - The encryption policy ID of each encrypted inode. (encrypted_files)
32 * Pass 1 is designed to stash away enough information so that the
33 * other passes should not need to read in the inode information
34 * during the normal course of a filesystem check. (Although if an
35 * inconsistency is detected, other passes may need to read in an
38 * Note that pass 1B will be invoked if there are any duplicate blocks
42 #define _GNU_SOURCE 1 /* get strnlen() */
51 #include <ext2fs/ext2_ext_attr.h>
56 #ifdef NO_INLINE_FUNCS
59 #define _INLINE_ inline
69 static int process_block(ext2_filsys fs, blk64_t *blocknr,
70 e2_blkcnt_t blockcnt, blk64_t ref_blk,
71 int ref_offset, void *priv_data);
72 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
73 e2_blkcnt_t blockcnt, blk64_t ref_blk,
74 int ref_offset, void *priv_data);
75 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
77 const struct ea_quota *ea_ibody_quota);
78 static void mark_table_blocks(e2fsck_t ctx);
79 static void alloc_bb_map(e2fsck_t ctx);
80 static void alloc_imagic_map(e2fsck_t ctx);
81 static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
82 static void handle_fs_bad_blocks(e2fsck_t ctx);
83 static void process_inodes(e2fsck_t ctx, char *block_buf);
84 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
85 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
86 dgrp_t group, void * priv_data);
87 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
88 char *block_buf, int adjust_sign);
89 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
91 struct process_block_struct {
93 unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
94 fragmented:1, compressed:1, bbcheck:1,
99 e2_blkcnt_t last_init_lblock;
100 e2_blkcnt_t last_db_block;
101 int num_illegal_blocks;
102 blk64_t previous_block;
103 struct ext2_inode *inode;
104 struct problem_context *pctx;
105 ext2fs_block_bitmap fs_meta_blocks;
108 struct extent_tree_info eti;
111 struct process_inode_block {
113 struct ea_quota ea_ibody_quota;
114 struct ext2_inode_large inode;
117 struct scan_callback_struct {
123 * For the inodes to process list.
125 static struct process_inode_block *inodes_to_process;
126 static int process_inode_count;
128 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
129 EXT2_MIN_BLOCK_LOG_SIZE + 1];
132 * Check to make sure a device inode is real. Returns 1 if the device
133 * checks out, 0 if not.
135 * Note: this routine is now also used to check FIFO's and Sockets,
136 * since they have the same requirement; the i_block fields should be
139 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
140 struct ext2_inode *inode)
145 * If the index or extents flag is set, then this is a bogus
148 if (inode->i_flags & (EXT2_INDEX_FL | EXT4_EXTENTS_FL))
152 * We should be able to do the test below all the time, but
153 * because the kernel doesn't forcibly clear the device
154 * inode's additional i_block fields, there are some rare
155 * occasions when a legitimate device inode will have non-zero
156 * additional i_block fields. So for now, we only complain
157 * when the immutable flag is set, which should never happen
158 * for devices. (And that's when the problem is caused, since
159 * you can't set or clear immutable flags for devices.) Once
160 * the kernel has been fixed we can change this...
162 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
163 for (i=4; i < EXT2_N_BLOCKS; i++)
164 if (inode->i_block[i])
171 * Check to make sure a symlink inode is real. Returns 1 if the symlink
172 * checks out, 0 if not.
174 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
175 struct ext2_inode *inode, char *buf)
180 if ((inode->i_size_high || inode->i_size == 0) ||
181 (inode->i_flags & EXT2_INDEX_FL))
184 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
187 if (inode->i_flags & EXT4_EXTENTS_FL)
189 if (ext2fs_inline_data_size(fs, ino, &inline_size))
191 if (inode->i_size != inline_size)
197 if (ext2fs_is_fast_symlink(inode)) {
198 if (inode->i_flags & EXT4_EXTENTS_FL)
200 buf = (char *)inode->i_block;
201 buflen = sizeof(inode->i_block);
203 ext2_extent_handle_t handle;
204 struct ext2_extent_info info;
205 struct ext2fs_extent extent;
209 if (inode->i_flags & EXT4_EXTENTS_FL) {
210 if (ext2fs_extent_open2(fs, ino, inode, &handle))
212 if (ext2fs_extent_get_info(handle, &info) ||
213 (info.num_entries != 1) ||
214 (info.max_depth != 0)) {
215 ext2fs_extent_free(handle);
218 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT,
220 (extent.e_lblk != 0) ||
221 (extent.e_len != 1)) {
222 ext2fs_extent_free(handle);
226 ext2fs_extent_free(handle);
228 blk = inode->i_block[0];
230 for (i = 1; i < EXT2_N_BLOCKS; i++)
231 if (inode->i_block[i])
235 if (blk < fs->super->s_first_data_block ||
236 blk >= ext2fs_blocks_count(fs->super))
239 if (io_channel_read_blk64(fs->io, blk, 1, buf))
242 buflen = fs->blocksize;
245 if (inode->i_flags & EXT4_ENCRYPT_FL)
246 len = ext2fs_le16_to_cpu(*(__u16 *)buf) + 2;
248 len = strnlen(buf, buflen);
253 if (len != inode->i_size)
259 * If the extents or inlinedata flags are set on the inode, offer to clear 'em.
261 #define BAD_SPECIAL_FLAGS (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)
262 static void check_extents_inlinedata(e2fsck_t ctx,
263 struct problem_context *pctx)
265 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
268 if (!fix_problem(ctx, PR_1_SPECIAL_EXTENTS_IDATA, pctx))
271 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
272 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
274 #undef BAD_SPECIAL_FLAGS
277 * If the immutable (or append-only) flag is set on the inode, offer
280 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
281 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
283 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
286 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
289 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
290 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
294 * If device, fifo or socket, check size is zero -- if not offer to
297 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
299 struct ext2_inode *inode = pctx->inode;
301 if (EXT2_I_SIZE(inode) == 0)
304 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
307 ext2fs_inode_size_set(ctx->fs, inode, 0);
308 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
312 * For a given size, calculate how many blocks would be charged towards quota.
314 static blk64_t size_to_quota_blocks(ext2_filsys fs, size_t size)
318 clusters = DIV_ROUND_UP(size, fs->blocksize << fs->cluster_ratio_bits);
319 return EXT2FS_C2B(fs, clusters);
323 * Check validity of EA inode. Return 0 if EA inode is valid, otherwise return
326 static problem_t check_large_ea_inode(e2fsck_t ctx,
327 struct ext2_ext_attr_entry *entry,
328 struct problem_context *pctx,
329 blk64_t *quota_blocks)
331 struct ext2_inode inode;
335 /* Check if inode is within valid range */
336 if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
337 (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
338 pctx->num = entry->e_value_inum;
339 return PR_1_ATTR_VALUE_EA_INODE;
342 e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
344 retval = ext2fs_ext_attr_hash_entry2(ctx->fs, entry, NULL, &hash);
346 com_err("check_large_ea_inode", retval,
347 _("while hashing entry with e_value_inum = %u"),
348 entry->e_value_inum);
352 if (hash == entry->e_hash) {
353 *quota_blocks = size_to_quota_blocks(ctx->fs,
354 entry->e_value_size);
356 /* This might be an old Lustre-style ea_inode reference. */
357 if (inode.i_mtime == pctx->ino &&
358 inode.i_generation == pctx->inode->i_generation) {
361 /* If target inode is also missing EA_INODE flag,
362 * this is likely to be a bad reference.
364 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
365 pctx->num = entry->e_value_inum;
366 return PR_1_ATTR_VALUE_EA_INODE;
368 pctx->num = entry->e_hash;
369 return PR_1_ATTR_HASH;
374 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
375 pctx->num = entry->e_value_inum;
376 if (fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
377 inode.i_flags |= EXT4_EA_INODE_FL;
378 ext2fs_write_inode(ctx->fs, entry->e_value_inum,
381 return PR_1_ATTR_NO_EA_INODE_FL;
387 static void inc_ea_inode_refs(e2fsck_t ctx, struct problem_context *pctx,
388 struct ext2_ext_attr_entry *first, void *end)
390 struct ext2_ext_attr_entry *entry;
393 (void *)entry < end && !EXT2_EXT_IS_LAST_ENTRY(entry);
394 entry = EXT2_EXT_ATTR_NEXT(entry)) {
395 if (!entry->e_value_inum)
397 if (!ctx->ea_inode_refs) {
398 pctx->errcode = ea_refcount_create(0,
399 &ctx->ea_inode_refs);
402 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
403 ctx->flags |= E2F_FLAG_ABORT;
407 ea_refcount_increment(ctx->ea_inode_refs, entry->e_value_inum,
412 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx,
413 struct ea_quota *ea_ibody_quota)
415 struct ext2_super_block *sb = ctx->fs->super;
416 struct ext2_inode_large *inode;
417 struct ext2_ext_attr_entry *entry;
418 char *start, *header, *end;
419 unsigned int storage_size, remain;
420 problem_t problem = 0;
423 ea_ibody_quota->blocks = 0;
424 ea_ibody_quota->inodes = 0;
426 inode = (struct ext2_inode_large *) pctx->inode;
427 storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
428 inode->i_extra_isize;
429 header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
430 inode->i_extra_isize;
431 end = header + storage_size;
432 start = header + sizeof(__u32);
433 entry = (struct ext2_ext_attr_entry *) start;
435 /* scan all entry's headers first */
437 /* take finish entry 0UL into account */
438 remain = storage_size - sizeof(__u32);
440 region = region_create(0, storage_size);
442 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
444 ctx->flags |= E2F_FLAG_ABORT;
447 if (region_allocate(region, 0, sizeof(__u32))) {
448 problem = PR_1_INODE_EA_ALLOC_COLLISION;
452 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
453 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
456 if (region_allocate(region, (char *)entry - (char *)header,
457 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
458 problem = PR_1_INODE_EA_ALLOC_COLLISION;
462 /* header eats this space */
463 remain -= sizeof(struct ext2_ext_attr_entry);
465 /* is attribute name valid? */
466 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
467 pctx->num = entry->e_name_len;
468 problem = PR_1_ATTR_NAME_LEN;
472 /* attribute len eats this space */
473 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
475 if (entry->e_value_inum == 0) {
476 /* check value size */
477 if (entry->e_value_size > remain) {
478 pctx->num = entry->e_value_size;
479 problem = PR_1_ATTR_VALUE_SIZE;
483 if (entry->e_value_size &&
484 region_allocate(region,
485 sizeof(__u32) + entry->e_value_offs,
487 entry->e_value_size))) {
488 problem = PR_1_INODE_EA_ALLOC_COLLISION;
492 hash = ext2fs_ext_attr_hash_entry(entry,
493 start + entry->e_value_offs);
495 /* e_hash may be 0 in older inode's ea */
496 if (entry->e_hash != 0 && entry->e_hash != hash) {
497 pctx->num = entry->e_hash;
498 problem = PR_1_ATTR_HASH;
502 blk64_t quota_blocks;
504 problem = check_large_ea_inode(ctx, entry, pctx,
509 ea_ibody_quota->blocks += quota_blocks;
510 ea_ibody_quota->inodes++;
513 /* If EA value is stored in external inode then it does not
514 * consume space here */
515 if (entry->e_value_inum == 0)
516 remain -= entry->e_value_size;
518 entry = EXT2_EXT_ATTR_NEXT(entry);
521 if (region_allocate(region, (char *)entry - (char *)header,
523 problem = PR_1_INODE_EA_ALLOC_COLLISION;
530 * it seems like a corruption. it's very unlikely we could repair
531 * EA(s) in automatic fashion -bzzz
533 if (problem == 0 || !fix_problem(ctx, problem, pctx)) {
534 inc_ea_inode_refs(ctx, pctx,
535 (struct ext2_ext_attr_entry *)start, end);
539 /* simply remove all possible EA(s) */
540 *((__u32 *)header) = 0UL;
541 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
542 EXT2_INODE_SIZE(sb), "pass1");
543 ea_ibody_quota->blocks = 0;
544 ea_ibody_quota->inodes = 0;
547 static int check_inode_extra_negative_epoch(__u32 xtime, __u32 extra) {
548 return (xtime & (1U << 31)) != 0 &&
549 (extra & EXT4_EPOCH_MASK) == EXT4_EPOCH_MASK;
552 #define CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, xtime) \
553 check_inode_extra_negative_epoch(inode->i_##xtime, \
554 inode->i_##xtime##_extra)
556 /* When today's date is earlier than 2242, we assume that atimes,
557 * ctimes, crtimes, and mtimes with years in the range 2310..2378 are
558 * actually pre-1970 dates mis-encoded.
560 #define EXT4_EXTRA_NEGATIVE_DATE_CUTOFF 2 * (1LL << 32)
562 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx,
563 struct ea_quota *ea_ibody_quota)
565 struct ext2_super_block *sb = ctx->fs->super;
566 struct ext2_inode_large *inode;
570 ea_ibody_quota->blocks = 0;
571 ea_ibody_quota->inodes = 0;
573 inode = (struct ext2_inode_large *) pctx->inode;
574 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
575 /* this isn't large inode. so, nothing to check */
580 printf("inode #%u, i_extra_size %d\n", pctx->ino,
581 inode->i_extra_isize);
583 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
584 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
585 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
587 * For now we will allow i_extra_isize to be 0, but really
588 * implementations should never allow i_extra_isize to be 0
590 if (inode->i_extra_isize &&
591 (inode->i_extra_isize < min || inode->i_extra_isize > max ||
592 inode->i_extra_isize & 3)) {
593 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
595 if (inode->i_extra_isize < min || inode->i_extra_isize > max)
596 inode->i_extra_isize = sb->s_want_extra_isize;
598 inode->i_extra_isize = (inode->i_extra_isize + 3) & ~3;
599 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
600 EXT2_INODE_SIZE(sb), "pass1");
603 /* check if there is no place for an EA header */
604 if (inode->i_extra_isize >= max - sizeof(__u32))
607 eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
608 inode->i_extra_isize);
609 if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
610 /* it seems inode has an extended attribute(s) in body */
611 check_ea_in_inode(ctx, pctx, ea_ibody_quota);
615 * If the inode's extended atime (ctime, crtime, mtime) is stored in
616 * the old, invalid format, repair it.
618 if (((sizeof(time_t) <= 4) ||
619 (((sizeof(time_t) > 4) &&
620 ctx->now < EXT4_EXTRA_NEGATIVE_DATE_CUTOFF))) &&
621 (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime) ||
622 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime) ||
623 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime) ||
624 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))) {
626 if (!fix_problem(ctx, PR_1_EA_TIME_OUT_OF_RANGE, pctx))
629 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime))
630 inode->i_atime_extra &= ~EXT4_EPOCH_MASK;
631 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime))
632 inode->i_ctime_extra &= ~EXT4_EPOCH_MASK;
633 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime))
634 inode->i_crtime_extra &= ~EXT4_EPOCH_MASK;
635 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))
636 inode->i_mtime_extra &= ~EXT4_EPOCH_MASK;
637 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
638 EXT2_INODE_SIZE(sb), "pass1");
644 * Check to see if the inode might really be a directory, despite i_mode
646 * This is a lot of complexity for something for which I'm not really
647 * convinced happens frequently in the wild. If for any reason this
648 * causes any problems, take this code out.
649 * [tytso:20070331.0827EDT]
651 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
654 struct ext2_inode *inode = pctx->inode;
655 struct ext2_dir_entry *dirent;
658 unsigned int i, rec_len, not_device = 0;
663 * If the mode looks OK, we believe it. If the first block in
664 * the i_block array is 0, this cannot be a directory. If the
665 * inode is extent-mapped, it is still the case that the latter
666 * cannot be 0 - the magic number in the extent header would make
669 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
670 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
674 * Check the block numbers in the i_block array for validity:
675 * zero blocks are skipped (but the first one cannot be zero -
676 * see above), other blocks are checked against the first and
677 * max data blocks (from the the superblock) and against the
678 * block bitmap. Any invalid block found means this cannot be
681 * If there are non-zero blocks past the fourth entry, then
682 * this cannot be a device file: we remember that for the next
685 * For extent mapped files, we don't do any sanity checking:
686 * just try to get the phys block of logical block 0 and run
689 * For inline data files, we just try to get the size of inline
690 * data. If it's true, we will treat it as a directory.
693 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
694 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
695 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
698 unsigned int rec_len2;
699 struct ext2_dir_entry de;
701 if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
704 * If the size isn't a multiple of 4, it's probably not a
710 * If the first 10 bytes don't look like a directory entry,
711 * it's probably not a directory.
713 memcpy(&dotdot, inode->i_block, sizeof(dotdot));
714 memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
715 EXT2_DIR_REC_LEN(0));
716 dotdot = ext2fs_le32_to_cpu(dotdot);
717 de.inode = ext2fs_le32_to_cpu(de.inode);
718 de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
719 ext2fs_get_rec_len(ctx->fs, &de, &rec_len2);
720 if (dotdot >= ctx->fs->super->s_inodes_count ||
721 (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
722 dotdot != EXT2_ROOT_INO) ||
723 de.inode >= ctx->fs->super->s_inodes_count ||
724 (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
726 rec_len2 > EXT4_MIN_INLINE_DATA_SIZE -
727 EXT4_INLINE_DATA_DOTDOT_SIZE)
729 /* device files never have a "system.data" entry */
731 } else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
733 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
736 /* device files are never extent mapped */
739 for (i=0; i < EXT2_N_BLOCKS; i++) {
740 blk = inode->i_block[i];
746 if (blk < ctx->fs->super->s_first_data_block ||
747 blk >= ext2fs_blocks_count(ctx->fs->super) ||
748 ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
750 return; /* Invalid block, can't be dir */
752 blk = inode->i_block[0];
756 * If the mode says this is a device file and the i_links_count field
757 * is sane and we have not ruled it out as a device file previously,
758 * we declare it a device file, not a directory.
760 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
761 (inode->i_links_count == 1) && !not_device)
764 /* read the first block */
765 ehandler_operation(_("reading directory block"));
766 retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
767 ehandler_operation(0);
771 dirent = (struct ext2_dir_entry *) buf;
772 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
775 if ((ext2fs_dirent_name_len(dirent) != 1) ||
776 (dirent->name[0] != '.') ||
777 (dirent->inode != pctx->ino) ||
780 (rec_len >= ctx->fs->blocksize - 12))
783 dirent = (struct ext2_dir_entry *) (buf + rec_len);
784 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
787 if ((ext2fs_dirent_name_len(dirent) != 2) ||
788 (dirent->name[0] != '.') ||
789 (dirent->name[1] != '.') ||
795 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
796 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
797 e2fsck_write_inode_full(ctx, pctx->ino, inode,
798 EXT2_INODE_SIZE(ctx->fs->super),
799 "check_is_really_dir");
803 extern errcode_t e2fsck_setup_icount(e2fsck_t ctx, const char *icount_name,
804 int flags, ext2_icount_t hint,
807 unsigned int threshold;
808 unsigned int save_type;
816 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
818 profile_get_uint(ctx->profile, "scratch_files",
819 "numdirs_threshold", 0, 0, &threshold);
820 profile_get_boolean(ctx->profile, "scratch_files",
821 "icount", 0, 1, &enable);
823 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
825 num_dirs = 1024; /* Guess */
827 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
828 (!threshold || num_dirs > threshold)) {
829 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir,
834 e2fsck_set_bitmap_type(ctx->fs, EXT2FS_BMAP64_RBTREE, icount_name,
836 if (ctx->options & E2F_OPT_ICOUNT_FULLMAP)
837 flags |= EXT2_ICOUNT_OPT_FULLMAP;
838 retval = ext2fs_create_icount2(ctx->fs, flags, 0, hint, ret);
839 ctx->fs->default_bitmap_type = save_type;
843 static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
845 struct problem_context *pctx)
848 struct ext2_inode_large inode;
851 * Reread inode. If we don't see checksum error, then this inode
852 * has been fixed elsewhere.
854 ctx->stashed_ino = 0;
855 retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
857 if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
863 * Checksum still doesn't match. That implies that the inode passes
864 * all the sanity checks, so maybe the checksum is simply corrupt.
865 * See if the user will go for fixing that.
867 if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
870 retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
875 static void reserve_block_for_root_repair(e2fsck_t ctx)
879 ext2_filsys fs = ctx->fs;
881 ctx->root_repair_block = 0;
882 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
885 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
888 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
889 ctx->root_repair_block = blk;
892 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
896 ext2_filsys fs = ctx->fs;
897 static const char name[] = "lost+found";
900 ctx->lnf_repair_block = 0;
901 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
904 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
907 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
908 ctx->lnf_repair_block = blk;
911 static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
915 struct ext2_xattr_handle *handle;
918 retval = ext2fs_xattrs_open(fs, ino, &handle);
922 retval = ext2fs_xattrs_read(handle);
926 retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
931 (void) ext2fs_xattrs_close(&handle);
935 static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
936 struct problem_context *pctx,
943 * If the inode failed the checksum and the user didn't
944 * clear the inode, test the checksum again -- if it still
945 * fails, ask the user if the checksum should be corrected.
947 pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
949 ctx->flags |= E2F_FLAG_ABORT;
951 #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
953 finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
954 if ((ctx)->flags & E2F_FLAG_ABORT) \
958 static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
963 for (i = 0; i < EXT2_N_BLOCKS; i++) {
964 x = inode->i_block[i];
965 #ifdef WORDS_BIGENDIAN
966 x = ext2fs_swab32(x);
968 if (x >= ext2fs_blocks_count(fs->super))
976 * Figure out what to do with an inode that has both extents and inline data
977 * inode flags set. Returns -1 if we decide to erase the inode, 0 otherwise.
979 static int fix_inline_data_extents_file(e2fsck_t ctx,
981 struct ext2_inode *inode,
983 struct problem_context *pctx)
985 size_t max_inline_ea_size;
986 ext2_filsys fs = ctx->fs;
989 /* Both feature flags not set? Just run the regular checks */
990 if (!ext2fs_has_feature_extents(fs->super) &&
991 !ext2fs_has_feature_inline_data(fs->super))
994 /* Clear both flags if it's a special file */
995 if (LINUX_S_ISCHR(inode->i_mode) ||
996 LINUX_S_ISBLK(inode->i_mode) ||
997 LINUX_S_ISFIFO(inode->i_mode) ||
998 LINUX_S_ISSOCK(inode->i_mode)) {
999 check_extents_inlinedata(ctx, pctx);
1003 /* If it looks like an extent tree, try to clear inlinedata */
1004 if (ext2fs_extent_header_verify(inode->i_block,
1005 sizeof(inode->i_block)) == 0 &&
1006 fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
1007 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1012 /* If it looks short enough to be inline data, try to clear extents */
1013 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
1014 max_inline_ea_size = inode_size -
1015 (EXT2_GOOD_OLD_INODE_SIZE +
1016 ((struct ext2_inode_large *)inode)->i_extra_isize);
1018 max_inline_ea_size = 0;
1019 if (EXT2_I_SIZE(inode) <
1020 EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
1021 fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
1022 inode->i_flags &= ~EXT4_EXTENTS_FL;
1028 * Too big for inline data, but no evidence of extent tree -
1029 * maybe it's a block map file? If the mappings all look valid?
1031 if (could_be_block_map(fs, inode) &&
1032 fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
1033 #ifdef WORDS_BIGENDIAN
1036 for (i = 0; i < EXT2_N_BLOCKS; i++)
1037 inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
1040 inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
1045 /* Oh well, just clear the busted inode. */
1046 if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
1047 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1053 e2fsck_write_inode(ctx, ino, inode, "pass1");
1058 static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
1060 ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
1061 dgrp_t start = *group, grp;
1062 blk64_t blocks_to_read = 0;
1063 errcode_t err = EXT2_ET_INVALID_ARGUMENT;
1065 if (ctx->readahead_kb == 0)
1068 /* Keep iterating groups until we have enough to readahead */
1069 inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
1070 for (grp = start; grp < ctx->fs->group_desc_count; grp++) {
1071 if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
1073 inodes_in_group = ctx->fs->super->s_inodes_per_group -
1074 ext2fs_bg_itable_unused(ctx->fs, grp);
1075 blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
1077 if (blocks_to_read * ctx->fs->blocksize >
1078 ctx->readahead_kb * 1024)
1082 err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
1084 if (err == EAGAIN) {
1085 ctx->readahead_kb /= 2;
1091 /* Error; disable itable readahead */
1092 *group = ctx->fs->group_desc_count;
1093 *next_ino = ctx->fs->super->s_inodes_count;
1096 * Don't do more readahead until we've reached the first inode
1097 * of the last inode scan buffer block for the last group.
1100 inodes_per_buffer = (ctx->inode_buffer_blocks ?
1101 ctx->inode_buffer_blocks :
1102 EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
1103 ctx->fs->blocksize /
1104 EXT2_INODE_SIZE(ctx->fs->super);
1106 *next_ino = inodes_in_group -
1107 (inodes_in_group % inodes_per_buffer) + 1 +
1108 (grp * ctx->fs->super->s_inodes_per_group);
1113 * Check if the passed ino is one of the used superblock quota inodes.
1115 * Before the quota inodes were journaled, older superblock quota inodes
1116 * were just regular files in the filesystem and not reserved inodes. This
1117 * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1118 * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1120 static int quota_inum_is_super(struct ext2_super_block *sb, ext2_ino_t ino)
1122 enum quota_type qtype;
1124 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1125 if (*quota_sb_inump(sb, qtype) == ino)
1132 * Check if the passed ino is one of the reserved quota inodes.
1133 * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1134 * inodes. These inodes may or may not be in use by the quota feature.
1136 static int quota_inum_is_reserved(ext2_filsys fs, ext2_ino_t ino)
1138 enum quota_type qtype;
1140 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1141 if (quota_type2inum(qtype, fs->super) == ino)
1147 void e2fsck_pass1(e2fsck_t ctx)
1151 ext2_filsys fs = ctx->fs;
1153 struct ext2_inode *inode = NULL;
1154 ext2_inode_scan scan = NULL;
1155 char *block_buf = NULL;
1156 #ifdef RESOURCE_TRACK
1157 struct resource_track rtrack;
1159 unsigned char frag, fsize;
1160 struct problem_context pctx;
1161 struct scan_callback_struct scan_struct;
1162 struct ext2_super_block *sb = ctx->fs->super;
1164 const char *eop_next_inode = _("getting next inode from scan");
1165 int imagic_fs, extent_fs, inlinedata_fs, casefold_fs;
1166 int low_dtime_check = 1;
1167 unsigned int inode_size = EXT2_INODE_SIZE(fs->super);
1168 unsigned int bufsize;
1169 int failed_csum = 0;
1170 ext2_ino_t ino_threshold = 0;
1171 dgrp_t ra_group = 0;
1172 struct ea_quota ea_ibody_quota;
1174 init_resource_track(&rtrack, ctx->fs->io);
1175 clear_problem_context(&pctx);
1177 /* If we can do readahead, figure out how many groups to pull in. */
1178 if (!e2fsck_can_readahead(ctx->fs))
1179 ctx->readahead_kb = 0;
1180 else if (ctx->readahead_kb == ~0ULL)
1181 ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
1182 pass1_readahead(ctx, &ra_group, &ino_threshold);
1184 if (!(ctx->options & E2F_OPT_PREEN))
1185 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1187 if (ext2fs_has_feature_dir_index(fs->super) &&
1188 !(ctx->options & E2F_OPT_NO)) {
1189 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
1190 ctx->dirs_to_hash = 0;
1194 mtrace_print("Pass 1");
1197 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1199 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
1200 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
1201 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
1202 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
1203 max_sizes = (max_sizes * (1UL << i));
1204 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
1208 imagic_fs = ext2fs_has_feature_imagic_inodes(sb);
1209 extent_fs = ext2fs_has_feature_extents(sb);
1210 inlinedata_fs = ext2fs_has_feature_inline_data(sb);
1211 casefold_fs = ext2fs_has_feature_casefold(sb);
1214 * Allocate bitmaps structures
1216 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1217 EXT2FS_BMAP64_RBTREE,
1219 &ctx->inode_used_map);
1222 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1223 ctx->flags |= E2F_FLAG_ABORT;
1226 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1227 _("directory inode map"),
1228 EXT2FS_BMAP64_AUTODIR,
1229 "inode_dir_map", &ctx->inode_dir_map);
1232 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1233 ctx->flags |= E2F_FLAG_ABORT;
1236 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1237 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1238 "inode_reg_map", &ctx->inode_reg_map);
1241 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1242 ctx->flags |= E2F_FLAG_ABORT;
1245 pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
1246 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
1247 "block_found_map", &ctx->block_found_map);
1250 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1251 ctx->flags |= E2F_FLAG_ABORT;
1254 pctx.errcode = e2fsck_allocate_block_bitmap(fs,
1255 _("metadata block map"), EXT2FS_BMAP64_RBTREE,
1256 "block_metadata_map", &ctx->block_metadata_map);
1259 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1260 ctx->flags |= E2F_FLAG_ABORT;
1263 pctx.errcode = e2fsck_setup_icount(ctx, "inode_link_info", 0, NULL,
1264 &ctx->inode_link_info);
1266 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1267 ctx->flags |= E2F_FLAG_ABORT;
1270 bufsize = inode_size;
1271 if (bufsize < sizeof(struct ext2_inode_large))
1272 bufsize = sizeof(struct ext2_inode_large);
1273 inode = (struct ext2_inode *)
1274 e2fsck_allocate_memory(ctx, bufsize, "scratch inode");
1276 inodes_to_process = (struct process_inode_block *)
1277 e2fsck_allocate_memory(ctx,
1278 (ctx->process_inode_size *
1279 sizeof(struct process_inode_block)),
1280 "array of inodes to process");
1281 process_inode_count = 0;
1283 pctx.errcode = ext2fs_init_dblist(fs, 0);
1285 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1286 ctx->flags |= E2F_FLAG_ABORT;
1291 * If the last orphan field is set, clear it, since the pass1
1292 * processing will automatically find and clear the orphans.
1293 * In the future, we may want to try using the last_orphan
1294 * linked list ourselves, but for now, we clear it so that the
1295 * ext3 mount code won't get confused.
1297 if (!(ctx->options & E2F_OPT_READONLY)) {
1298 if (fs->super->s_last_orphan) {
1299 fs->super->s_last_orphan = 0;
1300 ext2fs_mark_super_dirty(fs);
1304 mark_table_blocks(ctx);
1305 pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
1306 &ctx->block_found_map);
1308 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1309 ctx->flags |= E2F_FLAG_ABORT;
1312 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1313 "block interate buffer");
1314 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1315 e2fsck_use_inode_shortcuts(ctx, 1);
1316 e2fsck_intercept_block_allocations(ctx);
1317 old_op = ehandler_operation(_("opening inode scan"));
1318 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1320 ehandler_operation(old_op);
1322 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1323 ctx->flags |= E2F_FLAG_ABORT;
1326 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1327 EXT2_SF_WARN_GARBAGE_INODES, 0);
1328 ctx->stashed_inode = inode;
1329 scan_struct.ctx = ctx;
1330 scan_struct.block_buf = block_buf;
1331 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1332 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1333 ctx->fs->group_desc_count)))
1335 if ((fs->super->s_wtime &&
1336 fs->super->s_wtime < fs->super->s_inodes_count) ||
1337 (fs->super->s_mtime &&
1338 fs->super->s_mtime < fs->super->s_inodes_count) ||
1339 (fs->super->s_mkfs_time &&
1340 fs->super->s_mkfs_time < fs->super->s_inodes_count))
1341 low_dtime_check = 0;
1343 if (ext2fs_has_feature_mmp(fs->super) &&
1344 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1345 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1346 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1347 fs->super->s_mmp_block);
1349 /* Set up ctx->lost_and_found if possible */
1350 (void) e2fsck_get_lost_and_found(ctx, 0);
1353 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
1354 if (e2fsck_mmp_update(fs))
1355 fatal_error(ctx, 0);
1357 old_op = ehandler_operation(eop_next_inode);
1358 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1360 if (ino > ino_threshold)
1361 pass1_readahead(ctx, &ra_group, &ino_threshold);
1362 ehandler_operation(old_op);
1363 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1365 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1367 * If badblocks says badblocks is bad, offer to clear
1368 * the list, update the in-core bb list, and restart
1371 if (ino == EXT2_BAD_INO &&
1372 fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1376 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1377 ext2fs_badblocks_list_free(ctx->fs->badblocks);
1378 ctx->fs->badblocks = NULL;
1379 err = ext2fs_read_bb_inode(ctx->fs,
1380 &ctx->fs->badblocks);
1382 fix_problem(ctx, PR_1_ISCAN_ERROR,
1384 ctx->flags |= E2F_FLAG_ABORT;
1386 ctx->flags |= E2F_FLAG_RESTART;
1389 if (!ctx->inode_bb_map)
1391 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1392 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1396 pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1397 pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1398 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1399 ctx->flags |= E2F_FLAG_ABORT;
1406 ctx->stashed_ino = ino;
1408 /* Clear trashed inode? */
1409 if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
1410 inode->i_links_count > 0 &&
1411 fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
1413 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1415 failed_csum = pctx.errcode != 0;
1418 * Check for inodes who might have been part of the
1419 * orphaned list linked list. They should have gotten
1420 * dealt with by now, unless the list had somehow been
1423 * FIXME: In the future, inodes which are still in use
1424 * (and which are therefore) pending truncation should
1425 * be handled specially. Right now we just clear the
1426 * dtime field, and the normal e2fsck handling of
1427 * inodes where i_size and the inode blocks are
1428 * inconsistent is to fix i_size, instead of releasing
1429 * the extra blocks. This won't catch the inodes that
1430 * was at the end of the orphan list, but it's better
1431 * than nothing. The right answer is that there
1432 * shouldn't be any bugs in the orphan list handling. :-)
1434 if (inode->i_dtime && low_dtime_check &&
1435 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1436 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1437 inode->i_dtime = inode->i_links_count ?
1439 e2fsck_write_inode(ctx, ino, inode,
1445 if (inode->i_links_count) {
1446 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1447 ino, inode->i_links_count);
1449 pctx.num = inode->i_links_count;
1450 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1451 ctx->flags |= E2F_FLAG_ABORT;
1454 } else if ((ino >= EXT2_FIRST_INODE(fs->super)) &&
1455 !quota_inum_is_reserved(fs, ino)) {
1456 if (!inode->i_dtime && inode->i_mode) {
1457 if (fix_problem(ctx,
1458 PR_1_ZERO_DTIME, &pctx)) {
1459 inode->i_dtime = ctx->now;
1460 e2fsck_write_inode(ctx, ino, inode,
1465 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1469 if ((inode->i_flags & EXT4_CASEFOLD_FL) &&
1470 ((!LINUX_S_ISDIR(inode->i_mode) &&
1471 fix_problem(ctx, PR_1_CASEFOLD_NONDIR, &pctx)) ||
1473 fix_problem(ctx, PR_1_CASEFOLD_FEATURE, &pctx)))) {
1474 inode->i_flags &= ~EXT4_CASEFOLD_FL;
1475 e2fsck_write_inode(ctx, ino, inode, "pass1");
1478 /* Conflicting inlinedata/extents inode flags? */
1479 if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
1480 (inode->i_flags & EXT4_EXTENTS_FL)) {
1481 int res = fix_inline_data_extents_file(ctx, ino, inode,
1485 /* skip FINISH_INODE_LOOP */
1490 /* Test for incorrect inline_data flags settings. */
1491 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
1492 (ino >= EXT2_FIRST_INODE(fs->super))) {
1495 pctx.errcode = get_inline_data_ea_size(fs, ino, &size);
1496 if (!pctx.errcode &&
1497 fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
1498 ext2fs_set_feature_inline_data(sb);
1499 ext2fs_mark_super_dirty(fs);
1501 } else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
1502 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1503 /* skip FINISH_INODE_LOOP */
1508 /* Test for inline data flag but no attr */
1509 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
1510 (ino >= EXT2_FIRST_INODE(fs->super))) {
1517 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1518 err = get_inline_data_ea_size(fs, ino, &size);
1519 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1520 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1524 /* Everything is awesome... */
1526 case EXT2_ET_BAD_EA_BLOCK_NUM:
1527 case EXT2_ET_BAD_EA_HASH:
1528 case EXT2_ET_BAD_EA_HEADER:
1529 case EXT2_ET_EA_BAD_NAME_LEN:
1530 case EXT2_ET_EA_BAD_VALUE_SIZE:
1531 case EXT2_ET_EA_KEY_NOT_FOUND:
1532 case EXT2_ET_EA_NO_SPACE:
1533 case EXT2_ET_MISSING_EA_FEATURE:
1534 case EXT2_ET_INLINE_DATA_CANT_ITERATE:
1535 case EXT2_ET_INLINE_DATA_NO_BLOCK:
1536 case EXT2_ET_INLINE_DATA_NO_SPACE:
1537 case EXT2_ET_NO_INLINE_DATA:
1538 case EXT2_ET_EXT_ATTR_CSUM_INVALID:
1539 case EXT2_ET_EA_BAD_VALUE_OFFSET:
1540 case EXT2_ET_EA_INODE_CORRUPTED:
1541 /* broken EA or no system.data EA; truncate */
1542 if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
1544 err = ext2fs_inode_size_set(fs, inode, 0);
1547 ctx->flags |= E2F_FLAG_ABORT;
1550 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1551 memset(&inode->i_block, 0,
1552 sizeof(inode->i_block));
1553 e2fsck_write_inode(ctx, ino, inode,
1559 /* Some other kind of non-xattr error? */
1561 ctx->flags |= E2F_FLAG_ABORT;
1567 * Test for incorrect extent flag settings.
1569 * On big-endian machines we must be careful:
1570 * When the inode is read, the i_block array is not swapped
1571 * if the extent flag is set. Therefore if we are testing
1572 * for or fixing a wrongly-set flag, we must potentially
1573 * (un)swap before testing, or after fixing.
1577 * In this case the extents flag was set when read, so
1578 * extent_header_verify is ok. If the inode is cleared,
1579 * no need to swap... so no extra swapping here.
1581 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
1582 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1583 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1584 if ((ext2fs_extent_header_verify(inode->i_block,
1585 sizeof(inode->i_block)) == 0) &&
1586 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1587 ext2fs_set_feature_extents(sb);
1588 ext2fs_mark_super_dirty(fs);
1590 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1592 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1593 if (ino == EXT2_BAD_INO)
1594 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1596 /* skip FINISH_INODE_LOOP */
1602 * For big-endian machines:
1603 * If the inode didn't have the extents flag set when it
1604 * was read, then the i_blocks array was swapped. To test
1605 * as an extents header, we must swap it back first.
1606 * IF we then set the extents flag, the entire i_block
1607 * array must be un/re-swapped to make it proper extents data.
1609 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1610 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1611 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1612 (LINUX_S_ISREG(inode->i_mode) ||
1613 LINUX_S_ISDIR(inode->i_mode))) {
1615 #ifdef WORDS_BIGENDIAN
1616 __u32 tmp_block[EXT2_N_BLOCKS];
1618 for (i = 0; i < EXT2_N_BLOCKS; i++)
1619 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1622 ehp = inode->i_block;
1624 if ((ext2fs_extent_header_verify(ehp,
1625 sizeof(inode->i_block)) == 0) &&
1626 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
1627 inode->i_flags |= EXT4_EXTENTS_FL;
1628 #ifdef WORDS_BIGENDIAN
1629 memcpy(inode->i_block, tmp_block,
1630 sizeof(inode->i_block));
1632 e2fsck_write_inode(ctx, ino, inode, "pass1");
1637 if (ino == EXT2_BAD_INO) {
1638 struct process_block_struct pb;
1640 if ((failed_csum || inode->i_mode || inode->i_uid ||
1641 inode->i_gid || inode->i_links_count ||
1642 (inode->i_flags & EXT4_INLINE_DATA_FL) ||
1643 inode->i_file_acl) &&
1644 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1645 memset(inode, 0, sizeof(struct ext2_inode));
1646 e2fsck_write_inode(ctx, ino, inode,
1651 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1652 &pb.fs_meta_blocks);
1655 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1656 ctx->flags |= E2F_FLAG_ABORT;
1659 pb.ino = EXT2_BAD_INO;
1660 pb.num_blocks = pb.last_block = 0;
1661 pb.last_db_block = -1;
1662 pb.num_illegal_blocks = 0;
1663 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1664 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1668 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1669 block_buf, process_bad_block, &pb);
1670 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1672 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1673 ctx->flags |= E2F_FLAG_ABORT;
1677 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1678 ctx->flags |= E2F_FLAG_ABORT;
1681 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1682 clear_problem_context(&pctx);
1683 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1685 } else if (ino == EXT2_ROOT_INO) {
1687 * Make sure the root inode is a directory; if
1688 * not, offer to clear it. It will be
1689 * regenerated in pass #3.
1691 if (!LINUX_S_ISDIR(inode->i_mode)) {
1692 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1696 * If dtime is set, offer to clear it. mke2fs
1697 * version 0.2b created filesystems with the
1698 * dtime field set for the root and lost+found
1699 * directories. We won't worry about
1700 * /lost+found, since that can be regenerated
1701 * easily. But we will fix the root directory
1702 * as a special case.
1704 if (inode->i_dtime && inode->i_links_count) {
1705 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1707 e2fsck_write_inode(ctx, ino, inode,
1712 } else if (ino == EXT2_JOURNAL_INO) {
1713 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1714 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1715 if (!LINUX_S_ISREG(inode->i_mode) &&
1716 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1718 inode->i_mode = LINUX_S_IFREG;
1719 e2fsck_write_inode(ctx, ino, inode,
1723 check_blocks(ctx, &pctx, block_buf, NULL);
1724 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1727 if ((inode->i_links_count ||
1728 inode->i_blocks || inode->i_block[0]) &&
1729 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1731 memset(inode, 0, inode_size);
1732 ext2fs_icount_store(ctx->inode_link_info,
1734 e2fsck_write_inode_full(ctx, ino, inode,
1735 inode_size, "pass1");
1738 } else if (quota_inum_is_reserved(fs, ino)) {
1739 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1740 if (ext2fs_has_feature_quota(fs->super) &&
1741 quota_inum_is_super(fs->super, ino)) {
1742 if (!LINUX_S_ISREG(inode->i_mode) &&
1743 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1745 inode->i_mode = LINUX_S_IFREG;
1746 e2fsck_write_inode(ctx, ino, inode,
1750 check_blocks(ctx, &pctx, block_buf, NULL);
1751 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1754 if ((inode->i_links_count ||
1755 inode->i_blocks || inode->i_block[0]) &&
1756 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1758 memset(inode, 0, inode_size);
1759 ext2fs_icount_store(ctx->inode_link_info,
1761 e2fsck_write_inode_full(ctx, ino, inode,
1762 inode_size, "pass1");
1765 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1766 problem_t problem = 0;
1768 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1769 if (ino == EXT2_BOOT_LOADER_INO) {
1770 if (LINUX_S_ISDIR(inode->i_mode))
1771 problem = PR_1_RESERVED_BAD_MODE;
1772 } else if (ino == EXT2_RESIZE_INO) {
1773 if (inode->i_mode &&
1774 !LINUX_S_ISREG(inode->i_mode))
1775 problem = PR_1_RESERVED_BAD_MODE;
1777 if (inode->i_mode != 0)
1778 problem = PR_1_RESERVED_BAD_MODE;
1781 if (fix_problem(ctx, problem, &pctx)) {
1783 e2fsck_write_inode(ctx, ino, inode,
1788 check_blocks(ctx, &pctx, block_buf, NULL);
1789 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1793 if (!inode->i_links_count) {
1794 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1798 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1799 * deleted files. Oops.
1801 * Since all new ext2 implementations get this right,
1802 * we now assume that the case of non-zero
1803 * i_links_count and non-zero dtime means that we
1804 * should keep the file, not delete it.
1807 if (inode->i_dtime) {
1808 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1810 e2fsck_write_inode(ctx, ino, inode, "pass1");
1815 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1816 switch (fs->super->s_creator_os) {
1818 frag = inode->osd2.hurd2.h_i_frag;
1819 fsize = inode->osd2.hurd2.h_i_fsize;
1825 if (inode->i_faddr || frag || fsize ||
1826 (!ext2fs_has_feature_largedir(fs->super) &&
1827 (LINUX_S_ISDIR(inode->i_mode) && inode->i_size_high)))
1828 mark_inode_bad(ctx, ino);
1829 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
1830 !ext2fs_has_feature_64bit(fs->super) &&
1831 inode->osd2.linux2.l_i_file_acl_high != 0)
1832 mark_inode_bad(ctx, ino);
1833 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
1834 !ext2fs_has_feature_huge_file(fs->super) &&
1835 (inode->osd2.linux2.l_i_blocks_hi != 0))
1836 mark_inode_bad(ctx, ino);
1837 if (inode->i_flags & EXT2_IMAGIC_FL) {
1839 if (!ctx->inode_imagic_map)
1840 alloc_imagic_map(ctx);
1841 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1844 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1845 inode->i_flags &= ~EXT2_IMAGIC_FL;
1846 e2fsck_write_inode(ctx, ino,
1853 check_inode_extra_space(ctx, &pctx, &ea_ibody_quota);
1854 check_is_really_dir(ctx, &pctx, block_buf);
1857 * ext2fs_inode_has_valid_blocks2 does not actually look
1858 * at i_block[] values, so not endian-sensitive here.
1860 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1861 LINUX_S_ISLNK(inode->i_mode) &&
1862 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1863 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1864 inode->i_flags &= ~EXT4_EXTENTS_FL;
1865 e2fsck_write_inode(ctx, ino, inode, "pass1");
1869 if ((inode->i_flags & EXT4_ENCRYPT_FL) &&
1870 add_encrypted_file(ctx, &pctx) < 0)
1873 if (LINUX_S_ISDIR(inode->i_mode)) {
1874 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1875 e2fsck_add_dir_info(ctx, ino, 0);
1876 ctx->fs_directory_count++;
1877 } else if (LINUX_S_ISREG (inode->i_mode)) {
1878 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1879 ctx->fs_regular_count++;
1880 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1881 e2fsck_pass1_check_device_inode(fs, inode)) {
1882 check_extents_inlinedata(ctx, &pctx);
1883 check_immutable(ctx, &pctx);
1884 check_size(ctx, &pctx);
1885 ctx->fs_chardev_count++;
1886 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1887 e2fsck_pass1_check_device_inode(fs, inode)) {
1888 check_extents_inlinedata(ctx, &pctx);
1889 check_immutable(ctx, &pctx);
1890 check_size(ctx, &pctx);
1891 ctx->fs_blockdev_count++;
1892 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1893 e2fsck_pass1_check_symlink(fs, ino, inode,
1895 check_immutable(ctx, &pctx);
1896 ctx->fs_symlinks_count++;
1897 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
1898 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1900 } else if (ext2fs_is_fast_symlink(inode)) {
1901 ctx->fs_fast_symlinks_count++;
1902 check_blocks(ctx, &pctx, block_buf,
1904 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1908 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1909 e2fsck_pass1_check_device_inode(fs, inode)) {
1910 check_extents_inlinedata(ctx, &pctx);
1911 check_immutable(ctx, &pctx);
1912 check_size(ctx, &pctx);
1913 ctx->fs_fifo_count++;
1914 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1915 e2fsck_pass1_check_device_inode(fs, inode)) {
1916 check_extents_inlinedata(ctx, &pctx);
1917 check_immutable(ctx, &pctx);
1918 check_size(ctx, &pctx);
1919 ctx->fs_sockets_count++;
1921 mark_inode_bad(ctx, ino);
1922 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1923 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
1924 if (inode->i_block[EXT2_IND_BLOCK])
1925 ctx->fs_ind_count++;
1926 if (inode->i_block[EXT2_DIND_BLOCK])
1927 ctx->fs_dind_count++;
1928 if (inode->i_block[EXT2_TIND_BLOCK])
1929 ctx->fs_tind_count++;
1931 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1932 !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
1933 (inode->i_block[EXT2_IND_BLOCK] ||
1934 inode->i_block[EXT2_DIND_BLOCK] ||
1935 inode->i_block[EXT2_TIND_BLOCK] ||
1936 ext2fs_file_acl_block(fs, inode))) {
1937 struct process_inode_block *itp;
1939 itp = &inodes_to_process[process_inode_count];
1941 itp->ea_ibody_quota = ea_ibody_quota;
1942 if (inode_size < sizeof(struct ext2_inode_large))
1943 memcpy(&itp->inode, inode, inode_size);
1945 memcpy(&itp->inode, inode, sizeof(itp->inode));
1946 process_inode_count++;
1948 check_blocks(ctx, &pctx, block_buf, &ea_ibody_quota);
1950 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1952 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1955 if (process_inode_count >= ctx->process_inode_size) {
1956 process_inodes(ctx, block_buf);
1958 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1962 process_inodes(ctx, block_buf);
1963 ext2fs_close_inode_scan(scan);
1966 reserve_block_for_root_repair(ctx);
1967 reserve_block_for_lnf_repair(ctx);
1970 * If any extended attribute blocks' reference counts need to
1971 * be adjusted, either up (ctx->refcount_extra), or down
1972 * (ctx->refcount), then fix them.
1974 if (ctx->refcount) {
1975 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1976 ea_refcount_free(ctx->refcount);
1979 if (ctx->refcount_extra) {
1980 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1982 ea_refcount_free(ctx->refcount_extra);
1983 ctx->refcount_extra = 0;
1986 if (ctx->ea_block_quota_blocks) {
1987 ea_refcount_free(ctx->ea_block_quota_blocks);
1988 ctx->ea_block_quota_blocks = 0;
1991 if (ctx->ea_block_quota_inodes) {
1992 ea_refcount_free(ctx->ea_block_quota_inodes);
1993 ctx->ea_block_quota_inodes = 0;
1996 if (ctx->invalid_bitmaps)
1997 handle_fs_bad_blocks(ctx);
1999 /* We don't need the block_ea_map any more */
2000 if (ctx->block_ea_map) {
2001 ext2fs_free_block_bitmap(ctx->block_ea_map);
2002 ctx->block_ea_map = 0;
2005 /* We don't need the encryption policy => ID map any more */
2006 destroy_encryption_policy_map(ctx);
2008 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
2009 clear_problem_context(&pctx);
2010 pctx.errcode = ext2fs_create_resize_inode(fs);
2012 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
2014 ctx->flags |= E2F_FLAG_ABORT;
2019 if (!pctx.errcode) {
2020 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
2022 inode->i_mtime = ctx->now;
2023 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
2026 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
2029 if (ctx->flags & E2F_FLAG_RESTART) {
2031 * Only the master copy of the superblock and block
2032 * group descriptors are going to be written during a
2033 * restart, so set the superblock to be used to be the
2034 * master superblock.
2036 ctx->use_superblock = 0;
2040 if (ctx->block_dup_map) {
2041 if (ctx->options & E2F_OPT_PREEN) {
2042 clear_problem_context(&pctx);
2043 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
2045 e2fsck_pass1_dupblocks(ctx, block_buf);
2047 ctx->flags |= E2F_FLAG_ALLOC_OK;
2049 e2fsck_use_inode_shortcuts(ctx, 0);
2050 ext2fs_free_mem(&inodes_to_process);
2051 inodes_to_process = 0;
2054 ext2fs_close_inode_scan(scan);
2056 ext2fs_free_mem(&block_buf);
2058 ext2fs_free_mem(&inode);
2061 * The l+f inode may have been cleared, so zap it now and
2062 * later passes will recalculate it if necessary
2064 ctx->lost_and_found = 0;
2066 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
2067 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
2069 ctx->invalid_bitmaps++;
2071 #undef FINISH_INODE_LOOP
2074 * When the inode_scan routines call this callback at the end of the
2075 * glock group, call process_inodes.
2077 static errcode_t scan_callback(ext2_filsys fs,
2078 ext2_inode_scan scan EXT2FS_ATTR((unused)),
2079 dgrp_t group, void * priv_data)
2081 struct scan_callback_struct *scan_struct;
2084 scan_struct = (struct scan_callback_struct *) priv_data;
2085 ctx = scan_struct->ctx;
2087 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
2090 if ((ctx->progress)(ctx, 1, group+1,
2091 ctx->fs->group_desc_count))
2092 return EXT2_ET_CANCEL_REQUESTED;
2098 * Process the inodes in the "inodes to process" list.
2100 static void process_inodes(e2fsck_t ctx, char *block_buf)
2103 struct ext2_inode *old_stashed_inode;
2104 ext2_ino_t old_stashed_ino;
2105 const char *old_operation;
2107 struct problem_context pctx;
2110 printf("begin process_inodes: ");
2112 if (process_inode_count == 0)
2114 old_operation = ehandler_operation(0);
2115 old_stashed_inode = ctx->stashed_inode;
2116 old_stashed_ino = ctx->stashed_ino;
2117 qsort(inodes_to_process, process_inode_count,
2118 sizeof(struct process_inode_block), process_inode_cmp);
2119 clear_problem_context(&pctx);
2120 for (i=0; i < process_inode_count; i++) {
2121 pctx.inode = ctx->stashed_inode =
2122 (struct ext2_inode *) &inodes_to_process[i].inode;
2123 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
2126 printf("%u ", pctx.ino);
2128 sprintf(buf, _("reading indirect blocks of inode %u"),
2130 ehandler_operation(buf);
2131 check_blocks(ctx, &pctx, block_buf,
2132 &inodes_to_process[i].ea_ibody_quota);
2133 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2136 ctx->stashed_inode = old_stashed_inode;
2137 ctx->stashed_ino = old_stashed_ino;
2138 process_inode_count = 0;
2140 printf("end process inodes\n");
2142 ehandler_operation(old_operation);
2145 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
2147 const struct process_inode_block *ib_a =
2148 (const struct process_inode_block *) a;
2149 const struct process_inode_block *ib_b =
2150 (const struct process_inode_block *) b;
2153 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
2154 ib_b->inode.i_block[EXT2_IND_BLOCK]);
2157 * We only call process_inodes() for non-extent
2158 * inodes, so it's OK to pass NULL to
2159 * ext2fs_file_acl_block() here.
2161 ret = ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a->inode)) -
2162 ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b->inode));
2164 ret = ib_a->ino - ib_b->ino;
2169 * Mark an inode as being bad in some what
2171 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
2173 struct problem_context pctx;
2175 if (!ctx->inode_bad_map) {
2176 clear_problem_context(&pctx);
2178 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2179 _("bad inode map"), EXT2FS_BMAP64_RBTREE,
2180 "inode_bad_map", &ctx->inode_bad_map);
2183 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2184 /* Should never get here */
2185 ctx->flags |= E2F_FLAG_ABORT;
2189 ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
2193 * This procedure will allocate the inode "bb" (badblock) map table
2195 static void alloc_bb_map(e2fsck_t ctx)
2197 struct problem_context pctx;
2199 clear_problem_context(&pctx);
2200 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2201 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
2202 "inode_bb_map", &ctx->inode_bb_map);
2205 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2206 /* Should never get here */
2207 ctx->flags |= E2F_FLAG_ABORT;
2213 * This procedure will allocate the inode imagic table
2215 static void alloc_imagic_map(e2fsck_t ctx)
2217 struct problem_context pctx;
2219 clear_problem_context(&pctx);
2220 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2221 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
2222 "inode_imagic_map", &ctx->inode_imagic_map);
2225 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2226 /* Should never get here */
2227 ctx->flags |= E2F_FLAG_ABORT;
2233 * Marks a block as in use, setting the dup_map if it's been set
2234 * already. Called by process_block and process_bad_block.
2236 * WARNING: Assumes checks have already been done to make sure block
2237 * is valid. This is true in both process_block and process_bad_block.
2239 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
2241 struct problem_context pctx;
2243 clear_problem_context(&pctx);
2245 if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
2246 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
2247 !(ctx->options & E2F_OPT_UNSHARE_BLOCKS)) {
2250 if (!ctx->block_dup_map) {
2251 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
2252 _("multiply claimed block map"),
2253 EXT2FS_BMAP64_RBTREE, "block_dup_map",
2254 &ctx->block_dup_map);
2257 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
2259 /* Should never get here */
2260 ctx->flags |= E2F_FLAG_ABORT;
2264 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
2266 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
2271 * When cluster size is greater than one block, it is caller's responsibility
2272 * to make sure block parameter starts at a cluster boundary.
2274 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
2277 if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
2278 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
2282 for (i = 0; i < num; i += EXT2FS_CLUSTER_RATIO(ctx->fs))
2283 mark_block_used(ctx, block + i);
2288 * Adjust the extended attribute block's reference counts at the end
2289 * of pass 1, either by subtracting out references for EA blocks that
2290 * are still referenced in ctx->refcount, or by adding references for
2291 * EA blocks that had extra references as accounted for in
2292 * ctx->refcount_extra.
2294 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
2295 char *block_buf, int adjust_sign)
2297 struct ext2_ext_attr_header *header;
2298 struct problem_context pctx;
2299 ext2_filsys fs = ctx->fs;
2304 clear_problem_context(&pctx);
2306 ea_refcount_intr_begin(refcount);
2308 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
2311 pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
2314 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
2317 header = (struct ext2_ext_attr_header *) block_buf;
2318 pctx.blkcount = header->h_refcount;
2319 should_be = header->h_refcount + adjust_sign * (int)count;
2320 pctx.num = should_be;
2321 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
2322 header->h_refcount = should_be;
2323 pctx.errcode = ext2fs_write_ext_attr3(fs, blk,
2327 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
2336 * Handle processing the extended attribute blocks
2338 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
2339 char *block_buf, struct ea_quota *ea_block_quota)
2341 ext2_filsys fs = ctx->fs;
2342 ext2_ino_t ino = pctx->ino;
2343 struct ext2_inode *inode = pctx->inode;
2346 struct ext2_ext_attr_header *header;
2347 struct ext2_ext_attr_entry *first, *entry;
2348 blk64_t quota_blocks = EXT2FS_C2B(fs, 1);
2349 __u64 quota_inodes = 0;
2350 region_t region = 0;
2351 int failed_csum = 0;
2353 ea_block_quota->blocks = 0;
2354 ea_block_quota->inodes = 0;
2356 blk = ext2fs_file_acl_block(fs, inode);
2361 * If the Extended attribute flag isn't set, then a non-zero
2362 * file acl means that the inode is corrupted.
2364 * Or if the extended attribute block is an invalid block,
2365 * then the inode is also corrupted.
2367 if (!ext2fs_has_feature_xattr(fs->super) ||
2368 (blk < fs->super->s_first_data_block) ||
2369 (blk >= ext2fs_blocks_count(fs->super))) {
2370 mark_inode_bad(ctx, ino);
2374 /* If ea bitmap hasn't been allocated, create it */
2375 if (!ctx->block_ea_map) {
2376 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
2377 _("ext attr block map"),
2378 EXT2FS_BMAP64_RBTREE, "block_ea_map",
2379 &ctx->block_ea_map);
2380 if (pctx->errcode) {
2382 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
2383 ctx->flags |= E2F_FLAG_ABORT;
2388 /* Create the EA refcount structure if necessary */
2389 if (!ctx->refcount) {
2390 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
2391 if (pctx->errcode) {
2393 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2394 ctx->flags |= E2F_FLAG_ABORT;
2400 /* Debugging text */
2401 printf("Inode %u has EA block %u\n", ino, blk);
2404 /* Have we seen this EA block before? */
2405 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
2406 ea_block_quota->blocks = EXT2FS_C2B(fs, 1);
2407 ea_block_quota->inodes = 0;
2409 if (ctx->ea_block_quota_blocks) {
2410 ea_refcount_fetch(ctx->ea_block_quota_blocks, blk,
2413 ea_block_quota->blocks = quota_blocks;
2416 if (ctx->ea_block_quota_inodes)
2417 ea_refcount_fetch(ctx->ea_block_quota_inodes, blk,
2418 &ea_block_quota->inodes);
2420 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
2422 /* Ooops, this EA was referenced more than it stated */
2423 if (!ctx->refcount_extra) {
2424 pctx->errcode = ea_refcount_create(0,
2425 &ctx->refcount_extra);
2426 if (pctx->errcode) {
2428 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2429 ctx->flags |= E2F_FLAG_ABORT;
2433 ea_refcount_increment(ctx->refcount_extra, blk, 0);
2438 * OK, we haven't seen this EA block yet. So we need to
2442 pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
2443 if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
2446 } else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
2449 if (pctx->errcode &&
2450 fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
2454 header = (struct ext2_ext_attr_header *) block_buf;
2455 pctx->blk = ext2fs_file_acl_block(fs, inode);
2456 if (((ctx->ext_attr_ver == 1) &&
2457 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
2458 ((ctx->ext_attr_ver == 2) &&
2459 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
2460 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
2464 if (header->h_blocks != 1) {
2465 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
2469 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
2472 region = region_create(0, fs->blocksize);
2474 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
2475 ctx->flags |= E2F_FLAG_ABORT;
2478 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
2479 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2483 first = (struct ext2_ext_attr_entry *)(header+1);
2484 end = block_buf + fs->blocksize;
2486 while ((char *)entry < end && *(__u32 *)entry) {
2489 if (region_allocate(region, (char *)entry - (char *)header,
2490 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
2491 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2495 if ((ctx->ext_attr_ver == 1 &&
2496 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
2497 (ctx->ext_attr_ver == 2 &&
2498 entry->e_name_index == 0)) {
2499 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
2503 if (entry->e_value_inum == 0) {
2504 if (entry->e_value_offs + entry->e_value_size >
2506 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
2510 if (entry->e_value_size &&
2511 region_allocate(region, entry->e_value_offs,
2512 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
2513 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
2518 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
2519 entry->e_value_offs);
2521 if (entry->e_hash != hash) {
2522 pctx->num = entry->e_hash;
2523 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
2525 entry->e_hash = hash;
2529 blk64_t entry_quota_blocks;
2531 problem = check_large_ea_inode(ctx, entry, pctx,
2532 &entry_quota_blocks);
2533 if (problem && fix_problem(ctx, problem, pctx))
2536 quota_blocks += entry_quota_blocks;
2540 entry = EXT2_EXT_ATTR_NEXT(entry);
2542 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
2543 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2546 region_free(region);
2549 * We only get here if there was no other errors that were fixed.
2550 * If there was a checksum fail, ask to correct it.
2553 fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
2554 pctx->errcode = ext2fs_write_ext_attr3(fs, blk, block_buf,
2560 if (quota_blocks != EXT2FS_C2B(fs, 1U)) {
2561 if (!ctx->ea_block_quota_blocks) {
2562 pctx->errcode = ea_refcount_create(0,
2563 &ctx->ea_block_quota_blocks);
2564 if (pctx->errcode) {
2569 ea_refcount_store(ctx->ea_block_quota_blocks, blk,
2574 if (!ctx->ea_block_quota_inodes) {
2575 pctx->errcode = ea_refcount_create(0,
2576 &ctx->ea_block_quota_inodes);
2577 if (pctx->errcode) {
2580 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2581 ctx->flags |= E2F_FLAG_ABORT;
2586 ea_refcount_store(ctx->ea_block_quota_inodes, blk,
2589 ea_block_quota->blocks = quota_blocks;
2590 ea_block_quota->inodes = quota_inodes;
2592 inc_ea_inode_refs(ctx, pctx, first, end);
2593 ea_refcount_store(ctx->refcount, blk, header->h_refcount - 1);
2594 mark_block_used(ctx, blk);
2595 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
2600 region_free(region);
2601 ext2fs_file_acl_block_set(fs, inode, 0);
2602 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
2606 /* Returns 1 if bad htree, 0 if OK */
2607 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
2608 ext2_ino_t ino, struct ext2_inode *inode,
2611 struct ext2_dx_root_info *root;
2612 ext2_filsys fs = ctx->fs;
2616 if ((!LINUX_S_ISDIR(inode->i_mode) &&
2617 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
2618 (!ext2fs_has_feature_dir_index(fs->super) &&
2619 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
2622 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
2624 if ((pctx->errcode) ||
2626 (blk < fs->super->s_first_data_block) ||
2627 (blk >= ext2fs_blocks_count(fs->super))) {
2628 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2634 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
2635 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2638 /* XXX should check that beginning matches a directory */
2639 root = (struct ext2_dx_root_info *) (block_buf + 24);
2641 if ((root->reserved_zero || root->info_length < 8) &&
2642 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2645 pctx->num = root->hash_version;
2646 if ((root->hash_version != EXT2_HASH_LEGACY) &&
2647 (root->hash_version != EXT2_HASH_HALF_MD4) &&
2648 (root->hash_version != EXT2_HASH_TEA) &&
2649 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2652 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2653 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2656 pctx->num = root->indirect_levels;
2657 if ((root->indirect_levels >= ext2_dir_htree_level(fs)) &&
2658 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2664 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2665 struct ext2_inode *inode, int restart_flag,
2669 inode->i_links_count = 0;
2670 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2671 inode->i_dtime = ctx->now;
2674 * If a special inode has such rotten block mappings that we
2675 * want to clear the whole inode, be sure to actually zap
2676 * the block maps because i_links_count isn't checked for
2677 * special inodes, and we'll end up right back here the next
2680 if (ino < EXT2_FIRST_INODE(ctx->fs->super))
2681 memset(inode->i_block, 0, sizeof(inode->i_block));
2683 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2684 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2685 if (ctx->inode_reg_map)
2686 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2687 if (ctx->inode_bad_map)
2688 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
2691 * If the inode was partially accounted for before processing
2692 * was aborted, we need to restart the pass 1 scan.
2694 ctx->flags |= restart_flag;
2696 if (ino == EXT2_BAD_INO)
2697 memset(inode, 0, sizeof(struct ext2_inode));
2699 e2fsck_write_inode(ctx, ino, inode, source);
2703 * Use the multiple-blocks reclamation code to fix alignment problems in
2704 * a bigalloc filesystem. We want a logical cluster to map to *only* one
2705 * physical cluster, and we want the block offsets within that cluster to
2708 static int has_unaligned_cluster_map(e2fsck_t ctx,
2709 blk64_t last_pblk, blk64_t last_lblk,
2710 blk64_t pblk, blk64_t lblk)
2712 blk64_t cluster_mask;
2714 if (!ctx->fs->cluster_ratio_bits)
2716 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
2719 * If the block in the logical cluster doesn't align with the block in
2720 * the physical cluster...
2722 if ((lblk & cluster_mask) != (pblk & cluster_mask))
2726 * If we cross a physical cluster boundary within a logical cluster...
2728 if (last_pblk && (lblk & cluster_mask) != 0 &&
2729 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
2730 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
2736 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2737 struct process_block_struct *pb,
2738 blk64_t start_block, blk64_t end_block,
2740 ext2_extent_handle_t ehandle,
2743 struct ext2fs_extent extent;
2744 blk64_t blk, last_lblk;
2746 int is_dir, is_leaf;
2748 struct ext2_extent_info info;
2749 int failed_csum = 0;
2751 if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
2754 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2757 if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
2758 !pb->eti.force_rebuild) {
2759 struct extent_tree_level *etl;
2761 etl = pb->eti.ext_info + info.curr_level;
2762 etl->num_extents += info.num_entries;
2763 etl->max_extents += info.max_entries;
2765 * Implementation wart: Splitting extent blocks when appending
2766 * will leave the old block with one free entry. Therefore
2767 * unless the node is totally full, pretend that a non-root
2768 * extent block can hold one fewer entry than it actually does,
2769 * so that we don't repeatedly rebuild the extent tree.
2771 if (info.curr_level && info.num_entries < info.max_entries)
2775 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2777 while ((pctx->errcode == 0 ||
2778 pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
2779 info.num_entries-- > 0) {
2780 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2781 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2782 last_lblk = extent.e_lblk + extent.e_len - 1;
2785 pctx->blk = extent.e_pblk;
2786 pctx->blk2 = extent.e_lblk;
2787 pctx->num = extent.e_len;
2788 pctx->blkcount = extent.e_lblk + extent.e_len;
2790 if (extent.e_pblk == 0 ||
2791 extent.e_pblk < ctx->fs->super->s_first_data_block ||
2792 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2793 problem = PR_1_EXTENT_BAD_START_BLK;
2794 else if (extent.e_lblk < start_block)
2795 problem = PR_1_OUT_OF_ORDER_EXTENTS;
2796 else if ((end_block && last_lblk > end_block) &&
2797 !(last_lblk > eof_block &&
2798 ((extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) ||
2799 (pctx->inode->i_flags & EXT4_VERITY_FL))))
2800 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
2801 else if (is_leaf && extent.e_len == 0)
2802 problem = PR_1_EXTENT_LENGTH_ZERO;
2804 (extent.e_pblk + extent.e_len) >
2805 ext2fs_blocks_count(ctx->fs->super))
2806 problem = PR_1_EXTENT_ENDS_BEYOND;
2807 else if (is_leaf && is_dir &&
2808 ((extent.e_lblk + extent.e_len) >
2809 (1U << (21 - ctx->fs->super->s_log_block_size))))
2810 problem = PR_1_TOOBIG_DIR;
2812 if (is_leaf && problem == 0 && extent.e_len > 0) {
2814 printf("extent_region(ino=%u, expect=%llu, "
2815 "lblk=%llu, len=%u)\n",
2816 pb->ino, pb->next_lblock,
2817 extent.e_lblk, extent.e_len);
2819 if (extent.e_lblk < pb->next_lblock)
2820 problem = PR_1_EXTENT_COLLISION;
2821 else if (extent.e_lblk + extent.e_len > pb->next_lblock)
2822 pb->next_lblock = extent.e_lblk + extent.e_len;
2826 * Uninitialized blocks in a directory? Clear the flag and
2827 * we'll interpret the blocks later.
2829 if (try_repairs && is_dir && problem == 0 &&
2830 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2831 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
2832 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2833 pb->inode_modified = 1;
2834 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2840 #ifdef CONFIG_DEVELOPER_FEATURES
2841 if (try_repairs && !is_dir && problem == 0 &&
2842 (ctx->options & E2F_OPT_CLEAR_UNINIT) &&
2843 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2844 fix_problem(ctx, PR_1_CLEAR_UNINIT_EXTENT, pctx)) {
2845 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2846 pb->inode_modified = 1;
2847 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2854 if (try_repairs && problem) {
2856 if (fix_problem(ctx, problem, pctx)) {
2857 if (ctx->invalid_bitmaps) {
2859 * If fsck knows the bitmaps are bad,
2860 * skip to the next extent and
2861 * try to clear this extent again
2862 * after fixing the bitmaps, by
2865 pctx->errcode = ext2fs_extent_get(
2867 EXT2_EXTENT_NEXT_SIB,
2869 ctx->flags |= E2F_FLAG_RESTART_LATER;
2870 if (pctx->errcode ==
2871 EXT2_ET_NO_CURRENT_NODE) {
2877 e2fsck_read_bitmaps(ctx);
2878 pb->inode_modified = 1;
2880 ext2fs_extent_delete(ehandle, 0);
2881 if (pctx->errcode) {
2882 pctx->str = "ext2fs_extent_delete";
2885 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2886 if (pctx->errcode &&
2887 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
2888 pctx->str = "ext2fs_extent_fix_parents";
2891 pctx->errcode = ext2fs_extent_get(ehandle,
2892 EXT2_EXTENT_CURRENT,
2894 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2905 blk64_t lblk = extent.e_lblk;
2906 int next_try_repairs = 1;
2908 blk = extent.e_pblk;
2911 * If this lower extent block collides with critical
2912 * metadata, don't try to repair the damage. Pass 1b
2913 * will reallocate the block; then we can try again.
2915 if (pb->ino != EXT2_RESIZE_INO &&
2916 extent.e_pblk < ctx->fs->super->s_blocks_count &&
2917 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
2919 next_try_repairs = 0;
2922 PR_1_CRITICAL_METADATA_COLLISION,
2924 if ((ctx->options & E2F_OPT_NO) == 0)
2925 ctx->flags |= E2F_FLAG_RESTART_LATER;
2927 pctx->errcode = ext2fs_extent_get(ehandle,
2928 EXT2_EXTENT_DOWN, &extent);
2929 if (pctx->errcode &&
2930 pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
2931 pctx->str = "EXT2_EXTENT_DOWN";
2932 problem = PR_1_EXTENT_HEADER_INVALID;
2933 if (!next_try_repairs)
2935 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
2936 goto report_problem;
2939 /* The next extent should match this index's logical start */
2940 if (extent.e_lblk != lblk) {
2941 struct ext2_extent_info e_info;
2943 ext2fs_extent_get_info(ehandle, &e_info);
2945 pctx->blk2 = extent.e_lblk;
2946 pctx->num = e_info.curr_level - 1;
2947 problem = PR_1_EXTENT_INDEX_START_INVALID;
2948 if (fix_problem(ctx, problem, pctx)) {
2949 pb->inode_modified = 1;
2951 ext2fs_extent_fix_parents(ehandle);
2952 if (pctx->errcode) {
2953 pctx->str = "ext2fs_extent_fix_parents";
2958 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
2959 last_lblk, eof_block, ehandle,
2963 pctx->errcode = ext2fs_extent_get(ehandle,
2964 EXT2_EXTENT_UP, &extent);
2965 if (pctx->errcode) {
2966 pctx->str = "EXT2_EXTENT_UP";
2969 mark_block_used(ctx, blk);
2974 if ((pb->previous_block != 0) &&
2975 (pb->previous_block+1 != extent.e_pblk)) {
2976 if (ctx->options & E2F_OPT_FRAGCHECK) {
2981 else if (pb->is_reg)
2984 printf(("%6lu(%c): expecting %6lu "
2986 "phys %6lu log %lu len %lu\n"),
2987 (unsigned long) pctx->ino, type,
2988 (unsigned long) pb->previous_block+1,
2989 (unsigned long) extent.e_pblk,
2990 (unsigned long) extent.e_lblk,
2991 (unsigned long) extent.e_len);
2996 * If we notice a gap in the logical block mappings of an
2997 * extent-mapped directory, offer to close the hole by
2998 * moving the logical block down, otherwise we'll go mad in
2999 * pass 3 allocating empty directory blocks to fill the hole.
3001 if (try_repairs && is_dir &&
3002 pb->last_block + 1 < extent.e_lblk) {
3005 new_lblk = pb->last_block + 1;
3006 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
3007 new_lblk = ((new_lblk +
3008 EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
3009 ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
3011 EXT2FS_CLUSTER_MASK(ctx->fs));
3012 pctx->blk = extent.e_lblk;
3013 pctx->blk2 = new_lblk;
3014 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
3015 extent.e_lblk = new_lblk;
3016 pb->inode_modified = 1;
3017 pctx->errcode = ext2fs_extent_replace(ehandle,
3019 if (pctx->errcode) {
3023 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
3025 goto failed_add_dir_block;
3026 pctx->errcode = ext2fs_extent_goto(ehandle,
3029 goto failed_add_dir_block;
3030 last_lblk = extent.e_lblk + extent.e_len - 1;
3036 while (++pb->last_db_block <
3037 (e2_blkcnt_t) extent.e_lblk) {
3038 pctx->errcode = ext2fs_add_dir_block2(
3042 if (pctx->errcode) {
3044 pctx->num = pb->last_db_block;
3045 goto failed_add_dir_block;
3049 for (i = 0; i < extent.e_len; i++) {
3050 pctx->errcode = ext2fs_add_dir_block2(
3055 if (pctx->errcode) {
3056 pctx->blk = extent.e_pblk + i;
3057 pctx->num = extent.e_lblk + i;
3058 failed_add_dir_block:
3059 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3060 /* Should never get here */
3061 ctx->flags |= E2F_FLAG_ABORT;
3065 if (extent.e_len > 0)
3066 pb->last_db_block = extent.e_lblk + extent.e_len - 1;
3068 if (has_unaligned_cluster_map(ctx, pb->previous_block,
3072 for (i = 0; i < extent.e_len; i++) {
3073 pctx->blk = extent.e_lblk + i;
3074 pctx->blk2 = extent.e_pblk + i;
3075 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3076 mark_block_used(ctx, extent.e_pblk + i);
3077 mark_block_used(ctx, extent.e_pblk + i);
3082 * Check whether first cluster got marked in previous iteration.
3084 if (ctx->fs->cluster_ratio_bits &&
3085 pb->previous_block &&
3086 (EXT2FS_B2C(ctx->fs, extent.e_pblk) ==
3087 EXT2FS_B2C(ctx->fs, pb->previous_block)))
3088 /* Set blk to the beginning of next cluster. */
3091 EXT2FS_B2C(ctx->fs, extent.e_pblk) + 1);
3093 /* Set blk to the beginning of current cluster. */
3094 blk = EXT2FS_C2B(ctx->fs,
3095 EXT2FS_B2C(ctx->fs, extent.e_pblk));
3097 if (blk < extent.e_pblk + extent.e_len) {
3098 mark_blocks_used(ctx, blk,
3099 extent.e_pblk + extent.e_len - blk);
3100 n = DIV_ROUND_UP(extent.e_pblk + extent.e_len - blk,
3101 EXT2FS_CLUSTER_RATIO(ctx->fs));
3102 pb->num_blocks += n;
3104 pb->last_block = extent.e_lblk + extent.e_len - 1;
3105 pb->previous_block = extent.e_pblk + extent.e_len - 1;
3106 start_block = pb->last_block = last_lblk;
3107 if (is_leaf && !is_dir &&
3108 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
3109 pb->last_init_lblock = last_lblk;
3111 pctx->errcode = ext2fs_extent_get(ehandle,
3112 EXT2_EXTENT_NEXT_SIB,
3116 /* Failed csum but passes checks? Ask to fix checksum. */
3118 fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
3119 pb->inode_modified = 1;
3120 pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
3125 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
3129 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
3130 struct process_block_struct *pb)
3132 struct ext2_extent_info info;
3133 struct ext2_inode *inode = pctx->inode;
3134 ext2_extent_handle_t ehandle;
3135 ext2_filsys fs = ctx->fs;
3136 ext2_ino_t ino = pctx->ino;
3139 struct ext3_extent_header *eh;
3141 /* Check for a proper extent header... */
3142 eh = (struct ext3_extent_header *) &inode->i_block[0];
3143 retval = ext2fs_extent_header_verify(eh, sizeof(inode->i_block));
3145 if (fix_problem(ctx, PR_1_MISSING_EXTENT_HEADER, pctx))
3146 e2fsck_clear_inode(ctx, ino, inode, 0,
3147 "check_blocks_extents");
3152 /* ...since this function doesn't fail if i_block is zeroed. */
3153 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
3154 if (pctx->errcode) {
3155 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
3156 e2fsck_clear_inode(ctx, ino, inode, 0,
3157 "check_blocks_extents");
3162 retval = ext2fs_extent_get_info(ehandle, &info);
3164 int max_depth = info.max_depth;
3166 if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
3167 max_depth = MAX_EXTENT_DEPTH_COUNT-1;
3168 ctx->extent_depth_count[max_depth]++;
3171 /* Check maximum extent depth */
3172 pctx->blk = info.max_depth;
3173 pctx->blk2 = ext2fs_max_extent_depth(ehandle);
3174 if (pctx->blk2 < pctx->blk &&
3175 fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
3176 pb->eti.force_rebuild = 1;
3178 /* Can we collect extent tree level stats? */
3179 pctx->blk = MAX_EXTENT_DEPTH_COUNT;
3180 if (pctx->blk2 > pctx->blk)
3181 fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
3182 memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
3183 pb->eti.ino = pb->ino;
3185 pb->next_lblock = 0;
3187 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
3188 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
3189 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
3190 if (pctx->errcode &&
3191 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
3193 inode->i_blocks = 0;
3194 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
3195 "check_blocks_extents");
3198 ext2fs_extent_free(ehandle);
3200 /* Rebuild unless it's a dir and we're rehashing it */
3201 if (LINUX_S_ISDIR(inode->i_mode) &&
3202 e2fsck_dir_will_be_rehashed(ctx, ino))
3205 if (ctx->options & E2F_OPT_CONVERT_BMAP)
3206 e2fsck_rebuild_extents_later(ctx, ino);
3208 e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
3212 * In fact we don't need to check blocks for an inode with inline data
3213 * because this inode doesn't have any blocks. In this function all
3214 * we need to do is add this inode into dblist when it is a directory.
3216 static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
3217 struct process_block_struct *pb)
3220 size_t inline_data_size = 0;
3227 /* Process the dirents in i_block[] as the "first" block. */
3228 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
3232 /* Process the dirents in the EA as a "second" block. */
3233 flags = ctx->fs->flags;
3234 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3235 pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
3237 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3238 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3239 if (pctx->errcode) {
3244 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
3247 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
3255 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3256 ctx->flags |= E2F_FLAG_ABORT;
3260 * This subroutine is called on each inode to account for all of the
3261 * blocks used by that inode.
3263 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
3264 char *block_buf, const struct ea_quota *ea_ibody_quota)
3266 ext2_filsys fs = ctx->fs;
3267 struct process_block_struct pb;
3268 ext2_ino_t ino = pctx->ino;
3269 struct ext2_inode *inode = pctx->inode;
3270 unsigned bad_size = 0;
3271 int dirty_inode = 0;
3275 struct ea_quota ea_block_quota;
3278 pb.num_blocks = EXT2FS_B2C(ctx->fs,
3279 ea_ibody_quota ? ea_ibody_quota->blocks : 0);
3281 pb.last_init_lblock = -1;
3282 pb.last_db_block = -1;
3283 pb.num_illegal_blocks = 0;
3284 pb.suppress = 0; pb.clear = 0;
3287 pb.previous_block = 0;
3288 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
3289 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
3290 pb.max_blocks = 1U << (31 - fs->super->s_log_block_size);
3294 pb.inode_modified = 0;
3295 pb.eti.force_rebuild = 0;
3299 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
3300 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
3302 if (check_ext_attr(ctx, pctx, block_buf, &ea_block_quota)) {
3303 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3305 pb.num_blocks += EXT2FS_B2C(ctx->fs, ea_block_quota.blocks);
3308 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
3309 check_blocks_inline_data(ctx, pctx, &pb);
3310 else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
3311 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
3312 check_blocks_extents(ctx, pctx, &pb);
3316 * If we've modified the inode, write it out before
3317 * iterate() tries to use it.
3320 e2fsck_write_inode(ctx, ino, inode,
3325 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3326 pctx->errcode = ext2fs_block_iterate3(fs, ino,
3327 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
3328 block_buf, process_block, &pb);
3330 * We do not have uninitialized extents in non extent
3333 pb.last_init_lblock = pb.last_block;
3335 * If iterate() changed a block mapping, we have to
3336 * re-read the inode. If we decide to clear the
3337 * inode after clearing some stuff, we'll re-write the
3338 * bad mappings into the inode!
3340 if (pb.inode_modified)
3341 e2fsck_read_inode(ctx, ino, inode,
3343 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3344 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3346 if (ctx->options & E2F_OPT_CONVERT_BMAP) {
3348 printf("bmap rebuild ino=%d\n", ino);
3350 if (!LINUX_S_ISDIR(inode->i_mode) ||
3351 !e2fsck_dir_will_be_rehashed(ctx, ino))
3352 e2fsck_rebuild_extents_later(ctx, ino);
3356 end_problem_latch(ctx, PR_LATCH_BLOCK);
3357 end_problem_latch(ctx, PR_LATCH_TOOBIG);
3358 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3361 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
3363 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
3364 if (LINUX_S_ISDIR(inode->i_mode))
3365 ctx->fs_fragmented_dir++;
3367 ctx->fs_fragmented++;
3371 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
3376 if (inode->i_flags & EXT2_INDEX_FL) {
3377 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
3378 inode->i_flags &= ~EXT2_INDEX_FL;
3381 e2fsck_add_dx_dir(ctx, ino, inode, pb.last_block+1);
3385 if (!pb.num_blocks && pb.is_dir &&
3386 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
3387 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
3388 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
3389 ctx->fs_directory_count--;
3394 if (ino != quota_type2inum(PRJQUOTA, fs->super) &&
3395 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) &&
3396 !(inode->i_flags & EXT4_EA_INODE_FL)) {
3397 quota_data_add(ctx->qctx, (struct ext2_inode_large *) inode,
3399 pb.num_blocks * EXT2_CLUSTER_SIZE(fs->super));
3400 quota_data_inodes(ctx->qctx, (struct ext2_inode_large *) inode,
3401 ino, (ea_ibody_quota ?
3402 ea_ibody_quota->inodes : 0) +
3403 ea_block_quota.inodes + 1);
3406 if (!ext2fs_has_feature_huge_file(fs->super) ||
3407 !(inode->i_flags & EXT4_HUGE_FILE_FL))
3408 pb.num_blocks *= (fs->blocksize / 512);
3409 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
3411 printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
3412 ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
3416 unsigned nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
3417 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
3422 flags = ctx->fs->flags;
3423 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3424 err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
3426 ctx->fs->flags = (flags &
3427 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3429 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3430 if (err || sz != inode->i_size) {
3434 } else if (inode->i_size & (fs->blocksize - 1))
3436 else if (nblock > (pb.last_block + 1))
3438 else if (nblock < (pb.last_block + 1)) {
3439 if (((pb.last_block + 1) - nblock) >
3440 fs->super->s_prealloc_dir_blocks)
3444 size = EXT2_I_SIZE(inode);
3445 if ((pb.last_init_lblock >= 0) &&
3446 /* Do not allow initialized allocated blocks past i_size*/
3447 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
3448 !(inode->i_flags & EXT4_VERITY_FL))
3450 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
3451 size > ext2_max_sizes[fs->super->s_log_block_size])
3452 /* too big for a direct/indirect-mapped file */
3454 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
3456 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
3457 /* too big for an extent-based file - 32bit ee_block */
3460 /* i_size for symlinks is checked elsewhere */
3461 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
3462 /* Did inline_data set pctx->num earlier? */
3464 pctx->num = (pb.last_block + 1) * fs->blocksize;
3465 pctx->group = bad_size;
3466 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
3467 if (LINUX_S_ISDIR(inode->i_mode))
3468 pctx->num &= 0xFFFFFFFFULL;
3469 ext2fs_inode_size_set(fs, inode, pctx->num);
3470 if (EXT2_I_SIZE(inode) == 0 &&
3471 (inode->i_flags & EXT4_INLINE_DATA_FL)) {
3472 memset(inode->i_block, 0,
3473 sizeof(inode->i_block));
3474 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
3480 if (LINUX_S_ISREG(inode->i_mode) &&
3481 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
3483 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
3484 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
3485 (ext2fs_has_feature_huge_file(fs->super) &&
3486 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
3487 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
3488 pctx->num = pb.num_blocks;
3489 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
3490 inode->i_blocks = pb.num_blocks;
3491 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
3498 * The kernel gets mad if we ask it to allocate bigalloc clusters to
3499 * a block mapped file, so rebuild it as an extent file. We can skip
3500 * symlinks because they're never rewritten.
3502 if (ext2fs_has_feature_bigalloc(fs->super) &&
3503 (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
3504 ext2fs_inode_data_blocks2(fs, inode) > 0 &&
3505 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
3506 !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
3507 fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
3508 pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
3513 if (ctx->dirs_to_hash && pb.is_dir &&
3514 !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
3515 !(inode->i_flags & EXT2_INDEX_FL) &&
3516 ((inode->i_size / fs->blocksize) >= 3))
3517 e2fsck_rehash_dir_later(ctx, ino);
3521 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
3526 * Helper function called by process block when an illegal block is
3527 * found. It returns a description about why the block is illegal
3529 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
3533 static char problem[80];
3535 super = fs->super->s_first_data_block;
3536 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
3537 if (block < super) {
3538 sprintf(problem, "< FIRSTBLOCK (%u)", super);
3540 } else if (block >= ext2fs_blocks_count(fs->super)) {
3541 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
3544 for (i = 0; i < fs->group_desc_count; i++) {
3545 if (block == super) {
3546 sprintf(problem, "is the superblock in group %d", i);
3549 if (block > super &&
3550 block <= (super + fs->desc_blocks)) {
3551 sprintf(problem, "is in the group descriptors "
3555 if (block == ext2fs_block_bitmap_loc(fs, i)) {
3556 sprintf(problem, "is the block bitmap of group %d", i);
3559 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
3560 sprintf(problem, "is the inode bitmap of group %d", i);
3563 if (block >= ext2fs_inode_table_loc(fs, i) &&
3564 (block < ext2fs_inode_table_loc(fs, i)
3565 + fs->inode_blocks_per_group)) {
3566 sprintf(problem, "is in the inode table of group %d",
3570 super += fs->super->s_blocks_per_group;
3577 * This is a helper function for check_blocks().
3579 static int process_block(ext2_filsys fs,
3581 e2_blkcnt_t blockcnt,
3582 blk64_t ref_block EXT2FS_ATTR((unused)),
3583 int ref_offset EXT2FS_ATTR((unused)),
3586 struct process_block_struct *p;
3587 struct problem_context *pctx;
3588 blk64_t blk = *block_nr;
3590 problem_t problem = 0;
3593 p = (struct process_block_struct *) priv_data;
3598 * For a directory, add logical block zero for processing even if it's
3599 * not mapped or we'll be perennially stuck with broken "." and ".."
3602 if (p->is_dir && blockcnt == 0 && blk == 0) {
3603 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
3604 if (pctx->errcode) {
3606 pctx->num = blockcnt;
3607 goto failed_add_dir_block;
3616 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
3621 * Simplistic fragmentation check. We merely require that the
3622 * file be contiguous. (Which can never be true for really
3623 * big files that are greater than a block group.)
3625 if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
3626 if (p->previous_block+1 != blk) {
3627 if (ctx->options & E2F_OPT_FRAGCHECK) {
3635 printf(_("%6lu(%c): expecting %6lu "
3636 "got phys %6lu (blkcnt %lld)\n"),
3637 (unsigned long) pctx->ino, type,
3638 (unsigned long) p->previous_block+1,
3639 (unsigned long) blk,
3646 if (p->is_dir && !ext2fs_has_feature_largedir(fs->super) &&
3647 blockcnt > (1 << (21 - fs->super->s_log_block_size)))
3648 problem = PR_1_TOOBIG_DIR;
3649 if (p->is_dir && p->num_blocks + 1 >= p->max_blocks)
3650 problem = PR_1_TOOBIG_DIR;
3651 if (p->is_reg && p->num_blocks + 1 >= p->max_blocks)
3652 problem = PR_1_TOOBIG_REG;
3653 if (!p->is_dir && !p->is_reg && blockcnt > 0)
3654 problem = PR_1_TOOBIG_SYMLINK;
3656 if (blk < fs->super->s_first_data_block ||
3657 blk >= ext2fs_blocks_count(fs->super))
3658 problem = PR_1_ILLEGAL_BLOCK_NUM;
3661 * If this IND/DIND/TIND block is squatting atop some critical metadata
3662 * (group descriptors, superblock, bitmap, inode table), any write to
3663 * "fix" mapping problems will destroy the metadata. We'll let pass 1b
3664 * fix that and restart fsck.
3667 p->ino != EXT2_RESIZE_INO &&
3668 blk < ctx->fs->super->s_blocks_count &&
3669 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
3671 fix_problem(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx);
3672 if ((ctx->options & E2F_OPT_NO) == 0)
3673 ctx->flags |= E2F_FLAG_RESTART_LATER;
3677 p->num_illegal_blocks++;
3679 * A bit of subterfuge here -- we're trying to fix a block
3680 * mapping, but the IND/DIND/TIND block could have collided
3681 * with some critical metadata. So, fix the in-core mapping so
3682 * iterate won't go insane, but return 0 instead of
3683 * BLOCK_CHANGED so that it won't write the remapping out to
3684 * our multiply linked block.
3686 * Even if we previously determined that an *IND block
3687 * conflicts with critical metadata, we must still try to
3688 * iterate the *IND block as if it is an *IND block to find and
3689 * mark the blocks it points to. Better to be overly cautious
3690 * with the used_blocks map so that we don't move the *IND
3691 * block to a block that's really in use!
3693 if (p->ino != EXT2_RESIZE_INO &&
3695 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
3700 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
3701 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
3705 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
3707 set_latch_flags(PR_LATCH_BLOCK,
3712 pctx->blkcount = blockcnt;
3713 if (fix_problem(ctx, problem, pctx)) {
3714 blk = *block_nr = 0;
3715 ret_code = BLOCK_CHANGED;
3716 p->inode_modified = 1;
3718 * If the directory block is too big and is beyond the
3719 * end of the FS, don't bother trying to add it for
3720 * processing -- the kernel would never have created a
3721 * directory this large, and we risk an ENOMEM abort.
3722 * In any case, the toobig handler for extent-based
3723 * directories also doesn't feed toobig blocks to
3726 if (problem == PR_1_TOOBIG_DIR)
3733 if (p->ino == EXT2_RESIZE_INO) {
3735 * The resize inode has already be sanity checked
3736 * during pass #0 (the superblock checks). All we
3737 * have to do is mark the double indirect block as
3738 * being in use; all of the other blocks are handled
3739 * by mark_table_blocks()).
3741 if (blockcnt == BLOCK_COUNT_DIND)
3742 mark_block_used(ctx, blk);
3744 } else if (!(ctx->fs->cluster_ratio_bits &&
3745 p->previous_block &&
3746 (EXT2FS_B2C(ctx->fs, blk) ==
3747 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
3748 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
3749 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
3750 mark_block_used(ctx, blk);
3752 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
3753 p->last_block, blk, blockcnt)) {
3754 pctx->blk = blockcnt;
3756 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3757 mark_block_used(ctx, blk);
3758 mark_block_used(ctx, blk);
3761 p->last_block = blockcnt;
3762 p->previous_block = blk;
3764 if (p->is_dir && (blockcnt >= 0)) {
3765 while (++p->last_db_block < blockcnt) {
3766 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
3769 if (pctx->errcode) {
3771 pctx->num = p->last_db_block;
3772 goto failed_add_dir_block;
3775 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
3777 if (pctx->errcode) {
3779 pctx->num = blockcnt;
3780 failed_add_dir_block:
3781 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3782 /* Should never get here */
3783 ctx->flags |= E2F_FLAG_ABORT;
3790 static int process_bad_block(ext2_filsys fs,
3792 e2_blkcnt_t blockcnt,
3793 blk64_t ref_block EXT2FS_ATTR((unused)),
3794 int ref_offset EXT2FS_ATTR((unused)),
3797 struct process_block_struct *p;
3798 blk64_t blk = *block_nr;
3799 blk64_t first_block;
3801 struct problem_context *pctx;
3807 p = (struct process_block_struct *) priv_data;
3811 pctx->ino = EXT2_BAD_INO;
3813 pctx->blkcount = blockcnt;
3815 if ((blk < fs->super->s_first_data_block) ||
3816 (blk >= ext2fs_blocks_count(fs->super))) {
3817 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
3819 return BLOCK_CHANGED;
3825 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
3827 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
3829 return BLOCK_CHANGED;
3831 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3834 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
3837 return BLOCK_CHANGED;
3839 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3842 mark_block_used(ctx, blk);
3846 printf ("DEBUG: Marking %u as bad.\n", blk);
3848 ctx->fs_badblocks_count++;
3850 * If the block is not used, then mark it as used and return.
3851 * If it is already marked as found, this must mean that
3852 * there's an overlap between the filesystem table blocks
3853 * (bitmaps and inode table) and the bad block list.
3855 if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
3856 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3860 * Try to find the where the filesystem block was used...
3862 first_block = fs->super->s_first_data_block;
3864 for (i = 0; i < fs->group_desc_count; i++ ) {
3867 if (!ext2fs_bg_has_super(fs, i))
3869 if (blk == first_block) {
3871 if (fix_problem(ctx,
3872 PR_1_BAD_PRIMARY_SUPERBLOCK,
3875 return BLOCK_CHANGED;
3879 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
3882 if ((blk > first_block) &&
3883 (blk <= first_block + fs->desc_blocks)) {
3885 pctx->blk = *block_nr;
3886 if (fix_problem(ctx,
3887 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
3889 return BLOCK_CHANGED;
3893 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
3897 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
3898 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
3899 ctx->invalid_block_bitmap_flag[i]++;
3900 ctx->invalid_bitmaps++;
3904 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
3905 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
3906 ctx->invalid_inode_bitmap_flag[i]++;
3907 ctx->invalid_bitmaps++;
3911 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
3912 (blk < (ext2fs_inode_table_loc(fs, i) +
3913 fs->inode_blocks_per_group))) {
3915 * If there are bad blocks in the inode table,
3916 * the inode scan code will try to do
3917 * something reasonable automatically.
3921 first_block += fs->super->s_blocks_per_group;
3924 * If we've gotten to this point, then the only
3925 * possibility is that the bad block inode meta data
3926 * is using a bad block.
3928 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
3929 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
3930 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
3932 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
3934 return BLOCK_CHANGED;
3936 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3943 /* Warn user that the block wasn't claimed */
3944 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
3949 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
3950 const char *name, int num, blk64_t *new_block)
3952 ext2_filsys fs = ctx->fs;
3954 blk64_t old_block = *new_block;
3957 unsigned flexbg_size;
3960 struct problem_context pctx;
3962 clear_problem_context(&pctx);
3965 pctx.blk = old_block;
3969 * For flex_bg filesystems, first try to allocate the metadata
3970 * within the flex_bg, and if that fails then try finding the
3971 * space anywhere in the filesystem.
3973 is_flexbg = ext2fs_has_feature_flex_bg(fs->super);
3975 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3976 flexbg = group / flexbg_size;
3977 first_block = ext2fs_group_first_block2(fs,
3978 flexbg_size * flexbg);
3979 last_grp = group | (flexbg_size - 1);
3980 if (last_grp >= fs->group_desc_count)
3981 last_grp = fs->group_desc_count - 1;
3982 last_block = ext2fs_group_last_block2(fs, last_grp);
3984 last_block = ext2fs_group_last_block2(fs, group);
3985 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
3986 num, ctx->block_found_map,
3988 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
3989 pctx.errcode = ext2fs_get_free_blocks2(fs,
3990 fs->super->s_first_data_block,
3991 ext2fs_blocks_count(fs->super),
3992 num, ctx->block_found_map, new_block);
3995 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
3996 ext2fs_unmark_valid(fs);
3997 ctx->flags |= E2F_FLAG_ABORT;
4000 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
4002 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
4003 ext2fs_unmark_valid(fs);
4004 ctx->flags |= E2F_FLAG_ABORT;
4007 ext2fs_mark_super_dirty(fs);
4008 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
4009 pctx.blk2 = *new_block;
4010 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
4011 PR_1_RELOC_TO), &pctx);
4013 for (i = 0; i < num; i++) {
4015 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
4017 pctx.errcode = io_channel_read_blk64(fs->io,
4018 old_block + i, 1, buf);
4020 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
4021 pctx.blk = (*new_block) + i;
4022 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
4025 pctx.blk = (*new_block) + i;
4026 pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
4031 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
4033 ext2fs_free_mem(&buf);
4037 * This routine gets called at the end of pass 1 if bad blocks are
4038 * detected in the superblock, group descriptors, inode_bitmaps, or
4039 * block bitmaps. At this point, all of the blocks have been mapped
4040 * out, so we can try to allocate new block(s) to replace the bad
4043 static void handle_fs_bad_blocks(e2fsck_t ctx)
4045 ext2_filsys fs = ctx->fs;
4047 blk64_t first_block;
4050 for (i = 0; i < fs->group_desc_count; i++) {
4051 first_block = ext2fs_group_first_block2(fs, i);
4053 if (ctx->invalid_block_bitmap_flag[i]) {
4054 new_blk = ext2fs_block_bitmap_loc(fs, i);
4055 new_table_block(ctx, first_block, i, _("block bitmap"),
4057 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
4059 if (ctx->invalid_inode_bitmap_flag[i]) {
4060 new_blk = ext2fs_inode_bitmap_loc(fs, i);
4061 new_table_block(ctx, first_block, i, _("inode bitmap"),
4063 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
4065 if (ctx->invalid_inode_table_flag[i]) {
4066 new_blk = ext2fs_inode_table_loc(fs, i);
4067 new_table_block(ctx, first_block, i, _("inode table"),
4068 fs->inode_blocks_per_group,
4070 ext2fs_inode_table_loc_set(fs, i, new_blk);
4071 ctx->flags |= E2F_FLAG_RESTART;
4074 ctx->invalid_bitmaps = 0;
4078 * This routine marks all blocks which are used by the superblock,
4079 * group descriptors, inode bitmaps, and block bitmaps.
4081 static void mark_table_blocks(e2fsck_t ctx)
4083 ext2_filsys fs = ctx->fs;
4087 struct problem_context pctx;
4089 clear_problem_context(&pctx);
4091 for (i = 0; i < fs->group_desc_count; i++) {
4094 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
4095 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
4098 * Mark the blocks used for the inode table
4100 if (ext2fs_inode_table_loc(fs, i)) {
4101 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
4102 j < fs->inode_blocks_per_group;
4104 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4107 if (!ctx->invalid_inode_table_flag[i] &&
4109 PR_1_ITABLE_CONFLICT, &pctx)) {
4110 ctx->invalid_inode_table_flag[i]++;
4111 ctx->invalid_bitmaps++;
4114 ext2fs_mark_block_bitmap2(
4115 ctx->block_found_map, b);
4116 ext2fs_mark_block_bitmap2(
4117 ctx->block_metadata_map, b);
4123 * Mark block used for the block bitmap
4125 if (ext2fs_block_bitmap_loc(fs, i)) {
4126 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4127 ext2fs_block_bitmap_loc(fs, i))) {
4128 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
4129 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
4130 ctx->invalid_block_bitmap_flag[i]++;
4131 ctx->invalid_bitmaps++;
4134 ext2fs_mark_block_bitmap2(ctx->block_found_map,
4135 ext2fs_block_bitmap_loc(fs, i));
4136 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
4137 ext2fs_block_bitmap_loc(fs, i));
4141 * Mark block used for the inode bitmap
4143 if (ext2fs_inode_bitmap_loc(fs, i)) {
4144 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4145 ext2fs_inode_bitmap_loc(fs, i))) {
4146 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
4147 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
4148 ctx->invalid_inode_bitmap_flag[i]++;
4149 ctx->invalid_bitmaps++;
4152 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
4153 ext2fs_inode_bitmap_loc(fs, i));
4154 ext2fs_mark_block_bitmap2(ctx->block_found_map,
4155 ext2fs_inode_bitmap_loc(fs, i));
4162 * These subroutines short circuits ext2fs_get_blocks and
4163 * ext2fs_check_directory; we use them since we already have the inode
4164 * structure, so there's no point in letting the ext2fs library read
4167 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
4170 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4173 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4174 return EXT2_ET_CALLBACK_NOTHANDLED;
4176 for (i=0; i < EXT2_N_BLOCKS; i++)
4177 blocks[i] = ctx->stashed_inode->i_block[i];
4181 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
4182 struct ext2_inode *inode)
4184 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4186 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4187 return EXT2_ET_CALLBACK_NOTHANDLED;
4188 *inode = *ctx->stashed_inode;
4192 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
4193 struct ext2_inode *inode)
4195 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4197 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
4198 (inode != ctx->stashed_inode))
4199 *ctx->stashed_inode = *inode;
4200 return EXT2_ET_CALLBACK_NOTHANDLED;
4203 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
4205 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4207 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4208 return EXT2_ET_CALLBACK_NOTHANDLED;
4210 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
4211 return EXT2_ET_NO_DIRECTORY;
4215 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
4218 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4222 if (ctx->block_found_map) {
4223 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
4227 if (fs->block_map) {
4228 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
4229 ext2fs_mark_bb_dirty(fs);
4232 if (!fs->block_map) {
4233 retval = ext2fs_read_block_bitmap(fs);
4238 retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
4247 static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
4248 blk64_t len, blk64_t *pblk, blk64_t *plen)
4250 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4253 if (ctx->block_found_map)
4254 return ext2fs_new_range(fs, flags, goal, len,
4255 ctx->block_found_map, pblk, plen);
4257 if (!fs->block_map) {
4258 retval = ext2fs_read_block_bitmap(fs);
4263 return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
4267 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
4269 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4271 /* Never free a critical metadata block */
4272 if (ctx->block_found_map &&
4273 ctx->block_metadata_map &&
4275 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
4278 if (ctx->block_found_map) {
4280 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
4282 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
4286 static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
4287 blk_t num, int inuse)
4289 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4291 /* Never free a critical metadata block */
4292 if (ctx->block_found_map &&
4293 ctx->block_metadata_map &&
4295 ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
4298 if (ctx->block_found_map) {
4300 ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
4303 ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
4308 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
4310 ext2_filsys fs = ctx->fs;
4312 if (use_shortcuts) {
4313 fs->get_blocks = pass1_get_blocks;
4314 fs->check_directory = pass1_check_directory;
4315 fs->read_inode = pass1_read_inode;
4316 fs->write_inode = pass1_write_inode;
4317 ctx->stashed_ino = 0;
4320 fs->check_directory = 0;
4322 fs->write_inode = 0;
4326 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
4328 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
4329 ext2fs_set_block_alloc_stats_callback(ctx->fs,
4330 e2fsck_block_alloc_stats, 0);
4331 ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
4332 ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
4333 e2fsck_block_alloc_stats_range, NULL);