2 * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
11 * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12 * and applies the following tests to each inode:
14 * - The mode field of the inode must be legal.
15 * - The size and block count fields of the inode are correct.
16 * - A data block must not be used by another inode
18 * Pass 1 also gathers the collects the following information:
20 * - A bitmap of which inodes are in use. (inode_used_map)
21 * - A bitmap of which inodes are directories. (inode_dir_map)
22 * - A bitmap of which inodes are regular files. (inode_reg_map)
23 * - A bitmap of which inodes have bad fields. (inode_bad_map)
24 * - A bitmap of which inodes are in bad blocks. (inode_bb_map)
25 * - A bitmap of which inodes are imagic inodes. (inode_imagic_map)
26 * - A bitmap of which blocks are in use. (block_found_map)
27 * - A bitmap of which blocks are in use by two inodes (block_dup_map)
28 * - The data blocks of the directory inodes. (dir_map)
30 * Pass 1 is designed to stash away enough information so that the
31 * other passes should not need to read in the inode information
32 * during the normal course of a filesystem check. (Althogh if an
33 * inconsistency is detected, other passes may need to read in an
36 * Note that pass 1B will be invoked if there are any duplicate blocks
40 #define _GNU_SOURCE 1 /* get strnlen() */
48 #include <ext2fs/ext2_ext_attr.h>
52 #ifdef NO_INLINE_FUNCS
55 #define _INLINE_ inline
58 static int process_block(ext2_filsys fs, blk_t *blocknr,
59 e2_blkcnt_t blockcnt, blk_t ref_blk,
60 int ref_offset, void *priv_data);
61 static int process_bad_block(ext2_filsys fs, blk_t *block_nr,
62 e2_blkcnt_t blockcnt, blk_t ref_blk,
63 int ref_offset, void *priv_data);
64 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
66 static void mark_table_blocks(e2fsck_t ctx);
67 static void alloc_bb_map(e2fsck_t ctx);
68 static void alloc_imagic_map(e2fsck_t ctx);
69 static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
70 static void handle_fs_bad_blocks(e2fsck_t ctx);
71 static void process_inodes(e2fsck_t ctx, char *block_buf);
72 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
73 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
74 dgrp_t group, void * priv_data);
75 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
76 char *block_buf, int adjust_sign);
77 /* static char *describe_illegal_block(ext2_filsys fs, blk_t block); */
79 struct process_block_struct {
81 int is_dir:1, is_reg:1, clear:1, suppress:1,
82 fragmented:1, compressed:1, bbcheck:1;
85 e2_blkcnt_t last_block;
86 int num_illegal_blocks;
88 struct ext2_inode *inode;
89 struct problem_context *pctx;
90 ext2fs_block_bitmap fs_meta_blocks;
94 struct process_inode_block {
96 struct ext2_inode inode;
99 struct scan_callback_struct {
105 * For the inodes to process list.
107 static struct process_inode_block *inodes_to_process;
108 static int process_inode_count;
110 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
111 EXT2_MIN_BLOCK_LOG_SIZE + 1];
114 * Free all memory allocated by pass1 in preparation for restarting
117 static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
119 ext2fs_free_mem(&inodes_to_process);
120 inodes_to_process = 0;
124 * Check to make sure a device inode is real. Returns 1 if the device
125 * checks out, 0 if not.
127 * Note: this routine is now also used to check FIFO's and Sockets,
128 * since they have the same requirement; the i_block fields should be
131 int e2fsck_pass1_check_device_inode(ext2_filsys fs, struct ext2_inode *inode)
136 * If i_blocks is non-zero, or the index flag is set, then
137 * this is a bogus device/fifo/socket
139 if ((ext2fs_inode_data_blocks(fs, inode) != 0) ||
140 (inode->i_flags & EXT2_INDEX_FL))
144 * We should be able to do the test below all the time, but
145 * because the kernel doesn't forcibly clear the device
146 * inode's additional i_block fields, there are some rare
147 * occasions when a legitimate device inode will have non-zero
148 * additional i_block fields. So for now, we only complain
149 * when the immutable flag is set, which should never happen
150 * for devices. (And that's when the problem is caused, since
151 * you can't set or clear immutable flags for devices.) Once
152 * the kernel has been fixed we can change this...
154 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
155 for (i=4; i < EXT2_N_BLOCKS; i++)
156 if (inode->i_block[i])
163 * Check to make sure a symlink inode is real. Returns 1 if the symlink
164 * checks out, 0 if not.
166 int e2fsck_pass1_check_symlink(ext2_filsys fs, struct ext2_inode *inode,
173 if ((inode->i_size_high || inode->i_size == 0) ||
174 (inode->i_flags & EXT2_INDEX_FL))
177 blocks = ext2fs_inode_data_blocks(fs, inode);
179 if ((inode->i_size >= fs->blocksize) ||
180 (blocks != fs->blocksize >> 9) ||
181 (inode->i_block[0] < fs->super->s_first_data_block) ||
182 (inode->i_block[0] >= fs->super->s_blocks_count))
185 for (i = 1; i < EXT2_N_BLOCKS; i++)
186 if (inode->i_block[i])
189 if (io_channel_read_blk(fs->io, inode->i_block[0], 1, buf))
192 len = strnlen(buf, fs->blocksize);
193 if (len == fs->blocksize)
196 if (inode->i_size >= sizeof(inode->i_block))
199 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
200 if (len == sizeof(inode->i_block))
203 if (len != inode->i_size)
209 * If the immutable (or append-only) flag is set on the inode, offer
212 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
213 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
215 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
218 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
221 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
222 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
226 * If device, fifo or socket, check size is zero -- if not offer to
229 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
231 struct ext2_inode *inode = pctx->inode;
233 if ((inode->i_size == 0) && (inode->i_size_high == 0))
236 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
240 inode->i_size_high = 0;
241 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
245 void e2fsck_pass1(e2fsck_t ctx)
249 ext2_filsys fs = ctx->fs;
251 struct ext2_inode inode;
252 ext2_inode_scan scan;
254 #ifdef RESOURCE_TRACK
255 struct resource_track rtrack;
257 unsigned char frag, fsize;
258 struct problem_context pctx;
259 struct scan_callback_struct scan_struct;
260 struct ext2_super_block *sb = ctx->fs->super;
262 int busted_fs_time = 0;
264 #ifdef RESOURCE_TRACK
265 init_resource_track(&rtrack);
267 clear_problem_context(&pctx);
269 if (!(ctx->options & E2F_OPT_PREEN))
270 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
272 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
273 !(ctx->options & E2F_OPT_NO)) {
274 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
275 ctx->dirs_to_hash = 0;
279 mtrace_print("Pass 1");
282 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
284 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
285 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
286 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
287 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
288 max_sizes = (max_sizes * (1UL << i)) - 1;
289 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
293 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
296 * Allocate bitmaps structures
298 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
299 &ctx->inode_used_map);
302 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
303 ctx->flags |= E2F_FLAG_ABORT;
306 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
307 _("directory inode map"), &ctx->inode_dir_map);
310 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
311 ctx->flags |= E2F_FLAG_ABORT;
314 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
315 _("regular file inode map"), &ctx->inode_reg_map);
318 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
319 ctx->flags |= E2F_FLAG_ABORT;
322 pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
323 &ctx->block_found_map);
326 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
327 ctx->flags |= E2F_FLAG_ABORT;
330 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
331 &ctx->inode_link_info);
333 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
334 ctx->flags |= E2F_FLAG_ABORT;
337 inodes_to_process = (struct process_inode_block *)
338 e2fsck_allocate_memory(ctx,
339 (ctx->process_inode_size *
340 sizeof(struct process_inode_block)),
341 "array of inodes to process");
342 process_inode_count = 0;
344 pctx.errcode = ext2fs_init_dblist(fs, 0);
346 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
347 ctx->flags |= E2F_FLAG_ABORT;
352 * If the last orphan field is set, clear it, since the pass1
353 * processing will automatically find and clear the orphans.
354 * In the future, we may want to try using the last_orphan
355 * linked list ourselves, but for now, we clear it so that the
356 * ext3 mount code won't get confused.
358 if (!(ctx->options & E2F_OPT_READONLY)) {
359 if (fs->super->s_last_orphan) {
360 fs->super->s_last_orphan = 0;
361 ext2fs_mark_super_dirty(fs);
365 mark_table_blocks(ctx);
366 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
367 "block interate buffer");
368 e2fsck_use_inode_shortcuts(ctx, 1);
369 ehandler_operation(_("doing inode scan"));
370 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
373 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
374 ctx->flags |= E2F_FLAG_ABORT;
377 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
378 ctx->stashed_inode = &inode;
379 scan_struct.ctx = ctx;
380 scan_struct.block_buf = block_buf;
381 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
383 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
385 if (fs->super->s_wtime < fs->super->s_inodes_count)
389 pctx.errcode = ext2fs_get_next_inode(scan, &ino, &inode);
390 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
392 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
393 if (!ctx->inode_bb_map)
395 ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
396 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
400 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
401 ctx->flags |= E2F_FLAG_ABORT;
408 ctx->stashed_ino = ino;
409 if (inode.i_links_count) {
410 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
411 ino, inode.i_links_count);
413 pctx.num = inode.i_links_count;
414 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
415 ctx->flags |= E2F_FLAG_ABORT;
419 if (ino == EXT2_BAD_INO) {
420 struct process_block_struct pb;
422 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
426 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
427 ctx->flags |= E2F_FLAG_ABORT;
430 pb.ino = EXT2_BAD_INO;
431 pb.num_blocks = pb.last_block = 0;
432 pb.num_illegal_blocks = 0;
433 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
434 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
438 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0,
439 block_buf, process_bad_block, &pb);
440 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
442 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
443 ctx->flags |= E2F_FLAG_ABORT;
447 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
448 ctx->flags |= E2F_FLAG_ABORT;
451 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
452 clear_problem_context(&pctx);
454 } else if (ino == EXT2_ROOT_INO) {
456 * Make sure the root inode is a directory; if
457 * not, offer to clear it. It will be
458 * regnerated in pass #3.
460 if (!LINUX_S_ISDIR(inode.i_mode)) {
461 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
462 inode.i_dtime = time(0);
463 inode.i_links_count = 0;
464 ext2fs_icount_store(ctx->inode_link_info,
466 e2fsck_write_inode(ctx, ino, &inode,
472 * If dtime is set, offer to clear it. mke2fs
473 * version 0.2b created filesystems with the
474 * dtime field set for the root and lost+found
475 * directories. We won't worry about
476 * /lost+found, since that can be regenerated
477 * easily. But we will fix the root directory
480 if (inode.i_dtime && inode.i_links_count) {
481 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
483 e2fsck_write_inode(ctx, ino, &inode,
487 } else if (ino == EXT2_JOURNAL_INO) {
488 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
489 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
490 if (!LINUX_S_ISREG(inode.i_mode) &&
491 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
493 inode.i_mode = LINUX_S_IFREG;
494 e2fsck_write_inode(ctx, ino, &inode,
497 check_blocks(ctx, &pctx, block_buf);
500 if ((inode.i_links_count || inode.i_blocks ||
501 inode.i_blocks || inode.i_block[0]) &&
502 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
504 memset(&inode, 0, sizeof(inode));
505 ext2fs_icount_store(ctx->inode_link_info,
507 e2fsck_write_inode(ctx, ino, &inode, "pass1");
509 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
512 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
513 if (ino == EXT2_BOOT_LOADER_INO) {
514 if (LINUX_S_ISDIR(inode.i_mode))
515 problem = PR_1_RESERVED_BAD_MODE;
516 } else if (ino == EXT2_RESIZE_INO) {
518 !LINUX_S_ISREG(inode.i_mode))
519 problem = PR_1_RESERVED_BAD_MODE;
521 if (inode.i_mode != 0)
522 problem = PR_1_RESERVED_BAD_MODE;
525 if (fix_problem(ctx, problem, &pctx)) {
527 e2fsck_write_inode(ctx, ino, &inode,
531 check_blocks(ctx, &pctx, block_buf);
535 * Check for inodes who might have been part of the
536 * orphaned list linked list. They should have gotten
537 * dealt with by now, unless the list had somehow been
540 * FIXME: In the future, inodes which are still in use
541 * (and which are therefore) pending truncation should
542 * be handled specially. Right now we just clear the
543 * dtime field, and the normal e2fsck handling of
544 * inodes where i_size and the inode blocks are
545 * inconsistent is to fix i_size, instead of releasing
546 * the extra blocks. This won't catch the inodes that
547 * was at the end of the orphan list, but it's better
548 * than nothing. The right answer is that there
549 * shouldn't be any bugs in the orphan list handling. :-)
551 if (inode.i_dtime && !busted_fs_time &&
552 inode.i_dtime < ctx->fs->super->s_inodes_count) {
553 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
554 inode.i_dtime = inode.i_links_count ?
556 e2fsck_write_inode(ctx, ino, &inode,
562 * This code assumes that deleted inodes have
563 * i_links_count set to 0.
565 if (!inode.i_links_count) {
566 if (!inode.i_dtime && inode.i_mode) {
568 PR_1_ZERO_DTIME, &pctx)) {
569 inode.i_dtime = time(0);
570 e2fsck_write_inode(ctx, ino, &inode,
577 * n.b. 0.3c ext2fs code didn't clear i_links_count for
578 * deleted files. Oops.
580 * Since all new ext2 implementations get this right,
581 * we now assume that the case of non-zero
582 * i_links_count and non-zero dtime means that we
583 * should keep the file, not delete it.
587 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
589 e2fsck_write_inode(ctx, ino, &inode, "pass1");
593 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
594 switch (fs->super->s_creator_os) {
596 frag = inode.osd2.linux2.l_i_frag;
597 fsize = inode.osd2.linux2.l_i_fsize;
600 frag = inode.osd2.hurd2.h_i_frag;
601 fsize = inode.osd2.hurd2.h_i_fsize;
604 frag = inode.osd2.masix2.m_i_frag;
605 fsize = inode.osd2.masix2.m_i_fsize;
611 if (inode.i_faddr || frag || fsize ||
612 (LINUX_S_ISDIR(inode.i_mode) && inode.i_dir_acl))
613 mark_inode_bad(ctx, ino);
614 if (inode.i_flags & EXT2_IMAGIC_FL) {
616 if (!ctx->inode_imagic_map)
617 alloc_imagic_map(ctx);
618 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
621 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
622 inode.i_flags &= ~EXT2_IMAGIC_FL;
623 e2fsck_write_inode(ctx, ino,
629 if (LINUX_S_ISDIR(inode.i_mode)) {
630 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
631 e2fsck_add_dir_info(ctx, ino, 0);
632 ctx->fs_directory_count++;
633 } else if (LINUX_S_ISREG (inode.i_mode)) {
634 ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
635 ctx->fs_regular_count++;
636 } else if (LINUX_S_ISCHR (inode.i_mode) &&
637 e2fsck_pass1_check_device_inode(fs, &inode)) {
638 check_immutable(ctx, &pctx);
639 check_size(ctx, &pctx);
640 ctx->fs_chardev_count++;
641 } else if (LINUX_S_ISBLK (inode.i_mode) &&
642 e2fsck_pass1_check_device_inode(fs, &inode)) {
643 check_immutable(ctx, &pctx);
644 check_size(ctx, &pctx);
645 ctx->fs_blockdev_count++;
646 } else if (LINUX_S_ISLNK (inode.i_mode) &&
647 e2fsck_pass1_check_symlink(fs, &inode, block_buf)) {
648 check_immutable(ctx, &pctx);
649 ctx->fs_symlinks_count++;
650 if (ext2fs_inode_data_blocks(fs, &inode) == 0) {
651 ctx->fs_fast_symlinks_count++;
652 check_blocks(ctx, &pctx, block_buf);
656 else if (LINUX_S_ISFIFO (inode.i_mode) &&
657 e2fsck_pass1_check_device_inode(fs, &inode)) {
658 check_immutable(ctx, &pctx);
659 check_size(ctx, &pctx);
660 ctx->fs_fifo_count++;
661 } else if ((LINUX_S_ISSOCK (inode.i_mode)) &&
662 e2fsck_pass1_check_device_inode(fs, &inode)) {
663 check_immutable(ctx, &pctx);
664 check_size(ctx, &pctx);
665 ctx->fs_sockets_count++;
667 mark_inode_bad(ctx, ino);
668 if (inode.i_block[EXT2_IND_BLOCK])
670 if (inode.i_block[EXT2_DIND_BLOCK])
671 ctx->fs_dind_count++;
672 if (inode.i_block[EXT2_TIND_BLOCK])
673 ctx->fs_tind_count++;
674 if (inode.i_block[EXT2_IND_BLOCK] ||
675 inode.i_block[EXT2_DIND_BLOCK] ||
676 inode.i_block[EXT2_TIND_BLOCK] ||
678 inodes_to_process[process_inode_count].ino = ino;
679 inodes_to_process[process_inode_count].inode = inode;
680 process_inode_count++;
682 check_blocks(ctx, &pctx, block_buf);
684 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
687 if (process_inode_count >= ctx->process_inode_size) {
688 process_inodes(ctx, block_buf);
690 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
694 process_inodes(ctx, block_buf);
695 ext2fs_close_inode_scan(scan);
696 ehandler_operation(0);
699 * If any extended attribute blocks' reference counts need to
700 * be adjusted, either up (ctx->refcount_extra), or down
701 * (ctx->refcount), then fix them.
704 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
705 ea_refcount_free(ctx->refcount);
708 if (ctx->refcount_extra) {
709 adjust_extattr_refcount(ctx, ctx->refcount_extra,
711 ea_refcount_free(ctx->refcount_extra);
712 ctx->refcount_extra = 0;
715 if (ctx->invalid_bitmaps)
716 handle_fs_bad_blocks(ctx);
718 /* We don't need the block_ea_map any more */
719 if (ctx->block_ea_map) {
720 ext2fs_free_block_bitmap(ctx->block_ea_map);
721 ctx->block_ea_map = 0;
724 if (ctx->flags & E2F_FLAG_RESTART) {
726 * Only the master copy of the superblock and block
727 * group descriptors are going to be written during a
728 * restart, so set the superblock to be used to be the
731 ctx->use_superblock = 0;
736 if (ctx->block_dup_map) {
737 if (ctx->options & E2F_OPT_PREEN) {
738 clear_problem_context(&pctx);
739 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
741 e2fsck_pass1_dupblocks(ctx, block_buf);
743 ext2fs_free_mem(&inodes_to_process);
745 e2fsck_use_inode_shortcuts(ctx, 0);
747 ext2fs_free_mem(&block_buf);
749 #ifdef RESOURCE_TRACK
750 if (ctx->options & E2F_OPT_TIME2) {
751 e2fsck_clear_progbar(ctx);
752 print_resource_track(_("Pass 1"), &rtrack);
758 * When the inode_scan routines call this callback at the end of the
759 * glock group, call process_inodes.
761 static errcode_t scan_callback(ext2_filsys fs,
762 ext2_inode_scan scan EXT2FS_ATTR((unused)),
763 dgrp_t group, void * priv_data)
765 struct scan_callback_struct *scan_struct;
768 scan_struct = (struct scan_callback_struct *) priv_data;
769 ctx = scan_struct->ctx;
771 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
774 if ((ctx->progress)(ctx, 1, group+1,
775 ctx->fs->group_desc_count))
776 return EXT2_ET_CANCEL_REQUESTED;
782 * Process the inodes in the "inodes to process" list.
784 static void process_inodes(e2fsck_t ctx, char *block_buf)
787 struct ext2_inode *old_stashed_inode;
788 ext2_ino_t old_stashed_ino;
789 const char *old_operation;
791 struct problem_context pctx;
794 printf("begin process_inodes: ");
796 if (process_inode_count == 0)
798 old_operation = ehandler_operation(0);
799 old_stashed_inode = ctx->stashed_inode;
800 old_stashed_ino = ctx->stashed_ino;
801 qsort(inodes_to_process, process_inode_count,
802 sizeof(struct process_inode_block), process_inode_cmp);
803 clear_problem_context(&pctx);
804 for (i=0; i < process_inode_count; i++) {
805 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
806 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
809 printf("%u ", pctx.ino);
811 sprintf(buf, _("reading indirect blocks of inode %u"),
813 ehandler_operation(buf);
814 check_blocks(ctx, &pctx, block_buf);
815 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
818 ctx->stashed_inode = old_stashed_inode;
819 ctx->stashed_ino = old_stashed_ino;
820 process_inode_count = 0;
822 printf("end process inodes\n");
824 ehandler_operation(old_operation);
827 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
829 const struct process_inode_block *ib_a =
830 (const struct process_inode_block *) a;
831 const struct process_inode_block *ib_b =
832 (const struct process_inode_block *) b;
835 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
836 ib_b->inode.i_block[EXT2_IND_BLOCK]);
838 ret = ib_a->inode.i_file_acl - ib_b->inode.i_file_acl;
843 * Mark an inode as being bad in some what
845 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
847 struct problem_context pctx;
849 if (!ctx->inode_bad_map) {
850 clear_problem_context(&pctx);
852 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
853 _("bad inode map"), &ctx->inode_bad_map);
856 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
857 /* Should never get here */
858 ctx->flags |= E2F_FLAG_ABORT;
862 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
867 * This procedure will allocate the inode "bb" (badblock) map table
869 static void alloc_bb_map(e2fsck_t ctx)
871 struct problem_context pctx;
873 clear_problem_context(&pctx);
874 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
875 _("inode in bad block map"),
879 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
880 /* Should never get here */
881 ctx->flags |= E2F_FLAG_ABORT;
887 * This procedure will allocate the inode imagic table
889 static void alloc_imagic_map(e2fsck_t ctx)
891 struct problem_context pctx;
893 clear_problem_context(&pctx);
894 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
895 _("imagic inode map"),
896 &ctx->inode_imagic_map);
899 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
900 /* Should never get here */
901 ctx->flags |= E2F_FLAG_ABORT;
907 * Marks a block as in use, setting the dup_map if it's been set
908 * already. Called by process_block and process_bad_block.
910 * WARNING: Assumes checks have already been done to make sure block
911 * is valid. This is true in both process_block and process_bad_block.
913 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
915 struct problem_context pctx;
917 clear_problem_context(&pctx);
919 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
920 if (!ctx->block_dup_map) {
921 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
922 _("multiply claimed block map"),
923 &ctx->block_dup_map);
926 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
928 /* Should never get here */
929 ctx->flags |= E2F_FLAG_ABORT;
933 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
935 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
940 * Adjust the extended attribute block's reference counts at the end
941 * of pass 1, either by subtracting out references for EA blocks that
942 * are still referenced in ctx->refcount, or by adding references for
943 * EA blocks that had extra references as accounted for in
944 * ctx->refcount_extra.
946 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
947 char *block_buf, int adjust_sign)
949 struct ext2_ext_attr_header *header;
950 struct problem_context pctx;
951 ext2_filsys fs = ctx->fs;
956 clear_problem_context(&pctx);
958 ea_refcount_intr_begin(refcount);
960 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
963 pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
965 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
968 header = (struct ext2_ext_attr_header *) block_buf;
969 pctx.blkcount = header->h_refcount;
970 should_be = header->h_refcount + adjust_sign * count;
971 pctx.num = should_be;
972 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
973 header->h_refcount = should_be;
974 pctx.errcode = ext2fs_write_ext_attr(fs, blk,
977 fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx);
985 * Handle processing the extended attribute blocks
987 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
990 ext2_filsys fs = ctx->fs;
991 ext2_ino_t ino = pctx->ino;
992 struct ext2_inode *inode = pctx->inode;
995 struct ext2_ext_attr_header *header;
996 struct ext2_ext_attr_entry *entry;
1000 blk = inode->i_file_acl;
1005 * If the Extended attribute flag isn't set, then a non-zero
1006 * file acl means that the inode is corrupted.
1008 * Or if the extended attribute block is an invalid block,
1009 * then the inode is also corrupted.
1011 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1012 (blk < fs->super->s_first_data_block) ||
1013 (blk >= fs->super->s_blocks_count)) {
1014 mark_inode_bad(ctx, ino);
1018 /* If ea bitmap hasn't been allocated, create it */
1019 if (!ctx->block_ea_map) {
1020 pctx->errcode = ext2fs_allocate_block_bitmap(fs,
1021 _("ext attr block map"),
1022 &ctx->block_ea_map);
1023 if (pctx->errcode) {
1025 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1026 ctx->flags |= E2F_FLAG_ABORT;
1031 /* Create the EA refcount structure if necessary */
1032 if (!ctx->refcount) {
1033 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1034 if (pctx->errcode) {
1036 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1037 ctx->flags |= E2F_FLAG_ABORT;
1043 /* Debugging text */
1044 printf("Inode %u has EA block %u\n", ino, blk);
1047 /* Have we seen this EA block before? */
1048 if (ext2fs_fast_test_block_bitmap(ctx->block_ea_map, blk)) {
1049 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1051 /* Ooops, this EA was referenced more than it stated */
1052 if (!ctx->refcount_extra) {
1053 pctx->errcode = ea_refcount_create(0,
1054 &ctx->refcount_extra);
1055 if (pctx->errcode) {
1057 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1058 ctx->flags |= E2F_FLAG_ABORT;
1062 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1067 * OK, we haven't seen this EA block yet. So we need to
1071 pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1072 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1074 header = (struct ext2_ext_attr_header *) block_buf;
1075 pctx->blk = inode->i_file_acl;
1076 if (((ctx->ext_attr_ver == 1) &&
1077 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1078 ((ctx->ext_attr_ver == 2) &&
1079 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1080 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1084 if (header->h_blocks != 1) {
1085 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1089 region = region_create(0, fs->blocksize);
1091 fix_problem(ctx, PR_1_EA_ALLOC_REGION, pctx);
1092 ctx->flags |= E2F_FLAG_ABORT;
1095 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1096 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1100 entry = (struct ext2_ext_attr_entry *)(header+1);
1101 end = block_buf + fs->blocksize;
1102 while ((char *)entry < end && *(__u32 *)entry) {
1103 if (region_allocate(region, (char *)entry - (char *)header,
1104 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1105 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1108 if ((ctx->ext_attr_ver == 1 &&
1109 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1110 (ctx->ext_attr_ver == 2 &&
1111 entry->e_name_index == 0)) {
1112 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1115 if (entry->e_value_block != 0) {
1116 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1119 if (entry->e_value_size &&
1120 region_allocate(region, entry->e_value_offs,
1121 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1122 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1125 entry = EXT2_EXT_ATTR_NEXT(entry);
1127 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1128 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1131 region_free(region);
1133 count = header->h_refcount - 1;
1135 ea_refcount_store(ctx->refcount, blk, count);
1136 mark_block_used(ctx, blk);
1137 ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
1142 inode->i_file_acl = 0;
1143 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1147 /* Returns 1 if bad htree, 0 if OK */
1148 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1149 ext2_ino_t ino EXT2FS_ATTR((unused)),
1150 struct ext2_inode *inode,
1153 struct ext2_dx_root_info *root;
1154 ext2_filsys fs = ctx->fs;
1158 if ((!LINUX_S_ISDIR(inode->i_mode) &&
1159 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1160 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1161 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
1164 blk = inode->i_block[0];
1166 (blk < fs->super->s_first_data_block) ||
1167 (blk >= fs->super->s_blocks_count)) &&
1168 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1171 retval = io_channel_read_blk(fs->io, blk, 1, block_buf);
1172 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1175 /* XXX should check that beginning matches a directory */
1176 root = (struct ext2_dx_root_info *) (block_buf + 24);
1178 if ((root->reserved_zero || root->info_length < 8) &&
1179 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1182 pctx->num = root->hash_version;
1183 if ((root->hash_version != EXT2_HASH_LEGACY) &&
1184 (root->hash_version != EXT2_HASH_HALF_MD4) &&
1185 (root->hash_version != EXT2_HASH_TEA) &&
1186 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
1189 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
1190 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
1193 pctx->num = root->indirect_levels;
1194 if ((root->indirect_levels > 1) &&
1195 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
1202 * This subroutine is called on each inode to account for all of the
1203 * blocks used by that inode.
1205 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
1208 ext2_filsys fs = ctx->fs;
1209 struct process_block_struct pb;
1210 ext2_ino_t ino = pctx->ino;
1211 struct ext2_inode *inode = pctx->inode;
1213 int dirty_inode = 0;
1219 pb.num_illegal_blocks = 0;
1220 pb.suppress = 0; pb.clear = 0;
1223 pb.previous_block = 0;
1224 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1225 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
1226 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
1233 if (inode->i_flags & EXT2_COMPRBLK_FL) {
1234 if (fs->super->s_feature_incompat &
1235 EXT2_FEATURE_INCOMPAT_COMPRESSION)
1238 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
1239 inode->i_flags &= ~EXT2_COMPRBLK_FL;
1245 if (ext2fs_inode_has_valid_blocks(inode))
1246 pctx->errcode = ext2fs_block_iterate2(fs, ino,
1247 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
1248 block_buf, process_block, &pb);
1249 end_problem_latch(ctx, PR_LATCH_BLOCK);
1250 end_problem_latch(ctx, PR_LATCH_TOOBIG);
1251 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1254 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
1256 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
1257 ctx->fs_fragmented++;
1260 inode->i_links_count = 0;
1261 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1262 inode->i_dtime = time(0);
1264 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1265 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1266 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1268 * The inode was probably partially accounted for
1269 * before processing was aborted, so we need to
1270 * restart the pass 1 scan.
1272 ctx->flags |= E2F_FLAG_RESTART;
1276 if (inode->i_flags & EXT2_INDEX_FL) {
1277 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
1278 inode->i_flags &= ~EXT2_INDEX_FL;
1282 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
1286 if (ctx->dirs_to_hash && pb.is_dir &&
1287 !(inode->i_flags & EXT2_INDEX_FL) &&
1288 ((inode->i_size / fs->blocksize) >= 3))
1289 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1291 if (!pb.num_blocks && pb.is_dir) {
1292 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
1293 inode->i_links_count = 0;
1294 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1295 inode->i_dtime = time(0);
1297 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1298 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1299 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1300 ctx->fs_directory_count--;
1305 if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf))
1308 pb.num_blocks *= (fs->blocksize / 512);
1310 printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
1311 ino, inode->i_size, pb.last_block, inode->i_blocks,
1315 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
1316 if (nblock > (pb.last_block + 1))
1318 else if (nblock < (pb.last_block + 1)) {
1319 if (((pb.last_block + 1) - nblock) >
1320 fs->super->s_prealloc_dir_blocks)
1324 size = EXT2_I_SIZE(inode);
1325 if ((pb.last_block >= 0) &&
1326 (size < (__u64) pb.last_block * fs->blocksize))
1328 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
1331 /* i_size for symlinks is checked elsewhere */
1332 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
1333 pctx->num = (pb.last_block+1) * fs->blocksize;
1334 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
1335 inode->i_size = pctx->num;
1336 if (!LINUX_S_ISDIR(inode->i_mode))
1337 inode->i_size_high = pctx->num >> 32;
1342 if (LINUX_S_ISREG(inode->i_mode) &&
1343 (inode->i_size_high || inode->i_size & 0x80000000UL))
1345 if (pb.num_blocks != inode->i_blocks) {
1346 pctx->num = pb.num_blocks;
1347 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
1348 inode->i_blocks = pb.num_blocks;
1355 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
1360 * Helper function called by process block when an illegal block is
1361 * found. It returns a description about why the block is illegal
1363 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
1367 static char problem[80];
1369 super = fs->super->s_first_data_block;
1370 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
1371 if (block < super) {
1372 sprintf(problem, "< FIRSTBLOCK (%u)", super);
1374 } else if (block >= fs->super->s_blocks_count) {
1375 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
1378 for (i = 0; i < fs->group_desc_count; i++) {
1379 if (block == super) {
1380 sprintf(problem, "is the superblock in group %d", i);
1383 if (block > super &&
1384 block <= (super + fs->desc_blocks)) {
1385 sprintf(problem, "is in the group descriptors "
1389 if (block == fs->group_desc[i].bg_block_bitmap) {
1390 sprintf(problem, "is the block bitmap of group %d", i);
1393 if (block == fs->group_desc[i].bg_inode_bitmap) {
1394 sprintf(problem, "is the inode bitmap of group %d", i);
1397 if (block >= fs->group_desc[i].bg_inode_table &&
1398 (block < fs->group_desc[i].bg_inode_table
1399 + fs->inode_blocks_per_group)) {
1400 sprintf(problem, "is in the inode table of group %d",
1404 super += fs->super->s_blocks_per_group;
1411 * This is a helper function for check_blocks().
1413 static int process_block(ext2_filsys fs,
1415 e2_blkcnt_t blockcnt,
1416 blk_t ref_block EXT2FS_ATTR((unused)),
1417 int ref_offset EXT2FS_ATTR((unused)),
1420 struct process_block_struct *p;
1421 struct problem_context *pctx;
1422 blk_t blk = *block_nr;
1427 p = (struct process_block_struct *) priv_data;
1431 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
1432 /* todo: Check that the comprblk_fl is high, that the
1433 blkaddr pattern looks right (all non-holes up to
1434 first EXT2FS_COMPRESSED_BLKADDR, then all
1435 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
1436 that the feature_incompat bit is high, and that the
1437 inode is a regular file. If we're doing a "full
1438 check" (a concept introduced to e2fsck by e2compr,
1439 meaning that we look at data blocks as well as
1440 metadata) then call some library routine that
1441 checks the compressed data. I'll have to think
1442 about this, because one particularly important
1443 problem to be able to fix is to recalculate the
1444 cluster size if necessary. I think that perhaps
1445 we'd better do most/all e2compr-specific checks
1446 separately, after the non-e2compr checks. If not
1447 doing a full check, it may be useful to test that
1448 the personality is linux; e.g. if it isn't then
1449 perhaps this really is just an illegal block. */
1454 if (p->is_dir == 0) {
1456 * Should never happen, since only directories
1457 * get called with BLOCK_FLAG_HOLE
1460 printf("process_block() called with blk == 0, "
1461 "blockcnt=%d, inode %lu???\n",
1468 if (blockcnt * fs->blocksize < p->inode->i_size) {
1470 printf("Missing block (#%d) in directory inode %lu!\n",
1479 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
1484 * Simplistic fragmentation check. We merely require that the
1485 * file be contiguous. (Which can never be true for really
1486 * big files that are greater than a block group.)
1488 if (!HOLE_BLKADDR(p->previous_block)) {
1489 if (p->previous_block+1 != blk)
1492 p->previous_block = blk;
1494 if (p->is_dir && blockcnt > (1 << (15 - fs->super->s_log_block_size)))
1495 problem = PR_1_TOOBIG_DIR;
1496 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
1497 problem = PR_1_TOOBIG_REG;
1498 if (!p->is_dir && !p->is_reg && blockcnt > 0)
1499 problem = PR_1_TOOBIG_SYMLINK;
1501 if (blk < fs->super->s_first_data_block ||
1502 blk >= fs->super->s_blocks_count)
1503 problem = PR_1_ILLEGAL_BLOCK_NUM;
1506 p->num_illegal_blocks++;
1507 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
1508 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
1512 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
1514 set_latch_flags(PR_LATCH_BLOCK,
1519 pctx->blkcount = blockcnt;
1520 if (fix_problem(ctx, problem, pctx)) {
1521 blk = *block_nr = 0;
1522 ret_code = BLOCK_CHANGED;
1528 mark_block_used(ctx, blk);
1531 p->last_block = blockcnt;
1533 if (p->is_dir && (blockcnt >= 0)) {
1534 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1536 if (pctx->errcode) {
1538 pctx->num = blockcnt;
1539 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1540 /* Should never get here */
1541 ctx->flags |= E2F_FLAG_ABORT;
1548 static int process_bad_block(ext2_filsys fs,
1550 e2_blkcnt_t blockcnt,
1551 blk_t ref_block EXT2FS_ATTR((unused)),
1552 int ref_offset EXT2FS_ATTR((unused)),
1555 struct process_block_struct *p;
1556 blk_t blk = *block_nr;
1559 struct problem_context *pctx;
1563 * Note: This function processes blocks for the bad blocks
1564 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
1570 p = (struct process_block_struct *) priv_data;
1574 pctx->ino = EXT2_BAD_INO;
1576 pctx->blkcount = blockcnt;
1578 if ((blk < fs->super->s_first_data_block) ||
1579 (blk >= fs->super->s_blocks_count)) {
1580 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1582 return BLOCK_CHANGED;
1588 if (ext2fs_test_block_bitmap(p->fs_meta_blocks, blk)) {
1590 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
1592 return BLOCK_CHANGED;
1594 } else if (ext2fs_test_block_bitmap(ctx->block_found_map,
1597 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
1600 return BLOCK_CHANGED;
1602 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1605 mark_block_used(ctx, blk);
1609 printf ("DEBUG: Marking %u as bad.\n", blk);
1611 ctx->fs_badblocks_count++;
1613 * If the block is not used, then mark it as used and return.
1614 * If it is already marked as found, this must mean that
1615 * there's an overlap between the filesystem table blocks
1616 * (bitmaps and inode table) and the bad block list.
1618 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1619 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1623 * Try to find the where the filesystem block was used...
1625 first_block = fs->super->s_first_data_block;
1627 for (i = 0; i < fs->group_desc_count; i++ ) {
1630 if (!ext2fs_bg_has_super(fs, i))
1632 if (blk == first_block) {
1634 if (fix_problem(ctx,
1635 PR_1_BAD_PRIMARY_SUPERBLOCK,
1638 return BLOCK_CHANGED;
1642 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1645 if ((blk > first_block) &&
1646 (blk <= first_block + fs->desc_blocks)) {
1648 pctx->blk = *block_nr;
1649 if (fix_problem(ctx,
1650 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1652 return BLOCK_CHANGED;
1656 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1660 if (blk == fs->group_desc[i].bg_block_bitmap) {
1661 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1662 ctx->invalid_block_bitmap_flag[i]++;
1663 ctx->invalid_bitmaps++;
1667 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1668 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1669 ctx->invalid_inode_bitmap_flag[i]++;
1670 ctx->invalid_bitmaps++;
1674 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1675 (blk < (fs->group_desc[i].bg_inode_table +
1676 fs->inode_blocks_per_group))) {
1678 * If there are bad blocks in the inode table,
1679 * the inode scan code will try to do
1680 * something reasonable automatically.
1684 first_block += fs->super->s_blocks_per_group;
1687 * If we've gotten to this point, then the only
1688 * possibility is that the bad block inode meta data
1689 * is using a bad block.
1691 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1692 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
1693 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
1695 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
1697 return BLOCK_CHANGED;
1699 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1706 /* Warn user that the block wasn't claimed */
1707 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1712 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
1713 const char *name, int num, blk_t *new_block)
1715 ext2_filsys fs = ctx->fs;
1716 blk_t old_block = *new_block;
1719 struct problem_context pctx;
1721 clear_problem_context(&pctx);
1724 pctx.blk = old_block;
1727 pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
1728 first_block + fs->super->s_blocks_per_group,
1729 num, ctx->block_found_map, new_block);
1732 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
1733 ext2fs_unmark_valid(fs);
1736 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
1738 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
1739 ext2fs_unmark_valid(fs);
1742 ext2fs_mark_super_dirty(fs);
1743 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1744 pctx.blk2 = *new_block;
1745 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
1746 PR_1_RELOC_TO), &pctx);
1748 for (i = 0; i < num; i++) {
1750 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
1752 pctx.errcode = io_channel_read_blk(fs->io,
1753 old_block + i, 1, buf);
1755 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
1757 memset(buf, 0, fs->blocksize);
1759 pctx.blk = (*new_block) + i;
1760 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
1763 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
1765 ext2fs_free_mem(&buf);
1769 * This routine gets called at the end of pass 1 if bad blocks are
1770 * detected in the superblock, group descriptors, inode_bitmaps, or
1771 * block bitmaps. At this point, all of the blocks have been mapped
1772 * out, so we can try to allocate new block(s) to replace the bad
1775 static void handle_fs_bad_blocks(e2fsck_t ctx)
1777 ext2_filsys fs = ctx->fs;
1779 int first_block = fs->super->s_first_data_block;
1781 for (i = 0; i < fs->group_desc_count; i++) {
1782 if (ctx->invalid_block_bitmap_flag[i]) {
1783 new_table_block(ctx, first_block, i, _("block bitmap"),
1784 1, &fs->group_desc[i].bg_block_bitmap);
1786 if (ctx->invalid_inode_bitmap_flag[i]) {
1787 new_table_block(ctx, first_block, i, _("inode bitmap"),
1788 1, &fs->group_desc[i].bg_inode_bitmap);
1790 if (ctx->invalid_inode_table_flag[i]) {
1791 new_table_block(ctx, first_block, i, _("inode table"),
1792 fs->inode_blocks_per_group,
1793 &fs->group_desc[i].bg_inode_table);
1794 ctx->flags |= E2F_FLAG_RESTART;
1796 first_block += fs->super->s_blocks_per_group;
1798 ctx->invalid_bitmaps = 0;
1802 * This routine marks all blocks which are used by the superblock,
1803 * group descriptors, inode bitmaps, and block bitmaps.
1805 static void mark_table_blocks(e2fsck_t ctx)
1807 ext2_filsys fs = ctx->fs;
1811 struct problem_context pctx;
1813 clear_problem_context(&pctx);
1815 block = fs->super->s_first_data_block;
1816 for (i = 0; i < fs->group_desc_count; i++) {
1819 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
1822 * Mark the blocks used for the inode table
1824 if (fs->group_desc[i].bg_inode_table) {
1825 for (j = 0, b = fs->group_desc[i].bg_inode_table;
1826 j < fs->inode_blocks_per_group;
1828 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1831 if (fix_problem(ctx,
1832 PR_1_ITABLE_CONFLICT, &pctx)) {
1833 ctx->invalid_inode_table_flag[i]++;
1834 ctx->invalid_bitmaps++;
1837 ext2fs_mark_block_bitmap(ctx->block_found_map,
1844 * Mark block used for the block bitmap
1846 if (fs->group_desc[i].bg_block_bitmap) {
1847 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1848 fs->group_desc[i].bg_block_bitmap)) {
1849 pctx.blk = fs->group_desc[i].bg_block_bitmap;
1850 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
1851 ctx->invalid_block_bitmap_flag[i]++;
1852 ctx->invalid_bitmaps++;
1855 ext2fs_mark_block_bitmap(ctx->block_found_map,
1856 fs->group_desc[i].bg_block_bitmap);
1861 * Mark block used for the inode bitmap
1863 if (fs->group_desc[i].bg_inode_bitmap) {
1864 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1865 fs->group_desc[i].bg_inode_bitmap)) {
1866 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
1867 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
1868 ctx->invalid_inode_bitmap_flag[i]++;
1869 ctx->invalid_bitmaps++;
1872 ext2fs_mark_block_bitmap(ctx->block_found_map,
1873 fs->group_desc[i].bg_inode_bitmap);
1876 block += fs->super->s_blocks_per_group;
1881 * Thes subroutines short circuits ext2fs_get_blocks and
1882 * ext2fs_check_directory; we use them since we already have the inode
1883 * structure, so there's no point in letting the ext2fs library read
1886 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
1889 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1892 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1893 return EXT2_ET_CALLBACK_NOTHANDLED;
1895 for (i=0; i < EXT2_N_BLOCKS; i++)
1896 blocks[i] = ctx->stashed_inode->i_block[i];
1900 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
1901 struct ext2_inode *inode)
1903 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1905 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1906 return EXT2_ET_CALLBACK_NOTHANDLED;
1907 *inode = *ctx->stashed_inode;
1911 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
1912 struct ext2_inode *inode)
1914 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1916 if ((ino == ctx->stashed_ino) && ctx->stashed_inode)
1917 *ctx->stashed_inode = *inode;
1918 return EXT2_ET_CALLBACK_NOTHANDLED;
1921 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
1923 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1925 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1926 return EXT2_ET_CALLBACK_NOTHANDLED;
1928 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
1929 return EXT2_ET_NO_DIRECTORY;
1933 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
1935 ext2_filsys fs = ctx->fs;
1938 fs->get_blocks = pass1_get_blocks;
1939 fs->check_directory = pass1_check_directory;
1940 fs->read_inode = pass1_read_inode;
1941 fs->write_inode = pass1_write_inode;
1942 ctx->stashed_ino = 0;
1945 fs->check_directory = 0;
1947 fs->write_inode = 0;