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 | EXT2_APPEND_FL)) {
138 for (i=4; i < EXT2_N_BLOCKS; i++)
139 if (inode->i_block[i])
146 * If the immutable (or append-only) flag is set on the inode, offer
149 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
151 if (!(pctx->inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)))
154 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
157 pctx->inode->i_flags &= ~((EXT2_IMMUTABLE_FL | EXT2_APPEND_FL));
158 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
162 void e2fsck_pass1(e2fsck_t ctx)
166 ext2_filsys fs = ctx->fs;
168 struct ext2_inode inode;
169 ext2_inode_scan scan;
171 #ifdef RESOURCE_TRACK
172 struct resource_track rtrack;
174 unsigned char frag, fsize;
175 struct problem_context pctx;
176 struct scan_callback_struct scan_struct;
177 struct ext2fs_sb *sb;
180 #ifdef RESOURCE_TRACK
181 init_resource_track(&rtrack);
183 clear_problem_context(&pctx);
185 if (!(ctx->options & E2F_OPT_PREEN))
186 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
189 mtrace_print("Pass 1");
192 #define EXT2_BPP(bits) (1UL << ((bits) - 2))
194 for (i=0; i < 4; i++) {
195 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(10+i);
196 max_sizes = max_sizes + EXT2_BPP(10+i) * EXT2_BPP(10+i);
197 max_sizes = (max_sizes +
198 (__u64) EXT2_BPP(10+i) * EXT2_BPP(10+i) *
200 max_sizes = (max_sizes * (1UL << (10+i))) - 1;
201 ext2_max_sizes[i] = max_sizes;
205 sb = (struct ext2fs_sb *) fs->super;
206 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
209 * Allocate bitmaps structures
211 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
212 &ctx->inode_used_map);
215 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
216 ctx->flags |= E2F_FLAG_ABORT;
219 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
220 _("directory inode map"), &ctx->inode_dir_map);
223 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
224 ctx->flags |= E2F_FLAG_ABORT;
227 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
228 _("regular file inode map"), &ctx->inode_reg_map);
231 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
232 ctx->flags |= E2F_FLAG_ABORT;
235 pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
236 &ctx->block_found_map);
239 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
240 ctx->flags |= E2F_FLAG_ABORT;
243 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
244 &ctx->inode_link_info);
246 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
247 ctx->flags |= E2F_FLAG_ABORT;
250 inodes_to_process = (struct process_inode_block *)
251 e2fsck_allocate_memory(ctx,
252 (ctx->process_inode_size *
253 sizeof(struct process_inode_block)),
254 "array of inodes to process");
255 process_inode_count = 0;
257 pctx.errcode = ext2fs_init_dblist(fs, 0);
259 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
260 ctx->flags |= E2F_FLAG_ABORT;
264 mark_table_blocks(ctx);
265 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
266 "block interate buffer");
267 e2fsck_use_inode_shortcuts(ctx, 1);
268 ehandler_operation(_("doing inode scan"));
269 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
272 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
273 ctx->flags |= E2F_FLAG_ABORT;
276 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
277 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
279 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
280 ctx->flags |= E2F_FLAG_ABORT;
283 ctx->stashed_inode = &inode;
284 scan_struct.ctx = ctx;
285 scan_struct.block_buf = block_buf;
286 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
288 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
293 ctx->stashed_ino = ino;
294 if (inode.i_links_count) {
295 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
296 ino, inode.i_links_count);
298 pctx.num = inode.i_links_count;
299 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
300 ctx->flags |= E2F_FLAG_ABORT;
304 if (ino == EXT2_BAD_INO) {
305 struct process_block_struct pb;
307 pb.ino = EXT2_BAD_INO;
308 pb.num_blocks = pb.last_block = 0;
309 pb.num_illegal_blocks = 0;
310 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
315 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0,
316 block_buf, process_bad_block, &pb);
318 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
319 ctx->flags |= E2F_FLAG_ABORT;
322 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
323 clear_problem_context(&pctx);
326 if (ino == EXT2_ROOT_INO) {
328 * Make sure the root inode is a directory; if
329 * not, offer to clear it. It will be
330 * regnerated in pass #3.
332 if (!LINUX_S_ISDIR(inode.i_mode)) {
333 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
334 inode.i_dtime = time(0);
335 inode.i_links_count = 0;
336 ext2fs_icount_store(ctx->inode_link_info,
338 e2fsck_write_inode(ctx, ino, &inode,
343 * If dtime is set, offer to clear it. mke2fs
344 * version 0.2b created filesystems with the
345 * dtime field set for the root and lost+found
346 * directories. We won't worry about
347 * /lost+found, since that can be regenerated
348 * easily. But we will fix the root directory
351 if (inode.i_dtime && inode.i_links_count) {
352 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
354 e2fsck_write_inode(ctx, ino, &inode,
359 if ((ino != EXT2_ROOT_INO) &&
360 (ino < EXT2_FIRST_INODE(fs->super))) {
361 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
362 if (((ino == EXT2_BOOT_LOADER_INO) &&
363 LINUX_S_ISDIR(inode.i_mode)) ||
364 ((ino != EXT2_BOOT_LOADER_INO) &&
365 (inode.i_mode != 0))) {
367 PR_1_RESERVED_BAD_MODE, &pctx)) {
369 e2fsck_write_inode(ctx, ino, &inode,
373 check_blocks(ctx, &pctx, block_buf);
377 * This code assumes that deleted inodes have
378 * i_links_count set to 0.
380 if (!inode.i_links_count) {
381 if (!inode.i_dtime && inode.i_mode) {
383 PR_1_ZERO_DTIME, &pctx)) {
384 inode.i_dtime = time(0);
385 e2fsck_write_inode(ctx, ino, &inode,
392 * n.b. 0.3c ext2fs code didn't clear i_links_count for
393 * deleted files. Oops.
395 * Since all new ext2 implementations get this right,
396 * we now assume that the case of non-zero
397 * i_links_count and non-zero dtime means that we
398 * should keep the file, not delete it.
402 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
404 e2fsck_write_inode(ctx, ino, &inode, "pass1");
408 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
409 switch (fs->super->s_creator_os) {
411 frag = inode.osd2.linux2.l_i_frag;
412 fsize = inode.osd2.linux2.l_i_fsize;
415 frag = inode.osd2.hurd2.h_i_frag;
416 fsize = inode.osd2.hurd2.h_i_fsize;
419 frag = inode.osd2.masix2.m_i_frag;
420 fsize = inode.osd2.masix2.m_i_fsize;
426 if (inode.i_faddr || frag || fsize
427 || inode.i_file_acl ||
428 (LINUX_S_ISDIR(inode.i_mode) && inode.i_dir_acl)) {
429 if (!ctx->inode_bad_map)
431 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
433 if (inode.i_flags & EXT2_IMAGIC_FL) {
435 if (!ctx->inode_imagic_map)
436 alloc_imagic_map(ctx);
437 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
440 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
441 inode.i_flags &= ~EXT2_IMAGIC_FL;
442 e2fsck_write_inode(ctx, ino,
448 if (LINUX_S_ISDIR(inode.i_mode)) {
449 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
450 e2fsck_add_dir_info(ctx, ino, 0);
451 ctx->fs_directory_count++;
452 } else if (LINUX_S_ISREG (inode.i_mode)) {
453 ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
454 ctx->fs_regular_count++;
455 } else if (LINUX_S_ISCHR (inode.i_mode) &&
456 e2fsck_pass1_check_device_inode(&inode)) {
457 check_immutable(ctx, &pctx);
458 ctx->fs_chardev_count++;
459 } else if (LINUX_S_ISBLK (inode.i_mode) &&
460 e2fsck_pass1_check_device_inode(&inode)) {
461 check_immutable(ctx, &pctx);
462 ctx->fs_blockdev_count++;
463 } else if (LINUX_S_ISLNK (inode.i_mode)) {
464 ctx->fs_symlinks_count++;
465 if (!inode.i_blocks) {
466 ctx->fs_fast_symlinks_count++;
470 else if (LINUX_S_ISFIFO (inode.i_mode) &&
471 e2fsck_pass1_check_device_inode(&inode)) {
472 check_immutable(ctx, &pctx);
473 ctx->fs_fifo_count++;
474 } else if ((LINUX_S_ISSOCK (inode.i_mode)) &&
475 e2fsck_pass1_check_device_inode(&inode)) {
476 check_immutable(ctx, &pctx);
477 ctx->fs_sockets_count++;
479 if (!ctx->inode_bad_map)
481 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
483 if (inode.i_block[EXT2_IND_BLOCK])
485 if (inode.i_block[EXT2_DIND_BLOCK])
486 ctx->fs_dind_count++;
487 if (inode.i_block[EXT2_TIND_BLOCK])
488 ctx->fs_tind_count++;
489 if (inode.i_block[EXT2_IND_BLOCK] ||
490 inode.i_block[EXT2_DIND_BLOCK] ||
491 inode.i_block[EXT2_TIND_BLOCK]) {
492 inodes_to_process[process_inode_count].ino = ino;
493 inodes_to_process[process_inode_count].inode = inode;
494 process_inode_count++;
496 check_blocks(ctx, &pctx, block_buf);
498 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
501 if (process_inode_count >= ctx->process_inode_size) {
502 process_inodes(ctx, block_buf);
504 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
508 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
509 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
511 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
512 if (!ctx->inode_bb_map)
514 ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
515 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
519 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
520 ctx->flags |= E2F_FLAG_ABORT;
524 process_inodes(ctx, block_buf);
525 ext2fs_close_inode_scan(scan);
526 ehandler_operation(0);
528 if (ctx->invalid_bitmaps)
529 handle_fs_bad_blocks(ctx);
531 if (ctx->flags & E2F_FLAG_RESTART) {
533 * Only the master copy of the superblock and block
534 * group descriptors are going to be written during a
535 * restart, so set the superblock to be used to be the
538 ctx->use_superblock = 0;
543 if (ctx->block_dup_map) {
544 if (ctx->options & E2F_OPT_PREEN) {
545 clear_problem_context(&pctx);
546 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
548 e2fsck_pass1_dupblocks(ctx, block_buf);
550 ext2fs_free_mem((void **) &inodes_to_process);
552 e2fsck_use_inode_shortcuts(ctx, 0);
554 ext2fs_free_mem((void **) &block_buf);
556 if (ctx->large_files &&
557 !(sb->s_feature_ro_compat &
558 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
559 if (fix_problem(ctx, PR_1_FEATURE_LARGE_FILES, &pctx)) {
560 sb->s_feature_ro_compat |=
561 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
562 ext2fs_mark_super_dirty(fs);
564 } else if (!ctx->large_files &&
565 (sb->s_feature_ro_compat &
566 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
567 if (fs->flags & EXT2_FLAG_RW) {
568 sb->s_feature_ro_compat &=
569 ~EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
570 ext2fs_mark_super_dirty(fs);
574 #ifdef RESOURCE_TRACK
575 if (ctx->options & E2F_OPT_TIME2) {
576 e2fsck_clear_progbar(ctx);
577 print_resource_track(_("Pass 1"), &rtrack);
583 * When the inode_scan routines call this callback at the end of the
584 * glock group, call process_inodes.
586 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
587 dgrp_t group, void * priv_data)
589 struct scan_callback_struct *scan_struct;
592 scan_struct = (struct scan_callback_struct *) priv_data;
593 ctx = scan_struct->ctx;
595 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
598 if ((ctx->progress)(ctx, 1, group+1,
599 ctx->fs->group_desc_count))
600 return EXT2_ET_CANCEL_REQUESTED;
606 * Process the inodes in the "inodes to process" list.
608 static void process_inodes(e2fsck_t ctx, char *block_buf)
611 struct ext2_inode *old_stashed_inode;
612 ino_t old_stashed_ino;
613 const char *old_operation;
615 struct problem_context pctx;
618 printf("begin process_inodes: ");
620 if (process_inode_count == 0)
622 old_operation = ehandler_operation(0);
623 old_stashed_inode = ctx->stashed_inode;
624 old_stashed_ino = ctx->stashed_ino;
625 qsort(inodes_to_process, process_inode_count,
626 sizeof(struct process_inode_block), process_inode_cmp);
627 clear_problem_context(&pctx);
628 for (i=0; i < process_inode_count; i++) {
629 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
630 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
633 printf("%u ", pctx.ino);
635 sprintf(buf, _("reading indirect blocks of inode %lu"),
637 ehandler_operation(buf);
638 check_blocks(ctx, &pctx, block_buf);
639 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
642 ctx->stashed_inode = old_stashed_inode;
643 ctx->stashed_ino = old_stashed_ino;
644 process_inode_count = 0;
646 printf("end process inodes\n");
648 ehandler_operation(old_operation);
651 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
653 const struct process_inode_block *ib_a =
654 (const struct process_inode_block *) a;
655 const struct process_inode_block *ib_b =
656 (const struct process_inode_block *) b;
658 return (ib_a->inode.i_block[EXT2_IND_BLOCK] -
659 ib_b->inode.i_block[EXT2_IND_BLOCK]);
663 * This procedure will allocate the inode bad map table
665 static void alloc_bad_map(e2fsck_t ctx)
667 struct problem_context pctx;
669 clear_problem_context(&pctx);
671 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs, _("bad inode map"),
672 &ctx->inode_bad_map);
675 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
676 /* Should never get here */
677 ctx->flags |= E2F_FLAG_ABORT;
683 * This procedure will allocate the inode "bb" (badblock) map table
685 static void alloc_bb_map(e2fsck_t ctx)
687 struct problem_context pctx;
689 clear_problem_context(&pctx);
690 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
691 _("inode in bad block map"),
695 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
696 /* Should never get here */
697 ctx->flags |= E2F_FLAG_ABORT;
703 * This procedure will allocate the inode imagic table
705 static void alloc_imagic_map(e2fsck_t ctx)
707 struct problem_context pctx;
709 clear_problem_context(&pctx);
710 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
711 _("imagic inode map"),
712 &ctx->inode_imagic_map);
715 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
716 /* Should never get here */
717 ctx->flags |= E2F_FLAG_ABORT;
723 * Marks a block as in use, setting the dup_map if it's been set
724 * already. Called by process_block and process_bad_block.
726 * WARNING: Assumes checks have already been done to make sure block
727 * is valid. This is true in both process_block and process_bad_block.
729 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
731 struct problem_context pctx;
733 clear_problem_context(&pctx);
735 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
736 if (!ctx->block_dup_map) {
737 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
738 _("multiply claimed block map"),
739 &ctx->block_dup_map);
742 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
744 /* Should never get here */
745 ctx->flags |= E2F_FLAG_ABORT;
749 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
751 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
756 * This subroutine is called on each inode to account for all of the
757 * blocks used by that inode.
759 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
762 ext2_filsys fs = ctx->fs;
763 struct process_block_struct pb;
764 ino_t ino = pctx->ino;
765 struct ext2_inode *inode = pctx->inode;
768 struct ext2fs_sb *sb;
770 if (!ext2fs_inode_has_valid_blocks(pctx->inode))
774 pb.num_blocks = pb.last_block = 0;
775 pb.num_illegal_blocks = 0;
776 pb.suppress = 0; pb.clear = 0;
779 pb.previous_block = 0;
780 pb.is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
786 if (inode->i_flags & EXT2_COMPRBLK_FL) {
787 if (EXT2_HAS_INCOMPAT_FEATURE(fs->super,
788 EXT2_FEATURE_INCOMPAT_COMPRESSION))
791 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
792 inode->i_flags &= ~EXT2_COMPRBLK_FL;
793 e2fsck_write_inode(ctx, ino, inode,
799 pctx->errcode = ext2fs_block_iterate2(fs, ino,
800 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
801 block_buf, process_block, &pb);
802 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
804 end_problem_latch(ctx, PR_LATCH_BLOCK);
806 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
808 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
809 ctx->fs_fragmented++;
812 e2fsck_read_inode(ctx, ino, inode, "check_blocks");
813 inode->i_links_count = 0;
814 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
815 inode->i_dtime = time(0);
816 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
817 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
818 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
819 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
821 * The inode was probably partially accounted for
822 * before processing was aborted, so we need to
823 * restart the pass 1 scan.
825 ctx->flags |= E2F_FLAG_RESTART;
829 pb.num_blocks *= (fs->blocksize / 512);
831 printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
832 ino, inode->i_size, pb.last_block, inode->i_blocks,
835 if (!pb.num_blocks && pb.is_dir) {
836 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
837 inode->i_links_count = 0;
838 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
839 inode->i_dtime = time(0);
840 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
841 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
842 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
843 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
844 ctx->fs_directory_count--;
849 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
850 if ((nblock > (pb.last_block + 1)) ||
851 ((inode->i_size & (fs->blocksize-1)) != 0))
853 else if (nblock < (pb.last_block + 1)) {
854 sb = (struct ext2fs_sb *) fs->super;
855 if (((pb.last_block + 1) - nblock) >
856 sb->s_prealloc_dir_blocks)
860 size = inode->i_size + ((__u64) inode->i_size_high << 32);
861 if ((size < pb.last_block * fs->blocksize))
863 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
867 pctx->num = (pb.last_block+1) * fs->blocksize;
868 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
869 inode->i_size = pctx->num;
871 inode->i_size_high = pctx->num >> 32;
872 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
876 if (!pb.is_dir && inode->i_size_high)
878 if (pb.num_blocks != inode->i_blocks) {
879 pctx->num = pb.num_blocks;
880 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
881 inode->i_blocks = pb.num_blocks;
882 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
890 * Helper function called by process block when an illegal block is
891 * found. It returns a description about why the block is illegal
893 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
897 static char problem[80];
899 super = fs->super->s_first_data_block;
900 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
902 sprintf(problem, "< FIRSTBLOCK (%u)", super);
904 } else if (block >= fs->super->s_blocks_count) {
905 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
908 for (i = 0; i < fs->group_desc_count; i++) {
909 if (block == super) {
910 sprintf(problem, "is the superblock in group %d", i);
914 block <= (super + fs->desc_blocks)) {
915 sprintf(problem, "is in the group descriptors "
919 if (block == fs->group_desc[i].bg_block_bitmap) {
920 sprintf(problem, "is the block bitmap of group %d", i);
923 if (block == fs->group_desc[i].bg_inode_bitmap) {
924 sprintf(problem, "is the inode bitmap of group %d", i);
927 if (block >= fs->group_desc[i].bg_inode_table &&
928 (block < fs->group_desc[i].bg_inode_table
929 + fs->inode_blocks_per_group)) {
930 sprintf(problem, "is in the inode table of group %d",
934 super += fs->super->s_blocks_per_group;
941 * This is a helper function for check_blocks().
943 int process_block(ext2_filsys fs,
945 e2_blkcnt_t blockcnt,
950 struct process_block_struct *p;
951 struct problem_context *pctx;
952 blk_t blk = *block_nr;
957 p = (struct process_block_struct *) priv_data;
961 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
962 /* todo: Check that the comprblk_fl is high, that the
963 blkaddr pattern looks right (all non-holes up to
964 first EXT2FS_COMPRESSED_BLKADDR, then all
965 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
966 that the feature_incompat bit is high, and that the
967 inode is a regular file. If we're doing a "full
968 check" (a concept introduced to e2fsck by e2compr,
969 meaning that we look at data blocks as well as
970 metadata) then call some library routine that
971 checks the compressed data. I'll have to think
972 about this, because one particularly important
973 problem to be able to fix is to recalculate the
974 cluster size if necessary. I think that perhaps
975 we'd better do most/all e2compr-specific checks
976 separately, after the non-e2compr checks. If not
977 doing a full check, it may be useful to test that
978 the personality is linux; e.g. if it isn't then
979 perhaps this really is just an illegal block. */
984 if (p->is_dir == 0) {
986 * Should never happen, since only directories
987 * get called with BLOCK_FLAG_HOLE
990 printf("process_block() called with blk == 0, "
991 "blockcnt=%d, inode %lu???\n",
998 if (blockcnt * fs->blocksize < p->inode->i_size) {
1000 printf("Missing block (#%d) in directory inode %lu!\n",
1009 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
1014 * Simplistic fragmentation check. We merely require that the
1015 * file be contiguous. (Which can never be true for really
1016 * big files that are greater than a block group.)
1018 if (!HOLE_BLKADDR(p->previous_block)) {
1019 if (p->previous_block+1 != blk)
1022 p->previous_block = blk;
1024 if (blk < fs->super->s_first_data_block ||
1025 blk >= fs->super->s_blocks_count)
1026 problem = PR_1_ILLEGAL_BLOCK_NUM;
1029 p->num_illegal_blocks++;
1030 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
1031 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
1035 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
1037 set_latch_flags(PR_LATCH_BLOCK,
1042 pctx->blkcount = blockcnt;
1043 if (fix_problem(ctx, problem, pctx)) {
1044 blk = *block_nr = 0;
1045 ret_code = BLOCK_CHANGED;
1050 pctx->blkcount = -1;
1053 mark_block_used(ctx, blk);
1056 p->last_block = blockcnt;
1058 if (p->is_dir && (blockcnt >= 0)) {
1059 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1061 if (pctx->errcode) {
1063 pctx->num = blockcnt;
1064 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1065 /* Should never get here */
1066 ctx->flags |= E2F_FLAG_ABORT;
1073 static void bad_block_indirect(e2fsck_t ctx, blk_t blk)
1075 struct problem_context pctx;
1077 clear_problem_context(&pctx);
1079 * Prompt to see if we should continue or not.
1081 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, &pctx))
1082 ctx->flags |= E2F_FLAG_ABORT;
1085 int process_bad_block(ext2_filsys fs,
1087 e2_blkcnt_t blockcnt,
1092 struct process_block_struct *p;
1093 blk_t blk = *block_nr;
1096 struct problem_context *pctx;
1100 * Note: This function processes blocks for the bad blocks
1101 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
1107 p = (struct process_block_struct *) priv_data;
1111 pctx->ino = EXT2_BAD_INO;
1113 pctx->blkcount = blockcnt;
1115 if ((blk < fs->super->s_first_data_block) ||
1116 (blk >= fs->super->s_blocks_count)) {
1117 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1119 return BLOCK_CHANGED;
1125 if (ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1126 bad_block_indirect(ctx, blk);
1127 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1130 mark_block_used(ctx, blk);
1134 printf ("DEBUG: Marking %u as bad.\n", blk);
1136 ctx->fs_badblocks_count++;
1138 * If the block is not used, then mark it as used and return.
1139 * If it is already marked as found, this must mean that
1140 * there's an overlap between the filesystem table blocks
1141 * (bitmaps and inode table) and the bad block list.
1143 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1144 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1148 * Try to find the where the filesystem block was used...
1150 first_block = fs->super->s_first_data_block;
1152 for (i = 0; i < fs->group_desc_count; i++ ) {
1155 if (!ext2fs_bg_has_super(fs, i))
1157 if (blk == first_block) {
1159 if (fix_problem(ctx,
1160 PR_1_BAD_PRIMARY_SUPERBLOCK,
1163 return BLOCK_CHANGED;
1167 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1170 if ((blk > first_block) &&
1171 (blk <= first_block + fs->desc_blocks)) {
1173 pctx->blk = *block_nr;
1174 if (fix_problem(ctx,
1175 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1177 return BLOCK_CHANGED;
1181 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1185 if (blk == fs->group_desc[i].bg_block_bitmap) {
1186 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1187 ctx->invalid_block_bitmap_flag[i]++;
1188 ctx->invalid_bitmaps++;
1192 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1193 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1194 ctx->invalid_inode_bitmap_flag[i]++;
1195 ctx->invalid_bitmaps++;
1199 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1200 (blk < (fs->group_desc[i].bg_inode_table +
1201 fs->inode_blocks_per_group))) {
1203 * If there are bad blocks in the inode table,
1204 * the inode scan code will try to do
1205 * something reasonable automatically.
1209 first_block += fs->super->s_blocks_per_group;
1212 * If we've gotten to this point, then the only
1213 * possibility is that the bad block inode meta data
1214 * is using a bad block.
1216 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1217 p->inode->i_block[EXT2_DIND_BLOCK]) {
1218 bad_block_indirect(ctx, blk);
1219 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1226 /* Warn user that the block wasn't claimed */
1227 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1232 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
1233 const char *name, int num, blk_t *new_block)
1235 ext2_filsys fs = ctx->fs;
1236 blk_t old_block = *new_block;
1239 struct problem_context pctx;
1241 clear_problem_context(&pctx);
1244 pctx.blk = old_block;
1247 pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
1248 first_block + fs->super->s_blocks_per_group,
1249 num, ctx->block_found_map, new_block);
1252 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
1253 ext2fs_unmark_valid(fs);
1256 pctx.errcode = ext2fs_get_mem(fs->blocksize, (void **) &buf);
1258 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
1259 ext2fs_unmark_valid(fs);
1262 ext2fs_mark_super_dirty(fs);
1263 pctx.blk2 = *new_block;
1264 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
1265 PR_1_RELOC_TO), &pctx);
1267 for (i = 0; i < num; i++) {
1269 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
1271 pctx.errcode = io_channel_read_blk(fs->io,
1272 old_block + i, 1, buf);
1274 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
1276 memset(buf, 0, fs->blocksize);
1278 pctx.blk = (*new_block) + i;
1279 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
1282 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
1284 ext2fs_free_mem((void **) &buf);
1288 * This routine gets called at the end of pass 1 if bad blocks are
1289 * detected in the superblock, group descriptors, inode_bitmaps, or
1290 * block bitmaps. At this point, all of the blocks have been mapped
1291 * out, so we can try to allocate new block(s) to replace the bad
1294 static void handle_fs_bad_blocks(e2fsck_t ctx)
1296 ext2_filsys fs = ctx->fs;
1298 int first_block = fs->super->s_first_data_block;
1300 for (i = 0; i < fs->group_desc_count; i++) {
1301 if (ctx->invalid_block_bitmap_flag[i]) {
1302 new_table_block(ctx, first_block, i, _("block bitmap"),
1303 1, &fs->group_desc[i].bg_block_bitmap);
1305 if (ctx->invalid_inode_bitmap_flag[i]) {
1306 new_table_block(ctx, first_block, i, _("inode bitmap"),
1307 1, &fs->group_desc[i].bg_inode_bitmap);
1309 if (ctx->invalid_inode_table_flag[i]) {
1310 new_table_block(ctx, first_block, i, _("inode table"),
1311 fs->inode_blocks_per_group,
1312 &fs->group_desc[i].bg_inode_table);
1313 ctx->flags |= E2F_FLAG_RESTART;
1315 first_block += fs->super->s_blocks_per_group;
1317 ctx->invalid_bitmaps = 0;
1321 * This routine marks all blocks which are used by the superblock,
1322 * group descriptors, inode bitmaps, and block bitmaps.
1324 static void mark_table_blocks(e2fsck_t ctx)
1326 ext2_filsys fs = ctx->fs;
1329 struct problem_context pctx;
1331 clear_problem_context(&pctx);
1333 block = fs->super->s_first_data_block;
1334 for (i = 0; i < fs->group_desc_count; i++) {
1337 if (ext2fs_bg_has_super(fs, i)) {
1339 * Mark this group's copy of the superblock
1341 ext2fs_mark_block_bitmap(ctx->block_found_map, block);
1344 * Mark this group's copy of the descriptors
1346 for (j = 0; j < fs->desc_blocks; j++) {
1347 ext2fs_mark_block_bitmap(ctx->block_found_map,
1353 * Mark the blocks used for the inode table
1355 if (fs->group_desc[i].bg_inode_table) {
1356 for (j = 0, b = fs->group_desc[i].bg_inode_table;
1357 j < fs->inode_blocks_per_group;
1359 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1362 if (fix_problem(ctx,
1363 PR_1_ITABLE_CONFLICT, &pctx)) {
1364 ctx->invalid_inode_table_flag[i]++;
1365 ctx->invalid_bitmaps++;
1368 ext2fs_mark_block_bitmap(ctx->block_found_map,
1375 * Mark block used for the block bitmap
1377 if (fs->group_desc[i].bg_block_bitmap) {
1378 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1379 fs->group_desc[i].bg_block_bitmap)) {
1380 pctx.blk = fs->group_desc[i].bg_block_bitmap;
1381 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
1382 ctx->invalid_block_bitmap_flag[i]++;
1383 ctx->invalid_bitmaps++;
1386 ext2fs_mark_block_bitmap(ctx->block_found_map,
1387 fs->group_desc[i].bg_block_bitmap);
1392 * Mark block used for the inode bitmap
1394 if (fs->group_desc[i].bg_inode_bitmap) {
1395 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1396 fs->group_desc[i].bg_inode_bitmap)) {
1397 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
1398 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
1399 ctx->invalid_inode_bitmap_flag[i]++;
1400 ctx->invalid_bitmaps++;
1403 ext2fs_mark_block_bitmap(ctx->block_found_map,
1404 fs->group_desc[i].bg_inode_bitmap);
1407 block += fs->super->s_blocks_per_group;
1412 * Thes subroutines short circuits ext2fs_get_blocks and
1413 * ext2fs_check_directory; we use them since we already have the inode
1414 * structure, so there's no point in letting the ext2fs library read
1417 static errcode_t pass1_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks)
1419 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1422 if (ino != ctx->stashed_ino)
1423 return EXT2_ET_CALLBACK_NOTHANDLED;
1425 for (i=0; i < EXT2_N_BLOCKS; i++)
1426 blocks[i] = ctx->stashed_inode->i_block[i];
1430 static errcode_t pass1_read_inode(ext2_filsys fs, ino_t ino,
1431 struct ext2_inode *inode)
1433 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1435 if (ino != ctx->stashed_ino)
1436 return EXT2_ET_CALLBACK_NOTHANDLED;
1437 *inode = *ctx->stashed_inode;
1441 static errcode_t pass1_write_inode(ext2_filsys fs, ino_t ino,
1442 struct ext2_inode *inode)
1444 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1446 if (ino == ctx->stashed_ino)
1447 *ctx->stashed_inode = *inode;
1448 return EXT2_ET_CALLBACK_NOTHANDLED;
1451 static errcode_t pass1_check_directory(ext2_filsys fs, ino_t ino)
1453 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1455 if (ino != ctx->stashed_ino)
1456 return EXT2_ET_CALLBACK_NOTHANDLED;
1458 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
1459 return EXT2_ET_NO_DIRECTORY;
1463 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
1465 ext2_filsys fs = ctx->fs;
1468 fs->get_blocks = pass1_get_blocks;
1469 fs->check_directory = pass1_check_directory;
1470 fs->read_inode = pass1_read_inode;
1471 fs->write_inode = pass1_write_inode;
1472 ctx->stashed_ino = 0;
1475 fs->check_directory = 0;
1477 fs->write_inode = 0;