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,
76 fragmented:1, compressed:1;
78 e2_blkcnt_t last_block;
79 int num_illegal_blocks;
81 struct ext2_inode *inode;
82 struct problem_context *pctx;
86 struct process_inode_block {
88 struct ext2_inode inode;
91 struct scan_callback_struct {
97 * For the inodes to process list.
99 static struct process_inode_block *inodes_to_process;
100 static int process_inode_count;
102 static __u64 ext2_max_sizes[4];
105 * Free all memory allocated by pass1 in preparation for restarting
108 static void unwind_pass1(ext2_filsys fs)
110 ext2fs_free_mem((void **) &inodes_to_process);
111 inodes_to_process = 0;
115 * Check to make sure a device inode is real. Returns 1 if the device
116 * checks out, 0 if not.
118 * Note: this routine is now also used to check FIFO's and Sockets,
119 * since they have the same requirement; the i_block fields should be
122 int e2fsck_pass1_check_device_inode(struct ext2_inode *inode)
127 * We should be able to do the test below all the time, but
128 * because the kernel doesn't forcibly clear the device
129 * inode's additional i_block fields, there are some rare
130 * occasions when a legitimate device inode will have non-zero
131 * additional i_block fields. So for now, we only complain
132 * when the immutable flag is set, which should never happen
133 * for devices. (And that's when the problem is caused, since
134 * you can't set or clear immutable flags for devices.) Once
135 * the kernel has been fixed we can change this...
137 if (inode->i_flags & EXT2_IMMUTABLE_FL) {
138 for (i=4; i < EXT2_N_BLOCKS; i++)
139 if (inode->i_block[i])
146 * If the immutable flag is set on the inode, offer to clear it.
148 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
150 if (!(pctx->inode->i_flags & EXT2_IMMUTABLE_FL))
153 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
156 pctx->inode->i_flags &= ~EXT2_IMMUTABLE_FL;
157 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
161 void e2fsck_pass1(e2fsck_t ctx)
165 ext2_filsys fs = ctx->fs;
167 struct ext2_inode inode;
168 ext2_inode_scan scan;
170 #ifdef RESOURCE_TRACK
171 struct resource_track rtrack;
173 unsigned char frag, fsize;
174 struct problem_context pctx;
175 struct scan_callback_struct scan_struct;
176 struct ext2fs_sb *sb;
179 #ifdef RESOURCE_TRACK
180 init_resource_track(&rtrack);
182 clear_problem_context(&pctx);
184 if (!(ctx->options & E2F_OPT_PREEN))
185 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
188 mtrace_print("Pass 1");
191 #define EXT2_BPP(bits) (1UL << ((bits) - 2))
193 for (i=0; i < 4; i++) {
194 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(10+i);
195 max_sizes = max_sizes + EXT2_BPP(10+i) * EXT2_BPP(10+i);
196 max_sizes = (max_sizes +
197 (__u64) EXT2_BPP(10+i) * EXT2_BPP(10+i) *
199 max_sizes = (max_sizes * (1UL << (10+i))) - 1;
200 ext2_max_sizes[i] = max_sizes;
204 sb = (struct ext2fs_sb *) fs->super;
205 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
208 * Allocate bitmaps structures
210 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
211 &ctx->inode_used_map);
214 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
215 ctx->flags |= E2F_FLAG_ABORT;
218 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
219 _("directory inode map"), &ctx->inode_dir_map);
222 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
223 ctx->flags |= E2F_FLAG_ABORT;
226 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
227 _("regular file inode map"), &ctx->inode_reg_map);
230 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
231 ctx->flags |= E2F_FLAG_ABORT;
234 pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
235 &ctx->block_found_map);
238 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
239 ctx->flags |= E2F_FLAG_ABORT;
242 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
243 &ctx->inode_link_info);
245 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
246 ctx->flags |= E2F_FLAG_ABORT;
249 inodes_to_process = (struct process_inode_block *)
250 e2fsck_allocate_memory(ctx,
251 (ctx->process_inode_size *
252 sizeof(struct process_inode_block)),
253 "array of inodes to process");
254 process_inode_count = 0;
256 pctx.errcode = ext2fs_init_dblist(fs, 0);
258 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
259 ctx->flags |= E2F_FLAG_ABORT;
263 mark_table_blocks(ctx);
264 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
265 "block interate buffer");
266 e2fsck_use_inode_shortcuts(ctx, 1);
267 ehandler_operation(_("doing inode scan"));
268 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
271 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
272 ctx->flags |= E2F_FLAG_ABORT;
275 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
276 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
278 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
279 ctx->flags |= E2F_FLAG_ABORT;
282 ctx->stashed_inode = &inode;
283 scan_struct.ctx = ctx;
284 scan_struct.block_buf = block_buf;
285 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
287 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
292 ctx->stashed_ino = ino;
293 if (inode.i_links_count) {
294 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
295 ino, inode.i_links_count);
297 pctx.num = inode.i_links_count;
298 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
299 ctx->flags |= E2F_FLAG_ABORT;
303 if (ino == EXT2_BAD_INO) {
304 struct process_block_struct pb;
306 pb.ino = EXT2_BAD_INO;
307 pb.num_blocks = pb.last_block = 0;
308 pb.num_illegal_blocks = 0;
309 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
314 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0,
315 block_buf, process_bad_block, &pb);
317 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
318 ctx->flags |= E2F_FLAG_ABORT;
321 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
322 clear_problem_context(&pctx);
325 if (ino == EXT2_ROOT_INO) {
327 * Make sure the root inode is a directory; if
328 * not, offer to clear it. It will be
329 * regnerated in pass #3.
331 if (!LINUX_S_ISDIR(inode.i_mode)) {
332 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
333 inode.i_dtime = time(0);
334 inode.i_links_count = 0;
335 ext2fs_icount_store(ctx->inode_link_info,
337 e2fsck_write_inode(ctx, ino, &inode,
342 * If dtime is set, offer to clear it. mke2fs
343 * version 0.2b created filesystems with the
344 * dtime field set for the root and lost+found
345 * directories. We won't worry about
346 * /lost+found, since that can be regenerated
347 * easily. But we will fix the root directory
350 if (inode.i_dtime && inode.i_links_count) {
351 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
353 e2fsck_write_inode(ctx, ino, &inode,
358 if ((ino != EXT2_ROOT_INO) &&
359 (ino < EXT2_FIRST_INODE(fs->super))) {
360 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
361 if (((ino == EXT2_BOOT_LOADER_INO) &&
362 LINUX_S_ISDIR(inode.i_mode)) ||
363 ((ino != EXT2_BOOT_LOADER_INO) &&
364 (inode.i_mode != 0))) {
366 PR_1_RESERVED_BAD_MODE, &pctx)) {
368 e2fsck_write_inode(ctx, ino, &inode,
372 check_blocks(ctx, &pctx, block_buf);
376 * This code assumes that deleted inodes have
377 * i_links_count set to 0.
379 if (!inode.i_links_count) {
380 if (!inode.i_dtime && inode.i_mode) {
382 PR_1_ZERO_DTIME, &pctx)) {
383 inode.i_dtime = time(0);
384 e2fsck_write_inode(ctx, ino, &inode,
391 * n.b. 0.3c ext2fs code didn't clear i_links_count for
392 * deleted files. Oops.
394 * Since all new ext2 implementations get this right,
395 * we now assume that the case of non-zero
396 * i_links_count and non-zero dtime means that we
397 * should keep the file, not delete it.
401 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
403 e2fsck_write_inode(ctx, ino, &inode, "pass1");
407 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
408 switch (fs->super->s_creator_os) {
410 frag = inode.osd2.linux2.l_i_frag;
411 fsize = inode.osd2.linux2.l_i_fsize;
414 frag = inode.osd2.hurd2.h_i_frag;
415 fsize = inode.osd2.hurd2.h_i_fsize;
418 frag = inode.osd2.masix2.m_i_frag;
419 fsize = inode.osd2.masix2.m_i_fsize;
425 if (inode.i_faddr || frag || fsize
426 || inode.i_file_acl ||
427 (LINUX_S_ISDIR(inode.i_mode) && inode.i_dir_acl)) {
428 if (!ctx->inode_bad_map)
430 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
432 if (inode.i_flags & EXT2_IMAGIC_FL) {
434 if (!ctx->inode_imagic_map)
435 alloc_imagic_map(ctx);
436 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
439 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
440 inode.i_flags &= ~EXT2_IMAGIC_FL;
441 e2fsck_write_inode(ctx, ino,
447 if (LINUX_S_ISDIR(inode.i_mode)) {
448 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
449 e2fsck_add_dir_info(ctx, ino, 0);
450 ctx->fs_directory_count++;
451 } else if (LINUX_S_ISREG (inode.i_mode)) {
452 ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
453 ctx->fs_regular_count++;
454 } else if (LINUX_S_ISCHR (inode.i_mode) &&
455 e2fsck_pass1_check_device_inode(&inode)) {
456 check_immutable(ctx, &pctx);
457 ctx->fs_chardev_count++;
458 } else if (LINUX_S_ISBLK (inode.i_mode) &&
459 e2fsck_pass1_check_device_inode(&inode)) {
460 check_immutable(ctx, &pctx);
461 ctx->fs_blockdev_count++;
462 } else if (LINUX_S_ISLNK (inode.i_mode)) {
463 ctx->fs_symlinks_count++;
464 if (!inode.i_blocks) {
465 ctx->fs_fast_symlinks_count++;
469 else if (LINUX_S_ISFIFO (inode.i_mode) &&
470 e2fsck_pass1_check_device_inode(&inode)) {
471 check_immutable(ctx, &pctx);
472 ctx->fs_fifo_count++;
473 } else if ((LINUX_S_ISSOCK (inode.i_mode)) &&
474 e2fsck_pass1_check_device_inode(&inode)) {
475 check_immutable(ctx, &pctx);
476 ctx->fs_sockets_count++;
478 if (!ctx->inode_bad_map)
480 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
482 if (inode.i_block[EXT2_IND_BLOCK])
484 if (inode.i_block[EXT2_DIND_BLOCK])
485 ctx->fs_dind_count++;
486 if (inode.i_block[EXT2_TIND_BLOCK])
487 ctx->fs_tind_count++;
488 if (inode.i_block[EXT2_IND_BLOCK] ||
489 inode.i_block[EXT2_DIND_BLOCK] ||
490 inode.i_block[EXT2_TIND_BLOCK]) {
491 inodes_to_process[process_inode_count].ino = ino;
492 inodes_to_process[process_inode_count].inode = inode;
493 process_inode_count++;
495 check_blocks(ctx, &pctx, block_buf);
497 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
500 if (process_inode_count >= ctx->process_inode_size) {
501 process_inodes(ctx, block_buf);
503 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
507 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
508 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
510 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
511 if (!ctx->inode_bb_map)
513 ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
514 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
518 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
519 ctx->flags |= E2F_FLAG_ABORT;
523 process_inodes(ctx, block_buf);
524 ext2fs_close_inode_scan(scan);
525 ehandler_operation(0);
527 if (ctx->invalid_bitmaps)
528 handle_fs_bad_blocks(ctx);
530 if (ctx->flags & E2F_FLAG_RESTART) {
532 * Only the master copy of the superblock and block
533 * group descriptors are going to be written during a
534 * restart, so set the superblock to be used to be the
537 ctx->use_superblock = 0;
542 if (ctx->block_dup_map) {
543 if (ctx->options & E2F_OPT_PREEN) {
544 clear_problem_context(&pctx);
545 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
547 e2fsck_pass1_dupblocks(ctx, block_buf);
549 ext2fs_free_mem((void **) &inodes_to_process);
551 e2fsck_use_inode_shortcuts(ctx, 0);
553 ext2fs_free_mem((void **) &block_buf);
555 if (ctx->large_files &&
556 !(sb->s_feature_ro_compat &
557 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
558 if (fix_problem(ctx, PR_1_FEATURE_LARGE_FILES, &pctx)) {
559 sb->s_feature_ro_compat |=
560 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
561 ext2fs_mark_super_dirty(fs);
563 } else if (!ctx->large_files &&
564 (sb->s_feature_ro_compat &
565 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
566 if (fs->flags & EXT2_FLAG_RW) {
567 sb->s_feature_ro_compat &=
568 ~EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
569 ext2fs_mark_super_dirty(fs);
573 #ifdef RESOURCE_TRACK
574 if (ctx->options & E2F_OPT_TIME2) {
575 e2fsck_clear_progbar(ctx);
576 print_resource_track(_("Pass 1"), &rtrack);
582 * When the inode_scan routines call this callback at the end of the
583 * glock group, call process_inodes.
585 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
586 dgrp_t group, void * priv_data)
588 struct scan_callback_struct *scan_struct;
591 scan_struct = (struct scan_callback_struct *) priv_data;
592 ctx = scan_struct->ctx;
594 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
597 if ((ctx->progress)(ctx, 1, group+1,
598 ctx->fs->group_desc_count))
599 return EXT2_ET_CANCEL_REQUESTED;
605 * Process the inodes in the "inodes to process" list.
607 static void process_inodes(e2fsck_t ctx, char *block_buf)
610 struct ext2_inode *old_stashed_inode;
611 ino_t old_stashed_ino;
612 const char *old_operation;
614 struct problem_context pctx;
617 printf("begin process_inodes: ");
619 if (process_inode_count == 0)
621 old_operation = ehandler_operation(0);
622 old_stashed_inode = ctx->stashed_inode;
623 old_stashed_ino = ctx->stashed_ino;
624 qsort(inodes_to_process, process_inode_count,
625 sizeof(struct process_inode_block), process_inode_cmp);
626 clear_problem_context(&pctx);
627 for (i=0; i < process_inode_count; i++) {
628 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
629 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
632 printf("%u ", pctx.ino);
634 sprintf(buf, _("reading indirect blocks of inode %lu"),
636 ehandler_operation(buf);
637 check_blocks(ctx, &pctx, block_buf);
638 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
641 ctx->stashed_inode = old_stashed_inode;
642 ctx->stashed_ino = old_stashed_ino;
643 process_inode_count = 0;
645 printf("end process inodes\n");
647 ehandler_operation(old_operation);
650 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
652 const struct process_inode_block *ib_a =
653 (const struct process_inode_block *) a;
654 const struct process_inode_block *ib_b =
655 (const struct process_inode_block *) b;
657 return (ib_a->inode.i_block[EXT2_IND_BLOCK] -
658 ib_b->inode.i_block[EXT2_IND_BLOCK]);
662 * This procedure will allocate the inode bad map table
664 static void alloc_bad_map(e2fsck_t ctx)
666 struct problem_context pctx;
668 clear_problem_context(&pctx);
670 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs, _("bad inode map"),
671 &ctx->inode_bad_map);
674 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
675 /* Should never get here */
676 ctx->flags |= E2F_FLAG_ABORT;
682 * This procedure will allocate the inode "bb" (badblock) map table
684 static void alloc_bb_map(e2fsck_t ctx)
686 struct problem_context pctx;
688 clear_problem_context(&pctx);
689 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
690 _("inode in bad block map"),
694 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
695 /* Should never get here */
696 ctx->flags |= E2F_FLAG_ABORT;
702 * This procedure will allocate the inode imagic table
704 static void alloc_imagic_map(e2fsck_t ctx)
706 struct problem_context pctx;
708 clear_problem_context(&pctx);
709 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
710 _("imagic inode map"),
711 &ctx->inode_imagic_map);
714 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
715 /* Should never get here */
716 ctx->flags |= E2F_FLAG_ABORT;
722 * Marks a block as in use, setting the dup_map if it's been set
723 * already. Called by process_block and process_bad_block.
725 * WARNING: Assumes checks have already been done to make sure block
726 * is valid. This is true in both process_block and process_bad_block.
728 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
730 struct problem_context pctx;
732 clear_problem_context(&pctx);
734 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
735 if (!ctx->block_dup_map) {
736 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
737 _("multiply claimed block map"),
738 &ctx->block_dup_map);
741 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
743 /* Should never get here */
744 ctx->flags |= E2F_FLAG_ABORT;
748 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
750 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
755 * This subroutine is called on each inode to account for all of the
756 * blocks used by that inode.
758 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
761 ext2_filsys fs = ctx->fs;
762 struct process_block_struct pb;
763 ino_t ino = pctx->ino;
764 struct ext2_inode *inode = pctx->inode;
767 struct ext2fs_sb *sb;
769 if (!ext2fs_inode_has_valid_blocks(pctx->inode))
773 pb.num_blocks = pb.last_block = 0;
774 pb.num_illegal_blocks = 0;
775 pb.suppress = 0; pb.clear = 0;
778 pb.previous_block = 0;
779 pb.is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
785 if (inode->i_flags & EXT2_COMPRBLK_FL) {
786 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
787 EXT2_FEATURE_INCOMPAT_COMPRESSION))
790 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
791 inode->i_flags &= ~EXT2_COMPRBLK_FL;
792 e2fsck_write_inode(ctx, ino, inode,
798 pctx->errcode = ext2fs_block_iterate2(fs, ino,
799 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
800 block_buf, process_block, &pb);
801 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
803 end_problem_latch(ctx, PR_LATCH_BLOCK);
805 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
807 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
808 ctx->fs_fragmented++;
811 e2fsck_read_inode(ctx, ino, inode, "check_blocks");
812 inode->i_links_count = 0;
813 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
814 inode->i_dtime = time(0);
815 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
816 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
817 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
818 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
820 * The inode was probably partially accounted for
821 * before processing was aborted, so we need to
822 * restart the pass 1 scan.
824 ctx->flags |= E2F_FLAG_RESTART;
828 pb.num_blocks *= (fs->blocksize / 512);
830 printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
831 ino, inode->i_size, pb.last_block, inode->i_blocks,
834 if (!pb.num_blocks && pb.is_dir) {
835 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
836 inode->i_links_count = 0;
837 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
838 inode->i_dtime = time(0);
839 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
840 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
841 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
842 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
843 ctx->fs_directory_count--;
848 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
849 if ((nblock > (pb.last_block + 1)) ||
850 ((inode->i_size & (fs->blocksize-1)) != 0))
852 else if (nblock < (pb.last_block + 1)) {
853 sb = (struct ext2fs_sb *) fs->super;
854 if (((pb.last_block + 1) - nblock) >
855 sb->s_prealloc_dir_blocks)
859 size = inode->i_size + ((__u64) inode->i_size_high << 32);
860 if ((size < pb.last_block * fs->blocksize))
862 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
866 pctx->num = (pb.last_block+1) * fs->blocksize;
867 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
868 inode->i_size = pctx->num;
870 inode->i_size_high = pctx->num >> 32;
871 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
875 if (!pb.is_dir && inode->i_size_high)
877 if (pb.num_blocks != inode->i_blocks) {
878 pctx->num = pb.num_blocks;
879 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
880 inode->i_blocks = pb.num_blocks;
881 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
889 * Helper function called by process block when an illegal block is
890 * found. It returns a description about why the block is illegal
892 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
896 static char problem[80];
898 super = fs->super->s_first_data_block;
899 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
901 sprintf(problem, "< FIRSTBLOCK (%u)", super);
903 } else if (block >= fs->super->s_blocks_count) {
904 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
907 for (i = 0; i < fs->group_desc_count; i++) {
908 if (block == super) {
909 sprintf(problem, "is the superblock in group %d", i);
913 block <= (super + fs->desc_blocks)) {
914 sprintf(problem, "is in the group descriptors "
918 if (block == fs->group_desc[i].bg_block_bitmap) {
919 sprintf(problem, "is the block bitmap of group %d", i);
922 if (block == fs->group_desc[i].bg_inode_bitmap) {
923 sprintf(problem, "is the inode bitmap of group %d", i);
926 if (block >= fs->group_desc[i].bg_inode_table &&
927 (block < fs->group_desc[i].bg_inode_table
928 + fs->inode_blocks_per_group)) {
929 sprintf(problem, "is in the inode table of group %d",
933 super += fs->super->s_blocks_per_group;
940 * This is a helper function for check_blocks().
942 int process_block(ext2_filsys fs,
944 e2_blkcnt_t blockcnt,
949 struct process_block_struct *p;
950 struct problem_context *pctx;
951 blk_t blk = *block_nr;
956 p = (struct process_block_struct *) priv_data;
960 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
961 /* todo: Check that the comprblk_fl is high, that the
962 blkaddr pattern looks right (all non-holes up to
963 first EXT2FS_COMPRESSED_BLKADDR, then all
964 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
965 that the feature_incompat bit is high, and that the
966 inode is a regular file. If we're doing a "full
967 check" (a concept introduced to e2fsck by e2compr,
968 meaning that we look at data blocks as well as
969 metadata) then call some library routine that
970 checks the compressed data. I'll have to think
971 about this, because one particularly important
972 problem to be able to fix is to recalculate the
973 cluster size if necessary. I think that perhaps
974 we'd better do most/all e2compr-specific checks
975 separately, after the non-e2compr checks. If not
976 doing a full check, it may be useful to test that
977 the personality is linux; e.g. if it isn't then
978 perhaps this really is just an illegal block. */
983 if (p->is_dir == 0) {
985 * Should never happen, since only directories
986 * get called with BLOCK_FLAG_HOLE
989 printf("process_block() called with blk == 0, "
990 "blockcnt=%d, inode %lu???\n",
997 if (blockcnt * fs->blocksize < p->inode->i_size) {
999 printf("Missing block (#%d) in directory inode %lu!\n",
1008 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
1013 * Simplistic fragmentation check. We merely require that the
1014 * file be contiguous. (Which can never be true for really
1015 * big files that are greater than a block group.)
1017 if (!HOLE_BLKADDR(p->previous_block)) {
1018 if (p->previous_block+1 != blk)
1021 p->previous_block = blk;
1023 if (blk < fs->super->s_first_data_block ||
1024 blk >= fs->super->s_blocks_count)
1025 problem = PR_1_ILLEGAL_BLOCK_NUM;
1028 p->num_illegal_blocks++;
1029 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
1030 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
1034 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
1036 set_latch_flags(PR_LATCH_BLOCK,
1041 pctx->blkcount = blockcnt;
1042 if (fix_problem(ctx, problem, pctx)) {
1043 blk = *block_nr = 0;
1044 ret_code = BLOCK_CHANGED;
1049 pctx->blkcount = -1;
1052 mark_block_used(ctx, blk);
1055 p->last_block = blockcnt;
1057 if (p->is_dir && (blockcnt >= 0)) {
1058 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1060 if (pctx->errcode) {
1062 pctx->num = blockcnt;
1063 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1064 /* Should never get here */
1065 ctx->flags |= E2F_FLAG_ABORT;
1072 static void bad_block_indirect(e2fsck_t ctx, blk_t blk)
1074 struct problem_context pctx;
1076 clear_problem_context(&pctx);
1078 * Prompt to see if we should continue or not.
1080 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, &pctx))
1081 ctx->flags |= E2F_FLAG_ABORT;
1084 int process_bad_block(ext2_filsys fs,
1086 e2_blkcnt_t blockcnt,
1091 struct process_block_struct *p;
1092 blk_t blk = *block_nr;
1095 struct problem_context *pctx;
1099 * Note: This function processes blocks for the bad blocks
1100 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
1106 p = (struct process_block_struct *) priv_data;
1110 pctx->ino = EXT2_BAD_INO;
1112 pctx->blkcount = blockcnt;
1114 if ((blk < fs->super->s_first_data_block) ||
1115 (blk >= fs->super->s_blocks_count)) {
1116 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1118 return BLOCK_CHANGED;
1124 if (ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1125 bad_block_indirect(ctx, blk);
1126 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1129 mark_block_used(ctx, blk);
1133 printf ("DEBUG: Marking %u as bad.\n", blk);
1135 ctx->fs_badblocks_count++;
1137 * If the block is not used, then mark it as used and return.
1138 * If it is already marked as found, this must mean that
1139 * there's an overlap between the filesystem table blocks
1140 * (bitmaps and inode table) and the bad block list.
1142 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1143 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1147 * Try to find the where the filesystem block was used...
1149 first_block = fs->super->s_first_data_block;
1151 for (i = 0; i < fs->group_desc_count; i++ ) {
1154 if (!ext2fs_bg_has_super(fs, i))
1156 if (blk == first_block) {
1158 if (fix_problem(ctx,
1159 PR_1_BAD_PRIMARY_SUPERBLOCK,
1162 return BLOCK_CHANGED;
1166 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1169 if ((blk > first_block) &&
1170 (blk <= first_block + fs->desc_blocks)) {
1172 pctx->blk = *block_nr;
1173 if (fix_problem(ctx,
1174 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1176 return BLOCK_CHANGED;
1180 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1184 if (blk == fs->group_desc[i].bg_block_bitmap) {
1185 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1186 ctx->invalid_block_bitmap_flag[i]++;
1187 ctx->invalid_bitmaps++;
1191 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1192 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1193 ctx->invalid_inode_bitmap_flag[i]++;
1194 ctx->invalid_bitmaps++;
1198 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1199 (blk < (fs->group_desc[i].bg_inode_table +
1200 fs->inode_blocks_per_group))) {
1202 * If there are bad blocks in the inode table,
1203 * the inode scan code will try to do
1204 * something reasonable automatically.
1208 first_block += fs->super->s_blocks_per_group;
1211 * If we've gotten to this point, then the only
1212 * possibility is that the bad block inode meta data
1213 * is using a bad block.
1215 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1216 p->inode->i_block[EXT2_DIND_BLOCK]) {
1217 bad_block_indirect(ctx, blk);
1218 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1225 /* Warn user that the block wasn't claimed */
1226 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1231 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
1232 const char *name, int num, blk_t *new_block)
1234 ext2_filsys fs = ctx->fs;
1235 blk_t old_block = *new_block;
1238 struct problem_context pctx;
1240 clear_problem_context(&pctx);
1243 pctx.blk = old_block;
1246 pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
1247 first_block + fs->super->s_blocks_per_group,
1248 num, ctx->block_found_map, new_block);
1251 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
1252 ext2fs_unmark_valid(fs);
1255 pctx.errcode = ext2fs_get_mem(fs->blocksize, (void **) &buf);
1257 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
1258 ext2fs_unmark_valid(fs);
1261 ext2fs_mark_super_dirty(fs);
1262 pctx.blk2 = *new_block;
1263 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
1264 PR_1_RELOC_TO), &pctx);
1266 for (i = 0; i < num; i++) {
1268 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
1270 pctx.errcode = io_channel_read_blk(fs->io,
1271 old_block + i, 1, buf);
1273 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
1275 memset(buf, 0, fs->blocksize);
1277 pctx.blk = (*new_block) + i;
1278 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
1281 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
1283 ext2fs_free_mem((void **) &buf);
1287 * This routine gets called at the end of pass 1 if bad blocks are
1288 * detected in the superblock, group descriptors, inode_bitmaps, or
1289 * block bitmaps. At this point, all of the blocks have been mapped
1290 * out, so we can try to allocate new block(s) to replace the bad
1293 static void handle_fs_bad_blocks(e2fsck_t ctx)
1295 ext2_filsys fs = ctx->fs;
1297 int first_block = fs->super->s_first_data_block;
1299 for (i = 0; i < fs->group_desc_count; i++) {
1300 if (ctx->invalid_block_bitmap_flag[i]) {
1301 new_table_block(ctx, first_block, i, _("block bitmap"),
1302 1, &fs->group_desc[i].bg_block_bitmap);
1304 if (ctx->invalid_inode_bitmap_flag[i]) {
1305 new_table_block(ctx, first_block, i, _("inode bitmap"),
1306 1, &fs->group_desc[i].bg_inode_bitmap);
1308 if (ctx->invalid_inode_table_flag[i]) {
1309 new_table_block(ctx, first_block, i, _("inode table"),
1310 fs->inode_blocks_per_group,
1311 &fs->group_desc[i].bg_inode_table);
1312 ctx->flags |= E2F_FLAG_RESTART;
1314 first_block += fs->super->s_blocks_per_group;
1316 ctx->invalid_bitmaps = 0;
1320 * This routine marks all blocks which are used by the superblock,
1321 * group descriptors, inode bitmaps, and block bitmaps.
1323 static void mark_table_blocks(e2fsck_t ctx)
1325 ext2_filsys fs = ctx->fs;
1328 struct problem_context pctx;
1330 clear_problem_context(&pctx);
1332 block = fs->super->s_first_data_block;
1333 for (i = 0; i < fs->group_desc_count; i++) {
1336 if (ext2fs_bg_has_super(fs, i)) {
1338 * Mark this group's copy of the superblock
1340 ext2fs_mark_block_bitmap(ctx->block_found_map, block);
1343 * Mark this group's copy of the descriptors
1345 for (j = 0; j < fs->desc_blocks; j++) {
1346 ext2fs_mark_block_bitmap(ctx->block_found_map,
1352 * Mark the blocks used for the inode table
1354 if (fs->group_desc[i].bg_inode_table) {
1355 for (j = 0, b = fs->group_desc[i].bg_inode_table;
1356 j < fs->inode_blocks_per_group;
1358 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1361 if (fix_problem(ctx,
1362 PR_1_ITABLE_CONFLICT, &pctx)) {
1363 ctx->invalid_inode_table_flag[i]++;
1364 ctx->invalid_bitmaps++;
1367 ext2fs_mark_block_bitmap(ctx->block_found_map,
1374 * Mark block used for the block bitmap
1376 if (fs->group_desc[i].bg_block_bitmap) {
1377 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1378 fs->group_desc[i].bg_block_bitmap)) {
1379 pctx.blk = fs->group_desc[i].bg_block_bitmap;
1380 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
1381 ctx->invalid_block_bitmap_flag[i]++;
1382 ctx->invalid_bitmaps++;
1385 ext2fs_mark_block_bitmap(ctx->block_found_map,
1386 fs->group_desc[i].bg_block_bitmap);
1391 * Mark block used for the inode bitmap
1393 if (fs->group_desc[i].bg_inode_bitmap) {
1394 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1395 fs->group_desc[i].bg_inode_bitmap)) {
1396 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
1397 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
1398 ctx->invalid_inode_bitmap_flag[i]++;
1399 ctx->invalid_bitmaps++;
1402 ext2fs_mark_block_bitmap(ctx->block_found_map,
1403 fs->group_desc[i].bg_inode_bitmap);
1406 block += fs->super->s_blocks_per_group;
1411 * Thes subroutines short circuits ext2fs_get_blocks and
1412 * ext2fs_check_directory; we use them since we already have the inode
1413 * structure, so there's no point in letting the ext2fs library read
1416 static errcode_t pass1_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks)
1418 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1421 if (ino != ctx->stashed_ino)
1422 return EXT2_ET_CALLBACK_NOTHANDLED;
1424 for (i=0; i < EXT2_N_BLOCKS; i++)
1425 blocks[i] = ctx->stashed_inode->i_block[i];
1429 static errcode_t pass1_read_inode(ext2_filsys fs, ino_t ino,
1430 struct ext2_inode *inode)
1432 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1434 if (ino != ctx->stashed_ino)
1435 return EXT2_ET_CALLBACK_NOTHANDLED;
1436 *inode = *ctx->stashed_inode;
1440 static errcode_t pass1_write_inode(ext2_filsys fs, ino_t ino,
1441 struct ext2_inode *inode)
1443 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1445 if (ino == ctx->stashed_ino)
1446 *ctx->stashed_inode = *inode;
1447 return EXT2_ET_CALLBACK_NOTHANDLED;
1450 static errcode_t pass1_check_directory(ext2_filsys fs, ino_t ino)
1452 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1454 if (ino != ctx->stashed_ino)
1455 return EXT2_ET_CALLBACK_NOTHANDLED;
1457 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
1458 return EXT2_ET_NO_DIRECTORY;
1462 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
1464 ext2_filsys fs = ctx->fs;
1467 fs->get_blocks = pass1_get_blocks;
1468 fs->check_directory = pass1_check_directory;
1469 fs->read_inode = pass1_read_inode;
1470 fs->write_inode = pass1_write_inode;
1471 ctx->stashed_ino = 0;
1474 fs->check_directory = 0;
1476 fs->write_inode = 0;