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 EXT2FS_ATTR((unused)),
132 struct ext2_inode *inode)
137 * If the index flag is set, then this is a bogus
140 if (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;
250 unsigned int storage_size, remain;
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);
266 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
268 /* header eats this space */
269 remain -= sizeof(struct ext2_ext_attr_entry);
271 /* is attribute name valid? */
272 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
273 pctx->num = entry->e_name_len;
274 problem = PR_1_ATTR_NAME_LEN;
278 /* attribute len eats this space */
279 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
281 /* check value size */
282 if (entry->e_value_size == 0 || entry->e_value_size > remain) {
283 pctx->num = entry->e_value_size;
284 problem = PR_1_ATTR_VALUE_SIZE;
288 /* e_value_block must be 0 in inode's ea */
289 if (entry->e_value_block != 0) {
290 pctx->num = entry->e_value_block;
291 problem = PR_1_ATTR_VALUE_BLOCK;
295 /* e_hash must be 0 in inode's ea */
296 if (entry->e_hash != 0) {
297 pctx->num = entry->e_hash;
298 problem = PR_1_ATTR_HASH;
302 remain -= entry->e_value_size;
304 entry = EXT2_EXT_ATTR_NEXT(entry);
308 * it seems like a corruption. it's very unlikely we could repair
309 * EA(s) in automatic fashion -bzzz
312 problem = PR_1_ATTR_HASH;
314 if (problem == 0 || !fix_problem(ctx, problem, pctx))
317 /* simple remove all possible EA(s) */
318 *((__u32 *)start) = 0UL;
319 e2fsck_write_inode_full(ctx, pctx->ino, (struct ext2_inode *) inode,
320 EXT2_INODE_SIZE(sb), "pass1");
323 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
325 struct ext2_super_block *sb = ctx->fs->super;
326 struct ext2_inode_large *inode;
330 inode = (struct ext2_inode_large *) pctx->inode;
331 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
332 /* this isn't large inode. so, nothing to check */
337 printf("inode #%u, i_extra_size %d\n", pctx->ino,
338 inode->i_extra_isize);
340 /* i_extra_isize must cover i_extra_isize + i_pad1 at least */
341 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_pad1);
342 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
344 * For now we will allow i_extra_isize to be 0, but really
345 * implementations should never allow i_extra_isize to be 0
347 if (inode->i_extra_isize &&
348 (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
349 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
351 inode->i_extra_isize = min;
352 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
353 EXT2_INODE_SIZE(sb), "pass1");
357 eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
358 inode->i_extra_isize);
359 if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
360 /* it seems inode has an extended attribute(s) in body */
361 check_ea_in_inode(ctx, pctx);
366 * Check to see if the inode might really be a directory, despite i_mode
368 * This is a lot of complexity for something for which I'm not really
369 * convinced happens frequently in the wild. If for any reason this
370 * causes any problems, take this code out.
371 * [tytso:20070331.0827EDT]
373 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
376 struct ext2_inode *inode = pctx->inode;
377 struct ext2_dir_entry *dirent;
381 int i, not_device = 0;
383 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
384 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
387 for (i=0; i < EXT2_N_BLOCKS; i++) {
388 blk = inode->i_block[i];
394 if (blk < ctx->fs->super->s_first_data_block ||
395 blk >= ctx->fs->super->s_blocks_count ||
396 ext2fs_fast_test_block_bitmap(ctx->block_found_map, blk))
397 return; /* Invalid block, can't be dir */
400 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
401 (inode->i_links_count == 1) && !not_device)
404 old_op = ehandler_operation(_("reading directory block"));
405 retval = ext2fs_read_dir_block(ctx->fs, inode->i_block[0], buf);
406 ehandler_operation(0);
410 dirent = (struct ext2_dir_entry *) buf;
411 if (((dirent->name_len & 0xFF) != 1) ||
412 (dirent->name[0] != '.') ||
413 (dirent->inode != pctx->ino) ||
414 (dirent->rec_len < 12) ||
415 (dirent->rec_len % 4) ||
416 (dirent->rec_len >= ctx->fs->blocksize - 12))
419 dirent = (struct ext2_dir_entry *) (buf + dirent->rec_len);
420 if (((dirent->name_len & 0xFF) != 2) ||
421 (dirent->name[0] != '.') ||
422 (dirent->name[1] != '.') ||
423 (dirent->rec_len < 12) ||
424 (dirent->rec_len % 4))
427 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
428 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
429 e2fsck_write_inode_full(ctx, pctx->ino, inode,
430 EXT2_INODE_SIZE(ctx->fs->super),
431 "check_is_really_dir");
435 extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
438 unsigned int threshold;
446 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
448 profile_get_uint(ctx->profile, "scratch_files",
449 "numdirs_threshold", 0, 0, &threshold);
450 profile_get_boolean(ctx->profile, "scratch_files",
451 "icount", 0, 1, &enable);
453 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
455 num_dirs = 1024; /* Guess */
457 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
458 (threshold && num_dirs <= threshold))
461 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
466 void e2fsck_pass1(e2fsck_t ctx)
470 ext2_filsys fs = ctx->fs;
472 struct ext2_inode *inode;
473 ext2_inode_scan scan;
475 #ifdef RESOURCE_TRACK
476 struct resource_track rtrack;
478 unsigned char frag, fsize;
479 struct problem_context pctx;
480 struct scan_callback_struct scan_struct;
481 struct ext2_super_block *sb = ctx->fs->super;
484 int busted_fs_time = 0;
487 #ifdef RESOURCE_TRACK
488 init_resource_track(&rtrack);
490 clear_problem_context(&pctx);
492 if (!(ctx->options & E2F_OPT_PREEN))
493 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
495 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
496 !(ctx->options & E2F_OPT_NO)) {
497 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
498 ctx->dirs_to_hash = 0;
502 mtrace_print("Pass 1");
505 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
507 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
508 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
509 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
510 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
511 max_sizes = (max_sizes * (1UL << i)) - 1;
512 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
516 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
519 * Allocate bitmaps structures
521 pctx.errcode = ext2fs_allocate_inode_bitmap(fs, _("in-use inode map"),
522 &ctx->inode_used_map);
525 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
526 ctx->flags |= E2F_FLAG_ABORT;
529 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
530 _("directory inode map"), &ctx->inode_dir_map);
533 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
534 ctx->flags |= E2F_FLAG_ABORT;
537 pctx.errcode = ext2fs_allocate_inode_bitmap(fs,
538 _("regular file inode map"), &ctx->inode_reg_map);
541 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
542 ctx->flags |= E2F_FLAG_ABORT;
545 pctx.errcode = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
546 &ctx->block_found_map);
549 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
550 ctx->flags |= E2F_FLAG_ABORT;
553 e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
554 if (!ctx->inode_link_info)
555 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
556 &ctx->inode_link_info);
558 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
559 ctx->flags |= E2F_FLAG_ABORT;
562 inode_size = EXT2_INODE_SIZE(fs->super);
563 inode = (struct ext2_inode *)
564 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
566 inodes_to_process = (struct process_inode_block *)
567 e2fsck_allocate_memory(ctx,
568 (ctx->process_inode_size *
569 sizeof(struct process_inode_block)),
570 "array of inodes to process");
571 process_inode_count = 0;
573 pctx.errcode = ext2fs_init_dblist(fs, 0);
575 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
576 ctx->flags |= E2F_FLAG_ABORT;
577 ext2fs_free_mem(&inode);
582 * If the last orphan field is set, clear it, since the pass1
583 * processing will automatically find and clear the orphans.
584 * In the future, we may want to try using the last_orphan
585 * linked list ourselves, but for now, we clear it so that the
586 * ext3 mount code won't get confused.
588 if (!(ctx->options & E2F_OPT_READONLY)) {
589 if (fs->super->s_last_orphan) {
590 fs->super->s_last_orphan = 0;
591 ext2fs_mark_super_dirty(fs);
595 mark_table_blocks(ctx);
596 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
597 "block interate buffer");
598 e2fsck_use_inode_shortcuts(ctx, 1);
599 old_op = ehandler_operation(_("opening inode scan"));
600 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
602 ehandler_operation(old_op);
604 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
605 ctx->flags |= E2F_FLAG_ABORT;
606 ext2fs_free_mem(&block_buf);
607 ext2fs_free_mem(&inode);
610 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
611 ctx->stashed_inode = inode;
612 scan_struct.ctx = ctx;
613 scan_struct.block_buf = block_buf;
614 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
616 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
618 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
619 (fs->super->s_mtime < fs->super->s_inodes_count))
623 old_op = ehandler_operation(_("getting next inode from scan"));
624 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
626 ehandler_operation(old_op);
627 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
629 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
630 if (!ctx->inode_bb_map)
632 ext2fs_mark_inode_bitmap(ctx->inode_bb_map, ino);
633 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
637 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
638 ctx->flags |= E2F_FLAG_ABORT;
645 ctx->stashed_ino = ino;
646 if (inode->i_links_count) {
647 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
648 ino, inode->i_links_count);
650 pctx.num = inode->i_links_count;
651 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
652 ctx->flags |= E2F_FLAG_ABORT;
656 if (ino == EXT2_BAD_INO) {
657 struct process_block_struct pb;
659 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
663 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
664 ctx->flags |= E2F_FLAG_ABORT;
667 pb.ino = EXT2_BAD_INO;
668 pb.num_blocks = pb.last_block = 0;
669 pb.num_illegal_blocks = 0;
670 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
671 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
675 pctx.errcode = ext2fs_block_iterate2(fs, ino, 0,
676 block_buf, process_bad_block, &pb);
677 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
679 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
680 ctx->flags |= E2F_FLAG_ABORT;
684 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
685 ctx->flags |= E2F_FLAG_ABORT;
688 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
689 clear_problem_context(&pctx);
691 } else if (ino == EXT2_ROOT_INO) {
693 * Make sure the root inode is a directory; if
694 * not, offer to clear it. It will be
695 * regnerated in pass #3.
697 if (!LINUX_S_ISDIR(inode->i_mode)) {
698 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx)) {
699 inode->i_dtime = ctx->now;
700 inode->i_links_count = 0;
701 ext2fs_icount_store(ctx->inode_link_info,
703 e2fsck_write_inode(ctx, ino, inode,
709 * If dtime is set, offer to clear it. mke2fs
710 * version 0.2b created filesystems with the
711 * dtime field set for the root and lost+found
712 * directories. We won't worry about
713 * /lost+found, since that can be regenerated
714 * easily. But we will fix the root directory
717 if (inode->i_dtime && inode->i_links_count) {
718 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
720 e2fsck_write_inode(ctx, ino, inode,
724 } else if (ino == EXT2_JOURNAL_INO) {
725 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
726 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
727 if (!LINUX_S_ISREG(inode->i_mode) &&
728 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
730 inode->i_mode = LINUX_S_IFREG;
731 e2fsck_write_inode(ctx, ino, inode,
734 check_blocks(ctx, &pctx, block_buf);
737 if ((inode->i_links_count || inode->i_blocks ||
738 inode->i_blocks || inode->i_block[0]) &&
739 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
741 memset(inode, 0, inode_size);
742 ext2fs_icount_store(ctx->inode_link_info,
744 e2fsck_write_inode_full(ctx, ino, inode,
745 inode_size, "pass1");
747 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
750 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
751 if (ino == EXT2_BOOT_LOADER_INO) {
752 if (LINUX_S_ISDIR(inode->i_mode))
753 problem = PR_1_RESERVED_BAD_MODE;
754 } else if (ino == EXT2_RESIZE_INO) {
756 !LINUX_S_ISREG(inode->i_mode))
757 problem = PR_1_RESERVED_BAD_MODE;
759 if (inode->i_mode != 0)
760 problem = PR_1_RESERVED_BAD_MODE;
763 if (fix_problem(ctx, problem, &pctx)) {
765 e2fsck_write_inode(ctx, ino, inode,
769 check_blocks(ctx, &pctx, block_buf);
773 * Check for inodes who might have been part of the
774 * orphaned list linked list. They should have gotten
775 * dealt with by now, unless the list had somehow been
778 * FIXME: In the future, inodes which are still in use
779 * (and which are therefore) pending truncation should
780 * be handled specially. Right now we just clear the
781 * dtime field, and the normal e2fsck handling of
782 * inodes where i_size and the inode blocks are
783 * inconsistent is to fix i_size, instead of releasing
784 * the extra blocks. This won't catch the inodes that
785 * was at the end of the orphan list, but it's better
786 * than nothing. The right answer is that there
787 * shouldn't be any bugs in the orphan list handling. :-)
789 if (inode->i_dtime && !busted_fs_time &&
790 inode->i_dtime < ctx->fs->super->s_inodes_count) {
791 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
792 inode->i_dtime = inode->i_links_count ?
794 e2fsck_write_inode(ctx, ino, inode,
800 * This code assumes that deleted inodes have
801 * i_links_count set to 0.
803 if (!inode->i_links_count) {
804 if (!inode->i_dtime && inode->i_mode) {
806 PR_1_ZERO_DTIME, &pctx)) {
807 inode->i_dtime = ctx->now;
808 e2fsck_write_inode(ctx, ino, inode,
815 * n.b. 0.3c ext2fs code didn't clear i_links_count for
816 * deleted files. Oops.
818 * Since all new ext2 implementations get this right,
819 * we now assume that the case of non-zero
820 * i_links_count and non-zero dtime means that we
821 * should keep the file, not delete it.
824 if (inode->i_dtime) {
825 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
827 e2fsck_write_inode(ctx, ino, inode, "pass1");
831 ext2fs_mark_inode_bitmap(ctx->inode_used_map, ino);
832 switch (fs->super->s_creator_os) {
834 frag = inode->osd2.hurd2.h_i_frag;
835 fsize = inode->osd2.hurd2.h_i_fsize;
838 frag = inode->osd2.masix2.m_i_frag;
839 fsize = inode->osd2.masix2.m_i_fsize;
845 if (inode->i_faddr || frag || fsize ||
846 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
847 mark_inode_bad(ctx, ino);
848 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
849 !(fs->super->s_feature_ro_compat &
850 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
851 (inode->osd2.linux2.l_i_blocks_hi != 0))
852 mark_inode_bad(ctx, ino);
853 if (inode->i_flags & EXT2_IMAGIC_FL) {
855 if (!ctx->inode_imagic_map)
856 alloc_imagic_map(ctx);
857 ext2fs_mark_inode_bitmap(ctx->inode_imagic_map,
860 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
861 inode->i_flags &= ~EXT2_IMAGIC_FL;
862 e2fsck_write_inode(ctx, ino,
868 check_inode_extra_space(ctx, &pctx);
869 check_is_really_dir(ctx, &pctx, block_buf);
871 if (LINUX_S_ISDIR(inode->i_mode)) {
872 ext2fs_mark_inode_bitmap(ctx->inode_dir_map, ino);
873 e2fsck_add_dir_info(ctx, ino, 0);
874 ctx->fs_directory_count++;
875 } else if (LINUX_S_ISREG (inode->i_mode)) {
876 ext2fs_mark_inode_bitmap(ctx->inode_reg_map, ino);
877 ctx->fs_regular_count++;
878 } else if (LINUX_S_ISCHR (inode->i_mode) &&
879 e2fsck_pass1_check_device_inode(fs, inode)) {
880 check_immutable(ctx, &pctx);
881 check_size(ctx, &pctx);
882 ctx->fs_chardev_count++;
883 } else if (LINUX_S_ISBLK (inode->i_mode) &&
884 e2fsck_pass1_check_device_inode(fs, inode)) {
885 check_immutable(ctx, &pctx);
886 check_size(ctx, &pctx);
887 ctx->fs_blockdev_count++;
888 } else if (LINUX_S_ISLNK (inode->i_mode) &&
889 e2fsck_pass1_check_symlink(fs, inode, block_buf)) {
890 check_immutable(ctx, &pctx);
891 ctx->fs_symlinks_count++;
892 if (ext2fs_inode_data_blocks(fs, inode) == 0) {
893 ctx->fs_fast_symlinks_count++;
894 check_blocks(ctx, &pctx, block_buf);
898 else if (LINUX_S_ISFIFO (inode->i_mode) &&
899 e2fsck_pass1_check_device_inode(fs, inode)) {
900 check_immutable(ctx, &pctx);
901 check_size(ctx, &pctx);
902 ctx->fs_fifo_count++;
903 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
904 e2fsck_pass1_check_device_inode(fs, inode)) {
905 check_immutable(ctx, &pctx);
906 check_size(ctx, &pctx);
907 ctx->fs_sockets_count++;
909 mark_inode_bad(ctx, ino);
910 if (inode->i_block[EXT2_IND_BLOCK])
912 if (inode->i_block[EXT2_DIND_BLOCK])
913 ctx->fs_dind_count++;
914 if (inode->i_block[EXT2_TIND_BLOCK])
915 ctx->fs_tind_count++;
916 if (inode->i_block[EXT2_IND_BLOCK] ||
917 inode->i_block[EXT2_DIND_BLOCK] ||
918 inode->i_block[EXT2_TIND_BLOCK] ||
920 inodes_to_process[process_inode_count].ino = ino;
921 inodes_to_process[process_inode_count].inode = *inode;
922 process_inode_count++;
924 check_blocks(ctx, &pctx, block_buf);
926 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
929 if (process_inode_count >= ctx->process_inode_size) {
930 process_inodes(ctx, block_buf);
932 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
936 process_inodes(ctx, block_buf);
937 ext2fs_close_inode_scan(scan);
940 * If any extended attribute blocks' reference counts need to
941 * be adjusted, either up (ctx->refcount_extra), or down
942 * (ctx->refcount), then fix them.
945 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
946 ea_refcount_free(ctx->refcount);
949 if (ctx->refcount_extra) {
950 adjust_extattr_refcount(ctx, ctx->refcount_extra,
952 ea_refcount_free(ctx->refcount_extra);
953 ctx->refcount_extra = 0;
956 if (ctx->invalid_bitmaps)
957 handle_fs_bad_blocks(ctx);
959 /* We don't need the block_ea_map any more */
960 if (ctx->block_ea_map) {
961 ext2fs_free_block_bitmap(ctx->block_ea_map);
962 ctx->block_ea_map = 0;
965 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
966 ext2fs_block_bitmap save_bmap;
968 save_bmap = fs->block_map;
969 fs->block_map = ctx->block_found_map;
970 clear_problem_context(&pctx);
971 pctx.errcode = ext2fs_create_resize_inode(fs);
973 fix_problem(ctx, PR_1_RESIZE_INODE_CREATE, &pctx);
974 /* Should never get here */
975 ctx->flags |= E2F_FLAG_ABORT;
978 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
980 inode->i_mtime = ctx->now;
981 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
983 fs->block_map = save_bmap;
984 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
987 if (ctx->flags & E2F_FLAG_RESTART) {
989 * Only the master copy of the superblock and block
990 * group descriptors are going to be written during a
991 * restart, so set the superblock to be used to be the
994 ctx->use_superblock = 0;
999 if (ctx->block_dup_map) {
1000 if (ctx->options & E2F_OPT_PREEN) {
1001 clear_problem_context(&pctx);
1002 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1004 e2fsck_pass1_dupblocks(ctx, block_buf);
1006 ext2fs_free_mem(&inodes_to_process);
1008 e2fsck_use_inode_shortcuts(ctx, 0);
1010 ext2fs_free_mem(&block_buf);
1011 ext2fs_free_mem(&inode);
1013 #ifdef RESOURCE_TRACK
1014 if (ctx->options & E2F_OPT_TIME2) {
1015 e2fsck_clear_progbar(ctx);
1016 print_resource_track(_("Pass 1"), &rtrack);
1022 * When the inode_scan routines call this callback at the end of the
1023 * glock group, call process_inodes.
1025 static errcode_t scan_callback(ext2_filsys fs,
1026 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1027 dgrp_t group, void * priv_data)
1029 struct scan_callback_struct *scan_struct;
1032 scan_struct = (struct scan_callback_struct *) priv_data;
1033 ctx = scan_struct->ctx;
1035 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1038 if ((ctx->progress)(ctx, 1, group+1,
1039 ctx->fs->group_desc_count))
1040 return EXT2_ET_CANCEL_REQUESTED;
1046 * Process the inodes in the "inodes to process" list.
1048 static void process_inodes(e2fsck_t ctx, char *block_buf)
1051 struct ext2_inode *old_stashed_inode;
1052 ext2_ino_t old_stashed_ino;
1053 const char *old_operation;
1055 struct problem_context pctx;
1058 printf("begin process_inodes: ");
1060 if (process_inode_count == 0)
1062 old_operation = ehandler_operation(0);
1063 old_stashed_inode = ctx->stashed_inode;
1064 old_stashed_ino = ctx->stashed_ino;
1065 qsort(inodes_to_process, process_inode_count,
1066 sizeof(struct process_inode_block), process_inode_cmp);
1067 clear_problem_context(&pctx);
1068 for (i=0; i < process_inode_count; i++) {
1069 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1070 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1073 printf("%u ", pctx.ino);
1075 sprintf(buf, _("reading indirect blocks of inode %u"),
1077 ehandler_operation(buf);
1078 check_blocks(ctx, &pctx, block_buf);
1079 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1082 ctx->stashed_inode = old_stashed_inode;
1083 ctx->stashed_ino = old_stashed_ino;
1084 process_inode_count = 0;
1086 printf("end process inodes\n");
1088 ehandler_operation(old_operation);
1091 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1093 const struct process_inode_block *ib_a =
1094 (const struct process_inode_block *) a;
1095 const struct process_inode_block *ib_b =
1096 (const struct process_inode_block *) b;
1099 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1100 ib_b->inode.i_block[EXT2_IND_BLOCK]);
1102 ret = ib_a->inode.i_file_acl - ib_b->inode.i_file_acl;
1107 * Mark an inode as being bad in some what
1109 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
1111 struct problem_context pctx;
1113 if (!ctx->inode_bad_map) {
1114 clear_problem_context(&pctx);
1116 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1117 _("bad inode map"), &ctx->inode_bad_map);
1120 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1121 /* Should never get here */
1122 ctx->flags |= E2F_FLAG_ABORT;
1126 ext2fs_mark_inode_bitmap(ctx->inode_bad_map, ino);
1131 * This procedure will allocate the inode "bb" (badblock) map table
1133 static void alloc_bb_map(e2fsck_t ctx)
1135 struct problem_context pctx;
1137 clear_problem_context(&pctx);
1138 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1139 _("inode in bad block map"),
1140 &ctx->inode_bb_map);
1143 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1144 /* Should never get here */
1145 ctx->flags |= E2F_FLAG_ABORT;
1151 * This procedure will allocate the inode imagic table
1153 static void alloc_imagic_map(e2fsck_t ctx)
1155 struct problem_context pctx;
1157 clear_problem_context(&pctx);
1158 pctx.errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
1159 _("imagic inode map"),
1160 &ctx->inode_imagic_map);
1163 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1164 /* Should never get here */
1165 ctx->flags |= E2F_FLAG_ABORT;
1171 * Marks a block as in use, setting the dup_map if it's been set
1172 * already. Called by process_block and process_bad_block.
1174 * WARNING: Assumes checks have already been done to make sure block
1175 * is valid. This is true in both process_block and process_bad_block.
1177 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk_t block)
1179 struct problem_context pctx;
1181 clear_problem_context(&pctx);
1183 if (ext2fs_fast_test_block_bitmap(ctx->block_found_map, block)) {
1184 if (!ctx->block_dup_map) {
1185 pctx.errcode = ext2fs_allocate_block_bitmap(ctx->fs,
1186 _("multiply claimed block map"),
1187 &ctx->block_dup_map);
1190 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1192 /* Should never get here */
1193 ctx->flags |= E2F_FLAG_ABORT;
1197 ext2fs_fast_mark_block_bitmap(ctx->block_dup_map, block);
1199 ext2fs_fast_mark_block_bitmap(ctx->block_found_map, block);
1204 * Adjust the extended attribute block's reference counts at the end
1205 * of pass 1, either by subtracting out references for EA blocks that
1206 * are still referenced in ctx->refcount, or by adding references for
1207 * EA blocks that had extra references as accounted for in
1208 * ctx->refcount_extra.
1210 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1211 char *block_buf, int adjust_sign)
1213 struct ext2_ext_attr_header *header;
1214 struct problem_context pctx;
1215 ext2_filsys fs = ctx->fs;
1220 clear_problem_context(&pctx);
1222 ea_refcount_intr_begin(refcount);
1224 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1227 pctx.errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1229 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1232 header = (struct ext2_ext_attr_header *) block_buf;
1233 pctx.blkcount = header->h_refcount;
1234 should_be = header->h_refcount + adjust_sign * count;
1235 pctx.num = should_be;
1236 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1237 header->h_refcount = should_be;
1238 pctx.errcode = ext2fs_write_ext_attr(fs, blk,
1241 fix_problem(ctx, PR_1_EXTATTR_WRITE, &pctx);
1249 * Handle processing the extended attribute blocks
1251 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1254 ext2_filsys fs = ctx->fs;
1255 ext2_ino_t ino = pctx->ino;
1256 struct ext2_inode *inode = pctx->inode;
1259 struct ext2_ext_attr_header *header;
1260 struct ext2_ext_attr_entry *entry;
1262 region_t region = 0;
1264 blk = inode->i_file_acl;
1269 * If the Extended attribute flag isn't set, then a non-zero
1270 * file acl means that the inode is corrupted.
1272 * Or if the extended attribute block is an invalid block,
1273 * then the inode is also corrupted.
1275 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1276 (blk < fs->super->s_first_data_block) ||
1277 (blk >= fs->super->s_blocks_count)) {
1278 mark_inode_bad(ctx, ino);
1282 /* If ea bitmap hasn't been allocated, create it */
1283 if (!ctx->block_ea_map) {
1284 pctx->errcode = ext2fs_allocate_block_bitmap(fs,
1285 _("ext attr block map"),
1286 &ctx->block_ea_map);
1287 if (pctx->errcode) {
1289 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1290 ctx->flags |= E2F_FLAG_ABORT;
1295 /* Create the EA refcount structure if necessary */
1296 if (!ctx->refcount) {
1297 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1298 if (pctx->errcode) {
1300 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1301 ctx->flags |= E2F_FLAG_ABORT;
1307 /* Debugging text */
1308 printf("Inode %u has EA block %u\n", ino, blk);
1311 /* Have we seen this EA block before? */
1312 if (ext2fs_fast_test_block_bitmap(ctx->block_ea_map, blk)) {
1313 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1315 /* Ooops, this EA was referenced more than it stated */
1316 if (!ctx->refcount_extra) {
1317 pctx->errcode = ea_refcount_create(0,
1318 &ctx->refcount_extra);
1319 if (pctx->errcode) {
1321 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1322 ctx->flags |= E2F_FLAG_ABORT;
1326 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1331 * OK, we haven't seen this EA block yet. So we need to
1335 pctx->errcode = ext2fs_read_ext_attr(fs, blk, block_buf);
1336 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1338 header = (struct ext2_ext_attr_header *) block_buf;
1339 pctx->blk = inode->i_file_acl;
1340 if (((ctx->ext_attr_ver == 1) &&
1341 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1342 ((ctx->ext_attr_ver == 2) &&
1343 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1344 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1348 if (header->h_blocks != 1) {
1349 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1353 region = region_create(0, fs->blocksize);
1355 fix_problem(ctx, PR_1_EA_ALLOC_REGION, pctx);
1356 ctx->flags |= E2F_FLAG_ABORT;
1359 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1360 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1364 entry = (struct ext2_ext_attr_entry *)(header+1);
1365 end = block_buf + fs->blocksize;
1366 while ((char *)entry < end && *(__u32 *)entry) {
1367 if (region_allocate(region, (char *)entry - (char *)header,
1368 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1369 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1372 if ((ctx->ext_attr_ver == 1 &&
1373 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1374 (ctx->ext_attr_ver == 2 &&
1375 entry->e_name_index == 0)) {
1376 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1379 if (entry->e_value_block != 0) {
1380 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1383 if (entry->e_value_size &&
1384 region_allocate(region, entry->e_value_offs,
1385 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1386 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1389 entry = EXT2_EXT_ATTR_NEXT(entry);
1391 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1392 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1395 region_free(region);
1397 count = header->h_refcount - 1;
1399 ea_refcount_store(ctx->refcount, blk, count);
1400 mark_block_used(ctx, blk);
1401 ext2fs_fast_mark_block_bitmap(ctx->block_ea_map, blk);
1406 region_free(region);
1407 inode->i_file_acl = 0;
1408 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1412 /* Returns 1 if bad htree, 0 if OK */
1413 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1414 ext2_ino_t ino EXT2FS_ATTR((unused)),
1415 struct ext2_inode *inode,
1418 struct ext2_dx_root_info *root;
1419 ext2_filsys fs = ctx->fs;
1423 if ((!LINUX_S_ISDIR(inode->i_mode) &&
1424 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1425 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1426 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
1429 blk = inode->i_block[0];
1431 (blk < fs->super->s_first_data_block) ||
1432 (blk >= fs->super->s_blocks_count)) &&
1433 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1436 retval = io_channel_read_blk(fs->io, blk, 1, block_buf);
1437 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1440 /* XXX should check that beginning matches a directory */
1441 root = (struct ext2_dx_root_info *) (block_buf + 24);
1443 if ((root->reserved_zero || root->info_length < 8) &&
1444 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1447 pctx->num = root->hash_version;
1448 if ((root->hash_version != EXT2_HASH_LEGACY) &&
1449 (root->hash_version != EXT2_HASH_HALF_MD4) &&
1450 (root->hash_version != EXT2_HASH_TEA) &&
1451 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
1454 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
1455 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
1458 pctx->num = root->indirect_levels;
1459 if ((root->indirect_levels > 1) &&
1460 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
1467 * This subroutine is called on each inode to account for all of the
1468 * blocks used by that inode.
1470 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
1473 ext2_filsys fs = ctx->fs;
1474 struct process_block_struct pb;
1475 ext2_ino_t ino = pctx->ino;
1476 struct ext2_inode *inode = pctx->inode;
1478 int dirty_inode = 0;
1484 pb.num_illegal_blocks = 0;
1485 pb.suppress = 0; pb.clear = 0;
1488 pb.previous_block = 0;
1489 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
1490 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
1491 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
1498 if (inode->i_flags & EXT2_COMPRBLK_FL) {
1499 if (fs->super->s_feature_incompat &
1500 EXT2_FEATURE_INCOMPAT_COMPRESSION)
1503 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
1504 inode->i_flags &= ~EXT2_COMPRBLK_FL;
1510 if (inode->i_file_acl && check_ext_attr(ctx, pctx, block_buf))
1513 if (ext2fs_inode_has_valid_blocks(inode))
1514 pctx->errcode = ext2fs_block_iterate2(fs, ino,
1515 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
1516 block_buf, process_block, &pb);
1517 end_problem_latch(ctx, PR_LATCH_BLOCK);
1518 end_problem_latch(ctx, PR_LATCH_TOOBIG);
1519 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1522 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
1524 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group)
1525 ctx->fs_fragmented++;
1528 inode->i_links_count = 0;
1529 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1530 inode->i_dtime = ctx->now;
1532 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1533 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1534 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1536 * The inode was probably partially accounted for
1537 * before processing was aborted, so we need to
1538 * restart the pass 1 scan.
1540 ctx->flags |= E2F_FLAG_RESTART;
1544 if (inode->i_flags & EXT2_INDEX_FL) {
1545 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
1546 inode->i_flags &= ~EXT2_INDEX_FL;
1550 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
1554 if (ctx->dirs_to_hash && pb.is_dir &&
1555 !(inode->i_flags & EXT2_INDEX_FL) &&
1556 ((inode->i_size / fs->blocksize) >= 3))
1557 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
1559 if (!pb.num_blocks && pb.is_dir) {
1560 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
1561 inode->i_links_count = 0;
1562 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1563 inode->i_dtime = ctx->now;
1565 ext2fs_unmark_inode_bitmap(ctx->inode_dir_map, ino);
1566 ext2fs_unmark_inode_bitmap(ctx->inode_reg_map, ino);
1567 ext2fs_unmark_inode_bitmap(ctx->inode_used_map, ino);
1568 ctx->fs_directory_count--;
1573 pb.num_blocks *= (fs->blocksize / 512);
1575 printf("inode %u, i_size = %lu, last_block = %lld, i_blocks=%lu, num_blocks = %lu\n",
1576 ino, inode->i_size, pb.last_block, inode->i_blocks,
1580 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
1581 if (nblock > (pb.last_block + 1))
1583 else if (nblock < (pb.last_block + 1)) {
1584 if (((pb.last_block + 1) - nblock) >
1585 fs->super->s_prealloc_dir_blocks)
1589 size = EXT2_I_SIZE(inode);
1590 if ((pb.last_block >= 0) &&
1591 (size < (__u64) pb.last_block * fs->blocksize))
1593 else if (size > ext2_max_sizes[fs->super->s_log_block_size])
1596 /* i_size for symlinks is checked elsewhere */
1597 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
1598 pctx->num = (pb.last_block+1) * fs->blocksize;
1599 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
1600 inode->i_size = pctx->num;
1601 if (!LINUX_S_ISDIR(inode->i_mode))
1602 inode->i_size_high = pctx->num >> 32;
1607 if (LINUX_S_ISREG(inode->i_mode) &&
1608 (inode->i_size_high || inode->i_size & 0x80000000UL))
1610 if (pb.num_blocks != inode->i_blocks) {
1611 pctx->num = pb.num_blocks;
1612 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
1613 inode->i_blocks = pb.num_blocks;
1620 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
1625 * Helper function called by process block when an illegal block is
1626 * found. It returns a description about why the block is illegal
1628 static char *describe_illegal_block(ext2_filsys fs, blk_t block)
1632 static char problem[80];
1634 super = fs->super->s_first_data_block;
1635 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
1636 if (block < super) {
1637 sprintf(problem, "< FIRSTBLOCK (%u)", super);
1639 } else if (block >= fs->super->s_blocks_count) {
1640 sprintf(problem, "> BLOCKS (%u)", fs->super->s_blocks_count);
1643 for (i = 0; i < fs->group_desc_count; i++) {
1644 if (block == super) {
1645 sprintf(problem, "is the superblock in group %d", i);
1648 if (block > super &&
1649 block <= (super + fs->desc_blocks)) {
1650 sprintf(problem, "is in the group descriptors "
1654 if (block == fs->group_desc[i].bg_block_bitmap) {
1655 sprintf(problem, "is the block bitmap of group %d", i);
1658 if (block == fs->group_desc[i].bg_inode_bitmap) {
1659 sprintf(problem, "is the inode bitmap of group %d", i);
1662 if (block >= fs->group_desc[i].bg_inode_table &&
1663 (block < fs->group_desc[i].bg_inode_table
1664 + fs->inode_blocks_per_group)) {
1665 sprintf(problem, "is in the inode table of group %d",
1669 super += fs->super->s_blocks_per_group;
1676 * This is a helper function for check_blocks().
1678 static int process_block(ext2_filsys fs,
1680 e2_blkcnt_t blockcnt,
1681 blk_t ref_block EXT2FS_ATTR((unused)),
1682 int ref_offset EXT2FS_ATTR((unused)),
1685 struct process_block_struct *p;
1686 struct problem_context *pctx;
1687 blk_t blk = *block_nr;
1692 p = (struct process_block_struct *) priv_data;
1696 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
1697 /* todo: Check that the comprblk_fl is high, that the
1698 blkaddr pattern looks right (all non-holes up to
1699 first EXT2FS_COMPRESSED_BLKADDR, then all
1700 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
1701 that the feature_incompat bit is high, and that the
1702 inode is a regular file. If we're doing a "full
1703 check" (a concept introduced to e2fsck by e2compr,
1704 meaning that we look at data blocks as well as
1705 metadata) then call some library routine that
1706 checks the compressed data. I'll have to think
1707 about this, because one particularly important
1708 problem to be able to fix is to recalculate the
1709 cluster size if necessary. I think that perhaps
1710 we'd better do most/all e2compr-specific checks
1711 separately, after the non-e2compr checks. If not
1712 doing a full check, it may be useful to test that
1713 the personality is linux; e.g. if it isn't then
1714 perhaps this really is just an illegal block. */
1719 if (p->is_dir == 0) {
1721 * Should never happen, since only directories
1722 * get called with BLOCK_FLAG_HOLE
1725 printf("process_block() called with blk == 0, "
1726 "blockcnt=%d, inode %lu???\n",
1733 if (blockcnt * fs->blocksize < p->inode->i_size) {
1735 printf("Missing block (#%d) in directory inode %lu!\n",
1744 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
1749 * Simplistic fragmentation check. We merely require that the
1750 * file be contiguous. (Which can never be true for really
1751 * big files that are greater than a block group.)
1753 if (!HOLE_BLKADDR(p->previous_block)) {
1754 if (p->previous_block+1 != blk)
1757 p->previous_block = blk;
1759 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
1760 problem = PR_1_TOOBIG_DIR;
1761 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
1762 problem = PR_1_TOOBIG_REG;
1763 if (!p->is_dir && !p->is_reg && blockcnt > 0)
1764 problem = PR_1_TOOBIG_SYMLINK;
1766 if (blk < fs->super->s_first_data_block ||
1767 blk >= fs->super->s_blocks_count)
1768 problem = PR_1_ILLEGAL_BLOCK_NUM;
1771 p->num_illegal_blocks++;
1772 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
1773 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
1777 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
1779 set_latch_flags(PR_LATCH_BLOCK,
1784 pctx->blkcount = blockcnt;
1785 if (fix_problem(ctx, problem, pctx)) {
1786 blk = *block_nr = 0;
1787 ret_code = BLOCK_CHANGED;
1793 if (p->ino == EXT2_RESIZE_INO) {
1795 * The resize inode has already be sanity checked
1796 * during pass #0 (the superblock checks). All we
1797 * have to do is mark the double indirect block as
1798 * being in use; all of the other blocks are handled
1799 * by mark_table_blocks()).
1801 if (blockcnt == BLOCK_COUNT_DIND)
1802 mark_block_used(ctx, blk);
1804 mark_block_used(ctx, blk);
1807 p->last_block = blockcnt;
1809 if (p->is_dir && (blockcnt >= 0)) {
1810 pctx->errcode = ext2fs_add_dir_block(fs->dblist, p->ino,
1812 if (pctx->errcode) {
1814 pctx->num = blockcnt;
1815 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1816 /* Should never get here */
1817 ctx->flags |= E2F_FLAG_ABORT;
1824 static int process_bad_block(ext2_filsys fs,
1826 e2_blkcnt_t blockcnt,
1827 blk_t ref_block EXT2FS_ATTR((unused)),
1828 int ref_offset EXT2FS_ATTR((unused)),
1831 struct process_block_struct *p;
1832 blk_t blk = *block_nr;
1835 struct problem_context *pctx;
1839 * Note: This function processes blocks for the bad blocks
1840 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
1846 p = (struct process_block_struct *) priv_data;
1850 pctx->ino = EXT2_BAD_INO;
1852 pctx->blkcount = blockcnt;
1854 if ((blk < fs->super->s_first_data_block) ||
1855 (blk >= fs->super->s_blocks_count)) {
1856 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
1858 return BLOCK_CHANGED;
1864 if (ext2fs_test_block_bitmap(p->fs_meta_blocks, blk)) {
1866 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
1868 return BLOCK_CHANGED;
1870 } else if (ext2fs_test_block_bitmap(ctx->block_found_map,
1873 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
1876 return BLOCK_CHANGED;
1878 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1881 mark_block_used(ctx, blk);
1885 printf ("DEBUG: Marking %u as bad.\n", blk);
1887 ctx->fs_badblocks_count++;
1889 * If the block is not used, then mark it as used and return.
1890 * If it is already marked as found, this must mean that
1891 * there's an overlap between the filesystem table blocks
1892 * (bitmaps and inode table) and the bad block list.
1894 if (!ext2fs_test_block_bitmap(ctx->block_found_map, blk)) {
1895 ext2fs_mark_block_bitmap(ctx->block_found_map, blk);
1899 * Try to find the where the filesystem block was used...
1901 first_block = fs->super->s_first_data_block;
1903 for (i = 0; i < fs->group_desc_count; i++ ) {
1906 if (!ext2fs_bg_has_super(fs, i))
1908 if (blk == first_block) {
1910 if (fix_problem(ctx,
1911 PR_1_BAD_PRIMARY_SUPERBLOCK,
1914 return BLOCK_CHANGED;
1918 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
1921 if ((blk > first_block) &&
1922 (blk <= first_block + fs->desc_blocks)) {
1924 pctx->blk = *block_nr;
1925 if (fix_problem(ctx,
1926 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
1928 return BLOCK_CHANGED;
1932 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
1936 if (blk == fs->group_desc[i].bg_block_bitmap) {
1937 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
1938 ctx->invalid_block_bitmap_flag[i]++;
1939 ctx->invalid_bitmaps++;
1943 if (blk == fs->group_desc[i].bg_inode_bitmap) {
1944 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
1945 ctx->invalid_inode_bitmap_flag[i]++;
1946 ctx->invalid_bitmaps++;
1950 if ((blk >= fs->group_desc[i].bg_inode_table) &&
1951 (blk < (fs->group_desc[i].bg_inode_table +
1952 fs->inode_blocks_per_group))) {
1954 * If there are bad blocks in the inode table,
1955 * the inode scan code will try to do
1956 * something reasonable automatically.
1960 first_block += fs->super->s_blocks_per_group;
1963 * If we've gotten to this point, then the only
1964 * possibility is that the bad block inode meta data
1965 * is using a bad block.
1967 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
1968 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
1969 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
1971 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
1973 return BLOCK_CHANGED;
1975 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1982 /* Warn user that the block wasn't claimed */
1983 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
1988 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
1989 const char *name, int num, blk_t *new_block)
1991 ext2_filsys fs = ctx->fs;
1992 blk_t old_block = *new_block;
1996 struct problem_context pctx;
1998 clear_problem_context(&pctx);
2001 pctx.blk = old_block;
2004 last_block = ext2fs_group_last_block(fs, group);
2005 pctx.errcode = ext2fs_get_free_blocks(fs, first_block, last_block,
2006 num, ctx->block_found_map, new_block);
2009 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
2010 ext2fs_unmark_valid(fs);
2013 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
2015 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
2016 ext2fs_unmark_valid(fs);
2019 ext2fs_mark_super_dirty(fs);
2020 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2021 pctx.blk2 = *new_block;
2022 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
2023 PR_1_RELOC_TO), &pctx);
2025 for (i = 0; i < num; i++) {
2027 ext2fs_mark_block_bitmap(ctx->block_found_map, (*new_block)+i);
2029 pctx.errcode = io_channel_read_blk(fs->io,
2030 old_block + i, 1, buf);
2032 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
2034 memset(buf, 0, fs->blocksize);
2036 pctx.blk = (*new_block) + i;
2037 pctx.errcode = io_channel_write_blk(fs->io, pctx.blk,
2040 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
2042 ext2fs_free_mem(&buf);
2046 * This routine gets called at the end of pass 1 if bad blocks are
2047 * detected in the superblock, group descriptors, inode_bitmaps, or
2048 * block bitmaps. At this point, all of the blocks have been mapped
2049 * out, so we can try to allocate new block(s) to replace the bad
2052 static void handle_fs_bad_blocks(e2fsck_t ctx)
2054 ext2_filsys fs = ctx->fs;
2058 for (i = 0; i < fs->group_desc_count; i++) {
2059 first_block = ext2fs_group_first_block(fs, i);
2061 if (ctx->invalid_block_bitmap_flag[i]) {
2062 new_table_block(ctx, first_block, i, _("block bitmap"),
2063 1, &fs->group_desc[i].bg_block_bitmap);
2065 if (ctx->invalid_inode_bitmap_flag[i]) {
2066 new_table_block(ctx, first_block, i, _("inode bitmap"),
2067 1, &fs->group_desc[i].bg_inode_bitmap);
2069 if (ctx->invalid_inode_table_flag[i]) {
2070 new_table_block(ctx, first_block, i, _("inode table"),
2071 fs->inode_blocks_per_group,
2072 &fs->group_desc[i].bg_inode_table);
2073 ctx->flags |= E2F_FLAG_RESTART;
2076 ctx->invalid_bitmaps = 0;
2080 * This routine marks all blocks which are used by the superblock,
2081 * group descriptors, inode bitmaps, and block bitmaps.
2083 static void mark_table_blocks(e2fsck_t ctx)
2085 ext2_filsys fs = ctx->fs;
2089 struct problem_context pctx;
2091 clear_problem_context(&pctx);
2093 for (i = 0; i < fs->group_desc_count; i++) {
2096 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
2099 * Mark the blocks used for the inode table
2101 if (fs->group_desc[i].bg_inode_table) {
2102 for (j = 0, b = fs->group_desc[i].bg_inode_table;
2103 j < fs->inode_blocks_per_group;
2105 if (ext2fs_test_block_bitmap(ctx->block_found_map,
2108 if (fix_problem(ctx,
2109 PR_1_ITABLE_CONFLICT, &pctx)) {
2110 ctx->invalid_inode_table_flag[i]++;
2111 ctx->invalid_bitmaps++;
2114 ext2fs_mark_block_bitmap(ctx->block_found_map,
2121 * Mark block used for the block bitmap
2123 if (fs->group_desc[i].bg_block_bitmap) {
2124 if (ext2fs_test_block_bitmap(ctx->block_found_map,
2125 fs->group_desc[i].bg_block_bitmap)) {
2126 pctx.blk = fs->group_desc[i].bg_block_bitmap;
2127 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
2128 ctx->invalid_block_bitmap_flag[i]++;
2129 ctx->invalid_bitmaps++;
2132 ext2fs_mark_block_bitmap(ctx->block_found_map,
2133 fs->group_desc[i].bg_block_bitmap);
2138 * Mark block used for the inode bitmap
2140 if (fs->group_desc[i].bg_inode_bitmap) {
2141 if (ext2fs_test_block_bitmap(ctx->block_found_map,
2142 fs->group_desc[i].bg_inode_bitmap)) {
2143 pctx.blk = fs->group_desc[i].bg_inode_bitmap;
2144 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
2145 ctx->invalid_inode_bitmap_flag[i]++;
2146 ctx->invalid_bitmaps++;
2149 ext2fs_mark_block_bitmap(ctx->block_found_map,
2150 fs->group_desc[i].bg_inode_bitmap);
2157 * Thes subroutines short circuits ext2fs_get_blocks and
2158 * ext2fs_check_directory; we use them since we already have the inode
2159 * structure, so there's no point in letting the ext2fs library read
2162 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
2165 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2168 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2169 return EXT2_ET_CALLBACK_NOTHANDLED;
2171 for (i=0; i < EXT2_N_BLOCKS; i++)
2172 blocks[i] = ctx->stashed_inode->i_block[i];
2176 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
2177 struct ext2_inode *inode)
2179 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2181 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2182 return EXT2_ET_CALLBACK_NOTHANDLED;
2183 *inode = *ctx->stashed_inode;
2187 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
2188 struct ext2_inode *inode)
2190 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2192 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
2193 (inode != ctx->stashed_inode))
2194 *ctx->stashed_inode = *inode;
2195 return EXT2_ET_CALLBACK_NOTHANDLED;
2198 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
2200 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2202 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2203 return EXT2_ET_CALLBACK_NOTHANDLED;
2205 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
2206 return EXT2_ET_NO_DIRECTORY;
2210 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
2212 ext2_filsys fs = ctx->fs;
2215 fs->get_blocks = pass1_get_blocks;
2216 fs->check_directory = pass1_check_directory;
2217 fs->read_inode = pass1_read_inode;
2218 fs->write_inode = pass1_write_inode;
2219 ctx->stashed_ino = 0;
2222 fs->check_directory = 0;
2224 fs->write_inode = 0;