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>
53 #include "ext2fs/lfsck.h"
57 #ifdef NO_INLINE_FUNCS
60 #define _INLINE_ inline
63 static int process_block(ext2_filsys fs, blk64_t *blocknr,
64 e2_blkcnt_t blockcnt, blk64_t ref_blk,
65 int ref_offset, void *priv_data);
66 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
67 e2_blkcnt_t blockcnt, blk64_t ref_blk,
68 int ref_offset, void *priv_data);
69 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
71 static void mark_table_blocks(e2fsck_t ctx);
72 static void alloc_bb_map(e2fsck_t ctx);
73 static void alloc_imagic_map(e2fsck_t ctx);
74 static void handle_fs_bad_blocks(e2fsck_t ctx);
75 static void process_inodes(e2fsck_t ctx, char *block_buf);
76 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
77 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
78 dgrp_t group, void * priv_data);
79 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
80 char *block_buf, int adjust_sign);
81 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
83 struct process_block_struct {
85 unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
86 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 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
173 struct ext2_inode *inode, char *buf)
178 ext2_extent_handle_t handle;
179 struct ext2_extent_info info;
180 struct ext2fs_extent extent;
182 if ((inode->i_size_high || inode->i_size == 0) ||
183 (inode->i_flags & EXT2_INDEX_FL))
186 if (inode->i_flags & EXT4_EXTENTS_FL) {
187 if (inode->i_size > fs->blocksize)
189 if (ext2fs_extent_open2(fs, ino, inode, &handle))
192 if (ext2fs_extent_get_info(handle, &info) ||
193 (info.num_entries != 1) ||
194 (info.max_depth != 0))
196 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
197 (extent.e_lblk != 0) ||
198 (extent.e_len != 1) ||
199 (extent.e_pblk < fs->super->s_first_data_block) ||
200 (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
204 ext2fs_extent_free(handle);
208 blocks = ext2fs_inode_data_blocks2(fs, inode);
210 if ((inode->i_size >= fs->blocksize) ||
211 (blocks != fs->blocksize >> 9) ||
212 (inode->i_block[0] < fs->super->s_first_data_block) ||
213 (inode->i_block[0] >= ext2fs_blocks_count(fs->super)))
216 for (i = 1; i < EXT2_N_BLOCKS; i++)
217 if (inode->i_block[i])
220 if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf))
223 len = strnlen(buf, fs->blocksize);
224 if (len == fs->blocksize)
227 if (inode->i_size >= sizeof(inode->i_block))
230 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
231 if (len == sizeof(inode->i_block))
234 if (len != inode->i_size)
240 * If the immutable (or append-only) flag is set on the inode, offer
243 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
244 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
246 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
249 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
250 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
253 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
254 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
258 * If device, fifo or socket, check size is zero -- if not offer to
261 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
263 struct ext2_inode *inode = pctx->inode;
265 if (EXT2_I_SIZE(inode) == 0)
268 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
269 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
273 inode->i_size_high = 0;
274 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
277 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
279 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
281 if (ctx->block_found_map) {
283 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
285 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
289 static void mark_inode_ea_map(e2fsck_t ctx, struct problem_context *pctx,
292 if (!ctx->inode_ea_map) {
293 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
297 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
303 ext2fs_mark_inode_bitmap2(ctx->inode_ea_map, ino);
307 * Delete an EA entry. If this is the last entry to be deleted, then i_file_acl
308 * must have been freed, so we must update e2fsck block statistics and set
309 * i_file_acl_deleted.
310 * When we delete the entry successfully, this function returns 0, else
314 static int e2fsck_ea_entry_delete(e2fsck_t ctx,
315 struct ext2_ext_attr_entry *entry,
316 struct problem_context *pctx,
317 int *i_file_acl_deleted, problem_t prob)
319 blk_t i_file_acl = pctx->inode->i_file_acl;
322 pctx->num = entry->e_value_inum;
324 if (fix_problem(ctx, prob, pctx)) {
325 /* Delete corrupt EA entry */
326 err = ext2fs_attr_set(ctx->fs, pctx->ino, pctx->inode,
327 entry->e_name_index, entry->e_name,
330 if (i_file_acl && pctx->inode->i_file_acl == 0) {
331 e2fsck_block_alloc_stats(ctx->fs, i_file_acl,
333 *i_file_acl_deleted = 1;
343 * Check validity of EA inode. Return 0 if EA inode is valid, nonzero otherwise.
345 static int check_large_ea_inode(e2fsck_t ctx, struct ext2_ext_attr_entry *entry,
346 struct problem_context *pctx,
347 int *i_file_acl_deleted)
349 struct ext2_inode inode;
352 /* Check if inode is within valid range */
353 if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
354 (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
355 ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
357 PR_1_ATTR_VALUE_EA_INODE);
358 /* If user refuses to delete this entry, caller may try to set
359 * the bit for this out-of-bound inode in inode_ea_map, so
360 * always return failure */
364 e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
365 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
366 /* If EXT4_EA_INODE_FL flag is not present but back-pointer
367 * matches then we should set this flag */
368 if (inode.i_mtime == pctx->ino &&
369 inode.i_generation == pctx->inode->i_generation &&
370 fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
371 inode.i_flags |= EXT4_EA_INODE_FL;
372 ext2fs_write_inode(ctx->fs, entry->e_value_inum,&inode);
374 ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
376 PR_1_ATTR_NO_EA_INODE_FL);
379 } else if (inode.i_mtime != pctx->ino ||
380 inode.i_generation != pctx->inode->i_generation) {
381 ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
383 PR_1_ATTR_INVAL_EA_INODE);
391 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
393 struct ext2_super_block *sb = ctx->fs->super;
394 struct ext2_inode_large *inode;
395 struct ext2_ext_attr_entry *entry;
397 unsigned int storage_size, remain;
398 struct lov_user_md_v1 *lmm = NULL;
399 struct lustre_mdt_attrs *lma = NULL;
402 inode = (struct ext2_inode_large *) pctx->inode;
403 storage_size = EXT2_INODE_SIZE(ctx->fs->super) -
404 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize;
405 entry = &IHDR(inode)->h_first_entry[0];
406 start = (char *)entry;
407 end = (char *) inode + EXT2_INODE_SIZE(ctx->fs->super);
409 /* scan all entry's headers first */
411 /* take finish entry 0UL into account */
412 remain = storage_size - sizeof(__u32);
414 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
417 /* header eats this space */
418 remain -= sizeof(struct ext2_ext_attr_entry);
420 /* is attribute name valid? */
421 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
422 pctx->num = entry->e_name_len;
423 problem = PR_1_ATTR_NAME_LEN;
427 /* attribute len eats this space */
428 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
430 if (entry->e_value_size == 0) {
431 pctx->num = entry->e_value_size;
432 problem = PR_1_ATTR_VALUE_SIZE;
436 if (entry->e_value_inum == 0) {
437 /* check value size */
438 if (entry->e_value_size > remain) {
439 pctx->num = entry->e_value_size;
440 problem = PR_1_ATTR_VALUE_SIZE;
446 ret = check_large_ea_inode(ctx, entry, pctx, &tmp);
448 mark_inode_ea_map(ctx, pctx,
449 entry->e_value_inum);
452 /* Value size cannot be larger than EA space in inode */
453 if (entry->e_value_offs > storage_size ||
454 (entry->e_value_inum == 0 &&
455 entry->e_value_offs + entry->e_value_size > storage_size)) {
456 problem = PR_1_INODE_EA_BAD_VALUE;
460 hash = ext2fs_ext_attr_hash_entry(entry,
461 start + entry->e_value_offs);
463 /* e_hash may be 0 in older inode's ea */
464 if (entry->e_hash != 0 && entry->e_hash != hash) {
465 pctx->num = entry->e_hash;
466 problem = PR_1_ATTR_HASH;
470 e2fsck_lfsck_find_ea(ctx, inode, entry,
471 start + entry->e_value_offs, &lmm, &lma);
473 /* If EA value is stored in external inode then it does not
474 * consume space here */
475 if (entry->e_value_inum == 0)
476 remain -= entry->e_value_size;
478 entry = EXT2_EXT_ATTR_NEXT(entry);
482 e2fsck_lfsck_save_ea(ctx, pctx->ino, inode->i_generation,
486 ext2fs_free_mem(&lmm);
488 ext2fs_free_mem(&lma);
490 * it seems like a corruption. it's very unlikely we could repair
491 * EA(s) in automatic fashion -bzzz
493 if (problem == 0 || !fix_problem(ctx, problem, pctx))
496 /* simply remove all possible EA(s) */
497 *((__u32 *)start) = 0UL;
498 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
499 EXT2_INODE_SIZE(sb), "pass1");
502 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
504 struct ext2_super_block *sb = ctx->fs->super;
505 struct ext2_inode_large *inode;
507 int min, max, dirty = 0;
509 inode = (struct ext2_inode_large *) pctx->inode;
510 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
511 /* this isn't large inode. so, nothing to check */
516 printf("inode #%u, i_extra_size %d\n", pctx->ino,
517 inode->i_extra_isize);
519 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
520 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
521 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
523 * For now we will allow i_extra_isize to be 0, but really
524 * implementations should never allow i_extra_isize to be 0
526 if (inode->i_extra_isize &&
527 (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
528 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
529 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
531 inode->i_extra_isize = ctx->want_extra_isize;
537 if (EXT4_FITS_IN_INODE(inode, inode, i_crtime) &&
538 inode->i_crtime != 0 &&
539 (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, 2*ctx->time_fudge) ||
540 EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, 2*ctx->time_fudge))) {
541 pctx->num = inode->i_crtime;
542 if (fix_problem(ctx, PR_1_CRTIME_BAD, pctx)) {
546 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_HIGH);
549 eamagic = &IHDR(inode)->h_magic;
550 if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
551 (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
552 (inode->i_extra_isize < ctx->want_extra_isize)) {
553 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
554 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
555 EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
556 inode->i_extra_isize = ctx->want_extra_isize;
558 if (inode->i_extra_isize < ctx->min_extra_isize)
559 ctx->min_extra_isize = inode->i_extra_isize;
562 if (*eamagic == EXT2_EXT_ATTR_MAGIC)
563 check_ea_in_inode(ctx, pctx);
566 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
567 EXT2_INODE_SIZE(sb), "pass1");
571 * Check to see if the inode might really be a directory, despite i_mode
573 * This is a lot of complexity for something for which I'm not really
574 * convinced happens frequently in the wild. If for any reason this
575 * causes any problems, take this code out.
576 * [tytso:20070331.0827EDT]
578 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
581 struct ext2_inode *inode = pctx->inode;
582 struct ext2_dir_entry *dirent;
585 unsigned int i, rec_len, not_device = 0;
589 * If the mode looks OK, we believe it. If the first block in
590 * the i_block array is 0, this cannot be a directory. If the
591 * inode is extent-mapped, it is still the case that the latter
592 * cannot be 0 - the magic number in the extent header would make
595 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
596 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
600 * Check the block numbers in the i_block array for validity:
601 * zero blocks are skipped (but the first one cannot be zero -
602 * see above), other blocks are checked against the first and
603 * max data blocks (from the the superblock) and against the
604 * block bitmap. Any invalid block found means this cannot be
607 * If there are non-zero blocks past the fourth entry, then
608 * this cannot be a device file: we remember that for the next
611 * For extent mapped files, we don't do any sanity checking:
612 * just try to get the phys block of logical block 0 and run
616 extent_fs = (ctx->fs->super->s_feature_incompat &
617 EXT3_FEATURE_INCOMPAT_EXTENTS);
618 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
620 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
623 /* device files are never extent mapped */
626 for (i=0; i < EXT2_N_BLOCKS; i++) {
627 blk = inode->i_block[i];
633 if (blk < ctx->fs->super->s_first_data_block ||
634 blk >= ext2fs_blocks_count(ctx->fs->super) ||
635 ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
637 return; /* Invalid block, can't be dir */
639 blk = inode->i_block[0];
643 * If the mode says this is a device file and the i_links_count field
644 * is sane and we have not ruled it out as a device file previously,
645 * we declare it a device file, not a directory.
647 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
648 (inode->i_links_count == 1) && !not_device)
651 /* read the first block */
652 ehandler_operation(_("reading directory block"));
653 retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0);
654 ehandler_operation(0);
658 dirent = (struct ext2_dir_entry *) buf;
659 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
662 if (((dirent->name_len & 0xFF) != 1) ||
663 (dirent->name[0] != '.') ||
664 (dirent->inode != pctx->ino) ||
667 (rec_len >= ctx->fs->blocksize - 12))
670 dirent = (struct ext2_dir_entry *) (buf + rec_len);
671 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
674 if (((dirent->name_len & 0xFF) != 2) ||
675 (dirent->name[0] != '.') ||
676 (dirent->name[1] != '.') ||
681 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
682 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
683 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
684 e2fsck_write_inode_full(ctx, pctx->ino, inode,
685 EXT2_INODE_SIZE(ctx->fs->super),
686 "check_is_really_dir");
690 extern void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
693 unsigned int threshold;
701 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
703 profile_get_uint(ctx->profile, "scratch_files",
704 "numdirs_threshold", 0, 0, &threshold);
705 profile_get_boolean(ctx->profile, "scratch_files",
706 "icount", 0, 1, &enable);
708 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
710 num_dirs = 1024; /* Guess */
712 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
713 (threshold && num_dirs <= threshold))
716 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
721 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
722 struct problem_context *pctx, int needed_size)
724 struct ext2_ext_attr_header *header;
725 struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
726 char *start, name[4096], block_buf[4096];
727 int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
728 int in_inode = 1, error;
729 unsigned int freed_bytes = inode->i_extra_isize;
731 entry_ino = &IHDR(inode)->h_first_entry[0];
732 start = (char *)entry_ino;
734 if (inode->i_file_acl) {
735 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
737 /* We have already checked this block, shouldn't happen */
739 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
742 header = BHDR(block_buf);
743 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
744 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
748 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
751 len = sizeof(entry->e_name);
752 entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
754 while (freed_bytes < needed_size) {
755 if (entry_size && name[0] != '\0') {
757 if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
758 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
759 EXT2_EXT_ATTR_SIZE(entry->e_value_size);
760 error = ext2fs_attr_set(ctx->fs, pctx->ino,
761 (struct ext2_inode *)inode,
762 index, name, 0, 0, 0);
764 freed_bytes += ea_size;
767 len = sizeof(entry->e_name);
768 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
769 entry = EXT2_EXT_ATTR_NEXT(entry);
770 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
773 len = sizeof(entry->e_name);
774 entry_size = ext2fs_attr_get_next_attr(entry,
775 index, name, len, 1);
780 if (!entry && index < EXT2_ATTR_INDEX_MAX)
781 entry = (struct ext2_ext_attr_entry *)start;
791 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
792 struct problem_context *pctx)
794 int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
798 retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
799 ctx->want_extra_isize, &ret,
801 if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
802 goto mark_expand_eisize_map;
804 e2fsck_write_inode_full(ctx, pctx->ino,
805 (struct ext2_inode *)inode,
806 EXT2_INODE_SIZE(ctx->fs->super),
811 if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
812 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
813 fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
814 ctx->flags |= E2F_FLAG_ABORT;
819 pctx->num = ctx->fs->super->s_min_extra_isize;
820 fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
824 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
826 if (retval >= ctx->want_extra_isize)
829 needed_size -= retval;
832 * We loop here until either the user deletes EA(s) or
833 * EXTRA_ISIZE feature is disabled.
835 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
836 ctx->fs->super->s_feature_ro_compat &=
837 ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
838 ext2fs_mark_super_dirty(ctx->fs);
842 ctx->fs_unexpanded_inodes++;
844 /* No EA was deleted, inode cannot be expanded */
848 mark_expand_eisize_map:
849 if (!ctx->expand_eisize_map) {
850 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
851 _("expand extrz isize map"),
852 &ctx->expand_eisize_map);
854 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
860 /* Add this inode to the expand_eisize_map */
861 ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
865 void e2fsck_pass1(e2fsck_t ctx)
869 ext2_filsys fs = ctx->fs;
871 struct ext2_inode *inode;
872 ext2_inode_scan scan;
874 #ifdef RESOURCE_TRACK
875 struct resource_track rtrack;
877 unsigned char frag, fsize;
878 struct problem_context pctx;
879 struct scan_callback_struct scan_struct;
880 struct ext2_super_block *sb = ctx->fs->super;
882 unsigned int save_type;
883 int imagic_fs, extent_fs;
884 int busted_fs_time = 0;
889 init_resource_track(&rtrack, ctx->fs->io);
890 clear_problem_context(&pctx);
892 if (!(ctx->options & E2F_OPT_PREEN))
893 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
895 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
896 !(ctx->options & E2F_OPT_NO)) {
897 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
898 ctx->dirs_to_hash = 0;
902 mtrace_print("Pass 1");
905 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
907 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
908 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
909 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
910 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
911 max_sizes = (max_sizes * (1UL << i));
912 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
916 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
917 extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
920 * Allocate bitmaps structures
922 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
923 EXT2FS_BMAP64_RBTREE,
925 &ctx->inode_used_map);
928 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
929 ctx->flags |= E2F_FLAG_ABORT;
932 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
933 _("directory inode map"),
934 EXT2FS_BMAP64_AUTODIR,
935 "inode_dir_map", &ctx->inode_dir_map);
938 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
939 ctx->flags |= E2F_FLAG_ABORT;
942 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
943 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
944 "inode_reg_map", &ctx->inode_reg_map);
947 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
948 ctx->flags |= E2F_FLAG_ABORT;
951 pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
952 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
953 "block_found_map", &ctx->block_found_map);
956 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
957 ctx->flags |= E2F_FLAG_ABORT;
960 e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
961 if (!ctx->inode_link_info) {
962 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
963 "inode_link_info", &save_type);
964 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
965 &ctx->inode_link_info);
966 fs->default_bitmap_type = save_type;
970 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
971 ctx->flags |= E2F_FLAG_ABORT;
974 inode_size = EXT2_INODE_SIZE(fs->super);
975 inode = (struct ext2_inode *)
976 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
978 inodes_to_process = (struct process_inode_block *)
979 e2fsck_allocate_memory(ctx,
980 (ctx->process_inode_size *
981 sizeof(struct process_inode_block)),
982 "array of inodes to process");
983 process_inode_count = 0;
985 pctx.errcode = ext2fs_init_dblist(fs, 0);
987 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
988 ctx->flags |= E2F_FLAG_ABORT;
989 ext2fs_free_mem(&inode);
994 * If the last orphan field is set, clear it, since the pass1
995 * processing will automatically find and clear the orphans.
996 * In the future, we may want to try using the last_orphan
997 * linked list ourselves, but for now, we clear it so that the
998 * ext3 mount code won't get confused.
1000 if (!(ctx->options & E2F_OPT_READONLY)) {
1001 if (fs->super->s_last_orphan) {
1002 fs->super->s_last_orphan = 0;
1003 ext2fs_mark_super_dirty(fs);
1007 mark_table_blocks(ctx);
1008 pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
1009 &ctx->block_found_map);
1011 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1012 ctx->flags |= E2F_FLAG_ABORT;
1013 ext2fs_free_mem(&inode);
1016 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1017 "block interate buffer");
1018 e2fsck_use_inode_shortcuts(ctx, 1);
1019 old_op = ehandler_operation(_("opening inode scan"));
1020 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1022 ehandler_operation(old_op);
1024 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1025 ctx->flags |= E2F_FLAG_ABORT;
1026 ext2fs_free_mem(&block_buf);
1027 ext2fs_free_mem(&inode);
1030 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
1031 ctx->stashed_inode = inode;
1032 scan_struct.ctx = ctx;
1033 scan_struct.block_buf = block_buf;
1034 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1036 if ((ctx->progress)(ctx, 1, 0, ctx->fs->group_desc_count))
1038 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
1039 (fs->super->s_mtime < fs->super->s_inodes_count))
1042 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
1043 !(fs->super->s_mmp_block <= fs->super->s_first_data_block ||
1044 fs->super->s_mmp_block >= fs->super->s_blocks_count))
1045 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1046 fs->super->s_mmp_block);
1048 if (!(ctx->options & E2F_OPT_READONLY) &&
1049 (ctx->lustre_devtype & LUSTRE_TYPE) == LUSTRE_MDS) {
1050 if (e2fsck_lfsck_remove_pending(ctx, NULL))
1055 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
1056 if (e2fsck_mmp_update(fs))
1057 fatal_error(ctx, 0);
1059 old_op = ehandler_operation(_("getting next inode from scan"));
1060 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1062 ehandler_operation(old_op);
1063 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1065 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1066 if (!ctx->inode_bb_map)
1068 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1069 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1073 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1074 ctx->flags |= E2F_FLAG_ABORT;
1081 ctx->stashed_ino = ino;
1082 if (inode->i_links_count) {
1083 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1084 ino, inode->i_links_count);
1086 pctx.num = inode->i_links_count;
1087 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1088 ctx->flags |= E2F_FLAG_ABORT;
1094 * Test for incorrect extent flag settings.
1096 * On big-endian machines we must be careful:
1097 * When the inode is read, the i_block array is not swapped
1098 * if the extent flag is set. Therefore if we are testing
1099 * for or fixing a wrongly-set flag, we must potentially
1100 * (un)swap before testing, or after fixing.
1104 * In this case the extents flag was set when read, so
1105 * extent_header_verify is ok. If the inode is cleared,
1106 * no need to swap... so no extra swapping here.
1108 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
1109 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1110 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1111 if ((ext2fs_extent_header_verify(inode->i_block,
1112 sizeof(inode->i_block)) == 0) &&
1113 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1114 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
1115 ext2fs_mark_super_dirty(fs);
1117 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1119 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1120 if (ino == EXT2_BAD_INO)
1121 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1128 * For big-endian machines:
1129 * If the inode didn't have the extents flag set when it
1130 * was read, then the i_blocks array was swapped. To test
1131 * as an extents header, we must swap it back first.
1132 * IF we then set the extents flag, the entire i_block
1133 * array must be un/re-swapped to make it proper extents data.
1135 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1136 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1137 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1138 (LINUX_S_ISREG(inode->i_mode) ||
1139 LINUX_S_ISDIR(inode->i_mode))) {
1141 #ifdef WORDS_BIGENDIAN
1142 __u32 tmp_block[EXT2_N_BLOCKS];
1144 for (i = 0; i < EXT2_N_BLOCKS; i++)
1145 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1148 ehp = inode->i_block;
1150 if ((ext2fs_extent_header_verify(ehp,
1151 sizeof(inode->i_block)) == 0)) {
1152 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1153 if (fix_problem(ctx, PR_1_UNSET_EXTENT_FL,
1155 inode->i_flags |= EXT4_EXTENTS_FL;
1156 #ifdef WORDS_BIGENDIAN
1157 memcpy(inode->i_block, tmp_block,
1158 sizeof(inode->i_block));
1160 e2fsck_write_inode(ctx, ino, inode,
1166 if (ino == EXT2_BAD_INO) {
1167 struct process_block_struct pb;
1169 if ((inode->i_mode || inode->i_uid || inode->i_gid ||
1170 inode->i_links_count || inode->i_file_acl) &&
1171 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1172 memset(inode, 0, sizeof(struct ext2_inode));
1173 e2fsck_write_inode(ctx, ino, inode,
1177 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1178 &pb.fs_meta_blocks);
1181 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1182 ctx->flags |= E2F_FLAG_ABORT;
1185 pb.ino = EXT2_BAD_INO;
1186 pb.num_blocks = pb.last_block = 0;
1187 pb.last_db_block = -1;
1188 pb.num_illegal_blocks = 0;
1189 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1190 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1194 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1195 block_buf, process_bad_block, &pb);
1196 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1198 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1199 ctx->flags |= E2F_FLAG_ABORT;
1203 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1204 ctx->flags |= E2F_FLAG_ABORT;
1207 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1208 clear_problem_context(&pctx);
1210 } else if (ino == EXT2_ROOT_INO) {
1212 * Make sure the root inode is a directory; if
1213 * not, offer to clear it. It will be
1214 * regnerated in pass #3.
1216 if (!LINUX_S_ISDIR(inode->i_mode)) {
1217 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1221 * If dtime is set, offer to clear it. mke2fs
1222 * version 0.2b created filesystems with the
1223 * dtime field set for the root and lost+found
1224 * directories. We won't worry about
1225 * /lost+found, since that can be regenerated
1226 * easily. But we will fix the root directory
1227 * as a special case.
1229 if (inode->i_dtime && inode->i_links_count) {
1230 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1231 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1233 e2fsck_write_inode(ctx, ino, inode,
1237 } else if (ino == EXT2_JOURNAL_INO) {
1238 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1239 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1240 if (!LINUX_S_ISREG(inode->i_mode) &&
1241 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1243 inode->i_mode = LINUX_S_IFREG;
1244 e2fsck_write_inode(ctx, ino, inode,
1247 check_blocks(ctx, &pctx, block_buf);
1250 if ((inode->i_links_count ||
1251 inode->i_blocks || inode->i_block[0]) &&
1252 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1254 memset(inode, 0, inode_size);
1255 ext2fs_icount_store(ctx->inode_link_info,
1257 e2fsck_write_inode_full(ctx, ino, inode,
1258 inode_size, "pass1");
1260 } else if ((ino == EXT4_USR_QUOTA_INO) ||
1261 (ino == EXT4_GRP_QUOTA_INO)) {
1262 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1263 if ((fs->super->s_feature_ro_compat &
1264 EXT4_FEATURE_RO_COMPAT_QUOTA) &&
1265 ((fs->super->s_usr_quota_inum == ino) ||
1266 (fs->super->s_grp_quota_inum == ino))) {
1267 if (!LINUX_S_ISREG(inode->i_mode) &&
1268 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1270 inode->i_mode = LINUX_S_IFREG;
1271 e2fsck_write_inode(ctx, ino, inode,
1274 check_blocks(ctx, &pctx, block_buf);
1277 if ((inode->i_links_count ||
1278 inode->i_blocks || inode->i_block[0]) &&
1279 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1281 memset(inode, 0, inode_size);
1282 ext2fs_icount_store(ctx->inode_link_info,
1284 e2fsck_write_inode_full(ctx, ino, inode,
1285 inode_size, "pass1");
1287 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1290 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1291 if (ino == EXT2_BOOT_LOADER_INO) {
1292 if (LINUX_S_ISDIR(inode->i_mode))
1293 problem = PR_1_RESERVED_BAD_MODE;
1294 } else if (ino == EXT2_RESIZE_INO) {
1295 if (inode->i_mode &&
1296 !LINUX_S_ISREG(inode->i_mode))
1297 problem = PR_1_RESERVED_BAD_MODE;
1299 if (inode->i_mode != 0)
1300 problem = PR_1_RESERVED_BAD_MODE;
1303 if (fix_problem(ctx, problem, &pctx)) {
1305 e2fsck_write_inode(ctx, ino, inode,
1309 check_blocks(ctx, &pctx, block_buf);
1314 * Check for inodes who might have been part of the
1315 * orphaned list linked list. They should have gotten
1316 * dealt with by now, unless the list had somehow been
1319 * FIXME: In the future, inodes which are still in use
1320 * (and which are therefore) pending truncation should
1321 * be handled specially. Right now we just clear the
1322 * dtime field, and the normal e2fsck handling of
1323 * inodes where i_size and the inode blocks are
1324 * inconsistent is to fix i_size, instead of releasing
1325 * the extra blocks. This won't catch the inodes that
1326 * was at the end of the orphan list, but it's better
1327 * than nothing. The right answer is that there
1328 * shouldn't be any bugs in the orphan list handling. :-)
1330 if (inode->i_dtime && !busted_fs_time &&
1331 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1332 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1333 inode->i_dtime = inode->i_links_count ?
1335 e2fsck_write_inode(ctx, ino, inode,
1341 * This code assumes that deleted inodes have
1342 * i_links_count set to 0.
1344 if (!inode->i_links_count) {
1345 if (!inode->i_dtime && inode->i_mode) {
1346 if (fix_problem(ctx,
1347 PR_1_ZERO_DTIME, &pctx)) {
1348 inode->i_dtime = ctx->now;
1349 e2fsck_write_inode(ctx, ino, inode,
1356 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1357 * deleted files. Oops.
1359 * Since all new ext2 implementations get this right,
1360 * we now assume that the case of non-zero
1361 * i_links_count and non-zero dtime means that we
1362 * should keep the file, not delete it.
1365 if (inode->i_dtime) {
1366 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1367 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1369 e2fsck_write_inode(ctx, ino, inode, "pass1");
1373 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1374 switch (fs->super->s_creator_os) {
1376 frag = inode->osd2.hurd2.h_i_frag;
1377 fsize = inode->osd2.hurd2.h_i_fsize;
1383 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1384 if (inode->i_faddr || frag || fsize ||
1385 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
1386 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1387 if (!(fs->super->s_feature_incompat &
1388 EXT4_FEATURE_INCOMPAT_64BIT) &&
1389 inode->osd2.linux2.l_i_file_acl_high != 0)
1390 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1391 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1392 !(fs->super->s_feature_ro_compat &
1393 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1394 (inode->osd2.linux2.l_i_blocks_hi != 0))
1395 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1396 if (inode->i_flags & EXT2_IMAGIC_FL) {
1398 if (!ctx->inode_imagic_map)
1399 alloc_imagic_map(ctx);
1400 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1403 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1404 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1405 inode->i_flags &= ~EXT2_IMAGIC_FL;
1406 e2fsck_write_inode(ctx, ino,
1412 check_inode_extra_space(ctx, &pctx);
1413 check_is_really_dir(ctx, &pctx, block_buf);
1416 * ext2fs_inode_has_valid_blocks2 does not actually look
1417 * at i_block[] values, so not endian-sensitive here.
1419 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1420 LINUX_S_ISLNK(inode->i_mode) &&
1421 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1422 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1423 inode->i_flags &= ~EXT4_EXTENTS_FL;
1424 e2fsck_write_inode(ctx, ino, inode, "pass1");
1427 if (LINUX_S_ISDIR(inode->i_mode)) {
1428 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1429 e2fsck_add_dir_info(ctx, ino, 0);
1430 ctx->fs_directory_count++;
1431 } else if (LINUX_S_ISREG (inode->i_mode)) {
1432 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1433 ctx->fs_regular_count++;
1434 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1435 e2fsck_pass1_check_device_inode(fs, inode)) {
1436 check_immutable(ctx, &pctx);
1437 check_size(ctx, &pctx);
1438 ctx->fs_chardev_count++;
1439 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1440 e2fsck_pass1_check_device_inode(fs, inode)) {
1441 check_immutable(ctx, &pctx);
1442 check_size(ctx, &pctx);
1443 ctx->fs_blockdev_count++;
1444 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1445 e2fsck_pass1_check_symlink(fs, ino, inode,
1447 check_immutable(ctx, &pctx);
1448 ctx->fs_symlinks_count++;
1449 if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1450 ctx->fs_fast_symlinks_count++;
1451 check_blocks(ctx, &pctx, block_buf);
1455 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1456 e2fsck_pass1_check_device_inode(fs, inode)) {
1457 check_immutable(ctx, &pctx);
1458 check_size(ctx, &pctx);
1459 ctx->fs_fifo_count++;
1460 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1461 e2fsck_pass1_check_device_inode(fs, inode)) {
1462 check_immutable(ctx, &pctx);
1463 check_size(ctx, &pctx);
1464 ctx->fs_sockets_count++;
1466 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1469 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
1470 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1471 else if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime,
1473 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1475 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
1476 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1477 else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
1479 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1481 /* i_crtime is checked in check_inode_extra_space() */
1483 if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
1484 if (inode->i_block[EXT2_IND_BLOCK])
1485 ctx->fs_ind_count++;
1486 if (inode->i_block[EXT2_DIND_BLOCK])
1487 ctx->fs_dind_count++;
1488 if (inode->i_block[EXT2_TIND_BLOCK])
1489 ctx->fs_tind_count++;
1491 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1492 (inode->i_block[EXT2_IND_BLOCK] ||
1493 inode->i_block[EXT2_DIND_BLOCK] ||
1494 inode->i_block[EXT2_TIND_BLOCK] ||
1495 ext2fs_file_acl_block(fs, inode))) {
1496 inodes_to_process[process_inode_count].ino = ino;
1497 inodes_to_process[process_inode_count].inode = *inode;
1498 process_inode_count++;
1500 check_blocks(ctx, &pctx, block_buf);
1502 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
1503 struct ext2_inode_large *inode_l;
1505 inode_l = (struct ext2_inode_large *)inode;
1507 if (inode_l->i_extra_isize < ctx->want_extra_isize) {
1508 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
1509 inode_exp = e2fsck_pass1_expand_eisize(ctx,
1513 if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
1515 ctx->min_extra_isize = inode_l->i_extra_isize;
1518 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1521 if (process_inode_count >= ctx->process_inode_size) {
1522 process_inodes(ctx, block_buf);
1524 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1528 process_inodes(ctx, block_buf);
1529 ext2fs_close_inode_scan(scan);
1532 * If any extended attribute blocks' reference counts need to
1533 * be adjusted, either up (ctx->refcount_extra), or down
1534 * (ctx->refcount), then fix them.
1536 if (ctx->refcount) {
1537 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1538 ea_refcount_free(ctx->refcount);
1541 if (ctx->refcount_extra) {
1542 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1544 ea_refcount_free(ctx->refcount_extra);
1545 ctx->refcount_extra = 0;
1548 if (ctx->invalid_bitmaps)
1549 handle_fs_bad_blocks(ctx);
1551 /* We don't need the block_ea_map any more */
1552 if (ctx->block_ea_map) {
1553 ext2fs_free_block_bitmap(ctx->block_ea_map);
1554 ctx->block_ea_map = 0;
1557 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1558 ext2fs_block_bitmap save_bmap;
1560 save_bmap = fs->block_map;
1561 fs->block_map = ctx->block_found_map;
1562 clear_problem_context(&pctx);
1563 pctx.errcode = ext2fs_create_resize_inode(fs);
1565 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1567 ctx->flags |= E2F_FLAG_ABORT;
1572 if (!pctx.errcode) {
1573 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1575 inode->i_mtime = ctx->now;
1576 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1579 fs->block_map = save_bmap;
1580 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1583 if (ctx->flags & E2F_FLAG_RESTART) {
1585 * Only the master copy of the superblock and block
1586 * group descriptors are going to be written during a
1587 * restart, so set the superblock to be used to be the
1588 * master superblock.
1590 ctx->use_superblock = 0;
1595 if (ctx->block_dup_map) {
1596 if (ctx->options & E2F_OPT_PREEN) {
1597 clear_problem_context(&pctx);
1598 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1600 e2fsck_pass1_dupblocks(ctx, block_buf);
1603 e2fsck_lfsck_flush_ea(ctx);
1605 ext2fs_free_mem(&inodes_to_process);
1607 e2fsck_use_inode_shortcuts(ctx, 0);
1609 ext2fs_free_mem(&block_buf);
1610 ext2fs_free_mem(&inode);
1612 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1616 * When the inode_scan routines call this callback at the end of the
1617 * glock group, call process_inodes.
1619 static errcode_t scan_callback(ext2_filsys fs,
1620 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1621 dgrp_t group, void * priv_data)
1623 struct scan_callback_struct *scan_struct;
1626 scan_struct = (struct scan_callback_struct *) priv_data;
1627 ctx = scan_struct->ctx;
1629 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1632 if ((ctx->progress)(ctx, 1, group+1,
1633 ctx->fs->group_desc_count))
1634 return EXT2_ET_CANCEL_REQUESTED;
1640 * Process the inodes in the "inodes to process" list.
1642 static void process_inodes(e2fsck_t ctx, char *block_buf)
1645 struct ext2_inode *old_stashed_inode;
1646 ext2_ino_t old_stashed_ino;
1647 const char *old_operation;
1649 struct problem_context pctx;
1652 printf("begin process_inodes: ");
1654 if (process_inode_count == 0)
1656 old_operation = ehandler_operation(0);
1657 old_stashed_inode = ctx->stashed_inode;
1658 old_stashed_ino = ctx->stashed_ino;
1659 qsort(inodes_to_process, process_inode_count,
1660 sizeof(struct process_inode_block), process_inode_cmp);
1661 clear_problem_context(&pctx);
1662 for (i=0; i < process_inode_count; i++) {
1663 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1664 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1667 printf("%u ", pctx.ino);
1669 sprintf(buf, _("reading indirect blocks of inode %u"),
1671 ehandler_operation(buf);
1672 check_blocks(ctx, &pctx, block_buf);
1673 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1676 ctx->stashed_inode = old_stashed_inode;
1677 ctx->stashed_ino = old_stashed_ino;
1678 process_inode_count = 0;
1680 printf("end process inodes\n");
1682 ehandler_operation(old_operation);
1685 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1687 const struct process_inode_block *ib_a =
1688 (const struct process_inode_block *) a;
1689 const struct process_inode_block *ib_b =
1690 (const struct process_inode_block *) b;
1693 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1694 ib_b->inode.i_block[EXT2_IND_BLOCK]);
1697 * We only call process_inodes() for non-extent
1698 * inodes, so it's OK to pass NULL to
1699 * ext2fs_file_acl_block() here.
1701 ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
1702 ext2fs_file_acl_block(0, &(ib_b->inode));
1704 ret = ib_a->ino - ib_b->ino;
1709 * Mark an inode as being bad and increment its badness counter.
1711 void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
1712 const char *func, const int line)
1714 struct problem_context pctx;
1717 if (!ctx->inode_badness) {
1718 clear_problem_context(&pctx);
1720 pctx.errcode = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
1721 &ctx->inode_badness);
1723 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1724 ctx->flags |= E2F_FLAG_ABORT;
1728 ext2fs_icount_fetch(ctx->inode_badness, ino, &result);
1729 ext2fs_icount_store(ctx->inode_badness, ino, count + result);
1731 if (ctx->options & E2F_OPT_DEBUG)
1732 fprintf(stderr, "%s:%d: increase inode %lu badness %u to %u\n",
1733 func, line, (unsigned long)ino, result, count + result);
1738 * This procedure will allocate the inode "bb" (badblock) map table
1740 static void alloc_bb_map(e2fsck_t ctx)
1742 struct problem_context pctx;
1744 clear_problem_context(&pctx);
1745 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1746 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
1747 "inode_bb_map", &ctx->inode_bb_map);
1750 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1751 /* Should never get here */
1752 ctx->flags |= E2F_FLAG_ABORT;
1758 * This procedure will allocate the inode imagic table
1760 static void alloc_imagic_map(e2fsck_t ctx)
1762 struct problem_context pctx;
1764 clear_problem_context(&pctx);
1765 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1766 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
1767 "inode_imagic_map", &ctx->inode_imagic_map);
1770 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1771 /* Should never get here */
1772 ctx->flags |= E2F_FLAG_ABORT;
1778 * Marks a block as in use, setting the dup_map if it's been set
1779 * already. Called by process_block and process_bad_block.
1781 * WARNING: Assumes checks have already been done to make sure block
1782 * is valid. This is true in both process_block and process_bad_block.
1784 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
1786 struct problem_context pctx;
1788 clear_problem_context(&pctx);
1790 if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
1791 if (!ctx->block_dup_map) {
1792 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1793 _("multiply claimed block map"),
1794 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1795 &ctx->block_dup_map);
1798 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1800 /* Should never get here */
1801 ctx->flags |= E2F_FLAG_ABORT;
1805 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
1807 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
1812 * Adjust the extended attribute block's reference counts at the end
1813 * of pass 1, either by subtracting out references for EA blocks that
1814 * are still referenced in ctx->refcount, or by adding references for
1815 * EA blocks that had extra references as accounted for in
1816 * ctx->refcount_extra.
1818 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1819 char *block_buf, int adjust_sign)
1821 struct ext2_ext_attr_header *header;
1822 struct problem_context pctx;
1823 ext2_filsys fs = ctx->fs;
1828 clear_problem_context(&pctx);
1830 ea_refcount_intr_begin(refcount);
1832 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1835 pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1836 /* We already checked this block, shouldn't happen */
1838 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1841 header = BHDR(block_buf);
1842 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1843 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1847 pctx.blkcount = header->h_refcount;
1848 should_be = header->h_refcount + adjust_sign * count;
1849 pctx.num = should_be;
1850 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1851 header->h_refcount = should_be;
1852 pctx.errcode = ext2fs_write_ext_attr2(fs, blk,
1855 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
1864 * Handle processing the extended attribute blocks
1866 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1869 ext2_filsys fs = ctx->fs;
1870 ext2_ino_t ino = pctx->ino;
1871 struct ext2_inode *inode = pctx->inode;
1874 struct ext2_ext_attr_header *header;
1875 struct ext2_ext_attr_entry *entry;
1877 region_t region = 0;
1878 struct lov_user_md_v1 *lmm = NULL;
1879 struct lustre_mdt_attrs *lma = NULL;
1882 blk = ext2fs_file_acl_block(fs, inode);
1887 * If the Extended attribute flag isn't set, then a non-zero
1888 * file acl means that the inode is corrupted.
1890 * Or if the extended attribute block is an invalid block,
1891 * then the inode is also corrupted.
1893 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1894 (blk < fs->super->s_first_data_block) ||
1895 (blk >= ext2fs_blocks_count(fs->super))) {
1896 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1897 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1901 /* If ea bitmap hasn't been allocated, create it */
1902 if (!ctx->block_ea_map) {
1903 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
1904 _("ext attr block map"),
1905 EXT2FS_BMAP64_RBTREE, "block_ea_map",
1906 &ctx->block_ea_map);
1907 if (pctx->errcode) {
1909 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1910 ctx->flags |= E2F_FLAG_ABORT;
1915 /* Create the EA refcount structure if necessary */
1916 if (!ctx->refcount) {
1917 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1918 if (pctx->errcode) {
1920 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1921 ctx->flags |= E2F_FLAG_ABORT;
1927 /* Debugging text */
1928 printf("Inode %u has EA block %u\n", ino, blk);
1931 /* Have we seen this EA block before? */
1932 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
1933 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1935 /* Ooops, this EA was referenced more than it stated */
1936 if (!ctx->refcount_extra) {
1937 pctx->errcode = ea_refcount_create(0,
1938 &ctx->refcount_extra);
1939 if (pctx->errcode) {
1941 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1942 ctx->flags |= E2F_FLAG_ABORT;
1946 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1951 * OK, we haven't seen this EA block yet. So we need to
1955 pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1956 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1958 header = BHDR(block_buf);
1959 pctx->blk = ext2fs_file_acl_block(fs, inode);
1960 if (((ctx->ext_attr_ver == 1) &&
1961 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1962 ((ctx->ext_attr_ver == 2) &&
1963 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1964 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1968 if (header->h_blocks != 1) {
1969 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1973 region = region_create(0, fs->blocksize);
1975 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
1976 ctx->flags |= E2F_FLAG_ABORT;
1979 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1980 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1984 entry = (struct ext2_ext_attr_entry *)(header+1);
1985 end = block_buf + fs->blocksize;
1986 while ((char *)entry < end && *(__u32 *)entry) {
1989 if (region_allocate(region, (char *)entry - (char *)header,
1990 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1991 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1995 if ((ctx->ext_attr_ver == 1 &&
1996 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1997 (ctx->ext_attr_ver == 2 &&
1998 entry->e_name_index == 0)) {
1999 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
2003 if (entry->e_value_inum == 0) {
2004 if (entry->e_value_offs + entry->e_value_size >
2006 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
2010 if (entry->e_value_size &&
2011 region_allocate(region, entry->e_value_offs,
2012 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
2013 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
2018 int i_file_acl_deleted = 0;
2020 ret = check_large_ea_inode(ctx, entry, pctx,
2021 &i_file_acl_deleted);
2023 mark_inode_ea_map(ctx, pctx,
2024 entry->e_value_inum);
2026 if (i_file_acl_deleted)
2030 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
2031 entry->e_value_offs);
2033 if (entry->e_hash != hash) {
2034 pctx->num = entry->e_hash;
2035 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
2037 entry->e_hash = hash;
2040 if (e2fsck_lfsck_find_ea(ctx, (struct ext2_inode_large *)inode,
2041 entry, block_buf + entry->e_value_offs,
2043 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2047 entry = EXT2_EXT_ATTR_NEXT(entry);
2051 e2fsck_lfsck_save_ea(ctx, ino, inode->i_generation, lmm, lma);
2053 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
2054 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2057 region_free(region);
2059 ext2fs_free_mem(&lmm);
2061 ext2fs_free_mem(&lma);
2063 count = header->h_refcount - 1;
2065 ea_refcount_store(ctx->refcount, blk, count);
2066 mark_block_used(ctx, blk);
2067 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
2072 ext2fs_free_mem(&lmm);
2074 ext2fs_free_mem(&lma);
2077 region_free(region);
2078 ext2fs_file_acl_block_set(fs, inode, 0);
2079 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
2083 /* Returns 1 if bad htree, 0 if OK */
2084 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
2085 ext2_ino_t ino, struct ext2_inode *inode,
2088 struct ext2_dx_root_info *root;
2089 ext2_filsys fs = ctx->fs;
2093 if ((!LINUX_S_ISDIR(inode->i_mode) &&
2094 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
2095 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX))) {
2096 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2097 if (fix_problem(ctx, PR_1_HTREE_SET, pctx))
2101 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
2103 if ((pctx->errcode) ||
2105 (blk < fs->super->s_first_data_block) ||
2106 (blk >= ext2fs_blocks_count(fs->super))) {
2107 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2108 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2114 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
2116 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2117 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2121 /* XXX should check that beginning matches a directory */
2122 root = get_ext2_dx_root_info(fs, block_buf);
2124 if ((root->reserved_zero || root->info_length < 8) &&
2125 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2128 pctx->num = root->hash_version;
2129 if ((root->hash_version != EXT2_HASH_LEGACY) &&
2130 (root->hash_version != EXT2_HASH_HALF_MD4) &&
2131 (root->hash_version != EXT2_HASH_TEA) &&
2132 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2135 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2136 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2139 pctx->num = root->indirect_levels;
2140 if ((root->indirect_levels > 1) &&
2141 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2147 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2148 struct ext2_inode *inode, int restart_flag,
2152 inode->i_links_count = 0;
2153 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2154 inode->i_dtime = ctx->now;
2156 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2157 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2158 if (ctx->inode_reg_map)
2159 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2160 if (ctx->inode_badness)
2161 ext2fs_icount_store(ctx->inode_badness, ino, 0);
2164 * If the inode was partially accounted for before processing
2165 * was aborted, we need to restart the pass 1 scan.
2167 ctx->flags |= restart_flag;
2169 if (ino == EXT2_BAD_INO)
2170 memset(inode, 0, sizeof(struct ext2_inode));
2172 e2fsck_write_inode(ctx, ino, inode, source);
2175 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2176 struct process_block_struct *pb,
2177 blk64_t start_block,
2178 ext2_extent_handle_t ehandle)
2180 struct ext2fs_extent extent;
2182 e2_blkcnt_t blockcnt;
2184 int is_dir, is_leaf;
2186 struct ext2_extent_info info;
2188 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2192 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2194 while (!pctx->errcode && info.num_entries-- > 0) {
2195 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2196 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2199 if (extent.e_pblk == 0 ||
2200 extent.e_pblk < ctx->fs->super->s_first_data_block ||
2201 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2202 problem = PR_1_EXTENT_BAD_START_BLK;
2203 else if (extent.e_lblk < start_block)
2204 problem = PR_1_OUT_OF_ORDER_EXTENTS;
2205 else if (extent.e_len == 0)
2206 problem = PR_1_EXTENT_LENGTH_ZERO;
2208 (extent.e_pblk + extent.e_len) >
2209 ext2fs_blocks_count(ctx->fs->super))
2210 problem = PR_1_EXTENT_ENDS_BEYOND;
2213 /* To ensure that extent is in inode */
2214 if (info.curr_level == 0)
2215 e2fsck_mark_inode_bad(ctx, pctx->ino,
2219 pctx->blk = extent.e_pblk;
2220 pctx->blk2 = extent.e_lblk;
2221 pctx->num = extent.e_len;
2222 if (fix_problem(ctx, problem, pctx)) {
2223 e2fsck_read_bitmaps(ctx);
2225 ext2fs_extent_delete(ehandle, 0);
2226 if (pctx->errcode) {
2227 pctx->str = "ext2fs_extent_delete";
2230 pctx->errcode = ext2fs_extent_get(ehandle,
2231 EXT2_EXTENT_CURRENT,
2233 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2243 blk = extent.e_pblk;
2244 pctx->errcode = ext2fs_extent_get(ehandle,
2245 EXT2_EXTENT_DOWN, &extent);
2246 if (pctx->errcode) {
2247 pctx->str = "EXT2_EXTENT_DOWN";
2248 problem = PR_1_EXTENT_HEADER_INVALID;
2249 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
2250 goto report_problem;
2253 scan_extent_node(ctx, pctx, pb, extent.e_lblk, ehandle);
2256 pctx->errcode = ext2fs_extent_get(ehandle,
2257 EXT2_EXTENT_UP, &extent);
2258 if (pctx->errcode) {
2259 pctx->str = "EXT2_EXTENT_UP";
2262 mark_block_used(ctx, blk);
2267 if ((pb->previous_block != 0) &&
2268 (pb->previous_block+1 != extent.e_pblk)) {
2269 if (ctx->options & E2F_OPT_FRAGCHECK) {
2274 else if (pb->is_reg)
2277 printf(("%6lu(%c): expecting %6lu "
2279 "phys %6lu log %lu len %lu\n"),
2280 (unsigned long) pctx->ino, type,
2281 (unsigned long) pb->previous_block+1,
2282 (unsigned long) extent.e_pblk,
2283 (unsigned long) extent.e_lblk,
2284 (unsigned long) extent.e_len);
2288 while (is_dir && ++pb->last_db_block < extent.e_lblk) {
2289 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
2292 if (pctx->errcode) {
2294 pctx->num = pb->last_db_block;
2295 goto failed_add_dir_block;
2298 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
2300 blk++, blockcnt++, i++) {
2301 if (!(ctx->fs->cluster_ratio_bits &&
2302 pb->previous_block &&
2303 (EXT2FS_B2C(ctx->fs, blk) ==
2304 EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
2305 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2306 (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2307 mark_block_used(ctx, blk);
2311 pb->previous_block = blk;
2314 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
2315 if (pctx->errcode) {
2317 pctx->num = blockcnt;
2318 failed_add_dir_block:
2319 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2320 /* Should never get here */
2321 ctx->flags |= E2F_FLAG_ABORT;
2326 if (is_dir && extent.e_len > 0)
2327 pb->last_db_block = blockcnt - 1;
2328 pb->previous_block = extent.e_pblk + extent.e_len - 1;
2329 start_block = pb->last_block = extent.e_lblk + extent.e_len - 1;
2330 if (is_leaf && !is_dir &&
2331 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
2332 pb->last_init_lblock = extent.e_lblk + extent.e_len - 1;
2334 pctx->errcode = ext2fs_extent_get(ehandle,
2335 EXT2_EXTENT_NEXT_SIB,
2338 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
2342 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
2343 struct process_block_struct *pb)
2345 struct ext2_extent_info info;
2346 struct ext2_inode *inode = pctx->inode;
2347 ext2_extent_handle_t ehandle;
2348 ext2_filsys fs = ctx->fs;
2349 ext2_ino_t ino = pctx->ino;
2352 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
2353 if (pctx->errcode) {
2354 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
2355 e2fsck_clear_inode(ctx, ino, inode, 0,
2356 "check_blocks_extents");
2361 retval = ext2fs_extent_get_info(ehandle, &info);
2363 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
2364 info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
2365 ctx->extent_depth_count[info.max_depth]++;
2368 scan_extent_node(ctx, pctx, pb, 0, ehandle);
2369 if (pctx->errcode &&
2370 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
2372 inode->i_blocks = 0;
2373 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2374 "check_blocks_extents");
2377 ext2fs_extent_free(ehandle);
2381 * This subroutine is called on each inode to account for all of the
2382 * blocks used by that inode.
2384 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
2387 ext2_filsys fs = ctx->fs;
2388 struct process_block_struct pb;
2389 ext2_ino_t ino = pctx->ino;
2390 struct ext2_inode *inode = pctx->inode;
2392 int dirty_inode = 0;
2399 pb.last_init_lblock = -1;
2400 pb.last_db_block = -1;
2401 pb.num_illegal_blocks = 0;
2402 pb.suppress = 0; pb.clear = 0;
2405 pb.previous_block = 0;
2406 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2407 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
2408 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
2415 extent_fs = (ctx->fs->super->s_feature_incompat &
2416 EXT3_FEATURE_INCOMPAT_EXTENTS);
2418 if (inode->i_flags & EXT2_COMPRBLK_FL) {
2419 if (fs->super->s_feature_incompat &
2420 EXT2_FEATURE_INCOMPAT_COMPRESSION)
2423 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2424 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
2425 inode->i_flags &= ~EXT2_COMPRBLK_FL;
2431 if (ext2fs_file_acl_block(fs, inode) &&
2432 check_ext_attr(ctx, pctx, block_buf)) {
2433 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2438 if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
2439 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
2440 check_blocks_extents(ctx, pctx, &pb);
2442 pctx->errcode = ext2fs_block_iterate3(fs, ino,
2443 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
2444 block_buf, process_block, &pb);
2446 * We do not have uninitialized extents in non extent
2449 pb.last_init_lblock = pb.last_block;
2452 end_problem_latch(ctx, PR_LATCH_BLOCK);
2453 end_problem_latch(ctx, PR_LATCH_TOOBIG);
2454 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2457 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
2459 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
2460 if (LINUX_S_ISDIR(inode->i_mode))
2461 ctx->fs_fragmented_dir++;
2463 ctx->fs_fragmented++;
2467 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2472 if (inode->i_flags & EXT2_INDEX_FL) {
2473 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
2474 inode->i_flags &= ~EXT2_INDEX_FL;
2478 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
2483 if (!pb.num_blocks && pb.is_dir) {
2485 * The mode might be in-correct. Increasing the badness by
2486 * small amount won't hurt much.
2488 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2489 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
2490 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
2491 ctx->fs_directory_count--;
2496 if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
2497 quota_data_add(ctx->qctx, inode, ino,
2498 pb.num_blocks * fs->blocksize);
2499 quota_data_inodes(ctx->qctx, inode, ino, +1);
2502 if (!(fs->super->s_feature_ro_compat &
2503 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
2504 !(inode->i_flags & EXT4_HUGE_FILE_FL))
2505 pb.num_blocks *= (fs->blocksize / 512);
2506 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
2508 printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
2509 ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
2513 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
2514 if (inode->i_size & (fs->blocksize - 1))
2516 else if (nblock > (pb.last_block + 1))
2518 else if (nblock < (pb.last_block + 1)) {
2519 if (((pb.last_block + 1) - nblock) >
2520 fs->super->s_prealloc_dir_blocks)
2524 e2_blkcnt_t blkpg = ctx->blocks_per_page;
2526 size = EXT2_I_SIZE(inode);
2527 if ((pb.last_init_lblock >= 0) &&
2528 /* allow blocks beyond EOF to end of PAGE_SIZE */
2529 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
2530 ((pb.last_init_lblock + 1) / blkpg * blkpg !=
2531 (pb.last_init_lblock + 1) ||
2532 size < (__u64)(pb.last_init_lblock & ~(blkpg - 1)) *
2535 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2536 size > ext2_max_sizes[fs->super->s_log_block_size])
2537 /* too big for a direct/indirect-mapped file */
2539 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2541 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
2542 /* too big for an extent-based file - 32bit ee_block */
2545 /* i_size for symlinks is checked elsewhere */
2546 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
2547 pctx->num = (pb.last_block+1) * fs->blocksize;
2548 pctx->group = bad_size;
2549 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2550 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
2551 inode->i_size = pctx->num;
2552 if (!LINUX_S_ISDIR(inode->i_mode))
2553 inode->i_size_high = pctx->num >> 32;
2558 if (LINUX_S_ISREG(inode->i_mode) && EXT2_I_SIZE(inode) >= 0x80000000UL)
2560 if ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
2561 ((fs->super->s_feature_ro_compat &
2562 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
2563 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
2564 (inode->osd2.linux2.l_i_blocks_hi != 0))) {
2565 pctx->num = pb.num_blocks;
2566 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2567 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
2568 inode->i_blocks = pb.num_blocks;
2569 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
2575 if (ctx->dirs_to_hash && pb.is_dir &&
2576 !(inode->i_flags & EXT2_INDEX_FL) &&
2577 ((inode->i_size / fs->blocksize) >= 3))
2578 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
2582 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
2587 * Helper function called by process block when an illegal block is
2588 * found. It returns a description about why the block is illegal
2590 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
2594 static char problem[80];
2596 super = fs->super->s_first_data_block;
2597 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
2598 if (block < super) {
2599 sprintf(problem, "< FIRSTBLOCK (%u)", super);
2601 } else if (block >= ext2fs_blocks_count(fs->super)) {
2602 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
2605 for (i = 0; i < fs->group_desc_count; i++) {
2606 if (block == super) {
2607 sprintf(problem, "is the superblock in group %d", i);
2610 if (block > super &&
2611 block <= (super + fs->desc_blocks)) {
2612 sprintf(problem, "is in the group descriptors "
2616 if (block == ext2fs_block_bitmap_loc(fs, i)) {
2617 sprintf(problem, "is the block bitmap of group %d", i);
2620 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
2621 sprintf(problem, "is the inode bitmap of group %d", i);
2624 if (block >= ext2fs_inode_table_loc(fs, i) &&
2625 (block < ext2fs_inode_table_loc(fs, i)
2626 + fs->inode_blocks_per_group)) {
2627 sprintf(problem, "is in the inode table of group %d",
2631 super += fs->super->s_blocks_per_group;
2638 * This is a helper function for check_blocks().
2640 static int process_block(ext2_filsys fs,
2642 e2_blkcnt_t blockcnt,
2643 blk64_t ref_block EXT2FS_ATTR((unused)),
2644 int ref_offset EXT2FS_ATTR((unused)),
2647 struct process_block_struct *p;
2648 struct problem_context *pctx;
2649 blk64_t blk = *block_nr;
2654 p = (struct process_block_struct *) priv_data;
2658 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
2659 /* todo: Check that the comprblk_fl is high, that the
2660 blkaddr pattern looks right (all non-holes up to
2661 first EXT2FS_COMPRESSED_BLKADDR, then all
2662 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2663 that the feature_incompat bit is high, and that the
2664 inode is a regular file. If we're doing a "full
2665 check" (a concept introduced to e2fsck by e2compr,
2666 meaning that we look at data blocks as well as
2667 metadata) then call some library routine that
2668 checks the compressed data. I'll have to think
2669 about this, because one particularly important
2670 problem to be able to fix is to recalculate the
2671 cluster size if necessary. I think that perhaps
2672 we'd better do most/all e2compr-specific checks
2673 separately, after the non-e2compr checks. If not
2674 doing a full check, it may be useful to test that
2675 the personality is linux; e.g. if it isn't then
2676 perhaps this really is just an illegal block. */
2684 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
2689 * Simplistic fragmentation check. We merely require that the
2690 * file be contiguous. (Which can never be true for really
2691 * big files that are greater than a block group.)
2693 if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
2694 if (p->previous_block+1 != blk) {
2695 if (ctx->options & E2F_OPT_FRAGCHECK) {
2703 printf(_("%6lu(%c): expecting %6lu "
2704 "got phys %6lu (blkcnt %lld)\n"),
2705 (unsigned long) pctx->ino, type,
2706 (unsigned long) p->previous_block+1,
2707 (unsigned long) blk,
2714 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
2715 problem = PR_1_TOOBIG_DIR;
2716 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
2717 problem = PR_1_TOOBIG_REG;
2718 if (!p->is_dir && !p->is_reg && blockcnt > 0)
2719 problem = PR_1_TOOBIG_SYMLINK;
2721 if (blk < fs->super->s_first_data_block ||
2722 blk >= ext2fs_blocks_count(fs->super)) {
2723 problem = PR_1_ILLEGAL_BLOCK_NUM;
2724 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
2728 p->num_illegal_blocks++;
2729 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
2730 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
2734 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
2736 set_latch_flags(PR_LATCH_BLOCK,
2741 pctx->blkcount = blockcnt;
2742 if (fix_problem(ctx, problem, pctx)) {
2743 blk = *block_nr = 0;
2744 ret_code = BLOCK_CHANGED;
2750 if (p->ino == EXT2_RESIZE_INO) {
2752 * The resize inode has already be sanity checked
2753 * during pass #0 (the superblock checks). All we
2754 * have to do is mark the double indirect block as
2755 * being in use; all of the other blocks are handled
2756 * by mark_table_blocks()).
2758 if (blockcnt == BLOCK_COUNT_DIND)
2759 mark_block_used(ctx, blk);
2761 } else if (!(ctx->fs->cluster_ratio_bits &&
2762 p->previous_block &&
2763 (EXT2FS_B2C(ctx->fs, blk) ==
2764 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
2765 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2766 (blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2767 mark_block_used(ctx, blk);
2771 p->last_block = blockcnt;
2772 p->previous_block = blk;
2774 if (p->is_dir && (blockcnt >= 0)) {
2775 while (++p->last_db_block < blockcnt) {
2776 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
2779 if (pctx->errcode) {
2781 pctx->num = p->last_db_block;
2782 goto failed_add_dir_block;
2785 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
2787 if (pctx->errcode) {
2789 pctx->num = blockcnt;
2790 failed_add_dir_block:
2791 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2792 /* Should never get here */
2793 ctx->flags |= E2F_FLAG_ABORT;
2800 static int process_bad_block(ext2_filsys fs,
2802 e2_blkcnt_t blockcnt,
2803 blk64_t ref_block EXT2FS_ATTR((unused)),
2804 int ref_offset EXT2FS_ATTR((unused)),
2807 struct process_block_struct *p;
2808 blk64_t blk = *block_nr;
2809 blk64_t first_block;
2811 struct problem_context *pctx;
2815 * Note: This function processes blocks for the bad blocks
2816 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
2822 p = (struct process_block_struct *) priv_data;
2826 pctx->ino = EXT2_BAD_INO;
2828 pctx->blkcount = blockcnt;
2830 if ((blk < fs->super->s_first_data_block) ||
2831 (blk >= ext2fs_blocks_count(fs->super))) {
2832 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
2834 return BLOCK_CHANGED;
2840 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
2842 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
2844 return BLOCK_CHANGED;
2846 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2849 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
2852 return BLOCK_CHANGED;
2854 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2857 mark_block_used(ctx, blk);
2861 printf ("DEBUG: Marking %u as bad.\n", blk);
2863 ctx->fs_badblocks_count++;
2865 * If the block is not used, then mark it as used and return.
2866 * If it is already marked as found, this must mean that
2867 * there's an overlap between the filesystem table blocks
2868 * (bitmaps and inode table) and the bad block list.
2870 if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
2871 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
2875 * Try to find the where the filesystem block was used...
2877 first_block = fs->super->s_first_data_block;
2879 for (i = 0; i < fs->group_desc_count; i++ ) {
2882 if (!ext2fs_bg_has_super(fs, i))
2884 if (blk == first_block) {
2886 if (fix_problem(ctx,
2887 PR_1_BAD_PRIMARY_SUPERBLOCK,
2890 return BLOCK_CHANGED;
2894 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
2897 if ((blk > first_block) &&
2898 (blk <= first_block + fs->desc_blocks)) {
2900 pctx->blk = *block_nr;
2901 if (fix_problem(ctx,
2902 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
2904 return BLOCK_CHANGED;
2908 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
2912 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
2913 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
2914 ctx->invalid_block_bitmap_flag[i]++;
2915 ctx->invalid_bitmaps++;
2919 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
2920 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
2921 ctx->invalid_inode_bitmap_flag[i]++;
2922 ctx->invalid_bitmaps++;
2926 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
2927 (blk < (ext2fs_inode_table_loc(fs, i) +
2928 fs->inode_blocks_per_group))) {
2930 * If there are bad blocks in the inode table,
2931 * the inode scan code will try to do
2932 * something reasonable automatically.
2936 first_block += fs->super->s_blocks_per_group;
2939 * If we've gotten to this point, then the only
2940 * possibility is that the bad block inode meta data
2941 * is using a bad block.
2943 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
2944 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
2945 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
2947 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
2949 return BLOCK_CHANGED;
2951 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2958 /* Warn user that the block wasn't claimed */
2959 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
2964 static void new_table_block(e2fsck_t ctx, blk_t first_block, int group,
2965 const char *name, int num, blk64_t *new_block)
2967 ext2_filsys fs = ctx->fs;
2969 blk64_t old_block = *new_block;
2971 int i, is_flexbg, flexbg, flexbg_size;
2973 struct problem_context pctx;
2975 clear_problem_context(&pctx);
2978 pctx.blk = old_block;
2982 * For flex_bg filesystems, first try to allocate the metadata
2983 * within the flex_bg, and if that fails then try finding the
2984 * space anywhere in the filesystem.
2986 is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
2987 EXT4_FEATURE_INCOMPAT_FLEX_BG);
2989 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
2990 flexbg = group / flexbg_size;
2991 first_block = ext2fs_group_first_block2(fs,
2992 flexbg_size * flexbg);
2993 last_grp = group | (flexbg_size - 1);
2994 if (last_grp > fs->group_desc_count)
2995 last_grp = fs->group_desc_count;
2996 last_block = ext2fs_group_last_block2(fs, last_grp);
2998 last_block = ext2fs_group_last_block2(fs, group);
2999 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
3000 num, ctx->block_found_map,
3002 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
3003 pctx.errcode = ext2fs_get_free_blocks2(fs,
3004 fs->super->s_first_data_block,
3005 ext2fs_blocks_count(fs->super),
3006 num, ctx->block_found_map, new_block);
3009 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
3010 ext2fs_unmark_valid(fs);
3011 ctx->flags |= E2F_FLAG_ABORT;
3014 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
3016 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
3017 ext2fs_unmark_valid(fs);
3018 ctx->flags |= E2F_FLAG_ABORT;
3021 ext2fs_mark_super_dirty(fs);
3022 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3023 pctx.blk2 = *new_block;
3024 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
3025 PR_1_RELOC_TO), &pctx);
3027 for (i = 0; i < num; i++) {
3029 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
3031 pctx.errcode = io_channel_read_blk64(fs->io,
3032 old_block + i, 1, buf);
3034 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
3036 memset(buf, 0, fs->blocksize);
3038 pctx.blk = (*new_block) + i;
3039 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
3042 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
3044 ext2fs_free_mem(&buf);
3048 * This routine gets called at the end of pass 1 if bad blocks are
3049 * detected in the superblock, group descriptors, inode_bitmaps, or
3050 * block bitmaps. At this point, all of the blocks have been mapped
3051 * out, so we can try to allocate new block(s) to replace the bad
3054 static void handle_fs_bad_blocks(e2fsck_t ctx)
3056 ext2_filsys fs = ctx->fs;
3058 blk64_t first_block;
3061 for (i = 0; i < fs->group_desc_count; i++) {
3062 first_block = ext2fs_group_first_block2(fs, i);
3064 if (ctx->invalid_block_bitmap_flag[i]) {
3065 new_blk = ext2fs_block_bitmap_loc(fs, i);
3066 new_table_block(ctx, first_block, i, _("block bitmap"),
3068 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
3070 if (ctx->invalid_inode_bitmap_flag[i]) {
3071 new_blk = ext2fs_inode_bitmap_loc(fs, i);
3072 new_table_block(ctx, first_block, i, _("inode bitmap"),
3074 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
3076 if (ctx->invalid_inode_table_flag[i]) {
3077 new_blk = ext2fs_inode_table_loc(fs, i);
3078 new_table_block(ctx, first_block, i, _("inode table"),
3079 fs->inode_blocks_per_group,
3081 ext2fs_inode_table_loc_set(fs, i, new_blk);
3082 ctx->flags |= E2F_FLAG_RESTART;
3085 ctx->invalid_bitmaps = 0;
3089 * This routine marks all blocks which are used by the superblock,
3090 * group descriptors, inode bitmaps, and block bitmaps.
3092 static void mark_table_blocks(e2fsck_t ctx)
3094 ext2_filsys fs = ctx->fs;
3098 struct problem_context pctx;
3100 clear_problem_context(&pctx);
3102 for (i = 0; i < fs->group_desc_count; i++) {
3105 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
3108 * Mark the blocks used for the inode table
3110 if (ext2fs_inode_table_loc(fs, i)) {
3111 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
3112 j < fs->inode_blocks_per_group;
3114 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3117 if (!ctx->invalid_inode_table_flag[i] &&
3119 PR_1_ITABLE_CONFLICT, &pctx)) {
3120 ctx->invalid_inode_table_flag[i]++;
3121 ctx->invalid_bitmaps++;
3124 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3131 * Mark block used for the block bitmap
3133 if (ext2fs_block_bitmap_loc(fs, i)) {
3134 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3135 ext2fs_block_bitmap_loc(fs, i))) {
3136 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
3137 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
3138 ctx->invalid_block_bitmap_flag[i]++;
3139 ctx->invalid_bitmaps++;
3142 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3143 ext2fs_block_bitmap_loc(fs, i));
3148 * Mark block used for the inode bitmap
3150 if (ext2fs_inode_bitmap_loc(fs, i)) {
3151 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3152 ext2fs_inode_bitmap_loc(fs, i))) {
3153 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
3154 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
3155 ctx->invalid_inode_bitmap_flag[i]++;
3156 ctx->invalid_bitmaps++;
3159 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3160 ext2fs_inode_bitmap_loc(fs, i));
3167 * Thes subroutines short circuits ext2fs_get_blocks and
3168 * ext2fs_check_directory; we use them since we already have the inode
3169 * structure, so there's no point in letting the ext2fs library read
3172 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
3175 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3178 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3179 return EXT2_ET_CALLBACK_NOTHANDLED;
3181 for (i=0; i < EXT2_N_BLOCKS; i++)
3182 blocks[i] = ctx->stashed_inode->i_block[i];
3186 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
3187 struct ext2_inode *inode)
3189 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3191 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3192 return EXT2_ET_CALLBACK_NOTHANDLED;
3193 *inode = *ctx->stashed_inode;
3197 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
3198 struct ext2_inode *inode)
3200 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3202 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
3203 (inode != ctx->stashed_inode))
3204 *ctx->stashed_inode = *inode;
3205 return EXT2_ET_CALLBACK_NOTHANDLED;
3208 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
3210 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3212 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3213 return EXT2_ET_CALLBACK_NOTHANDLED;
3215 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
3216 return EXT2_ET_NO_DIRECTORY;
3220 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
3223 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3227 if (ctx->block_found_map) {
3228 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
3232 if (fs->block_map) {
3233 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
3234 ext2fs_mark_bb_dirty(fs);
3237 if (!fs->block_map) {
3238 retval = ext2fs_read_block_bitmap(fs);
3243 retval = ext2fs_new_block2(fs, goal, 0, &new_block);
3252 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool)
3254 ext2_filsys fs = ctx->fs;
3257 fs->get_blocks = pass1_get_blocks;
3258 fs->check_directory = pass1_check_directory;
3259 fs->read_inode = pass1_read_inode;
3260 fs->write_inode = pass1_write_inode;
3261 ctx->stashed_ino = 0;
3262 ext2fs_set_alloc_block_callback(fs, e2fsck_get_alloc_block,
3264 ext2fs_set_block_alloc_stats_callback(fs,
3265 e2fsck_block_alloc_stats,
3269 fs->check_directory = 0;
3271 fs->write_inode = 0;