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
48 #ifdef NO_INLINE_FUNCS
51 #define _INLINE_ inline
54 static int process_block(ext2_filsys fs, blk_t *blocknr,
55 e2_blkcnt_t blockcnt, blk_t ref_blk,
56 int ref_offset, void *priv_data);
57 static int process_bad_block(ext2_filsys fs, blk_t *block_nr,
58 e2_blkcnt_t blockcnt, blk_t ref_blk,
59 int ref_offset, void *priv_data);
60 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
62 static void mark_table_blocks(e2fsck_t ctx);
63 static void alloc_bad_map(e2fsck_t ctx);
64 static void alloc_bb_map(e2fsck_t ctx);
65 static void alloc_imagic_map(e2fsck_t ctx);
66 static void handle_fs_bad_blocks(e2fsck_t ctx);
67 static void process_inodes(e2fsck_t ctx, char *block_buf);
68 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
69 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
70 dgrp_t group, void * priv_data);
71 /* static char *describe_illegal_block(ext2_filsys fs, blk_t block); */
73 struct process_block_struct {
75 int is_dir:1, clear:1, suppress:1, fragmented:1;
77 e2_blkcnt_t last_block;
78 int num_illegal_blocks;
80 struct ext2_inode *inode;
81 struct problem_context *pctx;
85 struct process_inode_block {
87 struct ext2_inode inode;
90 struct scan_callback_struct {
96 * For the inodes to process list.
98 static struct process_inode_block *inodes_to_process;
99 static int process_inode_count;
101 static __u64 ext2_max_sizes[4];
104 * Free all memory allocated by pass1 in preparation for restarting
107 static void unwind_pass1(ext2_filsys fs)
109 ext2fs_free_mem((void **) &inodes_to_process);
110 inodes_to_process = 0;
114 * Check to make sure a device inode is real. Returns 1 if the device
115 * checks out, 0 if not.
117 * Note: this routine is now also used to check FIFO's and Sockets,
118 * since they have the same requirement; the i_block fields should be
121 int e2fsck_pass1_check_device_inode(struct ext2_inode *inode)
126 * We should be able to do the test below all the time, but
127 * because the kernel doesn't forcibly clear the device
128 * inode's additional i_block fields, there are some rare
129 * occasions when a legitimate device inode will have non-zero
130 * additional i_block fields. So for now, we only complain
131 * when the immutable flag is set, which should never happen
132 * for devices. (And that's when the problem is caused, since
133 * you can't set or clear immutable flags for devices.) Once
134 * the kernel has been fixed we can change this...
136 if (inode->i_flags & EXT2_IMMUTABLE_FL) {
137 for (i=4; i < EXT2_N_BLOCKS; i++)
138 if (inode->i_block[i])
145 * If the immutable flag is set on the inode, offer to clear it.
147 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
149 if (!(pctx->inode->i_flags & EXT2_IMMUTABLE_FL))
152 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
155 pctx->inode->i_flags &= ~EXT2_IMMUTABLE_FL;
156 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
160 void e2fsck_pass1(e2fsck_t ctx)
164 ext2_filsys fs = ctx->fs;
166 struct ext2_inode inode;
167 ext2_inode_scan scan;
169 #ifdef RESOURCE_TRACK
170 struct resource_track rtrack;
172 unsigned char frag, fsize;
173 struct problem_context pctx;
174 struct scan_callback_struct scan_struct;
175 struct ext2fs_sb *sb;
178 #ifdef RESOURCE_TRACK
179 init_resource_track(&rtrack);
181 clear_problem_context(&pctx);
183 if (!(ctx->options & E2F_OPT_PREEN))
184 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
187 mtrace_print("Pass 1");
190 #define EXT2_BPP(bits) (1UL << ((bits) - 2))
192 for (i=0; i < 4; i++) {
193 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(10+i);
194 max_sizes = max_sizes + EXT2_BPP(10+i) * EXT2_BPP(10+i);
195 max_sizes = (max_sizes +
196 (__u64) EXT2_BPP(10+i) * EXT2_BPP(10+i) *
198 max_sizes = (max_sizes * (1UL << (10+i))) - 1;
199 ext2_max_sizes[i] = max_sizes;
203 sb = (struct ext2fs_sb *) fs->super;
204 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
207 * Allocate bitmaps structures
209 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
210 &ctx->inode_used_map);
213 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
214 ctx->flags |= E2F_FLAG_ABORT;
217 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
218 _("directory inode map"), &ctx->inode_dir_map);
221 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
222 ctx->flags |= E2F_FLAG_ABORT;
225 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
226 _("regular file inode map"), &ctx->inode_reg_map);
229 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
230 ctx->flags |= E2F_FLAG_ABORT;
233 pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
234 &ctx->block_found_map);
237 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
238 ctx->flags |= E2F_FLAG_ABORT;
241 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
242 &ctx->inode_link_info);
244 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
245 ctx->flags |= E2F_FLAG_ABORT;
248 inodes_to_process = (struct process_inode_block *)
249 e2fsck_allocate_memory(ctx,
250 (ctx->process_inode_size *
251 sizeof(struct process_inode_block)),
252 "array of inodes to process");
253 process_inode_count = 0;
255 pctx.errcode = ext2fs_init_dblist(fs, 0);
257 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
258 ctx->flags |= E2F_FLAG_ABORT;
262 mark_table_blocks(ctx);
263 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
264 "block interate buffer");
265 e2fsck_use_inode_shortcuts(ctx, 1);
266 ehandler_operation(_("doing inode scan"));
267 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
270 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
271 ctx->flags |= E2F_FLAG_ABORT;
274 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
275 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
277 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
278 ctx->flags |= E2F_FLAG_ABORT;
281 ctx->stashed_inode = &inode;
282 scan_struct.ctx = ctx;
283 scan_struct.block_buf = block_buf;
284 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
286 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
291 ctx->stashed_ino = ino;
292 if (inode.i_links_count) {
293 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
294 ino, inode.i_links_count);
296 pctx.num = inode.i_links_count;
297 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
298 ctx->flags |= E2F_FLAG_ABORT;
302 if (ino == EXT2_BAD_INO) {
303 struct process_block_struct pb;
305 pb.ino = EXT2_BAD_INO;
306 pb.num_blocks = pb.last_block = 0;
307 pb.num_illegal_blocks = 0;
308 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
313 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0,
314 block_buf, process_bad_block, &pb);
316 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
317 ctx->flags |= E2F_FLAG_ABORT;
320 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
321 clear_problem_context(&pctx);
324 if (ino == EXT2_ROOT_INO) {
326 * Make sure the root inode is a directory; if
327 * not, offer to clear it. It will be
328 * regnerated in pass #3.
330 if (!LINUX_S_ISDIR(inode.i_mode)) {
331 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
332 inode.i_dtime = time(0);
333 inode.i_links_count = 0;
334 ext2fs_icount_store(ctx->inode_link_info,
336 e2fsck_write_inode(ctx, ino, &inode,
341 * If dtime is set, offer to clear it. mke2fs
342 * version 0.2b created filesystems with the
343 * dtime field set for the root and lost+found
344 * directories. We won't worry about
345 * /lost+found, since that can be regenerated
346 * easily. But we will fix the root directory
349 if (inode.i_dtime && inode.i_links_count) {
350 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
352 e2fsck_write_inode(ctx, ino, &inode,
357 if ((ino != EXT2_ROOT_INO) &&
358 (ino < EXT2_FIRST_INODE(fs->super))) {
359 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
360 if (((ino == EXT2_BOOT_LOADER_INO) &&
361 LINUX_S_ISDIR(inode.i_mode)) ||
362 ((ino != EXT2_BOOT_LOADER_INO) &&
363 (inode.i_mode != 0))) {
365 PR_1_RESERVED_BAD_MODE, &pctx)) {
367 e2fsck_write_inode(ctx, ino, &inode,
371 check_blocks(ctx, &pctx, block_buf);
375 * This code assumes that deleted inodes have
376 * i_links_count set to 0.
378 if (!inode.i_links_count) {
379 if (!inode.i_dtime && inode.i_mode) {
381 PR_1_ZERO_DTIME, &pctx)) {
382 inode.i_dtime = time(0);
383 e2fsck_write_inode(ctx, ino, &inode,
390 * n.b. 0.3c ext2fs code didn't clear i_links_count for
391 * deleted files. Oops.
393 * Since all new ext2 implementations get this right,
394 * we now assume that the case of non-zero
395 * i_links_count and non-zero dtime means that we
396 * should keep the file, not delete it.
400 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
402 e2fsck_write_inode(ctx, ino, &inode, "pass1");
406 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
407 switch (fs->super->s_creator_os) {
409 frag = inode.osd2.linux2.l_i_frag;
410 fsize = inode.osd2.linux2.l_i_fsize;
413 frag = inode.osd2.hurd2.h_i_frag;
414 fsize = inode.osd2.hurd2.h_i_fsize;
417 frag = inode.osd2.masix2.m_i_frag;
418 fsize = inode.osd2.masix2.m_i_fsize;
424 if (inode.i_faddr || frag || fsize
425 || inode.i_file_acl ||
426 (LINUX_S_ISDIR(inode.i_mode) && inode.i_dir_acl)) {
427 if (!ctx->inode_bad_map)
429 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
431 if (inode.i_flags & EXT2_IMAGIC_FL) {
433 if (!ctx->inode_imagic_map)
434 alloc_imagic_map(ctx);
435 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
438 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
439 inode.i_flags &= ~EXT2_IMAGIC_FL;
440 e2fsck_write_inode(ctx, ino,
446 if (LINUX_S_ISDIR(inode.i_mode)) {
447 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
448 e2fsck_add_dir_info(ctx, ino, 0);
449 ctx->fs_directory_count++;
450 } else if (LINUX_S_ISREG (inode.i_mode)) {
451 ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
452 ctx->fs_regular_count++;
453 } else if (LINUX_S_ISCHR (inode.i_mode) &&
454 e2fsck_pass1_check_device_inode(&inode)) {
455 check_immutable(ctx, &pctx);
456 ctx->fs_chardev_count++;
457 } else if (LINUX_S_ISBLK (inode.i_mode) &&
458 e2fsck_pass1_check_device_inode(&inode)) {
459 check_immutable(ctx, &pctx);
460 ctx->fs_blockdev_count++;
461 } else if (LINUX_S_ISLNK (inode.i_mode)) {
462 ctx->fs_symlinks_count++;
463 if (!inode.i_blocks) {
464 ctx->fs_fast_symlinks_count++;
468 else if (LINUX_S_ISFIFO (inode.i_mode) &&
469 e2fsck_pass1_check_device_inode(&inode)) {
470 check_immutable(ctx, &pctx);
471 ctx->fs_fifo_count++;
472 } else if ((LINUX_S_ISSOCK (inode.i_mode)) &&
473 e2fsck_pass1_check_device_inode(&inode)) {
474 check_immutable(ctx, &pctx);
475 ctx->fs_sockets_count++;
477 if (!ctx->inode_bad_map)
479 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
481 if (inode.i_block[EXT2_IND_BLOCK])
483 if (inode.i_block[EXT2_DIND_BLOCK])
484 ctx->fs_dind_count++;
485 if (inode.i_block[EXT2_TIND_BLOCK])
486 ctx->fs_tind_count++;
487 if (inode.i_block[EXT2_IND_BLOCK] ||
488 inode.i_block[EXT2_DIND_BLOCK] ||
489 inode.i_block[EXT2_TIND_BLOCK]) {
490 inodes_to_process[process_inode_count].ino = ino;
491 inodes_to_process[process_inode_count].inode = inode;
492 process_inode_count++;
494 check_blocks(ctx, &pctx, block_buf);
496 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
499 if (process_inode_count >= ctx->process_inode_size) {
500 process_inodes(ctx, block_buf);
502 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
506 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
507 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
509 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
510 if (!ctx->inode_bb_map)
512 ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
513 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
517 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
518 ctx->flags |= E2F_FLAG_ABORT;
522 process_inodes(ctx, block_buf);
523 ext2fs_close_inode_scan(scan);
524 ehandler_operation(0);
526 if (ctx->invalid_bitmaps)
527 handle_fs_bad_blocks(ctx);
529 if (ctx->flags & E2F_FLAG_RESTART) {
531 * Only the master copy of the superblock and block
532 * group descriptors are going to be written during a
533 * restart, so set the superblock to be used to be the
536 ctx->use_superblock = 0;
541 if (ctx->block_dup_map) {
542 if (ctx->options & E2F_OPT_PREEN) {
543 clear_problem_context(&pctx);
544 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
546 e2fsck_pass1_dupblocks(ctx, block_buf);
548 ext2fs_free_mem((void **) &inodes_to_process);
550 e2fsck_use_inode_shortcuts(ctx, 0);
552 ext2fs_free_mem((void **) &block_buf);
554 if (ctx->large_files &&
555 !(sb->s_feature_ro_compat &
556 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
557 if (fix_problem(ctx, PR_1_FEATURE_LARGE_FILES, &pctx)) {
558 sb->s_feature_ro_compat |=
559 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
560 ext2fs_mark_super_dirty(fs);
562 } else if (!ctx->large_files &&
563 (sb->s_feature_ro_compat &
564 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
565 if (fs->flags & EXT2_FLAG_RW) {
566 sb->s_feature_ro_compat &=
567 ~EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
568 ext2fs_mark_super_dirty(fs);
572 #ifdef RESOURCE_TRACK
573 if (ctx->options & E2F_OPT_TIME2) {
574 e2fsck_clear_progbar(ctx);
575 print_resource_track(_("Pass 1"), &rtrack);
581 * When the inode_scan routines call this callback at the end of the
582 * glock group, call process_inodes.
584 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
585 dgrp_t group, void * priv_data)
587 struct scan_callback_struct *scan_struct;
590 scan_struct = (struct scan_callback_struct *) priv_data;
591 ctx = scan_struct->ctx;
593 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
596 if ((ctx->progress)(ctx, 1, group+1,
597 ctx->fs->group_desc_count))
598 return EXT2_ET_CANCEL_REQUESTED;
604 * Process the inodes in the "inodes to process" list.
606 static void process_inodes(e2fsck_t ctx, char *block_buf)
609 struct ext2_inode *old_stashed_inode;
610 ino_t old_stashed_ino;
611 const char *old_operation;
613 struct problem_context pctx;
616 printf("begin process_inodes: ");
618 if (process_inode_count == 0)
620 old_operation = ehandler_operation(0);
621 old_stashed_inode = ctx->stashed_inode;
622 old_stashed_ino = ctx->stashed_ino;
623 qsort(inodes_to_process, process_inode_count,
624 sizeof(struct process_inode_block), process_inode_cmp);
625 clear_problem_context(&pctx);
626 for (i=0; i < process_inode_count; i++) {
627 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
628 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
631 printf("%u ", pctx.ino);
633 sprintf(buf, _("reading indirect blocks of inode %lu"),
635 ehandler_operation(buf);
636 check_blocks(ctx, &pctx, block_buf);
637 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
640 ctx->stashed_inode = old_stashed_inode;
641 ctx->stashed_ino = old_stashed_ino;
642 process_inode_count = 0;
644 printf("end process inodes\n");
646 ehandler_operation(old_operation);
649 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
651 const struct process_inode_block *ib_a =
652 (const struct process_inode_block *) a;
653 const struct process_inode_block *ib_b =
654 (const struct process_inode_block *) b;
656 return (ib_a->inode.i_block[EXT2_IND_BLOCK] -
657 ib_b->inode.i_block[EXT2_IND_BLOCK]);
661 * This procedure will allocate the inode bad map table
663 static void alloc_bad_map(e2fsck_t ctx)
665 struct problem_context pctx;
667 clear_problem_context(&pctx);
669 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs, _("bad inode map"),
670 &ctx->inode_bad_map);
673 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
674 /* Should never get here */
675 ctx->flags |= E2F_FLAG_ABORT;
681 * This procedure will allocate the inode "bb" (badblock) map table
683 static void alloc_bb_map(e2fsck_t ctx)
685 struct problem_context pctx;
687 clear_problem_context(&pctx);
688 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
689 _("inode in bad block map"),
693 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
694 /* Should never get here */
695 ctx->flags |= E2F_FLAG_ABORT;
701 * This procedure will allocate the inode imagic table
703 static void alloc_imagic_map(e2fsck_t ctx)
705 struct problem_context pctx;
707 clear_problem_context(&pctx);
708 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
709 _("imagic inode map"),
710 &ctx->inode_imagic_map);
713 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
714 /* Should never get here */
715 ctx->flags |= E2F_FLAG_ABORT;
721 * Marks a block as in use, setting the dup_map if it's been set
722 * already. Called by process_block and process_bad_block.
724 * WARNING: Assumes checks have already been done to make sure block
725 * is valid. This is true in both process_block and process_bad_block.
727 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
729 struct problem_context pctx;
731 clear_problem_context(&pctx);
733 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
734 if (!ctx->block_dup_map) {
735 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
736 _("multiply claimed block map"),
737 &ctx->block_dup_map);
740 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
742 /* Should never get here */
743 ctx->flags |= E2F_FLAG_ABORT;
747 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
749 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
754 * This subroutine is called on each inode to account for all of the
755 * blocks used by that inode.
757 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
760 ext2_filsys fs = ctx->fs;
761 struct process_block_struct pb;
762 ino_t ino = pctx->ino;
763 struct ext2_inode *inode = pctx->inode;
766 struct ext2fs_sb *sb;
768 if (!ext2fs_inode_has_valid_blocks(pctx->inode))
772 pb.num_blocks = pb.last_block = 0;
773 pb.num_illegal_blocks = 0;
774 pb.suppress = 0; pb.clear = 0;
776 pb.previous_block = 0;
777 pb.is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
782 pctx->errcode = ext2fs_block_iterate2(fs, ino,
783 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
784 block_buf, process_block, &pb);
785 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
787 end_problem_latch(ctx, PR_LATCH_BLOCK);
789 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
791 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
792 ctx->fs_fragmented++;
795 e2fsck_read_inode(ctx, ino, inode, "check_blocks");
796 inode->i_links_count = 0;
797 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
798 inode->i_dtime = time(0);
799 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
800 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
801 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
802 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
804 * The inode was probably partially accounted for
805 * before processing was aborted, so we need to
806 * restart the pass 1 scan.
808 ctx->flags |= E2F_FLAG_RESTART;
812 pb.num_blocks *= (fs->blocksize / 512);
814 printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
815 ino, inode->i_size, pb.last_block, inode->i_blocks,
818 if (!pb.num_blocks && pb.is_dir) {
819 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
820 inode->i_links_count = 0;
821 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
822 inode->i_dtime = time(0);
823 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
824 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
825 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
826 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
827 ctx->fs_directory_count--;
832 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
833 if ((nblock > (pb.last_block + 1)) ||
834 ((inode->i_size & (fs->blocksize-1)) != 0))
836 else if (nblock < (pb.last_block + 1)) {
837 sb = (struct ext2fs_sb *) fs->super;
838 if (((pb.last_block + 1) - nblock) >
839 sb->s_prealloc_dir_blocks)
843 size = inode->i_size + ((__u64) inode->i_size_high << 32);
844 if ((size < pb.last_block * fs->blocksize))
846 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
850 pctx->num = (pb.last_block+1) * fs->blocksize;
851 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
852 inode->i_size = pctx->num;
854 inode->i_size_high = pctx->num >> 32;
855 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
859 if (!pb.is_dir && inode->i_size_high)
861 if (pb.num_blocks != inode->i_blocks) {
862 pctx->num = pb.num_blocks;
863 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
864 inode->i_blocks = pb.num_blocks;
865 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
873 * Helper function called by process block when an illegal block is
874 * found. It returns a description about why the block is illegal
876 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
880 static char problem[80];
882 super = fs->super->s_first_data_block;
883 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
885 sprintf(problem, "< FIRSTBLOCK (%u)", super);
887 } else if (block >= fs->super->s_blocks_count) {
888 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
891 for (i = 0; i < fs->group_desc_count; i++) {
892 if (block == super) {
893 sprintf(problem, "is the superblock in group %d", i);
897 block <= (super + fs->desc_blocks)) {
898 sprintf(problem, "is in the group descriptors "
902 if (block == fs->group_desc[i].bg_block_bitmap) {
903 sprintf(problem, "is the block bitmap of group %d", i);
906 if (block == fs->group_desc[i].bg_inode_bitmap) {
907 sprintf(problem, "is the inode bitmap of group %d", i);
910 if (block >= fs->group_desc[i].bg_inode_table &&
911 (block < fs->group_desc[i].bg_inode_table
912 + fs->inode_blocks_per_group)) {
913 sprintf(problem, "is in the inode table of group %d",
917 super += fs->super->s_blocks_per_group;
924 * This is a helper function for check_blocks().
926 int process_block(ext2_filsys fs,
928 e2_blkcnt_t blockcnt,
933 struct process_block_struct *p;
934 struct problem_context *pctx;
935 blk_t blk = *block_nr;
940 p = (struct process_block_struct *) priv_data;
945 if (p->is_dir == 0) {
947 * Should never happen, since only directories
948 * get called with BLOCK_FLAG_HOLE
951 printf("process_block() called with blk == 0, "
952 "blockcnt=%d, inode %lu???\n",
959 if (blockcnt * fs->blocksize < p->inode->i_size) {
961 printf("Missing block (#%d) in directory inode %lu!\n",
970 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
975 * Simplistic fragmentation check. We merely require that the
976 * file be contiguous. (Which can never be true for really
977 * big files that are greater than a block group.)
979 if (p->previous_block) {
980 if (p->previous_block+1 != blk)
983 p->previous_block = blk;
985 if (blk < fs->super->s_first_data_block ||
986 blk >= fs->super->s_blocks_count)
987 problem = PR_1_ILLEGAL_BLOCK_NUM;
990 p->num_illegal_blocks++;
991 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
992 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
996 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
998 set_latch_flags(PR_LATCH_BLOCK,
1003 pctx->blkcount = blockcnt;
1004 if (fix_problem(ctx, problem, pctx)) {
1005 blk = *block_nr = 0;
1006 ret_code = BLOCK_CHANGED;
1011 pctx->blkcount = -1;
1014 mark_block_used(ctx, blk);
1017 p->last_block = blockcnt;
1019 if (p->is_dir && (blockcnt >= 0)) {
1020 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1022 if (pctx->errcode) {
1024 pctx->num = blockcnt;
1025 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1026 /* Should never get here */
1027 ctx->flags |= E2F_FLAG_ABORT;
1034 static void bad_block_indirect(e2fsck_t ctx, blk_t blk)
1036 struct problem_context pctx;
1038 clear_problem_context(&pctx);
1040 * Prompt to see if we should continue or not.
1042 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, &pctx))
1043 ctx->flags |= E2F_FLAG_ABORT;
1046 int process_bad_block(ext2_filsys fs,
1048 e2_blkcnt_t blockcnt,
1053 struct process_block_struct *p;
1054 blk_t blk = *block_nr;
1057 struct problem_context *pctx;
1063 p = (struct process_block_struct *) priv_data;
1067 pctx->ino = EXT2_BAD_INO;
1069 pctx->blkcount = blockcnt;
1071 if ((blk < fs->super->s_first_data_block) ||
1072 (blk >= fs->super->s_blocks_count)) {
1073 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1075 return BLOCK_CHANGED;
1081 if (ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1082 bad_block_indirect(ctx, blk);
1083 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1086 mark_block_used(ctx, blk);
1090 printf ("DEBUG: Marking %u as bad.\n", blk);
1092 ctx->fs_badblocks_count++;
1094 * If the block is not used, then mark it as used and return.
1095 * If it is already marked as found, this must mean that
1096 * there's an overlap between the filesystem table blocks
1097 * (bitmaps and inode table) and the bad block list.
1099 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1100 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1104 * Try to find the where the filesystem block was used...
1106 first_block = fs->super->s_first_data_block;
1108 for (i = 0; i < fs->group_desc_count; i++ ) {
1111 if (!ext2fs_bg_has_super(fs, i))
1113 if (blk == first_block) {
1115 if (fix_problem(ctx,
1116 PR_1_BAD_PRIMARY_SUPERBLOCK,
1119 return BLOCK_CHANGED;
1123 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1126 if ((blk > first_block) &&
1127 (blk <= first_block + fs->desc_blocks)) {
1129 pctx->blk = *block_nr;
1130 if (fix_problem(ctx,
1131 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1133 return BLOCK_CHANGED;
1137 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1141 if (blk == fs->group_desc[i].bg_block_bitmap) {
1142 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1143 ctx->invalid_block_bitmap_flag[i]++;
1144 ctx->invalid_bitmaps++;
1148 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1149 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1150 ctx->invalid_inode_bitmap_flag[i]++;
1151 ctx->invalid_bitmaps++;
1155 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1156 (blk < (fs->group_desc[i].bg_inode_table +
1157 fs->inode_blocks_per_group))) {
1159 * If there are bad blocks in the inode table,
1160 * the inode scan code will try to do
1161 * something reasonable automatically.
1165 first_block += fs->super->s_blocks_per_group;
1168 * If we've gotten to this point, then the only
1169 * possibility is that the bad block inode meta data
1170 * is using a bad block.
1172 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1173 p->inode->i_block[EXT2_DIND_BLOCK]) {
1174 bad_block_indirect(ctx, blk);
1175 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1182 /* Warn user that the block wasn't claimed */
1183 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1188 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
1189 const char *name, int num, blk_t *new_block)
1191 ext2_filsys fs = ctx->fs;
1192 blk_t old_block = *new_block;
1195 struct problem_context pctx;
1197 clear_problem_context(&pctx);
1200 pctx.blk = old_block;
1203 pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
1204 first_block + fs->super->s_blocks_per_group,
1205 num, ctx->block_found_map, new_block);
1208 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
1209 ext2fs_unmark_valid(fs);
1212 pctx.errcode = ext2fs_get_mem(fs->blocksize, (void **) &buf);
1214 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
1215 ext2fs_unmark_valid(fs);
1218 ext2fs_mark_super_dirty(fs);
1219 pctx.blk2 = *new_block;
1220 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
1221 PR_1_RELOC_TO), &pctx);
1223 for (i = 0; i < num; i++) {
1225 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
1227 pctx.errcode = io_channel_read_blk(fs->io,
1228 old_block + i, 1, buf);
1230 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
1232 memset(buf, 0, fs->blocksize);
1234 pctx.blk = (*new_block) + i;
1235 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
1238 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
1240 ext2fs_free_mem((void **) &buf);
1244 * This routine gets called at the end of pass 1 if bad blocks are
1245 * detected in the superblock, group descriptors, inode_bitmaps, or
1246 * block bitmaps. At this point, all of the blocks have been mapped
1247 * out, so we can try to allocate new block(s) to replace the bad
1250 static void handle_fs_bad_blocks(e2fsck_t ctx)
1252 ext2_filsys fs = ctx->fs;
1254 int first_block = fs->super->s_first_data_block;
1256 for (i = 0; i < fs->group_desc_count; i++) {
1257 if (ctx->invalid_block_bitmap_flag[i]) {
1258 new_table_block(ctx, first_block, i, _("block bitmap"),
1259 1, &fs->group_desc[i].bg_block_bitmap);
1261 if (ctx->invalid_inode_bitmap_flag[i]) {
1262 new_table_block(ctx, first_block, i, _("inode bitmap"),
1263 1, &fs->group_desc[i].bg_inode_bitmap);
1265 if (ctx->invalid_inode_table_flag[i]) {
1266 new_table_block(ctx, first_block, i, _("inode table"),
1267 fs->inode_blocks_per_group,
1268 &fs->group_desc[i].bg_inode_table);
1269 ctx->flags |= E2F_FLAG_RESTART;
1271 first_block += fs->super->s_blocks_per_group;
1273 ctx->invalid_bitmaps = 0;
1277 * This routine marks all blocks which are used by the superblock,
1278 * group descriptors, inode bitmaps, and block bitmaps.
1280 static void mark_table_blocks(e2fsck_t ctx)
1282 ext2_filsys fs = ctx->fs;
1285 struct problem_context pctx;
1287 clear_problem_context(&pctx);
1289 block = fs->super->s_first_data_block;
1290 for (i = 0; i < fs->group_desc_count; i++) {
1293 if (ext2fs_bg_has_super(fs, i)) {
1295 * Mark this group's copy of the superblock
1297 ext2fs_mark_block_bitmap(ctx->block_found_map, block);
1300 * Mark this group's copy of the descriptors
1302 for (j = 0; j < fs->desc_blocks; j++) {
1303 ext2fs_mark_block_bitmap(ctx->block_found_map,
1309 * Mark the blocks used for the inode table
1311 if (fs->group_desc[i].bg_inode_table) {
1312 for (j = 0, b = fs->group_desc[i].bg_inode_table;
1313 j < fs->inode_blocks_per_group;
1315 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1318 if (fix_problem(ctx,
1319 PR_1_ITABLE_CONFLICT, &pctx)) {
1320 ctx->invalid_inode_table_flag[i]++;
1321 ctx->invalid_bitmaps++;
1324 ext2fs_mark_block_bitmap(ctx->block_found_map,
1331 * Mark block used for the block bitmap
1333 if (fs->group_desc[i].bg_block_bitmap) {
1334 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1335 fs->group_desc[i].bg_block_bitmap)) {
1336 pctx.blk = fs->group_desc[i].bg_block_bitmap;
1337 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
1338 ctx->invalid_block_bitmap_flag[i]++;
1339 ctx->invalid_bitmaps++;
1342 ext2fs_mark_block_bitmap(ctx->block_found_map,
1343 fs->group_desc[i].bg_block_bitmap);
1348 * Mark block used for the inode bitmap
1350 if (fs->group_desc[i].bg_inode_bitmap) {
1351 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1352 fs->group_desc[i].bg_inode_bitmap)) {
1353 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
1354 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
1355 ctx->invalid_inode_bitmap_flag[i]++;
1356 ctx->invalid_bitmaps++;
1359 ext2fs_mark_block_bitmap(ctx->block_found_map,
1360 fs->group_desc[i].bg_inode_bitmap);
1363 block += fs->super->s_blocks_per_group;
1368 * Thes subroutines short circuits ext2fs_get_blocks and
1369 * ext2fs_check_directory; we use them since we already have the inode
1370 * structure, so there's no point in letting the ext2fs library read
1373 static errcode_t pass1_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks)
1375 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1378 if (ino != ctx->stashed_ino)
1379 return EXT2_ET_CALLBACK_NOTHANDLED;
1381 for (i=0; i < EXT2_N_BLOCKS; i++)
1382 blocks[i] = ctx->stashed_inode->i_block[i];
1386 static errcode_t pass1_read_inode(ext2_filsys fs, ino_t ino,
1387 struct ext2_inode *inode)
1389 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1391 if (ino != ctx->stashed_ino)
1392 return EXT2_ET_CALLBACK_NOTHANDLED;
1393 *inode = *ctx->stashed_inode;
1397 static errcode_t pass1_write_inode(ext2_filsys fs, ino_t ino,
1398 struct ext2_inode *inode)
1400 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1402 if (ino == ctx->stashed_ino)
1403 *ctx->stashed_inode = *inode;
1404 return EXT2_ET_CALLBACK_NOTHANDLED;
1407 static errcode_t pass1_check_directory(ext2_filsys fs, ino_t ino)
1409 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1411 if (ino != ctx->stashed_ino)
1412 return EXT2_ET_CALLBACK_NOTHANDLED;
1414 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
1415 return EXT2_ET_NO_DIRECTORY;
1419 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
1421 ext2_filsys fs = ctx->fs;
1424 fs->get_blocks = pass1_get_blocks;
1425 fs->check_directory = pass1_check_directory;
1426 fs->read_inode = pass1_read_inode;
1427 fs->write_inode = pass1_write_inode;
1428 ctx->stashed_ino = 0;
1431 fs->check_directory = 0;
1433 fs->write_inode = 0;