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 * - An icount mechanism is used to keep track of
24 * inodes with bad fields and its badness (ctx->inode_badness)
25 * - A bitmap of which inodes are in bad blocks. (inode_bb_map)
26 * - A bitmap of which inodes are imagic inodes. (inode_imagic_map)
27 * - A bitmap of which inodes need to be expanded (expand_eisize_map)
28 * - A bitmap of which blocks are in use. (block_found_map)
29 * - A bitmap of which blocks are in use by two inodes (block_dup_map)
30 * - The data blocks of the directory inodes. (dir_map)
31 * - A bitmap of EA inodes. (inode_ea_map)
33 * Pass 1 is designed to stash away enough information so that the
34 * other passes should not need to read in the inode information
35 * during the normal course of a filesystem check. (Althogh if an
36 * inconsistency is detected, other passes may need to read in an
39 * Note that pass 1B will be invoked if there are any duplicate blocks
43 #define _GNU_SOURCE 1 /* get strnlen() */
52 #include <ext2fs/ext2_ext_attr.h>
56 #ifdef NO_INLINE_FUNCS
59 #define _INLINE_ inline
62 static int process_block(ext2_filsys fs, blk64_t *blocknr,
63 e2_blkcnt_t blockcnt, blk64_t ref_blk,
64 int ref_offset, void *priv_data);
65 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
66 e2_blkcnt_t blockcnt, blk64_t ref_blk,
67 int ref_offset, void *priv_data);
68 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
70 static void mark_table_blocks(e2fsck_t ctx);
71 static void alloc_bb_map(e2fsck_t ctx);
72 static void alloc_imagic_map(e2fsck_t ctx);
73 static void handle_fs_bad_blocks(e2fsck_t ctx);
74 static void process_inodes(e2fsck_t ctx, char *block_buf);
75 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
76 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
77 dgrp_t group, void * priv_data);
78 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
79 char *block_buf, int adjust_sign);
80 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
82 struct process_block_struct {
84 unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
85 fragmented:1, compressed:1, bbcheck:1,
89 e2_blkcnt_t last_block;
90 e2_blkcnt_t last_init_lblock;
91 e2_blkcnt_t last_db_block;
92 int num_illegal_blocks;
93 blk64_t previous_block;
94 struct ext2_inode *inode;
95 struct problem_context *pctx;
96 ext2fs_block_bitmap fs_meta_blocks;
100 struct process_inode_block {
102 struct ext2_inode inode;
105 struct scan_callback_struct {
111 * For the inodes to process list.
113 static struct process_inode_block *inodes_to_process;
114 static int process_inode_count;
116 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
117 EXT2_MIN_BLOCK_LOG_SIZE + 1];
120 * Free all memory allocated by pass1 in preparation for restarting
123 static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
125 ext2fs_free_mem(&inodes_to_process);
126 inodes_to_process = 0;
130 * Check to make sure a device inode is real. Returns 1 if the device
131 * checks out, 0 if not.
133 * Note: this routine is now also used to check FIFO's and Sockets,
134 * since they have the same requirement; the i_block fields should be
137 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
138 struct ext2_inode *inode)
143 * If the index flag is set, then this is a bogus
146 if (inode->i_flags & (EXT2_INDEX_FL | EXT4_EXTENTS_FL))
150 * We should be able to do the test below all the time, but
151 * because the kernel doesn't forcibly clear the device
152 * inode's additional i_block fields, there are some rare
153 * occasions when a legitimate device inode will have non-zero
154 * additional i_block fields. So for now, we only complain
155 * when the immutable flag is set, which should never happen
156 * for devices. (And that's when the problem is caused, since
157 * you can't set or clear immutable flags for devices.) Once
158 * the kernel has been fixed we can change this...
160 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
161 for (i=4; i < EXT2_N_BLOCKS; i++)
162 if (inode->i_block[i])
169 * Check to make sure a symlink inode is real. Returns 1 if the symlink
170 * checks out, 0 if not.
172 static int check_symlink(e2fsck_t ctx, struct problem_context *pctx,
173 ext2_ino_t ino, struct ext2_inode *inode, char *buf)
175 blk64_t block, num_blocks;
178 if ((inode->i_size_high || inode->i_size == 0) ||
179 (inode->i_flags & EXT2_INDEX_FL))
182 num_blocks = ext2fs_inode_data_blocks2(ctx->fs, inode);
183 if (inode->i_flags & EXT4_EXTENTS_FL) { /* in extent-mapped block */
184 struct ext2_extent_info info;
185 ext2_extent_handle_t handle;
186 struct ext2fs_extent extent;
188 if (inode->i_size > ctx->fs->blocksize)
190 if (ext2fs_extent_open2(ctx->fs, ino, inode, &handle))
192 if (ext2fs_extent_get_info(handle, &info) ||
193 (info.num_entries != 1) ||
194 (info.max_depth != 0) ||
196 ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
197 (extent.e_lblk != 0) ||
198 (extent.e_len != 1)) {
199 ext2fs_extent_free(handle);
203 block = extent.e_pblk;
204 limit = ctx->fs->blocksize;
206 ext2fs_extent_free(handle);
207 } else if (num_blocks > 0) { /* in block-mapped block */
210 block = inode->i_block[0];
211 limit = ctx->fs->blocksize;
213 for (i = 1; i < EXT2_N_BLOCKS; i++)
214 if (inode->i_block[i])
216 } else { /* inside inode i_block[] */
218 limit = sizeof(inode->i_block);
221 if (inode->i_size >= limit)
225 if ((num_blocks != ctx->fs->blocksize >> 9) || /* == 1 block */
226 (block < ctx->fs->super->s_first_data_block) ||
227 (block >= ext2fs_blocks_count(ctx->fs->super)))
230 if (io_channel_read_blk64(ctx->fs->io, block, 1, buf))
233 buf = (char *)inode->i_block;
236 len = strnlen(buf, limit);
237 /* Add missing NUL terminator at end of symlink (LU-1540),
238 * but only offer to fix this in pass1, not from pass2. */
239 if (len > inode->i_size && pctx != NULL &&
240 fix_problem(ctx, PR_1_SYMLINK_NUL, pctx)) {
241 buf[inode->i_size] = '\0';
242 if (num_blocks != 0) {
243 if (io_channel_write_blk64(ctx->fs->io, block, 1, buf))
246 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
250 if (len != inode->i_size)
256 int e2fsck_pass1_check_symlink(e2fsck_t ctx, ext2_ino_t ino,
257 struct ext2_inode *inode, char *buf)
259 return check_symlink(ctx, NULL, ino, inode, buf);
263 * If the immutable (or append-only) flag is set on the inode, offer
266 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
267 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
269 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
272 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
273 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
276 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
277 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
281 * If device, fifo or socket, check size is zero -- if not offer to
284 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
286 struct ext2_inode *inode = pctx->inode;
288 if (EXT2_I_SIZE(inode) == 0)
291 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
292 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
295 ext2fs_inode_size_set(ctx->fs, inode, 0);
296 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
299 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
301 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
303 if (ctx->block_found_map) {
305 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
307 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
311 static void mark_inode_ea_map(e2fsck_t ctx, struct problem_context *pctx,
314 if (!ctx->inode_ea_map) {
315 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
319 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
325 ext2fs_mark_inode_bitmap2(ctx->inode_ea_map, ino);
329 * Delete an EA entry. If this is the last entry to be deleted, then i_file_acl
330 * must have been freed, so we must update e2fsck block statistics and set
331 * i_file_acl_deleted.
332 * When we delete the entry successfully, this function returns 0, else
336 static int e2fsck_ea_entry_delete(e2fsck_t ctx,
337 struct ext2_ext_attr_entry *entry,
338 struct problem_context *pctx,
339 int *i_file_acl_deleted, problem_t prob)
341 blk_t i_file_acl = pctx->inode->i_file_acl;
344 pctx->num = entry->e_value_inum;
346 if (fix_problem(ctx, prob, pctx)) {
347 /* Delete corrupt EA entry */
348 err = ext2fs_attr_set(ctx->fs, pctx->ino, pctx->inode,
349 entry->e_name_index, entry->e_name,
352 if (i_file_acl && pctx->inode->i_file_acl == 0) {
353 e2fsck_block_alloc_stats(ctx->fs, i_file_acl,
355 *i_file_acl_deleted = 1;
365 * Check validity of EA inode. Return 0 if EA inode is valid, nonzero otherwise.
367 static int check_large_ea_inode(e2fsck_t ctx, struct ext2_ext_attr_entry *entry,
368 struct problem_context *pctx,
369 int *i_file_acl_deleted)
371 struct ext2_inode inode;
374 /* Check if inode is within valid range */
375 if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
376 (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
377 ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
379 PR_1_ATTR_VALUE_EA_INODE);
380 /* If user refuses to delete this entry, caller may try to set
381 * the bit for this out-of-bound inode in inode_ea_map, so
382 * always return failure */
386 e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
387 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
388 /* If EXT4_EA_INODE_FL flag is not present but back-pointer
389 * matches then we should set this flag */
390 if (inode.i_mtime == pctx->ino &&
391 inode.i_generation == pctx->inode->i_generation &&
392 fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
393 inode.i_flags |= EXT4_EA_INODE_FL;
394 ext2fs_write_inode(ctx->fs, entry->e_value_inum,&inode);
396 ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
398 PR_1_ATTR_NO_EA_INODE_FL);
401 } else if (inode.i_mtime != pctx->ino ||
402 inode.i_generation != pctx->inode->i_generation) {
403 ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
405 PR_1_ATTR_INVAL_EA_INODE);
413 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
415 struct ext2_super_block *sb = ctx->fs->super;
416 struct ext2_inode_large *inode;
417 struct ext2_ext_attr_entry *entry;
419 unsigned int storage_size, remain;
420 problem_t problem = 0;
422 inode = (struct ext2_inode_large *) pctx->inode;
423 storage_size = EXT2_INODE_SIZE(ctx->fs->super) -
424 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize;
425 entry = &IHDR(inode)->h_first_entry[0];
426 start = (char *)entry;
428 /* scan all entry's headers first */
430 /* take finish entry 0UL into account */
431 remain = storage_size - sizeof(__u32);
433 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
436 /* header eats this space */
437 remain -= sizeof(struct ext2_ext_attr_entry);
439 /* is attribute name valid? */
440 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
441 pctx->num = entry->e_name_len;
442 problem = PR_1_ATTR_NAME_LEN;
446 /* attribute len eats this space */
447 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
449 if (entry->e_value_inum == 0) {
450 /* check value size */
451 if (entry->e_value_size > remain) {
452 pctx->num = entry->e_value_size;
453 problem = PR_1_ATTR_VALUE_SIZE;
459 ret = check_large_ea_inode(ctx, entry, pctx, &tmp);
461 mark_inode_ea_map(ctx, pctx,
462 entry->e_value_inum);
465 /* Value size cannot be larger than EA space in inode */
466 if (entry->e_value_offs > storage_size ||
467 (entry->e_value_inum == 0 &&
468 entry->e_value_offs + entry->e_value_size > storage_size)) {
469 problem = PR_1_INODE_EA_BAD_VALUE;
473 hash = ext2fs_ext_attr_hash_entry(entry,
474 start + entry->e_value_offs);
476 /* e_hash may be 0 in older inode's ea */
477 if (entry->e_hash != 0 && entry->e_hash != hash) {
478 pctx->num = entry->e_hash;
479 problem = PR_1_ATTR_HASH;
483 /* If EA value is stored in external inode then it does not
484 * consume space here */
485 if (entry->e_value_inum == 0)
486 remain -= entry->e_value_size;
488 entry = EXT2_EXT_ATTR_NEXT(entry);
492 * it seems like a corruption. it's very unlikely we could repair
493 * EA(s) in automatic fashion -bzzz
495 if (problem == 0 || !fix_problem(ctx, problem, pctx))
498 /* simply remove all possible EA(s) */
499 *((__u32 *)start) = 0UL;
500 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
501 EXT2_INODE_SIZE(sb), "pass1");
504 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
506 struct ext2_super_block *sb = ctx->fs->super;
507 struct ext2_inode_large *inode;
509 int min, max, dirty = 0;
511 inode = (struct ext2_inode_large *) pctx->inode;
512 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
513 /* this isn't large inode. so, nothing to check */
518 printf("inode #%u, i_extra_size %d\n", pctx->ino,
519 inode->i_extra_isize);
521 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
522 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
523 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
525 * For now we will allow i_extra_isize to be 0, but really
526 * implementations should never allow i_extra_isize to be 0
528 if (inode->i_extra_isize &&
529 (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
530 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
531 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
533 inode->i_extra_isize = ctx->want_extra_isize;
539 if (EXT4_FITS_IN_INODE(inode, inode, i_crtime) &&
540 inode->i_crtime != 0 &&
541 (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, 2*ctx->time_fudge) ||
542 EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, 2*ctx->time_fudge))) {
543 pctx->num = inode->i_crtime;
544 if (fix_problem(ctx, PR_1_CRTIME_BAD, pctx)) {
548 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_HIGH);
551 eamagic = &IHDR(inode)->h_magic;
552 if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
553 (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
554 (inode->i_extra_isize < ctx->want_extra_isize)) {
555 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
556 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
557 EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
558 inode->i_extra_isize = ctx->want_extra_isize;
560 if (inode->i_extra_isize < ctx->min_extra_isize)
561 ctx->min_extra_isize = inode->i_extra_isize;
564 if (*eamagic == EXT2_EXT_ATTR_MAGIC)
565 check_ea_in_inode(ctx, pctx);
568 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
569 EXT2_INODE_SIZE(sb), "pass1");
573 * Check to see if the inode might really be a directory, despite i_mode
575 * This is a lot of complexity for something for which I'm not really
576 * convinced happens frequently in the wild. If for any reason this
577 * causes any problems, take this code out.
578 * [tytso:20070331.0827EDT]
580 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
583 struct ext2_inode *inode = pctx->inode;
584 struct ext2_dir_entry *dirent;
587 unsigned int i, rec_len, not_device = 0;
591 * If the mode looks OK, we believe it. If the first block in
592 * the i_block array is 0, this cannot be a directory. If the
593 * inode is extent-mapped, it is still the case that the latter
594 * cannot be 0 - the magic number in the extent header would make
597 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
598 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
602 * Check the block numbers in the i_block array for validity:
603 * zero blocks are skipped (but the first one cannot be zero -
604 * see above), other blocks are checked against the first and
605 * max data blocks (from the the superblock) and against the
606 * block bitmap. Any invalid block found means this cannot be
609 * If there are non-zero blocks past the fourth entry, then
610 * this cannot be a device file: we remember that for the next
613 * For extent mapped files, we don't do any sanity checking:
614 * just try to get the phys block of logical block 0 and run
618 extent_fs = (ctx->fs->super->s_feature_incompat &
619 EXT3_FEATURE_INCOMPAT_EXTENTS);
620 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
622 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
625 /* device files are never extent mapped */
628 for (i=0; i < EXT2_N_BLOCKS; i++) {
629 blk = inode->i_block[i];
635 if (blk < ctx->fs->super->s_first_data_block ||
636 blk >= ext2fs_blocks_count(ctx->fs->super) ||
637 ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
639 return; /* Invalid block, can't be dir */
641 blk = inode->i_block[0];
645 * If the mode says this is a device file and the i_links_count field
646 * is sane and we have not ruled it out as a device file previously,
647 * we declare it a device file, not a directory.
649 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
650 (inode->i_links_count == 1) && !not_device)
653 /* read the first block */
654 ehandler_operation(_("reading directory block"));
655 retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0);
656 ehandler_operation(0);
660 dirent = (struct ext2_dir_entry *) buf;
661 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
664 if (((dirent->name_len & 0xFF) != 1) ||
665 (dirent->name[0] != '.') ||
666 (dirent->inode != pctx->ino) ||
669 (rec_len >= ctx->fs->blocksize - 12))
672 dirent = (struct ext2_dir_entry *) (buf + rec_len);
673 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
676 if (((dirent->name_len & 0xFF) != 2) ||
677 (dirent->name[0] != '.') ||
678 (dirent->name[1] != '.') ||
683 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
684 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
685 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
686 e2fsck_write_inode_full(ctx, pctx->ino, inode,
687 EXT2_INODE_SIZE(ctx->fs->super),
688 "check_is_really_dir");
692 void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
695 unsigned int threshold;
703 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
705 profile_get_uint(ctx->profile, "scratch_files",
706 "numdirs_threshold", 0, 0, &threshold);
707 profile_get_boolean(ctx->profile, "scratch_files",
708 "icount", 0, 1, &enable);
710 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
712 num_dirs = 1024; /* Guess */
714 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
715 (threshold && num_dirs <= threshold))
718 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
723 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
724 struct problem_context *pctx, int needed_size)
726 struct ext2_ext_attr_header *header;
727 struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
728 char *start, name[4096], block_buf[4096];
729 int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
730 int in_inode = 1, error;
731 unsigned int freed_bytes = inode->i_extra_isize;
733 entry_ino = &IHDR(inode)->h_first_entry[0];
734 start = (char *)entry_ino;
736 if (inode->i_file_acl) {
737 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
739 /* We have already checked this block, shouldn't happen */
741 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
744 header = BHDR(block_buf);
745 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
746 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
750 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
753 len = sizeof(entry->e_name);
754 entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
756 while (freed_bytes < needed_size) {
757 if (entry_size && name[0] != '\0') {
759 if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
760 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
761 EXT2_EXT_ATTR_SIZE(entry->e_value_size);
762 error = ext2fs_attr_set(ctx->fs, pctx->ino,
763 (struct ext2_inode *)inode,
764 index, name, 0, 0, 0);
766 freed_bytes += ea_size;
769 len = sizeof(entry->e_name);
770 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
771 entry = EXT2_EXT_ATTR_NEXT(entry);
772 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
775 len = sizeof(entry->e_name);
776 entry_size = ext2fs_attr_get_next_attr(entry,
777 index, name, len, 1);
782 if (!entry && index < EXT2_ATTR_INDEX_MAX)
783 entry = (struct ext2_ext_attr_entry *)start;
793 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
794 struct problem_context *pctx)
796 int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
800 retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
801 ctx->want_extra_isize, &ret,
803 if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
804 goto mark_expand_eisize_map;
806 e2fsck_write_inode_full(ctx, pctx->ino,
807 (struct ext2_inode *)inode,
808 EXT2_INODE_SIZE(ctx->fs->super),
813 if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
814 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
815 fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
816 ctx->flags |= E2F_FLAG_ABORT;
821 pctx->num = ctx->fs->super->s_min_extra_isize;
822 fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
826 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
828 if (retval >= ctx->want_extra_isize)
831 needed_size -= retval;
834 * We loop here until either the user deletes EA(s) or
835 * EXTRA_ISIZE feature is disabled.
837 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
838 ctx->fs->super->s_feature_ro_compat &=
839 ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
840 ext2fs_mark_super_dirty(ctx->fs);
844 ctx->fs_unexpanded_inodes++;
846 /* No EA was deleted, inode cannot be expanded */
850 mark_expand_eisize_map:
851 if (!ctx->expand_eisize_map) {
852 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
853 _("expand extrz isize map"),
854 &ctx->expand_eisize_map);
856 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
862 /* Add this inode to the expand_eisize_map */
863 ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
867 static void reserve_block_for_root_repair(e2fsck_t ctx)
871 ext2_filsys fs = ctx->fs;
873 ctx->root_repair_block = 0;
874 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
877 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
880 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
881 ctx->root_repair_block = blk;
884 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
888 ext2_filsys fs = ctx->fs;
889 static const char name[] = "lost+found";
892 ctx->lnf_repair_block = 0;
893 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
896 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
899 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
900 ctx->lnf_repair_block = blk;
903 void e2fsck_pass1(e2fsck_t ctx)
907 ext2_filsys fs = ctx->fs;
909 struct ext2_inode *inode = NULL;
910 ext2_inode_scan scan = NULL;
911 char *block_buf = NULL;
912 #ifdef RESOURCE_TRACK
913 struct resource_track rtrack;
915 unsigned char frag, fsize;
916 struct problem_context pctx;
917 struct scan_callback_struct scan_struct;
918 struct ext2_super_block *sb = ctx->fs->super;
920 unsigned int save_type;
921 int imagic_fs, extent_fs;
922 int low_dtime_check = 1;
927 init_resource_track(&rtrack, ctx->fs->io);
928 clear_problem_context(&pctx);
930 if (!(ctx->options & E2F_OPT_PREEN))
931 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
933 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
934 !(ctx->options & E2F_OPT_NO)) {
935 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
936 ctx->dirs_to_hash = 0;
940 mtrace_print("Pass 1");
943 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
945 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
946 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
947 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
948 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
949 max_sizes = (max_sizes * (1UL << i));
950 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
954 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
955 extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
958 * Allocate bitmaps structures
960 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
961 EXT2FS_BMAP64_RBTREE,
963 &ctx->inode_used_map);
966 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
967 ctx->flags |= E2F_FLAG_ABORT;
970 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
971 _("directory inode map"),
972 EXT2FS_BMAP64_AUTODIR,
973 "inode_dir_map", &ctx->inode_dir_map);
976 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
977 ctx->flags |= E2F_FLAG_ABORT;
980 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
981 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
982 "inode_reg_map", &ctx->inode_reg_map);
985 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
986 ctx->flags |= E2F_FLAG_ABORT;
989 pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
990 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
991 "block_found_map", &ctx->block_found_map);
994 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
995 ctx->flags |= E2F_FLAG_ABORT;
998 e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
999 if (!ctx->inode_link_info) {
1000 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
1001 "inode_link_info", &save_type);
1002 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
1003 &ctx->inode_link_info);
1004 fs->default_bitmap_type = save_type;
1008 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1009 ctx->flags |= E2F_FLAG_ABORT;
1012 inode_size = EXT2_INODE_SIZE(fs->super);
1013 inode = (struct ext2_inode *)
1014 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
1016 inodes_to_process = (struct process_inode_block *)
1017 e2fsck_allocate_memory(ctx,
1018 (ctx->process_inode_size *
1019 sizeof(struct process_inode_block)),
1020 "array of inodes to process");
1021 process_inode_count = 0;
1023 pctx.errcode = ext2fs_init_dblist(fs, 0);
1025 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1026 ctx->flags |= E2F_FLAG_ABORT;
1031 * If the last orphan field is set, clear it, since the pass1
1032 * processing will automatically find and clear the orphans.
1033 * In the future, we may want to try using the last_orphan
1034 * linked list ourselves, but for now, we clear it so that the
1035 * ext3 mount code won't get confused.
1037 if (!(ctx->options & E2F_OPT_READONLY)) {
1038 if (fs->super->s_last_orphan) {
1039 fs->super->s_last_orphan = 0;
1040 ext2fs_mark_super_dirty(fs);
1044 mark_table_blocks(ctx);
1045 pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
1046 &ctx->block_found_map);
1048 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1049 ctx->flags |= E2F_FLAG_ABORT;
1052 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1053 "block interate buffer");
1054 e2fsck_use_inode_shortcuts(ctx, 1);
1055 e2fsck_intercept_block_allocations(ctx);
1056 old_op = ehandler_operation(_("opening inode scan"));
1057 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1059 ehandler_operation(old_op);
1061 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1062 ctx->flags |= E2F_FLAG_ABORT;
1065 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
1066 ctx->stashed_inode = inode;
1067 scan_struct.ctx = ctx;
1068 scan_struct.block_buf = block_buf;
1069 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1070 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1071 ctx->fs->group_desc_count)))
1073 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
1074 (fs->super->s_mtime < fs->super->s_inodes_count) ||
1075 (fs->super->s_mkfs_time &&
1076 fs->super->s_mkfs_time < fs->super->s_inodes_count))
1077 low_dtime_check = 0;
1079 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
1080 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1081 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1082 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1083 fs->super->s_mmp_block);
1086 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
1087 if (e2fsck_mmp_update(fs))
1088 fatal_error(ctx, 0);
1090 old_op = ehandler_operation(_("getting next inode from scan"));
1091 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1093 ehandler_operation(old_op);
1094 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1096 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1097 if (!ctx->inode_bb_map)
1099 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1100 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1104 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1105 ctx->flags |= E2F_FLAG_ABORT;
1112 ctx->stashed_ino = ino;
1113 if (inode->i_links_count) {
1114 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1115 ino, inode->i_links_count);
1117 pctx.num = inode->i_links_count;
1118 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1119 ctx->flags |= E2F_FLAG_ABORT;
1125 * Test for incorrect extent flag settings.
1127 * On big-endian machines we must be careful:
1128 * When the inode is read, the i_block array is not swapped
1129 * if the extent flag is set. Therefore if we are testing
1130 * for or fixing a wrongly-set flag, we must potentially
1131 * (un)swap before testing, or after fixing.
1135 * In this case the extents flag was set when read, so
1136 * extent_header_verify is ok. If the inode is cleared,
1137 * no need to swap... so no extra swapping here.
1139 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
1140 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1141 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1142 if ((ext2fs_extent_header_verify(inode->i_block,
1143 sizeof(inode->i_block)) == 0) &&
1144 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1145 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
1146 ext2fs_mark_super_dirty(fs);
1148 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1150 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1151 if (ino == EXT2_BAD_INO)
1152 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1159 * For big-endian machines:
1160 * If the inode didn't have the extents flag set when it
1161 * was read, then the i_blocks array was swapped. To test
1162 * as an extents header, we must swap it back first.
1163 * IF we then set the extents flag, the entire i_block
1164 * array must be un/re-swapped to make it proper extents data.
1166 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1167 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1168 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1169 (LINUX_S_ISREG(inode->i_mode) ||
1170 LINUX_S_ISDIR(inode->i_mode))) {
1172 #ifdef WORDS_BIGENDIAN
1173 __u32 tmp_block[EXT2_N_BLOCKS];
1175 for (i = 0; i < EXT2_N_BLOCKS; i++)
1176 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1179 ehp = inode->i_block;
1181 if ((ext2fs_extent_header_verify(ehp,
1182 sizeof(inode->i_block)) == 0)) {
1183 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1184 if (fix_problem(ctx, PR_1_UNSET_EXTENT_FL,
1186 inode->i_flags |= EXT4_EXTENTS_FL;
1187 #ifdef WORDS_BIGENDIAN
1188 memcpy(inode->i_block, tmp_block,
1189 sizeof(inode->i_block));
1191 e2fsck_write_inode(ctx, ino, inode,
1197 if (ino == EXT2_BAD_INO) {
1198 struct process_block_struct pb;
1200 if ((inode->i_mode || inode->i_uid || inode->i_gid ||
1201 inode->i_links_count || inode->i_file_acl) &&
1202 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1203 memset(inode, 0, sizeof(struct ext2_inode));
1204 e2fsck_write_inode(ctx, ino, inode,
1208 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1209 &pb.fs_meta_blocks);
1212 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1213 ctx->flags |= E2F_FLAG_ABORT;
1216 pb.ino = EXT2_BAD_INO;
1217 pb.num_blocks = pb.last_block = 0;
1218 pb.last_db_block = -1;
1219 pb.num_illegal_blocks = 0;
1220 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1221 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1225 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1226 block_buf, process_bad_block, &pb);
1227 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1229 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1230 ctx->flags |= E2F_FLAG_ABORT;
1234 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1235 ctx->flags |= E2F_FLAG_ABORT;
1238 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1239 clear_problem_context(&pctx);
1241 } else if (ino == EXT2_ROOT_INO) {
1243 * Make sure the root inode is a directory; if
1244 * not, offer to clear it. It will be
1245 * regnerated in pass #3.
1247 if (!LINUX_S_ISDIR(inode->i_mode)) {
1248 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1252 * If dtime is set, offer to clear it. mke2fs
1253 * version 0.2b created filesystems with the
1254 * dtime field set for the root and lost+found
1255 * directories. We won't worry about
1256 * /lost+found, since that can be regenerated
1257 * easily. But we will fix the root directory
1258 * as a special case.
1260 if (inode->i_dtime && inode->i_links_count) {
1261 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1262 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1264 e2fsck_write_inode(ctx, ino, inode,
1268 } else if (ino == EXT2_JOURNAL_INO) {
1269 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1270 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1271 if (!LINUX_S_ISREG(inode->i_mode) &&
1272 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1274 inode->i_mode = LINUX_S_IFREG;
1275 e2fsck_write_inode(ctx, ino, inode,
1278 check_blocks(ctx, &pctx, block_buf);
1281 if ((inode->i_links_count ||
1282 inode->i_blocks || inode->i_block[0]) &&
1283 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1285 memset(inode, 0, inode_size);
1286 ext2fs_icount_store(ctx->inode_link_info,
1288 e2fsck_write_inode_full(ctx, ino, inode,
1289 inode_size, "pass1");
1291 } else if ((ino == EXT4_USR_QUOTA_INO) ||
1292 (ino == EXT4_GRP_QUOTA_INO)) {
1293 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1294 if ((fs->super->s_feature_ro_compat &
1295 EXT4_FEATURE_RO_COMPAT_QUOTA) &&
1296 ((fs->super->s_usr_quota_inum == ino) ||
1297 (fs->super->s_grp_quota_inum == ino))) {
1298 if (!LINUX_S_ISREG(inode->i_mode) &&
1299 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1301 inode->i_mode = LINUX_S_IFREG;
1302 e2fsck_write_inode(ctx, ino, inode,
1305 check_blocks(ctx, &pctx, block_buf);
1308 if ((inode->i_links_count ||
1309 inode->i_blocks || inode->i_block[0]) &&
1310 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1312 memset(inode, 0, inode_size);
1313 ext2fs_icount_store(ctx->inode_link_info,
1315 e2fsck_write_inode_full(ctx, ino, inode,
1316 inode_size, "pass1");
1318 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1319 problem_t problem = 0;
1321 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1322 if (ino == EXT2_BOOT_LOADER_INO) {
1323 if (LINUX_S_ISDIR(inode->i_mode))
1324 problem = PR_1_RESERVED_BAD_MODE;
1325 } else if (ino == EXT2_RESIZE_INO) {
1326 if (inode->i_mode &&
1327 !LINUX_S_ISREG(inode->i_mode))
1328 problem = PR_1_RESERVED_BAD_MODE;
1330 if (inode->i_mode != 0)
1331 problem = PR_1_RESERVED_BAD_MODE;
1334 if (fix_problem(ctx, problem, &pctx)) {
1336 e2fsck_write_inode(ctx, ino, inode,
1340 check_blocks(ctx, &pctx, block_buf);
1345 * Check for inodes who might have been part of the
1346 * orphaned list linked list. They should have gotten
1347 * dealt with by now, unless the list had somehow been
1350 * FIXME: In the future, inodes which are still in use
1351 * (and which are therefore) pending truncation should
1352 * be handled specially. Right now we just clear the
1353 * dtime field, and the normal e2fsck handling of
1354 * inodes where i_size and the inode blocks are
1355 * inconsistent is to fix i_size, instead of releasing
1356 * the extra blocks. This won't catch the inodes that
1357 * was at the end of the orphan list, but it's better
1358 * than nothing. The right answer is that there
1359 * shouldn't be any bugs in the orphan list handling. :-)
1361 if (inode->i_dtime && low_dtime_check &&
1362 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1363 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1364 inode->i_dtime = inode->i_links_count ?
1366 e2fsck_write_inode(ctx, ino, inode,
1372 * This code assumes that deleted inodes have
1373 * i_links_count set to 0.
1375 if (!inode->i_links_count) {
1376 if (!inode->i_dtime && inode->i_mode) {
1377 if (fix_problem(ctx,
1378 PR_1_ZERO_DTIME, &pctx)) {
1379 inode->i_dtime = ctx->now;
1380 e2fsck_write_inode(ctx, ino, inode,
1387 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1388 * deleted files. Oops.
1390 * Since all new ext2 implementations get this right,
1391 * we now assume that the case of non-zero
1392 * i_links_count and non-zero dtime means that we
1393 * should keep the file, not delete it.
1396 if (inode->i_dtime) {
1397 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1398 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1400 e2fsck_write_inode(ctx, ino, inode, "pass1");
1404 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1405 switch (fs->super->s_creator_os) {
1407 frag = inode->osd2.hurd2.h_i_frag;
1408 fsize = inode->osd2.hurd2.h_i_fsize;
1414 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1415 if (inode->i_faddr || frag || fsize ||
1416 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
1417 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1418 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1419 !(fs->super->s_feature_incompat &
1420 EXT4_FEATURE_INCOMPAT_64BIT) &&
1421 inode->osd2.linux2.l_i_file_acl_high != 0)
1422 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1423 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1424 !(fs->super->s_feature_ro_compat &
1425 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1426 (inode->osd2.linux2.l_i_blocks_hi != 0))
1427 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1428 if (inode->i_flags & EXT2_IMAGIC_FL) {
1430 if (!ctx->inode_imagic_map)
1431 alloc_imagic_map(ctx);
1432 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1435 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1436 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1437 inode->i_flags &= ~EXT2_IMAGIC_FL;
1438 e2fsck_write_inode(ctx, ino,
1444 check_inode_extra_space(ctx, &pctx);
1445 check_is_really_dir(ctx, &pctx, block_buf);
1448 * ext2fs_inode_has_valid_blocks2 does not actually look
1449 * at i_block[] values, so not endian-sensitive here.
1451 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1452 LINUX_S_ISLNK(inode->i_mode) &&
1453 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1454 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1455 inode->i_flags &= ~EXT4_EXTENTS_FL;
1456 e2fsck_write_inode(ctx, ino, inode, "pass1");
1459 if (LINUX_S_ISDIR(inode->i_mode)) {
1460 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1461 e2fsck_add_dir_info(ctx, ino, 0);
1462 ctx->fs_directory_count++;
1463 } else if (LINUX_S_ISREG (inode->i_mode)) {
1464 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1465 ctx->fs_regular_count++;
1466 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1467 e2fsck_pass1_check_device_inode(fs, inode)) {
1468 check_immutable(ctx, &pctx);
1469 check_size(ctx, &pctx);
1470 ctx->fs_chardev_count++;
1471 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1472 e2fsck_pass1_check_device_inode(fs, inode)) {
1473 check_immutable(ctx, &pctx);
1474 check_size(ctx, &pctx);
1475 ctx->fs_blockdev_count++;
1476 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1477 check_symlink(ctx, &pctx, ino, inode, block_buf)) {
1478 check_immutable(ctx, &pctx);
1479 ctx->fs_symlinks_count++;
1480 if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1481 ctx->fs_fast_symlinks_count++;
1482 check_blocks(ctx, &pctx, block_buf);
1486 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1487 e2fsck_pass1_check_device_inode(fs, inode)) {
1488 check_immutable(ctx, &pctx);
1489 check_size(ctx, &pctx);
1490 ctx->fs_fifo_count++;
1491 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1492 e2fsck_pass1_check_device_inode(fs, inode)) {
1493 check_immutable(ctx, &pctx);
1494 check_size(ctx, &pctx);
1495 ctx->fs_sockets_count++;
1497 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1500 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
1501 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1502 else if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime,
1504 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1506 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
1507 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1508 else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
1510 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1512 /* i_crtime is checked in check_inode_extra_space() */
1514 if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
1515 if (inode->i_block[EXT2_IND_BLOCK])
1516 ctx->fs_ind_count++;
1517 if (inode->i_block[EXT2_DIND_BLOCK])
1518 ctx->fs_dind_count++;
1519 if (inode->i_block[EXT2_TIND_BLOCK])
1520 ctx->fs_tind_count++;
1522 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1523 (inode->i_block[EXT2_IND_BLOCK] ||
1524 inode->i_block[EXT2_DIND_BLOCK] ||
1525 inode->i_block[EXT2_TIND_BLOCK] ||
1526 ext2fs_file_acl_block(fs, inode))) {
1527 inodes_to_process[process_inode_count].ino = ino;
1528 inodes_to_process[process_inode_count].inode = *inode;
1529 process_inode_count++;
1531 check_blocks(ctx, &pctx, block_buf);
1533 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
1534 struct ext2_inode_large *inode_l;
1536 inode_l = (struct ext2_inode_large *)inode;
1538 if (inode_l->i_extra_isize < ctx->want_extra_isize) {
1539 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
1540 inode_exp = e2fsck_pass1_expand_eisize(ctx,
1544 if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
1546 ctx->min_extra_isize = inode_l->i_extra_isize;
1549 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1552 if (process_inode_count >= ctx->process_inode_size) {
1553 process_inodes(ctx, block_buf);
1555 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1559 process_inodes(ctx, block_buf);
1560 ext2fs_close_inode_scan(scan);
1563 reserve_block_for_root_repair(ctx);
1564 reserve_block_for_lnf_repair(ctx);
1567 * If any extended attribute blocks' reference counts need to
1568 * be adjusted, either up (ctx->refcount_extra), or down
1569 * (ctx->refcount), then fix them.
1571 if (ctx->refcount) {
1572 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1573 ea_refcount_free(ctx->refcount);
1576 if (ctx->refcount_extra) {
1577 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1579 ea_refcount_free(ctx->refcount_extra);
1580 ctx->refcount_extra = 0;
1583 if (ctx->invalid_bitmaps)
1584 handle_fs_bad_blocks(ctx);
1586 /* We don't need the block_ea_map any more */
1587 if (ctx->block_ea_map) {
1588 ext2fs_free_block_bitmap(ctx->block_ea_map);
1589 ctx->block_ea_map = 0;
1592 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1593 clear_problem_context(&pctx);
1594 pctx.errcode = ext2fs_create_resize_inode(fs);
1596 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1598 ctx->flags |= E2F_FLAG_ABORT;
1603 if (!pctx.errcode) {
1604 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1606 inode->i_mtime = ctx->now;
1607 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1610 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1613 if (ctx->flags & E2F_FLAG_RESTART) {
1615 * Only the master copy of the superblock and block
1616 * group descriptors are going to be written during a
1617 * restart, so set the superblock to be used to be the
1618 * master superblock.
1620 ctx->use_superblock = 0;
1625 if (ctx->block_dup_map) {
1626 if (ctx->options & E2F_OPT_PREEN) {
1627 clear_problem_context(&pctx);
1628 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1630 e2fsck_pass1_dupblocks(ctx, block_buf);
1632 ext2fs_free_mem(&inodes_to_process);
1634 e2fsck_use_inode_shortcuts(ctx, 0);
1637 ext2fs_close_inode_scan(scan);
1639 ext2fs_free_mem(&block_buf);
1641 ext2fs_free_mem(&inode);
1643 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
1644 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1646 ctx->invalid_bitmaps++;
1650 * When the inode_scan routines call this callback at the end of the
1651 * glock group, call process_inodes.
1653 static errcode_t scan_callback(ext2_filsys fs,
1654 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1655 dgrp_t group, void * priv_data)
1657 struct scan_callback_struct *scan_struct;
1660 scan_struct = (struct scan_callback_struct *) priv_data;
1661 ctx = scan_struct->ctx;
1663 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1666 if ((ctx->progress)(ctx, 1, group+1,
1667 ctx->fs->group_desc_count))
1668 return EXT2_ET_CANCEL_REQUESTED;
1674 * Process the inodes in the "inodes to process" list.
1676 static void process_inodes(e2fsck_t ctx, char *block_buf)
1679 struct ext2_inode *old_stashed_inode;
1680 ext2_ino_t old_stashed_ino;
1681 const char *old_operation;
1683 struct problem_context pctx;
1686 printf("begin process_inodes: ");
1688 if (process_inode_count == 0)
1690 old_operation = ehandler_operation(0);
1691 old_stashed_inode = ctx->stashed_inode;
1692 old_stashed_ino = ctx->stashed_ino;
1693 qsort(inodes_to_process, process_inode_count,
1694 sizeof(struct process_inode_block), process_inode_cmp);
1695 clear_problem_context(&pctx);
1696 for (i=0; i < process_inode_count; i++) {
1697 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1698 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1701 printf("%u ", pctx.ino);
1703 sprintf(buf, _("reading indirect blocks of inode %u"),
1705 ehandler_operation(buf);
1706 check_blocks(ctx, &pctx, block_buf);
1707 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1710 ctx->stashed_inode = old_stashed_inode;
1711 ctx->stashed_ino = old_stashed_ino;
1712 process_inode_count = 0;
1714 printf("end process inodes\n");
1716 ehandler_operation(old_operation);
1719 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1721 const struct process_inode_block *ib_a =
1722 (const struct process_inode_block *) a;
1723 const struct process_inode_block *ib_b =
1724 (const struct process_inode_block *) b;
1727 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1728 ib_b->inode.i_block[EXT2_IND_BLOCK]);
1731 * We only call process_inodes() for non-extent
1732 * inodes, so it's OK to pass NULL to
1733 * ext2fs_file_acl_block() here.
1735 ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
1736 ext2fs_file_acl_block(0, &(ib_b->inode));
1738 ret = ib_a->ino - ib_b->ino;
1743 * Mark an inode as being bad and increment its badness counter.
1745 void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
1746 const char *func, const int line)
1748 struct problem_context pctx;
1751 if (!ctx->inode_badness) {
1752 clear_problem_context(&pctx);
1754 pctx.errcode = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
1755 &ctx->inode_badness);
1757 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1758 ctx->flags |= E2F_FLAG_ABORT;
1762 ext2fs_icount_fetch(ctx->inode_badness, ino, &result);
1763 ext2fs_icount_store(ctx->inode_badness, ino, count + result);
1765 if (ctx->options & E2F_OPT_DEBUG)
1766 fprintf(stderr, "%s:%d: increase inode %lu badness %u to %u\n",
1767 func, line, (unsigned long)ino, result, count + result);
1772 * This procedure will allocate the inode "bb" (badblock) map table
1774 static void alloc_bb_map(e2fsck_t ctx)
1776 struct problem_context pctx;
1778 clear_problem_context(&pctx);
1779 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1780 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
1781 "inode_bb_map", &ctx->inode_bb_map);
1784 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1785 /* Should never get here */
1786 ctx->flags |= E2F_FLAG_ABORT;
1792 * This procedure will allocate the inode imagic table
1794 static void alloc_imagic_map(e2fsck_t ctx)
1796 struct problem_context pctx;
1798 clear_problem_context(&pctx);
1799 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1800 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
1801 "inode_imagic_map", &ctx->inode_imagic_map);
1804 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1805 /* Should never get here */
1806 ctx->flags |= E2F_FLAG_ABORT;
1812 * Marks a block as in use, setting the dup_map if it's been set
1813 * already. Called by process_block and process_bad_block.
1815 * WARNING: Assumes checks have already been done to make sure block
1816 * is valid. This is true in both process_block and process_bad_block.
1818 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
1820 struct problem_context pctx;
1822 clear_problem_context(&pctx);
1824 if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
1825 if (!ctx->block_dup_map) {
1826 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1827 _("multiply claimed block map"),
1828 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1829 &ctx->block_dup_map);
1832 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1834 /* Should never get here */
1835 ctx->flags |= E2F_FLAG_ABORT;
1839 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
1841 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
1845 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
1848 if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
1849 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
1852 mark_block_used(ctx, block++);
1856 * Adjust the extended attribute block's reference counts at the end
1857 * of pass 1, either by subtracting out references for EA blocks that
1858 * are still referenced in ctx->refcount, or by adding references for
1859 * EA blocks that had extra references as accounted for in
1860 * ctx->refcount_extra.
1862 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1863 char *block_buf, int adjust_sign)
1865 struct ext2_ext_attr_header *header;
1866 struct problem_context pctx;
1867 ext2_filsys fs = ctx->fs;
1872 clear_problem_context(&pctx);
1874 ea_refcount_intr_begin(refcount);
1876 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1879 pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1880 /* We already checked this block, shouldn't happen */
1882 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1885 header = BHDR(block_buf);
1886 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1887 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1891 pctx.blkcount = header->h_refcount;
1892 should_be = header->h_refcount + adjust_sign * count;
1893 pctx.num = should_be;
1894 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1895 header->h_refcount = should_be;
1896 pctx.errcode = ext2fs_write_ext_attr2(fs, blk,
1899 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
1908 * Handle processing the extended attribute blocks
1910 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1913 ext2_filsys fs = ctx->fs;
1914 ext2_ino_t ino = pctx->ino;
1915 struct ext2_inode *inode = pctx->inode;
1918 struct ext2_ext_attr_header *header;
1919 struct ext2_ext_attr_entry *entry;
1921 region_t region = 0;
1924 blk = ext2fs_file_acl_block(fs, inode);
1929 * If the Extended attribute flag isn't set, then a non-zero
1930 * file acl means that the inode is corrupted.
1932 * Or if the extended attribute block is an invalid block,
1933 * then the inode is also corrupted.
1935 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1936 (blk < fs->super->s_first_data_block) ||
1937 (blk >= ext2fs_blocks_count(fs->super))) {
1938 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1939 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1943 /* If ea bitmap hasn't been allocated, create it */
1944 if (!ctx->block_ea_map) {
1945 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
1946 _("ext attr block map"),
1947 EXT2FS_BMAP64_RBTREE, "block_ea_map",
1948 &ctx->block_ea_map);
1949 if (pctx->errcode) {
1951 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1952 ctx->flags |= E2F_FLAG_ABORT;
1957 /* Create the EA refcount structure if necessary */
1958 if (!ctx->refcount) {
1959 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1960 if (pctx->errcode) {
1962 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1963 ctx->flags |= E2F_FLAG_ABORT;
1969 /* Debugging text */
1970 printf("Inode %u has EA block %u\n", ino, blk);
1973 /* Have we seen this EA block before? */
1974 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
1975 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1977 /* Ooops, this EA was referenced more than it stated */
1978 if (!ctx->refcount_extra) {
1979 pctx->errcode = ea_refcount_create(0,
1980 &ctx->refcount_extra);
1981 if (pctx->errcode) {
1983 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1984 ctx->flags |= E2F_FLAG_ABORT;
1988 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1993 * OK, we haven't seen this EA block yet. So we need to
1997 pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1998 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
2000 header = BHDR(block_buf);
2001 pctx->blk = ext2fs_file_acl_block(fs, inode);
2002 if (((ctx->ext_attr_ver == 1) &&
2003 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
2004 ((ctx->ext_attr_ver == 2) &&
2005 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
2006 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
2010 if (header->h_blocks != 1) {
2011 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
2015 region = region_create(0, fs->blocksize);
2017 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
2018 ctx->flags |= E2F_FLAG_ABORT;
2021 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
2022 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2026 entry = (struct ext2_ext_attr_entry *)(header+1);
2027 end = block_buf + fs->blocksize;
2028 while ((char *)entry < end && *(__u32 *)entry) {
2031 if (region_allocate(region, (char *)entry - (char *)header,
2032 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
2033 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2037 if ((ctx->ext_attr_ver == 1 &&
2038 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
2039 (ctx->ext_attr_ver == 2 &&
2040 entry->e_name_index == 0)) {
2041 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
2045 if (entry->e_value_inum == 0) {
2046 if (entry->e_value_offs + entry->e_value_size >
2048 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
2052 if (entry->e_value_size &&
2053 region_allocate(region, entry->e_value_offs,
2054 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
2055 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
2060 int i_file_acl_deleted = 0;
2062 ret = check_large_ea_inode(ctx, entry, pctx,
2063 &i_file_acl_deleted);
2065 mark_inode_ea_map(ctx, pctx,
2066 entry->e_value_inum);
2068 if (i_file_acl_deleted)
2072 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
2073 entry->e_value_offs);
2075 if (entry->e_hash != hash) {
2076 pctx->num = entry->e_hash;
2077 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
2079 entry->e_hash = hash;
2082 entry = EXT2_EXT_ATTR_NEXT(entry);
2084 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
2085 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2088 region_free(region);
2090 count = header->h_refcount - 1;
2092 ea_refcount_store(ctx->refcount, blk, count);
2093 mark_block_used(ctx, blk);
2094 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
2099 region_free(region);
2100 ext2fs_file_acl_block_set(fs, inode, 0);
2101 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
2105 /* Returns 1 if bad htree, 0 if OK */
2106 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
2107 ext2_ino_t ino, struct ext2_inode *inode,
2110 struct ext2_dx_root_info *root;
2111 ext2_filsys fs = ctx->fs;
2115 if ((!LINUX_S_ISDIR(inode->i_mode) &&
2116 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
2117 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX))) {
2118 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2119 if (fix_problem(ctx, PR_1_HTREE_SET, pctx))
2123 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
2125 if ((pctx->errcode) ||
2127 (blk < fs->super->s_first_data_block) ||
2128 (blk >= ext2fs_blocks_count(fs->super))) {
2129 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2130 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2136 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
2138 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2139 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2143 /* XXX should check that beginning matches a directory */
2144 root = get_ext2_dx_root_info(fs, block_buf);
2146 if ((root->reserved_zero || root->info_length < 8) &&
2147 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2150 pctx->num = root->hash_version;
2151 if ((root->hash_version != EXT2_HASH_LEGACY) &&
2152 (root->hash_version != EXT2_HASH_HALF_MD4) &&
2153 (root->hash_version != EXT2_HASH_TEA) &&
2154 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2157 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2158 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2161 pctx->num = root->indirect_levels;
2162 if ((root->indirect_levels > 1) &&
2163 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2169 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2170 struct ext2_inode *inode, int restart_flag,
2174 inode->i_links_count = 0;
2175 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2176 inode->i_dtime = ctx->now;
2178 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2179 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2180 if (ctx->inode_reg_map)
2181 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2182 if (ctx->inode_badness)
2183 ext2fs_icount_store(ctx->inode_badness, ino, 0);
2186 * If the inode was partially accounted for before processing
2187 * was aborted, we need to restart the pass 1 scan.
2189 ctx->flags |= restart_flag;
2191 if (ino == EXT2_BAD_INO)
2192 memset(inode, 0, sizeof(struct ext2_inode));
2194 e2fsck_write_inode(ctx, ino, inode, source);
2198 * Use the multiple-blocks reclamation code to fix alignment problems in
2199 * a bigalloc filesystem. We want a logical cluster to map to *only* one
2200 * physical cluster, and we want the block offsets within that cluster to
2203 static int has_unaligned_cluster_map(e2fsck_t ctx,
2204 blk64_t last_pblk, e2_blkcnt_t last_lblk,
2205 blk64_t pblk, blk64_t lblk)
2207 blk64_t cluster_mask;
2209 if (!ctx->fs->cluster_ratio_bits)
2211 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
2214 * If the block in the logical cluster doesn't align with the block in
2215 * the physical cluster...
2217 if ((lblk & cluster_mask) != (pblk & cluster_mask))
2221 * If we cross a physical cluster boundary within a logical cluster...
2223 if (last_pblk && (lblk & cluster_mask) != 0 &&
2224 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
2225 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
2231 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2232 struct process_block_struct *pb,
2233 blk64_t start_block, blk64_t end_block,
2235 ext2_extent_handle_t ehandle)
2237 struct ext2fs_extent extent;
2238 blk64_t blk, last_lblk;
2239 e2_blkcnt_t blockcnt;
2241 int is_dir, is_leaf;
2243 struct ext2_extent_info info;
2245 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2249 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2251 while (!pctx->errcode && info.num_entries-- > 0) {
2252 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2253 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2254 last_lblk = extent.e_lblk + extent.e_len - 1;
2257 if (extent.e_pblk == 0 ||
2258 extent.e_pblk < ctx->fs->super->s_first_data_block ||
2259 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2260 problem = PR_1_EXTENT_BAD_START_BLK;
2261 else if (extent.e_lblk < start_block)
2262 problem = PR_1_OUT_OF_ORDER_EXTENTS;
2263 else if ((end_block && last_lblk > end_block) &&
2264 (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
2265 last_lblk > eof_block)))
2266 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
2267 else if (is_leaf && extent.e_len == 0)
2268 problem = PR_1_EXTENT_LENGTH_ZERO;
2270 (extent.e_pblk + extent.e_len) >
2271 ext2fs_blocks_count(ctx->fs->super))
2272 problem = PR_1_EXTENT_ENDS_BEYOND;
2273 else if (is_leaf && is_dir &&
2274 ((extent.e_lblk + extent.e_len) >
2275 (1 << (21 - ctx->fs->super->s_log_block_size))))
2276 problem = PR_1_TOOBIG_DIR;
2279 * Uninitialized blocks in a directory? Clear the flag and
2280 * we'll interpret the blocks later.
2282 if (is_dir && problem == 0 &&
2283 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2284 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
2285 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2286 pb->inode_modified = 1;
2287 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2294 /* To ensure that extent is in inode */
2295 if (info.curr_level == 0)
2296 e2fsck_mark_inode_bad(ctx, pctx->ino,
2299 pctx->blk = extent.e_pblk;
2300 pctx->blk2 = extent.e_lblk;
2301 pctx->num = extent.e_len;
2302 pctx->blkcount = extent.e_lblk + extent.e_len;
2303 if (fix_problem(ctx, problem, pctx)) {
2304 if (ctx->invalid_bitmaps) {
2306 * If fsck knows the bitmaps are bad,
2307 * skip to the next extent and
2308 * try to clear this extent again
2309 * after fixing the bitmaps, by
2312 pctx->errcode = ext2fs_extent_get(
2314 EXT2_EXTENT_NEXT_SIB,
2316 ctx->flags |= E2F_FLAG_RESTART_LATER;
2317 if (pctx->errcode ==
2318 EXT2_ET_NO_CURRENT_NODE) {
2324 e2fsck_read_bitmaps(ctx);
2325 pb->inode_modified = 1;
2327 ext2fs_extent_delete(ehandle, 0);
2328 if (pctx->errcode) {
2329 pctx->str = "ext2fs_extent_delete";
2332 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2333 if (pctx->errcode &&
2334 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
2335 pctx->str = "ext2fs_extent_fix_parents";
2338 pctx->errcode = ext2fs_extent_get(ehandle,
2339 EXT2_EXTENT_CURRENT,
2341 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2351 blk64_t lblk = extent.e_lblk;
2353 blk = extent.e_pblk;
2354 pctx->errcode = ext2fs_extent_get(ehandle,
2355 EXT2_EXTENT_DOWN, &extent);
2356 if (pctx->errcode) {
2357 pctx->str = "EXT2_EXTENT_DOWN";
2358 problem = PR_1_EXTENT_HEADER_INVALID;
2359 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
2360 goto report_problem;
2363 /* The next extent should match this index's logical start */
2364 if (extent.e_lblk != lblk) {
2365 struct ext2_extent_info e_info;
2367 ext2fs_extent_get_info(ehandle, &e_info);
2369 pctx->blk2 = extent.e_lblk;
2370 pctx->num = e_info.curr_level - 1;
2371 problem = PR_1_EXTENT_INDEX_START_INVALID;
2372 if (fix_problem(ctx, problem, pctx)) {
2373 pb->inode_modified = 1;
2375 ext2fs_extent_fix_parents(ehandle);
2376 if (pctx->errcode) {
2377 pctx->str = "ext2fs_extent_fix_parents";
2382 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
2383 last_lblk, eof_block, ehandle);
2386 pctx->errcode = ext2fs_extent_get(ehandle,
2387 EXT2_EXTENT_UP, &extent);
2388 if (pctx->errcode) {
2389 pctx->str = "EXT2_EXTENT_UP";
2392 mark_block_used(ctx, blk);
2397 if ((pb->previous_block != 0) &&
2398 (pb->previous_block+1 != extent.e_pblk)) {
2399 if (ctx->options & E2F_OPT_FRAGCHECK) {
2404 else if (pb->is_reg)
2407 printf(("%6lu(%c): expecting %6lu "
2409 "phys %6lu log %lu len %lu\n"),
2410 (unsigned long) pctx->ino, type,
2411 (unsigned long) pb->previous_block+1,
2412 (unsigned long) extent.e_pblk,
2413 (unsigned long) extent.e_lblk,
2414 (unsigned long) extent.e_len);
2419 * If we notice a gap in the logical block mappings of an
2420 * extent-mapped directory, offer to close the hole by
2421 * moving the logical block down, otherwise we'll go mad in
2422 * pass 3 allocating empty directory blocks to fill the hole.
2425 pb->last_block + 1 < (e2_blkcnt_t)extent.e_lblk) {
2428 new_lblk = pb->last_block + 1;
2429 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
2430 new_lblk = ((new_lblk +
2431 EXT2FS_CLUSTER_RATIO(ctx->fs)) &
2432 EXT2FS_CLUSTER_MASK(ctx->fs)) |
2434 EXT2FS_CLUSTER_MASK(ctx->fs));
2435 pctx->blk = extent.e_lblk;
2436 pctx->blk2 = new_lblk;
2437 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
2438 extent.e_lblk = new_lblk;
2439 pb->inode_modified = 1;
2440 pctx->errcode = ext2fs_extent_replace(ehandle,
2442 if (pctx->errcode) {
2446 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2448 goto failed_add_dir_block;
2449 pctx->errcode = ext2fs_extent_goto(ehandle,
2452 goto failed_add_dir_block;
2453 last_lblk = extent.e_lblk + extent.e_len - 1;
2457 while (is_dir && (++pb->last_db_block <
2458 (e2_blkcnt_t) extent.e_lblk)) {
2459 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
2462 if (pctx->errcode) {
2464 pctx->num = pb->last_db_block;
2465 goto failed_add_dir_block;
2468 if (!ctx->fs->cluster_ratio_bits) {
2469 mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
2470 pb->num_blocks += extent.e_len;
2472 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
2474 blk++, blockcnt++, i++) {
2475 if (ctx->fs->cluster_ratio_bits &&
2476 !(pb->previous_block &&
2477 (EXT2FS_B2C(ctx->fs, blk) ==
2478 EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
2479 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2480 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2481 mark_block_used(ctx, blk);
2484 if (has_unaligned_cluster_map(ctx, pb->previous_block,
2485 pb->last_block, blk,
2487 pctx->blk = blockcnt;
2489 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2490 mark_block_used(ctx, blk);
2491 mark_block_used(ctx, blk);
2493 pb->last_block = blockcnt;
2494 pb->previous_block = blk;
2497 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
2498 if (pctx->errcode) {
2500 pctx->num = blockcnt;
2501 failed_add_dir_block:
2502 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2503 /* Should never get here */
2504 ctx->flags |= E2F_FLAG_ABORT;
2509 if (is_dir && extent.e_len > 0)
2510 pb->last_db_block = blockcnt - 1;
2511 pb->previous_block = extent.e_pblk + extent.e_len - 1;
2512 start_block = pb->last_block = last_lblk;
2513 if (is_leaf && !is_dir &&
2514 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
2515 pb->last_init_lblock = last_lblk;
2517 pctx->errcode = ext2fs_extent_get(ehandle,
2518 EXT2_EXTENT_NEXT_SIB,
2521 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
2525 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
2526 struct process_block_struct *pb)
2528 struct ext2_extent_info info;
2529 struct ext2_inode *inode = pctx->inode;
2530 ext2_extent_handle_t ehandle;
2531 ext2_filsys fs = ctx->fs;
2532 ext2_ino_t ino = pctx->ino;
2536 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
2537 if (pctx->errcode) {
2538 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
2539 e2fsck_clear_inode(ctx, ino, inode, 0,
2540 "check_blocks_extents");
2545 retval = ext2fs_extent_get_info(ehandle, &info);
2547 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
2548 info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
2549 ctx->extent_depth_count[info.max_depth]++;
2552 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
2553 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
2554 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle);
2555 if (pctx->errcode &&
2556 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
2558 inode->i_blocks = 0;
2559 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2560 "check_blocks_extents");
2563 ext2fs_extent_free(ehandle);
2567 * This subroutine is called on each inode to account for all of the
2568 * blocks used by that inode.
2570 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
2573 ext2_filsys fs = ctx->fs;
2574 struct process_block_struct pb;
2575 ext2_ino_t ino = pctx->ino;
2576 struct ext2_inode *inode = pctx->inode;
2577 unsigned bad_size = 0;
2578 int dirty_inode = 0;
2585 pb.last_init_lblock = -1;
2586 pb.last_db_block = -1;
2587 pb.num_illegal_blocks = 0;
2588 pb.suppress = 0; pb.clear = 0;
2591 pb.previous_block = 0;
2592 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2593 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
2594 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
2598 pb.inode_modified = 0;
2602 extent_fs = (ctx->fs->super->s_feature_incompat &
2603 EXT3_FEATURE_INCOMPAT_EXTENTS);
2605 if (inode->i_flags & EXT2_COMPRBLK_FL) {
2606 if (fs->super->s_feature_incompat &
2607 EXT2_FEATURE_INCOMPAT_COMPRESSION)
2610 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2611 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
2612 inode->i_flags &= ~EXT2_COMPRBLK_FL;
2618 if (ext2fs_file_acl_block(fs, inode) &&
2619 check_ext_attr(ctx, pctx, block_buf)) {
2620 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2625 if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
2626 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
2627 check_blocks_extents(ctx, pctx, &pb);
2630 * If we've modified the inode, write it out before
2631 * iterate() tries to use it.
2634 e2fsck_write_inode(ctx, ino, inode,
2638 pctx->errcode = ext2fs_block_iterate3(fs, ino,
2639 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
2640 block_buf, process_block, &pb);
2642 * We do not have uninitialized extents in non extent
2645 pb.last_init_lblock = pb.last_block;
2647 * If iterate() changed a block mapping, we have to
2648 * re-read the inode. If we decide to clear the
2649 * inode after clearing some stuff, we'll re-write the
2650 * bad mappings into the inode!
2652 if (pb.inode_modified)
2653 e2fsck_read_inode(ctx, ino, inode,
2657 end_problem_latch(ctx, PR_LATCH_BLOCK);
2658 end_problem_latch(ctx, PR_LATCH_TOOBIG);
2659 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2662 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
2664 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
2665 if (LINUX_S_ISDIR(inode->i_mode))
2666 ctx->fs_fragmented_dir++;
2668 ctx->fs_fragmented++;
2672 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2677 if (inode->i_flags & EXT2_INDEX_FL) {
2678 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
2679 inode->i_flags &= ~EXT2_INDEX_FL;
2683 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
2688 if (!pb.num_blocks && pb.is_dir) {
2690 * The mode might be in-correct. Increasing the badness by
2691 * small amount won't hurt much.
2693 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2694 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
2695 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
2696 ctx->fs_directory_count--;
2701 if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
2702 quota_data_add(ctx->qctx, inode, ino,
2703 pb.num_blocks * fs->blocksize);
2704 quota_data_inodes(ctx->qctx, inode, ino, +1);
2707 if (!(fs->super->s_feature_ro_compat &
2708 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
2709 !(inode->i_flags & EXT4_HUGE_FILE_FL))
2710 pb.num_blocks *= (fs->blocksize / 512);
2711 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
2713 printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
2714 ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
2718 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
2719 if (inode->i_size & (fs->blocksize - 1))
2721 else if (nblock > (pb.last_block + 1))
2723 else if (nblock < (pb.last_block + 1)) {
2724 if (((pb.last_block + 1) - nblock) >
2725 fs->super->s_prealloc_dir_blocks)
2729 e2_blkcnt_t blkpg = ctx->blocks_per_page;
2731 size = EXT2_I_SIZE(inode);
2732 if ((pb.last_init_lblock >= 0) &&
2733 /* if size is smaller than expected by the block count,
2734 * allow allocated blocks to end of PAGE_SIZE.
2735 * last_init_lblock is the last in-use block, so it is
2736 * the minimum expected file size. */
2737 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
2738 ((pb.last_init_lblock + 1) / blkpg * blkpg !=
2739 (pb.last_init_lblock + 1) ||
2740 size < (__u64)(pb.last_init_lblock & ~(blkpg - 1)) *
2743 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2744 size > ext2_max_sizes[fs->super->s_log_block_size])
2745 /* too big for a direct/indirect-mapped file */
2747 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2749 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
2750 /* too big for an extent-based file - 32bit ee_block */
2753 /* i_size for symlinks is checked elsewhere */
2754 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
2755 pctx->num = (pb.last_block+1) * fs->blocksize;
2756 pctx->group = bad_size;
2757 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2758 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
2759 if (LINUX_S_ISDIR(inode->i_mode))
2760 pctx->num &= 0xFFFFFFFFULL;
2761 ext2fs_inode_size_set(fs, inode, pctx->num);
2766 if (LINUX_S_ISREG(inode->i_mode) &&
2767 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
2769 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
2770 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
2771 ((fs->super->s_feature_ro_compat &
2772 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
2773 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
2774 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
2775 pctx->num = pb.num_blocks;
2776 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2777 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
2778 inode->i_blocks = pb.num_blocks;
2779 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
2785 if (ctx->dirs_to_hash && pb.is_dir &&
2786 !(inode->i_flags & EXT2_INDEX_FL) &&
2787 ((inode->i_size / fs->blocksize) >= 3))
2788 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
2792 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
2797 * Helper function called by process block when an illegal block is
2798 * found. It returns a description about why the block is illegal
2800 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
2804 static char problem[80];
2806 super = fs->super->s_first_data_block;
2807 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
2808 if (block < super) {
2809 sprintf(problem, "< FIRSTBLOCK (%u)", super);
2811 } else if (block >= ext2fs_blocks_count(fs->super)) {
2812 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
2815 for (i = 0; i < fs->group_desc_count; i++) {
2816 if (block == super) {
2817 sprintf(problem, "is the superblock in group %d", i);
2820 if (block > super &&
2821 block <= (super + fs->desc_blocks)) {
2822 sprintf(problem, "is in the group descriptors "
2826 if (block == ext2fs_block_bitmap_loc(fs, i)) {
2827 sprintf(problem, "is the block bitmap of group %d", i);
2830 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
2831 sprintf(problem, "is the inode bitmap of group %d", i);
2834 if (block >= ext2fs_inode_table_loc(fs, i) &&
2835 (block < ext2fs_inode_table_loc(fs, i)
2836 + fs->inode_blocks_per_group)) {
2837 sprintf(problem, "is in the inode table of group %d",
2841 super += fs->super->s_blocks_per_group;
2848 * This is a helper function for check_blocks().
2850 static int process_block(ext2_filsys fs,
2852 e2_blkcnt_t blockcnt,
2853 blk64_t ref_block EXT2FS_ATTR((unused)),
2854 int ref_offset EXT2FS_ATTR((unused)),
2857 struct process_block_struct *p;
2858 struct problem_context *pctx;
2859 blk64_t blk = *block_nr;
2861 problem_t problem = 0;
2864 p = (struct process_block_struct *) priv_data;
2868 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
2869 /* todo: Check that the comprblk_fl is high, that the
2870 blkaddr pattern looks right (all non-holes up to
2871 first EXT2FS_COMPRESSED_BLKADDR, then all
2872 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2873 that the feature_incompat bit is high, and that the
2874 inode is a regular file. If we're doing a "full
2875 check" (a concept introduced to e2fsck by e2compr,
2876 meaning that we look at data blocks as well as
2877 metadata) then call some library routine that
2878 checks the compressed data. I'll have to think
2879 about this, because one particularly important
2880 problem to be able to fix is to recalculate the
2881 cluster size if necessary. I think that perhaps
2882 we'd better do most/all e2compr-specific checks
2883 separately, after the non-e2compr checks. If not
2884 doing a full check, it may be useful to test that
2885 the personality is linux; e.g. if it isn't then
2886 perhaps this really is just an illegal block. */
2891 * For a directory, add logical block zero for processing even if it's
2892 * not mapped or we'll be perennially stuck with broken "." and ".."
2895 if (p->is_dir && blockcnt == 0 && blk == 0) {
2896 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
2897 if (pctx->errcode) {
2899 pctx->num = blockcnt;
2900 goto failed_add_dir_block;
2909 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
2914 * Simplistic fragmentation check. We merely require that the
2915 * file be contiguous. (Which can never be true for really
2916 * big files that are greater than a block group.)
2918 if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
2919 if (p->previous_block+1 != blk) {
2920 if (ctx->options & E2F_OPT_FRAGCHECK) {
2928 printf(_("%6lu(%c): expecting %6lu "
2929 "got phys %6lu (blkcnt %lld)\n"),
2930 (unsigned long) pctx->ino, type,
2931 (unsigned long) p->previous_block+1,
2932 (unsigned long) blk,
2939 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
2940 problem = PR_1_TOOBIG_DIR;
2941 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
2942 problem = PR_1_TOOBIG_REG;
2943 if (!p->is_dir && !p->is_reg && blockcnt > 0)
2944 problem = PR_1_TOOBIG_SYMLINK;
2946 if (blk < fs->super->s_first_data_block ||
2947 blk >= ext2fs_blocks_count(fs->super)) {
2948 problem = PR_1_ILLEGAL_BLOCK_NUM;
2949 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
2953 p->num_illegal_blocks++;
2954 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
2955 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
2959 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
2961 set_latch_flags(PR_LATCH_BLOCK,
2966 pctx->blkcount = blockcnt;
2967 if (fix_problem(ctx, problem, pctx)) {
2968 blk = *block_nr = 0;
2969 ret_code = BLOCK_CHANGED;
2970 p->inode_modified = 1;
2972 * If the directory block is too big and is beyond the
2973 * end of the FS, don't bother trying to add it for
2974 * processing -- the kernel would never have created a
2975 * directory this large, and we risk an ENOMEM abort.
2976 * In any case, the toobig handler for extent-based
2977 * directories also doesn't feed toobig blocks to
2980 if (problem == PR_1_TOOBIG_DIR)
2987 if (p->ino == EXT2_RESIZE_INO) {
2989 * The resize inode has already be sanity checked
2990 * during pass #0 (the superblock checks). All we
2991 * have to do is mark the double indirect block as
2992 * being in use; all of the other blocks are handled
2993 * by mark_table_blocks()).
2995 if (blockcnt == BLOCK_COUNT_DIND)
2996 mark_block_used(ctx, blk);
2998 } else if (!(ctx->fs->cluster_ratio_bits &&
2999 p->previous_block &&
3000 (EXT2FS_B2C(ctx->fs, blk) ==
3001 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
3002 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
3003 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
3004 mark_block_used(ctx, blk);
3006 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
3007 p->last_block, blk, blockcnt)) {
3008 pctx->blk = blockcnt;
3010 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3011 mark_block_used(ctx, blk);
3012 mark_block_used(ctx, blk);
3015 p->last_block = blockcnt;
3016 p->previous_block = blk;
3018 if (p->is_dir && (blockcnt >= 0)) {
3019 while (++p->last_db_block < blockcnt) {
3020 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
3023 if (pctx->errcode) {
3025 pctx->num = p->last_db_block;
3026 goto failed_add_dir_block;
3029 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
3031 if (pctx->errcode) {
3033 pctx->num = blockcnt;
3034 failed_add_dir_block:
3035 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3036 /* Should never get here */
3037 ctx->flags |= E2F_FLAG_ABORT;
3044 static int process_bad_block(ext2_filsys fs,
3046 e2_blkcnt_t blockcnt,
3047 blk64_t ref_block EXT2FS_ATTR((unused)),
3048 int ref_offset EXT2FS_ATTR((unused)),
3051 struct process_block_struct *p;
3052 blk64_t blk = *block_nr;
3053 blk64_t first_block;
3055 struct problem_context *pctx;
3059 * Note: This function processes blocks for the bad blocks
3060 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
3066 p = (struct process_block_struct *) priv_data;
3070 pctx->ino = EXT2_BAD_INO;
3072 pctx->blkcount = blockcnt;
3074 if ((blk < fs->super->s_first_data_block) ||
3075 (blk >= ext2fs_blocks_count(fs->super))) {
3076 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
3078 return BLOCK_CHANGED;
3084 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
3086 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
3088 return BLOCK_CHANGED;
3090 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3093 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
3096 return BLOCK_CHANGED;
3098 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3101 mark_block_used(ctx, blk);
3105 printf ("DEBUG: Marking %u as bad.\n", blk);
3107 ctx->fs_badblocks_count++;
3109 * If the block is not used, then mark it as used and return.
3110 * If it is already marked as found, this must mean that
3111 * there's an overlap between the filesystem table blocks
3112 * (bitmaps and inode table) and the bad block list.
3114 if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
3115 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3119 * Try to find the where the filesystem block was used...
3121 first_block = fs->super->s_first_data_block;
3123 for (i = 0; i < fs->group_desc_count; i++ ) {
3126 if (!ext2fs_bg_has_super(fs, i))
3128 if (blk == first_block) {
3130 if (fix_problem(ctx,
3131 PR_1_BAD_PRIMARY_SUPERBLOCK,
3134 return BLOCK_CHANGED;
3138 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
3141 if ((blk > first_block) &&
3142 (blk <= first_block + fs->desc_blocks)) {
3144 pctx->blk = *block_nr;
3145 if (fix_problem(ctx,
3146 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
3148 return BLOCK_CHANGED;
3152 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
3156 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
3157 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
3158 ctx->invalid_block_bitmap_flag[i]++;
3159 ctx->invalid_bitmaps++;
3163 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
3164 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
3165 ctx->invalid_inode_bitmap_flag[i]++;
3166 ctx->invalid_bitmaps++;
3170 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
3171 (blk < (ext2fs_inode_table_loc(fs, i) +
3172 fs->inode_blocks_per_group))) {
3174 * If there are bad blocks in the inode table,
3175 * the inode scan code will try to do
3176 * something reasonable automatically.
3180 first_block += fs->super->s_blocks_per_group;
3183 * If we've gotten to this point, then the only
3184 * possibility is that the bad block inode meta data
3185 * is using a bad block.
3187 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
3188 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
3189 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
3191 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
3193 return BLOCK_CHANGED;
3195 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3202 /* Warn user that the block wasn't claimed */
3203 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
3208 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
3209 const char *name, int num, blk64_t *new_block)
3211 ext2_filsys fs = ctx->fs;
3213 blk64_t old_block = *new_block;
3216 unsigned flexbg_size;
3219 struct problem_context pctx;
3221 clear_problem_context(&pctx);
3224 pctx.blk = old_block;
3228 * For flex_bg filesystems, first try to allocate the metadata
3229 * within the flex_bg, and if that fails then try finding the
3230 * space anywhere in the filesystem.
3232 is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
3233 EXT4_FEATURE_INCOMPAT_FLEX_BG);
3235 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3236 flexbg = group / flexbg_size;
3237 first_block = ext2fs_group_first_block2(fs,
3238 flexbg_size * flexbg);
3239 last_grp = group | (flexbg_size - 1);
3240 if (last_grp >= fs->group_desc_count)
3241 last_grp = fs->group_desc_count - 1;
3242 last_block = ext2fs_group_last_block2(fs, last_grp);
3244 last_block = ext2fs_group_last_block2(fs, group);
3245 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
3246 num, ctx->block_found_map,
3248 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
3249 pctx.errcode = ext2fs_get_free_blocks2(fs,
3250 fs->super->s_first_data_block,
3251 ext2fs_blocks_count(fs->super),
3252 num, ctx->block_found_map, new_block);
3255 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
3256 ext2fs_unmark_valid(fs);
3257 ctx->flags |= E2F_FLAG_ABORT;
3260 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
3262 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
3263 ext2fs_unmark_valid(fs);
3264 ctx->flags |= E2F_FLAG_ABORT;
3267 ext2fs_mark_super_dirty(fs);
3268 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3269 pctx.blk2 = *new_block;
3270 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
3271 PR_1_RELOC_TO), &pctx);
3273 for (i = 0; i < num; i++) {
3275 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
3277 pctx.errcode = io_channel_read_blk64(fs->io,
3278 old_block + i, 1, buf);
3280 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
3282 memset(buf, 0, fs->blocksize);
3284 pctx.blk = (*new_block) + i;
3285 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
3288 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
3290 ext2fs_free_mem(&buf);
3294 * This routine gets called at the end of pass 1 if bad blocks are
3295 * detected in the superblock, group descriptors, inode_bitmaps, or
3296 * block bitmaps. At this point, all of the blocks have been mapped
3297 * out, so we can try to allocate new block(s) to replace the bad
3300 static void handle_fs_bad_blocks(e2fsck_t ctx)
3302 ext2_filsys fs = ctx->fs;
3304 blk64_t first_block;
3307 for (i = 0; i < fs->group_desc_count; i++) {
3308 first_block = ext2fs_group_first_block2(fs, i);
3310 if (ctx->invalid_block_bitmap_flag[i]) {
3311 new_blk = ext2fs_block_bitmap_loc(fs, i);
3312 new_table_block(ctx, first_block, i, _("block bitmap"),
3314 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
3316 if (ctx->invalid_inode_bitmap_flag[i]) {
3317 new_blk = ext2fs_inode_bitmap_loc(fs, i);
3318 new_table_block(ctx, first_block, i, _("inode bitmap"),
3320 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
3322 if (ctx->invalid_inode_table_flag[i]) {
3323 new_blk = ext2fs_inode_table_loc(fs, i);
3324 new_table_block(ctx, first_block, i, _("inode table"),
3325 fs->inode_blocks_per_group,
3327 ext2fs_inode_table_loc_set(fs, i, new_blk);
3328 ctx->flags |= E2F_FLAG_RESTART;
3331 ctx->invalid_bitmaps = 0;
3335 * This routine marks all blocks which are used by the superblock,
3336 * group descriptors, inode bitmaps, and block bitmaps.
3338 static void mark_table_blocks(e2fsck_t ctx)
3340 ext2_filsys fs = ctx->fs;
3344 struct problem_context pctx;
3346 clear_problem_context(&pctx);
3348 for (i = 0; i < fs->group_desc_count; i++) {
3351 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
3354 * Mark the blocks used for the inode table
3356 if (ext2fs_inode_table_loc(fs, i)) {
3357 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
3358 j < fs->inode_blocks_per_group;
3360 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3363 if (!ctx->invalid_inode_table_flag[i] &&
3365 PR_1_ITABLE_CONFLICT, &pctx)) {
3366 ctx->invalid_inode_table_flag[i]++;
3367 ctx->invalid_bitmaps++;
3370 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3377 * Mark block used for the block bitmap
3379 if (ext2fs_block_bitmap_loc(fs, i)) {
3380 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3381 ext2fs_block_bitmap_loc(fs, i))) {
3382 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
3383 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
3384 ctx->invalid_block_bitmap_flag[i]++;
3385 ctx->invalid_bitmaps++;
3388 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3389 ext2fs_block_bitmap_loc(fs, i));
3394 * Mark block used for the inode bitmap
3396 if (ext2fs_inode_bitmap_loc(fs, i)) {
3397 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3398 ext2fs_inode_bitmap_loc(fs, i))) {
3399 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
3400 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
3401 ctx->invalid_inode_bitmap_flag[i]++;
3402 ctx->invalid_bitmaps++;
3405 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3406 ext2fs_inode_bitmap_loc(fs, i));
3413 * Thes subroutines short circuits ext2fs_get_blocks and
3414 * ext2fs_check_directory; we use them since we already have the inode
3415 * structure, so there's no point in letting the ext2fs library read
3418 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
3421 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3424 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3425 return EXT2_ET_CALLBACK_NOTHANDLED;
3427 for (i=0; i < EXT2_N_BLOCKS; i++)
3428 blocks[i] = ctx->stashed_inode->i_block[i];
3432 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
3433 struct ext2_inode *inode)
3435 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3437 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3438 return EXT2_ET_CALLBACK_NOTHANDLED;
3439 *inode = *ctx->stashed_inode;
3443 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
3444 struct ext2_inode *inode)
3446 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3448 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
3449 (inode != ctx->stashed_inode))
3450 *ctx->stashed_inode = *inode;
3451 return EXT2_ET_CALLBACK_NOTHANDLED;
3454 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
3456 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3458 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3459 return EXT2_ET_CALLBACK_NOTHANDLED;
3461 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
3462 return EXT2_ET_NO_DIRECTORY;
3466 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
3469 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3473 if (ctx->block_found_map) {
3474 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
3478 if (fs->block_map) {
3479 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
3480 ext2fs_mark_bb_dirty(fs);
3483 if (!fs->block_map) {
3484 retval = ext2fs_read_block_bitmap(fs);
3489 retval = ext2fs_new_block2(fs, goal, 0, &new_block);
3498 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
3500 ext2_filsys fs = ctx->fs;
3502 if (use_shortcuts) {
3503 fs->get_blocks = pass1_get_blocks;
3504 fs->check_directory = pass1_check_directory;
3505 fs->read_inode = pass1_read_inode;
3506 fs->write_inode = pass1_write_inode;
3507 ctx->stashed_ino = 0;
3510 fs->check_directory = 0;
3512 fs->write_inode = 0;
3516 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
3518 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
3519 ext2fs_set_block_alloc_stats_callback(ctx->fs,
3520 e2fsck_block_alloc_stats, 0);