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 /* check value size */
450 if (entry->e_value_size > remain) {
451 pctx->num = entry->e_value_size;
452 problem = PR_1_ATTR_VALUE_SIZE;
456 if (entry->e_value_inum == 0) {
457 /* check value size */
458 if (entry->e_value_size > remain) {
459 pctx->num = entry->e_value_size;
460 problem = PR_1_ATTR_VALUE_SIZE;
466 ret = check_large_ea_inode(ctx, entry, pctx, &tmp);
468 mark_inode_ea_map(ctx, pctx,
469 entry->e_value_inum);
472 /* Value size cannot be larger than EA space in inode */
473 if (entry->e_value_offs > storage_size ||
474 (entry->e_value_inum == 0 &&
475 entry->e_value_offs + entry->e_value_size > storage_size)) {
476 problem = PR_1_INODE_EA_BAD_VALUE;
480 hash = ext2fs_ext_attr_hash_entry(entry,
481 start + entry->e_value_offs);
483 /* e_hash may be 0 in older inode's ea */
484 if (entry->e_hash != 0 && entry->e_hash != hash) {
485 pctx->num = entry->e_hash;
486 problem = PR_1_ATTR_HASH;
490 /* If EA value is stored in external inode then it does not
491 * consume space here */
492 if (entry->e_value_inum == 0)
493 remain -= entry->e_value_size;
495 entry = EXT2_EXT_ATTR_NEXT(entry);
499 * it seems like a corruption. it's very unlikely we could repair
500 * EA(s) in automatic fashion -bzzz
502 if (problem == 0 || !fix_problem(ctx, problem, pctx))
505 /* simply remove all possible EA(s) */
506 *((__u32 *)start) = 0UL;
507 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
508 EXT2_INODE_SIZE(sb), "pass1");
511 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
513 struct ext2_super_block *sb = ctx->fs->super;
514 struct ext2_inode_large *inode;
516 int min, max, dirty = 0;
518 inode = (struct ext2_inode_large *) pctx->inode;
519 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
520 /* this isn't large inode. so, nothing to check */
525 printf("inode #%u, i_extra_size %d\n", pctx->ino,
526 inode->i_extra_isize);
528 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
529 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
530 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
532 * For now we will allow i_extra_isize to be 0, but really
533 * implementations should never allow i_extra_isize to be 0
535 if (inode->i_extra_isize &&
536 (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
537 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
538 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
540 inode->i_extra_isize = ctx->want_extra_isize;
546 if (EXT4_FITS_IN_INODE(inode, inode, i_crtime) &&
547 inode->i_crtime != 0 &&
548 (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, 2*ctx->time_fudge) ||
549 EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, 2*ctx->time_fudge))) {
550 pctx->num = inode->i_crtime;
551 if (fix_problem(ctx, PR_1_CRTIME_BAD, pctx)) {
555 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_HIGH);
558 eamagic = &IHDR(inode)->h_magic;
559 if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
560 (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
561 (inode->i_extra_isize < ctx->want_extra_isize)) {
562 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
563 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
564 EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
565 inode->i_extra_isize = ctx->want_extra_isize;
567 if (inode->i_extra_isize < ctx->min_extra_isize)
568 ctx->min_extra_isize = inode->i_extra_isize;
571 if (*eamagic == EXT2_EXT_ATTR_MAGIC)
572 check_ea_in_inode(ctx, pctx);
575 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
576 EXT2_INODE_SIZE(sb), "pass1");
580 * Check to see if the inode might really be a directory, despite i_mode
582 * This is a lot of complexity for something for which I'm not really
583 * convinced happens frequently in the wild. If for any reason this
584 * causes any problems, take this code out.
585 * [tytso:20070331.0827EDT]
587 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
590 struct ext2_inode *inode = pctx->inode;
591 struct ext2_dir_entry *dirent;
594 unsigned int i, rec_len, not_device = 0;
598 * If the mode looks OK, we believe it. If the first block in
599 * the i_block array is 0, this cannot be a directory. If the
600 * inode is extent-mapped, it is still the case that the latter
601 * cannot be 0 - the magic number in the extent header would make
604 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
605 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
609 * Check the block numbers in the i_block array for validity:
610 * zero blocks are skipped (but the first one cannot be zero -
611 * see above), other blocks are checked against the first and
612 * max data blocks (from the the superblock) and against the
613 * block bitmap. Any invalid block found means this cannot be
616 * If there are non-zero blocks past the fourth entry, then
617 * this cannot be a device file: we remember that for the next
620 * For extent mapped files, we don't do any sanity checking:
621 * just try to get the phys block of logical block 0 and run
625 extent_fs = (ctx->fs->super->s_feature_incompat &
626 EXT3_FEATURE_INCOMPAT_EXTENTS);
627 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
629 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
632 /* device files are never extent mapped */
635 for (i=0; i < EXT2_N_BLOCKS; i++) {
636 blk = inode->i_block[i];
642 if (blk < ctx->fs->super->s_first_data_block ||
643 blk >= ext2fs_blocks_count(ctx->fs->super) ||
644 ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
646 return; /* Invalid block, can't be dir */
648 blk = inode->i_block[0];
652 * If the mode says this is a device file and the i_links_count field
653 * is sane and we have not ruled it out as a device file previously,
654 * we declare it a device file, not a directory.
656 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
657 (inode->i_links_count == 1) && !not_device)
660 /* read the first block */
661 ehandler_operation(_("reading directory block"));
662 retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0);
663 ehandler_operation(0);
667 dirent = (struct ext2_dir_entry *) buf;
668 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
671 if (((dirent->name_len & 0xFF) != 1) ||
672 (dirent->name[0] != '.') ||
673 (dirent->inode != pctx->ino) ||
676 (rec_len >= ctx->fs->blocksize - 12))
679 dirent = (struct ext2_dir_entry *) (buf + rec_len);
680 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
683 if (((dirent->name_len & 0xFF) != 2) ||
684 (dirent->name[0] != '.') ||
685 (dirent->name[1] != '.') ||
690 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
691 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
692 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
693 e2fsck_write_inode_full(ctx, pctx->ino, inode,
694 EXT2_INODE_SIZE(ctx->fs->super),
695 "check_is_really_dir");
699 void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
702 unsigned int threshold;
710 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
712 profile_get_uint(ctx->profile, "scratch_files",
713 "numdirs_threshold", 0, 0, &threshold);
714 profile_get_boolean(ctx->profile, "scratch_files",
715 "icount", 0, 1, &enable);
717 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
719 num_dirs = 1024; /* Guess */
721 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
722 (threshold && num_dirs <= threshold))
725 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
730 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
731 struct problem_context *pctx, int needed_size)
733 struct ext2_ext_attr_header *header;
734 struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
735 char *start, name[4096], block_buf[4096];
736 int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
737 int in_inode = 1, error;
738 unsigned int freed_bytes = inode->i_extra_isize;
740 entry_ino = &IHDR(inode)->h_first_entry[0];
741 start = (char *)entry_ino;
743 if (inode->i_file_acl) {
744 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
746 /* We have already checked this block, shouldn't happen */
748 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
751 header = BHDR(block_buf);
752 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
753 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
757 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
760 len = sizeof(entry->e_name);
761 entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
763 while (freed_bytes < needed_size) {
764 if (entry_size && name[0] != '\0') {
766 if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
767 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
768 EXT2_EXT_ATTR_SIZE(entry->e_value_size);
769 error = ext2fs_attr_set(ctx->fs, pctx->ino,
770 (struct ext2_inode *)inode,
771 index, name, 0, 0, 0);
773 freed_bytes += ea_size;
776 len = sizeof(entry->e_name);
777 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
778 entry = EXT2_EXT_ATTR_NEXT(entry);
779 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
782 len = sizeof(entry->e_name);
783 entry_size = ext2fs_attr_get_next_attr(entry,
784 index, name, len, 1);
789 if (!entry && index < EXT2_ATTR_INDEX_MAX)
790 entry = (struct ext2_ext_attr_entry *)start;
800 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
801 struct problem_context *pctx)
803 int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
807 retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
808 ctx->want_extra_isize, &ret,
810 if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
811 goto mark_expand_eisize_map;
813 e2fsck_write_inode_full(ctx, pctx->ino,
814 (struct ext2_inode *)inode,
815 EXT2_INODE_SIZE(ctx->fs->super),
820 if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
821 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
822 fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
823 ctx->flags |= E2F_FLAG_ABORT;
828 pctx->num = ctx->fs->super->s_min_extra_isize;
829 fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
833 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
835 if (retval >= ctx->want_extra_isize)
838 needed_size -= retval;
841 * We loop here until either the user deletes EA(s) or
842 * EXTRA_ISIZE feature is disabled.
844 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
845 ctx->fs->super->s_feature_ro_compat &=
846 ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
847 ext2fs_mark_super_dirty(ctx->fs);
851 ctx->fs_unexpanded_inodes++;
853 /* No EA was deleted, inode cannot be expanded */
857 mark_expand_eisize_map:
858 if (!ctx->expand_eisize_map) {
859 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
860 _("expand extrz isize map"),
861 &ctx->expand_eisize_map);
863 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
869 /* Add this inode to the expand_eisize_map */
870 ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
874 static void reserve_block_for_root_repair(e2fsck_t ctx)
878 ext2_filsys fs = ctx->fs;
880 ctx->root_repair_block = 0;
881 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
884 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
887 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
888 ctx->root_repair_block = blk;
891 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
895 ext2_filsys fs = ctx->fs;
896 static const char name[] = "lost+found";
899 ctx->lnf_repair_block = 0;
900 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
903 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
906 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
907 ctx->lnf_repair_block = blk;
910 void e2fsck_pass1(e2fsck_t ctx)
914 ext2_filsys fs = ctx->fs;
916 struct ext2_inode *inode = NULL;
917 ext2_inode_scan scan = NULL;
918 char *block_buf = NULL;
919 #ifdef RESOURCE_TRACK
920 struct resource_track rtrack;
922 unsigned char frag, fsize;
923 struct problem_context pctx;
924 struct scan_callback_struct scan_struct;
925 struct ext2_super_block *sb = ctx->fs->super;
927 unsigned int save_type;
928 int imagic_fs, extent_fs;
929 int low_dtime_check = 1;
934 init_resource_track(&rtrack, ctx->fs->io);
935 clear_problem_context(&pctx);
937 if (!(ctx->options & E2F_OPT_PREEN))
938 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
940 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
941 !(ctx->options & E2F_OPT_NO)) {
942 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
943 ctx->dirs_to_hash = 0;
947 mtrace_print("Pass 1");
950 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
952 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
953 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
954 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
955 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
956 max_sizes = (max_sizes * (1UL << i));
957 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
961 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
962 extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
965 * Allocate bitmaps structures
967 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
968 EXT2FS_BMAP64_RBTREE,
970 &ctx->inode_used_map);
973 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
974 ctx->flags |= E2F_FLAG_ABORT;
977 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
978 _("directory inode map"),
979 EXT2FS_BMAP64_AUTODIR,
980 "inode_dir_map", &ctx->inode_dir_map);
983 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
984 ctx->flags |= E2F_FLAG_ABORT;
987 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
988 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
989 "inode_reg_map", &ctx->inode_reg_map);
992 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
993 ctx->flags |= E2F_FLAG_ABORT;
996 pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
997 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
998 "block_found_map", &ctx->block_found_map);
1001 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1002 ctx->flags |= E2F_FLAG_ABORT;
1005 e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
1006 if (!ctx->inode_link_info) {
1007 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
1008 "inode_link_info", &save_type);
1009 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
1010 &ctx->inode_link_info);
1011 fs->default_bitmap_type = save_type;
1015 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1016 ctx->flags |= E2F_FLAG_ABORT;
1019 inode_size = EXT2_INODE_SIZE(fs->super);
1020 inode = (struct ext2_inode *)
1021 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
1023 inodes_to_process = (struct process_inode_block *)
1024 e2fsck_allocate_memory(ctx,
1025 (ctx->process_inode_size *
1026 sizeof(struct process_inode_block)),
1027 "array of inodes to process");
1028 process_inode_count = 0;
1030 pctx.errcode = ext2fs_init_dblist(fs, 0);
1032 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1033 ctx->flags |= E2F_FLAG_ABORT;
1038 * If the last orphan field is set, clear it, since the pass1
1039 * processing will automatically find and clear the orphans.
1040 * In the future, we may want to try using the last_orphan
1041 * linked list ourselves, but for now, we clear it so that the
1042 * ext3 mount code won't get confused.
1044 if (!(ctx->options & E2F_OPT_READONLY)) {
1045 if (fs->super->s_last_orphan) {
1046 fs->super->s_last_orphan = 0;
1047 ext2fs_mark_super_dirty(fs);
1051 mark_table_blocks(ctx);
1052 pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
1053 &ctx->block_found_map);
1055 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1056 ctx->flags |= E2F_FLAG_ABORT;
1059 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1060 "block interate buffer");
1061 e2fsck_use_inode_shortcuts(ctx, 1);
1062 e2fsck_intercept_block_allocations(ctx);
1063 old_op = ehandler_operation(_("opening inode scan"));
1064 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1066 ehandler_operation(old_op);
1068 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1069 ctx->flags |= E2F_FLAG_ABORT;
1072 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
1073 ctx->stashed_inode = inode;
1074 scan_struct.ctx = ctx;
1075 scan_struct.block_buf = block_buf;
1076 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1077 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1078 ctx->fs->group_desc_count)))
1080 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
1081 (fs->super->s_mtime < fs->super->s_inodes_count) ||
1082 (fs->super->s_mkfs_time &&
1083 fs->super->s_mkfs_time < fs->super->s_inodes_count))
1084 low_dtime_check = 0;
1086 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
1087 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1088 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1089 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1090 fs->super->s_mmp_block);
1093 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
1094 if (e2fsck_mmp_update(fs))
1095 fatal_error(ctx, 0);
1097 old_op = ehandler_operation(_("getting next inode from scan"));
1098 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1100 ehandler_operation(old_op);
1101 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1103 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1104 if (!ctx->inode_bb_map)
1106 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1107 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1111 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1112 ctx->flags |= E2F_FLAG_ABORT;
1119 ctx->stashed_ino = ino;
1120 if (inode->i_links_count) {
1121 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1122 ino, inode->i_links_count);
1124 pctx.num = inode->i_links_count;
1125 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1126 ctx->flags |= E2F_FLAG_ABORT;
1132 * Test for incorrect extent flag settings.
1134 * On big-endian machines we must be careful:
1135 * When the inode is read, the i_block array is not swapped
1136 * if the extent flag is set. Therefore if we are testing
1137 * for or fixing a wrongly-set flag, we must potentially
1138 * (un)swap before testing, or after fixing.
1142 * In this case the extents flag was set when read, so
1143 * extent_header_verify is ok. If the inode is cleared,
1144 * no need to swap... so no extra swapping here.
1146 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
1147 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1148 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1149 if ((ext2fs_extent_header_verify(inode->i_block,
1150 sizeof(inode->i_block)) == 0) &&
1151 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1152 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
1153 ext2fs_mark_super_dirty(fs);
1155 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1157 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1158 if (ino == EXT2_BAD_INO)
1159 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1166 * For big-endian machines:
1167 * If the inode didn't have the extents flag set when it
1168 * was read, then the i_blocks array was swapped. To test
1169 * as an extents header, we must swap it back first.
1170 * IF we then set the extents flag, the entire i_block
1171 * array must be un/re-swapped to make it proper extents data.
1173 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1174 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1175 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1176 (LINUX_S_ISREG(inode->i_mode) ||
1177 LINUX_S_ISDIR(inode->i_mode))) {
1179 #ifdef WORDS_BIGENDIAN
1180 __u32 tmp_block[EXT2_N_BLOCKS];
1182 for (i = 0; i < EXT2_N_BLOCKS; i++)
1183 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1186 ehp = inode->i_block;
1188 if ((ext2fs_extent_header_verify(ehp,
1189 sizeof(inode->i_block)) == 0)) {
1190 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1191 if (fix_problem(ctx, PR_1_UNSET_EXTENT_FL,
1193 inode->i_flags |= EXT4_EXTENTS_FL;
1194 #ifdef WORDS_BIGENDIAN
1195 memcpy(inode->i_block, tmp_block,
1196 sizeof(inode->i_block));
1198 e2fsck_write_inode(ctx, ino, inode,
1204 if (ino == EXT2_BAD_INO) {
1205 struct process_block_struct pb;
1207 if ((inode->i_mode || inode->i_uid || inode->i_gid ||
1208 inode->i_links_count || inode->i_file_acl) &&
1209 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1210 memset(inode, 0, sizeof(struct ext2_inode));
1211 e2fsck_write_inode(ctx, ino, inode,
1215 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1216 &pb.fs_meta_blocks);
1219 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1220 ctx->flags |= E2F_FLAG_ABORT;
1223 pb.ino = EXT2_BAD_INO;
1224 pb.num_blocks = pb.last_block = 0;
1225 pb.last_db_block = -1;
1226 pb.num_illegal_blocks = 0;
1227 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1228 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1232 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1233 block_buf, process_bad_block, &pb);
1234 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1236 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1237 ctx->flags |= E2F_FLAG_ABORT;
1241 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1242 ctx->flags |= E2F_FLAG_ABORT;
1245 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1246 clear_problem_context(&pctx);
1248 } else if (ino == EXT2_ROOT_INO) {
1250 * Make sure the root inode is a directory; if
1251 * not, offer to clear it. It will be
1252 * regnerated in pass #3.
1254 if (!LINUX_S_ISDIR(inode->i_mode)) {
1255 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1259 * If dtime is set, offer to clear it. mke2fs
1260 * version 0.2b created filesystems with the
1261 * dtime field set for the root and lost+found
1262 * directories. We won't worry about
1263 * /lost+found, since that can be regenerated
1264 * easily. But we will fix the root directory
1265 * as a special case.
1267 if (inode->i_dtime && inode->i_links_count) {
1268 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1269 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1271 e2fsck_write_inode(ctx, ino, inode,
1275 } else if (ino == EXT2_JOURNAL_INO) {
1276 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1277 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1278 if (!LINUX_S_ISREG(inode->i_mode) &&
1279 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1281 inode->i_mode = LINUX_S_IFREG;
1282 e2fsck_write_inode(ctx, ino, inode,
1285 check_blocks(ctx, &pctx, block_buf);
1288 if ((inode->i_links_count ||
1289 inode->i_blocks || inode->i_block[0]) &&
1290 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1292 memset(inode, 0, inode_size);
1293 ext2fs_icount_store(ctx->inode_link_info,
1295 e2fsck_write_inode_full(ctx, ino, inode,
1296 inode_size, "pass1");
1298 } else if ((ino == EXT4_USR_QUOTA_INO) ||
1299 (ino == EXT4_GRP_QUOTA_INO)) {
1300 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1301 if ((fs->super->s_feature_ro_compat &
1302 EXT4_FEATURE_RO_COMPAT_QUOTA) &&
1303 ((fs->super->s_usr_quota_inum == ino) ||
1304 (fs->super->s_grp_quota_inum == ino))) {
1305 if (!LINUX_S_ISREG(inode->i_mode) &&
1306 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1308 inode->i_mode = LINUX_S_IFREG;
1309 e2fsck_write_inode(ctx, ino, inode,
1312 check_blocks(ctx, &pctx, block_buf);
1315 if ((inode->i_links_count ||
1316 inode->i_blocks || inode->i_block[0]) &&
1317 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1319 memset(inode, 0, inode_size);
1320 ext2fs_icount_store(ctx->inode_link_info,
1322 e2fsck_write_inode_full(ctx, ino, inode,
1323 inode_size, "pass1");
1325 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1326 problem_t problem = 0;
1328 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1329 if (ino == EXT2_BOOT_LOADER_INO) {
1330 if (LINUX_S_ISDIR(inode->i_mode))
1331 problem = PR_1_RESERVED_BAD_MODE;
1332 } else if (ino == EXT2_RESIZE_INO) {
1333 if (inode->i_mode &&
1334 !LINUX_S_ISREG(inode->i_mode))
1335 problem = PR_1_RESERVED_BAD_MODE;
1337 if (inode->i_mode != 0)
1338 problem = PR_1_RESERVED_BAD_MODE;
1341 if (fix_problem(ctx, problem, &pctx)) {
1343 e2fsck_write_inode(ctx, ino, inode,
1347 check_blocks(ctx, &pctx, block_buf);
1352 * Check for inodes who might have been part of the
1353 * orphaned list linked list. They should have gotten
1354 * dealt with by now, unless the list had somehow been
1357 * FIXME: In the future, inodes which are still in use
1358 * (and which are therefore) pending truncation should
1359 * be handled specially. Right now we just clear the
1360 * dtime field, and the normal e2fsck handling of
1361 * inodes where i_size and the inode blocks are
1362 * inconsistent is to fix i_size, instead of releasing
1363 * the extra blocks. This won't catch the inodes that
1364 * was at the end of the orphan list, but it's better
1365 * than nothing. The right answer is that there
1366 * shouldn't be any bugs in the orphan list handling. :-)
1368 if (inode->i_dtime && low_dtime_check &&
1369 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1370 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1371 inode->i_dtime = inode->i_links_count ?
1373 e2fsck_write_inode(ctx, ino, inode,
1379 * This code assumes that deleted inodes have
1380 * i_links_count set to 0.
1382 if (!inode->i_links_count) {
1383 if (!inode->i_dtime && inode->i_mode) {
1384 if (fix_problem(ctx,
1385 PR_1_ZERO_DTIME, &pctx)) {
1386 inode->i_dtime = ctx->now;
1387 e2fsck_write_inode(ctx, ino, inode,
1394 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1395 * deleted files. Oops.
1397 * Since all new ext2 implementations get this right,
1398 * we now assume that the case of non-zero
1399 * i_links_count and non-zero dtime means that we
1400 * should keep the file, not delete it.
1403 if (inode->i_dtime) {
1404 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1405 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1407 e2fsck_write_inode(ctx, ino, inode, "pass1");
1411 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1412 switch (fs->super->s_creator_os) {
1414 frag = inode->osd2.hurd2.h_i_frag;
1415 fsize = inode->osd2.hurd2.h_i_fsize;
1421 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1422 if (inode->i_faddr || frag || fsize ||
1423 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
1424 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1425 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1426 !(fs->super->s_feature_incompat &
1427 EXT4_FEATURE_INCOMPAT_64BIT) &&
1428 inode->osd2.linux2.l_i_file_acl_high != 0)
1429 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1430 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1431 !(fs->super->s_feature_ro_compat &
1432 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1433 (inode->osd2.linux2.l_i_blocks_hi != 0))
1434 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1435 if (inode->i_flags & EXT2_IMAGIC_FL) {
1437 if (!ctx->inode_imagic_map)
1438 alloc_imagic_map(ctx);
1439 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1442 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1443 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1444 inode->i_flags &= ~EXT2_IMAGIC_FL;
1445 e2fsck_write_inode(ctx, ino,
1451 check_inode_extra_space(ctx, &pctx);
1452 check_is_really_dir(ctx, &pctx, block_buf);
1455 * ext2fs_inode_has_valid_blocks2 does not actually look
1456 * at i_block[] values, so not endian-sensitive here.
1458 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1459 LINUX_S_ISLNK(inode->i_mode) &&
1460 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1461 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1462 inode->i_flags &= ~EXT4_EXTENTS_FL;
1463 e2fsck_write_inode(ctx, ino, inode, "pass1");
1466 if (LINUX_S_ISDIR(inode->i_mode)) {
1467 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1468 e2fsck_add_dir_info(ctx, ino, 0);
1469 ctx->fs_directory_count++;
1470 } else if (LINUX_S_ISREG (inode->i_mode)) {
1471 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1472 ctx->fs_regular_count++;
1473 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1474 e2fsck_pass1_check_device_inode(fs, inode)) {
1475 check_immutable(ctx, &pctx);
1476 check_size(ctx, &pctx);
1477 ctx->fs_chardev_count++;
1478 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1479 e2fsck_pass1_check_device_inode(fs, inode)) {
1480 check_immutable(ctx, &pctx);
1481 check_size(ctx, &pctx);
1482 ctx->fs_blockdev_count++;
1483 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1484 check_symlink(ctx, &pctx, ino, inode, block_buf)) {
1485 check_immutable(ctx, &pctx);
1486 ctx->fs_symlinks_count++;
1487 if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1488 ctx->fs_fast_symlinks_count++;
1489 check_blocks(ctx, &pctx, block_buf);
1493 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1494 e2fsck_pass1_check_device_inode(fs, inode)) {
1495 check_immutable(ctx, &pctx);
1496 check_size(ctx, &pctx);
1497 ctx->fs_fifo_count++;
1498 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1499 e2fsck_pass1_check_device_inode(fs, inode)) {
1500 check_immutable(ctx, &pctx);
1501 check_size(ctx, &pctx);
1502 ctx->fs_sockets_count++;
1504 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1507 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
1508 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1509 else if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime,
1511 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1513 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
1514 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1515 else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
1517 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1519 /* i_crtime is checked in check_inode_extra_space() */
1521 if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
1522 if (inode->i_block[EXT2_IND_BLOCK])
1523 ctx->fs_ind_count++;
1524 if (inode->i_block[EXT2_DIND_BLOCK])
1525 ctx->fs_dind_count++;
1526 if (inode->i_block[EXT2_TIND_BLOCK])
1527 ctx->fs_tind_count++;
1529 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1530 (inode->i_block[EXT2_IND_BLOCK] ||
1531 inode->i_block[EXT2_DIND_BLOCK] ||
1532 inode->i_block[EXT2_TIND_BLOCK] ||
1533 ext2fs_file_acl_block(fs, inode))) {
1534 inodes_to_process[process_inode_count].ino = ino;
1535 inodes_to_process[process_inode_count].inode = *inode;
1536 process_inode_count++;
1538 check_blocks(ctx, &pctx, block_buf);
1540 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
1541 struct ext2_inode_large *inode_l;
1543 inode_l = (struct ext2_inode_large *)inode;
1545 if (inode_l->i_extra_isize < ctx->want_extra_isize) {
1546 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
1547 inode_exp = e2fsck_pass1_expand_eisize(ctx,
1551 if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
1553 ctx->min_extra_isize = inode_l->i_extra_isize;
1556 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1559 if (process_inode_count >= ctx->process_inode_size) {
1560 process_inodes(ctx, block_buf);
1562 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1566 process_inodes(ctx, block_buf);
1567 ext2fs_close_inode_scan(scan);
1570 reserve_block_for_root_repair(ctx);
1571 reserve_block_for_lnf_repair(ctx);
1574 * If any extended attribute blocks' reference counts need to
1575 * be adjusted, either up (ctx->refcount_extra), or down
1576 * (ctx->refcount), then fix them.
1578 if (ctx->refcount) {
1579 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1580 ea_refcount_free(ctx->refcount);
1583 if (ctx->refcount_extra) {
1584 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1586 ea_refcount_free(ctx->refcount_extra);
1587 ctx->refcount_extra = 0;
1590 if (ctx->invalid_bitmaps)
1591 handle_fs_bad_blocks(ctx);
1593 /* We don't need the block_ea_map any more */
1594 if (ctx->block_ea_map) {
1595 ext2fs_free_block_bitmap(ctx->block_ea_map);
1596 ctx->block_ea_map = 0;
1599 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1600 clear_problem_context(&pctx);
1601 pctx.errcode = ext2fs_create_resize_inode(fs);
1603 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1605 ctx->flags |= E2F_FLAG_ABORT;
1610 if (!pctx.errcode) {
1611 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1613 inode->i_mtime = ctx->now;
1614 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1617 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1620 if (ctx->flags & E2F_FLAG_RESTART) {
1622 * Only the master copy of the superblock and block
1623 * group descriptors are going to be written during a
1624 * restart, so set the superblock to be used to be the
1625 * master superblock.
1627 ctx->use_superblock = 0;
1632 if (ctx->block_dup_map) {
1633 if (ctx->options & E2F_OPT_PREEN) {
1634 clear_problem_context(&pctx);
1635 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1637 e2fsck_pass1_dupblocks(ctx, block_buf);
1639 ext2fs_free_mem(&inodes_to_process);
1641 e2fsck_use_inode_shortcuts(ctx, 0);
1644 ext2fs_close_inode_scan(scan);
1646 ext2fs_free_mem(&block_buf);
1648 ext2fs_free_mem(&inode);
1650 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
1651 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1655 * When the inode_scan routines call this callback at the end of the
1656 * glock group, call process_inodes.
1658 static errcode_t scan_callback(ext2_filsys fs,
1659 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1660 dgrp_t group, void * priv_data)
1662 struct scan_callback_struct *scan_struct;
1665 scan_struct = (struct scan_callback_struct *) priv_data;
1666 ctx = scan_struct->ctx;
1668 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1671 if ((ctx->progress)(ctx, 1, group+1,
1672 ctx->fs->group_desc_count))
1673 return EXT2_ET_CANCEL_REQUESTED;
1679 * Process the inodes in the "inodes to process" list.
1681 static void process_inodes(e2fsck_t ctx, char *block_buf)
1684 struct ext2_inode *old_stashed_inode;
1685 ext2_ino_t old_stashed_ino;
1686 const char *old_operation;
1688 struct problem_context pctx;
1691 printf("begin process_inodes: ");
1693 if (process_inode_count == 0)
1695 old_operation = ehandler_operation(0);
1696 old_stashed_inode = ctx->stashed_inode;
1697 old_stashed_ino = ctx->stashed_ino;
1698 qsort(inodes_to_process, process_inode_count,
1699 sizeof(struct process_inode_block), process_inode_cmp);
1700 clear_problem_context(&pctx);
1701 for (i=0; i < process_inode_count; i++) {
1702 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1703 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1706 printf("%u ", pctx.ino);
1708 sprintf(buf, _("reading indirect blocks of inode %u"),
1710 ehandler_operation(buf);
1711 check_blocks(ctx, &pctx, block_buf);
1712 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1715 ctx->stashed_inode = old_stashed_inode;
1716 ctx->stashed_ino = old_stashed_ino;
1717 process_inode_count = 0;
1719 printf("end process inodes\n");
1721 ehandler_operation(old_operation);
1724 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1726 const struct process_inode_block *ib_a =
1727 (const struct process_inode_block *) a;
1728 const struct process_inode_block *ib_b =
1729 (const struct process_inode_block *) b;
1732 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1733 ib_b->inode.i_block[EXT2_IND_BLOCK]);
1736 * We only call process_inodes() for non-extent
1737 * inodes, so it's OK to pass NULL to
1738 * ext2fs_file_acl_block() here.
1740 ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
1741 ext2fs_file_acl_block(0, &(ib_b->inode));
1743 ret = ib_a->ino - ib_b->ino;
1748 * Mark an inode as being bad and increment its badness counter.
1750 void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
1751 const char *func, const int line)
1753 struct problem_context pctx;
1756 if (!ctx->inode_badness) {
1757 clear_problem_context(&pctx);
1759 pctx.errcode = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
1760 &ctx->inode_badness);
1762 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1763 ctx->flags |= E2F_FLAG_ABORT;
1767 ext2fs_icount_fetch(ctx->inode_badness, ino, &result);
1768 ext2fs_icount_store(ctx->inode_badness, ino, count + result);
1770 if (ctx->options & E2F_OPT_DEBUG)
1771 fprintf(stderr, "%s:%d: increase inode %lu badness %u to %u\n",
1772 func, line, (unsigned long)ino, result, count + result);
1777 * This procedure will allocate the inode "bb" (badblock) map table
1779 static void alloc_bb_map(e2fsck_t ctx)
1781 struct problem_context pctx;
1783 clear_problem_context(&pctx);
1784 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1785 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
1786 "inode_bb_map", &ctx->inode_bb_map);
1789 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1790 /* Should never get here */
1791 ctx->flags |= E2F_FLAG_ABORT;
1797 * This procedure will allocate the inode imagic table
1799 static void alloc_imagic_map(e2fsck_t ctx)
1801 struct problem_context pctx;
1803 clear_problem_context(&pctx);
1804 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1805 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
1806 "inode_imagic_map", &ctx->inode_imagic_map);
1809 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1810 /* Should never get here */
1811 ctx->flags |= E2F_FLAG_ABORT;
1817 * Marks a block as in use, setting the dup_map if it's been set
1818 * already. Called by process_block and process_bad_block.
1820 * WARNING: Assumes checks have already been done to make sure block
1821 * is valid. This is true in both process_block and process_bad_block.
1823 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
1825 struct problem_context pctx;
1827 clear_problem_context(&pctx);
1829 if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
1830 if (!ctx->block_dup_map) {
1831 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1832 _("multiply claimed block map"),
1833 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1834 &ctx->block_dup_map);
1837 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1839 /* Should never get here */
1840 ctx->flags |= E2F_FLAG_ABORT;
1844 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
1846 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
1850 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
1853 if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
1854 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
1857 mark_block_used(ctx, block++);
1861 * Adjust the extended attribute block's reference counts at the end
1862 * of pass 1, either by subtracting out references for EA blocks that
1863 * are still referenced in ctx->refcount, or by adding references for
1864 * EA blocks that had extra references as accounted for in
1865 * ctx->refcount_extra.
1867 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1868 char *block_buf, int adjust_sign)
1870 struct ext2_ext_attr_header *header;
1871 struct problem_context pctx;
1872 ext2_filsys fs = ctx->fs;
1877 clear_problem_context(&pctx);
1879 ea_refcount_intr_begin(refcount);
1881 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1884 pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1885 /* We already checked this block, shouldn't happen */
1887 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1890 header = BHDR(block_buf);
1891 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1892 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1896 pctx.blkcount = header->h_refcount;
1897 should_be = header->h_refcount + adjust_sign * count;
1898 pctx.num = should_be;
1899 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1900 header->h_refcount = should_be;
1901 pctx.errcode = ext2fs_write_ext_attr2(fs, blk,
1904 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
1913 * Handle processing the extended attribute blocks
1915 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1918 ext2_filsys fs = ctx->fs;
1919 ext2_ino_t ino = pctx->ino;
1920 struct ext2_inode *inode = pctx->inode;
1923 struct ext2_ext_attr_header *header;
1924 struct ext2_ext_attr_entry *entry;
1926 region_t region = 0;
1929 blk = ext2fs_file_acl_block(fs, inode);
1934 * If the Extended attribute flag isn't set, then a non-zero
1935 * file acl means that the inode is corrupted.
1937 * Or if the extended attribute block is an invalid block,
1938 * then the inode is also corrupted.
1940 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1941 (blk < fs->super->s_first_data_block) ||
1942 (blk >= ext2fs_blocks_count(fs->super))) {
1943 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1944 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1948 /* If ea bitmap hasn't been allocated, create it */
1949 if (!ctx->block_ea_map) {
1950 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
1951 _("ext attr block map"),
1952 EXT2FS_BMAP64_RBTREE, "block_ea_map",
1953 &ctx->block_ea_map);
1954 if (pctx->errcode) {
1956 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1957 ctx->flags |= E2F_FLAG_ABORT;
1962 /* Create the EA refcount structure if necessary */
1963 if (!ctx->refcount) {
1964 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1965 if (pctx->errcode) {
1967 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1968 ctx->flags |= E2F_FLAG_ABORT;
1974 /* Debugging text */
1975 printf("Inode %u has EA block %u\n", ino, blk);
1978 /* Have we seen this EA block before? */
1979 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
1980 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1982 /* Ooops, this EA was referenced more than it stated */
1983 if (!ctx->refcount_extra) {
1984 pctx->errcode = ea_refcount_create(0,
1985 &ctx->refcount_extra);
1986 if (pctx->errcode) {
1988 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1989 ctx->flags |= E2F_FLAG_ABORT;
1993 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1998 * OK, we haven't seen this EA block yet. So we need to
2002 pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
2003 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
2005 header = BHDR(block_buf);
2006 pctx->blk = ext2fs_file_acl_block(fs, inode);
2007 if (((ctx->ext_attr_ver == 1) &&
2008 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
2009 ((ctx->ext_attr_ver == 2) &&
2010 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
2011 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
2015 if (header->h_blocks != 1) {
2016 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
2020 region = region_create(0, fs->blocksize);
2022 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
2023 ctx->flags |= E2F_FLAG_ABORT;
2026 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
2027 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2031 entry = (struct ext2_ext_attr_entry *)(header+1);
2032 end = block_buf + fs->blocksize;
2033 while ((char *)entry < end && *(__u32 *)entry) {
2036 if (region_allocate(region, (char *)entry - (char *)header,
2037 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
2038 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2042 if ((ctx->ext_attr_ver == 1 &&
2043 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
2044 (ctx->ext_attr_ver == 2 &&
2045 entry->e_name_index == 0)) {
2046 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
2050 if (entry->e_value_inum == 0) {
2051 if (entry->e_value_offs + entry->e_value_size >
2053 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
2057 if (entry->e_value_size &&
2058 region_allocate(region, entry->e_value_offs,
2059 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
2060 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
2065 int i_file_acl_deleted = 0;
2067 ret = check_large_ea_inode(ctx, entry, pctx,
2068 &i_file_acl_deleted);
2070 mark_inode_ea_map(ctx, pctx,
2071 entry->e_value_inum);
2073 if (i_file_acl_deleted)
2077 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
2078 entry->e_value_offs);
2080 if (entry->e_hash != hash) {
2081 pctx->num = entry->e_hash;
2082 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
2084 entry->e_hash = hash;
2087 entry = EXT2_EXT_ATTR_NEXT(entry);
2089 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
2090 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2093 region_free(region);
2095 count = header->h_refcount - 1;
2097 ea_refcount_store(ctx->refcount, blk, count);
2098 mark_block_used(ctx, blk);
2099 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
2104 region_free(region);
2105 ext2fs_file_acl_block_set(fs, inode, 0);
2106 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
2110 /* Returns 1 if bad htree, 0 if OK */
2111 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
2112 ext2_ino_t ino, struct ext2_inode *inode,
2115 struct ext2_dx_root_info *root;
2116 ext2_filsys fs = ctx->fs;
2120 if ((!LINUX_S_ISDIR(inode->i_mode) &&
2121 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
2122 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX))) {
2123 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2124 if (fix_problem(ctx, PR_1_HTREE_SET, pctx))
2128 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
2130 if ((pctx->errcode) ||
2132 (blk < fs->super->s_first_data_block) ||
2133 (blk >= ext2fs_blocks_count(fs->super))) {
2134 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2135 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2141 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
2143 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2144 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2148 /* XXX should check that beginning matches a directory */
2149 root = get_ext2_dx_root_info(fs, block_buf);
2151 if ((root->reserved_zero || root->info_length < 8) &&
2152 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2155 pctx->num = root->hash_version;
2156 if ((root->hash_version != EXT2_HASH_LEGACY) &&
2157 (root->hash_version != EXT2_HASH_HALF_MD4) &&
2158 (root->hash_version != EXT2_HASH_TEA) &&
2159 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2162 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2163 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2166 pctx->num = root->indirect_levels;
2167 if ((root->indirect_levels > 1) &&
2168 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2174 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2175 struct ext2_inode *inode, int restart_flag,
2179 inode->i_links_count = 0;
2180 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2181 inode->i_dtime = ctx->now;
2183 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2184 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2185 if (ctx->inode_reg_map)
2186 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2187 if (ctx->inode_badness)
2188 ext2fs_icount_store(ctx->inode_badness, ino, 0);
2191 * If the inode was partially accounted for before processing
2192 * was aborted, we need to restart the pass 1 scan.
2194 ctx->flags |= restart_flag;
2196 if (ino == EXT2_BAD_INO)
2197 memset(inode, 0, sizeof(struct ext2_inode));
2199 e2fsck_write_inode(ctx, ino, inode, source);
2203 * Use the multiple-blocks reclamation code to fix alignment problems in
2204 * a bigalloc filesystem. We want a logical cluster to map to *only* one
2205 * physical cluster, and we want the block offsets within that cluster to
2208 static int has_unaligned_cluster_map(e2fsck_t ctx,
2209 blk64_t last_pblk, e2_blkcnt_t last_lblk,
2210 blk64_t pblk, blk64_t lblk)
2212 blk64_t cluster_mask;
2214 if (!ctx->fs->cluster_ratio_bits)
2216 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
2219 * If the block in the logical cluster doesn't align with the block in
2220 * the physical cluster...
2222 if ((lblk & cluster_mask) != (pblk & cluster_mask))
2226 * If we cross a physical cluster boundary within a logical cluster...
2228 if (last_pblk && (lblk & cluster_mask) != 0 &&
2229 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
2230 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
2236 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2237 struct process_block_struct *pb,
2238 blk64_t start_block, blk64_t end_block,
2240 ext2_extent_handle_t ehandle)
2242 struct ext2fs_extent extent;
2243 blk64_t blk, last_lblk;
2244 e2_blkcnt_t blockcnt;
2246 int is_dir, is_leaf;
2248 struct ext2_extent_info info;
2250 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2254 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2256 while (!pctx->errcode && info.num_entries-- > 0) {
2257 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2258 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2259 last_lblk = extent.e_lblk + extent.e_len - 1;
2262 if (extent.e_pblk == 0 ||
2263 extent.e_pblk < ctx->fs->super->s_first_data_block ||
2264 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2265 problem = PR_1_EXTENT_BAD_START_BLK;
2266 else if (extent.e_lblk < start_block)
2267 problem = PR_1_OUT_OF_ORDER_EXTENTS;
2268 else if ((end_block && last_lblk > end_block) &&
2269 (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
2270 last_lblk > eof_block)))
2271 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
2272 else if (is_leaf && extent.e_len == 0)
2273 problem = PR_1_EXTENT_LENGTH_ZERO;
2275 (extent.e_pblk + extent.e_len) >
2276 ext2fs_blocks_count(ctx->fs->super))
2277 problem = PR_1_EXTENT_ENDS_BEYOND;
2278 else if (is_leaf && is_dir &&
2279 ((extent.e_lblk + extent.e_len) >
2280 (1 << (21 - ctx->fs->super->s_log_block_size))))
2281 problem = PR_1_TOOBIG_DIR;
2284 * Uninitialized blocks in a directory? Clear the flag and
2285 * we'll interpret the blocks later.
2287 if (is_dir && problem == 0 &&
2288 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2289 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
2290 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2291 pb->inode_modified = 1;
2292 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2299 /* To ensure that extent is in inode */
2300 if (info.curr_level == 0)
2301 e2fsck_mark_inode_bad(ctx, pctx->ino,
2304 pctx->blk = extent.e_pblk;
2305 pctx->blk2 = extent.e_lblk;
2306 pctx->num = extent.e_len;
2307 pctx->blkcount = extent.e_lblk + extent.e_len;
2308 if (fix_problem(ctx, problem, pctx)) {
2309 if (ctx->invalid_bitmaps) {
2311 * If fsck knows the bitmaps are bad,
2312 * skip to the next extent and
2313 * try to clear this extent again
2314 * after fixing the bitmaps, by
2317 pctx->errcode = ext2fs_extent_get(
2319 EXT2_EXTENT_NEXT_SIB,
2321 ctx->flags |= E2F_FLAG_RESTART_LATER;
2322 if (pctx->errcode ==
2323 EXT2_ET_NO_CURRENT_NODE) {
2329 e2fsck_read_bitmaps(ctx);
2330 pb->inode_modified = 1;
2332 ext2fs_extent_delete(ehandle, 0);
2333 if (pctx->errcode) {
2334 pctx->str = "ext2fs_extent_delete";
2337 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2338 if (pctx->errcode &&
2339 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
2340 pctx->str = "ext2fs_extent_fix_parents";
2343 pctx->errcode = ext2fs_extent_get(ehandle,
2344 EXT2_EXTENT_CURRENT,
2346 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2356 blk64_t lblk = extent.e_lblk;
2358 blk = extent.e_pblk;
2359 pctx->errcode = ext2fs_extent_get(ehandle,
2360 EXT2_EXTENT_DOWN, &extent);
2361 if (pctx->errcode) {
2362 pctx->str = "EXT2_EXTENT_DOWN";
2363 problem = PR_1_EXTENT_HEADER_INVALID;
2364 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
2365 goto report_problem;
2368 /* The next extent should match this index's logical start */
2369 if (extent.e_lblk != lblk) {
2370 struct ext2_extent_info e_info;
2372 ext2fs_extent_get_info(ehandle, &e_info);
2374 pctx->blk2 = extent.e_lblk;
2375 pctx->num = e_info.curr_level - 1;
2376 problem = PR_1_EXTENT_INDEX_START_INVALID;
2377 if (fix_problem(ctx, problem, pctx)) {
2378 pb->inode_modified = 1;
2380 ext2fs_extent_fix_parents(ehandle);
2381 if (pctx->errcode) {
2382 pctx->str = "ext2fs_extent_fix_parents";
2387 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
2388 last_lblk, eof_block, ehandle);
2391 pctx->errcode = ext2fs_extent_get(ehandle,
2392 EXT2_EXTENT_UP, &extent);
2393 if (pctx->errcode) {
2394 pctx->str = "EXT2_EXTENT_UP";
2397 mark_block_used(ctx, blk);
2402 if ((pb->previous_block != 0) &&
2403 (pb->previous_block+1 != extent.e_pblk)) {
2404 if (ctx->options & E2F_OPT_FRAGCHECK) {
2409 else if (pb->is_reg)
2412 printf(("%6lu(%c): expecting %6lu "
2414 "phys %6lu log %lu len %lu\n"),
2415 (unsigned long) pctx->ino, type,
2416 (unsigned long) pb->previous_block+1,
2417 (unsigned long) extent.e_pblk,
2418 (unsigned long) extent.e_lblk,
2419 (unsigned long) extent.e_len);
2424 * If we notice a gap in the logical block mappings of an
2425 * extent-mapped directory, offer to close the hole by
2426 * moving the logical block down, otherwise we'll go mad in
2427 * pass 3 allocating empty directory blocks to fill the hole.
2430 pb->last_block + 1 < (e2_blkcnt_t)extent.e_lblk) {
2433 new_lblk = pb->last_block + 1;
2434 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
2435 new_lblk = ((new_lblk +
2436 EXT2FS_CLUSTER_RATIO(ctx->fs)) &
2437 EXT2FS_CLUSTER_MASK(ctx->fs)) |
2439 EXT2FS_CLUSTER_MASK(ctx->fs));
2440 pctx->blk = extent.e_lblk;
2441 pctx->blk2 = new_lblk;
2442 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
2443 extent.e_lblk = new_lblk;
2444 pb->inode_modified = 1;
2445 pctx->errcode = ext2fs_extent_replace(ehandle,
2447 if (pctx->errcode) {
2451 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2453 goto failed_add_dir_block;
2454 pctx->errcode = ext2fs_extent_goto(ehandle,
2457 goto failed_add_dir_block;
2458 last_lblk = extent.e_lblk + extent.e_len - 1;
2462 while (is_dir && (++pb->last_db_block <
2463 (e2_blkcnt_t) extent.e_lblk)) {
2464 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
2467 if (pctx->errcode) {
2469 pctx->num = pb->last_db_block;
2470 goto failed_add_dir_block;
2473 if (!ctx->fs->cluster_ratio_bits) {
2474 mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
2475 pb->num_blocks += extent.e_len;
2477 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
2479 blk++, blockcnt++, i++) {
2480 if (ctx->fs->cluster_ratio_bits &&
2481 !(pb->previous_block &&
2482 (EXT2FS_B2C(ctx->fs, blk) ==
2483 EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
2484 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2485 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2486 mark_block_used(ctx, blk);
2489 if (has_unaligned_cluster_map(ctx, pb->previous_block,
2490 pb->last_block, blk,
2492 pctx->blk = blockcnt;
2494 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2495 mark_block_used(ctx, blk);
2496 mark_block_used(ctx, blk);
2498 pb->last_block = blockcnt;
2499 pb->previous_block = blk;
2502 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
2503 if (pctx->errcode) {
2505 pctx->num = blockcnt;
2506 failed_add_dir_block:
2507 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2508 /* Should never get here */
2509 ctx->flags |= E2F_FLAG_ABORT;
2514 if (is_dir && extent.e_len > 0)
2515 pb->last_db_block = blockcnt - 1;
2516 pb->previous_block = extent.e_pblk + extent.e_len - 1;
2517 start_block = pb->last_block = last_lblk;
2518 if (is_leaf && !is_dir &&
2519 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
2520 pb->last_init_lblock = last_lblk;
2522 pctx->errcode = ext2fs_extent_get(ehandle,
2523 EXT2_EXTENT_NEXT_SIB,
2526 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
2530 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
2531 struct process_block_struct *pb)
2533 struct ext2_extent_info info;
2534 struct ext2_inode *inode = pctx->inode;
2535 ext2_extent_handle_t ehandle;
2536 ext2_filsys fs = ctx->fs;
2537 ext2_ino_t ino = pctx->ino;
2541 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
2542 if (pctx->errcode) {
2543 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
2544 e2fsck_clear_inode(ctx, ino, inode, 0,
2545 "check_blocks_extents");
2550 retval = ext2fs_extent_get_info(ehandle, &info);
2552 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
2553 info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
2554 ctx->extent_depth_count[info.max_depth]++;
2557 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
2558 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
2559 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle);
2560 if (pctx->errcode &&
2561 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
2563 inode->i_blocks = 0;
2564 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2565 "check_blocks_extents");
2568 ext2fs_extent_free(ehandle);
2572 * This subroutine is called on each inode to account for all of the
2573 * blocks used by that inode.
2575 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
2578 ext2_filsys fs = ctx->fs;
2579 struct process_block_struct pb;
2580 ext2_ino_t ino = pctx->ino;
2581 struct ext2_inode *inode = pctx->inode;
2582 unsigned bad_size = 0;
2583 int dirty_inode = 0;
2590 pb.last_init_lblock = -1;
2591 pb.last_db_block = -1;
2592 pb.num_illegal_blocks = 0;
2593 pb.suppress = 0; pb.clear = 0;
2596 pb.previous_block = 0;
2597 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2598 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
2599 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
2603 pb.inode_modified = 0;
2607 extent_fs = (ctx->fs->super->s_feature_incompat &
2608 EXT3_FEATURE_INCOMPAT_EXTENTS);
2610 if (inode->i_flags & EXT2_COMPRBLK_FL) {
2611 if (fs->super->s_feature_incompat &
2612 EXT2_FEATURE_INCOMPAT_COMPRESSION)
2615 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2616 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
2617 inode->i_flags &= ~EXT2_COMPRBLK_FL;
2623 if (ext2fs_file_acl_block(fs, inode) &&
2624 check_ext_attr(ctx, pctx, block_buf)) {
2625 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2630 if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
2631 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
2632 check_blocks_extents(ctx, pctx, &pb);
2635 * If we've modified the inode, write it out before
2636 * iterate() tries to use it.
2639 e2fsck_write_inode(ctx, ino, inode,
2643 pctx->errcode = ext2fs_block_iterate3(fs, ino,
2644 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
2645 block_buf, process_block, &pb);
2647 * We do not have uninitialized extents in non extent
2650 pb.last_init_lblock = pb.last_block;
2652 * If iterate() changed a block mapping, we have to
2653 * re-read the inode. If we decide to clear the
2654 * inode after clearing some stuff, we'll re-write the
2655 * bad mappings into the inode!
2657 if (pb.inode_modified)
2658 e2fsck_read_inode(ctx, ino, inode,
2662 end_problem_latch(ctx, PR_LATCH_BLOCK);
2663 end_problem_latch(ctx, PR_LATCH_TOOBIG);
2664 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2667 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
2669 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
2670 if (LINUX_S_ISDIR(inode->i_mode))
2671 ctx->fs_fragmented_dir++;
2673 ctx->fs_fragmented++;
2677 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2682 if (inode->i_flags & EXT2_INDEX_FL) {
2683 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
2684 inode->i_flags &= ~EXT2_INDEX_FL;
2688 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
2693 if (!pb.num_blocks && pb.is_dir) {
2695 * The mode might be in-correct. Increasing the badness by
2696 * small amount won't hurt much.
2698 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2699 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
2700 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
2701 ctx->fs_directory_count--;
2706 if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
2707 quota_data_add(ctx->qctx, inode, ino,
2708 pb.num_blocks * fs->blocksize);
2709 quota_data_inodes(ctx->qctx, inode, ino, +1);
2712 if (!(fs->super->s_feature_ro_compat &
2713 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
2714 !(inode->i_flags & EXT4_HUGE_FILE_FL))
2715 pb.num_blocks *= (fs->blocksize / 512);
2716 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
2718 printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
2719 ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
2723 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
2724 if (inode->i_size & (fs->blocksize - 1))
2726 else if (nblock > (pb.last_block + 1))
2728 else if (nblock < (pb.last_block + 1)) {
2729 if (((pb.last_block + 1) - nblock) >
2730 fs->super->s_prealloc_dir_blocks)
2734 e2_blkcnt_t blkpg = ctx->blocks_per_page;
2736 size = EXT2_I_SIZE(inode);
2737 if ((pb.last_init_lblock >= 0) &&
2738 /* if size is smaller than expected by the block count,
2739 * allow allocated blocks to end of PAGE_SIZE.
2740 * last_init_lblock is the last in-use block, so it is
2741 * the minimum expected file size. */
2742 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
2743 ((pb.last_init_lblock + 1) / blkpg * blkpg !=
2744 (pb.last_init_lblock + 1) ||
2745 size < (__u64)(pb.last_init_lblock & ~(blkpg - 1)) *
2748 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2749 size > ext2_max_sizes[fs->super->s_log_block_size])
2750 /* too big for a direct/indirect-mapped file */
2752 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2754 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
2755 /* too big for an extent-based file - 32bit ee_block */
2758 /* i_size for symlinks is checked elsewhere */
2759 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
2760 pctx->num = (pb.last_block+1) * fs->blocksize;
2761 pctx->group = bad_size;
2762 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2763 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
2764 if (LINUX_S_ISDIR(inode->i_mode))
2765 pctx->num &= 0xFFFFFFFFULL;
2766 ext2fs_inode_size_set(fs, inode, pctx->num);
2771 if (LINUX_S_ISREG(inode->i_mode) &&
2772 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
2774 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
2775 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
2776 ((fs->super->s_feature_ro_compat &
2777 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
2778 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
2779 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
2780 pctx->num = pb.num_blocks;
2781 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2782 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
2783 inode->i_blocks = pb.num_blocks;
2784 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
2790 if (ctx->dirs_to_hash && pb.is_dir &&
2791 !(inode->i_flags & EXT2_INDEX_FL) &&
2792 ((inode->i_size / fs->blocksize) >= 3))
2793 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
2797 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
2802 * Helper function called by process block when an illegal block is
2803 * found. It returns a description about why the block is illegal
2805 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
2809 static char problem[80];
2811 super = fs->super->s_first_data_block;
2812 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
2813 if (block < super) {
2814 sprintf(problem, "< FIRSTBLOCK (%u)", super);
2816 } else if (block >= ext2fs_blocks_count(fs->super)) {
2817 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
2820 for (i = 0; i < fs->group_desc_count; i++) {
2821 if (block == super) {
2822 sprintf(problem, "is the superblock in group %d", i);
2825 if (block > super &&
2826 block <= (super + fs->desc_blocks)) {
2827 sprintf(problem, "is in the group descriptors "
2831 if (block == ext2fs_block_bitmap_loc(fs, i)) {
2832 sprintf(problem, "is the block bitmap of group %d", i);
2835 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
2836 sprintf(problem, "is the inode bitmap of group %d", i);
2839 if (block >= ext2fs_inode_table_loc(fs, i) &&
2840 (block < ext2fs_inode_table_loc(fs, i)
2841 + fs->inode_blocks_per_group)) {
2842 sprintf(problem, "is in the inode table of group %d",
2846 super += fs->super->s_blocks_per_group;
2853 * This is a helper function for check_blocks().
2855 static int process_block(ext2_filsys fs,
2857 e2_blkcnt_t blockcnt,
2858 blk64_t ref_block EXT2FS_ATTR((unused)),
2859 int ref_offset EXT2FS_ATTR((unused)),
2862 struct process_block_struct *p;
2863 struct problem_context *pctx;
2864 blk64_t blk = *block_nr;
2866 problem_t problem = 0;
2869 p = (struct process_block_struct *) priv_data;
2873 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
2874 /* todo: Check that the comprblk_fl is high, that the
2875 blkaddr pattern looks right (all non-holes up to
2876 first EXT2FS_COMPRESSED_BLKADDR, then all
2877 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2878 that the feature_incompat bit is high, and that the
2879 inode is a regular file. If we're doing a "full
2880 check" (a concept introduced to e2fsck by e2compr,
2881 meaning that we look at data blocks as well as
2882 metadata) then call some library routine that
2883 checks the compressed data. I'll have to think
2884 about this, because one particularly important
2885 problem to be able to fix is to recalculate the
2886 cluster size if necessary. I think that perhaps
2887 we'd better do most/all e2compr-specific checks
2888 separately, after the non-e2compr checks. If not
2889 doing a full check, it may be useful to test that
2890 the personality is linux; e.g. if it isn't then
2891 perhaps this really is just an illegal block. */
2896 * For a directory, add logical block zero for processing even if it's
2897 * not mapped or we'll be perennially stuck with broken "." and ".."
2900 if (p->is_dir && blockcnt == 0 && blk == 0) {
2901 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
2902 if (pctx->errcode) {
2904 pctx->num = blockcnt;
2905 goto failed_add_dir_block;
2914 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
2919 * Simplistic fragmentation check. We merely require that the
2920 * file be contiguous. (Which can never be true for really
2921 * big files that are greater than a block group.)
2923 if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
2924 if (p->previous_block+1 != blk) {
2925 if (ctx->options & E2F_OPT_FRAGCHECK) {
2933 printf(_("%6lu(%c): expecting %6lu "
2934 "got phys %6lu (blkcnt %lld)\n"),
2935 (unsigned long) pctx->ino, type,
2936 (unsigned long) p->previous_block+1,
2937 (unsigned long) blk,
2944 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
2945 problem = PR_1_TOOBIG_DIR;
2946 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
2947 problem = PR_1_TOOBIG_REG;
2948 if (!p->is_dir && !p->is_reg && blockcnt > 0)
2949 problem = PR_1_TOOBIG_SYMLINK;
2951 if (blk < fs->super->s_first_data_block ||
2952 blk >= ext2fs_blocks_count(fs->super)) {
2953 problem = PR_1_ILLEGAL_BLOCK_NUM;
2954 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
2958 p->num_illegal_blocks++;
2959 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
2960 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
2964 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
2966 set_latch_flags(PR_LATCH_BLOCK,
2971 pctx->blkcount = blockcnt;
2972 if (fix_problem(ctx, problem, pctx)) {
2973 blk = *block_nr = 0;
2974 ret_code = BLOCK_CHANGED;
2975 p->inode_modified = 1;
2977 * If the directory block is too big and is beyond the
2978 * end of the FS, don't bother trying to add it for
2979 * processing -- the kernel would never have created a
2980 * directory this large, and we risk an ENOMEM abort.
2981 * In any case, the toobig handler for extent-based
2982 * directories also doesn't feed toobig blocks to
2985 if (problem == PR_1_TOOBIG_DIR)
2992 if (p->ino == EXT2_RESIZE_INO) {
2994 * The resize inode has already be sanity checked
2995 * during pass #0 (the superblock checks). All we
2996 * have to do is mark the double indirect block as
2997 * being in use; all of the other blocks are handled
2998 * by mark_table_blocks()).
3000 if (blockcnt == BLOCK_COUNT_DIND)
3001 mark_block_used(ctx, blk);
3003 } else if (!(ctx->fs->cluster_ratio_bits &&
3004 p->previous_block &&
3005 (EXT2FS_B2C(ctx->fs, blk) ==
3006 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
3007 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
3008 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
3009 mark_block_used(ctx, blk);
3011 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
3012 p->last_block, blk, blockcnt)) {
3013 pctx->blk = blockcnt;
3015 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
3016 mark_block_used(ctx, blk);
3017 mark_block_used(ctx, blk);
3020 p->last_block = blockcnt;
3021 p->previous_block = blk;
3023 if (p->is_dir && (blockcnt >= 0)) {
3024 while (++p->last_db_block < blockcnt) {
3025 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
3028 if (pctx->errcode) {
3030 pctx->num = p->last_db_block;
3031 goto failed_add_dir_block;
3034 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
3036 if (pctx->errcode) {
3038 pctx->num = blockcnt;
3039 failed_add_dir_block:
3040 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3041 /* Should never get here */
3042 ctx->flags |= E2F_FLAG_ABORT;
3049 static int process_bad_block(ext2_filsys fs,
3051 e2_blkcnt_t blockcnt,
3052 blk64_t ref_block EXT2FS_ATTR((unused)),
3053 int ref_offset EXT2FS_ATTR((unused)),
3056 struct process_block_struct *p;
3057 blk64_t blk = *block_nr;
3058 blk64_t first_block;
3060 struct problem_context *pctx;
3064 * Note: This function processes blocks for the bad blocks
3065 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
3071 p = (struct process_block_struct *) priv_data;
3075 pctx->ino = EXT2_BAD_INO;
3077 pctx->blkcount = blockcnt;
3079 if ((blk < fs->super->s_first_data_block) ||
3080 (blk >= ext2fs_blocks_count(fs->super))) {
3081 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
3083 return BLOCK_CHANGED;
3089 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
3091 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
3093 return BLOCK_CHANGED;
3095 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3098 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
3101 return BLOCK_CHANGED;
3103 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3106 mark_block_used(ctx, blk);
3110 printf ("DEBUG: Marking %u as bad.\n", blk);
3112 ctx->fs_badblocks_count++;
3114 * If the block is not used, then mark it as used and return.
3115 * If it is already marked as found, this must mean that
3116 * there's an overlap between the filesystem table blocks
3117 * (bitmaps and inode table) and the bad block list.
3119 if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
3120 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3124 * Try to find the where the filesystem block was used...
3126 first_block = fs->super->s_first_data_block;
3128 for (i = 0; i < fs->group_desc_count; i++ ) {
3131 if (!ext2fs_bg_has_super(fs, i))
3133 if (blk == first_block) {
3135 if (fix_problem(ctx,
3136 PR_1_BAD_PRIMARY_SUPERBLOCK,
3139 return BLOCK_CHANGED;
3143 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
3146 if ((blk > first_block) &&
3147 (blk <= first_block + fs->desc_blocks)) {
3149 pctx->blk = *block_nr;
3150 if (fix_problem(ctx,
3151 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
3153 return BLOCK_CHANGED;
3157 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
3161 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
3162 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
3163 ctx->invalid_block_bitmap_flag[i]++;
3164 ctx->invalid_bitmaps++;
3168 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
3169 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
3170 ctx->invalid_inode_bitmap_flag[i]++;
3171 ctx->invalid_bitmaps++;
3175 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
3176 (blk < (ext2fs_inode_table_loc(fs, i) +
3177 fs->inode_blocks_per_group))) {
3179 * If there are bad blocks in the inode table,
3180 * the inode scan code will try to do
3181 * something reasonable automatically.
3185 first_block += fs->super->s_blocks_per_group;
3188 * If we've gotten to this point, then the only
3189 * possibility is that the bad block inode meta data
3190 * is using a bad block.
3192 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
3193 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
3194 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
3196 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
3198 return BLOCK_CHANGED;
3200 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3207 /* Warn user that the block wasn't claimed */
3208 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
3213 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
3214 const char *name, int num, blk64_t *new_block)
3216 ext2_filsys fs = ctx->fs;
3218 blk64_t old_block = *new_block;
3221 unsigned flexbg_size;
3224 struct problem_context pctx;
3226 clear_problem_context(&pctx);
3229 pctx.blk = old_block;
3233 * For flex_bg filesystems, first try to allocate the metadata
3234 * within the flex_bg, and if that fails then try finding the
3235 * space anywhere in the filesystem.
3237 is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
3238 EXT4_FEATURE_INCOMPAT_FLEX_BG);
3240 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3241 flexbg = group / flexbg_size;
3242 first_block = ext2fs_group_first_block2(fs,
3243 flexbg_size * flexbg);
3244 last_grp = group | (flexbg_size - 1);
3245 if (last_grp >= fs->group_desc_count)
3246 last_grp = fs->group_desc_count - 1;
3247 last_block = ext2fs_group_last_block2(fs, last_grp);
3249 last_block = ext2fs_group_last_block2(fs, group);
3250 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
3251 num, ctx->block_found_map,
3253 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
3254 pctx.errcode = ext2fs_get_free_blocks2(fs,
3255 fs->super->s_first_data_block,
3256 ext2fs_blocks_count(fs->super),
3257 num, ctx->block_found_map, new_block);
3260 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
3261 ext2fs_unmark_valid(fs);
3262 ctx->flags |= E2F_FLAG_ABORT;
3265 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
3267 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
3268 ext2fs_unmark_valid(fs);
3269 ctx->flags |= E2F_FLAG_ABORT;
3272 ext2fs_mark_super_dirty(fs);
3273 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3274 pctx.blk2 = *new_block;
3275 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
3276 PR_1_RELOC_TO), &pctx);
3278 for (i = 0; i < num; i++) {
3280 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
3282 pctx.errcode = io_channel_read_blk64(fs->io,
3283 old_block + i, 1, buf);
3285 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
3287 memset(buf, 0, fs->blocksize);
3289 pctx.blk = (*new_block) + i;
3290 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
3293 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
3295 ext2fs_free_mem(&buf);
3299 * This routine gets called at the end of pass 1 if bad blocks are
3300 * detected in the superblock, group descriptors, inode_bitmaps, or
3301 * block bitmaps. At this point, all of the blocks have been mapped
3302 * out, so we can try to allocate new block(s) to replace the bad
3305 static void handle_fs_bad_blocks(e2fsck_t ctx)
3307 ext2_filsys fs = ctx->fs;
3309 blk64_t first_block;
3312 for (i = 0; i < fs->group_desc_count; i++) {
3313 first_block = ext2fs_group_first_block2(fs, i);
3315 if (ctx->invalid_block_bitmap_flag[i]) {
3316 new_blk = ext2fs_block_bitmap_loc(fs, i);
3317 new_table_block(ctx, first_block, i, _("block bitmap"),
3319 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
3321 if (ctx->invalid_inode_bitmap_flag[i]) {
3322 new_blk = ext2fs_inode_bitmap_loc(fs, i);
3323 new_table_block(ctx, first_block, i, _("inode bitmap"),
3325 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
3327 if (ctx->invalid_inode_table_flag[i]) {
3328 new_blk = ext2fs_inode_table_loc(fs, i);
3329 new_table_block(ctx, first_block, i, _("inode table"),
3330 fs->inode_blocks_per_group,
3332 ext2fs_inode_table_loc_set(fs, i, new_blk);
3333 ctx->flags |= E2F_FLAG_RESTART;
3336 ctx->invalid_bitmaps = 0;
3340 * This routine marks all blocks which are used by the superblock,
3341 * group descriptors, inode bitmaps, and block bitmaps.
3343 static void mark_table_blocks(e2fsck_t ctx)
3345 ext2_filsys fs = ctx->fs;
3349 struct problem_context pctx;
3351 clear_problem_context(&pctx);
3353 for (i = 0; i < fs->group_desc_count; i++) {
3356 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
3359 * Mark the blocks used for the inode table
3361 if (ext2fs_inode_table_loc(fs, i)) {
3362 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
3363 j < fs->inode_blocks_per_group;
3365 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3368 if (!ctx->invalid_inode_table_flag[i] &&
3370 PR_1_ITABLE_CONFLICT, &pctx)) {
3371 ctx->invalid_inode_table_flag[i]++;
3372 ctx->invalid_bitmaps++;
3375 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3382 * Mark block used for the block bitmap
3384 if (ext2fs_block_bitmap_loc(fs, i)) {
3385 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3386 ext2fs_block_bitmap_loc(fs, i))) {
3387 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
3388 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
3389 ctx->invalid_block_bitmap_flag[i]++;
3390 ctx->invalid_bitmaps++;
3393 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3394 ext2fs_block_bitmap_loc(fs, i));
3399 * Mark block used for the inode bitmap
3401 if (ext2fs_inode_bitmap_loc(fs, i)) {
3402 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3403 ext2fs_inode_bitmap_loc(fs, i))) {
3404 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
3405 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
3406 ctx->invalid_inode_bitmap_flag[i]++;
3407 ctx->invalid_bitmaps++;
3410 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3411 ext2fs_inode_bitmap_loc(fs, i));
3418 * Thes subroutines short circuits ext2fs_get_blocks and
3419 * ext2fs_check_directory; we use them since we already have the inode
3420 * structure, so there's no point in letting the ext2fs library read
3423 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
3426 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3429 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3430 return EXT2_ET_CALLBACK_NOTHANDLED;
3432 for (i=0; i < EXT2_N_BLOCKS; i++)
3433 blocks[i] = ctx->stashed_inode->i_block[i];
3437 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
3438 struct ext2_inode *inode)
3440 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3442 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3443 return EXT2_ET_CALLBACK_NOTHANDLED;
3444 *inode = *ctx->stashed_inode;
3448 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
3449 struct ext2_inode *inode)
3451 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3453 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
3454 (inode != ctx->stashed_inode))
3455 *ctx->stashed_inode = *inode;
3456 return EXT2_ET_CALLBACK_NOTHANDLED;
3459 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
3461 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3463 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3464 return EXT2_ET_CALLBACK_NOTHANDLED;
3466 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
3467 return EXT2_ET_NO_DIRECTORY;
3471 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
3474 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3478 if (ctx->block_found_map) {
3479 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
3483 if (fs->block_map) {
3484 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
3485 ext2fs_mark_bb_dirty(fs);
3488 if (!fs->block_map) {
3489 retval = ext2fs_read_block_bitmap(fs);
3494 retval = ext2fs_new_block2(fs, goal, 0, &new_block);
3503 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
3505 ext2_filsys fs = ctx->fs;
3507 if (use_shortcuts) {
3508 fs->get_blocks = pass1_get_blocks;
3509 fs->check_directory = pass1_check_directory;
3510 fs->read_inode = pass1_read_inode;
3511 fs->write_inode = pass1_write_inode;
3512 ctx->stashed_ino = 0;
3515 fs->check_directory = 0;
3517 fs->write_inode = 0;
3521 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
3523 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
3524 ext2fs_set_block_alloc_stats_callback(ctx->fs,
3525 e2fsck_block_alloc_stats, 0);