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 * - A bitmap of EA inodes. (inode_ea_map)
31 * Pass 1 is designed to stash away enough information so that the
32 * other passes should not need to read in the inode information
33 * during the normal course of a filesystem check. (Althogh if an
34 * inconsistency is detected, other passes may need to read in an
37 * Note that pass 1B will be invoked if there are any duplicate blocks
41 #define _GNU_SOURCE 1 /* get strnlen() */
50 #include <ext2fs/ext2_ext_attr.h>
54 #ifdef NO_INLINE_FUNCS
57 #define _INLINE_ inline
62 static int process_block(ext2_filsys fs, blk64_t *blocknr,
63 e2_blkcnt_t blockcnt, blk64_t ref_blk,
64 int ref_offset, void *priv_data);
65 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
66 e2_blkcnt_t blockcnt, blk64_t ref_blk,
67 int ref_offset, void *priv_data);
68 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
69 char *block_buf, blk64_t ea_ibody_quota_blocks);
70 static void mark_table_blocks(e2fsck_t ctx);
71 static void alloc_bb_map(e2fsck_t ctx);
72 static void alloc_imagic_map(e2fsck_t ctx);
73 static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
74 static void add_encrypted_dir(e2fsck_t ctx, ino_t ino);
75 static void handle_fs_bad_blocks(e2fsck_t ctx);
76 static void process_inodes(e2fsck_t ctx, char *block_buf);
77 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
78 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
79 dgrp_t group, void * priv_data);
80 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
81 char *block_buf, int adjust_sign);
82 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
84 struct process_block_struct {
86 unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
87 fragmented:1, compressed:1, bbcheck:1,
92 e2_blkcnt_t last_init_lblock;
93 e2_blkcnt_t last_db_block;
94 int num_illegal_blocks;
95 blk64_t previous_block;
96 struct ext2_inode *inode;
97 struct problem_context *pctx;
98 ext2fs_block_bitmap fs_meta_blocks;
101 struct extent_tree_info eti;
104 struct process_inode_block {
106 blk64_t ea_ibody_quota_blocks;
107 struct ext2_inode_large inode;
110 struct scan_callback_struct {
116 * For the inodes to process list.
118 static struct process_inode_block *inodes_to_process;
119 static int process_inode_count;
121 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
122 EXT2_MIN_BLOCK_LOG_SIZE + 1];
125 * Free all memory allocated by pass1 in preparation for restarting
128 static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
130 ext2fs_free_mem(&inodes_to_process);
131 inodes_to_process = 0;
135 * Check to make sure a device inode is real. Returns 1 if the device
136 * checks out, 0 if not.
138 * Note: this routine is now also used to check FIFO's and Sockets,
139 * since they have the same requirement; the i_block fields should be
142 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
143 struct ext2_inode *inode)
148 * If the index flag is set, then this is a bogus
151 if (inode->i_flags & EXT2_INDEX_FL)
155 * We should be able to do the test below all the time, but
156 * because the kernel doesn't forcibly clear the device
157 * inode's additional i_block fields, there are some rare
158 * occasions when a legitimate device inode will have non-zero
159 * additional i_block fields. So for now, we only complain
160 * when the immutable flag is set, which should never happen
161 * for devices. (And that's when the problem is caused, since
162 * you can't set or clear immutable flags for devices.) Once
163 * the kernel has been fixed we can change this...
165 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
166 for (i=4; i < EXT2_N_BLOCKS; i++)
167 if (inode->i_block[i])
174 * Check to make sure a symlink inode is real. Returns 1 if the symlink
175 * checks out, 0 if not.
177 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
178 struct ext2_inode *inode, char *buf)
183 ext2_extent_handle_t handle;
184 struct ext2_extent_info info;
185 struct ext2fs_extent extent;
187 if ((inode->i_size_high || inode->i_size == 0) ||
188 (inode->i_flags & EXT2_INDEX_FL))
191 if (inode->i_flags & EXT4_EXTENTS_FL) {
192 if (inode->i_flags & EXT4_INLINE_DATA_FL)
194 if (inode->i_size > fs->blocksize)
196 if (ext2fs_extent_open2(fs, ino, inode, &handle))
199 if (ext2fs_extent_get_info(handle, &info) ||
200 (info.num_entries != 1) ||
201 (info.max_depth != 0))
203 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
204 (extent.e_lblk != 0) ||
205 (extent.e_len != 1) ||
206 (extent.e_pblk < fs->super->s_first_data_block) ||
207 (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
211 ext2fs_extent_free(handle);
215 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
218 if (ext2fs_inline_data_size(fs, ino, &inline_size))
220 if (inode->i_size != inline_size)
226 blocks = ext2fs_inode_data_blocks2(fs, inode);
228 if (inode->i_flags & EXT4_INLINE_DATA_FL)
230 if ((inode->i_size >= fs->blocksize) ||
231 (blocks != fs->blocksize >> 9) ||
232 (inode->i_block[0] < fs->super->s_first_data_block) ||
233 (inode->i_block[0] >= ext2fs_blocks_count(fs->super)))
236 for (i = 1; i < EXT2_N_BLOCKS; i++)
237 if (inode->i_block[i])
240 if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf))
243 if (inode->i_flags & EXT4_ENCRYPT_FL) {
244 len = ext2fs_le32_to_cpu(*((__u32 *)buf)) + 4;
246 len = strnlen(buf, fs->blocksize);
248 if (len == fs->blocksize)
250 } else if (inode->i_flags & EXT4_INLINE_DATA_FL) {
251 char *inline_buf = NULL;
252 size_t inline_sz = 0;
254 if (ext2fs_inline_data_size(fs, ino, &inline_sz))
256 if (inode->i_size != inline_sz)
258 if (ext2fs_get_mem(inline_sz + 1, &inline_buf))
261 if (ext2fs_inline_data_get(fs, ino, inode, inline_buf, NULL))
263 inline_buf[inline_sz] = 0;
264 len = strnlen(inline_buf, inline_sz);
265 if (len != inline_sz)
269 ext2fs_free_mem(&inline_buf);
272 if (inode->i_size >= sizeof(inode->i_block))
275 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
276 if (len == sizeof(inode->i_block))
279 if (len != inode->i_size)
280 if ((inode->i_flags & EXT4_ENCRYPT_FL) == 0)
286 * If the extents or inlinedata flags are set on the inode, offer to clear 'em.
288 #define BAD_SPECIAL_FLAGS (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)
289 static void check_extents_inlinedata(e2fsck_t ctx,
290 struct problem_context *pctx)
292 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
295 if (!fix_problem(ctx, PR_1_SPECIAL_EXTENTS_IDATA, pctx))
298 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
299 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
301 #undef BAD_SPECIAL_FLAGS
304 * If the immutable (or append-only) flag is set on the inode, offer
307 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
308 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
310 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
313 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
316 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
317 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
321 * If device, fifo or socket, check size is zero -- if not offer to
324 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
326 struct ext2_inode *inode = pctx->inode;
328 if (EXT2_I_SIZE(inode) == 0)
331 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
334 ext2fs_inode_size_set(ctx->fs, inode, 0);
335 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
338 static void mark_inode_ea_map(e2fsck_t ctx, struct problem_context *pctx,
341 if (!ctx->inode_ea_map) {
342 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
346 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
352 ext2fs_mark_inode_bitmap2(ctx->inode_ea_map, ino);
356 * For a given size, calculate how many blocks would be charged towards quota.
358 static blk64_t size_to_quota_blocks(ext2_filsys fs, size_t size)
362 clusters = DIV_ROUND_UP(size, fs->blocksize << fs->cluster_ratio_bits);
363 return EXT2FS_C2B(fs, clusters);
367 * Check validity of EA inode. Return 0 if EA inode is valid, otherwise return
370 static problem_t check_large_ea_inode(e2fsck_t ctx,
371 struct ext2_ext_attr_entry *entry,
372 struct problem_context *pctx,
373 blk64_t *quota_blocks)
375 struct ext2_inode inode;
379 /* Check if inode is within valid range */
380 if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
381 (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
382 pctx->num = entry->e_value_inum;
383 return PR_1_ATTR_VALUE_EA_INODE;
386 e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
388 retval = ext2fs_ext_attr_hash_entry2(ctx->fs, entry, NULL, &hash);
390 com_err("check_large_ea_inode", retval,
391 _("while hashing entry with e_value_inum = %u"),
392 entry->e_value_inum);
396 if (hash == entry->e_hash) {
397 *quota_blocks = size_to_quota_blocks(ctx->fs,
398 entry->e_value_size);
400 /* This might be an old Lustre-style ea_inode reference. */
401 if (inode.i_mtime == pctx->ino &&
402 inode.i_generation == pctx->inode->i_generation) {
405 /* If target inode is also missing EA_INODE flag,
406 * this is likely to be a bad reference.
408 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
409 pctx->num = entry->e_value_inum;
410 return PR_1_ATTR_VALUE_EA_INODE;
412 pctx->num = entry->e_hash;
413 return PR_1_ATTR_HASH;
418 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
419 pctx->num = entry->e_value_inum;
420 if (fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
421 inode.i_flags |= EXT4_EA_INODE_FL;
422 ext2fs_write_inode(ctx->fs, entry->e_value_inum,
425 return PR_1_ATTR_NO_EA_INODE_FL;
431 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx,
432 blk64_t *ea_ibody_quota_blocks)
434 struct ext2_super_block *sb = ctx->fs->super;
435 struct ext2_inode_large *inode;
436 struct ext2_ext_attr_entry *entry;
437 char *start, *header;
438 unsigned int storage_size, remain;
439 problem_t problem = 0;
441 blk64_t quota_blocks = 0;
443 *ea_ibody_quota_blocks = 0;
445 inode = (struct ext2_inode_large *) pctx->inode;
446 storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
447 inode->i_extra_isize;
448 header = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
449 inode->i_extra_isize;
450 start = header + sizeof(__u32);
451 entry = (struct ext2_ext_attr_entry *) start;
453 /* scan all entry's headers first */
455 /* take finish entry 0UL into account */
456 remain = storage_size - sizeof(__u32);
458 region = region_create(0, storage_size);
460 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
462 ctx->flags |= E2F_FLAG_ABORT;
465 if (region_allocate(region, 0, sizeof(__u32))) {
466 problem = PR_1_INODE_EA_ALLOC_COLLISION;
470 while (remain >= sizeof(struct ext2_ext_attr_entry) &&
471 !EXT2_EXT_IS_LAST_ENTRY(entry)) {
474 if (region_allocate(region, (char *)entry - (char *)header,
475 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
476 problem = PR_1_INODE_EA_ALLOC_COLLISION;
480 /* header eats this space */
481 remain -= sizeof(struct ext2_ext_attr_entry);
483 /* is attribute name valid? */
484 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
485 pctx->num = entry->e_name_len;
486 problem = PR_1_ATTR_NAME_LEN;
490 /* attribute len eats this space */
491 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
493 if (entry->e_value_inum == 0) {
494 /* check value size */
495 if (entry->e_value_size > remain) {
496 pctx->num = entry->e_value_size;
497 problem = PR_1_ATTR_VALUE_SIZE;
501 if (entry->e_value_size &&
502 region_allocate(region,
503 sizeof(__u32) + entry->e_value_offs,
505 entry->e_value_size))) {
506 problem = PR_1_INODE_EA_ALLOC_COLLISION;
510 hash = ext2fs_ext_attr_hash_entry(entry,
511 start + entry->e_value_offs);
513 /* e_hash may be 0 in older inode's ea */
514 if (entry->e_hash != 0 && entry->e_hash != hash) {
515 pctx->num = entry->e_hash;
516 problem = PR_1_ATTR_HASH;
520 blk64_t entry_quota_blocks;
522 problem = check_large_ea_inode(ctx, entry, pctx,
523 &entry_quota_blocks);
527 mark_inode_ea_map(ctx, pctx, entry->e_value_inum);
528 quota_blocks += entry_quota_blocks;
531 /* If EA value is stored in external inode then it does not
532 * consume space here */
533 if (entry->e_value_inum == 0)
534 remain -= entry->e_value_size;
536 entry = EXT2_EXT_ATTR_NEXT(entry);
539 if (region_allocate(region, (char *)entry - (char *)header,
541 problem = PR_1_INODE_EA_ALLOC_COLLISION;
548 * it seems like a corruption. it's very unlikely we could repair
549 * EA(s) in automatic fashion -bzzz
551 if (problem == 0 || !fix_problem(ctx, problem, pctx)) {
552 *ea_ibody_quota_blocks = quota_blocks;
556 /* simply remove all possible EA(s) */
557 *((__u32 *)header) = 0UL;
558 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
559 EXT2_INODE_SIZE(sb), "pass1");
562 static int check_inode_extra_negative_epoch(__u32 xtime, __u32 extra) {
563 return (xtime & (1U << 31)) != 0 &&
564 (extra & EXT4_EPOCH_MASK) == EXT4_EPOCH_MASK;
567 #define CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, xtime) \
568 check_inode_extra_negative_epoch(inode->i_##xtime, \
569 inode->i_##xtime##_extra)
571 /* When today's date is earlier than 2242, we assume that atimes,
572 * ctimes, crtimes, and mtimes with years in the range 2310..2378 are
573 * actually pre-1970 dates mis-encoded.
575 #define EXT4_EXTRA_NEGATIVE_DATE_CUTOFF 2 * (1LL << 32)
577 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx,
578 blk64_t *ea_ibody_quota_blocks)
580 struct ext2_super_block *sb = ctx->fs->super;
581 struct ext2_inode_large *inode;
585 *ea_ibody_quota_blocks = 0;
587 inode = (struct ext2_inode_large *) pctx->inode;
588 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
589 /* this isn't large inode. so, nothing to check */
594 printf("inode #%u, i_extra_size %d\n", pctx->ino,
595 inode->i_extra_isize);
597 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
598 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
599 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
601 * For now we will allow i_extra_isize to be 0, but really
602 * implementations should never allow i_extra_isize to be 0
604 if (inode->i_extra_isize &&
605 (inode->i_extra_isize < min || inode->i_extra_isize > max ||
606 inode->i_extra_isize & 3)) {
607 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
609 if (inode->i_extra_isize < min || inode->i_extra_isize > max)
610 inode->i_extra_isize = sb->s_want_extra_isize;
612 inode->i_extra_isize = (inode->i_extra_isize + 3) & ~3;
613 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
614 EXT2_INODE_SIZE(sb), "pass1");
617 /* check if there is no place for an EA header */
618 if (inode->i_extra_isize >= max - sizeof(__u32))
621 eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
622 inode->i_extra_isize);
623 if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
624 /* it seems inode has an extended attribute(s) in body */
625 check_ea_in_inode(ctx, pctx, ea_ibody_quota_blocks);
629 * If the inode's extended atime (ctime, crtime, mtime) is stored in
630 * the old, invalid format, repair it.
632 if (((sizeof(time_t) <= 4) ||
633 (((sizeof(time_t) > 4) &&
634 ctx->now < EXT4_EXTRA_NEGATIVE_DATE_CUTOFF))) &&
635 (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime) ||
636 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime) ||
637 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime) ||
638 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))) {
640 if (!fix_problem(ctx, PR_1_EA_TIME_OUT_OF_RANGE, pctx))
643 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, atime))
644 inode->i_atime_extra &= ~EXT4_EPOCH_MASK;
645 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, ctime))
646 inode->i_ctime_extra &= ~EXT4_EPOCH_MASK;
647 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, crtime))
648 inode->i_crtime_extra &= ~EXT4_EPOCH_MASK;
649 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, mtime))
650 inode->i_mtime_extra &= ~EXT4_EPOCH_MASK;
651 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
652 EXT2_INODE_SIZE(sb), "pass1");
658 * Check to see if the inode might really be a directory, despite i_mode
660 * This is a lot of complexity for something for which I'm not really
661 * convinced happens frequently in the wild. If for any reason this
662 * causes any problems, take this code out.
663 * [tytso:20070331.0827EDT]
665 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
668 struct ext2_inode *inode = pctx->inode;
669 struct ext2_dir_entry *dirent;
672 unsigned int i, rec_len, not_device = 0;
677 * If the mode looks OK, we believe it. If the first block in
678 * the i_block array is 0, this cannot be a directory. If the
679 * inode is extent-mapped, it is still the case that the latter
680 * cannot be 0 - the magic number in the extent header would make
683 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
684 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
688 * Check the block numbers in the i_block array for validity:
689 * zero blocks are skipped (but the first one cannot be zero -
690 * see above), other blocks are checked against the first and
691 * max data blocks (from the the superblock) and against the
692 * block bitmap. Any invalid block found means this cannot be
695 * If there are non-zero blocks past the fourth entry, then
696 * this cannot be a device file: we remember that for the next
699 * For extent mapped files, we don't do any sanity checking:
700 * just try to get the phys block of logical block 0 and run
703 * For inline data files, we just try to get the size of inline
704 * data. If it's true, we will treat it as a directory.
707 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
708 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
709 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL)) {
712 unsigned int rec_len2;
713 struct ext2_dir_entry de;
715 if (ext2fs_inline_data_size(ctx->fs, pctx->ino, &size))
718 * If the size isn't a multiple of 4, it's probably not a
724 * If the first 10 bytes don't look like a directory entry,
725 * it's probably not a directory.
727 memcpy(&dotdot, inode->i_block, sizeof(dotdot));
728 memcpy(&de, ((char *)inode->i_block) + EXT4_INLINE_DATA_DOTDOT_SIZE,
729 EXT2_DIR_REC_LEN(0));
730 dotdot = ext2fs_le32_to_cpu(dotdot);
731 de.inode = ext2fs_le32_to_cpu(de.inode);
732 de.rec_len = ext2fs_le16_to_cpu(de.rec_len);
733 ext2fs_get_rec_len(ctx->fs, &de, &rec_len2);
734 if (dotdot >= ctx->fs->super->s_inodes_count ||
735 (dotdot < EXT2_FIRST_INO(ctx->fs->super) &&
736 dotdot != EXT2_ROOT_INO) ||
737 de.inode >= ctx->fs->super->s_inodes_count ||
738 (de.inode < EXT2_FIRST_INO(ctx->fs->super) &&
740 rec_len2 > EXT4_MIN_INLINE_DATA_SIZE -
741 EXT4_INLINE_DATA_DOTDOT_SIZE)
743 /* device files never have a "system.data" entry */
745 } else if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
747 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
750 /* device files are never extent mapped */
753 for (i=0; i < EXT2_N_BLOCKS; i++) {
754 blk = inode->i_block[i];
760 if (blk < ctx->fs->super->s_first_data_block ||
761 blk >= ext2fs_blocks_count(ctx->fs->super) ||
762 ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
764 return; /* Invalid block, can't be dir */
766 blk = inode->i_block[0];
770 * If the mode says this is a device file and the i_links_count field
771 * is sane and we have not ruled it out as a device file previously,
772 * we declare it a device file, not a directory.
774 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
775 (inode->i_links_count == 1) && !not_device)
778 /* read the first block */
779 ehandler_operation(_("reading directory block"));
780 retval = ext2fs_read_dir_block4(ctx->fs, blk, buf, 0, pctx->ino);
781 ehandler_operation(0);
785 dirent = (struct ext2_dir_entry *) buf;
786 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
789 if ((ext2fs_dirent_name_len(dirent) != 1) ||
790 (dirent->name[0] != '.') ||
791 (dirent->inode != pctx->ino) ||
794 (rec_len >= ctx->fs->blocksize - 12))
797 dirent = (struct ext2_dir_entry *) (buf + rec_len);
798 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
801 if ((ext2fs_dirent_name_len(dirent) != 2) ||
802 (dirent->name[0] != '.') ||
803 (dirent->name[1] != '.') ||
809 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
810 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
811 e2fsck_write_inode_full(ctx, pctx->ino, inode,
812 EXT2_INODE_SIZE(ctx->fs->super),
813 "check_is_really_dir");
817 extern errcode_t e2fsck_setup_icount(e2fsck_t ctx, const char *icount_name,
818 int flags, ext2_icount_t hint,
821 unsigned int threshold;
822 unsigned int save_type;
830 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
832 profile_get_uint(ctx->profile, "scratch_files",
833 "numdirs_threshold", 0, 0, &threshold);
834 profile_get_boolean(ctx->profile, "scratch_files",
835 "icount", 0, 1, &enable);
837 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
839 num_dirs = 1024; /* Guess */
841 if (enable && tdb_dir && !access(tdb_dir, W_OK) &&
842 (!threshold || num_dirs > threshold)) {
843 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir,
848 e2fsck_set_bitmap_type(ctx->fs, EXT2FS_BMAP64_RBTREE, icount_name,
850 retval = ext2fs_create_icount2(ctx->fs, flags, 0, hint, ret);
851 ctx->fs->default_bitmap_type = save_type;
855 static errcode_t recheck_bad_inode_checksum(ext2_filsys fs, ext2_ino_t ino,
857 struct problem_context *pctx)
860 struct ext2_inode_large inode;
863 * Reread inode. If we don't see checksum error, then this inode
864 * has been fixed elsewhere.
866 ctx->stashed_ino = 0;
867 retval = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
869 if (retval && retval != EXT2_ET_INODE_CSUM_INVALID)
875 * Checksum still doesn't match. That implies that the inode passes
876 * all the sanity checks, so maybe the checksum is simply corrupt.
877 * See if the user will go for fixing that.
879 if (!fix_problem(ctx, PR_1_INODE_ONLY_CSUM_INVALID, pctx))
882 retval = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
887 static void reserve_block_for_root_repair(e2fsck_t ctx)
891 ext2_filsys fs = ctx->fs;
893 ctx->root_repair_block = 0;
894 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
897 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
900 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
901 ctx->root_repair_block = blk;
904 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
908 ext2_filsys fs = ctx->fs;
909 static const char name[] = "lost+found";
912 ctx->lnf_repair_block = 0;
913 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
916 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
919 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
920 ctx->lnf_repair_block = blk;
923 static errcode_t get_inline_data_ea_size(ext2_filsys fs, ext2_ino_t ino,
927 struct ext2_xattr_handle *handle;
930 retval = ext2fs_xattrs_open(fs, ino, &handle);
934 retval = ext2fs_xattrs_read(handle);
938 retval = ext2fs_xattr_get(handle, "system.data", &p, sz);
943 (void) ext2fs_xattrs_close(&handle);
947 static void finish_processing_inode(e2fsck_t ctx, ext2_ino_t ino,
948 struct problem_context *pctx,
955 * If the inode failed the checksum and the user didn't
956 * clear the inode, test the checksum again -- if it still
957 * fails, ask the user if the checksum should be corrected.
959 pctx->errcode = recheck_bad_inode_checksum(ctx->fs, ino, ctx, pctx);
961 ctx->flags |= E2F_FLAG_ABORT;
963 #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
965 finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
966 if ((ctx)->flags & E2F_FLAG_ABORT) \
970 static int could_be_block_map(ext2_filsys fs, struct ext2_inode *inode)
975 for (i = 0; i < EXT2_N_BLOCKS; i++) {
976 x = inode->i_block[i];
977 #ifdef WORDS_BIGENDIAN
978 x = ext2fs_swab32(x);
980 if (x >= ext2fs_blocks_count(fs->super))
988 * Figure out what to do with an inode that has both extents and inline data
989 * inode flags set. Returns -1 if we decide to erase the inode, 0 otherwise.
991 static int fix_inline_data_extents_file(e2fsck_t ctx,
993 struct ext2_inode *inode,
995 struct problem_context *pctx)
997 size_t max_inline_ea_size;
998 ext2_filsys fs = ctx->fs;
1001 /* Both feature flags not set? Just run the regular checks */
1002 if (!ext2fs_has_feature_extents(fs->super) &&
1003 !ext2fs_has_feature_inline_data(fs->super))
1006 /* Clear both flags if it's a special file */
1007 if (LINUX_S_ISCHR(inode->i_mode) ||
1008 LINUX_S_ISBLK(inode->i_mode) ||
1009 LINUX_S_ISFIFO(inode->i_mode) ||
1010 LINUX_S_ISSOCK(inode->i_mode)) {
1011 check_extents_inlinedata(ctx, pctx);
1015 /* If it looks like an extent tree, try to clear inlinedata */
1016 if (ext2fs_extent_header_verify(inode->i_block,
1017 sizeof(inode->i_block)) == 0 &&
1018 fix_problem(ctx, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT, pctx)) {
1019 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1024 /* If it looks short enough to be inline data, try to clear extents */
1025 if (inode_size > EXT2_GOOD_OLD_INODE_SIZE)
1026 max_inline_ea_size = inode_size -
1027 (EXT2_GOOD_OLD_INODE_SIZE +
1028 ((struct ext2_inode_large *)inode)->i_extra_isize);
1030 max_inline_ea_size = 0;
1031 if (EXT2_I_SIZE(inode) <
1032 EXT4_MIN_INLINE_DATA_SIZE + max_inline_ea_size &&
1033 fix_problem(ctx, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA, pctx)) {
1034 inode->i_flags &= ~EXT4_EXTENTS_FL;
1040 * Too big for inline data, but no evidence of extent tree -
1041 * maybe it's a block map file? If the mappings all look valid?
1043 if (could_be_block_map(fs, inode) &&
1044 fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS, pctx)) {
1045 #ifdef WORDS_BIGENDIAN
1048 for (i = 0; i < EXT2_N_BLOCKS; i++)
1049 inode->i_block[i] = ext2fs_swab32(inode->i_block[i]);
1052 inode->i_flags &= ~(EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL);
1057 /* Oh well, just clear the busted inode. */
1058 if (fix_problem(ctx, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE, pctx)) {
1059 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1065 e2fsck_write_inode(ctx, ino, inode, "pass1");
1070 static void pass1_readahead(e2fsck_t ctx, dgrp_t *group, ext2_ino_t *next_ino)
1072 ext2_ino_t inodes_in_group = 0, inodes_per_block, inodes_per_buffer;
1073 dgrp_t start = *group, grp;
1074 blk64_t blocks_to_read = 0;
1075 errcode_t err = EXT2_ET_INVALID_ARGUMENT;
1077 if (ctx->readahead_kb == 0)
1080 /* Keep iterating groups until we have enough to readahead */
1081 inodes_per_block = EXT2_INODES_PER_BLOCK(ctx->fs->super);
1082 for (grp = start; grp < ctx->fs->group_desc_count; grp++) {
1083 if (ext2fs_bg_flags_test(ctx->fs, grp, EXT2_BG_INODE_UNINIT))
1085 inodes_in_group = ctx->fs->super->s_inodes_per_group -
1086 ext2fs_bg_itable_unused(ctx->fs, grp);
1087 blocks_to_read += (inodes_in_group + inodes_per_block - 1) /
1089 if (blocks_to_read * ctx->fs->blocksize >
1090 ctx->readahead_kb * 1024)
1094 err = e2fsck_readahead(ctx->fs, E2FSCK_READA_ITABLE, start,
1096 if (err == EAGAIN) {
1097 ctx->readahead_kb /= 2;
1103 /* Error; disable itable readahead */
1104 *group = ctx->fs->group_desc_count;
1105 *next_ino = ctx->fs->super->s_inodes_count;
1108 * Don't do more readahead until we've reached the first inode
1109 * of the last inode scan buffer block for the last group.
1112 inodes_per_buffer = (ctx->inode_buffer_blocks ?
1113 ctx->inode_buffer_blocks :
1114 EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS) *
1115 ctx->fs->blocksize /
1116 EXT2_INODE_SIZE(ctx->fs->super);
1118 *next_ino = inodes_in_group -
1119 (inodes_in_group % inodes_per_buffer) + 1 +
1120 (grp * ctx->fs->super->s_inodes_per_group);
1125 * Check if the passed ino is one of the used superblock quota inodes.
1127 * Before the quota inodes were journaled, older superblock quota inodes
1128 * were just regular files in the filesystem and not reserved inodes. This
1129 * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1130 * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1132 static int quota_inum_is_super(struct ext2_super_block *sb, ext2_ino_t ino)
1134 enum quota_type qtype;
1136 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1137 if (*quota_sb_inump(sb, qtype) == ino)
1144 * Check if the passed ino is one of the reserved quota inodes.
1145 * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1146 * inodes. These inodes may or may not be in use by the quota feature.
1148 static int quota_inum_is_reserved(ext2_filsys fs, ext2_ino_t ino)
1150 enum quota_type qtype;
1152 for (qtype = 0; qtype < MAXQUOTAS; qtype++)
1153 if (quota_type2inum(qtype, fs->super) == ino)
1159 void e2fsck_pass1(e2fsck_t ctx)
1163 ext2_filsys fs = ctx->fs;
1165 struct ext2_inode *inode = NULL;
1166 ext2_inode_scan scan = NULL;
1167 char *block_buf = NULL;
1168 #ifdef RESOURCE_TRACK
1169 struct resource_track rtrack;
1171 unsigned char frag, fsize;
1172 struct problem_context pctx;
1173 struct scan_callback_struct scan_struct;
1174 struct ext2_super_block *sb = ctx->fs->super;
1176 int imagic_fs, extent_fs, inlinedata_fs;
1177 int low_dtime_check = 1;
1178 int inode_size = EXT2_INODE_SIZE(fs->super);
1180 int failed_csum = 0;
1181 ext2_ino_t ino_threshold = 0;
1182 dgrp_t ra_group = 0;
1183 blk64_t ea_ibody_quota_blocks;
1185 init_resource_track(&rtrack, ctx->fs->io);
1186 clear_problem_context(&pctx);
1188 /* If we can do readahead, figure out how many groups to pull in. */
1189 if (!e2fsck_can_readahead(ctx->fs))
1190 ctx->readahead_kb = 0;
1191 else if (ctx->readahead_kb == ~0ULL)
1192 ctx->readahead_kb = e2fsck_guess_readahead(ctx->fs);
1193 pass1_readahead(ctx, &ra_group, &ino_threshold);
1195 if (!(ctx->options & E2F_OPT_PREEN))
1196 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
1198 if (ext2fs_has_feature_dir_index(fs->super) &&
1199 !(ctx->options & E2F_OPT_NO)) {
1200 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
1201 ctx->dirs_to_hash = 0;
1205 mtrace_print("Pass 1");
1208 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1210 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
1211 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
1212 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
1213 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
1214 max_sizes = (max_sizes * (1UL << i));
1215 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
1219 imagic_fs = ext2fs_has_feature_imagic_inodes(sb);
1220 extent_fs = ext2fs_has_feature_extents(sb);
1221 inlinedata_fs = ext2fs_has_feature_inline_data(sb);
1224 * Allocate bitmaps structures
1226 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
1227 EXT2FS_BMAP64_RBTREE,
1229 &ctx->inode_used_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 _("directory inode map"),
1238 EXT2FS_BMAP64_AUTODIR,
1239 "inode_dir_map", &ctx->inode_dir_map);
1242 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1243 ctx->flags |= E2F_FLAG_ABORT;
1246 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
1247 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
1248 "inode_reg_map", &ctx->inode_reg_map);
1251 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1252 ctx->flags |= E2F_FLAG_ABORT;
1255 pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
1256 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
1257 "block_found_map", &ctx->block_found_map);
1260 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1261 ctx->flags |= E2F_FLAG_ABORT;
1264 pctx.errcode = e2fsck_allocate_block_bitmap(fs,
1265 _("metadata block map"), EXT2FS_BMAP64_RBTREE,
1266 "block_metadata_map", &ctx->block_metadata_map);
1269 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1270 ctx->flags |= E2F_FLAG_ABORT;
1273 pctx.errcode = e2fsck_setup_icount(ctx, "inode_link_info", 0, NULL,
1274 &ctx->inode_link_info);
1276 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1277 ctx->flags |= E2F_FLAG_ABORT;
1280 bufsize = inode_size;
1281 if (bufsize < sizeof(struct ext2_inode_large))
1282 bufsize = sizeof(struct ext2_inode_large);
1283 inode = (struct ext2_inode *)
1284 e2fsck_allocate_memory(ctx, bufsize, "scratch inode");
1286 inodes_to_process = (struct process_inode_block *)
1287 e2fsck_allocate_memory(ctx,
1288 (ctx->process_inode_size *
1289 sizeof(struct process_inode_block)),
1290 "array of inodes to process");
1291 process_inode_count = 0;
1293 pctx.errcode = ext2fs_init_dblist(fs, 0);
1295 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1296 ctx->flags |= E2F_FLAG_ABORT;
1301 * If the last orphan field is set, clear it, since the pass1
1302 * processing will automatically find and clear the orphans.
1303 * In the future, we may want to try using the last_orphan
1304 * linked list ourselves, but for now, we clear it so that the
1305 * ext3 mount code won't get confused.
1307 if (!(ctx->options & E2F_OPT_READONLY)) {
1308 if (fs->super->s_last_orphan) {
1309 fs->super->s_last_orphan = 0;
1310 ext2fs_mark_super_dirty(fs);
1314 mark_table_blocks(ctx);
1315 pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
1316 &ctx->block_found_map);
1318 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1319 ctx->flags |= E2F_FLAG_ABORT;
1322 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1323 "block interate buffer");
1324 if (EXT2_INODE_SIZE(fs->super) == EXT2_GOOD_OLD_INODE_SIZE)
1325 e2fsck_use_inode_shortcuts(ctx, 1);
1326 e2fsck_intercept_block_allocations(ctx);
1327 old_op = ehandler_operation(_("opening inode scan"));
1328 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1330 ehandler_operation(old_op);
1332 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1333 ctx->flags |= E2F_FLAG_ABORT;
1336 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE |
1337 EXT2_SF_WARN_GARBAGE_INODES, 0);
1338 ctx->stashed_inode = inode;
1339 scan_struct.ctx = ctx;
1340 scan_struct.block_buf = block_buf;
1341 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1342 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1343 ctx->fs->group_desc_count)))
1345 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
1346 (fs->super->s_mtime < fs->super->s_inodes_count) ||
1347 (fs->super->s_mkfs_time &&
1348 fs->super->s_mkfs_time < fs->super->s_inodes_count))
1349 low_dtime_check = 0;
1351 if (ext2fs_has_feature_mmp(fs->super) &&
1352 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1353 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1354 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1355 fs->super->s_mmp_block);
1357 /* Set up ctx->lost_and_found if possible */
1358 (void) e2fsck_get_lost_and_found(ctx, 0);
1361 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
1362 if (e2fsck_mmp_update(fs))
1363 fatal_error(ctx, 0);
1365 old_op = ehandler_operation(_("getting next inode from scan"));
1366 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1368 if (ino > ino_threshold)
1369 pass1_readahead(ctx, &ra_group, &ino_threshold);
1370 ehandler_operation(old_op);
1371 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1373 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1375 * If badblocks says badblocks is bad, offer to clear
1376 * the list, update the in-core bb list, and restart
1379 if (ino == EXT2_BAD_INO &&
1380 fix_problem(ctx, PR_1_BADBLOCKS_IN_BADBLOCKS,
1384 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1385 ext2fs_badblocks_list_free(ctx->fs->badblocks);
1386 ctx->fs->badblocks = NULL;
1387 err = ext2fs_read_bb_inode(ctx->fs,
1388 &ctx->fs->badblocks);
1390 fix_problem(ctx, PR_1_ISCAN_ERROR,
1392 ctx->flags |= E2F_FLAG_ABORT;
1395 err = ext2fs_inode_scan_goto_blockgroup(scan,
1398 fix_problem(ctx, PR_1_ISCAN_ERROR,
1400 ctx->flags |= E2F_FLAG_ABORT;
1405 if (!ctx->inode_bb_map)
1407 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1408 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1412 pctx.errcode != EXT2_ET_INODE_CSUM_INVALID &&
1413 pctx.errcode != EXT2_ET_INODE_IS_GARBAGE) {
1414 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1415 ctx->flags |= E2F_FLAG_ABORT;
1422 ctx->stashed_ino = ino;
1424 /* Clear trashed inode? */
1425 if (pctx.errcode == EXT2_ET_INODE_IS_GARBAGE &&
1426 inode->i_links_count > 0 &&
1427 fix_problem(ctx, PR_1_INODE_IS_GARBAGE, &pctx)) {
1429 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1431 failed_csum = pctx.errcode != 0;
1434 * Check for inodes who might have been part of the
1435 * orphaned list linked list. They should have gotten
1436 * dealt with by now, unless the list had somehow been
1439 * FIXME: In the future, inodes which are still in use
1440 * (and which are therefore) pending truncation should
1441 * be handled specially. Right now we just clear the
1442 * dtime field, and the normal e2fsck handling of
1443 * inodes where i_size and the inode blocks are
1444 * inconsistent is to fix i_size, instead of releasing
1445 * the extra blocks. This won't catch the inodes that
1446 * was at the end of the orphan list, but it's better
1447 * than nothing. The right answer is that there
1448 * shouldn't be any bugs in the orphan list handling. :-)
1450 if (inode->i_dtime && low_dtime_check &&
1451 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1452 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1453 inode->i_dtime = inode->i_links_count ?
1455 e2fsck_write_inode(ctx, ino, inode,
1461 if (inode->i_links_count) {
1462 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1463 ino, inode->i_links_count);
1465 pctx.num = inode->i_links_count;
1466 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1467 ctx->flags |= E2F_FLAG_ABORT;
1470 } else if ((ino >= EXT2_FIRST_INODE(fs->super)) &&
1471 !quota_inum_is_reserved(fs, ino)) {
1472 if (!inode->i_dtime && inode->i_mode) {
1473 if (fix_problem(ctx,
1474 PR_1_ZERO_DTIME, &pctx)) {
1475 inode->i_dtime = ctx->now;
1476 e2fsck_write_inode(ctx, ino, inode,
1481 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1485 /* Conflicting inlinedata/extents inode flags? */
1486 if ((inode->i_flags & EXT4_INLINE_DATA_FL) &&
1487 (inode->i_flags & EXT4_EXTENTS_FL)) {
1488 int res = fix_inline_data_extents_file(ctx, ino, inode,
1492 /* skip FINISH_INODE_LOOP */
1497 /* Test for incorrect inline_data flags settings. */
1498 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && !inlinedata_fs &&
1499 (ino >= EXT2_FIRST_INODE(fs->super))) {
1502 pctx.errcode = ext2fs_inline_data_size(fs, ino, &size);
1503 if (!pctx.errcode && size &&
1504 fix_problem(ctx, PR_1_INLINE_DATA_FEATURE, &pctx)) {
1505 ext2fs_set_feature_inline_data(sb);
1506 ext2fs_mark_super_dirty(fs);
1508 } else if (fix_problem(ctx, PR_1_INLINE_DATA_SET, &pctx)) {
1509 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1510 /* skip FINISH_INODE_LOOP */
1515 /* Test for inline data flag but no attr */
1516 if ((inode->i_flags & EXT4_INLINE_DATA_FL) && inlinedata_fs &&
1517 (ino >= EXT2_FIRST_INODE(fs->super))) {
1524 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
1525 err = get_inline_data_ea_size(fs, ino, &size);
1526 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
1527 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
1531 /* Everything is awesome... */
1533 case EXT2_ET_BAD_EA_BLOCK_NUM:
1534 case EXT2_ET_BAD_EA_HASH:
1535 case EXT2_ET_BAD_EA_HEADER:
1536 case EXT2_ET_EA_BAD_NAME_LEN:
1537 case EXT2_ET_EA_BAD_VALUE_SIZE:
1538 case EXT2_ET_EA_KEY_NOT_FOUND:
1539 case EXT2_ET_EA_NO_SPACE:
1540 case EXT2_ET_MISSING_EA_FEATURE:
1541 case EXT2_ET_INLINE_DATA_CANT_ITERATE:
1542 case EXT2_ET_INLINE_DATA_NO_BLOCK:
1543 case EXT2_ET_INLINE_DATA_NO_SPACE:
1544 case EXT2_ET_NO_INLINE_DATA:
1545 case EXT2_ET_EXT_ATTR_CSUM_INVALID:
1546 case EXT2_ET_EA_BAD_VALUE_OFFSET:
1547 /* broken EA or no system.data EA; truncate */
1548 if (fix_problem(ctx, PR_1_INLINE_DATA_NO_ATTR,
1550 err = ext2fs_inode_size_set(fs, inode, 0);
1553 ctx->flags |= E2F_FLAG_ABORT;
1556 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
1557 memset(&inode->i_block, 0,
1558 sizeof(inode->i_block));
1559 e2fsck_write_inode(ctx, ino, inode,
1565 /* Some other kind of non-xattr error? */
1567 ctx->flags |= E2F_FLAG_ABORT;
1573 * Test for incorrect extent flag settings.
1575 * On big-endian machines we must be careful:
1576 * When the inode is read, the i_block array is not swapped
1577 * if the extent flag is set. Therefore if we are testing
1578 * for or fixing a wrongly-set flag, we must potentially
1579 * (un)swap before testing, or after fixing.
1583 * In this case the extents flag was set when read, so
1584 * extent_header_verify is ok. If the inode is cleared,
1585 * no need to swap... so no extra swapping here.
1587 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
1588 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1589 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1590 if ((ext2fs_extent_header_verify(inode->i_block,
1591 sizeof(inode->i_block)) == 0) &&
1592 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1593 ext2fs_set_feature_extents(sb);
1594 ext2fs_mark_super_dirty(fs);
1596 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1598 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1599 if (ino == EXT2_BAD_INO)
1600 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1602 /* skip FINISH_INODE_LOOP */
1608 * For big-endian machines:
1609 * If the inode didn't have the extents flag set when it
1610 * was read, then the i_blocks array was swapped. To test
1611 * as an extents header, we must swap it back first.
1612 * IF we then set the extents flag, the entire i_block
1613 * array must be un/re-swapped to make it proper extents data.
1615 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1616 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1617 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1618 (LINUX_S_ISREG(inode->i_mode) ||
1619 LINUX_S_ISDIR(inode->i_mode))) {
1621 #ifdef WORDS_BIGENDIAN
1622 __u32 tmp_block[EXT2_N_BLOCKS];
1624 for (i = 0; i < EXT2_N_BLOCKS; i++)
1625 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1628 ehp = inode->i_block;
1630 if ((ext2fs_extent_header_verify(ehp,
1631 sizeof(inode->i_block)) == 0) &&
1632 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
1633 inode->i_flags |= EXT4_EXTENTS_FL;
1634 #ifdef WORDS_BIGENDIAN
1635 memcpy(inode->i_block, tmp_block,
1636 sizeof(inode->i_block));
1638 e2fsck_write_inode(ctx, ino, inode, "pass1");
1643 if (ino == EXT2_BAD_INO) {
1644 struct process_block_struct pb;
1646 if ((failed_csum || inode->i_mode || inode->i_uid ||
1647 inode->i_gid || inode->i_links_count ||
1648 (inode->i_flags & EXT4_INLINE_DATA_FL) ||
1649 inode->i_file_acl) &&
1650 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1651 memset(inode, 0, sizeof(struct ext2_inode));
1652 e2fsck_write_inode(ctx, ino, inode,
1657 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1658 &pb.fs_meta_blocks);
1661 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1662 ctx->flags |= E2F_FLAG_ABORT;
1665 pb.ino = EXT2_BAD_INO;
1666 pb.num_blocks = pb.last_block = 0;
1667 pb.last_db_block = -1;
1668 pb.num_illegal_blocks = 0;
1669 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1670 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1674 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1675 block_buf, process_bad_block, &pb);
1676 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1678 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1679 ctx->flags |= E2F_FLAG_ABORT;
1683 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1684 ctx->flags |= E2F_FLAG_ABORT;
1687 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1688 clear_problem_context(&pctx);
1689 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1691 } else if (ino == EXT2_ROOT_INO) {
1693 * Make sure the root inode is a directory; if
1694 * not, offer to clear it. It will be
1695 * regnerated in pass #3.
1697 if (!LINUX_S_ISDIR(inode->i_mode)) {
1698 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1702 * If dtime is set, offer to clear it. mke2fs
1703 * version 0.2b created filesystems with the
1704 * dtime field set for the root and lost+found
1705 * directories. We won't worry about
1706 * /lost+found, since that can be regenerated
1707 * easily. But we will fix the root directory
1708 * as a special case.
1710 if (inode->i_dtime && inode->i_links_count) {
1711 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1713 e2fsck_write_inode(ctx, ino, inode,
1718 } else if (ino == EXT2_JOURNAL_INO) {
1719 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1720 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1721 if (!LINUX_S_ISREG(inode->i_mode) &&
1722 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1724 inode->i_mode = LINUX_S_IFREG;
1725 e2fsck_write_inode(ctx, ino, inode,
1729 check_blocks(ctx, &pctx, block_buf, 0);
1730 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1733 if ((inode->i_links_count ||
1734 inode->i_blocks || inode->i_block[0]) &&
1735 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1737 memset(inode, 0, inode_size);
1738 ext2fs_icount_store(ctx->inode_link_info,
1740 e2fsck_write_inode_full(ctx, ino, inode,
1741 inode_size, "pass1");
1744 } else if (quota_inum_is_reserved(fs, ino)) {
1745 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1746 if (ext2fs_has_feature_quota(fs->super) &&
1747 quota_inum_is_super(fs->super, ino)) {
1748 if (!LINUX_S_ISREG(inode->i_mode) &&
1749 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1751 inode->i_mode = LINUX_S_IFREG;
1752 e2fsck_write_inode(ctx, ino, inode,
1756 check_blocks(ctx, &pctx, block_buf, 0);
1757 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1760 if ((inode->i_links_count ||
1761 inode->i_blocks || inode->i_block[0]) &&
1762 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1764 memset(inode, 0, inode_size);
1765 ext2fs_icount_store(ctx->inode_link_info,
1767 e2fsck_write_inode_full(ctx, ino, inode,
1768 inode_size, "pass1");
1771 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1772 problem_t problem = 0;
1774 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1775 if (ino == EXT2_BOOT_LOADER_INO) {
1776 if (LINUX_S_ISDIR(inode->i_mode))
1777 problem = PR_1_RESERVED_BAD_MODE;
1778 } else if (ino == EXT2_RESIZE_INO) {
1779 if (inode->i_mode &&
1780 !LINUX_S_ISREG(inode->i_mode))
1781 problem = PR_1_RESERVED_BAD_MODE;
1783 if (inode->i_mode != 0)
1784 problem = PR_1_RESERVED_BAD_MODE;
1787 if (fix_problem(ctx, problem, &pctx)) {
1789 e2fsck_write_inode(ctx, ino, inode,
1794 check_blocks(ctx, &pctx, block_buf, 0);
1795 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1799 if (!inode->i_links_count) {
1800 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1804 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1805 * deleted files. Oops.
1807 * Since all new ext2 implementations get this right,
1808 * we now assume that the case of non-zero
1809 * i_links_count and non-zero dtime means that we
1810 * should keep the file, not delete it.
1813 if (inode->i_dtime) {
1814 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1816 e2fsck_write_inode(ctx, ino, inode, "pass1");
1821 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1822 switch (fs->super->s_creator_os) {
1824 frag = inode->osd2.hurd2.h_i_frag;
1825 fsize = inode->osd2.hurd2.h_i_fsize;
1831 if (inode->i_faddr || frag || fsize ||
1832 (!ext2fs_has_feature_largedir(fs->super) &&
1833 (LINUX_S_ISDIR(inode->i_mode) && inode->i_size_high)))
1834 mark_inode_bad(ctx, ino);
1835 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
1836 !ext2fs_has_feature_64bit(fs->super) &&
1837 inode->osd2.linux2.l_i_file_acl_high != 0)
1838 mark_inode_bad(ctx, ino);
1839 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
1840 !ext2fs_has_feature_huge_file(fs->super) &&
1841 (inode->osd2.linux2.l_i_blocks_hi != 0))
1842 mark_inode_bad(ctx, ino);
1843 if (inode->i_flags & EXT2_IMAGIC_FL) {
1845 if (!ctx->inode_imagic_map)
1846 alloc_imagic_map(ctx);
1847 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1850 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1851 inode->i_flags &= ~EXT2_IMAGIC_FL;
1852 e2fsck_write_inode(ctx, ino,
1859 check_inode_extra_space(ctx, &pctx, &ea_ibody_quota_blocks);
1860 check_is_really_dir(ctx, &pctx, block_buf);
1863 * ext2fs_inode_has_valid_blocks2 does not actually look
1864 * at i_block[] values, so not endian-sensitive here.
1866 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1867 LINUX_S_ISLNK(inode->i_mode) &&
1868 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1869 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1870 inode->i_flags &= ~EXT4_EXTENTS_FL;
1871 e2fsck_write_inode(ctx, ino, inode, "pass1");
1875 if (LINUX_S_ISDIR(inode->i_mode)) {
1876 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1877 e2fsck_add_dir_info(ctx, ino, 0);
1878 ctx->fs_directory_count++;
1879 if (inode->i_flags & EXT4_ENCRYPT_FL)
1880 add_encrypted_dir(ctx, ino);
1881 } else if (LINUX_S_ISREG (inode->i_mode)) {
1882 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1883 ctx->fs_regular_count++;
1884 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1885 e2fsck_pass1_check_device_inode(fs, inode)) {
1886 check_extents_inlinedata(ctx, &pctx);
1887 check_immutable(ctx, &pctx);
1888 check_size(ctx, &pctx);
1889 ctx->fs_chardev_count++;
1890 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1891 e2fsck_pass1_check_device_inode(fs, inode)) {
1892 check_extents_inlinedata(ctx, &pctx);
1893 check_immutable(ctx, &pctx);
1894 check_size(ctx, &pctx);
1895 ctx->fs_blockdev_count++;
1896 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1897 e2fsck_pass1_check_symlink(fs, ino, inode,
1899 check_immutable(ctx, &pctx);
1900 ctx->fs_symlinks_count++;
1901 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
1902 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1904 } else if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1905 ctx->fs_fast_symlinks_count++;
1906 check_blocks(ctx, &pctx, block_buf,
1907 ea_ibody_quota_blocks);
1908 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1912 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1913 e2fsck_pass1_check_device_inode(fs, inode)) {
1914 check_extents_inlinedata(ctx, &pctx);
1915 check_immutable(ctx, &pctx);
1916 check_size(ctx, &pctx);
1917 ctx->fs_fifo_count++;
1918 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1919 e2fsck_pass1_check_device_inode(fs, inode)) {
1920 check_extents_inlinedata(ctx, &pctx);
1921 check_immutable(ctx, &pctx);
1922 check_size(ctx, &pctx);
1923 ctx->fs_sockets_count++;
1925 mark_inode_bad(ctx, ino);
1926 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1927 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
1928 if (inode->i_block[EXT2_IND_BLOCK])
1929 ctx->fs_ind_count++;
1930 if (inode->i_block[EXT2_DIND_BLOCK])
1931 ctx->fs_dind_count++;
1932 if (inode->i_block[EXT2_TIND_BLOCK])
1933 ctx->fs_tind_count++;
1935 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1936 !(inode->i_flags & EXT4_INLINE_DATA_FL) &&
1937 (inode->i_block[EXT2_IND_BLOCK] ||
1938 inode->i_block[EXT2_DIND_BLOCK] ||
1939 inode->i_block[EXT2_TIND_BLOCK] ||
1940 ext2fs_file_acl_block(fs, inode))) {
1941 struct process_inode_block *itp;
1943 itp = &inodes_to_process[process_inode_count];
1945 itp->ea_ibody_quota_blocks = ea_ibody_quota_blocks;
1946 if (inode_size < sizeof(struct ext2_inode_large))
1947 memcpy(&itp->inode, inode, inode_size);
1949 memcpy(&itp->inode, inode, sizeof(itp->inode));
1950 process_inode_count++;
1952 check_blocks(ctx, &pctx, block_buf,
1953 ea_ibody_quota_blocks);
1955 FINISH_INODE_LOOP(ctx, ino, &pctx, failed_csum);
1957 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1960 if (process_inode_count >= ctx->process_inode_size) {
1961 process_inodes(ctx, block_buf);
1963 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1967 process_inodes(ctx, block_buf);
1968 ext2fs_close_inode_scan(scan);
1971 reserve_block_for_root_repair(ctx);
1972 reserve_block_for_lnf_repair(ctx);
1975 * If any extended attribute blocks' reference counts need to
1976 * be adjusted, either up (ctx->refcount_extra), or down
1977 * (ctx->refcount), then fix them.
1979 if (ctx->refcount) {
1980 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1981 ea_refcount_free(ctx->refcount);
1984 if (ctx->refcount_extra) {
1985 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1987 ea_refcount_free(ctx->refcount_extra);
1988 ctx->refcount_extra = 0;
1991 if (ctx->invalid_bitmaps)
1992 handle_fs_bad_blocks(ctx);
1994 /* We don't need the block_ea_map any more */
1995 if (ctx->block_ea_map) {
1996 ext2fs_free_block_bitmap(ctx->block_ea_map);
1997 ctx->block_ea_map = 0;
2000 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
2001 clear_problem_context(&pctx);
2002 pctx.errcode = ext2fs_create_resize_inode(fs);
2004 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
2006 ctx->flags |= E2F_FLAG_ABORT;
2011 if (!pctx.errcode) {
2012 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
2014 inode->i_mtime = ctx->now;
2015 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
2018 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
2021 if (ctx->flags & E2F_FLAG_RESTART) {
2023 * Only the master copy of the superblock and block
2024 * group descriptors are going to be written during a
2025 * restart, so set the superblock to be used to be the
2026 * master superblock.
2028 ctx->use_superblock = 0;
2033 if (ctx->block_dup_map) {
2034 if (ctx->options & E2F_OPT_PREEN) {
2035 clear_problem_context(&pctx);
2036 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
2038 e2fsck_pass1_dupblocks(ctx, block_buf);
2040 ctx->flags |= E2F_FLAG_ALLOC_OK;
2041 ext2fs_free_mem(&inodes_to_process);
2043 e2fsck_use_inode_shortcuts(ctx, 0);
2046 ext2fs_close_inode_scan(scan);
2048 ext2fs_free_mem(&block_buf);
2050 ext2fs_free_mem(&inode);
2053 * The l+f inode may have been cleared, so zap it now and
2054 * later passes will recalculate it if necessary
2056 ctx->lost_and_found = 0;
2058 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
2059 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
2061 ctx->invalid_bitmaps++;
2063 #undef FINISH_INODE_LOOP
2066 * When the inode_scan routines call this callback at the end of the
2067 * glock group, call process_inodes.
2069 static errcode_t scan_callback(ext2_filsys fs,
2070 ext2_inode_scan scan EXT2FS_ATTR((unused)),
2071 dgrp_t group, void * priv_data)
2073 struct scan_callback_struct *scan_struct;
2076 scan_struct = (struct scan_callback_struct *) priv_data;
2077 ctx = scan_struct->ctx;
2079 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
2082 if ((ctx->progress)(ctx, 1, group+1,
2083 ctx->fs->group_desc_count))
2084 return EXT2_ET_CANCEL_REQUESTED;
2090 * Process the inodes in the "inodes to process" list.
2092 static void process_inodes(e2fsck_t ctx, char *block_buf)
2095 struct ext2_inode *old_stashed_inode;
2096 ext2_ino_t old_stashed_ino;
2097 const char *old_operation;
2099 struct problem_context pctx;
2102 printf("begin process_inodes: ");
2104 if (process_inode_count == 0)
2106 old_operation = ehandler_operation(0);
2107 old_stashed_inode = ctx->stashed_inode;
2108 old_stashed_ino = ctx->stashed_ino;
2109 qsort(inodes_to_process, process_inode_count,
2110 sizeof(struct process_inode_block), process_inode_cmp);
2111 clear_problem_context(&pctx);
2112 for (i=0; i < process_inode_count; i++) {
2113 pctx.inode = ctx->stashed_inode =
2114 (struct ext2_inode *) &inodes_to_process[i].inode;
2115 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
2118 printf("%u ", pctx.ino);
2120 sprintf(buf, _("reading indirect blocks of inode %u"),
2122 ehandler_operation(buf);
2123 check_blocks(ctx, &pctx, block_buf,
2124 inodes_to_process[i].ea_ibody_quota_blocks);
2125 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2128 ctx->stashed_inode = old_stashed_inode;
2129 ctx->stashed_ino = old_stashed_ino;
2130 process_inode_count = 0;
2132 printf("end process inodes\n");
2134 ehandler_operation(old_operation);
2137 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
2139 const struct process_inode_block *ib_a =
2140 (const struct process_inode_block *) a;
2141 const struct process_inode_block *ib_b =
2142 (const struct process_inode_block *) b;
2145 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
2146 ib_b->inode.i_block[EXT2_IND_BLOCK]);
2149 * We only call process_inodes() for non-extent
2150 * inodes, so it's OK to pass NULL to
2151 * ext2fs_file_acl_block() here.
2153 ret = ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a->inode)) -
2154 ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b->inode));
2156 ret = ib_a->ino - ib_b->ino;
2161 * Mark an inode as being bad in some what
2163 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
2165 struct problem_context pctx;
2167 if (!ctx->inode_bad_map) {
2168 clear_problem_context(&pctx);
2170 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2171 _("bad inode map"), EXT2FS_BMAP64_RBTREE,
2172 "inode_bad_map", &ctx->inode_bad_map);
2175 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2176 /* Should never get here */
2177 ctx->flags |= E2F_FLAG_ABORT;
2181 ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
2184 static void add_encrypted_dir(e2fsck_t ctx, ino_t ino)
2186 struct problem_context pctx;
2188 if (!ctx->encrypted_dirs) {
2189 pctx.errcode = ext2fs_u32_list_create(&ctx->encrypted_dirs, 0);
2193 pctx.errcode = ext2fs_u32_list_add(ctx->encrypted_dirs, ino);
2194 if (pctx.errcode == 0)
2197 fix_problem(ctx, PR_1_ALLOCATE_ENCRYPTED_DIRLIST, &pctx);
2198 /* Should never get here */
2199 ctx->flags |= E2F_FLAG_ABORT;
2203 * This procedure will allocate the inode "bb" (badblock) map table
2205 static void alloc_bb_map(e2fsck_t ctx)
2207 struct problem_context pctx;
2209 clear_problem_context(&pctx);
2210 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2211 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
2212 "inode_bb_map", &ctx->inode_bb_map);
2215 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2216 /* Should never get here */
2217 ctx->flags |= E2F_FLAG_ABORT;
2223 * This procedure will allocate the inode imagic table
2225 static void alloc_imagic_map(e2fsck_t ctx)
2227 struct problem_context pctx;
2229 clear_problem_context(&pctx);
2230 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
2231 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
2232 "inode_imagic_map", &ctx->inode_imagic_map);
2235 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
2236 /* Should never get here */
2237 ctx->flags |= E2F_FLAG_ABORT;
2243 * Marks a block as in use, setting the dup_map if it's been set
2244 * already. Called by process_block and process_bad_block.
2246 * WARNING: Assumes checks have already been done to make sure block
2247 * is valid. This is true in both process_block and process_bad_block.
2249 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
2251 struct problem_context pctx;
2253 clear_problem_context(&pctx);
2255 if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
2256 if (!ctx->block_dup_map) {
2257 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
2258 _("multiply claimed block map"),
2259 EXT2FS_BMAP64_RBTREE, "block_dup_map",
2260 &ctx->block_dup_map);
2263 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
2265 /* Should never get here */
2266 ctx->flags |= E2F_FLAG_ABORT;
2270 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
2272 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
2277 * When cluster size is greater than one block, it is caller's responsibility
2278 * to make sure block parameter starts at a cluster boundary.
2280 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
2283 if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
2284 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
2287 for (i = 0; i < num; i += EXT2FS_CLUSTER_RATIO(ctx->fs))
2288 mark_block_used(ctx, block + i);
2293 * Adjust the extended attribute block's reference counts at the end
2294 * of pass 1, either by subtracting out references for EA blocks that
2295 * are still referenced in ctx->refcount, or by adding references for
2296 * EA blocks that had extra references as accounted for in
2297 * ctx->refcount_extra.
2299 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
2300 char *block_buf, int adjust_sign)
2302 struct ext2_ext_attr_header *header;
2303 struct problem_context pctx;
2304 ext2_filsys fs = ctx->fs;
2309 clear_problem_context(&pctx);
2311 ea_refcount_intr_begin(refcount);
2313 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
2316 pctx.errcode = ext2fs_read_ext_attr3(fs, blk, block_buf,
2319 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
2322 header = (struct ext2_ext_attr_header *) block_buf;
2323 pctx.blkcount = header->h_refcount;
2324 should_be = header->h_refcount + adjust_sign * (int)count;
2325 pctx.num = should_be;
2326 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
2327 header->h_refcount = should_be;
2328 pctx.errcode = ext2fs_write_ext_attr3(fs, blk,
2332 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
2341 * Handle processing the extended attribute blocks
2343 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
2344 char *block_buf, blk64_t *ea_block_quota_blocks)
2346 ext2_filsys fs = ctx->fs;
2347 ext2_ino_t ino = pctx->ino;
2348 struct ext2_inode *inode = pctx->inode;
2351 struct ext2_ext_attr_header *header;
2352 struct ext2_ext_attr_entry *entry;
2353 blk64_t quota_blocks = EXT2FS_C2B(fs, 1);
2354 region_t region = 0;
2355 int failed_csum = 0;
2357 blk = ext2fs_file_acl_block(fs, inode);
2362 * If the Extended attribute flag isn't set, then a non-zero
2363 * file acl means that the inode is corrupted.
2365 * Or if the extended attribute block is an invalid block,
2366 * then the inode is also corrupted.
2368 if (!ext2fs_has_feature_xattr(fs->super) ||
2369 (blk < fs->super->s_first_data_block) ||
2370 (blk >= ext2fs_blocks_count(fs->super))) {
2371 mark_inode_bad(ctx, ino);
2375 /* If ea bitmap hasn't been allocated, create it */
2376 if (!ctx->block_ea_map) {
2377 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
2378 _("ext attr block map"),
2379 EXT2FS_BMAP64_RBTREE, "block_ea_map",
2380 &ctx->block_ea_map);
2381 if (pctx->errcode) {
2383 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
2384 ctx->flags |= E2F_FLAG_ABORT;
2389 /* Create the EA refcount structure if necessary */
2390 if (!ctx->refcount) {
2391 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
2392 if (pctx->errcode) {
2394 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2395 ctx->flags |= E2F_FLAG_ABORT;
2401 /* Debugging text */
2402 printf("Inode %u has EA block %u\n", ino, blk);
2405 /* Have we seen this EA block before? */
2406 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
2407 if (ctx->ea_block_quota)
2408 ea_refcount_fetch(ctx->ea_block_quota, blk,
2409 ea_block_quota_blocks);
2411 *ea_block_quota_blocks = 0;
2413 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
2415 /* Ooops, this EA was referenced more than it stated */
2416 if (!ctx->refcount_extra) {
2417 pctx->errcode = ea_refcount_create(0,
2418 &ctx->refcount_extra);
2419 if (pctx->errcode) {
2421 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2422 ctx->flags |= E2F_FLAG_ABORT;
2426 ea_refcount_increment(ctx->refcount_extra, blk, 0);
2431 * OK, we haven't seen this EA block yet. So we need to
2435 pctx->errcode = ext2fs_read_ext_attr3(fs, blk, block_buf, pctx->ino);
2436 if (pctx->errcode == EXT2_ET_EXT_ATTR_CSUM_INVALID) {
2439 } else if (pctx->errcode == EXT2_ET_BAD_EA_HEADER)
2442 if (pctx->errcode &&
2443 fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx)) {
2447 header = (struct ext2_ext_attr_header *) block_buf;
2448 pctx->blk = ext2fs_file_acl_block(fs, inode);
2449 if (((ctx->ext_attr_ver == 1) &&
2450 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
2451 ((ctx->ext_attr_ver == 2) &&
2452 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
2453 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
2457 if (header->h_blocks != 1) {
2458 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
2462 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
2465 region = region_create(0, fs->blocksize);
2467 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
2468 ctx->flags |= E2F_FLAG_ABORT;
2471 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
2472 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2476 entry = (struct ext2_ext_attr_entry *)(header+1);
2477 end = block_buf + fs->blocksize;
2478 while ((char *)entry < end && *(__u32 *)entry) {
2481 if (region_allocate(region, (char *)entry - (char *)header,
2482 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
2483 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2487 if ((ctx->ext_attr_ver == 1 &&
2488 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
2489 (ctx->ext_attr_ver == 2 &&
2490 entry->e_name_index == 0)) {
2491 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
2495 if (entry->e_value_inum == 0) {
2496 if (entry->e_value_offs + entry->e_value_size >
2498 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
2502 if (entry->e_value_size &&
2503 region_allocate(region, entry->e_value_offs,
2504 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
2505 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
2510 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
2511 entry->e_value_offs);
2513 if (entry->e_hash != hash) {
2514 pctx->num = entry->e_hash;
2515 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
2517 entry->e_hash = hash;
2521 blk64_t entry_quota_blocks;
2523 problem = check_large_ea_inode(ctx, entry, pctx,
2524 &entry_quota_blocks);
2526 mark_inode_ea_map(ctx, pctx,
2527 entry->e_value_inum);
2528 else if (fix_problem(ctx, problem, pctx))
2531 quota_blocks += entry_quota_blocks;
2534 entry = EXT2_EXT_ATTR_NEXT(entry);
2536 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
2537 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2540 region_free(region);
2543 * We only get here if there was no other errors that were fixed.
2544 * If there was a checksum fail, ask to correct it.
2547 fix_problem(ctx, PR_1_EA_BLOCK_ONLY_CSUM_INVALID, pctx)) {
2548 pctx->errcode = ext2fs_write_ext_attr3(fs, blk, block_buf,
2554 *ea_block_quota_blocks = quota_blocks;
2556 if (!ctx->ea_block_quota) {
2557 pctx->errcode = ea_refcount_create(0,
2558 &ctx->ea_block_quota);
2559 if (pctx->errcode) {
2561 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
2562 ctx->flags |= E2F_FLAG_ABORT;
2566 ea_refcount_store(ctx->ea_block_quota, blk, quota_blocks);
2568 ea_refcount_store(ctx->refcount, blk, header->h_refcount - 1);
2569 mark_block_used(ctx, blk);
2570 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
2575 region_free(region);
2576 ext2fs_file_acl_block_set(fs, inode, 0);
2577 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
2581 /* Returns 1 if bad htree, 0 if OK */
2582 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
2583 ext2_ino_t ino, struct ext2_inode *inode,
2586 struct ext2_dx_root_info *root;
2587 ext2_filsys fs = ctx->fs;
2591 if ((!LINUX_S_ISDIR(inode->i_mode) &&
2592 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
2593 (!ext2fs_has_feature_dir_index(fs->super) &&
2594 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
2597 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
2599 if ((pctx->errcode) ||
2601 (blk < fs->super->s_first_data_block) ||
2602 (blk >= ext2fs_blocks_count(fs->super))) {
2603 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2609 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
2610 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2613 /* XXX should check that beginning matches a directory */
2614 root = (struct ext2_dx_root_info *) (block_buf + 24);
2616 if ((root->reserved_zero || root->info_length < 8) &&
2617 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2620 pctx->num = root->hash_version;
2621 if ((root->hash_version != EXT2_HASH_LEGACY) &&
2622 (root->hash_version != EXT2_HASH_HALF_MD4) &&
2623 (root->hash_version != EXT2_HASH_TEA) &&
2624 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2627 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2628 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2631 pctx->num = root->indirect_levels;
2632 if ((root->indirect_levels > ext2_dir_htree_level(fs)) &&
2633 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2639 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2640 struct ext2_inode *inode, int restart_flag,
2644 inode->i_links_count = 0;
2645 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2646 inode->i_dtime = ctx->now;
2649 * If a special inode has such rotten block mappings that we
2650 * want to clear the whole inode, be sure to actually zap
2651 * the block maps because i_links_count isn't checked for
2652 * special inodes, and we'll end up right back here the next
2655 if (ino < EXT2_FIRST_INODE(ctx->fs->super))
2656 memset(inode->i_block, 0, sizeof(inode->i_block));
2658 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2659 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2660 if (ctx->inode_reg_map)
2661 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2662 if (ctx->inode_bad_map)
2663 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
2666 * If the inode was partially accounted for before processing
2667 * was aborted, we need to restart the pass 1 scan.
2669 ctx->flags |= restart_flag;
2671 if (ino == EXT2_BAD_INO)
2672 memset(inode, 0, sizeof(struct ext2_inode));
2674 e2fsck_write_inode(ctx, ino, inode, source);
2678 * Use the multiple-blocks reclamation code to fix alignment problems in
2679 * a bigalloc filesystem. We want a logical cluster to map to *only* one
2680 * physical cluster, and we want the block offsets within that cluster to
2683 static int has_unaligned_cluster_map(e2fsck_t ctx,
2684 blk64_t last_pblk, blk64_t last_lblk,
2685 blk64_t pblk, blk64_t lblk)
2687 blk64_t cluster_mask;
2689 if (!ctx->fs->cluster_ratio_bits)
2691 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
2694 * If the block in the logical cluster doesn't align with the block in
2695 * the physical cluster...
2697 if ((lblk & cluster_mask) != (pblk & cluster_mask))
2701 * If we cross a physical cluster boundary within a logical cluster...
2703 if (last_pblk && (lblk & cluster_mask) != 0 &&
2704 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
2705 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
2711 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2712 struct process_block_struct *pb,
2713 blk64_t start_block, blk64_t end_block,
2715 ext2_extent_handle_t ehandle,
2718 struct ext2fs_extent extent;
2719 blk64_t blk, last_lblk;
2721 int is_dir, is_leaf;
2723 struct ext2_extent_info info;
2724 int failed_csum = 0;
2726 if (pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID)
2729 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2732 if (!(ctx->options & E2F_OPT_FIXES_ONLY) &&
2733 !pb->eti.force_rebuild) {
2734 struct extent_tree_level *etl;
2736 etl = pb->eti.ext_info + info.curr_level;
2737 etl->num_extents += info.num_entries;
2738 etl->max_extents += info.max_entries;
2740 * Implementation wart: Splitting extent blocks when appending
2741 * will leave the old block with one free entry. Therefore
2742 * unless the node is totally full, pretend that a non-root
2743 * extent block can hold one fewer entry than it actually does,
2744 * so that we don't repeatedly rebuild the extent tree.
2746 if (info.curr_level && info.num_entries < info.max_entries)
2750 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2752 while ((pctx->errcode == 0 ||
2753 pctx->errcode == EXT2_ET_EXTENT_CSUM_INVALID) &&
2754 info.num_entries-- > 0) {
2755 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2756 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2757 last_lblk = extent.e_lblk + extent.e_len - 1;
2760 pctx->blk = extent.e_pblk;
2761 pctx->blk2 = extent.e_lblk;
2762 pctx->num = extent.e_len;
2763 pctx->blkcount = extent.e_lblk + extent.e_len;
2765 if (extent.e_pblk == 0 ||
2766 extent.e_pblk < ctx->fs->super->s_first_data_block ||
2767 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2768 problem = PR_1_EXTENT_BAD_START_BLK;
2769 else if (extent.e_lblk < start_block)
2770 problem = PR_1_OUT_OF_ORDER_EXTENTS;
2771 else if ((end_block && last_lblk > end_block) &&
2772 (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
2773 last_lblk > eof_block)))
2774 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
2775 else if (is_leaf && extent.e_len == 0)
2776 problem = PR_1_EXTENT_LENGTH_ZERO;
2778 (extent.e_pblk + extent.e_len) >
2779 ext2fs_blocks_count(ctx->fs->super))
2780 problem = PR_1_EXTENT_ENDS_BEYOND;
2781 else if (is_leaf && is_dir &&
2782 ((extent.e_lblk + extent.e_len) >
2783 (1U << (21 - ctx->fs->super->s_log_block_size))))
2784 problem = PR_1_TOOBIG_DIR;
2786 if (is_leaf && problem == 0 && extent.e_len > 0 &&
2787 region_allocate(pb->region, extent.e_lblk, extent.e_len))
2788 problem = PR_1_EXTENT_COLLISION;
2791 * Uninitialized blocks in a directory? Clear the flag and
2792 * we'll interpret the blocks later.
2794 if (try_repairs && is_dir && problem == 0 &&
2795 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2796 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
2797 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2798 pb->inode_modified = 1;
2799 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2806 if (try_repairs && problem) {
2808 if (fix_problem(ctx, problem, pctx)) {
2809 if (ctx->invalid_bitmaps) {
2811 * If fsck knows the bitmaps are bad,
2812 * skip to the next extent and
2813 * try to clear this extent again
2814 * after fixing the bitmaps, by
2817 pctx->errcode = ext2fs_extent_get(
2819 EXT2_EXTENT_NEXT_SIB,
2821 ctx->flags |= E2F_FLAG_RESTART_LATER;
2822 if (pctx->errcode ==
2823 EXT2_ET_NO_CURRENT_NODE) {
2829 e2fsck_read_bitmaps(ctx);
2830 pb->inode_modified = 1;
2832 ext2fs_extent_delete(ehandle, 0);
2833 if (pctx->errcode) {
2834 pctx->str = "ext2fs_extent_delete";
2837 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2838 if (pctx->errcode &&
2839 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
2840 pctx->str = "ext2fs_extent_fix_parents";
2843 pctx->errcode = ext2fs_extent_get(ehandle,
2844 EXT2_EXTENT_CURRENT,
2846 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2857 blk64_t lblk = extent.e_lblk;
2858 int next_try_repairs = 1;
2860 blk = extent.e_pblk;
2863 * If this lower extent block collides with critical
2864 * metadata, don't try to repair the damage. Pass 1b
2865 * will reallocate the block; then we can try again.
2867 if (pb->ino != EXT2_RESIZE_INO &&
2868 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
2870 next_try_repairs = 0;
2873 PR_1_CRITICAL_METADATA_COLLISION,
2875 ctx->flags |= E2F_FLAG_RESTART_LATER;
2877 pctx->errcode = ext2fs_extent_get(ehandle,
2878 EXT2_EXTENT_DOWN, &extent);
2879 if (pctx->errcode &&
2880 pctx->errcode != EXT2_ET_EXTENT_CSUM_INVALID) {
2881 pctx->str = "EXT2_EXTENT_DOWN";
2882 problem = PR_1_EXTENT_HEADER_INVALID;
2883 if (!next_try_repairs)
2885 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
2886 goto report_problem;
2889 /* The next extent should match this index's logical start */
2890 if (extent.e_lblk != lblk) {
2891 struct ext2_extent_info e_info;
2893 ext2fs_extent_get_info(ehandle, &e_info);
2895 pctx->blk2 = extent.e_lblk;
2896 pctx->num = e_info.curr_level - 1;
2897 problem = PR_1_EXTENT_INDEX_START_INVALID;
2898 if (fix_problem(ctx, problem, pctx)) {
2899 pb->inode_modified = 1;
2901 ext2fs_extent_fix_parents(ehandle);
2902 if (pctx->errcode) {
2903 pctx->str = "ext2fs_extent_fix_parents";
2908 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
2909 last_lblk, eof_block, ehandle,
2913 pctx->errcode = ext2fs_extent_get(ehandle,
2914 EXT2_EXTENT_UP, &extent);
2915 if (pctx->errcode) {
2916 pctx->str = "EXT2_EXTENT_UP";
2919 mark_block_used(ctx, blk);
2924 if ((pb->previous_block != 0) &&
2925 (pb->previous_block+1 != extent.e_pblk)) {
2926 if (ctx->options & E2F_OPT_FRAGCHECK) {
2931 else if (pb->is_reg)
2934 printf(("%6lu(%c): expecting %6lu "
2936 "phys %6lu log %lu len %lu\n"),
2937 (unsigned long) pctx->ino, type,
2938 (unsigned long) pb->previous_block+1,
2939 (unsigned long) extent.e_pblk,
2940 (unsigned long) extent.e_lblk,
2941 (unsigned long) extent.e_len);
2946 * If we notice a gap in the logical block mappings of an
2947 * extent-mapped directory, offer to close the hole by
2948 * moving the logical block down, otherwise we'll go mad in
2949 * pass 3 allocating empty directory blocks to fill the hole.
2951 if (try_repairs && is_dir &&
2952 pb->last_block + 1 < extent.e_lblk) {
2955 new_lblk = pb->last_block + 1;
2956 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
2957 new_lblk = ((new_lblk +
2958 EXT2FS_CLUSTER_RATIO(ctx->fs) - 1) &
2959 ~EXT2FS_CLUSTER_MASK(ctx->fs)) |
2961 EXT2FS_CLUSTER_MASK(ctx->fs));
2962 pctx->blk = extent.e_lblk;
2963 pctx->blk2 = new_lblk;
2964 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
2965 extent.e_lblk = new_lblk;
2966 pb->inode_modified = 1;
2967 pctx->errcode = ext2fs_extent_replace(ehandle,
2969 if (pctx->errcode) {
2973 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2975 goto failed_add_dir_block;
2976 pctx->errcode = ext2fs_extent_goto(ehandle,
2979 goto failed_add_dir_block;
2980 last_lblk = extent.e_lblk + extent.e_len - 1;
2986 while (++pb->last_db_block <
2987 (e2_blkcnt_t) extent.e_lblk) {
2988 pctx->errcode = ext2fs_add_dir_block2(
2992 if (pctx->errcode) {
2994 pctx->num = pb->last_db_block;
2995 goto failed_add_dir_block;
2999 for (i = 0; i < extent.e_len; i++) {
3000 pctx->errcode = ext2fs_add_dir_block2(
3005 if (pctx->errcode) {
3006 pctx->blk = extent.e_pblk + i;
3007 pctx->num = extent.e_lblk + i;
3008 failed_add_dir_block:
3009 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3010 /* Should never get here */
3011 ctx->flags |= E2F_FLAG_ABORT;
3015 if (extent.e_len > 0)
3016 pb->last_db_block = extent.e_lblk + extent.e_len - 1;
3018 if (has_unaligned_cluster_map(ctx, pb->previous_block,
3022 for (i = 0; i < extent.e_len; i++) {
3023 pctx->blk = extent.e_lblk + i;
3024 pctx->blk2 = extent.e_pblk + i;
3025 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3026 mark_block_used(ctx, extent.e_pblk + i);
3027 mark_block_used(ctx, extent.e_pblk + i);
3032 * Check whether first cluster got marked in previous iteration.
3034 if (ctx->fs->cluster_ratio_bits &&
3035 pb->previous_block &&
3036 (EXT2FS_B2C(ctx->fs, extent.e_pblk) ==
3037 EXT2FS_B2C(ctx->fs, pb->previous_block)))
3038 /* Set blk to the beginning of next cluster. */
3041 EXT2FS_B2C(ctx->fs, extent.e_pblk) + 1);
3043 /* Set blk to the beginning of current cluster. */
3044 blk = EXT2FS_C2B(ctx->fs,
3045 EXT2FS_B2C(ctx->fs, extent.e_pblk));
3047 if (blk < extent.e_pblk + extent.e_len) {
3048 mark_blocks_used(ctx, blk,
3049 extent.e_pblk + extent.e_len - blk);
3050 n = DIV_ROUND_UP(extent.e_pblk + extent.e_len - blk,
3051 EXT2FS_CLUSTER_RATIO(ctx->fs));
3052 pb->num_blocks += n;
3054 pb->last_block = extent.e_lblk + extent.e_len - 1;
3055 pb->previous_block = extent.e_pblk + extent.e_len - 1;
3056 start_block = pb->last_block = last_lblk;
3057 if (is_leaf && !is_dir &&
3058 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
3059 pb->last_init_lblock = last_lblk;
3061 pctx->errcode = ext2fs_extent_get(ehandle,
3062 EXT2_EXTENT_NEXT_SIB,
3066 /* Failed csum but passes checks? Ask to fix checksum. */
3068 fix_problem(ctx, PR_1_EXTENT_ONLY_CSUM_INVALID, pctx)) {
3069 pb->inode_modified = 1;
3070 pctx->errcode = ext2fs_extent_replace(ehandle, 0, &extent);
3075 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
3079 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
3080 struct process_block_struct *pb)
3082 struct ext2_extent_info info;
3083 struct ext2_inode *inode = pctx->inode;
3084 ext2_extent_handle_t ehandle;
3085 ext2_filsys fs = ctx->fs;
3086 ext2_ino_t ino = pctx->ino;
3089 struct ext3_extent_header *eh;
3091 /* Check for a proper extent header... */
3092 eh = (struct ext3_extent_header *) &inode->i_block[0];
3093 retval = ext2fs_extent_header_verify(eh, sizeof(inode->i_block));
3095 if (fix_problem(ctx, PR_1_MISSING_EXTENT_HEADER, pctx))
3096 e2fsck_clear_inode(ctx, ino, inode, 0,
3097 "check_blocks_extents");
3102 /* ...since this function doesn't fail if i_block is zeroed. */
3103 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
3104 if (pctx->errcode) {
3105 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
3106 e2fsck_clear_inode(ctx, ino, inode, 0,
3107 "check_blocks_extents");
3112 retval = ext2fs_extent_get_info(ehandle, &info);
3114 int max_depth = info.max_depth;
3116 if (max_depth >= MAX_EXTENT_DEPTH_COUNT)
3117 max_depth = MAX_EXTENT_DEPTH_COUNT-1;
3118 ctx->extent_depth_count[max_depth]++;
3121 /* Check maximum extent depth */
3122 pctx->blk = info.max_depth;
3123 pctx->blk2 = ext2fs_max_extent_depth(ehandle);
3124 if (pctx->blk2 < pctx->blk &&
3125 fix_problem(ctx, PR_1_EXTENT_BAD_MAX_DEPTH, pctx))
3126 pb->eti.force_rebuild = 1;
3128 /* Can we collect extent tree level stats? */
3129 pctx->blk = MAX_EXTENT_DEPTH_COUNT;
3130 if (pctx->blk2 > pctx->blk)
3131 fix_problem(ctx, PR_1E_MAX_EXTENT_TREE_DEPTH, pctx);
3132 memset(pb->eti.ext_info, 0, sizeof(pb->eti.ext_info));
3133 pb->eti.ino = pb->ino;
3135 pb->region = region_create(0, info.max_lblk);
3137 ext2fs_extent_free(ehandle);
3138 fix_problem(ctx, PR_1_EXTENT_ALLOC_REGION_ABORT, pctx);
3139 ctx->flags |= E2F_FLAG_ABORT;
3143 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
3144 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
3145 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle, 1);
3146 if (pctx->errcode &&
3147 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
3149 inode->i_blocks = 0;
3150 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
3151 "check_blocks_extents");
3154 region_free(pb->region);
3156 ext2fs_extent_free(ehandle);
3158 /* Rebuild unless it's a dir and we're rehashing it */
3159 if (LINUX_S_ISDIR(inode->i_mode) &&
3160 e2fsck_dir_will_be_rehashed(ctx, ino))
3163 if (ctx->options & E2F_OPT_CONVERT_BMAP)
3164 e2fsck_rebuild_extents_later(ctx, ino);
3166 e2fsck_should_rebuild_extents(ctx, pctx, &pb->eti, &info);
3170 * In fact we don't need to check blocks for an inode with inline data
3171 * because this inode doesn't have any blocks. In this function all
3172 * we need to do is add this inode into dblist when it is a directory.
3174 static void check_blocks_inline_data(e2fsck_t ctx, struct problem_context *pctx,
3175 struct process_block_struct *pb)
3178 size_t inline_data_size = 0;
3185 /* Process the dirents in i_block[] as the "first" block. */
3186 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 0);
3190 /* Process the dirents in the EA as a "second" block. */
3191 flags = ctx->fs->flags;
3192 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3193 pctx->errcode = ext2fs_inline_data_size(ctx->fs, pb->ino,
3195 ctx->fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3196 (ctx->fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3197 if (pctx->errcode) {
3202 if (inline_data_size <= EXT4_MIN_INLINE_DATA_SIZE)
3205 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pb->ino, 0, 1);
3213 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3214 ctx->flags |= E2F_FLAG_ABORT;
3218 * This subroutine is called on each inode to account for all of the
3219 * blocks used by that inode.
3221 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
3222 char *block_buf, blk64_t ea_ibody_quota_blocks)
3224 ext2_filsys fs = ctx->fs;
3225 struct process_block_struct pb;
3226 ext2_ino_t ino = pctx->ino;
3227 struct ext2_inode *inode = pctx->inode;
3228 unsigned bad_size = 0;
3229 int dirty_inode = 0;
3233 blk64_t ea_block_quota_blocks = 0;
3236 pb.num_blocks = EXT2FS_B2C(ctx->fs, ea_ibody_quota_blocks);
3238 pb.last_init_lblock = -1;
3239 pb.last_db_block = -1;
3240 pb.num_illegal_blocks = 0;
3241 pb.suppress = 0; pb.clear = 0;
3244 pb.previous_block = 0;
3245 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
3246 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
3247 pb.max_blocks = 1U << (31 - fs->super->s_log_block_size);
3251 pb.inode_modified = 0;
3252 pb.eti.force_rebuild = 0;
3256 extent_fs = ext2fs_has_feature_extents(ctx->fs->super);
3257 inlinedata_fs = ext2fs_has_feature_inline_data(ctx->fs->super);
3259 if (check_ext_attr(ctx, pctx, block_buf, &ea_block_quota_blocks)) {
3260 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3262 pb.num_blocks += EXT2FS_B2C(ctx->fs, ea_block_quota_blocks);
3265 if (inlinedata_fs && (inode->i_flags & EXT4_INLINE_DATA_FL))
3266 check_blocks_inline_data(ctx, pctx, &pb);
3267 else if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
3268 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
3269 check_blocks_extents(ctx, pctx, &pb);
3273 * If we've modified the inode, write it out before
3274 * iterate() tries to use it.
3277 e2fsck_write_inode(ctx, ino, inode,
3282 fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3283 pctx->errcode = ext2fs_block_iterate3(fs, ino,
3284 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
3285 block_buf, process_block, &pb);
3287 * We do not have uninitialized extents in non extent
3290 pb.last_init_lblock = pb.last_block;
3292 * If iterate() changed a block mapping, we have to
3293 * re-read the inode. If we decide to clear the
3294 * inode after clearing some stuff, we'll re-write the
3295 * bad mappings into the inode!
3297 if (pb.inode_modified)
3298 e2fsck_read_inode(ctx, ino, inode,
3300 fs->flags = (flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3301 (fs->flags & ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3303 if (ctx->options & E2F_OPT_CONVERT_BMAP) {
3305 printf("bmap rebuild ino=%d\n", ino);
3307 if (!LINUX_S_ISDIR(inode->i_mode) ||
3308 !e2fsck_dir_will_be_rehashed(ctx, ino))
3309 e2fsck_rebuild_extents_later(ctx, ino);
3313 end_problem_latch(ctx, PR_LATCH_BLOCK);
3314 end_problem_latch(ctx, PR_LATCH_TOOBIG);
3315 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3318 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
3320 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
3321 if (LINUX_S_ISDIR(inode->i_mode))
3322 ctx->fs_fragmented_dir++;
3324 ctx->fs_fragmented++;
3328 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
3333 if (inode->i_flags & EXT2_INDEX_FL) {
3334 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
3335 inode->i_flags &= ~EXT2_INDEX_FL;
3338 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
3342 if (!pb.num_blocks && pb.is_dir &&
3343 !(inode->i_flags & EXT4_INLINE_DATA_FL)) {
3344 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
3345 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
3346 ctx->fs_directory_count--;
3351 if (ino != quota_type2inum(PRJQUOTA, fs->super) &&
3352 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super))) {
3353 quota_data_add(ctx->qctx, (struct ext2_inode_large *) inode,
3355 pb.num_blocks * EXT2_CLUSTER_SIZE(fs->super));
3356 quota_data_inodes(ctx->qctx, (struct ext2_inode_large *) inode,
3360 if (!ext2fs_has_feature_huge_file(fs->super) ||
3361 !(inode->i_flags & EXT4_HUGE_FILE_FL))
3362 pb.num_blocks *= (fs->blocksize / 512);
3363 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
3365 printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
3366 ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
3370 unsigned nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
3371 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
3376 flags = ctx->fs->flags;
3377 ctx->fs->flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
3378 err = ext2fs_inline_data_size(ctx->fs, pctx->ino,
3380 ctx->fs->flags = (flags &
3381 EXT2_FLAG_IGNORE_CSUM_ERRORS) |
3383 ~EXT2_FLAG_IGNORE_CSUM_ERRORS);
3384 if (err || sz != inode->i_size) {
3388 } else if (inode->i_size & (fs->blocksize - 1))
3390 else if (nblock > (pb.last_block + 1))
3392 else if (nblock < (pb.last_block + 1)) {
3393 if (((pb.last_block + 1) - nblock) >
3394 fs->super->s_prealloc_dir_blocks)
3398 e2_blkcnt_t blkpg = ctx->blocks_per_page;
3400 size = EXT2_I_SIZE(inode);
3401 if ((pb.last_init_lblock >= 0) &&
3402 /* allow allocated blocks to end of PAGE_SIZE */
3403 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
3404 (pb.last_init_lblock / blkpg * blkpg != pb.last_init_lblock ||
3405 size < (__u64)(pb.last_init_lblock & ~(blkpg-1)) *
3408 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
3409 size > ext2_max_sizes[fs->super->s_log_block_size])
3410 /* too big for a direct/indirect-mapped file */
3412 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
3414 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
3415 /* too big for an extent-based file - 32bit ee_block */
3418 /* i_size for symlinks is checked elsewhere */
3419 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
3420 /* Did inline_data set pctx->num earlier? */
3422 pctx->num = (pb.last_block + 1) * fs->blocksize;
3423 pctx->group = bad_size;
3424 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
3425 if (LINUX_S_ISDIR(inode->i_mode))
3426 pctx->num &= 0xFFFFFFFFULL;
3427 ext2fs_inode_size_set(fs, inode, pctx->num);
3428 if (EXT2_I_SIZE(inode) == 0 &&
3429 (inode->i_flags & EXT4_INLINE_DATA_FL)) {
3430 memset(inode->i_block, 0,
3431 sizeof(inode->i_block));
3432 inode->i_flags &= ~EXT4_INLINE_DATA_FL;
3438 if (LINUX_S_ISREG(inode->i_mode) &&
3439 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
3441 if ((fs->super->s_creator_os != EXT2_OS_HURD) &&
3442 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
3443 (ext2fs_has_feature_huge_file(fs->super) &&
3444 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
3445 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
3446 pctx->num = pb.num_blocks;
3447 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
3448 inode->i_blocks = pb.num_blocks;
3449 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
3456 * The kernel gets mad if we ask it to allocate bigalloc clusters to
3457 * a block mapped file, so rebuild it as an extent file. We can skip
3458 * symlinks because they're never rewritten.
3460 if (ext2fs_has_feature_bigalloc(fs->super) &&
3461 (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode)) &&
3462 ext2fs_inode_data_blocks2(fs, inode) > 0 &&
3463 (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INO(fs->super)) &&
3464 !(inode->i_flags & (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)) &&
3465 fix_problem(ctx, PR_1_NO_BIGALLOC_BLOCKMAP_FILES, pctx)) {
3466 pctx->errcode = e2fsck_rebuild_extents_later(ctx, ino);
3471 if (ctx->dirs_to_hash && pb.is_dir &&
3472 !(ctx->lost_and_found && ctx->lost_and_found == ino) &&
3473 !(inode->i_flags & EXT2_INDEX_FL) &&
3474 ((inode->i_size / fs->blocksize) >= 3))
3475 e2fsck_rehash_dir_later(ctx, ino);
3479 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
3484 * Helper function called by process block when an illegal block is
3485 * found. It returns a description about why the block is illegal
3487 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
3491 static char problem[80];
3493 super = fs->super->s_first_data_block;
3494 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
3495 if (block < super) {
3496 sprintf(problem, "< FIRSTBLOCK (%u)", super);
3498 } else if (block >= ext2fs_blocks_count(fs->super)) {
3499 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
3502 for (i = 0; i < fs->group_desc_count; i++) {
3503 if (block == super) {
3504 sprintf(problem, "is the superblock in group %d", i);
3507 if (block > super &&
3508 block <= (super + fs->desc_blocks)) {
3509 sprintf(problem, "is in the group descriptors "
3513 if (block == ext2fs_block_bitmap_loc(fs, i)) {
3514 sprintf(problem, "is the block bitmap of group %d", i);
3517 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
3518 sprintf(problem, "is the inode bitmap of group %d", i);
3521 if (block >= ext2fs_inode_table_loc(fs, i) &&
3522 (block < ext2fs_inode_table_loc(fs, i)
3523 + fs->inode_blocks_per_group)) {
3524 sprintf(problem, "is in the inode table of group %d",
3528 super += fs->super->s_blocks_per_group;
3535 * This is a helper function for check_blocks().
3537 static int process_block(ext2_filsys fs,
3539 e2_blkcnt_t blockcnt,
3540 blk64_t ref_block EXT2FS_ATTR((unused)),
3541 int ref_offset EXT2FS_ATTR((unused)),
3544 struct process_block_struct *p;
3545 struct problem_context *pctx;
3546 blk64_t blk = *block_nr;
3548 problem_t problem = 0;
3551 p = (struct process_block_struct *) priv_data;
3556 * For a directory, add logical block zero for processing even if it's
3557 * not mapped or we'll be perennially stuck with broken "." and ".."
3560 if (p->is_dir && blockcnt == 0 && blk == 0) {
3561 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
3562 if (pctx->errcode) {
3564 pctx->num = blockcnt;
3565 goto failed_add_dir_block;
3574 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
3579 * Simplistic fragmentation check. We merely require that the
3580 * file be contiguous. (Which can never be true for really
3581 * big files that are greater than a block group.)
3583 if (p->previous_block && p->ino != EXT2_RESIZE_INO) {
3584 if (p->previous_block+1 != blk) {
3585 if (ctx->options & E2F_OPT_FRAGCHECK) {
3593 printf(_("%6lu(%c): expecting %6lu "
3594 "got phys %6lu (blkcnt %lld)\n"),
3595 (unsigned long) pctx->ino, type,
3596 (unsigned long) p->previous_block+1,
3597 (unsigned long) blk,
3604 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
3605 problem = PR_1_TOOBIG_DIR;
3606 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
3607 problem = PR_1_TOOBIG_REG;
3608 if (!p->is_dir && !p->is_reg && blockcnt > 0)
3609 problem = PR_1_TOOBIG_SYMLINK;
3611 if (blk < fs->super->s_first_data_block ||
3612 blk >= ext2fs_blocks_count(fs->super))
3613 problem = PR_1_ILLEGAL_BLOCK_NUM;
3616 * If this IND/DIND/TIND block is squatting atop some critical metadata
3617 * (group descriptors, superblock, bitmap, inode table), any write to
3618 * "fix" mapping problems will destroy the metadata. We'll let pass 1b
3619 * fix that and restart fsck.
3622 p->ino != EXT2_RESIZE_INO &&
3623 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk)) {
3625 fix_problem(ctx, PR_1_CRITICAL_METADATA_COLLISION, pctx);
3626 ctx->flags |= E2F_FLAG_RESTART_LATER;
3630 p->num_illegal_blocks++;
3632 * A bit of subterfuge here -- we're trying to fix a block
3633 * mapping, but the IND/DIND/TIND block could have collided
3634 * with some critical metadata. So, fix the in-core mapping so
3635 * iterate won't go insane, but return 0 instead of
3636 * BLOCK_CHANGED so that it won't write the remapping out to
3637 * our multiply linked block.
3639 * Even if we previously determined that an *IND block
3640 * conflicts with critical metadata, we must still try to
3641 * iterate the *IND block as if it is an *IND block to find and
3642 * mark the blocks it points to. Better to be overly cautious
3643 * with the used_blocks map so that we don't move the *IND
3644 * block to a block that's really in use!
3646 if (p->ino != EXT2_RESIZE_INO &&
3648 ext2fs_test_block_bitmap2(ctx->block_metadata_map,
3653 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
3654 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
3658 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
3660 set_latch_flags(PR_LATCH_BLOCK,
3665 pctx->blkcount = blockcnt;
3666 if (fix_problem(ctx, problem, pctx)) {
3667 blk = *block_nr = 0;
3668 ret_code = BLOCK_CHANGED;
3669 p->inode_modified = 1;
3671 * If the directory block is too big and is beyond the
3672 * end of the FS, don't bother trying to add it for
3673 * processing -- the kernel would never have created a
3674 * directory this large, and we risk an ENOMEM abort.
3675 * In any case, the toobig handler for extent-based
3676 * directories also doesn't feed toobig blocks to
3679 if (problem == PR_1_TOOBIG_DIR)
3686 if (p->ino == EXT2_RESIZE_INO) {
3688 * The resize inode has already be sanity checked
3689 * during pass #0 (the superblock checks). All we
3690 * have to do is mark the double indirect block as
3691 * being in use; all of the other blocks are handled
3692 * by mark_table_blocks()).
3694 if (blockcnt == BLOCK_COUNT_DIND)
3695 mark_block_used(ctx, blk);
3697 } else if (!(ctx->fs->cluster_ratio_bits &&
3698 p->previous_block &&
3699 (EXT2FS_B2C(ctx->fs, blk) ==
3700 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
3701 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
3702 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
3703 mark_block_used(ctx, blk);
3705 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
3706 p->last_block, blk, blockcnt)) {
3707 pctx->blk = blockcnt;
3709 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3710 mark_block_used(ctx, blk);
3711 mark_block_used(ctx, blk);
3714 p->last_block = blockcnt;
3715 p->previous_block = blk;
3717 if (p->is_dir && (blockcnt >= 0)) {
3718 while (++p->last_db_block < blockcnt) {
3719 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
3722 if (pctx->errcode) {
3724 pctx->num = p->last_db_block;
3725 goto failed_add_dir_block;
3728 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
3730 if (pctx->errcode) {
3732 pctx->num = blockcnt;
3733 failed_add_dir_block:
3734 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3735 /* Should never get here */
3736 ctx->flags |= E2F_FLAG_ABORT;
3743 static int process_bad_block(ext2_filsys fs,
3745 e2_blkcnt_t blockcnt,
3746 blk64_t ref_block EXT2FS_ATTR((unused)),
3747 int ref_offset EXT2FS_ATTR((unused)),
3750 struct process_block_struct *p;
3751 blk64_t blk = *block_nr;
3752 blk64_t first_block;
3754 struct problem_context *pctx;
3760 p = (struct process_block_struct *) priv_data;
3764 pctx->ino = EXT2_BAD_INO;
3766 pctx->blkcount = blockcnt;
3768 if ((blk < fs->super->s_first_data_block) ||
3769 (blk >= ext2fs_blocks_count(fs->super))) {
3770 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
3772 return BLOCK_CHANGED;
3778 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
3780 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
3782 return BLOCK_CHANGED;
3784 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3787 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
3790 return BLOCK_CHANGED;
3792 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3795 mark_block_used(ctx, blk);
3799 printf ("DEBUG: Marking %u as bad.\n", blk);
3801 ctx->fs_badblocks_count++;
3803 * If the block is not used, then mark it as used and return.
3804 * If it is already marked as found, this must mean that
3805 * there's an overlap between the filesystem table blocks
3806 * (bitmaps and inode table) and the bad block list.
3808 if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
3809 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3813 * Try to find the where the filesystem block was used...
3815 first_block = fs->super->s_first_data_block;
3817 for (i = 0; i < fs->group_desc_count; i++ ) {
3820 if (!ext2fs_bg_has_super(fs, i))
3822 if (blk == first_block) {
3824 if (fix_problem(ctx,
3825 PR_1_BAD_PRIMARY_SUPERBLOCK,
3828 return BLOCK_CHANGED;
3832 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
3835 if ((blk > first_block) &&
3836 (blk <= first_block + fs->desc_blocks)) {
3838 pctx->blk = *block_nr;
3839 if (fix_problem(ctx,
3840 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
3842 return BLOCK_CHANGED;
3846 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
3850 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
3851 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
3852 ctx->invalid_block_bitmap_flag[i]++;
3853 ctx->invalid_bitmaps++;
3857 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
3858 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
3859 ctx->invalid_inode_bitmap_flag[i]++;
3860 ctx->invalid_bitmaps++;
3864 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
3865 (blk < (ext2fs_inode_table_loc(fs, i) +
3866 fs->inode_blocks_per_group))) {
3868 * If there are bad blocks in the inode table,
3869 * the inode scan code will try to do
3870 * something reasonable automatically.
3874 first_block += fs->super->s_blocks_per_group;
3877 * If we've gotten to this point, then the only
3878 * possibility is that the bad block inode meta data
3879 * is using a bad block.
3881 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
3882 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
3883 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
3885 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
3887 return BLOCK_CHANGED;
3889 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3896 /* Warn user that the block wasn't claimed */
3897 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
3902 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
3903 const char *name, int num, blk64_t *new_block)
3905 ext2_filsys fs = ctx->fs;
3907 blk64_t old_block = *new_block;
3910 unsigned flexbg_size;
3913 struct problem_context pctx;
3915 clear_problem_context(&pctx);
3918 pctx.blk = old_block;
3922 * For flex_bg filesystems, first try to allocate the metadata
3923 * within the flex_bg, and if that fails then try finding the
3924 * space anywhere in the filesystem.
3926 is_flexbg = ext2fs_has_feature_flex_bg(fs->super);
3928 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3929 flexbg = group / flexbg_size;
3930 first_block = ext2fs_group_first_block2(fs,
3931 flexbg_size * flexbg);
3932 last_grp = group | (flexbg_size - 1);
3933 if (last_grp >= fs->group_desc_count)
3934 last_grp = fs->group_desc_count - 1;
3935 last_block = ext2fs_group_last_block2(fs, last_grp);
3937 last_block = ext2fs_group_last_block2(fs, group);
3938 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
3939 num, ctx->block_found_map,
3941 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
3942 pctx.errcode = ext2fs_get_free_blocks2(fs,
3943 fs->super->s_first_data_block,
3944 ext2fs_blocks_count(fs->super),
3945 num, ctx->block_found_map, new_block);
3948 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
3949 ext2fs_unmark_valid(fs);
3950 ctx->flags |= E2F_FLAG_ABORT;
3953 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
3955 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
3956 ext2fs_unmark_valid(fs);
3957 ctx->flags |= E2F_FLAG_ABORT;
3960 ext2fs_mark_super_dirty(fs);
3961 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3962 pctx.blk2 = *new_block;
3963 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
3964 PR_1_RELOC_TO), &pctx);
3966 for (i = 0; i < num; i++) {
3968 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
3970 pctx.errcode = io_channel_read_blk64(fs->io,
3971 old_block + i, 1, buf);
3973 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
3974 pctx.blk = (*new_block) + i;
3975 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
3978 pctx.blk = (*new_block) + i;
3979 pctx.errcode = ext2fs_zero_blocks2(fs, pctx.blk, 1,
3984 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
3986 ext2fs_free_mem(&buf);
3990 * This routine gets called at the end of pass 1 if bad blocks are
3991 * detected in the superblock, group descriptors, inode_bitmaps, or
3992 * block bitmaps. At this point, all of the blocks have been mapped
3993 * out, so we can try to allocate new block(s) to replace the bad
3996 static void handle_fs_bad_blocks(e2fsck_t ctx)
3998 ext2_filsys fs = ctx->fs;
4000 blk64_t first_block;
4003 for (i = 0; i < fs->group_desc_count; i++) {
4004 first_block = ext2fs_group_first_block2(fs, i);
4006 if (ctx->invalid_block_bitmap_flag[i]) {
4007 new_blk = ext2fs_block_bitmap_loc(fs, i);
4008 new_table_block(ctx, first_block, i, _("block bitmap"),
4010 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
4012 if (ctx->invalid_inode_bitmap_flag[i]) {
4013 new_blk = ext2fs_inode_bitmap_loc(fs, i);
4014 new_table_block(ctx, first_block, i, _("inode bitmap"),
4016 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
4018 if (ctx->invalid_inode_table_flag[i]) {
4019 new_blk = ext2fs_inode_table_loc(fs, i);
4020 new_table_block(ctx, first_block, i, _("inode table"),
4021 fs->inode_blocks_per_group,
4023 ext2fs_inode_table_loc_set(fs, i, new_blk);
4024 ctx->flags |= E2F_FLAG_RESTART;
4027 ctx->invalid_bitmaps = 0;
4031 * This routine marks all blocks which are used by the superblock,
4032 * group descriptors, inode bitmaps, and block bitmaps.
4034 static void mark_table_blocks(e2fsck_t ctx)
4036 ext2_filsys fs = ctx->fs;
4040 struct problem_context pctx;
4042 clear_problem_context(&pctx);
4044 for (i = 0; i < fs->group_desc_count; i++) {
4047 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
4048 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_metadata_map);
4051 * Mark the blocks used for the inode table
4053 if (ext2fs_inode_table_loc(fs, i)) {
4054 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
4055 j < fs->inode_blocks_per_group;
4057 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4060 if (!ctx->invalid_inode_table_flag[i] &&
4062 PR_1_ITABLE_CONFLICT, &pctx)) {
4063 ctx->invalid_inode_table_flag[i]++;
4064 ctx->invalid_bitmaps++;
4067 ext2fs_mark_block_bitmap2(
4068 ctx->block_found_map, b);
4069 ext2fs_mark_block_bitmap2(
4070 ctx->block_metadata_map, b);
4076 * Mark block used for the block bitmap
4078 if (ext2fs_block_bitmap_loc(fs, i)) {
4079 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4080 ext2fs_block_bitmap_loc(fs, i))) {
4081 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
4082 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
4083 ctx->invalid_block_bitmap_flag[i]++;
4084 ctx->invalid_bitmaps++;
4087 ext2fs_mark_block_bitmap2(ctx->block_found_map,
4088 ext2fs_block_bitmap_loc(fs, i));
4089 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
4090 ext2fs_block_bitmap_loc(fs, i));
4094 * Mark block used for the inode bitmap
4096 if (ext2fs_inode_bitmap_loc(fs, i)) {
4097 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
4098 ext2fs_inode_bitmap_loc(fs, i))) {
4099 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
4100 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
4101 ctx->invalid_inode_bitmap_flag[i]++;
4102 ctx->invalid_bitmaps++;
4105 ext2fs_mark_block_bitmap2(ctx->block_metadata_map,
4106 ext2fs_inode_bitmap_loc(fs, i));
4107 ext2fs_mark_block_bitmap2(ctx->block_found_map,
4108 ext2fs_inode_bitmap_loc(fs, i));
4115 * Thes subroutines short circuits ext2fs_get_blocks and
4116 * ext2fs_check_directory; we use them since we already have the inode
4117 * structure, so there's no point in letting the ext2fs library read
4120 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
4123 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4126 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4127 return EXT2_ET_CALLBACK_NOTHANDLED;
4129 for (i=0; i < EXT2_N_BLOCKS; i++)
4130 blocks[i] = ctx->stashed_inode->i_block[i];
4134 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
4135 struct ext2_inode *inode)
4137 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4139 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4140 return EXT2_ET_CALLBACK_NOTHANDLED;
4141 *inode = *ctx->stashed_inode;
4145 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
4146 struct ext2_inode *inode)
4148 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4150 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
4151 (inode != ctx->stashed_inode))
4152 *ctx->stashed_inode = *inode;
4153 return EXT2_ET_CALLBACK_NOTHANDLED;
4156 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
4158 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4160 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
4161 return EXT2_ET_CALLBACK_NOTHANDLED;
4163 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
4164 return EXT2_ET_NO_DIRECTORY;
4168 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
4171 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4175 if (ctx->block_found_map) {
4176 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
4180 if (fs->block_map) {
4181 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
4182 ext2fs_mark_bb_dirty(fs);
4185 if (!fs->block_map) {
4186 retval = ext2fs_read_block_bitmap(fs);
4191 retval = ext2fs_new_block2(fs, goal, fs->block_map, &new_block);
4200 static errcode_t e2fsck_new_range(ext2_filsys fs, int flags, blk64_t goal,
4201 blk64_t len, blk64_t *pblk, blk64_t *plen)
4203 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4206 if (ctx->block_found_map)
4207 return ext2fs_new_range(fs, flags, goal, len,
4208 ctx->block_found_map, pblk, plen);
4210 if (!fs->block_map) {
4211 retval = ext2fs_read_block_bitmap(fs);
4216 return ext2fs_new_range(fs, flags, goal, len, fs->block_map,
4220 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
4222 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4224 /* Never free a critical metadata block */
4225 if (ctx->block_found_map &&
4226 ctx->block_metadata_map &&
4228 ext2fs_test_block_bitmap2(ctx->block_metadata_map, blk))
4231 if (ctx->block_found_map) {
4233 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
4235 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
4239 static void e2fsck_block_alloc_stats_range(ext2_filsys fs, blk64_t blk,
4240 blk_t num, int inuse)
4242 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
4244 /* Never free a critical metadata block */
4245 if (ctx->block_found_map &&
4246 ctx->block_metadata_map &&
4248 ext2fs_test_block_bitmap_range2(ctx->block_metadata_map, blk, num))
4251 if (ctx->block_found_map) {
4253 ext2fs_mark_block_bitmap_range2(ctx->block_found_map,
4256 ext2fs_unmark_block_bitmap_range2(ctx->block_found_map,
4261 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
4263 ext2_filsys fs = ctx->fs;
4265 if (use_shortcuts) {
4266 fs->get_blocks = pass1_get_blocks;
4267 fs->check_directory = pass1_check_directory;
4268 fs->read_inode = pass1_read_inode;
4269 fs->write_inode = pass1_write_inode;
4270 ctx->stashed_ino = 0;
4273 fs->check_directory = 0;
4275 fs->write_inode = 0;
4279 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
4281 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
4282 ext2fs_set_block_alloc_stats_callback(ctx->fs,
4283 e2fsck_block_alloc_stats, 0);
4284 ext2fs_set_new_range_callback(ctx->fs, e2fsck_new_range, NULL);
4285 ext2fs_set_block_alloc_stats_range_callback(ctx->fs,
4286 e2fsck_block_alloc_stats_range, NULL);