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;
86 e2_blkcnt_t last_block;
87 e2_blkcnt_t last_init_lblock;
88 e2_blkcnt_t last_db_block;
89 int num_illegal_blocks;
90 blk64_t previous_block;
91 struct ext2_inode *inode;
92 struct problem_context *pctx;
93 ext2fs_block_bitmap fs_meta_blocks;
97 struct process_inode_block {
99 struct ext2_inode inode;
102 struct scan_callback_struct {
108 * For the inodes to process list.
110 static struct process_inode_block *inodes_to_process;
111 static int process_inode_count;
113 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
114 EXT2_MIN_BLOCK_LOG_SIZE + 1];
117 * Free all memory allocated by pass1 in preparation for restarting
120 static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
122 ext2fs_free_mem(&inodes_to_process);
123 inodes_to_process = 0;
127 * Check to make sure a device inode is real. Returns 1 if the device
128 * checks out, 0 if not.
130 * Note: this routine is now also used to check FIFO's and Sockets,
131 * since they have the same requirement; the i_block fields should be
134 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
135 struct ext2_inode *inode)
140 * If the index flag is set, then this is a bogus
143 if (inode->i_flags & EXT2_INDEX_FL)
147 * We should be able to do the test below all the time, but
148 * because the kernel doesn't forcibly clear the device
149 * inode's additional i_block fields, there are some rare
150 * occasions when a legitimate device inode will have non-zero
151 * additional i_block fields. So for now, we only complain
152 * when the immutable flag is set, which should never happen
153 * for devices. (And that's when the problem is caused, since
154 * you can't set or clear immutable flags for devices.) Once
155 * the kernel has been fixed we can change this...
157 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
158 for (i=4; i < EXT2_N_BLOCKS; i++)
159 if (inode->i_block[i])
166 * Check to make sure a symlink inode is real. Returns 1 if the symlink
167 * checks out, 0 if not.
169 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
170 struct ext2_inode *inode, char *buf)
175 ext2_extent_handle_t handle;
176 struct ext2_extent_info info;
177 struct ext2fs_extent extent;
179 if ((inode->i_size_high || inode->i_size == 0) ||
180 (inode->i_flags & EXT2_INDEX_FL))
183 if (inode->i_flags & EXT4_EXTENTS_FL) {
184 if (inode->i_size > fs->blocksize)
186 if (ext2fs_extent_open2(fs, ino, inode, &handle))
189 if (ext2fs_extent_get_info(handle, &info) ||
190 (info.num_entries != 1) ||
191 (info.max_depth != 0))
193 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
194 (extent.e_lblk != 0) ||
195 (extent.e_len != 1) ||
196 (extent.e_pblk < fs->super->s_first_data_block) ||
197 (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
201 ext2fs_extent_free(handle);
205 blocks = ext2fs_inode_data_blocks2(fs, inode);
207 if ((inode->i_size >= fs->blocksize) ||
208 (blocks != fs->blocksize >> 9) ||
209 (inode->i_block[0] < fs->super->s_first_data_block) ||
210 (inode->i_block[0] >= ext2fs_blocks_count(fs->super)))
213 for (i = 1; i < EXT2_N_BLOCKS; i++)
214 if (inode->i_block[i])
217 if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf))
220 len = strnlen(buf, fs->blocksize);
221 if (len == fs->blocksize)
224 if (inode->i_size >= sizeof(inode->i_block))
227 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
228 if (len == sizeof(inode->i_block))
231 if (len != inode->i_size)
237 * If the immutable (or append-only) flag is set on the inode, offer
240 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
241 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
243 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
246 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
249 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
250 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
254 * If device, fifo or socket, check size is zero -- if not offer to
257 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
259 struct ext2_inode *inode = pctx->inode;
261 if (EXT2_I_SIZE(inode) == 0)
264 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
268 inode->i_size_high = 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;
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 hash = ext2fs_ext_attr_hash_entry(entry,
324 start + entry->e_value_offs);
326 /* e_hash may be 0 in older inode's ea */
327 if (entry->e_hash != 0 && entry->e_hash != hash) {
328 pctx->num = entry->e_hash;
329 problem = PR_1_ATTR_HASH;
333 remain -= entry->e_value_size;
335 entry = EXT2_EXT_ATTR_NEXT(entry);
339 * it seems like a corruption. it's very unlikely we could repair
340 * EA(s) in automatic fashion -bzzz
342 if (problem == 0 || !fix_problem(ctx, problem, pctx))
345 /* simply remove all possible EA(s) */
346 *((__u32 *)start) = 0UL;
347 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
348 EXT2_INODE_SIZE(sb), "pass1");
351 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
353 struct ext2_super_block *sb = ctx->fs->super;
354 struct ext2_inode_large *inode;
358 inode = (struct ext2_inode_large *) pctx->inode;
359 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
360 /* this isn't large inode. so, nothing to check */
365 printf("inode #%u, i_extra_size %d\n", pctx->ino,
366 inode->i_extra_isize);
368 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
369 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
370 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
372 * For now we will allow i_extra_isize to be 0, but really
373 * implementations should never allow i_extra_isize to be 0
375 if (inode->i_extra_isize &&
376 (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
377 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
379 inode->i_extra_isize = min;
380 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
381 EXT2_INODE_SIZE(sb), "pass1");
385 eamagic = (__u32 *) (((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
386 inode->i_extra_isize);
387 if (*eamagic == EXT2_EXT_ATTR_MAGIC) {
388 /* it seems inode has an extended attribute(s) in body */
389 check_ea_in_inode(ctx, pctx);
394 * Check to see if the inode might really be a directory, despite i_mode
396 * This is a lot of complexity for something for which I'm not really
397 * convinced happens frequently in the wild. If for any reason this
398 * causes any problems, take this code out.
399 * [tytso:20070331.0827EDT]
401 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
404 struct ext2_inode *inode = pctx->inode;
405 struct ext2_dir_entry *dirent;
408 unsigned int i, rec_len, not_device = 0;
412 * If the mode looks OK, we believe it. If the first block in
413 * the i_block array is 0, this cannot be a directory. If the
414 * inode is extent-mapped, it is still the case that the latter
415 * cannot be 0 - the magic number in the extent header would make
418 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
419 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
423 * Check the block numbers in the i_block array for validity:
424 * zero blocks are skipped (but the first one cannot be zero -
425 * see above), other blocks are checked against the first and
426 * max data blocks (from the the superblock) and against the
427 * block bitmap. Any invalid block found means this cannot be
430 * If there are non-zero blocks past the fourth entry, then
431 * this cannot be a device file: we remember that for the next
434 * For extent mapped files, we don't do any sanity checking:
435 * just try to get the phys block of logical block 0 and run
439 extent_fs = (ctx->fs->super->s_feature_incompat &
440 EXT3_FEATURE_INCOMPAT_EXTENTS);
441 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
443 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
446 /* device files are never extent mapped */
449 for (i=0; i < EXT2_N_BLOCKS; i++) {
450 blk = inode->i_block[i];
456 if (blk < ctx->fs->super->s_first_data_block ||
457 blk >= ext2fs_blocks_count(ctx->fs->super) ||
458 ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
460 return; /* Invalid block, can't be dir */
462 blk = inode->i_block[0];
466 * If the mode says this is a device file and the i_links_count field
467 * is sane and we have not ruled it out as a device file previously,
468 * we declare it a device file, not a directory.
470 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
471 (inode->i_links_count == 1) && !not_device)
474 /* read the first block */
475 ehandler_operation(_("reading directory block"));
476 retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0);
477 ehandler_operation(0);
481 dirent = (struct ext2_dir_entry *) buf;
482 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
485 if (((dirent->name_len & 0xFF) != 1) ||
486 (dirent->name[0] != '.') ||
487 (dirent->inode != pctx->ino) ||
490 (rec_len >= ctx->fs->blocksize - 12))
493 dirent = (struct ext2_dir_entry *) (buf + rec_len);
494 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
497 if (((dirent->name_len & 0xFF) != 2) ||
498 (dirent->name[0] != '.') ||
499 (dirent->name[1] != '.') ||
504 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
505 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
506 e2fsck_write_inode_full(ctx, pctx->ino, inode,
507 EXT2_INODE_SIZE(ctx->fs->super),
508 "check_is_really_dir");
512 extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
515 unsigned int threshold;
523 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
525 profile_get_uint(ctx->profile, "scratch_files",
526 "numdirs_threshold", 0, 0, &threshold);
527 profile_get_boolean(ctx->profile, "scratch_files",
528 "icount", 0, 1, &enable);
530 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
532 num_dirs = 1024; /* Guess */
534 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
535 (threshold && num_dirs <= threshold))
538 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
543 void e2fsck_pass1(e2fsck_t ctx)
547 ext2_filsys fs = ctx->fs;
549 struct ext2_inode *inode;
550 ext2_inode_scan scan;
552 #ifdef RESOURCE_TRACK
553 struct resource_track rtrack;
555 unsigned char frag, fsize;
556 struct problem_context pctx;
557 struct scan_callback_struct scan_struct;
558 struct ext2_super_block *sb = ctx->fs->super;
560 unsigned int save_type;
561 int imagic_fs, extent_fs;
562 int busted_fs_time = 0;
565 init_resource_track(&rtrack, ctx->fs->io);
566 clear_problem_context(&pctx);
568 if (!(ctx->options & E2F_OPT_PREEN))
569 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
571 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
572 !(ctx->options & E2F_OPT_NO)) {
573 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
574 ctx->dirs_to_hash = 0;
578 mtrace_print("Pass 1");
581 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
583 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
584 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
585 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
586 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
587 max_sizes = (max_sizes * (1UL << i));
588 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
592 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
593 extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
596 * Allocate bitmaps structures
598 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
599 EXT2FS_BMAP64_RBTREE,
601 &ctx->inode_used_map);
604 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
605 ctx->flags |= E2F_FLAG_ABORT;
608 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
609 _("directory inode map"),
610 EXT2FS_BMAP64_AUTODIR,
611 "inode_dir_map", &ctx->inode_dir_map);
614 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
615 ctx->flags |= E2F_FLAG_ABORT;
618 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
619 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
620 "inode_reg_map", &ctx->inode_reg_map);
623 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
624 ctx->flags |= E2F_FLAG_ABORT;
627 pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
628 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
629 "block_found_map", &ctx->block_found_map);
632 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
633 ctx->flags |= E2F_FLAG_ABORT;
636 e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
637 if (!ctx->inode_link_info) {
638 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
639 "inode_link_info", &save_type);
640 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
641 &ctx->inode_link_info);
642 fs->default_bitmap_type = save_type;
646 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
647 ctx->flags |= E2F_FLAG_ABORT;
650 inode_size = EXT2_INODE_SIZE(fs->super);
651 inode = (struct ext2_inode *)
652 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
654 inodes_to_process = (struct process_inode_block *)
655 e2fsck_allocate_memory(ctx,
656 (ctx->process_inode_size *
657 sizeof(struct process_inode_block)),
658 "array of inodes to process");
659 process_inode_count = 0;
661 pctx.errcode = ext2fs_init_dblist(fs, 0);
663 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
664 ctx->flags |= E2F_FLAG_ABORT;
665 ext2fs_free_mem(&inode);
670 * If the last orphan field is set, clear it, since the pass1
671 * processing will automatically find and clear the orphans.
672 * In the future, we may want to try using the last_orphan
673 * linked list ourselves, but for now, we clear it so that the
674 * ext3 mount code won't get confused.
676 if (!(ctx->options & E2F_OPT_READONLY)) {
677 if (fs->super->s_last_orphan) {
678 fs->super->s_last_orphan = 0;
679 ext2fs_mark_super_dirty(fs);
683 mark_table_blocks(ctx);
684 pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
685 &ctx->block_found_map);
687 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
688 ctx->flags |= E2F_FLAG_ABORT;
689 ext2fs_free_mem(&inode);
692 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
693 "block interate buffer");
694 e2fsck_use_inode_shortcuts(ctx, 1);
695 old_op = ehandler_operation(_("opening inode scan"));
696 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
698 ehandler_operation(old_op);
700 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
701 ctx->flags |= E2F_FLAG_ABORT;
702 ext2fs_free_mem(&block_buf);
703 ext2fs_free_mem(&inode);
706 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
707 ctx->stashed_inode = inode;
708 scan_struct.ctx = ctx;
709 scan_struct.block_buf = block_buf;
710 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
712 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
714 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
715 (fs->super->s_mtime < fs->super->s_inodes_count))
718 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
719 !(fs->super->s_mmp_block <= fs->super->s_first_data_block ||
720 fs->super->s_mmp_block >= fs->super->s_blocks_count))
721 ext2fs_mark_block_bitmap2(ctx->block_found_map,
722 fs->super->s_mmp_block);
725 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
726 if (e2fsck_mmp_update(fs))
729 old_op = ehandler_operation(_("getting next inode from scan"));
730 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
732 ehandler_operation(old_op);
733 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
735 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
736 if (!ctx->inode_bb_map)
738 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
739 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
743 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
744 ctx->flags |= E2F_FLAG_ABORT;
751 ctx->stashed_ino = ino;
752 if (inode->i_links_count) {
753 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
754 ino, inode->i_links_count);
756 pctx.num = inode->i_links_count;
757 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
758 ctx->flags |= E2F_FLAG_ABORT;
764 * Test for incorrect extent flag settings.
766 * On big-endian machines we must be careful:
767 * When the inode is read, the i_block array is not swapped
768 * if the extent flag is set. Therefore if we are testing
769 * for or fixing a wrongly-set flag, we must potentially
770 * (un)swap before testing, or after fixing.
774 * In this case the extents flag was set when read, so
775 * extent_header_verify is ok. If the inode is cleared,
776 * no need to swap... so no extra swapping here.
778 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
779 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
780 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
781 if ((ext2fs_extent_header_verify(inode->i_block,
782 sizeof(inode->i_block)) == 0) &&
783 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
784 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
785 ext2fs_mark_super_dirty(fs);
787 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
789 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
790 if (ino == EXT2_BAD_INO)
791 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
798 * For big-endian machines:
799 * If the inode didn't have the extents flag set when it
800 * was read, then the i_blocks array was swapped. To test
801 * as an extents header, we must swap it back first.
802 * IF we then set the extents flag, the entire i_block
803 * array must be un/re-swapped to make it proper extents data.
805 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
806 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
807 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
808 (LINUX_S_ISREG(inode->i_mode) ||
809 LINUX_S_ISDIR(inode->i_mode))) {
811 #ifdef WORDS_BIGENDIAN
812 __u32 tmp_block[EXT2_N_BLOCKS];
814 for (i = 0; i < EXT2_N_BLOCKS; i++)
815 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
818 ehp = inode->i_block;
820 if ((ext2fs_extent_header_verify(ehp,
821 sizeof(inode->i_block)) == 0) &&
822 (fix_problem(ctx, PR_1_UNSET_EXTENT_FL, &pctx))) {
823 inode->i_flags |= EXT4_EXTENTS_FL;
824 #ifdef WORDS_BIGENDIAN
825 memcpy(inode->i_block, tmp_block,
826 sizeof(inode->i_block));
828 e2fsck_write_inode(ctx, ino, inode, "pass1");
832 if (ino == EXT2_BAD_INO) {
833 struct process_block_struct pb;
835 if ((inode->i_mode || inode->i_uid || inode->i_gid ||
836 inode->i_links_count || inode->i_file_acl) &&
837 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
838 memset(inode, 0, sizeof(struct ext2_inode));
839 e2fsck_write_inode(ctx, ino, inode,
843 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
847 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
848 ctx->flags |= E2F_FLAG_ABORT;
851 pb.ino = EXT2_BAD_INO;
852 pb.num_blocks = pb.last_block = 0;
853 pb.last_db_block = -1;
854 pb.num_illegal_blocks = 0;
855 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
856 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
860 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
861 block_buf, process_bad_block, &pb);
862 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
864 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
865 ctx->flags |= E2F_FLAG_ABORT;
869 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
870 ctx->flags |= E2F_FLAG_ABORT;
873 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
874 clear_problem_context(&pctx);
876 } else if (ino == EXT2_ROOT_INO) {
878 * Make sure the root inode is a directory; if
879 * not, offer to clear it. It will be
880 * regnerated in pass #3.
882 if (!LINUX_S_ISDIR(inode->i_mode)) {
883 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
887 * If dtime is set, offer to clear it. mke2fs
888 * version 0.2b created filesystems with the
889 * dtime field set for the root and lost+found
890 * directories. We won't worry about
891 * /lost+found, since that can be regenerated
892 * easily. But we will fix the root directory
895 if (inode->i_dtime && inode->i_links_count) {
896 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
898 e2fsck_write_inode(ctx, ino, inode,
902 } else if (ino == EXT2_JOURNAL_INO) {
903 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
904 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
905 if (!LINUX_S_ISREG(inode->i_mode) &&
906 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
908 inode->i_mode = LINUX_S_IFREG;
909 e2fsck_write_inode(ctx, ino, inode,
912 check_blocks(ctx, &pctx, block_buf);
915 if ((inode->i_links_count ||
916 inode->i_blocks || inode->i_block[0]) &&
917 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
919 memset(inode, 0, inode_size);
920 ext2fs_icount_store(ctx->inode_link_info,
922 e2fsck_write_inode_full(ctx, ino, inode,
923 inode_size, "pass1");
925 } else if ((ino == EXT4_USR_QUOTA_INO) ||
926 (ino == EXT4_GRP_QUOTA_INO)) {
927 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
928 if ((fs->super->s_feature_ro_compat &
929 EXT4_FEATURE_RO_COMPAT_QUOTA) &&
930 ((fs->super->s_usr_quota_inum == ino) ||
931 (fs->super->s_grp_quota_inum == ino))) {
932 if (!LINUX_S_ISREG(inode->i_mode) &&
933 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
935 inode->i_mode = LINUX_S_IFREG;
936 e2fsck_write_inode(ctx, ino, inode,
939 check_blocks(ctx, &pctx, block_buf);
942 if ((inode->i_links_count ||
943 inode->i_blocks || inode->i_block[0]) &&
944 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
946 memset(inode, 0, inode_size);
947 ext2fs_icount_store(ctx->inode_link_info,
949 e2fsck_write_inode_full(ctx, ino, inode,
950 inode_size, "pass1");
952 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
955 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
956 if (ino == EXT2_BOOT_LOADER_INO) {
957 if (LINUX_S_ISDIR(inode->i_mode))
958 problem = PR_1_RESERVED_BAD_MODE;
959 } else if (ino == EXT2_RESIZE_INO) {
961 !LINUX_S_ISREG(inode->i_mode))
962 problem = PR_1_RESERVED_BAD_MODE;
964 if (inode->i_mode != 0)
965 problem = PR_1_RESERVED_BAD_MODE;
968 if (fix_problem(ctx, problem, &pctx)) {
970 e2fsck_write_inode(ctx, ino, inode,
974 check_blocks(ctx, &pctx, block_buf);
979 * Check for inodes who might have been part of the
980 * orphaned list linked list. They should have gotten
981 * dealt with by now, unless the list had somehow been
984 * FIXME: In the future, inodes which are still in use
985 * (and which are therefore) pending truncation should
986 * be handled specially. Right now we just clear the
987 * dtime field, and the normal e2fsck handling of
988 * inodes where i_size and the inode blocks are
989 * inconsistent is to fix i_size, instead of releasing
990 * the extra blocks. This won't catch the inodes that
991 * was at the end of the orphan list, but it's better
992 * than nothing. The right answer is that there
993 * shouldn't be any bugs in the orphan list handling. :-)
995 if (inode->i_dtime && !busted_fs_time &&
996 inode->i_dtime < ctx->fs->super->s_inodes_count) {
997 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
998 inode->i_dtime = inode->i_links_count ?
1000 e2fsck_write_inode(ctx, ino, inode,
1006 * This code assumes that deleted inodes have
1007 * i_links_count set to 0.
1009 if (!inode->i_links_count) {
1010 if (!inode->i_dtime && inode->i_mode) {
1011 if (fix_problem(ctx,
1012 PR_1_ZERO_DTIME, &pctx)) {
1013 inode->i_dtime = ctx->now;
1014 e2fsck_write_inode(ctx, ino, inode,
1021 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1022 * deleted files. Oops.
1024 * Since all new ext2 implementations get this right,
1025 * we now assume that the case of non-zero
1026 * i_links_count and non-zero dtime means that we
1027 * should keep the file, not delete it.
1030 if (inode->i_dtime) {
1031 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1033 e2fsck_write_inode(ctx, ino, inode, "pass1");
1037 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1038 switch (fs->super->s_creator_os) {
1040 frag = inode->osd2.hurd2.h_i_frag;
1041 fsize = inode->osd2.hurd2.h_i_fsize;
1047 if (inode->i_faddr || frag || fsize ||
1048 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
1049 mark_inode_bad(ctx, ino);
1050 if (!(fs->super->s_feature_incompat &
1051 EXT4_FEATURE_INCOMPAT_64BIT) &&
1052 inode->osd2.linux2.l_i_file_acl_high != 0)
1053 mark_inode_bad(ctx, ino);
1054 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1055 !(fs->super->s_feature_ro_compat &
1056 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1057 (inode->osd2.linux2.l_i_blocks_hi != 0))
1058 mark_inode_bad(ctx, ino);
1059 if (inode->i_flags & EXT2_IMAGIC_FL) {
1061 if (!ctx->inode_imagic_map)
1062 alloc_imagic_map(ctx);
1063 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1066 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1067 inode->i_flags &= ~EXT2_IMAGIC_FL;
1068 e2fsck_write_inode(ctx, ino,
1074 check_inode_extra_space(ctx, &pctx);
1075 check_is_really_dir(ctx, &pctx, block_buf);
1078 * ext2fs_inode_has_valid_blocks2 does not actually look
1079 * at i_block[] values, so not endian-sensitive here.
1081 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1082 LINUX_S_ISLNK(inode->i_mode) &&
1083 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1084 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1085 inode->i_flags &= ~EXT4_EXTENTS_FL;
1086 e2fsck_write_inode(ctx, ino, inode, "pass1");
1089 if (LINUX_S_ISDIR(inode->i_mode)) {
1090 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1091 e2fsck_add_dir_info(ctx, ino, 0);
1092 ctx->fs_directory_count++;
1093 } else if (LINUX_S_ISREG (inode->i_mode)) {
1094 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1095 ctx->fs_regular_count++;
1096 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1097 e2fsck_pass1_check_device_inode(fs, inode)) {
1098 check_immutable(ctx, &pctx);
1099 check_size(ctx, &pctx);
1100 ctx->fs_chardev_count++;
1101 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1102 e2fsck_pass1_check_device_inode(fs, inode)) {
1103 check_immutable(ctx, &pctx);
1104 check_size(ctx, &pctx);
1105 ctx->fs_blockdev_count++;
1106 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1107 e2fsck_pass1_check_symlink(fs, ino, inode,
1109 check_immutable(ctx, &pctx);
1110 ctx->fs_symlinks_count++;
1111 if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1112 ctx->fs_fast_symlinks_count++;
1113 check_blocks(ctx, &pctx, block_buf);
1117 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1118 e2fsck_pass1_check_device_inode(fs, inode)) {
1119 check_immutable(ctx, &pctx);
1120 check_size(ctx, &pctx);
1121 ctx->fs_fifo_count++;
1122 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1123 e2fsck_pass1_check_device_inode(fs, inode)) {
1124 check_immutable(ctx, &pctx);
1125 check_size(ctx, &pctx);
1126 ctx->fs_sockets_count++;
1128 mark_inode_bad(ctx, ino);
1129 if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
1130 if (inode->i_block[EXT2_IND_BLOCK])
1131 ctx->fs_ind_count++;
1132 if (inode->i_block[EXT2_DIND_BLOCK])
1133 ctx->fs_dind_count++;
1134 if (inode->i_block[EXT2_TIND_BLOCK])
1135 ctx->fs_tind_count++;
1137 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1138 (inode->i_block[EXT2_IND_BLOCK] ||
1139 inode->i_block[EXT2_DIND_BLOCK] ||
1140 inode->i_block[EXT2_TIND_BLOCK] ||
1141 ext2fs_file_acl_block(fs, inode))) {
1142 inodes_to_process[process_inode_count].ino = ino;
1143 inodes_to_process[process_inode_count].inode = *inode;
1144 process_inode_count++;
1146 check_blocks(ctx, &pctx, block_buf);
1148 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1151 if (process_inode_count >= ctx->process_inode_size) {
1152 process_inodes(ctx, block_buf);
1154 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1158 process_inodes(ctx, block_buf);
1159 ext2fs_close_inode_scan(scan);
1162 * If any extended attribute blocks' reference counts need to
1163 * be adjusted, either up (ctx->refcount_extra), or down
1164 * (ctx->refcount), then fix them.
1166 if (ctx->refcount) {
1167 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1168 ea_refcount_free(ctx->refcount);
1171 if (ctx->refcount_extra) {
1172 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1174 ea_refcount_free(ctx->refcount_extra);
1175 ctx->refcount_extra = 0;
1178 if (ctx->invalid_bitmaps)
1179 handle_fs_bad_blocks(ctx);
1181 /* We don't need the block_ea_map any more */
1182 if (ctx->block_ea_map) {
1183 ext2fs_free_block_bitmap(ctx->block_ea_map);
1184 ctx->block_ea_map = 0;
1187 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1188 ext2fs_block_bitmap save_bmap;
1190 save_bmap = fs->block_map;
1191 fs->block_map = ctx->block_found_map;
1192 clear_problem_context(&pctx);
1193 pctx.errcode = ext2fs_create_resize_inode(fs);
1195 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1197 ctx->flags |= E2F_FLAG_ABORT;
1202 if (!pctx.errcode) {
1203 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1205 inode->i_mtime = ctx->now;
1206 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1209 fs->block_map = save_bmap;
1210 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1213 if (ctx->flags & E2F_FLAG_RESTART) {
1215 * Only the master copy of the superblock and block
1216 * group descriptors are going to be written during a
1217 * restart, so set the superblock to be used to be the
1218 * master superblock.
1220 ctx->use_superblock = 0;
1225 if (ctx->block_dup_map) {
1226 if (ctx->options & E2F_OPT_PREEN) {
1227 clear_problem_context(&pctx);
1228 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1230 e2fsck_pass1_dupblocks(ctx, block_buf);
1232 ext2fs_free_mem(&inodes_to_process);
1234 e2fsck_use_inode_shortcuts(ctx, 0);
1236 ext2fs_free_mem(&block_buf);
1237 ext2fs_free_mem(&inode);
1239 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1243 * When the inode_scan routines call this callback at the end of the
1244 * glock group, call process_inodes.
1246 static errcode_t scan_callback(ext2_filsys fs,
1247 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1248 dgrp_t group, void * priv_data)
1250 struct scan_callback_struct *scan_struct;
1253 scan_struct = (struct scan_callback_struct *) priv_data;
1254 ctx = scan_struct->ctx;
1256 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1259 if ((ctx->progress)(ctx, 1, group+1,
1260 ctx->fs->group_desc_count))
1261 return EXT2_ET_CANCEL_REQUESTED;
1267 * Process the inodes in the "inodes to process" list.
1269 static void process_inodes(e2fsck_t ctx, char *block_buf)
1272 struct ext2_inode *old_stashed_inode;
1273 ext2_ino_t old_stashed_ino;
1274 const char *old_operation;
1276 struct problem_context pctx;
1279 printf("begin process_inodes: ");
1281 if (process_inode_count == 0)
1283 old_operation = ehandler_operation(0);
1284 old_stashed_inode = ctx->stashed_inode;
1285 old_stashed_ino = ctx->stashed_ino;
1286 qsort(inodes_to_process, process_inode_count,
1287 sizeof(struct process_inode_block), process_inode_cmp);
1288 clear_problem_context(&pctx);
1289 for (i=0; i < process_inode_count; i++) {
1290 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1291 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1294 printf("%u ", pctx.ino);
1296 sprintf(buf, _("reading indirect blocks of inode %u"),
1298 ehandler_operation(buf);
1299 check_blocks(ctx, &pctx, block_buf);
1300 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1303 ctx->stashed_inode = old_stashed_inode;
1304 ctx->stashed_ino = old_stashed_ino;
1305 process_inode_count = 0;
1307 printf("end process inodes\n");
1309 ehandler_operation(old_operation);
1312 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1314 const struct process_inode_block *ib_a =
1315 (const struct process_inode_block *) a;
1316 const struct process_inode_block *ib_b =
1317 (const struct process_inode_block *) b;
1320 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1321 ib_b->inode.i_block[EXT2_IND_BLOCK]);
1324 * We only call process_inodes() for non-extent
1325 * inodes, so it's OK to pass NULL to
1326 * ext2fs_file_acl_block() here.
1328 ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
1329 ext2fs_file_acl_block(0, &(ib_b->inode));
1331 ret = ib_a->ino - ib_b->ino;
1336 * Mark an inode as being bad in some what
1338 static void mark_inode_bad(e2fsck_t ctx, ino_t ino)
1340 struct problem_context pctx;
1342 if (!ctx->inode_bad_map) {
1343 clear_problem_context(&pctx);
1345 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1346 _("bad inode map"), EXT2FS_BMAP64_RBTREE,
1347 "inode_bad_map", &ctx->inode_bad_map);
1350 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1351 /* Should never get here */
1352 ctx->flags |= E2F_FLAG_ABORT;
1356 ext2fs_mark_inode_bitmap2(ctx->inode_bad_map, ino);
1361 * This procedure will allocate the inode "bb" (badblock) map table
1363 static void alloc_bb_map(e2fsck_t ctx)
1365 struct problem_context pctx;
1367 clear_problem_context(&pctx);
1368 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1369 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
1370 "inode_bb_map", &ctx->inode_bb_map);
1373 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1374 /* Should never get here */
1375 ctx->flags |= E2F_FLAG_ABORT;
1381 * This procedure will allocate the inode imagic table
1383 static void alloc_imagic_map(e2fsck_t ctx)
1385 struct problem_context pctx;
1387 clear_problem_context(&pctx);
1388 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1389 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
1390 "inode_imagic_map", &ctx->inode_imagic_map);
1393 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1394 /* Should never get here */
1395 ctx->flags |= E2F_FLAG_ABORT;
1401 * Marks a block as in use, setting the dup_map if it's been set
1402 * already. Called by process_block and process_bad_block.
1404 * WARNING: Assumes checks have already been done to make sure block
1405 * is valid. This is true in both process_block and process_bad_block.
1407 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
1409 struct problem_context pctx;
1411 clear_problem_context(&pctx);
1413 if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
1414 if (!ctx->block_dup_map) {
1415 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1416 _("multiply claimed block map"),
1417 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1418 &ctx->block_dup_map);
1421 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1423 /* Should never get here */
1424 ctx->flags |= E2F_FLAG_ABORT;
1428 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
1430 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
1434 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
1437 if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
1438 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
1441 mark_block_used(ctx, block++);
1445 * Adjust the extended attribute block's reference counts at the end
1446 * of pass 1, either by subtracting out references for EA blocks that
1447 * are still referenced in ctx->refcount, or by adding references for
1448 * EA blocks that had extra references as accounted for in
1449 * ctx->refcount_extra.
1451 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1452 char *block_buf, int adjust_sign)
1454 struct ext2_ext_attr_header *header;
1455 struct problem_context pctx;
1456 ext2_filsys fs = ctx->fs;
1461 clear_problem_context(&pctx);
1463 ea_refcount_intr_begin(refcount);
1465 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1468 pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1470 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1473 header = (struct ext2_ext_attr_header *) block_buf;
1474 pctx.blkcount = header->h_refcount;
1475 should_be = header->h_refcount + adjust_sign * count;
1476 pctx.num = should_be;
1477 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1478 header->h_refcount = should_be;
1479 pctx.errcode = ext2fs_write_ext_attr2(fs, blk,
1482 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
1491 * Handle processing the extended attribute blocks
1493 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1496 ext2_filsys fs = ctx->fs;
1497 ext2_ino_t ino = pctx->ino;
1498 struct ext2_inode *inode = pctx->inode;
1501 struct ext2_ext_attr_header *header;
1502 struct ext2_ext_attr_entry *entry;
1504 region_t region = 0;
1506 blk = ext2fs_file_acl_block(fs, inode);
1511 * If the Extended attribute flag isn't set, then a non-zero
1512 * file acl means that the inode is corrupted.
1514 * Or if the extended attribute block is an invalid block,
1515 * then the inode is also corrupted.
1517 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1518 (blk < fs->super->s_first_data_block) ||
1519 (blk >= ext2fs_blocks_count(fs->super))) {
1520 mark_inode_bad(ctx, ino);
1524 /* If ea bitmap hasn't been allocated, create it */
1525 if (!ctx->block_ea_map) {
1526 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
1527 _("ext attr block map"),
1528 EXT2FS_BMAP64_RBTREE, "block_ea_map",
1529 &ctx->block_ea_map);
1530 if (pctx->errcode) {
1532 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1533 ctx->flags |= E2F_FLAG_ABORT;
1538 /* Create the EA refcount structure if necessary */
1539 if (!ctx->refcount) {
1540 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1541 if (pctx->errcode) {
1543 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1544 ctx->flags |= E2F_FLAG_ABORT;
1550 /* Debugging text */
1551 printf("Inode %u has EA block %u\n", ino, blk);
1554 /* Have we seen this EA block before? */
1555 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
1556 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1558 /* Ooops, this EA was referenced more than it stated */
1559 if (!ctx->refcount_extra) {
1560 pctx->errcode = ea_refcount_create(0,
1561 &ctx->refcount_extra);
1562 if (pctx->errcode) {
1564 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1565 ctx->flags |= E2F_FLAG_ABORT;
1569 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1574 * OK, we haven't seen this EA block yet. So we need to
1578 pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1579 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1581 header = (struct ext2_ext_attr_header *) block_buf;
1582 pctx->blk = ext2fs_file_acl_block(fs, inode);
1583 if (((ctx->ext_attr_ver == 1) &&
1584 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1585 ((ctx->ext_attr_ver == 2) &&
1586 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1587 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1591 if (header->h_blocks != 1) {
1592 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1596 region = region_create(0, fs->blocksize);
1598 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
1599 ctx->flags |= E2F_FLAG_ABORT;
1602 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1603 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1607 entry = (struct ext2_ext_attr_entry *)(header+1);
1608 end = block_buf + fs->blocksize;
1609 while ((char *)entry < end && *(__u32 *)entry) {
1612 if (region_allocate(region, (char *)entry - (char *)header,
1613 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1614 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1618 if ((ctx->ext_attr_ver == 1 &&
1619 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1620 (ctx->ext_attr_ver == 2 &&
1621 entry->e_name_index == 0)) {
1622 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1626 if (entry->e_value_block != 0) {
1627 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1630 if (entry->e_value_offs + entry->e_value_size > fs->blocksize) {
1631 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1635 if (entry->e_value_size &&
1636 region_allocate(region, entry->e_value_offs,
1637 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1638 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1642 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
1643 entry->e_value_offs);
1645 if (entry->e_hash != hash) {
1646 pctx->num = entry->e_hash;
1647 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
1649 entry->e_hash = hash;
1652 entry = EXT2_EXT_ATTR_NEXT(entry);
1654 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1655 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1658 region_free(region);
1660 count = header->h_refcount - 1;
1662 ea_refcount_store(ctx->refcount, blk, count);
1663 mark_block_used(ctx, blk);
1664 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
1669 region_free(region);
1670 ext2fs_file_acl_block_set(fs, inode, 0);
1671 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1675 /* Returns 1 if bad htree, 0 if OK */
1676 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1677 ext2_ino_t ino, struct ext2_inode *inode,
1680 struct ext2_dx_root_info *root;
1681 ext2_filsys fs = ctx->fs;
1685 if ((!LINUX_S_ISDIR(inode->i_mode) &&
1686 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1687 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
1688 fix_problem(ctx, PR_1_HTREE_SET, pctx)))
1691 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
1693 if ((pctx->errcode) ||
1695 (blk < fs->super->s_first_data_block) ||
1696 (blk >= ext2fs_blocks_count(fs->super))) {
1697 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1703 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
1704 if (retval && fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1707 /* XXX should check that beginning matches a directory */
1708 root = (struct ext2_dx_root_info *) (block_buf + 24);
1710 if ((root->reserved_zero || root->info_length < 8) &&
1711 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1714 pctx->num = root->hash_version;
1715 if ((root->hash_version != EXT2_HASH_LEGACY) &&
1716 (root->hash_version != EXT2_HASH_HALF_MD4) &&
1717 (root->hash_version != EXT2_HASH_TEA) &&
1718 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
1721 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
1722 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
1725 pctx->num = root->indirect_levels;
1726 if ((root->indirect_levels > 1) &&
1727 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
1733 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
1734 struct ext2_inode *inode, int restart_flag,
1738 inode->i_links_count = 0;
1739 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
1740 inode->i_dtime = ctx->now;
1742 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
1743 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
1744 if (ctx->inode_reg_map)
1745 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
1746 if (ctx->inode_bad_map)
1747 ext2fs_unmark_inode_bitmap2(ctx->inode_bad_map, ino);
1750 * If the inode was partially accounted for before processing
1751 * was aborted, we need to restart the pass 1 scan.
1753 ctx->flags |= restart_flag;
1755 if (ino == EXT2_BAD_INO)
1756 memset(inode, 0, sizeof(struct ext2_inode));
1758 e2fsck_write_inode(ctx, ino, inode, source);
1761 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
1762 struct process_block_struct *pb,
1763 blk64_t start_block,
1764 ext2_extent_handle_t ehandle)
1766 struct ext2fs_extent extent;
1768 e2_blkcnt_t blockcnt;
1770 int is_dir, is_leaf;
1772 struct ext2_extent_info info;
1774 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
1778 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
1780 while (!pctx->errcode && info.num_entries-- > 0) {
1781 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
1782 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
1785 if (extent.e_pblk == 0 ||
1786 extent.e_pblk < ctx->fs->super->s_first_data_block ||
1787 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
1788 problem = PR_1_EXTENT_BAD_START_BLK;
1789 else if (extent.e_lblk < start_block)
1790 problem = PR_1_OUT_OF_ORDER_EXTENTS;
1791 else if (is_leaf && extent.e_len == 0)
1792 problem = PR_1_EXTENT_LENGTH_ZERO;
1794 (extent.e_pblk + extent.e_len) >
1795 ext2fs_blocks_count(ctx->fs->super))
1796 problem = PR_1_EXTENT_ENDS_BEYOND;
1800 pctx->blk = extent.e_pblk;
1801 pctx->blk2 = extent.e_lblk;
1802 pctx->num = extent.e_len;
1803 if (fix_problem(ctx, problem, pctx)) {
1804 e2fsck_read_bitmaps(ctx);
1806 ext2fs_extent_delete(ehandle, 0);
1807 if (pctx->errcode) {
1808 pctx->str = "ext2fs_extent_delete";
1811 ext2fs_extent_fix_parents(ehandle);
1812 pctx->errcode = ext2fs_extent_get(ehandle,
1813 EXT2_EXTENT_CURRENT,
1815 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
1827 blk = extent.e_pblk;
1828 lblk = extent.e_lblk;
1829 pctx->errcode = ext2fs_extent_get(ehandle,
1830 EXT2_EXTENT_DOWN, &extent);
1831 if (pctx->errcode) {
1832 pctx->str = "EXT2_EXTENT_DOWN";
1833 problem = PR_1_EXTENT_HEADER_INVALID;
1834 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
1835 goto report_problem;
1838 /* The next extent should match this index's logical start */
1839 if (extent.e_lblk != lblk) {
1840 struct ext2_extent_info e_info;
1842 ext2fs_extent_get_info(ehandle, &e_info);
1844 pctx->blk2 = extent.e_lblk;
1845 pctx->num = e_info.curr_level - 1;
1846 problem = PR_1_EXTENT_INDEX_START_INVALID;
1847 if (fix_problem(ctx, problem, pctx))
1848 ext2fs_extent_fix_parents(ehandle);
1850 scan_extent_node(ctx, pctx, pb, extent.e_lblk, ehandle);
1853 pctx->errcode = ext2fs_extent_get(ehandle,
1854 EXT2_EXTENT_UP, &extent);
1855 if (pctx->errcode) {
1856 pctx->str = "EXT2_EXTENT_UP";
1859 mark_block_used(ctx, blk);
1864 if ((pb->previous_block != 0) &&
1865 (pb->previous_block+1 != extent.e_pblk)) {
1866 if (ctx->options & E2F_OPT_FRAGCHECK) {
1871 else if (pb->is_reg)
1874 printf(("%6lu(%c): expecting %6lu "
1876 "phys %6lu log %lu len %lu\n"),
1877 (unsigned long) pctx->ino, type,
1878 (unsigned long) pb->previous_block+1,
1879 (unsigned long) extent.e_pblk,
1880 (unsigned long) extent.e_lblk,
1881 (unsigned long) extent.e_len);
1885 while (is_dir && (++pb->last_db_block <
1886 (e2_blkcnt_t) extent.e_lblk)) {
1887 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
1890 if (pctx->errcode) {
1892 pctx->num = pb->last_db_block;
1893 goto failed_add_dir_block;
1896 if (!ctx->fs->cluster_ratio_bits) {
1897 mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
1898 pb->num_blocks += extent.e_len;
1900 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
1902 blk++, blockcnt++, i++) {
1903 if (ctx->fs->cluster_ratio_bits &&
1904 !(pb->previous_block &&
1905 (EXT2FS_B2C(ctx->fs, blk) ==
1906 EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
1907 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
1908 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
1909 mark_block_used(ctx, blk);
1913 pb->previous_block = blk;
1916 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
1917 if (pctx->errcode) {
1919 pctx->num = blockcnt;
1920 failed_add_dir_block:
1921 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
1922 /* Should never get here */
1923 ctx->flags |= E2F_FLAG_ABORT;
1928 if (is_dir && extent.e_len > 0)
1929 pb->last_db_block = blockcnt - 1;
1930 pb->previous_block = extent.e_pblk + extent.e_len - 1;
1931 start_block = pb->last_block = extent.e_lblk + extent.e_len - 1;
1932 if (is_leaf && !is_dir &&
1933 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
1934 pb->last_init_lblock = extent.e_lblk + extent.e_len - 1;
1936 pctx->errcode = ext2fs_extent_get(ehandle,
1937 EXT2_EXTENT_NEXT_SIB,
1940 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
1944 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
1945 struct process_block_struct *pb)
1947 struct ext2_extent_info info;
1948 struct ext2_inode *inode = pctx->inode;
1949 ext2_extent_handle_t ehandle;
1950 ext2_filsys fs = ctx->fs;
1951 ext2_ino_t ino = pctx->ino;
1954 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
1955 if (pctx->errcode) {
1956 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
1957 e2fsck_clear_inode(ctx, ino, inode, 0,
1958 "check_blocks_extents");
1963 retval = ext2fs_extent_get_info(ehandle, &info);
1965 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
1966 info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
1967 ctx->extent_depth_count[info.max_depth]++;
1970 scan_extent_node(ctx, pctx, pb, 0, ehandle);
1971 if (pctx->errcode &&
1972 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
1974 inode->i_blocks = 0;
1975 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
1976 "check_blocks_extents");
1979 ext2fs_extent_free(ehandle);
1983 * This subroutine is called on each inode to account for all of the
1984 * blocks used by that inode.
1986 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
1989 ext2_filsys fs = ctx->fs;
1990 struct process_block_struct pb;
1991 ext2_ino_t ino = pctx->ino;
1992 struct ext2_inode *inode = pctx->inode;
1994 int dirty_inode = 0;
2001 pb.last_init_lblock = -1;
2002 pb.last_db_block = -1;
2003 pb.num_illegal_blocks = 0;
2004 pb.suppress = 0; pb.clear = 0;
2007 pb.previous_block = 0;
2008 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2009 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
2010 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
2017 extent_fs = (ctx->fs->super->s_feature_incompat &
2018 EXT3_FEATURE_INCOMPAT_EXTENTS);
2020 if (inode->i_flags & EXT2_COMPRBLK_FL) {
2021 if (fs->super->s_feature_incompat &
2022 EXT2_FEATURE_INCOMPAT_COMPRESSION)
2025 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
2026 inode->i_flags &= ~EXT2_COMPRBLK_FL;
2032 if (ext2fs_file_acl_block(fs, inode) &&
2033 check_ext_attr(ctx, pctx, block_buf)) {
2034 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2039 if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
2040 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
2041 check_blocks_extents(ctx, pctx, &pb);
2043 pctx->errcode = ext2fs_block_iterate3(fs, ino,
2044 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
2045 block_buf, process_block, &pb);
2047 * We do not have uninitialized extents in non extent
2050 pb.last_init_lblock = pb.last_block;
2053 end_problem_latch(ctx, PR_LATCH_BLOCK);
2054 end_problem_latch(ctx, PR_LATCH_TOOBIG);
2055 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2058 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
2060 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
2061 if (LINUX_S_ISDIR(inode->i_mode))
2062 ctx->fs_fragmented_dir++;
2064 ctx->fs_fragmented++;
2068 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2073 if (inode->i_flags & EXT2_INDEX_FL) {
2074 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
2075 inode->i_flags &= ~EXT2_INDEX_FL;
2079 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
2084 if (!pb.num_blocks && pb.is_dir) {
2085 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
2086 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
2087 ctx->fs_directory_count--;
2092 if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
2093 quota_data_add(ctx->qctx, inode, ino,
2094 pb.num_blocks * fs->blocksize);
2095 quota_data_inodes(ctx->qctx, inode, ino, +1);
2098 if (!(fs->super->s_feature_ro_compat &
2099 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
2100 !(inode->i_flags & EXT4_HUGE_FILE_FL))
2101 pb.num_blocks *= (fs->blocksize / 512);
2102 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
2104 printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
2105 ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
2109 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
2110 if (inode->i_size & (fs->blocksize - 1))
2112 else if (nblock > (pb.last_block + 1))
2114 else if (nblock < (pb.last_block + 1)) {
2115 if (((pb.last_block + 1) - nblock) >
2116 fs->super->s_prealloc_dir_blocks)
2120 e2_blkcnt_t blkpg = ctx->blocks_per_page;
2122 size = EXT2_I_SIZE(inode);
2123 if ((pb.last_init_lblock >= 0) &&
2124 /* allow allocated blocks to end of PAGE_SIZE */
2125 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
2126 (pb.last_init_lblock / blkpg * blkpg != pb.last_init_lblock ||
2127 size < (__u64)(pb.last_init_lblock & ~(blkpg-1)) *
2130 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2131 size > ext2_max_sizes[fs->super->s_log_block_size])
2132 /* too big for a direct/indirect-mapped file */
2134 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2136 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
2137 /* too big for an extent-based file - 32bit ee_block */
2140 /* i_size for symlinks is checked elsewhere */
2141 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
2142 pctx->num = (pb.last_block+1) * fs->blocksize;
2143 pctx->group = bad_size;
2144 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
2145 inode->i_size = pctx->num;
2146 if (!LINUX_S_ISDIR(inode->i_mode))
2147 inode->i_size_high = pctx->num >> 32;
2152 if (LINUX_S_ISREG(inode->i_mode) && EXT2_I_SIZE(inode) >= 0x80000000UL)
2154 if ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
2155 ((fs->super->s_feature_ro_compat &
2156 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
2157 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
2158 (inode->osd2.linux2.l_i_blocks_hi != 0))) {
2159 pctx->num = pb.num_blocks;
2160 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
2161 inode->i_blocks = pb.num_blocks;
2162 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
2168 if (ctx->dirs_to_hash && pb.is_dir &&
2169 !(inode->i_flags & EXT2_INDEX_FL) &&
2170 ((inode->i_size / fs->blocksize) >= 3))
2171 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
2175 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
2180 * Helper function called by process block when an illegal block is
2181 * found. It returns a description about why the block is illegal
2183 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
2187 static char problem[80];
2189 super = fs->super->s_first_data_block;
2190 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
2191 if (block < super) {
2192 sprintf(problem, "< FIRSTBLOCK (%u)", super);
2194 } else if (block >= ext2fs_blocks_count(fs->super)) {
2195 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
2198 for (i = 0; i < fs->group_desc_count; i++) {
2199 if (block == super) {
2200 sprintf(problem, "is the superblock in group %d", i);
2203 if (block > super &&
2204 block <= (super + fs->desc_blocks)) {
2205 sprintf(problem, "is in the group descriptors "
2209 if (block == ext2fs_block_bitmap_loc(fs, i)) {
2210 sprintf(problem, "is the block bitmap of group %d", i);
2213 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
2214 sprintf(problem, "is the inode bitmap of group %d", i);
2217 if (block >= ext2fs_inode_table_loc(fs, i) &&
2218 (block < ext2fs_inode_table_loc(fs, i)
2219 + fs->inode_blocks_per_group)) {
2220 sprintf(problem, "is in the inode table of group %d",
2224 super += fs->super->s_blocks_per_group;
2231 * This is a helper function for check_blocks().
2233 static int process_block(ext2_filsys fs,
2235 e2_blkcnt_t blockcnt,
2236 blk64_t ref_block EXT2FS_ATTR((unused)),
2237 int ref_offset EXT2FS_ATTR((unused)),
2240 struct process_block_struct *p;
2241 struct problem_context *pctx;
2242 blk64_t blk = *block_nr;
2247 p = (struct process_block_struct *) priv_data;
2251 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
2252 /* todo: Check that the comprblk_fl is high, that the
2253 blkaddr pattern looks right (all non-holes up to
2254 first EXT2FS_COMPRESSED_BLKADDR, then all
2255 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2256 that the feature_incompat bit is high, and that the
2257 inode is a regular file. If we're doing a "full
2258 check" (a concept introduced to e2fsck by e2compr,
2259 meaning that we look at data blocks as well as
2260 metadata) then call some library routine that
2261 checks the compressed data. I'll have to think
2262 about this, because one particularly important
2263 problem to be able to fix is to recalculate the
2264 cluster size if necessary. I think that perhaps
2265 we'd better do most/all e2compr-specific checks
2266 separately, after the non-e2compr checks. If not
2267 doing a full check, it may be useful to test that
2268 the personality is linux; e.g. if it isn't then
2269 perhaps this really is just an illegal block. */
2277 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
2282 * Simplistic fragmentation check. We merely require that the
2283 * file be contiguous. (Which can never be true for really
2284 * big files that are greater than a block group.)
2286 if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
2287 if (p->previous_block+1 != blk) {
2288 if (ctx->options & E2F_OPT_FRAGCHECK) {
2296 printf(_("%6lu(%c): expecting %6lu "
2297 "got phys %6lu (blkcnt %lld)\n"),
2298 (unsigned long) pctx->ino, type,
2299 (unsigned long) p->previous_block+1,
2300 (unsigned long) blk,
2307 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
2308 problem = PR_1_TOOBIG_DIR;
2309 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
2310 problem = PR_1_TOOBIG_REG;
2311 if (!p->is_dir && !p->is_reg && blockcnt > 0)
2312 problem = PR_1_TOOBIG_SYMLINK;
2314 if (blk < fs->super->s_first_data_block ||
2315 blk >= ext2fs_blocks_count(fs->super))
2316 problem = PR_1_ILLEGAL_BLOCK_NUM;
2319 p->num_illegal_blocks++;
2320 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
2321 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
2325 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
2327 set_latch_flags(PR_LATCH_BLOCK,
2332 pctx->blkcount = blockcnt;
2333 if (fix_problem(ctx, problem, pctx)) {
2334 blk = *block_nr = 0;
2335 ret_code = BLOCK_CHANGED;
2341 if (p->ino == EXT2_RESIZE_INO) {
2343 * The resize inode has already be sanity checked
2344 * during pass #0 (the superblock checks). All we
2345 * have to do is mark the double indirect block as
2346 * being in use; all of the other blocks are handled
2347 * by mark_table_blocks()).
2349 if (blockcnt == BLOCK_COUNT_DIND)
2350 mark_block_used(ctx, blk);
2352 } else if (!(ctx->fs->cluster_ratio_bits &&
2353 p->previous_block &&
2354 (EXT2FS_B2C(ctx->fs, blk) ==
2355 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
2356 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2357 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2358 mark_block_used(ctx, blk);
2362 p->last_block = blockcnt;
2363 p->previous_block = blk;
2365 if (p->is_dir && (blockcnt >= 0)) {
2366 while (++p->last_db_block < blockcnt) {
2367 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
2370 if (pctx->errcode) {
2372 pctx->num = p->last_db_block;
2373 goto failed_add_dir_block;
2376 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
2378 if (pctx->errcode) {
2380 pctx->num = blockcnt;
2381 failed_add_dir_block:
2382 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2383 /* Should never get here */
2384 ctx->flags |= E2F_FLAG_ABORT;
2391 static int process_bad_block(ext2_filsys fs,
2393 e2_blkcnt_t blockcnt,
2394 blk64_t ref_block EXT2FS_ATTR((unused)),
2395 int ref_offset EXT2FS_ATTR((unused)),
2398 struct process_block_struct *p;
2399 blk64_t blk = *block_nr;
2400 blk64_t first_block;
2402 struct problem_context *pctx;
2406 * Note: This function processes blocks for the bad blocks
2407 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
2413 p = (struct process_block_struct *) priv_data;
2417 pctx->ino = EXT2_BAD_INO;
2419 pctx->blkcount = blockcnt;
2421 if ((blk < fs->super->s_first_data_block) ||
2422 (blk >= ext2fs_blocks_count(fs->super))) {
2423 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
2425 return BLOCK_CHANGED;
2431 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
2433 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
2435 return BLOCK_CHANGED;
2437 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2440 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
2443 return BLOCK_CHANGED;
2445 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2448 mark_block_used(ctx, blk);
2452 printf ("DEBUG: Marking %u as bad.\n", blk);
2454 ctx->fs_badblocks_count++;
2456 * If the block is not used, then mark it as used and return.
2457 * If it is already marked as found, this must mean that
2458 * there's an overlap between the filesystem table blocks
2459 * (bitmaps and inode table) and the bad block list.
2461 if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
2462 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
2466 * Try to find the where the filesystem block was used...
2468 first_block = fs->super->s_first_data_block;
2470 for (i = 0; i < fs->group_desc_count; i++ ) {
2473 if (!ext2fs_bg_has_super(fs, i))
2475 if (blk == first_block) {
2477 if (fix_problem(ctx,
2478 PR_1_BAD_PRIMARY_SUPERBLOCK,
2481 return BLOCK_CHANGED;
2485 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
2488 if ((blk > first_block) &&
2489 (blk <= first_block + fs->desc_blocks)) {
2491 pctx->blk = *block_nr;
2492 if (fix_problem(ctx,
2493 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
2495 return BLOCK_CHANGED;
2499 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
2503 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
2504 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
2505 ctx->invalid_block_bitmap_flag[i]++;
2506 ctx->invalid_bitmaps++;
2510 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
2511 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
2512 ctx->invalid_inode_bitmap_flag[i]++;
2513 ctx->invalid_bitmaps++;
2517 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
2518 (blk < (ext2fs_inode_table_loc(fs, i) +
2519 fs->inode_blocks_per_group))) {
2521 * If there are bad blocks in the inode table,
2522 * the inode scan code will try to do
2523 * something reasonable automatically.
2527 first_block += fs->super->s_blocks_per_group;
2530 * If we've gotten to this point, then the only
2531 * possibility is that the bad block inode meta data
2532 * is using a bad block.
2534 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
2535 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
2536 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
2538 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
2540 return BLOCK_CHANGED;
2542 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2549 /* Warn user that the block wasn't claimed */
2550 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
2555 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
2556 const char *name, int num, blk64_t *new_block)
2558 ext2_filsys fs = ctx->fs;
2560 blk64_t old_block = *new_block;
2562 int i, is_flexbg, flexbg, flexbg_size;
2564 struct problem_context pctx;
2566 clear_problem_context(&pctx);
2569 pctx.blk = old_block;
2573 * For flex_bg filesystems, first try to allocate the metadata
2574 * within the flex_bg, and if that fails then try finding the
2575 * space anywhere in the filesystem.
2577 is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2578 EXT4_FEATURE_INCOMPAT_FLEX_BG);
2580 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2581 flexbg = group / flexbg_size;
2582 first_block = ext2fs_group_first_block2(fs,
2583 flexbg_size * flexbg);
2584 last_grp = group | (flexbg_size - 1);
2585 if (last_grp > fs->group_desc_count)
2586 last_grp = fs->group_desc_count;
2587 last_block = ext2fs_group_last_block2(fs, last_grp);
2589 last_block = ext2fs_group_last_block2(fs, group);
2590 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
2591 num, ctx->block_found_map,
2593 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
2594 pctx.errcode = ext2fs_get_free_blocks2(fs,
2595 fs->super->s_first_data_block,
2596 ext2fs_blocks_count(fs->super),
2597 num, ctx->block_found_map, new_block);
2600 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
2601 ext2fs_unmark_valid(fs);
2602 ctx->flags |= E2F_FLAG_ABORT;
2605 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
2607 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
2608 ext2fs_unmark_valid(fs);
2609 ctx->flags |= E2F_FLAG_ABORT;
2612 ext2fs_mark_super_dirty(fs);
2613 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2614 pctx.blk2 = *new_block;
2615 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
2616 PR_1_RELOC_TO), &pctx);
2618 for (i = 0; i < num; i++) {
2620 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
2622 pctx.errcode = io_channel_read_blk64(fs->io,
2623 old_block + i, 1, buf);
2625 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
2627 memset(buf, 0, fs->blocksize);
2629 pctx.blk = (*new_block) + i;
2630 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
2633 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
2635 ext2fs_free_mem(&buf);
2639 * This routine gets called at the end of pass 1 if bad blocks are
2640 * detected in the superblock, group descriptors, inode_bitmaps, or
2641 * block bitmaps. At this point, all of the blocks have been mapped
2642 * out, so we can try to allocate new block(s) to replace the bad
2645 static void handle_fs_bad_blocks(e2fsck_t ctx)
2647 ext2_filsys fs = ctx->fs;
2649 blk64_t first_block;
2652 for (i = 0; i < fs->group_desc_count; i++) {
2653 first_block = ext2fs_group_first_block2(fs, i);
2655 if (ctx->invalid_block_bitmap_flag[i]) {
2656 new_blk = ext2fs_block_bitmap_loc(fs, i);
2657 new_table_block(ctx, first_block, i, _("block bitmap"),
2659 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
2661 if (ctx->invalid_inode_bitmap_flag[i]) {
2662 new_blk = ext2fs_inode_bitmap_loc(fs, i);
2663 new_table_block(ctx, first_block, i, _("inode bitmap"),
2665 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
2667 if (ctx->invalid_inode_table_flag[i]) {
2668 new_blk = ext2fs_inode_table_loc(fs, i);
2669 new_table_block(ctx, first_block, i, _("inode table"),
2670 fs->inode_blocks_per_group,
2672 ext2fs_inode_table_loc_set(fs, i, new_blk);
2673 ctx->flags |= E2F_FLAG_RESTART;
2676 ctx->invalid_bitmaps = 0;
2680 * This routine marks all blocks which are used by the superblock,
2681 * group descriptors, inode bitmaps, and block bitmaps.
2683 static void mark_table_blocks(e2fsck_t ctx)
2685 ext2_filsys fs = ctx->fs;
2689 struct problem_context pctx;
2691 clear_problem_context(&pctx);
2693 for (i = 0; i < fs->group_desc_count; i++) {
2696 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
2699 * Mark the blocks used for the inode table
2701 if (ext2fs_inode_table_loc(fs, i)) {
2702 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
2703 j < fs->inode_blocks_per_group;
2705 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2708 if (!ctx->invalid_inode_table_flag[i] &&
2710 PR_1_ITABLE_CONFLICT, &pctx)) {
2711 ctx->invalid_inode_table_flag[i]++;
2712 ctx->invalid_bitmaps++;
2715 ext2fs_mark_block_bitmap2(ctx->block_found_map,
2722 * Mark block used for the block bitmap
2724 if (ext2fs_block_bitmap_loc(fs, i)) {
2725 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2726 ext2fs_block_bitmap_loc(fs, i))) {
2727 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
2728 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
2729 ctx->invalid_block_bitmap_flag[i]++;
2730 ctx->invalid_bitmaps++;
2733 ext2fs_mark_block_bitmap2(ctx->block_found_map,
2734 ext2fs_block_bitmap_loc(fs, i));
2739 * Mark block used for the inode bitmap
2741 if (ext2fs_inode_bitmap_loc(fs, i)) {
2742 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2743 ext2fs_inode_bitmap_loc(fs, i))) {
2744 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
2745 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
2746 ctx->invalid_inode_bitmap_flag[i]++;
2747 ctx->invalid_bitmaps++;
2750 ext2fs_mark_block_bitmap2(ctx->block_found_map,
2751 ext2fs_inode_bitmap_loc(fs, i));
2758 * Thes subroutines short circuits ext2fs_get_blocks and
2759 * ext2fs_check_directory; we use them since we already have the inode
2760 * structure, so there's no point in letting the ext2fs library read
2763 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
2766 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2769 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2770 return EXT2_ET_CALLBACK_NOTHANDLED;
2772 for (i=0; i < EXT2_N_BLOCKS; i++)
2773 blocks[i] = ctx->stashed_inode->i_block[i];
2777 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
2778 struct ext2_inode *inode)
2780 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2782 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2783 return EXT2_ET_CALLBACK_NOTHANDLED;
2784 *inode = *ctx->stashed_inode;
2788 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
2789 struct ext2_inode *inode)
2791 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2793 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
2794 (inode != ctx->stashed_inode))
2795 *ctx->stashed_inode = *inode;
2796 return EXT2_ET_CALLBACK_NOTHANDLED;
2799 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
2801 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2803 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
2804 return EXT2_ET_CALLBACK_NOTHANDLED;
2806 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
2807 return EXT2_ET_NO_DIRECTORY;
2811 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
2814 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2818 if (ctx->block_found_map) {
2819 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
2823 if (fs->block_map) {
2824 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
2825 ext2fs_mark_bb_dirty(fs);
2828 if (!fs->block_map) {
2829 retval = ext2fs_read_block_bitmap(fs);
2834 retval = ext2fs_new_block2(fs, goal, 0, &new_block);
2843 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
2845 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
2847 if (ctx->block_found_map) {
2849 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
2851 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
2855 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
2857 ext2_filsys fs = ctx->fs;
2859 if (use_shortcuts) {
2860 fs->get_blocks = pass1_get_blocks;
2861 fs->check_directory = pass1_check_directory;
2862 fs->read_inode = pass1_read_inode;
2863 fs->write_inode = pass1_write_inode;
2864 ctx->stashed_ino = 0;
2865 ext2fs_set_alloc_block_callback(fs, e2fsck_get_alloc_block,
2867 ext2fs_set_block_alloc_stats_callback(fs,
2868 e2fsck_block_alloc_stats,
2872 fs->check_directory = 0;
2874 fs->write_inode = 0;