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");
244 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
246 struct ext2_super_block *sb = ctx->fs->super;
247 struct ext2_inode_large *inode;
248 struct ext2_ext_attr_entry *entry;
249 char *start, *end, *name;
250 int storage_size, remain, offs;
253 inode = (struct ext2_inode_large *) pctx->inode;
254 storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
255 inode->i_extra_isize;
256 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
257 inode->i_extra_isize + sizeof(__u32);
258 end = (char *) inode + EXT2_INODE_SIZE(ctx->fs->super);
259 entry = (struct ext2_ext_attr_entry *) start;
261 /* scan all entry's headers first */
263 /* take finish entry 0UL into account */
264 remain = storage_size - sizeof(__u32);
267 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
269 /* header eats this space */
270 remain -= sizeof(struct ext2_ext_attr_entry);
272 /* is attribute name valid? */
273 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
274 pctx->num = entry->e_name_len;
275 problem = PR_1_ATTR_NAME_LEN;
279 /* attribute len eats this space */
280 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
282 /* check value size */
283 if (entry->e_value_size == 0 || entry->e_value_size > remain) {
284 pctx->num = entry->e_value_size;
285 problem = PR_1_ATTR_VALUE_SIZE;
289 /* check value placement */
290 if (entry->e_value_offs +
291 EXT2_XATTR_SIZE(entry->e_value_size) != offs) {
292 printf("(entry->e_value_offs + entry->e_value_size: %d, offs: %d)\n", entry->e_value_offs + entry->e_value_size, offs);
293 pctx->num = entry->e_value_offs;
294 problem = PR_1_ATTR_VALUE_OFFSET;
298 /* e_value_block must be 0 in inode's ea */
299 if (entry->e_value_block != 0) {
300 pctx->num = entry->e_value_block;
301 problem = PR_1_ATTR_VALUE_BLOCK;
305 /* e_hash must be 0 in inode's ea */
306 if (entry->e_hash != 0) {
307 pctx->num = entry->e_hash;
308 problem = PR_1_ATTR_HASH;
312 remain -= entry->e_value_size;
313 offs -= EXT2_XATTR_SIZE(entry->e_value_size);
315 entry = EXT2_EXT_ATTR_NEXT(entry);
319 * it seems like a corruption. it's very unlikely we could repair
320 * EA(s) in automatic fashion -bzzz
323 problem = PR_1_ATTR_HASH;
325 if (problem == 0 || !fix_problem(ctx, problem, pctx))
328 /* simple remove all possible EA(s) */
329 *((__u32 *)start) = 0UL;
330 e2fsck_write_inode_full(ctx, pctx->ino, inode,
331 EXT2_INODE_SIZE(sb), "pass1");
334 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
336 struct ext2_super_block *sb = ctx->fs->super;
337 struct ext2_inode_large *inode;
341 inode = (struct ext2_inode_large *) pctx->inode;
342 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
343 /* this isn't large inode. so, nothing to check */
348 printf("inode #%u, i_extra_size %d\n", pctx->ino,
349 inode->i_extra_isize);
351 /* i_extra_isize must cover i_extra_isize + i_pad1 at least */
352 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_pad1);
353 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
355 * For now we will allow i_extra_isize to be 0, but really
356 * implementations should never allow i_extra_isize to be 0
358 if (inode->i_extra_isize &&
359 (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
360 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
362 inode->i_extra_isize = min;
363 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
364 EXT2_INODE_SIZE(sb), "pass1");
368 eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
369 inode->i_extra_isize);
370 if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
371 /* it seems inode has an extended attribute(s) in body */
372 check_ea_in_inode(ctx, pctx);
376 void e2fsck_pass1(e2fsck_t ctx)
380 ext2_filsys fs = ctx->fs;
382 struct ext2_inode *inode;
383 ext2_inode_scan scan;
385 #ifdef RESOURCE_TRACK
386 struct resource_track rtrack;
388 unsigned char frag, fsize;
389 struct problem_context pctx;
390 struct scan_callback_struct scan_struct;
391 struct ext2_super_block *sb = ctx->fs->super;
393 int busted_fs_time = 0;
396 #ifdef RESOURCE_TRACK
397 init_resource_track(&rtrack);
399 clear_problem_context(&pctx);
401 if (!(ctx->options & E2F_OPT_PREEN))
402 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
404 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
405 !(ctx->options & E2F_OPT_NO)) {
406 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
407 ctx->dirs_to_hash = 0;
411 mtrace_print("Pass 1");
414 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
416 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
417 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
418 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
419 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
420 max_sizes = (max_sizes * (1UL << i)) - 1;
421 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
425 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
428 * Allocate bitmaps structures
430 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
431 &ctx->inode_used_map);
434 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
435 ctx->flags |= E2F_FLAG_ABORT;
438 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
439 _("directory inode map"), &ctx->inode_dir_map);
442 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
443 ctx->flags |= E2F_FLAG_ABORT;
446 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
447 _("regular file inode map"), &ctx->inode_reg_map);
450 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
451 ctx->flags |= E2F_FLAG_ABORT;
454 pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
455 &ctx->block_found_map);
458 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
459 ctx->flags |= E2F_FLAG_ABORT;
462 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
463 &ctx->inode_link_info);
465 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
466 ctx->flags |= E2F_FLAG_ABORT;
469 inode_size = EXT2_INODE_SIZE(fs->super);
470 inode = (struct ext2_inode *)
471 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
473 inodes_to_process = (struct process_inode_block *)
474 e2fsck_allocate_memory(ctx,
475 (ctx->process_inode_size *
476 sizeof(struct process_inode_block)),
477 "array of inodes to process");
478 process_inode_count = 0;
480 pctx.errcode = ext2fs_init_dblist(fs, 0);
482 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
483 ctx->flags |= E2F_FLAG_ABORT;
488 * If the last orphan field is set, clear it, since the pass1
489 * processing will automatically find and clear the orphans.
490 * In the future, we may want to try using the last_orphan
491 * linked list ourselves, but for now, we clear it so that the
492 * ext3 mount code won't get confused.
494 if (!(ctx->options & E2F_OPT_READONLY)) {
495 if (fs->super->s_last_orphan) {
496 fs->super->s_last_orphan = 0;
497 ext2fs_mark_super_dirty(fs);
501 mark_table_blocks(ctx);
502 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
503 "block interate buffer");
504 e2fsck_use_inode_shortcuts(ctx, 1);
505 ehandler_operation(_("doing inode scan"));
506 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
509 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
510 ctx->flags |= E2F_FLAG_ABORT;
513 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
514 ctx->stashed_inode = inode;
515 scan_struct.ctx = ctx;
516 scan_struct.block_buf = block_buf;
517 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
519 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
521 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
522 (fs->super->s_mtime < fs->super->s_inodes_count))
526 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
528 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
530 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
531 if (!ctx->inode_bb_map)
533 ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
534 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
538 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
539 ctx->flags |= E2F_FLAG_ABORT;
546 ctx->stashed_ino = ino;
547 if (inode->i_links_count) {
548 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
549 ino, inode->i_links_count);
551 pctx.num = inode->i_links_count;
552 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
553 ctx->flags |= E2F_FLAG_ABORT;
557 if (ino == EXT2_BAD_INO) {
558 struct process_block_struct pb;
560 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
564 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
565 ctx->flags |= E2F_FLAG_ABORT;
568 pb.ino = EXT2_BAD_INO;
569 pb.num_blocks = pb.last_block = 0;
570 pb.num_illegal_blocks = 0;
571 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
572 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
576 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0,
577 block_buf, process_bad_block, &pb);
578 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
580 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
581 ctx->flags |= E2F_FLAG_ABORT;
585 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
586 ctx->flags |= E2F_FLAG_ABORT;
589 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
590 clear_problem_context(&pctx);
592 } else if (ino == EXT2_ROOT_INO) {
594 * Make sure the root inode is a directory; if
595 * not, offer to clear it. It will be
596 * regnerated in pass #3.
598 if (!LINUX_S_ISDIR(inode->i_mode)) {
599 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
600 inode->i_dtime = ctx->now;
601 inode->i_links_count = 0;
602 ext2fs_icount_store(ctx->inode_link_info,
604 e2fsck_write_inode(ctx, ino, inode,
610 * If dtime is set, offer to clear it. mke2fs
611 * version 0.2b created filesystems with the
612 * dtime field set for the root and lost+found
613 * directories. We won't worry about
614 * /lost+found, since that can be regenerated
615 * easily. But we will fix the root directory
618 if (inode->i_dtime && inode->i_links_count) {
619 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
621 e2fsck_write_inode(ctx, ino, inode,
625 } else if (ino == EXT2_JOURNAL_INO) {
626 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
627 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
628 if (!LINUX_S_ISREG(inode->i_mode) &&
629 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
631 inode->i_mode = LINUX_S_IFREG;
632 e2fsck_write_inode(ctx, ino, inode,
635 check_blocks(ctx, &pctx, block_buf);
638 if ((inode->i_links_count || inode->i_blocks ||
639 inode->i_blocks || inode->i_block[0]) &&
640 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
642 memset(inode, 0, inode_size);
643 ext2fs_icount_store(ctx->inode_link_info,
645 e2fsck_write_inode_full(ctx, ino, inode,
646 inode_size, "pass1");
648 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
651 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
652 if (ino == EXT2_BOOT_LOADER_INO) {
653 if (LINUX_S_ISDIR(inode->i_mode))
654 problem = PR_1_RESERVED_BAD_MODE;
655 } else if (ino == EXT2_RESIZE_INO) {
657 !LINUX_S_ISREG(inode->i_mode))
658 problem = PR_1_RESERVED_BAD_MODE;
660 if (inode->i_mode != 0)
661 problem = PR_1_RESERVED_BAD_MODE;
664 if (fix_problem(ctx, problem, &pctx)) {
666 e2fsck_write_inode(ctx, ino, inode,
670 check_blocks(ctx, &pctx, block_buf);
674 * Check for inodes who might have been part of the
675 * orphaned list linked list. They should have gotten
676 * dealt with by now, unless the list had somehow been
679 * FIXME: In the future, inodes which are still in use
680 * (and which are therefore) pending truncation should
681 * be handled specially. Right now we just clear the
682 * dtime field, and the normal e2fsck handling of
683 * inodes where i_size and the inode blocks are
684 * inconsistent is to fix i_size, instead of releasing
685 * the extra blocks. This won't catch the inodes that
686 * was at the end of the orphan list, but it's better
687 * than nothing. The right answer is that there
688 * shouldn't be any bugs in the orphan list handling. :-)
690 if (inode->i_dtime && !busted_fs_time &&
691 inode->i_dtime < ctx->fs->super->s_inodes_count) {
692 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
693 inode->i_dtime = inode->i_links_count ?
695 e2fsck_write_inode(ctx, ino, inode,
701 * This code assumes that deleted inodes have
702 * i_links_count set to 0.
704 if (!inode->i_links_count) {
705 if (!inode->i_dtime && inode->i_mode) {
707 PR_1_ZERO_DTIME, &pctx)) {
708 inode->i_dtime = ctx->now;
709 e2fsck_write_inode(ctx, ino, inode,
716 * n.b. 0.3c ext2fs code didn't clear i_links_count for
717 * deleted files. Oops.
719 * Since all new ext2 implementations get this right,
720 * we now assume that the case of non-zero
721 * i_links_count and non-zero dtime means that we
722 * should keep the file, not delete it.
725 if (inode->i_dtime) {
726 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
728 e2fsck_write_inode(ctx, ino, inode, "pass1");
732 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
733 switch (fs->super->s_creator_os) {
735 frag = inode->osd2.linux2.l_i_frag;
736 fsize = inode->osd2.linux2.l_i_fsize;
739 frag = inode->osd2.hurd2.h_i_frag;
740 fsize = inode->osd2.hurd2.h_i_fsize;
743 frag = inode->osd2.masix2.m_i_frag;
744 fsize = inode->osd2.masix2.m_i_fsize;
750 if (inode->i_faddr || frag || fsize ||
751 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
752 mark_inode_bad(ctx, ino);
753 if (inode->i_flags & EXT2_IMAGIC_FL) {
755 if (!ctx->inode_imagic_map)
756 alloc_imagic_map(ctx);
757 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
760 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
761 inode->i_flags &= ~EXT2_IMAGIC_FL;
762 e2fsck_write_inode(ctx, ino,
768 check_inode_extra_space(ctx, &pctx);
770 if (LINUX_S_ISDIR(inode->i_mode)) {
771 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
772 e2fsck_add_dir_info(ctx, ino, 0);
773 ctx->fs_directory_count++;
774 } else if (LINUX_S_ISREG (inode->i_mode)) {
775 ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
776 ctx->fs_regular_count++;
777 } else if (LINUX_S_ISCHR (inode->i_mode) &&
778 e2fsck_pass1_check_device_inode(fs, inode)) {
779 check_immutable(ctx, &pctx);
780 check_size(ctx, &pctx);
781 ctx->fs_chardev_count++;
782 } else if (LINUX_S_ISBLK (inode->i_mode) &&
783 e2fsck_pass1_check_device_inode(fs, inode)) {
784 check_immutable(ctx, &pctx);
785 check_size(ctx, &pctx);
786 ctx->fs_blockdev_count++;
787 } else if (LINUX_S_ISLNK (inode->i_mode) &&
788 e2fsck_pass1_check_symlink(fs, inode, block_buf)) {
789 check_immutable(ctx, &pctx);
790 ctx->fs_symlinks_count++;
791 if (ext2fs_inode_data_blocks(fs, inode) == 0) {
792 ctx->fs_fast_symlinks_count++;
793 check_blocks(ctx, &pctx, block_buf);
797 else if (LINUX_S_ISFIFO (inode->i_mode) &&
798 e2fsck_pass1_check_device_inode(fs, inode)) {
799 check_immutable(ctx, &pctx);
800 check_size(ctx, &pctx);
801 ctx->fs_fifo_count++;
802 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
803 e2fsck_pass1_check_device_inode(fs, inode)) {
804 check_immutable(ctx, &pctx);
805 check_size(ctx, &pctx);
806 ctx->fs_sockets_count++;
808 mark_inode_bad(ctx, ino);
809 if (inode->i_block[EXT2_IND_BLOCK])
811 if (inode->i_block[EXT2_DIND_BLOCK])
812 ctx->fs_dind_count++;
813 if (inode->i_block[EXT2_TIND_BLOCK])
814 ctx->fs_tind_count++;
815 if (inode->i_block[EXT2_IND_BLOCK] ||
816 inode->i_block[EXT2_DIND_BLOCK] ||
817 inode->i_block[EXT2_TIND_BLOCK] ||
819 inodes_to_process[process_inode_count].ino = ino;
820 inodes_to_process[process_inode_count].inode = *inode;
821 process_inode_count++;
823 check_blocks(ctx, &pctx, block_buf);
825 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
828 if (process_inode_count >= ctx->process_inode_size) {
829 process_inodes(ctx, block_buf);
831 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
835 process_inodes(ctx, block_buf);
836 ext2fs_close_inode_scan(scan);
837 ehandler_operation(0);
840 * If any extended attribute blocks' reference counts need to
841 * be adjusted, either up (ctx->refcount_extra), or down
842 * (ctx->refcount), then fix them.
845 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
846 ea_refcount_free(ctx->refcount);
849 if (ctx->refcount_extra) {
850 adjust_extattr_refcount(ctx, ctx->refcount_extra,
852 ea_refcount_free(ctx->refcount_extra);
853 ctx->refcount_extra = 0;
856 if (ctx->invalid_bitmaps)
857 handle_fs_bad_blocks(ctx);
859 /* We don't need the block_ea_map any more */
860 if (ctx->block_ea_map) {
861 ext2fs_free_block_bitmap(ctx->block_ea_map);
862 ctx->block_ea_map = 0;
865 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
866 ext2fs_block_bitmap save_bmap;
869 save_bmap = fs->block_map;
870 fs->block_map = ctx->block_found_map;
871 clear_problem_context(&pctx);
872 pctx.errcode = ext2fs_create_resize_inode(fs);
874 fix_problem(ctx, PR_1_RESIZE_INODE_CREATE, &pctx);
875 /* Should never get here */
876 ctx->flags |= E2F_FLAG_ABORT;
879 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
881 inode->i_mtime = ctx->now;
882 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
884 fs->block_map = save_bmap;
885 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
888 if (ctx->flags & E2F_FLAG_RESTART) {
890 * Only the master copy of the superblock and block
891 * group descriptors are going to be written during a
892 * restart, so set the superblock to be used to be the
895 ctx->use_superblock = 0;
900 if (ctx->block_dup_map) {
901 if (ctx->options & E2F_OPT_PREEN) {
902 clear_problem_context(&pctx);
903 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
905 e2fsck_pass1_dupblocks(ctx, block_buf);
907 ext2fs_free_mem(&inodes_to_process);
909 e2fsck_use_inode_shortcuts(ctx, 0);
911 ext2fs_free_mem(&block_buf);
912 ext2fs_free_mem(&inode);
914 #ifdef RESOURCE_TRACK
915 if (ctx->options & E2F_OPT_TIME2) {
916 e2fsck_clear_progbar(ctx);
917 print_resource_track(_("Pass 1"), &rtrack);
923 * When the inode_scan routines call this callback at the end of the
924 * glock group, call process_inodes.
926 static errcode_t scan_callback(ext2_filsys fs,
927 ext2_inode_scan scan EXT2FS_ATTR((unused)),
928 dgrp_t group, void * priv_data)
930 struct scan_callback_struct *scan_struct;
933 scan_struct = (struct scan_callback_struct *) priv_data;
934 ctx = scan_struct->ctx;
936 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
939 if ((ctx->progress)(ctx, 1, group+1,
940 ctx->fs->group_desc_count))
941 return EXT2_ET_CANCEL_REQUESTED;
947 * Process the inodes in the "inodes to process" list.
949 static void process_inodes(e2fsck_t ctx, char *block_buf)
952 struct ext2_inode *old_stashed_inode;
953 ext2_ino_t old_stashed_ino;
954 const char *old_operation;
956 struct problem_context pctx;
959 printf("begin process_inodes: ");
961 if (process_inode_count == 0)
963 old_operation = ehandler_operation(0);
964 old_stashed_inode = ctx->stashed_inode;
965 old_stashed_ino = ctx->stashed_ino;
966 qsort(inodes_to_process, process_inode_count,
967 sizeof(struct process_inode_block), process_inode_cmp);
968 clear_problem_context(&pctx);
969 for (i=0; i < process_inode_count; i++) {
970 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
971 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
974 printf("%u ", pctx.ino);
976 sprintf(buf, _("reading indirect blocks of inode %u"),
978 ehandler_operation(buf);
979 check_blocks(ctx, &pctx, block_buf);
980 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
983 ctx->stashed_inode = old_stashed_inode;
984 ctx->stashed_ino = old_stashed_ino;
985 process_inode_count = 0;
987 printf("end process inodes\n");
989 ehandler_operation(old_operation);
992 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
994 const struct process_inode_block *ib_a =
995 (const struct process_inode_block *) a;
996 const struct process_inode_block *ib_b =
997 (const struct process_inode_block *) b;
1000 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1001 ib_b->inode.i_block[EXT2_IND_BLOCK]);
1003 ret = ib_a->inode.i_file_acl - ib_b->inode.i_file_acl;
1008 * Mark an inode as being bad in some what
1010 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
1012 struct problem_context pctx;
1014 if (!ctx->inode_bad_map) {
1015 clear_problem_context(&pctx);
1017 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1018 _("bad inode map"), &ctx->inode_bad_map);
1021 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1022 /* Should never get here */
1023 ctx->flags |= E2F_FLAG_ABORT;
1027 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
1032 * This procedure will allocate the inode "bb" (badblock) map table
1034 static void alloc_bb_map(e2fsck_t ctx)
1036 struct problem_context pctx;
1038 clear_problem_context(&pctx);
1039 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1040 _("inode in bad block map"),
1041 &ctx->inode_bb_map);
1044 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1045 /* Should never get here */
1046 ctx->flags |= E2F_FLAG_ABORT;
1052 * This procedure will allocate the inode imagic table
1054 static void alloc_imagic_map(e2fsck_t ctx)
1056 struct problem_context pctx;
1058 clear_problem_context(&pctx);
1059 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1060 _("imagic inode map"),
1061 &ctx->inode_imagic_map);
1064 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1065 /* Should never get here */
1066 ctx->flags |= E2F_FLAG_ABORT;
1072 * Marks a block as in use, setting the dup_map if it's been set
1073 * already. Called by process_block and process_bad_block.
1075 * WARNING: Assumes checks have already been done to make sure block
1076 * is valid. This is true in both process_block and process_bad_block.
1078 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
1080 struct problem_context pctx;
1082 clear_problem_context(&pctx);
1084 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
1085 if (!ctx->block_dup_map) {
1086 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
1087 _("multiply claimed block map"),
1088 &ctx->block_dup_map);
1091 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1093 /* Should never get here */
1094 ctx->flags |= E2F_FLAG_ABORT;
1098 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
1100 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
1105 * Adjust the extended attribute block's reference counts at the end
1106 * of pass 1, either by subtracting out references for EA blocks that
1107 * are still referenced in ctx->refcount, or by adding references for
1108 * EA blocks that had extra references as accounted for in
1109 * ctx->refcount_extra.
1111 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1112 char *block_buf, int adjust_sign)
1114 struct ext2_ext_attr_header *header;
1115 struct problem_context pctx;
1116 ext2_filsys fs = ctx->fs;
1121 clear_problem_context(&pctx);
1123 ea_refcount_intr_begin(refcount);
1125 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1128 pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1130 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1133 header = (struct ext2_ext_attr_header *) block_buf;
1134 pctx.blkcount = header->h_refcount;
1135 should_be = header->h_refcount + adjust_sign * count;
1136 pctx.num = should_be;
1137 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1138 header->h_refcount = should_be;
1139 pctx.errcode = ext2fs_write_ext_attr(fs, blk,
1142 fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx);
1150 * Handle processing the extended attribute blocks
1152 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1155 ext2_filsys fs = ctx->fs;
1156 ext2_ino_t ino = pctx->ino;
1157 struct ext2_inode *inode = pctx->inode;
1160 struct ext2_ext_attr_header *header;
1161 struct ext2_ext_attr_entry *entry;
1165 blk = inode->i_file_acl;
1170 * If the Extended attribute flag isn't set, then a non-zero
1171 * file acl means that the inode is corrupted.
1173 * Or if the extended attribute block is an invalid block,
1174 * then the inode is also corrupted.
1176 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1177 (blk < fs->super->s_first_data_block) ||
1178 (blk >= fs->super->s_blocks_count)) {
1179 mark_inode_bad(ctx, ino);
1183 /* If ea bitmap hasn't been allocated, create it */
1184 if (!ctx->block_ea_map) {
1185 pctx->errcode = ext2fs_allocate_block_bitmap(fs,
1186 _("ext attr block map"),
1187 &ctx->block_ea_map);
1188 if (pctx->errcode) {
1190 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1191 ctx->flags |= E2F_FLAG_ABORT;
1196 /* Create the EA refcount structure if necessary */
1197 if (!ctx->refcount) {
1198 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1199 if (pctx->errcode) {
1201 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1202 ctx->flags |= E2F_FLAG_ABORT;
1208 /* Debugging text */
1209 printf("Inode %u has EA block %u\n", ino, blk);
1212 /* Have we seen this EA block before? */
1213 if (ext2fs_fast_test_block_bitmap(ctx->block_ea_map, blk)) {
1214 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1216 /* Ooops, this EA was referenced more than it stated */
1217 if (!ctx->refcount_extra) {
1218 pctx->errcode = ea_refcount_create(0,
1219 &ctx->refcount_extra);
1220 if (pctx->errcode) {
1222 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1223 ctx->flags |= E2F_FLAG_ABORT;
1227 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1232 * OK, we haven't seen this EA block yet. So we need to
1236 pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1237 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1239 header = (struct ext2_ext_attr_header *) block_buf;
1240 pctx->blk = inode->i_file_acl;
1241 if (((ctx->ext_attr_ver == 1) &&
1242 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1243 ((ctx->ext_attr_ver == 2) &&
1244 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1245 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1249 if (header->h_blocks != 1) {
1250 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1254 region = region_create(0, fs->blocksize);
1256 fix_problem(ctx, PR_1_EA_ALLOC_REGION, pctx);
1257 ctx->flags |= E2F_FLAG_ABORT;
1260 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1261 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1265 entry = (struct ext2_ext_attr_entry *)(header+1);
1266 end = block_buf + fs->blocksize;
1267 while ((char *)entry < end && *(__u32 *)entry) {
1268 if (region_allocate(region, (char *)entry - (char *)header,
1269 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1270 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1273 if ((ctx->ext_attr_ver == 1 &&
1274 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1275 (ctx->ext_attr_ver == 2 &&
1276 entry->e_name_index == 0)) {
1277 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1280 if (entry->e_value_block != 0) {
1281 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1284 if (entry->e_value_size &&
1285 region_allocate(region, entry->e_value_offs,
1286 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1287 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1290 entry = EXT2_EXT_ATTR_NEXT(entry);
1292 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1293 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1296 region_free(region);
1298 count = header->h_refcount - 1;
1300 ea_refcount_store(ctx->refcount, blk, count);
1301 mark_block_used(ctx, blk);
1302 ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
1307 inode->i_file_acl = 0;
1308 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1312 /* Returns 1 if bad htree, 0 if OK */
1313 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1314 ext2_ino_t ino EXT2FS_ATTR((unused)),
1315 struct ext2_inode *inode,
1318 struct ext2_dx_root_info *root;
1319 ext2_filsys fs = ctx->fs;
1323 if ((!LINUX_S_ISDIR(inode->i_mode) &&
1324 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1325 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1326 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
1329 blk = inode->i_block[0];
1331 (blk < fs->super->s_first_data_block) ||
1332 (blk >= fs->super->s_blocks_count)) &&
1333 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1336 retval = io_channel_read_blk(fs->io, blk, 1, block_buf);
1337 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1340 /* XXX should check that beginning matches a directory */
1341 root = (struct ext2_dx_root_info *) (block_buf + 24);
1343 if ((root->reserved_zero || root->info_length < 8) &&
1344 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1347 pctx->num = root->hash_version;
1348 if ((root->hash_version != EXT2_HASH_LEGACY) &&
1349 (root->hash_version != EXT2_HASH_HALF_MD4) &&
1350 (root->hash_version != EXT2_HASH_TEA) &&
1351 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
1354 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
1355 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
1358 pctx->num = root->indirect_levels;
1359 if ((root->indirect_levels > 1) &&
1360 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
1367 * This subroutine is called on each inode to account for all of the
1368 * blocks used by that inode.
1370 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
1373 ext2_filsys fs = ctx->fs;
1374 struct process_block_struct pb;
1375 ext2_ino_t ino = pctx->ino;
1376 struct ext2_inode *inode = pctx->inode;
1378 int dirty_inode = 0;
1384 pb.num_illegal_blocks = 0;
1385 pb.suppress = 0; pb.clear = 0;
1388 pb.previous_block = 0;
1389 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1390 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
1391 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
1398 if (inode->i_flags & EXT2_COMPRBLK_FL) {
1399 if (fs->super->s_feature_incompat &
1400 EXT2_FEATURE_INCOMPAT_COMPRESSION)
1403 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
1404 inode->i_flags &= ~EXT2_COMPRBLK_FL;
1410 if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf))
1413 if (ext2fs_inode_has_valid_blocks(inode))
1414 pctx->errcode = ext2fs_block_iterate2(fs, ino,
1415 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
1416 block_buf, process_block, &pb);
1417 end_problem_latch(ctx, PR_LATCH_BLOCK);
1418 end_problem_latch(ctx, PR_LATCH_TOOBIG);
1419 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1422 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
1424 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
1425 ctx->fs_fragmented++;
1428 inode->i_links_count = 0;
1429 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1430 inode->i_dtime = ctx->now;
1432 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1433 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1434 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1436 * The inode was probably partially accounted for
1437 * before processing was aborted, so we need to
1438 * restart the pass 1 scan.
1440 ctx->flags |= E2F_FLAG_RESTART;
1444 if (inode->i_flags & EXT2_INDEX_FL) {
1445 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
1446 inode->i_flags &= ~EXT2_INDEX_FL;
1450 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
1454 if (ctx->dirs_to_hash && pb.is_dir &&
1455 !(inode->i_flags & EXT2_INDEX_FL) &&
1456 ((inode->i_size / fs->blocksize) >= 3))
1457 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1459 if (!pb.num_blocks && pb.is_dir) {
1460 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
1461 inode->i_links_count = 0;
1462 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1463 inode->i_dtime = ctx->now;
1465 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1466 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1467 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1468 ctx->fs_directory_count--;
1473 pb.num_blocks *= (fs->blocksize / 512);
1475 printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
1476 ino, inode->i_size, pb.last_block, inode->i_blocks,
1480 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
1481 if (nblock > (pb.last_block + 1))
1483 else if (nblock < (pb.last_block + 1)) {
1484 if (((pb.last_block + 1) - nblock) >
1485 fs->super->s_prealloc_dir_blocks)
1489 size = EXT2_I_SIZE(inode);
1490 if ((pb.last_block >= 0) &&
1491 (size < (__u64) pb.last_block * fs->blocksize))
1493 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
1496 /* i_size for symlinks is checked elsewhere */
1497 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
1498 pctx->num = (pb.last_block+1) * fs->blocksize;
1499 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
1500 inode->i_size = pctx->num;
1501 if (!LINUX_S_ISDIR(inode->i_mode))
1502 inode->i_size_high = pctx->num >> 32;
1507 if (LINUX_S_ISREG(inode->i_mode) &&
1508 (inode->i_size_high || inode->i_size & 0x80000000UL))
1510 if (pb.num_blocks != inode->i_blocks) {
1511 pctx->num = pb.num_blocks;
1512 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
1513 inode->i_blocks = pb.num_blocks;
1520 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
1525 * Helper function called by process block when an illegal block is
1526 * found. It returns a description about why the block is illegal
1528 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
1532 static char problem[80];
1534 super = fs->super->s_first_data_block;
1535 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
1536 if (block < super) {
1537 sprintf(problem, "< FIRSTBLOCK (%u)", super);
1539 } else if (block >= fs->super->s_blocks_count) {
1540 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
1543 for (i = 0; i < fs->group_desc_count; i++) {
1544 if (block == super) {
1545 sprintf(problem, "is the superblock in group %d", i);
1548 if (block > super &&
1549 block <= (super + fs->desc_blocks)) {
1550 sprintf(problem, "is in the group descriptors "
1554 if (block == fs->group_desc[i].bg_block_bitmap) {
1555 sprintf(problem, "is the block bitmap of group %d", i);
1558 if (block == fs->group_desc[i].bg_inode_bitmap) {
1559 sprintf(problem, "is the inode bitmap of group %d", i);
1562 if (block >= fs->group_desc[i].bg_inode_table &&
1563 (block < fs->group_desc[i].bg_inode_table
1564 + fs->inode_blocks_per_group)) {
1565 sprintf(problem, "is in the inode table of group %d",
1569 super += fs->super->s_blocks_per_group;
1576 * This is a helper function for check_blocks().
1578 static int process_block(ext2_filsys fs,
1580 e2_blkcnt_t blockcnt,
1581 blk_t ref_block EXT2FS_ATTR((unused)),
1582 int ref_offset EXT2FS_ATTR((unused)),
1585 struct process_block_struct *p;
1586 struct problem_context *pctx;
1587 blk_t blk = *block_nr;
1592 p = (struct process_block_struct *) priv_data;
1596 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
1597 /* todo: Check that the comprblk_fl is high, that the
1598 blkaddr pattern looks right (all non-holes up to
1599 first EXT2FS_COMPRESSED_BLKADDR, then all
1600 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
1601 that the feature_incompat bit is high, and that the
1602 inode is a regular file. If we're doing a "full
1603 check" (a concept introduced to e2fsck by e2compr,
1604 meaning that we look at data blocks as well as
1605 metadata) then call some library routine that
1606 checks the compressed data. I'll have to think
1607 about this, because one particularly important
1608 problem to be able to fix is to recalculate the
1609 cluster size if necessary. I think that perhaps
1610 we'd better do most/all e2compr-specific checks
1611 separately, after the non-e2compr checks. If not
1612 doing a full check, it may be useful to test that
1613 the personality is linux; e.g. if it isn't then
1614 perhaps this really is just an illegal block. */
1619 if (p->is_dir == 0) {
1621 * Should never happen, since only directories
1622 * get called with BLOCK_FLAG_HOLE
1625 printf("process_block() called with blk == 0, "
1626 "blockcnt=%d, inode %lu???\n",
1633 if (blockcnt * fs->blocksize < p->inode->i_size) {
1635 printf("Missing block (#%d) in directory inode %lu!\n",
1644 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
1649 * Simplistic fragmentation check. We merely require that the
1650 * file be contiguous. (Which can never be true for really
1651 * big files that are greater than a block group.)
1653 if (!HOLE_BLKADDR(p->previous_block)) {
1654 if (p->previous_block+1 != blk)
1657 p->previous_block = blk;
1659 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
1660 problem = PR_1_TOOBIG_DIR;
1661 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
1662 problem = PR_1_TOOBIG_REG;
1663 if (!p->is_dir && !p->is_reg && blockcnt > 0)
1664 problem = PR_1_TOOBIG_SYMLINK;
1666 if (blk < fs->super->s_first_data_block ||
1667 blk >= fs->super->s_blocks_count)
1668 problem = PR_1_ILLEGAL_BLOCK_NUM;
1671 p->num_illegal_blocks++;
1672 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
1673 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
1677 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
1679 set_latch_flags(PR_LATCH_BLOCK,
1684 pctx->blkcount = blockcnt;
1685 if (fix_problem(ctx, problem, pctx)) {
1686 blk = *block_nr = 0;
1687 ret_code = BLOCK_CHANGED;
1693 if (p->ino == EXT2_RESIZE_INO) {
1695 * The resize inode has already be sanity checked
1696 * during pass #0 (the superblock checks). All we
1697 * have to do is mark the double indirect block as
1698 * being in use; all of the other blocks are handled
1699 * by mark_table_blocks()).
1701 if (blockcnt == BLOCK_COUNT_DIND)
1702 mark_block_used(ctx, blk);
1704 mark_block_used(ctx, blk);
1707 p->last_block = blockcnt;
1709 if (p->is_dir && (blockcnt >= 0)) {
1710 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1712 if (pctx->errcode) {
1714 pctx->num = blockcnt;
1715 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1716 /* Should never get here */
1717 ctx->flags |= E2F_FLAG_ABORT;
1724 static int process_bad_block(ext2_filsys fs,
1726 e2_blkcnt_t blockcnt,
1727 blk_t ref_block EXT2FS_ATTR((unused)),
1728 int ref_offset EXT2FS_ATTR((unused)),
1731 struct process_block_struct *p;
1732 blk_t blk = *block_nr;
1735 struct problem_context *pctx;
1739 * Note: This function processes blocks for the bad blocks
1740 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
1746 p = (struct process_block_struct *) priv_data;
1750 pctx->ino = EXT2_BAD_INO;
1752 pctx->blkcount = blockcnt;
1754 if ((blk < fs->super->s_first_data_block) ||
1755 (blk >= fs->super->s_blocks_count)) {
1756 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1758 return BLOCK_CHANGED;
1764 if (ext2fs_test_block_bitmap(p->fs_meta_blocks, blk)) {
1766 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
1768 return BLOCK_CHANGED;
1770 } else if (ext2fs_test_block_bitmap(ctx->block_found_map,
1773 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
1776 return BLOCK_CHANGED;
1778 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1781 mark_block_used(ctx, blk);
1785 printf ("DEBUG: Marking %u as bad.\n", blk);
1787 ctx->fs_badblocks_count++;
1789 * If the block is not used, then mark it as used and return.
1790 * If it is already marked as found, this must mean that
1791 * there's an overlap between the filesystem table blocks
1792 * (bitmaps and inode table) and the bad block list.
1794 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1795 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1799 * Try to find the where the filesystem block was used...
1801 first_block = fs->super->s_first_data_block;
1803 for (i = 0; i < fs->group_desc_count; i++ ) {
1806 if (!ext2fs_bg_has_super(fs, i))
1808 if (blk == first_block) {
1810 if (fix_problem(ctx,
1811 PR_1_BAD_PRIMARY_SUPERBLOCK,
1814 return BLOCK_CHANGED;
1818 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1821 if ((blk > first_block) &&
1822 (blk <= first_block + fs->desc_blocks)) {
1824 pctx->blk = *block_nr;
1825 if (fix_problem(ctx,
1826 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1828 return BLOCK_CHANGED;
1832 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1836 if (blk == fs->group_desc[i].bg_block_bitmap) {
1837 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1838 ctx->invalid_block_bitmap_flag[i]++;
1839 ctx->invalid_bitmaps++;
1843 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1844 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1845 ctx->invalid_inode_bitmap_flag[i]++;
1846 ctx->invalid_bitmaps++;
1850 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1851 (blk < (fs->group_desc[i].bg_inode_table +
1852 fs->inode_blocks_per_group))) {
1854 * If there are bad blocks in the inode table,
1855 * the inode scan code will try to do
1856 * something reasonable automatically.
1860 first_block += fs->super->s_blocks_per_group;
1863 * If we've gotten to this point, then the only
1864 * possibility is that the bad block inode meta data
1865 * is using a bad block.
1867 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1868 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
1869 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
1871 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
1873 return BLOCK_CHANGED;
1875 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1882 /* Warn user that the block wasn't claimed */
1883 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1888 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
1889 const char *name, int num, blk_t *new_block)
1891 ext2_filsys fs = ctx->fs;
1892 blk_t old_block = *new_block;
1895 struct problem_context pctx;
1897 clear_problem_context(&pctx);
1900 pctx.blk = old_block;
1903 pctx.errcode = ext2fs_get_free_blocks(fs, first_block,
1904 first_block + fs->super->s_blocks_per_group,
1905 num, ctx->block_found_map, new_block);
1908 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
1909 ext2fs_unmark_valid(fs);
1912 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
1914 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
1915 ext2fs_unmark_valid(fs);
1918 ext2fs_mark_super_dirty(fs);
1919 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1920 pctx.blk2 = *new_block;
1921 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
1922 PR_1_RELOC_TO), &pctx);
1924 for (i = 0; i < num; i++) {
1926 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
1928 pctx.errcode = io_channel_read_blk(fs->io,
1929 old_block + i, 1, buf);
1931 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
1933 memset(buf, 0, fs->blocksize);
1935 pctx.blk = (*new_block) + i;
1936 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
1939 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
1941 ext2fs_free_mem(&buf);
1945 * This routine gets called at the end of pass 1 if bad blocks are
1946 * detected in the superblock, group descriptors, inode_bitmaps, or
1947 * block bitmaps. At this point, all of the blocks have been mapped
1948 * out, so we can try to allocate new block(s) to replace the bad
1951 static void handle_fs_bad_blocks(e2fsck_t ctx)
1953 ext2_filsys fs = ctx->fs;
1955 int first_block = fs->super->s_first_data_block;
1957 for (i = 0; i < fs->group_desc_count; i++) {
1958 if (ctx->invalid_block_bitmap_flag[i]) {
1959 new_table_block(ctx, first_block, i, _("block bitmap"),
1960 1, &fs->group_desc[i].bg_block_bitmap);
1962 if (ctx->invalid_inode_bitmap_flag[i]) {
1963 new_table_block(ctx, first_block, i, _("inode bitmap"),
1964 1, &fs->group_desc[i].bg_inode_bitmap);
1966 if (ctx->invalid_inode_table_flag[i]) {
1967 new_table_block(ctx, first_block, i, _("inode table"),
1968 fs->inode_blocks_per_group,
1969 &fs->group_desc[i].bg_inode_table);
1970 ctx->flags |= E2F_FLAG_RESTART;
1972 first_block += fs->super->s_blocks_per_group;
1974 ctx->invalid_bitmaps = 0;
1978 * This routine marks all blocks which are used by the superblock,
1979 * group descriptors, inode bitmaps, and block bitmaps.
1981 static void mark_table_blocks(e2fsck_t ctx)
1983 ext2_filsys fs = ctx->fs;
1987 struct problem_context pctx;
1989 clear_problem_context(&pctx);
1991 block = fs->super->s_first_data_block;
1992 for (i = 0; i < fs->group_desc_count; i++) {
1995 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
1998 * Mark the blocks used for the inode table
2000 if (fs->group_desc[i].bg_inode_table) {
2001 for (j = 0, b = fs->group_desc[i].bg_inode_table;
2002 j < fs->inode_blocks_per_group;
2004 if (ext2fs_test_block_bitmap(ctx->block_found_map,
2007 if (fix_problem(ctx,
2008 PR_1_ITABLE_CONFLICT, &pctx)) {
2009 ctx->invalid_inode_table_flag[i]++;
2010 ctx->invalid_bitmaps++;
2013 ext2fs_mark_block_bitmap(ctx->block_found_map,
2020 * Mark block used for the block bitmap
2022 if (fs->group_desc[i].bg_block_bitmap) {
2023 if (ext2fs_test_block_bitmap(ctx->block_found_map,
2024 fs->group_desc[i].bg_block_bitmap)) {
2025 pctx.blk = fs->group_desc[i].bg_block_bitmap;
2026 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
2027 ctx->invalid_block_bitmap_flag[i]++;
2028 ctx->invalid_bitmaps++;
2031 ext2fs_mark_block_bitmap(ctx->block_found_map,
2032 fs->group_desc[i].bg_block_bitmap);
2037 * Mark block used for the inode bitmap
2039 if (fs->group_desc[i].bg_inode_bitmap) {
2040 if (ext2fs_test_block_bitmap(ctx->block_found_map,
2041 fs->group_desc[i].bg_inode_bitmap)) {
2042 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
2043 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
2044 ctx->invalid_inode_bitmap_flag[i]++;
2045 ctx->invalid_bitmaps++;
2048 ext2fs_mark_block_bitmap(ctx->block_found_map,
2049 fs->group_desc[i].bg_inode_bitmap);
2052 block += fs->super->s_blocks_per_group;
2057 * Thes subroutines short circuits ext2fs_get_blocks and
2058 * ext2fs_check_directory; we use them since we already have the inode
2059 * structure, so there's no point in letting the ext2fs library read
2062 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
2065 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2068 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2069 return EXT2_ET_CALLBACK_NOTHANDLED;
2071 for (i=0; i < EXT2_N_BLOCKS; i++)
2072 blocks[i] = ctx->stashed_inode->i_block[i];
2076 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
2077 struct ext2_inode *inode)
2079 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2081 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2082 return EXT2_ET_CALLBACK_NOTHANDLED;
2083 *inode = *ctx->stashed_inode;
2087 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
2088 struct ext2_inode *inode)
2090 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2092 if ((ino == ctx->stashed_ino) && ctx->stashed_inode)
2093 *ctx->stashed_inode = *inode;
2094 return EXT2_ET_CALLBACK_NOTHANDLED;
2097 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
2099 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2101 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2102 return EXT2_ET_CALLBACK_NOTHANDLED;
2104 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
2105 return EXT2_ET_NO_DIRECTORY;
2109 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
2111 ext2_filsys fs = ctx->fs;
2114 fs->get_blocks = pass1_get_blocks;
2115 fs->check_directory = pass1_check_directory;
2116 fs->read_inode = pass1_read_inode;
2117 fs->write_inode = pass1_write_inode;
2118 ctx->stashed_ino = 0;
2121 fs->check_directory = 0;
2123 fs->write_inode = 0;