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() */
49 #include <ext2fs/ext2_ext_attr.h>
53 #ifdef NO_INLINE_FUNCS
56 #define _INLINE_ inline
59 static int process_block(ext2_filsys fs, blk64_t *blocknr,
60 e2_blkcnt_t blockcnt, blk64_t ref_blk,
61 int ref_offset, void *priv_data);
62 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
63 e2_blkcnt_t blockcnt, blk64_t ref_blk,
64 int ref_offset, void *priv_data);
65 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
67 static void mark_table_blocks(e2fsck_t ctx);
68 static void alloc_bb_map(e2fsck_t ctx);
69 static void alloc_imagic_map(e2fsck_t ctx);
70 static void mark_inode_bad(e2fsck_t ctx, ino_t ino);
71 static void handle_fs_bad_blocks(e2fsck_t ctx);
72 static void process_inodes(e2fsck_t ctx, char *block_buf);
73 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
74 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
75 dgrp_t group, void * priv_data);
76 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
77 char *block_buf, int adjust_sign);
78 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
80 struct process_block_struct {
82 unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
83 fragmented:1, compressed:1, bbcheck:1,
87 e2_blkcnt_t last_block;
88 e2_blkcnt_t last_init_lblock;
89 e2_blkcnt_t last_db_block;
90 int num_illegal_blocks;
91 blk64_t previous_block;
92 struct ext2_inode *inode;
93 struct problem_context *pctx;
94 ext2fs_block_bitmap fs_meta_blocks;
98 struct process_inode_block {
100 struct ext2_inode inode;
103 struct scan_callback_struct {
109 * For the inodes to process list.
111 static struct process_inode_block *inodes_to_process;
112 static int process_inode_count;
114 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
115 EXT2_MIN_BLOCK_LOG_SIZE + 1];
118 * Free all memory allocated by pass1 in preparation for restarting
121 static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
123 ext2fs_free_mem(&inodes_to_process);
124 inodes_to_process = 0;
128 * Check to make sure a device inode is real. Returns 1 if the device
129 * checks out, 0 if not.
131 * Note: this routine is now also used to check FIFO's and Sockets,
132 * since they have the same requirement; the i_block fields should be
135 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
136 struct ext2_inode *inode)
141 * If the index flag is set, then this is a bogus
144 if (inode->i_flags & EXT2_INDEX_FL)
148 * We should be able to do the test below all the time, but
149 * because the kernel doesn't forcibly clear the device
150 * inode's additional i_block fields, there are some rare
151 * occasions when a legitimate device inode will have non-zero
152 * additional i_block fields. So for now, we only complain
153 * when the immutable flag is set, which should never happen
154 * for devices. (And that's when the problem is caused, since
155 * you can't set or clear immutable flags for devices.) Once
156 * the kernel has been fixed we can change this...
158 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
159 for (i=4; i < EXT2_N_BLOCKS; i++)
160 if (inode->i_block[i])
167 * Check to make sure a symlink inode is real. Returns 1 if the symlink
168 * checks out, 0 if not.
170 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
171 struct ext2_inode *inode, char *buf)
176 ext2_extent_handle_t handle;
177 struct ext2_extent_info info;
178 struct ext2fs_extent extent;
180 if ((inode->i_size_high || inode->i_size == 0) ||
181 (inode->i_flags & EXT2_INDEX_FL))
184 if (inode->i_flags & EXT4_EXTENTS_FL) {
185 if (inode->i_size > fs->blocksize)
187 if (ext2fs_extent_open2(fs, ino, inode, &handle))
190 if (ext2fs_extent_get_info(handle, &info) ||
191 (info.num_entries != 1) ||
192 (info.max_depth != 0))
194 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
195 (extent.e_lblk != 0) ||
196 (extent.e_len != 1) ||
197 (extent.e_pblk < fs->super->s_first_data_block) ||
198 (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
202 ext2fs_extent_free(handle);
206 blocks = ext2fs_inode_data_blocks2(fs, inode);
208 if ((inode->i_size >= fs->blocksize) ||
209 (blocks != fs->blocksize >> 9) ||
210 (inode->i_block[0] < fs->super->s_first_data_block) ||
211 (inode->i_block[0] >= ext2fs_blocks_count(fs->super)))
214 for (i = 1; i < EXT2_N_BLOCKS; i++)
215 if (inode->i_block[i])
218 if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf))
221 len = strnlen(buf, fs->blocksize);
222 if (len == fs->blocksize)
225 if (inode->i_size >= sizeof(inode->i_block))
228 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
229 if (len == sizeof(inode->i_block))
232 if (len != inode->i_size)
238 * If the immutable (or append-only) flag is set on the inode, offer
241 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
242 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
244 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
247 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
250 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
251 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
255 * If device, fifo or socket, check size is zero -- if not offer to
258 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
260 struct ext2_inode *inode = pctx->inode;
262 if (EXT2_I_SIZE(inode) == 0)
265 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
268 ext2fs_inode_size_set(ctx->fs, inode, 0);
269 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
272 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
274 struct ext2_super_block *sb = ctx->fs->super;
275 struct ext2_inode_large *inode;
276 struct ext2_ext_attr_entry *entry;
278 unsigned int storage_size, remain;
279 problem_t problem = 0;
281 inode = (struct ext2_inode_large *) pctx->inode;
282 storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
283 inode->i_extra_isize;
284 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
285 inode->i_extra_isize + sizeof(__u32);
286 entry = (struct ext2_ext_attr_entry *) start;
288 /* scan all entry's headers first */
290 /* take finish entry 0UL into account */
291 remain = storage_size - sizeof(__u32);
293 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
296 /* header eats this space */
297 remain -= sizeof(struct ext2_ext_attr_entry);
299 /* is attribute name valid? */
300 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
301 pctx->num = entry->e_name_len;
302 problem = PR_1_ATTR_NAME_LEN;
306 /* attribute len eats this space */
307 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
309 /* check value size */
310 if (entry->e_value_size > remain) {
311 pctx->num = entry->e_value_size;
312 problem = PR_1_ATTR_VALUE_SIZE;
316 /* e_value_block must be 0 in inode's ea */
317 if (entry->e_value_block != 0) {
318 pctx->num = entry->e_value_block;
319 problem = PR_1_ATTR_VALUE_BLOCK;
323 /* Value size cannot be larger than EA space in inode */
324 if (entry->e_value_offs > storage_size ||
325 entry->e_value_offs + entry->e_value_size > storage_size) {
326 problem = PR_1_INODE_EA_BAD_VALUE;
330 hash = ext2fs_ext_attr_hash_entry(entry,
331 start + entry->e_value_offs);
333 /* e_hash may be 0 in older inode's ea */
334 if (entry->e_hash != 0 && entry->e_hash != hash) {
335 pctx->num = entry->e_hash;
336 problem = PR_1_ATTR_HASH;
340 remain -= entry->e_value_size;
342 entry = EXT2_EXT_ATTR_NEXT(entry);
346 * it seems like a corruption. it's very unlikely we could repair
347 * EA(s) in automatic fashion -bzzz
349 if (problem == 0 || !fix_problem(ctx, problem, pctx))
352 /* simply remove all possible EA(s) */
353 *((__u32 *)start) = 0UL;
354 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
355 EXT2_INODE_SIZE(sb), "pass1");
358 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
360 struct ext2_super_block *sb = ctx->fs->super;
361 struct ext2_inode_large *inode;
365 inode = (struct ext2_inode_large *) pctx->inode;
366 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
367 /* this isn't large inode. so, nothing to check */
372 printf("inode #%u, i_extra_size %d\n", pctx->ino,
373 inode->i_extra_isize);
375 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
376 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
377 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
379 * For now we will allow i_extra_isize to be 0, but really
380 * implementations should never allow i_extra_isize to be 0
382 if (inode->i_extra_isize &&
383 (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
384 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
386 inode->i_extra_isize = min;
387 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
388 EXT2_INODE_SIZE(sb), "pass1");
392 eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
393 inode->i_extra_isize);
394 if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
395 /* it seems inode has an extended attribute(s) in body */
396 check_ea_in_inode(ctx, pctx);
401 * Check to see if the inode might really be a directory, despite i_mode
403 * This is a lot of complexity for something for which I'm not really
404 * convinced happens frequently in the wild. If for any reason this
405 * causes any problems, take this code out.
406 * [tytso:20070331.0827EDT]
408 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
411 struct ext2_inode *inode = pctx->inode;
412 struct ext2_dir_entry *dirent;
415 unsigned int i, rec_len, not_device = 0;
419 * If the mode looks OK, we believe it. If the first block in
420 * the i_block array is 0, this cannot be a directory. If the
421 * inode is extent-mapped, it is still the case that the latter
422 * cannot be 0 - the magic number in the extent header would make
425 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
426 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
430 * Check the block numbers in the i_block array for validity:
431 * zero blocks are skipped (but the first one cannot be zero -
432 * see above), other blocks are checked against the first and
433 * max data blocks (from the the superblock) and against the
434 * block bitmap. Any invalid block found means this cannot be
437 * If there are non-zero blocks past the fourth entry, then
438 * this cannot be a device file: we remember that for the next
441 * For extent mapped files, we don't do any sanity checking:
442 * just try to get the phys block of logical block 0 and run
446 extent_fs = (ctx->fs->super->s_feature_incompat &
447 EXT3_FEATURE_INCOMPAT_EXTENTS);
448 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
450 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
453 /* device files are never extent mapped */
456 for (i=0; i < EXT2_N_BLOCKS; i++) {
457 blk = inode->i_block[i];
463 if (blk < ctx->fs->super->s_first_data_block ||
464 blk >= ext2fs_blocks_count(ctx->fs->super) ||
465 ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
467 return; /* Invalid block, can't be dir */
469 blk = inode->i_block[0];
473 * If the mode says this is a device file and the i_links_count field
474 * is sane and we have not ruled it out as a device file previously,
475 * we declare it a device file, not a directory.
477 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
478 (inode->i_links_count == 1) && !not_device)
481 /* read the first block */
482 ehandler_operation(_("reading directory block"));
483 retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0);
484 ehandler_operation(0);
488 dirent = (struct ext2_dir_entry *) buf;
489 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
492 if (((dirent->name_len & 0xFF) != 1) ||
493 (dirent->name[0] != '.') ||
494 (dirent->inode != pctx->ino) ||
497 (rec_len >= ctx->fs->blocksize - 12))
500 dirent = (struct ext2_dir_entry *) (buf + rec_len);
501 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
504 if (((dirent->name_len & 0xFF) != 2) ||
505 (dirent->name[0] != '.') ||
506 (dirent->name[1] != '.') ||
511 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
512 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
513 e2fsck_write_inode_full(ctx, pctx->ino, inode,
514 EXT2_INODE_SIZE(ctx->fs->super),
515 "check_is_really_dir");
519 void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
522 unsigned int threshold;
530 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
532 profile_get_uint(ctx->profile, "scratch_files",
533 "numdirs_threshold", 0, 0, &threshold);
534 profile_get_boolean(ctx->profile, "scratch_files",
535 "icount", 0, 1, &enable);
537 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
539 num_dirs = 1024; /* Guess */
541 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
542 (threshold && num_dirs <= threshold))
545 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
550 static void reserve_block_for_root_repair(e2fsck_t ctx)
554 ext2_filsys fs = ctx->fs;
556 ctx->root_repair_block = 0;
557 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
560 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
563 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
564 ctx->root_repair_block = blk;
567 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
571 ext2_filsys fs = ctx->fs;
572 static const char name[] = "lost+found";
575 ctx->lnf_repair_block = 0;
576 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
579 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
582 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
583 ctx->lnf_repair_block = blk;
586 void e2fsck_pass1(e2fsck_t ctx)
590 ext2_filsys fs = ctx->fs;
592 struct ext2_inode *inode = NULL;
593 ext2_inode_scan scan = NULL;
594 char *block_buf = NULL;
595 #ifdef RESOURCE_TRACK
596 struct resource_track rtrack;
598 unsigned char frag, fsize;
599 struct problem_context pctx;
600 struct scan_callback_struct scan_struct;
601 struct ext2_super_block *sb = ctx->fs->super;
603 unsigned int save_type;
604 int imagic_fs, extent_fs;
605 int low_dtime_check = 1;
608 init_resource_track(&rtrack, ctx->fs->io);
609 clear_problem_context(&pctx);
611 if (!(ctx->options & E2F_OPT_PREEN))
612 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
614 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
615 !(ctx->options & E2F_OPT_NO)) {
616 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
617 ctx->dirs_to_hash = 0;
621 mtrace_print("Pass 1");
624 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
626 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
627 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
628 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
629 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
630 max_sizes = (max_sizes * (1UL << i));
631 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
635 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
636 extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
639 * Allocate bitmaps structures
641 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
642 EXT2FS_BMAP64_RBTREE,
644 &ctx->inode_used_map);
647 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
648 ctx->flags |= E2F_FLAG_ABORT;
651 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
652 _("directory inode map"),
653 EXT2FS_BMAP64_AUTODIR,
654 "inode_dir_map", &ctx->inode_dir_map);
657 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
658 ctx->flags |= E2F_FLAG_ABORT;
661 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
662 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
663 "inode_reg_map", &ctx->inode_reg_map);
666 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
667 ctx->flags |= E2F_FLAG_ABORT;
670 pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
671 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
672 "block_found_map", &ctx->block_found_map);
675 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
676 ctx->flags |= E2F_FLAG_ABORT;
679 e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
680 if (!ctx->inode_link_info) {
681 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
682 "inode_link_info", &save_type);
683 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
684 &ctx->inode_link_info);
685 fs->default_bitmap_type = save_type;
689 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
690 ctx->flags |= E2F_FLAG_ABORT;
693 inode_size = EXT2_INODE_SIZE(fs->super);
694 inode = (struct ext2_inode *)
695 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
697 inodes_to_process = (struct process_inode_block *)
698 e2fsck_allocate_memory(ctx,
699 (ctx->process_inode_size *
700 sizeof(struct process_inode_block)),
701 "array of inodes to process");
702 process_inode_count = 0;
704 pctx.errcode = ext2fs_init_dblist(fs, 0);
706 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
707 ctx->flags |= E2F_FLAG_ABORT;
712 * If the last orphan field is set, clear it, since the pass1
713 * processing will automatically find and clear the orphans.
714 * In the future, we may want to try using the last_orphan
715 * linked list ourselves, but for now, we clear it so that the
716 * ext3 mount code won't get confused.
718 if (!(ctx->options & E2F_OPT_READONLY)) {
719 if (fs->super->s_last_orphan) {
720 fs->super->s_last_orphan = 0;
721 ext2fs_mark_super_dirty(fs);
725 mark_table_blocks(ctx);
726 pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
727 &ctx->block_found_map);
729 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
730 ctx->flags |= E2F_FLAG_ABORT;
733 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
734 "block interate buffer");
735 e2fsck_use_inode_shortcuts(ctx, 1);
736 e2fsck_intercept_block_allocations(ctx);
737 old_op = ehandler_operation(_("opening inode scan"));
738 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
740 ehandler_operation(old_op);
742 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
743 ctx->flags |= E2F_FLAG_ABORT;
746 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
747 ctx->stashed_inode = inode;
748 scan_struct.ctx = ctx;
749 scan_struct.block_buf = block_buf;
750 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
751 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
752 ctx->fs->group_desc_count)))
754 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
755 (fs->super->s_mtime < fs->super->s_inodes_count) ||
756 (fs->super->s_mkfs_time &&
757 fs->super->s_mkfs_time < fs->super->s_inodes_count))
760 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
761 fs->super->s_mmp_block > fs->super->s_first_data_block &&
762 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
763 ext2fs_mark_block_bitmap2(ctx->block_found_map,
764 fs->super->s_mmp_block);
767 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
768 if (e2fsck_mmp_update(fs))
771 old_op = ehandler_operation(_("getting next inode from scan"));
772 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
774 ehandler_operation(old_op);
775 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
777 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
778 if (!ctx->inode_bb_map)
780 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
781 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
785 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
786 ctx->flags |= E2F_FLAG_ABORT;
793 ctx->stashed_ino = ino;
794 if (inode->i_links_count) {
795 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
796 ino, inode->i_links_count);
798 pctx.num = inode->i_links_count;
799 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
800 ctx->flags |= E2F_FLAG_ABORT;
806 * Test for incorrect extent flag settings.
808 * On big-endian machines we must be careful:
809 * When the inode is read, the i_block array is not swapped
810 * if the extent flag is set. Therefore if we are testing
811 * for or fixing a wrongly-set flag, we must potentially
812 * (un)swap before testing, or after fixing.
816 * In this case the extents flag was set when read, so
817 * extent_header_verify is ok. If the inode is cleared,
818 * no need to swap... so no extra swapping here.
820 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
821 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
822 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
823 if ((ext2fs_extent_header_verify(inode->i_block,
824 sizeof(inode->i_block)) == 0) &&
825 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
826 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
827 ext2fs_mark_super_dirty(fs);
829 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
831 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
832 if (ino == EXT2_BAD_INO)
833 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
840 * For big-endian machines:
841 * If the inode didn't have the extents flag set when it
842 * was read, then the i_blocks array was swapped. To test
843 * as an extents header, we must swap it back first.
844 * IF we then set the extents flag, the entire i_block
845 * array must be un/re-swapped to make it proper extents data.
847 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
848 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
849 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
850 (LINUX_S_ISREG(inode->i_mode) ||
851 LINUX_S_ISDIR(inode->i_mode))) {
853 #ifdef WORDS_BIGENDIAN
854 __u32 tmp_block[EXT2_N_BLOCKS];
856 for (i = 0; i < EXT2_N_BLOCKS; i++)
857 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
860 ehp = inode->i_block;
862 if ((ext2fs_extent_header_verify(ehp,
863 sizeof(inode->i_block)) == 0) &&
864 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
865 inode->i_flags |= EXT4_EXTENTS_FL;
866 #ifdef WORDS_BIGENDIAN
867 memcpy(inode->i_block, tmp_block,
868 sizeof(inode->i_block));
870 e2fsck_write_inode(ctx, ino, inode, "pass1");
874 if (ino == EXT2_BAD_INO) {
875 struct process_block_struct pb;
877 if ((inode->i_mode || inode->i_uid || inode->i_gid ||
878 inode->i_links_count || inode->i_file_acl) &&
879 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
880 memset(inode, 0, sizeof(struct ext2_inode));
881 e2fsck_write_inode(ctx, ino, inode,
885 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
889 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
890 ctx->flags |= E2F_FLAG_ABORT;
893 pb.ino = EXT2_BAD_INO;
894 pb.num_blocks = pb.last_block = 0;
895 pb.last_db_block = -1;
896 pb.num_illegal_blocks = 0;
897 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
898 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
902 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
903 block_buf, process_bad_block, &pb);
904 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
906 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
907 ctx->flags |= E2F_FLAG_ABORT;
911 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
912 ctx->flags |= E2F_FLAG_ABORT;
915 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
916 clear_problem_context(&pctx);
918 } else if (ino == EXT2_ROOT_INO) {
920 * Make sure the root inode is a directory; if
921 * not, offer to clear it. It will be
922 * regnerated in pass #3.
924 if (!LINUX_S_ISDIR(inode->i_mode)) {
925 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
929 * If dtime is set, offer to clear it. mke2fs
930 * version 0.2b created filesystems with the
931 * dtime field set for the root and lost+found
932 * directories. We won't worry about
933 * /lost+found, since that can be regenerated
934 * easily. But we will fix the root directory
937 if (inode->i_dtime && inode->i_links_count) {
938 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
940 e2fsck_write_inode(ctx, ino, inode,
944 } else if (ino == EXT2_JOURNAL_INO) {
945 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
946 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
947 if (!LINUX_S_ISREG(inode->i_mode) &&
948 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
950 inode->i_mode = LINUX_S_IFREG;
951 e2fsck_write_inode(ctx, ino, inode,
954 check_blocks(ctx, &pctx, block_buf);
957 if ((inode->i_links_count ||
958 inode->i_blocks || inode->i_block[0]) &&
959 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
961 memset(inode, 0, inode_size);
962 ext2fs_icount_store(ctx->inode_link_info,
964 e2fsck_write_inode_full(ctx, ino, inode,
965 inode_size, "pass1");
967 } else if ((ino == EXT4_USR_QUOTA_INO) ||
968 (ino == EXT4_GRP_QUOTA_INO)) {
969 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
970 if ((fs->super->s_feature_ro_compat &
971 EXT4_FEATURE_RO_COMPAT_QUOTA) &&
972 ((fs->super->s_usr_quota_inum == ino) ||
973 (fs->super->s_grp_quota_inum == ino))) {
974 if (!LINUX_S_ISREG(inode->i_mode) &&
975 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
977 inode->i_mode = LINUX_S_IFREG;
978 e2fsck_write_inode(ctx, ino, inode,
981 check_blocks(ctx, &pctx, block_buf);
984 if ((inode->i_links_count ||
985 inode->i_blocks || inode->i_block[0]) &&
986 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
988 memset(inode, 0, inode_size);
989 ext2fs_icount_store(ctx->inode_link_info,
991 e2fsck_write_inode_full(ctx, ino, inode,
992 inode_size, "pass1");
994 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
995 problem_t problem = 0;
997 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
998 if (ino == EXT2_BOOT_LOADER_INO) {
999 if (LINUX_S_ISDIR(inode->i_mode))
1000 problem = PR_1_RESERVED_BAD_MODE;
1001 } else if (ino == EXT2_RESIZE_INO) {
1002 if (inode->i_mode &&
1003 !LINUX_S_ISREG(inode->i_mode))
1004 problem = PR_1_RESERVED_BAD_MODE;
1006 if (inode->i_mode != 0)
1007 problem = PR_1_RESERVED_BAD_MODE;
1010 if (fix_problem(ctx, problem, &pctx)) {
1012 e2fsck_write_inode(ctx, ino, inode,
1016 check_blocks(ctx, &pctx, block_buf);
1021 * Check for inodes who might have been part of the
1022 * orphaned list linked list. They should have gotten
1023 * dealt with by now, unless the list had somehow been
1026 * FIXME: In the future, inodes which are still in use
1027 * (and which are therefore) pending truncation should
1028 * be handled specially. Right now we just clear the
1029 * dtime field, and the normal e2fsck handling of
1030 * inodes where i_size and the inode blocks are
1031 * inconsistent is to fix i_size, instead of releasing
1032 * the extra blocks. This won't catch the inodes that
1033 * was at the end of the orphan list, but it's better
1034 * than nothing. The right answer is that there
1035 * shouldn't be any bugs in the orphan list handling. :-)
1037 if (inode->i_dtime && low_dtime_check &&
1038 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1039 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1040 inode->i_dtime = inode->i_links_count ?
1042 e2fsck_write_inode(ctx, ino, inode,
1048 * This code assumes that deleted inodes have
1049 * i_links_count set to 0.
1051 if (!inode->i_links_count) {
1052 if (!inode->i_dtime && inode->i_mode) {
1053 if (fix_problem(ctx,
1054 PR_1_ZERO_DTIME, &pctx)) {
1055 inode->i_dtime = ctx->now;
1056 e2fsck_write_inode(ctx, ino, inode,
1063 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1064 * deleted files. Oops.
1066 * Since all new ext2 implementations get this right,
1067 * we now assume that the case of non-zero
1068 * i_links_count and non-zero dtime means that we
1069 * should keep the file, not delete it.
1072 if (inode->i_dtime) {
1073 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1075 e2fsck_write_inode(ctx, ino, inode, "pass1");
1079 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1080 switch (fs->super->s_creator_os) {
1082 frag = inode->osd2.hurd2.h_i_frag;
1083 fsize = inode->osd2.hurd2.h_i_fsize;
1089 if (inode->i_faddr || frag || fsize ||
1090 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
1091 mark_inode_bad(ctx, ino);
1092 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1093 !(fs->super->s_feature_incompat &
1094 EXT4_FEATURE_INCOMPAT_64BIT) &&
1095 inode->osd2.linux2.l_i_file_acl_high != 0)
1096 mark_inode_bad(ctx, ino);
1097 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1098 !(fs->super->s_feature_ro_compat &
1099 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1100 (inode->osd2.linux2.l_i_blocks_hi != 0))
1101 mark_inode_bad(ctx, ino);
1102 if (inode->i_flags & EXT2_IMAGIC_FL) {
1104 if (!ctx->inode_imagic_map)
1105 alloc_imagic_map(ctx);
1106 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1109 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1110 inode->i_flags &= ~EXT2_IMAGIC_FL;
1111 e2fsck_write_inode(ctx, ino,
1117 check_inode_extra_space(ctx, &pctx);
1118 check_is_really_dir(ctx, &pctx, block_buf);
1121 * ext2fs_inode_has_valid_blocks2 does not actually look
1122 * at i_block[] values, so not endian-sensitive here.
1124 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1125 LINUX_S_ISLNK(inode->i_mode) &&
1126 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1127 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1128 inode->i_flags &= ~EXT4_EXTENTS_FL;
1129 e2fsck_write_inode(ctx, ino, inode, "pass1");
1132 if (LINUX_S_ISDIR(inode->i_mode)) {
1133 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1134 e2fsck_add_dir_info(ctx, ino, 0);
1135 ctx->fs_directory_count++;
1136 } else if (LINUX_S_ISREG (inode->i_mode)) {
1137 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1138 ctx->fs_regular_count++;
1139 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1140 e2fsck_pass1_check_device_inode(fs, inode)) {
1141 check_immutable(ctx, &pctx);
1142 check_size(ctx, &pctx);
1143 ctx->fs_chardev_count++;
1144 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1145 e2fsck_pass1_check_device_inode(fs, inode)) {
1146 check_immutable(ctx, &pctx);
1147 check_size(ctx, &pctx);
1148 ctx->fs_blockdev_count++;
1149 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1150 e2fsck_pass1_check_symlink(fs, ino, inode,
1152 check_immutable(ctx, &pctx);
1153 ctx->fs_symlinks_count++;
1154 if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1155 ctx->fs_fast_symlinks_count++;
1156 check_blocks(ctx, &pctx, block_buf);
1160 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1161 e2fsck_pass1_check_device_inode(fs, inode)) {
1162 check_immutable(ctx, &pctx);
1163 check_size(ctx, &pctx);
1164 ctx->fs_fifo_count++;
1165 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1166 e2fsck_pass1_check_device_inode(fs, inode)) {
1167 check_immutable(ctx, &pctx);
1168 check_size(ctx, &pctx);
1169 ctx->fs_sockets_count++;
1171 mark_inode_bad(ctx, ino);
1172 if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
1173 if (inode->i_block[EXT2_IND_BLOCK])
1174 ctx->fs_ind_count++;
1175 if (inode->i_block[EXT2_DIND_BLOCK])
1176 ctx->fs_dind_count++;
1177 if (inode->i_block[EXT2_TIND_BLOCK])
1178 ctx->fs_tind_count++;
1180 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1181 (inode->i_block[EXT2_IND_BLOCK] ||
1182 inode->i_block[EXT2_DIND_BLOCK] ||
1183 inode->i_block[EXT2_TIND_BLOCK] ||
1184 ext2fs_file_acl_block(fs, inode))) {
1185 inodes_to_process[process_inode_count].ino = ino;
1186 inodes_to_process[process_inode_count].inode = *inode;
1187 process_inode_count++;
1189 check_blocks(ctx, &pctx, block_buf);
1191 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1194 if (process_inode_count >= ctx->process_inode_size) {
1195 process_inodes(ctx, block_buf);
1197 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1201 process_inodes(ctx, block_buf);
1202 ext2fs_close_inode_scan(scan);
1205 reserve_block_for_root_repair(ctx);
1206 reserve_block_for_lnf_repair(ctx);
1209 * If any extended attribute blocks' reference counts need to
1210 * be adjusted, either up (ctx->refcount_extra), or down
1211 * (ctx->refcount), then fix them.
1213 if (ctx->refcount) {
1214 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1215 ea_refcount_free(ctx->refcount);
1218 if (ctx->refcount_extra) {
1219 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1221 ea_refcount_free(ctx->refcount_extra);
1222 ctx->refcount_extra = 0;
1225 if (ctx->invalid_bitmaps)
1226 handle_fs_bad_blocks(ctx);
1228 /* We don't need the block_ea_map any more */
1229 if (ctx->block_ea_map) {
1230 ext2fs_free_block_bitmap(ctx->block_ea_map);
1231 ctx->block_ea_map = 0;
1234 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1235 clear_problem_context(&pctx);
1236 pctx.errcode = ext2fs_create_resize_inode(fs);
1238 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1240 ctx->flags |= E2F_FLAG_ABORT;
1245 if (!pctx.errcode) {
1246 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1248 inode->i_mtime = ctx->now;
1249 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1252 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1255 if (ctx->flags & E2F_FLAG_RESTART) {
1257 * Only the master copy of the superblock and block
1258 * group descriptors are going to be written during a
1259 * restart, so set the superblock to be used to be the
1260 * master superblock.
1262 ctx->use_superblock = 0;
1267 if (ctx->block_dup_map) {
1268 if (ctx->options & E2F_OPT_PREEN) {
1269 clear_problem_context(&pctx);
1270 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1272 e2fsck_pass1_dupblocks(ctx, block_buf);
1274 ext2fs_free_mem(&inodes_to_process);
1276 e2fsck_use_inode_shortcuts(ctx, 0);
1279 ext2fs_close_inode_scan(scan);
1281 ext2fs_free_mem(&block_buf);
1283 ext2fs_free_mem(&inode);
1285 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
1286 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1290 * When the inode_scan routines call this callback at the end of the
1291 * glock group, call process_inodes.
1293 static errcode_t scan_callback(ext2_filsys fs,
1294 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1295 dgrp_t group, void * priv_data)
1297 struct scan_callback_struct *scan_struct;
1300 scan_struct = (struct scan_callback_struct *) priv_data;
1301 ctx = scan_struct->ctx;
1303 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1306 if ((ctx->progress)(ctx, 1, group+1,
1307 ctx->fs->group_desc_count))
1308 return EXT2_ET_CANCEL_REQUESTED;
1314 * Process the inodes in the "inodes to process" list.
1316 static void process_inodes(e2fsck_t ctx, char *block_buf)
1319 struct ext2_inode *old_stashed_inode;
1320 ext2_ino_t old_stashed_ino;
1321 const char *old_operation;
1323 struct problem_context pctx;
1326 printf("begin process_inodes: ");
1328 if (process_inode_count == 0)
1330 old_operation = ehandler_operation(0);
1331 old_stashed_inode = ctx->stashed_inode;
1332 old_stashed_ino = ctx->stashed_ino;
1333 qsort(inodes_to_process, process_inode_count,
1334 sizeof(struct process_inode_block), process_inode_cmp);
1335 clear_problem_context(&pctx);
1336 for (i=0; i < process_inode_count; i++) {
1337 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1338 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1341 printf("%u ", pctx.ino);
1343 sprintf(buf, _("reading indirect blocks of inode %u"),
1345 ehandler_operation(buf);
1346 check_blocks(ctx, &pctx, block_buf);
1347 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1350 ctx->stashed_inode = old_stashed_inode;
1351 ctx->stashed_ino = old_stashed_ino;
1352 process_inode_count = 0;
1354 printf("end process inodes\n");
1356 ehandler_operation(old_operation);
1359 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1361 const struct process_inode_block *ib_a =
1362 (const struct process_inode_block *) a;
1363 const struct process_inode_block *ib_b =
1364 (const struct process_inode_block *) b;
1367 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1368 ib_b->inode.i_block[EXT2_IND_BLOCK]);
1371 * We only call process_inodes() for non-extent
1372 * inodes, so it's OK to pass NULL to
1373 * ext2fs_file_acl_block() here.
1375 ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
1376 ext2fs_file_acl_block(0, &(ib_b->inode));
1378 ret = ib_a->ino - ib_b->ino;
1383 * Mark an inode as being bad in some what
1385 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
1387 struct problem_context pctx;
1389 if (!ctx->inode_bad_map) {
1390 clear_problem_context(&pctx);
1392 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1393 _("bad inode map"), EXT2FS_BMAP64_RBTREE,
1394 "inode_bad_map", &ctx->inode_bad_map);
1397 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1398 /* Should never get here */
1399 ctx->flags |= E2F_FLAG_ABORT;
1403 ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
1408 * This procedure will allocate the inode "bb" (badblock) map table
1410 static void alloc_bb_map(e2fsck_t ctx)
1412 struct problem_context pctx;
1414 clear_problem_context(&pctx);
1415 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1416 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
1417 "inode_bb_map", &ctx->inode_bb_map);
1420 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1421 /* Should never get here */
1422 ctx->flags |= E2F_FLAG_ABORT;
1428 * This procedure will allocate the inode imagic table
1430 static void alloc_imagic_map(e2fsck_t ctx)
1432 struct problem_context pctx;
1434 clear_problem_context(&pctx);
1435 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1436 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
1437 "inode_imagic_map", &ctx->inode_imagic_map);
1440 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1441 /* Should never get here */
1442 ctx->flags |= E2F_FLAG_ABORT;
1448 * Marks a block as in use, setting the dup_map if it's been set
1449 * already. Called by process_block and process_bad_block.
1451 * WARNING: Assumes checks have already been done to make sure block
1452 * is valid. This is true in both process_block and process_bad_block.
1454 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
1456 struct problem_context pctx;
1458 clear_problem_context(&pctx);
1460 if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
1461 if (!ctx->block_dup_map) {
1462 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1463 _("multiply claimed block map"),
1464 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1465 &ctx->block_dup_map);
1468 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1470 /* Should never get here */
1471 ctx->flags |= E2F_FLAG_ABORT;
1475 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
1477 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
1481 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
1484 if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
1485 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
1488 mark_block_used(ctx, block++);
1492 * Adjust the extended attribute block's reference counts at the end
1493 * of pass 1, either by subtracting out references for EA blocks that
1494 * are still referenced in ctx->refcount, or by adding references for
1495 * EA blocks that had extra references as accounted for in
1496 * ctx->refcount_extra.
1498 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1499 char *block_buf, int adjust_sign)
1501 struct ext2_ext_attr_header *header;
1502 struct problem_context pctx;
1503 ext2_filsys fs = ctx->fs;
1508 clear_problem_context(&pctx);
1510 ea_refcount_intr_begin(refcount);
1512 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1515 pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1517 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1520 header = (struct ext2_ext_attr_header *) block_buf;
1521 pctx.blkcount = header->h_refcount;
1522 should_be = header->h_refcount + adjust_sign * count;
1523 pctx.num = should_be;
1524 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1525 header->h_refcount = should_be;
1526 pctx.errcode = ext2fs_write_ext_attr2(fs, blk,
1529 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
1538 * Handle processing the extended attribute blocks
1540 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1543 ext2_filsys fs = ctx->fs;
1544 ext2_ino_t ino = pctx->ino;
1545 struct ext2_inode *inode = pctx->inode;
1548 struct ext2_ext_attr_header *header;
1549 struct ext2_ext_attr_entry *entry;
1551 region_t region = 0;
1553 blk = ext2fs_file_acl_block(fs, inode);
1558 * If the Extended attribute flag isn't set, then a non-zero
1559 * file acl means that the inode is corrupted.
1561 * Or if the extended attribute block is an invalid block,
1562 * then the inode is also corrupted.
1564 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1565 (blk < fs->super->s_first_data_block) ||
1566 (blk >= ext2fs_blocks_count(fs->super))) {
1567 mark_inode_bad(ctx, ino);
1571 /* If ea bitmap hasn't been allocated, create it */
1572 if (!ctx->block_ea_map) {
1573 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
1574 _("ext attr block map"),
1575 EXT2FS_BMAP64_RBTREE, "block_ea_map",
1576 &ctx->block_ea_map);
1577 if (pctx->errcode) {
1579 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1580 ctx->flags |= E2F_FLAG_ABORT;
1585 /* Create the EA refcount structure if necessary */
1586 if (!ctx->refcount) {
1587 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1588 if (pctx->errcode) {
1590 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1591 ctx->flags |= E2F_FLAG_ABORT;
1597 /* Debugging text */
1598 printf("Inode %u has EA block %u\n", ino, blk);
1601 /* Have we seen this EA block before? */
1602 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
1603 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1605 /* Ooops, this EA was referenced more than it stated */
1606 if (!ctx->refcount_extra) {
1607 pctx->errcode = ea_refcount_create(0,
1608 &ctx->refcount_extra);
1609 if (pctx->errcode) {
1611 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1612 ctx->flags |= E2F_FLAG_ABORT;
1616 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1621 * OK, we haven't seen this EA block yet. So we need to
1625 pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1626 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1628 header = (struct ext2_ext_attr_header *) block_buf;
1629 pctx->blk = ext2fs_file_acl_block(fs, inode);
1630 if (((ctx->ext_attr_ver == 1) &&
1631 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1632 ((ctx->ext_attr_ver == 2) &&
1633 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1634 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1638 if (header->h_blocks != 1) {
1639 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1643 region = region_create(0, fs->blocksize);
1645 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
1646 ctx->flags |= E2F_FLAG_ABORT;
1649 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1650 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1654 entry = (struct ext2_ext_attr_entry *)(header+1);
1655 end = block_buf + fs->blocksize;
1656 while ((char *)entry < end && *(__u32 *)entry) {
1659 if (region_allocate(region, (char *)entry - (char *)header,
1660 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1661 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1665 if ((ctx->ext_attr_ver == 1 &&
1666 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1667 (ctx->ext_attr_ver == 2 &&
1668 entry->e_name_index == 0)) {
1669 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1673 if (entry->e_value_block != 0) {
1674 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1677 if (entry->e_value_offs + entry->e_value_size > fs->blocksize) {
1678 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1682 if (entry->e_value_size &&
1683 region_allocate(region, entry->e_value_offs,
1684 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1685 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1689 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
1690 entry->e_value_offs);
1692 if (entry->e_hash != hash) {
1693 pctx->num = entry->e_hash;
1694 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
1696 entry->e_hash = hash;
1699 entry = EXT2_EXT_ATTR_NEXT(entry);
1701 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1702 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1705 region_free(region);
1707 count = header->h_refcount - 1;
1709 ea_refcount_store(ctx->refcount, blk, count);
1710 mark_block_used(ctx, blk);
1711 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
1716 region_free(region);
1717 ext2fs_file_acl_block_set(fs, inode, 0);
1718 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1722 /* Returns 1 if bad htree, 0 if OK */
1723 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1724 ext2_ino_t ino, struct ext2_inode *inode,
1727 struct ext2_dx_root_info *root;
1728 ext2_filsys fs = ctx->fs;
1732 if ((!LINUX_S_ISDIR(inode->i_mode) &&
1733 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1734 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1735 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
1738 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
1740 if ((pctx->errcode) ||
1742 (blk < fs->super->s_first_data_block) ||
1743 (blk >= ext2fs_blocks_count(fs->super))) {
1744 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1750 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
1751 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1754 /* XXX should check that beginning matches a directory */
1755 root = (struct ext2_dx_root_info *) (block_buf + 24);
1757 if ((root->reserved_zero || root->info_length < 8) &&
1758 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1761 pctx->num = root->hash_version;
1762 if ((root->hash_version != EXT2_HASH_LEGACY) &&
1763 (root->hash_version != EXT2_HASH_HALF_MD4) &&
1764 (root->hash_version != EXT2_HASH_TEA) &&
1765 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
1768 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
1769 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
1772 pctx->num = root->indirect_levels;
1773 if ((root->indirect_levels > 1) &&
1774 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
1780 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
1781 struct ext2_inode *inode, int restart_flag,
1785 inode->i_links_count = 0;
1786 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1787 inode->i_dtime = ctx->now;
1789 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
1790 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
1791 if (ctx->inode_reg_map)
1792 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
1793 if (ctx->inode_bad_map)
1794 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
1797 * If the inode was partially accounted for before processing
1798 * was aborted, we need to restart the pass 1 scan.
1800 ctx->flags |= restart_flag;
1802 if (ino == EXT2_BAD_INO)
1803 memset(inode, 0, sizeof(struct ext2_inode));
1805 e2fsck_write_inode(ctx, ino, inode, source);
1809 * Use the multiple-blocks reclamation code to fix alignment problems in
1810 * a bigalloc filesystem. We want a logical cluster to map to *only* one
1811 * physical cluster, and we want the block offsets within that cluster to
1814 static int has_unaligned_cluster_map(e2fsck_t ctx,
1815 blk64_t last_pblk, e2_blkcnt_t last_lblk,
1816 blk64_t pblk, blk64_t lblk)
1818 blk64_t cluster_mask;
1820 if (!ctx->fs->cluster_ratio_bits)
1822 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
1825 * If the block in the logical cluster doesn't align with the block in
1826 * the physical cluster...
1828 if ((lblk & cluster_mask) != (pblk & cluster_mask))
1832 * If we cross a physical cluster boundary within a logical cluster...
1834 if (last_pblk && (lblk & cluster_mask) != 0 &&
1835 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
1836 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
1842 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
1843 struct process_block_struct *pb,
1844 blk64_t start_block, blk64_t end_block,
1846 ext2_extent_handle_t ehandle)
1848 struct ext2fs_extent extent;
1849 blk64_t blk, last_lblk;
1850 e2_blkcnt_t blockcnt;
1852 int is_dir, is_leaf;
1854 struct ext2_extent_info info;
1856 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
1860 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
1862 while (!pctx->errcode && info.num_entries-- > 0) {
1863 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
1864 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
1865 last_lblk = extent.e_lblk + extent.e_len - 1;
1868 if (extent.e_pblk == 0 ||
1869 extent.e_pblk < ctx->fs->super->s_first_data_block ||
1870 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
1871 problem = PR_1_EXTENT_BAD_START_BLK;
1872 else if (extent.e_lblk < start_block)
1873 problem = PR_1_OUT_OF_ORDER_EXTENTS;
1874 else if ((end_block && last_lblk > end_block) &&
1875 (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
1876 last_lblk > eof_block)))
1877 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
1878 else if (is_leaf && extent.e_len == 0)
1879 problem = PR_1_EXTENT_LENGTH_ZERO;
1881 (extent.e_pblk + extent.e_len) >
1882 ext2fs_blocks_count(ctx->fs->super))
1883 problem = PR_1_EXTENT_ENDS_BEYOND;
1884 else if (is_leaf && is_dir &&
1885 ((extent.e_lblk + extent.e_len) >
1886 (1 << (21 - ctx->fs->super->s_log_block_size))))
1887 problem = PR_1_TOOBIG_DIR;
1890 * Uninitialized blocks in a directory? Clear the flag and
1891 * we'll interpret the blocks later.
1893 if (is_dir && problem == 0 &&
1894 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
1895 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
1896 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
1897 pb->inode_modified = 1;
1898 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
1906 pctx->blk = extent.e_pblk;
1907 pctx->blk2 = extent.e_lblk;
1908 pctx->num = extent.e_len;
1909 pctx->blkcount = extent.e_lblk + extent.e_len;
1910 if (fix_problem(ctx, problem, pctx)) {
1911 if (ctx->invalid_bitmaps) {
1913 * If fsck knows the bitmaps are bad,
1914 * skip to the next extent and
1915 * try to clear this extent again
1916 * after fixing the bitmaps, by
1919 pctx->errcode = ext2fs_extent_get(
1921 EXT2_EXTENT_NEXT_SIB,
1923 ctx->flags |= E2F_FLAG_RESTART_LATER;
1924 if (pctx->errcode ==
1925 EXT2_ET_NO_CURRENT_NODE) {
1931 e2fsck_read_bitmaps(ctx);
1932 pb->inode_modified = 1;
1934 ext2fs_extent_delete(ehandle, 0);
1935 if (pctx->errcode) {
1936 pctx->str = "ext2fs_extent_delete";
1939 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
1940 if (pctx->errcode &&
1941 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
1942 pctx->str = "ext2fs_extent_fix_parents";
1945 pctx->errcode = ext2fs_extent_get(ehandle,
1946 EXT2_EXTENT_CURRENT,
1948 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
1958 blk64_t lblk = extent.e_lblk;
1960 blk = extent.e_pblk;
1961 pctx->errcode = ext2fs_extent_get(ehandle,
1962 EXT2_EXTENT_DOWN, &extent);
1963 if (pctx->errcode) {
1964 pctx->str = "EXT2_EXTENT_DOWN";
1965 problem = PR_1_EXTENT_HEADER_INVALID;
1966 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
1967 goto report_problem;
1970 /* The next extent should match this index's logical start */
1971 if (extent.e_lblk != lblk) {
1972 struct ext2_extent_info e_info;
1974 ext2fs_extent_get_info(ehandle, &e_info);
1976 pctx->blk2 = extent.e_lblk;
1977 pctx->num = e_info.curr_level - 1;
1978 problem = PR_1_EXTENT_INDEX_START_INVALID;
1979 if (fix_problem(ctx, problem, pctx)) {
1980 pb->inode_modified = 1;
1982 ext2fs_extent_fix_parents(ehandle);
1983 if (pctx->errcode) {
1984 pctx->str = "ext2fs_extent_fix_parents";
1989 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
1990 last_lblk, eof_block, ehandle);
1993 pctx->errcode = ext2fs_extent_get(ehandle,
1994 EXT2_EXTENT_UP, &extent);
1995 if (pctx->errcode) {
1996 pctx->str = "EXT2_EXTENT_UP";
1999 mark_block_used(ctx, blk);
2004 if ((pb->previous_block != 0) &&
2005 (pb->previous_block+1 != extent.e_pblk)) {
2006 if (ctx->options & E2F_OPT_FRAGCHECK) {
2011 else if (pb->is_reg)
2014 printf(("%6lu(%c): expecting %6lu "
2016 "phys %6lu log %lu len %lu\n"),
2017 (unsigned long) pctx->ino, type,
2018 (unsigned long) pb->previous_block+1,
2019 (unsigned long) extent.e_pblk,
2020 (unsigned long) extent.e_lblk,
2021 (unsigned long) extent.e_len);
2026 * If we notice a gap in the logical block mappings of an
2027 * extent-mapped directory, offer to close the hole by
2028 * moving the logical block down, otherwise we'll go mad in
2029 * pass 3 allocating empty directory blocks to fill the hole.
2032 pb->last_block + 1 < (e2_blkcnt_t)extent.e_lblk) {
2035 new_lblk = pb->last_block + 1;
2036 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
2037 new_lblk = ((new_lblk +
2038 EXT2FS_CLUSTER_RATIO(ctx->fs)) &
2039 EXT2FS_CLUSTER_MASK(ctx->fs)) |
2041 EXT2FS_CLUSTER_MASK(ctx->fs));
2042 pctx->blk = extent.e_lblk;
2043 pctx->blk2 = new_lblk;
2044 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
2045 extent.e_lblk = new_lblk;
2046 pb->inode_modified = 1;
2047 pctx->errcode = ext2fs_extent_replace(ehandle,
2049 if (pctx->errcode) {
2053 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2055 goto failed_add_dir_block;
2056 pctx->errcode = ext2fs_extent_goto(ehandle,
2059 goto failed_add_dir_block;
2060 last_lblk = extent.e_lblk + extent.e_len - 1;
2064 while (is_dir && (++pb->last_db_block <
2065 (e2_blkcnt_t) extent.e_lblk)) {
2066 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
2069 if (pctx->errcode) {
2071 pctx->num = pb->last_db_block;
2072 goto failed_add_dir_block;
2075 if (!ctx->fs->cluster_ratio_bits) {
2076 mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
2077 pb->num_blocks += extent.e_len;
2079 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
2081 blk++, blockcnt++, i++) {
2082 if (ctx->fs->cluster_ratio_bits &&
2083 !(pb->previous_block &&
2084 (EXT2FS_B2C(ctx->fs, blk) ==
2085 EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
2086 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2087 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2088 mark_block_used(ctx, blk);
2091 if (has_unaligned_cluster_map(ctx, pb->previous_block,
2092 pb->last_block, blk,
2094 pctx->blk = blockcnt;
2096 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2097 mark_block_used(ctx, blk);
2098 mark_block_used(ctx, blk);
2100 pb->last_block = blockcnt;
2101 pb->previous_block = blk;
2104 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
2105 if (pctx->errcode) {
2107 pctx->num = blockcnt;
2108 failed_add_dir_block:
2109 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2110 /* Should never get here */
2111 ctx->flags |= E2F_FLAG_ABORT;
2116 if (is_dir && extent.e_len > 0)
2117 pb->last_db_block = blockcnt - 1;
2118 pb->previous_block = extent.e_pblk + extent.e_len - 1;
2119 start_block = pb->last_block = last_lblk;
2120 if (is_leaf && !is_dir &&
2121 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
2122 pb->last_init_lblock = last_lblk;
2124 pctx->errcode = ext2fs_extent_get(ehandle,
2125 EXT2_EXTENT_NEXT_SIB,
2128 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
2132 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
2133 struct process_block_struct *pb)
2135 struct ext2_extent_info info;
2136 struct ext2_inode *inode = pctx->inode;
2137 ext2_extent_handle_t ehandle;
2138 ext2_filsys fs = ctx->fs;
2139 ext2_ino_t ino = pctx->ino;
2143 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
2144 if (pctx->errcode) {
2145 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
2146 e2fsck_clear_inode(ctx, ino, inode, 0,
2147 "check_blocks_extents");
2152 retval = ext2fs_extent_get_info(ehandle, &info);
2154 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
2155 info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
2156 ctx->extent_depth_count[info.max_depth]++;
2159 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
2160 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
2161 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle);
2162 if (pctx->errcode &&
2163 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
2165 inode->i_blocks = 0;
2166 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2167 "check_blocks_extents");
2170 ext2fs_extent_free(ehandle);
2174 * This subroutine is called on each inode to account for all of the
2175 * blocks used by that inode.
2177 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
2180 ext2_filsys fs = ctx->fs;
2181 struct process_block_struct pb;
2182 ext2_ino_t ino = pctx->ino;
2183 struct ext2_inode *inode = pctx->inode;
2184 unsigned bad_size = 0;
2185 int dirty_inode = 0;
2192 pb.last_init_lblock = -1;
2193 pb.last_db_block = -1;
2194 pb.num_illegal_blocks = 0;
2195 pb.suppress = 0; pb.clear = 0;
2198 pb.previous_block = 0;
2199 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2200 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
2201 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
2205 pb.inode_modified = 0;
2209 extent_fs = (ctx->fs->super->s_feature_incompat &
2210 EXT3_FEATURE_INCOMPAT_EXTENTS);
2212 if (inode->i_flags & EXT2_COMPRBLK_FL) {
2213 if (fs->super->s_feature_incompat &
2214 EXT2_FEATURE_INCOMPAT_COMPRESSION)
2217 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
2218 inode->i_flags &= ~EXT2_COMPRBLK_FL;
2224 if (ext2fs_file_acl_block(fs, inode) &&
2225 check_ext_attr(ctx, pctx, block_buf)) {
2226 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2231 if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
2232 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
2233 check_blocks_extents(ctx, pctx, &pb);
2236 * If we've modified the inode, write it out before
2237 * iterate() tries to use it.
2240 e2fsck_write_inode(ctx, ino, inode,
2244 pctx->errcode = ext2fs_block_iterate3(fs, ino,
2245 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
2246 block_buf, process_block, &pb);
2248 * We do not have uninitialized extents in non extent
2251 pb.last_init_lblock = pb.last_block;
2253 * If iterate() changed a block mapping, we have to
2254 * re-read the inode. If we decide to clear the
2255 * inode after clearing some stuff, we'll re-write the
2256 * bad mappings into the inode!
2258 if (pb.inode_modified)
2259 e2fsck_read_inode(ctx, ino, inode,
2263 end_problem_latch(ctx, PR_LATCH_BLOCK);
2264 end_problem_latch(ctx, PR_LATCH_TOOBIG);
2265 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2268 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
2270 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
2271 if (LINUX_S_ISDIR(inode->i_mode))
2272 ctx->fs_fragmented_dir++;
2274 ctx->fs_fragmented++;
2278 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2283 if (inode->i_flags & EXT2_INDEX_FL) {
2284 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
2285 inode->i_flags &= ~EXT2_INDEX_FL;
2289 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
2294 if (!pb.num_blocks && pb.is_dir) {
2295 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
2296 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
2297 ctx->fs_directory_count--;
2302 if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
2303 quota_data_add(ctx->qctx, inode, ino,
2304 pb.num_blocks * fs->blocksize);
2305 quota_data_inodes(ctx->qctx, inode, ino, +1);
2308 if (!(fs->super->s_feature_ro_compat &
2309 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
2310 !(inode->i_flags & EXT4_HUGE_FILE_FL))
2311 pb.num_blocks *= (fs->blocksize / 512);
2312 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
2314 printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
2315 ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
2319 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
2320 if (inode->i_size & (fs->blocksize - 1))
2322 else if (nblock > (pb.last_block + 1))
2324 else if (nblock < (pb.last_block + 1)) {
2325 if (((pb.last_block + 1) - nblock) >
2326 fs->super->s_prealloc_dir_blocks)
2330 e2_blkcnt_t blkpg = ctx->blocks_per_page;
2332 size = EXT2_I_SIZE(inode);
2333 if ((pb.last_init_lblock >= 0) &&
2334 /* allow allocated blocks to end of PAGE_SIZE */
2335 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
2336 (pb.last_init_lblock / blkpg * blkpg != pb.last_init_lblock ||
2337 size < (__u64)(pb.last_init_lblock & ~(blkpg-1)) *
2340 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2341 size > ext2_max_sizes[fs->super->s_log_block_size])
2342 /* too big for a direct/indirect-mapped file */
2344 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2346 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
2347 /* too big for an extent-based file - 32bit ee_block */
2350 /* i_size for symlinks is checked elsewhere */
2351 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
2352 pctx->num = (pb.last_block+1) * fs->blocksize;
2353 pctx->group = bad_size;
2354 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
2355 if (LINUX_S_ISDIR(inode->i_mode))
2356 pctx->num &= 0xFFFFFFFFULL;
2357 ext2fs_inode_size_set(fs, inode, pctx->num);
2362 if (LINUX_S_ISREG(inode->i_mode) &&
2363 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
2365 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
2366 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
2367 ((fs->super->s_feature_ro_compat &
2368 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
2369 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
2370 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
2371 pctx->num = pb.num_blocks;
2372 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
2373 inode->i_blocks = pb.num_blocks;
2374 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
2380 if (ctx->dirs_to_hash && pb.is_dir &&
2381 !(inode->i_flags & EXT2_INDEX_FL) &&
2382 ((inode->i_size / fs->blocksize) >= 3))
2383 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
2387 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
2392 * Helper function called by process block when an illegal block is
2393 * found. It returns a description about why the block is illegal
2395 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
2399 static char problem[80];
2401 super = fs->super->s_first_data_block;
2402 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
2403 if (block < super) {
2404 sprintf(problem, "< FIRSTBLOCK (%u)", super);
2406 } else if (block >= ext2fs_blocks_count(fs->super)) {
2407 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
2410 for (i = 0; i < fs->group_desc_count; i++) {
2411 if (block == super) {
2412 sprintf(problem, "is the superblock in group %d", i);
2415 if (block > super &&
2416 block <= (super + fs->desc_blocks)) {
2417 sprintf(problem, "is in the group descriptors "
2421 if (block == ext2fs_block_bitmap_loc(fs, i)) {
2422 sprintf(problem, "is the block bitmap of group %d", i);
2425 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
2426 sprintf(problem, "is the inode bitmap of group %d", i);
2429 if (block >= ext2fs_inode_table_loc(fs, i) &&
2430 (block < ext2fs_inode_table_loc(fs, i)
2431 + fs->inode_blocks_per_group)) {
2432 sprintf(problem, "is in the inode table of group %d",
2436 super += fs->super->s_blocks_per_group;
2443 * This is a helper function for check_blocks().
2445 static int process_block(ext2_filsys fs,
2447 e2_blkcnt_t blockcnt,
2448 blk64_t ref_block EXT2FS_ATTR((unused)),
2449 int ref_offset EXT2FS_ATTR((unused)),
2452 struct process_block_struct *p;
2453 struct problem_context *pctx;
2454 blk64_t blk = *block_nr;
2456 problem_t problem = 0;
2459 p = (struct process_block_struct *) priv_data;
2463 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
2464 /* todo: Check that the comprblk_fl is high, that the
2465 blkaddr pattern looks right (all non-holes up to
2466 first EXT2FS_COMPRESSED_BLKADDR, then all
2467 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2468 that the feature_incompat bit is high, and that the
2469 inode is a regular file. If we're doing a "full
2470 check" (a concept introduced to e2fsck by e2compr,
2471 meaning that we look at data blocks as well as
2472 metadata) then call some library routine that
2473 checks the compressed data. I'll have to think
2474 about this, because one particularly important
2475 problem to be able to fix is to recalculate the
2476 cluster size if necessary. I think that perhaps
2477 we'd better do most/all e2compr-specific checks
2478 separately, after the non-e2compr checks. If not
2479 doing a full check, it may be useful to test that
2480 the personality is linux; e.g. if it isn't then
2481 perhaps this really is just an illegal block. */
2486 * For a directory, add logical block zero for processing even if it's
2487 * not mapped or we'll be perennially stuck with broken "." and ".."
2490 if (p->is_dir && blockcnt == 0 && blk == 0) {
2491 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
2492 if (pctx->errcode) {
2494 pctx->num = blockcnt;
2495 goto failed_add_dir_block;
2504 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
2509 * Simplistic fragmentation check. We merely require that the
2510 * file be contiguous. (Which can never be true for really
2511 * big files that are greater than a block group.)
2513 if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
2514 if (p->previous_block+1 != blk) {
2515 if (ctx->options & E2F_OPT_FRAGCHECK) {
2523 printf(_("%6lu(%c): expecting %6lu "
2524 "got phys %6lu (blkcnt %lld)\n"),
2525 (unsigned long) pctx->ino, type,
2526 (unsigned long) p->previous_block+1,
2527 (unsigned long) blk,
2534 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
2535 problem = PR_1_TOOBIG_DIR;
2536 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
2537 problem = PR_1_TOOBIG_REG;
2538 if (!p->is_dir && !p->is_reg && blockcnt > 0)
2539 problem = PR_1_TOOBIG_SYMLINK;
2541 if (blk < fs->super->s_first_data_block ||
2542 blk >= ext2fs_blocks_count(fs->super))
2543 problem = PR_1_ILLEGAL_BLOCK_NUM;
2546 p->num_illegal_blocks++;
2547 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
2548 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
2552 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
2554 set_latch_flags(PR_LATCH_BLOCK,
2559 pctx->blkcount = blockcnt;
2560 if (fix_problem(ctx, problem, pctx)) {
2561 blk = *block_nr = 0;
2562 ret_code = BLOCK_CHANGED;
2563 p->inode_modified = 1;
2565 * If the directory block is too big and is beyond the
2566 * end of the FS, don't bother trying to add it for
2567 * processing -- the kernel would never have created a
2568 * directory this large, and we risk an ENOMEM abort.
2569 * In any case, the toobig handler for extent-based
2570 * directories also doesn't feed toobig blocks to
2573 if (problem == PR_1_TOOBIG_DIR)
2580 if (p->ino == EXT2_RESIZE_INO) {
2582 * The resize inode has already be sanity checked
2583 * during pass #0 (the superblock checks). All we
2584 * have to do is mark the double indirect block as
2585 * being in use; all of the other blocks are handled
2586 * by mark_table_blocks()).
2588 if (blockcnt == BLOCK_COUNT_DIND)
2589 mark_block_used(ctx, blk);
2591 } else if (!(ctx->fs->cluster_ratio_bits &&
2592 p->previous_block &&
2593 (EXT2FS_B2C(ctx->fs, blk) ==
2594 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
2595 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2596 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2597 mark_block_used(ctx, blk);
2599 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
2600 p->last_block, blk, blockcnt)) {
2601 pctx->blk = blockcnt;
2603 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2604 mark_block_used(ctx, blk);
2605 mark_block_used(ctx, blk);
2608 p->last_block = blockcnt;
2609 p->previous_block = blk;
2611 if (p->is_dir && (blockcnt >= 0)) {
2612 while (++p->last_db_block < blockcnt) {
2613 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
2616 if (pctx->errcode) {
2618 pctx->num = p->last_db_block;
2619 goto failed_add_dir_block;
2622 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
2624 if (pctx->errcode) {
2626 pctx->num = blockcnt;
2627 failed_add_dir_block:
2628 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2629 /* Should never get here */
2630 ctx->flags |= E2F_FLAG_ABORT;
2637 static int process_bad_block(ext2_filsys fs,
2639 e2_blkcnt_t blockcnt,
2640 blk64_t ref_block EXT2FS_ATTR((unused)),
2641 int ref_offset EXT2FS_ATTR((unused)),
2644 struct process_block_struct *p;
2645 blk64_t blk = *block_nr;
2646 blk64_t first_block;
2648 struct problem_context *pctx;
2652 * Note: This function processes blocks for the bad blocks
2653 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
2659 p = (struct process_block_struct *) priv_data;
2663 pctx->ino = EXT2_BAD_INO;
2665 pctx->blkcount = blockcnt;
2667 if ((blk < fs->super->s_first_data_block) ||
2668 (blk >= ext2fs_blocks_count(fs->super))) {
2669 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
2671 return BLOCK_CHANGED;
2677 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
2679 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
2681 return BLOCK_CHANGED;
2683 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2686 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
2689 return BLOCK_CHANGED;
2691 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2694 mark_block_used(ctx, blk);
2698 printf ("DEBUG: Marking %u as bad.\n", blk);
2700 ctx->fs_badblocks_count++;
2702 * If the block is not used, then mark it as used and return.
2703 * If it is already marked as found, this must mean that
2704 * there's an overlap between the filesystem table blocks
2705 * (bitmaps and inode table) and the bad block list.
2707 if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
2708 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
2712 * Try to find the where the filesystem block was used...
2714 first_block = fs->super->s_first_data_block;
2716 for (i = 0; i < fs->group_desc_count; i++ ) {
2719 if (!ext2fs_bg_has_super(fs, i))
2721 if (blk == first_block) {
2723 if (fix_problem(ctx,
2724 PR_1_BAD_PRIMARY_SUPERBLOCK,
2727 return BLOCK_CHANGED;
2731 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
2734 if ((blk > first_block) &&
2735 (blk <= first_block + fs->desc_blocks)) {
2737 pctx->blk = *block_nr;
2738 if (fix_problem(ctx,
2739 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
2741 return BLOCK_CHANGED;
2745 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
2749 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
2750 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
2751 ctx->invalid_block_bitmap_flag[i]++;
2752 ctx->invalid_bitmaps++;
2756 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
2757 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
2758 ctx->invalid_inode_bitmap_flag[i]++;
2759 ctx->invalid_bitmaps++;
2763 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
2764 (blk < (ext2fs_inode_table_loc(fs, i) +
2765 fs->inode_blocks_per_group))) {
2767 * If there are bad blocks in the inode table,
2768 * the inode scan code will try to do
2769 * something reasonable automatically.
2773 first_block += fs->super->s_blocks_per_group;
2776 * If we've gotten to this point, then the only
2777 * possibility is that the bad block inode meta data
2778 * is using a bad block.
2780 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
2781 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
2782 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
2784 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
2786 return BLOCK_CHANGED;
2788 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2795 /* Warn user that the block wasn't claimed */
2796 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
2801 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
2802 const char *name, int num, blk64_t *new_block)
2804 ext2_filsys fs = ctx->fs;
2806 blk64_t old_block = *new_block;
2809 unsigned flexbg_size;
2812 struct problem_context pctx;
2814 clear_problem_context(&pctx);
2817 pctx.blk = old_block;
2821 * For flex_bg filesystems, first try to allocate the metadata
2822 * within the flex_bg, and if that fails then try finding the
2823 * space anywhere in the filesystem.
2825 is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2826 EXT4_FEATURE_INCOMPAT_FLEX_BG);
2828 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2829 flexbg = group / flexbg_size;
2830 first_block = ext2fs_group_first_block2(fs,
2831 flexbg_size * flexbg);
2832 last_grp = group | (flexbg_size - 1);
2833 if (last_grp >= fs->group_desc_count)
2834 last_grp = fs->group_desc_count - 1;
2835 last_block = ext2fs_group_last_block2(fs, last_grp);
2837 last_block = ext2fs_group_last_block2(fs, group);
2838 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
2839 num, ctx->block_found_map,
2841 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
2842 pctx.errcode = ext2fs_get_free_blocks2(fs,
2843 fs->super->s_first_data_block,
2844 ext2fs_blocks_count(fs->super),
2845 num, ctx->block_found_map, new_block);
2848 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
2849 ext2fs_unmark_valid(fs);
2850 ctx->flags |= E2F_FLAG_ABORT;
2853 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
2855 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
2856 ext2fs_unmark_valid(fs);
2857 ctx->flags |= E2F_FLAG_ABORT;
2860 ext2fs_mark_super_dirty(fs);
2861 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2862 pctx.blk2 = *new_block;
2863 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
2864 PR_1_RELOC_TO), &pctx);
2866 for (i = 0; i < num; i++) {
2868 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
2870 pctx.errcode = io_channel_read_blk64(fs->io,
2871 old_block + i, 1, buf);
2873 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
2875 memset(buf, 0, fs->blocksize);
2877 pctx.blk = (*new_block) + i;
2878 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
2881 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
2883 ext2fs_free_mem(&buf);
2887 * This routine gets called at the end of pass 1 if bad blocks are
2888 * detected in the superblock, group descriptors, inode_bitmaps, or
2889 * block bitmaps. At this point, all of the blocks have been mapped
2890 * out, so we can try to allocate new block(s) to replace the bad
2893 static void handle_fs_bad_blocks(e2fsck_t ctx)
2895 ext2_filsys fs = ctx->fs;
2897 blk64_t first_block;
2900 for (i = 0; i < fs->group_desc_count; i++) {
2901 first_block = ext2fs_group_first_block2(fs, i);
2903 if (ctx->invalid_block_bitmap_flag[i]) {
2904 new_blk = ext2fs_block_bitmap_loc(fs, i);
2905 new_table_block(ctx, first_block, i, _("block bitmap"),
2907 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
2909 if (ctx->invalid_inode_bitmap_flag[i]) {
2910 new_blk = ext2fs_inode_bitmap_loc(fs, i);
2911 new_table_block(ctx, first_block, i, _("inode bitmap"),
2913 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
2915 if (ctx->invalid_inode_table_flag[i]) {
2916 new_blk = ext2fs_inode_table_loc(fs, i);
2917 new_table_block(ctx, first_block, i, _("inode table"),
2918 fs->inode_blocks_per_group,
2920 ext2fs_inode_table_loc_set(fs, i, new_blk);
2921 ctx->flags |= E2F_FLAG_RESTART;
2924 ctx->invalid_bitmaps = 0;
2928 * This routine marks all blocks which are used by the superblock,
2929 * group descriptors, inode bitmaps, and block bitmaps.
2931 static void mark_table_blocks(e2fsck_t ctx)
2933 ext2_filsys fs = ctx->fs;
2937 struct problem_context pctx;
2939 clear_problem_context(&pctx);
2941 for (i = 0; i < fs->group_desc_count; i++) {
2944 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
2947 * Mark the blocks used for the inode table
2949 if (ext2fs_inode_table_loc(fs, i)) {
2950 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
2951 j < fs->inode_blocks_per_group;
2953 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2956 if (!ctx->invalid_inode_table_flag[i] &&
2958 PR_1_ITABLE_CONFLICT, &pctx)) {
2959 ctx->invalid_inode_table_flag[i]++;
2960 ctx->invalid_bitmaps++;
2963 ext2fs_mark_block_bitmap2(ctx->block_found_map,
2970 * Mark block used for the block bitmap
2972 if (ext2fs_block_bitmap_loc(fs, i)) {
2973 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2974 ext2fs_block_bitmap_loc(fs, i))) {
2975 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
2976 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
2977 ctx->invalid_block_bitmap_flag[i]++;
2978 ctx->invalid_bitmaps++;
2981 ext2fs_mark_block_bitmap2(ctx->block_found_map,
2982 ext2fs_block_bitmap_loc(fs, i));
2987 * Mark block used for the inode bitmap
2989 if (ext2fs_inode_bitmap_loc(fs, i)) {
2990 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2991 ext2fs_inode_bitmap_loc(fs, i))) {
2992 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
2993 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
2994 ctx->invalid_inode_bitmap_flag[i]++;
2995 ctx->invalid_bitmaps++;
2998 ext2fs_mark_block_bitmap2(ctx->block_found_map,
2999 ext2fs_inode_bitmap_loc(fs, i));
3006 * Thes subroutines short circuits ext2fs_get_blocks and
3007 * ext2fs_check_directory; we use them since we already have the inode
3008 * structure, so there's no point in letting the ext2fs library read
3011 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
3014 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3017 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3018 return EXT2_ET_CALLBACK_NOTHANDLED;
3020 for (i=0; i < EXT2_N_BLOCKS; i++)
3021 blocks[i] = ctx->stashed_inode->i_block[i];
3025 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
3026 struct ext2_inode *inode)
3028 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3030 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3031 return EXT2_ET_CALLBACK_NOTHANDLED;
3032 *inode = *ctx->stashed_inode;
3036 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
3037 struct ext2_inode *inode)
3039 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3041 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
3042 (inode != ctx->stashed_inode))
3043 *ctx->stashed_inode = *inode;
3044 return EXT2_ET_CALLBACK_NOTHANDLED;
3047 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
3049 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3051 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3052 return EXT2_ET_CALLBACK_NOTHANDLED;
3054 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
3055 return EXT2_ET_NO_DIRECTORY;
3059 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
3062 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3066 if (ctx->block_found_map) {
3067 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
3071 if (fs->block_map) {
3072 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
3073 ext2fs_mark_bb_dirty(fs);
3076 if (!fs->block_map) {
3077 retval = ext2fs_read_block_bitmap(fs);
3082 retval = ext2fs_new_block2(fs, goal, 0, &new_block);
3091 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
3093 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3095 if (ctx->block_found_map) {
3097 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3099 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
3103 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
3105 ext2_filsys fs = ctx->fs;
3107 if (use_shortcuts) {
3108 fs->get_blocks = pass1_get_blocks;
3109 fs->check_directory = pass1_check_directory;
3110 fs->read_inode = pass1_read_inode;
3111 fs->write_inode = pass1_write_inode;
3112 ctx->stashed_ino = 0;
3115 fs->check_directory = 0;
3117 fs->write_inode = 0;
3121 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
3123 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
3124 ext2fs_set_block_alloc_stats_callback(ctx->fs,
3125 e2fsck_block_alloc_stats, 0);