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 unsigned 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_RESIZE_INODE) {
725 ext2fs_block_bitmap save_bmap;
728 save_bmap = fs->block_map;
729 fs->block_map = ctx->block_found_map;
730 clear_problem_context(&pctx);
731 pctx.errcode = ext2fs_create_resize_inode(fs);
733 fix_problem(ctx, PR_1_RESIZE_INODE_CREATE, &pctx);
734 /* Should never get here */
735 ctx->flags |= E2F_FLAG_ABORT;
738 fs->block_map = save_bmap;
739 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
742 if (ctx->flags & E2F_FLAG_RESTART) {
744 * Only the master copy of the superblock and block
745 * group descriptors are going to be written during a
746 * restart, so set the superblock to be used to be the
749 ctx->use_superblock = 0;
754 if (ctx->block_dup_map) {
755 if (ctx->options & E2F_OPT_PREEN) {
756 clear_problem_context(&pctx);
757 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
759 e2fsck_pass1_dupblocks(ctx, block_buf);
761 ext2fs_free_mem(&inodes_to_process);
763 e2fsck_use_inode_shortcuts(ctx, 0);
765 ext2fs_free_mem(&block_buf);
767 #ifdef RESOURCE_TRACK
768 if (ctx->options & E2F_OPT_TIME2) {
769 e2fsck_clear_progbar(ctx);
770 print_resource_track(_("Pass 1"), &rtrack);
776 * When the inode_scan routines call this callback at the end of the
777 * glock group, call process_inodes.
779 static errcode_t scan_callback(ext2_filsys fs,
780 ext2_inode_scan scan EXT2FS_ATTR((unused)),
781 dgrp_t group, void * priv_data)
783 struct scan_callback_struct *scan_struct;
786 scan_struct = (struct scan_callback_struct *) priv_data;
787 ctx = scan_struct->ctx;
789 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
792 if ((ctx->progress)(ctx, 1, group+1,
793 ctx->fs->group_desc_count))
794 return EXT2_ET_CANCEL_REQUESTED;
800 * Process the inodes in the "inodes to process" list.
802 static void process_inodes(e2fsck_t ctx, char *block_buf)
805 struct ext2_inode *old_stashed_inode;
806 ext2_ino_t old_stashed_ino;
807 const char *old_operation;
809 struct problem_context pctx;
812 printf("begin process_inodes: ");
814 if (process_inode_count == 0)
816 old_operation = ehandler_operation(0);
817 old_stashed_inode = ctx->stashed_inode;
818 old_stashed_ino = ctx->stashed_ino;
819 qsort(inodes_to_process, process_inode_count,
820 sizeof(struct process_inode_block), process_inode_cmp);
821 clear_problem_context(&pctx);
822 for (i=0; i < process_inode_count; i++) {
823 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
824 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
827 printf("%u ", pctx.ino);
829 sprintf(buf, _("reading indirect blocks of inode %u"),
831 ehandler_operation(buf);
832 check_blocks(ctx, &pctx, block_buf);
833 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
836 ctx->stashed_inode = old_stashed_inode;
837 ctx->stashed_ino = old_stashed_ino;
838 process_inode_count = 0;
840 printf("end process inodes\n");
842 ehandler_operation(old_operation);
845 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
847 const struct process_inode_block *ib_a =
848 (const struct process_inode_block *) a;
849 const struct process_inode_block *ib_b =
850 (const struct process_inode_block *) b;
853 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
854 ib_b->inode.i_block[EXT2_IND_BLOCK]);
856 ret = ib_a->inode.i_file_acl - ib_b->inode.i_file_acl;
861 * Mark an inode as being bad in some what
863 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
865 struct problem_context pctx;
867 if (!ctx->inode_bad_map) {
868 clear_problem_context(&pctx);
870 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
871 _("bad inode map"), &ctx->inode_bad_map);
874 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
875 /* Should never get here */
876 ctx->flags |= E2F_FLAG_ABORT;
880 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
885 * This procedure will allocate the inode "bb" (badblock) map table
887 static void alloc_bb_map(e2fsck_t ctx)
889 struct problem_context pctx;
891 clear_problem_context(&pctx);
892 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
893 _("inode in bad block map"),
897 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
898 /* Should never get here */
899 ctx->flags |= E2F_FLAG_ABORT;
905 * This procedure will allocate the inode imagic table
907 static void alloc_imagic_map(e2fsck_t ctx)
909 struct problem_context pctx;
911 clear_problem_context(&pctx);
912 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
913 _("imagic inode map"),
914 &ctx->inode_imagic_map);
917 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
918 /* Should never get here */
919 ctx->flags |= E2F_FLAG_ABORT;
925 * Marks a block as in use, setting the dup_map if it's been set
926 * already. Called by process_block and process_bad_block.
928 * WARNING: Assumes checks have already been done to make sure block
929 * is valid. This is true in both process_block and process_bad_block.
931 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
933 struct problem_context pctx;
935 clear_problem_context(&pctx);
937 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
938 if (!ctx->block_dup_map) {
939 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
940 _("multiply claimed block map"),
941 &ctx->block_dup_map);
944 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
946 /* Should never get here */
947 ctx->flags |= E2F_FLAG_ABORT;
951 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
953 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
958 * Adjust the extended attribute block's reference counts at the end
959 * of pass 1, either by subtracting out references for EA blocks that
960 * are still referenced in ctx->refcount, or by adding references for
961 * EA blocks that had extra references as accounted for in
962 * ctx->refcount_extra.
964 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
965 char *block_buf, int adjust_sign)
967 struct ext2_ext_attr_header *header;
968 struct problem_context pctx;
969 ext2_filsys fs = ctx->fs;
974 clear_problem_context(&pctx);
976 ea_refcount_intr_begin(refcount);
978 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
981 pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
983 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
986 header = (struct ext2_ext_attr_header *) block_buf;
987 pctx.blkcount = header->h_refcount;
988 should_be = header->h_refcount + adjust_sign * count;
989 pctx.num = should_be;
990 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
991 header->h_refcount = should_be;
992 pctx.errcode = ext2fs_write_ext_attr(fs, blk,
995 fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx);
1003 * Handle processing the extended attribute blocks
1005 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1008 ext2_filsys fs = ctx->fs;
1009 ext2_ino_t ino = pctx->ino;
1010 struct ext2_inode *inode = pctx->inode;
1013 struct ext2_ext_attr_header *header;
1014 struct ext2_ext_attr_entry *entry;
1018 blk = inode->i_file_acl;
1023 * If the Extended attribute flag isn't set, then a non-zero
1024 * file acl means that the inode is corrupted.
1026 * Or if the extended attribute block is an invalid block,
1027 * then the inode is also corrupted.
1029 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1030 (blk < fs->super->s_first_data_block) ||
1031 (blk >= fs->super->s_blocks_count)) {
1032 mark_inode_bad(ctx, ino);
1036 /* If ea bitmap hasn't been allocated, create it */
1037 if (!ctx->block_ea_map) {
1038 pctx->errcode = ext2fs_allocate_block_bitmap(fs,
1039 _("ext attr block map"),
1040 &ctx->block_ea_map);
1041 if (pctx->errcode) {
1043 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1044 ctx->flags |= E2F_FLAG_ABORT;
1049 /* Create the EA refcount structure if necessary */
1050 if (!ctx->refcount) {
1051 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1052 if (pctx->errcode) {
1054 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1055 ctx->flags |= E2F_FLAG_ABORT;
1061 /* Debugging text */
1062 printf("Inode %u has EA block %u\n", ino, blk);
1065 /* Have we seen this EA block before? */
1066 if (ext2fs_fast_test_block_bitmap(ctx->block_ea_map, blk)) {
1067 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1069 /* Ooops, this EA was referenced more than it stated */
1070 if (!ctx->refcount_extra) {
1071 pctx->errcode = ea_refcount_create(0,
1072 &ctx->refcount_extra);
1073 if (pctx->errcode) {
1075 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1076 ctx->flags |= E2F_FLAG_ABORT;
1080 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1085 * OK, we haven't seen this EA block yet. So we need to
1089 pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1090 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1092 header = (struct ext2_ext_attr_header *) block_buf;
1093 pctx->blk = inode->i_file_acl;
1094 if (((ctx->ext_attr_ver == 1) &&
1095 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1096 ((ctx->ext_attr_ver == 2) &&
1097 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1098 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1102 if (header->h_blocks != 1) {
1103 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1107 region = region_create(0, fs->blocksize);
1109 fix_problem(ctx, PR_1_EA_ALLOC_REGION, pctx);
1110 ctx->flags |= E2F_FLAG_ABORT;
1113 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1114 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1118 entry = (struct ext2_ext_attr_entry *)(header+1);
1119 end = block_buf + fs->blocksize;
1120 while ((char *)entry < end && *(__u32 *)entry) {
1121 if (region_allocate(region, (char *)entry - (char *)header,
1122 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1123 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1126 if ((ctx->ext_attr_ver == 1 &&
1127 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1128 (ctx->ext_attr_ver == 2 &&
1129 entry->e_name_index == 0)) {
1130 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1133 if (entry->e_value_block != 0) {
1134 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1137 if (entry->e_value_size &&
1138 region_allocate(region, entry->e_value_offs,
1139 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1140 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1143 entry = EXT2_EXT_ATTR_NEXT(entry);
1145 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1146 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1149 region_free(region);
1151 count = header->h_refcount - 1;
1153 ea_refcount_store(ctx->refcount, blk, count);
1154 mark_block_used(ctx, blk);
1155 ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
1160 inode->i_file_acl = 0;
1161 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1165 /* Returns 1 if bad htree, 0 if OK */
1166 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1167 ext2_ino_t ino EXT2FS_ATTR((unused)),
1168 struct ext2_inode *inode,
1171 struct ext2_dx_root_info *root;
1172 ext2_filsys fs = ctx->fs;
1176 if ((!LINUX_S_ISDIR(inode->i_mode) &&
1177 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1178 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1179 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
1182 blk = inode->i_block[0];
1184 (blk < fs->super->s_first_data_block) ||
1185 (blk >= fs->super->s_blocks_count)) &&
1186 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1189 retval = io_channel_read_blk(fs->io, blk, 1, block_buf);
1190 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1193 /* XXX should check that beginning matches a directory */
1194 root = (struct ext2_dx_root_info *) (block_buf + 24);
1196 if ((root->reserved_zero || root->info_length < 8) &&
1197 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1200 pctx->num = root->hash_version;
1201 if ((root->hash_version != EXT2_HASH_LEGACY) &&
1202 (root->hash_version != EXT2_HASH_HALF_MD4) &&
1203 (root->hash_version != EXT2_HASH_TEA) &&
1204 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
1207 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
1208 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
1211 pctx->num = root->indirect_levels;
1212 if ((root->indirect_levels > 1) &&
1213 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
1220 * This subroutine is called on each inode to account for all of the
1221 * blocks used by that inode.
1223 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
1226 ext2_filsys fs = ctx->fs;
1227 struct process_block_struct pb;
1228 ext2_ino_t ino = pctx->ino;
1229 struct ext2_inode *inode = pctx->inode;
1231 int dirty_inode = 0;
1237 pb.num_illegal_blocks = 0;
1238 pb.suppress = 0; pb.clear = 0;
1241 pb.previous_block = 0;
1242 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1243 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
1244 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
1251 if (inode->i_flags & EXT2_COMPRBLK_FL) {
1252 if (fs->super->s_feature_incompat &
1253 EXT2_FEATURE_INCOMPAT_COMPRESSION)
1256 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
1257 inode->i_flags &= ~EXT2_COMPRBLK_FL;
1263 if (ext2fs_inode_has_valid_blocks(inode))
1264 pctx->errcode = ext2fs_block_iterate2(fs, ino,
1265 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
1266 block_buf, process_block, &pb);
1267 end_problem_latch(ctx, PR_LATCH_BLOCK);
1268 end_problem_latch(ctx, PR_LATCH_TOOBIG);
1269 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1272 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
1274 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
1275 ctx->fs_fragmented++;
1278 inode->i_links_count = 0;
1279 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1280 inode->i_dtime = time(0);
1282 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1283 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1284 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1286 * The inode was probably partially accounted for
1287 * before processing was aborted, so we need to
1288 * restart the pass 1 scan.
1290 ctx->flags |= E2F_FLAG_RESTART;
1294 if (inode->i_flags & EXT2_INDEX_FL) {
1295 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
1296 inode->i_flags &= ~EXT2_INDEX_FL;
1300 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
1304 if (ctx->dirs_to_hash && pb.is_dir &&
1305 !(inode->i_flags & EXT2_INDEX_FL) &&
1306 ((inode->i_size / fs->blocksize) >= 3))
1307 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1309 if (!pb.num_blocks && pb.is_dir) {
1310 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
1311 inode->i_links_count = 0;
1312 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1313 inode->i_dtime = time(0);
1315 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1316 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1317 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1318 ctx->fs_directory_count--;
1323 if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf))
1326 pb.num_blocks *= (fs->blocksize / 512);
1328 printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
1329 ino, inode->i_size, pb.last_block, inode->i_blocks,
1333 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
1334 if (nblock > (pb.last_block + 1))
1336 else if (nblock < (pb.last_block + 1)) {
1337 if (((pb.last_block + 1) - nblock) >
1338 fs->super->s_prealloc_dir_blocks)
1342 size = EXT2_I_SIZE(inode);
1343 if ((pb.last_block >= 0) &&
1344 (size < (__u64) pb.last_block * fs->blocksize))
1346 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
1349 /* i_size for symlinks is checked elsewhere */
1350 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
1351 pctx->num = (pb.last_block+1) * fs->blocksize;
1352 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
1353 inode->i_size = pctx->num;
1354 if (!LINUX_S_ISDIR(inode->i_mode))
1355 inode->i_size_high = pctx->num >> 32;
1360 if (LINUX_S_ISREG(inode->i_mode) &&
1361 (inode->i_size_high || inode->i_size & 0x80000000UL))
1363 if (pb.num_blocks != inode->i_blocks) {
1364 pctx->num = pb.num_blocks;
1365 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
1366 inode->i_blocks = pb.num_blocks;
1373 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
1378 * Helper function called by process block when an illegal block is
1379 * found. It returns a description about why the block is illegal
1381 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
1385 static char problem[80];
1387 super = fs->super->s_first_data_block;
1388 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
1389 if (block < super) {
1390 sprintf(problem, "< FIRSTBLOCK (%u)", super);
1392 } else if (block >= fs->super->s_blocks_count) {
1393 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
1396 for (i = 0; i < fs->group_desc_count; i++) {
1397 if (block == super) {
1398 sprintf(problem, "is the superblock in group %d", i);
1401 if (block > super &&
1402 block <= (super + fs->desc_blocks)) {
1403 sprintf(problem, "is in the group descriptors "
1407 if (block == fs->group_desc[i].bg_block_bitmap) {
1408 sprintf(problem, "is the block bitmap of group %d", i);
1411 if (block == fs->group_desc[i].bg_inode_bitmap) {
1412 sprintf(problem, "is the inode bitmap of group %d", i);
1415 if (block >= fs->group_desc[i].bg_inode_table &&
1416 (block < fs->group_desc[i].bg_inode_table
1417 + fs->inode_blocks_per_group)) {
1418 sprintf(problem, "is in the inode table of group %d",
1422 super += fs->super->s_blocks_per_group;
1429 * This is a helper function for check_blocks().
1431 static int process_block(ext2_filsys fs,
1433 e2_blkcnt_t blockcnt,
1434 blk_t ref_block EXT2FS_ATTR((unused)),
1435 int ref_offset EXT2FS_ATTR((unused)),
1438 struct process_block_struct *p;
1439 struct problem_context *pctx;
1440 blk_t blk = *block_nr;
1445 p = (struct process_block_struct *) priv_data;
1449 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
1450 /* todo: Check that the comprblk_fl is high, that the
1451 blkaddr pattern looks right (all non-holes up to
1452 first EXT2FS_COMPRESSED_BLKADDR, then all
1453 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
1454 that the feature_incompat bit is high, and that the
1455 inode is a regular file. If we're doing a "full
1456 check" (a concept introduced to e2fsck by e2compr,
1457 meaning that we look at data blocks as well as
1458 metadata) then call some library routine that
1459 checks the compressed data. I'll have to think
1460 about this, because one particularly important
1461 problem to be able to fix is to recalculate the
1462 cluster size if necessary. I think that perhaps
1463 we'd better do most/all e2compr-specific checks
1464 separately, after the non-e2compr checks. If not
1465 doing a full check, it may be useful to test that
1466 the personality is linux; e.g. if it isn't then
1467 perhaps this really is just an illegal block. */
1472 if (p->is_dir == 0) {
1474 * Should never happen, since only directories
1475 * get called with BLOCK_FLAG_HOLE
1478 printf("process_block() called with blk == 0, "
1479 "blockcnt=%d, inode %lu???\n",
1486 if (blockcnt * fs->blocksize < p->inode->i_size) {
1488 printf("Missing block (#%d) in directory inode %lu!\n",
1497 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
1502 * Simplistic fragmentation check. We merely require that the
1503 * file be contiguous. (Which can never be true for really
1504 * big files that are greater than a block group.)
1506 if (!HOLE_BLKADDR(p->previous_block)) {
1507 if (p->previous_block+1 != blk)
1510 p->previous_block = blk;
1512 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
1513 problem = PR_1_TOOBIG_DIR;
1514 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
1515 problem = PR_1_TOOBIG_REG;
1516 if (!p->is_dir && !p->is_reg && blockcnt > 0)
1517 problem = PR_1_TOOBIG_SYMLINK;
1519 if (blk < fs->super->s_first_data_block ||
1520 blk >= fs->super->s_blocks_count)
1521 problem = PR_1_ILLEGAL_BLOCK_NUM;
1524 p->num_illegal_blocks++;
1525 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
1526 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
1530 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
1532 set_latch_flags(PR_LATCH_BLOCK,
1537 pctx->blkcount = blockcnt;
1538 if (fix_problem(ctx, problem, pctx)) {
1539 blk = *block_nr = 0;
1540 ret_code = BLOCK_CHANGED;
1546 if (p->ino == EXT2_RESIZE_INO) {
1548 * The resize inode has already be sanity checked
1549 * during pass #0 (the superblock checks). All we
1550 * have to do is mark the double indirect block as
1551 * being in use; all of the other blocks are handled
1552 * by mark_table_blocks()).
1554 if (blockcnt == BLOCK_COUNT_DIND)
1555 mark_block_used(ctx, blk);
1557 mark_block_used(ctx, blk);
1560 p->last_block = blockcnt;
1562 if (p->is_dir && (blockcnt >= 0)) {
1563 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1565 if (pctx->errcode) {
1567 pctx->num = blockcnt;
1568 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1569 /* Should never get here */
1570 ctx->flags |= E2F_FLAG_ABORT;
1577 static int process_bad_block(ext2_filsys fs,
1579 e2_blkcnt_t blockcnt,
1580 blk_t ref_block EXT2FS_ATTR((unused)),
1581 int ref_offset EXT2FS_ATTR((unused)),
1584 struct process_block_struct *p;
1585 blk_t blk = *block_nr;
1588 struct problem_context *pctx;
1592 * Note: This function processes blocks for the bad blocks
1593 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
1599 p = (struct process_block_struct *) priv_data;
1603 pctx->ino = EXT2_BAD_INO;
1605 pctx->blkcount = blockcnt;
1607 if ((blk < fs->super->s_first_data_block) ||
1608 (blk >= fs->super->s_blocks_count)) {
1609 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1611 return BLOCK_CHANGED;
1617 if (ext2fs_test_block_bitmap(p->fs_meta_blocks, blk)) {
1619 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
1621 return BLOCK_CHANGED;
1623 } else if (ext2fs_test_block_bitmap(ctx->block_found_map,
1626 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
1629 return BLOCK_CHANGED;
1631 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1634 mark_block_used(ctx, blk);
1638 printf ("DEBUG: Marking %u as bad.\n", blk);
1640 ctx->fs_badblocks_count++;
1642 * If the block is not used, then mark it as used and return.
1643 * If it is already marked as found, this must mean that
1644 * there's an overlap between the filesystem table blocks
1645 * (bitmaps and inode table) and the bad block list.
1647 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1648 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1652 * Try to find the where the filesystem block was used...
1654 first_block = fs->super->s_first_data_block;
1656 for (i = 0; i < fs->group_desc_count; i++ ) {
1659 if (!ext2fs_bg_has_super(fs, i))
1661 if (blk == first_block) {
1663 if (fix_problem(ctx,
1664 PR_1_BAD_PRIMARY_SUPERBLOCK,
1667 return BLOCK_CHANGED;
1671 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1674 if ((blk > first_block) &&
1675 (blk <= first_block + fs->desc_blocks)) {
1677 pctx->blk = *block_nr;
1678 if (fix_problem(ctx,
1679 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1681 return BLOCK_CHANGED;
1685 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1689 if (blk == fs->group_desc[i].bg_block_bitmap) {
1690 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1691 ctx->invalid_block_bitmap_flag[i]++;
1692 ctx->invalid_bitmaps++;
1696 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1697 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1698 ctx->invalid_inode_bitmap_flag[i]++;
1699 ctx->invalid_bitmaps++;
1703 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1704 (blk < (fs->group_desc[i].bg_inode_table +
1705 fs->inode_blocks_per_group))) {
1707 * If there are bad blocks in the inode table,
1708 * the inode scan code will try to do
1709 * something reasonable automatically.
1713 first_block += fs->super->s_blocks_per_group;
1716 * If we've gotten to this point, then the only
1717 * possibility is that the bad block inode meta data
1718 * is using a bad block.
1720 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1721 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
1722 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
1724 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
1726 return BLOCK_CHANGED;
1728 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1735 /* Warn user that the block wasn't claimed */
1736 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1741 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
1742 const char *name, int num, blk_t *new_block)
1744 ext2_filsys fs = ctx->fs;
1745 blk_t old_block = *new_block;
1748 struct problem_context pctx;
1750 clear_problem_context(&pctx);
1753 pctx.blk = old_block;
1756 pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
1757 first_block + fs->super->s_blocks_per_group,
1758 num, ctx->block_found_map, new_block);
1761 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
1762 ext2fs_unmark_valid(fs);
1765 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
1767 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
1768 ext2fs_unmark_valid(fs);
1771 ext2fs_mark_super_dirty(fs);
1772 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1773 pctx.blk2 = *new_block;
1774 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
1775 PR_1_RELOC_TO), &pctx);
1777 for (i = 0; i < num; i++) {
1779 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
1781 pctx.errcode = io_channel_read_blk(fs->io,
1782 old_block + i, 1, buf);
1784 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
1786 memset(buf, 0, fs->blocksize);
1788 pctx.blk = (*new_block) + i;
1789 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
1792 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
1794 ext2fs_free_mem(&buf);
1798 * This routine gets called at the end of pass 1 if bad blocks are
1799 * detected in the superblock, group descriptors, inode_bitmaps, or
1800 * block bitmaps. At this point, all of the blocks have been mapped
1801 * out, so we can try to allocate new block(s) to replace the bad
1804 static void handle_fs_bad_blocks(e2fsck_t ctx)
1806 ext2_filsys fs = ctx->fs;
1808 int first_block = fs->super->s_first_data_block;
1810 for (i = 0; i < fs->group_desc_count; i++) {
1811 if (ctx->invalid_block_bitmap_flag[i]) {
1812 new_table_block(ctx, first_block, i, _("block bitmap"),
1813 1, &fs->group_desc[i].bg_block_bitmap);
1815 if (ctx->invalid_inode_bitmap_flag[i]) {
1816 new_table_block(ctx, first_block, i, _("inode bitmap"),
1817 1, &fs->group_desc[i].bg_inode_bitmap);
1819 if (ctx->invalid_inode_table_flag[i]) {
1820 new_table_block(ctx, first_block, i, _("inode table"),
1821 fs->inode_blocks_per_group,
1822 &fs->group_desc[i].bg_inode_table);
1823 ctx->flags |= E2F_FLAG_RESTART;
1825 first_block += fs->super->s_blocks_per_group;
1827 ctx->invalid_bitmaps = 0;
1831 * This routine marks all blocks which are used by the superblock,
1832 * group descriptors, inode bitmaps, and block bitmaps.
1834 static void mark_table_blocks(e2fsck_t ctx)
1836 ext2_filsys fs = ctx->fs;
1840 struct problem_context pctx;
1842 clear_problem_context(&pctx);
1844 block = fs->super->s_first_data_block;
1845 for (i = 0; i < fs->group_desc_count; i++) {
1848 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
1851 * Mark the blocks used for the inode table
1853 if (fs->group_desc[i].bg_inode_table) {
1854 for (j = 0, b = fs->group_desc[i].bg_inode_table;
1855 j < fs->inode_blocks_per_group;
1857 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1860 if (fix_problem(ctx,
1861 PR_1_ITABLE_CONFLICT, &pctx)) {
1862 ctx->invalid_inode_table_flag[i]++;
1863 ctx->invalid_bitmaps++;
1866 ext2fs_mark_block_bitmap(ctx->block_found_map,
1873 * Mark block used for the block bitmap
1875 if (fs->group_desc[i].bg_block_bitmap) {
1876 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1877 fs->group_desc[i].bg_block_bitmap)) {
1878 pctx.blk = fs->group_desc[i].bg_block_bitmap;
1879 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
1880 ctx->invalid_block_bitmap_flag[i]++;
1881 ctx->invalid_bitmaps++;
1884 ext2fs_mark_block_bitmap(ctx->block_found_map,
1885 fs->group_desc[i].bg_block_bitmap);
1890 * Mark block used for the inode bitmap
1892 if (fs->group_desc[i].bg_inode_bitmap) {
1893 if (ext2fs_test_block_bitmap(ctx->block_found_map,
1894 fs->group_desc[i].bg_inode_bitmap)) {
1895 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
1896 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
1897 ctx->invalid_inode_bitmap_flag[i]++;
1898 ctx->invalid_bitmaps++;
1901 ext2fs_mark_block_bitmap(ctx->block_found_map,
1902 fs->group_desc[i].bg_inode_bitmap);
1905 block += fs->super->s_blocks_per_group;
1910 * Thes subroutines short circuits ext2fs_get_blocks and
1911 * ext2fs_check_directory; we use them since we already have the inode
1912 * structure, so there's no point in letting the ext2fs library read
1915 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
1918 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1921 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1922 return EXT2_ET_CALLBACK_NOTHANDLED;
1924 for (i=0; i < EXT2_N_BLOCKS; i++)
1925 blocks[i] = ctx->stashed_inode->i_block[i];
1929 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
1930 struct ext2_inode *inode)
1932 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1934 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1935 return EXT2_ET_CALLBACK_NOTHANDLED;
1936 *inode = *ctx->stashed_inode;
1940 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
1941 struct ext2_inode *inode)
1943 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1945 if ((ino == ctx->stashed_ino) && ctx->stashed_inode)
1946 *ctx->stashed_inode = *inode;
1947 return EXT2_ET_CALLBACK_NOTHANDLED;
1950 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
1952 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
1954 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
1955 return EXT2_ET_CALLBACK_NOTHANDLED;
1957 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
1958 return EXT2_ET_NO_DIRECTORY;
1962 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
1964 ext2_filsys fs = ctx->fs;
1967 fs->get_blocks = pass1_get_blocks;
1968 fs->check_directory = pass1_check_directory;
1969 fs->read_inode = pass1_read_inode;
1970 fs->write_inode = pass1_write_inode;
1971 ctx->stashed_ino = 0;
1974 fs->check_directory = 0;
1976 fs->write_inode = 0;