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 * If device, fifo or socket, check size is zero -- if not offer to
165 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
167 struct ext2_inode *inode = pctx->inode;
169 if ((LINUX_S_ISBLK(inode->i_mode) ||
170 LINUX_S_ISCHR(inode->i_mode) ||
171 LINUX_S_ISFIFO(inode->i_mode) ||
172 LINUX_S_ISSOCK(inode->i_mode)) &&
176 if(!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
180 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
184 void e2fsck_pass1(e2fsck_t ctx)
188 ext2_filsys fs = ctx->fs;
190 struct ext2_inode inode;
191 ext2_inode_scan scan;
193 #ifdef RESOURCE_TRACK
194 struct resource_track rtrack;
196 unsigned char frag, fsize;
197 struct problem_context pctx;
198 struct scan_callback_struct scan_struct;
199 struct ext2_super_block *sb = ctx->fs->super;
202 #ifdef RESOURCE_TRACK
203 init_resource_track(&rtrack);
205 clear_problem_context(&pctx);
207 if (!(ctx->options & E2F_OPT_PREEN))
208 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
211 mtrace_print("Pass 1");
214 #define EXT2_BPP(bits) (1UL << ((bits) - 2))
216 for (i=0; i < 4; i++) {
217 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(10+i);
218 max_sizes = max_sizes + EXT2_BPP(10+i) * EXT2_BPP(10+i);
219 max_sizes = (max_sizes +
220 (__u64) EXT2_BPP(10+i) * EXT2_BPP(10+i) *
222 max_sizes = (max_sizes * (1UL << (10+i))) - 1;
223 ext2_max_sizes[i] = max_sizes;
227 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
230 * Allocate bitmaps structures
232 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
233 &ctx->inode_used_map);
236 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
237 ctx->flags |= E2F_FLAG_ABORT;
240 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
241 _("directory inode map"), &ctx->inode_dir_map);
244 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
245 ctx->flags |= E2F_FLAG_ABORT;
248 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
249 _("regular file inode map"), &ctx->inode_reg_map);
252 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
253 ctx->flags |= E2F_FLAG_ABORT;
256 pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
257 &ctx->block_found_map);
260 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
261 ctx->flags |= E2F_FLAG_ABORT;
264 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
265 &ctx->inode_link_info);
267 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
268 ctx->flags |= E2F_FLAG_ABORT;
271 inodes_to_process = (struct process_inode_block *)
272 e2fsck_allocate_memory(ctx,
273 (ctx->process_inode_size *
274 sizeof(struct process_inode_block)),
275 "array of inodes to process");
276 process_inode_count = 0;
278 pctx.errcode = ext2fs_init_dblist(fs, 0);
280 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
281 ctx->flags |= E2F_FLAG_ABORT;
286 * If the last orphan field is set, clear it, since the pass1
287 * processing will automatically find and clear the orphans.
288 * In the future, we may want to try using the last_orphan
289 * linked list ourselves, but for now, we clear it so that the
290 * ext3 mount code won't get confused.
292 if (!(ctx->options & E2F_OPT_READONLY)) {
293 if (fs->super->s_last_orphan) {
294 fs->super->s_last_orphan = 0;
295 ext2fs_mark_super_dirty(fs);
299 mark_table_blocks(ctx);
300 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
301 "block interate buffer");
302 e2fsck_use_inode_shortcuts(ctx, 1);
303 ehandler_operation(_("doing inode scan"));
304 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
307 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
308 ctx->flags |= E2F_FLAG_ABORT;
311 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
312 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
314 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
315 ctx->flags |= E2F_FLAG_ABORT;
318 ctx->stashed_inode = &inode;
319 scan_struct.ctx = ctx;
320 scan_struct.block_buf = block_buf;
321 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
323 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
328 ctx->stashed_ino = ino;
329 if (inode.i_links_count) {
330 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
331 ino, inode.i_links_count);
333 pctx.num = inode.i_links_count;
334 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
335 ctx->flags |= E2F_FLAG_ABORT;
339 if (ino == EXT2_BAD_INO) {
340 struct process_block_struct pb;
342 pb.ino = EXT2_BAD_INO;
343 pb.num_blocks = pb.last_block = 0;
344 pb.num_illegal_blocks = 0;
345 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
350 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0,
351 block_buf, process_bad_block, &pb);
353 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
354 ctx->flags |= E2F_FLAG_ABORT;
357 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
358 clear_problem_context(&pctx);
361 if (ino == EXT2_ROOT_INO) {
363 * Make sure the root inode is a directory; if
364 * not, offer to clear it. It will be
365 * regnerated in pass #3.
367 if (!LINUX_S_ISDIR(inode.i_mode)) {
368 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
369 inode.i_dtime = time(0);
370 inode.i_links_count = 0;
371 ext2fs_icount_store(ctx->inode_link_info,
373 e2fsck_write_inode(ctx, ino, &inode,
379 * If dtime is set, offer to clear it. mke2fs
380 * version 0.2b created filesystems with the
381 * dtime field set for the root and lost+found
382 * directories. We won't worry about
383 * /lost+found, since that can be regenerated
384 * easily. But we will fix the root directory
387 if (inode.i_dtime && inode.i_links_count) {
388 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
390 e2fsck_write_inode(ctx, ino, &inode,
395 if (ino == EXT2_JOURNAL_INO) {
396 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
397 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
398 check_blocks(ctx, &pctx, block_buf);
401 if ((inode.i_blocks || inode.i_block[0]) &&
402 fix_problem(ctx, PR1_JOURNAL_INODE_NOT_CLEAR,
404 memset(&inode, 0, sizeof(inode));
405 e2fsck_write_inode(ctx, ino, &inode, "pass1");
408 if ((ino != EXT2_ROOT_INO) &&
409 (ino < EXT2_FIRST_INODE(fs->super))) {
412 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
413 if (ino == EXT2_BOOT_LOADER_INO) {
414 if (LINUX_S_ISDIR(inode.i_mode))
415 problem = PR_1_RESERVED_BAD_MODE;
417 if (inode.i_mode != 0)
418 problem = PR_1_RESERVED_BAD_MODE;
421 if (fix_problem(ctx, problem, &pctx)) {
423 e2fsck_write_inode(ctx, ino, &inode,
427 check_blocks(ctx, &pctx, block_buf);
431 * This code assumes that deleted inodes have
432 * i_links_count set to 0.
434 if (!inode.i_links_count) {
435 if (!inode.i_dtime && inode.i_mode) {
437 PR_1_ZERO_DTIME, &pctx)) {
438 inode.i_dtime = time(0);
439 e2fsck_write_inode(ctx, ino, &inode,
446 * n.b. 0.3c ext2fs code didn't clear i_links_count for
447 * deleted files. Oops.
449 * Since all new ext2 implementations get this right,
450 * we now assume that the case of non-zero
451 * i_links_count and non-zero dtime means that we
452 * should keep the file, not delete it.
456 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
458 e2fsck_write_inode(ctx, ino, &inode, "pass1");
462 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
463 switch (fs->super->s_creator_os) {
465 frag = inode.osd2.linux2.l_i_frag;
466 fsize = inode.osd2.linux2.l_i_fsize;
469 frag = inode.osd2.hurd2.h_i_frag;
470 fsize = inode.osd2.hurd2.h_i_fsize;
473 frag = inode.osd2.masix2.m_i_frag;
474 fsize = inode.osd2.masix2.m_i_fsize;
480 if (inode.i_faddr || frag || fsize
481 || inode.i_file_acl ||
482 (LINUX_S_ISDIR(inode.i_mode) && inode.i_dir_acl)) {
483 if (!ctx->inode_bad_map)
485 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
487 if (inode.i_flags & EXT2_IMAGIC_FL) {
489 if (!ctx->inode_imagic_map)
490 alloc_imagic_map(ctx);
491 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
494 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
495 inode.i_flags &= ~EXT2_IMAGIC_FL;
496 e2fsck_write_inode(ctx, ino,
502 if (LINUX_S_ISDIR(inode.i_mode)) {
503 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
504 e2fsck_add_dir_info(ctx, ino, 0);
505 ctx->fs_directory_count++;
506 } else if (LINUX_S_ISREG (inode.i_mode)) {
507 ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
508 ctx->fs_regular_count++;
509 } else if (LINUX_S_ISCHR (inode.i_mode) &&
510 e2fsck_pass1_check_device_inode(&inode)) {
511 check_immutable(ctx, &pctx);
512 check_size(ctx, &pctx);
513 ctx->fs_chardev_count++;
514 } else if (LINUX_S_ISBLK (inode.i_mode) &&
515 e2fsck_pass1_check_device_inode(&inode)) {
516 check_immutable(ctx, &pctx);
517 check_size(ctx, &pctx);
518 ctx->fs_blockdev_count++;
519 } else if (LINUX_S_ISLNK (inode.i_mode)) {
520 ctx->fs_symlinks_count++;
521 if (!inode.i_blocks) {
522 ctx->fs_fast_symlinks_count++;
526 else if (LINUX_S_ISFIFO (inode.i_mode) &&
527 e2fsck_pass1_check_device_inode(&inode)) {
528 check_immutable(ctx, &pctx);
529 check_size(ctx, &pctx);
530 ctx->fs_fifo_count++;
531 } else if ((LINUX_S_ISSOCK (inode.i_mode)) &&
532 e2fsck_pass1_check_device_inode(&inode)) {
533 check_immutable(ctx, &pctx);
534 check_size(ctx, &pctx);
535 ctx->fs_sockets_count++;
537 if (!ctx->inode_bad_map)
539 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
541 if (inode.i_block[EXT2_IND_BLOCK])
543 if (inode.i_block[EXT2_DIND_BLOCK])
544 ctx->fs_dind_count++;
545 if (inode.i_block[EXT2_TIND_BLOCK])
546 ctx->fs_tind_count++;
547 if (inode.i_block[EXT2_IND_BLOCK] ||
548 inode.i_block[EXT2_DIND_BLOCK] ||
549 inode.i_block[EXT2_TIND_BLOCK]) {
550 inodes_to_process[process_inode_count].ino = ino;
551 inodes_to_process[process_inode_count].inode = inode;
552 process_inode_count++;
554 check_blocks(ctx, &pctx, block_buf);
556 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
559 if (process_inode_count >= ctx->process_inode_size) {
560 process_inodes(ctx, block_buf);
562 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
566 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
567 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
569 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
570 if (!ctx->inode_bb_map)
572 ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
573 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
577 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
578 ctx->flags |= E2F_FLAG_ABORT;
582 process_inodes(ctx, block_buf);
583 ext2fs_close_inode_scan(scan);
584 ehandler_operation(0);
586 if (ctx->invalid_bitmaps)
587 handle_fs_bad_blocks(ctx);
589 if (ctx->flags & E2F_FLAG_RESTART) {
591 * Only the master copy of the superblock and block
592 * group descriptors are going to be written during a
593 * restart, so set the superblock to be used to be the
596 ctx->use_superblock = 0;
601 if (ctx->block_dup_map) {
602 if (ctx->options & E2F_OPT_PREEN) {
603 clear_problem_context(&pctx);
604 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
606 e2fsck_pass1_dupblocks(ctx, block_buf);
608 ext2fs_free_mem((void **) &inodes_to_process);
610 e2fsck_use_inode_shortcuts(ctx, 0);
612 ext2fs_free_mem((void **) &block_buf);
614 if (ctx->large_files) {
615 if (!(sb->s_feature_ro_compat &
616 EXT2_FEATURE_RO_COMPAT_LARGE_FILE) &&
617 fix_problem(ctx, PR_1_FEATURE_LARGE_FILES, &pctx)) {
618 sb->s_feature_ro_compat |=
619 EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
620 ext2fs_mark_super_dirty(fs);
622 if (sb->s_rev_level == EXT2_GOOD_OLD_REV &&
623 fix_problem(ctx, PR_1_FS_REV_LEVEL, &pctx)) {
624 ext2fs_update_dynamic_rev(fs);
625 ext2fs_mark_super_dirty(fs);
627 } else if (!ctx->large_files &&
628 (sb->s_feature_ro_compat &
629 EXT2_FEATURE_RO_COMPAT_LARGE_FILE)) {
630 if (fs->flags & EXT2_FLAG_RW) {
631 sb->s_feature_ro_compat &=
632 ~EXT2_FEATURE_RO_COMPAT_LARGE_FILE;
633 ext2fs_mark_super_dirty(fs);
637 #ifdef RESOURCE_TRACK
638 if (ctx->options & E2F_OPT_TIME2) {
639 e2fsck_clear_progbar(ctx);
640 print_resource_track(_("Pass 1"), &rtrack);
646 * When the inode_scan routines call this callback at the end of the
647 * glock group, call process_inodes.
649 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
650 dgrp_t group, void * priv_data)
652 struct scan_callback_struct *scan_struct;
655 scan_struct = (struct scan_callback_struct *) priv_data;
656 ctx = scan_struct->ctx;
658 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
661 if ((ctx->progress)(ctx, 1, group+1,
662 ctx->fs->group_desc_count))
663 return EXT2_ET_CANCEL_REQUESTED;
669 * Process the inodes in the "inodes to process" list.
671 static void process_inodes(e2fsck_t ctx, char *block_buf)
674 struct ext2_inode *old_stashed_inode;
675 ino_t old_stashed_ino;
676 const char *old_operation;
678 struct problem_context pctx;
681 printf("begin process_inodes: ");
683 if (process_inode_count == 0)
685 old_operation = ehandler_operation(0);
686 old_stashed_inode = ctx->stashed_inode;
687 old_stashed_ino = ctx->stashed_ino;
688 qsort(inodes_to_process, process_inode_count,
689 sizeof(struct process_inode_block), process_inode_cmp);
690 clear_problem_context(&pctx);
691 for (i=0; i < process_inode_count; i++) {
692 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
693 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
696 printf("%u ", pctx.ino);
698 sprintf(buf, _("reading indirect blocks of inode %lu"),
700 ehandler_operation(buf);
701 check_blocks(ctx, &pctx, block_buf);
702 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
705 ctx->stashed_inode = old_stashed_inode;
706 ctx->stashed_ino = old_stashed_ino;
707 process_inode_count = 0;
709 printf("end process inodes\n");
711 ehandler_operation(old_operation);
714 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
716 const struct process_inode_block *ib_a =
717 (const struct process_inode_block *) a;
718 const struct process_inode_block *ib_b =
719 (const struct process_inode_block *) b;
721 return (ib_a->inode.i_block[EXT2_IND_BLOCK] -
722 ib_b->inode.i_block[EXT2_IND_BLOCK]);
726 * This procedure will allocate the inode bad map table
728 static void alloc_bad_map(e2fsck_t ctx)
730 struct problem_context pctx;
732 clear_problem_context(&pctx);
734 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs, _("bad inode map"),
735 &ctx->inode_bad_map);
738 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
739 /* Should never get here */
740 ctx->flags |= E2F_FLAG_ABORT;
746 * This procedure will allocate the inode "bb" (badblock) map table
748 static void alloc_bb_map(e2fsck_t ctx)
750 struct problem_context pctx;
752 clear_problem_context(&pctx);
753 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
754 _("inode in bad block map"),
758 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
759 /* Should never get here */
760 ctx->flags |= E2F_FLAG_ABORT;
766 * This procedure will allocate the inode imagic table
768 static void alloc_imagic_map(e2fsck_t ctx)
770 struct problem_context pctx;
772 clear_problem_context(&pctx);
773 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
774 _("imagic inode map"),
775 &ctx->inode_imagic_map);
778 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
779 /* Should never get here */
780 ctx->flags |= E2F_FLAG_ABORT;
786 * Marks a block as in use, setting the dup_map if it's been set
787 * already. Called by process_block and process_bad_block.
789 * WARNING: Assumes checks have already been done to make sure block
790 * is valid. This is true in both process_block and process_bad_block.
792 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
794 struct problem_context pctx;
796 clear_problem_context(&pctx);
798 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
799 if (!ctx->block_dup_map) {
800 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
801 _("multiply claimed block map"),
802 &ctx->block_dup_map);
805 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
807 /* Should never get here */
808 ctx->flags |= E2F_FLAG_ABORT;
812 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
814 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
819 * This subroutine is called on each inode to account for all of the
820 * blocks used by that inode.
822 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
825 ext2_filsys fs = ctx->fs;
826 struct process_block_struct pb;
827 ino_t ino = pctx->ino;
828 struct ext2_inode *inode = pctx->inode;
832 if (!ext2fs_inode_has_valid_blocks(pctx->inode))
836 pb.num_blocks = pb.last_block = 0;
837 pb.num_illegal_blocks = 0;
838 pb.suppress = 0; pb.clear = 0;
841 pb.previous_block = 0;
842 pb.is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
848 if (inode->i_flags & EXT2_COMPRBLK_FL) {
849 if (fs->super->s_feature_incompat &
850 EXT2_FEATURE_INCOMPAT_COMPRESSION)
853 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
854 inode->i_flags &= ~EXT2_COMPRBLK_FL;
855 e2fsck_write_inode(ctx, ino, inode,
861 pctx->errcode = ext2fs_block_iterate2(fs, ino,
862 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
863 block_buf, process_block, &pb);
864 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
866 end_problem_latch(ctx, PR_LATCH_BLOCK);
868 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
870 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
871 ctx->fs_fragmented++;
874 e2fsck_read_inode(ctx, ino, inode, "check_blocks");
875 inode->i_links_count = 0;
876 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
877 inode->i_dtime = time(0);
878 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
879 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
880 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
881 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
883 * The inode was probably partially accounted for
884 * before processing was aborted, so we need to
885 * restart the pass 1 scan.
887 ctx->flags |= E2F_FLAG_RESTART;
891 pb.num_blocks *= (fs->blocksize / 512);
893 printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
894 ino, inode->i_size, pb.last_block, inode->i_blocks,
897 if (!pb.num_blocks && pb.is_dir) {
898 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
899 inode->i_links_count = 0;
900 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
901 inode->i_dtime = time(0);
902 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
903 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
904 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
905 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
906 ctx->fs_directory_count--;
911 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
912 if ((nblock > (pb.last_block + 1)) ||
913 ((inode->i_size & (fs->blocksize-1)) != 0))
915 else if (nblock < (pb.last_block + 1)) {
916 if (((pb.last_block + 1) - nblock) >
917 fs->super->s_prealloc_dir_blocks)
921 size = inode->i_size + ((__u64) inode->i_size_high << 32);
922 if ((size < pb.last_block * fs->blocksize))
924 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
928 pctx->num = (pb.last_block+1) * fs->blocksize;
929 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
930 inode->i_size = pctx->num;
932 inode->i_size_high = pctx->num >> 32;
933 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
937 if (!pb.is_dir && inode->i_size_high)
939 if (pb.num_blocks != inode->i_blocks) {
940 pctx->num = pb.num_blocks;
941 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
942 inode->i_blocks = pb.num_blocks;
943 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
951 * Helper function called by process block when an illegal block is
952 * found. It returns a description about why the block is illegal
954 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
958 static char problem[80];
960 super = fs->super->s_first_data_block;
961 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
963 sprintf(problem, "< FIRSTBLOCK (%u)", super);
965 } else if (block >= fs->super->s_blocks_count) {
966 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
969 for (i = 0; i < fs->group_desc_count; i++) {
970 if (block == super) {
971 sprintf(problem, "is the superblock in group %d", i);
975 block <= (super + fs->desc_blocks)) {
976 sprintf(problem, "is in the group descriptors "
980 if (block == fs->group_desc[i].bg_block_bitmap) {
981 sprintf(problem, "is the block bitmap of group %d", i);
984 if (block == fs->group_desc[i].bg_inode_bitmap) {
985 sprintf(problem, "is the inode bitmap of group %d", i);
988 if (block >= fs->group_desc[i].bg_inode_table &&
989 (block < fs->group_desc[i].bg_inode_table
990 + fs->inode_blocks_per_group)) {
991 sprintf(problem, "is in the inode table of group %d",
995 super += fs->super->s_blocks_per_group;
1002 * This is a helper function for check_blocks().
1004 static int process_block(ext2_filsys fs,
1006 e2_blkcnt_t blockcnt,
1011 struct process_block_struct *p;
1012 struct problem_context *pctx;
1013 blk_t blk = *block_nr;
1018 p = (struct process_block_struct *) priv_data;
1022 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
1023 /* todo: Check that the comprblk_fl is high, that the
1024 blkaddr pattern looks right (all non-holes up to
1025 first EXT2FS_COMPRESSED_BLKADDR, then all
1026 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
1027 that the feature_incompat bit is high, and that the
1028 inode is a regular file. If we're doing a "full
1029 check" (a concept introduced to e2fsck by e2compr,
1030 meaning that we look at data blocks as well as
1031 metadata) then call some library routine that
1032 checks the compressed data. I'll have to think
1033 about this, because one particularly important
1034 problem to be able to fix is to recalculate the
1035 cluster size if necessary. I think that perhaps
1036 we'd better do most/all e2compr-specific checks
1037 separately, after the non-e2compr checks. If not
1038 doing a full check, it may be useful to test that
1039 the personality is linux; e.g. if it isn't then
1040 perhaps this really is just an illegal block. */
1045 if (p->is_dir == 0) {
1047 * Should never happen, since only directories
1048 * get called with BLOCK_FLAG_HOLE
1051 printf("process_block() called with blk == 0, "
1052 "blockcnt=%d, inode %lu???\n",
1059 if (blockcnt * fs->blocksize < p->inode->i_size) {
1061 printf("Missing block (#%d) in directory inode %lu!\n",
1070 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
1075 * Simplistic fragmentation check. We merely require that the
1076 * file be contiguous. (Which can never be true for really
1077 * big files that are greater than a block group.)
1079 if (!HOLE_BLKADDR(p->previous_block)) {
1080 if (p->previous_block+1 != blk)
1083 p->previous_block = blk;
1085 if (blk < fs->super->s_first_data_block ||
1086 blk >= fs->super->s_blocks_count)
1087 problem = PR_1_ILLEGAL_BLOCK_NUM;
1090 p->num_illegal_blocks++;
1091 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
1092 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
1096 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
1098 set_latch_flags(PR_LATCH_BLOCK,
1103 pctx->blkcount = blockcnt;
1104 if (fix_problem(ctx, problem, pctx)) {
1105 blk = *block_nr = 0;
1106 ret_code = BLOCK_CHANGED;
1111 pctx->blkcount = -1;
1114 mark_block_used(ctx, blk);
1117 p->last_block = blockcnt;
1119 if (p->is_dir && (blockcnt >= 0)) {
1120 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1122 if (pctx->errcode) {
1124 pctx->num = blockcnt;
1125 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1126 /* Should never get here */
1127 ctx->flags |= E2F_FLAG_ABORT;
1134 static void bad_block_indirect(e2fsck_t ctx, blk_t blk)
1136 struct problem_context pctx;
1138 clear_problem_context(&pctx);
1140 * Prompt to see if we should continue or not.
1142 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, &pctx))
1143 ctx->flags |= E2F_FLAG_ABORT;
1146 static int process_bad_block(ext2_filsys fs,
1148 e2_blkcnt_t blockcnt,
1153 struct process_block_struct *p;
1154 blk_t blk = *block_nr;
1157 struct problem_context *pctx;
1161 * Note: This function processes blocks for the bad blocks
1162 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
1168 p = (struct process_block_struct *) priv_data;
1172 pctx->ino = EXT2_BAD_INO;
1174 pctx->blkcount = blockcnt;
1176 if ((blk < fs->super->s_first_data_block) ||
1177 (blk >= fs->super->s_blocks_count)) {
1178 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1180 return BLOCK_CHANGED;
1186 if (ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1187 bad_block_indirect(ctx, blk);
1188 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1191 mark_block_used(ctx, blk);
1195 printf ("DEBUG: Marking %u as bad.\n", blk);
1197 ctx->fs_badblocks_count++;
1199 * If the block is not used, then mark it as used and return.
1200 * If it is already marked as found, this must mean that
1201 * there's an overlap between the filesystem table blocks
1202 * (bitmaps and inode table) and the bad block list.
1204 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1205 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1209 * Try to find the where the filesystem block was used...
1211 first_block = fs->super->s_first_data_block;
1213 for (i = 0; i < fs->group_desc_count; i++ ) {
1216 if (!ext2fs_bg_has_super(fs, i))
1218 if (blk == first_block) {
1220 if (fix_problem(ctx,
1221 PR_1_BAD_PRIMARY_SUPERBLOCK,
1224 return BLOCK_CHANGED;
1228 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1231 if ((blk > first_block) &&
1232 (blk <= first_block + fs->desc_blocks)) {
1234 pctx->blk = *block_nr;
1235 if (fix_problem(ctx,
1236 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1238 return BLOCK_CHANGED;
1242 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1246 if (blk == fs->group_desc[i].bg_block_bitmap) {
1247 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1248 ctx->invalid_block_bitmap_flag[i]++;
1249 ctx->invalid_bitmaps++;
1253 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1254 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1255 ctx->invalid_inode_bitmap_flag[i]++;
1256 ctx->invalid_bitmaps++;
1260 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1261 (blk < (fs->group_desc[i].bg_inode_table +
1262 fs->inode_blocks_per_group))) {
1264 * If there are bad blocks in the inode table,
1265 * the inode scan code will try to do
1266 * something reasonable automatically.
1270 first_block += fs->super->s_blocks_per_group;
1273 * If we've gotten to this point, then the only
1274 * possibility is that the bad block inode meta data
1275 * is using a bad block.
1277 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1278 p->inode->i_block[EXT2_DIND_BLOCK]) {
1279 bad_block_indirect(ctx, blk);
1280 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1287 /* Warn user that the block wasn't claimed */
1288 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1293 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
1294 const char *name, int num, blk_t *new_block)
1296 ext2_filsys fs = ctx->fs;
1297 blk_t old_block = *new_block;
1300 struct problem_context pctx;
1302 clear_problem_context(&pctx);
1305 pctx.blk = old_block;
1308 pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
1309 first_block + fs->super->s_blocks_per_group,
1310 num, ctx->block_found_map, new_block);
1313 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
1314 ext2fs_unmark_valid(fs);
1317 pctx.errcode = ext2fs_get_mem(fs->blocksize, (void **) &buf);
1319 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
1320 ext2fs_unmark_valid(fs);
1323 ext2fs_mark_super_dirty(fs);
1324 pctx.blk2 = *new_block;
1325 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
1326 PR_1_RELOC_TO), &pctx);
1328 for (i = 0; i < num; i++) {
1330 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
1332 pctx.errcode = io_channel_read_blk(fs->io,
1333 old_block + i, 1, buf);
1335 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
1337 memset(buf, 0, fs->blocksize);
1339 pctx.blk = (*new_block) + i;
1340 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
1343 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
1345 ext2fs_free_mem((void **) &buf);
1349 * This routine gets called at the end of pass 1 if bad blocks are
1350 * detected in the superblock, group descriptors, inode_bitmaps, or
1351 * block bitmaps. At this point, all of the blocks have been mapped
1352 * out, so we can try to allocate new block(s) to replace the bad
1355 static void handle_fs_bad_blocks(e2fsck_t ctx)
1357 ext2_filsys fs = ctx->fs;
1359 int first_block = fs->super->s_first_data_block;
1361 for (i = 0; i < fs->group_desc_count; i++) {
1362 if (ctx->invalid_block_bitmap_flag[i]) {
1363 new_table_block(ctx, first_block, i, _("block bitmap"),
1364 1, &fs->group_desc[i].bg_block_bitmap);
1366 if (ctx->invalid_inode_bitmap_flag[i]) {
1367 new_table_block(ctx, first_block, i, _("inode bitmap"),
1368 1, &fs->group_desc[i].bg_inode_bitmap);
1370 if (ctx->invalid_inode_table_flag[i]) {
1371 new_table_block(ctx, first_block, i, _("inode table"),
1372 fs->inode_blocks_per_group,
1373 &fs->group_desc[i].bg_inode_table);
1374 ctx->flags |= E2F_FLAG_RESTART;
1376 first_block += fs->super->s_blocks_per_group;
1378 ctx->invalid_bitmaps = 0;
1382 * This routine marks all blocks which are used by the superblock,
1383 * group descriptors, inode bitmaps, and block bitmaps.
1385 static void mark_table_blocks(e2fsck_t ctx)
1387 ext2_filsys fs = ctx->fs;
1390 struct problem_context pctx;
1392 clear_problem_context(&pctx);
1394 block = fs->super->s_first_data_block;
1395 for (i = 0; i < fs->group_desc_count; i++) {
1398 if (ext2fs_bg_has_super(fs, i)) {
1400 * Mark this group's copy of the superblock
1402 ext2fs_mark_block_bitmap(ctx->block_found_map, block);
1405 * Mark this group's copy of the descriptors
1407 for (j = 0; j < fs->desc_blocks; j++) {
1408 ext2fs_mark_block_bitmap(ctx->block_found_map,
1414 * Mark the blocks used for the inode table
1416 if (fs->group_desc[i].bg_inode_table) {
1417 for (j = 0, b = fs->group_desc[i].bg_inode_table;
1418 j < fs->inode_blocks_per_group;
1420 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1423 if (fix_problem(ctx,
1424 PR_1_ITABLE_CONFLICT, &pctx)) {
1425 ctx->invalid_inode_table_flag[i]++;
1426 ctx->invalid_bitmaps++;
1429 ext2fs_mark_block_bitmap(ctx->block_found_map,
1436 * Mark block used for the block bitmap
1438 if (fs->group_desc[i].bg_block_bitmap) {
1439 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1440 fs->group_desc[i].bg_block_bitmap)) {
1441 pctx.blk = fs->group_desc[i].bg_block_bitmap;
1442 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
1443 ctx->invalid_block_bitmap_flag[i]++;
1444 ctx->invalid_bitmaps++;
1447 ext2fs_mark_block_bitmap(ctx->block_found_map,
1448 fs->group_desc[i].bg_block_bitmap);
1453 * Mark block used for the inode bitmap
1455 if (fs->group_desc[i].bg_inode_bitmap) {
1456 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1457 fs->group_desc[i].bg_inode_bitmap)) {
1458 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
1459 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
1460 ctx->invalid_inode_bitmap_flag[i]++;
1461 ctx->invalid_bitmaps++;
1464 ext2fs_mark_block_bitmap(ctx->block_found_map,
1465 fs->group_desc[i].bg_inode_bitmap);
1468 block += fs->super->s_blocks_per_group;
1473 * Thes subroutines short circuits ext2fs_get_blocks and
1474 * ext2fs_check_directory; we use them since we already have the inode
1475 * structure, so there's no point in letting the ext2fs library read
1478 static errcode_t pass1_get_blocks(ext2_filsys fs, ino_t ino, blk_t *blocks)
1480 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1483 if (ino != ctx->stashed_ino)
1484 return EXT2_ET_CALLBACK_NOTHANDLED;
1486 for (i=0; i < EXT2_N_BLOCKS; i++)
1487 blocks[i] = ctx->stashed_inode->i_block[i];
1491 static errcode_t pass1_read_inode(ext2_filsys fs, ino_t ino,
1492 struct ext2_inode *inode)
1494 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1496 if (ino != ctx->stashed_ino)
1497 return EXT2_ET_CALLBACK_NOTHANDLED;
1498 *inode = *ctx->stashed_inode;
1502 static errcode_t pass1_write_inode(ext2_filsys fs, ino_t ino,
1503 struct ext2_inode *inode)
1505 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1507 if (ino == ctx->stashed_ino)
1508 *ctx->stashed_inode = *inode;
1509 return EXT2_ET_CALLBACK_NOTHANDLED;
1512 static errcode_t pass1_check_directory(ext2_filsys fs, ino_t ino)
1514 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1516 if (ino != ctx->stashed_ino)
1517 return EXT2_ET_CALLBACK_NOTHANDLED;
1519 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
1520 return EXT2_ET_NO_DIRECTORY;
1524 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
1526 ext2_filsys fs = ctx->fs;
1529 fs->get_blocks = pass1_get_blocks;
1530 fs->check_directory = pass1_check_directory;
1531 fs->read_inode = pass1_read_inode;
1532 fs->write_inode = pass1_write_inode;
1533 ctx->stashed_ino = 0;
1536 fs->check_directory = 0;
1538 fs->write_inode = 0;