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)
30 * Pass 1 is designed to stash away enough information so that the
31 * other passes should not need to read in the inode information
32 * during the normal course of a filesystem check. (Althogh if an
33 * inconsistency is detected, other passes may need to read in an
36 * Note that pass 1B will be invoked if there are any duplicate blocks
40 #define _GNU_SOURCE 1 /* get strnlen() */
49 #include <ext2fs/ext2_ext_attr.h>
53 #ifdef NO_INLINE_FUNCS
56 #define _INLINE_ inline
59 static int process_block(ext2_filsys fs
, blk64_t
*blocknr
,
60 e2_blkcnt_t blockcnt
, blk64_t ref_blk
,
61 int ref_offset
, void *priv_data
);
62 static int process_bad_block(ext2_filsys fs
, blk64_t
*block_nr
,
63 e2_blkcnt_t blockcnt
, blk64_t ref_blk
,
64 int ref_offset
, void *priv_data
);
65 static void check_blocks(e2fsck_t ctx
, struct problem_context
*pctx
,
67 static void mark_table_blocks(e2fsck_t ctx
);
68 static void alloc_bb_map(e2fsck_t ctx
);
69 static void alloc_imagic_map(e2fsck_t ctx
);
70 static void mark_inode_bad(e2fsck_t ctx
, ino_t ino
);
71 static void handle_fs_bad_blocks(e2fsck_t ctx
);
72 static void process_inodes(e2fsck_t ctx
, char *block_buf
);
73 static EXT2_QSORT_TYPE
process_inode_cmp(const void *a
, const void *b
);
74 static errcode_t
scan_callback(ext2_filsys fs
, ext2_inode_scan scan
,
75 dgrp_t group
, void * priv_data
);
76 static void adjust_extattr_refcount(e2fsck_t ctx
, ext2_refcount_t refcount
,
77 char *block_buf
, int adjust_sign
);
78 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
80 struct process_block_struct
{
82 unsigned is_dir
:1, is_reg
:1, clear
:1, suppress
:1,
83 fragmented
:1, compressed
:1, bbcheck
:1,
87 e2_blkcnt_t last_block
;
88 e2_blkcnt_t last_init_lblock
;
89 e2_blkcnt_t last_db_block
;
90 int num_illegal_blocks
;
91 blk64_t previous_block
;
92 struct ext2_inode
*inode
;
93 struct problem_context
*pctx
;
94 ext2fs_block_bitmap fs_meta_blocks
;
99 struct process_inode_block
{
101 struct ext2_inode inode
;
104 struct scan_callback_struct
{
110 * For the inodes to process list.
112 static struct process_inode_block
*inodes_to_process
;
113 static int process_inode_count
;
115 static __u64 ext2_max_sizes
[EXT2_MAX_BLOCK_LOG_SIZE
-
116 EXT2_MIN_BLOCK_LOG_SIZE
+ 1];
119 * Free all memory allocated by pass1 in preparation for restarting
122 static void unwind_pass1(ext2_filsys fs
EXT2FS_ATTR((unused
)))
124 ext2fs_free_mem(&inodes_to_process
);
125 inodes_to_process
= 0;
129 * Check to make sure a device inode is real. Returns 1 if the device
130 * checks out, 0 if not.
132 * Note: this routine is now also used to check FIFO's and Sockets,
133 * since they have the same requirement; the i_block fields should be
136 int e2fsck_pass1_check_device_inode(ext2_filsys fs
EXT2FS_ATTR((unused
)),
137 struct ext2_inode
*inode
)
142 * If the index flag is set, then this is a bogus
145 if (inode
->i_flags
& EXT2_INDEX_FL
)
149 * We should be able to do the test below all the time, but
150 * because the kernel doesn't forcibly clear the device
151 * inode's additional i_block fields, there are some rare
152 * occasions when a legitimate device inode will have non-zero
153 * additional i_block fields. So for now, we only complain
154 * when the immutable flag is set, which should never happen
155 * for devices. (And that's when the problem is caused, since
156 * you can't set or clear immutable flags for devices.) Once
157 * the kernel has been fixed we can change this...
159 if (inode
->i_flags
& (EXT2_IMMUTABLE_FL
| EXT2_APPEND_FL
)) {
160 for (i
=4; i
< EXT2_N_BLOCKS
; i
++)
161 if (inode
->i_block
[i
])
168 * Check to make sure a symlink inode is real. Returns 1 if the symlink
169 * checks out, 0 if not.
171 int e2fsck_pass1_check_symlink(ext2_filsys fs
, ext2_ino_t ino
,
172 struct ext2_inode
*inode
, char *buf
)
177 ext2_extent_handle_t handle
;
178 struct ext2_extent_info info
;
179 struct ext2fs_extent extent
;
181 if ((inode
->i_size_high
|| inode
->i_size
== 0) ||
182 (inode
->i_flags
& EXT2_INDEX_FL
))
185 if (inode
->i_flags
& EXT4_EXTENTS_FL
) {
186 if (inode
->i_size
> fs
->blocksize
)
188 if (ext2fs_extent_open2(fs
, ino
, inode
, &handle
))
191 if (ext2fs_extent_get_info(handle
, &info
) ||
192 (info
.num_entries
!= 1) ||
193 (info
.max_depth
!= 0))
195 if (ext2fs_extent_get(handle
, EXT2_EXTENT_ROOT
, &extent
) ||
196 (extent
.e_lblk
!= 0) ||
197 (extent
.e_len
!= 1) ||
198 (extent
.e_pblk
< fs
->super
->s_first_data_block
) ||
199 (extent
.e_pblk
>= ext2fs_blocks_count(fs
->super
)))
203 ext2fs_extent_free(handle
);
207 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
) {
210 if (ext2fs_inline_data_size(fs
, ino
, &inline_size
))
212 if (inode
->i_size
!= inline_size
)
218 blocks
= ext2fs_inode_data_blocks2(fs
, inode
);
220 if ((inode
->i_size
>= fs
->blocksize
) ||
221 (blocks
!= fs
->blocksize
>> 9) ||
222 (inode
->i_block
[0] < fs
->super
->s_first_data_block
) ||
223 (inode
->i_block
[0] >= ext2fs_blocks_count(fs
->super
)))
226 for (i
= 1; i
< EXT2_N_BLOCKS
; i
++)
227 if (inode
->i_block
[i
])
230 if (io_channel_read_blk64(fs
->io
, inode
->i_block
[0], 1, buf
))
233 len
= strnlen(buf
, fs
->blocksize
);
234 if (len
== fs
->blocksize
)
237 if (inode
->i_size
>= sizeof(inode
->i_block
))
240 len
= strnlen((char *)inode
->i_block
, sizeof(inode
->i_block
));
241 if (len
== sizeof(inode
->i_block
))
244 if (len
!= inode
->i_size
)
250 * If the immutable (or append-only) flag is set on the inode, offer
253 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
254 static void check_immutable(e2fsck_t ctx
, struct problem_context
*pctx
)
256 if (!(pctx
->inode
->i_flags
& BAD_SPECIAL_FLAGS
))
259 if (!fix_problem(ctx
, PR_1_SET_IMMUTABLE
, pctx
))
262 pctx
->inode
->i_flags
&= ~BAD_SPECIAL_FLAGS
;
263 e2fsck_write_inode(ctx
, pctx
->ino
, pctx
->inode
, "pass1");
267 * If device, fifo or socket, check size is zero -- if not offer to
270 static void check_size(e2fsck_t ctx
, struct problem_context
*pctx
)
272 struct ext2_inode
*inode
= pctx
->inode
;
274 if (EXT2_I_SIZE(inode
) == 0)
277 if (!fix_problem(ctx
, PR_1_SET_NONZSIZE
, pctx
))
280 ext2fs_inode_size_set(ctx
->fs
, inode
, 0);
281 e2fsck_write_inode(ctx
, pctx
->ino
, pctx
->inode
, "pass1");
284 static void check_ea_in_inode(e2fsck_t ctx
, struct problem_context
*pctx
)
286 struct ext2_super_block
*sb
= ctx
->fs
->super
;
287 struct ext2_inode_large
*inode
;
288 struct ext2_ext_attr_entry
*entry
;
290 unsigned int storage_size
, remain
;
291 problem_t problem
= 0;
293 inode
= (struct ext2_inode_large
*) pctx
->inode
;
294 storage_size
= EXT2_INODE_SIZE(ctx
->fs
->super
) - EXT2_GOOD_OLD_INODE_SIZE
-
295 inode
->i_extra_isize
;
296 start
= ((char *) inode
) + EXT2_GOOD_OLD_INODE_SIZE
+
297 inode
->i_extra_isize
+ sizeof(__u32
);
298 entry
= (struct ext2_ext_attr_entry
*) start
;
300 /* scan all entry's headers first */
302 /* take finish entry 0UL into account */
303 remain
= storage_size
- sizeof(__u32
);
305 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
308 /* header eats this space */
309 remain
-= sizeof(struct ext2_ext_attr_entry
);
311 /* is attribute name valid? */
312 if (EXT2_EXT_ATTR_SIZE(entry
->e_name_len
) > remain
) {
313 pctx
->num
= entry
->e_name_len
;
314 problem
= PR_1_ATTR_NAME_LEN
;
318 /* attribute len eats this space */
319 remain
-= EXT2_EXT_ATTR_SIZE(entry
->e_name_len
);
321 /* check value size */
322 if (entry
->e_value_size
> remain
) {
323 pctx
->num
= entry
->e_value_size
;
324 problem
= PR_1_ATTR_VALUE_SIZE
;
328 /* e_value_block must be 0 in inode's ea */
329 if (entry
->e_value_block
!= 0) {
330 pctx
->num
= entry
->e_value_block
;
331 problem
= PR_1_ATTR_VALUE_BLOCK
;
335 hash
= ext2fs_ext_attr_hash_entry(entry
,
336 start
+ entry
->e_value_offs
);
338 /* e_hash may be 0 in older inode's ea */
339 if (entry
->e_hash
!= 0 && entry
->e_hash
!= hash
) {
340 pctx
->num
= entry
->e_hash
;
341 problem
= PR_1_ATTR_HASH
;
345 remain
-= entry
->e_value_size
;
347 entry
= EXT2_EXT_ATTR_NEXT(entry
);
351 * it seems like a corruption. it's very unlikely we could repair
352 * EA(s) in automatic fashion -bzzz
354 if (problem
== 0 || !fix_problem(ctx
, problem
, pctx
))
357 /* simply remove all possible EA(s) */
358 *((__u32
*)start
) = 0UL;
359 e2fsck_write_inode_full(ctx
, pctx
->ino
, pctx
->inode
,
360 EXT2_INODE_SIZE(sb
), "pass1");
363 static void check_inode_extra_space(e2fsck_t ctx
, struct problem_context
*pctx
)
365 struct ext2_super_block
*sb
= ctx
->fs
->super
;
366 struct ext2_inode_large
*inode
;
370 inode
= (struct ext2_inode_large
*) pctx
->inode
;
371 if (EXT2_INODE_SIZE(sb
) == EXT2_GOOD_OLD_INODE_SIZE
) {
372 /* this isn't large inode. so, nothing to check */
377 printf("inode #%u, i_extra_size %d\n", pctx
->ino
,
378 inode
->i_extra_isize
);
380 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
381 min
= sizeof(inode
->i_extra_isize
) + sizeof(inode
->i_checksum_hi
);
382 max
= EXT2_INODE_SIZE(sb
) - EXT2_GOOD_OLD_INODE_SIZE
;
384 * For now we will allow i_extra_isize to be 0, but really
385 * implementations should never allow i_extra_isize to be 0
387 if (inode
->i_extra_isize
&&
388 (inode
->i_extra_isize
< min
|| inode
->i_extra_isize
> max
)) {
389 if (!fix_problem(ctx
, PR_1_EXTRA_ISIZE
, pctx
))
391 inode
->i_extra_isize
= min
;
392 e2fsck_write_inode_full(ctx
, pctx
->ino
, pctx
->inode
,
393 EXT2_INODE_SIZE(sb
), "pass1");
397 eamagic
= (__u32
*) (((char *) inode
) + EXT2_GOOD_OLD_INODE_SIZE
+
398 inode
->i_extra_isize
);
399 if (*eamagic
== EXT2_EXT_ATTR_MAGIC
) {
400 /* it seems inode has an extended attribute(s) in body */
401 check_ea_in_inode(ctx
, pctx
);
406 * Check to see if the inode might really be a directory, despite i_mode
408 * This is a lot of complexity for something for which I'm not really
409 * convinced happens frequently in the wild. If for any reason this
410 * causes any problems, take this code out.
411 * [tytso:20070331.0827EDT]
413 static void check_is_really_dir(e2fsck_t ctx
, struct problem_context
*pctx
,
416 struct ext2_inode
*inode
= pctx
->inode
;
417 struct ext2_dir_entry
*dirent
;
420 unsigned int i
, rec_len
, not_device
= 0;
425 * If the mode looks OK, we believe it. If the first block in
426 * the i_block array is 0, this cannot be a directory. If the
427 * inode is extent-mapped, it is still the case that the latter
428 * cannot be 0 - the magic number in the extent header would make
431 if (LINUX_S_ISDIR(inode
->i_mode
) || LINUX_S_ISREG(inode
->i_mode
) ||
432 LINUX_S_ISLNK(inode
->i_mode
) || inode
->i_block
[0] == 0)
436 * Check the block numbers in the i_block array for validity:
437 * zero blocks are skipped (but the first one cannot be zero -
438 * see above), other blocks are checked against the first and
439 * max data blocks (from the the superblock) and against the
440 * block bitmap. Any invalid block found means this cannot be
443 * If there are non-zero blocks past the fourth entry, then
444 * this cannot be a device file: we remember that for the next
447 * For extent mapped files, we don't do any sanity checking:
448 * just try to get the phys block of logical block 0 and run
451 * For inline data files, we just try to get the size of inline
452 * data. If it's true, we will treat it as a directory.
455 extent_fs
= (ctx
->fs
->super
->s_feature_incompat
&
456 EXT3_FEATURE_INCOMPAT_EXTENTS
);
457 inlinedata_fs
= (ctx
->fs
->super
->s_feature_incompat
&
458 EXT4_FEATURE_INCOMPAT_INLINE_DATA
);
459 if (inlinedata_fs
&& (inode
->i_flags
& EXT4_INLINE_DATA_FL
)) {
462 if (ext2fs_inline_data_size(ctx
->fs
, pctx
->ino
, &size
))
464 /* device files never have a "system.data" entry */
466 } else if (extent_fs
&& (inode
->i_flags
& EXT4_EXTENTS_FL
)) {
468 if (ext2fs_bmap2(ctx
->fs
, pctx
->ino
, inode
, 0, 0, 0, 0,
471 /* device files are never extent mapped */
474 for (i
=0; i
< EXT2_N_BLOCKS
; i
++) {
475 blk
= inode
->i_block
[i
];
481 if (blk
< ctx
->fs
->super
->s_first_data_block
||
482 blk
>= ext2fs_blocks_count(ctx
->fs
->super
) ||
483 ext2fs_fast_test_block_bitmap2(ctx
->block_found_map
,
485 return; /* Invalid block, can't be dir */
487 blk
= inode
->i_block
[0];
491 * If the mode says this is a device file and the i_links_count field
492 * is sane and we have not ruled it out as a device file previously,
493 * we declare it a device file, not a directory.
495 if ((LINUX_S_ISCHR(inode
->i_mode
) || LINUX_S_ISBLK(inode
->i_mode
)) &&
496 (inode
->i_links_count
== 1) && !not_device
)
499 /* read the first block */
500 ehandler_operation(_("reading directory block"));
501 retval
= ext2fs_read_dir_block4(ctx
->fs
, blk
, buf
, 0, pctx
->ino
);
502 ehandler_operation(0);
506 dirent
= (struct ext2_dir_entry
*) buf
;
507 retval
= ext2fs_get_rec_len(ctx
->fs
, dirent
, &rec_len
);
510 if ((ext2fs_dirent_name_len(dirent
) != 1) ||
511 (dirent
->name
[0] != '.') ||
512 (dirent
->inode
!= pctx
->ino
) ||
515 (rec_len
>= ctx
->fs
->blocksize
- 12))
518 dirent
= (struct ext2_dir_entry
*) (buf
+ rec_len
);
519 retval
= ext2fs_get_rec_len(ctx
->fs
, dirent
, &rec_len
);
522 if ((ext2fs_dirent_name_len(dirent
) != 2) ||
523 (dirent
->name
[0] != '.') ||
524 (dirent
->name
[1] != '.') ||
530 if (fix_problem(ctx
, PR_1_TREAT_AS_DIRECTORY
, pctx
)) {
531 inode
->i_mode
= (inode
->i_mode
& 07777) | LINUX_S_IFDIR
;
532 e2fsck_write_inode_full(ctx
, pctx
->ino
, inode
,
533 EXT2_INODE_SIZE(ctx
->fs
->super
),
534 "check_is_really_dir");
538 void e2fsck_setup_tdb_icount(e2fsck_t ctx
, int flags
,
541 unsigned int threshold
;
549 profile_get_string(ctx
->profile
, "scratch_files", "directory", 0, 0,
551 profile_get_uint(ctx
->profile
, "scratch_files",
552 "numdirs_threshold", 0, 0, &threshold
);
553 profile_get_boolean(ctx
->profile
, "scratch_files",
554 "icount", 0, 1, &enable
);
556 retval
= ext2fs_get_num_dirs(ctx
->fs
, &num_dirs
);
558 num_dirs
= 1024; /* Guess */
560 if (!enable
|| !tdb_dir
|| access(tdb_dir
, W_OK
) ||
561 (threshold
&& num_dirs
<= threshold
))
564 retval
= ext2fs_create_icount_tdb(ctx
->fs
, tdb_dir
, flags
, ret
);
569 static errcode_t
recheck_bad_inode_checksum(ext2_filsys fs
, ext2_ino_t ino
,
571 struct problem_context
*pctx
)
574 struct ext2_inode_large inode
;
577 * Reread inode. If we don't see checksum error, then this inode
578 * has been fixed elsewhere.
580 retval
= ext2fs_read_inode_full(fs
, ino
, (struct ext2_inode
*)&inode
,
582 if (retval
&& retval
!= EXT2_ET_INODE_CSUM_INVALID
)
588 * Checksum still doesn't match. That implies that the inode passes
589 * all the sanity checks, so maybe the checksum is simply corrupt.
590 * See if the user will go for fixing that.
592 if (!fix_problem(ctx
, PR_1_INODE_ONLY_CSUM_INVALID
, pctx
))
595 retval
= ext2fs_write_inode_full(fs
, ino
, (struct ext2_inode
*)&inode
,
603 static void reserve_block_for_root_repair(e2fsck_t ctx
)
607 ext2_filsys fs
= ctx
->fs
;
609 ctx
->root_repair_block
= 0;
610 if (ext2fs_test_inode_bitmap2(ctx
->inode_used_map
, EXT2_ROOT_INO
))
613 err
= ext2fs_new_block2(fs
, 0, ctx
->block_found_map
, &blk
);
616 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, blk
);
617 ctx
->root_repair_block
= blk
;
620 static void reserve_block_for_lnf_repair(e2fsck_t ctx
)
624 ext2_filsys fs
= ctx
->fs
;
625 static const char name
[] = "lost+found";
628 ctx
->lnf_repair_block
= 0;
629 if (!ext2fs_lookup(fs
, EXT2_ROOT_INO
, name
, sizeof(name
)-1, 0, &ino
))
632 err
= ext2fs_new_block2(fs
, 0, ctx
->block_found_map
, &blk
);
635 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, blk
);
636 ctx
->lnf_repair_block
= blk
;
639 void e2fsck_pass1(e2fsck_t ctx
)
643 ext2_filsys fs
= ctx
->fs
;
645 struct ext2_inode
*inode
= NULL
;
646 ext2_inode_scan scan
= NULL
;
647 char *block_buf
= NULL
;
648 #ifdef RESOURCE_TRACK
649 struct resource_track rtrack
;
651 unsigned char frag
, fsize
;
652 struct problem_context pctx
;
653 struct scan_callback_struct scan_struct
;
654 struct ext2_super_block
*sb
= ctx
->fs
->super
;
656 unsigned int save_type
;
657 int imagic_fs
, extent_fs
, inlinedata_fs
;
658 int low_dtime_check
= 1;
662 init_resource_track(&rtrack
, ctx
->fs
->io
);
663 clear_problem_context(&pctx
);
665 if (!(ctx
->options
& E2F_OPT_PREEN
))
666 fix_problem(ctx
, PR_1_PASS_HEADER
, &pctx
);
668 if ((fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_DIR_INDEX
) &&
669 !(ctx
->options
& E2F_OPT_NO
)) {
670 if (ext2fs_u32_list_create(&ctx
->dirs_to_hash
, 50))
671 ctx
->dirs_to_hash
= 0;
675 mtrace_print("Pass 1");
678 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
680 for (i
= EXT2_MIN_BLOCK_LOG_SIZE
; i
<= EXT2_MAX_BLOCK_LOG_SIZE
; i
++) {
681 max_sizes
= EXT2_NDIR_BLOCKS
+ EXT2_BPP(i
);
682 max_sizes
= max_sizes
+ EXT2_BPP(i
) * EXT2_BPP(i
);
683 max_sizes
= max_sizes
+ EXT2_BPP(i
) * EXT2_BPP(i
) * EXT2_BPP(i
);
684 max_sizes
= (max_sizes
* (1UL << i
));
685 ext2_max_sizes
[i
- EXT2_MIN_BLOCK_LOG_SIZE
] = max_sizes
;
689 imagic_fs
= (sb
->s_feature_compat
& EXT2_FEATURE_COMPAT_IMAGIC_INODES
);
690 extent_fs
= (sb
->s_feature_incompat
& EXT3_FEATURE_INCOMPAT_EXTENTS
);
691 inlinedata_fs
= (sb
->s_feature_incompat
&
692 EXT4_FEATURE_INCOMPAT_INLINE_DATA
);
695 * Allocate bitmaps structures
697 pctx
.errcode
= e2fsck_allocate_inode_bitmap(fs
, _("in-use inode map"),
698 EXT2FS_BMAP64_RBTREE
,
700 &ctx
->inode_used_map
);
703 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
704 ctx
->flags
|= E2F_FLAG_ABORT
;
707 pctx
.errcode
= e2fsck_allocate_inode_bitmap(fs
,
708 _("directory inode map"),
709 EXT2FS_BMAP64_AUTODIR
,
710 "inode_dir_map", &ctx
->inode_dir_map
);
713 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
714 ctx
->flags
|= E2F_FLAG_ABORT
;
717 pctx
.errcode
= e2fsck_allocate_inode_bitmap(fs
,
718 _("regular file inode map"), EXT2FS_BMAP64_RBTREE
,
719 "inode_reg_map", &ctx
->inode_reg_map
);
722 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
723 ctx
->flags
|= E2F_FLAG_ABORT
;
726 pctx
.errcode
= e2fsck_allocate_subcluster_bitmap(fs
,
727 _("in-use block map"), EXT2FS_BMAP64_RBTREE
,
728 "block_found_map", &ctx
->block_found_map
);
731 fix_problem(ctx
, PR_1_ALLOCATE_BBITMAP_ERROR
, &pctx
);
732 ctx
->flags
|= E2F_FLAG_ABORT
;
735 pctx
.errcode
= e2fsck_allocate_block_bitmap(fs
,
736 _("metadata block map"), EXT2FS_BMAP64_RBTREE
,
737 "block_metadata_map", &ctx
->block_metadata_map
);
740 fix_problem(ctx
, PR_1_ALLOCATE_BBITMAP_ERROR
, &pctx
);
741 ctx
->flags
|= E2F_FLAG_ABORT
;
744 e2fsck_setup_tdb_icount(ctx
, 0, &ctx
->inode_link_info
);
745 if (!ctx
->inode_link_info
) {
746 e2fsck_set_bitmap_type(fs
, EXT2FS_BMAP64_RBTREE
,
747 "inode_link_info", &save_type
);
748 pctx
.errcode
= ext2fs_create_icount2(fs
, 0, 0, 0,
749 &ctx
->inode_link_info
);
750 fs
->default_bitmap_type
= save_type
;
754 fix_problem(ctx
, PR_1_ALLOCATE_ICOUNT
, &pctx
);
755 ctx
->flags
|= E2F_FLAG_ABORT
;
758 inode_size
= EXT2_INODE_SIZE(fs
->super
);
759 inode
= (struct ext2_inode
*)
760 e2fsck_allocate_memory(ctx
, inode_size
, "scratch inode");
762 inodes_to_process
= (struct process_inode_block
*)
763 e2fsck_allocate_memory(ctx
,
764 (ctx
->process_inode_size
*
765 sizeof(struct process_inode_block
)),
766 "array of inodes to process");
767 process_inode_count
= 0;
769 pctx
.errcode
= ext2fs_init_dblist(fs
, 0);
771 fix_problem(ctx
, PR_1_ALLOCATE_DBCOUNT
, &pctx
);
772 ctx
->flags
|= E2F_FLAG_ABORT
;
777 * If the last orphan field is set, clear it, since the pass1
778 * processing will automatically find and clear the orphans.
779 * In the future, we may want to try using the last_orphan
780 * linked list ourselves, but for now, we clear it so that the
781 * ext3 mount code won't get confused.
783 if (!(ctx
->options
& E2F_OPT_READONLY
)) {
784 if (fs
->super
->s_last_orphan
) {
785 fs
->super
->s_last_orphan
= 0;
786 ext2fs_mark_super_dirty(fs
);
790 mark_table_blocks(ctx
);
791 pctx
.errcode
= ext2fs_convert_subcluster_bitmap(fs
,
792 &ctx
->block_found_map
);
794 fix_problem(ctx
, PR_1_CONVERT_SUBCLUSTER
, &pctx
);
795 ctx
->flags
|= E2F_FLAG_ABORT
;
798 block_buf
= (char *) e2fsck_allocate_memory(ctx
, fs
->blocksize
* 3,
799 "block interate buffer");
800 if (EXT2_INODE_SIZE(fs
->super
) == EXT2_GOOD_OLD_INODE_SIZE
)
801 e2fsck_use_inode_shortcuts(ctx
, 1);
802 e2fsck_intercept_block_allocations(ctx
);
803 old_op
= ehandler_operation(_("opening inode scan"));
804 pctx
.errcode
= ext2fs_open_inode_scan(fs
, ctx
->inode_buffer_blocks
,
806 ehandler_operation(old_op
);
808 fix_problem(ctx
, PR_1_ISCAN_ERROR
, &pctx
);
809 ctx
->flags
|= E2F_FLAG_ABORT
;
812 ext2fs_inode_scan_flags(scan
, EXT2_SF_SKIP_MISSING_ITABLE
, 0);
813 ctx
->stashed_inode
= inode
;
814 scan_struct
.ctx
= ctx
;
815 scan_struct
.block_buf
= block_buf
;
816 ext2fs_set_inode_callback(scan
, scan_callback
, &scan_struct
);
817 if (ctx
->progress
&& ((ctx
->progress
)(ctx
, 1, 0,
818 ctx
->fs
->group_desc_count
)))
820 if ((fs
->super
->s_wtime
< fs
->super
->s_inodes_count
) ||
821 (fs
->super
->s_mtime
< fs
->super
->s_inodes_count
) ||
822 (fs
->super
->s_mkfs_time
&&
823 fs
->super
->s_mkfs_time
< fs
->super
->s_inodes_count
))
826 if ((fs
->super
->s_feature_incompat
& EXT4_FEATURE_INCOMPAT_MMP
) &&
827 fs
->super
->s_mmp_block
> fs
->super
->s_first_data_block
&&
828 fs
->super
->s_mmp_block
< ext2fs_blocks_count(fs
->super
))
829 ext2fs_mark_block_bitmap2(ctx
->block_found_map
,
830 fs
->super
->s_mmp_block
);
832 /* Set up ctx->lost_and_found if possible */
833 (void) e2fsck_get_lost_and_found(ctx
, 0);
836 if (ino
% (fs
->super
->s_inodes_per_group
* 4) == 1) {
837 if (e2fsck_mmp_update(fs
))
840 old_op
= ehandler_operation(_("getting next inode from scan"));
841 pctx
.errcode
= ext2fs_get_next_inode_full(scan
, &ino
,
843 ehandler_operation(old_op
);
844 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
846 if (pctx
.errcode
== EXT2_ET_BAD_BLOCK_IN_INODE_TABLE
) {
847 if (!ctx
->inode_bb_map
)
849 ext2fs_mark_inode_bitmap2(ctx
->inode_bb_map
, ino
);
850 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
854 pctx
.errcode
!= EXT2_ET_INODE_CSUM_INVALID
) {
855 fix_problem(ctx
, PR_1_ISCAN_ERROR
, &pctx
);
856 ctx
->flags
|= E2F_FLAG_ABORT
;
863 ctx
->stashed_ino
= ino
;
865 /* Clear corrupt inode? */
866 if (pctx
.errcode
== EXT2_ET_INODE_CSUM_INVALID
) {
867 if (fix_problem(ctx
, PR_1_INODE_CSUM_INVALID
, &pctx
))
872 if (inode
->i_links_count
) {
873 pctx
.errcode
= ext2fs_icount_store(ctx
->inode_link_info
,
874 ino
, inode
->i_links_count
);
876 pctx
.num
= inode
->i_links_count
;
877 fix_problem(ctx
, PR_1_ICOUNT_STORE
, &pctx
);
878 ctx
->flags
|= E2F_FLAG_ABORT
;
883 /* Test for incorrect inline_data flags settings. */
884 if ((inode
->i_flags
& EXT4_INLINE_DATA_FL
) && !inlinedata_fs
&&
885 (ino
>= EXT2_FIRST_INODE(fs
->super
))) {
888 pctx
.errcode
= ext2fs_inline_data_size(fs
, ino
, &size
);
889 if (!pctx
.errcode
&& size
&&
890 !fix_problem(ctx
, PR_1_INLINE_DATA_FEATURE
, &pctx
)) {
891 sb
->s_feature_incompat
|=
892 EXT4_FEATURE_INCOMPAT_INLINE_DATA
;
893 ext2fs_mark_super_dirty(fs
);
895 } else if (!fix_problem(ctx
, PR_1_INLINE_DATA_SET
, &pctx
)) {
896 e2fsck_clear_inode(ctx
, ino
, inode
, 0, "pass1");
902 * Test for incorrect extent flag settings.
904 * On big-endian machines we must be careful:
905 * When the inode is read, the i_block array is not swapped
906 * if the extent flag is set. Therefore if we are testing
907 * for or fixing a wrongly-set flag, we must potentially
908 * (un)swap before testing, or after fixing.
912 * In this case the extents flag was set when read, so
913 * extent_header_verify is ok. If the inode is cleared,
914 * no need to swap... so no extra swapping here.
916 if ((inode
->i_flags
& EXT4_EXTENTS_FL
) && !extent_fs
&&
917 (inode
->i_links_count
|| (ino
== EXT2_BAD_INO
) ||
918 (ino
== EXT2_ROOT_INO
) || (ino
== EXT2_JOURNAL_INO
))) {
919 if ((ext2fs_extent_header_verify(inode
->i_block
,
920 sizeof(inode
->i_block
)) == 0) &&
921 fix_problem(ctx
, PR_1_EXTENT_FEATURE
, &pctx
)) {
922 sb
->s_feature_incompat
|= EXT3_FEATURE_INCOMPAT_EXTENTS
;
923 ext2fs_mark_super_dirty(fs
);
925 } else if (fix_problem(ctx
, PR_1_EXTENTS_SET
, &pctx
)) {
927 e2fsck_clear_inode(ctx
, ino
, inode
, 0, "pass1");
928 if (ino
== EXT2_BAD_INO
)
929 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
,
936 * For big-endian machines:
937 * If the inode didn't have the extents flag set when it
938 * was read, then the i_blocks array was swapped. To test
939 * as an extents header, we must swap it back first.
940 * IF we then set the extents flag, the entire i_block
941 * array must be un/re-swapped to make it proper extents data.
943 if (extent_fs
&& !(inode
->i_flags
& EXT4_EXTENTS_FL
) &&
944 (inode
->i_links_count
|| (ino
== EXT2_BAD_INO
) ||
945 (ino
== EXT2_ROOT_INO
) || (ino
== EXT2_JOURNAL_INO
)) &&
946 (LINUX_S_ISREG(inode
->i_mode
) ||
947 LINUX_S_ISDIR(inode
->i_mode
))) {
949 #ifdef WORDS_BIGENDIAN
950 __u32 tmp_block
[EXT2_N_BLOCKS
];
952 for (i
= 0; i
< EXT2_N_BLOCKS
; i
++)
953 tmp_block
[i
] = ext2fs_swab32(inode
->i_block
[i
]);
956 ehp
= inode
->i_block
;
958 if ((ext2fs_extent_header_verify(ehp
,
959 sizeof(inode
->i_block
)) == 0) &&
960 (fix_problem(ctx
, PR_1_UNSET_EXTENT_FL
, &pctx
))) {
961 inode
->i_flags
|= EXT4_EXTENTS_FL
;
962 #ifdef WORDS_BIGENDIAN
963 memcpy(inode
->i_block
, tmp_block
,
964 sizeof(inode
->i_block
));
966 e2fsck_write_inode(ctx
, ino
, inode
, "pass1");
970 if (ino
== EXT2_BAD_INO
) {
971 struct process_block_struct pb
;
973 if ((failed_csum
|| inode
->i_mode
|| inode
->i_uid
||
974 inode
->i_gid
|| inode
->i_links_count
||
975 (inode
->i_flags
& EXT4_INLINE_DATA_FL
) ||
976 inode
->i_file_acl
) &&
977 fix_problem(ctx
, PR_1_INVALID_BAD_INODE
, &pctx
)) {
978 memset(inode
, 0, sizeof(struct ext2_inode
));
979 e2fsck_write_inode(ctx
, ino
, inode
,
983 pctx
.errcode
= ext2fs_copy_bitmap(ctx
->block_found_map
,
987 fix_problem(ctx
, PR_1_ALLOCATE_BBITMAP_ERROR
, &pctx
);
988 ctx
->flags
|= E2F_FLAG_ABORT
;
991 pb
.ino
= EXT2_BAD_INO
;
992 pb
.num_blocks
= pb
.last_block
= 0;
993 pb
.last_db_block
= -1;
994 pb
.num_illegal_blocks
= 0;
995 pb
.suppress
= 0; pb
.clear
= 0; pb
.is_dir
= 0;
996 pb
.is_reg
= 0; pb
.fragmented
= 0; pb
.bbcheck
= 0;
1000 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
, 0,
1001 block_buf
, process_bad_block
, &pb
);
1002 ext2fs_free_block_bitmap(pb
.fs_meta_blocks
);
1004 fix_problem(ctx
, PR_1_BLOCK_ITERATE
, &pctx
);
1005 ctx
->flags
|= E2F_FLAG_ABORT
;
1009 if (!fix_problem(ctx
, PR_1_BBINODE_BAD_METABLOCK_PROMPT
, &pctx
)) {
1010 ctx
->flags
|= E2F_FLAG_ABORT
;
1013 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1014 clear_problem_context(&pctx
);
1016 } else if (ino
== EXT2_ROOT_INO
) {
1018 * Make sure the root inode is a directory; if
1019 * not, offer to clear it. It will be
1020 * regnerated in pass #3.
1022 if (!LINUX_S_ISDIR(inode
->i_mode
)) {
1023 if (fix_problem(ctx
, PR_1_ROOT_NO_DIR
, &pctx
))
1027 * If dtime is set, offer to clear it. mke2fs
1028 * version 0.2b created filesystems with the
1029 * dtime field set for the root and lost+found
1030 * directories. We won't worry about
1031 * /lost+found, since that can be regenerated
1032 * easily. But we will fix the root directory
1033 * as a special case.
1035 if (inode
->i_dtime
&& inode
->i_links_count
) {
1036 if (fix_problem(ctx
, PR_1_ROOT_DTIME
, &pctx
)) {
1038 e2fsck_write_inode(ctx
, ino
, inode
,
1042 } else if (ino
== EXT2_JOURNAL_INO
) {
1043 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1044 if (fs
->super
->s_journal_inum
== EXT2_JOURNAL_INO
) {
1045 if (!LINUX_S_ISREG(inode
->i_mode
) &&
1046 fix_problem(ctx
, PR_1_JOURNAL_BAD_MODE
,
1048 inode
->i_mode
= LINUX_S_IFREG
;
1049 e2fsck_write_inode(ctx
, ino
, inode
,
1052 check_blocks(ctx
, &pctx
, block_buf
);
1055 if ((inode
->i_links_count
||
1056 inode
->i_blocks
|| inode
->i_block
[0]) &&
1057 fix_problem(ctx
, PR_1_JOURNAL_INODE_NOT_CLEAR
,
1059 memset(inode
, 0, inode_size
);
1060 ext2fs_icount_store(ctx
->inode_link_info
,
1062 e2fsck_write_inode_full(ctx
, ino
, inode
,
1063 inode_size
, "pass1");
1065 } else if ((ino
== EXT4_USR_QUOTA_INO
) ||
1066 (ino
== EXT4_GRP_QUOTA_INO
)) {
1067 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1068 if ((fs
->super
->s_feature_ro_compat
&
1069 EXT4_FEATURE_RO_COMPAT_QUOTA
) &&
1070 ((fs
->super
->s_usr_quota_inum
== ino
) ||
1071 (fs
->super
->s_grp_quota_inum
== ino
))) {
1072 if (!LINUX_S_ISREG(inode
->i_mode
) &&
1073 fix_problem(ctx
, PR_1_QUOTA_BAD_MODE
,
1075 inode
->i_mode
= LINUX_S_IFREG
;
1076 e2fsck_write_inode(ctx
, ino
, inode
,
1079 check_blocks(ctx
, &pctx
, block_buf
);
1082 if ((inode
->i_links_count
||
1083 inode
->i_blocks
|| inode
->i_block
[0]) &&
1084 fix_problem(ctx
, PR_1_QUOTA_INODE_NOT_CLEAR
,
1086 memset(inode
, 0, inode_size
);
1087 ext2fs_icount_store(ctx
->inode_link_info
,
1089 e2fsck_write_inode_full(ctx
, ino
, inode
,
1090 inode_size
, "pass1");
1092 } else if (ino
< EXT2_FIRST_INODE(fs
->super
)) {
1093 problem_t problem
= 0;
1095 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1096 if (ino
== EXT2_BOOT_LOADER_INO
) {
1097 if (LINUX_S_ISDIR(inode
->i_mode
))
1098 problem
= PR_1_RESERVED_BAD_MODE
;
1099 } else if (ino
== EXT2_RESIZE_INO
) {
1100 if (inode
->i_mode
&&
1101 !LINUX_S_ISREG(inode
->i_mode
))
1102 problem
= PR_1_RESERVED_BAD_MODE
;
1104 if (inode
->i_mode
!= 0)
1105 problem
= PR_1_RESERVED_BAD_MODE
;
1108 if (fix_problem(ctx
, problem
, &pctx
)) {
1110 e2fsck_write_inode(ctx
, ino
, inode
,
1114 check_blocks(ctx
, &pctx
, block_buf
);
1119 * Check for inodes who might have been part of the
1120 * orphaned list linked list. They should have gotten
1121 * dealt with by now, unless the list had somehow been
1124 * FIXME: In the future, inodes which are still in use
1125 * (and which are therefore) pending truncation should
1126 * be handled specially. Right now we just clear the
1127 * dtime field, and the normal e2fsck handling of
1128 * inodes where i_size and the inode blocks are
1129 * inconsistent is to fix i_size, instead of releasing
1130 * the extra blocks. This won't catch the inodes that
1131 * was at the end of the orphan list, but it's better
1132 * than nothing. The right answer is that there
1133 * shouldn't be any bugs in the orphan list handling. :-)
1135 if (inode
->i_dtime
&& low_dtime_check
&&
1136 inode
->i_dtime
< ctx
->fs
->super
->s_inodes_count
) {
1137 if (fix_problem(ctx
, PR_1_LOW_DTIME
, &pctx
)) {
1138 inode
->i_dtime
= inode
->i_links_count
?
1140 e2fsck_write_inode(ctx
, ino
, inode
,
1146 * This code assumes that deleted inodes have
1147 * i_links_count set to 0.
1149 if (!inode
->i_links_count
) {
1150 if (!inode
->i_dtime
&& inode
->i_mode
) {
1151 if (fix_problem(ctx
,
1152 PR_1_ZERO_DTIME
, &pctx
)) {
1153 inode
->i_dtime
= ctx
->now
;
1154 e2fsck_write_inode(ctx
, ino
, inode
,
1161 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1162 * deleted files. Oops.
1164 * Since all new ext2 implementations get this right,
1165 * we now assume that the case of non-zero
1166 * i_links_count and non-zero dtime means that we
1167 * should keep the file, not delete it.
1170 if (inode
->i_dtime
) {
1171 if (fix_problem(ctx
, PR_1_SET_DTIME
, &pctx
)) {
1173 e2fsck_write_inode(ctx
, ino
, inode
, "pass1");
1177 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1178 switch (fs
->super
->s_creator_os
) {
1180 frag
= inode
->osd2
.hurd2
.h_i_frag
;
1181 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
1187 if (inode
->i_faddr
|| frag
|| fsize
||
1188 (LINUX_S_ISDIR(inode
->i_mode
) && inode
->i_dir_acl
))
1189 mark_inode_bad(ctx
, ino
);
1190 if (!(fs
->super
->s_feature_incompat
&
1191 EXT4_FEATURE_INCOMPAT_64BIT
) &&
1192 inode
->osd2
.linux2
.l_i_file_acl_high
!= 0)
1193 mark_inode_bad(ctx
, ino
);
1194 if ((fs
->super
->s_creator_os
== EXT2_OS_LINUX
) &&
1195 !(fs
->super
->s_feature_ro_compat
&
1196 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
) &&
1197 (inode
->osd2
.linux2
.l_i_blocks_hi
!= 0))
1198 mark_inode_bad(ctx
, ino
);
1199 if (inode
->i_flags
& EXT2_IMAGIC_FL
) {
1201 if (!ctx
->inode_imagic_map
)
1202 alloc_imagic_map(ctx
);
1203 ext2fs_mark_inode_bitmap2(ctx
->inode_imagic_map
,
1206 if (fix_problem(ctx
, PR_1_SET_IMAGIC
, &pctx
)) {
1207 inode
->i_flags
&= ~EXT2_IMAGIC_FL
;
1208 e2fsck_write_inode(ctx
, ino
,
1214 check_inode_extra_space(ctx
, &pctx
);
1215 check_is_really_dir(ctx
, &pctx
, block_buf
);
1218 * ext2fs_inode_has_valid_blocks2 does not actually look
1219 * at i_block[] values, so not endian-sensitive here.
1221 if (extent_fs
&& (inode
->i_flags
& EXT4_EXTENTS_FL
) &&
1222 LINUX_S_ISLNK(inode
->i_mode
) &&
1223 !ext2fs_inode_has_valid_blocks2(fs
, inode
) &&
1224 fix_problem(ctx
, PR_1_FAST_SYMLINK_EXTENT_FL
, &pctx
)) {
1225 inode
->i_flags
&= ~EXT4_EXTENTS_FL
;
1226 e2fsck_write_inode(ctx
, ino
, inode
, "pass1");
1229 if (LINUX_S_ISDIR(inode
->i_mode
)) {
1230 ext2fs_mark_inode_bitmap2(ctx
->inode_dir_map
, ino
);
1231 e2fsck_add_dir_info(ctx
, ino
, 0);
1232 ctx
->fs_directory_count
++;
1233 } else if (LINUX_S_ISREG (inode
->i_mode
)) {
1234 ext2fs_mark_inode_bitmap2(ctx
->inode_reg_map
, ino
);
1235 ctx
->fs_regular_count
++;
1236 } else if (LINUX_S_ISCHR (inode
->i_mode
) &&
1237 e2fsck_pass1_check_device_inode(fs
, inode
)) {
1238 check_immutable(ctx
, &pctx
);
1239 check_size(ctx
, &pctx
);
1240 ctx
->fs_chardev_count
++;
1241 } else if (LINUX_S_ISBLK (inode
->i_mode
) &&
1242 e2fsck_pass1_check_device_inode(fs
, inode
)) {
1243 check_immutable(ctx
, &pctx
);
1244 check_size(ctx
, &pctx
);
1245 ctx
->fs_blockdev_count
++;
1246 } else if (LINUX_S_ISLNK (inode
->i_mode
) &&
1247 e2fsck_pass1_check_symlink(fs
, ino
, inode
,
1249 check_immutable(ctx
, &pctx
);
1250 ctx
->fs_symlinks_count
++;
1251 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
) {
1253 } else if (ext2fs_inode_data_blocks(fs
, inode
) == 0) {
1254 ctx
->fs_fast_symlinks_count
++;
1255 check_blocks(ctx
, &pctx
, block_buf
);
1259 else if (LINUX_S_ISFIFO (inode
->i_mode
) &&
1260 e2fsck_pass1_check_device_inode(fs
, inode
)) {
1261 check_immutable(ctx
, &pctx
);
1262 check_size(ctx
, &pctx
);
1263 ctx
->fs_fifo_count
++;
1264 } else if ((LINUX_S_ISSOCK (inode
->i_mode
)) &&
1265 e2fsck_pass1_check_device_inode(fs
, inode
)) {
1266 check_immutable(ctx
, &pctx
);
1267 check_size(ctx
, &pctx
);
1268 ctx
->fs_sockets_count
++;
1270 mark_inode_bad(ctx
, ino
);
1271 if (!(inode
->i_flags
& EXT4_EXTENTS_FL
) &&
1272 !(inode
->i_flags
& EXT4_INLINE_DATA_FL
)) {
1273 if (inode
->i_block
[EXT2_IND_BLOCK
])
1274 ctx
->fs_ind_count
++;
1275 if (inode
->i_block
[EXT2_DIND_BLOCK
])
1276 ctx
->fs_dind_count
++;
1277 if (inode
->i_block
[EXT2_TIND_BLOCK
])
1278 ctx
->fs_tind_count
++;
1280 if (!(inode
->i_flags
& EXT4_EXTENTS_FL
) &&
1281 !(inode
->i_flags
& EXT4_INLINE_DATA_FL
) &&
1282 (inode
->i_block
[EXT2_IND_BLOCK
] ||
1283 inode
->i_block
[EXT2_DIND_BLOCK
] ||
1284 inode
->i_block
[EXT2_TIND_BLOCK
] ||
1285 ext2fs_file_acl_block(fs
, inode
))) {
1286 inodes_to_process
[process_inode_count
].ino
= ino
;
1287 inodes_to_process
[process_inode_count
].inode
= *inode
;
1288 process_inode_count
++;
1290 check_blocks(ctx
, &pctx
, block_buf
);
1293 * If the inode failed the checksum and the user didn't
1294 * clear the inode, test the checksum again -- if it still
1295 * fails, ask the user if the checksum should be corrected.
1298 pctx
.errcode
= recheck_bad_inode_checksum(fs
, ino
, ctx
,
1301 ctx
->flags
|= E2F_FLAG_ABORT
;
1306 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1309 if (process_inode_count
>= ctx
->process_inode_size
) {
1310 process_inodes(ctx
, block_buf
);
1312 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1316 process_inodes(ctx
, block_buf
);
1317 ext2fs_close_inode_scan(scan
);
1320 reserve_block_for_root_repair(ctx
);
1321 reserve_block_for_lnf_repair(ctx
);
1324 * If any extended attribute blocks' reference counts need to
1325 * be adjusted, either up (ctx->refcount_extra), or down
1326 * (ctx->refcount), then fix them.
1328 if (ctx
->refcount
) {
1329 adjust_extattr_refcount(ctx
, ctx
->refcount
, block_buf
, -1);
1330 ea_refcount_free(ctx
->refcount
);
1333 if (ctx
->refcount_extra
) {
1334 adjust_extattr_refcount(ctx
, ctx
->refcount_extra
,
1336 ea_refcount_free(ctx
->refcount_extra
);
1337 ctx
->refcount_extra
= 0;
1340 if (ctx
->invalid_bitmaps
)
1341 handle_fs_bad_blocks(ctx
);
1343 /* We don't need the block_ea_map any more */
1344 if (ctx
->block_ea_map
) {
1345 ext2fs_free_block_bitmap(ctx
->block_ea_map
);
1346 ctx
->block_ea_map
= 0;
1349 if (ctx
->flags
& E2F_FLAG_RESIZE_INODE
) {
1350 clear_problem_context(&pctx
);
1351 pctx
.errcode
= ext2fs_create_resize_inode(fs
);
1353 if (!fix_problem(ctx
, PR_1_RESIZE_INODE_CREATE
,
1355 ctx
->flags
|= E2F_FLAG_ABORT
;
1360 if (!pctx
.errcode
) {
1361 e2fsck_read_inode(ctx
, EXT2_RESIZE_INO
, inode
,
1363 inode
->i_mtime
= ctx
->now
;
1364 e2fsck_write_inode(ctx
, EXT2_RESIZE_INO
, inode
,
1367 ctx
->flags
&= ~E2F_FLAG_RESIZE_INODE
;
1370 if (ctx
->flags
& E2F_FLAG_RESTART
) {
1372 * Only the master copy of the superblock and block
1373 * group descriptors are going to be written during a
1374 * restart, so set the superblock to be used to be the
1375 * master superblock.
1377 ctx
->use_superblock
= 0;
1382 if (ctx
->block_dup_map
) {
1383 if (ctx
->options
& E2F_OPT_PREEN
) {
1384 clear_problem_context(&pctx
);
1385 fix_problem(ctx
, PR_1_DUP_BLOCKS_PREENSTOP
, &pctx
);
1387 e2fsck_pass1_dupblocks(ctx
, block_buf
);
1389 ext2fs_free_mem(&inodes_to_process
);
1391 e2fsck_use_inode_shortcuts(ctx
, 0);
1394 ext2fs_close_inode_scan(scan
);
1396 ext2fs_free_mem(&block_buf
);
1398 ext2fs_free_mem(&inode
);
1401 * The l+f inode may have been cleared, so zap it now and
1402 * later passes will recalculate it if necessary
1404 ctx
->lost_and_found
= 0;
1406 if ((ctx
->flags
& E2F_FLAG_SIGNAL_MASK
) == 0)
1407 print_resource_track(ctx
, _("Pass 1"), &rtrack
, ctx
->fs
->io
);
1411 * When the inode_scan routines call this callback at the end of the
1412 * glock group, call process_inodes.
1414 static errcode_t
scan_callback(ext2_filsys fs
,
1415 ext2_inode_scan scan
EXT2FS_ATTR((unused
)),
1416 dgrp_t group
, void * priv_data
)
1418 struct scan_callback_struct
*scan_struct
;
1421 scan_struct
= (struct scan_callback_struct
*) priv_data
;
1422 ctx
= scan_struct
->ctx
;
1424 process_inodes((e2fsck_t
) fs
->priv_data
, scan_struct
->block_buf
);
1427 if ((ctx
->progress
)(ctx
, 1, group
+1,
1428 ctx
->fs
->group_desc_count
))
1429 return EXT2_ET_CANCEL_REQUESTED
;
1435 * Process the inodes in the "inodes to process" list.
1437 static void process_inodes(e2fsck_t ctx
, char *block_buf
)
1440 struct ext2_inode
*old_stashed_inode
;
1441 ext2_ino_t old_stashed_ino
;
1442 const char *old_operation
;
1444 struct problem_context pctx
;
1447 printf("begin process_inodes: ");
1449 if (process_inode_count
== 0)
1451 old_operation
= ehandler_operation(0);
1452 old_stashed_inode
= ctx
->stashed_inode
;
1453 old_stashed_ino
= ctx
->stashed_ino
;
1454 qsort(inodes_to_process
, process_inode_count
,
1455 sizeof(struct process_inode_block
), process_inode_cmp
);
1456 clear_problem_context(&pctx
);
1457 for (i
=0; i
< process_inode_count
; i
++) {
1458 pctx
.inode
= ctx
->stashed_inode
= &inodes_to_process
[i
].inode
;
1459 pctx
.ino
= ctx
->stashed_ino
= inodes_to_process
[i
].ino
;
1462 printf("%u ", pctx
.ino
);
1464 sprintf(buf
, _("reading indirect blocks of inode %u"),
1466 ehandler_operation(buf
);
1467 check_blocks(ctx
, &pctx
, block_buf
);
1468 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1471 ctx
->stashed_inode
= old_stashed_inode
;
1472 ctx
->stashed_ino
= old_stashed_ino
;
1473 process_inode_count
= 0;
1475 printf("end process inodes\n");
1477 ehandler_operation(old_operation
);
1480 static EXT2_QSORT_TYPE
process_inode_cmp(const void *a
, const void *b
)
1482 const struct process_inode_block
*ib_a
=
1483 (const struct process_inode_block
*) a
;
1484 const struct process_inode_block
*ib_b
=
1485 (const struct process_inode_block
*) b
;
1488 ret
= (ib_a
->inode
.i_block
[EXT2_IND_BLOCK
] -
1489 ib_b
->inode
.i_block
[EXT2_IND_BLOCK
]);
1492 * We only call process_inodes() for non-extent
1493 * inodes, so it's OK to pass NULL to
1494 * ext2fs_file_acl_block() here.
1496 ret
= ext2fs_file_acl_block(0, &(ib_a
->inode
)) -
1497 ext2fs_file_acl_block(0, &(ib_b
->inode
));
1499 ret
= ib_a
->ino
- ib_b
->ino
;
1504 * Mark an inode as being bad in some what
1506 static void mark_inode_bad(e2fsck_t ctx
, ino_t ino
)
1508 struct problem_context pctx
;
1510 if (!ctx
->inode_bad_map
) {
1511 clear_problem_context(&pctx
);
1513 pctx
.errcode
= e2fsck_allocate_inode_bitmap(ctx
->fs
,
1514 _("bad inode map"), EXT2FS_BMAP64_RBTREE
,
1515 "inode_bad_map", &ctx
->inode_bad_map
);
1518 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
1519 /* Should never get here */
1520 ctx
->flags
|= E2F_FLAG_ABORT
;
1524 ext2fs_mark_inode_bitmap2(ctx
->inode_bad_map
, ino
);
1529 * This procedure will allocate the inode "bb" (badblock) map table
1531 static void alloc_bb_map(e2fsck_t ctx
)
1533 struct problem_context pctx
;
1535 clear_problem_context(&pctx
);
1536 pctx
.errcode
= e2fsck_allocate_inode_bitmap(ctx
->fs
,
1537 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE
,
1538 "inode_bb_map", &ctx
->inode_bb_map
);
1541 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
1542 /* Should never get here */
1543 ctx
->flags
|= E2F_FLAG_ABORT
;
1549 * This procedure will allocate the inode imagic table
1551 static void alloc_imagic_map(e2fsck_t ctx
)
1553 struct problem_context pctx
;
1555 clear_problem_context(&pctx
);
1556 pctx
.errcode
= e2fsck_allocate_inode_bitmap(ctx
->fs
,
1557 _("imagic inode map"), EXT2FS_BMAP64_RBTREE
,
1558 "inode_imagic_map", &ctx
->inode_imagic_map
);
1561 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
1562 /* Should never get here */
1563 ctx
->flags
|= E2F_FLAG_ABORT
;
1569 * Marks a block as in use, setting the dup_map if it's been set
1570 * already. Called by process_block and process_bad_block.
1572 * WARNING: Assumes checks have already been done to make sure block
1573 * is valid. This is true in both process_block and process_bad_block.
1575 static _INLINE_
void mark_block_used(e2fsck_t ctx
, blk64_t block
)
1577 struct problem_context pctx
;
1579 clear_problem_context(&pctx
);
1581 if (ext2fs_fast_test_block_bitmap2(ctx
->block_found_map
, block
)) {
1582 if (!ctx
->block_dup_map
) {
1583 pctx
.errcode
= e2fsck_allocate_block_bitmap(ctx
->fs
,
1584 _("multiply claimed block map"),
1585 EXT2FS_BMAP64_RBTREE
, "block_dup_map",
1586 &ctx
->block_dup_map
);
1589 fix_problem(ctx
, PR_1_ALLOCATE_BBITMAP_ERROR
,
1591 /* Should never get here */
1592 ctx
->flags
|= E2F_FLAG_ABORT
;
1596 ext2fs_fast_mark_block_bitmap2(ctx
->block_dup_map
, block
);
1598 ext2fs_fast_mark_block_bitmap2(ctx
->block_found_map
, block
);
1602 static _INLINE_
void mark_blocks_used(e2fsck_t ctx
, blk64_t block
,
1605 if (ext2fs_test_block_bitmap_range2(ctx
->block_found_map
, block
, num
))
1606 ext2fs_mark_block_bitmap_range2(ctx
->block_found_map
, block
, num
);
1609 mark_block_used(ctx
, block
++);
1613 * Adjust the extended attribute block's reference counts at the end
1614 * of pass 1, either by subtracting out references for EA blocks that
1615 * are still referenced in ctx->refcount, or by adding references for
1616 * EA blocks that had extra references as accounted for in
1617 * ctx->refcount_extra.
1619 static void adjust_extattr_refcount(e2fsck_t ctx
, ext2_refcount_t refcount
,
1620 char *block_buf
, int adjust_sign
)
1622 struct ext2_ext_attr_header
*header
;
1623 struct problem_context pctx
;
1624 ext2_filsys fs
= ctx
->fs
;
1629 clear_problem_context(&pctx
);
1631 ea_refcount_intr_begin(refcount
);
1633 if ((blk
= ea_refcount_intr_next(refcount
, &count
)) == 0)
1636 pctx
.errcode
= ext2fs_read_ext_attr3(fs
, blk
, block_buf
,
1639 fix_problem(ctx
, PR_1_EXTATTR_READ_ABORT
, &pctx
);
1642 header
= (struct ext2_ext_attr_header
*) block_buf
;
1643 pctx
.blkcount
= header
->h_refcount
;
1644 should_be
= header
->h_refcount
+ adjust_sign
* count
;
1645 pctx
.num
= should_be
;
1646 if (fix_problem(ctx
, PR_1_EXTATTR_REFCOUNT
, &pctx
)) {
1647 header
->h_refcount
= should_be
;
1648 pctx
.errcode
= ext2fs_write_ext_attr3(fs
, blk
,
1652 fix_problem(ctx
, PR_1_EXTATTR_WRITE_ABORT
,
1661 * Handle processing the extended attribute blocks
1663 static int check_ext_attr(e2fsck_t ctx
, struct problem_context
*pctx
,
1666 ext2_filsys fs
= ctx
->fs
;
1667 ext2_ino_t ino
= pctx
->ino
;
1668 struct ext2_inode
*inode
= pctx
->inode
;
1671 struct ext2_ext_attr_header
*header
;
1672 struct ext2_ext_attr_entry
*entry
;
1674 region_t region
= 0;
1675 int failed_csum
= 0;
1677 blk
= ext2fs_file_acl_block(fs
, inode
);
1682 * If the Extended attribute flag isn't set, then a non-zero
1683 * file acl means that the inode is corrupted.
1685 * Or if the extended attribute block is an invalid block,
1686 * then the inode is also corrupted.
1688 if (!(fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_EXT_ATTR
) ||
1689 (blk
< fs
->super
->s_first_data_block
) ||
1690 (blk
>= ext2fs_blocks_count(fs
->super
))) {
1691 mark_inode_bad(ctx
, ino
);
1695 /* If ea bitmap hasn't been allocated, create it */
1696 if (!ctx
->block_ea_map
) {
1697 pctx
->errcode
= e2fsck_allocate_block_bitmap(fs
,
1698 _("ext attr block map"),
1699 EXT2FS_BMAP64_RBTREE
, "block_ea_map",
1700 &ctx
->block_ea_map
);
1701 if (pctx
->errcode
) {
1703 fix_problem(ctx
, PR_1_ALLOCATE_BBITMAP_ERROR
, pctx
);
1704 ctx
->flags
|= E2F_FLAG_ABORT
;
1709 /* Create the EA refcount structure if necessary */
1710 if (!ctx
->refcount
) {
1711 pctx
->errcode
= ea_refcount_create(0, &ctx
->refcount
);
1712 if (pctx
->errcode
) {
1714 fix_problem(ctx
, PR_1_ALLOCATE_REFCOUNT
, pctx
);
1715 ctx
->flags
|= E2F_FLAG_ABORT
;
1721 /* Debugging text */
1722 printf("Inode %u has EA block %u\n", ino
, blk
);
1725 /* Have we seen this EA block before? */
1726 if (ext2fs_fast_test_block_bitmap2(ctx
->block_ea_map
, blk
)) {
1727 if (ea_refcount_decrement(ctx
->refcount
, blk
, 0) == 0)
1729 /* Ooops, this EA was referenced more than it stated */
1730 if (!ctx
->refcount_extra
) {
1731 pctx
->errcode
= ea_refcount_create(0,
1732 &ctx
->refcount_extra
);
1733 if (pctx
->errcode
) {
1735 fix_problem(ctx
, PR_1_ALLOCATE_REFCOUNT
, pctx
);
1736 ctx
->flags
|= E2F_FLAG_ABORT
;
1740 ea_refcount_increment(ctx
->refcount_extra
, blk
, 0);
1745 * OK, we haven't seen this EA block yet. So we need to
1749 pctx
->errcode
= ext2fs_read_ext_attr3(fs
, blk
, block_buf
, pctx
->ino
);
1750 if (pctx
->errcode
== EXT2_ET_EXT_ATTR_CSUM_INVALID
) {
1753 } else if (pctx
->errcode
== EXT2_ET_BAD_EA_HEADER
)
1756 if (pctx
->errcode
&&
1757 fix_problem(ctx
, PR_1_READ_EA_BLOCK
, pctx
)) {
1761 header
= (struct ext2_ext_attr_header
*) block_buf
;
1762 pctx
->blk
= ext2fs_file_acl_block(fs
, inode
);
1763 if (((ctx
->ext_attr_ver
== 1) &&
1764 (header
->h_magic
!= EXT2_EXT_ATTR_MAGIC_v1
)) ||
1765 ((ctx
->ext_attr_ver
== 2) &&
1766 (header
->h_magic
!= EXT2_EXT_ATTR_MAGIC
))) {
1767 if (fix_problem(ctx
, PR_1_BAD_EA_BLOCK
, pctx
))
1771 if (header
->h_blocks
!= 1) {
1772 if (fix_problem(ctx
, PR_1_EA_MULTI_BLOCK
, pctx
))
1776 if (pctx
->errcode
&& fix_problem(ctx
, PR_1_READ_EA_BLOCK
, pctx
))
1779 region
= region_create(0, fs
->blocksize
);
1781 fix_problem(ctx
, PR_1_EA_ALLOC_REGION_ABORT
, pctx
);
1782 ctx
->flags
|= E2F_FLAG_ABORT
;
1785 if (region_allocate(region
, 0, sizeof(struct ext2_ext_attr_header
))) {
1786 if (fix_problem(ctx
, PR_1_EA_ALLOC_COLLISION
, pctx
))
1790 entry
= (struct ext2_ext_attr_entry
*)(header
+1);
1791 end
= block_buf
+ fs
->blocksize
;
1792 while ((char *)entry
< end
&& *(__u32
*)entry
) {
1795 if (region_allocate(region
, (char *)entry
- (char *)header
,
1796 EXT2_EXT_ATTR_LEN(entry
->e_name_len
))) {
1797 if (fix_problem(ctx
, PR_1_EA_ALLOC_COLLISION
, pctx
))
1801 if ((ctx
->ext_attr_ver
== 1 &&
1802 (entry
->e_name_len
== 0 || entry
->e_name_index
!= 0)) ||
1803 (ctx
->ext_attr_ver
== 2 &&
1804 entry
->e_name_index
== 0)) {
1805 if (fix_problem(ctx
, PR_1_EA_BAD_NAME
, pctx
))
1809 if (entry
->e_value_block
!= 0) {
1810 if (fix_problem(ctx
, PR_1_EA_BAD_VALUE
, pctx
))
1813 if (entry
->e_value_offs
+ entry
->e_value_size
> fs
->blocksize
) {
1814 if (fix_problem(ctx
, PR_1_EA_BAD_VALUE
, pctx
))
1818 if (entry
->e_value_size
&&
1819 region_allocate(region
, entry
->e_value_offs
,
1820 EXT2_EXT_ATTR_SIZE(entry
->e_value_size
))) {
1821 if (fix_problem(ctx
, PR_1_EA_ALLOC_COLLISION
, pctx
))
1825 hash
= ext2fs_ext_attr_hash_entry(entry
, block_buf
+
1826 entry
->e_value_offs
);
1828 if (entry
->e_hash
!= hash
) {
1829 pctx
->num
= entry
->e_hash
;
1830 if (fix_problem(ctx
, PR_1_ATTR_HASH
, pctx
))
1832 entry
->e_hash
= hash
;
1835 entry
= EXT2_EXT_ATTR_NEXT(entry
);
1837 if (region_allocate(region
, (char *)entry
- (char *)header
, 4)) {
1838 if (fix_problem(ctx
, PR_1_EA_ALLOC_COLLISION
, pctx
))
1841 region_free(region
);
1844 * We only get here if there was no other errors that were fixed.
1845 * If there was a checksum fail, ask to correct it.
1848 fix_problem(ctx
, PR_1_EA_BLOCK_ONLY_CSUM_INVALID
, pctx
)) {
1849 pctx
->errcode
= ext2fs_write_ext_attr3(fs
, blk
, block_buf
,
1855 count
= header
->h_refcount
- 1;
1857 ea_refcount_store(ctx
->refcount
, blk
, count
);
1858 mark_block_used(ctx
, blk
);
1859 ext2fs_fast_mark_block_bitmap2(ctx
->block_ea_map
, blk
);
1864 region_free(region
);
1865 ext2fs_file_acl_block_set(fs
, inode
, 0);
1866 e2fsck_write_inode(ctx
, ino
, inode
, "check_ext_attr");
1870 /* Returns 1 if bad htree, 0 if OK */
1871 static int handle_htree(e2fsck_t ctx
, struct problem_context
*pctx
,
1872 ext2_ino_t ino
, struct ext2_inode
*inode
,
1875 struct ext2_dx_root_info
*root
;
1876 ext2_filsys fs
= ctx
->fs
;
1880 if ((!LINUX_S_ISDIR(inode
->i_mode
) &&
1881 fix_problem(ctx
, PR_1_HTREE_NODIR
, pctx
)) ||
1882 (!(fs
->super
->s_feature_compat
& EXT2_FEATURE_COMPAT_DIR_INDEX
) &&
1883 fix_problem(ctx
, PR_1_HTREE_SET
, pctx
)))
1886 pctx
->errcode
= ext2fs_bmap2(fs
, ino
, inode
, 0, 0, 0, 0, &blk
);
1888 if ((pctx
->errcode
) ||
1890 (blk
< fs
->super
->s_first_data_block
) ||
1891 (blk
>= ext2fs_blocks_count(fs
->super
))) {
1892 if (fix_problem(ctx
, PR_1_HTREE_BADROOT
, pctx
))
1898 retval
= io_channel_read_blk64(fs
->io
, blk
, 1, block_buf
);
1899 if (retval
&& fix_problem(ctx
, PR_1_HTREE_BADROOT
, pctx
))
1902 /* XXX should check that beginning matches a directory */
1903 root
= (struct ext2_dx_root_info
*) (block_buf
+ 24);
1905 if ((root
->reserved_zero
|| root
->info_length
< 8) &&
1906 fix_problem(ctx
, PR_1_HTREE_BADROOT
, pctx
))
1909 pctx
->num
= root
->hash_version
;
1910 if ((root
->hash_version
!= EXT2_HASH_LEGACY
) &&
1911 (root
->hash_version
!= EXT2_HASH_HALF_MD4
) &&
1912 (root
->hash_version
!= EXT2_HASH_TEA
) &&
1913 fix_problem(ctx
, PR_1_HTREE_HASHV
, pctx
))
1916 if ((root
->unused_flags
& EXT2_HASH_FLAG_INCOMPAT
) &&
1917 fix_problem(ctx
, PR_1_HTREE_INCOMPAT
, pctx
))
1920 pctx
->num
= root
->indirect_levels
;
1921 if ((root
->indirect_levels
> 1) &&
1922 fix_problem(ctx
, PR_1_HTREE_DEPTH
, pctx
))
1928 void e2fsck_clear_inode(e2fsck_t ctx
, ext2_ino_t ino
,
1929 struct ext2_inode
*inode
, int restart_flag
,
1933 inode
->i_links_count
= 0;
1934 ext2fs_icount_store(ctx
->inode_link_info
, ino
, 0);
1935 inode
->i_dtime
= ctx
->now
;
1937 ext2fs_unmark_inode_bitmap2(ctx
->inode_dir_map
, ino
);
1938 ext2fs_unmark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1939 if (ctx
->inode_reg_map
)
1940 ext2fs_unmark_inode_bitmap2(ctx
->inode_reg_map
, ino
);
1941 if (ctx
->inode_bad_map
)
1942 ext2fs_unmark_inode_bitmap2(ctx
->inode_bad_map
, ino
);
1945 * If the inode was partially accounted for before processing
1946 * was aborted, we need to restart the pass 1 scan.
1948 ctx
->flags
|= restart_flag
;
1950 if (ino
== EXT2_BAD_INO
)
1951 memset(inode
, 0, sizeof(struct ext2_inode
));
1953 e2fsck_write_inode(ctx
, ino
, inode
, source
);
1957 * Use the multiple-blocks reclamation code to fix alignment problems in
1958 * a bigalloc filesystem. We want a logical cluster to map to *only* one
1959 * physical cluster, and we want the block offsets within that cluster to
1962 static int has_unaligned_cluster_map(e2fsck_t ctx
,
1963 blk64_t last_pblk
, e2_blkcnt_t last_lblk
,
1964 blk64_t pblk
, blk64_t lblk
)
1966 blk64_t cluster_mask
;
1968 if (!ctx
->fs
->cluster_ratio_bits
)
1970 cluster_mask
= EXT2FS_CLUSTER_MASK(ctx
->fs
);
1973 * If the block in the logical cluster doesn't align with the block in
1974 * the physical cluster...
1976 if ((lblk
& cluster_mask
) != (pblk
& cluster_mask
))
1980 * If we cross a physical cluster boundary within a logical cluster...
1982 if (last_pblk
&& (lblk
& cluster_mask
) != 0 &&
1983 EXT2FS_B2C(ctx
->fs
, lblk
) == EXT2FS_B2C(ctx
->fs
, last_lblk
) &&
1984 EXT2FS_B2C(ctx
->fs
, pblk
) != EXT2FS_B2C(ctx
->fs
, last_pblk
))
1990 static void scan_extent_node(e2fsck_t ctx
, struct problem_context
*pctx
,
1991 struct process_block_struct
*pb
,
1992 blk64_t start_block
, blk64_t end_block
,
1994 ext2_extent_handle_t ehandle
,
1997 struct ext2fs_extent extent
;
1998 blk64_t blk
, last_lblk
;
1999 e2_blkcnt_t blockcnt
;
2001 int is_dir
, is_leaf
;
2003 struct ext2_extent_info info
;
2006 pctx
->errcode
= ext2fs_extent_get_info(ehandle
, &info
);
2010 pctx
->errcode
= ext2fs_extent_get(ehandle
, EXT2_EXTENT_FIRST_SIB
,
2012 while ((pctx
->errcode
== 0 ||
2013 pctx
->errcode
== EXT2_ET_EXTENT_CSUM_INVALID
) &&
2014 info
.num_entries
-- > 0) {
2016 is_leaf
= extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
;
2017 is_dir
= LINUX_S_ISDIR(pctx
->inode
->i_mode
);
2018 last_lblk
= extent
.e_lblk
+ extent
.e_len
- 1;
2021 pctx
->blk
= extent
.e_pblk
;
2022 pctx
->blk2
= extent
.e_lblk
;
2023 pctx
->num
= extent
.e_len
;
2024 pctx
->blkcount
= extent
.e_lblk
+ extent
.e_len
;
2026 /* Ask to clear a corrupt extent block */
2028 pctx
->errcode
== EXT2_ET_EXTENT_CSUM_INVALID
) {
2029 problem
= PR_1_EXTENT_CSUM_INVALID
;
2030 if (fix_problem(ctx
, problem
, pctx
))
2031 goto fix_problem_now
;
2035 if (extent
.e_pblk
== 0 ||
2036 extent
.e_pblk
< ctx
->fs
->super
->s_first_data_block
||
2037 extent
.e_pblk
>= ext2fs_blocks_count(ctx
->fs
->super
))
2038 problem
= PR_1_EXTENT_BAD_START_BLK
;
2039 else if (extent
.e_lblk
< start_block
)
2040 problem
= PR_1_OUT_OF_ORDER_EXTENTS
;
2041 else if ((end_block
&& last_lblk
> end_block
) &&
2042 (!(extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
&&
2043 last_lblk
> eof_block
)))
2044 problem
= PR_1_EXTENT_END_OUT_OF_BOUNDS
;
2045 else if (is_leaf
&& extent
.e_len
== 0)
2046 problem
= PR_1_EXTENT_LENGTH_ZERO
;
2048 (extent
.e_pblk
+ extent
.e_len
) >
2049 ext2fs_blocks_count(ctx
->fs
->super
))
2050 problem
= PR_1_EXTENT_ENDS_BEYOND
;
2051 else if (is_leaf
&& is_dir
&&
2052 ((extent
.e_lblk
+ extent
.e_len
) >
2053 (1 << (21 - ctx
->fs
->super
->s_log_block_size
))))
2054 problem
= PR_1_TOOBIG_DIR
;
2057 * Uninitialized blocks in a directory? Clear the flag and
2058 * we'll interpret the blocks later.
2060 if (try_repairs
&& is_dir
&& problem
== 0 &&
2061 (extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
) &&
2062 fix_problem(ctx
, PR_1_UNINIT_DBLOCK
, pctx
)) {
2063 extent
.e_flags
&= ~EXT2_EXTENT_FLAGS_UNINIT
;
2064 pb
->inode_modified
= 1;
2065 pctx
->errcode
= ext2fs_extent_replace(ehandle
, 0,
2072 /* Failed csum but passes checks? Ask to fix checksum. */
2073 if (try_repairs
&& failed_csum
&& problem
== 0 &&
2074 fix_problem(ctx
, PR_1_EXTENT_ONLY_CSUM_INVALID
, pctx
)) {
2075 pb
->inode_modified
= 1;
2076 pctx
->errcode
= ext2fs_extent_replace(ehandle
,
2082 if (try_repairs
&& problem
) {
2084 if (fix_problem(ctx
, problem
, pctx
)) {
2086 if (ctx
->invalid_bitmaps
) {
2088 * If fsck knows the bitmaps are bad,
2089 * skip to the next extent and
2090 * try to clear this extent again
2091 * after fixing the bitmaps, by
2094 pctx
->errcode
= ext2fs_extent_get(
2096 EXT2_EXTENT_NEXT_SIB
,
2098 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
2099 if (pctx
->errcode
==
2100 EXT2_ET_NO_CURRENT_NODE
) {
2106 e2fsck_read_bitmaps(ctx
);
2107 pb
->inode_modified
= 1;
2109 ext2fs_extent_delete(ehandle
, 0);
2110 if (pctx
->errcode
) {
2111 pctx
->str
= "ext2fs_extent_delete";
2114 pctx
->errcode
= ext2fs_extent_fix_parents(ehandle
);
2115 if (pctx
->errcode
&&
2116 pctx
->errcode
!= EXT2_ET_NO_CURRENT_NODE
) {
2117 pctx
->str
= "ext2fs_extent_fix_parents";
2120 pctx
->errcode
= ext2fs_extent_get(ehandle
,
2121 EXT2_EXTENT_CURRENT
,
2123 if (pctx
->errcode
== EXT2_ET_NO_CURRENT_NODE
) {
2133 blk64_t lblk
= extent
.e_lblk
;
2134 int next_try_repairs
= 1;
2136 blk
= extent
.e_pblk
;
2139 * If this lower extent block collides with critical
2140 * metadata, don't try to repair the damage. Pass 1b
2141 * will reallocate the block; then we can try again.
2143 if (pb
->ino
!= EXT2_RESIZE_INO
&&
2144 ext2fs_test_block_bitmap2(ctx
->block_metadata_map
,
2146 next_try_repairs
= 0;
2149 PR_1_CRITICAL_METADATA_COLLISION
,
2151 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
2153 pctx
->errcode
= ext2fs_extent_get(ehandle
,
2154 EXT2_EXTENT_DOWN
, &extent
);
2155 if (pctx
->errcode
) {
2156 pctx
->str
= "EXT2_EXTENT_DOWN";
2157 problem
= PR_1_EXTENT_HEADER_INVALID
;
2158 if (!next_try_repairs
)
2160 if (pctx
->errcode
==
2161 EXT2_ET_EXTENT_HEADER_BAD
||
2163 EXT2_ET_EXTENT_CSUM_INVALID
)
2164 goto report_problem
;
2167 /* The next extent should match this index's logical start */
2168 if (extent
.e_lblk
!= lblk
) {
2169 struct ext2_extent_info e_info
;
2171 ext2fs_extent_get_info(ehandle
, &e_info
);
2173 pctx
->blk2
= extent
.e_lblk
;
2174 pctx
->num
= e_info
.curr_level
- 1;
2175 problem
= PR_1_EXTENT_INDEX_START_INVALID
;
2176 if (fix_problem(ctx
, problem
, pctx
)) {
2177 pb
->inode_modified
= 1;
2179 ext2fs_extent_fix_parents(ehandle
);
2180 if (pctx
->errcode
) {
2181 pctx
->str
= "ext2fs_extent_fix_parents";
2186 scan_extent_node(ctx
, pctx
, pb
, extent
.e_lblk
,
2187 last_lblk
, eof_block
, ehandle
,
2191 pctx
->errcode
= ext2fs_extent_get(ehandle
,
2192 EXT2_EXTENT_UP
, &extent
);
2193 if (pctx
->errcode
) {
2194 pctx
->str
= "EXT2_EXTENT_UP";
2197 mark_block_used(ctx
, blk
);
2202 if ((pb
->previous_block
!= 0) &&
2203 (pb
->previous_block
+1 != extent
.e_pblk
)) {
2204 if (ctx
->options
& E2F_OPT_FRAGCHECK
) {
2209 else if (pb
->is_reg
)
2212 printf(("%6lu(%c): expecting %6lu "
2214 "phys %6lu log %lu len %lu\n"),
2215 (unsigned long) pctx
->ino
, type
,
2216 (unsigned long) pb
->previous_block
+1,
2217 (unsigned long) extent
.e_pblk
,
2218 (unsigned long) extent
.e_lblk
,
2219 (unsigned long) extent
.e_len
);
2224 * If we notice a gap in the logical block mappings of an
2225 * extent-mapped directory, offer to close the hole by
2226 * moving the logical block down, otherwise we'll go mad in
2227 * pass 3 allocating empty directory blocks to fill the hole.
2229 if (try_repairs
&& is_dir
&&
2230 pb
->last_block
+ 1 < (e2_blkcnt_t
)extent
.e_lblk
) {
2233 new_lblk
= pb
->last_block
+ 1;
2234 if (EXT2FS_CLUSTER_RATIO(ctx
->fs
) > 1)
2235 new_lblk
= ((new_lblk
+
2236 EXT2FS_CLUSTER_RATIO(ctx
->fs
)) &
2237 EXT2FS_CLUSTER_MASK(ctx
->fs
)) |
2239 EXT2FS_CLUSTER_MASK(ctx
->fs
));
2240 pctx
->blk
= extent
.e_lblk
;
2241 pctx
->blk2
= new_lblk
;
2242 if (fix_problem(ctx
, PR_1_COLLAPSE_DBLOCK
, pctx
)) {
2243 extent
.e_lblk
= new_lblk
;
2244 pb
->inode_modified
= 1;
2245 pctx
->errcode
= ext2fs_extent_replace(ehandle
,
2247 if (pctx
->errcode
) {
2251 pctx
->errcode
= ext2fs_extent_fix_parents(ehandle
);
2253 goto failed_add_dir_block
;
2254 pctx
->errcode
= ext2fs_extent_goto(ehandle
,
2257 goto failed_add_dir_block
;
2258 last_lblk
= extent
.e_lblk
+ extent
.e_len
- 1;
2262 while (is_dir
&& (++pb
->last_db_block
<
2263 (e2_blkcnt_t
) extent
.e_lblk
)) {
2264 pctx
->errcode
= ext2fs_add_dir_block2(ctx
->fs
->dblist
,
2267 if (pctx
->errcode
) {
2269 pctx
->num
= pb
->last_db_block
;
2270 goto failed_add_dir_block
;
2273 if (!ctx
->fs
->cluster_ratio_bits
) {
2274 mark_blocks_used(ctx
, extent
.e_pblk
, extent
.e_len
);
2275 pb
->num_blocks
+= extent
.e_len
;
2277 for (blk
= extent
.e_pblk
, blockcnt
= extent
.e_lblk
, i
= 0;
2279 blk
++, blockcnt
++, i
++) {
2280 if (ctx
->fs
->cluster_ratio_bits
&&
2281 !(pb
->previous_block
&&
2282 (EXT2FS_B2C(ctx
->fs
, blk
) ==
2283 EXT2FS_B2C(ctx
->fs
, pb
->previous_block
)) &&
2284 (blk
& EXT2FS_CLUSTER_MASK(ctx
->fs
)) ==
2285 ((unsigned) blockcnt
& EXT2FS_CLUSTER_MASK(ctx
->fs
)))) {
2286 mark_block_used(ctx
, blk
);
2289 if (has_unaligned_cluster_map(ctx
, pb
->previous_block
,
2290 pb
->last_block
, blk
,
2292 pctx
->blk
= blockcnt
;
2294 fix_problem(ctx
, PR_1_MISALIGNED_CLUSTER
, pctx
);
2295 mark_block_used(ctx
, blk
);
2296 mark_block_used(ctx
, blk
);
2298 pb
->last_block
= blockcnt
;
2299 pb
->previous_block
= blk
;
2302 pctx
->errcode
= ext2fs_add_dir_block2(ctx
->fs
->dblist
, pctx
->ino
, blk
, blockcnt
);
2303 if (pctx
->errcode
) {
2305 pctx
->num
= blockcnt
;
2306 failed_add_dir_block
:
2307 fix_problem(ctx
, PR_1_ADD_DBLOCK
, pctx
);
2308 /* Should never get here */
2309 ctx
->flags
|= E2F_FLAG_ABORT
;
2314 if (is_dir
&& extent
.e_len
> 0)
2315 pb
->last_db_block
= blockcnt
- 1;
2316 pb
->previous_block
= extent
.e_pblk
+ extent
.e_len
- 1;
2317 start_block
= pb
->last_block
= last_lblk
;
2318 if (is_leaf
&& !is_dir
&&
2319 !(extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
))
2320 pb
->last_init_lblock
= last_lblk
;
2322 pctx
->errcode
= ext2fs_extent_get(ehandle
,
2323 EXT2_EXTENT_NEXT_SIB
,
2326 if (pctx
->errcode
== EXT2_ET_EXTENT_NO_NEXT
)
2330 static void check_blocks_extents(e2fsck_t ctx
, struct problem_context
*pctx
,
2331 struct process_block_struct
*pb
)
2333 struct ext2_extent_info info
;
2334 struct ext2_inode
*inode
= pctx
->inode
;
2335 ext2_extent_handle_t ehandle
;
2336 ext2_filsys fs
= ctx
->fs
;
2337 ext2_ino_t ino
= pctx
->ino
;
2341 pctx
->errcode
= ext2fs_extent_open2(fs
, ino
, inode
, &ehandle
);
2342 if (pctx
->errcode
) {
2343 if (fix_problem(ctx
, PR_1_READ_EXTENT
, pctx
))
2344 e2fsck_clear_inode(ctx
, ino
, inode
, 0,
2345 "check_blocks_extents");
2350 retval
= ext2fs_extent_get_info(ehandle
, &info
);
2352 if (info
.max_depth
>= MAX_EXTENT_DEPTH_COUNT
)
2353 info
.max_depth
= MAX_EXTENT_DEPTH_COUNT
-1;
2354 ctx
->extent_depth_count
[info
.max_depth
]++;
2357 eof_lblk
= ((EXT2_I_SIZE(inode
) + fs
->blocksize
- 1) >>
2358 EXT2_BLOCK_SIZE_BITS(fs
->super
)) - 1;
2359 scan_extent_node(ctx
, pctx
, pb
, 0, 0, eof_lblk
, ehandle
, 1);
2360 if (pctx
->errcode
&&
2361 fix_problem(ctx
, PR_1_EXTENT_ITERATE_FAILURE
, pctx
)) {
2363 inode
->i_blocks
= 0;
2364 e2fsck_clear_inode(ctx
, ino
, inode
, E2F_FLAG_RESTART
,
2365 "check_blocks_extents");
2368 ext2fs_extent_free(ehandle
);
2372 * In fact we don't need to check blocks for an inode with inline data
2373 * because this inode doesn't have any blocks. In this function all
2374 * we need to do is add this inode into dblist when it is a directory.
2376 static void check_blocks_inline_data(e2fsck_t ctx
, struct problem_context
*pctx
,
2377 struct process_block_struct
*pb
)
2384 pctx
->errcode
= ext2fs_add_dir_block2(ctx
->fs
->dblist
, pb
->ino
, 0, 0);
2385 if (pctx
->errcode
) {
2388 fix_problem(ctx
, PR_1_ADD_DBLOCK
, pctx
);
2389 ctx
->flags
|= E2F_FLAG_ABORT
;
2394 * This subroutine is called on each inode to account for all of the
2395 * blocks used by that inode.
2397 static void check_blocks(e2fsck_t ctx
, struct problem_context
*pctx
,
2400 ext2_filsys fs
= ctx
->fs
;
2401 struct process_block_struct pb
;
2402 ext2_ino_t ino
= pctx
->ino
;
2403 struct ext2_inode
*inode
= pctx
->inode
;
2404 unsigned bad_size
= 0;
2405 int dirty_inode
= 0;
2413 pb
.last_init_lblock
= -1;
2414 pb
.last_db_block
= -1;
2415 pb
.num_illegal_blocks
= 0;
2416 pb
.suppress
= 0; pb
.clear
= 0;
2419 pb
.previous_block
= 0;
2420 pb
.is_dir
= LINUX_S_ISDIR(inode
->i_mode
);
2421 pb
.is_reg
= LINUX_S_ISREG(inode
->i_mode
);
2422 pb
.max_blocks
= 1 << (31 - fs
->super
->s_log_block_size
);
2426 pb
.inode_modified
= 0;
2431 extent_fs
= (ctx
->fs
->super
->s_feature_incompat
&
2432 EXT3_FEATURE_INCOMPAT_EXTENTS
);
2433 inlinedata_fs
= (ctx
->fs
->super
->s_feature_incompat
&
2434 EXT4_FEATURE_INCOMPAT_INLINE_DATA
);
2436 if (inode
->i_flags
& EXT2_COMPRBLK_FL
) {
2437 if (fs
->super
->s_feature_incompat
&
2438 EXT2_FEATURE_INCOMPAT_COMPRESSION
)
2441 if (fix_problem(ctx
, PR_1_COMPR_SET
, pctx
)) {
2442 inode
->i_flags
&= ~EXT2_COMPRBLK_FL
;
2448 if (check_ext_attr(ctx
, pctx
, block_buf
)) {
2449 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
2454 if (ext2fs_inode_has_valid_blocks2(fs
, inode
)) {
2455 if (extent_fs
&& (inode
->i_flags
& EXT4_EXTENTS_FL
))
2456 check_blocks_extents(ctx
, pctx
, &pb
);
2459 * If we've modified the inode, write it out before
2460 * iterate() tries to use it.
2463 e2fsck_write_inode(ctx
, ino
, inode
,
2467 fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2468 pctx
->errcode
= ext2fs_block_iterate3(fs
, ino
,
2469 pb
.is_dir
? BLOCK_FLAG_HOLE
: 0,
2470 block_buf
, process_block
, &pb
);
2472 * We do not have uninitialized extents in non extent
2475 pb
.last_init_lblock
= pb
.last_block
;
2477 * If iterate() changed a block mapping, we have to
2478 * re-read the inode. If we decide to clear the
2479 * inode after clearing some stuff, we'll re-write the
2480 * bad mappings into the inode!
2482 if (pb
.inode_modified
)
2483 e2fsck_read_inode(ctx
, ino
, inode
,
2485 fs
->flags
&= ~EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2488 /* check inline data */
2489 if (inlinedata_fs
&& (inode
->i_flags
& EXT4_INLINE_DATA_FL
))
2490 check_blocks_inline_data(ctx
, pctx
, &pb
);
2492 end_problem_latch(ctx
, PR_LATCH_BLOCK
);
2493 end_problem_latch(ctx
, PR_LATCH_TOOBIG
);
2494 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
2497 fix_problem(ctx
, PR_1_BLOCK_ITERATE
, pctx
);
2499 if (pb
.fragmented
&& pb
.num_blocks
< fs
->super
->s_blocks_per_group
) {
2500 if (LINUX_S_ISDIR(inode
->i_mode
))
2501 ctx
->fs_fragmented_dir
++;
2503 ctx
->fs_fragmented
++;
2507 e2fsck_clear_inode(ctx
, ino
, inode
, E2F_FLAG_RESTART
,
2512 if (inode
->i_flags
& EXT2_INDEX_FL
) {
2513 if (handle_htree(ctx
, pctx
, ino
, inode
, block_buf
)) {
2514 inode
->i_flags
&= ~EXT2_INDEX_FL
;
2518 e2fsck_add_dx_dir(ctx
, ino
, pb
.last_block
+1);
2523 if (!pb
.num_blocks
&& pb
.is_dir
&&
2524 !(inode
->i_flags
& EXT4_INLINE_DATA_FL
)) {
2525 if (fix_problem(ctx
, PR_1_ZERO_LENGTH_DIR
, pctx
)) {
2526 e2fsck_clear_inode(ctx
, ino
, inode
, 0, "check_blocks");
2527 ctx
->fs_directory_count
--;
2532 if (ino
== EXT2_ROOT_INO
|| ino
>= EXT2_FIRST_INODE(ctx
->fs
->super
)) {
2533 quota_data_add(ctx
->qctx
, inode
, ino
,
2534 pb
.num_blocks
* fs
->blocksize
);
2535 quota_data_inodes(ctx
->qctx
, inode
, ino
, +1);
2538 if (!(fs
->super
->s_feature_ro_compat
&
2539 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
) ||
2540 !(inode
->i_flags
& EXT4_HUGE_FILE_FL
))
2541 pb
.num_blocks
*= (fs
->blocksize
/ 512);
2542 pb
.num_blocks
*= EXT2FS_CLUSTER_RATIO(fs
);
2544 printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
2545 ino
, inode
->i_size
, pb
.last_block
, ext2fs_inode_i_blocks(fs
, inode
),
2549 int nblock
= inode
->i_size
>> EXT2_BLOCK_SIZE_BITS(fs
->super
);
2550 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
) {
2553 if (ext2fs_inline_data_size(ctx
->fs
, pctx
->ino
, &size
))
2555 if (size
!= inode
->i_size
)
2557 } else if (inode
->i_size
& (fs
->blocksize
- 1))
2559 else if (nblock
> (pb
.last_block
+ 1))
2561 else if (nblock
< (pb
.last_block
+ 1)) {
2562 if (((pb
.last_block
+ 1) - nblock
) >
2563 fs
->super
->s_prealloc_dir_blocks
)
2567 e2_blkcnt_t blkpg
= ctx
->blocks_per_page
;
2569 size
= EXT2_I_SIZE(inode
);
2570 if ((pb
.last_init_lblock
>= 0) &&
2571 /* allow allocated blocks to end of PAGE_SIZE */
2572 (size
< (__u64
)pb
.last_init_lblock
* fs
->blocksize
) &&
2573 (pb
.last_init_lblock
/ blkpg
* blkpg
!= pb
.last_init_lblock
||
2574 size
< (__u64
)(pb
.last_init_lblock
& ~(blkpg
-1)) *
2577 else if (!(extent_fs
&& (inode
->i_flags
& EXT4_EXTENTS_FL
)) &&
2578 size
> ext2_max_sizes
[fs
->super
->s_log_block_size
])
2579 /* too big for a direct/indirect-mapped file */
2581 else if ((extent_fs
&& (inode
->i_flags
& EXT4_EXTENTS_FL
)) &&
2583 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs
->super
))) - 1))
2584 /* too big for an extent-based file - 32bit ee_block */
2587 /* i_size for symlinks is checked elsewhere */
2588 if (bad_size
&& !LINUX_S_ISLNK(inode
->i_mode
)) {
2589 pctx
->num
= (pb
.last_block
+1) * fs
->blocksize
;
2590 pctx
->group
= bad_size
;
2591 if (fix_problem(ctx
, PR_1_BAD_I_SIZE
, pctx
)) {
2592 if (LINUX_S_ISDIR(inode
->i_mode
))
2593 pctx
->num
&= 0xFFFFFFFFULL
;
2594 ext2fs_inode_size_set(fs
, inode
, pctx
->num
);
2599 if (LINUX_S_ISREG(inode
->i_mode
) &&
2600 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode
)))
2602 if ((pb
.num_blocks
!= ext2fs_inode_i_blocks(fs
, inode
)) ||
2603 ((fs
->super
->s_feature_ro_compat
&
2604 EXT4_FEATURE_RO_COMPAT_HUGE_FILE
) &&
2605 (inode
->i_flags
& EXT4_HUGE_FILE_FL
) &&
2606 (inode
->osd2
.linux2
.l_i_blocks_hi
!= 0))) {
2607 pctx
->num
= pb
.num_blocks
;
2608 if (fix_problem(ctx
, PR_1_BAD_I_BLOCKS
, pctx
)) {
2609 inode
->i_blocks
= pb
.num_blocks
;
2610 inode
->osd2
.linux2
.l_i_blocks_hi
= pb
.num_blocks
>> 32;
2616 if (ctx
->dirs_to_hash
&& pb
.is_dir
&&
2617 !(ctx
->lost_and_found
&& ctx
->lost_and_found
== ino
) &&
2618 !(inode
->i_flags
& EXT2_INDEX_FL
) &&
2619 ((inode
->i_size
/ fs
->blocksize
) >= 3))
2620 e2fsck_rehash_dir_later(ctx
, ino
);
2624 e2fsck_write_inode(ctx
, ino
, inode
, "check_blocks");
2629 * Helper function called by process block when an illegal block is
2630 * found. It returns a description about why the block is illegal
2632 static char *describe_illegal_block(ext2_filsys fs
, blk64_t block
)
2636 static char problem
[80];
2638 super
= fs
->super
->s_first_data_block
;
2639 strcpy(problem
, "PROGRAMMING ERROR: Unknown reason for illegal block");
2640 if (block
< super
) {
2641 sprintf(problem
, "< FIRSTBLOCK (%u)", super
);
2643 } else if (block
>= ext2fs_blocks_count(fs
->super
)) {
2644 sprintf(problem
, "> BLOCKS (%u)", ext2fs_blocks_count(fs
->super
));
2647 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
2648 if (block
== super
) {
2649 sprintf(problem
, "is the superblock in group %d", i
);
2652 if (block
> super
&&
2653 block
<= (super
+ fs
->desc_blocks
)) {
2654 sprintf(problem
, "is in the group descriptors "
2658 if (block
== ext2fs_block_bitmap_loc(fs
, i
)) {
2659 sprintf(problem
, "is the block bitmap of group %d", i
);
2662 if (block
== ext2fs_inode_bitmap_loc(fs
, i
)) {
2663 sprintf(problem
, "is the inode bitmap of group %d", i
);
2666 if (block
>= ext2fs_inode_table_loc(fs
, i
) &&
2667 (block
< ext2fs_inode_table_loc(fs
, i
)
2668 + fs
->inode_blocks_per_group
)) {
2669 sprintf(problem
, "is in the inode table of group %d",
2673 super
+= fs
->super
->s_blocks_per_group
;
2680 * This is a helper function for check_blocks().
2682 static int process_block(ext2_filsys fs
,
2684 e2_blkcnt_t blockcnt
,
2685 blk64_t ref_block
EXT2FS_ATTR((unused
)),
2686 int ref_offset
EXT2FS_ATTR((unused
)),
2689 struct process_block_struct
*p
;
2690 struct problem_context
*pctx
;
2691 blk64_t blk
= *block_nr
;
2693 problem_t problem
= 0;
2696 p
= (struct process_block_struct
*) priv_data
;
2700 if (p
->compressed
&& (blk
== EXT2FS_COMPRESSED_BLKADDR
)) {
2701 /* todo: Check that the comprblk_fl is high, that the
2702 blkaddr pattern looks right (all non-holes up to
2703 first EXT2FS_COMPRESSED_BLKADDR, then all
2704 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2705 that the feature_incompat bit is high, and that the
2706 inode is a regular file. If we're doing a "full
2707 check" (a concept introduced to e2fsck by e2compr,
2708 meaning that we look at data blocks as well as
2709 metadata) then call some library routine that
2710 checks the compressed data. I'll have to think
2711 about this, because one particularly important
2712 problem to be able to fix is to recalculate the
2713 cluster size if necessary. I think that perhaps
2714 we'd better do most/all e2compr-specific checks
2715 separately, after the non-e2compr checks. If not
2716 doing a full check, it may be useful to test that
2717 the personality is linux; e.g. if it isn't then
2718 perhaps this really is just an illegal block. */
2723 * For a directory, add logical block zero for processing even if it's
2724 * not mapped or we'll be perennially stuck with broken "." and ".."
2727 if (p
->is_dir
&& blockcnt
== 0 && blk
== 0) {
2728 pctx
->errcode
= ext2fs_add_dir_block2(fs
->dblist
, p
->ino
, 0, 0);
2729 if (pctx
->errcode
) {
2731 pctx
->num
= blockcnt
;
2732 goto failed_add_dir_block
;
2741 printf("Process_block, inode %lu, block %u, #%d\n", p
->ino
, blk
,
2746 * Simplistic fragmentation check. We merely require that the
2747 * file be contiguous. (Which can never be true for really
2748 * big files that are greater than a block group.)
2750 if (!HOLE_BLKADDR(p
->previous_block
) && p
->ino
!= EXT2_RESIZE_INO
) {
2751 if (p
->previous_block
+1 != blk
) {
2752 if (ctx
->options
& E2F_OPT_FRAGCHECK
) {
2760 printf(_("%6lu(%c): expecting %6lu "
2761 "got phys %6lu (blkcnt %lld)\n"),
2762 (unsigned long) pctx
->ino
, type
,
2763 (unsigned long) p
->previous_block
+1,
2764 (unsigned long) blk
,
2771 if (p
->is_dir
&& blockcnt
> (1 << (21 - fs
->super
->s_log_block_size
)))
2772 problem
= PR_1_TOOBIG_DIR
;
2773 if (p
->is_reg
&& p
->num_blocks
+1 >= p
->max_blocks
)
2774 problem
= PR_1_TOOBIG_REG
;
2775 if (!p
->is_dir
&& !p
->is_reg
&& blockcnt
> 0)
2776 problem
= PR_1_TOOBIG_SYMLINK
;
2778 if (blk
< fs
->super
->s_first_data_block
||
2779 blk
>= ext2fs_blocks_count(fs
->super
))
2780 problem
= PR_1_ILLEGAL_BLOCK_NUM
;
2783 * If this IND/DIND/TIND block is squatting atop some critical metadata
2784 * (group descriptors, superblock, bitmap, inode table), any write to
2785 * "fix" mapping problems will destroy the metadata. We'll let pass 1b
2786 * fix that and restart fsck.
2789 p
->ino
!= EXT2_RESIZE_INO
&&
2790 ext2fs_test_block_bitmap2(ctx
->block_metadata_map
, blk
)) {
2793 fix_problem(ctx
, PR_1_CRITICAL_METADATA_COLLISION
, pctx
);
2794 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
2798 p
->num_illegal_blocks
++;
2800 * A bit of subterfuge here -- we're trying to fix a block
2801 * mapping, but know that the IND/DIND/TIND block has collided
2802 * with some critical metadata. So, fix the in-core mapping so
2803 * iterate won't go insane, but return 0 instead of
2804 * BLOCK_CHANGED so that it won't write the remapping out to
2805 * our multiply linked block.
2807 if (p
->bad_ref
&& ref_block
== p
->bad_ref
) {
2811 if (!p
->suppress
&& (p
->num_illegal_blocks
% 12) == 0) {
2812 if (fix_problem(ctx
, PR_1_TOO_MANY_BAD_BLOCKS
, pctx
)) {
2816 if (fix_problem(ctx
, PR_1_SUPPRESS_MESSAGES
, pctx
)) {
2818 set_latch_flags(PR_LATCH_BLOCK
,
2823 pctx
->blkcount
= blockcnt
;
2824 if (fix_problem(ctx
, problem
, pctx
)) {
2825 blk
= *block_nr
= 0;
2826 ret_code
= BLOCK_CHANGED
;
2827 p
->inode_modified
= 1;
2829 * If the directory block is too big and is beyond the
2830 * end of the FS, don't bother trying to add it for
2831 * processing -- the kernel would never have created a
2832 * directory this large, and we risk an ENOMEM abort.
2833 * In any case, the toobig handler for extent-based
2834 * directories also doesn't feed toobig blocks to
2837 if (problem
== PR_1_TOOBIG_DIR
)
2844 if (p
->ino
== EXT2_RESIZE_INO
) {
2846 * The resize inode has already be sanity checked
2847 * during pass #0 (the superblock checks). All we
2848 * have to do is mark the double indirect block as
2849 * being in use; all of the other blocks are handled
2850 * by mark_table_blocks()).
2852 if (blockcnt
== BLOCK_COUNT_DIND
)
2853 mark_block_used(ctx
, blk
);
2855 } else if (!(ctx
->fs
->cluster_ratio_bits
&&
2856 p
->previous_block
&&
2857 (EXT2FS_B2C(ctx
->fs
, blk
) ==
2858 EXT2FS_B2C(ctx
->fs
, p
->previous_block
)) &&
2859 (blk
& EXT2FS_CLUSTER_MASK(ctx
->fs
)) ==
2860 ((unsigned) blockcnt
& EXT2FS_CLUSTER_MASK(ctx
->fs
)))) {
2861 mark_block_used(ctx
, blk
);
2863 } else if (has_unaligned_cluster_map(ctx
, p
->previous_block
,
2864 p
->last_block
, blk
, blockcnt
)) {
2865 pctx
->blk
= blockcnt
;
2867 fix_problem(ctx
, PR_1_MISALIGNED_CLUSTER
, pctx
);
2868 mark_block_used(ctx
, blk
);
2869 mark_block_used(ctx
, blk
);
2872 p
->last_block
= blockcnt
;
2873 p
->previous_block
= blk
;
2875 if (p
->is_dir
&& (blockcnt
>= 0)) {
2876 while (++p
->last_db_block
< blockcnt
) {
2877 pctx
->errcode
= ext2fs_add_dir_block2(fs
->dblist
,
2880 if (pctx
->errcode
) {
2882 pctx
->num
= p
->last_db_block
;
2883 goto failed_add_dir_block
;
2886 pctx
->errcode
= ext2fs_add_dir_block2(fs
->dblist
, p
->ino
,
2888 if (pctx
->errcode
) {
2890 pctx
->num
= blockcnt
;
2891 failed_add_dir_block
:
2892 fix_problem(ctx
, PR_1_ADD_DBLOCK
, pctx
);
2893 /* Should never get here */
2894 ctx
->flags
|= E2F_FLAG_ABORT
;
2901 static int process_bad_block(ext2_filsys fs
,
2903 e2_blkcnt_t blockcnt
,
2904 blk64_t ref_block
EXT2FS_ATTR((unused
)),
2905 int ref_offset
EXT2FS_ATTR((unused
)),
2908 struct process_block_struct
*p
;
2909 blk64_t blk
= *block_nr
;
2910 blk64_t first_block
;
2912 struct problem_context
*pctx
;
2916 * Note: This function processes blocks for the bad blocks
2917 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
2923 p
= (struct process_block_struct
*) priv_data
;
2927 pctx
->ino
= EXT2_BAD_INO
;
2929 pctx
->blkcount
= blockcnt
;
2931 if ((blk
< fs
->super
->s_first_data_block
) ||
2932 (blk
>= ext2fs_blocks_count(fs
->super
))) {
2933 if (fix_problem(ctx
, PR_1_BB_ILLEGAL_BLOCK_NUM
, pctx
)) {
2935 return BLOCK_CHANGED
;
2941 if (ext2fs_test_block_bitmap2(p
->fs_meta_blocks
, blk
)) {
2943 if (fix_problem(ctx
, PR_1_BB_FS_BLOCK
, pctx
)) {
2945 return BLOCK_CHANGED
;
2947 } else if (ext2fs_test_block_bitmap2(ctx
->block_found_map
,
2950 if (fix_problem(ctx
, PR_1_BBINODE_BAD_METABLOCK
,
2953 return BLOCK_CHANGED
;
2955 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
2958 mark_block_used(ctx
, blk
);
2962 printf ("DEBUG: Marking %u as bad.\n", blk
);
2964 ctx
->fs_badblocks_count
++;
2966 * If the block is not used, then mark it as used and return.
2967 * If it is already marked as found, this must mean that
2968 * there's an overlap between the filesystem table blocks
2969 * (bitmaps and inode table) and the bad block list.
2971 if (!ext2fs_test_block_bitmap2(ctx
->block_found_map
, blk
)) {
2972 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, blk
);
2976 * Try to find the where the filesystem block was used...
2978 first_block
= fs
->super
->s_first_data_block
;
2980 for (i
= 0; i
< fs
->group_desc_count
; i
++ ) {
2983 if (!ext2fs_bg_has_super(fs
, i
))
2985 if (blk
== first_block
) {
2987 if (fix_problem(ctx
,
2988 PR_1_BAD_PRIMARY_SUPERBLOCK
,
2991 return BLOCK_CHANGED
;
2995 fix_problem(ctx
, PR_1_BAD_SUPERBLOCK
, pctx
);
2998 if ((blk
> first_block
) &&
2999 (blk
<= first_block
+ fs
->desc_blocks
)) {
3001 pctx
->blk
= *block_nr
;
3002 if (fix_problem(ctx
,
3003 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR
, pctx
)) {
3005 return BLOCK_CHANGED
;
3009 fix_problem(ctx
, PR_1_BAD_GROUP_DESCRIPTORS
, pctx
);
3013 if (blk
== ext2fs_block_bitmap_loc(fs
, i
)) {
3014 if (fix_problem(ctx
, PR_1_BB_BAD_BLOCK
, pctx
)) {
3015 ctx
->invalid_block_bitmap_flag
[i
]++;
3016 ctx
->invalid_bitmaps
++;
3020 if (blk
== ext2fs_inode_bitmap_loc(fs
, i
)) {
3021 if (fix_problem(ctx
, PR_1_IB_BAD_BLOCK
, pctx
)) {
3022 ctx
->invalid_inode_bitmap_flag
[i
]++;
3023 ctx
->invalid_bitmaps
++;
3027 if ((blk
>= ext2fs_inode_table_loc(fs
, i
)) &&
3028 (blk
< (ext2fs_inode_table_loc(fs
, i
) +
3029 fs
->inode_blocks_per_group
))) {
3031 * If there are bad blocks in the inode table,
3032 * the inode scan code will try to do
3033 * something reasonable automatically.
3037 first_block
+= fs
->super
->s_blocks_per_group
;
3040 * If we've gotten to this point, then the only
3041 * possibility is that the bad block inode meta data
3042 * is using a bad block.
3044 if ((blk
== p
->inode
->i_block
[EXT2_IND_BLOCK
]) ||
3045 (blk
== p
->inode
->i_block
[EXT2_DIND_BLOCK
]) ||
3046 (blk
== p
->inode
->i_block
[EXT2_TIND_BLOCK
])) {
3048 if (fix_problem(ctx
, PR_1_BBINODE_BAD_METABLOCK
, pctx
)) {
3050 return BLOCK_CHANGED
;
3052 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
3059 /* Warn user that the block wasn't claimed */
3060 fix_problem(ctx
, PR_1_PROGERR_CLAIMED_BLOCK
, pctx
);
3065 static void new_table_block(e2fsck_t ctx
, blk64_t first_block
, dgrp_t group
,
3066 const char *name
, int num
, blk64_t
*new_block
)
3068 ext2_filsys fs
= ctx
->fs
;
3070 blk64_t old_block
= *new_block
;
3073 unsigned flexbg_size
;
3076 struct problem_context pctx
;
3078 clear_problem_context(&pctx
);
3081 pctx
.blk
= old_block
;
3085 * For flex_bg filesystems, first try to allocate the metadata
3086 * within the flex_bg, and if that fails then try finding the
3087 * space anywhere in the filesystem.
3089 is_flexbg
= EXT2_HAS_INCOMPAT_FEATURE(fs
->super
,
3090 EXT4_FEATURE_INCOMPAT_FLEX_BG
);
3092 flexbg_size
= 1 << fs
->super
->s_log_groups_per_flex
;
3093 flexbg
= group
/ flexbg_size
;
3094 first_block
= ext2fs_group_first_block2(fs
,
3095 flexbg_size
* flexbg
);
3096 last_grp
= group
| (flexbg_size
- 1);
3097 if (last_grp
>= fs
->group_desc_count
)
3098 last_grp
= fs
->group_desc_count
- 1;
3099 last_block
= ext2fs_group_last_block2(fs
, last_grp
);
3101 last_block
= ext2fs_group_last_block2(fs
, group
);
3102 pctx
.errcode
= ext2fs_get_free_blocks2(fs
, first_block
, last_block
,
3103 num
, ctx
->block_found_map
,
3105 if (is_flexbg
&& (pctx
.errcode
== EXT2_ET_BLOCK_ALLOC_FAIL
))
3106 pctx
.errcode
= ext2fs_get_free_blocks2(fs
,
3107 fs
->super
->s_first_data_block
,
3108 ext2fs_blocks_count(fs
->super
),
3109 num
, ctx
->block_found_map
, new_block
);
3112 fix_problem(ctx
, PR_1_RELOC_BLOCK_ALLOCATE
, &pctx
);
3113 ext2fs_unmark_valid(fs
);
3114 ctx
->flags
|= E2F_FLAG_ABORT
;
3117 pctx
.errcode
= ext2fs_get_mem(fs
->blocksize
, &buf
);
3119 fix_problem(ctx
, PR_1_RELOC_MEMORY_ALLOCATE
, &pctx
);
3120 ext2fs_unmark_valid(fs
);
3121 ctx
->flags
|= E2F_FLAG_ABORT
;
3124 ext2fs_mark_super_dirty(fs
);
3125 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
3126 pctx
.blk2
= *new_block
;
3127 fix_problem(ctx
, (old_block
? PR_1_RELOC_FROM_TO
:
3128 PR_1_RELOC_TO
), &pctx
);
3130 for (i
= 0; i
< num
; i
++) {
3132 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, (*new_block
)+i
);
3134 pctx
.errcode
= io_channel_read_blk64(fs
->io
,
3135 old_block
+ i
, 1, buf
);
3137 fix_problem(ctx
, PR_1_RELOC_READ_ERR
, &pctx
);
3139 memset(buf
, 0, fs
->blocksize
);
3141 pctx
.blk
= (*new_block
) + i
;
3142 pctx
.errcode
= io_channel_write_blk64(fs
->io
, pctx
.blk
,
3145 fix_problem(ctx
, PR_1_RELOC_WRITE_ERR
, &pctx
);
3147 ext2fs_free_mem(&buf
);
3151 * This routine gets called at the end of pass 1 if bad blocks are
3152 * detected in the superblock, group descriptors, inode_bitmaps, or
3153 * block bitmaps. At this point, all of the blocks have been mapped
3154 * out, so we can try to allocate new block(s) to replace the bad
3157 static void handle_fs_bad_blocks(e2fsck_t ctx
)
3159 ext2_filsys fs
= ctx
->fs
;
3161 blk64_t first_block
;
3164 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
3165 first_block
= ext2fs_group_first_block2(fs
, i
);
3167 if (ctx
->invalid_block_bitmap_flag
[i
]) {
3168 new_blk
= ext2fs_block_bitmap_loc(fs
, i
);
3169 new_table_block(ctx
, first_block
, i
, _("block bitmap"),
3171 ext2fs_block_bitmap_loc_set(fs
, i
, new_blk
);
3173 if (ctx
->invalid_inode_bitmap_flag
[i
]) {
3174 new_blk
= ext2fs_inode_bitmap_loc(fs
, i
);
3175 new_table_block(ctx
, first_block
, i
, _("inode bitmap"),
3177 ext2fs_inode_bitmap_loc_set(fs
, i
, new_blk
);
3179 if (ctx
->invalid_inode_table_flag
[i
]) {
3180 new_blk
= ext2fs_inode_table_loc(fs
, i
);
3181 new_table_block(ctx
, first_block
, i
, _("inode table"),
3182 fs
->inode_blocks_per_group
,
3184 ext2fs_inode_table_loc_set(fs
, i
, new_blk
);
3185 ctx
->flags
|= E2F_FLAG_RESTART
;
3188 ctx
->invalid_bitmaps
= 0;
3192 * This routine marks all blocks which are used by the superblock,
3193 * group descriptors, inode bitmaps, and block bitmaps.
3195 static void mark_table_blocks(e2fsck_t ctx
)
3197 ext2_filsys fs
= ctx
->fs
;
3201 struct problem_context pctx
;
3203 clear_problem_context(&pctx
);
3205 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
3208 ext2fs_reserve_super_and_bgd(fs
, i
, ctx
->block_found_map
);
3209 ext2fs_reserve_super_and_bgd(fs
, i
, ctx
->block_metadata_map
);
3212 * Mark the blocks used for the inode table
3214 if (ext2fs_inode_table_loc(fs
, i
)) {
3215 for (j
= 0, b
= ext2fs_inode_table_loc(fs
, i
);
3216 j
< fs
->inode_blocks_per_group
;
3218 if (ext2fs_test_block_bitmap2(ctx
->block_found_map
,
3221 if (!ctx
->invalid_inode_table_flag
[i
] &&
3223 PR_1_ITABLE_CONFLICT
, &pctx
)) {
3224 ctx
->invalid_inode_table_flag
[i
]++;
3225 ctx
->invalid_bitmaps
++;
3228 ext2fs_mark_block_bitmap2(
3229 ctx
->block_found_map
, b
);
3230 ext2fs_mark_block_bitmap2(
3231 ctx
->block_metadata_map
, b
);
3237 * Mark block used for the block bitmap
3239 if (ext2fs_block_bitmap_loc(fs
, i
)) {
3240 if (ext2fs_test_block_bitmap2(ctx
->block_found_map
,
3241 ext2fs_block_bitmap_loc(fs
, i
))) {
3242 pctx
.blk
= ext2fs_block_bitmap_loc(fs
, i
);
3243 if (fix_problem(ctx
, PR_1_BB_CONFLICT
, &pctx
)) {
3244 ctx
->invalid_block_bitmap_flag
[i
]++;
3245 ctx
->invalid_bitmaps
++;
3248 ext2fs_mark_block_bitmap2(ctx
->block_found_map
,
3249 ext2fs_block_bitmap_loc(fs
, i
));
3250 ext2fs_mark_block_bitmap2(ctx
->block_metadata_map
,
3251 ext2fs_block_bitmap_loc(fs
, i
));
3255 * Mark block used for the inode bitmap
3257 if (ext2fs_inode_bitmap_loc(fs
, i
)) {
3258 if (ext2fs_test_block_bitmap2(ctx
->block_found_map
,
3259 ext2fs_inode_bitmap_loc(fs
, i
))) {
3260 pctx
.blk
= ext2fs_inode_bitmap_loc(fs
, i
);
3261 if (fix_problem(ctx
, PR_1_IB_CONFLICT
, &pctx
)) {
3262 ctx
->invalid_inode_bitmap_flag
[i
]++;
3263 ctx
->invalid_bitmaps
++;
3266 ext2fs_mark_block_bitmap2(ctx
->block_metadata_map
,
3267 ext2fs_inode_bitmap_loc(fs
, i
));
3268 ext2fs_mark_block_bitmap2(ctx
->block_found_map
,
3269 ext2fs_inode_bitmap_loc(fs
, i
));
3276 * Thes subroutines short circuits ext2fs_get_blocks and
3277 * ext2fs_check_directory; we use them since we already have the inode
3278 * structure, so there's no point in letting the ext2fs library read
3281 static errcode_t
pass1_get_blocks(ext2_filsys fs
, ext2_ino_t ino
,
3284 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
3287 if ((ino
!= ctx
->stashed_ino
) || !ctx
->stashed_inode
)
3288 return EXT2_ET_CALLBACK_NOTHANDLED
;
3290 for (i
=0; i
< EXT2_N_BLOCKS
; i
++)
3291 blocks
[i
] = ctx
->stashed_inode
->i_block
[i
];
3295 static errcode_t
pass1_read_inode(ext2_filsys fs
, ext2_ino_t ino
,
3296 struct ext2_inode
*inode
)
3298 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
3300 if ((ino
!= ctx
->stashed_ino
) || !ctx
->stashed_inode
)
3301 return EXT2_ET_CALLBACK_NOTHANDLED
;
3302 *inode
= *ctx
->stashed_inode
;
3306 static errcode_t
pass1_write_inode(ext2_filsys fs
, ext2_ino_t ino
,
3307 struct ext2_inode
*inode
)
3309 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
3311 if ((ino
== ctx
->stashed_ino
) && ctx
->stashed_inode
&&
3312 (inode
!= ctx
->stashed_inode
))
3313 *ctx
->stashed_inode
= *inode
;
3314 return EXT2_ET_CALLBACK_NOTHANDLED
;
3317 static errcode_t
pass1_check_directory(ext2_filsys fs
, ext2_ino_t ino
)
3319 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
3321 if ((ino
!= ctx
->stashed_ino
) || !ctx
->stashed_inode
)
3322 return EXT2_ET_CALLBACK_NOTHANDLED
;
3324 if (!LINUX_S_ISDIR(ctx
->stashed_inode
->i_mode
))
3325 return EXT2_ET_NO_DIRECTORY
;
3329 static errcode_t
e2fsck_get_alloc_block(ext2_filsys fs
, blk64_t goal
,
3332 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
3336 if (ctx
->block_found_map
) {
3337 retval
= ext2fs_new_block2(fs
, goal
, ctx
->block_found_map
,
3341 if (fs
->block_map
) {
3342 ext2fs_mark_block_bitmap2(fs
->block_map
, new_block
);
3343 ext2fs_mark_bb_dirty(fs
);
3346 if (!fs
->block_map
) {
3347 retval
= ext2fs_read_block_bitmap(fs
);
3352 retval
= ext2fs_new_block2(fs
, goal
, 0, &new_block
);
3361 static void e2fsck_block_alloc_stats(ext2_filsys fs
, blk64_t blk
, int inuse
)
3363 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
3365 /* Never free a critical metadata block */
3366 if (ctx
->block_found_map
&&
3367 ctx
->block_metadata_map
&&
3369 ext2fs_test_block_bitmap2(ctx
->block_metadata_map
, blk
))
3372 if (ctx
->block_found_map
) {
3374 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, blk
);
3376 ext2fs_unmark_block_bitmap2(ctx
->block_found_map
, blk
);
3380 void e2fsck_use_inode_shortcuts(e2fsck_t ctx
, int use_shortcuts
)
3382 ext2_filsys fs
= ctx
->fs
;
3384 if (use_shortcuts
) {
3385 fs
->get_blocks
= pass1_get_blocks
;
3386 fs
->check_directory
= pass1_check_directory
;
3387 fs
->read_inode
= pass1_read_inode
;
3388 fs
->write_inode
= pass1_write_inode
;
3389 ctx
->stashed_ino
= 0;
3392 fs
->check_directory
= 0;
3394 fs
->write_inode
= 0;
3398 void e2fsck_intercept_block_allocations(e2fsck_t ctx
)
3400 ext2fs_set_alloc_block_callback(ctx
->fs
, e2fsck_get_alloc_block
, 0);
3401 ext2fs_set_block_alloc_stats_callback(ctx
->fs
,
3402 e2fsck_block_alloc_stats
, 0);