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)
32 * Pass 1 is designed to stash away enough information so that the
33 * other passes should not need to read in the inode information
34 * during the normal course of a filesystem check. (Althogh if an
35 * inconsistency is detected, other passes may need to read in an
38 * Note that pass 1B will be invoked if there are any duplicate blocks
42 #define _GNU_SOURCE 1 /* get strnlen() */
51 #include <ext2fs/ext2_ext_attr.h>
55 #ifdef NO_INLINE_FUNCS
58 #define _INLINE_ inline
61 static int process_block(ext2_filsys fs, blk64_t *blocknr,
62 e2_blkcnt_t blockcnt, blk64_t ref_blk,
63 int ref_offset, void *priv_data);
64 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
65 e2_blkcnt_t blockcnt, blk64_t ref_blk,
66 int ref_offset, void *priv_data);
67 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
69 static void mark_table_blocks(e2fsck_t ctx);
70 static void alloc_bb_map(e2fsck_t ctx);
71 static void alloc_imagic_map(e2fsck_t ctx);
72 static void handle_fs_bad_blocks(e2fsck_t ctx);
73 static void process_inodes(e2fsck_t ctx, char *block_buf);
74 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
75 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
76 dgrp_t group, void * priv_data);
77 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
78 char *block_buf, int adjust_sign);
79 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
81 struct process_block_struct {
83 unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
84 fragmented:1, compressed:1, bbcheck:1,
88 e2_blkcnt_t last_block;
89 e2_blkcnt_t last_init_lblock;
90 e2_blkcnt_t last_db_block;
91 int num_illegal_blocks;
92 blk64_t previous_block;
93 struct ext2_inode *inode;
94 struct problem_context *pctx;
95 ext2fs_block_bitmap fs_meta_blocks;
99 struct process_inode_block {
101 struct ext2_inode inode;
104 struct scan_callback_struct {
110 * For the inodes to process list.
112 static struct process_inode_block *inodes_to_process;
113 static int process_inode_count;
115 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
116 EXT2_MIN_BLOCK_LOG_SIZE + 1];
119 * Free all memory allocated by pass1 in preparation for restarting
122 static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
124 ext2fs_free_mem(&inodes_to_process);
125 inodes_to_process = 0;
129 * Check to make sure a device inode is real. Returns 1 if the device
130 * checks out, 0 if not.
132 * Note: this routine is now also used to check FIFO's and Sockets,
133 * since they have the same requirement; the i_block fields should be
136 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
137 struct ext2_inode *inode)
142 * If the index flag is set, then this is a bogus
145 if (inode->i_flags & (EXT2_INDEX_FL | EXT4_EXTENTS_FL))
149 * We should be able to do the test below all the time, but
150 * because the kernel doesn't forcibly clear the device
151 * inode's additional i_block fields, there are some rare
152 * occasions when a legitimate device inode will have non-zero
153 * additional i_block fields. So for now, we only complain
154 * when the immutable flag is set, which should never happen
155 * for devices. (And that's when the problem is caused, since
156 * you can't set or clear immutable flags for devices.) Once
157 * the kernel has been fixed we can change this...
159 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
160 for (i=4; i < EXT2_N_BLOCKS; i++)
161 if (inode->i_block[i])
168 * Check to make sure a symlink inode is real. Returns 1 if the symlink
169 * checks out, 0 if not.
171 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
172 struct ext2_inode *inode, char *buf)
177 ext2_extent_handle_t handle;
178 struct ext2_extent_info info;
179 struct ext2fs_extent extent;
181 if ((inode->i_size_high || inode->i_size == 0) ||
182 (inode->i_flags & EXT2_INDEX_FL))
185 if (inode->i_flags & EXT4_EXTENTS_FL) {
186 if (inode->i_size > fs->blocksize)
188 if (ext2fs_extent_open2(fs, ino, inode, &handle))
191 if (ext2fs_extent_get_info(handle, &info) ||
192 (info.num_entries != 1) ||
193 (info.max_depth != 0))
195 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
196 (extent.e_lblk != 0) ||
197 (extent.e_len != 1) ||
198 (extent.e_pblk < fs->super->s_first_data_block) ||
199 (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
203 ext2fs_extent_free(handle);
207 blocks = ext2fs_inode_data_blocks2(fs, inode);
209 if ((inode->i_size >= fs->blocksize) ||
210 (blocks != fs->blocksize >> 9) ||
211 (inode->i_block[0] < fs->super->s_first_data_block) ||
212 (inode->i_block[0] >= ext2fs_blocks_count(fs->super)))
215 for (i = 1; i < EXT2_N_BLOCKS; i++)
216 if (inode->i_block[i])
219 if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf))
222 len = strnlen(buf, fs->blocksize);
223 if (len == fs->blocksize)
226 if (inode->i_size >= sizeof(inode->i_block))
229 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
230 if (len == sizeof(inode->i_block))
233 if (len != inode->i_size)
239 * If the immutable (or append-only) flag is set on the inode, offer
242 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
243 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
245 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
248 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
249 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
252 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
253 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
257 * If device, fifo or socket, check size is zero -- if not offer to
260 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
262 struct ext2_inode *inode = pctx->inode;
264 if (EXT2_I_SIZE(inode) == 0)
267 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
268 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
271 ext2fs_inode_size_set(ctx->fs, inode, 0);
272 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
275 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
277 struct ext2_super_block *sb = ctx->fs->super;
278 struct ext2_inode_large *inode;
279 struct ext2_ext_attr_entry *entry;
281 unsigned int storage_size, remain;
282 problem_t problem = 0;
284 inode = (struct ext2_inode_large *) pctx->inode;
285 storage_size = EXT2_INODE_SIZE(ctx->fs->super) - EXT2_GOOD_OLD_INODE_SIZE -
286 inode->i_extra_isize;
287 start = ((char *) inode) + EXT2_GOOD_OLD_INODE_SIZE +
288 inode->i_extra_isize + sizeof(__u32);
289 entry = (struct ext2_ext_attr_entry *) start;
291 /* scan all entry's headers first */
293 /* take finish entry 0UL into account */
294 remain = storage_size - sizeof(__u32);
296 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
299 /* header eats this space */
300 remain -= sizeof(struct ext2_ext_attr_entry);
302 /* is attribute name valid? */
303 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
304 pctx->num = entry->e_name_len;
305 problem = PR_1_ATTR_NAME_LEN;
309 /* attribute len eats this space */
310 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
312 /* check value size */
313 if (entry->e_value_size > remain) {
314 pctx->num = entry->e_value_size;
315 problem = PR_1_ATTR_VALUE_SIZE;
319 /* e_value_block must be 0 in inode's ea */
320 if (entry->e_value_block != 0) {
321 pctx->num = entry->e_value_block;
322 problem = PR_1_ATTR_VALUE_BLOCK;
326 /* Value size cannot be larger than EA space in inode */
327 if (entry->e_value_offs > storage_size ||
328 entry->e_value_offs + entry->e_value_size > storage_size) {
329 problem = PR_1_INODE_EA_BAD_VALUE;
333 hash = ext2fs_ext_attr_hash_entry(entry,
334 start + entry->e_value_offs);
336 /* e_hash may be 0 in older inode's ea */
337 if (entry->e_hash != 0 && entry->e_hash != hash) {
338 pctx->num = entry->e_hash;
339 problem = PR_1_ATTR_HASH;
343 remain -= entry->e_value_size;
345 entry = EXT2_EXT_ATTR_NEXT(entry);
349 * it seems like a corruption. it's very unlikely we could repair
350 * EA(s) in automatic fashion -bzzz
352 if (problem == 0 || !fix_problem(ctx, problem, pctx))
355 /* simply remove all possible EA(s) */
356 *((__u32 *)start) = 0UL;
357 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
358 EXT2_INODE_SIZE(sb), "pass1");
361 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
363 struct ext2_super_block *sb = ctx->fs->super;
364 struct ext2_inode_large *inode;
366 int min, max, dirty = 0;
368 inode = (struct ext2_inode_large *) pctx->inode;
369 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
370 /* this isn't large inode. so, nothing to check */
375 printf("inode #%u, i_extra_size %d\n", pctx->ino,
376 inode->i_extra_isize);
378 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
379 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
380 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
382 * For now we will allow i_extra_isize to be 0, but really
383 * implementations should never allow i_extra_isize to be 0
385 if (inode->i_extra_isize &&
386 (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
387 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
388 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
390 inode->i_extra_isize = ctx->want_extra_isize;
396 if (EXT4_FITS_IN_INODE(inode, inode, i_crtime) &&
397 inode->i_crtime != 0 &&
398 (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, 2*ctx->time_fudge) ||
399 EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, 2*ctx->time_fudge))) {
400 pctx->num = inode->i_crtime;
401 if (fix_problem(ctx, PR_1_CRTIME_BAD, pctx)) {
405 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_HIGH);
408 eamagic = IHDR(inode);
409 if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
410 (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
411 (inode->i_extra_isize < ctx->want_extra_isize)) {
412 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
413 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
414 EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
415 inode->i_extra_isize = ctx->want_extra_isize;
417 if (inode->i_extra_isize < ctx->min_extra_isize)
418 ctx->min_extra_isize = inode->i_extra_isize;
421 if (*eamagic == EXT2_EXT_ATTR_MAGIC)
422 check_ea_in_inode(ctx, pctx);
425 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
426 EXT2_INODE_SIZE(sb), "pass1");
430 * Check to see if the inode might really be a directory, despite i_mode
432 * This is a lot of complexity for something for which I'm not really
433 * convinced happens frequently in the wild. If for any reason this
434 * causes any problems, take this code out.
435 * [tytso:20070331.0827EDT]
437 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
440 struct ext2_inode *inode = pctx->inode;
441 struct ext2_dir_entry *dirent;
444 unsigned int i, rec_len, not_device = 0;
448 * If the mode looks OK, we believe it. If the first block in
449 * the i_block array is 0, this cannot be a directory. If the
450 * inode is extent-mapped, it is still the case that the latter
451 * cannot be 0 - the magic number in the extent header would make
454 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
455 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
459 * Check the block numbers in the i_block array for validity:
460 * zero blocks are skipped (but the first one cannot be zero -
461 * see above), other blocks are checked against the first and
462 * max data blocks (from the the superblock) and against the
463 * block bitmap. Any invalid block found means this cannot be
466 * If there are non-zero blocks past the fourth entry, then
467 * this cannot be a device file: we remember that for the next
470 * For extent mapped files, we don't do any sanity checking:
471 * just try to get the phys block of logical block 0 and run
475 extent_fs = (ctx->fs->super->s_feature_incompat &
476 EXT3_FEATURE_INCOMPAT_EXTENTS);
477 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
479 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
482 /* device files are never extent mapped */
485 for (i=0; i < EXT2_N_BLOCKS; i++) {
486 blk = inode->i_block[i];
492 if (blk < ctx->fs->super->s_first_data_block ||
493 blk >= ext2fs_blocks_count(ctx->fs->super) ||
494 ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
496 return; /* Invalid block, can't be dir */
498 blk = inode->i_block[0];
502 * If the mode says this is a device file and the i_links_count field
503 * is sane and we have not ruled it out as a device file previously,
504 * we declare it a device file, not a directory.
506 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
507 (inode->i_links_count == 1) && !not_device)
510 /* read the first block */
511 ehandler_operation(_("reading directory block"));
512 retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0);
513 ehandler_operation(0);
517 dirent = (struct ext2_dir_entry *) buf;
518 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
521 if (((dirent->name_len & 0xFF) != 1) ||
522 (dirent->name[0] != '.') ||
523 (dirent->inode != pctx->ino) ||
526 (rec_len >= ctx->fs->blocksize - 12))
529 dirent = (struct ext2_dir_entry *) (buf + rec_len);
530 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
533 if (((dirent->name_len & 0xFF) != 2) ||
534 (dirent->name[0] != '.') ||
535 (dirent->name[1] != '.') ||
540 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
541 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
542 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
543 e2fsck_write_inode_full(ctx, pctx->ino, inode,
544 EXT2_INODE_SIZE(ctx->fs->super),
545 "check_is_really_dir");
549 void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
552 unsigned int threshold;
560 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
562 profile_get_uint(ctx->profile, "scratch_files",
563 "numdirs_threshold", 0, 0, &threshold);
564 profile_get_boolean(ctx->profile, "scratch_files",
565 "icount", 0, 1, &enable);
567 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
569 num_dirs = 1024; /* Guess */
571 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
572 (threshold && num_dirs <= threshold))
575 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
580 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
581 struct problem_context *pctx, int needed_size)
583 struct ext2_ext_attr_header *header;
584 struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
585 char *start, name[4096], block_buf[4096];
586 int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
587 int in_inode = 1, error;
588 unsigned int freed_bytes = inode->i_extra_isize;
590 start = (char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
591 inode->i_extra_isize + sizeof(__u32);
592 entry_ino = (struct ext2_ext_attr_entry *)start;
594 if (inode->i_file_acl) {
595 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
597 /* We have already checked this block, shouldn't happen */
599 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
602 header = BHDR(block_buf);
603 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
604 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
608 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
611 len = sizeof(entry->e_name);
612 entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
614 while (freed_bytes < needed_size) {
615 if (entry_size && name[0] != '\0') {
617 if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
618 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
619 EXT2_EXT_ATTR_SIZE(entry->e_value_size);
620 error = ext2fs_attr_set(ctx->fs, pctx->ino,
621 (struct ext2_inode *)inode,
622 index, name, 0, 0, 0);
624 freed_bytes += ea_size;
627 len = sizeof(entry->e_name);
628 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
629 entry = EXT2_EXT_ATTR_NEXT(entry);
630 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
633 len = sizeof(entry->e_name);
634 entry_size = ext2fs_attr_get_next_attr(entry,
635 index, name, len, 1);
640 if (!entry && index < EXT2_ATTR_INDEX_MAX)
641 entry = (struct ext2_ext_attr_entry *)start;
651 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
652 struct problem_context *pctx)
654 int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
658 retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
659 ctx->want_extra_isize, &ret,
661 if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
662 goto mark_expand_eisize_map;
664 e2fsck_write_inode_full(ctx, pctx->ino,
665 (struct ext2_inode *)inode,
666 EXT2_INODE_SIZE(ctx->fs->super),
671 if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
672 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
673 fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
674 ctx->flags |= E2F_FLAG_ABORT;
679 pctx->num = ctx->fs->super->s_min_extra_isize;
680 fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
684 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
686 if (retval >= ctx->want_extra_isize)
689 needed_size -= retval;
692 * We loop here until either the user deletes EA(s) or
693 * EXTRA_ISIZE feature is disabled.
695 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
696 ctx->fs->super->s_feature_ro_compat &=
697 ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
698 ext2fs_mark_super_dirty(ctx->fs);
702 ctx->fs_unexpanded_inodes++;
704 /* No EA was deleted, inode cannot be expanded */
708 mark_expand_eisize_map:
709 if (!ctx->expand_eisize_map) {
710 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
711 _("expand extrz isize map"),
712 &ctx->expand_eisize_map);
714 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
720 /* Add this inode to the expand_eisize_map */
721 ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
725 static void reserve_block_for_root_repair(e2fsck_t ctx)
729 ext2_filsys fs = ctx->fs;
731 ctx->root_repair_block = 0;
732 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
735 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
738 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
739 ctx->root_repair_block = blk;
742 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
746 ext2_filsys fs = ctx->fs;
747 static const char name[] = "lost+found";
750 ctx->lnf_repair_block = 0;
751 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
754 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
757 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
758 ctx->lnf_repair_block = blk;
761 void e2fsck_pass1(e2fsck_t ctx)
765 ext2_filsys fs = ctx->fs;
767 struct ext2_inode *inode = NULL;
768 ext2_inode_scan scan = NULL;
769 char *block_buf = NULL;
770 #ifdef RESOURCE_TRACK
771 struct resource_track rtrack;
773 unsigned char frag, fsize;
774 struct problem_context pctx;
775 struct scan_callback_struct scan_struct;
776 struct ext2_super_block *sb = ctx->fs->super;
778 unsigned int save_type;
779 int imagic_fs, extent_fs;
780 int low_dtime_check = 1;
785 init_resource_track(&rtrack, ctx->fs->io);
786 clear_problem_context(&pctx);
788 if (!(ctx->options & E2F_OPT_PREEN))
789 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
791 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
792 !(ctx->options & E2F_OPT_NO)) {
793 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
794 ctx->dirs_to_hash = 0;
798 mtrace_print("Pass 1");
801 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
803 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
804 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
805 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
806 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
807 max_sizes = (max_sizes * (1UL << i));
808 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
812 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
813 extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
816 * Allocate bitmaps structures
818 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
819 EXT2FS_BMAP64_RBTREE,
821 &ctx->inode_used_map);
824 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
825 ctx->flags |= E2F_FLAG_ABORT;
828 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
829 _("directory inode map"),
830 EXT2FS_BMAP64_AUTODIR,
831 "inode_dir_map", &ctx->inode_dir_map);
834 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
835 ctx->flags |= E2F_FLAG_ABORT;
838 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
839 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
840 "inode_reg_map", &ctx->inode_reg_map);
843 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
844 ctx->flags |= E2F_FLAG_ABORT;
847 pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
848 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
849 "block_found_map", &ctx->block_found_map);
852 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
853 ctx->flags |= E2F_FLAG_ABORT;
856 e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
857 if (!ctx->inode_link_info) {
858 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
859 "inode_link_info", &save_type);
860 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
861 &ctx->inode_link_info);
862 fs->default_bitmap_type = save_type;
866 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
867 ctx->flags |= E2F_FLAG_ABORT;
870 inode_size = EXT2_INODE_SIZE(fs->super);
871 inode = (struct ext2_inode *)
872 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
874 inodes_to_process = (struct process_inode_block *)
875 e2fsck_allocate_memory(ctx,
876 (ctx->process_inode_size *
877 sizeof(struct process_inode_block)),
878 "array of inodes to process");
879 process_inode_count = 0;
881 pctx.errcode = ext2fs_init_dblist(fs, 0);
883 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
884 ctx->flags |= E2F_FLAG_ABORT;
889 * If the last orphan field is set, clear it, since the pass1
890 * processing will automatically find and clear the orphans.
891 * In the future, we may want to try using the last_orphan
892 * linked list ourselves, but for now, we clear it so that the
893 * ext3 mount code won't get confused.
895 if (!(ctx->options & E2F_OPT_READONLY)) {
896 if (fs->super->s_last_orphan) {
897 fs->super->s_last_orphan = 0;
898 ext2fs_mark_super_dirty(fs);
902 mark_table_blocks(ctx);
903 pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
904 &ctx->block_found_map);
906 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
907 ctx->flags |= E2F_FLAG_ABORT;
910 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
911 "block interate buffer");
912 e2fsck_use_inode_shortcuts(ctx, 1);
913 e2fsck_intercept_block_allocations(ctx);
914 old_op = ehandler_operation(_("opening inode scan"));
915 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
917 ehandler_operation(old_op);
919 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
920 ctx->flags |= E2F_FLAG_ABORT;
923 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
924 ctx->stashed_inode = inode;
925 scan_struct.ctx = ctx;
926 scan_struct.block_buf = block_buf;
927 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
928 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
929 ctx->fs->group_desc_count)))
931 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
932 (fs->super->s_mtime < fs->super->s_inodes_count) ||
933 (fs->super->s_mkfs_time &&
934 fs->super->s_mkfs_time < fs->super->s_inodes_count))
937 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
938 fs->super->s_mmp_block > fs->super->s_first_data_block &&
939 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
940 ext2fs_mark_block_bitmap2(ctx->block_found_map,
941 fs->super->s_mmp_block);
944 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
945 if (e2fsck_mmp_update(fs))
948 old_op = ehandler_operation(_("getting next inode from scan"));
949 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
951 ehandler_operation(old_op);
952 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
954 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
955 if (!ctx->inode_bb_map)
957 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
958 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
962 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
963 ctx->flags |= E2F_FLAG_ABORT;
970 ctx->stashed_ino = ino;
971 if (inode->i_links_count) {
972 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
973 ino, inode->i_links_count);
975 pctx.num = inode->i_links_count;
976 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
977 ctx->flags |= E2F_FLAG_ABORT;
983 * Test for incorrect extent flag settings.
985 * On big-endian machines we must be careful:
986 * When the inode is read, the i_block array is not swapped
987 * if the extent flag is set. Therefore if we are testing
988 * for or fixing a wrongly-set flag, we must potentially
989 * (un)swap before testing, or after fixing.
993 * In this case the extents flag was set when read, so
994 * extent_header_verify is ok. If the inode is cleared,
995 * no need to swap... so no extra swapping here.
997 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
998 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
999 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1000 if ((ext2fs_extent_header_verify(inode->i_block,
1001 sizeof(inode->i_block)) == 0) &&
1002 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1003 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
1004 ext2fs_mark_super_dirty(fs);
1006 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1008 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1009 if (ino == EXT2_BAD_INO)
1010 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1017 * For big-endian machines:
1018 * If the inode didn't have the extents flag set when it
1019 * was read, then the i_blocks array was swapped. To test
1020 * as an extents header, we must swap it back first.
1021 * IF we then set the extents flag, the entire i_block
1022 * array must be un/re-swapped to make it proper extents data.
1024 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1025 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1026 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1027 (LINUX_S_ISREG(inode->i_mode) ||
1028 LINUX_S_ISDIR(inode->i_mode))) {
1030 #ifdef WORDS_BIGENDIAN
1031 __u32 tmp_block[EXT2_N_BLOCKS];
1033 for (i = 0; i < EXT2_N_BLOCKS; i++)
1034 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1037 ehp = inode->i_block;
1039 if ((ext2fs_extent_header_verify(ehp,
1040 sizeof(inode->i_block)) == 0)) {
1041 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1042 if (fix_problem(ctx, PR_1_UNSET_EXTENT_FL,
1044 inode->i_flags |= EXT4_EXTENTS_FL;
1045 #ifdef WORDS_BIGENDIAN
1046 memcpy(inode->i_block, tmp_block,
1047 sizeof(inode->i_block));
1049 e2fsck_write_inode(ctx, ino, inode,
1055 if (ino == EXT2_BAD_INO) {
1056 struct process_block_struct pb;
1058 if ((inode->i_mode || inode->i_uid || inode->i_gid ||
1059 inode->i_links_count || inode->i_file_acl) &&
1060 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1061 memset(inode, 0, sizeof(struct ext2_inode));
1062 e2fsck_write_inode(ctx, ino, inode,
1066 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1067 &pb.fs_meta_blocks);
1070 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1071 ctx->flags |= E2F_FLAG_ABORT;
1074 pb.ino = EXT2_BAD_INO;
1075 pb.num_blocks = pb.last_block = 0;
1076 pb.last_db_block = -1;
1077 pb.num_illegal_blocks = 0;
1078 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1079 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1083 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1084 block_buf, process_bad_block, &pb);
1085 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1087 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1088 ctx->flags |= E2F_FLAG_ABORT;
1092 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1093 ctx->flags |= E2F_FLAG_ABORT;
1096 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1097 clear_problem_context(&pctx);
1099 } else if (ino == EXT2_ROOT_INO) {
1101 * Make sure the root inode is a directory; if
1102 * not, offer to clear it. It will be
1103 * regnerated in pass #3.
1105 if (!LINUX_S_ISDIR(inode->i_mode)) {
1106 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1110 * If dtime is set, offer to clear it. mke2fs
1111 * version 0.2b created filesystems with the
1112 * dtime field set for the root and lost+found
1113 * directories. We won't worry about
1114 * /lost+found, since that can be regenerated
1115 * easily. But we will fix the root directory
1116 * as a special case.
1118 if (inode->i_dtime && inode->i_links_count) {
1119 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1120 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1122 e2fsck_write_inode(ctx, ino, inode,
1126 } else if (ino == EXT2_JOURNAL_INO) {
1127 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1128 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1129 if (!LINUX_S_ISREG(inode->i_mode) &&
1130 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1132 inode->i_mode = LINUX_S_IFREG;
1133 e2fsck_write_inode(ctx, ino, inode,
1136 check_blocks(ctx, &pctx, block_buf);
1139 if ((inode->i_links_count ||
1140 inode->i_blocks || inode->i_block[0]) &&
1141 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1143 memset(inode, 0, inode_size);
1144 ext2fs_icount_store(ctx->inode_link_info,
1146 e2fsck_write_inode_full(ctx, ino, inode,
1147 inode_size, "pass1");
1149 } else if ((ino == EXT4_USR_QUOTA_INO) ||
1150 (ino == EXT4_GRP_QUOTA_INO)) {
1151 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1152 if ((fs->super->s_feature_ro_compat &
1153 EXT4_FEATURE_RO_COMPAT_QUOTA) &&
1154 ((fs->super->s_usr_quota_inum == ino) ||
1155 (fs->super->s_grp_quota_inum == ino))) {
1156 if (!LINUX_S_ISREG(inode->i_mode) &&
1157 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1159 inode->i_mode = LINUX_S_IFREG;
1160 e2fsck_write_inode(ctx, ino, inode,
1163 check_blocks(ctx, &pctx, block_buf);
1166 if ((inode->i_links_count ||
1167 inode->i_blocks || inode->i_block[0]) &&
1168 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1170 memset(inode, 0, inode_size);
1171 ext2fs_icount_store(ctx->inode_link_info,
1173 e2fsck_write_inode_full(ctx, ino, inode,
1174 inode_size, "pass1");
1176 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1177 problem_t problem = 0;
1179 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1180 if (ino == EXT2_BOOT_LOADER_INO) {
1181 if (LINUX_S_ISDIR(inode->i_mode))
1182 problem = PR_1_RESERVED_BAD_MODE;
1183 } else if (ino == EXT2_RESIZE_INO) {
1184 if (inode->i_mode &&
1185 !LINUX_S_ISREG(inode->i_mode))
1186 problem = PR_1_RESERVED_BAD_MODE;
1188 if (inode->i_mode != 0)
1189 problem = PR_1_RESERVED_BAD_MODE;
1192 if (fix_problem(ctx, problem, &pctx)) {
1194 e2fsck_write_inode(ctx, ino, inode,
1198 check_blocks(ctx, &pctx, block_buf);
1203 * Check for inodes who might have been part of the
1204 * orphaned list linked list. They should have gotten
1205 * dealt with by now, unless the list had somehow been
1208 * FIXME: In the future, inodes which are still in use
1209 * (and which are therefore) pending truncation should
1210 * be handled specially. Right now we just clear the
1211 * dtime field, and the normal e2fsck handling of
1212 * inodes where i_size and the inode blocks are
1213 * inconsistent is to fix i_size, instead of releasing
1214 * the extra blocks. This won't catch the inodes that
1215 * was at the end of the orphan list, but it's better
1216 * than nothing. The right answer is that there
1217 * shouldn't be any bugs in the orphan list handling. :-)
1219 if (inode->i_dtime && low_dtime_check &&
1220 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1221 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1222 inode->i_dtime = inode->i_links_count ?
1224 e2fsck_write_inode(ctx, ino, inode,
1230 * This code assumes that deleted inodes have
1231 * i_links_count set to 0.
1233 if (!inode->i_links_count) {
1234 if (!inode->i_dtime && inode->i_mode) {
1235 if (fix_problem(ctx,
1236 PR_1_ZERO_DTIME, &pctx)) {
1237 inode->i_dtime = ctx->now;
1238 e2fsck_write_inode(ctx, ino, inode,
1245 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1246 * deleted files. Oops.
1248 * Since all new ext2 implementations get this right,
1249 * we now assume that the case of non-zero
1250 * i_links_count and non-zero dtime means that we
1251 * should keep the file, not delete it.
1254 if (inode->i_dtime) {
1255 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1256 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1258 e2fsck_write_inode(ctx, ino, inode, "pass1");
1262 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1263 switch (fs->super->s_creator_os) {
1265 frag = inode->osd2.hurd2.h_i_frag;
1266 fsize = inode->osd2.hurd2.h_i_fsize;
1272 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1273 if (inode->i_faddr || frag || fsize ||
1274 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
1275 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1276 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1277 !(fs->super->s_feature_incompat &
1278 EXT4_FEATURE_INCOMPAT_64BIT) &&
1279 inode->osd2.linux2.l_i_file_acl_high != 0)
1280 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1281 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1282 !(fs->super->s_feature_ro_compat &
1283 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1284 (inode->osd2.linux2.l_i_blocks_hi != 0))
1285 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1286 if (inode->i_flags & EXT2_IMAGIC_FL) {
1288 if (!ctx->inode_imagic_map)
1289 alloc_imagic_map(ctx);
1290 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1293 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1294 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1295 inode->i_flags &= ~EXT2_IMAGIC_FL;
1296 e2fsck_write_inode(ctx, ino,
1302 check_inode_extra_space(ctx, &pctx);
1303 check_is_really_dir(ctx, &pctx, block_buf);
1306 * ext2fs_inode_has_valid_blocks2 does not actually look
1307 * at i_block[] values, so not endian-sensitive here.
1309 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1310 LINUX_S_ISLNK(inode->i_mode) &&
1311 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1312 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1313 inode->i_flags &= ~EXT4_EXTENTS_FL;
1314 e2fsck_write_inode(ctx, ino, inode, "pass1");
1317 if (LINUX_S_ISDIR(inode->i_mode)) {
1318 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1319 e2fsck_add_dir_info(ctx, ino, 0);
1320 ctx->fs_directory_count++;
1321 } else if (LINUX_S_ISREG (inode->i_mode)) {
1322 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1323 ctx->fs_regular_count++;
1324 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1325 e2fsck_pass1_check_device_inode(fs, inode)) {
1326 check_immutable(ctx, &pctx);
1327 check_size(ctx, &pctx);
1328 ctx->fs_chardev_count++;
1329 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1330 e2fsck_pass1_check_device_inode(fs, inode)) {
1331 check_immutable(ctx, &pctx);
1332 check_size(ctx, &pctx);
1333 ctx->fs_blockdev_count++;
1334 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1335 e2fsck_pass1_check_symlink(fs, ino, inode,
1337 check_immutable(ctx, &pctx);
1338 ctx->fs_symlinks_count++;
1339 if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1340 ctx->fs_fast_symlinks_count++;
1341 check_blocks(ctx, &pctx, block_buf);
1345 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1346 e2fsck_pass1_check_device_inode(fs, inode)) {
1347 check_immutable(ctx, &pctx);
1348 check_size(ctx, &pctx);
1349 ctx->fs_fifo_count++;
1350 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1351 e2fsck_pass1_check_device_inode(fs, inode)) {
1352 check_immutable(ctx, &pctx);
1353 check_size(ctx, &pctx);
1354 ctx->fs_sockets_count++;
1356 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1359 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
1360 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1361 else if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime,
1363 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1365 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
1366 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1367 else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
1369 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1371 /* i_crtime is checked in check_inode_extra_space() */
1373 if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
1374 if (inode->i_block[EXT2_IND_BLOCK])
1375 ctx->fs_ind_count++;
1376 if (inode->i_block[EXT2_DIND_BLOCK])
1377 ctx->fs_dind_count++;
1378 if (inode->i_block[EXT2_TIND_BLOCK])
1379 ctx->fs_tind_count++;
1381 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1382 (inode->i_block[EXT2_IND_BLOCK] ||
1383 inode->i_block[EXT2_DIND_BLOCK] ||
1384 inode->i_block[EXT2_TIND_BLOCK] ||
1385 ext2fs_file_acl_block(fs, inode))) {
1386 inodes_to_process[process_inode_count].ino = ino;
1387 inodes_to_process[process_inode_count].inode = *inode;
1388 process_inode_count++;
1390 check_blocks(ctx, &pctx, block_buf);
1392 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
1393 struct ext2_inode_large *inode_l;
1395 inode_l = (struct ext2_inode_large *)inode;
1397 if (inode_l->i_extra_isize < ctx->want_extra_isize) {
1398 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
1399 inode_exp = e2fsck_pass1_expand_eisize(ctx,
1403 if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
1405 ctx->min_extra_isize = inode_l->i_extra_isize;
1408 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1411 if (process_inode_count >= ctx->process_inode_size) {
1412 process_inodes(ctx, block_buf);
1414 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1418 process_inodes(ctx, block_buf);
1419 ext2fs_close_inode_scan(scan);
1422 reserve_block_for_root_repair(ctx);
1423 reserve_block_for_lnf_repair(ctx);
1426 * If any extended attribute blocks' reference counts need to
1427 * be adjusted, either up (ctx->refcount_extra), or down
1428 * (ctx->refcount), then fix them.
1430 if (ctx->refcount) {
1431 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1432 ea_refcount_free(ctx->refcount);
1435 if (ctx->refcount_extra) {
1436 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1438 ea_refcount_free(ctx->refcount_extra);
1439 ctx->refcount_extra = 0;
1442 if (ctx->invalid_bitmaps)
1443 handle_fs_bad_blocks(ctx);
1445 /* We don't need the block_ea_map any more */
1446 if (ctx->block_ea_map) {
1447 ext2fs_free_block_bitmap(ctx->block_ea_map);
1448 ctx->block_ea_map = 0;
1451 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1452 clear_problem_context(&pctx);
1453 pctx.errcode = ext2fs_create_resize_inode(fs);
1455 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1457 ctx->flags |= E2F_FLAG_ABORT;
1462 if (!pctx.errcode) {
1463 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1465 inode->i_mtime = ctx->now;
1466 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1469 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1472 if (ctx->flags & E2F_FLAG_RESTART) {
1474 * Only the master copy of the superblock and block
1475 * group descriptors are going to be written during a
1476 * restart, so set the superblock to be used to be the
1477 * master superblock.
1479 ctx->use_superblock = 0;
1484 if (ctx->block_dup_map) {
1485 if (ctx->options & E2F_OPT_PREEN) {
1486 clear_problem_context(&pctx);
1487 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1489 e2fsck_pass1_dupblocks(ctx, block_buf);
1491 ext2fs_free_mem(&inodes_to_process);
1493 e2fsck_use_inode_shortcuts(ctx, 0);
1496 ext2fs_close_inode_scan(scan);
1498 ext2fs_free_mem(&block_buf);
1500 ext2fs_free_mem(&inode);
1502 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
1503 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1507 * When the inode_scan routines call this callback at the end of the
1508 * glock group, call process_inodes.
1510 static errcode_t scan_callback(ext2_filsys fs,
1511 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1512 dgrp_t group, void * priv_data)
1514 struct scan_callback_struct *scan_struct;
1517 scan_struct = (struct scan_callback_struct *) priv_data;
1518 ctx = scan_struct->ctx;
1520 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1523 if ((ctx->progress)(ctx, 1, group+1,
1524 ctx->fs->group_desc_count))
1525 return EXT2_ET_CANCEL_REQUESTED;
1531 * Process the inodes in the "inodes to process" list.
1533 static void process_inodes(e2fsck_t ctx, char *block_buf)
1536 struct ext2_inode *old_stashed_inode;
1537 ext2_ino_t old_stashed_ino;
1538 const char *old_operation;
1540 struct problem_context pctx;
1543 printf("begin process_inodes: ");
1545 if (process_inode_count == 0)
1547 old_operation = ehandler_operation(0);
1548 old_stashed_inode = ctx->stashed_inode;
1549 old_stashed_ino = ctx->stashed_ino;
1550 qsort(inodes_to_process, process_inode_count,
1551 sizeof(struct process_inode_block), process_inode_cmp);
1552 clear_problem_context(&pctx);
1553 for (i=0; i < process_inode_count; i++) {
1554 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1555 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1558 printf("%u ", pctx.ino);
1560 sprintf(buf, _("reading indirect blocks of inode %u"),
1562 ehandler_operation(buf);
1563 check_blocks(ctx, &pctx, block_buf);
1564 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1567 ctx->stashed_inode = old_stashed_inode;
1568 ctx->stashed_ino = old_stashed_ino;
1569 process_inode_count = 0;
1571 printf("end process inodes\n");
1573 ehandler_operation(old_operation);
1576 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1578 const struct process_inode_block *ib_a =
1579 (const struct process_inode_block *) a;
1580 const struct process_inode_block *ib_b =
1581 (const struct process_inode_block *) b;
1584 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1585 ib_b->inode.i_block[EXT2_IND_BLOCK]);
1588 * We only call process_inodes() for non-extent
1589 * inodes, so it's OK to pass NULL to
1590 * ext2fs_file_acl_block() here.
1592 ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
1593 ext2fs_file_acl_block(0, &(ib_b->inode));
1595 ret = ib_a->ino - ib_b->ino;
1600 * Mark an inode as being bad and increment its badness counter.
1602 void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
1603 const char *func, const int line)
1605 struct problem_context pctx;
1608 if (!ctx->inode_badness) {
1609 clear_problem_context(&pctx);
1611 pctx.errcode = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
1612 &ctx->inode_badness);
1614 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1615 ctx->flags |= E2F_FLAG_ABORT;
1619 ext2fs_icount_fetch(ctx->inode_badness, ino, &result);
1620 ext2fs_icount_store(ctx->inode_badness, ino, count + result);
1622 if (ctx->options & E2F_OPT_DEBUG)
1623 fprintf(stderr, "%s:%d: increase inode %lu badness %u to %u\n",
1624 func, line, (unsigned long)ino, result, count + result);
1629 * This procedure will allocate the inode "bb" (badblock) map table
1631 static void alloc_bb_map(e2fsck_t ctx)
1633 struct problem_context pctx;
1635 clear_problem_context(&pctx);
1636 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1637 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
1638 "inode_bb_map", &ctx->inode_bb_map);
1641 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1642 /* Should never get here */
1643 ctx->flags |= E2F_FLAG_ABORT;
1649 * This procedure will allocate the inode imagic table
1651 static void alloc_imagic_map(e2fsck_t ctx)
1653 struct problem_context pctx;
1655 clear_problem_context(&pctx);
1656 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1657 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
1658 "inode_imagic_map", &ctx->inode_imagic_map);
1661 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1662 /* Should never get here */
1663 ctx->flags |= E2F_FLAG_ABORT;
1669 * Marks a block as in use, setting the dup_map if it's been set
1670 * already. Called by process_block and process_bad_block.
1672 * WARNING: Assumes checks have already been done to make sure block
1673 * is valid. This is true in both process_block and process_bad_block.
1675 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
1677 struct problem_context pctx;
1679 clear_problem_context(&pctx);
1681 if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
1682 if (!ctx->block_dup_map) {
1683 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1684 _("multiply claimed block map"),
1685 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1686 &ctx->block_dup_map);
1689 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1691 /* Should never get here */
1692 ctx->flags |= E2F_FLAG_ABORT;
1696 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
1698 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
1702 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
1705 if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
1706 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
1709 mark_block_used(ctx, block++);
1713 * Adjust the extended attribute block's reference counts at the end
1714 * of pass 1, either by subtracting out references for EA blocks that
1715 * are still referenced in ctx->refcount, or by adding references for
1716 * EA blocks that had extra references as accounted for in
1717 * ctx->refcount_extra.
1719 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1720 char *block_buf, int adjust_sign)
1722 struct ext2_ext_attr_header *header;
1723 struct problem_context pctx;
1724 ext2_filsys fs = ctx->fs;
1729 clear_problem_context(&pctx);
1731 ea_refcount_intr_begin(refcount);
1733 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1736 pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1737 /* We already checked this block, shouldn't happen */
1739 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1742 header = BHDR(block_buf);
1743 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1744 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1748 pctx.blkcount = header->h_refcount;
1749 should_be = header->h_refcount + adjust_sign * count;
1750 pctx.num = should_be;
1751 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1752 header->h_refcount = should_be;
1753 pctx.errcode = ext2fs_write_ext_attr2(fs, blk,
1756 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
1765 * Handle processing the extended attribute blocks
1767 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1770 ext2_filsys fs = ctx->fs;
1771 ext2_ino_t ino = pctx->ino;
1772 struct ext2_inode *inode = pctx->inode;
1775 struct ext2_ext_attr_header *header;
1776 struct ext2_ext_attr_entry *entry;
1778 region_t region = 0;
1780 blk = ext2fs_file_acl_block(fs, inode);
1785 * If the Extended attribute flag isn't set, then a non-zero
1786 * file acl means that the inode is corrupted.
1788 * Or if the extended attribute block is an invalid block,
1789 * then the inode is also corrupted.
1791 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1792 (blk < fs->super->s_first_data_block) ||
1793 (blk >= ext2fs_blocks_count(fs->super))) {
1794 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1795 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1799 /* If ea bitmap hasn't been allocated, create it */
1800 if (!ctx->block_ea_map) {
1801 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
1802 _("ext attr block map"),
1803 EXT2FS_BMAP64_RBTREE, "block_ea_map",
1804 &ctx->block_ea_map);
1805 if (pctx->errcode) {
1807 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1808 ctx->flags |= E2F_FLAG_ABORT;
1813 /* Create the EA refcount structure if necessary */
1814 if (!ctx->refcount) {
1815 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1816 if (pctx->errcode) {
1818 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1819 ctx->flags |= E2F_FLAG_ABORT;
1825 /* Debugging text */
1826 printf("Inode %u has EA block %u\n", ino, blk);
1829 /* Have we seen this EA block before? */
1830 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
1831 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1833 /* Ooops, this EA was referenced more than it stated */
1834 if (!ctx->refcount_extra) {
1835 pctx->errcode = ea_refcount_create(0,
1836 &ctx->refcount_extra);
1837 if (pctx->errcode) {
1839 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1840 ctx->flags |= E2F_FLAG_ABORT;
1844 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1849 * OK, we haven't seen this EA block yet. So we need to
1853 pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1854 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1856 header = BHDR(block_buf);
1857 pctx->blk = ext2fs_file_acl_block(fs, inode);
1858 if (((ctx->ext_attr_ver == 1) &&
1859 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1860 ((ctx->ext_attr_ver == 2) &&
1861 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1862 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1866 if (header->h_blocks != 1) {
1867 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1871 region = region_create(0, fs->blocksize);
1873 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
1874 ctx->flags |= E2F_FLAG_ABORT;
1877 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1878 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1882 entry = (struct ext2_ext_attr_entry *)(header+1);
1883 end = block_buf + fs->blocksize;
1884 while ((char *)entry < end && *(__u32 *)entry) {
1887 if (region_allocate(region, (char *)entry - (char *)header,
1888 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1889 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1893 if ((ctx->ext_attr_ver == 1 &&
1894 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1895 (ctx->ext_attr_ver == 2 &&
1896 entry->e_name_index == 0)) {
1897 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1901 if (entry->e_value_block != 0) {
1902 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1905 if (entry->e_value_offs + entry->e_value_size > fs->blocksize) {
1906 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1910 if (entry->e_value_size &&
1911 region_allocate(region, entry->e_value_offs,
1912 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1913 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1917 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
1918 entry->e_value_offs);
1920 if (entry->e_hash != hash) {
1921 pctx->num = entry->e_hash;
1922 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
1924 entry->e_hash = hash;
1927 entry = EXT2_EXT_ATTR_NEXT(entry);
1929 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1930 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1933 region_free(region);
1935 count = header->h_refcount - 1;
1937 ea_refcount_store(ctx->refcount, blk, count);
1938 mark_block_used(ctx, blk);
1939 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
1944 region_free(region);
1945 ext2fs_file_acl_block_set(fs, inode, 0);
1946 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1950 /* Returns 1 if bad htree, 0 if OK */
1951 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1952 ext2_ino_t ino, struct ext2_inode *inode,
1955 struct ext2_dx_root_info *root;
1956 ext2_filsys fs = ctx->fs;
1960 if ((!LINUX_S_ISDIR(inode->i_mode) &&
1961 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1962 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX))) {
1963 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1964 if (fix_problem(ctx, PR_1_HTREE_SET, pctx))
1968 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
1970 if ((pctx->errcode) ||
1972 (blk < fs->super->s_first_data_block) ||
1973 (blk >= ext2fs_blocks_count(fs->super))) {
1974 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1975 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1981 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
1983 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1984 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1988 /* XXX should check that beginning matches a directory */
1989 root = (struct ext2_dx_root_info *) (block_buf + 24);
1991 if ((root->reserved_zero || root->info_length < 8) &&
1992 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1995 pctx->num = root->hash_version;
1996 if ((root->hash_version != EXT2_HASH_LEGACY) &&
1997 (root->hash_version != EXT2_HASH_HALF_MD4) &&
1998 (root->hash_version != EXT2_HASH_TEA) &&
1999 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2002 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2003 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2006 pctx->num = root->indirect_levels;
2007 if ((root->indirect_levels > 1) &&
2008 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2014 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2015 struct ext2_inode *inode, int restart_flag,
2019 inode->i_links_count = 0;
2020 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2021 inode->i_dtime = ctx->now;
2023 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2024 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2025 if (ctx->inode_reg_map)
2026 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2027 if (ctx->inode_badness)
2028 ext2fs_icount_store(ctx->inode_badness, ino, 0);
2031 * If the inode was partially accounted for before processing
2032 * was aborted, we need to restart the pass 1 scan.
2034 ctx->flags |= restart_flag;
2036 if (ino == EXT2_BAD_INO)
2037 memset(inode, 0, sizeof(struct ext2_inode));
2039 e2fsck_write_inode(ctx, ino, inode, source);
2043 * Use the multiple-blocks reclamation code to fix alignment problems in
2044 * a bigalloc filesystem. We want a logical cluster to map to *only* one
2045 * physical cluster, and we want the block offsets within that cluster to
2048 static int has_unaligned_cluster_map(e2fsck_t ctx,
2049 blk64_t last_pblk, e2_blkcnt_t last_lblk,
2050 blk64_t pblk, blk64_t lblk)
2052 blk64_t cluster_mask;
2054 if (!ctx->fs->cluster_ratio_bits)
2056 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
2059 * If the block in the logical cluster doesn't align with the block in
2060 * the physical cluster...
2062 if ((lblk & cluster_mask) != (pblk & cluster_mask))
2066 * If we cross a physical cluster boundary within a logical cluster...
2068 if (last_pblk && (lblk & cluster_mask) != 0 &&
2069 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
2070 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
2076 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2077 struct process_block_struct *pb,
2078 blk64_t start_block, blk64_t end_block,
2080 ext2_extent_handle_t ehandle)
2082 struct ext2fs_extent extent;
2083 blk64_t blk, last_lblk;
2084 e2_blkcnt_t blockcnt;
2086 int is_dir, is_leaf;
2088 struct ext2_extent_info info;
2090 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2094 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2096 while (!pctx->errcode && info.num_entries-- > 0) {
2097 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2098 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2099 last_lblk = extent.e_lblk + extent.e_len - 1;
2102 if (extent.e_pblk == 0 ||
2103 extent.e_pblk < ctx->fs->super->s_first_data_block ||
2104 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2105 problem = PR_1_EXTENT_BAD_START_BLK;
2106 else if (extent.e_lblk < start_block)
2107 problem = PR_1_OUT_OF_ORDER_EXTENTS;
2108 else if ((end_block && last_lblk > end_block) &&
2109 (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
2110 last_lblk > eof_block)))
2111 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
2112 else if (is_leaf && extent.e_len == 0)
2113 problem = PR_1_EXTENT_LENGTH_ZERO;
2115 (extent.e_pblk + extent.e_len) >
2116 ext2fs_blocks_count(ctx->fs->super))
2117 problem = PR_1_EXTENT_ENDS_BEYOND;
2118 else if (is_leaf && is_dir &&
2119 ((extent.e_lblk + extent.e_len) >
2120 (1 << (21 - ctx->fs->super->s_log_block_size))))
2121 problem = PR_1_TOOBIG_DIR;
2124 * Uninitialized blocks in a directory? Clear the flag and
2125 * we'll interpret the blocks later.
2127 if (is_dir && problem == 0 &&
2128 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2129 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
2130 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2131 pb->inode_modified = 1;
2132 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2139 /* To ensure that extent is in inode */
2140 if (info.curr_level == 0)
2141 e2fsck_mark_inode_bad(ctx, pctx->ino,
2144 pctx->blk = extent.e_pblk;
2145 pctx->blk2 = extent.e_lblk;
2146 pctx->num = extent.e_len;
2147 pctx->blkcount = extent.e_lblk + extent.e_len;
2148 if (fix_problem(ctx, problem, pctx)) {
2149 if (ctx->invalid_bitmaps) {
2151 * If fsck knows the bitmaps are bad,
2152 * skip to the next extent and
2153 * try to clear this extent again
2154 * after fixing the bitmaps, by
2157 pctx->errcode = ext2fs_extent_get(
2159 EXT2_EXTENT_NEXT_SIB,
2161 ctx->flags |= E2F_FLAG_RESTART_LATER;
2162 if (pctx->errcode ==
2163 EXT2_ET_NO_CURRENT_NODE) {
2169 e2fsck_read_bitmaps(ctx);
2170 pb->inode_modified = 1;
2172 ext2fs_extent_delete(ehandle, 0);
2173 if (pctx->errcode) {
2174 pctx->str = "ext2fs_extent_delete";
2177 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2178 if (pctx->errcode &&
2179 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
2180 pctx->str = "ext2fs_extent_fix_parents";
2183 pctx->errcode = ext2fs_extent_get(ehandle,
2184 EXT2_EXTENT_CURRENT,
2186 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2196 blk64_t lblk = extent.e_lblk;
2198 blk = extent.e_pblk;
2199 pctx->errcode = ext2fs_extent_get(ehandle,
2200 EXT2_EXTENT_DOWN, &extent);
2201 if (pctx->errcode) {
2202 pctx->str = "EXT2_EXTENT_DOWN";
2203 problem = PR_1_EXTENT_HEADER_INVALID;
2204 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
2205 goto report_problem;
2208 /* The next extent should match this index's logical start */
2209 if (extent.e_lblk != lblk) {
2210 struct ext2_extent_info e_info;
2212 ext2fs_extent_get_info(ehandle, &e_info);
2214 pctx->blk2 = extent.e_lblk;
2215 pctx->num = e_info.curr_level - 1;
2216 problem = PR_1_EXTENT_INDEX_START_INVALID;
2217 if (fix_problem(ctx, problem, pctx)) {
2218 pb->inode_modified = 1;
2220 ext2fs_extent_fix_parents(ehandle);
2221 if (pctx->errcode) {
2222 pctx->str = "ext2fs_extent_fix_parents";
2227 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
2228 last_lblk, eof_block, ehandle);
2231 pctx->errcode = ext2fs_extent_get(ehandle,
2232 EXT2_EXTENT_UP, &extent);
2233 if (pctx->errcode) {
2234 pctx->str = "EXT2_EXTENT_UP";
2237 mark_block_used(ctx, blk);
2242 if ((pb->previous_block != 0) &&
2243 (pb->previous_block+1 != extent.e_pblk)) {
2244 if (ctx->options & E2F_OPT_FRAGCHECK) {
2249 else if (pb->is_reg)
2252 printf(("%6lu(%c): expecting %6lu "
2254 "phys %6lu log %lu len %lu\n"),
2255 (unsigned long) pctx->ino, type,
2256 (unsigned long) pb->previous_block+1,
2257 (unsigned long) extent.e_pblk,
2258 (unsigned long) extent.e_lblk,
2259 (unsigned long) extent.e_len);
2264 * If we notice a gap in the logical block mappings of an
2265 * extent-mapped directory, offer to close the hole by
2266 * moving the logical block down, otherwise we'll go mad in
2267 * pass 3 allocating empty directory blocks to fill the hole.
2270 pb->last_block + 1 < (e2_blkcnt_t)extent.e_lblk) {
2273 new_lblk = pb->last_block + 1;
2274 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
2275 new_lblk = ((new_lblk +
2276 EXT2FS_CLUSTER_RATIO(ctx->fs)) &
2277 EXT2FS_CLUSTER_MASK(ctx->fs)) |
2279 EXT2FS_CLUSTER_MASK(ctx->fs));
2280 pctx->blk = extent.e_lblk;
2281 pctx->blk2 = new_lblk;
2282 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
2283 extent.e_lblk = new_lblk;
2284 pb->inode_modified = 1;
2285 pctx->errcode = ext2fs_extent_replace(ehandle,
2287 if (pctx->errcode) {
2291 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2293 goto failed_add_dir_block;
2294 pctx->errcode = ext2fs_extent_goto(ehandle,
2297 goto failed_add_dir_block;
2298 last_lblk = extent.e_lblk + extent.e_len - 1;
2302 while (is_dir && (++pb->last_db_block <
2303 (e2_blkcnt_t) extent.e_lblk)) {
2304 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
2307 if (pctx->errcode) {
2309 pctx->num = pb->last_db_block;
2310 goto failed_add_dir_block;
2313 if (!ctx->fs->cluster_ratio_bits) {
2314 mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
2315 pb->num_blocks += extent.e_len;
2317 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
2319 blk++, blockcnt++, i++) {
2320 if (ctx->fs->cluster_ratio_bits &&
2321 !(pb->previous_block &&
2322 (EXT2FS_B2C(ctx->fs, blk) ==
2323 EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
2324 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2325 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2326 mark_block_used(ctx, blk);
2329 if (has_unaligned_cluster_map(ctx, pb->previous_block,
2330 pb->last_block, blk,
2332 pctx->blk = blockcnt;
2334 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2335 mark_block_used(ctx, blk);
2336 mark_block_used(ctx, blk);
2338 pb->last_block = blockcnt;
2339 pb->previous_block = blk;
2342 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
2343 if (pctx->errcode) {
2345 pctx->num = blockcnt;
2346 failed_add_dir_block:
2347 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2348 /* Should never get here */
2349 ctx->flags |= E2F_FLAG_ABORT;
2354 if (is_dir && extent.e_len > 0)
2355 pb->last_db_block = blockcnt - 1;
2356 pb->previous_block = extent.e_pblk + extent.e_len - 1;
2357 start_block = pb->last_block = last_lblk;
2358 if (is_leaf && !is_dir &&
2359 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
2360 pb->last_init_lblock = last_lblk;
2362 pctx->errcode = ext2fs_extent_get(ehandle,
2363 EXT2_EXTENT_NEXT_SIB,
2366 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
2370 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
2371 struct process_block_struct *pb)
2373 struct ext2_extent_info info;
2374 struct ext2_inode *inode = pctx->inode;
2375 ext2_extent_handle_t ehandle;
2376 ext2_filsys fs = ctx->fs;
2377 ext2_ino_t ino = pctx->ino;
2381 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
2382 if (pctx->errcode) {
2383 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
2384 e2fsck_clear_inode(ctx, ino, inode, 0,
2385 "check_blocks_extents");
2390 retval = ext2fs_extent_get_info(ehandle, &info);
2392 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
2393 info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
2394 ctx->extent_depth_count[info.max_depth]++;
2397 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
2398 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
2399 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle);
2400 if (pctx->errcode &&
2401 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
2403 inode->i_blocks = 0;
2404 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2405 "check_blocks_extents");
2408 ext2fs_extent_free(ehandle);
2412 * This subroutine is called on each inode to account for all of the
2413 * blocks used by that inode.
2415 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
2418 ext2_filsys fs = ctx->fs;
2419 struct process_block_struct pb;
2420 ext2_ino_t ino = pctx->ino;
2421 struct ext2_inode *inode = pctx->inode;
2422 unsigned bad_size = 0;
2423 int dirty_inode = 0;
2430 pb.last_init_lblock = -1;
2431 pb.last_db_block = -1;
2432 pb.num_illegal_blocks = 0;
2433 pb.suppress = 0; pb.clear = 0;
2436 pb.previous_block = 0;
2437 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2438 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
2439 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
2443 pb.inode_modified = 0;
2447 extent_fs = (ctx->fs->super->s_feature_incompat &
2448 EXT3_FEATURE_INCOMPAT_EXTENTS);
2450 if (inode->i_flags & EXT2_COMPRBLK_FL) {
2451 if (fs->super->s_feature_incompat &
2452 EXT2_FEATURE_INCOMPAT_COMPRESSION)
2455 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2456 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
2457 inode->i_flags &= ~EXT2_COMPRBLK_FL;
2463 if (ext2fs_file_acl_block(fs, inode) &&
2464 check_ext_attr(ctx, pctx, block_buf)) {
2465 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2470 if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
2471 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
2472 check_blocks_extents(ctx, pctx, &pb);
2475 * If we've modified the inode, write it out before
2476 * iterate() tries to use it.
2479 e2fsck_write_inode(ctx, ino, inode,
2483 pctx->errcode = ext2fs_block_iterate3(fs, ino,
2484 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
2485 block_buf, process_block, &pb);
2487 * We do not have uninitialized extents in non extent
2490 pb.last_init_lblock = pb.last_block;
2492 * If iterate() changed a block mapping, we have to
2493 * re-read the inode. If we decide to clear the
2494 * inode after clearing some stuff, we'll re-write the
2495 * bad mappings into the inode!
2497 if (pb.inode_modified)
2498 e2fsck_read_inode(ctx, ino, inode,
2502 end_problem_latch(ctx, PR_LATCH_BLOCK);
2503 end_problem_latch(ctx, PR_LATCH_TOOBIG);
2504 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2507 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
2509 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
2510 if (LINUX_S_ISDIR(inode->i_mode))
2511 ctx->fs_fragmented_dir++;
2513 ctx->fs_fragmented++;
2517 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2522 if (inode->i_flags & EXT2_INDEX_FL) {
2523 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
2524 inode->i_flags &= ~EXT2_INDEX_FL;
2528 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
2533 if (!pb.num_blocks && pb.is_dir) {
2535 * The mode might be in-correct. Increasing the badness by
2536 * small amount won't hurt much.
2538 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2539 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
2540 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
2541 ctx->fs_directory_count--;
2546 if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
2547 quota_data_add(ctx->qctx, inode, ino,
2548 pb.num_blocks * fs->blocksize);
2549 quota_data_inodes(ctx->qctx, inode, ino, +1);
2552 if (!(fs->super->s_feature_ro_compat &
2553 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
2554 !(inode->i_flags & EXT4_HUGE_FILE_FL))
2555 pb.num_blocks *= (fs->blocksize / 512);
2556 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
2558 printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
2559 ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
2563 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
2564 if (inode->i_size & (fs->blocksize - 1))
2566 else if (nblock > (pb.last_block + 1))
2568 else if (nblock < (pb.last_block + 1)) {
2569 if (((pb.last_block + 1) - nblock) >
2570 fs->super->s_prealloc_dir_blocks)
2574 e2_blkcnt_t blkpg = ctx->blocks_per_page;
2576 size = EXT2_I_SIZE(inode);
2577 if ((pb.last_init_lblock >= 0) &&
2578 /* if size is smaller than expected by the block count,
2579 * allow allocated blocks to end of PAGE_SIZE.
2580 * last_init_lblock is the last in-use block, so it is
2581 * the minimum expected file size. */
2582 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
2583 ((pb.last_init_lblock + 1) / blkpg * blkpg !=
2584 (pb.last_init_lblock + 1) ||
2585 size < (__u64)(pb.last_init_lblock & ~(blkpg - 1)) *
2588 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2589 size > ext2_max_sizes[fs->super->s_log_block_size])
2590 /* too big for a direct/indirect-mapped file */
2592 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2594 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
2595 /* too big for an extent-based file - 32bit ee_block */
2598 /* i_size for symlinks is checked elsewhere */
2599 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
2600 pctx->num = (pb.last_block+1) * fs->blocksize;
2601 pctx->group = bad_size;
2602 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2603 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
2604 if (LINUX_S_ISDIR(inode->i_mode))
2605 pctx->num &= 0xFFFFFFFFULL;
2606 ext2fs_inode_size_set(fs, inode, pctx->num);
2611 if (LINUX_S_ISREG(inode->i_mode) &&
2612 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
2614 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
2615 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
2616 ((fs->super->s_feature_ro_compat &
2617 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
2618 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
2619 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
2620 pctx->num = pb.num_blocks;
2621 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2622 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
2623 inode->i_blocks = pb.num_blocks;
2624 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
2630 if (ctx->dirs_to_hash && pb.is_dir &&
2631 !(inode->i_flags & EXT2_INDEX_FL) &&
2632 ((inode->i_size / fs->blocksize) >= 3))
2633 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
2637 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
2642 * Helper function called by process block when an illegal block is
2643 * found. It returns a description about why the block is illegal
2645 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
2649 static char problem[80];
2651 super = fs->super->s_first_data_block;
2652 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
2653 if (block < super) {
2654 sprintf(problem, "< FIRSTBLOCK (%u)", super);
2656 } else if (block >= ext2fs_blocks_count(fs->super)) {
2657 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
2660 for (i = 0; i < fs->group_desc_count; i++) {
2661 if (block == super) {
2662 sprintf(problem, "is the superblock in group %d", i);
2665 if (block > super &&
2666 block <= (super + fs->desc_blocks)) {
2667 sprintf(problem, "is in the group descriptors "
2671 if (block == ext2fs_block_bitmap_loc(fs, i)) {
2672 sprintf(problem, "is the block bitmap of group %d", i);
2675 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
2676 sprintf(problem, "is the inode bitmap of group %d", i);
2679 if (block >= ext2fs_inode_table_loc(fs, i) &&
2680 (block < ext2fs_inode_table_loc(fs, i)
2681 + fs->inode_blocks_per_group)) {
2682 sprintf(problem, "is in the inode table of group %d",
2686 super += fs->super->s_blocks_per_group;
2693 * This is a helper function for check_blocks().
2695 static int process_block(ext2_filsys fs,
2697 e2_blkcnt_t blockcnt,
2698 blk64_t ref_block EXT2FS_ATTR((unused)),
2699 int ref_offset EXT2FS_ATTR((unused)),
2702 struct process_block_struct *p;
2703 struct problem_context *pctx;
2704 blk64_t blk = *block_nr;
2706 problem_t problem = 0;
2709 p = (struct process_block_struct *) priv_data;
2713 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
2714 /* todo: Check that the comprblk_fl is high, that the
2715 blkaddr pattern looks right (all non-holes up to
2716 first EXT2FS_COMPRESSED_BLKADDR, then all
2717 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2718 that the feature_incompat bit is high, and that the
2719 inode is a regular file. If we're doing a "full
2720 check" (a concept introduced to e2fsck by e2compr,
2721 meaning that we look at data blocks as well as
2722 metadata) then call some library routine that
2723 checks the compressed data. I'll have to think
2724 about this, because one particularly important
2725 problem to be able to fix is to recalculate the
2726 cluster size if necessary. I think that perhaps
2727 we'd better do most/all e2compr-specific checks
2728 separately, after the non-e2compr checks. If not
2729 doing a full check, it may be useful to test that
2730 the personality is linux; e.g. if it isn't then
2731 perhaps this really is just an illegal block. */
2736 * For a directory, add logical block zero for processing even if it's
2737 * not mapped or we'll be perennially stuck with broken "." and ".."
2740 if (p->is_dir && blockcnt == 0 && blk == 0) {
2741 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
2742 if (pctx->errcode) {
2744 pctx->num = blockcnt;
2745 goto failed_add_dir_block;
2754 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
2759 * Simplistic fragmentation check. We merely require that the
2760 * file be contiguous. (Which can never be true for really
2761 * big files that are greater than a block group.)
2763 if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
2764 if (p->previous_block+1 != blk) {
2765 if (ctx->options & E2F_OPT_FRAGCHECK) {
2773 printf(_("%6lu(%c): expecting %6lu "
2774 "got phys %6lu (blkcnt %lld)\n"),
2775 (unsigned long) pctx->ino, type,
2776 (unsigned long) p->previous_block+1,
2777 (unsigned long) blk,
2784 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
2785 problem = PR_1_TOOBIG_DIR;
2786 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
2787 problem = PR_1_TOOBIG_REG;
2788 if (!p->is_dir && !p->is_reg && blockcnt > 0)
2789 problem = PR_1_TOOBIG_SYMLINK;
2791 if (blk < fs->super->s_first_data_block ||
2792 blk >= ext2fs_blocks_count(fs->super)) {
2793 problem = PR_1_ILLEGAL_BLOCK_NUM;
2794 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
2798 p->num_illegal_blocks++;
2799 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
2800 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
2804 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
2806 set_latch_flags(PR_LATCH_BLOCK,
2811 pctx->blkcount = blockcnt;
2812 if (fix_problem(ctx, problem, pctx)) {
2813 blk = *block_nr = 0;
2814 ret_code = BLOCK_CHANGED;
2815 p->inode_modified = 1;
2817 * If the directory block is too big and is beyond the
2818 * end of the FS, don't bother trying to add it for
2819 * processing -- the kernel would never have created a
2820 * directory this large, and we risk an ENOMEM abort.
2821 * In any case, the toobig handler for extent-based
2822 * directories also doesn't feed toobig blocks to
2825 if (problem == PR_1_TOOBIG_DIR)
2832 if (p->ino == EXT2_RESIZE_INO) {
2834 * The resize inode has already be sanity checked
2835 * during pass #0 (the superblock checks). All we
2836 * have to do is mark the double indirect block as
2837 * being in use; all of the other blocks are handled
2838 * by mark_table_blocks()).
2840 if (blockcnt == BLOCK_COUNT_DIND)
2841 mark_block_used(ctx, blk);
2843 } else if (!(ctx->fs->cluster_ratio_bits &&
2844 p->previous_block &&
2845 (EXT2FS_B2C(ctx->fs, blk) ==
2846 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
2847 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2848 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2849 mark_block_used(ctx, blk);
2851 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
2852 p->last_block, blk, blockcnt)) {
2853 pctx->blk = blockcnt;
2855 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2856 mark_block_used(ctx, blk);
2857 mark_block_used(ctx, blk);
2860 p->last_block = blockcnt;
2861 p->previous_block = blk;
2863 if (p->is_dir && (blockcnt >= 0)) {
2864 while (++p->last_db_block < blockcnt) {
2865 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
2868 if (pctx->errcode) {
2870 pctx->num = p->last_db_block;
2871 goto failed_add_dir_block;
2874 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
2876 if (pctx->errcode) {
2878 pctx->num = blockcnt;
2879 failed_add_dir_block:
2880 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2881 /* Should never get here */
2882 ctx->flags |= E2F_FLAG_ABORT;
2889 static int process_bad_block(ext2_filsys fs,
2891 e2_blkcnt_t blockcnt,
2892 blk64_t ref_block EXT2FS_ATTR((unused)),
2893 int ref_offset EXT2FS_ATTR((unused)),
2896 struct process_block_struct *p;
2897 blk64_t blk = *block_nr;
2898 blk64_t first_block;
2900 struct problem_context *pctx;
2904 * Note: This function processes blocks for the bad blocks
2905 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
2911 p = (struct process_block_struct *) priv_data;
2915 pctx->ino = EXT2_BAD_INO;
2917 pctx->blkcount = blockcnt;
2919 if ((blk < fs->super->s_first_data_block) ||
2920 (blk >= ext2fs_blocks_count(fs->super))) {
2921 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
2923 return BLOCK_CHANGED;
2929 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
2931 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
2933 return BLOCK_CHANGED;
2935 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2938 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
2941 return BLOCK_CHANGED;
2943 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2946 mark_block_used(ctx, blk);
2950 printf ("DEBUG: Marking %u as bad.\n", blk);
2952 ctx->fs_badblocks_count++;
2954 * If the block is not used, then mark it as used and return.
2955 * If it is already marked as found, this must mean that
2956 * there's an overlap between the filesystem table blocks
2957 * (bitmaps and inode table) and the bad block list.
2959 if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
2960 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
2964 * Try to find the where the filesystem block was used...
2966 first_block = fs->super->s_first_data_block;
2968 for (i = 0; i < fs->group_desc_count; i++ ) {
2971 if (!ext2fs_bg_has_super(fs, i))
2973 if (blk == first_block) {
2975 if (fix_problem(ctx,
2976 PR_1_BAD_PRIMARY_SUPERBLOCK,
2979 return BLOCK_CHANGED;
2983 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
2986 if ((blk > first_block) &&
2987 (blk <= first_block + fs->desc_blocks)) {
2989 pctx->blk = *block_nr;
2990 if (fix_problem(ctx,
2991 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
2993 return BLOCK_CHANGED;
2997 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
3001 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
3002 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
3003 ctx->invalid_block_bitmap_flag[i]++;
3004 ctx->invalid_bitmaps++;
3008 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
3009 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
3010 ctx->invalid_inode_bitmap_flag[i]++;
3011 ctx->invalid_bitmaps++;
3015 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
3016 (blk < (ext2fs_inode_table_loc(fs, i) +
3017 fs->inode_blocks_per_group))) {
3019 * If there are bad blocks in the inode table,
3020 * the inode scan code will try to do
3021 * something reasonable automatically.
3025 first_block += fs->super->s_blocks_per_group;
3028 * If we've gotten to this point, then the only
3029 * possibility is that the bad block inode meta data
3030 * is using a bad block.
3032 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
3033 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
3034 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
3036 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
3038 return BLOCK_CHANGED;
3040 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3047 /* Warn user that the block wasn't claimed */
3048 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
3053 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
3054 const char *name, int num, blk64_t *new_block)
3056 ext2_filsys fs = ctx->fs;
3058 blk64_t old_block = *new_block;
3061 unsigned flexbg_size;
3064 struct problem_context pctx;
3066 clear_problem_context(&pctx);
3069 pctx.blk = old_block;
3073 * For flex_bg filesystems, first try to allocate the metadata
3074 * within the flex_bg, and if that fails then try finding the
3075 * space anywhere in the filesystem.
3077 is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
3078 EXT4_FEATURE_INCOMPAT_FLEX_BG);
3080 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3081 flexbg = group / flexbg_size;
3082 first_block = ext2fs_group_first_block2(fs,
3083 flexbg_size * flexbg);
3084 last_grp = group | (flexbg_size - 1);
3085 if (last_grp >= fs->group_desc_count)
3086 last_grp = fs->group_desc_count - 1;
3087 last_block = ext2fs_group_last_block2(fs, last_grp);
3089 last_block = ext2fs_group_last_block2(fs, group);
3090 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
3091 num, ctx->block_found_map,
3093 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
3094 pctx.errcode = ext2fs_get_free_blocks2(fs,
3095 fs->super->s_first_data_block,
3096 ext2fs_blocks_count(fs->super),
3097 num, ctx->block_found_map, new_block);
3100 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
3101 ext2fs_unmark_valid(fs);
3102 ctx->flags |= E2F_FLAG_ABORT;
3105 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
3107 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
3108 ext2fs_unmark_valid(fs);
3109 ctx->flags |= E2F_FLAG_ABORT;
3112 ext2fs_mark_super_dirty(fs);
3113 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3114 pctx.blk2 = *new_block;
3115 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
3116 PR_1_RELOC_TO), &pctx);
3118 for (i = 0; i < num; i++) {
3120 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
3122 pctx.errcode = io_channel_read_blk64(fs->io,
3123 old_block + i, 1, buf);
3125 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
3127 memset(buf, 0, fs->blocksize);
3129 pctx.blk = (*new_block) + i;
3130 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
3133 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
3135 ext2fs_free_mem(&buf);
3139 * This routine gets called at the end of pass 1 if bad blocks are
3140 * detected in the superblock, group descriptors, inode_bitmaps, or
3141 * block bitmaps. At this point, all of the blocks have been mapped
3142 * out, so we can try to allocate new block(s) to replace the bad
3145 static void handle_fs_bad_blocks(e2fsck_t ctx)
3147 ext2_filsys fs = ctx->fs;
3149 blk64_t first_block;
3152 for (i = 0; i < fs->group_desc_count; i++) {
3153 first_block = ext2fs_group_first_block2(fs, i);
3155 if (ctx->invalid_block_bitmap_flag[i]) {
3156 new_blk = ext2fs_block_bitmap_loc(fs, i);
3157 new_table_block(ctx, first_block, i, _("block bitmap"),
3159 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
3161 if (ctx->invalid_inode_bitmap_flag[i]) {
3162 new_blk = ext2fs_inode_bitmap_loc(fs, i);
3163 new_table_block(ctx, first_block, i, _("inode bitmap"),
3165 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
3167 if (ctx->invalid_inode_table_flag[i]) {
3168 new_blk = ext2fs_inode_table_loc(fs, i);
3169 new_table_block(ctx, first_block, i, _("inode table"),
3170 fs->inode_blocks_per_group,
3172 ext2fs_inode_table_loc_set(fs, i, new_blk);
3173 ctx->flags |= E2F_FLAG_RESTART;
3176 ctx->invalid_bitmaps = 0;
3180 * This routine marks all blocks which are used by the superblock,
3181 * group descriptors, inode bitmaps, and block bitmaps.
3183 static void mark_table_blocks(e2fsck_t ctx)
3185 ext2_filsys fs = ctx->fs;
3189 struct problem_context pctx;
3191 clear_problem_context(&pctx);
3193 for (i = 0; i < fs->group_desc_count; i++) {
3196 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
3199 * Mark the blocks used for the inode table
3201 if (ext2fs_inode_table_loc(fs, i)) {
3202 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
3203 j < fs->inode_blocks_per_group;
3205 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3208 if (!ctx->invalid_inode_table_flag[i] &&
3210 PR_1_ITABLE_CONFLICT, &pctx)) {
3211 ctx->invalid_inode_table_flag[i]++;
3212 ctx->invalid_bitmaps++;
3215 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3222 * Mark block used for the block bitmap
3224 if (ext2fs_block_bitmap_loc(fs, i)) {
3225 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3226 ext2fs_block_bitmap_loc(fs, i))) {
3227 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
3228 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
3229 ctx->invalid_block_bitmap_flag[i]++;
3230 ctx->invalid_bitmaps++;
3233 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3234 ext2fs_block_bitmap_loc(fs, i));
3239 * Mark block used for the inode bitmap
3241 if (ext2fs_inode_bitmap_loc(fs, i)) {
3242 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3243 ext2fs_inode_bitmap_loc(fs, i))) {
3244 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
3245 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
3246 ctx->invalid_inode_bitmap_flag[i]++;
3247 ctx->invalid_bitmaps++;
3250 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3251 ext2fs_inode_bitmap_loc(fs, i));
3258 * Thes subroutines short circuits ext2fs_get_blocks and
3259 * ext2fs_check_directory; we use them since we already have the inode
3260 * structure, so there's no point in letting the ext2fs library read
3263 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
3266 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3269 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3270 return EXT2_ET_CALLBACK_NOTHANDLED;
3272 for (i=0; i < EXT2_N_BLOCKS; i++)
3273 blocks[i] = ctx->stashed_inode->i_block[i];
3277 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
3278 struct ext2_inode *inode)
3280 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3282 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3283 return EXT2_ET_CALLBACK_NOTHANDLED;
3284 *inode = *ctx->stashed_inode;
3288 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
3289 struct ext2_inode *inode)
3291 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3293 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
3294 (inode != ctx->stashed_inode))
3295 *ctx->stashed_inode = *inode;
3296 return EXT2_ET_CALLBACK_NOTHANDLED;
3299 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
3301 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3303 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3304 return EXT2_ET_CALLBACK_NOTHANDLED;
3306 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
3307 return EXT2_ET_NO_DIRECTORY;
3311 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
3314 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3318 if (ctx->block_found_map) {
3319 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
3323 if (fs->block_map) {
3324 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
3325 ext2fs_mark_bb_dirty(fs);
3328 if (!fs->block_map) {
3329 retval = ext2fs_read_block_bitmap(fs);
3334 retval = ext2fs_new_block2(fs, goal, 0, &new_block);
3343 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
3345 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3347 if (ctx->block_found_map) {
3349 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3351 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
3355 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
3357 ext2_filsys fs = ctx->fs;
3359 if (use_shortcuts) {
3360 fs->get_blocks = pass1_get_blocks;
3361 fs->check_directory = pass1_check_directory;
3362 fs->read_inode = pass1_read_inode;
3363 fs->write_inode = pass1_write_inode;
3364 ctx->stashed_ino = 0;
3367 fs->check_directory = 0;
3369 fs->write_inode = 0;
3373 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
3375 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
3376 ext2fs_set_block_alloc_stats_callback(ctx->fs,
3377 e2fsck_block_alloc_stats, 0);