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
61 static int process_block(ext2_filsys fs
, blk64_t
*blocknr
,
62 e2_blkcnt_t blockcnt
, blk64_t ref_blk
,
63 int ref_offset
, void *priv_data
);
64 static int process_bad_block(ext2_filsys fs
, blk64_t
*block_nr
,
65 e2_blkcnt_t blockcnt
, blk64_t ref_blk
,
66 int ref_offset
, void *priv_data
);
67 static void check_blocks(e2fsck_t ctx
, struct problem_context
*pctx
,
69 static void mark_table_blocks(e2fsck_t ctx
);
70 static void alloc_bb_map(e2fsck_t ctx
);
71 static void alloc_imagic_map(e2fsck_t ctx
);
72 static void mark_inode_bad(e2fsck_t ctx
, ino_t ino
);
73 static void add_encrypted_dir(e2fsck_t ctx
, ino_t ino
);
74 static void handle_fs_bad_blocks(e2fsck_t ctx
);
75 static void process_inodes(e2fsck_t ctx
, char *block_buf
);
76 static EXT2_QSORT_TYPE
process_inode_cmp(const void *a
, const void *b
);
77 static errcode_t
scan_callback(ext2_filsys fs
, ext2_inode_scan scan
,
78 dgrp_t group
, void * priv_data
);
79 static void adjust_extattr_refcount(e2fsck_t ctx
, ext2_refcount_t refcount
,
80 char *block_buf
, int adjust_sign
);
81 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
83 struct process_block_struct
{
85 unsigned is_dir
:1, is_reg
:1, clear
:1, suppress
:1,
86 fragmented
:1, compressed
:1, bbcheck
:1,
91 e2_blkcnt_t last_init_lblock
;
92 e2_blkcnt_t last_db_block
;
93 int num_illegal_blocks
;
94 blk64_t previous_block
;
95 struct ext2_inode
*inode
;
96 struct problem_context
*pctx
;
97 ext2fs_block_bitmap fs_meta_blocks
;
100 struct extent_tree_info eti
;
103 struct process_inode_block
{
105 struct ext2_inode_large inode
;
108 struct scan_callback_struct
{
114 * For the inodes to process list.
116 static struct process_inode_block
*inodes_to_process
;
117 static int process_inode_count
;
119 static __u64 ext2_max_sizes
[EXT2_MAX_BLOCK_LOG_SIZE
-
120 EXT2_MIN_BLOCK_LOG_SIZE
+ 1];
123 * Free all memory allocated by pass1 in preparation for restarting
126 static void unwind_pass1(ext2_filsys fs
EXT2FS_ATTR((unused
)))
128 ext2fs_free_mem(&inodes_to_process
);
129 inodes_to_process
= 0;
133 * Check to make sure a device inode is real. Returns 1 if the device
134 * checks out, 0 if not.
136 * Note: this routine is now also used to check FIFO's and Sockets,
137 * since they have the same requirement; the i_block fields should be
140 int e2fsck_pass1_check_device_inode(ext2_filsys fs
EXT2FS_ATTR((unused
)),
141 struct ext2_inode
*inode
)
146 * If the index flag is set, then this is a bogus
149 if (inode
->i_flags
& EXT2_INDEX_FL
)
153 * We should be able to do the test below all the time, but
154 * because the kernel doesn't forcibly clear the device
155 * inode's additional i_block fields, there are some rare
156 * occasions when a legitimate device inode will have non-zero
157 * additional i_block fields. So for now, we only complain
158 * when the immutable flag is set, which should never happen
159 * for devices. (And that's when the problem is caused, since
160 * you can't set or clear immutable flags for devices.) Once
161 * the kernel has been fixed we can change this...
163 if (inode
->i_flags
& (EXT2_IMMUTABLE_FL
| EXT2_APPEND_FL
)) {
164 for (i
=4; i
< EXT2_N_BLOCKS
; i
++)
165 if (inode
->i_block
[i
])
172 * Check to make sure a symlink inode is real. Returns 1 if the symlink
173 * checks out, 0 if not.
175 int e2fsck_pass1_check_symlink(ext2_filsys fs
, ext2_ino_t ino
,
176 struct ext2_inode
*inode
, char *buf
)
181 ext2_extent_handle_t handle
;
182 struct ext2_extent_info info
;
183 struct ext2fs_extent extent
;
185 if ((inode
->i_size_high
|| inode
->i_size
== 0) ||
186 (inode
->i_flags
& EXT2_INDEX_FL
))
189 if (inode
->i_flags
& EXT4_EXTENTS_FL
) {
190 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
)
192 if (inode
->i_size
> fs
->blocksize
)
194 if (ext2fs_extent_open2(fs
, ino
, inode
, &handle
))
197 if (ext2fs_extent_get_info(handle
, &info
) ||
198 (info
.num_entries
!= 1) ||
199 (info
.max_depth
!= 0))
201 if (ext2fs_extent_get(handle
, EXT2_EXTENT_ROOT
, &extent
) ||
202 (extent
.e_lblk
!= 0) ||
203 (extent
.e_len
!= 1) ||
204 (extent
.e_pblk
< fs
->super
->s_first_data_block
) ||
205 (extent
.e_pblk
>= ext2fs_blocks_count(fs
->super
)))
209 ext2fs_extent_free(handle
);
213 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
) {
216 if (ext2fs_inline_data_size(fs
, ino
, &inline_size
))
218 if (inode
->i_size
!= inline_size
)
224 blocks
= ext2fs_inode_data_blocks2(fs
, inode
);
226 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
)
228 if ((inode
->i_size
>= fs
->blocksize
) ||
229 (blocks
!= fs
->blocksize
>> 9) ||
230 (inode
->i_block
[0] < fs
->super
->s_first_data_block
) ||
231 (inode
->i_block
[0] >= ext2fs_blocks_count(fs
->super
)))
234 for (i
= 1; i
< EXT2_N_BLOCKS
; i
++)
235 if (inode
->i_block
[i
])
238 if (io_channel_read_blk64(fs
->io
, inode
->i_block
[0], 1, buf
))
241 if (inode
->i_flags
& EXT4_ENCRYPT_FL
) {
242 len
= ext2fs_le32_to_cpu(*((__u32
*)buf
)) + 4;
244 len
= strnlen(buf
, fs
->blocksize
);
246 if (len
== fs
->blocksize
)
248 } else if (inode
->i_flags
& EXT4_INLINE_DATA_FL
) {
249 char *inline_buf
= NULL
;
250 size_t inline_sz
= 0;
252 if (ext2fs_inline_data_size(fs
, ino
, &inline_sz
))
254 if (inode
->i_size
!= inline_sz
)
256 if (ext2fs_get_mem(inline_sz
+ 1, &inline_buf
))
259 if (ext2fs_inline_data_get(fs
, ino
, inode
, inline_buf
, NULL
))
261 inline_buf
[inline_sz
] = 0;
262 len
= strnlen(inline_buf
, inline_sz
);
263 if (len
!= inline_sz
)
267 ext2fs_free_mem(&inline_buf
);
270 if (inode
->i_size
>= sizeof(inode
->i_block
))
273 len
= strnlen((char *)inode
->i_block
, sizeof(inode
->i_block
));
274 if (len
== sizeof(inode
->i_block
))
277 if (len
!= inode
->i_size
)
278 if ((inode
->i_flags
& EXT4_ENCRYPT_FL
) == 0)
284 * If the extents or inlinedata flags are set on the inode, offer to clear 'em.
286 #define BAD_SPECIAL_FLAGS (EXT4_EXTENTS_FL | EXT4_INLINE_DATA_FL)
287 static void check_extents_inlinedata(e2fsck_t ctx
,
288 struct problem_context
*pctx
)
290 if (!(pctx
->inode
->i_flags
& BAD_SPECIAL_FLAGS
))
293 if (!fix_problem(ctx
, PR_1_SPECIAL_EXTENTS_IDATA
, pctx
))
296 pctx
->inode
->i_flags
&= ~BAD_SPECIAL_FLAGS
;
297 e2fsck_write_inode(ctx
, pctx
->ino
, pctx
->inode
, "pass1");
299 #undef BAD_SPECIAL_FLAGS
302 * If the immutable (or append-only) flag is set on the inode, offer
305 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
306 static void check_immutable(e2fsck_t ctx
, struct problem_context
*pctx
)
308 if (!(pctx
->inode
->i_flags
& BAD_SPECIAL_FLAGS
))
311 if (!fix_problem(ctx
, PR_1_SET_IMMUTABLE
, pctx
))
314 pctx
->inode
->i_flags
&= ~BAD_SPECIAL_FLAGS
;
315 e2fsck_write_inode(ctx
, pctx
->ino
, pctx
->inode
, "pass1");
319 * If device, fifo or socket, check size is zero -- if not offer to
322 static void check_size(e2fsck_t ctx
, struct problem_context
*pctx
)
324 struct ext2_inode
*inode
= pctx
->inode
;
326 if (EXT2_I_SIZE(inode
) == 0)
329 if (!fix_problem(ctx
, PR_1_SET_NONZSIZE
, pctx
))
332 ext2fs_inode_size_set(ctx
->fs
, inode
, 0);
333 e2fsck_write_inode(ctx
, pctx
->ino
, pctx
->inode
, "pass1");
336 static void check_ea_in_inode(e2fsck_t ctx
, struct problem_context
*pctx
)
338 struct ext2_super_block
*sb
= ctx
->fs
->super
;
339 struct ext2_inode_large
*inode
;
340 struct ext2_ext_attr_entry
*entry
;
341 char *start
, *header
;
342 unsigned int storage_size
, remain
;
343 problem_t problem
= 0;
346 inode
= (struct ext2_inode_large
*) pctx
->inode
;
347 storage_size
= EXT2_INODE_SIZE(ctx
->fs
->super
) - EXT2_GOOD_OLD_INODE_SIZE
-
348 inode
->i_extra_isize
;
349 header
= ((char *) inode
) + EXT2_GOOD_OLD_INODE_SIZE
+
350 inode
->i_extra_isize
;
351 start
= header
+ sizeof(__u32
);
352 entry
= (struct ext2_ext_attr_entry
*) start
;
354 /* scan all entry's headers first */
356 /* take finish entry 0UL into account */
357 remain
= storage_size
- sizeof(__u32
);
359 region
= region_create(0, storage_size
);
361 fix_problem(ctx
, PR_1_EA_ALLOC_REGION_ABORT
, pctx
);
363 ctx
->flags
|= E2F_FLAG_ABORT
;
366 if (region_allocate(region
, 0, sizeof(__u32
))) {
367 problem
= PR_1_INODE_EA_ALLOC_COLLISION
;
371 while (remain
>= sizeof(struct ext2_ext_attr_entry
) &&
372 !EXT2_EXT_IS_LAST_ENTRY(entry
)) {
375 if (region_allocate(region
, (char *)entry
- (char *)header
,
376 EXT2_EXT_ATTR_LEN(entry
->e_name_len
))) {
377 problem
= PR_1_INODE_EA_ALLOC_COLLISION
;
381 /* header eats this space */
382 remain
-= sizeof(struct ext2_ext_attr_entry
);
384 /* is attribute name valid? */
385 if (EXT2_EXT_ATTR_SIZE(entry
->e_name_len
) > remain
) {
386 pctx
->num
= entry
->e_name_len
;
387 problem
= PR_1_ATTR_NAME_LEN
;
391 /* attribute len eats this space */
392 remain
-= EXT2_EXT_ATTR_SIZE(entry
->e_name_len
);
394 /* check value size */
395 if (entry
->e_value_size
> remain
) {
396 pctx
->num
= entry
->e_value_size
;
397 problem
= PR_1_ATTR_VALUE_SIZE
;
401 /* e_value_block must be 0 in inode's ea */
402 if (entry
->e_value_block
!= 0) {
403 pctx
->num
= entry
->e_value_block
;
404 problem
= PR_1_ATTR_VALUE_BLOCK
;
408 if (entry
->e_value_size
&&
409 region_allocate(region
, sizeof(__u32
) + entry
->e_value_offs
,
410 EXT2_EXT_ATTR_SIZE(entry
->e_value_size
))) {
411 problem
= PR_1_INODE_EA_ALLOC_COLLISION
;
415 hash
= ext2fs_ext_attr_hash_entry(entry
,
416 start
+ entry
->e_value_offs
);
418 /* e_hash may be 0 in older inode's ea */
419 if (entry
->e_hash
!= 0 && entry
->e_hash
!= hash
) {
420 pctx
->num
= entry
->e_hash
;
421 problem
= PR_1_ATTR_HASH
;
425 remain
-= entry
->e_value_size
;
427 entry
= EXT2_EXT_ATTR_NEXT(entry
);
430 if (region_allocate(region
, (char *)entry
- (char *)header
,
432 problem
= PR_1_INODE_EA_ALLOC_COLLISION
;
439 * it seems like a corruption. it's very unlikely we could repair
440 * EA(s) in automatic fashion -bzzz
442 if (problem
== 0 || !fix_problem(ctx
, problem
, pctx
))
445 /* simply remove all possible EA(s) */
446 *((__u32
*)header
) = 0UL;
447 e2fsck_write_inode_full(ctx
, pctx
->ino
, pctx
->inode
,
448 EXT2_INODE_SIZE(sb
), "pass1");
451 static int check_inode_extra_negative_epoch(__u32 xtime
, __u32 extra
) {
452 return (xtime
& (1U << 31)) != 0 &&
453 (extra
& EXT4_EPOCH_MASK
) == EXT4_EPOCH_MASK
;
456 #define CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode, xtime) \
457 check_inode_extra_negative_epoch(inode->i_##xtime, \
458 inode->i_##xtime##_extra)
460 /* When today's date is earlier than 2242, we assume that atimes,
461 * ctimes, crtimes, and mtimes with years in the range 2310..2378 are
462 * actually pre-1970 dates mis-encoded.
464 #define EXT4_EXTRA_NEGATIVE_DATE_CUTOFF 2 * (1LL << 32)
466 static void check_inode_extra_space(e2fsck_t ctx
, struct problem_context
*pctx
)
468 struct ext2_super_block
*sb
= ctx
->fs
->super
;
469 struct ext2_inode_large
*inode
;
473 inode
= (struct ext2_inode_large
*) pctx
->inode
;
474 if (EXT2_INODE_SIZE(sb
) == EXT2_GOOD_OLD_INODE_SIZE
) {
475 /* this isn't large inode. so, nothing to check */
480 printf("inode #%u, i_extra_size %d\n", pctx
->ino
,
481 inode
->i_extra_isize
);
483 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
484 min
= sizeof(inode
->i_extra_isize
) + sizeof(inode
->i_checksum_hi
);
485 max
= EXT2_INODE_SIZE(sb
) - EXT2_GOOD_OLD_INODE_SIZE
;
487 * For now we will allow i_extra_isize to be 0, but really
488 * implementations should never allow i_extra_isize to be 0
490 if (inode
->i_extra_isize
&&
491 (inode
->i_extra_isize
< min
|| inode
->i_extra_isize
> max
||
492 inode
->i_extra_isize
& 3)) {
493 if (!fix_problem(ctx
, PR_1_EXTRA_ISIZE
, pctx
))
495 if (inode
->i_extra_isize
< min
|| inode
->i_extra_isize
> max
)
496 inode
->i_extra_isize
= sb
->s_want_extra_isize
;
498 inode
->i_extra_isize
= (inode
->i_extra_isize
+ 3) & ~3;
499 e2fsck_write_inode_full(ctx
, pctx
->ino
, pctx
->inode
,
500 EXT2_INODE_SIZE(sb
), "pass1");
504 /* check if there is no place for an EA header */
505 if (inode
->i_extra_isize
>= max
- sizeof(__u32
))
508 eamagic
= (__u32
*) (((char *) inode
) + EXT2_GOOD_OLD_INODE_SIZE
+
509 inode
->i_extra_isize
);
510 if (*eamagic
== EXT2_EXT_ATTR_MAGIC
) {
511 /* it seems inode has an extended attribute(s) in body */
512 check_ea_in_inode(ctx
, pctx
);
516 * If the inode's extended atime (ctime, crtime, mtime) is stored in
517 * the old, invalid format, repair it.
519 if (((sizeof(time_t) <= 4) ||
520 (((sizeof(time_t) > 4) &&
521 ctx
->now
< EXT4_EXTRA_NEGATIVE_DATE_CUTOFF
))) &&
522 (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode
, atime
) ||
523 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode
, ctime
) ||
524 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode
, crtime
) ||
525 CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode
, mtime
))) {
527 if (!fix_problem(ctx
, PR_1_EA_TIME_OUT_OF_RANGE
, pctx
))
530 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode
, atime
))
531 inode
->i_atime_extra
&= ~EXT4_EPOCH_MASK
;
532 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode
, ctime
))
533 inode
->i_ctime_extra
&= ~EXT4_EPOCH_MASK
;
534 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode
, crtime
))
535 inode
->i_crtime_extra
&= ~EXT4_EPOCH_MASK
;
536 if (CHECK_INODE_EXTRA_NEGATIVE_EPOCH(inode
, mtime
))
537 inode
->i_mtime_extra
&= ~EXT4_EPOCH_MASK
;
538 e2fsck_write_inode_full(ctx
, pctx
->ino
, pctx
->inode
,
539 EXT2_INODE_SIZE(sb
), "pass1");
545 * Check to see if the inode might really be a directory, despite i_mode
547 * This is a lot of complexity for something for which I'm not really
548 * convinced happens frequently in the wild. If for any reason this
549 * causes any problems, take this code out.
550 * [tytso:20070331.0827EDT]
552 static void check_is_really_dir(e2fsck_t ctx
, struct problem_context
*pctx
,
555 struct ext2_inode
*inode
= pctx
->inode
;
556 struct ext2_dir_entry
*dirent
;
559 unsigned int i
, rec_len
, not_device
= 0;
564 * If the mode looks OK, we believe it. If the first block in
565 * the i_block array is 0, this cannot be a directory. If the
566 * inode is extent-mapped, it is still the case that the latter
567 * cannot be 0 - the magic number in the extent header would make
570 if (LINUX_S_ISDIR(inode
->i_mode
) || LINUX_S_ISREG(inode
->i_mode
) ||
571 LINUX_S_ISLNK(inode
->i_mode
) || inode
->i_block
[0] == 0)
575 * Check the block numbers in the i_block array for validity:
576 * zero blocks are skipped (but the first one cannot be zero -
577 * see above), other blocks are checked against the first and
578 * max data blocks (from the the superblock) and against the
579 * block bitmap. Any invalid block found means this cannot be
582 * If there are non-zero blocks past the fourth entry, then
583 * this cannot be a device file: we remember that for the next
586 * For extent mapped files, we don't do any sanity checking:
587 * just try to get the phys block of logical block 0 and run
590 * For inline data files, we just try to get the size of inline
591 * data. If it's true, we will treat it as a directory.
594 extent_fs
= ext2fs_has_feature_extents(ctx
->fs
->super
);
595 inlinedata_fs
= ext2fs_has_feature_inline_data(ctx
->fs
->super
);
596 if (inlinedata_fs
&& (inode
->i_flags
& EXT4_INLINE_DATA_FL
)) {
599 unsigned int rec_len2
;
600 struct ext2_dir_entry de
;
602 if (ext2fs_inline_data_size(ctx
->fs
, pctx
->ino
, &size
))
605 * If the size isn't a multiple of 4, it's probably not a
611 * If the first 10 bytes don't look like a directory entry,
612 * it's probably not a directory.
614 memcpy(&dotdot
, inode
->i_block
, sizeof(dotdot
));
615 memcpy(&de
, ((char *)inode
->i_block
) + EXT4_INLINE_DATA_DOTDOT_SIZE
,
616 EXT2_DIR_REC_LEN(0));
617 dotdot
= ext2fs_le32_to_cpu(dotdot
);
618 de
.inode
= ext2fs_le32_to_cpu(de
.inode
);
619 de
.rec_len
= ext2fs_le16_to_cpu(de
.rec_len
);
620 ext2fs_get_rec_len(ctx
->fs
, &de
, &rec_len2
);
621 if (dotdot
>= ctx
->fs
->super
->s_inodes_count
||
622 (dotdot
< EXT2_FIRST_INO(ctx
->fs
->super
) &&
623 dotdot
!= EXT2_ROOT_INO
) ||
624 de
.inode
>= ctx
->fs
->super
->s_inodes_count
||
625 (de
.inode
< EXT2_FIRST_INO(ctx
->fs
->super
) &&
627 rec_len2
> EXT4_MIN_INLINE_DATA_SIZE
-
628 EXT4_INLINE_DATA_DOTDOT_SIZE
)
630 /* device files never have a "system.data" entry */
632 } else if (extent_fs
&& (inode
->i_flags
& EXT4_EXTENTS_FL
)) {
634 if (ext2fs_bmap2(ctx
->fs
, pctx
->ino
, inode
, 0, 0, 0, 0,
637 /* device files are never extent mapped */
640 for (i
=0; i
< EXT2_N_BLOCKS
; i
++) {
641 blk
= inode
->i_block
[i
];
647 if (blk
< ctx
->fs
->super
->s_first_data_block
||
648 blk
>= ext2fs_blocks_count(ctx
->fs
->super
) ||
649 ext2fs_fast_test_block_bitmap2(ctx
->block_found_map
,
651 return; /* Invalid block, can't be dir */
653 blk
= inode
->i_block
[0];
657 * If the mode says this is a device file and the i_links_count field
658 * is sane and we have not ruled it out as a device file previously,
659 * we declare it a device file, not a directory.
661 if ((LINUX_S_ISCHR(inode
->i_mode
) || LINUX_S_ISBLK(inode
->i_mode
)) &&
662 (inode
->i_links_count
== 1) && !not_device
)
665 /* read the first block */
666 ehandler_operation(_("reading directory block"));
667 retval
= ext2fs_read_dir_block4(ctx
->fs
, blk
, buf
, 0, pctx
->ino
);
668 ehandler_operation(0);
672 dirent
= (struct ext2_dir_entry
*) buf
;
673 retval
= ext2fs_get_rec_len(ctx
->fs
, dirent
, &rec_len
);
676 if ((ext2fs_dirent_name_len(dirent
) != 1) ||
677 (dirent
->name
[0] != '.') ||
678 (dirent
->inode
!= pctx
->ino
) ||
681 (rec_len
>= ctx
->fs
->blocksize
- 12))
684 dirent
= (struct ext2_dir_entry
*) (buf
+ rec_len
);
685 retval
= ext2fs_get_rec_len(ctx
->fs
, dirent
, &rec_len
);
688 if ((ext2fs_dirent_name_len(dirent
) != 2) ||
689 (dirent
->name
[0] != '.') ||
690 (dirent
->name
[1] != '.') ||
696 if (fix_problem(ctx
, PR_1_TREAT_AS_DIRECTORY
, pctx
)) {
697 inode
->i_mode
= (inode
->i_mode
& 07777) | LINUX_S_IFDIR
;
698 e2fsck_write_inode_full(ctx
, pctx
->ino
, inode
,
699 EXT2_INODE_SIZE(ctx
->fs
->super
),
700 "check_is_really_dir");
704 extern errcode_t
e2fsck_setup_icount(e2fsck_t ctx
, const char *icount_name
,
705 int flags
, ext2_icount_t hint
,
708 unsigned int threshold
;
709 unsigned int save_type
;
717 profile_get_string(ctx
->profile
, "scratch_files", "directory", 0, 0,
719 profile_get_uint(ctx
->profile
, "scratch_files",
720 "numdirs_threshold", 0, 0, &threshold
);
721 profile_get_boolean(ctx
->profile
, "scratch_files",
722 "icount", 0, 1, &enable
);
724 retval
= ext2fs_get_num_dirs(ctx
->fs
, &num_dirs
);
726 num_dirs
= 1024; /* Guess */
728 if (enable
&& tdb_dir
&& !access(tdb_dir
, W_OK
) &&
729 (!threshold
|| num_dirs
> threshold
)) {
730 retval
= ext2fs_create_icount_tdb(ctx
->fs
, tdb_dir
,
735 e2fsck_set_bitmap_type(ctx
->fs
, EXT2FS_BMAP64_RBTREE
, icount_name
,
737 retval
= ext2fs_create_icount2(ctx
->fs
, flags
, 0, hint
, ret
);
738 ctx
->fs
->default_bitmap_type
= save_type
;
742 static errcode_t
recheck_bad_inode_checksum(ext2_filsys fs
, ext2_ino_t ino
,
744 struct problem_context
*pctx
)
747 struct ext2_inode_large inode
;
750 * Reread inode. If we don't see checksum error, then this inode
751 * has been fixed elsewhere.
753 ctx
->stashed_ino
= 0;
754 retval
= ext2fs_read_inode_full(fs
, ino
, (struct ext2_inode
*)&inode
,
756 if (retval
&& retval
!= EXT2_ET_INODE_CSUM_INVALID
)
762 * Checksum still doesn't match. That implies that the inode passes
763 * all the sanity checks, so maybe the checksum is simply corrupt.
764 * See if the user will go for fixing that.
766 if (!fix_problem(ctx
, PR_1_INODE_ONLY_CSUM_INVALID
, pctx
))
769 retval
= ext2fs_write_inode_full(fs
, ino
, (struct ext2_inode
*)&inode
,
774 static void reserve_block_for_root_repair(e2fsck_t ctx
)
778 ext2_filsys fs
= ctx
->fs
;
780 ctx
->root_repair_block
= 0;
781 if (ext2fs_test_inode_bitmap2(ctx
->inode_used_map
, EXT2_ROOT_INO
))
784 err
= ext2fs_new_block2(fs
, 0, ctx
->block_found_map
, &blk
);
787 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, blk
);
788 ctx
->root_repair_block
= blk
;
791 static void reserve_block_for_lnf_repair(e2fsck_t ctx
)
795 ext2_filsys fs
= ctx
->fs
;
796 static const char name
[] = "lost+found";
799 ctx
->lnf_repair_block
= 0;
800 if (!ext2fs_lookup(fs
, EXT2_ROOT_INO
, name
, sizeof(name
)-1, 0, &ino
))
803 err
= ext2fs_new_block2(fs
, 0, ctx
->block_found_map
, &blk
);
806 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, blk
);
807 ctx
->lnf_repair_block
= blk
;
810 static errcode_t
get_inline_data_ea_size(ext2_filsys fs
, ext2_ino_t ino
,
814 struct ext2_xattr_handle
*handle
;
817 retval
= ext2fs_xattrs_open(fs
, ino
, &handle
);
821 retval
= ext2fs_xattrs_read(handle
);
825 retval
= ext2fs_xattr_get(handle
, "system.data", &p
, sz
);
830 (void) ext2fs_xattrs_close(&handle
);
834 static void finish_processing_inode(e2fsck_t ctx
, ext2_ino_t ino
,
835 struct problem_context
*pctx
,
842 * If the inode failed the checksum and the user didn't
843 * clear the inode, test the checksum again -- if it still
844 * fails, ask the user if the checksum should be corrected.
846 pctx
->errcode
= recheck_bad_inode_checksum(ctx
->fs
, ino
, ctx
, pctx
);
848 ctx
->flags
|= E2F_FLAG_ABORT
;
850 #define FINISH_INODE_LOOP(ctx, ino, pctx, failed_csum) \
852 finish_processing_inode((ctx), (ino), (pctx), (failed_csum)); \
853 if ((ctx)->flags & E2F_FLAG_ABORT) \
857 static int could_be_block_map(ext2_filsys fs
, struct ext2_inode
*inode
)
862 for (i
= 0; i
< EXT2_N_BLOCKS
; i
++) {
863 x
= inode
->i_block
[i
];
864 #ifdef WORDS_BIGENDIAN
865 x
= ext2fs_swab32(x
);
867 if (x
>= ext2fs_blocks_count(fs
->super
))
875 * Figure out what to do with an inode that has both extents and inline data
876 * inode flags set. Returns -1 if we decide to erase the inode, 0 otherwise.
878 static int fix_inline_data_extents_file(e2fsck_t ctx
,
880 struct ext2_inode
*inode
,
882 struct problem_context
*pctx
)
884 size_t max_inline_ea_size
;
885 ext2_filsys fs
= ctx
->fs
;
888 /* Both feature flags not set? Just run the regular checks */
889 if (!ext2fs_has_feature_extents(fs
->super
) &&
890 !ext2fs_has_feature_inline_data(fs
->super
))
893 /* Clear both flags if it's a special file */
894 if (LINUX_S_ISCHR(inode
->i_mode
) ||
895 LINUX_S_ISBLK(inode
->i_mode
) ||
896 LINUX_S_ISFIFO(inode
->i_mode
) ||
897 LINUX_S_ISSOCK(inode
->i_mode
)) {
898 check_extents_inlinedata(ctx
, pctx
);
902 /* If it looks like an extent tree, try to clear inlinedata */
903 if (ext2fs_extent_header_verify(inode
->i_block
,
904 sizeof(inode
->i_block
)) == 0 &&
905 fix_problem(ctx
, PR_1_CLEAR_INLINE_DATA_FOR_EXTENT
, pctx
)) {
906 inode
->i_flags
&= ~EXT4_INLINE_DATA_FL
;
911 /* If it looks short enough to be inline data, try to clear extents */
912 if (inode_size
> EXT2_GOOD_OLD_INODE_SIZE
)
913 max_inline_ea_size
= inode_size
-
914 (EXT2_GOOD_OLD_INODE_SIZE
+
915 ((struct ext2_inode_large
*)inode
)->i_extra_isize
);
917 max_inline_ea_size
= 0;
918 if (EXT2_I_SIZE(inode
) <
919 EXT4_MIN_INLINE_DATA_SIZE
+ max_inline_ea_size
&&
920 fix_problem(ctx
, PR_1_CLEAR_EXTENT_FOR_INLINE_DATA
, pctx
)) {
921 inode
->i_flags
&= ~EXT4_EXTENTS_FL
;
927 * Too big for inline data, but no evidence of extent tree -
928 * maybe it's a block map file? If the mappings all look valid?
930 if (could_be_block_map(fs
, inode
) &&
931 fix_problem(ctx
, PR_1_CLEAR_EXTENT_INLINE_DATA_FLAGS
, pctx
)) {
932 #ifdef WORDS_BIGENDIAN
935 for (i
= 0; i
< EXT2_N_BLOCKS
; i
++)
936 inode
->i_block
[i
] = ext2fs_swab32(inode
->i_block
[i
]);
939 inode
->i_flags
&= ~(EXT4_EXTENTS_FL
| EXT4_INLINE_DATA_FL
);
944 /* Oh well, just clear the busted inode. */
945 if (fix_problem(ctx
, PR_1_CLEAR_EXTENT_INLINE_DATA_INODE
, pctx
)) {
946 e2fsck_clear_inode(ctx
, ino
, inode
, 0, "pass1");
952 e2fsck_write_inode(ctx
, ino
, inode
, "pass1");
957 static void pass1_readahead(e2fsck_t ctx
, dgrp_t
*group
, ext2_ino_t
*next_ino
)
959 ext2_ino_t inodes_in_group
= 0, inodes_per_block
, inodes_per_buffer
;
960 dgrp_t start
= *group
, grp
;
961 blk64_t blocks_to_read
= 0;
962 errcode_t err
= EXT2_ET_INVALID_ARGUMENT
;
964 if (ctx
->readahead_kb
== 0)
967 /* Keep iterating groups until we have enough to readahead */
968 inodes_per_block
= EXT2_INODES_PER_BLOCK(ctx
->fs
->super
);
969 for (grp
= start
; grp
< ctx
->fs
->group_desc_count
; grp
++) {
970 if (ext2fs_bg_flags_test(ctx
->fs
, grp
, EXT2_BG_INODE_UNINIT
))
972 inodes_in_group
= ctx
->fs
->super
->s_inodes_per_group
-
973 ext2fs_bg_itable_unused(ctx
->fs
, grp
);
974 blocks_to_read
+= (inodes_in_group
+ inodes_per_block
- 1) /
976 if (blocks_to_read
* ctx
->fs
->blocksize
>
977 ctx
->readahead_kb
* 1024)
981 err
= e2fsck_readahead(ctx
->fs
, E2FSCK_READA_ITABLE
, start
,
984 ctx
->readahead_kb
/= 2;
990 /* Error; disable itable readahead */
991 *group
= ctx
->fs
->group_desc_count
;
992 *next_ino
= ctx
->fs
->super
->s_inodes_count
;
995 * Don't do more readahead until we've reached the first inode
996 * of the last inode scan buffer block for the last group.
999 inodes_per_buffer
= (ctx
->inode_buffer_blocks
?
1000 ctx
->inode_buffer_blocks
:
1001 EXT2_INODE_SCAN_DEFAULT_BUFFER_BLOCKS
) *
1002 ctx
->fs
->blocksize
/
1003 EXT2_INODE_SIZE(ctx
->fs
->super
);
1005 *next_ino
= inodes_in_group
-
1006 (inodes_in_group
% inodes_per_buffer
) + 1 +
1007 (grp
* ctx
->fs
->super
->s_inodes_per_group
);
1012 * Check if the passed ino is one of the used superblock quota inodes.
1014 * Before the quota inodes were journaled, older superblock quota inodes
1015 * were just regular files in the filesystem and not reserved inodes. This
1016 * checks if the passed ino is one of the s_*_quota_inum superblock fields,
1017 * which may not always be the same as the EXT4_*_QUOTA_INO fields.
1019 static int quota_inum_is_super(struct ext2_super_block
*sb
, ext2_ino_t ino
)
1021 enum quota_type qtype
;
1023 for (qtype
= 0; qtype
< MAXQUOTAS
; qtype
++)
1024 if (*quota_sb_inump(sb
, qtype
) == ino
)
1031 * Check if the passed ino is one of the reserved quota inodes.
1032 * This checks if the inode number is one of the reserved EXT4_*_QUOTA_INO
1033 * inodes. These inodes may or may not be in use by the quota feature.
1035 static int quota_inum_is_reserved(ext2_filsys fs
, ext2_ino_t ino
)
1037 enum quota_type qtype
;
1039 for (qtype
= 0; qtype
< MAXQUOTAS
; qtype
++)
1040 if (quota_type2inum(qtype
, fs
->super
) == ino
)
1046 void e2fsck_pass1(e2fsck_t ctx
)
1050 ext2_filsys fs
= ctx
->fs
;
1052 struct ext2_inode
*inode
= NULL
;
1053 ext2_inode_scan scan
= NULL
;
1054 char *block_buf
= NULL
;
1055 #ifdef RESOURCE_TRACK
1056 struct resource_track rtrack
;
1058 unsigned char frag
, fsize
;
1059 struct problem_context pctx
;
1060 struct scan_callback_struct scan_struct
;
1061 struct ext2_super_block
*sb
= ctx
->fs
->super
;
1063 int imagic_fs
, extent_fs
, inlinedata_fs
;
1064 int low_dtime_check
= 1;
1065 int inode_size
= EXT2_INODE_SIZE(fs
->super
);
1067 int failed_csum
= 0;
1068 ext2_ino_t ino_threshold
= 0;
1069 dgrp_t ra_group
= 0;
1071 init_resource_track(&rtrack
, ctx
->fs
->io
);
1072 clear_problem_context(&pctx
);
1074 /* If we can do readahead, figure out how many groups to pull in. */
1075 if (!e2fsck_can_readahead(ctx
->fs
))
1076 ctx
->readahead_kb
= 0;
1077 else if (ctx
->readahead_kb
== ~0ULL)
1078 ctx
->readahead_kb
= e2fsck_guess_readahead(ctx
->fs
);
1079 pass1_readahead(ctx
, &ra_group
, &ino_threshold
);
1081 if (!(ctx
->options
& E2F_OPT_PREEN
))
1082 fix_problem(ctx
, PR_1_PASS_HEADER
, &pctx
);
1084 if (ext2fs_has_feature_dir_index(fs
->super
) &&
1085 !(ctx
->options
& E2F_OPT_NO
)) {
1086 if (ext2fs_u32_list_create(&ctx
->dirs_to_hash
, 50))
1087 ctx
->dirs_to_hash
= 0;
1091 mtrace_print("Pass 1");
1094 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
1096 for (i
= EXT2_MIN_BLOCK_LOG_SIZE
; i
<= EXT2_MAX_BLOCK_LOG_SIZE
; i
++) {
1097 max_sizes
= EXT2_NDIR_BLOCKS
+ EXT2_BPP(i
);
1098 max_sizes
= max_sizes
+ EXT2_BPP(i
) * EXT2_BPP(i
);
1099 max_sizes
= max_sizes
+ EXT2_BPP(i
) * EXT2_BPP(i
) * EXT2_BPP(i
);
1100 max_sizes
= (max_sizes
* (1UL << i
));
1101 ext2_max_sizes
[i
- EXT2_MIN_BLOCK_LOG_SIZE
] = max_sizes
;
1105 imagic_fs
= ext2fs_has_feature_imagic_inodes(sb
);
1106 extent_fs
= ext2fs_has_feature_extents(sb
);
1107 inlinedata_fs
= ext2fs_has_feature_inline_data(sb
);
1110 * Allocate bitmaps structures
1112 pctx
.errcode
= e2fsck_allocate_inode_bitmap(fs
, _("in-use inode map"),
1113 EXT2FS_BMAP64_RBTREE
,
1115 &ctx
->inode_used_map
);
1118 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
1119 ctx
->flags
|= E2F_FLAG_ABORT
;
1122 pctx
.errcode
= e2fsck_allocate_inode_bitmap(fs
,
1123 _("directory inode map"),
1124 EXT2FS_BMAP64_AUTODIR
,
1125 "inode_dir_map", &ctx
->inode_dir_map
);
1128 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
1129 ctx
->flags
|= E2F_FLAG_ABORT
;
1132 pctx
.errcode
= e2fsck_allocate_inode_bitmap(fs
,
1133 _("regular file inode map"), EXT2FS_BMAP64_RBTREE
,
1134 "inode_reg_map", &ctx
->inode_reg_map
);
1137 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
1138 ctx
->flags
|= E2F_FLAG_ABORT
;
1141 pctx
.errcode
= e2fsck_allocate_subcluster_bitmap(fs
,
1142 _("in-use block map"), EXT2FS_BMAP64_RBTREE
,
1143 "block_found_map", &ctx
->block_found_map
);
1146 fix_problem(ctx
, PR_1_ALLOCATE_BBITMAP_ERROR
, &pctx
);
1147 ctx
->flags
|= E2F_FLAG_ABORT
;
1150 pctx
.errcode
= e2fsck_allocate_block_bitmap(fs
,
1151 _("metadata block map"), EXT2FS_BMAP64_RBTREE
,
1152 "block_metadata_map", &ctx
->block_metadata_map
);
1155 fix_problem(ctx
, PR_1_ALLOCATE_BBITMAP_ERROR
, &pctx
);
1156 ctx
->flags
|= E2F_FLAG_ABORT
;
1159 pctx
.errcode
= e2fsck_setup_icount(ctx
, "inode_link_info", 0, NULL
,
1160 &ctx
->inode_link_info
);
1162 fix_problem(ctx
, PR_1_ALLOCATE_ICOUNT
, &pctx
);
1163 ctx
->flags
|= E2F_FLAG_ABORT
;
1166 bufsize
= inode_size
;
1167 if (bufsize
< sizeof(struct ext2_inode_large
))
1168 bufsize
= sizeof(struct ext2_inode_large
);
1169 inode
= (struct ext2_inode
*)
1170 e2fsck_allocate_memory(ctx
, bufsize
, "scratch inode");
1172 inodes_to_process
= (struct process_inode_block
*)
1173 e2fsck_allocate_memory(ctx
,
1174 (ctx
->process_inode_size
*
1175 sizeof(struct process_inode_block
)),
1176 "array of inodes to process");
1177 process_inode_count
= 0;
1179 pctx
.errcode
= ext2fs_init_dblist(fs
, 0);
1181 fix_problem(ctx
, PR_1_ALLOCATE_DBCOUNT
, &pctx
);
1182 ctx
->flags
|= E2F_FLAG_ABORT
;
1187 * If the last orphan field is set, clear it, since the pass1
1188 * processing will automatically find and clear the orphans.
1189 * In the future, we may want to try using the last_orphan
1190 * linked list ourselves, but for now, we clear it so that the
1191 * ext3 mount code won't get confused.
1193 if (!(ctx
->options
& E2F_OPT_READONLY
)) {
1194 if (fs
->super
->s_last_orphan
) {
1195 fs
->super
->s_last_orphan
= 0;
1196 ext2fs_mark_super_dirty(fs
);
1200 mark_table_blocks(ctx
);
1201 pctx
.errcode
= ext2fs_convert_subcluster_bitmap(fs
,
1202 &ctx
->block_found_map
);
1204 fix_problem(ctx
, PR_1_CONVERT_SUBCLUSTER
, &pctx
);
1205 ctx
->flags
|= E2F_FLAG_ABORT
;
1208 block_buf
= (char *) e2fsck_allocate_memory(ctx
, fs
->blocksize
* 3,
1209 "block interate buffer");
1210 if (EXT2_INODE_SIZE(fs
->super
) == EXT2_GOOD_OLD_INODE_SIZE
)
1211 e2fsck_use_inode_shortcuts(ctx
, 1);
1212 e2fsck_intercept_block_allocations(ctx
);
1213 old_op
= ehandler_operation(_("opening inode scan"));
1214 pctx
.errcode
= ext2fs_open_inode_scan(fs
, ctx
->inode_buffer_blocks
,
1216 ehandler_operation(old_op
);
1218 fix_problem(ctx
, PR_1_ISCAN_ERROR
, &pctx
);
1219 ctx
->flags
|= E2F_FLAG_ABORT
;
1222 ext2fs_inode_scan_flags(scan
, EXT2_SF_SKIP_MISSING_ITABLE
|
1223 EXT2_SF_WARN_GARBAGE_INODES
, 0);
1224 ctx
->stashed_inode
= inode
;
1225 scan_struct
.ctx
= ctx
;
1226 scan_struct
.block_buf
= block_buf
;
1227 ext2fs_set_inode_callback(scan
, scan_callback
, &scan_struct
);
1228 if (ctx
->progress
&& ((ctx
->progress
)(ctx
, 1, 0,
1229 ctx
->fs
->group_desc_count
)))
1231 if ((fs
->super
->s_wtime
< fs
->super
->s_inodes_count
) ||
1232 (fs
->super
->s_mtime
< fs
->super
->s_inodes_count
) ||
1233 (fs
->super
->s_mkfs_time
&&
1234 fs
->super
->s_mkfs_time
< fs
->super
->s_inodes_count
))
1235 low_dtime_check
= 0;
1237 if (ext2fs_has_feature_mmp(fs
->super
) &&
1238 fs
->super
->s_mmp_block
> fs
->super
->s_first_data_block
&&
1239 fs
->super
->s_mmp_block
< ext2fs_blocks_count(fs
->super
))
1240 ext2fs_mark_block_bitmap2(ctx
->block_found_map
,
1241 fs
->super
->s_mmp_block
);
1243 /* Set up ctx->lost_and_found if possible */
1244 (void) e2fsck_get_lost_and_found(ctx
, 0);
1247 if (ino
% (fs
->super
->s_inodes_per_group
* 4) == 1) {
1248 if (e2fsck_mmp_update(fs
))
1249 fatal_error(ctx
, 0);
1251 old_op
= ehandler_operation(_("getting next inode from scan"));
1252 pctx
.errcode
= ext2fs_get_next_inode_full(scan
, &ino
,
1254 if (ino
> ino_threshold
)
1255 pass1_readahead(ctx
, &ra_group
, &ino_threshold
);
1256 ehandler_operation(old_op
);
1257 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1259 if (pctx
.errcode
== EXT2_ET_BAD_BLOCK_IN_INODE_TABLE
) {
1261 * If badblocks says badblocks is bad, offer to clear
1262 * the list, update the in-core bb list, and restart
1265 if (ino
== EXT2_BAD_INO
&&
1266 fix_problem(ctx
, PR_1_BADBLOCKS_IN_BADBLOCKS
,
1270 e2fsck_clear_inode(ctx
, ino
, inode
, 0, "pass1");
1271 ext2fs_badblocks_list_free(ctx
->fs
->badblocks
);
1272 ctx
->fs
->badblocks
= NULL
;
1273 err
= ext2fs_read_bb_inode(ctx
->fs
,
1274 &ctx
->fs
->badblocks
);
1276 fix_problem(ctx
, PR_1_ISCAN_ERROR
,
1278 ctx
->flags
|= E2F_FLAG_ABORT
;
1281 err
= ext2fs_inode_scan_goto_blockgroup(scan
,
1284 fix_problem(ctx
, PR_1_ISCAN_ERROR
,
1286 ctx
->flags
|= E2F_FLAG_ABORT
;
1291 if (!ctx
->inode_bb_map
)
1293 ext2fs_mark_inode_bitmap2(ctx
->inode_bb_map
, ino
);
1294 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1298 pctx
.errcode
!= EXT2_ET_INODE_CSUM_INVALID
&&
1299 pctx
.errcode
!= EXT2_ET_INODE_IS_GARBAGE
) {
1300 fix_problem(ctx
, PR_1_ISCAN_ERROR
, &pctx
);
1301 ctx
->flags
|= E2F_FLAG_ABORT
;
1308 ctx
->stashed_ino
= ino
;
1310 /* Clear trashed inode? */
1311 if (pctx
.errcode
== EXT2_ET_INODE_IS_GARBAGE
&&
1312 inode
->i_links_count
> 0 &&
1313 fix_problem(ctx
, PR_1_INODE_IS_GARBAGE
, &pctx
)) {
1315 e2fsck_clear_inode(ctx
, ino
, inode
, 0, "pass1");
1317 failed_csum
= pctx
.errcode
!= 0;
1320 * Check for inodes who might have been part of the
1321 * orphaned list linked list. They should have gotten
1322 * dealt with by now, unless the list had somehow been
1325 * FIXME: In the future, inodes which are still in use
1326 * (and which are therefore) pending truncation should
1327 * be handled specially. Right now we just clear the
1328 * dtime field, and the normal e2fsck handling of
1329 * inodes where i_size and the inode blocks are
1330 * inconsistent is to fix i_size, instead of releasing
1331 * the extra blocks. This won't catch the inodes that
1332 * was at the end of the orphan list, but it's better
1333 * than nothing. The right answer is that there
1334 * shouldn't be any bugs in the orphan list handling. :-)
1336 if (inode
->i_dtime
&& low_dtime_check
&&
1337 inode
->i_dtime
< ctx
->fs
->super
->s_inodes_count
) {
1338 if (fix_problem(ctx
, PR_1_LOW_DTIME
, &pctx
)) {
1339 inode
->i_dtime
= inode
->i_links_count
?
1341 e2fsck_write_inode(ctx
, ino
, inode
,
1347 if (inode
->i_links_count
) {
1348 pctx
.errcode
= ext2fs_icount_store(ctx
->inode_link_info
,
1349 ino
, inode
->i_links_count
);
1351 pctx
.num
= inode
->i_links_count
;
1352 fix_problem(ctx
, PR_1_ICOUNT_STORE
, &pctx
);
1353 ctx
->flags
|= E2F_FLAG_ABORT
;
1356 } else if ((ino
>= EXT2_FIRST_INODE(fs
->super
)) &&
1357 !quota_inum_is_reserved(fs
, ino
)) {
1358 if (!inode
->i_dtime
&& inode
->i_mode
) {
1359 if (fix_problem(ctx
,
1360 PR_1_ZERO_DTIME
, &pctx
)) {
1361 inode
->i_dtime
= ctx
->now
;
1362 e2fsck_write_inode(ctx
, ino
, inode
,
1367 FINISH_INODE_LOOP(ctx
, ino
, &pctx
, failed_csum
);
1371 /* Conflicting inlinedata/extents inode flags? */
1372 if ((inode
->i_flags
& EXT4_INLINE_DATA_FL
) &&
1373 (inode
->i_flags
& EXT4_EXTENTS_FL
)) {
1374 int res
= fix_inline_data_extents_file(ctx
, ino
, inode
,
1378 /* skip FINISH_INODE_LOOP */
1383 /* Test for incorrect inline_data flags settings. */
1384 if ((inode
->i_flags
& EXT4_INLINE_DATA_FL
) && !inlinedata_fs
&&
1385 (ino
>= EXT2_FIRST_INODE(fs
->super
))) {
1388 pctx
.errcode
= ext2fs_inline_data_size(fs
, ino
, &size
);
1389 if (!pctx
.errcode
&& size
&&
1390 fix_problem(ctx
, PR_1_INLINE_DATA_FEATURE
, &pctx
)) {
1391 ext2fs_set_feature_inline_data(sb
);
1392 ext2fs_mark_super_dirty(fs
);
1394 } else if (fix_problem(ctx
, PR_1_INLINE_DATA_SET
, &pctx
)) {
1395 e2fsck_clear_inode(ctx
, ino
, inode
, 0, "pass1");
1396 /* skip FINISH_INODE_LOOP */
1401 /* Test for inline data flag but no attr */
1402 if ((inode
->i_flags
& EXT4_INLINE_DATA_FL
) && inlinedata_fs
&&
1403 (ino
>= EXT2_FIRST_INODE(fs
->super
))) {
1410 fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1411 err
= get_inline_data_ea_size(fs
, ino
, &size
);
1412 fs
->flags
= (flags
& EXT2_FLAG_IGNORE_CSUM_ERRORS
) |
1413 (fs
->flags
& ~EXT2_FLAG_IGNORE_CSUM_ERRORS
);
1417 /* Everything is awesome... */
1419 case EXT2_ET_BAD_EA_BLOCK_NUM
:
1420 case EXT2_ET_BAD_EA_HASH
:
1421 case EXT2_ET_BAD_EA_HEADER
:
1422 case EXT2_ET_EA_BAD_NAME_LEN
:
1423 case EXT2_ET_EA_BAD_VALUE_SIZE
:
1424 case EXT2_ET_EA_KEY_NOT_FOUND
:
1425 case EXT2_ET_EA_NO_SPACE
:
1426 case EXT2_ET_MISSING_EA_FEATURE
:
1427 case EXT2_ET_INLINE_DATA_CANT_ITERATE
:
1428 case EXT2_ET_INLINE_DATA_NO_BLOCK
:
1429 case EXT2_ET_INLINE_DATA_NO_SPACE
:
1430 case EXT2_ET_NO_INLINE_DATA
:
1431 case EXT2_ET_EXT_ATTR_CSUM_INVALID
:
1432 case EXT2_ET_EA_BAD_VALUE_OFFSET
:
1433 /* broken EA or no system.data EA; truncate */
1434 if (fix_problem(ctx
, PR_1_INLINE_DATA_NO_ATTR
,
1436 err
= ext2fs_inode_size_set(fs
, inode
, 0);
1439 ctx
->flags
|= E2F_FLAG_ABORT
;
1442 inode
->i_flags
&= ~EXT4_INLINE_DATA_FL
;
1443 memset(&inode
->i_block
, 0,
1444 sizeof(inode
->i_block
));
1445 e2fsck_write_inode(ctx
, ino
, inode
,
1451 /* Some other kind of non-xattr error? */
1453 ctx
->flags
|= E2F_FLAG_ABORT
;
1459 * Test for incorrect extent flag settings.
1461 * On big-endian machines we must be careful:
1462 * When the inode is read, the i_block array is not swapped
1463 * if the extent flag is set. Therefore if we are testing
1464 * for or fixing a wrongly-set flag, we must potentially
1465 * (un)swap before testing, or after fixing.
1469 * In this case the extents flag was set when read, so
1470 * extent_header_verify is ok. If the inode is cleared,
1471 * no need to swap... so no extra swapping here.
1473 if ((inode
->i_flags
& EXT4_EXTENTS_FL
) && !extent_fs
&&
1474 (inode
->i_links_count
|| (ino
== EXT2_BAD_INO
) ||
1475 (ino
== EXT2_ROOT_INO
) || (ino
== EXT2_JOURNAL_INO
))) {
1476 if ((ext2fs_extent_header_verify(inode
->i_block
,
1477 sizeof(inode
->i_block
)) == 0) &&
1478 fix_problem(ctx
, PR_1_EXTENT_FEATURE
, &pctx
)) {
1479 ext2fs_set_feature_extents(sb
);
1480 ext2fs_mark_super_dirty(fs
);
1482 } else if (fix_problem(ctx
, PR_1_EXTENTS_SET
, &pctx
)) {
1484 e2fsck_clear_inode(ctx
, ino
, inode
, 0, "pass1");
1485 if (ino
== EXT2_BAD_INO
)
1486 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
,
1488 /* skip FINISH_INODE_LOOP */
1494 * For big-endian machines:
1495 * If the inode didn't have the extents flag set when it
1496 * was read, then the i_blocks array was swapped. To test
1497 * as an extents header, we must swap it back first.
1498 * IF we then set the extents flag, the entire i_block
1499 * array must be un/re-swapped to make it proper extents data.
1501 if (extent_fs
&& !(inode
->i_flags
& EXT4_EXTENTS_FL
) &&
1502 (inode
->i_links_count
|| (ino
== EXT2_BAD_INO
) ||
1503 (ino
== EXT2_ROOT_INO
) || (ino
== EXT2_JOURNAL_INO
)) &&
1504 (LINUX_S_ISREG(inode
->i_mode
) ||
1505 LINUX_S_ISDIR(inode
->i_mode
))) {
1507 #ifdef WORDS_BIGENDIAN
1508 __u32 tmp_block
[EXT2_N_BLOCKS
];
1510 for (i
= 0; i
< EXT2_N_BLOCKS
; i
++)
1511 tmp_block
[i
] = ext2fs_swab32(inode
->i_block
[i
]);
1514 ehp
= inode
->i_block
;
1516 if ((ext2fs_extent_header_verify(ehp
,
1517 sizeof(inode
->i_block
)) == 0) &&
1518 (fix_problem(ctx
, PR_1_UNSET_EXTENT_FL
, &pctx
))) {
1519 inode
->i_flags
|= EXT4_EXTENTS_FL
;
1520 #ifdef WORDS_BIGENDIAN
1521 memcpy(inode
->i_block
, tmp_block
,
1522 sizeof(inode
->i_block
));
1524 e2fsck_write_inode(ctx
, ino
, inode
, "pass1");
1529 if (ino
== EXT2_BAD_INO
) {
1530 struct process_block_struct pb
;
1532 if ((failed_csum
|| inode
->i_mode
|| inode
->i_uid
||
1533 inode
->i_gid
|| inode
->i_links_count
||
1534 (inode
->i_flags
& EXT4_INLINE_DATA_FL
) ||
1535 inode
->i_file_acl
) &&
1536 fix_problem(ctx
, PR_1_INVALID_BAD_INODE
, &pctx
)) {
1537 memset(inode
, 0, sizeof(struct ext2_inode
));
1538 e2fsck_write_inode(ctx
, ino
, inode
,
1543 pctx
.errcode
= ext2fs_copy_bitmap(ctx
->block_found_map
,
1544 &pb
.fs_meta_blocks
);
1547 fix_problem(ctx
, PR_1_ALLOCATE_BBITMAP_ERROR
, &pctx
);
1548 ctx
->flags
|= E2F_FLAG_ABORT
;
1551 pb
.ino
= EXT2_BAD_INO
;
1552 pb
.num_blocks
= pb
.last_block
= 0;
1553 pb
.last_db_block
= -1;
1554 pb
.num_illegal_blocks
= 0;
1555 pb
.suppress
= 0; pb
.clear
= 0; pb
.is_dir
= 0;
1556 pb
.is_reg
= 0; pb
.fragmented
= 0; pb
.bbcheck
= 0;
1560 pctx
.errcode
= ext2fs_block_iterate3(fs
, ino
, 0,
1561 block_buf
, process_bad_block
, &pb
);
1562 ext2fs_free_block_bitmap(pb
.fs_meta_blocks
);
1564 fix_problem(ctx
, PR_1_BLOCK_ITERATE
, &pctx
);
1565 ctx
->flags
|= E2F_FLAG_ABORT
;
1569 if (!fix_problem(ctx
, PR_1_BBINODE_BAD_METABLOCK_PROMPT
, &pctx
)) {
1570 ctx
->flags
|= E2F_FLAG_ABORT
;
1573 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1574 clear_problem_context(&pctx
);
1575 FINISH_INODE_LOOP(ctx
, ino
, &pctx
, failed_csum
);
1577 } else if (ino
== EXT2_ROOT_INO
) {
1579 * Make sure the root inode is a directory; if
1580 * not, offer to clear it. It will be
1581 * regnerated in pass #3.
1583 if (!LINUX_S_ISDIR(inode
->i_mode
)) {
1584 if (fix_problem(ctx
, PR_1_ROOT_NO_DIR
, &pctx
))
1588 * If dtime is set, offer to clear it. mke2fs
1589 * version 0.2b created filesystems with the
1590 * dtime field set for the root and lost+found
1591 * directories. We won't worry about
1592 * /lost+found, since that can be regenerated
1593 * easily. But we will fix the root directory
1594 * as a special case.
1596 if (inode
->i_dtime
&& inode
->i_links_count
) {
1597 if (fix_problem(ctx
, PR_1_ROOT_DTIME
, &pctx
)) {
1599 e2fsck_write_inode(ctx
, ino
, inode
,
1604 } else if (ino
== EXT2_JOURNAL_INO
) {
1605 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1606 if (fs
->super
->s_journal_inum
== EXT2_JOURNAL_INO
) {
1607 if (!LINUX_S_ISREG(inode
->i_mode
) &&
1608 fix_problem(ctx
, PR_1_JOURNAL_BAD_MODE
,
1610 inode
->i_mode
= LINUX_S_IFREG
;
1611 e2fsck_write_inode(ctx
, ino
, inode
,
1615 check_blocks(ctx
, &pctx
, block_buf
);
1616 FINISH_INODE_LOOP(ctx
, ino
, &pctx
, failed_csum
);
1619 if ((inode
->i_links_count
||
1620 inode
->i_blocks
|| inode
->i_block
[0]) &&
1621 fix_problem(ctx
, PR_1_JOURNAL_INODE_NOT_CLEAR
,
1623 memset(inode
, 0, inode_size
);
1624 ext2fs_icount_store(ctx
->inode_link_info
,
1626 e2fsck_write_inode_full(ctx
, ino
, inode
,
1627 inode_size
, "pass1");
1630 } else if (quota_inum_is_reserved(fs
, ino
)) {
1631 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1632 if (ext2fs_has_feature_quota(fs
->super
) &&
1633 quota_inum_is_super(fs
->super
, ino
)) {
1634 if (!LINUX_S_ISREG(inode
->i_mode
) &&
1635 fix_problem(ctx
, PR_1_QUOTA_BAD_MODE
,
1637 inode
->i_mode
= LINUX_S_IFREG
;
1638 e2fsck_write_inode(ctx
, ino
, inode
,
1642 check_blocks(ctx
, &pctx
, block_buf
);
1643 FINISH_INODE_LOOP(ctx
, ino
, &pctx
, failed_csum
);
1646 if ((inode
->i_links_count
||
1647 inode
->i_blocks
|| inode
->i_block
[0]) &&
1648 fix_problem(ctx
, PR_1_QUOTA_INODE_NOT_CLEAR
,
1650 memset(inode
, 0, inode_size
);
1651 ext2fs_icount_store(ctx
->inode_link_info
,
1653 e2fsck_write_inode_full(ctx
, ino
, inode
,
1654 inode_size
, "pass1");
1657 } else if (ino
< EXT2_FIRST_INODE(fs
->super
)) {
1658 problem_t problem
= 0;
1660 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1661 if (ino
== EXT2_BOOT_LOADER_INO
) {
1662 if (LINUX_S_ISDIR(inode
->i_mode
))
1663 problem
= PR_1_RESERVED_BAD_MODE
;
1664 } else if (ino
== EXT2_RESIZE_INO
) {
1665 if (inode
->i_mode
&&
1666 !LINUX_S_ISREG(inode
->i_mode
))
1667 problem
= PR_1_RESERVED_BAD_MODE
;
1669 if (inode
->i_mode
!= 0)
1670 problem
= PR_1_RESERVED_BAD_MODE
;
1673 if (fix_problem(ctx
, problem
, &pctx
)) {
1675 e2fsck_write_inode(ctx
, ino
, inode
,
1680 check_blocks(ctx
, &pctx
, block_buf
);
1681 FINISH_INODE_LOOP(ctx
, ino
, &pctx
, failed_csum
);
1685 if (!inode
->i_links_count
) {
1686 FINISH_INODE_LOOP(ctx
, ino
, &pctx
, failed_csum
);
1690 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1691 * deleted files. Oops.
1693 * Since all new ext2 implementations get this right,
1694 * we now assume that the case of non-zero
1695 * i_links_count and non-zero dtime means that we
1696 * should keep the file, not delete it.
1699 if (inode
->i_dtime
) {
1700 if (fix_problem(ctx
, PR_1_SET_DTIME
, &pctx
)) {
1702 e2fsck_write_inode(ctx
, ino
, inode
, "pass1");
1707 ext2fs_mark_inode_bitmap2(ctx
->inode_used_map
, ino
);
1708 switch (fs
->super
->s_creator_os
) {
1710 frag
= inode
->osd2
.hurd2
.h_i_frag
;
1711 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
1717 if (inode
->i_faddr
|| frag
|| fsize
||
1718 (LINUX_S_ISDIR(inode
->i_mode
) && inode
->i_dir_acl
))
1719 mark_inode_bad(ctx
, ino
);
1720 if ((fs
->super
->s_creator_os
!= EXT2_OS_HURD
) &&
1721 !ext2fs_has_feature_64bit(fs
->super
) &&
1722 inode
->osd2
.linux2
.l_i_file_acl_high
!= 0)
1723 mark_inode_bad(ctx
, ino
);
1724 if ((fs
->super
->s_creator_os
!= EXT2_OS_HURD
) &&
1725 !ext2fs_has_feature_huge_file(fs
->super
) &&
1726 (inode
->osd2
.linux2
.l_i_blocks_hi
!= 0))
1727 mark_inode_bad(ctx
, ino
);
1728 if (inode
->i_flags
& EXT2_IMAGIC_FL
) {
1730 if (!ctx
->inode_imagic_map
)
1731 alloc_imagic_map(ctx
);
1732 ext2fs_mark_inode_bitmap2(ctx
->inode_imagic_map
,
1735 if (fix_problem(ctx
, PR_1_SET_IMAGIC
, &pctx
)) {
1736 inode
->i_flags
&= ~EXT2_IMAGIC_FL
;
1737 e2fsck_write_inode(ctx
, ino
,
1744 check_inode_extra_space(ctx
, &pctx
);
1745 check_is_really_dir(ctx
, &pctx
, block_buf
);
1748 * ext2fs_inode_has_valid_blocks2 does not actually look
1749 * at i_block[] values, so not endian-sensitive here.
1751 if (extent_fs
&& (inode
->i_flags
& EXT4_EXTENTS_FL
) &&
1752 LINUX_S_ISLNK(inode
->i_mode
) &&
1753 !ext2fs_inode_has_valid_blocks2(fs
, inode
) &&
1754 fix_problem(ctx
, PR_1_FAST_SYMLINK_EXTENT_FL
, &pctx
)) {
1755 inode
->i_flags
&= ~EXT4_EXTENTS_FL
;
1756 e2fsck_write_inode(ctx
, ino
, inode
, "pass1");
1760 if (LINUX_S_ISDIR(inode
->i_mode
)) {
1761 ext2fs_mark_inode_bitmap2(ctx
->inode_dir_map
, ino
);
1762 e2fsck_add_dir_info(ctx
, ino
, 0);
1763 ctx
->fs_directory_count
++;
1764 if (inode
->i_flags
& EXT4_ENCRYPT_FL
)
1765 add_encrypted_dir(ctx
, ino
);
1766 } else if (LINUX_S_ISREG (inode
->i_mode
)) {
1767 ext2fs_mark_inode_bitmap2(ctx
->inode_reg_map
, ino
);
1768 ctx
->fs_regular_count
++;
1769 } else if (LINUX_S_ISCHR (inode
->i_mode
) &&
1770 e2fsck_pass1_check_device_inode(fs
, inode
)) {
1771 check_extents_inlinedata(ctx
, &pctx
);
1772 check_immutable(ctx
, &pctx
);
1773 check_size(ctx
, &pctx
);
1774 ctx
->fs_chardev_count
++;
1775 } else if (LINUX_S_ISBLK (inode
->i_mode
) &&
1776 e2fsck_pass1_check_device_inode(fs
, inode
)) {
1777 check_extents_inlinedata(ctx
, &pctx
);
1778 check_immutable(ctx
, &pctx
);
1779 check_size(ctx
, &pctx
);
1780 ctx
->fs_blockdev_count
++;
1781 } else if (LINUX_S_ISLNK (inode
->i_mode
) &&
1782 e2fsck_pass1_check_symlink(fs
, ino
, inode
,
1784 check_immutable(ctx
, &pctx
);
1785 ctx
->fs_symlinks_count
++;
1786 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
) {
1787 FINISH_INODE_LOOP(ctx
, ino
, &pctx
, failed_csum
);
1789 } else if (ext2fs_inode_data_blocks(fs
, inode
) == 0) {
1790 ctx
->fs_fast_symlinks_count
++;
1791 check_blocks(ctx
, &pctx
, block_buf
);
1792 FINISH_INODE_LOOP(ctx
, ino
, &pctx
, failed_csum
);
1796 else if (LINUX_S_ISFIFO (inode
->i_mode
) &&
1797 e2fsck_pass1_check_device_inode(fs
, inode
)) {
1798 check_extents_inlinedata(ctx
, &pctx
);
1799 check_immutable(ctx
, &pctx
);
1800 check_size(ctx
, &pctx
);
1801 ctx
->fs_fifo_count
++;
1802 } else if ((LINUX_S_ISSOCK (inode
->i_mode
)) &&
1803 e2fsck_pass1_check_device_inode(fs
, inode
)) {
1804 check_extents_inlinedata(ctx
, &pctx
);
1805 check_immutable(ctx
, &pctx
);
1806 check_size(ctx
, &pctx
);
1807 ctx
->fs_sockets_count
++;
1809 mark_inode_bad(ctx
, ino
);
1810 if (!(inode
->i_flags
& EXT4_EXTENTS_FL
) &&
1811 !(inode
->i_flags
& EXT4_INLINE_DATA_FL
)) {
1812 if (inode
->i_block
[EXT2_IND_BLOCK
])
1813 ctx
->fs_ind_count
++;
1814 if (inode
->i_block
[EXT2_DIND_BLOCK
])
1815 ctx
->fs_dind_count
++;
1816 if (inode
->i_block
[EXT2_TIND_BLOCK
])
1817 ctx
->fs_tind_count
++;
1819 if (!(inode
->i_flags
& EXT4_EXTENTS_FL
) &&
1820 !(inode
->i_flags
& EXT4_INLINE_DATA_FL
) &&
1821 (inode
->i_block
[EXT2_IND_BLOCK
] ||
1822 inode
->i_block
[EXT2_DIND_BLOCK
] ||
1823 inode
->i_block
[EXT2_TIND_BLOCK
] ||
1824 ext2fs_file_acl_block(fs
, inode
))) {
1825 struct ext2_inode_large
*ip
;
1827 inodes_to_process
[process_inode_count
].ino
= ino
;
1828 ip
= &inodes_to_process
[process_inode_count
].inode
;
1829 if (inode_size
< sizeof(struct ext2_inode_large
))
1830 memcpy(ip
, inode
, inode_size
);
1832 memcpy(ip
, inode
, sizeof(*ip
));
1833 process_inode_count
++;
1835 check_blocks(ctx
, &pctx
, block_buf
);
1837 FINISH_INODE_LOOP(ctx
, ino
, &pctx
, failed_csum
);
1839 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1842 if (process_inode_count
>= ctx
->process_inode_size
) {
1843 process_inodes(ctx
, block_buf
);
1845 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
1849 process_inodes(ctx
, block_buf
);
1850 ext2fs_close_inode_scan(scan
);
1853 reserve_block_for_root_repair(ctx
);
1854 reserve_block_for_lnf_repair(ctx
);
1857 * If any extended attribute blocks' reference counts need to
1858 * be adjusted, either up (ctx->refcount_extra), or down
1859 * (ctx->refcount), then fix them.
1861 if (ctx
->refcount
) {
1862 adjust_extattr_refcount(ctx
, ctx
->refcount
, block_buf
, -1);
1863 ea_refcount_free(ctx
->refcount
);
1866 if (ctx
->refcount_extra
) {
1867 adjust_extattr_refcount(ctx
, ctx
->refcount_extra
,
1869 ea_refcount_free(ctx
->refcount_extra
);
1870 ctx
->refcount_extra
= 0;
1873 if (ctx
->invalid_bitmaps
)
1874 handle_fs_bad_blocks(ctx
);
1876 /* We don't need the block_ea_map any more */
1877 if (ctx
->block_ea_map
) {
1878 ext2fs_free_block_bitmap(ctx
->block_ea_map
);
1879 ctx
->block_ea_map
= 0;
1882 if (ctx
->flags
& E2F_FLAG_RESIZE_INODE
) {
1883 clear_problem_context(&pctx
);
1884 pctx
.errcode
= ext2fs_create_resize_inode(fs
);
1886 if (!fix_problem(ctx
, PR_1_RESIZE_INODE_CREATE
,
1888 ctx
->flags
|= E2F_FLAG_ABORT
;
1893 if (!pctx
.errcode
) {
1894 e2fsck_read_inode(ctx
, EXT2_RESIZE_INO
, inode
,
1896 inode
->i_mtime
= ctx
->now
;
1897 e2fsck_write_inode(ctx
, EXT2_RESIZE_INO
, inode
,
1900 ctx
->flags
&= ~E2F_FLAG_RESIZE_INODE
;
1903 if (ctx
->flags
& E2F_FLAG_RESTART
) {
1905 * Only the master copy of the superblock and block
1906 * group descriptors are going to be written during a
1907 * restart, so set the superblock to be used to be the
1908 * master superblock.
1910 ctx
->use_superblock
= 0;
1915 if (ctx
->block_dup_map
) {
1916 if (ctx
->options
& E2F_OPT_PREEN
) {
1917 clear_problem_context(&pctx
);
1918 fix_problem(ctx
, PR_1_DUP_BLOCKS_PREENSTOP
, &pctx
);
1920 e2fsck_pass1_dupblocks(ctx
, block_buf
);
1922 ctx
->flags
|= E2F_FLAG_ALLOC_OK
;
1923 ext2fs_free_mem(&inodes_to_process
);
1925 e2fsck_use_inode_shortcuts(ctx
, 0);
1928 ext2fs_close_inode_scan(scan
);
1930 ext2fs_free_mem(&block_buf
);
1932 ext2fs_free_mem(&inode
);
1935 * The l+f inode may have been cleared, so zap it now and
1936 * later passes will recalculate it if necessary
1938 ctx
->lost_and_found
= 0;
1940 if ((ctx
->flags
& E2F_FLAG_SIGNAL_MASK
) == 0)
1941 print_resource_track(ctx
, _("Pass 1"), &rtrack
, ctx
->fs
->io
);
1943 ctx
->invalid_bitmaps
++;
1945 #undef FINISH_INODE_LOOP
1948 * When the inode_scan routines call this callback at the end of the
1949 * glock group, call process_inodes.
1951 static errcode_t
scan_callback(ext2_filsys fs
,
1952 ext2_inode_scan scan
EXT2FS_ATTR((unused
)),
1953 dgrp_t group
, void * priv_data
)
1955 struct scan_callback_struct
*scan_struct
;
1958 scan_struct
= (struct scan_callback_struct
*) priv_data
;
1959 ctx
= scan_struct
->ctx
;
1961 process_inodes((e2fsck_t
) fs
->priv_data
, scan_struct
->block_buf
);
1964 if ((ctx
->progress
)(ctx
, 1, group
+1,
1965 ctx
->fs
->group_desc_count
))
1966 return EXT2_ET_CANCEL_REQUESTED
;
1972 * Process the inodes in the "inodes to process" list.
1974 static void process_inodes(e2fsck_t ctx
, char *block_buf
)
1977 struct ext2_inode
*old_stashed_inode
;
1978 ext2_ino_t old_stashed_ino
;
1979 const char *old_operation
;
1981 struct problem_context pctx
;
1984 printf("begin process_inodes: ");
1986 if (process_inode_count
== 0)
1988 old_operation
= ehandler_operation(0);
1989 old_stashed_inode
= ctx
->stashed_inode
;
1990 old_stashed_ino
= ctx
->stashed_ino
;
1991 qsort(inodes_to_process
, process_inode_count
,
1992 sizeof(struct process_inode_block
), process_inode_cmp
);
1993 clear_problem_context(&pctx
);
1994 for (i
=0; i
< process_inode_count
; i
++) {
1995 pctx
.inode
= ctx
->stashed_inode
=
1996 (struct ext2_inode
*) &inodes_to_process
[i
].inode
;
1997 pctx
.ino
= ctx
->stashed_ino
= inodes_to_process
[i
].ino
;
2000 printf("%u ", pctx
.ino
);
2002 sprintf(buf
, _("reading indirect blocks of inode %u"),
2004 ehandler_operation(buf
);
2005 check_blocks(ctx
, &pctx
, block_buf
);
2006 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
2009 ctx
->stashed_inode
= old_stashed_inode
;
2010 ctx
->stashed_ino
= old_stashed_ino
;
2011 process_inode_count
= 0;
2013 printf("end process inodes\n");
2015 ehandler_operation(old_operation
);
2018 static EXT2_QSORT_TYPE
process_inode_cmp(const void *a
, const void *b
)
2020 const struct process_inode_block
*ib_a
=
2021 (const struct process_inode_block
*) a
;
2022 const struct process_inode_block
*ib_b
=
2023 (const struct process_inode_block
*) b
;
2026 ret
= (ib_a
->inode
.i_block
[EXT2_IND_BLOCK
] -
2027 ib_b
->inode
.i_block
[EXT2_IND_BLOCK
]);
2030 * We only call process_inodes() for non-extent
2031 * inodes, so it's OK to pass NULL to
2032 * ext2fs_file_acl_block() here.
2034 ret
= ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_a
->inode
)) -
2035 ext2fs_file_acl_block(0, ext2fs_const_inode(&ib_b
->inode
));
2037 ret
= ib_a
->ino
- ib_b
->ino
;
2042 * Mark an inode as being bad in some what
2044 static void mark_inode_bad(e2fsck_t ctx
, ino_t ino
)
2046 struct problem_context pctx
;
2048 if (!ctx
->inode_bad_map
) {
2049 clear_problem_context(&pctx
);
2051 pctx
.errcode
= e2fsck_allocate_inode_bitmap(ctx
->fs
,
2052 _("bad inode map"), EXT2FS_BMAP64_RBTREE
,
2053 "inode_bad_map", &ctx
->inode_bad_map
);
2056 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
2057 /* Should never get here */
2058 ctx
->flags
|= E2F_FLAG_ABORT
;
2062 ext2fs_mark_inode_bitmap2(ctx
->inode_bad_map
, ino
);
2065 static void add_encrypted_dir(e2fsck_t ctx
, ino_t ino
)
2067 struct problem_context pctx
;
2069 if (!ctx
->encrypted_dirs
) {
2070 pctx
.errcode
= ext2fs_u32_list_create(&ctx
->encrypted_dirs
, 0);
2074 pctx
.errcode
= ext2fs_u32_list_add(ctx
->encrypted_dirs
, ino
);
2075 if (pctx
.errcode
== 0)
2078 fix_problem(ctx
, PR_1_ALLOCATE_ENCRYPTED_DIRLIST
, &pctx
);
2079 /* Should never get here */
2080 ctx
->flags
|= E2F_FLAG_ABORT
;
2084 * This procedure will allocate the inode "bb" (badblock) map table
2086 static void alloc_bb_map(e2fsck_t ctx
)
2088 struct problem_context pctx
;
2090 clear_problem_context(&pctx
);
2091 pctx
.errcode
= e2fsck_allocate_inode_bitmap(ctx
->fs
,
2092 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE
,
2093 "inode_bb_map", &ctx
->inode_bb_map
);
2096 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
2097 /* Should never get here */
2098 ctx
->flags
|= E2F_FLAG_ABORT
;
2104 * This procedure will allocate the inode imagic table
2106 static void alloc_imagic_map(e2fsck_t ctx
)
2108 struct problem_context pctx
;
2110 clear_problem_context(&pctx
);
2111 pctx
.errcode
= e2fsck_allocate_inode_bitmap(ctx
->fs
,
2112 _("imagic inode map"), EXT2FS_BMAP64_RBTREE
,
2113 "inode_imagic_map", &ctx
->inode_imagic_map
);
2116 fix_problem(ctx
, PR_1_ALLOCATE_IBITMAP_ERROR
, &pctx
);
2117 /* Should never get here */
2118 ctx
->flags
|= E2F_FLAG_ABORT
;
2124 * Marks a block as in use, setting the dup_map if it's been set
2125 * already. Called by process_block and process_bad_block.
2127 * WARNING: Assumes checks have already been done to make sure block
2128 * is valid. This is true in both process_block and process_bad_block.
2130 static _INLINE_
void mark_block_used(e2fsck_t ctx
, blk64_t block
)
2132 struct problem_context pctx
;
2134 clear_problem_context(&pctx
);
2136 if (ext2fs_fast_test_block_bitmap2(ctx
->block_found_map
, block
)) {
2137 if (!ctx
->block_dup_map
) {
2138 pctx
.errcode
= e2fsck_allocate_block_bitmap(ctx
->fs
,
2139 _("multiply claimed block map"),
2140 EXT2FS_BMAP64_RBTREE
, "block_dup_map",
2141 &ctx
->block_dup_map
);
2144 fix_problem(ctx
, PR_1_ALLOCATE_BBITMAP_ERROR
,
2146 /* Should never get here */
2147 ctx
->flags
|= E2F_FLAG_ABORT
;
2151 ext2fs_fast_mark_block_bitmap2(ctx
->block_dup_map
, block
);
2153 ext2fs_fast_mark_block_bitmap2(ctx
->block_found_map
, block
);
2157 static _INLINE_
void mark_blocks_used(e2fsck_t ctx
, blk64_t block
,
2160 if (ext2fs_test_block_bitmap_range2(ctx
->block_found_map
, block
, num
))
2161 ext2fs_mark_block_bitmap_range2(ctx
->block_found_map
, block
, num
);
2164 mark_block_used(ctx
, block
++);
2168 * Adjust the extended attribute block's reference counts at the end
2169 * of pass 1, either by subtracting out references for EA blocks that
2170 * are still referenced in ctx->refcount, or by adding references for
2171 * EA blocks that had extra references as accounted for in
2172 * ctx->refcount_extra.
2174 static void adjust_extattr_refcount(e2fsck_t ctx
, ext2_refcount_t refcount
,
2175 char *block_buf
, int adjust_sign
)
2177 struct ext2_ext_attr_header
*header
;
2178 struct problem_context pctx
;
2179 ext2_filsys fs
= ctx
->fs
;
2184 clear_problem_context(&pctx
);
2186 ea_refcount_intr_begin(refcount
);
2188 if ((blk
= ea_refcount_intr_next(refcount
, &count
)) == 0)
2191 pctx
.errcode
= ext2fs_read_ext_attr3(fs
, blk
, block_buf
,
2194 fix_problem(ctx
, PR_1_EXTATTR_READ_ABORT
, &pctx
);
2197 header
= (struct ext2_ext_attr_header
*) block_buf
;
2198 pctx
.blkcount
= header
->h_refcount
;
2199 should_be
= header
->h_refcount
+ adjust_sign
* count
;
2200 pctx
.num
= should_be
;
2201 if (fix_problem(ctx
, PR_1_EXTATTR_REFCOUNT
, &pctx
)) {
2202 header
->h_refcount
= should_be
;
2203 pctx
.errcode
= ext2fs_write_ext_attr3(fs
, blk
,
2207 fix_problem(ctx
, PR_1_EXTATTR_WRITE_ABORT
,
2216 * Handle processing the extended attribute blocks
2218 static int check_ext_attr(e2fsck_t ctx
, struct problem_context
*pctx
,
2221 ext2_filsys fs
= ctx
->fs
;
2222 ext2_ino_t ino
= pctx
->ino
;
2223 struct ext2_inode
*inode
= pctx
->inode
;
2226 struct ext2_ext_attr_header
*header
;
2227 struct ext2_ext_attr_entry
*entry
;
2229 region_t region
= 0;
2230 int failed_csum
= 0;
2232 blk
= ext2fs_file_acl_block(fs
, inode
);
2237 * If the Extended attribute flag isn't set, then a non-zero
2238 * file acl means that the inode is corrupted.
2240 * Or if the extended attribute block is an invalid block,
2241 * then the inode is also corrupted.
2243 if (!ext2fs_has_feature_xattr(fs
->super
) ||
2244 (blk
< fs
->super
->s_first_data_block
) ||
2245 (blk
>= ext2fs_blocks_count(fs
->super
))) {
2246 mark_inode_bad(ctx
, ino
);
2250 /* If ea bitmap hasn't been allocated, create it */
2251 if (!ctx
->block_ea_map
) {
2252 pctx
->errcode
= e2fsck_allocate_block_bitmap(fs
,
2253 _("ext attr block map"),
2254 EXT2FS_BMAP64_RBTREE
, "block_ea_map",
2255 &ctx
->block_ea_map
);
2256 if (pctx
->errcode
) {
2258 fix_problem(ctx
, PR_1_ALLOCATE_BBITMAP_ERROR
, pctx
);
2259 ctx
->flags
|= E2F_FLAG_ABORT
;
2264 /* Create the EA refcount structure if necessary */
2265 if (!ctx
->refcount
) {
2266 pctx
->errcode
= ea_refcount_create(0, &ctx
->refcount
);
2267 if (pctx
->errcode
) {
2269 fix_problem(ctx
, PR_1_ALLOCATE_REFCOUNT
, pctx
);
2270 ctx
->flags
|= E2F_FLAG_ABORT
;
2276 /* Debugging text */
2277 printf("Inode %u has EA block %u\n", ino
, blk
);
2280 /* Have we seen this EA block before? */
2281 if (ext2fs_fast_test_block_bitmap2(ctx
->block_ea_map
, blk
)) {
2282 if (ea_refcount_decrement(ctx
->refcount
, blk
, 0) == 0)
2284 /* Ooops, this EA was referenced more than it stated */
2285 if (!ctx
->refcount_extra
) {
2286 pctx
->errcode
= ea_refcount_create(0,
2287 &ctx
->refcount_extra
);
2288 if (pctx
->errcode
) {
2290 fix_problem(ctx
, PR_1_ALLOCATE_REFCOUNT
, pctx
);
2291 ctx
->flags
|= E2F_FLAG_ABORT
;
2295 ea_refcount_increment(ctx
->refcount_extra
, blk
, 0);
2300 * OK, we haven't seen this EA block yet. So we need to
2304 pctx
->errcode
= ext2fs_read_ext_attr3(fs
, blk
, block_buf
, pctx
->ino
);
2305 if (pctx
->errcode
== EXT2_ET_EXT_ATTR_CSUM_INVALID
) {
2308 } else if (pctx
->errcode
== EXT2_ET_BAD_EA_HEADER
)
2311 if (pctx
->errcode
&&
2312 fix_problem(ctx
, PR_1_READ_EA_BLOCK
, pctx
)) {
2316 header
= (struct ext2_ext_attr_header
*) block_buf
;
2317 pctx
->blk
= ext2fs_file_acl_block(fs
, inode
);
2318 if (((ctx
->ext_attr_ver
== 1) &&
2319 (header
->h_magic
!= EXT2_EXT_ATTR_MAGIC_v1
)) ||
2320 ((ctx
->ext_attr_ver
== 2) &&
2321 (header
->h_magic
!= EXT2_EXT_ATTR_MAGIC
))) {
2322 if (fix_problem(ctx
, PR_1_BAD_EA_BLOCK
, pctx
))
2326 if (header
->h_blocks
!= 1) {
2327 if (fix_problem(ctx
, PR_1_EA_MULTI_BLOCK
, pctx
))
2331 if (pctx
->errcode
&& fix_problem(ctx
, PR_1_READ_EA_BLOCK
, pctx
))
2334 region
= region_create(0, fs
->blocksize
);
2336 fix_problem(ctx
, PR_1_EA_ALLOC_REGION_ABORT
, pctx
);
2337 ctx
->flags
|= E2F_FLAG_ABORT
;
2340 if (region_allocate(region
, 0, sizeof(struct ext2_ext_attr_header
))) {
2341 if (fix_problem(ctx
, PR_1_EA_ALLOC_COLLISION
, pctx
))
2345 entry
= (struct ext2_ext_attr_entry
*)(header
+1);
2346 end
= block_buf
+ fs
->blocksize
;
2347 while ((char *)entry
< end
&& *(__u32
*)entry
) {
2350 if (region_allocate(region
, (char *)entry
- (char *)header
,
2351 EXT2_EXT_ATTR_LEN(entry
->e_name_len
))) {
2352 if (fix_problem(ctx
, PR_1_EA_ALLOC_COLLISION
, pctx
))
2356 if ((ctx
->ext_attr_ver
== 1 &&
2357 (entry
->e_name_len
== 0 || entry
->e_name_index
!= 0)) ||
2358 (ctx
->ext_attr_ver
== 2 &&
2359 entry
->e_name_index
== 0)) {
2360 if (fix_problem(ctx
, PR_1_EA_BAD_NAME
, pctx
))
2364 if (entry
->e_value_block
!= 0) {
2365 if (fix_problem(ctx
, PR_1_EA_BAD_VALUE
, pctx
))
2368 if (entry
->e_value_offs
+ entry
->e_value_size
> fs
->blocksize
) {
2369 if (fix_problem(ctx
, PR_1_EA_BAD_VALUE
, pctx
))
2373 if (entry
->e_value_size
&&
2374 region_allocate(region
, entry
->e_value_offs
,
2375 EXT2_EXT_ATTR_SIZE(entry
->e_value_size
))) {
2376 if (fix_problem(ctx
, PR_1_EA_ALLOC_COLLISION
, pctx
))
2380 hash
= ext2fs_ext_attr_hash_entry(entry
, block_buf
+
2381 entry
->e_value_offs
);
2383 if (entry
->e_hash
!= hash
) {
2384 pctx
->num
= entry
->e_hash
;
2385 if (fix_problem(ctx
, PR_1_ATTR_HASH
, pctx
))
2387 entry
->e_hash
= hash
;
2390 entry
= EXT2_EXT_ATTR_NEXT(entry
);
2392 if (region_allocate(region
, (char *)entry
- (char *)header
, 4)) {
2393 if (fix_problem(ctx
, PR_1_EA_ALLOC_COLLISION
, pctx
))
2396 region_free(region
);
2399 * We only get here if there was no other errors that were fixed.
2400 * If there was a checksum fail, ask to correct it.
2403 fix_problem(ctx
, PR_1_EA_BLOCK_ONLY_CSUM_INVALID
, pctx
)) {
2404 pctx
->errcode
= ext2fs_write_ext_attr3(fs
, blk
, block_buf
,
2410 count
= header
->h_refcount
- 1;
2412 ea_refcount_store(ctx
->refcount
, blk
, count
);
2413 mark_block_used(ctx
, blk
);
2414 ext2fs_fast_mark_block_bitmap2(ctx
->block_ea_map
, blk
);
2419 region_free(region
);
2420 ext2fs_file_acl_block_set(fs
, inode
, 0);
2421 e2fsck_write_inode(ctx
, ino
, inode
, "check_ext_attr");
2425 /* Returns 1 if bad htree, 0 if OK */
2426 static int handle_htree(e2fsck_t ctx
, struct problem_context
*pctx
,
2427 ext2_ino_t ino
, struct ext2_inode
*inode
,
2430 struct ext2_dx_root_info
*root
;
2431 ext2_filsys fs
= ctx
->fs
;
2435 if ((!LINUX_S_ISDIR(inode
->i_mode
) &&
2436 fix_problem(ctx
, PR_1_HTREE_NODIR
, pctx
)) ||
2437 (!ext2fs_has_feature_dir_index(fs
->super
) &&
2438 fix_problem(ctx
, PR_1_HTREE_SET
, pctx
)))
2441 pctx
->errcode
= ext2fs_bmap2(fs
, ino
, inode
, 0, 0, 0, 0, &blk
);
2443 if ((pctx
->errcode
) ||
2445 (blk
< fs
->super
->s_first_data_block
) ||
2446 (blk
>= ext2fs_blocks_count(fs
->super
))) {
2447 if (fix_problem(ctx
, PR_1_HTREE_BADROOT
, pctx
))
2453 retval
= io_channel_read_blk64(fs
->io
, blk
, 1, block_buf
);
2454 if (retval
&& fix_problem(ctx
, PR_1_HTREE_BADROOT
, pctx
))
2457 /* XXX should check that beginning matches a directory */
2458 root
= (struct ext2_dx_root_info
*) (block_buf
+ 24);
2460 if ((root
->reserved_zero
|| root
->info_length
< 8) &&
2461 fix_problem(ctx
, PR_1_HTREE_BADROOT
, pctx
))
2464 pctx
->num
= root
->hash_version
;
2465 if ((root
->hash_version
!= EXT2_HASH_LEGACY
) &&
2466 (root
->hash_version
!= EXT2_HASH_HALF_MD4
) &&
2467 (root
->hash_version
!= EXT2_HASH_TEA
) &&
2468 fix_problem(ctx
, PR_1_HTREE_HASHV
, pctx
))
2471 if ((root
->unused_flags
& EXT2_HASH_FLAG_INCOMPAT
) &&
2472 fix_problem(ctx
, PR_1_HTREE_INCOMPAT
, pctx
))
2475 pctx
->num
= root
->indirect_levels
;
2476 if ((root
->indirect_levels
> 1) &&
2477 fix_problem(ctx
, PR_1_HTREE_DEPTH
, pctx
))
2483 void e2fsck_clear_inode(e2fsck_t ctx
, ext2_ino_t ino
,
2484 struct ext2_inode
*inode
, int restart_flag
,
2488 inode
->i_links_count
= 0;
2489 ext2fs_icount_store(ctx
->inode_link_info
, ino
, 0);
2490 inode
->i_dtime
= ctx
->now
;
2493 * If a special inode has such rotten block mappings that we
2494 * want to clear the whole inode, be sure to actually zap
2495 * the block maps because i_links_count isn't checked for
2496 * special inodes, and we'll end up right back here the next
2499 if (ino
< EXT2_FIRST_INODE(ctx
->fs
->super
))
2500 memset(inode
->i_block
, 0, sizeof(inode
->i_block
));
2502 ext2fs_unmark_inode_bitmap2(ctx
->inode_dir_map
, ino
);
2503 ext2fs_unmark_inode_bitmap2(ctx
->inode_used_map
, ino
);
2504 if (ctx
->inode_reg_map
)
2505 ext2fs_unmark_inode_bitmap2(ctx
->inode_reg_map
, ino
);
2506 if (ctx
->inode_bad_map
)
2507 ext2fs_unmark_inode_bitmap2(ctx
->inode_bad_map
, ino
);
2510 * If the inode was partially accounted for before processing
2511 * was aborted, we need to restart the pass 1 scan.
2513 ctx
->flags
|= restart_flag
;
2515 if (ino
== EXT2_BAD_INO
)
2516 memset(inode
, 0, sizeof(struct ext2_inode
));
2518 e2fsck_write_inode(ctx
, ino
, inode
, source
);
2522 * Use the multiple-blocks reclamation code to fix alignment problems in
2523 * a bigalloc filesystem. We want a logical cluster to map to *only* one
2524 * physical cluster, and we want the block offsets within that cluster to
2527 static int has_unaligned_cluster_map(e2fsck_t ctx
,
2528 blk64_t last_pblk
, blk64_t last_lblk
,
2529 blk64_t pblk
, blk64_t lblk
)
2531 blk64_t cluster_mask
;
2533 if (!ctx
->fs
->cluster_ratio_bits
)
2535 cluster_mask
= EXT2FS_CLUSTER_MASK(ctx
->fs
);
2538 * If the block in the logical cluster doesn't align with the block in
2539 * the physical cluster...
2541 if ((lblk
& cluster_mask
) != (pblk
& cluster_mask
))
2545 * If we cross a physical cluster boundary within a logical cluster...
2547 if (last_pblk
&& (lblk
& cluster_mask
) != 0 &&
2548 EXT2FS_B2C(ctx
->fs
, lblk
) == EXT2FS_B2C(ctx
->fs
, last_lblk
) &&
2549 EXT2FS_B2C(ctx
->fs
, pblk
) != EXT2FS_B2C(ctx
->fs
, last_pblk
))
2555 static void scan_extent_node(e2fsck_t ctx
, struct problem_context
*pctx
,
2556 struct process_block_struct
*pb
,
2557 blk64_t start_block
, blk64_t end_block
,
2559 ext2_extent_handle_t ehandle
,
2562 struct ext2fs_extent extent
;
2563 blk64_t blk
, last_lblk
;
2564 e2_blkcnt_t blockcnt
;
2566 int is_dir
, is_leaf
;
2568 struct ext2_extent_info info
;
2569 int failed_csum
= 0;
2571 if (pctx
->errcode
== EXT2_ET_EXTENT_CSUM_INVALID
)
2574 pctx
->errcode
= ext2fs_extent_get_info(ehandle
, &info
);
2577 if (!(ctx
->options
& E2F_OPT_FIXES_ONLY
) &&
2578 !pb
->eti
.force_rebuild
) {
2579 struct extent_tree_level
*etl
;
2581 etl
= pb
->eti
.ext_info
+ info
.curr_level
;
2582 etl
->num_extents
+= info
.num_entries
;
2583 etl
->max_extents
+= info
.max_entries
;
2585 * Implementation wart: Splitting extent blocks when appending
2586 * will leave the old block with one free entry. Therefore
2587 * unless the node is totally full, pretend that a non-root
2588 * extent block can hold one fewer entry than it actually does,
2589 * so that we don't repeatedly rebuild the extent tree.
2591 if (info
.curr_level
&& info
.num_entries
< info
.max_entries
)
2595 pctx
->errcode
= ext2fs_extent_get(ehandle
, EXT2_EXTENT_FIRST_SIB
,
2597 while ((pctx
->errcode
== 0 ||
2598 pctx
->errcode
== EXT2_ET_EXTENT_CSUM_INVALID
) &&
2599 info
.num_entries
-- > 0) {
2600 is_leaf
= extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
;
2601 is_dir
= LINUX_S_ISDIR(pctx
->inode
->i_mode
);
2602 last_lblk
= extent
.e_lblk
+ extent
.e_len
- 1;
2605 pctx
->blk
= extent
.e_pblk
;
2606 pctx
->blk2
= extent
.e_lblk
;
2607 pctx
->num
= extent
.e_len
;
2608 pctx
->blkcount
= extent
.e_lblk
+ extent
.e_len
;
2610 if (extent
.e_pblk
== 0 ||
2611 extent
.e_pblk
< ctx
->fs
->super
->s_first_data_block
||
2612 extent
.e_pblk
>= ext2fs_blocks_count(ctx
->fs
->super
))
2613 problem
= PR_1_EXTENT_BAD_START_BLK
;
2614 else if (extent
.e_lblk
< start_block
)
2615 problem
= PR_1_OUT_OF_ORDER_EXTENTS
;
2616 else if ((end_block
&& last_lblk
> end_block
) &&
2617 (!(extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
&&
2618 last_lblk
> eof_block
)))
2619 problem
= PR_1_EXTENT_END_OUT_OF_BOUNDS
;
2620 else if (is_leaf
&& extent
.e_len
== 0)
2621 problem
= PR_1_EXTENT_LENGTH_ZERO
;
2623 (extent
.e_pblk
+ extent
.e_len
) >
2624 ext2fs_blocks_count(ctx
->fs
->super
))
2625 problem
= PR_1_EXTENT_ENDS_BEYOND
;
2626 else if (is_leaf
&& is_dir
&&
2627 ((extent
.e_lblk
+ extent
.e_len
) >
2628 (1U << (21 - ctx
->fs
->super
->s_log_block_size
))))
2629 problem
= PR_1_TOOBIG_DIR
;
2631 if (is_leaf
&& problem
== 0 && extent
.e_len
> 0 &&
2632 region_allocate(pb
->region
, extent
.e_lblk
, extent
.e_len
))
2633 problem
= PR_1_EXTENT_COLLISION
;
2636 * Uninitialized blocks in a directory? Clear the flag and
2637 * we'll interpret the blocks later.
2639 if (try_repairs
&& is_dir
&& problem
== 0 &&
2640 (extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
) &&
2641 fix_problem(ctx
, PR_1_UNINIT_DBLOCK
, pctx
)) {
2642 extent
.e_flags
&= ~EXT2_EXTENT_FLAGS_UNINIT
;
2643 pb
->inode_modified
= 1;
2644 pctx
->errcode
= ext2fs_extent_replace(ehandle
, 0,
2651 if (try_repairs
&& problem
) {
2653 if (fix_problem(ctx
, problem
, pctx
)) {
2654 if (ctx
->invalid_bitmaps
) {
2656 * If fsck knows the bitmaps are bad,
2657 * skip to the next extent and
2658 * try to clear this extent again
2659 * after fixing the bitmaps, by
2662 pctx
->errcode
= ext2fs_extent_get(
2664 EXT2_EXTENT_NEXT_SIB
,
2666 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
2667 if (pctx
->errcode
==
2668 EXT2_ET_NO_CURRENT_NODE
) {
2674 e2fsck_read_bitmaps(ctx
);
2675 pb
->inode_modified
= 1;
2677 ext2fs_extent_delete(ehandle
, 0);
2678 if (pctx
->errcode
) {
2679 pctx
->str
= "ext2fs_extent_delete";
2682 pctx
->errcode
= ext2fs_extent_fix_parents(ehandle
);
2683 if (pctx
->errcode
&&
2684 pctx
->errcode
!= EXT2_ET_NO_CURRENT_NODE
) {
2685 pctx
->str
= "ext2fs_extent_fix_parents";
2688 pctx
->errcode
= ext2fs_extent_get(ehandle
,
2689 EXT2_EXTENT_CURRENT
,
2691 if (pctx
->errcode
== EXT2_ET_NO_CURRENT_NODE
) {
2702 blk64_t lblk
= extent
.e_lblk
;
2703 int next_try_repairs
= 1;
2705 blk
= extent
.e_pblk
;
2708 * If this lower extent block collides with critical
2709 * metadata, don't try to repair the damage. Pass 1b
2710 * will reallocate the block; then we can try again.
2712 if (pb
->ino
!= EXT2_RESIZE_INO
&&
2713 extent
.e_pblk
< ctx
->fs
->super
->s_blocks_count
&&
2714 ext2fs_test_block_bitmap2(ctx
->block_metadata_map
,
2716 next_try_repairs
= 0;
2719 PR_1_CRITICAL_METADATA_COLLISION
,
2721 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
2723 pctx
->errcode
= ext2fs_extent_get(ehandle
,
2724 EXT2_EXTENT_DOWN
, &extent
);
2725 if (pctx
->errcode
&&
2726 pctx
->errcode
!= EXT2_ET_EXTENT_CSUM_INVALID
) {
2727 pctx
->str
= "EXT2_EXTENT_DOWN";
2728 problem
= PR_1_EXTENT_HEADER_INVALID
;
2729 if (!next_try_repairs
)
2731 if (pctx
->errcode
== EXT2_ET_EXTENT_HEADER_BAD
)
2732 goto report_problem
;
2735 /* The next extent should match this index's logical start */
2736 if (extent
.e_lblk
!= lblk
) {
2737 struct ext2_extent_info e_info
;
2739 ext2fs_extent_get_info(ehandle
, &e_info
);
2741 pctx
->blk2
= extent
.e_lblk
;
2742 pctx
->num
= e_info
.curr_level
- 1;
2743 problem
= PR_1_EXTENT_INDEX_START_INVALID
;
2744 if (fix_problem(ctx
, problem
, pctx
)) {
2745 pb
->inode_modified
= 1;
2747 ext2fs_extent_fix_parents(ehandle
);
2748 if (pctx
->errcode
) {
2749 pctx
->str
= "ext2fs_extent_fix_parents";
2754 scan_extent_node(ctx
, pctx
, pb
, extent
.e_lblk
,
2755 last_lblk
, eof_block
, ehandle
,
2759 pctx
->errcode
= ext2fs_extent_get(ehandle
,
2760 EXT2_EXTENT_UP
, &extent
);
2761 if (pctx
->errcode
) {
2762 pctx
->str
= "EXT2_EXTENT_UP";
2765 mark_block_used(ctx
, blk
);
2770 if ((pb
->previous_block
!= 0) &&
2771 (pb
->previous_block
+1 != extent
.e_pblk
)) {
2772 if (ctx
->options
& E2F_OPT_FRAGCHECK
) {
2777 else if (pb
->is_reg
)
2780 printf(("%6lu(%c): expecting %6lu "
2782 "phys %6lu log %lu len %lu\n"),
2783 (unsigned long) pctx
->ino
, type
,
2784 (unsigned long) pb
->previous_block
+1,
2785 (unsigned long) extent
.e_pblk
,
2786 (unsigned long) extent
.e_lblk
,
2787 (unsigned long) extent
.e_len
);
2792 * If we notice a gap in the logical block mappings of an
2793 * extent-mapped directory, offer to close the hole by
2794 * moving the logical block down, otherwise we'll go mad in
2795 * pass 3 allocating empty directory blocks to fill the hole.
2797 if (try_repairs
&& is_dir
&&
2798 pb
->last_block
+ 1 < extent
.e_lblk
) {
2801 new_lblk
= pb
->last_block
+ 1;
2802 if (EXT2FS_CLUSTER_RATIO(ctx
->fs
) > 1)
2803 new_lblk
= ((new_lblk
+
2804 EXT2FS_CLUSTER_RATIO(ctx
->fs
) - 1) &
2805 ~EXT2FS_CLUSTER_MASK(ctx
->fs
)) |
2807 EXT2FS_CLUSTER_MASK(ctx
->fs
));
2808 pctx
->blk
= extent
.e_lblk
;
2809 pctx
->blk2
= new_lblk
;
2810 if (fix_problem(ctx
, PR_1_COLLAPSE_DBLOCK
, pctx
)) {
2811 extent
.e_lblk
= new_lblk
;
2812 pb
->inode_modified
= 1;
2813 pctx
->errcode
= ext2fs_extent_replace(ehandle
,
2815 if (pctx
->errcode
) {
2819 pctx
->errcode
= ext2fs_extent_fix_parents(ehandle
);
2821 goto failed_add_dir_block
;
2822 pctx
->errcode
= ext2fs_extent_goto(ehandle
,
2825 goto failed_add_dir_block
;
2826 last_lblk
= extent
.e_lblk
+ extent
.e_len
- 1;
2831 while (is_dir
&& (++pb
->last_db_block
<
2832 (e2_blkcnt_t
) extent
.e_lblk
)) {
2833 pctx
->errcode
= ext2fs_add_dir_block2(ctx
->fs
->dblist
,
2836 if (pctx
->errcode
) {
2838 pctx
->num
= pb
->last_db_block
;
2839 goto failed_add_dir_block
;
2842 if (!ctx
->fs
->cluster_ratio_bits
) {
2843 mark_blocks_used(ctx
, extent
.e_pblk
, extent
.e_len
);
2844 pb
->num_blocks
+= extent
.e_len
;
2846 for (blk
= extent
.e_pblk
, blockcnt
= extent
.e_lblk
, i
= 0;
2848 blk
++, blockcnt
++, i
++) {
2849 if (ctx
->fs
->cluster_ratio_bits
&&
2850 !(pb
->previous_block
&&
2851 (EXT2FS_B2C(ctx
->fs
, blk
) ==
2852 EXT2FS_B2C(ctx
->fs
, pb
->previous_block
)) &&
2853 (blk
& EXT2FS_CLUSTER_MASK(ctx
->fs
)) ==
2854 ((unsigned) blockcnt
& EXT2FS_CLUSTER_MASK(ctx
->fs
)))) {
2855 mark_block_used(ctx
, blk
);
2858 if (has_unaligned_cluster_map(ctx
, pb
->previous_block
,
2859 pb
->last_block
, blk
,
2861 pctx
->blk
= blockcnt
;
2863 fix_problem(ctx
, PR_1_MISALIGNED_CLUSTER
, pctx
);
2864 mark_block_used(ctx
, blk
);
2865 mark_block_used(ctx
, blk
);
2867 pb
->last_block
= blockcnt
;
2868 pb
->previous_block
= blk
;
2871 pctx
->errcode
= ext2fs_add_dir_block2(ctx
->fs
->dblist
, pctx
->ino
, blk
, blockcnt
);
2872 if (pctx
->errcode
) {
2874 pctx
->num
= blockcnt
;
2875 failed_add_dir_block
:
2876 fix_problem(ctx
, PR_1_ADD_DBLOCK
, pctx
);
2877 /* Should never get here */
2878 ctx
->flags
|= E2F_FLAG_ABORT
;
2883 if (is_dir
&& extent
.e_len
> 0)
2884 pb
->last_db_block
= blockcnt
- 1;
2885 pb
->previous_block
= extent
.e_pblk
+ extent
.e_len
- 1;
2886 start_block
= pb
->last_block
= last_lblk
;
2887 if (is_leaf
&& !is_dir
&&
2888 !(extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
))
2889 pb
->last_init_lblock
= last_lblk
;
2891 pctx
->errcode
= ext2fs_extent_get(ehandle
,
2892 EXT2_EXTENT_NEXT_SIB
,
2896 /* Failed csum but passes checks? Ask to fix checksum. */
2898 fix_problem(ctx
, PR_1_EXTENT_ONLY_CSUM_INVALID
, pctx
)) {
2899 pb
->inode_modified
= 1;
2900 pctx
->errcode
= ext2fs_extent_replace(ehandle
, 0, &extent
);
2905 if (pctx
->errcode
== EXT2_ET_EXTENT_NO_NEXT
)
2909 static void check_blocks_extents(e2fsck_t ctx
, struct problem_context
*pctx
,
2910 struct process_block_struct
*pb
)
2912 struct ext2_extent_info info
;
2913 struct ext2_inode
*inode
= pctx
->inode
;
2914 ext2_extent_handle_t ehandle
;
2915 ext2_filsys fs
= ctx
->fs
;
2916 ext2_ino_t ino
= pctx
->ino
;
2919 struct ext3_extent_header
*eh
;
2921 /* Check for a proper extent header... */
2922 eh
= (struct ext3_extent_header
*) &inode
->i_block
[0];
2923 retval
= ext2fs_extent_header_verify(eh
, sizeof(inode
->i_block
));
2925 if (fix_problem(ctx
, PR_1_MISSING_EXTENT_HEADER
, pctx
))
2926 e2fsck_clear_inode(ctx
, ino
, inode
, 0,
2927 "check_blocks_extents");
2932 /* ...since this function doesn't fail if i_block is zeroed. */
2933 pctx
->errcode
= ext2fs_extent_open2(fs
, ino
, inode
, &ehandle
);
2934 if (pctx
->errcode
) {
2935 if (fix_problem(ctx
, PR_1_READ_EXTENT
, pctx
))
2936 e2fsck_clear_inode(ctx
, ino
, inode
, 0,
2937 "check_blocks_extents");
2942 retval
= ext2fs_extent_get_info(ehandle
, &info
);
2944 int max_depth
= info
.max_depth
;
2946 if (max_depth
>= MAX_EXTENT_DEPTH_COUNT
)
2947 max_depth
= MAX_EXTENT_DEPTH_COUNT
-1;
2948 ctx
->extent_depth_count
[max_depth
]++;
2951 /* Check maximum extent depth */
2952 pctx
->blk
= info
.max_depth
;
2953 pctx
->blk2
= ext2fs_max_extent_depth(ehandle
);
2954 if (pctx
->blk2
< pctx
->blk
&&
2955 fix_problem(ctx
, PR_1_EXTENT_BAD_MAX_DEPTH
, pctx
))
2956 pb
->eti
.force_rebuild
= 1;
2958 /* Can we collect extent tree level stats? */
2959 pctx
->blk
= MAX_EXTENT_DEPTH_COUNT
;
2960 if (pctx
->blk2
> pctx
->blk
)
2961 fix_problem(ctx
, PR_1E_MAX_EXTENT_TREE_DEPTH
, pctx
);
2962 memset(pb
->eti
.ext_info
, 0, sizeof(pb
->eti
.ext_info
));
2963 pb
->eti
.ino
= pb
->ino
;
2965 pb
->region
= region_create(0, info
.max_lblk
);
2967 ext2fs_extent_free(ehandle
);
2968 fix_problem(ctx
, PR_1_EXTENT_ALLOC_REGION_ABORT
, pctx
);
2969 ctx
->flags
|= E2F_FLAG_ABORT
;
2973 eof_lblk
= ((EXT2_I_SIZE(inode
) + fs
->blocksize
- 1) >>
2974 EXT2_BLOCK_SIZE_BITS(fs
->super
)) - 1;
2975 scan_extent_node(ctx
, pctx
, pb
, 0, 0, eof_lblk
, ehandle
, 1);
2976 if (pctx
->errcode
&&
2977 fix_problem(ctx
, PR_1_EXTENT_ITERATE_FAILURE
, pctx
)) {
2979 inode
->i_blocks
= 0;
2980 e2fsck_clear_inode(ctx
, ino
, inode
, E2F_FLAG_RESTART
,
2981 "check_blocks_extents");
2984 region_free(pb
->region
);
2986 ext2fs_extent_free(ehandle
);
2988 /* Rebuild unless it's a dir and we're rehashing it */
2989 if (LINUX_S_ISDIR(inode
->i_mode
) &&
2990 e2fsck_dir_will_be_rehashed(ctx
, ino
))
2993 if (ctx
->options
& E2F_OPT_CONVERT_BMAP
)
2994 e2fsck_rebuild_extents_later(ctx
, ino
);
2996 e2fsck_should_rebuild_extents(ctx
, pctx
, &pb
->eti
, &info
);
3000 * In fact we don't need to check blocks for an inode with inline data
3001 * because this inode doesn't have any blocks. In this function all
3002 * we need to do is add this inode into dblist when it is a directory.
3004 static void check_blocks_inline_data(e2fsck_t ctx
, struct problem_context
*pctx
,
3005 struct process_block_struct
*pb
)
3008 size_t inline_data_size
= 0;
3015 /* Process the dirents in i_block[] as the "first" block. */
3016 pctx
->errcode
= ext2fs_add_dir_block2(ctx
->fs
->dblist
, pb
->ino
, 0, 0);
3020 /* Process the dirents in the EA as a "second" block. */
3021 flags
= ctx
->fs
->flags
;
3022 ctx
->fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
3023 pctx
->errcode
= ext2fs_inline_data_size(ctx
->fs
, pb
->ino
,
3025 ctx
->fs
->flags
= (flags
& EXT2_FLAG_IGNORE_CSUM_ERRORS
) |
3026 (ctx
->fs
->flags
& ~EXT2_FLAG_IGNORE_CSUM_ERRORS
);
3027 if (pctx
->errcode
) {
3032 if (inline_data_size
<= EXT4_MIN_INLINE_DATA_SIZE
)
3035 pctx
->errcode
= ext2fs_add_dir_block2(ctx
->fs
->dblist
, pb
->ino
, 0, 1);
3043 fix_problem(ctx
, PR_1_ADD_DBLOCK
, pctx
);
3044 ctx
->flags
|= E2F_FLAG_ABORT
;
3048 * This subroutine is called on each inode to account for all of the
3049 * blocks used by that inode.
3051 static void check_blocks(e2fsck_t ctx
, struct problem_context
*pctx
,
3054 ext2_filsys fs
= ctx
->fs
;
3055 struct process_block_struct pb
;
3056 ext2_ino_t ino
= pctx
->ino
;
3057 struct ext2_inode
*inode
= pctx
->inode
;
3058 unsigned bad_size
= 0;
3059 int dirty_inode
= 0;
3067 pb
.last_init_lblock
= -1;
3068 pb
.last_db_block
= -1;
3069 pb
.num_illegal_blocks
= 0;
3070 pb
.suppress
= 0; pb
.clear
= 0;
3073 pb
.previous_block
= 0;
3074 pb
.is_dir
= LINUX_S_ISDIR(inode
->i_mode
);
3075 pb
.is_reg
= LINUX_S_ISREG(inode
->i_mode
);
3076 pb
.max_blocks
= 1U << (31 - fs
->super
->s_log_block_size
);
3080 pb
.inode_modified
= 0;
3081 pb
.eti
.force_rebuild
= 0;
3085 extent_fs
= ext2fs_has_feature_extents(ctx
->fs
->super
);
3086 inlinedata_fs
= ext2fs_has_feature_inline_data(ctx
->fs
->super
);
3088 if (check_ext_attr(ctx
, pctx
, block_buf
)) {
3089 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
3094 if (inlinedata_fs
&& (inode
->i_flags
& EXT4_INLINE_DATA_FL
))
3095 check_blocks_inline_data(ctx
, pctx
, &pb
);
3096 else if (ext2fs_inode_has_valid_blocks2(fs
, inode
)) {
3097 if (extent_fs
&& (inode
->i_flags
& EXT4_EXTENTS_FL
))
3098 check_blocks_extents(ctx
, pctx
, &pb
);
3102 * If we've modified the inode, write it out before
3103 * iterate() tries to use it.
3106 e2fsck_write_inode(ctx
, ino
, inode
,
3111 fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
3112 pctx
->errcode
= ext2fs_block_iterate3(fs
, ino
,
3113 pb
.is_dir
? BLOCK_FLAG_HOLE
: 0,
3114 block_buf
, process_block
, &pb
);
3116 * We do not have uninitialized extents in non extent
3119 pb
.last_init_lblock
= pb
.last_block
;
3121 * If iterate() changed a block mapping, we have to
3122 * re-read the inode. If we decide to clear the
3123 * inode after clearing some stuff, we'll re-write the
3124 * bad mappings into the inode!
3126 if (pb
.inode_modified
)
3127 e2fsck_read_inode(ctx
, ino
, inode
,
3129 fs
->flags
= (flags
& EXT2_FLAG_IGNORE_CSUM_ERRORS
) |
3130 (fs
->flags
& ~EXT2_FLAG_IGNORE_CSUM_ERRORS
);
3132 if (ctx
->options
& E2F_OPT_CONVERT_BMAP
) {
3134 printf("bmap rebuild ino=%d\n", ino
);
3136 if (!LINUX_S_ISDIR(inode
->i_mode
) ||
3137 !e2fsck_dir_will_be_rehashed(ctx
, ino
))
3138 e2fsck_rebuild_extents_later(ctx
, ino
);
3142 end_problem_latch(ctx
, PR_LATCH_BLOCK
);
3143 end_problem_latch(ctx
, PR_LATCH_TOOBIG
);
3144 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
3147 fix_problem(ctx
, PR_1_BLOCK_ITERATE
, pctx
);
3149 if (pb
.fragmented
&& pb
.num_blocks
< fs
->super
->s_blocks_per_group
) {
3150 if (LINUX_S_ISDIR(inode
->i_mode
))
3151 ctx
->fs_fragmented_dir
++;
3153 ctx
->fs_fragmented
++;
3157 e2fsck_clear_inode(ctx
, ino
, inode
, E2F_FLAG_RESTART
,
3162 if (inode
->i_flags
& EXT2_INDEX_FL
) {
3163 if (handle_htree(ctx
, pctx
, ino
, inode
, block_buf
)) {
3164 inode
->i_flags
&= ~EXT2_INDEX_FL
;
3167 e2fsck_add_dx_dir(ctx
, ino
, pb
.last_block
+1);
3171 if (!pb
.num_blocks
&& pb
.is_dir
&&
3172 !(inode
->i_flags
& EXT4_INLINE_DATA_FL
)) {
3173 if (fix_problem(ctx
, PR_1_ZERO_LENGTH_DIR
, pctx
)) {
3174 e2fsck_clear_inode(ctx
, ino
, inode
, 0, "check_blocks");
3175 ctx
->fs_directory_count
--;
3180 if (ino
!= quota_type2inum(PRJQUOTA
, fs
->super
) &&
3181 (ino
== EXT2_ROOT_INO
|| ino
>= EXT2_FIRST_INODE(ctx
->fs
->super
))) {
3182 quota_data_add(ctx
->qctx
, (struct ext2_inode_large
*) inode
,
3184 pb
.num_blocks
* EXT2_CLUSTER_SIZE(fs
->super
));
3185 quota_data_inodes(ctx
->qctx
, (struct ext2_inode_large
*) inode
,
3189 if (!ext2fs_has_feature_huge_file(fs
->super
) ||
3190 !(inode
->i_flags
& EXT4_HUGE_FILE_FL
))
3191 pb
.num_blocks
*= (fs
->blocksize
/ 512);
3192 pb
.num_blocks
*= EXT2FS_CLUSTER_RATIO(fs
);
3194 printf("inode %u, i_size = %u, last_block = %llu, i_blocks=%llu, num_blocks = %llu\n",
3195 ino
, inode
->i_size
, pb
.last_block
, ext2fs_inode_i_blocks(fs
, inode
),
3199 unsigned nblock
= inode
->i_size
>> EXT2_BLOCK_SIZE_BITS(fs
->super
);
3200 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
) {
3205 flags
= ctx
->fs
->flags
;
3206 ctx
->fs
->flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
3207 err
= ext2fs_inline_data_size(ctx
->fs
, pctx
->ino
,
3209 ctx
->fs
->flags
= (flags
&
3210 EXT2_FLAG_IGNORE_CSUM_ERRORS
) |
3212 ~EXT2_FLAG_IGNORE_CSUM_ERRORS
);
3213 if (err
|| sz
!= inode
->i_size
) {
3217 } else if (inode
->i_size
& (fs
->blocksize
- 1))
3219 else if (nblock
> (pb
.last_block
+ 1))
3221 else if (nblock
< (pb
.last_block
+ 1)) {
3222 if (((pb
.last_block
+ 1) - nblock
) >
3223 fs
->super
->s_prealloc_dir_blocks
)
3227 e2_blkcnt_t blkpg
= ctx
->blocks_per_page
;
3229 size
= EXT2_I_SIZE(inode
);
3230 if ((pb
.last_init_lblock
>= 0) &&
3231 /* allow allocated blocks to end of PAGE_SIZE */
3232 (size
< (__u64
)pb
.last_init_lblock
* fs
->blocksize
) &&
3233 (pb
.last_init_lblock
/ blkpg
* blkpg
!= pb
.last_init_lblock
||
3234 size
< (__u64
)(pb
.last_init_lblock
& ~(blkpg
-1)) *
3237 else if (!(extent_fs
&& (inode
->i_flags
& EXT4_EXTENTS_FL
)) &&
3238 size
> ext2_max_sizes
[fs
->super
->s_log_block_size
])
3239 /* too big for a direct/indirect-mapped file */
3241 else if ((extent_fs
&& (inode
->i_flags
& EXT4_EXTENTS_FL
)) &&
3243 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs
->super
))) - 1))
3244 /* too big for an extent-based file - 32bit ee_block */
3247 /* i_size for symlinks is checked elsewhere */
3248 if (bad_size
&& !LINUX_S_ISLNK(inode
->i_mode
)) {
3249 /* Did inline_data set pctx->num earlier? */
3251 pctx
->num
= (pb
.last_block
+ 1) * fs
->blocksize
;
3252 pctx
->group
= bad_size
;
3253 if (fix_problem(ctx
, PR_1_BAD_I_SIZE
, pctx
)) {
3254 if (LINUX_S_ISDIR(inode
->i_mode
))
3255 pctx
->num
&= 0xFFFFFFFFULL
;
3256 ext2fs_inode_size_set(fs
, inode
, pctx
->num
);
3257 if (EXT2_I_SIZE(inode
) == 0 &&
3258 (inode
->i_flags
& EXT4_INLINE_DATA_FL
)) {
3259 memset(inode
->i_block
, 0,
3260 sizeof(inode
->i_block
));
3261 inode
->i_flags
&= ~EXT4_INLINE_DATA_FL
;
3267 if (LINUX_S_ISREG(inode
->i_mode
) &&
3268 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode
)))
3270 if ((fs
->super
->s_creator_os
!= EXT2_OS_HURD
) &&
3271 ((pb
.num_blocks
!= ext2fs_inode_i_blocks(fs
, inode
)) ||
3272 (ext2fs_has_feature_huge_file(fs
->super
) &&
3273 (inode
->i_flags
& EXT4_HUGE_FILE_FL
) &&
3274 (inode
->osd2
.linux2
.l_i_blocks_hi
!= 0)))) {
3275 pctx
->num
= pb
.num_blocks
;
3276 if (fix_problem(ctx
, PR_1_BAD_I_BLOCKS
, pctx
)) {
3277 inode
->i_blocks
= pb
.num_blocks
;
3278 inode
->osd2
.linux2
.l_i_blocks_hi
= pb
.num_blocks
>> 32;
3285 * The kernel gets mad if we ask it to allocate bigalloc clusters to
3286 * a block mapped file, so rebuild it as an extent file. We can skip
3287 * symlinks because they're never rewritten.
3289 if (ext2fs_has_feature_bigalloc(fs
->super
) &&
3290 (LINUX_S_ISREG(inode
->i_mode
) || LINUX_S_ISDIR(inode
->i_mode
)) &&
3291 ext2fs_inode_data_blocks2(fs
, inode
) > 0 &&
3292 (ino
== EXT2_ROOT_INO
|| ino
>= EXT2_FIRST_INO(fs
->super
)) &&
3293 !(inode
->i_flags
& (EXT4_EXTENTS_FL
| EXT4_INLINE_DATA_FL
)) &&
3294 fix_problem(ctx
, PR_1_NO_BIGALLOC_BLOCKMAP_FILES
, pctx
)) {
3295 pctx
->errcode
= e2fsck_rebuild_extents_later(ctx
, ino
);
3300 if (ctx
->dirs_to_hash
&& pb
.is_dir
&&
3301 !(ctx
->lost_and_found
&& ctx
->lost_and_found
== ino
) &&
3302 !(inode
->i_flags
& EXT2_INDEX_FL
) &&
3303 ((inode
->i_size
/ fs
->blocksize
) >= 3))
3304 e2fsck_rehash_dir_later(ctx
, ino
);
3308 e2fsck_write_inode(ctx
, ino
, inode
, "check_blocks");
3313 * Helper function called by process block when an illegal block is
3314 * found. It returns a description about why the block is illegal
3316 static char *describe_illegal_block(ext2_filsys fs
, blk64_t block
)
3320 static char problem
[80];
3322 super
= fs
->super
->s_first_data_block
;
3323 strcpy(problem
, "PROGRAMMING ERROR: Unknown reason for illegal block");
3324 if (block
< super
) {
3325 sprintf(problem
, "< FIRSTBLOCK (%u)", super
);
3327 } else if (block
>= ext2fs_blocks_count(fs
->super
)) {
3328 sprintf(problem
, "> BLOCKS (%u)", ext2fs_blocks_count(fs
->super
));
3331 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
3332 if (block
== super
) {
3333 sprintf(problem
, "is the superblock in group %d", i
);
3336 if (block
> super
&&
3337 block
<= (super
+ fs
->desc_blocks
)) {
3338 sprintf(problem
, "is in the group descriptors "
3342 if (block
== ext2fs_block_bitmap_loc(fs
, i
)) {
3343 sprintf(problem
, "is the block bitmap of group %d", i
);
3346 if (block
== ext2fs_inode_bitmap_loc(fs
, i
)) {
3347 sprintf(problem
, "is the inode bitmap of group %d", i
);
3350 if (block
>= ext2fs_inode_table_loc(fs
, i
) &&
3351 (block
< ext2fs_inode_table_loc(fs
, i
)
3352 + fs
->inode_blocks_per_group
)) {
3353 sprintf(problem
, "is in the inode table of group %d",
3357 super
+= fs
->super
->s_blocks_per_group
;
3364 * This is a helper function for check_blocks().
3366 static int process_block(ext2_filsys fs
,
3368 e2_blkcnt_t blockcnt
,
3369 blk64_t ref_block
EXT2FS_ATTR((unused
)),
3370 int ref_offset
EXT2FS_ATTR((unused
)),
3373 struct process_block_struct
*p
;
3374 struct problem_context
*pctx
;
3375 blk64_t blk
= *block_nr
;
3377 problem_t problem
= 0;
3380 p
= (struct process_block_struct
*) priv_data
;
3385 * For a directory, add logical block zero for processing even if it's
3386 * not mapped or we'll be perennially stuck with broken "." and ".."
3389 if (p
->is_dir
&& blockcnt
== 0 && blk
== 0) {
3390 pctx
->errcode
= ext2fs_add_dir_block2(fs
->dblist
, p
->ino
, 0, 0);
3391 if (pctx
->errcode
) {
3393 pctx
->num
= blockcnt
;
3394 goto failed_add_dir_block
;
3403 printf("Process_block, inode %lu, block %u, #%d\n", p
->ino
, blk
,
3408 * Simplistic fragmentation check. We merely require that the
3409 * file be contiguous. (Which can never be true for really
3410 * big files that are greater than a block group.)
3412 if (p
->previous_block
&& p
->ino
!= EXT2_RESIZE_INO
) {
3413 if (p
->previous_block
+1 != blk
) {
3414 if (ctx
->options
& E2F_OPT_FRAGCHECK
) {
3422 printf(_("%6lu(%c): expecting %6lu "
3423 "got phys %6lu (blkcnt %lld)\n"),
3424 (unsigned long) pctx
->ino
, type
,
3425 (unsigned long) p
->previous_block
+1,
3426 (unsigned long) blk
,
3433 if (p
->is_dir
&& blockcnt
> (1 << (21 - fs
->super
->s_log_block_size
)))
3434 problem
= PR_1_TOOBIG_DIR
;
3435 if (p
->is_reg
&& p
->num_blocks
+1 >= p
->max_blocks
)
3436 problem
= PR_1_TOOBIG_REG
;
3437 if (!p
->is_dir
&& !p
->is_reg
&& blockcnt
> 0)
3438 problem
= PR_1_TOOBIG_SYMLINK
;
3440 if (blk
< fs
->super
->s_first_data_block
||
3441 blk
>= ext2fs_blocks_count(fs
->super
))
3442 problem
= PR_1_ILLEGAL_BLOCK_NUM
;
3445 * If this IND/DIND/TIND block is squatting atop some critical metadata
3446 * (group descriptors, superblock, bitmap, inode table), any write to
3447 * "fix" mapping problems will destroy the metadata. We'll let pass 1b
3448 * fix that and restart fsck.
3451 p
->ino
!= EXT2_RESIZE_INO
&&
3452 blk
< ctx
->fs
->super
->s_blocks_count
&&
3453 ext2fs_test_block_bitmap2(ctx
->block_metadata_map
, blk
)) {
3455 fix_problem(ctx
, PR_1_CRITICAL_METADATA_COLLISION
, pctx
);
3456 ctx
->flags
|= E2F_FLAG_RESTART_LATER
;
3460 p
->num_illegal_blocks
++;
3462 * A bit of subterfuge here -- we're trying to fix a block
3463 * mapping, but the IND/DIND/TIND block could have collided
3464 * with some critical metadata. So, fix the in-core mapping so
3465 * iterate won't go insane, but return 0 instead of
3466 * BLOCK_CHANGED so that it won't write the remapping out to
3467 * our multiply linked block.
3469 * Even if we previously determined that an *IND block
3470 * conflicts with critical metadata, we must still try to
3471 * iterate the *IND block as if it is an *IND block to find and
3472 * mark the blocks it points to. Better to be overly cautious
3473 * with the used_blocks map so that we don't move the *IND
3474 * block to a block that's really in use!
3476 if (p
->ino
!= EXT2_RESIZE_INO
&&
3478 ext2fs_test_block_bitmap2(ctx
->block_metadata_map
,
3483 if (!p
->suppress
&& (p
->num_illegal_blocks
% 12) == 0) {
3484 if (fix_problem(ctx
, PR_1_TOO_MANY_BAD_BLOCKS
, pctx
)) {
3488 if (fix_problem(ctx
, PR_1_SUPPRESS_MESSAGES
, pctx
)) {
3490 set_latch_flags(PR_LATCH_BLOCK
,
3495 pctx
->blkcount
= blockcnt
;
3496 if (fix_problem(ctx
, problem
, pctx
)) {
3497 blk
= *block_nr
= 0;
3498 ret_code
= BLOCK_CHANGED
;
3499 p
->inode_modified
= 1;
3501 * If the directory block is too big and is beyond the
3502 * end of the FS, don't bother trying to add it for
3503 * processing -- the kernel would never have created a
3504 * directory this large, and we risk an ENOMEM abort.
3505 * In any case, the toobig handler for extent-based
3506 * directories also doesn't feed toobig blocks to
3509 if (problem
== PR_1_TOOBIG_DIR
)
3516 if (p
->ino
== EXT2_RESIZE_INO
) {
3518 * The resize inode has already be sanity checked
3519 * during pass #0 (the superblock checks). All we
3520 * have to do is mark the double indirect block as
3521 * being in use; all of the other blocks are handled
3522 * by mark_table_blocks()).
3524 if (blockcnt
== BLOCK_COUNT_DIND
)
3525 mark_block_used(ctx
, blk
);
3527 } else if (!(ctx
->fs
->cluster_ratio_bits
&&
3528 p
->previous_block
&&
3529 (EXT2FS_B2C(ctx
->fs
, blk
) ==
3530 EXT2FS_B2C(ctx
->fs
, p
->previous_block
)) &&
3531 (blk
& EXT2FS_CLUSTER_MASK(ctx
->fs
)) ==
3532 ((unsigned) blockcnt
& EXT2FS_CLUSTER_MASK(ctx
->fs
)))) {
3533 mark_block_used(ctx
, blk
);
3535 } else if (has_unaligned_cluster_map(ctx
, p
->previous_block
,
3536 p
->last_block
, blk
, blockcnt
)) {
3537 pctx
->blk
= blockcnt
;
3539 fix_problem(ctx
, PR_1_MISALIGNED_CLUSTER
, pctx
);
3540 mark_block_used(ctx
, blk
);
3541 mark_block_used(ctx
, blk
);
3544 p
->last_block
= blockcnt
;
3545 p
->previous_block
= blk
;
3547 if (p
->is_dir
&& (blockcnt
>= 0)) {
3548 while (++p
->last_db_block
< blockcnt
) {
3549 pctx
->errcode
= ext2fs_add_dir_block2(fs
->dblist
,
3552 if (pctx
->errcode
) {
3554 pctx
->num
= p
->last_db_block
;
3555 goto failed_add_dir_block
;
3558 pctx
->errcode
= ext2fs_add_dir_block2(fs
->dblist
, p
->ino
,
3560 if (pctx
->errcode
) {
3562 pctx
->num
= blockcnt
;
3563 failed_add_dir_block
:
3564 fix_problem(ctx
, PR_1_ADD_DBLOCK
, pctx
);
3565 /* Should never get here */
3566 ctx
->flags
|= E2F_FLAG_ABORT
;
3573 static int process_bad_block(ext2_filsys fs
,
3575 e2_blkcnt_t blockcnt
,
3576 blk64_t ref_block
EXT2FS_ATTR((unused
)),
3577 int ref_offset
EXT2FS_ATTR((unused
)),
3580 struct process_block_struct
*p
;
3581 blk64_t blk
= *block_nr
;
3582 blk64_t first_block
;
3584 struct problem_context
*pctx
;
3590 p
= (struct process_block_struct
*) priv_data
;
3594 pctx
->ino
= EXT2_BAD_INO
;
3596 pctx
->blkcount
= blockcnt
;
3598 if ((blk
< fs
->super
->s_first_data_block
) ||
3599 (blk
>= ext2fs_blocks_count(fs
->super
))) {
3600 if (fix_problem(ctx
, PR_1_BB_ILLEGAL_BLOCK_NUM
, pctx
)) {
3602 return BLOCK_CHANGED
;
3608 if (ext2fs_test_block_bitmap2(p
->fs_meta_blocks
, blk
)) {
3610 if (fix_problem(ctx
, PR_1_BB_FS_BLOCK
, pctx
)) {
3612 return BLOCK_CHANGED
;
3614 } else if (ext2fs_test_block_bitmap2(ctx
->block_found_map
,
3617 if (fix_problem(ctx
, PR_1_BBINODE_BAD_METABLOCK
,
3620 return BLOCK_CHANGED
;
3622 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
3625 mark_block_used(ctx
, blk
);
3629 printf ("DEBUG: Marking %u as bad.\n", blk
);
3631 ctx
->fs_badblocks_count
++;
3633 * If the block is not used, then mark it as used and return.
3634 * If it is already marked as found, this must mean that
3635 * there's an overlap between the filesystem table blocks
3636 * (bitmaps and inode table) and the bad block list.
3638 if (!ext2fs_test_block_bitmap2(ctx
->block_found_map
, blk
)) {
3639 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, blk
);
3643 * Try to find the where the filesystem block was used...
3645 first_block
= fs
->super
->s_first_data_block
;
3647 for (i
= 0; i
< fs
->group_desc_count
; i
++ ) {
3650 if (!ext2fs_bg_has_super(fs
, i
))
3652 if (blk
== first_block
) {
3654 if (fix_problem(ctx
,
3655 PR_1_BAD_PRIMARY_SUPERBLOCK
,
3658 return BLOCK_CHANGED
;
3662 fix_problem(ctx
, PR_1_BAD_SUPERBLOCK
, pctx
);
3665 if ((blk
> first_block
) &&
3666 (blk
<= first_block
+ fs
->desc_blocks
)) {
3668 pctx
->blk
= *block_nr
;
3669 if (fix_problem(ctx
,
3670 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR
, pctx
)) {
3672 return BLOCK_CHANGED
;
3676 fix_problem(ctx
, PR_1_BAD_GROUP_DESCRIPTORS
, pctx
);
3680 if (blk
== ext2fs_block_bitmap_loc(fs
, i
)) {
3681 if (fix_problem(ctx
, PR_1_BB_BAD_BLOCK
, pctx
)) {
3682 ctx
->invalid_block_bitmap_flag
[i
]++;
3683 ctx
->invalid_bitmaps
++;
3687 if (blk
== ext2fs_inode_bitmap_loc(fs
, i
)) {
3688 if (fix_problem(ctx
, PR_1_IB_BAD_BLOCK
, pctx
)) {
3689 ctx
->invalid_inode_bitmap_flag
[i
]++;
3690 ctx
->invalid_bitmaps
++;
3694 if ((blk
>= ext2fs_inode_table_loc(fs
, i
)) &&
3695 (blk
< (ext2fs_inode_table_loc(fs
, i
) +
3696 fs
->inode_blocks_per_group
))) {
3698 * If there are bad blocks in the inode table,
3699 * the inode scan code will try to do
3700 * something reasonable automatically.
3704 first_block
+= fs
->super
->s_blocks_per_group
;
3707 * If we've gotten to this point, then the only
3708 * possibility is that the bad block inode meta data
3709 * is using a bad block.
3711 if ((blk
== p
->inode
->i_block
[EXT2_IND_BLOCK
]) ||
3712 (blk
== p
->inode
->i_block
[EXT2_DIND_BLOCK
]) ||
3713 (blk
== p
->inode
->i_block
[EXT2_TIND_BLOCK
])) {
3715 if (fix_problem(ctx
, PR_1_BBINODE_BAD_METABLOCK
, pctx
)) {
3717 return BLOCK_CHANGED
;
3719 if (ctx
->flags
& E2F_FLAG_SIGNAL_MASK
)
3726 /* Warn user that the block wasn't claimed */
3727 fix_problem(ctx
, PR_1_PROGERR_CLAIMED_BLOCK
, pctx
);
3732 static void new_table_block(e2fsck_t ctx
, blk64_t first_block
, dgrp_t group
,
3733 const char *name
, int num
, blk64_t
*new_block
)
3735 ext2_filsys fs
= ctx
->fs
;
3737 blk64_t old_block
= *new_block
;
3740 unsigned flexbg_size
;
3743 struct problem_context pctx
;
3745 clear_problem_context(&pctx
);
3748 pctx
.blk
= old_block
;
3752 * For flex_bg filesystems, first try to allocate the metadata
3753 * within the flex_bg, and if that fails then try finding the
3754 * space anywhere in the filesystem.
3756 is_flexbg
= ext2fs_has_feature_flex_bg(fs
->super
);
3758 flexbg_size
= 1 << fs
->super
->s_log_groups_per_flex
;
3759 flexbg
= group
/ flexbg_size
;
3760 first_block
= ext2fs_group_first_block2(fs
,
3761 flexbg_size
* flexbg
);
3762 last_grp
= group
| (flexbg_size
- 1);
3763 if (last_grp
>= fs
->group_desc_count
)
3764 last_grp
= fs
->group_desc_count
- 1;
3765 last_block
= ext2fs_group_last_block2(fs
, last_grp
);
3767 last_block
= ext2fs_group_last_block2(fs
, group
);
3768 pctx
.errcode
= ext2fs_get_free_blocks2(fs
, first_block
, last_block
,
3769 num
, ctx
->block_found_map
,
3771 if (is_flexbg
&& (pctx
.errcode
== EXT2_ET_BLOCK_ALLOC_FAIL
))
3772 pctx
.errcode
= ext2fs_get_free_blocks2(fs
,
3773 fs
->super
->s_first_data_block
,
3774 ext2fs_blocks_count(fs
->super
),
3775 num
, ctx
->block_found_map
, new_block
);
3778 fix_problem(ctx
, PR_1_RELOC_BLOCK_ALLOCATE
, &pctx
);
3779 ext2fs_unmark_valid(fs
);
3780 ctx
->flags
|= E2F_FLAG_ABORT
;
3783 pctx
.errcode
= ext2fs_get_mem(fs
->blocksize
, &buf
);
3785 fix_problem(ctx
, PR_1_RELOC_MEMORY_ALLOCATE
, &pctx
);
3786 ext2fs_unmark_valid(fs
);
3787 ctx
->flags
|= E2F_FLAG_ABORT
;
3790 ext2fs_mark_super_dirty(fs
);
3791 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
3792 pctx
.blk2
= *new_block
;
3793 fix_problem(ctx
, (old_block
? PR_1_RELOC_FROM_TO
:
3794 PR_1_RELOC_TO
), &pctx
);
3796 for (i
= 0; i
< num
; i
++) {
3798 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, (*new_block
)+i
);
3800 pctx
.errcode
= io_channel_read_blk64(fs
->io
,
3801 old_block
+ i
, 1, buf
);
3803 fix_problem(ctx
, PR_1_RELOC_READ_ERR
, &pctx
);
3804 pctx
.blk
= (*new_block
) + i
;
3805 pctx
.errcode
= io_channel_write_blk64(fs
->io
, pctx
.blk
,
3808 pctx
.blk
= (*new_block
) + i
;
3809 pctx
.errcode
= ext2fs_zero_blocks2(fs
, pctx
.blk
, 1,
3814 fix_problem(ctx
, PR_1_RELOC_WRITE_ERR
, &pctx
);
3816 ext2fs_free_mem(&buf
);
3820 * This routine gets called at the end of pass 1 if bad blocks are
3821 * detected in the superblock, group descriptors, inode_bitmaps, or
3822 * block bitmaps. At this point, all of the blocks have been mapped
3823 * out, so we can try to allocate new block(s) to replace the bad
3826 static void handle_fs_bad_blocks(e2fsck_t ctx
)
3828 ext2_filsys fs
= ctx
->fs
;
3830 blk64_t first_block
;
3833 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
3834 first_block
= ext2fs_group_first_block2(fs
, i
);
3836 if (ctx
->invalid_block_bitmap_flag
[i
]) {
3837 new_blk
= ext2fs_block_bitmap_loc(fs
, i
);
3838 new_table_block(ctx
, first_block
, i
, _("block bitmap"),
3840 ext2fs_block_bitmap_loc_set(fs
, i
, new_blk
);
3842 if (ctx
->invalid_inode_bitmap_flag
[i
]) {
3843 new_blk
= ext2fs_inode_bitmap_loc(fs
, i
);
3844 new_table_block(ctx
, first_block
, i
, _("inode bitmap"),
3846 ext2fs_inode_bitmap_loc_set(fs
, i
, new_blk
);
3848 if (ctx
->invalid_inode_table_flag
[i
]) {
3849 new_blk
= ext2fs_inode_table_loc(fs
, i
);
3850 new_table_block(ctx
, first_block
, i
, _("inode table"),
3851 fs
->inode_blocks_per_group
,
3853 ext2fs_inode_table_loc_set(fs
, i
, new_blk
);
3854 ctx
->flags
|= E2F_FLAG_RESTART
;
3857 ctx
->invalid_bitmaps
= 0;
3861 * This routine marks all blocks which are used by the superblock,
3862 * group descriptors, inode bitmaps, and block bitmaps.
3864 static void mark_table_blocks(e2fsck_t ctx
)
3866 ext2_filsys fs
= ctx
->fs
;
3870 struct problem_context pctx
;
3872 clear_problem_context(&pctx
);
3874 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
3877 ext2fs_reserve_super_and_bgd(fs
, i
, ctx
->block_found_map
);
3878 ext2fs_reserve_super_and_bgd(fs
, i
, ctx
->block_metadata_map
);
3881 * Mark the blocks used for the inode table
3883 if (ext2fs_inode_table_loc(fs
, i
)) {
3884 for (j
= 0, b
= ext2fs_inode_table_loc(fs
, i
);
3885 j
< fs
->inode_blocks_per_group
;
3887 if (ext2fs_test_block_bitmap2(ctx
->block_found_map
,
3890 if (!ctx
->invalid_inode_table_flag
[i
] &&
3892 PR_1_ITABLE_CONFLICT
, &pctx
)) {
3893 ctx
->invalid_inode_table_flag
[i
]++;
3894 ctx
->invalid_bitmaps
++;
3897 ext2fs_mark_block_bitmap2(
3898 ctx
->block_found_map
, b
);
3899 ext2fs_mark_block_bitmap2(
3900 ctx
->block_metadata_map
, b
);
3906 * Mark block used for the block bitmap
3908 if (ext2fs_block_bitmap_loc(fs
, i
)) {
3909 if (ext2fs_test_block_bitmap2(ctx
->block_found_map
,
3910 ext2fs_block_bitmap_loc(fs
, i
))) {
3911 pctx
.blk
= ext2fs_block_bitmap_loc(fs
, i
);
3912 if (fix_problem(ctx
, PR_1_BB_CONFLICT
, &pctx
)) {
3913 ctx
->invalid_block_bitmap_flag
[i
]++;
3914 ctx
->invalid_bitmaps
++;
3917 ext2fs_mark_block_bitmap2(ctx
->block_found_map
,
3918 ext2fs_block_bitmap_loc(fs
, i
));
3919 ext2fs_mark_block_bitmap2(ctx
->block_metadata_map
,
3920 ext2fs_block_bitmap_loc(fs
, i
));
3924 * Mark block used for the inode bitmap
3926 if (ext2fs_inode_bitmap_loc(fs
, i
)) {
3927 if (ext2fs_test_block_bitmap2(ctx
->block_found_map
,
3928 ext2fs_inode_bitmap_loc(fs
, i
))) {
3929 pctx
.blk
= ext2fs_inode_bitmap_loc(fs
, i
);
3930 if (fix_problem(ctx
, PR_1_IB_CONFLICT
, &pctx
)) {
3931 ctx
->invalid_inode_bitmap_flag
[i
]++;
3932 ctx
->invalid_bitmaps
++;
3935 ext2fs_mark_block_bitmap2(ctx
->block_metadata_map
,
3936 ext2fs_inode_bitmap_loc(fs
, i
));
3937 ext2fs_mark_block_bitmap2(ctx
->block_found_map
,
3938 ext2fs_inode_bitmap_loc(fs
, i
));
3945 * Thes subroutines short circuits ext2fs_get_blocks and
3946 * ext2fs_check_directory; we use them since we already have the inode
3947 * structure, so there's no point in letting the ext2fs library read
3950 static errcode_t
pass1_get_blocks(ext2_filsys fs
, ext2_ino_t ino
,
3953 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
3956 if ((ino
!= ctx
->stashed_ino
) || !ctx
->stashed_inode
)
3957 return EXT2_ET_CALLBACK_NOTHANDLED
;
3959 for (i
=0; i
< EXT2_N_BLOCKS
; i
++)
3960 blocks
[i
] = ctx
->stashed_inode
->i_block
[i
];
3964 static errcode_t
pass1_read_inode(ext2_filsys fs
, ext2_ino_t ino
,
3965 struct ext2_inode
*inode
)
3967 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
3969 if ((ino
!= ctx
->stashed_ino
) || !ctx
->stashed_inode
)
3970 return EXT2_ET_CALLBACK_NOTHANDLED
;
3971 *inode
= *ctx
->stashed_inode
;
3975 static errcode_t
pass1_write_inode(ext2_filsys fs
, ext2_ino_t ino
,
3976 struct ext2_inode
*inode
)
3978 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
3980 if ((ino
== ctx
->stashed_ino
) && ctx
->stashed_inode
&&
3981 (inode
!= ctx
->stashed_inode
))
3982 *ctx
->stashed_inode
= *inode
;
3983 return EXT2_ET_CALLBACK_NOTHANDLED
;
3986 static errcode_t
pass1_check_directory(ext2_filsys fs
, ext2_ino_t ino
)
3988 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
3990 if ((ino
!= ctx
->stashed_ino
) || !ctx
->stashed_inode
)
3991 return EXT2_ET_CALLBACK_NOTHANDLED
;
3993 if (!LINUX_S_ISDIR(ctx
->stashed_inode
->i_mode
))
3994 return EXT2_ET_NO_DIRECTORY
;
3998 static errcode_t
e2fsck_get_alloc_block(ext2_filsys fs
, blk64_t goal
,
4001 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
4005 if (ctx
->block_found_map
) {
4006 retval
= ext2fs_new_block2(fs
, goal
, ctx
->block_found_map
,
4010 if (fs
->block_map
) {
4011 ext2fs_mark_block_bitmap2(fs
->block_map
, new_block
);
4012 ext2fs_mark_bb_dirty(fs
);
4015 if (!fs
->block_map
) {
4016 retval
= ext2fs_read_block_bitmap(fs
);
4021 retval
= ext2fs_new_block2(fs
, goal
, fs
->block_map
, &new_block
);
4030 static errcode_t
e2fsck_new_range(ext2_filsys fs
, int flags
, blk64_t goal
,
4031 blk64_t len
, blk64_t
*pblk
, blk64_t
*plen
)
4033 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
4036 if (ctx
->block_found_map
)
4037 return ext2fs_new_range(fs
, flags
, goal
, len
,
4038 ctx
->block_found_map
, pblk
, plen
);
4040 if (!fs
->block_map
) {
4041 retval
= ext2fs_read_block_bitmap(fs
);
4046 return ext2fs_new_range(fs
, flags
, goal
, len
, fs
->block_map
,
4050 static void e2fsck_block_alloc_stats(ext2_filsys fs
, blk64_t blk
, int inuse
)
4052 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
4054 /* Never free a critical metadata block */
4055 if (ctx
->block_found_map
&&
4056 ctx
->block_metadata_map
&&
4058 ext2fs_test_block_bitmap2(ctx
->block_metadata_map
, blk
))
4061 if (ctx
->block_found_map
) {
4063 ext2fs_mark_block_bitmap2(ctx
->block_found_map
, blk
);
4065 ext2fs_unmark_block_bitmap2(ctx
->block_found_map
, blk
);
4069 static void e2fsck_block_alloc_stats_range(ext2_filsys fs
, blk64_t blk
,
4070 blk_t num
, int inuse
)
4072 e2fsck_t ctx
= (e2fsck_t
) fs
->priv_data
;
4074 /* Never free a critical metadata block */
4075 if (ctx
->block_found_map
&&
4076 ctx
->block_metadata_map
&&
4078 ext2fs_test_block_bitmap_range2(ctx
->block_metadata_map
, blk
, num
))
4081 if (ctx
->block_found_map
) {
4083 ext2fs_mark_block_bitmap_range2(ctx
->block_found_map
,
4086 ext2fs_unmark_block_bitmap_range2(ctx
->block_found_map
,
4091 void e2fsck_use_inode_shortcuts(e2fsck_t ctx
, int use_shortcuts
)
4093 ext2_filsys fs
= ctx
->fs
;
4095 if (use_shortcuts
) {
4096 fs
->get_blocks
= pass1_get_blocks
;
4097 fs
->check_directory
= pass1_check_directory
;
4098 fs
->read_inode
= pass1_read_inode
;
4099 fs
->write_inode
= pass1_write_inode
;
4100 ctx
->stashed_ino
= 0;
4103 fs
->check_directory
= 0;
4105 fs
->write_inode
= 0;
4109 void e2fsck_intercept_block_allocations(e2fsck_t ctx
)
4111 ext2fs_set_alloc_block_callback(ctx
->fs
, e2fsck_get_alloc_block
, 0);
4112 ext2fs_set_block_alloc_stats_callback(ctx
->fs
,
4113 e2fsck_block_alloc_stats
, 0);
4114 ext2fs_set_new_range_callback(ctx
->fs
, e2fsck_new_range
, NULL
);
4115 ext2fs_set_block_alloc_stats_range_callback(ctx
->fs
,
4116 e2fsck_block_alloc_stats_range
, NULL
);