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) -
286 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize;
287 entry = &IHDR(inode)->h_first_entry[0];
288 start = (char *)entry;
290 /* scan all entry's headers first */
292 /* take finish entry 0UL into account */
293 remain = storage_size - sizeof(__u32);
295 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
298 /* header eats this space */
299 remain -= sizeof(struct ext2_ext_attr_entry);
301 /* is attribute name valid? */
302 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
303 pctx->num = entry->e_name_len;
304 problem = PR_1_ATTR_NAME_LEN;
308 /* attribute len eats this space */
309 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
311 /* check value size */
312 if (entry->e_value_size > remain) {
313 pctx->num = entry->e_value_size;
314 problem = PR_1_ATTR_VALUE_SIZE;
318 /* e_value_block must be 0 in inode's ea */
319 if (entry->e_value_block != 0) {
320 pctx->num = entry->e_value_block;
321 problem = PR_1_ATTR_VALUE_BLOCK;
325 /* Value size cannot be larger than EA space in inode */
326 if (entry->e_value_offs > storage_size ||
327 entry->e_value_offs + entry->e_value_size > storage_size) {
328 problem = PR_1_INODE_EA_BAD_VALUE;
332 hash = ext2fs_ext_attr_hash_entry(entry,
333 start + entry->e_value_offs);
335 /* e_hash may be 0 in older inode's ea */
336 if (entry->e_hash != 0 && entry->e_hash != hash) {
337 pctx->num = entry->e_hash;
338 problem = PR_1_ATTR_HASH;
342 remain -= entry->e_value_size;
344 entry = EXT2_EXT_ATTR_NEXT(entry);
348 * it seems like a corruption. it's very unlikely we could repair
349 * EA(s) in automatic fashion -bzzz
351 if (problem == 0 || !fix_problem(ctx, problem, pctx))
354 /* simply remove all possible EA(s) */
355 *((__u32 *)start) = 0UL;
356 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
357 EXT2_INODE_SIZE(sb), "pass1");
360 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
362 struct ext2_super_block *sb = ctx->fs->super;
363 struct ext2_inode_large *inode;
365 int min, max, dirty = 0;
367 inode = (struct ext2_inode_large *) pctx->inode;
368 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
369 /* this isn't large inode. so, nothing to check */
374 printf("inode #%u, i_extra_size %d\n", pctx->ino,
375 inode->i_extra_isize);
377 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
378 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
379 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
381 * For now we will allow i_extra_isize to be 0, but really
382 * implementations should never allow i_extra_isize to be 0
384 if (inode->i_extra_isize &&
385 (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
386 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
387 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
389 inode->i_extra_isize = ctx->want_extra_isize;
395 if (EXT4_FITS_IN_INODE(inode, inode, i_crtime) &&
396 inode->i_crtime != 0 &&
397 (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, 2*ctx->time_fudge) ||
398 EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, 2*ctx->time_fudge))) {
399 pctx->num = inode->i_crtime;
400 if (fix_problem(ctx, PR_1_CRTIME_BAD, pctx)) {
404 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_HIGH);
407 eamagic = &IHDR(inode)->h_magic;
408 if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
409 (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
410 (inode->i_extra_isize < ctx->want_extra_isize)) {
411 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
412 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
413 EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
414 inode->i_extra_isize = ctx->want_extra_isize;
416 if (inode->i_extra_isize < ctx->min_extra_isize)
417 ctx->min_extra_isize = inode->i_extra_isize;
420 if (*eamagic == EXT2_EXT_ATTR_MAGIC)
421 check_ea_in_inode(ctx, pctx);
424 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
425 EXT2_INODE_SIZE(sb), "pass1");
429 * Check to see if the inode might really be a directory, despite i_mode
431 * This is a lot of complexity for something for which I'm not really
432 * convinced happens frequently in the wild. If for any reason this
433 * causes any problems, take this code out.
434 * [tytso:20070331.0827EDT]
436 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
439 struct ext2_inode *inode = pctx->inode;
440 struct ext2_dir_entry *dirent;
443 unsigned int i, rec_len, not_device = 0;
447 * If the mode looks OK, we believe it. If the first block in
448 * the i_block array is 0, this cannot be a directory. If the
449 * inode is extent-mapped, it is still the case that the latter
450 * cannot be 0 - the magic number in the extent header would make
453 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
454 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
458 * Check the block numbers in the i_block array for validity:
459 * zero blocks are skipped (but the first one cannot be zero -
460 * see above), other blocks are checked against the first and
461 * max data blocks (from the the superblock) and against the
462 * block bitmap. Any invalid block found means this cannot be
465 * If there are non-zero blocks past the fourth entry, then
466 * this cannot be a device file: we remember that for the next
469 * For extent mapped files, we don't do any sanity checking:
470 * just try to get the phys block of logical block 0 and run
474 extent_fs = (ctx->fs->super->s_feature_incompat &
475 EXT3_FEATURE_INCOMPAT_EXTENTS);
476 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
478 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
481 /* device files are never extent mapped */
484 for (i=0; i < EXT2_N_BLOCKS; i++) {
485 blk = inode->i_block[i];
491 if (blk < ctx->fs->super->s_first_data_block ||
492 blk >= ext2fs_blocks_count(ctx->fs->super) ||
493 ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
495 return; /* Invalid block, can't be dir */
497 blk = inode->i_block[0];
501 * If the mode says this is a device file and the i_links_count field
502 * is sane and we have not ruled it out as a device file previously,
503 * we declare it a device file, not a directory.
505 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
506 (inode->i_links_count == 1) && !not_device)
509 /* read the first block */
510 ehandler_operation(_("reading directory block"));
511 retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0);
512 ehandler_operation(0);
516 dirent = (struct ext2_dir_entry *) buf;
517 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
520 if (((dirent->name_len & 0xFF) != 1) ||
521 (dirent->name[0] != '.') ||
522 (dirent->inode != pctx->ino) ||
525 (rec_len >= ctx->fs->blocksize - 12))
528 dirent = (struct ext2_dir_entry *) (buf + rec_len);
529 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
532 if (((dirent->name_len & 0xFF) != 2) ||
533 (dirent->name[0] != '.') ||
534 (dirent->name[1] != '.') ||
539 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
540 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
541 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
542 e2fsck_write_inode_full(ctx, pctx->ino, inode,
543 EXT2_INODE_SIZE(ctx->fs->super),
544 "check_is_really_dir");
548 void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
551 unsigned int threshold;
559 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
561 profile_get_uint(ctx->profile, "scratch_files",
562 "numdirs_threshold", 0, 0, &threshold);
563 profile_get_boolean(ctx->profile, "scratch_files",
564 "icount", 0, 1, &enable);
566 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
568 num_dirs = 1024; /* Guess */
570 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
571 (threshold && num_dirs <= threshold))
574 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
579 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
580 struct problem_context *pctx, int needed_size)
582 struct ext2_ext_attr_header *header;
583 struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
584 char *start, name[4096], block_buf[4096];
585 int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
586 int in_inode = 1, error;
587 unsigned int freed_bytes = inode->i_extra_isize;
589 entry_ino = &IHDR(inode)->h_first_entry[0];
590 start = (char *)entry_ino;
592 if (inode->i_file_acl) {
593 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
595 /* We have already checked this block, shouldn't happen */
597 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
600 header = BHDR(block_buf);
601 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
602 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
606 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
609 len = sizeof(entry->e_name);
610 entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
612 while (freed_bytes < needed_size) {
613 if (entry_size && name[0] != '\0') {
615 if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
616 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
617 EXT2_EXT_ATTR_SIZE(entry->e_value_size);
618 error = ext2fs_attr_set(ctx->fs, pctx->ino,
619 (struct ext2_inode *)inode,
620 index, name, 0, 0, 0);
622 freed_bytes += ea_size;
625 len = sizeof(entry->e_name);
626 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
627 entry = EXT2_EXT_ATTR_NEXT(entry);
628 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
631 len = sizeof(entry->e_name);
632 entry_size = ext2fs_attr_get_next_attr(entry,
633 index, name, len, 1);
638 if (!entry && index < EXT2_ATTR_INDEX_MAX)
639 entry = (struct ext2_ext_attr_entry *)start;
649 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
650 struct problem_context *pctx)
652 int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
656 retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
657 ctx->want_extra_isize, &ret,
659 if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
660 goto mark_expand_eisize_map;
662 e2fsck_write_inode_full(ctx, pctx->ino,
663 (struct ext2_inode *)inode,
664 EXT2_INODE_SIZE(ctx->fs->super),
669 if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
670 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
671 fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
672 ctx->flags |= E2F_FLAG_ABORT;
677 pctx->num = ctx->fs->super->s_min_extra_isize;
678 fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
682 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
684 if (retval >= ctx->want_extra_isize)
687 needed_size -= retval;
690 * We loop here until either the user deletes EA(s) or
691 * EXTRA_ISIZE feature is disabled.
693 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
694 ctx->fs->super->s_feature_ro_compat &=
695 ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
696 ext2fs_mark_super_dirty(ctx->fs);
700 ctx->fs_unexpanded_inodes++;
702 /* No EA was deleted, inode cannot be expanded */
706 mark_expand_eisize_map:
707 if (!ctx->expand_eisize_map) {
708 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
709 _("expand extrz isize map"),
710 &ctx->expand_eisize_map);
712 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
718 /* Add this inode to the expand_eisize_map */
719 ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
723 static void reserve_block_for_root_repair(e2fsck_t ctx)
727 ext2_filsys fs = ctx->fs;
729 ctx->root_repair_block = 0;
730 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
733 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
736 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
737 ctx->root_repair_block = blk;
740 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
744 ext2_filsys fs = ctx->fs;
745 static const char name[] = "lost+found";
748 ctx->lnf_repair_block = 0;
749 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
752 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
755 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
756 ctx->lnf_repair_block = blk;
759 void e2fsck_pass1(e2fsck_t ctx)
763 ext2_filsys fs = ctx->fs;
765 struct ext2_inode *inode = NULL;
766 ext2_inode_scan scan = NULL;
767 char *block_buf = NULL;
768 #ifdef RESOURCE_TRACK
769 struct resource_track rtrack;
771 unsigned char frag, fsize;
772 struct problem_context pctx;
773 struct scan_callback_struct scan_struct;
774 struct ext2_super_block *sb = ctx->fs->super;
776 unsigned int save_type;
777 int imagic_fs, extent_fs;
778 int low_dtime_check = 1;
783 init_resource_track(&rtrack, ctx->fs->io);
784 clear_problem_context(&pctx);
786 if (!(ctx->options & E2F_OPT_PREEN))
787 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
789 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
790 !(ctx->options & E2F_OPT_NO)) {
791 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
792 ctx->dirs_to_hash = 0;
796 mtrace_print("Pass 1");
799 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
801 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
802 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
803 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
804 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
805 max_sizes = (max_sizes * (1UL << i));
806 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
810 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
811 extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
814 * Allocate bitmaps structures
816 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
817 EXT2FS_BMAP64_RBTREE,
819 &ctx->inode_used_map);
822 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
823 ctx->flags |= E2F_FLAG_ABORT;
826 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
827 _("directory inode map"),
828 EXT2FS_BMAP64_AUTODIR,
829 "inode_dir_map", &ctx->inode_dir_map);
832 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
833 ctx->flags |= E2F_FLAG_ABORT;
836 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
837 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
838 "inode_reg_map", &ctx->inode_reg_map);
841 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
842 ctx->flags |= E2F_FLAG_ABORT;
845 pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
846 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
847 "block_found_map", &ctx->block_found_map);
850 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
851 ctx->flags |= E2F_FLAG_ABORT;
854 e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
855 if (!ctx->inode_link_info) {
856 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
857 "inode_link_info", &save_type);
858 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
859 &ctx->inode_link_info);
860 fs->default_bitmap_type = save_type;
864 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
865 ctx->flags |= E2F_FLAG_ABORT;
868 inode_size = EXT2_INODE_SIZE(fs->super);
869 inode = (struct ext2_inode *)
870 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
872 inodes_to_process = (struct process_inode_block *)
873 e2fsck_allocate_memory(ctx,
874 (ctx->process_inode_size *
875 sizeof(struct process_inode_block)),
876 "array of inodes to process");
877 process_inode_count = 0;
879 pctx.errcode = ext2fs_init_dblist(fs, 0);
881 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
882 ctx->flags |= E2F_FLAG_ABORT;
887 * If the last orphan field is set, clear it, since the pass1
888 * processing will automatically find and clear the orphans.
889 * In the future, we may want to try using the last_orphan
890 * linked list ourselves, but for now, we clear it so that the
891 * ext3 mount code won't get confused.
893 if (!(ctx->options & E2F_OPT_READONLY)) {
894 if (fs->super->s_last_orphan) {
895 fs->super->s_last_orphan = 0;
896 ext2fs_mark_super_dirty(fs);
900 mark_table_blocks(ctx);
901 pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
902 &ctx->block_found_map);
904 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
905 ctx->flags |= E2F_FLAG_ABORT;
908 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
909 "block interate buffer");
910 e2fsck_use_inode_shortcuts(ctx, 1);
911 e2fsck_intercept_block_allocations(ctx);
912 old_op = ehandler_operation(_("opening inode scan"));
913 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
915 ehandler_operation(old_op);
917 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
918 ctx->flags |= E2F_FLAG_ABORT;
921 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
922 ctx->stashed_inode = inode;
923 scan_struct.ctx = ctx;
924 scan_struct.block_buf = block_buf;
925 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
926 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
927 ctx->fs->group_desc_count)))
929 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
930 (fs->super->s_mtime < fs->super->s_inodes_count) ||
931 (fs->super->s_mkfs_time &&
932 fs->super->s_mkfs_time < fs->super->s_inodes_count))
935 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
936 fs->super->s_mmp_block > fs->super->s_first_data_block &&
937 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
938 ext2fs_mark_block_bitmap2(ctx->block_found_map,
939 fs->super->s_mmp_block);
942 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
943 if (e2fsck_mmp_update(fs))
946 old_op = ehandler_operation(_("getting next inode from scan"));
947 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
949 ehandler_operation(old_op);
950 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
952 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
953 if (!ctx->inode_bb_map)
955 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
956 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
960 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
961 ctx->flags |= E2F_FLAG_ABORT;
968 ctx->stashed_ino = ino;
969 if (inode->i_links_count) {
970 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
971 ino, inode->i_links_count);
973 pctx.num = inode->i_links_count;
974 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
975 ctx->flags |= E2F_FLAG_ABORT;
981 * Test for incorrect extent flag settings.
983 * On big-endian machines we must be careful:
984 * When the inode is read, the i_block array is not swapped
985 * if the extent flag is set. Therefore if we are testing
986 * for or fixing a wrongly-set flag, we must potentially
987 * (un)swap before testing, or after fixing.
991 * In this case the extents flag was set when read, so
992 * extent_header_verify is ok. If the inode is cleared,
993 * no need to swap... so no extra swapping here.
995 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
996 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
997 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
998 if ((ext2fs_extent_header_verify(inode->i_block,
999 sizeof(inode->i_block)) == 0) &&
1000 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1001 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
1002 ext2fs_mark_super_dirty(fs);
1004 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1006 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1007 if (ino == EXT2_BAD_INO)
1008 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1015 * For big-endian machines:
1016 * If the inode didn't have the extents flag set when it
1017 * was read, then the i_blocks array was swapped. To test
1018 * as an extents header, we must swap it back first.
1019 * IF we then set the extents flag, the entire i_block
1020 * array must be un/re-swapped to make it proper extents data.
1022 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1023 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1024 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1025 (LINUX_S_ISREG(inode->i_mode) ||
1026 LINUX_S_ISDIR(inode->i_mode))) {
1028 #ifdef WORDS_BIGENDIAN
1029 __u32 tmp_block[EXT2_N_BLOCKS];
1031 for (i = 0; i < EXT2_N_BLOCKS; i++)
1032 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1035 ehp = inode->i_block;
1037 if ((ext2fs_extent_header_verify(ehp,
1038 sizeof(inode->i_block)) == 0)) {
1039 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1040 if (fix_problem(ctx, PR_1_UNSET_EXTENT_FL,
1042 inode->i_flags |= EXT4_EXTENTS_FL;
1043 #ifdef WORDS_BIGENDIAN
1044 memcpy(inode->i_block, tmp_block,
1045 sizeof(inode->i_block));
1047 e2fsck_write_inode(ctx, ino, inode,
1053 if (ino == EXT2_BAD_INO) {
1054 struct process_block_struct pb;
1056 if ((inode->i_mode || inode->i_uid || inode->i_gid ||
1057 inode->i_links_count || inode->i_file_acl) &&
1058 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1059 memset(inode, 0, sizeof(struct ext2_inode));
1060 e2fsck_write_inode(ctx, ino, inode,
1064 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1065 &pb.fs_meta_blocks);
1068 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1069 ctx->flags |= E2F_FLAG_ABORT;
1072 pb.ino = EXT2_BAD_INO;
1073 pb.num_blocks = pb.last_block = 0;
1074 pb.last_db_block = -1;
1075 pb.num_illegal_blocks = 0;
1076 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1077 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1081 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1082 block_buf, process_bad_block, &pb);
1083 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1085 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1086 ctx->flags |= E2F_FLAG_ABORT;
1090 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1091 ctx->flags |= E2F_FLAG_ABORT;
1094 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1095 clear_problem_context(&pctx);
1097 } else if (ino == EXT2_ROOT_INO) {
1099 * Make sure the root inode is a directory; if
1100 * not, offer to clear it. It will be
1101 * regnerated in pass #3.
1103 if (!LINUX_S_ISDIR(inode->i_mode)) {
1104 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1108 * If dtime is set, offer to clear it. mke2fs
1109 * version 0.2b created filesystems with the
1110 * dtime field set for the root and lost+found
1111 * directories. We won't worry about
1112 * /lost+found, since that can be regenerated
1113 * easily. But we will fix the root directory
1114 * as a special case.
1116 if (inode->i_dtime && inode->i_links_count) {
1117 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1118 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1120 e2fsck_write_inode(ctx, ino, inode,
1124 } else if (ino == EXT2_JOURNAL_INO) {
1125 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1126 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1127 if (!LINUX_S_ISREG(inode->i_mode) &&
1128 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1130 inode->i_mode = LINUX_S_IFREG;
1131 e2fsck_write_inode(ctx, ino, inode,
1134 check_blocks(ctx, &pctx, block_buf);
1137 if ((inode->i_links_count ||
1138 inode->i_blocks || inode->i_block[0]) &&
1139 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1141 memset(inode, 0, inode_size);
1142 ext2fs_icount_store(ctx->inode_link_info,
1144 e2fsck_write_inode_full(ctx, ino, inode,
1145 inode_size, "pass1");
1147 } else if ((ino == EXT4_USR_QUOTA_INO) ||
1148 (ino == EXT4_GRP_QUOTA_INO)) {
1149 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1150 if ((fs->super->s_feature_ro_compat &
1151 EXT4_FEATURE_RO_COMPAT_QUOTA) &&
1152 ((fs->super->s_usr_quota_inum == ino) ||
1153 (fs->super->s_grp_quota_inum == ino))) {
1154 if (!LINUX_S_ISREG(inode->i_mode) &&
1155 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1157 inode->i_mode = LINUX_S_IFREG;
1158 e2fsck_write_inode(ctx, ino, inode,
1161 check_blocks(ctx, &pctx, block_buf);
1164 if ((inode->i_links_count ||
1165 inode->i_blocks || inode->i_block[0]) &&
1166 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1168 memset(inode, 0, inode_size);
1169 ext2fs_icount_store(ctx->inode_link_info,
1171 e2fsck_write_inode_full(ctx, ino, inode,
1172 inode_size, "pass1");
1174 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1175 problem_t problem = 0;
1177 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1178 if (ino == EXT2_BOOT_LOADER_INO) {
1179 if (LINUX_S_ISDIR(inode->i_mode))
1180 problem = PR_1_RESERVED_BAD_MODE;
1181 } else if (ino == EXT2_RESIZE_INO) {
1182 if (inode->i_mode &&
1183 !LINUX_S_ISREG(inode->i_mode))
1184 problem = PR_1_RESERVED_BAD_MODE;
1186 if (inode->i_mode != 0)
1187 problem = PR_1_RESERVED_BAD_MODE;
1190 if (fix_problem(ctx, problem, &pctx)) {
1192 e2fsck_write_inode(ctx, ino, inode,
1196 check_blocks(ctx, &pctx, block_buf);
1201 * Check for inodes who might have been part of the
1202 * orphaned list linked list. They should have gotten
1203 * dealt with by now, unless the list had somehow been
1206 * FIXME: In the future, inodes which are still in use
1207 * (and which are therefore) pending truncation should
1208 * be handled specially. Right now we just clear the
1209 * dtime field, and the normal e2fsck handling of
1210 * inodes where i_size and the inode blocks are
1211 * inconsistent is to fix i_size, instead of releasing
1212 * the extra blocks. This won't catch the inodes that
1213 * was at the end of the orphan list, but it's better
1214 * than nothing. The right answer is that there
1215 * shouldn't be any bugs in the orphan list handling. :-)
1217 if (inode->i_dtime && low_dtime_check &&
1218 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1219 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1220 inode->i_dtime = inode->i_links_count ?
1222 e2fsck_write_inode(ctx, ino, inode,
1228 * This code assumes that deleted inodes have
1229 * i_links_count set to 0.
1231 if (!inode->i_links_count) {
1232 if (!inode->i_dtime && inode->i_mode) {
1233 if (fix_problem(ctx,
1234 PR_1_ZERO_DTIME, &pctx)) {
1235 inode->i_dtime = ctx->now;
1236 e2fsck_write_inode(ctx, ino, inode,
1243 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1244 * deleted files. Oops.
1246 * Since all new ext2 implementations get this right,
1247 * we now assume that the case of non-zero
1248 * i_links_count and non-zero dtime means that we
1249 * should keep the file, not delete it.
1252 if (inode->i_dtime) {
1253 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1254 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1256 e2fsck_write_inode(ctx, ino, inode, "pass1");
1260 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1261 switch (fs->super->s_creator_os) {
1263 frag = inode->osd2.hurd2.h_i_frag;
1264 fsize = inode->osd2.hurd2.h_i_fsize;
1270 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1271 if (inode->i_faddr || frag || fsize ||
1272 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
1273 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1274 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1275 !(fs->super->s_feature_incompat &
1276 EXT4_FEATURE_INCOMPAT_64BIT) &&
1277 inode->osd2.linux2.l_i_file_acl_high != 0)
1278 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1279 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1280 !(fs->super->s_feature_ro_compat &
1281 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1282 (inode->osd2.linux2.l_i_blocks_hi != 0))
1283 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1284 if (inode->i_flags & EXT2_IMAGIC_FL) {
1286 if (!ctx->inode_imagic_map)
1287 alloc_imagic_map(ctx);
1288 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1291 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1292 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1293 inode->i_flags &= ~EXT2_IMAGIC_FL;
1294 e2fsck_write_inode(ctx, ino,
1300 check_inode_extra_space(ctx, &pctx);
1301 check_is_really_dir(ctx, &pctx, block_buf);
1304 * ext2fs_inode_has_valid_blocks2 does not actually look
1305 * at i_block[] values, so not endian-sensitive here.
1307 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1308 LINUX_S_ISLNK(inode->i_mode) &&
1309 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1310 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1311 inode->i_flags &= ~EXT4_EXTENTS_FL;
1312 e2fsck_write_inode(ctx, ino, inode, "pass1");
1315 if (LINUX_S_ISDIR(inode->i_mode)) {
1316 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1317 e2fsck_add_dir_info(ctx, ino, 0);
1318 ctx->fs_directory_count++;
1319 } else if (LINUX_S_ISREG (inode->i_mode)) {
1320 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1321 ctx->fs_regular_count++;
1322 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1323 e2fsck_pass1_check_device_inode(fs, inode)) {
1324 check_immutable(ctx, &pctx);
1325 check_size(ctx, &pctx);
1326 ctx->fs_chardev_count++;
1327 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1328 e2fsck_pass1_check_device_inode(fs, inode)) {
1329 check_immutable(ctx, &pctx);
1330 check_size(ctx, &pctx);
1331 ctx->fs_blockdev_count++;
1332 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1333 e2fsck_pass1_check_symlink(fs, ino, inode,
1335 check_immutable(ctx, &pctx);
1336 ctx->fs_symlinks_count++;
1337 if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1338 ctx->fs_fast_symlinks_count++;
1339 check_blocks(ctx, &pctx, block_buf);
1343 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1344 e2fsck_pass1_check_device_inode(fs, inode)) {
1345 check_immutable(ctx, &pctx);
1346 check_size(ctx, &pctx);
1347 ctx->fs_fifo_count++;
1348 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1349 e2fsck_pass1_check_device_inode(fs, inode)) {
1350 check_immutable(ctx, &pctx);
1351 check_size(ctx, &pctx);
1352 ctx->fs_sockets_count++;
1354 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1357 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
1358 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1359 else if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime,
1361 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1363 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
1364 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1365 else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
1367 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1369 /* i_crtime is checked in check_inode_extra_space() */
1371 if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
1372 if (inode->i_block[EXT2_IND_BLOCK])
1373 ctx->fs_ind_count++;
1374 if (inode->i_block[EXT2_DIND_BLOCK])
1375 ctx->fs_dind_count++;
1376 if (inode->i_block[EXT2_TIND_BLOCK])
1377 ctx->fs_tind_count++;
1379 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1380 (inode->i_block[EXT2_IND_BLOCK] ||
1381 inode->i_block[EXT2_DIND_BLOCK] ||
1382 inode->i_block[EXT2_TIND_BLOCK] ||
1383 ext2fs_file_acl_block(fs, inode))) {
1384 inodes_to_process[process_inode_count].ino = ino;
1385 inodes_to_process[process_inode_count].inode = *inode;
1386 process_inode_count++;
1388 check_blocks(ctx, &pctx, block_buf);
1390 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
1391 struct ext2_inode_large *inode_l;
1393 inode_l = (struct ext2_inode_large *)inode;
1395 if (inode_l->i_extra_isize < ctx->want_extra_isize) {
1396 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
1397 inode_exp = e2fsck_pass1_expand_eisize(ctx,
1401 if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
1403 ctx->min_extra_isize = inode_l->i_extra_isize;
1406 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1409 if (process_inode_count >= ctx->process_inode_size) {
1410 process_inodes(ctx, block_buf);
1412 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1416 process_inodes(ctx, block_buf);
1417 ext2fs_close_inode_scan(scan);
1420 reserve_block_for_root_repair(ctx);
1421 reserve_block_for_lnf_repair(ctx);
1424 * If any extended attribute blocks' reference counts need to
1425 * be adjusted, either up (ctx->refcount_extra), or down
1426 * (ctx->refcount), then fix them.
1428 if (ctx->refcount) {
1429 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1430 ea_refcount_free(ctx->refcount);
1433 if (ctx->refcount_extra) {
1434 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1436 ea_refcount_free(ctx->refcount_extra);
1437 ctx->refcount_extra = 0;
1440 if (ctx->invalid_bitmaps)
1441 handle_fs_bad_blocks(ctx);
1443 /* We don't need the block_ea_map any more */
1444 if (ctx->block_ea_map) {
1445 ext2fs_free_block_bitmap(ctx->block_ea_map);
1446 ctx->block_ea_map = 0;
1449 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1450 clear_problem_context(&pctx);
1451 pctx.errcode = ext2fs_create_resize_inode(fs);
1453 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1455 ctx->flags |= E2F_FLAG_ABORT;
1460 if (!pctx.errcode) {
1461 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1463 inode->i_mtime = ctx->now;
1464 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1467 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1470 if (ctx->flags & E2F_FLAG_RESTART) {
1472 * Only the master copy of the superblock and block
1473 * group descriptors are going to be written during a
1474 * restart, so set the superblock to be used to be the
1475 * master superblock.
1477 ctx->use_superblock = 0;
1482 if (ctx->block_dup_map) {
1483 if (ctx->options & E2F_OPT_PREEN) {
1484 clear_problem_context(&pctx);
1485 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1487 e2fsck_pass1_dupblocks(ctx, block_buf);
1489 ext2fs_free_mem(&inodes_to_process);
1491 e2fsck_use_inode_shortcuts(ctx, 0);
1494 ext2fs_close_inode_scan(scan);
1496 ext2fs_free_mem(&block_buf);
1498 ext2fs_free_mem(&inode);
1500 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
1501 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1505 * When the inode_scan routines call this callback at the end of the
1506 * glock group, call process_inodes.
1508 static errcode_t scan_callback(ext2_filsys fs,
1509 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1510 dgrp_t group, void * priv_data)
1512 struct scan_callback_struct *scan_struct;
1515 scan_struct = (struct scan_callback_struct *) priv_data;
1516 ctx = scan_struct->ctx;
1518 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1521 if ((ctx->progress)(ctx, 1, group+1,
1522 ctx->fs->group_desc_count))
1523 return EXT2_ET_CANCEL_REQUESTED;
1529 * Process the inodes in the "inodes to process" list.
1531 static void process_inodes(e2fsck_t ctx, char *block_buf)
1534 struct ext2_inode *old_stashed_inode;
1535 ext2_ino_t old_stashed_ino;
1536 const char *old_operation;
1538 struct problem_context pctx;
1541 printf("begin process_inodes: ");
1543 if (process_inode_count == 0)
1545 old_operation = ehandler_operation(0);
1546 old_stashed_inode = ctx->stashed_inode;
1547 old_stashed_ino = ctx->stashed_ino;
1548 qsort(inodes_to_process, process_inode_count,
1549 sizeof(struct process_inode_block), process_inode_cmp);
1550 clear_problem_context(&pctx);
1551 for (i=0; i < process_inode_count; i++) {
1552 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1553 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1556 printf("%u ", pctx.ino);
1558 sprintf(buf, _("reading indirect blocks of inode %u"),
1560 ehandler_operation(buf);
1561 check_blocks(ctx, &pctx, block_buf);
1562 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1565 ctx->stashed_inode = old_stashed_inode;
1566 ctx->stashed_ino = old_stashed_ino;
1567 process_inode_count = 0;
1569 printf("end process inodes\n");
1571 ehandler_operation(old_operation);
1574 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1576 const struct process_inode_block *ib_a =
1577 (const struct process_inode_block *) a;
1578 const struct process_inode_block *ib_b =
1579 (const struct process_inode_block *) b;
1582 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1583 ib_b->inode.i_block[EXT2_IND_BLOCK]);
1586 * We only call process_inodes() for non-extent
1587 * inodes, so it's OK to pass NULL to
1588 * ext2fs_file_acl_block() here.
1590 ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
1591 ext2fs_file_acl_block(0, &(ib_b->inode));
1593 ret = ib_a->ino - ib_b->ino;
1598 * Mark an inode as being bad and increment its badness counter.
1600 void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
1601 const char *func, const int line)
1603 struct problem_context pctx;
1606 if (!ctx->inode_badness) {
1607 clear_problem_context(&pctx);
1609 pctx.errcode = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
1610 &ctx->inode_badness);
1612 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1613 ctx->flags |= E2F_FLAG_ABORT;
1617 ext2fs_icount_fetch(ctx->inode_badness, ino, &result);
1618 ext2fs_icount_store(ctx->inode_badness, ino, count + result);
1620 if (ctx->options & E2F_OPT_DEBUG)
1621 fprintf(stderr, "%s:%d: increase inode %lu badness %u to %u\n",
1622 func, line, (unsigned long)ino, result, count + result);
1627 * This procedure will allocate the inode "bb" (badblock) map table
1629 static void alloc_bb_map(e2fsck_t ctx)
1631 struct problem_context pctx;
1633 clear_problem_context(&pctx);
1634 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1635 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
1636 "inode_bb_map", &ctx->inode_bb_map);
1639 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1640 /* Should never get here */
1641 ctx->flags |= E2F_FLAG_ABORT;
1647 * This procedure will allocate the inode imagic table
1649 static void alloc_imagic_map(e2fsck_t ctx)
1651 struct problem_context pctx;
1653 clear_problem_context(&pctx);
1654 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1655 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
1656 "inode_imagic_map", &ctx->inode_imagic_map);
1659 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1660 /* Should never get here */
1661 ctx->flags |= E2F_FLAG_ABORT;
1667 * Marks a block as in use, setting the dup_map if it's been set
1668 * already. Called by process_block and process_bad_block.
1670 * WARNING: Assumes checks have already been done to make sure block
1671 * is valid. This is true in both process_block and process_bad_block.
1673 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
1675 struct problem_context pctx;
1677 clear_problem_context(&pctx);
1679 if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
1680 if (!ctx->block_dup_map) {
1681 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1682 _("multiply claimed block map"),
1683 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1684 &ctx->block_dup_map);
1687 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1689 /* Should never get here */
1690 ctx->flags |= E2F_FLAG_ABORT;
1694 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
1696 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
1700 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
1703 if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
1704 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
1707 mark_block_used(ctx, block++);
1711 * Adjust the extended attribute block's reference counts at the end
1712 * of pass 1, either by subtracting out references for EA blocks that
1713 * are still referenced in ctx->refcount, or by adding references for
1714 * EA blocks that had extra references as accounted for in
1715 * ctx->refcount_extra.
1717 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1718 char *block_buf, int adjust_sign)
1720 struct ext2_ext_attr_header *header;
1721 struct problem_context pctx;
1722 ext2_filsys fs = ctx->fs;
1727 clear_problem_context(&pctx);
1729 ea_refcount_intr_begin(refcount);
1731 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1734 pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1735 /* We already checked this block, shouldn't happen */
1737 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1740 header = BHDR(block_buf);
1741 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1742 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1746 pctx.blkcount = header->h_refcount;
1747 should_be = header->h_refcount + adjust_sign * count;
1748 pctx.num = should_be;
1749 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1750 header->h_refcount = should_be;
1751 pctx.errcode = ext2fs_write_ext_attr2(fs, blk,
1754 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
1763 * Handle processing the extended attribute blocks
1765 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1768 ext2_filsys fs = ctx->fs;
1769 ext2_ino_t ino = pctx->ino;
1770 struct ext2_inode *inode = pctx->inode;
1773 struct ext2_ext_attr_header *header;
1774 struct ext2_ext_attr_entry *entry;
1776 region_t region = 0;
1778 blk = ext2fs_file_acl_block(fs, inode);
1783 * If the Extended attribute flag isn't set, then a non-zero
1784 * file acl means that the inode is corrupted.
1786 * Or if the extended attribute block is an invalid block,
1787 * then the inode is also corrupted.
1789 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1790 (blk < fs->super->s_first_data_block) ||
1791 (blk >= ext2fs_blocks_count(fs->super))) {
1792 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1793 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1797 /* If ea bitmap hasn't been allocated, create it */
1798 if (!ctx->block_ea_map) {
1799 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
1800 _("ext attr block map"),
1801 EXT2FS_BMAP64_RBTREE, "block_ea_map",
1802 &ctx->block_ea_map);
1803 if (pctx->errcode) {
1805 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1806 ctx->flags |= E2F_FLAG_ABORT;
1811 /* Create the EA refcount structure if necessary */
1812 if (!ctx->refcount) {
1813 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1814 if (pctx->errcode) {
1816 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1817 ctx->flags |= E2F_FLAG_ABORT;
1823 /* Debugging text */
1824 printf("Inode %u has EA block %u\n", ino, blk);
1827 /* Have we seen this EA block before? */
1828 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
1829 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1831 /* Ooops, this EA was referenced more than it stated */
1832 if (!ctx->refcount_extra) {
1833 pctx->errcode = ea_refcount_create(0,
1834 &ctx->refcount_extra);
1835 if (pctx->errcode) {
1837 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1838 ctx->flags |= E2F_FLAG_ABORT;
1842 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1847 * OK, we haven't seen this EA block yet. So we need to
1851 pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1852 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1854 header = BHDR(block_buf);
1855 pctx->blk = ext2fs_file_acl_block(fs, inode);
1856 if (((ctx->ext_attr_ver == 1) &&
1857 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1858 ((ctx->ext_attr_ver == 2) &&
1859 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1860 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1864 if (header->h_blocks != 1) {
1865 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1869 region = region_create(0, fs->blocksize);
1871 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
1872 ctx->flags |= E2F_FLAG_ABORT;
1875 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
1876 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1880 entry = (struct ext2_ext_attr_entry *)(header+1);
1881 end = block_buf + fs->blocksize;
1882 while ((char *)entry < end && *(__u32 *)entry) {
1885 if (region_allocate(region, (char *)entry - (char *)header,
1886 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
1887 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1891 if ((ctx->ext_attr_ver == 1 &&
1892 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
1893 (ctx->ext_attr_ver == 2 &&
1894 entry->e_name_index == 0)) {
1895 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
1899 if (entry->e_value_block != 0) {
1900 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1903 if (entry->e_value_offs + entry->e_value_size > fs->blocksize) {
1904 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
1908 if (entry->e_value_size &&
1909 region_allocate(region, entry->e_value_offs,
1910 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
1911 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1915 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
1916 entry->e_value_offs);
1918 if (entry->e_hash != hash) {
1919 pctx->num = entry->e_hash;
1920 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
1922 entry->e_hash = hash;
1925 entry = EXT2_EXT_ATTR_NEXT(entry);
1927 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
1928 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
1931 region_free(region);
1933 count = header->h_refcount - 1;
1935 ea_refcount_store(ctx->refcount, blk, count);
1936 mark_block_used(ctx, blk);
1937 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
1942 region_free(region);
1943 ext2fs_file_acl_block_set(fs, inode, 0);
1944 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
1948 /* Returns 1 if bad htree, 0 if OK */
1949 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
1950 ext2_ino_t ino, struct ext2_inode *inode,
1953 struct ext2_dx_root_info *root;
1954 ext2_filsys fs = ctx->fs;
1958 if ((!LINUX_S_ISDIR(inode->i_mode) &&
1959 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
1960 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX))) {
1961 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1962 if (fix_problem(ctx, PR_1_HTREE_SET, pctx))
1966 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
1968 if ((pctx->errcode) ||
1970 (blk < fs->super->s_first_data_block) ||
1971 (blk >= ext2fs_blocks_count(fs->super))) {
1972 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1973 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1979 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
1981 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1982 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1986 /* XXX should check that beginning matches a directory */
1987 root = (struct ext2_dx_root_info *) (block_buf + 24);
1989 if ((root->reserved_zero || root->info_length < 8) &&
1990 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
1993 pctx->num = root->hash_version;
1994 if ((root->hash_version != EXT2_HASH_LEGACY) &&
1995 (root->hash_version != EXT2_HASH_HALF_MD4) &&
1996 (root->hash_version != EXT2_HASH_TEA) &&
1997 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2000 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2001 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2004 pctx->num = root->indirect_levels;
2005 if ((root->indirect_levels > 1) &&
2006 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2012 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2013 struct ext2_inode *inode, int restart_flag,
2017 inode->i_links_count = 0;
2018 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2019 inode->i_dtime = ctx->now;
2021 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2022 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2023 if (ctx->inode_reg_map)
2024 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2025 if (ctx->inode_badness)
2026 ext2fs_icount_store(ctx->inode_badness, ino, 0);
2029 * If the inode was partially accounted for before processing
2030 * was aborted, we need to restart the pass 1 scan.
2032 ctx->flags |= restart_flag;
2034 if (ino == EXT2_BAD_INO)
2035 memset(inode, 0, sizeof(struct ext2_inode));
2037 e2fsck_write_inode(ctx, ino, inode, source);
2041 * Use the multiple-blocks reclamation code to fix alignment problems in
2042 * a bigalloc filesystem. We want a logical cluster to map to *only* one
2043 * physical cluster, and we want the block offsets within that cluster to
2046 static int has_unaligned_cluster_map(e2fsck_t ctx,
2047 blk64_t last_pblk, e2_blkcnt_t last_lblk,
2048 blk64_t pblk, blk64_t lblk)
2050 blk64_t cluster_mask;
2052 if (!ctx->fs->cluster_ratio_bits)
2054 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
2057 * If the block in the logical cluster doesn't align with the block in
2058 * the physical cluster...
2060 if ((lblk & cluster_mask) != (pblk & cluster_mask))
2064 * If we cross a physical cluster boundary within a logical cluster...
2066 if (last_pblk && (lblk & cluster_mask) != 0 &&
2067 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
2068 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
2074 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2075 struct process_block_struct *pb,
2076 blk64_t start_block, blk64_t end_block,
2078 ext2_extent_handle_t ehandle)
2080 struct ext2fs_extent extent;
2081 blk64_t blk, last_lblk;
2082 e2_blkcnt_t blockcnt;
2084 int is_dir, is_leaf;
2086 struct ext2_extent_info info;
2088 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2092 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2094 while (!pctx->errcode && info.num_entries-- > 0) {
2095 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2096 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2097 last_lblk = extent.e_lblk + extent.e_len - 1;
2100 if (extent.e_pblk == 0 ||
2101 extent.e_pblk < ctx->fs->super->s_first_data_block ||
2102 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2103 problem = PR_1_EXTENT_BAD_START_BLK;
2104 else if (extent.e_lblk < start_block)
2105 problem = PR_1_OUT_OF_ORDER_EXTENTS;
2106 else if ((end_block && last_lblk > end_block) &&
2107 (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
2108 last_lblk > eof_block)))
2109 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
2110 else if (is_leaf && extent.e_len == 0)
2111 problem = PR_1_EXTENT_LENGTH_ZERO;
2113 (extent.e_pblk + extent.e_len) >
2114 ext2fs_blocks_count(ctx->fs->super))
2115 problem = PR_1_EXTENT_ENDS_BEYOND;
2116 else if (is_leaf && is_dir &&
2117 ((extent.e_lblk + extent.e_len) >
2118 (1 << (21 - ctx->fs->super->s_log_block_size))))
2119 problem = PR_1_TOOBIG_DIR;
2122 * Uninitialized blocks in a directory? Clear the flag and
2123 * we'll interpret the blocks later.
2125 if (is_dir && problem == 0 &&
2126 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2127 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
2128 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2129 pb->inode_modified = 1;
2130 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2137 /* To ensure that extent is in inode */
2138 if (info.curr_level == 0)
2139 e2fsck_mark_inode_bad(ctx, pctx->ino,
2142 pctx->blk = extent.e_pblk;
2143 pctx->blk2 = extent.e_lblk;
2144 pctx->num = extent.e_len;
2145 pctx->blkcount = extent.e_lblk + extent.e_len;
2146 if (fix_problem(ctx, problem, pctx)) {
2147 if (ctx->invalid_bitmaps) {
2149 * If fsck knows the bitmaps are bad,
2150 * skip to the next extent and
2151 * try to clear this extent again
2152 * after fixing the bitmaps, by
2155 pctx->errcode = ext2fs_extent_get(
2157 EXT2_EXTENT_NEXT_SIB,
2159 ctx->flags |= E2F_FLAG_RESTART_LATER;
2160 if (pctx->errcode ==
2161 EXT2_ET_NO_CURRENT_NODE) {
2167 e2fsck_read_bitmaps(ctx);
2168 pb->inode_modified = 1;
2170 ext2fs_extent_delete(ehandle, 0);
2171 if (pctx->errcode) {
2172 pctx->str = "ext2fs_extent_delete";
2175 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2176 if (pctx->errcode &&
2177 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
2178 pctx->str = "ext2fs_extent_fix_parents";
2181 pctx->errcode = ext2fs_extent_get(ehandle,
2182 EXT2_EXTENT_CURRENT,
2184 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2194 blk64_t lblk = extent.e_lblk;
2196 blk = extent.e_pblk;
2197 pctx->errcode = ext2fs_extent_get(ehandle,
2198 EXT2_EXTENT_DOWN, &extent);
2199 if (pctx->errcode) {
2200 pctx->str = "EXT2_EXTENT_DOWN";
2201 problem = PR_1_EXTENT_HEADER_INVALID;
2202 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
2203 goto report_problem;
2206 /* The next extent should match this index's logical start */
2207 if (extent.e_lblk != lblk) {
2208 struct ext2_extent_info e_info;
2210 ext2fs_extent_get_info(ehandle, &e_info);
2212 pctx->blk2 = extent.e_lblk;
2213 pctx->num = e_info.curr_level - 1;
2214 problem = PR_1_EXTENT_INDEX_START_INVALID;
2215 if (fix_problem(ctx, problem, pctx)) {
2216 pb->inode_modified = 1;
2218 ext2fs_extent_fix_parents(ehandle);
2219 if (pctx->errcode) {
2220 pctx->str = "ext2fs_extent_fix_parents";
2225 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
2226 last_lblk, eof_block, ehandle);
2229 pctx->errcode = ext2fs_extent_get(ehandle,
2230 EXT2_EXTENT_UP, &extent);
2231 if (pctx->errcode) {
2232 pctx->str = "EXT2_EXTENT_UP";
2235 mark_block_used(ctx, blk);
2240 if ((pb->previous_block != 0) &&
2241 (pb->previous_block+1 != extent.e_pblk)) {
2242 if (ctx->options & E2F_OPT_FRAGCHECK) {
2247 else if (pb->is_reg)
2250 printf(("%6lu(%c): expecting %6lu "
2252 "phys %6lu log %lu len %lu\n"),
2253 (unsigned long) pctx->ino, type,
2254 (unsigned long) pb->previous_block+1,
2255 (unsigned long) extent.e_pblk,
2256 (unsigned long) extent.e_lblk,
2257 (unsigned long) extent.e_len);
2262 * If we notice a gap in the logical block mappings of an
2263 * extent-mapped directory, offer to close the hole by
2264 * moving the logical block down, otherwise we'll go mad in
2265 * pass 3 allocating empty directory blocks to fill the hole.
2268 pb->last_block + 1 < (e2_blkcnt_t)extent.e_lblk) {
2271 new_lblk = pb->last_block + 1;
2272 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
2273 new_lblk = ((new_lblk +
2274 EXT2FS_CLUSTER_RATIO(ctx->fs)) &
2275 EXT2FS_CLUSTER_MASK(ctx->fs)) |
2277 EXT2FS_CLUSTER_MASK(ctx->fs));
2278 pctx->blk = extent.e_lblk;
2279 pctx->blk2 = new_lblk;
2280 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
2281 extent.e_lblk = new_lblk;
2282 pb->inode_modified = 1;
2283 pctx->errcode = ext2fs_extent_replace(ehandle,
2285 if (pctx->errcode) {
2289 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2291 goto failed_add_dir_block;
2292 pctx->errcode = ext2fs_extent_goto(ehandle,
2295 goto failed_add_dir_block;
2296 last_lblk = extent.e_lblk + extent.e_len - 1;
2300 while (is_dir && (++pb->last_db_block <
2301 (e2_blkcnt_t) extent.e_lblk)) {
2302 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
2305 if (pctx->errcode) {
2307 pctx->num = pb->last_db_block;
2308 goto failed_add_dir_block;
2311 if (!ctx->fs->cluster_ratio_bits) {
2312 mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
2313 pb->num_blocks += extent.e_len;
2315 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
2317 blk++, blockcnt++, i++) {
2318 if (ctx->fs->cluster_ratio_bits &&
2319 !(pb->previous_block &&
2320 (EXT2FS_B2C(ctx->fs, blk) ==
2321 EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
2322 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2323 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2324 mark_block_used(ctx, blk);
2327 if (has_unaligned_cluster_map(ctx, pb->previous_block,
2328 pb->last_block, blk,
2330 pctx->blk = blockcnt;
2332 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2333 mark_block_used(ctx, blk);
2334 mark_block_used(ctx, blk);
2336 pb->last_block = blockcnt;
2337 pb->previous_block = blk;
2340 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
2341 if (pctx->errcode) {
2343 pctx->num = blockcnt;
2344 failed_add_dir_block:
2345 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2346 /* Should never get here */
2347 ctx->flags |= E2F_FLAG_ABORT;
2352 if (is_dir && extent.e_len > 0)
2353 pb->last_db_block = blockcnt - 1;
2354 pb->previous_block = extent.e_pblk + extent.e_len - 1;
2355 start_block = pb->last_block = last_lblk;
2356 if (is_leaf && !is_dir &&
2357 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
2358 pb->last_init_lblock = last_lblk;
2360 pctx->errcode = ext2fs_extent_get(ehandle,
2361 EXT2_EXTENT_NEXT_SIB,
2364 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
2368 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
2369 struct process_block_struct *pb)
2371 struct ext2_extent_info info;
2372 struct ext2_inode *inode = pctx->inode;
2373 ext2_extent_handle_t ehandle;
2374 ext2_filsys fs = ctx->fs;
2375 ext2_ino_t ino = pctx->ino;
2379 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
2380 if (pctx->errcode) {
2381 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
2382 e2fsck_clear_inode(ctx, ino, inode, 0,
2383 "check_blocks_extents");
2388 retval = ext2fs_extent_get_info(ehandle, &info);
2390 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
2391 info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
2392 ctx->extent_depth_count[info.max_depth]++;
2395 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
2396 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
2397 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle);
2398 if (pctx->errcode &&
2399 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
2401 inode->i_blocks = 0;
2402 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2403 "check_blocks_extents");
2406 ext2fs_extent_free(ehandle);
2410 * This subroutine is called on each inode to account for all of the
2411 * blocks used by that inode.
2413 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
2416 ext2_filsys fs = ctx->fs;
2417 struct process_block_struct pb;
2418 ext2_ino_t ino = pctx->ino;
2419 struct ext2_inode *inode = pctx->inode;
2420 unsigned bad_size = 0;
2421 int dirty_inode = 0;
2428 pb.last_init_lblock = -1;
2429 pb.last_db_block = -1;
2430 pb.num_illegal_blocks = 0;
2431 pb.suppress = 0; pb.clear = 0;
2434 pb.previous_block = 0;
2435 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2436 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
2437 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
2441 pb.inode_modified = 0;
2445 extent_fs = (ctx->fs->super->s_feature_incompat &
2446 EXT3_FEATURE_INCOMPAT_EXTENTS);
2448 if (inode->i_flags & EXT2_COMPRBLK_FL) {
2449 if (fs->super->s_feature_incompat &
2450 EXT2_FEATURE_INCOMPAT_COMPRESSION)
2453 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2454 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
2455 inode->i_flags &= ~EXT2_COMPRBLK_FL;
2461 if (ext2fs_file_acl_block(fs, inode) &&
2462 check_ext_attr(ctx, pctx, block_buf)) {
2463 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2468 if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
2469 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
2470 check_blocks_extents(ctx, pctx, &pb);
2473 * If we've modified the inode, write it out before
2474 * iterate() tries to use it.
2477 e2fsck_write_inode(ctx, ino, inode,
2481 pctx->errcode = ext2fs_block_iterate3(fs, ino,
2482 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
2483 block_buf, process_block, &pb);
2485 * We do not have uninitialized extents in non extent
2488 pb.last_init_lblock = pb.last_block;
2490 * If iterate() changed a block mapping, we have to
2491 * re-read the inode. If we decide to clear the
2492 * inode after clearing some stuff, we'll re-write the
2493 * bad mappings into the inode!
2495 if (pb.inode_modified)
2496 e2fsck_read_inode(ctx, ino, inode,
2500 end_problem_latch(ctx, PR_LATCH_BLOCK);
2501 end_problem_latch(ctx, PR_LATCH_TOOBIG);
2502 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2505 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
2507 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
2508 if (LINUX_S_ISDIR(inode->i_mode))
2509 ctx->fs_fragmented_dir++;
2511 ctx->fs_fragmented++;
2515 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2520 if (inode->i_flags & EXT2_INDEX_FL) {
2521 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
2522 inode->i_flags &= ~EXT2_INDEX_FL;
2526 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
2531 if (!pb.num_blocks && pb.is_dir) {
2533 * The mode might be in-correct. Increasing the badness by
2534 * small amount won't hurt much.
2536 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2537 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
2538 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
2539 ctx->fs_directory_count--;
2544 if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
2545 quota_data_add(ctx->qctx, inode, ino,
2546 pb.num_blocks * fs->blocksize);
2547 quota_data_inodes(ctx->qctx, inode, ino, +1);
2550 if (!(fs->super->s_feature_ro_compat &
2551 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
2552 !(inode->i_flags & EXT4_HUGE_FILE_FL))
2553 pb.num_blocks *= (fs->blocksize / 512);
2554 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
2556 printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
2557 ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
2561 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
2562 if (inode->i_size & (fs->blocksize - 1))
2564 else if (nblock > (pb.last_block + 1))
2566 else if (nblock < (pb.last_block + 1)) {
2567 if (((pb.last_block + 1) - nblock) >
2568 fs->super->s_prealloc_dir_blocks)
2572 e2_blkcnt_t blkpg = ctx->blocks_per_page;
2574 size = EXT2_I_SIZE(inode);
2575 if ((pb.last_init_lblock >= 0) &&
2576 /* if size is smaller than expected by the block count,
2577 * allow allocated blocks to end of PAGE_SIZE.
2578 * last_init_lblock is the last in-use block, so it is
2579 * the minimum expected file size. */
2580 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
2581 ((pb.last_init_lblock + 1) / blkpg * blkpg !=
2582 (pb.last_init_lblock + 1) ||
2583 size < (__u64)(pb.last_init_lblock & ~(blkpg - 1)) *
2586 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2587 size > ext2_max_sizes[fs->super->s_log_block_size])
2588 /* too big for a direct/indirect-mapped file */
2590 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2592 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
2593 /* too big for an extent-based file - 32bit ee_block */
2596 /* i_size for symlinks is checked elsewhere */
2597 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
2598 pctx->num = (pb.last_block+1) * fs->blocksize;
2599 pctx->group = bad_size;
2600 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2601 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
2602 if (LINUX_S_ISDIR(inode->i_mode))
2603 pctx->num &= 0xFFFFFFFFULL;
2604 ext2fs_inode_size_set(fs, inode, pctx->num);
2609 if (LINUX_S_ISREG(inode->i_mode) &&
2610 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
2612 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
2613 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
2614 ((fs->super->s_feature_ro_compat &
2615 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
2616 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
2617 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
2618 pctx->num = pb.num_blocks;
2619 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2620 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
2621 inode->i_blocks = pb.num_blocks;
2622 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
2628 if (ctx->dirs_to_hash && pb.is_dir &&
2629 !(inode->i_flags & EXT2_INDEX_FL) &&
2630 ((inode->i_size / fs->blocksize) >= 3))
2631 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
2635 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
2640 * Helper function called by process block when an illegal block is
2641 * found. It returns a description about why the block is illegal
2643 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
2647 static char problem[80];
2649 super = fs->super->s_first_data_block;
2650 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
2651 if (block < super) {
2652 sprintf(problem, "< FIRSTBLOCK (%u)", super);
2654 } else if (block >= ext2fs_blocks_count(fs->super)) {
2655 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
2658 for (i = 0; i < fs->group_desc_count; i++) {
2659 if (block == super) {
2660 sprintf(problem, "is the superblock in group %d", i);
2663 if (block > super &&
2664 block <= (super + fs->desc_blocks)) {
2665 sprintf(problem, "is in the group descriptors "
2669 if (block == ext2fs_block_bitmap_loc(fs, i)) {
2670 sprintf(problem, "is the block bitmap of group %d", i);
2673 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
2674 sprintf(problem, "is the inode bitmap of group %d", i);
2677 if (block >= ext2fs_inode_table_loc(fs, i) &&
2678 (block < ext2fs_inode_table_loc(fs, i)
2679 + fs->inode_blocks_per_group)) {
2680 sprintf(problem, "is in the inode table of group %d",
2684 super += fs->super->s_blocks_per_group;
2691 * This is a helper function for check_blocks().
2693 static int process_block(ext2_filsys fs,
2695 e2_blkcnt_t blockcnt,
2696 blk64_t ref_block EXT2FS_ATTR((unused)),
2697 int ref_offset EXT2FS_ATTR((unused)),
2700 struct process_block_struct *p;
2701 struct problem_context *pctx;
2702 blk64_t blk = *block_nr;
2704 problem_t problem = 0;
2707 p = (struct process_block_struct *) priv_data;
2711 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
2712 /* todo: Check that the comprblk_fl is high, that the
2713 blkaddr pattern looks right (all non-holes up to
2714 first EXT2FS_COMPRESSED_BLKADDR, then all
2715 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2716 that the feature_incompat bit is high, and that the
2717 inode is a regular file. If we're doing a "full
2718 check" (a concept introduced to e2fsck by e2compr,
2719 meaning that we look at data blocks as well as
2720 metadata) then call some library routine that
2721 checks the compressed data. I'll have to think
2722 about this, because one particularly important
2723 problem to be able to fix is to recalculate the
2724 cluster size if necessary. I think that perhaps
2725 we'd better do most/all e2compr-specific checks
2726 separately, after the non-e2compr checks. If not
2727 doing a full check, it may be useful to test that
2728 the personality is linux; e.g. if it isn't then
2729 perhaps this really is just an illegal block. */
2734 * For a directory, add logical block zero for processing even if it's
2735 * not mapped or we'll be perennially stuck with broken "." and ".."
2738 if (p->is_dir && blockcnt == 0 && blk == 0) {
2739 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
2740 if (pctx->errcode) {
2742 pctx->num = blockcnt;
2743 goto failed_add_dir_block;
2752 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
2757 * Simplistic fragmentation check. We merely require that the
2758 * file be contiguous. (Which can never be true for really
2759 * big files that are greater than a block group.)
2761 if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
2762 if (p->previous_block+1 != blk) {
2763 if (ctx->options & E2F_OPT_FRAGCHECK) {
2771 printf(_("%6lu(%c): expecting %6lu "
2772 "got phys %6lu (blkcnt %lld)\n"),
2773 (unsigned long) pctx->ino, type,
2774 (unsigned long) p->previous_block+1,
2775 (unsigned long) blk,
2782 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
2783 problem = PR_1_TOOBIG_DIR;
2784 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
2785 problem = PR_1_TOOBIG_REG;
2786 if (!p->is_dir && !p->is_reg && blockcnt > 0)
2787 problem = PR_1_TOOBIG_SYMLINK;
2789 if (blk < fs->super->s_first_data_block ||
2790 blk >= ext2fs_blocks_count(fs->super)) {
2791 problem = PR_1_ILLEGAL_BLOCK_NUM;
2792 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
2796 p->num_illegal_blocks++;
2797 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
2798 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
2802 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
2804 set_latch_flags(PR_LATCH_BLOCK,
2809 pctx->blkcount = blockcnt;
2810 if (fix_problem(ctx, problem, pctx)) {
2811 blk = *block_nr = 0;
2812 ret_code = BLOCK_CHANGED;
2813 p->inode_modified = 1;
2815 * If the directory block is too big and is beyond the
2816 * end of the FS, don't bother trying to add it for
2817 * processing -- the kernel would never have created a
2818 * directory this large, and we risk an ENOMEM abort.
2819 * In any case, the toobig handler for extent-based
2820 * directories also doesn't feed toobig blocks to
2823 if (problem == PR_1_TOOBIG_DIR)
2830 if (p->ino == EXT2_RESIZE_INO) {
2832 * The resize inode has already be sanity checked
2833 * during pass #0 (the superblock checks). All we
2834 * have to do is mark the double indirect block as
2835 * being in use; all of the other blocks are handled
2836 * by mark_table_blocks()).
2838 if (blockcnt == BLOCK_COUNT_DIND)
2839 mark_block_used(ctx, blk);
2841 } else if (!(ctx->fs->cluster_ratio_bits &&
2842 p->previous_block &&
2843 (EXT2FS_B2C(ctx->fs, blk) ==
2844 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
2845 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2846 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2847 mark_block_used(ctx, blk);
2849 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
2850 p->last_block, blk, blockcnt)) {
2851 pctx->blk = blockcnt;
2853 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2854 mark_block_used(ctx, blk);
2855 mark_block_used(ctx, blk);
2858 p->last_block = blockcnt;
2859 p->previous_block = blk;
2861 if (p->is_dir && (blockcnt >= 0)) {
2862 while (++p->last_db_block < blockcnt) {
2863 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
2866 if (pctx->errcode) {
2868 pctx->num = p->last_db_block;
2869 goto failed_add_dir_block;
2872 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
2874 if (pctx->errcode) {
2876 pctx->num = blockcnt;
2877 failed_add_dir_block:
2878 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2879 /* Should never get here */
2880 ctx->flags |= E2F_FLAG_ABORT;
2887 static int process_bad_block(ext2_filsys fs,
2889 e2_blkcnt_t blockcnt,
2890 blk64_t ref_block EXT2FS_ATTR((unused)),
2891 int ref_offset EXT2FS_ATTR((unused)),
2894 struct process_block_struct *p;
2895 blk64_t blk = *block_nr;
2896 blk64_t first_block;
2898 struct problem_context *pctx;
2902 * Note: This function processes blocks for the bad blocks
2903 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
2909 p = (struct process_block_struct *) priv_data;
2913 pctx->ino = EXT2_BAD_INO;
2915 pctx->blkcount = blockcnt;
2917 if ((blk < fs->super->s_first_data_block) ||
2918 (blk >= ext2fs_blocks_count(fs->super))) {
2919 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
2921 return BLOCK_CHANGED;
2927 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
2929 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
2931 return BLOCK_CHANGED;
2933 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
2936 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
2939 return BLOCK_CHANGED;
2941 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2944 mark_block_used(ctx, blk);
2948 printf ("DEBUG: Marking %u as bad.\n", blk);
2950 ctx->fs_badblocks_count++;
2952 * If the block is not used, then mark it as used and return.
2953 * If it is already marked as found, this must mean that
2954 * there's an overlap between the filesystem table blocks
2955 * (bitmaps and inode table) and the bad block list.
2957 if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
2958 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
2962 * Try to find the where the filesystem block was used...
2964 first_block = fs->super->s_first_data_block;
2966 for (i = 0; i < fs->group_desc_count; i++ ) {
2969 if (!ext2fs_bg_has_super(fs, i))
2971 if (blk == first_block) {
2973 if (fix_problem(ctx,
2974 PR_1_BAD_PRIMARY_SUPERBLOCK,
2977 return BLOCK_CHANGED;
2981 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
2984 if ((blk > first_block) &&
2985 (blk <= first_block + fs->desc_blocks)) {
2987 pctx->blk = *block_nr;
2988 if (fix_problem(ctx,
2989 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
2991 return BLOCK_CHANGED;
2995 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
2999 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
3000 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
3001 ctx->invalid_block_bitmap_flag[i]++;
3002 ctx->invalid_bitmaps++;
3006 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
3007 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
3008 ctx->invalid_inode_bitmap_flag[i]++;
3009 ctx->invalid_bitmaps++;
3013 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
3014 (blk < (ext2fs_inode_table_loc(fs, i) +
3015 fs->inode_blocks_per_group))) {
3017 * If there are bad blocks in the inode table,
3018 * the inode scan code will try to do
3019 * something reasonable automatically.
3023 first_block += fs->super->s_blocks_per_group;
3026 * If we've gotten to this point, then the only
3027 * possibility is that the bad block inode meta data
3028 * is using a bad block.
3030 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
3031 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
3032 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
3034 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
3036 return BLOCK_CHANGED;
3038 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3045 /* Warn user that the block wasn't claimed */
3046 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
3051 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
3052 const char *name, int num, blk64_t *new_block)
3054 ext2_filsys fs = ctx->fs;
3056 blk64_t old_block = *new_block;
3059 unsigned flexbg_size;
3062 struct problem_context pctx;
3064 clear_problem_context(&pctx);
3067 pctx.blk = old_block;
3071 * For flex_bg filesystems, first try to allocate the metadata
3072 * within the flex_bg, and if that fails then try finding the
3073 * space anywhere in the filesystem.
3075 is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
3076 EXT4_FEATURE_INCOMPAT_FLEX_BG);
3078 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3079 flexbg = group / flexbg_size;
3080 first_block = ext2fs_group_first_block2(fs,
3081 flexbg_size * flexbg);
3082 last_grp = group | (flexbg_size - 1);
3083 if (last_grp >= fs->group_desc_count)
3084 last_grp = fs->group_desc_count - 1;
3085 last_block = ext2fs_group_last_block2(fs, last_grp);
3087 last_block = ext2fs_group_last_block2(fs, group);
3088 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
3089 num, ctx->block_found_map,
3091 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
3092 pctx.errcode = ext2fs_get_free_blocks2(fs,
3093 fs->super->s_first_data_block,
3094 ext2fs_blocks_count(fs->super),
3095 num, ctx->block_found_map, new_block);
3098 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
3099 ext2fs_unmark_valid(fs);
3100 ctx->flags |= E2F_FLAG_ABORT;
3103 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
3105 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
3106 ext2fs_unmark_valid(fs);
3107 ctx->flags |= E2F_FLAG_ABORT;
3110 ext2fs_mark_super_dirty(fs);
3111 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3112 pctx.blk2 = *new_block;
3113 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
3114 PR_1_RELOC_TO), &pctx);
3116 for (i = 0; i < num; i++) {
3118 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
3120 pctx.errcode = io_channel_read_blk64(fs->io,
3121 old_block + i, 1, buf);
3123 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
3125 memset(buf, 0, fs->blocksize);
3127 pctx.blk = (*new_block) + i;
3128 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
3131 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
3133 ext2fs_free_mem(&buf);
3137 * This routine gets called at the end of pass 1 if bad blocks are
3138 * detected in the superblock, group descriptors, inode_bitmaps, or
3139 * block bitmaps. At this point, all of the blocks have been mapped
3140 * out, so we can try to allocate new block(s) to replace the bad
3143 static void handle_fs_bad_blocks(e2fsck_t ctx)
3145 ext2_filsys fs = ctx->fs;
3147 blk64_t first_block;
3150 for (i = 0; i < fs->group_desc_count; i++) {
3151 first_block = ext2fs_group_first_block2(fs, i);
3153 if (ctx->invalid_block_bitmap_flag[i]) {
3154 new_blk = ext2fs_block_bitmap_loc(fs, i);
3155 new_table_block(ctx, first_block, i, _("block bitmap"),
3157 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
3159 if (ctx->invalid_inode_bitmap_flag[i]) {
3160 new_blk = ext2fs_inode_bitmap_loc(fs, i);
3161 new_table_block(ctx, first_block, i, _("inode bitmap"),
3163 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
3165 if (ctx->invalid_inode_table_flag[i]) {
3166 new_blk = ext2fs_inode_table_loc(fs, i);
3167 new_table_block(ctx, first_block, i, _("inode table"),
3168 fs->inode_blocks_per_group,
3170 ext2fs_inode_table_loc_set(fs, i, new_blk);
3171 ctx->flags |= E2F_FLAG_RESTART;
3174 ctx->invalid_bitmaps = 0;
3178 * This routine marks all blocks which are used by the superblock,
3179 * group descriptors, inode bitmaps, and block bitmaps.
3181 static void mark_table_blocks(e2fsck_t ctx)
3183 ext2_filsys fs = ctx->fs;
3187 struct problem_context pctx;
3189 clear_problem_context(&pctx);
3191 for (i = 0; i < fs->group_desc_count; i++) {
3194 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
3197 * Mark the blocks used for the inode table
3199 if (ext2fs_inode_table_loc(fs, i)) {
3200 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
3201 j < fs->inode_blocks_per_group;
3203 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3206 if (!ctx->invalid_inode_table_flag[i] &&
3208 PR_1_ITABLE_CONFLICT, &pctx)) {
3209 ctx->invalid_inode_table_flag[i]++;
3210 ctx->invalid_bitmaps++;
3213 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3220 * Mark block used for the block bitmap
3222 if (ext2fs_block_bitmap_loc(fs, i)) {
3223 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3224 ext2fs_block_bitmap_loc(fs, i))) {
3225 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
3226 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
3227 ctx->invalid_block_bitmap_flag[i]++;
3228 ctx->invalid_bitmaps++;
3231 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3232 ext2fs_block_bitmap_loc(fs, i));
3237 * Mark block used for the inode bitmap
3239 if (ext2fs_inode_bitmap_loc(fs, i)) {
3240 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3241 ext2fs_inode_bitmap_loc(fs, i))) {
3242 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
3243 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
3244 ctx->invalid_inode_bitmap_flag[i]++;
3245 ctx->invalid_bitmaps++;
3248 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3249 ext2fs_inode_bitmap_loc(fs, i));
3256 * Thes subroutines short circuits ext2fs_get_blocks and
3257 * ext2fs_check_directory; we use them since we already have the inode
3258 * structure, so there's no point in letting the ext2fs library read
3261 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
3264 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3267 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3268 return EXT2_ET_CALLBACK_NOTHANDLED;
3270 for (i=0; i < EXT2_N_BLOCKS; i++)
3271 blocks[i] = ctx->stashed_inode->i_block[i];
3275 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
3276 struct ext2_inode *inode)
3278 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3280 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3281 return EXT2_ET_CALLBACK_NOTHANDLED;
3282 *inode = *ctx->stashed_inode;
3286 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
3287 struct ext2_inode *inode)
3289 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3291 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
3292 (inode != ctx->stashed_inode))
3293 *ctx->stashed_inode = *inode;
3294 return EXT2_ET_CALLBACK_NOTHANDLED;
3297 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
3299 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3301 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3302 return EXT2_ET_CALLBACK_NOTHANDLED;
3304 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
3305 return EXT2_ET_NO_DIRECTORY;
3309 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
3312 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3316 if (ctx->block_found_map) {
3317 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
3321 if (fs->block_map) {
3322 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
3323 ext2fs_mark_bb_dirty(fs);
3326 if (!fs->block_map) {
3327 retval = ext2fs_read_block_bitmap(fs);
3332 retval = ext2fs_new_block2(fs, goal, 0, &new_block);
3341 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
3343 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3345 if (ctx->block_found_map) {
3347 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3349 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
3353 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
3355 ext2_filsys fs = ctx->fs;
3357 if (use_shortcuts) {
3358 fs->get_blocks = pass1_get_blocks;
3359 fs->check_directory = pass1_check_directory;
3360 fs->read_inode = pass1_read_inode;
3361 fs->write_inode = pass1_write_inode;
3362 ctx->stashed_ino = 0;
3365 fs->check_directory = 0;
3367 fs->write_inode = 0;
3371 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
3373 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
3374 ext2fs_set_block_alloc_stats_callback(ctx->fs,
3375 e2fsck_block_alloc_stats, 0);