2 * pass1.c -- pass #1 of e2fsck: sequential scan of the inode table
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
11 * Pass 1 of e2fsck iterates over all the inodes in the filesystems,
12 * and applies the following tests to each inode:
14 * - The mode field of the inode must be legal.
15 * - The size and block count fields of the inode are correct.
16 * - A data block must not be used by another inode
18 * Pass 1 also gathers the collects the following information:
20 * - A bitmap of which inodes are in use. (inode_used_map)
21 * - A bitmap of which inodes are directories. (inode_dir_map)
22 * - A bitmap of which inodes are regular files. (inode_reg_map)
23 * - An icount mechanism is used to keep track of
24 * inodes with bad fields and its badness (ctx->inode_badness)
25 * - A bitmap of which inodes are in bad blocks. (inode_bb_map)
26 * - A bitmap of which inodes are imagic inodes. (inode_imagic_map)
27 * - A bitmap of which inodes need to be expanded (expand_eisize_map)
28 * - A bitmap of which blocks are in use. (block_found_map)
29 * - A bitmap of which blocks are in use by two inodes (block_dup_map)
30 * - The data blocks of the directory inodes. (dir_map)
31 * - A bitmap of EA inodes. (inode_ea_map)
33 * Pass 1 is designed to stash away enough information so that the
34 * other passes should not need to read in the inode information
35 * during the normal course of a filesystem check. (Althogh if an
36 * inconsistency is detected, other passes may need to read in an
39 * Note that pass 1B will be invoked if there are any duplicate blocks
43 #define _GNU_SOURCE 1 /* get strnlen() */
52 #include <ext2fs/ext2_ext_attr.h>
56 #ifdef NO_INLINE_FUNCS
59 #define _INLINE_ inline
62 static int process_block(ext2_filsys fs, blk64_t *blocknr,
63 e2_blkcnt_t blockcnt, blk64_t ref_blk,
64 int ref_offset, void *priv_data);
65 static int process_bad_block(ext2_filsys fs, blk64_t *block_nr,
66 e2_blkcnt_t blockcnt, blk64_t ref_blk,
67 int ref_offset, void *priv_data);
68 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
70 static void mark_table_blocks(e2fsck_t ctx);
71 static void alloc_bb_map(e2fsck_t ctx);
72 static void alloc_imagic_map(e2fsck_t ctx);
73 static void handle_fs_bad_blocks(e2fsck_t ctx);
74 static void process_inodes(e2fsck_t ctx, char *block_buf);
75 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b);
76 static errcode_t scan_callback(ext2_filsys fs, ext2_inode_scan scan,
77 dgrp_t group, void * priv_data);
78 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
79 char *block_buf, int adjust_sign);
80 /* static char *describe_illegal_block(ext2_filsys fs, blk64_t block); */
82 struct process_block_struct {
84 unsigned is_dir:1, is_reg:1, clear:1, suppress:1,
85 fragmented:1, compressed:1, bbcheck:1,
89 e2_blkcnt_t last_block;
90 e2_blkcnt_t last_init_lblock;
91 e2_blkcnt_t last_db_block;
92 int num_illegal_blocks;
93 blk64_t previous_block;
94 struct ext2_inode *inode;
95 struct problem_context *pctx;
96 ext2fs_block_bitmap fs_meta_blocks;
100 struct process_inode_block {
102 struct ext2_inode inode;
105 struct scan_callback_struct {
111 * For the inodes to process list.
113 static struct process_inode_block *inodes_to_process;
114 static int process_inode_count;
116 static __u64 ext2_max_sizes[EXT2_MAX_BLOCK_LOG_SIZE -
117 EXT2_MIN_BLOCK_LOG_SIZE + 1];
120 * Free all memory allocated by pass1 in preparation for restarting
123 static void unwind_pass1(ext2_filsys fs EXT2FS_ATTR((unused)))
125 ext2fs_free_mem(&inodes_to_process);
126 inodes_to_process = 0;
130 * Check to make sure a device inode is real. Returns 1 if the device
131 * checks out, 0 if not.
133 * Note: this routine is now also used to check FIFO's and Sockets,
134 * since they have the same requirement; the i_block fields should be
137 int e2fsck_pass1_check_device_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
138 struct ext2_inode *inode)
143 * If the index flag is set, then this is a bogus
146 if (inode->i_flags & (EXT2_INDEX_FL | EXT4_EXTENTS_FL))
150 * We should be able to do the test below all the time, but
151 * because the kernel doesn't forcibly clear the device
152 * inode's additional i_block fields, there are some rare
153 * occasions when a legitimate device inode will have non-zero
154 * additional i_block fields. So for now, we only complain
155 * when the immutable flag is set, which should never happen
156 * for devices. (And that's when the problem is caused, since
157 * you can't set or clear immutable flags for devices.) Once
158 * the kernel has been fixed we can change this...
160 if (inode->i_flags & (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)) {
161 for (i=4; i < EXT2_N_BLOCKS; i++)
162 if (inode->i_block[i])
169 * Check to make sure a symlink inode is real. Returns 1 if the symlink
170 * checks out, 0 if not.
172 int e2fsck_pass1_check_symlink(ext2_filsys fs, ext2_ino_t ino,
173 struct ext2_inode *inode, char *buf)
178 ext2_extent_handle_t handle;
179 struct ext2_extent_info info;
180 struct ext2fs_extent extent;
182 if ((inode->i_size_high || inode->i_size == 0) ||
183 (inode->i_flags & EXT2_INDEX_FL))
186 if (inode->i_flags & EXT4_EXTENTS_FL) {
187 if (inode->i_size > fs->blocksize)
189 if (ext2fs_extent_open2(fs, ino, inode, &handle))
192 if (ext2fs_extent_get_info(handle, &info) ||
193 (info.num_entries != 1) ||
194 (info.max_depth != 0))
196 if (ext2fs_extent_get(handle, EXT2_EXTENT_ROOT, &extent) ||
197 (extent.e_lblk != 0) ||
198 (extent.e_len != 1) ||
199 (extent.e_pblk < fs->super->s_first_data_block) ||
200 (extent.e_pblk >= ext2fs_blocks_count(fs->super)))
204 ext2fs_extent_free(handle);
208 blocks = ext2fs_inode_data_blocks2(fs, inode);
210 if ((inode->i_size >= fs->blocksize) ||
211 (blocks != fs->blocksize >> 9) ||
212 (inode->i_block[0] < fs->super->s_first_data_block) ||
213 (inode->i_block[0] >= ext2fs_blocks_count(fs->super)))
216 for (i = 1; i < EXT2_N_BLOCKS; i++)
217 if (inode->i_block[i])
220 if (io_channel_read_blk64(fs->io, inode->i_block[0], 1, buf))
223 len = strnlen(buf, fs->blocksize);
224 if (len == fs->blocksize)
227 if (inode->i_size >= sizeof(inode->i_block))
230 len = strnlen((char *)inode->i_block, sizeof(inode->i_block));
231 if (len == sizeof(inode->i_block))
234 if (len != inode->i_size)
240 * If the immutable (or append-only) flag is set on the inode, offer
243 #define BAD_SPECIAL_FLAGS (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL)
244 static void check_immutable(e2fsck_t ctx, struct problem_context *pctx)
246 if (!(pctx->inode->i_flags & BAD_SPECIAL_FLAGS))
249 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
250 if (!fix_problem(ctx, PR_1_SET_IMMUTABLE, pctx))
253 pctx->inode->i_flags &= ~BAD_SPECIAL_FLAGS;
254 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
258 * If device, fifo or socket, check size is zero -- if not offer to
261 static void check_size(e2fsck_t ctx, struct problem_context *pctx)
263 struct ext2_inode *inode = pctx->inode;
265 if (EXT2_I_SIZE(inode) == 0)
268 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
269 if (!fix_problem(ctx, PR_1_SET_NONZSIZE, pctx))
272 ext2fs_inode_size_set(ctx->fs, inode, 0);
273 e2fsck_write_inode(ctx, pctx->ino, pctx->inode, "pass1");
276 static void e2fsck_block_alloc_stats(ext2_filsys fs, blk64_t blk, int inuse)
278 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
280 if (ctx->block_found_map) {
282 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
284 ext2fs_unmark_block_bitmap2(ctx->block_found_map, blk);
288 static void mark_inode_ea_map(e2fsck_t ctx, struct problem_context *pctx,
291 if (!ctx->inode_ea_map) {
292 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
296 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
302 ext2fs_mark_inode_bitmap2(ctx->inode_ea_map, ino);
306 * Delete an EA entry. If this is the last entry to be deleted, then i_file_acl
307 * must have been freed, so we must update e2fsck block statistics and set
308 * i_file_acl_deleted.
309 * When we delete the entry successfully, this function returns 0, else
313 static int e2fsck_ea_entry_delete(e2fsck_t ctx,
314 struct ext2_ext_attr_entry *entry,
315 struct problem_context *pctx,
316 int *i_file_acl_deleted, problem_t prob)
318 blk_t i_file_acl = pctx->inode->i_file_acl;
321 pctx->num = entry->e_value_inum;
323 if (fix_problem(ctx, prob, pctx)) {
324 /* Delete corrupt EA entry */
325 err = ext2fs_attr_set(ctx->fs, pctx->ino, pctx->inode,
326 entry->e_name_index, entry->e_name,
329 if (i_file_acl && pctx->inode->i_file_acl == 0) {
330 e2fsck_block_alloc_stats(ctx->fs, i_file_acl,
332 *i_file_acl_deleted = 1;
342 * Check validity of EA inode. Return 0 if EA inode is valid, nonzero otherwise.
344 static int check_large_ea_inode(e2fsck_t ctx, struct ext2_ext_attr_entry *entry,
345 struct problem_context *pctx,
346 int *i_file_acl_deleted)
348 struct ext2_inode inode;
351 /* Check if inode is within valid range */
352 if ((entry->e_value_inum < EXT2_FIRST_INODE(ctx->fs->super)) ||
353 (entry->e_value_inum > ctx->fs->super->s_inodes_count)) {
354 ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
356 PR_1_ATTR_VALUE_EA_INODE);
357 /* If user refuses to delete this entry, caller may try to set
358 * the bit for this out-of-bound inode in inode_ea_map, so
359 * always return failure */
363 e2fsck_read_inode(ctx, entry->e_value_inum, &inode, "pass1");
364 if (!(inode.i_flags & EXT4_EA_INODE_FL)) {
365 /* If EXT4_EA_INODE_FL flag is not present but back-pointer
366 * matches then we should set this flag */
367 if (inode.i_mtime == pctx->ino &&
368 inode.i_generation == pctx->inode->i_generation &&
369 fix_problem(ctx, PR_1_ATTR_SET_EA_INODE_FL, pctx)) {
370 inode.i_flags |= EXT4_EA_INODE_FL;
371 ext2fs_write_inode(ctx->fs, entry->e_value_inum,&inode);
373 ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
375 PR_1_ATTR_NO_EA_INODE_FL);
378 } else if (inode.i_mtime != pctx->ino ||
379 inode.i_generation != pctx->inode->i_generation) {
380 ret = e2fsck_ea_entry_delete(ctx, entry, pctx,
382 PR_1_ATTR_INVAL_EA_INODE);
390 static void check_ea_in_inode(e2fsck_t ctx, struct problem_context *pctx)
392 struct ext2_super_block *sb = ctx->fs->super;
393 struct ext2_inode_large *inode;
394 struct ext2_ext_attr_entry *entry;
396 unsigned int storage_size, remain;
397 problem_t problem = 0;
399 inode = (struct ext2_inode_large *) pctx->inode;
400 storage_size = EXT2_INODE_SIZE(ctx->fs->super) -
401 EXT2_GOOD_OLD_INODE_SIZE - inode->i_extra_isize;
402 entry = &IHDR(inode)->h_first_entry[0];
403 start = (char *)entry;
405 /* scan all entry's headers first */
407 /* take finish entry 0UL into account */
408 remain = storage_size - sizeof(__u32);
410 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
413 /* header eats this space */
414 remain -= sizeof(struct ext2_ext_attr_entry);
416 /* is attribute name valid? */
417 if (EXT2_EXT_ATTR_SIZE(entry->e_name_len) > remain) {
418 pctx->num = entry->e_name_len;
419 problem = PR_1_ATTR_NAME_LEN;
423 /* attribute len eats this space */
424 remain -= EXT2_EXT_ATTR_SIZE(entry->e_name_len);
426 /* check value size */
427 if (entry->e_value_size > remain) {
428 pctx->num = entry->e_value_size;
429 problem = PR_1_ATTR_VALUE_SIZE;
433 if (entry->e_value_inum == 0) {
434 /* check value size */
435 if (entry->e_value_size > remain) {
436 pctx->num = entry->e_value_size;
437 problem = PR_1_ATTR_VALUE_SIZE;
443 ret = check_large_ea_inode(ctx, entry, pctx, &tmp);
445 mark_inode_ea_map(ctx, pctx,
446 entry->e_value_inum);
449 /* Value size cannot be larger than EA space in inode */
450 if (entry->e_value_offs > storage_size ||
451 (entry->e_value_inum == 0 &&
452 entry->e_value_offs + entry->e_value_size > storage_size)) {
453 problem = PR_1_INODE_EA_BAD_VALUE;
457 hash = ext2fs_ext_attr_hash_entry(entry,
458 start + entry->e_value_offs);
460 /* e_hash may be 0 in older inode's ea */
461 if (entry->e_hash != 0 && entry->e_hash != hash) {
462 pctx->num = entry->e_hash;
463 problem = PR_1_ATTR_HASH;
467 /* If EA value is stored in external inode then it does not
468 * consume space here */
469 if (entry->e_value_inum == 0)
470 remain -= entry->e_value_size;
472 entry = EXT2_EXT_ATTR_NEXT(entry);
476 * it seems like a corruption. it's very unlikely we could repair
477 * EA(s) in automatic fashion -bzzz
479 if (problem == 0 || !fix_problem(ctx, problem, pctx))
482 /* simply remove all possible EA(s) */
483 *((__u32 *)start) = 0UL;
484 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
485 EXT2_INODE_SIZE(sb), "pass1");
488 static void check_inode_extra_space(e2fsck_t ctx, struct problem_context *pctx)
490 struct ext2_super_block *sb = ctx->fs->super;
491 struct ext2_inode_large *inode;
493 int min, max, dirty = 0;
495 inode = (struct ext2_inode_large *) pctx->inode;
496 if (EXT2_INODE_SIZE(sb) == EXT2_GOOD_OLD_INODE_SIZE) {
497 /* this isn't large inode. so, nothing to check */
502 printf("inode #%u, i_extra_size %d\n", pctx->ino,
503 inode->i_extra_isize);
505 /* i_extra_isize must cover i_extra_isize + i_checksum_hi at least */
506 min = sizeof(inode->i_extra_isize) + sizeof(inode->i_checksum_hi);
507 max = EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE;
509 * For now we will allow i_extra_isize to be 0, but really
510 * implementations should never allow i_extra_isize to be 0
512 if (inode->i_extra_isize &&
513 (inode->i_extra_isize < min || inode->i_extra_isize > max)) {
514 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
515 if (!fix_problem(ctx, PR_1_EXTRA_ISIZE, pctx))
517 inode->i_extra_isize = ctx->want_extra_isize;
523 if (EXT4_FITS_IN_INODE(inode, inode, i_crtime) &&
524 inode->i_crtime != 0 &&
525 (EXT4_XTIME_FUTURE(ctx, sb, inode->i_crtime, 2*ctx->time_fudge) ||
526 EXT4_XTIME_ANCIENT(ctx, sb, inode->i_crtime, 2*ctx->time_fudge))) {
527 pctx->num = inode->i_crtime;
528 if (fix_problem(ctx, PR_1_CRTIME_BAD, pctx)) {
532 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_HIGH);
535 eamagic = &IHDR(inode)->h_magic;
536 if (*eamagic != EXT2_EXT_ATTR_MAGIC &&
537 (ctx->flags & E2F_FLAG_EXPAND_EISIZE) &&
538 (inode->i_extra_isize < ctx->want_extra_isize)) {
539 fix_problem(ctx, PR_1_EXPAND_EISIZE, pctx);
540 memset((char *)inode + EXT2_GOOD_OLD_INODE_SIZE, 0,
541 EXT2_INODE_SIZE(sb) - EXT2_GOOD_OLD_INODE_SIZE);
542 inode->i_extra_isize = ctx->want_extra_isize;
544 if (inode->i_extra_isize < ctx->min_extra_isize)
545 ctx->min_extra_isize = inode->i_extra_isize;
548 if (*eamagic == EXT2_EXT_ATTR_MAGIC)
549 check_ea_in_inode(ctx, pctx);
552 e2fsck_write_inode_full(ctx, pctx->ino, pctx->inode,
553 EXT2_INODE_SIZE(sb), "pass1");
557 * Check to see if the inode might really be a directory, despite i_mode
559 * This is a lot of complexity for something for which I'm not really
560 * convinced happens frequently in the wild. If for any reason this
561 * causes any problems, take this code out.
562 * [tytso:20070331.0827EDT]
564 static void check_is_really_dir(e2fsck_t ctx, struct problem_context *pctx,
567 struct ext2_inode *inode = pctx->inode;
568 struct ext2_dir_entry *dirent;
571 unsigned int i, rec_len, not_device = 0;
575 * If the mode looks OK, we believe it. If the first block in
576 * the i_block array is 0, this cannot be a directory. If the
577 * inode is extent-mapped, it is still the case that the latter
578 * cannot be 0 - the magic number in the extent header would make
581 if (LINUX_S_ISDIR(inode->i_mode) || LINUX_S_ISREG(inode->i_mode) ||
582 LINUX_S_ISLNK(inode->i_mode) || inode->i_block[0] == 0)
586 * Check the block numbers in the i_block array for validity:
587 * zero blocks are skipped (but the first one cannot be zero -
588 * see above), other blocks are checked against the first and
589 * max data blocks (from the the superblock) and against the
590 * block bitmap. Any invalid block found means this cannot be
593 * If there are non-zero blocks past the fourth entry, then
594 * this cannot be a device file: we remember that for the next
597 * For extent mapped files, we don't do any sanity checking:
598 * just try to get the phys block of logical block 0 and run
602 extent_fs = (ctx->fs->super->s_feature_incompat &
603 EXT3_FEATURE_INCOMPAT_EXTENTS);
604 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) {
606 if (ext2fs_bmap2(ctx->fs, pctx->ino, inode, 0, 0, 0, 0,
609 /* device files are never extent mapped */
612 for (i=0; i < EXT2_N_BLOCKS; i++) {
613 blk = inode->i_block[i];
619 if (blk < ctx->fs->super->s_first_data_block ||
620 blk >= ext2fs_blocks_count(ctx->fs->super) ||
621 ext2fs_fast_test_block_bitmap2(ctx->block_found_map,
623 return; /* Invalid block, can't be dir */
625 blk = inode->i_block[0];
629 * If the mode says this is a device file and the i_links_count field
630 * is sane and we have not ruled it out as a device file previously,
631 * we declare it a device file, not a directory.
633 if ((LINUX_S_ISCHR(inode->i_mode) || LINUX_S_ISBLK(inode->i_mode)) &&
634 (inode->i_links_count == 1) && !not_device)
637 /* read the first block */
638 ehandler_operation(_("reading directory block"));
639 retval = ext2fs_read_dir_block3(ctx->fs, blk, buf, 0);
640 ehandler_operation(0);
644 dirent = (struct ext2_dir_entry *) buf;
645 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
648 if (((dirent->name_len & 0xFF) != 1) ||
649 (dirent->name[0] != '.') ||
650 (dirent->inode != pctx->ino) ||
653 (rec_len >= ctx->fs->blocksize - 12))
656 dirent = (struct ext2_dir_entry *) (buf + rec_len);
657 retval = ext2fs_get_rec_len(ctx->fs, dirent, &rec_len);
660 if (((dirent->name_len & 0xFF) != 2) ||
661 (dirent->name[0] != '.') ||
662 (dirent->name[1] != '.') ||
667 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
668 if (fix_problem(ctx, PR_1_TREAT_AS_DIRECTORY, pctx)) {
669 inode->i_mode = (inode->i_mode & 07777) | LINUX_S_IFDIR;
670 e2fsck_write_inode_full(ctx, pctx->ino, inode,
671 EXT2_INODE_SIZE(ctx->fs->super),
672 "check_is_really_dir");
676 void e2fsck_setup_tdb_icount(e2fsck_t ctx, int flags,
679 unsigned int threshold;
687 profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
689 profile_get_uint(ctx->profile, "scratch_files",
690 "numdirs_threshold", 0, 0, &threshold);
691 profile_get_boolean(ctx->profile, "scratch_files",
692 "icount", 0, 1, &enable);
694 retval = ext2fs_get_num_dirs(ctx->fs, &num_dirs);
696 num_dirs = 1024; /* Guess */
698 if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
699 (threshold && num_dirs <= threshold))
702 retval = ext2fs_create_icount_tdb(ctx->fs, tdb_dir, flags, ret);
707 int e2fsck_pass1_delete_attr(e2fsck_t ctx, struct ext2_inode_large *inode,
708 struct problem_context *pctx, int needed_size)
710 struct ext2_ext_attr_header *header;
711 struct ext2_ext_attr_entry *entry_ino, *entry_blk = NULL, *entry;
712 char *start, name[4096], block_buf[4096];
713 int len, index = EXT2_ATTR_INDEX_USER, entry_size, ea_size;
714 int in_inode = 1, error;
715 unsigned int freed_bytes = inode->i_extra_isize;
717 entry_ino = &IHDR(inode)->h_first_entry[0];
718 start = (char *)entry_ino;
720 if (inode->i_file_acl) {
721 error = ext2fs_read_ext_attr(ctx->fs, inode->i_file_acl,
723 /* We have already checked this block, shouldn't happen */
725 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
728 header = BHDR(block_buf);
729 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
730 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, pctx);
734 entry_blk = (struct ext2_ext_attr_entry *)(header+1);
737 len = sizeof(entry->e_name);
738 entry_size = ext2fs_attr_get_next_attr(entry, index, name, len, 1);
740 while (freed_bytes < needed_size) {
741 if (entry_size && name[0] != '\0') {
743 if (fix_problem(ctx, PR_1_EISIZE_DELETE_EA, pctx)) {
744 ea_size = EXT2_EXT_ATTR_LEN(entry->e_name_len) +
745 EXT2_EXT_ATTR_SIZE(entry->e_value_size);
746 error = ext2fs_attr_set(ctx->fs, pctx->ino,
747 (struct ext2_inode *)inode,
748 index, name, 0, 0, 0);
750 freed_bytes += ea_size;
753 len = sizeof(entry->e_name);
754 entry_size = ext2fs_attr_get_next_attr(entry, index,name,len,0);
755 entry = EXT2_EXT_ATTR_NEXT(entry);
756 if (EXT2_EXT_IS_LAST_ENTRY(entry)) {
759 len = sizeof(entry->e_name);
760 entry_size = ext2fs_attr_get_next_attr(entry,
761 index, name, len, 1);
766 if (!entry && index < EXT2_ATTR_INDEX_MAX)
767 entry = (struct ext2_ext_attr_entry *)start;
777 int e2fsck_pass1_expand_eisize(e2fsck_t ctx, struct ext2_inode_large *inode,
778 struct problem_context *pctx)
780 int needed_size = 0, retval, ret = EXT2_EXPAND_EISIZE_UNSAFE;
784 retval = ext2fs_expand_extra_isize(ctx->fs, pctx->ino, inode,
785 ctx->want_extra_isize, &ret,
787 if (ret & EXT2_EXPAND_EISIZE_NEW_BLOCK)
788 goto mark_expand_eisize_map;
790 e2fsck_write_inode_full(ctx, pctx->ino,
791 (struct ext2_inode *)inode,
792 EXT2_INODE_SIZE(ctx->fs->super),
797 if (ret & EXT2_EXPAND_EISIZE_NOSPC) {
798 if (ctx->options & (E2F_OPT_PREEN | E2F_OPT_YES)) {
799 fix_problem(ctx, PR_1_EA_BLK_NOSPC, pctx);
800 ctx->flags |= E2F_FLAG_ABORT;
805 pctx->num = ctx->fs->super->s_min_extra_isize;
806 fix_problem(ctx, PR_1_EXPAND_EISIZE_WARNING, pctx);
810 retval = e2fsck_pass1_delete_attr(ctx, inode, pctx,
812 if (retval >= ctx->want_extra_isize)
815 needed_size -= retval;
818 * We loop here until either the user deletes EA(s) or
819 * EXTRA_ISIZE feature is disabled.
821 if (fix_problem(ctx, PR_1_CLEAR_EXTRA_ISIZE, pctx)) {
822 ctx->fs->super->s_feature_ro_compat &=
823 ~EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE;
824 ext2fs_mark_super_dirty(ctx->fs);
828 ctx->fs_unexpanded_inodes++;
830 /* No EA was deleted, inode cannot be expanded */
834 mark_expand_eisize_map:
835 if (!ctx->expand_eisize_map) {
836 pctx->errcode = ext2fs_allocate_inode_bitmap(ctx->fs,
837 _("expand extrz isize map"),
838 &ctx->expand_eisize_map);
840 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR,
846 /* Add this inode to the expand_eisize_map */
847 ext2fs_mark_inode_bitmap2(ctx->expand_eisize_map, pctx->ino);
851 static void reserve_block_for_root_repair(e2fsck_t ctx)
855 ext2_filsys fs = ctx->fs;
857 ctx->root_repair_block = 0;
858 if (ext2fs_test_inode_bitmap2(ctx->inode_used_map, EXT2_ROOT_INO))
861 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
864 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
865 ctx->root_repair_block = blk;
868 static void reserve_block_for_lnf_repair(e2fsck_t ctx)
872 ext2_filsys fs = ctx->fs;
873 static const char name[] = "lost+found";
876 ctx->lnf_repair_block = 0;
877 if (!ext2fs_lookup(fs, EXT2_ROOT_INO, name, sizeof(name)-1, 0, &ino))
880 err = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
883 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
884 ctx->lnf_repair_block = blk;
887 void e2fsck_pass1(e2fsck_t ctx)
891 ext2_filsys fs = ctx->fs;
893 struct ext2_inode *inode = NULL;
894 ext2_inode_scan scan = NULL;
895 char *block_buf = NULL;
896 #ifdef RESOURCE_TRACK
897 struct resource_track rtrack;
899 unsigned char frag, fsize;
900 struct problem_context pctx;
901 struct scan_callback_struct scan_struct;
902 struct ext2_super_block *sb = ctx->fs->super;
904 unsigned int save_type;
905 int imagic_fs, extent_fs;
906 int low_dtime_check = 1;
911 init_resource_track(&rtrack, ctx->fs->io);
912 clear_problem_context(&pctx);
914 if (!(ctx->options & E2F_OPT_PREEN))
915 fix_problem(ctx, PR_1_PASS_HEADER, &pctx);
917 if ((fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) &&
918 !(ctx->options & E2F_OPT_NO)) {
919 if (ext2fs_u32_list_create(&ctx->dirs_to_hash, 50))
920 ctx->dirs_to_hash = 0;
924 mtrace_print("Pass 1");
927 #define EXT2_BPP(bits) (1ULL << ((bits) - 2))
929 for (i = EXT2_MIN_BLOCK_LOG_SIZE; i <= EXT2_MAX_BLOCK_LOG_SIZE; i++) {
930 max_sizes = EXT2_NDIR_BLOCKS + EXT2_BPP(i);
931 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i);
932 max_sizes = max_sizes + EXT2_BPP(i) * EXT2_BPP(i) * EXT2_BPP(i);
933 max_sizes = (max_sizes * (1UL << i));
934 ext2_max_sizes[i - EXT2_MIN_BLOCK_LOG_SIZE] = max_sizes;
938 imagic_fs = (sb->s_feature_compat & EXT2_FEATURE_COMPAT_IMAGIC_INODES);
939 extent_fs = (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_EXTENTS);
942 * Allocate bitmaps structures
944 pctx.errcode = e2fsck_allocate_inode_bitmap(fs, _("in-use inode map"),
945 EXT2FS_BMAP64_RBTREE,
947 &ctx->inode_used_map);
950 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
951 ctx->flags |= E2F_FLAG_ABORT;
954 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
955 _("directory inode map"),
956 EXT2FS_BMAP64_AUTODIR,
957 "inode_dir_map", &ctx->inode_dir_map);
960 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
961 ctx->flags |= E2F_FLAG_ABORT;
964 pctx.errcode = e2fsck_allocate_inode_bitmap(fs,
965 _("regular file inode map"), EXT2FS_BMAP64_RBTREE,
966 "inode_reg_map", &ctx->inode_reg_map);
969 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
970 ctx->flags |= E2F_FLAG_ABORT;
973 pctx.errcode = e2fsck_allocate_subcluster_bitmap(fs,
974 _("in-use block map"), EXT2FS_BMAP64_RBTREE,
975 "block_found_map", &ctx->block_found_map);
978 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
979 ctx->flags |= E2F_FLAG_ABORT;
982 e2fsck_setup_tdb_icount(ctx, 0, &ctx->inode_link_info);
983 if (!ctx->inode_link_info) {
984 e2fsck_set_bitmap_type(fs, EXT2FS_BMAP64_RBTREE,
985 "inode_link_info", &save_type);
986 pctx.errcode = ext2fs_create_icount2(fs, 0, 0, 0,
987 &ctx->inode_link_info);
988 fs->default_bitmap_type = save_type;
992 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
993 ctx->flags |= E2F_FLAG_ABORT;
996 inode_size = EXT2_INODE_SIZE(fs->super);
997 inode = (struct ext2_inode *)
998 e2fsck_allocate_memory(ctx, inode_size, "scratch inode");
1000 inodes_to_process = (struct process_inode_block *)
1001 e2fsck_allocate_memory(ctx,
1002 (ctx->process_inode_size *
1003 sizeof(struct process_inode_block)),
1004 "array of inodes to process");
1005 process_inode_count = 0;
1007 pctx.errcode = ext2fs_init_dblist(fs, 0);
1009 fix_problem(ctx, PR_1_ALLOCATE_DBCOUNT, &pctx);
1010 ctx->flags |= E2F_FLAG_ABORT;
1015 * If the last orphan field is set, clear it, since the pass1
1016 * processing will automatically find and clear the orphans.
1017 * In the future, we may want to try using the last_orphan
1018 * linked list ourselves, but for now, we clear it so that the
1019 * ext3 mount code won't get confused.
1021 if (!(ctx->options & E2F_OPT_READONLY)) {
1022 if (fs->super->s_last_orphan) {
1023 fs->super->s_last_orphan = 0;
1024 ext2fs_mark_super_dirty(fs);
1028 mark_table_blocks(ctx);
1029 pctx.errcode = ext2fs_convert_subcluster_bitmap(fs,
1030 &ctx->block_found_map);
1032 fix_problem(ctx, PR_1_CONVERT_SUBCLUSTER, &pctx);
1033 ctx->flags |= E2F_FLAG_ABORT;
1036 block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 3,
1037 "block interate buffer");
1038 e2fsck_use_inode_shortcuts(ctx, 1);
1039 e2fsck_intercept_block_allocations(ctx);
1040 old_op = ehandler_operation(_("opening inode scan"));
1041 pctx.errcode = ext2fs_open_inode_scan(fs, ctx->inode_buffer_blocks,
1043 ehandler_operation(old_op);
1045 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1046 ctx->flags |= E2F_FLAG_ABORT;
1049 ext2fs_inode_scan_flags(scan, EXT2_SF_SKIP_MISSING_ITABLE, 0);
1050 ctx->stashed_inode = inode;
1051 scan_struct.ctx = ctx;
1052 scan_struct.block_buf = block_buf;
1053 ext2fs_set_inode_callback(scan, scan_callback, &scan_struct);
1054 if (ctx->progress && ((ctx->progress)(ctx, 1, 0,
1055 ctx->fs->group_desc_count)))
1057 if ((fs->super->s_wtime < fs->super->s_inodes_count) ||
1058 (fs->super->s_mtime < fs->super->s_inodes_count) ||
1059 (fs->super->s_mkfs_time &&
1060 fs->super->s_mkfs_time < fs->super->s_inodes_count))
1061 low_dtime_check = 0;
1063 if ((fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) &&
1064 fs->super->s_mmp_block > fs->super->s_first_data_block &&
1065 fs->super->s_mmp_block < ext2fs_blocks_count(fs->super))
1066 ext2fs_mark_block_bitmap2(ctx->block_found_map,
1067 fs->super->s_mmp_block);
1070 if (ino % (fs->super->s_inodes_per_group * 4) == 1) {
1071 if (e2fsck_mmp_update(fs))
1072 fatal_error(ctx, 0);
1074 old_op = ehandler_operation(_("getting next inode from scan"));
1075 pctx.errcode = ext2fs_get_next_inode_full(scan, &ino,
1077 ehandler_operation(old_op);
1078 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1080 if (pctx.errcode == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE) {
1081 if (!ctx->inode_bb_map)
1083 ext2fs_mark_inode_bitmap2(ctx->inode_bb_map, ino);
1084 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1088 fix_problem(ctx, PR_1_ISCAN_ERROR, &pctx);
1089 ctx->flags |= E2F_FLAG_ABORT;
1096 ctx->stashed_ino = ino;
1097 if (inode->i_links_count) {
1098 pctx.errcode = ext2fs_icount_store(ctx->inode_link_info,
1099 ino, inode->i_links_count);
1101 pctx.num = inode->i_links_count;
1102 fix_problem(ctx, PR_1_ICOUNT_STORE, &pctx);
1103 ctx->flags |= E2F_FLAG_ABORT;
1109 * Test for incorrect extent flag settings.
1111 * On big-endian machines we must be careful:
1112 * When the inode is read, the i_block array is not swapped
1113 * if the extent flag is set. Therefore if we are testing
1114 * for or fixing a wrongly-set flag, we must potentially
1115 * (un)swap before testing, or after fixing.
1119 * In this case the extents flag was set when read, so
1120 * extent_header_verify is ok. If the inode is cleared,
1121 * no need to swap... so no extra swapping here.
1123 if ((inode->i_flags & EXT4_EXTENTS_FL) && !extent_fs &&
1124 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1125 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO))) {
1126 if ((ext2fs_extent_header_verify(inode->i_block,
1127 sizeof(inode->i_block)) == 0) &&
1128 fix_problem(ctx, PR_1_EXTENT_FEATURE, &pctx)) {
1129 sb->s_feature_incompat |= EXT3_FEATURE_INCOMPAT_EXTENTS;
1130 ext2fs_mark_super_dirty(fs);
1132 } else if (fix_problem(ctx, PR_1_EXTENTS_SET, &pctx)) {
1134 e2fsck_clear_inode(ctx, ino, inode, 0, "pass1");
1135 if (ino == EXT2_BAD_INO)
1136 ext2fs_mark_inode_bitmap2(ctx->inode_used_map,
1143 * For big-endian machines:
1144 * If the inode didn't have the extents flag set when it
1145 * was read, then the i_blocks array was swapped. To test
1146 * as an extents header, we must swap it back first.
1147 * IF we then set the extents flag, the entire i_block
1148 * array must be un/re-swapped to make it proper extents data.
1150 if (extent_fs && !(inode->i_flags & EXT4_EXTENTS_FL) &&
1151 (inode->i_links_count || (ino == EXT2_BAD_INO) ||
1152 (ino == EXT2_ROOT_INO) || (ino == EXT2_JOURNAL_INO)) &&
1153 (LINUX_S_ISREG(inode->i_mode) ||
1154 LINUX_S_ISDIR(inode->i_mode))) {
1156 #ifdef WORDS_BIGENDIAN
1157 __u32 tmp_block[EXT2_N_BLOCKS];
1159 for (i = 0; i < EXT2_N_BLOCKS; i++)
1160 tmp_block[i] = ext2fs_swab32(inode->i_block[i]);
1163 ehp = inode->i_block;
1165 if ((ext2fs_extent_header_verify(ehp,
1166 sizeof(inode->i_block)) == 0)) {
1167 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1168 if (fix_problem(ctx, PR_1_UNSET_EXTENT_FL,
1170 inode->i_flags |= EXT4_EXTENTS_FL;
1171 #ifdef WORDS_BIGENDIAN
1172 memcpy(inode->i_block, tmp_block,
1173 sizeof(inode->i_block));
1175 e2fsck_write_inode(ctx, ino, inode,
1181 if (ino == EXT2_BAD_INO) {
1182 struct process_block_struct pb;
1184 if ((inode->i_mode || inode->i_uid || inode->i_gid ||
1185 inode->i_links_count || inode->i_file_acl) &&
1186 fix_problem(ctx, PR_1_INVALID_BAD_INODE, &pctx)) {
1187 memset(inode, 0, sizeof(struct ext2_inode));
1188 e2fsck_write_inode(ctx, ino, inode,
1192 pctx.errcode = ext2fs_copy_bitmap(ctx->block_found_map,
1193 &pb.fs_meta_blocks);
1196 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, &pctx);
1197 ctx->flags |= E2F_FLAG_ABORT;
1200 pb.ino = EXT2_BAD_INO;
1201 pb.num_blocks = pb.last_block = 0;
1202 pb.last_db_block = -1;
1203 pb.num_illegal_blocks = 0;
1204 pb.suppress = 0; pb.clear = 0; pb.is_dir = 0;
1205 pb.is_reg = 0; pb.fragmented = 0; pb.bbcheck = 0;
1209 pctx.errcode = ext2fs_block_iterate3(fs, ino, 0,
1210 block_buf, process_bad_block, &pb);
1211 ext2fs_free_block_bitmap(pb.fs_meta_blocks);
1213 fix_problem(ctx, PR_1_BLOCK_ITERATE, &pctx);
1214 ctx->flags |= E2F_FLAG_ABORT;
1218 if (!fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK_PROMPT, &pctx)) {
1219 ctx->flags |= E2F_FLAG_ABORT;
1222 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1223 clear_problem_context(&pctx);
1225 } else if (ino == EXT2_ROOT_INO) {
1227 * Make sure the root inode is a directory; if
1228 * not, offer to clear it. It will be
1229 * regnerated in pass #3.
1231 if (!LINUX_S_ISDIR(inode->i_mode)) {
1232 if (fix_problem(ctx, PR_1_ROOT_NO_DIR, &pctx))
1236 * If dtime is set, offer to clear it. mke2fs
1237 * version 0.2b created filesystems with the
1238 * dtime field set for the root and lost+found
1239 * directories. We won't worry about
1240 * /lost+found, since that can be regenerated
1241 * easily. But we will fix the root directory
1242 * as a special case.
1244 if (inode->i_dtime && inode->i_links_count) {
1245 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1246 if (fix_problem(ctx, PR_1_ROOT_DTIME, &pctx)) {
1248 e2fsck_write_inode(ctx, ino, inode,
1252 } else if (ino == EXT2_JOURNAL_INO) {
1253 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1254 if (fs->super->s_journal_inum == EXT2_JOURNAL_INO) {
1255 if (!LINUX_S_ISREG(inode->i_mode) &&
1256 fix_problem(ctx, PR_1_JOURNAL_BAD_MODE,
1258 inode->i_mode = LINUX_S_IFREG;
1259 e2fsck_write_inode(ctx, ino, inode,
1262 check_blocks(ctx, &pctx, block_buf);
1265 if ((inode->i_links_count ||
1266 inode->i_blocks || inode->i_block[0]) &&
1267 fix_problem(ctx, PR_1_JOURNAL_INODE_NOT_CLEAR,
1269 memset(inode, 0, inode_size);
1270 ext2fs_icount_store(ctx->inode_link_info,
1272 e2fsck_write_inode_full(ctx, ino, inode,
1273 inode_size, "pass1");
1275 } else if ((ino == EXT4_USR_QUOTA_INO) ||
1276 (ino == EXT4_GRP_QUOTA_INO)) {
1277 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1278 if ((fs->super->s_feature_ro_compat &
1279 EXT4_FEATURE_RO_COMPAT_QUOTA) &&
1280 ((fs->super->s_usr_quota_inum == ino) ||
1281 (fs->super->s_grp_quota_inum == ino))) {
1282 if (!LINUX_S_ISREG(inode->i_mode) &&
1283 fix_problem(ctx, PR_1_QUOTA_BAD_MODE,
1285 inode->i_mode = LINUX_S_IFREG;
1286 e2fsck_write_inode(ctx, ino, inode,
1289 check_blocks(ctx, &pctx, block_buf);
1292 if ((inode->i_links_count ||
1293 inode->i_blocks || inode->i_block[0]) &&
1294 fix_problem(ctx, PR_1_QUOTA_INODE_NOT_CLEAR,
1296 memset(inode, 0, inode_size);
1297 ext2fs_icount_store(ctx->inode_link_info,
1299 e2fsck_write_inode_full(ctx, ino, inode,
1300 inode_size, "pass1");
1302 } else if (ino < EXT2_FIRST_INODE(fs->super)) {
1303 problem_t problem = 0;
1305 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1306 if (ino == EXT2_BOOT_LOADER_INO) {
1307 if (LINUX_S_ISDIR(inode->i_mode))
1308 problem = PR_1_RESERVED_BAD_MODE;
1309 } else if (ino == EXT2_RESIZE_INO) {
1310 if (inode->i_mode &&
1311 !LINUX_S_ISREG(inode->i_mode))
1312 problem = PR_1_RESERVED_BAD_MODE;
1314 if (inode->i_mode != 0)
1315 problem = PR_1_RESERVED_BAD_MODE;
1318 if (fix_problem(ctx, problem, &pctx)) {
1320 e2fsck_write_inode(ctx, ino, inode,
1324 check_blocks(ctx, &pctx, block_buf);
1329 * Check for inodes who might have been part of the
1330 * orphaned list linked list. They should have gotten
1331 * dealt with by now, unless the list had somehow been
1334 * FIXME: In the future, inodes which are still in use
1335 * (and which are therefore) pending truncation should
1336 * be handled specially. Right now we just clear the
1337 * dtime field, and the normal e2fsck handling of
1338 * inodes where i_size and the inode blocks are
1339 * inconsistent is to fix i_size, instead of releasing
1340 * the extra blocks. This won't catch the inodes that
1341 * was at the end of the orphan list, but it's better
1342 * than nothing. The right answer is that there
1343 * shouldn't be any bugs in the orphan list handling. :-)
1345 if (inode->i_dtime && low_dtime_check &&
1346 inode->i_dtime < ctx->fs->super->s_inodes_count) {
1347 if (fix_problem(ctx, PR_1_LOW_DTIME, &pctx)) {
1348 inode->i_dtime = inode->i_links_count ?
1350 e2fsck_write_inode(ctx, ino, inode,
1356 * This code assumes that deleted inodes have
1357 * i_links_count set to 0.
1359 if (!inode->i_links_count) {
1360 if (!inode->i_dtime && inode->i_mode) {
1361 if (fix_problem(ctx,
1362 PR_1_ZERO_DTIME, &pctx)) {
1363 inode->i_dtime = ctx->now;
1364 e2fsck_write_inode(ctx, ino, inode,
1371 * n.b. 0.3c ext2fs code didn't clear i_links_count for
1372 * deleted files. Oops.
1374 * Since all new ext2 implementations get this right,
1375 * we now assume that the case of non-zero
1376 * i_links_count and non-zero dtime means that we
1377 * should keep the file, not delete it.
1380 if (inode->i_dtime) {
1381 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1382 if (fix_problem(ctx, PR_1_SET_DTIME, &pctx)) {
1384 e2fsck_write_inode(ctx, ino, inode, "pass1");
1388 ext2fs_mark_inode_bitmap2(ctx->inode_used_map, ino);
1389 switch (fs->super->s_creator_os) {
1391 frag = inode->osd2.hurd2.h_i_frag;
1392 fsize = inode->osd2.hurd2.h_i_fsize;
1398 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1399 if (inode->i_faddr || frag || fsize ||
1400 (LINUX_S_ISDIR(inode->i_mode) && inode->i_dir_acl))
1401 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1402 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1403 !(fs->super->s_feature_incompat &
1404 EXT4_FEATURE_INCOMPAT_64BIT) &&
1405 inode->osd2.linux2.l_i_file_acl_high != 0)
1406 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1407 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
1408 !(fs->super->s_feature_ro_compat &
1409 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
1410 (inode->osd2.linux2.l_i_blocks_hi != 0))
1411 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1412 if (inode->i_flags & EXT2_IMAGIC_FL) {
1414 if (!ctx->inode_imagic_map)
1415 alloc_imagic_map(ctx);
1416 ext2fs_mark_inode_bitmap2(ctx->inode_imagic_map,
1419 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1420 if (fix_problem(ctx, PR_1_SET_IMAGIC, &pctx)) {
1421 inode->i_flags &= ~EXT2_IMAGIC_FL;
1422 e2fsck_write_inode(ctx, ino,
1428 check_inode_extra_space(ctx, &pctx);
1429 check_is_really_dir(ctx, &pctx, block_buf);
1432 * ext2fs_inode_has_valid_blocks2 does not actually look
1433 * at i_block[] values, so not endian-sensitive here.
1435 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL) &&
1436 LINUX_S_ISLNK(inode->i_mode) &&
1437 !ext2fs_inode_has_valid_blocks2(fs, inode) &&
1438 fix_problem(ctx, PR_1_FAST_SYMLINK_EXTENT_FL, &pctx)) {
1439 inode->i_flags &= ~EXT4_EXTENTS_FL;
1440 e2fsck_write_inode(ctx, ino, inode, "pass1");
1443 if (LINUX_S_ISDIR(inode->i_mode)) {
1444 ext2fs_mark_inode_bitmap2(ctx->inode_dir_map, ino);
1445 e2fsck_add_dir_info(ctx, ino, 0);
1446 ctx->fs_directory_count++;
1447 } else if (LINUX_S_ISREG (inode->i_mode)) {
1448 ext2fs_mark_inode_bitmap2(ctx->inode_reg_map, ino);
1449 ctx->fs_regular_count++;
1450 } else if (LINUX_S_ISCHR (inode->i_mode) &&
1451 e2fsck_pass1_check_device_inode(fs, inode)) {
1452 check_immutable(ctx, &pctx);
1453 check_size(ctx, &pctx);
1454 ctx->fs_chardev_count++;
1455 } else if (LINUX_S_ISBLK (inode->i_mode) &&
1456 e2fsck_pass1_check_device_inode(fs, inode)) {
1457 check_immutable(ctx, &pctx);
1458 check_size(ctx, &pctx);
1459 ctx->fs_blockdev_count++;
1460 } else if (LINUX_S_ISLNK (inode->i_mode) &&
1461 e2fsck_pass1_check_symlink(fs, ino, inode,
1463 check_immutable(ctx, &pctx);
1464 ctx->fs_symlinks_count++;
1465 if (ext2fs_inode_data_blocks(fs, inode) == 0) {
1466 ctx->fs_fast_symlinks_count++;
1467 check_blocks(ctx, &pctx, block_buf);
1471 else if (LINUX_S_ISFIFO (inode->i_mode) &&
1472 e2fsck_pass1_check_device_inode(fs, inode)) {
1473 check_immutable(ctx, &pctx);
1474 check_size(ctx, &pctx);
1475 ctx->fs_fifo_count++;
1476 } else if ((LINUX_S_ISSOCK (inode->i_mode)) &&
1477 e2fsck_pass1_check_device_inode(fs, inode)) {
1478 check_immutable(ctx, &pctx);
1479 check_size(ctx, &pctx);
1480 ctx->fs_sockets_count++;
1482 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1485 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_atime, ctx->time_fudge))
1486 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1487 else if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_mtime,
1489 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1491 if (EXT4_XTIME_FUTURE(ctx, sb, inode->i_ctime, ctx->time_fudge))
1492 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1493 else if (EXT4_XTIME_ANCIENT(ctx, sb, inode->i_ctime,
1495 e2fsck_mark_inode_bad(ctx, ino, BADNESS_HIGH);
1497 /* i_crtime is checked in check_inode_extra_space() */
1499 if (!(inode->i_flags & EXT4_EXTENTS_FL)) {
1500 if (inode->i_block[EXT2_IND_BLOCK])
1501 ctx->fs_ind_count++;
1502 if (inode->i_block[EXT2_DIND_BLOCK])
1503 ctx->fs_dind_count++;
1504 if (inode->i_block[EXT2_TIND_BLOCK])
1505 ctx->fs_tind_count++;
1507 if (!(inode->i_flags & EXT4_EXTENTS_FL) &&
1508 (inode->i_block[EXT2_IND_BLOCK] ||
1509 inode->i_block[EXT2_DIND_BLOCK] ||
1510 inode->i_block[EXT2_TIND_BLOCK] ||
1511 ext2fs_file_acl_block(fs, inode))) {
1512 inodes_to_process[process_inode_count].ino = ino;
1513 inodes_to_process[process_inode_count].inode = *inode;
1514 process_inode_count++;
1516 check_blocks(ctx, &pctx, block_buf);
1518 if (ctx->flags & E2F_FLAG_EXPAND_EISIZE) {
1519 struct ext2_inode_large *inode_l;
1521 inode_l = (struct ext2_inode_large *)inode;
1523 if (inode_l->i_extra_isize < ctx->want_extra_isize) {
1524 fix_problem(ctx, PR_1_EXPAND_EISIZE, &pctx);
1525 inode_exp = e2fsck_pass1_expand_eisize(ctx,
1529 if ((inode_l->i_extra_isize < ctx->min_extra_isize) &&
1531 ctx->min_extra_isize = inode_l->i_extra_isize;
1534 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1537 if (process_inode_count >= ctx->process_inode_size) {
1538 process_inodes(ctx, block_buf);
1540 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1544 process_inodes(ctx, block_buf);
1545 ext2fs_close_inode_scan(scan);
1548 reserve_block_for_root_repair(ctx);
1549 reserve_block_for_lnf_repair(ctx);
1552 * If any extended attribute blocks' reference counts need to
1553 * be adjusted, either up (ctx->refcount_extra), or down
1554 * (ctx->refcount), then fix them.
1556 if (ctx->refcount) {
1557 adjust_extattr_refcount(ctx, ctx->refcount, block_buf, -1);
1558 ea_refcount_free(ctx->refcount);
1561 if (ctx->refcount_extra) {
1562 adjust_extattr_refcount(ctx, ctx->refcount_extra,
1564 ea_refcount_free(ctx->refcount_extra);
1565 ctx->refcount_extra = 0;
1568 if (ctx->invalid_bitmaps)
1569 handle_fs_bad_blocks(ctx);
1571 /* We don't need the block_ea_map any more */
1572 if (ctx->block_ea_map) {
1573 ext2fs_free_block_bitmap(ctx->block_ea_map);
1574 ctx->block_ea_map = 0;
1577 if (ctx->flags & E2F_FLAG_RESIZE_INODE) {
1578 clear_problem_context(&pctx);
1579 pctx.errcode = ext2fs_create_resize_inode(fs);
1581 if (!fix_problem(ctx, PR_1_RESIZE_INODE_CREATE,
1583 ctx->flags |= E2F_FLAG_ABORT;
1588 if (!pctx.errcode) {
1589 e2fsck_read_inode(ctx, EXT2_RESIZE_INO, inode,
1591 inode->i_mtime = ctx->now;
1592 e2fsck_write_inode(ctx, EXT2_RESIZE_INO, inode,
1595 ctx->flags &= ~E2F_FLAG_RESIZE_INODE;
1598 if (ctx->flags & E2F_FLAG_RESTART) {
1600 * Only the master copy of the superblock and block
1601 * group descriptors are going to be written during a
1602 * restart, so set the superblock to be used to be the
1603 * master superblock.
1605 ctx->use_superblock = 0;
1610 if (ctx->block_dup_map) {
1611 if (ctx->options & E2F_OPT_PREEN) {
1612 clear_problem_context(&pctx);
1613 fix_problem(ctx, PR_1_DUP_BLOCKS_PREENSTOP, &pctx);
1615 e2fsck_pass1_dupblocks(ctx, block_buf);
1617 ext2fs_free_mem(&inodes_to_process);
1619 e2fsck_use_inode_shortcuts(ctx, 0);
1622 ext2fs_close_inode_scan(scan);
1624 ext2fs_free_mem(&block_buf);
1626 ext2fs_free_mem(&inode);
1628 if ((ctx->flags & E2F_FLAG_SIGNAL_MASK) == 0)
1629 print_resource_track(ctx, _("Pass 1"), &rtrack, ctx->fs->io);
1633 * When the inode_scan routines call this callback at the end of the
1634 * glock group, call process_inodes.
1636 static errcode_t scan_callback(ext2_filsys fs,
1637 ext2_inode_scan scan EXT2FS_ATTR((unused)),
1638 dgrp_t group, void * priv_data)
1640 struct scan_callback_struct *scan_struct;
1643 scan_struct = (struct scan_callback_struct *) priv_data;
1644 ctx = scan_struct->ctx;
1646 process_inodes((e2fsck_t) fs->priv_data, scan_struct->block_buf);
1649 if ((ctx->progress)(ctx, 1, group+1,
1650 ctx->fs->group_desc_count))
1651 return EXT2_ET_CANCEL_REQUESTED;
1657 * Process the inodes in the "inodes to process" list.
1659 static void process_inodes(e2fsck_t ctx, char *block_buf)
1662 struct ext2_inode *old_stashed_inode;
1663 ext2_ino_t old_stashed_ino;
1664 const char *old_operation;
1666 struct problem_context pctx;
1669 printf("begin process_inodes: ");
1671 if (process_inode_count == 0)
1673 old_operation = ehandler_operation(0);
1674 old_stashed_inode = ctx->stashed_inode;
1675 old_stashed_ino = ctx->stashed_ino;
1676 qsort(inodes_to_process, process_inode_count,
1677 sizeof(struct process_inode_block), process_inode_cmp);
1678 clear_problem_context(&pctx);
1679 for (i=0; i < process_inode_count; i++) {
1680 pctx.inode = ctx->stashed_inode = &inodes_to_process[i].inode;
1681 pctx.ino = ctx->stashed_ino = inodes_to_process[i].ino;
1684 printf("%u ", pctx.ino);
1686 sprintf(buf, _("reading indirect blocks of inode %u"),
1688 ehandler_operation(buf);
1689 check_blocks(ctx, &pctx, block_buf);
1690 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1693 ctx->stashed_inode = old_stashed_inode;
1694 ctx->stashed_ino = old_stashed_ino;
1695 process_inode_count = 0;
1697 printf("end process inodes\n");
1699 ehandler_operation(old_operation);
1702 static EXT2_QSORT_TYPE process_inode_cmp(const void *a, const void *b)
1704 const struct process_inode_block *ib_a =
1705 (const struct process_inode_block *) a;
1706 const struct process_inode_block *ib_b =
1707 (const struct process_inode_block *) b;
1710 ret = (ib_a->inode.i_block[EXT2_IND_BLOCK] -
1711 ib_b->inode.i_block[EXT2_IND_BLOCK]);
1714 * We only call process_inodes() for non-extent
1715 * inodes, so it's OK to pass NULL to
1716 * ext2fs_file_acl_block() here.
1718 ret = ext2fs_file_acl_block(0, &(ib_a->inode)) -
1719 ext2fs_file_acl_block(0, &(ib_b->inode));
1721 ret = ib_a->ino - ib_b->ino;
1726 * Mark an inode as being bad and increment its badness counter.
1728 void e2fsck_mark_inode_bad_loc(e2fsck_t ctx, ino_t ino, int count,
1729 const char *func, const int line)
1731 struct problem_context pctx;
1734 if (!ctx->inode_badness) {
1735 clear_problem_context(&pctx);
1737 pctx.errcode = ext2fs_create_icount2(ctx->fs, 0, 0, NULL,
1738 &ctx->inode_badness);
1740 fix_problem(ctx, PR_1_ALLOCATE_ICOUNT, &pctx);
1741 ctx->flags |= E2F_FLAG_ABORT;
1745 ext2fs_icount_fetch(ctx->inode_badness, ino, &result);
1746 ext2fs_icount_store(ctx->inode_badness, ino, count + result);
1748 if (ctx->options & E2F_OPT_DEBUG)
1749 fprintf(stderr, "%s:%d: increase inode %lu badness %u to %u\n",
1750 func, line, (unsigned long)ino, result, count + result);
1755 * This procedure will allocate the inode "bb" (badblock) map table
1757 static void alloc_bb_map(e2fsck_t ctx)
1759 struct problem_context pctx;
1761 clear_problem_context(&pctx);
1762 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1763 _("inode in bad block map"), EXT2FS_BMAP64_RBTREE,
1764 "inode_bb_map", &ctx->inode_bb_map);
1767 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1768 /* Should never get here */
1769 ctx->flags |= E2F_FLAG_ABORT;
1775 * This procedure will allocate the inode imagic table
1777 static void alloc_imagic_map(e2fsck_t ctx)
1779 struct problem_context pctx;
1781 clear_problem_context(&pctx);
1782 pctx.errcode = e2fsck_allocate_inode_bitmap(ctx->fs,
1783 _("imagic inode map"), EXT2FS_BMAP64_RBTREE,
1784 "inode_imagic_map", &ctx->inode_imagic_map);
1787 fix_problem(ctx, PR_1_ALLOCATE_IBITMAP_ERROR, &pctx);
1788 /* Should never get here */
1789 ctx->flags |= E2F_FLAG_ABORT;
1795 * Marks a block as in use, setting the dup_map if it's been set
1796 * already. Called by process_block and process_bad_block.
1798 * WARNING: Assumes checks have already been done to make sure block
1799 * is valid. This is true in both process_block and process_bad_block.
1801 static _INLINE_ void mark_block_used(e2fsck_t ctx, blk64_t block)
1803 struct problem_context pctx;
1805 clear_problem_context(&pctx);
1807 if (ext2fs_fast_test_block_bitmap2(ctx->block_found_map, block)) {
1808 if (!ctx->block_dup_map) {
1809 pctx.errcode = e2fsck_allocate_block_bitmap(ctx->fs,
1810 _("multiply claimed block map"),
1811 EXT2FS_BMAP64_RBTREE, "block_dup_map",
1812 &ctx->block_dup_map);
1815 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR,
1817 /* Should never get here */
1818 ctx->flags |= E2F_FLAG_ABORT;
1822 ext2fs_fast_mark_block_bitmap2(ctx->block_dup_map, block);
1824 ext2fs_fast_mark_block_bitmap2(ctx->block_found_map, block);
1828 static _INLINE_ void mark_blocks_used(e2fsck_t ctx, blk64_t block,
1831 if (ext2fs_test_block_bitmap_range2(ctx->block_found_map, block, num))
1832 ext2fs_mark_block_bitmap_range2(ctx->block_found_map, block, num);
1835 mark_block_used(ctx, block++);
1839 * Adjust the extended attribute block's reference counts at the end
1840 * of pass 1, either by subtracting out references for EA blocks that
1841 * are still referenced in ctx->refcount, or by adding references for
1842 * EA blocks that had extra references as accounted for in
1843 * ctx->refcount_extra.
1845 static void adjust_extattr_refcount(e2fsck_t ctx, ext2_refcount_t refcount,
1846 char *block_buf, int adjust_sign)
1848 struct ext2_ext_attr_header *header;
1849 struct problem_context pctx;
1850 ext2_filsys fs = ctx->fs;
1855 clear_problem_context(&pctx);
1857 ea_refcount_intr_begin(refcount);
1859 if ((blk = ea_refcount_intr_next(refcount, &count)) == 0)
1862 pctx.errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1863 /* We already checked this block, shouldn't happen */
1865 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1868 header = BHDR(block_buf);
1869 if (header->h_magic != EXT2_EXT_ATTR_MAGIC) {
1870 fix_problem(ctx, PR_1_EXTATTR_READ_ABORT, &pctx);
1874 pctx.blkcount = header->h_refcount;
1875 should_be = header->h_refcount + adjust_sign * count;
1876 pctx.num = should_be;
1877 if (fix_problem(ctx, PR_1_EXTATTR_REFCOUNT, &pctx)) {
1878 header->h_refcount = should_be;
1879 pctx.errcode = ext2fs_write_ext_attr2(fs, blk,
1882 fix_problem(ctx, PR_1_EXTATTR_WRITE_ABORT,
1891 * Handle processing the extended attribute blocks
1893 static int check_ext_attr(e2fsck_t ctx, struct problem_context *pctx,
1896 ext2_filsys fs = ctx->fs;
1897 ext2_ino_t ino = pctx->ino;
1898 struct ext2_inode *inode = pctx->inode;
1901 struct ext2_ext_attr_header *header;
1902 struct ext2_ext_attr_entry *entry;
1904 region_t region = 0;
1907 blk = ext2fs_file_acl_block(fs, inode);
1912 * If the Extended attribute flag isn't set, then a non-zero
1913 * file acl means that the inode is corrupted.
1915 * Or if the extended attribute block is an invalid block,
1916 * then the inode is also corrupted.
1918 if (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_EXT_ATTR) ||
1919 (blk < fs->super->s_first_data_block) ||
1920 (blk >= ext2fs_blocks_count(fs->super))) {
1921 /* Fixed in pass2, e2fsck_process_bad_inode(). */
1922 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
1926 /* If ea bitmap hasn't been allocated, create it */
1927 if (!ctx->block_ea_map) {
1928 pctx->errcode = e2fsck_allocate_block_bitmap(fs,
1929 _("ext attr block map"),
1930 EXT2FS_BMAP64_RBTREE, "block_ea_map",
1931 &ctx->block_ea_map);
1932 if (pctx->errcode) {
1934 fix_problem(ctx, PR_1_ALLOCATE_BBITMAP_ERROR, pctx);
1935 ctx->flags |= E2F_FLAG_ABORT;
1940 /* Create the EA refcount structure if necessary */
1941 if (!ctx->refcount) {
1942 pctx->errcode = ea_refcount_create(0, &ctx->refcount);
1943 if (pctx->errcode) {
1945 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1946 ctx->flags |= E2F_FLAG_ABORT;
1952 /* Debugging text */
1953 printf("Inode %u has EA block %u\n", ino, blk);
1956 /* Have we seen this EA block before? */
1957 if (ext2fs_fast_test_block_bitmap2(ctx->block_ea_map, blk)) {
1958 if (ea_refcount_decrement(ctx->refcount, blk, 0) == 0)
1960 /* Ooops, this EA was referenced more than it stated */
1961 if (!ctx->refcount_extra) {
1962 pctx->errcode = ea_refcount_create(0,
1963 &ctx->refcount_extra);
1964 if (pctx->errcode) {
1966 fix_problem(ctx, PR_1_ALLOCATE_REFCOUNT, pctx);
1967 ctx->flags |= E2F_FLAG_ABORT;
1971 ea_refcount_increment(ctx->refcount_extra, blk, 0);
1976 * OK, we haven't seen this EA block yet. So we need to
1980 pctx->errcode = ext2fs_read_ext_attr2(fs, blk, block_buf);
1981 if (pctx->errcode && fix_problem(ctx, PR_1_READ_EA_BLOCK, pctx))
1983 header = BHDR(block_buf);
1984 pctx->blk = ext2fs_file_acl_block(fs, inode);
1985 if (((ctx->ext_attr_ver == 1) &&
1986 (header->h_magic != EXT2_EXT_ATTR_MAGIC_v1)) ||
1987 ((ctx->ext_attr_ver == 2) &&
1988 (header->h_magic != EXT2_EXT_ATTR_MAGIC))) {
1989 if (fix_problem(ctx, PR_1_BAD_EA_BLOCK, pctx))
1993 if (header->h_blocks != 1) {
1994 if (fix_problem(ctx, PR_1_EA_MULTI_BLOCK, pctx))
1998 region = region_create(0, fs->blocksize);
2000 fix_problem(ctx, PR_1_EA_ALLOC_REGION_ABORT, pctx);
2001 ctx->flags |= E2F_FLAG_ABORT;
2004 if (region_allocate(region, 0, sizeof(struct ext2_ext_attr_header))) {
2005 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2009 entry = (struct ext2_ext_attr_entry *)(header+1);
2010 end = block_buf + fs->blocksize;
2011 while ((char *)entry < end && *(__u32 *)entry) {
2014 if (region_allocate(region, (char *)entry - (char *)header,
2015 EXT2_EXT_ATTR_LEN(entry->e_name_len))) {
2016 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2020 if ((ctx->ext_attr_ver == 1 &&
2021 (entry->e_name_len == 0 || entry->e_name_index != 0)) ||
2022 (ctx->ext_attr_ver == 2 &&
2023 entry->e_name_index == 0)) {
2024 if (fix_problem(ctx, PR_1_EA_BAD_NAME, pctx))
2028 if (entry->e_value_inum == 0) {
2029 if (entry->e_value_offs + entry->e_value_size >
2031 if (fix_problem(ctx, PR_1_EA_BAD_VALUE, pctx))
2035 if (entry->e_value_size &&
2036 region_allocate(region, entry->e_value_offs,
2037 EXT2_EXT_ATTR_SIZE(entry->e_value_size))) {
2038 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION,
2043 int i_file_acl_deleted = 0;
2045 ret = check_large_ea_inode(ctx, entry, pctx,
2046 &i_file_acl_deleted);
2048 mark_inode_ea_map(ctx, pctx,
2049 entry->e_value_inum);
2051 if (i_file_acl_deleted)
2055 hash = ext2fs_ext_attr_hash_entry(entry, block_buf +
2056 entry->e_value_offs);
2058 if (entry->e_hash != hash) {
2059 pctx->num = entry->e_hash;
2060 if (fix_problem(ctx, PR_1_ATTR_HASH, pctx))
2062 entry->e_hash = hash;
2065 entry = EXT2_EXT_ATTR_NEXT(entry);
2067 if (region_allocate(region, (char *)entry - (char *)header, 4)) {
2068 if (fix_problem(ctx, PR_1_EA_ALLOC_COLLISION, pctx))
2071 region_free(region);
2073 count = header->h_refcount - 1;
2075 ea_refcount_store(ctx->refcount, blk, count);
2076 mark_block_used(ctx, blk);
2077 ext2fs_fast_mark_block_bitmap2(ctx->block_ea_map, blk);
2082 region_free(region);
2083 ext2fs_file_acl_block_set(fs, inode, 0);
2084 e2fsck_write_inode(ctx, ino, inode, "check_ext_attr");
2088 /* Returns 1 if bad htree, 0 if OK */
2089 static int handle_htree(e2fsck_t ctx, struct problem_context *pctx,
2090 ext2_ino_t ino, struct ext2_inode *inode,
2093 struct ext2_dx_root_info *root;
2094 ext2_filsys fs = ctx->fs;
2098 if ((!LINUX_S_ISDIR(inode->i_mode) &&
2099 fix_problem(ctx, PR_1_HTREE_NODIR, pctx)) ||
2100 (!(fs->super->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX))) {
2101 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2102 if (fix_problem(ctx, PR_1_HTREE_SET, pctx))
2106 pctx->errcode = ext2fs_bmap2(fs, ino, inode, 0, 0, 0, 0, &blk);
2108 if ((pctx->errcode) ||
2110 (blk < fs->super->s_first_data_block) ||
2111 (blk >= ext2fs_blocks_count(fs->super))) {
2112 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2113 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2119 retval = io_channel_read_blk64(fs->io, blk, 1, block_buf);
2121 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2122 if (fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2126 /* XXX should check that beginning matches a directory */
2127 root = get_ext2_dx_root_info(fs, block_buf);
2129 if ((root->reserved_zero || root->info_length < 8) &&
2130 fix_problem(ctx, PR_1_HTREE_BADROOT, pctx))
2133 pctx->num = root->hash_version;
2134 if ((root->hash_version != EXT2_HASH_LEGACY) &&
2135 (root->hash_version != EXT2_HASH_HALF_MD4) &&
2136 (root->hash_version != EXT2_HASH_TEA) &&
2137 fix_problem(ctx, PR_1_HTREE_HASHV, pctx))
2140 if ((root->unused_flags & EXT2_HASH_FLAG_INCOMPAT) &&
2141 fix_problem(ctx, PR_1_HTREE_INCOMPAT, pctx))
2144 pctx->num = root->indirect_levels;
2145 if ((root->indirect_levels > 1) &&
2146 fix_problem(ctx, PR_1_HTREE_DEPTH, pctx))
2152 void e2fsck_clear_inode(e2fsck_t ctx, ext2_ino_t ino,
2153 struct ext2_inode *inode, int restart_flag,
2157 inode->i_links_count = 0;
2158 ext2fs_icount_store(ctx->inode_link_info, ino, 0);
2159 inode->i_dtime = ctx->now;
2161 ext2fs_unmark_inode_bitmap2(ctx->inode_dir_map, ino);
2162 ext2fs_unmark_inode_bitmap2(ctx->inode_used_map, ino);
2163 if (ctx->inode_reg_map)
2164 ext2fs_unmark_inode_bitmap2(ctx->inode_reg_map, ino);
2165 if (ctx->inode_badness)
2166 ext2fs_icount_store(ctx->inode_badness, ino, 0);
2169 * If the inode was partially accounted for before processing
2170 * was aborted, we need to restart the pass 1 scan.
2172 ctx->flags |= restart_flag;
2174 if (ino == EXT2_BAD_INO)
2175 memset(inode, 0, sizeof(struct ext2_inode));
2177 e2fsck_write_inode(ctx, ino, inode, source);
2181 * Use the multiple-blocks reclamation code to fix alignment problems in
2182 * a bigalloc filesystem. We want a logical cluster to map to *only* one
2183 * physical cluster, and we want the block offsets within that cluster to
2186 static int has_unaligned_cluster_map(e2fsck_t ctx,
2187 blk64_t last_pblk, e2_blkcnt_t last_lblk,
2188 blk64_t pblk, blk64_t lblk)
2190 blk64_t cluster_mask;
2192 if (!ctx->fs->cluster_ratio_bits)
2194 cluster_mask = EXT2FS_CLUSTER_MASK(ctx->fs);
2197 * If the block in the logical cluster doesn't align with the block in
2198 * the physical cluster...
2200 if ((lblk & cluster_mask) != (pblk & cluster_mask))
2204 * If we cross a physical cluster boundary within a logical cluster...
2206 if (last_pblk && (lblk & cluster_mask) != 0 &&
2207 EXT2FS_B2C(ctx->fs, lblk) == EXT2FS_B2C(ctx->fs, last_lblk) &&
2208 EXT2FS_B2C(ctx->fs, pblk) != EXT2FS_B2C(ctx->fs, last_pblk))
2214 static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx,
2215 struct process_block_struct *pb,
2216 blk64_t start_block, blk64_t end_block,
2218 ext2_extent_handle_t ehandle)
2220 struct ext2fs_extent extent;
2221 blk64_t blk, last_lblk;
2222 e2_blkcnt_t blockcnt;
2224 int is_dir, is_leaf;
2226 struct ext2_extent_info info;
2228 pctx->errcode = ext2fs_extent_get_info(ehandle, &info);
2232 pctx->errcode = ext2fs_extent_get(ehandle, EXT2_EXTENT_FIRST_SIB,
2234 while (!pctx->errcode && info.num_entries-- > 0) {
2235 is_leaf = extent.e_flags & EXT2_EXTENT_FLAGS_LEAF;
2236 is_dir = LINUX_S_ISDIR(pctx->inode->i_mode);
2237 last_lblk = extent.e_lblk + extent.e_len - 1;
2240 if (extent.e_pblk == 0 ||
2241 extent.e_pblk < ctx->fs->super->s_first_data_block ||
2242 extent.e_pblk >= ext2fs_blocks_count(ctx->fs->super))
2243 problem = PR_1_EXTENT_BAD_START_BLK;
2244 else if (extent.e_lblk < start_block)
2245 problem = PR_1_OUT_OF_ORDER_EXTENTS;
2246 else if ((end_block && last_lblk > end_block) &&
2247 (!(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT &&
2248 last_lblk > eof_block)))
2249 problem = PR_1_EXTENT_END_OUT_OF_BOUNDS;
2250 else if (is_leaf && extent.e_len == 0)
2251 problem = PR_1_EXTENT_LENGTH_ZERO;
2253 (extent.e_pblk + extent.e_len) >
2254 ext2fs_blocks_count(ctx->fs->super))
2255 problem = PR_1_EXTENT_ENDS_BEYOND;
2256 else if (is_leaf && is_dir &&
2257 ((extent.e_lblk + extent.e_len) >
2258 (1 << (21 - ctx->fs->super->s_log_block_size))))
2259 problem = PR_1_TOOBIG_DIR;
2262 * Uninitialized blocks in a directory? Clear the flag and
2263 * we'll interpret the blocks later.
2265 if (is_dir && problem == 0 &&
2266 (extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT) &&
2267 fix_problem(ctx, PR_1_UNINIT_DBLOCK, pctx)) {
2268 extent.e_flags &= ~EXT2_EXTENT_FLAGS_UNINIT;
2269 pb->inode_modified = 1;
2270 pctx->errcode = ext2fs_extent_replace(ehandle, 0,
2277 /* To ensure that extent is in inode */
2278 if (info.curr_level == 0)
2279 e2fsck_mark_inode_bad(ctx, pctx->ino,
2282 pctx->blk = extent.e_pblk;
2283 pctx->blk2 = extent.e_lblk;
2284 pctx->num = extent.e_len;
2285 pctx->blkcount = extent.e_lblk + extent.e_len;
2286 if (fix_problem(ctx, problem, pctx)) {
2287 if (ctx->invalid_bitmaps) {
2289 * If fsck knows the bitmaps are bad,
2290 * skip to the next extent and
2291 * try to clear this extent again
2292 * after fixing the bitmaps, by
2295 pctx->errcode = ext2fs_extent_get(
2297 EXT2_EXTENT_NEXT_SIB,
2299 ctx->flags |= E2F_FLAG_RESTART_LATER;
2300 if (pctx->errcode ==
2301 EXT2_ET_NO_CURRENT_NODE) {
2307 e2fsck_read_bitmaps(ctx);
2308 pb->inode_modified = 1;
2310 ext2fs_extent_delete(ehandle, 0);
2311 if (pctx->errcode) {
2312 pctx->str = "ext2fs_extent_delete";
2315 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2316 if (pctx->errcode &&
2317 pctx->errcode != EXT2_ET_NO_CURRENT_NODE) {
2318 pctx->str = "ext2fs_extent_fix_parents";
2321 pctx->errcode = ext2fs_extent_get(ehandle,
2322 EXT2_EXTENT_CURRENT,
2324 if (pctx->errcode == EXT2_ET_NO_CURRENT_NODE) {
2334 blk64_t lblk = extent.e_lblk;
2336 blk = extent.e_pblk;
2337 pctx->errcode = ext2fs_extent_get(ehandle,
2338 EXT2_EXTENT_DOWN, &extent);
2339 if (pctx->errcode) {
2340 pctx->str = "EXT2_EXTENT_DOWN";
2341 problem = PR_1_EXTENT_HEADER_INVALID;
2342 if (pctx->errcode == EXT2_ET_EXTENT_HEADER_BAD)
2343 goto report_problem;
2346 /* The next extent should match this index's logical start */
2347 if (extent.e_lblk != lblk) {
2348 struct ext2_extent_info e_info;
2350 ext2fs_extent_get_info(ehandle, &e_info);
2352 pctx->blk2 = extent.e_lblk;
2353 pctx->num = e_info.curr_level - 1;
2354 problem = PR_1_EXTENT_INDEX_START_INVALID;
2355 if (fix_problem(ctx, problem, pctx)) {
2356 pb->inode_modified = 1;
2358 ext2fs_extent_fix_parents(ehandle);
2359 if (pctx->errcode) {
2360 pctx->str = "ext2fs_extent_fix_parents";
2365 scan_extent_node(ctx, pctx, pb, extent.e_lblk,
2366 last_lblk, eof_block, ehandle);
2369 pctx->errcode = ext2fs_extent_get(ehandle,
2370 EXT2_EXTENT_UP, &extent);
2371 if (pctx->errcode) {
2372 pctx->str = "EXT2_EXTENT_UP";
2375 mark_block_used(ctx, blk);
2380 if ((pb->previous_block != 0) &&
2381 (pb->previous_block+1 != extent.e_pblk)) {
2382 if (ctx->options & E2F_OPT_FRAGCHECK) {
2387 else if (pb->is_reg)
2390 printf(("%6lu(%c): expecting %6lu "
2392 "phys %6lu log %lu len %lu\n"),
2393 (unsigned long) pctx->ino, type,
2394 (unsigned long) pb->previous_block+1,
2395 (unsigned long) extent.e_pblk,
2396 (unsigned long) extent.e_lblk,
2397 (unsigned long) extent.e_len);
2402 * If we notice a gap in the logical block mappings of an
2403 * extent-mapped directory, offer to close the hole by
2404 * moving the logical block down, otherwise we'll go mad in
2405 * pass 3 allocating empty directory blocks to fill the hole.
2408 pb->last_block + 1 < (e2_blkcnt_t)extent.e_lblk) {
2411 new_lblk = pb->last_block + 1;
2412 if (EXT2FS_CLUSTER_RATIO(ctx->fs) > 1)
2413 new_lblk = ((new_lblk +
2414 EXT2FS_CLUSTER_RATIO(ctx->fs)) &
2415 EXT2FS_CLUSTER_MASK(ctx->fs)) |
2417 EXT2FS_CLUSTER_MASK(ctx->fs));
2418 pctx->blk = extent.e_lblk;
2419 pctx->blk2 = new_lblk;
2420 if (fix_problem(ctx, PR_1_COLLAPSE_DBLOCK, pctx)) {
2421 extent.e_lblk = new_lblk;
2422 pb->inode_modified = 1;
2423 pctx->errcode = ext2fs_extent_replace(ehandle,
2425 if (pctx->errcode) {
2429 pctx->errcode = ext2fs_extent_fix_parents(ehandle);
2431 goto failed_add_dir_block;
2432 pctx->errcode = ext2fs_extent_goto(ehandle,
2435 goto failed_add_dir_block;
2436 last_lblk = extent.e_lblk + extent.e_len - 1;
2440 while (is_dir && (++pb->last_db_block <
2441 (e2_blkcnt_t) extent.e_lblk)) {
2442 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist,
2445 if (pctx->errcode) {
2447 pctx->num = pb->last_db_block;
2448 goto failed_add_dir_block;
2451 if (!ctx->fs->cluster_ratio_bits) {
2452 mark_blocks_used(ctx, extent.e_pblk, extent.e_len);
2453 pb->num_blocks += extent.e_len;
2455 for (blk = extent.e_pblk, blockcnt = extent.e_lblk, i = 0;
2457 blk++, blockcnt++, i++) {
2458 if (ctx->fs->cluster_ratio_bits &&
2459 !(pb->previous_block &&
2460 (EXT2FS_B2C(ctx->fs, blk) ==
2461 EXT2FS_B2C(ctx->fs, pb->previous_block)) &&
2462 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2463 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2464 mark_block_used(ctx, blk);
2467 if (has_unaligned_cluster_map(ctx, pb->previous_block,
2468 pb->last_block, blk,
2470 pctx->blk = blockcnt;
2472 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2473 mark_block_used(ctx, blk);
2474 mark_block_used(ctx, blk);
2476 pb->last_block = blockcnt;
2477 pb->previous_block = blk;
2480 pctx->errcode = ext2fs_add_dir_block2(ctx->fs->dblist, pctx->ino, blk, blockcnt);
2481 if (pctx->errcode) {
2483 pctx->num = blockcnt;
2484 failed_add_dir_block:
2485 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
2486 /* Should never get here */
2487 ctx->flags |= E2F_FLAG_ABORT;
2492 if (is_dir && extent.e_len > 0)
2493 pb->last_db_block = blockcnt - 1;
2494 pb->previous_block = extent.e_pblk + extent.e_len - 1;
2495 start_block = pb->last_block = last_lblk;
2496 if (is_leaf && !is_dir &&
2497 !(extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT))
2498 pb->last_init_lblock = last_lblk;
2500 pctx->errcode = ext2fs_extent_get(ehandle,
2501 EXT2_EXTENT_NEXT_SIB,
2504 if (pctx->errcode == EXT2_ET_EXTENT_NO_NEXT)
2508 static void check_blocks_extents(e2fsck_t ctx, struct problem_context *pctx,
2509 struct process_block_struct *pb)
2511 struct ext2_extent_info info;
2512 struct ext2_inode *inode = pctx->inode;
2513 ext2_extent_handle_t ehandle;
2514 ext2_filsys fs = ctx->fs;
2515 ext2_ino_t ino = pctx->ino;
2519 pctx->errcode = ext2fs_extent_open2(fs, ino, inode, &ehandle);
2520 if (pctx->errcode) {
2521 if (fix_problem(ctx, PR_1_READ_EXTENT, pctx))
2522 e2fsck_clear_inode(ctx, ino, inode, 0,
2523 "check_blocks_extents");
2528 retval = ext2fs_extent_get_info(ehandle, &info);
2530 if (info.max_depth >= MAX_EXTENT_DEPTH_COUNT)
2531 info.max_depth = MAX_EXTENT_DEPTH_COUNT-1;
2532 ctx->extent_depth_count[info.max_depth]++;
2535 eof_lblk = ((EXT2_I_SIZE(inode) + fs->blocksize - 1) >>
2536 EXT2_BLOCK_SIZE_BITS(fs->super)) - 1;
2537 scan_extent_node(ctx, pctx, pb, 0, 0, eof_lblk, ehandle);
2538 if (pctx->errcode &&
2539 fix_problem(ctx, PR_1_EXTENT_ITERATE_FAILURE, pctx)) {
2541 inode->i_blocks = 0;
2542 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2543 "check_blocks_extents");
2546 ext2fs_extent_free(ehandle);
2550 * This subroutine is called on each inode to account for all of the
2551 * blocks used by that inode.
2553 static void check_blocks(e2fsck_t ctx, struct problem_context *pctx,
2556 ext2_filsys fs = ctx->fs;
2557 struct process_block_struct pb;
2558 ext2_ino_t ino = pctx->ino;
2559 struct ext2_inode *inode = pctx->inode;
2560 unsigned bad_size = 0;
2561 int dirty_inode = 0;
2568 pb.last_init_lblock = -1;
2569 pb.last_db_block = -1;
2570 pb.num_illegal_blocks = 0;
2571 pb.suppress = 0; pb.clear = 0;
2574 pb.previous_block = 0;
2575 pb.is_dir = LINUX_S_ISDIR(inode->i_mode);
2576 pb.is_reg = LINUX_S_ISREG(inode->i_mode);
2577 pb.max_blocks = 1 << (31 - fs->super->s_log_block_size);
2581 pb.inode_modified = 0;
2585 extent_fs = (ctx->fs->super->s_feature_incompat &
2586 EXT3_FEATURE_INCOMPAT_EXTENTS);
2588 if (inode->i_flags & EXT2_COMPRBLK_FL) {
2589 if (fs->super->s_feature_incompat &
2590 EXT2_FEATURE_INCOMPAT_COMPRESSION)
2593 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2594 if (fix_problem(ctx, PR_1_COMPR_SET, pctx)) {
2595 inode->i_flags &= ~EXT2_COMPRBLK_FL;
2601 if (ext2fs_file_acl_block(fs, inode) &&
2602 check_ext_attr(ctx, pctx, block_buf)) {
2603 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2608 if (ext2fs_inode_has_valid_blocks2(fs, inode)) {
2609 if (extent_fs && (inode->i_flags & EXT4_EXTENTS_FL))
2610 check_blocks_extents(ctx, pctx, &pb);
2613 * If we've modified the inode, write it out before
2614 * iterate() tries to use it.
2617 e2fsck_write_inode(ctx, ino, inode,
2621 pctx->errcode = ext2fs_block_iterate3(fs, ino,
2622 pb.is_dir ? BLOCK_FLAG_HOLE : 0,
2623 block_buf, process_block, &pb);
2625 * We do not have uninitialized extents in non extent
2628 pb.last_init_lblock = pb.last_block;
2630 * If iterate() changed a block mapping, we have to
2631 * re-read the inode. If we decide to clear the
2632 * inode after clearing some stuff, we'll re-write the
2633 * bad mappings into the inode!
2635 if (pb.inode_modified)
2636 e2fsck_read_inode(ctx, ino, inode,
2640 end_problem_latch(ctx, PR_LATCH_BLOCK);
2641 end_problem_latch(ctx, PR_LATCH_TOOBIG);
2642 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
2645 fix_problem(ctx, PR_1_BLOCK_ITERATE, pctx);
2647 if (pb.fragmented && pb.num_blocks < fs->super->s_blocks_per_group) {
2648 if (LINUX_S_ISDIR(inode->i_mode))
2649 ctx->fs_fragmented_dir++;
2651 ctx->fs_fragmented++;
2655 e2fsck_clear_inode(ctx, ino, inode, E2F_FLAG_RESTART,
2660 if (inode->i_flags & EXT2_INDEX_FL) {
2661 if (handle_htree(ctx, pctx, ino, inode, block_buf)) {
2662 inode->i_flags &= ~EXT2_INDEX_FL;
2666 e2fsck_add_dx_dir(ctx, ino, pb.last_block+1);
2671 if (!pb.num_blocks && pb.is_dir) {
2673 * The mode might be in-correct. Increasing the badness by
2674 * small amount won't hurt much.
2676 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2677 if (fix_problem(ctx, PR_1_ZERO_LENGTH_DIR, pctx)) {
2678 e2fsck_clear_inode(ctx, ino, inode, 0, "check_blocks");
2679 ctx->fs_directory_count--;
2684 if (ino == EXT2_ROOT_INO || ino >= EXT2_FIRST_INODE(ctx->fs->super)) {
2685 quota_data_add(ctx->qctx, inode, ino,
2686 pb.num_blocks * fs->blocksize);
2687 quota_data_inodes(ctx->qctx, inode, ino, +1);
2690 if (!(fs->super->s_feature_ro_compat &
2691 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
2692 !(inode->i_flags & EXT4_HUGE_FILE_FL))
2693 pb.num_blocks *= (fs->blocksize / 512);
2694 pb.num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
2696 printf("inode %u, i_size = %u, last_block = %lld, i_blocks=%llu, num_blocks = %llu\n",
2697 ino, inode->i_size, pb.last_block, ext2fs_inode_i_blocks(fs, inode),
2701 int nblock = inode->i_size >> EXT2_BLOCK_SIZE_BITS(fs->super);
2702 if (inode->i_size & (fs->blocksize - 1))
2704 else if (nblock > (pb.last_block + 1))
2706 else if (nblock < (pb.last_block + 1)) {
2707 if (((pb.last_block + 1) - nblock) >
2708 fs->super->s_prealloc_dir_blocks)
2712 e2_blkcnt_t blkpg = ctx->blocks_per_page;
2714 size = EXT2_I_SIZE(inode);
2715 if ((pb.last_init_lblock >= 0) &&
2716 /* if size is smaller than expected by the block count,
2717 * allow allocated blocks to end of PAGE_SIZE.
2718 * last_init_lblock is the last in-use block, so it is
2719 * the minimum expected file size. */
2720 (size < (__u64)pb.last_init_lblock * fs->blocksize) &&
2721 ((pb.last_init_lblock + 1) / blkpg * blkpg !=
2722 (pb.last_init_lblock + 1) ||
2723 size < (__u64)(pb.last_init_lblock & ~(blkpg - 1)) *
2726 else if (!(extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2727 size > ext2_max_sizes[fs->super->s_log_block_size])
2728 /* too big for a direct/indirect-mapped file */
2730 else if ((extent_fs && (inode->i_flags & EXT4_EXTENTS_FL)) &&
2732 ((1ULL << (32 + EXT2_BLOCK_SIZE_BITS(fs->super))) - 1))
2733 /* too big for an extent-based file - 32bit ee_block */
2736 /* i_size for symlinks is checked elsewhere */
2737 if (bad_size && !LINUX_S_ISLNK(inode->i_mode)) {
2738 pctx->num = (pb.last_block+1) * fs->blocksize;
2739 pctx->group = bad_size;
2740 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2741 if (fix_problem(ctx, PR_1_BAD_I_SIZE, pctx)) {
2742 if (LINUX_S_ISDIR(inode->i_mode))
2743 pctx->num &= 0xFFFFFFFFULL;
2744 ext2fs_inode_size_set(fs, inode, pctx->num);
2749 if (LINUX_S_ISREG(inode->i_mode) &&
2750 ext2fs_needs_large_file_feature(EXT2_I_SIZE(inode)))
2752 if ((fs->super->s_creator_os == EXT2_OS_LINUX) &&
2753 ((pb.num_blocks != ext2fs_inode_i_blocks(fs, inode)) ||
2754 ((fs->super->s_feature_ro_compat &
2755 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) &&
2756 (inode->i_flags & EXT4_HUGE_FILE_FL) &&
2757 (inode->osd2.linux2.l_i_blocks_hi != 0)))) {
2758 pctx->num = pb.num_blocks;
2759 e2fsck_mark_inode_bad(ctx, ino, BADNESS_NORMAL);
2760 if (fix_problem(ctx, PR_1_BAD_I_BLOCKS, pctx)) {
2761 inode->i_blocks = pb.num_blocks;
2762 inode->osd2.linux2.l_i_blocks_hi = pb.num_blocks >> 32;
2768 if (ctx->dirs_to_hash && pb.is_dir &&
2769 !(inode->i_flags & EXT2_INDEX_FL) &&
2770 ((inode->i_size / fs->blocksize) >= 3))
2771 ext2fs_u32_list_add(ctx->dirs_to_hash, ino);
2775 e2fsck_write_inode(ctx, ino, inode, "check_blocks");
2780 * Helper function called by process block when an illegal block is
2781 * found. It returns a description about why the block is illegal
2783 static char *describe_illegal_block(ext2_filsys fs, blk64_t block)
2787 static char problem[80];
2789 super = fs->super->s_first_data_block;
2790 strcpy(problem, "PROGRAMMING ERROR: Unknown reason for illegal block");
2791 if (block < super) {
2792 sprintf(problem, "< FIRSTBLOCK (%u)", super);
2794 } else if (block >= ext2fs_blocks_count(fs->super)) {
2795 sprintf(problem, "> BLOCKS (%u)", ext2fs_blocks_count(fs->super));
2798 for (i = 0; i < fs->group_desc_count; i++) {
2799 if (block == super) {
2800 sprintf(problem, "is the superblock in group %d", i);
2803 if (block > super &&
2804 block <= (super + fs->desc_blocks)) {
2805 sprintf(problem, "is in the group descriptors "
2809 if (block == ext2fs_block_bitmap_loc(fs, i)) {
2810 sprintf(problem, "is the block bitmap of group %d", i);
2813 if (block == ext2fs_inode_bitmap_loc(fs, i)) {
2814 sprintf(problem, "is the inode bitmap of group %d", i);
2817 if (block >= ext2fs_inode_table_loc(fs, i) &&
2818 (block < ext2fs_inode_table_loc(fs, i)
2819 + fs->inode_blocks_per_group)) {
2820 sprintf(problem, "is in the inode table of group %d",
2824 super += fs->super->s_blocks_per_group;
2831 * This is a helper function for check_blocks().
2833 static int process_block(ext2_filsys fs,
2835 e2_blkcnt_t blockcnt,
2836 blk64_t ref_block EXT2FS_ATTR((unused)),
2837 int ref_offset EXT2FS_ATTR((unused)),
2840 struct process_block_struct *p;
2841 struct problem_context *pctx;
2842 blk64_t blk = *block_nr;
2844 problem_t problem = 0;
2847 p = (struct process_block_struct *) priv_data;
2851 if (p->compressed && (blk == EXT2FS_COMPRESSED_BLKADDR)) {
2852 /* todo: Check that the comprblk_fl is high, that the
2853 blkaddr pattern looks right (all non-holes up to
2854 first EXT2FS_COMPRESSED_BLKADDR, then all
2855 EXT2FS_COMPRESSED_BLKADDR up to end of cluster),
2856 that the feature_incompat bit is high, and that the
2857 inode is a regular file. If we're doing a "full
2858 check" (a concept introduced to e2fsck by e2compr,
2859 meaning that we look at data blocks as well as
2860 metadata) then call some library routine that
2861 checks the compressed data. I'll have to think
2862 about this, because one particularly important
2863 problem to be able to fix is to recalculate the
2864 cluster size if necessary. I think that perhaps
2865 we'd better do most/all e2compr-specific checks
2866 separately, after the non-e2compr checks. If not
2867 doing a full check, it may be useful to test that
2868 the personality is linux; e.g. if it isn't then
2869 perhaps this really is just an illegal block. */
2874 * For a directory, add logical block zero for processing even if it's
2875 * not mapped or we'll be perennially stuck with broken "." and ".."
2878 if (p->is_dir && blockcnt == 0 && blk == 0) {
2879 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino, 0, 0);
2880 if (pctx->errcode) {
2882 pctx->num = blockcnt;
2883 goto failed_add_dir_block;
2892 printf("Process_block, inode %lu, block %u, #%d\n", p->ino, blk,
2897 * Simplistic fragmentation check. We merely require that the
2898 * file be contiguous. (Which can never be true for really
2899 * big files that are greater than a block group.)
2901 if (!HOLE_BLKADDR(p->previous_block) && p->ino != EXT2_RESIZE_INO) {
2902 if (p->previous_block+1 != blk) {
2903 if (ctx->options & E2F_OPT_FRAGCHECK) {
2911 printf(_("%6lu(%c): expecting %6lu "
2912 "got phys %6lu (blkcnt %lld)\n"),
2913 (unsigned long) pctx->ino, type,
2914 (unsigned long) p->previous_block+1,
2915 (unsigned long) blk,
2922 if (p->is_dir && blockcnt > (1 << (21 - fs->super->s_log_block_size)))
2923 problem = PR_1_TOOBIG_DIR;
2924 if (p->is_reg && p->num_blocks+1 >= p->max_blocks)
2925 problem = PR_1_TOOBIG_REG;
2926 if (!p->is_dir && !p->is_reg && blockcnt > 0)
2927 problem = PR_1_TOOBIG_SYMLINK;
2929 if (blk < fs->super->s_first_data_block ||
2930 blk >= ext2fs_blocks_count(fs->super)) {
2931 problem = PR_1_ILLEGAL_BLOCK_NUM;
2932 e2fsck_mark_inode_bad(ctx, pctx->ino, BADNESS_NORMAL);
2936 p->num_illegal_blocks++;
2937 if (!p->suppress && (p->num_illegal_blocks % 12) == 0) {
2938 if (fix_problem(ctx, PR_1_TOO_MANY_BAD_BLOCKS, pctx)) {
2942 if (fix_problem(ctx, PR_1_SUPPRESS_MESSAGES, pctx)) {
2944 set_latch_flags(PR_LATCH_BLOCK,
2949 pctx->blkcount = blockcnt;
2950 if (fix_problem(ctx, problem, pctx)) {
2951 blk = *block_nr = 0;
2952 ret_code = BLOCK_CHANGED;
2953 p->inode_modified = 1;
2955 * If the directory block is too big and is beyond the
2956 * end of the FS, don't bother trying to add it for
2957 * processing -- the kernel would never have created a
2958 * directory this large, and we risk an ENOMEM abort.
2959 * In any case, the toobig handler for extent-based
2960 * directories also doesn't feed toobig blocks to
2963 if (problem == PR_1_TOOBIG_DIR)
2970 if (p->ino == EXT2_RESIZE_INO) {
2972 * The resize inode has already be sanity checked
2973 * during pass #0 (the superblock checks). All we
2974 * have to do is mark the double indirect block as
2975 * being in use; all of the other blocks are handled
2976 * by mark_table_blocks()).
2978 if (blockcnt == BLOCK_COUNT_DIND)
2979 mark_block_used(ctx, blk);
2981 } else if (!(ctx->fs->cluster_ratio_bits &&
2982 p->previous_block &&
2983 (EXT2FS_B2C(ctx->fs, blk) ==
2984 EXT2FS_B2C(ctx->fs, p->previous_block)) &&
2985 (blk & EXT2FS_CLUSTER_MASK(ctx->fs)) ==
2986 ((unsigned) blockcnt & EXT2FS_CLUSTER_MASK(ctx->fs)))) {
2987 mark_block_used(ctx, blk);
2989 } else if (has_unaligned_cluster_map(ctx, p->previous_block,
2990 p->last_block, blk, blockcnt)) {
2991 pctx->blk = blockcnt;
2993 fix_problem(ctx, PR_1_MISALIGNED_CLUSTER, pctx);
2994 mark_block_used(ctx, blk);
2995 mark_block_used(ctx, blk);
2998 p->last_block = blockcnt;
2999 p->previous_block = blk;
3001 if (p->is_dir && (blockcnt >= 0)) {
3002 while (++p->last_db_block < blockcnt) {
3003 pctx->errcode = ext2fs_add_dir_block2(fs->dblist,
3006 if (pctx->errcode) {
3008 pctx->num = p->last_db_block;
3009 goto failed_add_dir_block;
3012 pctx->errcode = ext2fs_add_dir_block2(fs->dblist, p->ino,
3014 if (pctx->errcode) {
3016 pctx->num = blockcnt;
3017 failed_add_dir_block:
3018 fix_problem(ctx, PR_1_ADD_DBLOCK, pctx);
3019 /* Should never get here */
3020 ctx->flags |= E2F_FLAG_ABORT;
3027 static int process_bad_block(ext2_filsys fs,
3029 e2_blkcnt_t blockcnt,
3030 blk64_t ref_block EXT2FS_ATTR((unused)),
3031 int ref_offset EXT2FS_ATTR((unused)),
3034 struct process_block_struct *p;
3035 blk64_t blk = *block_nr;
3036 blk64_t first_block;
3038 struct problem_context *pctx;
3042 * Note: This function processes blocks for the bad blocks
3043 * inode, which is never compressed. So we don't use HOLE_BLKADDR().
3049 p = (struct process_block_struct *) priv_data;
3053 pctx->ino = EXT2_BAD_INO;
3055 pctx->blkcount = blockcnt;
3057 if ((blk < fs->super->s_first_data_block) ||
3058 (blk >= ext2fs_blocks_count(fs->super))) {
3059 if (fix_problem(ctx, PR_1_BB_ILLEGAL_BLOCK_NUM, pctx)) {
3061 return BLOCK_CHANGED;
3067 if (ext2fs_test_block_bitmap2(p->fs_meta_blocks, blk)) {
3069 if (fix_problem(ctx, PR_1_BB_FS_BLOCK, pctx)) {
3071 return BLOCK_CHANGED;
3073 } else if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3076 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK,
3079 return BLOCK_CHANGED;
3081 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3084 mark_block_used(ctx, blk);
3088 printf ("DEBUG: Marking %u as bad.\n", blk);
3090 ctx->fs_badblocks_count++;
3092 * If the block is not used, then mark it as used and return.
3093 * If it is already marked as found, this must mean that
3094 * there's an overlap between the filesystem table blocks
3095 * (bitmaps and inode table) and the bad block list.
3097 if (!ext2fs_test_block_bitmap2(ctx->block_found_map, blk)) {
3098 ext2fs_mark_block_bitmap2(ctx->block_found_map, blk);
3102 * Try to find the where the filesystem block was used...
3104 first_block = fs->super->s_first_data_block;
3106 for (i = 0; i < fs->group_desc_count; i++ ) {
3109 if (!ext2fs_bg_has_super(fs, i))
3111 if (blk == first_block) {
3113 if (fix_problem(ctx,
3114 PR_1_BAD_PRIMARY_SUPERBLOCK,
3117 return BLOCK_CHANGED;
3121 fix_problem(ctx, PR_1_BAD_SUPERBLOCK, pctx);
3124 if ((blk > first_block) &&
3125 (blk <= first_block + fs->desc_blocks)) {
3127 pctx->blk = *block_nr;
3128 if (fix_problem(ctx,
3129 PR_1_BAD_PRIMARY_GROUP_DESCRIPTOR, pctx)) {
3131 return BLOCK_CHANGED;
3135 fix_problem(ctx, PR_1_BAD_GROUP_DESCRIPTORS, pctx);
3139 if (blk == ext2fs_block_bitmap_loc(fs, i)) {
3140 if (fix_problem(ctx, PR_1_BB_BAD_BLOCK, pctx)) {
3141 ctx->invalid_block_bitmap_flag[i]++;
3142 ctx->invalid_bitmaps++;
3146 if (blk == ext2fs_inode_bitmap_loc(fs, i)) {
3147 if (fix_problem(ctx, PR_1_IB_BAD_BLOCK, pctx)) {
3148 ctx->invalid_inode_bitmap_flag[i]++;
3149 ctx->invalid_bitmaps++;
3153 if ((blk >= ext2fs_inode_table_loc(fs, i)) &&
3154 (blk < (ext2fs_inode_table_loc(fs, i) +
3155 fs->inode_blocks_per_group))) {
3157 * If there are bad blocks in the inode table,
3158 * the inode scan code will try to do
3159 * something reasonable automatically.
3163 first_block += fs->super->s_blocks_per_group;
3166 * If we've gotten to this point, then the only
3167 * possibility is that the bad block inode meta data
3168 * is using a bad block.
3170 if ((blk == p->inode->i_block[EXT2_IND_BLOCK]) ||
3171 (blk == p->inode->i_block[EXT2_DIND_BLOCK]) ||
3172 (blk == p->inode->i_block[EXT2_TIND_BLOCK])) {
3174 if (fix_problem(ctx, PR_1_BBINODE_BAD_METABLOCK, pctx)) {
3176 return BLOCK_CHANGED;
3178 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
3185 /* Warn user that the block wasn't claimed */
3186 fix_problem(ctx, PR_1_PROGERR_CLAIMED_BLOCK, pctx);
3191 static void new_table_block(e2fsck_t ctx, blk64_t first_block, dgrp_t group,
3192 const char *name, int num, blk64_t *new_block)
3194 ext2_filsys fs = ctx->fs;
3196 blk64_t old_block = *new_block;
3199 unsigned flexbg_size;
3202 struct problem_context pctx;
3204 clear_problem_context(&pctx);
3207 pctx.blk = old_block;
3211 * For flex_bg filesystems, first try to allocate the metadata
3212 * within the flex_bg, and if that fails then try finding the
3213 * space anywhere in the filesystem.
3215 is_flexbg = EXT2_HAS_INCOMPAT_FEATURE(fs->super,
3216 EXT4_FEATURE_INCOMPAT_FLEX_BG);
3218 flexbg_size = 1 << fs->super->s_log_groups_per_flex;
3219 flexbg = group / flexbg_size;
3220 first_block = ext2fs_group_first_block2(fs,
3221 flexbg_size * flexbg);
3222 last_grp = group | (flexbg_size - 1);
3223 if (last_grp >= fs->group_desc_count)
3224 last_grp = fs->group_desc_count - 1;
3225 last_block = ext2fs_group_last_block2(fs, last_grp);
3227 last_block = ext2fs_group_last_block2(fs, group);
3228 pctx.errcode = ext2fs_get_free_blocks2(fs, first_block, last_block,
3229 num, ctx->block_found_map,
3231 if (is_flexbg && (pctx.errcode == EXT2_ET_BLOCK_ALLOC_FAIL))
3232 pctx.errcode = ext2fs_get_free_blocks2(fs,
3233 fs->super->s_first_data_block,
3234 ext2fs_blocks_count(fs->super),
3235 num, ctx->block_found_map, new_block);
3238 fix_problem(ctx, PR_1_RELOC_BLOCK_ALLOCATE, &pctx);
3239 ext2fs_unmark_valid(fs);
3240 ctx->flags |= E2F_FLAG_ABORT;
3243 pctx.errcode = ext2fs_get_mem(fs->blocksize, &buf);
3245 fix_problem(ctx, PR_1_RELOC_MEMORY_ALLOCATE, &pctx);
3246 ext2fs_unmark_valid(fs);
3247 ctx->flags |= E2F_FLAG_ABORT;
3250 ext2fs_mark_super_dirty(fs);
3251 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
3252 pctx.blk2 = *new_block;
3253 fix_problem(ctx, (old_block ? PR_1_RELOC_FROM_TO :
3254 PR_1_RELOC_TO), &pctx);
3256 for (i = 0; i < num; i++) {
3258 ext2fs_mark_block_bitmap2(ctx->block_found_map, (*new_block)+i);
3260 pctx.errcode = io_channel_read_blk64(fs->io,
3261 old_block + i, 1, buf);
3263 fix_problem(ctx, PR_1_RELOC_READ_ERR, &pctx);
3265 memset(buf, 0, fs->blocksize);
3267 pctx.blk = (*new_block) + i;
3268 pctx.errcode = io_channel_write_blk64(fs->io, pctx.blk,
3271 fix_problem(ctx, PR_1_RELOC_WRITE_ERR, &pctx);
3273 ext2fs_free_mem(&buf);
3277 * This routine gets called at the end of pass 1 if bad blocks are
3278 * detected in the superblock, group descriptors, inode_bitmaps, or
3279 * block bitmaps. At this point, all of the blocks have been mapped
3280 * out, so we can try to allocate new block(s) to replace the bad
3283 static void handle_fs_bad_blocks(e2fsck_t ctx)
3285 ext2_filsys fs = ctx->fs;
3287 blk64_t first_block;
3290 for (i = 0; i < fs->group_desc_count; i++) {
3291 first_block = ext2fs_group_first_block2(fs, i);
3293 if (ctx->invalid_block_bitmap_flag[i]) {
3294 new_blk = ext2fs_block_bitmap_loc(fs, i);
3295 new_table_block(ctx, first_block, i, _("block bitmap"),
3297 ext2fs_block_bitmap_loc_set(fs, i, new_blk);
3299 if (ctx->invalid_inode_bitmap_flag[i]) {
3300 new_blk = ext2fs_inode_bitmap_loc(fs, i);
3301 new_table_block(ctx, first_block, i, _("inode bitmap"),
3303 ext2fs_inode_bitmap_loc_set(fs, i, new_blk);
3305 if (ctx->invalid_inode_table_flag[i]) {
3306 new_blk = ext2fs_inode_table_loc(fs, i);
3307 new_table_block(ctx, first_block, i, _("inode table"),
3308 fs->inode_blocks_per_group,
3310 ext2fs_inode_table_loc_set(fs, i, new_blk);
3311 ctx->flags |= E2F_FLAG_RESTART;
3314 ctx->invalid_bitmaps = 0;
3318 * This routine marks all blocks which are used by the superblock,
3319 * group descriptors, inode bitmaps, and block bitmaps.
3321 static void mark_table_blocks(e2fsck_t ctx)
3323 ext2_filsys fs = ctx->fs;
3327 struct problem_context pctx;
3329 clear_problem_context(&pctx);
3331 for (i = 0; i < fs->group_desc_count; i++) {
3334 ext2fs_reserve_super_and_bgd(fs, i, ctx->block_found_map);
3337 * Mark the blocks used for the inode table
3339 if (ext2fs_inode_table_loc(fs, i)) {
3340 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
3341 j < fs->inode_blocks_per_group;
3343 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3346 if (!ctx->invalid_inode_table_flag[i] &&
3348 PR_1_ITABLE_CONFLICT, &pctx)) {
3349 ctx->invalid_inode_table_flag[i]++;
3350 ctx->invalid_bitmaps++;
3353 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3360 * Mark block used for the block bitmap
3362 if (ext2fs_block_bitmap_loc(fs, i)) {
3363 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3364 ext2fs_block_bitmap_loc(fs, i))) {
3365 pctx.blk = ext2fs_block_bitmap_loc(fs, i);
3366 if (fix_problem(ctx, PR_1_BB_CONFLICT, &pctx)) {
3367 ctx->invalid_block_bitmap_flag[i]++;
3368 ctx->invalid_bitmaps++;
3371 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3372 ext2fs_block_bitmap_loc(fs, i));
3377 * Mark block used for the inode bitmap
3379 if (ext2fs_inode_bitmap_loc(fs, i)) {
3380 if (ext2fs_test_block_bitmap2(ctx->block_found_map,
3381 ext2fs_inode_bitmap_loc(fs, i))) {
3382 pctx.blk = ext2fs_inode_bitmap_loc(fs, i);
3383 if (fix_problem(ctx, PR_1_IB_CONFLICT, &pctx)) {
3384 ctx->invalid_inode_bitmap_flag[i]++;
3385 ctx->invalid_bitmaps++;
3388 ext2fs_mark_block_bitmap2(ctx->block_found_map,
3389 ext2fs_inode_bitmap_loc(fs, i));
3396 * Thes subroutines short circuits ext2fs_get_blocks and
3397 * ext2fs_check_directory; we use them since we already have the inode
3398 * structure, so there's no point in letting the ext2fs library read
3401 static errcode_t pass1_get_blocks(ext2_filsys fs, ext2_ino_t ino,
3404 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3407 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3408 return EXT2_ET_CALLBACK_NOTHANDLED;
3410 for (i=0; i < EXT2_N_BLOCKS; i++)
3411 blocks[i] = ctx->stashed_inode->i_block[i];
3415 static errcode_t pass1_read_inode(ext2_filsys fs, ext2_ino_t ino,
3416 struct ext2_inode *inode)
3418 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3420 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3421 return EXT2_ET_CALLBACK_NOTHANDLED;
3422 *inode = *ctx->stashed_inode;
3426 static errcode_t pass1_write_inode(ext2_filsys fs, ext2_ino_t ino,
3427 struct ext2_inode *inode)
3429 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3431 if ((ino == ctx->stashed_ino) && ctx->stashed_inode &&
3432 (inode != ctx->stashed_inode))
3433 *ctx->stashed_inode = *inode;
3434 return EXT2_ET_CALLBACK_NOTHANDLED;
3437 static errcode_t pass1_check_directory(ext2_filsys fs, ext2_ino_t ino)
3439 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3441 if ((ino != ctx->stashed_ino) || !ctx->stashed_inode)
3442 return EXT2_ET_CALLBACK_NOTHANDLED;
3444 if (!LINUX_S_ISDIR(ctx->stashed_inode->i_mode))
3445 return EXT2_ET_NO_DIRECTORY;
3449 static errcode_t e2fsck_get_alloc_block(ext2_filsys fs, blk64_t goal,
3452 e2fsck_t ctx = (e2fsck_t) fs->priv_data;
3456 if (ctx->block_found_map) {
3457 retval = ext2fs_new_block2(fs, goal, ctx->block_found_map,
3461 if (fs->block_map) {
3462 ext2fs_mark_block_bitmap2(fs->block_map, new_block);
3463 ext2fs_mark_bb_dirty(fs);
3466 if (!fs->block_map) {
3467 retval = ext2fs_read_block_bitmap(fs);
3472 retval = ext2fs_new_block2(fs, goal, 0, &new_block);
3481 void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int use_shortcuts)
3483 ext2_filsys fs = ctx->fs;
3485 if (use_shortcuts) {
3486 fs->get_blocks = pass1_get_blocks;
3487 fs->check_directory = pass1_check_directory;
3488 fs->read_inode = pass1_read_inode;
3489 fs->write_inode = pass1_write_inode;
3490 ctx->stashed_ino = 0;
3493 fs->check_directory = 0;
3495 fs->write_inode = 0;
3499 void e2fsck_intercept_block_allocations(e2fsck_t ctx)
3501 ext2fs_set_alloc_block_callback(ctx->fs, e2fsck_get_alloc_block, 0);
3502 ext2fs_set_block_alloc_stats_callback(ctx->fs,
3503 e2fsck_block_alloc_stats, 0);