Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * e2fsck.c - superblock checks |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 4 | * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
| 10 | */ |
| 11 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 12 | #ifdef HAVE_ERRNO_H |
| 13 | #include <errno.h> |
| 14 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 15 | |
Theodore Ts'o | 54be2cc | 1998-02-01 12:58:48 +0000 | [diff] [blame] | 16 | #ifndef EXT2_SKIP_UUID |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 17 | #include "uuid/uuid.h" |
Theodore Ts'o | 54be2cc | 1998-02-01 12:58:48 +0000 | [diff] [blame] | 18 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 19 | #include "e2fsck.h" |
| 20 | #include "problem.h" |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 21 | |
| 22 | #define MIN_CHECK 1 |
| 23 | #define MAX_CHECK 2 |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 24 | #define LOG2_CHECK 4 |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 25 | |
| 26 | static void check_super_value(e2fsck_t ctx, const char *descr, |
| 27 | unsigned long value, int flags, |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 28 | unsigned long min_val, unsigned long max_val) |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 29 | { |
| 30 | struct problem_context pctx; |
| 31 | |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 32 | if (((flags & MIN_CHECK) && (value < min_val)) || |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 33 | ((flags & MAX_CHECK) && (value > max_val)) || |
| 34 | ((flags & LOG2_CHECK) && (value & (value - 1) != 0))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 35 | clear_problem_context(&pctx); |
| 36 | pctx.num = value; |
| 37 | pctx.str = descr; |
| 38 | fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 39 | ctx->flags |= E2F_FLAG_ABORT; /* never get here! */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Theodore Ts'o | 4313932 | 1998-02-16 22:34:46 +0000 | [diff] [blame] | 43 | /* |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 44 | * helper function to release an inode |
| 45 | */ |
| 46 | struct process_block_struct { |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 47 | e2fsck_t ctx; |
| 48 | char *buf; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 49 | struct problem_context *pctx; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 50 | int truncating; |
| 51 | int truncate_offset; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 52 | e2_blkcnt_t truncate_block; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 53 | int truncated_blocks; |
| 54 | int abort; |
| 55 | errcode_t errcode; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | static int release_inode_block(ext2_filsys fs, |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 59 | blk64_t *block_nr, |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 60 | e2_blkcnt_t blockcnt, |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 61 | blk64_t ref_blk EXT2FS_ATTR((unused)), |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 62 | int ref_offset EXT2FS_ATTR((unused)), |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 63 | void *priv_data) |
| 64 | { |
| 65 | struct process_block_struct *pb; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 66 | e2fsck_t ctx; |
| 67 | struct problem_context *pctx; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 68 | blk64_t blk = *block_nr; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 69 | int retval = 0; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 70 | |
| 71 | pb = (struct process_block_struct *) priv_data; |
| 72 | ctx = pb->ctx; |
| 73 | pctx = pb->pctx; |
| 74 | |
| 75 | pctx->blk = blk; |
| 76 | pctx->blkcount = blockcnt; |
| 77 | |
| 78 | if (HOLE_BLKADDR(blk)) |
| 79 | return 0; |
| 80 | |
| 81 | if ((blk < fs->super->s_first_data_block) || |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 82 | (blk >= ext2fs_blocks_count(fs->super))) { |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 83 | fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx); |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 84 | return_abort: |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 85 | pb->abort = 1; |
| 86 | return BLOCK_ABORT; |
| 87 | } |
| 88 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 89 | if (!ext2fs_test_block_bitmap2(fs->block_map, blk)) { |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 90 | fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx); |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 91 | goto return_abort; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
| 95 | * If we are deleting an orphan, then we leave the fields alone. |
| 96 | * If we are truncating an orphan, then update the inode fields |
| 97 | * and clean up any partial block data. |
| 98 | */ |
| 99 | if (pb->truncating) { |
| 100 | /* |
| 101 | * We only remove indirect blocks if they are |
| 102 | * completely empty. |
| 103 | */ |
| 104 | if (blockcnt < 0) { |
| 105 | int i, limit; |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 106 | blk_t *bp; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 107 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 108 | pb->errcode = io_channel_read_blk64(fs->io, blk, 1, |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 109 | pb->buf); |
| 110 | if (pb->errcode) |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 111 | goto return_abort; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 112 | |
| 113 | limit = fs->blocksize >> 2; |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 114 | for (i = 0, bp = (blk_t *) pb->buf; |
| 115 | i < limit; i++, bp++) |
| 116 | if (*bp) |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | /* |
| 120 | * We don't remove direct blocks until we've reached |
| 121 | * the truncation block. |
| 122 | */ |
| 123 | if (blockcnt >= 0 && blockcnt < pb->truncate_block) |
| 124 | return 0; |
| 125 | /* |
| 126 | * If part of the last block needs truncating, we do |
| 127 | * it here. |
| 128 | */ |
| 129 | if ((blockcnt == pb->truncate_block) && pb->truncate_offset) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 130 | pb->errcode = io_channel_read_blk64(fs->io, blk, 1, |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 131 | pb->buf); |
| 132 | if (pb->errcode) |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 133 | goto return_abort; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 134 | memset(pb->buf + pb->truncate_offset, 0, |
| 135 | fs->blocksize - pb->truncate_offset); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 136 | pb->errcode = io_channel_write_blk64(fs->io, blk, 1, |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 137 | pb->buf); |
| 138 | if (pb->errcode) |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 139 | goto return_abort; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 140 | } |
| 141 | pb->truncated_blocks++; |
| 142 | *block_nr = 0; |
| 143 | retval |= BLOCK_CHANGED; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 144 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 145 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 146 | ext2fs_block_alloc_stats2(fs, blk, -1); |
| 147 | ctx->free_blocks++; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 148 | return retval; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 149 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 150 | |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 151 | /* |
| 152 | * This function releases an inode. Returns 1 if an inconsistency was |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 153 | * found. If the inode has a link count, then it is being truncated and |
| 154 | * not deleted. |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 155 | */ |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 156 | static int release_inode_blocks(e2fsck_t ctx, ext2_ino_t ino, |
Theodore Ts'o | 721edd0 | 2001-01-12 21:05:57 +0000 | [diff] [blame] | 157 | struct ext2_inode *inode, char *block_buf, |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 158 | struct problem_context *pctx) |
| 159 | { |
Theodore Ts'o | 0684a4f | 2002-08-17 10:19:44 -0400 | [diff] [blame] | 160 | struct process_block_struct pb; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 161 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 162 | errcode_t retval; |
Theodore Ts'o | 0684a4f | 2002-08-17 10:19:44 -0400 | [diff] [blame] | 163 | __u32 count; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 164 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 165 | if (!ext2fs_inode_has_valid_blocks2(fs, inode)) |
Theodore Ts'o | 42475e2 | 2001-06-22 20:35:38 -0400 | [diff] [blame] | 166 | return 0; |
| 167 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 168 | pb.buf = block_buf + 3 * ctx->fs->blocksize; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 169 | pb.ctx = ctx; |
| 170 | pb.abort = 0; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 171 | pb.errcode = 0; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 172 | pb.pctx = pctx; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 173 | if (inode->i_links_count) { |
| 174 | pb.truncating = 1; |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 175 | pb.truncate_block = (e2_blkcnt_t) |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 176 | ((EXT2_I_SIZE(inode) + fs->blocksize - 1) / |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 177 | fs->blocksize); |
| 178 | pb.truncate_offset = inode->i_size % fs->blocksize; |
| 179 | } else { |
| 180 | pb.truncating = 0; |
| 181 | pb.truncate_block = 0; |
| 182 | pb.truncate_offset = 0; |
| 183 | } |
| 184 | pb.truncated_blocks = 0; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 185 | retval = ext2fs_block_iterate3(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE, |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 186 | block_buf, release_inode_block, &pb); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 187 | if (retval) { |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 188 | com_err("release_inode_blocks", retval, |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 189 | _("while calling ext2fs_block_iterate for inode %d"), |
| 190 | ino); |
| 191 | return 1; |
| 192 | } |
| 193 | if (pb.abort) |
| 194 | return 1; |
| 195 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 196 | /* Refresh the inode since ext2fs_block_iterate may have changed it */ |
| 197 | e2fsck_read_inode(ctx, ino, inode, "release_inode_blocks"); |
| 198 | |
| 199 | if (pb.truncated_blocks) |
Theodore Ts'o | 1ca1059 | 2008-04-09 11:39:11 -0400 | [diff] [blame] | 200 | ext2fs_iblk_sub_blocks(fs, inode, pb.truncated_blocks); |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 201 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 202 | if (ext2fs_file_acl_block(fs, inode)) { |
| 203 | retval = ext2fs_adjust_ea_refcount2(fs, |
| 204 | ext2fs_file_acl_block(fs, inode), |
| 205 | block_buf, -1, &count); |
Theodore Ts'o | 0684a4f | 2002-08-17 10:19:44 -0400 | [diff] [blame] | 206 | if (retval == EXT2_ET_BAD_EA_BLOCK_NUM) { |
| 207 | retval = 0; |
| 208 | count = 1; |
| 209 | } |
| 210 | if (retval) { |
| 211 | com_err("release_inode_blocks", retval, |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 212 | _("while calling ext2fs_adjust_ea_refcount2 for inode %d"), |
Theodore Ts'o | 0684a4f | 2002-08-17 10:19:44 -0400 | [diff] [blame] | 213 | ino); |
| 214 | return 1; |
| 215 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 216 | if (count == 0) { |
| 217 | ext2fs_block_alloc_stats2(fs, |
| 218 | ext2fs_file_acl_block(fs, inode), -1); |
| 219 | ctx->free_blocks++; |
| 220 | } |
| 221 | ext2fs_file_acl_block_set(fs, inode, 0); |
Theodore Ts'o | 0684a4f | 2002-08-17 10:19:44 -0400 | [diff] [blame] | 222 | } |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * This function releases all of the orphan inodes. It returns 1 if |
| 228 | * it hit some error, and 0 on success. |
| 229 | */ |
| 230 | static int release_orphan_inodes(e2fsck_t ctx) |
| 231 | { |
| 232 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 233 | ext2_ino_t ino, next_ino; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 234 | struct ext2_inode inode; |
| 235 | struct problem_context pctx; |
| 236 | char *block_buf; |
| 237 | |
| 238 | if ((ino = fs->super->s_last_orphan) == 0) |
| 239 | return 0; |
| 240 | |
Theodore Ts'o | 25c63ba | 2000-08-18 15:31:37 +0000 | [diff] [blame] | 241 | /* |
| 242 | * Win or lose, we won't be using the head of the orphan inode |
| 243 | * list again. |
| 244 | */ |
| 245 | fs->super->s_last_orphan = 0; |
| 246 | ext2fs_mark_super_dirty(fs); |
Theodore Ts'o | eb4ab51 | 2001-08-13 10:58:41 -0400 | [diff] [blame] | 247 | |
| 248 | /* |
| 249 | * If the filesystem contains errors, don't run the orphan |
| 250 | * list, since the orphan list can't be trusted; and we're |
| 251 | * going to be running a full e2fsck run anyway... |
| 252 | */ |
| 253 | if (fs->super->s_state & EXT2_ERROR_FS) |
| 254 | return 0; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 255 | |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 256 | if ((ino < EXT2_FIRST_INODE(fs->super)) || |
| 257 | (ino > fs->super->s_inodes_count)) { |
| 258 | clear_problem_context(&pctx); |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 259 | pctx.ino = ino; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 260 | fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx); |
| 261 | return 1; |
| 262 | } |
| 263 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 264 | block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4, |
Theodore Ts'o | 9b56575 | 2000-12-13 18:50:22 +0000 | [diff] [blame] | 265 | "block iterate buffer"); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 266 | e2fsck_read_bitmaps(ctx); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 267 | |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 268 | while (ino) { |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 269 | e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes"); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 270 | clear_problem_context(&pctx); |
Theodore Ts'o | ecf1b77 | 2000-08-20 22:06:31 +0000 | [diff] [blame] | 271 | pctx.ino = ino; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 272 | pctx.inode = &inode; |
Theodore Ts'o | f0b8c87 | 2001-05-09 06:03:58 +0000 | [diff] [blame] | 273 | pctx.str = inode.i_links_count ? _("Truncating") : |
| 274 | _("Clearing"); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 275 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 276 | fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx); |
Theodore Ts'o | ecf1b77 | 2000-08-20 22:06:31 +0000 | [diff] [blame] | 277 | |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 278 | next_ino = inode.i_dtime; |
| 279 | if (next_ino && |
Theodore Ts'o | 99a2cc9 | 2000-08-22 21:41:52 +0000 | [diff] [blame] | 280 | ((next_ino < EXT2_FIRST_INODE(fs->super)) || |
| 281 | (next_ino > fs->super->s_inodes_count))) { |
| 282 | pctx.ino = next_ino; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 283 | fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx); |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 284 | goto return_abort; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 287 | if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx)) |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 288 | goto return_abort; |
Theodore Ts'o | ecf1b77 | 2000-08-20 22:06:31 +0000 | [diff] [blame] | 289 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 290 | if (!inode.i_links_count) { |
Theodore Ts'o | 0684a4f | 2002-08-17 10:19:44 -0400 | [diff] [blame] | 291 | ext2fs_inode_alloc_stats2(fs, ino, -1, |
| 292 | LINUX_S_ISDIR(inode.i_mode)); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 293 | ctx->free_inodes++; |
Theodore Ts'o | 1f3ad14 | 2005-04-14 14:07:53 -0400 | [diff] [blame] | 294 | inode.i_dtime = ctx->now; |
Stephen Tweedie | eb16f86 | 2002-08-14 11:00:44 +0100 | [diff] [blame] | 295 | } else { |
| 296 | inode.i_dtime = 0; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 297 | } |
| 298 | e2fsck_write_inode(ctx, ino, &inode, "delete_file"); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 299 | ino = next_ino; |
| 300 | } |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 301 | ext2fs_free_mem(&block_buf); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 302 | return 0; |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 303 | return_abort: |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 304 | ext2fs_free_mem(&block_buf); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 305 | return 1; |
| 306 | } |
| 307 | |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 308 | /* |
| 309 | * Check the resize inode to make sure it is sane. We check both for |
| 310 | * the case where on-line resizing is not enabled (in which case the |
| 311 | * resize inode should be cleared) as well as the case where on-line |
| 312 | * resizing is enabled. |
| 313 | */ |
Theodore Ts'o | 69d0edf | 2009-04-23 00:39:06 -0400 | [diff] [blame] | 314 | void check_resize_inode(e2fsck_t ctx) |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 315 | { |
| 316 | ext2_filsys fs = ctx->fs; |
| 317 | struct ext2_inode inode; |
| 318 | struct problem_context pctx; |
Theodore Ts'o | 642935c | 2006-11-14 23:38:17 -0500 | [diff] [blame] | 319 | int i, gdt_off, ind_off; |
| 320 | dgrp_t j; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 321 | blk_t blk, pblk; |
| 322 | blk_t expect; /* for resize inode, which is 32-bit only */ |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 323 | __u32 *dind_buf = 0, *ind_buf; |
| 324 | errcode_t retval; |
| 325 | |
| 326 | clear_problem_context(&pctx); |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 327 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 328 | /* |
| 329 | * If the resize inode feature isn't set, then |
Theodore Ts'o | ea77431 | 2005-01-28 11:45:28 -0500 | [diff] [blame] | 330 | * s_reserved_gdt_blocks must be zero. |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 331 | */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 332 | if (!(fs->super->s_feature_compat & |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 333 | EXT2_FEATURE_COMPAT_RESIZE_INODE)) { |
| 334 | if (fs->super->s_reserved_gdt_blocks) { |
| 335 | pctx.num = fs->super->s_reserved_gdt_blocks; |
| 336 | if (fix_problem(ctx, PR_0_NONZERO_RESERVED_GDT_BLOCKS, |
| 337 | &pctx)) { |
| 338 | fs->super->s_reserved_gdt_blocks = 0; |
| 339 | ext2fs_mark_super_dirty(fs); |
| 340 | } |
| 341 | } |
Theodore Ts'o | ea77431 | 2005-01-28 11:45:28 -0500 | [diff] [blame] | 342 | } |
| 343 | |
Theodore Ts'o | 1f3ad14 | 2005-04-14 14:07:53 -0400 | [diff] [blame] | 344 | /* Read the resize inode */ |
Theodore Ts'o | ea77431 | 2005-01-28 11:45:28 -0500 | [diff] [blame] | 345 | pctx.ino = EXT2_RESIZE_INO; |
| 346 | retval = ext2fs_read_inode(fs, EXT2_RESIZE_INO, &inode); |
| 347 | if (retval) { |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 348 | if (fs->super->s_feature_compat & |
Theodore Ts'o | ea77431 | 2005-01-28 11:45:28 -0500 | [diff] [blame] | 349 | EXT2_FEATURE_COMPAT_RESIZE_INODE) |
| 350 | ctx->flags |= E2F_FLAG_RESIZE_INODE; |
| 351 | return; |
| 352 | } |
| 353 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 354 | /* |
| 355 | * If the resize inode feature isn't set, check to make sure |
Theodore Ts'o | ea77431 | 2005-01-28 11:45:28 -0500 | [diff] [blame] | 356 | * the resize inode is cleared; then we're done. |
| 357 | */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 358 | if (!(fs->super->s_feature_compat & |
Theodore Ts'o | ea77431 | 2005-01-28 11:45:28 -0500 | [diff] [blame] | 359 | EXT2_FEATURE_COMPAT_RESIZE_INODE)) { |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 360 | for (i=0; i < EXT2_N_BLOCKS; i++) { |
| 361 | if (inode.i_block[i]) |
| 362 | break; |
| 363 | } |
| 364 | if ((i < EXT2_N_BLOCKS) && |
| 365 | fix_problem(ctx, PR_0_CLEAR_RESIZE_INODE, &pctx)) { |
| 366 | memset(&inode, 0, sizeof(inode)); |
| 367 | e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode, |
| 368 | "clear_resize"); |
| 369 | } |
| 370 | return; |
| 371 | } |
Theodore Ts'o | ea77431 | 2005-01-28 11:45:28 -0500 | [diff] [blame] | 372 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 373 | /* |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 374 | * The resize inode feature is enabled; check to make sure the |
| 375 | * only block in use is the double indirect block |
| 376 | */ |
| 377 | blk = inode.i_block[EXT2_DIND_BLOCK]; |
| 378 | for (i=0; i < EXT2_N_BLOCKS; i++) { |
| 379 | if (i != EXT2_DIND_BLOCK && inode.i_block[i]) |
| 380 | break; |
| 381 | } |
Theodore Ts'o | d4dc0a9 | 2005-01-27 18:47:51 -0500 | [diff] [blame] | 382 | if ((i < EXT2_N_BLOCKS) || !blk || !inode.i_links_count || |
| 383 | !(inode.i_mode & LINUX_S_IFREG) || |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 384 | (blk < fs->super->s_first_data_block || |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 385 | blk >= ext2fs_blocks_count(fs->super))) { |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 386 | resize_inode_invalid: |
| 387 | if (fix_problem(ctx, PR_0_RESIZE_INODE_INVALID, &pctx)) { |
| 388 | memset(&inode, 0, sizeof(inode)); |
| 389 | e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode, |
| 390 | "clear_resize"); |
| 391 | ctx->flags |= E2F_FLAG_RESIZE_INODE; |
| 392 | } |
| 393 | if (!(ctx->options & E2F_OPT_READONLY)) { |
| 394 | fs->super->s_state &= ~EXT2_VALID_FS; |
| 395 | ext2fs_mark_super_dirty(fs); |
| 396 | } |
| 397 | goto cleanup; |
| 398 | } |
| 399 | dind_buf = (__u32 *) e2fsck_allocate_memory(ctx, fs->blocksize * 2, |
| 400 | "resize dind buffer"); |
| 401 | ind_buf = (__u32 *) ((char *) dind_buf + fs->blocksize); |
| 402 | |
Theodore Ts'o | dc8ce34 | 2005-01-06 00:04:24 -0500 | [diff] [blame] | 403 | retval = ext2fs_read_ind_block(fs, blk, dind_buf); |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 404 | if (retval) |
| 405 | goto resize_inode_invalid; |
| 406 | |
| 407 | gdt_off = fs->desc_blocks; |
| 408 | pblk = fs->super->s_first_data_block + 1 + fs->desc_blocks; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 409 | if (fs->blocksize == 1024 && fs->super->s_first_data_block == 0) |
| 410 | pblk++; /* Deal with 1024 blocksize bigalloc fs */ |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 411 | for (i = 0; i < fs->super->s_reserved_gdt_blocks / 4; |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 412 | i++, gdt_off++, pblk++) { |
| 413 | gdt_off %= fs->blocksize/4; |
| 414 | if (dind_buf[gdt_off] != pblk) |
| 415 | goto resize_inode_invalid; |
Theodore Ts'o | dc8ce34 | 2005-01-06 00:04:24 -0500 | [diff] [blame] | 416 | retval = ext2fs_read_ind_block(fs, pblk, ind_buf); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 417 | if (retval) |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 418 | goto resize_inode_invalid; |
| 419 | ind_off = 0; |
| 420 | for (j = 1; j < fs->group_desc_count; j++) { |
| 421 | if (!ext2fs_bg_has_super(fs, j)) |
| 422 | continue; |
| 423 | expect = pblk + (j * fs->super->s_blocks_per_group); |
| 424 | if (ind_buf[ind_off] != expect) |
| 425 | goto resize_inode_invalid; |
| 426 | ind_off++; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | cleanup: |
| 431 | if (dind_buf) |
| 432 | ext2fs_free_mem(&dind_buf); |
| 433 | |
| 434 | } |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 435 | |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 436 | /* |
| 437 | * This function checks the dirhash signed/unsigned hint if necessary. |
| 438 | */ |
Theodore Ts'o | 642935c | 2006-11-14 23:38:17 -0500 | [diff] [blame] | 439 | static void e2fsck_fix_dirhash_hint(e2fsck_t ctx) |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 440 | { |
| 441 | struct ext2_super_block *sb = ctx->fs->super; |
| 442 | struct problem_context pctx; |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 443 | char c; |
| 444 | |
| 445 | if ((ctx->options & E2F_OPT_READONLY) || |
| 446 | !(sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) || |
| 447 | (sb->s_flags & (EXT2_FLAGS_SIGNED_HASH|EXT2_FLAGS_UNSIGNED_HASH))) |
| 448 | return; |
| 449 | |
| 450 | c = (char) 255; |
| 451 | |
| 452 | clear_problem_context(&pctx); |
| 453 | if (fix_problem(ctx, PR_0_DIRHASH_HINT, &pctx)) { |
| 454 | if (((int) c) == -1) { |
| 455 | sb->s_flags |= EXT2_FLAGS_SIGNED_HASH; |
| 456 | } else { |
| 457 | sb->s_flags |= EXT2_FLAGS_UNSIGNED_HASH; |
| 458 | } |
| 459 | ext2fs_mark_super_dirty(ctx->fs); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 464 | void check_super_block(e2fsck_t ctx) |
| 465 | { |
| 466 | ext2_filsys fs = ctx->fs; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 467 | blk64_t first_block, last_block; |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 468 | struct ext2_super_block *sb = fs->super; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 469 | unsigned int ipg_max; |
Theodore Ts'o | 26ea489 | 2009-07-16 23:44:50 -0400 | [diff] [blame] | 470 | problem_t problem; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 471 | blk64_t blocks_per_group = fs->super->s_blocks_per_group; |
| 472 | __u32 bpg_max, cpg_max; |
Theodore Ts'o | 78cf054 | 1999-10-20 18:29:18 +0000 | [diff] [blame] | 473 | int inodes_per_block; |
Theodore Ts'o | 89db86d | 2005-03-20 22:47:54 -0500 | [diff] [blame] | 474 | int inode_size; |
Theodore Ts'o | ba5131f | 2009-10-16 20:46:45 -0400 | [diff] [blame] | 475 | int accept_time_fudge; |
Theodore Ts'o | 177839e | 2010-05-13 17:36:36 -0400 | [diff] [blame] | 476 | int broken_system_clock; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 477 | dgrp_t i; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 478 | blk64_t should_be; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 479 | struct problem_context pctx; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 480 | blk64_t free_blocks = 0; |
Valerie Clement | 5d38ef1 | 2007-08-30 17:33:40 +0200 | [diff] [blame] | 481 | ino_t free_inodes = 0; |
Theodore Ts'o | 80875db | 2008-10-12 23:09:26 -0400 | [diff] [blame] | 482 | int csum_flag, clear_test_fs_flag; |
Andreas Dilger | b21bf26 | 2002-06-10 11:05:56 -0600 | [diff] [blame] | 483 | |
| 484 | inodes_per_block = EXT2_INODES_PER_BLOCK(fs->super); |
| 485 | ipg_max = inodes_per_block * (blocks_per_group - 4); |
| 486 | if (ipg_max > EXT2_MAX_INODES_PER_GROUP(sb)) |
| 487 | ipg_max = EXT2_MAX_INODES_PER_GROUP(sb); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 488 | cpg_max = 8 * EXT2_BLOCK_SIZE(sb); |
| 489 | if (cpg_max > EXT2_MAX_CLUSTERS_PER_GROUP(sb)) |
| 490 | cpg_max = EXT2_MAX_CLUSTERS_PER_GROUP(sb); |
| 491 | bpg_max = 8 * EXT2_BLOCK_SIZE(sb) * EXT2FS_CLUSTER_RATIO(fs); |
Andreas Dilger | b21bf26 | 2002-06-10 11:05:56 -0600 | [diff] [blame] | 492 | if (bpg_max > EXT2_MAX_BLOCKS_PER_GROUP(sb)) |
| 493 | bpg_max = EXT2_MAX_BLOCKS_PER_GROUP(sb); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 494 | |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 495 | ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 496 | sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap"); |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 497 | ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 498 | sizeof(int) * fs->group_desc_count, "invalid_block_bitmap"); |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 499 | ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx, |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 500 | sizeof(int) * fs->group_desc_count, "invalid_inode_table"); |
Andreas Dilger | b21bf26 | 2002-06-10 11:05:56 -0600 | [diff] [blame] | 501 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 502 | clear_problem_context(&pctx); |
| 503 | |
| 504 | /* |
| 505 | * Verify the super block constants... |
| 506 | */ |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 507 | check_super_value(ctx, "inodes_count", sb->s_inodes_count, |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 508 | MIN_CHECK, 1, 0); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 509 | check_super_value(ctx, "blocks_count", ext2fs_blocks_count(sb), |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 510 | MIN_CHECK, 1, 0); |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 511 | check_super_value(ctx, "first_data_block", sb->s_first_data_block, |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 512 | MAX_CHECK, 0, ext2fs_blocks_count(sb)); |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 513 | check_super_value(ctx, "log_block_size", sb->s_log_block_size, |
Theodore Ts'o | 31e29a1 | 2002-05-17 10:53:07 -0400 | [diff] [blame] | 514 | MIN_CHECK | MAX_CHECK, 0, |
| 515 | EXT2_MAX_BLOCK_LOG_SIZE - EXT2_MIN_BLOCK_LOG_SIZE); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 516 | check_super_value(ctx, "log_cluster_size", |
| 517 | sb->s_log_cluster_size, |
| 518 | MIN_CHECK | MAX_CHECK, sb->s_log_block_size, |
| 519 | (EXT2_MAX_CLUSTER_LOG_SIZE - |
| 520 | EXT2_MIN_CLUSTER_LOG_SIZE)); |
| 521 | check_super_value(ctx, "clusters_per_group", sb->s_clusters_per_group, |
| 522 | MIN_CHECK | MAX_CHECK, 8, cpg_max); |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 523 | check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group, |
Andreas Dilger | b21bf26 | 2002-06-10 11:05:56 -0600 | [diff] [blame] | 524 | MIN_CHECK | MAX_CHECK, 8, bpg_max); |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 525 | check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group, |
Andreas Dilger | b21bf26 | 2002-06-10 11:05:56 -0600 | [diff] [blame] | 526 | MIN_CHECK | MAX_CHECK, inodes_per_block, ipg_max); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 527 | check_super_value(ctx, "r_blocks_count", ext2fs_r_blocks_count(sb), |
| 528 | MAX_CHECK, 0, ext2fs_blocks_count(sb) / 2); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 529 | check_super_value(ctx, "reserved_gdt_blocks", |
Theodore Ts'o | c3ffaf8 | 2004-12-24 01:42:22 -0500 | [diff] [blame] | 530 | sb->s_reserved_gdt_blocks, MAX_CHECK, 0, |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 531 | fs->blocksize / sizeof(__u32)); |
| 532 | check_super_value(ctx, "desc_size", |
| 533 | sb->s_desc_size, MAX_CHECK | LOG2_CHECK, 0, |
| 534 | EXT2_MAX_DESC_SIZE); |
Theodore Ts'o | 1f790a7 | 2009-01-22 15:52:50 -0500 | [diff] [blame] | 535 | if (sb->s_rev_level > EXT2_GOOD_OLD_REV) |
| 536 | check_super_value(ctx, "first_ino", sb->s_first_ino, |
| 537 | MIN_CHECK | MAX_CHECK, |
| 538 | EXT2_GOOD_OLD_FIRST_INO, sb->s_inodes_count); |
Theodore Ts'o | 89db86d | 2005-03-20 22:47:54 -0500 | [diff] [blame] | 539 | inode_size = EXT2_INODE_SIZE(sb); |
| 540 | check_super_value(ctx, "inode_size", |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 541 | inode_size, MIN_CHECK | MAX_CHECK | LOG2_CHECK, |
Theodore Ts'o | 89db86d | 2005-03-20 22:47:54 -0500 | [diff] [blame] | 542 | EXT2_GOOD_OLD_INODE_SIZE, fs->blocksize); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 543 | if (sb->s_blocks_per_group != (sb->s_clusters_per_group * |
| 544 | EXT2FS_CLUSTER_RATIO(fs))) { |
| 545 | pctx.num = sb->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs); |
| 546 | pctx.str = "block_size"; |
Theodore Ts'o | 44fe08f | 2011-06-10 18:58:16 -0400 | [diff] [blame] | 547 | fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx); |
| 548 | ctx->flags |= E2F_FLAG_ABORT; /* never get here! */ |
| 549 | return; |
| 550 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 551 | |
Theodore Ts'o | d2af1bd | 2007-06-04 01:14:52 -0400 | [diff] [blame] | 552 | if ((ctx->flags & E2F_FLAG_GOT_DEVSIZE) && |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 553 | (ctx->num_blocks < ext2fs_blocks_count(sb))) { |
| 554 | pctx.blk = ext2fs_blocks_count(sb); |
Theodore Ts'o | d2af1bd | 2007-06-04 01:14:52 -0400 | [diff] [blame] | 555 | pctx.blk2 = ctx->num_blocks; |
| 556 | if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) { |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 557 | ctx->flags |= E2F_FLAG_ABORT; |
| 558 | return; |
| 559 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 560 | } |
| 561 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 562 | should_be = (sb->s_log_block_size == 0 && |
| 563 | EXT2FS_CLUSTER_RATIO(fs) == 1) ? 1 : 0; |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 564 | if (sb->s_first_data_block != should_be) { |
| 565 | pctx.blk = sb->s_first_data_block; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 566 | pctx.blk2 = should_be; |
| 567 | fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 568 | ctx->flags |= E2F_FLAG_ABORT; |
| 569 | return; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 572 | should_be = sb->s_inodes_per_group * fs->group_desc_count; |
| 573 | if (sb->s_inodes_count != should_be) { |
| 574 | pctx.ino = sb->s_inodes_count; |
Theodore Ts'o | d4b0ce0 | 1999-06-18 01:09:29 +0000 | [diff] [blame] | 575 | pctx.ino2 = should_be; |
| 576 | if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) { |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 577 | sb->s_inodes_count = should_be; |
Theodore Ts'o | d4b0ce0 | 1999-06-18 01:09:29 +0000 | [diff] [blame] | 578 | ext2fs_mark_super_dirty(fs); |
| 579 | } |
| 580 | } |
| 581 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 582 | /* Is 64bit set and extents unset? */ |
| 583 | if (EXT2_HAS_INCOMPAT_FEATURE(fs->super, |
| 584 | EXT4_FEATURE_INCOMPAT_64BIT) && |
| 585 | !EXT2_HAS_INCOMPAT_FEATURE(fs->super, |
| 586 | EXT3_FEATURE_INCOMPAT_EXTENTS) && |
| 587 | fix_problem(ctx, PR_0_64BIT_WITHOUT_EXTENTS, &pctx)) { |
| 588 | fs->super->s_feature_incompat |= |
| 589 | EXT3_FEATURE_INCOMPAT_EXTENTS; |
| 590 | ext2fs_mark_super_dirty(fs); |
| 591 | } |
| 592 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 593 | /* |
| 594 | * Verify the group descriptors.... |
| 595 | */ |
Jose R. Santos | 88b34b1 | 2007-08-13 23:33:03 -0500 | [diff] [blame] | 596 | first_block = sb->s_first_data_block; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 597 | last_block = ext2fs_blocks_count(sb)-1; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 598 | |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 599 | csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super, |
| 600 | EXT4_FEATURE_RO_COMPAT_GDT_CSUM); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 601 | for (i = 0; i < fs->group_desc_count; i++) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 602 | pctx.group = i; |
Eric Sandeen | abf2343 | 2006-09-12 14:56:16 -0400 | [diff] [blame] | 603 | |
Jose R. Santos | 88b34b1 | 2007-08-13 23:33:03 -0500 | [diff] [blame] | 604 | if (!EXT2_HAS_INCOMPAT_FEATURE(fs->super, |
| 605 | EXT4_FEATURE_INCOMPAT_FLEX_BG)) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 606 | first_block = ext2fs_group_first_block2(fs, i); |
| 607 | last_block = ext2fs_group_last_block2(fs, i); |
Jose R. Santos | 88b34b1 | 2007-08-13 23:33:03 -0500 | [diff] [blame] | 608 | } |
Eric Sandeen | bb1a46a | 2006-09-12 14:55:22 -0400 | [diff] [blame] | 609 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 610 | if ((ext2fs_block_bitmap_loc(fs, i) < first_block) || |
| 611 | (ext2fs_block_bitmap_loc(fs, i) > last_block)) { |
| 612 | pctx.blk = ext2fs_block_bitmap_loc(fs, i); |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 613 | if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx)) |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 614 | ext2fs_block_bitmap_loc_set(fs, i, 0); |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 615 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 616 | if (ext2fs_block_bitmap_loc(fs, i) == 0) { |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 617 | ctx->invalid_block_bitmap_flag[i]++; |
| 618 | ctx->invalid_bitmaps++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 619 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 620 | if ((ext2fs_inode_bitmap_loc(fs, i) < first_block) || |
| 621 | (ext2fs_inode_bitmap_loc(fs, i) > last_block)) { |
| 622 | pctx.blk = ext2fs_inode_bitmap_loc(fs, i); |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 623 | if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx)) |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 624 | ext2fs_inode_bitmap_loc_set(fs, i, 0); |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 625 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 626 | if (ext2fs_inode_bitmap_loc(fs, i) == 0) { |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 627 | ctx->invalid_inode_bitmap_flag[i]++; |
| 628 | ctx->invalid_bitmaps++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 629 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 630 | if ((ext2fs_inode_table_loc(fs, i) < first_block) || |
| 631 | ((ext2fs_inode_table_loc(fs, i) + |
Eric Sandeen | bb1a46a | 2006-09-12 14:55:22 -0400 | [diff] [blame] | 632 | fs->inode_blocks_per_group - 1) > last_block)) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 633 | pctx.blk = ext2fs_inode_table_loc(fs, i); |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 634 | if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx)) |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 635 | ext2fs_inode_table_loc_set(fs, i, 0); |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 636 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 637 | if (ext2fs_inode_table_loc(fs, i) == 0) { |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 638 | ctx->invalid_inode_table_flag[i]++; |
| 639 | ctx->invalid_bitmaps++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 640 | } |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 641 | free_blocks += ext2fs_bg_free_blocks_count(fs, i); |
| 642 | free_inodes += ext2fs_bg_free_inodes_count(fs, i); |
Theodore Ts'o | 550a4af | 2005-01-25 03:09:24 -0500 | [diff] [blame] | 643 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 644 | if ((ext2fs_bg_free_blocks_count(fs, i) > sb->s_blocks_per_group) || |
| 645 | (ext2fs_bg_free_inodes_count(fs, i) > sb->s_inodes_per_group) || |
| 646 | (ext2fs_bg_used_dirs_count(fs, i) > sb->s_inodes_per_group)) |
Theodore Ts'o | 550a4af | 2005-01-25 03:09:24 -0500 | [diff] [blame] | 647 | ext2fs_unmark_valid(fs); |
| 648 | |
Andreas Dilger | 0d5439c | 2008-03-31 12:14:22 -0400 | [diff] [blame] | 649 | should_be = 0; |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 650 | if (!ext2fs_group_desc_csum_verify(fs, i)) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 651 | pctx.csum1 = ext2fs_bg_checksum(fs, i); |
| 652 | pctx.csum2 = ext2fs_group_desc_csum(fs, i); |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 653 | if (fix_problem(ctx, PR_0_GDT_CSUM, &pctx)) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 654 | ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT); |
| 655 | ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT); |
| 656 | ext2fs_bg_itable_unused_set(fs, i, 0); |
Andreas Dilger | 0d5439c | 2008-03-31 12:14:22 -0400 | [diff] [blame] | 657 | should_be = 1; |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 658 | } |
| 659 | ext2fs_unmark_valid(fs); |
| 660 | } |
| 661 | |
Theodore Ts'o | 16b851c | 2008-04-20 23:33:34 -0400 | [diff] [blame] | 662 | if (!csum_flag && |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 663 | (ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) || |
| 664 | ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) || |
| 665 | ext2fs_bg_itable_unused(fs, i) != 0)) { |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 666 | if (fix_problem(ctx, PR_0_GDT_UNINIT, &pctx)) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 667 | ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT); |
| 668 | ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT); |
| 669 | ext2fs_bg_itable_unused_set(fs, i, 0); |
Andreas Dilger | 0d5439c | 2008-03-31 12:14:22 -0400 | [diff] [blame] | 670 | should_be = 1; |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 671 | } |
| 672 | ext2fs_unmark_valid(fs); |
| 673 | } |
Andreas Dilger | 0d5439c | 2008-03-31 12:14:22 -0400 | [diff] [blame] | 674 | |
| 675 | if (i == fs->group_desc_count - 1 && |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 676 | ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT)) { |
Andreas Dilger | 0d5439c | 2008-03-31 12:14:22 -0400 | [diff] [blame] | 677 | if (fix_problem(ctx, PR_0_BB_UNINIT_LAST, &pctx)) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 678 | ext2fs_bg_flags_clear(fs, i, EXT2_BG_BLOCK_UNINIT); |
Andreas Dilger | 0d5439c | 2008-03-31 12:14:22 -0400 | [diff] [blame] | 679 | should_be = 1; |
| 680 | } |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 681 | ext2fs_unmark_valid(fs); |
| 682 | } |
Andreas Dilger | 0d5439c | 2008-03-31 12:14:22 -0400 | [diff] [blame] | 683 | |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 684 | if (csum_flag && |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 685 | (ext2fs_bg_itable_unused(fs, i) > ext2fs_bg_free_inodes_count(fs, i) || |
| 686 | ext2fs_bg_itable_unused(fs, i) > sb->s_inodes_per_group)) { |
| 687 | pctx.blk = ext2fs_bg_itable_unused(fs, i); |
Andreas Dilger | 0d5439c | 2008-03-31 12:14:22 -0400 | [diff] [blame] | 688 | if (fix_problem(ctx, PR_0_GDT_ITABLE_UNUSED, &pctx)) { |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 689 | ext2fs_bg_itable_unused_set(fs, i, 0); |
Andreas Dilger | 0d5439c | 2008-03-31 12:14:22 -0400 | [diff] [blame] | 690 | should_be = 1; |
| 691 | } |
Jose R. Santos | 49a7360 | 2007-10-21 21:04:03 -0500 | [diff] [blame] | 692 | ext2fs_unmark_valid(fs); |
| 693 | } |
Andreas Dilger | 0d5439c | 2008-03-31 12:14:22 -0400 | [diff] [blame] | 694 | |
| 695 | if (should_be) |
| 696 | ext2fs_group_desc_csum_set(fs, i); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 697 | /* If the user aborts e2fsck by typing ^C, stop right away */ |
| 698 | if (ctx->flags & E2F_FLAG_SIGNAL_MASK) |
| 699 | return; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 700 | } |
Theodore Ts'o | 550a4af | 2005-01-25 03:09:24 -0500 | [diff] [blame] | 701 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 702 | ctx->free_blocks = EXT2FS_C2B(fs, free_blocks); |
| 703 | ctx->free_inodes = free_inodes; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 704 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 705 | if ((ext2fs_free_blocks_count(sb) > ext2fs_blocks_count(sb)) || |
Theodore Ts'o | 550a4af | 2005-01-25 03:09:24 -0500 | [diff] [blame] | 706 | (sb->s_free_inodes_count > sb->s_inodes_count)) |
| 707 | ext2fs_unmark_valid(fs); |
| 708 | |
| 709 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 710 | /* |
| 711 | * If we have invalid bitmaps, set the error state of the |
| 712 | * filesystem. |
| 713 | */ |
| 714 | if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) { |
Theodore Ts'o | 550a4af | 2005-01-25 03:09:24 -0500 | [diff] [blame] | 715 | sb->s_state &= ~EXT2_VALID_FS; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 716 | ext2fs_mark_super_dirty(fs); |
| 717 | } |
| 718 | |
Theodore Ts'o | 4ea0a11 | 2000-05-08 13:33:17 +0000 | [diff] [blame] | 719 | clear_problem_context(&pctx); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 720 | |
Theodore Ts'o | 54be2cc | 1998-02-01 12:58:48 +0000 | [diff] [blame] | 721 | #ifndef EXT2_SKIP_UUID |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 722 | /* |
| 723 | * If the UUID field isn't assigned, assign it. |
| 724 | */ |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 725 | if (!(ctx->options & E2F_OPT_READONLY) && uuid_is_null(sb->s_uuid)) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 726 | if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) { |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 727 | uuid_generate(sb->s_uuid); |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 728 | fs->flags |= EXT2_FLAG_DIRTY; |
Theodore Ts'o | 299d742 | 2002-11-08 11:10:28 -0500 | [diff] [blame] | 729 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
Theodore Ts'o | 54be2cc | 1998-02-01 12:58:48 +0000 | [diff] [blame] | 732 | #endif |
Theodore Ts'o | 2a77a78 | 2003-04-05 22:50:44 -0500 | [diff] [blame] | 733 | |
| 734 | /* |
Theodore Ts'o | 80875db | 2008-10-12 23:09:26 -0400 | [diff] [blame] | 735 | * Check to see if we should disable the test_fs flag |
| 736 | */ |
| 737 | profile_get_boolean(ctx->profile, "options", |
| 738 | "clear_test_fs_flag", 0, 1, |
| 739 | &clear_test_fs_flag); |
| 740 | if (!(ctx->options & E2F_OPT_READONLY) && |
| 741 | clear_test_fs_flag && |
| 742 | (fs->super->s_flags & EXT2_FLAGS_TEST_FILESYS) && |
| 743 | (fs_proc_check("ext4") || check_for_modules("ext4"))) { |
| 744 | if (fix_problem(ctx, PR_0_CLEAR_TESTFS_FLAG, &pctx)) { |
| 745 | fs->super->s_flags &= ~EXT2_FLAGS_TEST_FILESYS; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 746 | fs->flags |= EXT2_FLAG_DIRTY; |
Theodore Ts'o | 80875db | 2008-10-12 23:09:26 -0400 | [diff] [blame] | 747 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | /* |
Theodore Ts'o | 4ea0a11 | 2000-05-08 13:33:17 +0000 | [diff] [blame] | 752 | * For the Hurd, check to see if the filetype option is set, |
| 753 | * since it doesn't support it. |
| 754 | */ |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 755 | if (!(ctx->options & E2F_OPT_READONLY) && |
| 756 | fs->super->s_creator_os == EXT2_OS_HURD && |
Theodore Ts'o | 4ea0a11 | 2000-05-08 13:33:17 +0000 | [diff] [blame] | 757 | (fs->super->s_feature_incompat & |
| 758 | EXT2_FEATURE_INCOMPAT_FILETYPE)) { |
| 759 | if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) { |
| 760 | fs->super->s_feature_incompat &= |
| 761 | ~EXT2_FEATURE_INCOMPAT_FILETYPE; |
| 762 | ext2fs_mark_super_dirty(fs); |
Theodore Ts'o | 0cfce7f | 2007-10-06 12:37:08 -0400 | [diff] [blame] | 763 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
Theodore Ts'o | 4ea0a11 | 2000-05-08 13:33:17 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 766 | |
| 767 | /* |
Theodore Ts'o | 7b59f1e | 2000-12-13 18:11:44 +0000 | [diff] [blame] | 768 | * If we have any of the compatibility flags set, we need to have a |
| 769 | * revision 1 filesystem. Most kernels will not check the flags on |
| 770 | * a rev 0 filesystem and we may have corruption issues because of |
| 771 | * the incompatible changes to the filesystem. |
| 772 | */ |
| 773 | if (!(ctx->options & E2F_OPT_READONLY) && |
| 774 | fs->super->s_rev_level == EXT2_GOOD_OLD_REV && |
| 775 | (fs->super->s_feature_compat || |
| 776 | fs->super->s_feature_ro_compat || |
| 777 | fs->super->s_feature_incompat) && |
| 778 | fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) { |
Theodore Ts'o | a917d1c | 2000-12-13 18:36:23 +0000 | [diff] [blame] | 779 | ext2fs_update_dynamic_rev(fs); |
Theodore Ts'o | 7b59f1e | 2000-12-13 18:11:44 +0000 | [diff] [blame] | 780 | ext2fs_mark_super_dirty(fs); |
Theodore Ts'o | 0cfce7f | 2007-10-06 12:37:08 -0400 | [diff] [blame] | 781 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
Theodore Ts'o | 7b59f1e | 2000-12-13 18:11:44 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /* |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 785 | * Clean up any orphan inodes, if present. |
| 786 | */ |
| 787 | if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) { |
| 788 | fs->super->s_state &= ~EXT2_VALID_FS; |
Theodore Ts'o | 7b59f1e | 2000-12-13 18:11:44 +0000 | [diff] [blame] | 789 | ext2fs_mark_super_dirty(fs); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Theodore Ts'o | 60702c2 | 2007-09-22 20:42:04 -0400 | [diff] [blame] | 792 | /* |
Theodore Ts'o | ba5131f | 2009-10-16 20:46:45 -0400 | [diff] [blame] | 793 | * Unfortunately, due to Windows' unfortunate design decision |
| 794 | * to configure the hardware clock to tick localtime, instead |
| 795 | * of the more proper and less error-prone UTC time, many |
| 796 | * users end up in the situation where the system clock is |
| 797 | * incorrectly set at the time when e2fsck is run. |
| 798 | * |
| 799 | * Historically this was usually due to some distributions |
| 800 | * having buggy init scripts and/or installers that didn't |
| 801 | * correctly detect this case and take appropriate |
| 802 | * countermeasures. However, it's still possible, despite the |
| 803 | * best efforts of init script and installer authors to not be |
| 804 | * able to detect this misconfiguration, usually due to a |
| 805 | * buggy or misconfigured virtualization manager or the |
| 806 | * installer not having access to a network time server during |
| 807 | * the installation process. So by default, we allow the |
| 808 | * superblock times to be fudged by up to 24 hours. This can |
| 809 | * be disabled by setting options.accept_time_fudge to the |
| 810 | * boolean value of false in e2fsck.conf. We also support |
| 811 | * options.buggy_init_scripts for backwards compatibility. |
Theodore Ts'o | 60702c2 | 2007-09-22 20:42:04 -0400 | [diff] [blame] | 812 | */ |
Theodore Ts'o | ba5131f | 2009-10-16 20:46:45 -0400 | [diff] [blame] | 813 | profile_get_boolean(ctx->profile, "options", "accept_time_fudge", |
| 814 | 0, 1, &accept_time_fudge); |
Theodore Ts'o | 60702c2 | 2007-09-22 20:42:04 -0400 | [diff] [blame] | 815 | profile_get_boolean(ctx->profile, "options", "buggy_init_scripts", |
Theodore Ts'o | ba5131f | 2009-10-16 20:46:45 -0400 | [diff] [blame] | 816 | 0, accept_time_fudge, &accept_time_fudge); |
| 817 | ctx->time_fudge = accept_time_fudge ? 86400 : 0; |
Theodore Ts'o | 60702c2 | 2007-09-22 20:42:04 -0400 | [diff] [blame] | 818 | |
Theodore Ts'o | 177839e | 2010-05-13 17:36:36 -0400 | [diff] [blame] | 819 | profile_get_boolean(ctx->profile, "options", "broken_system_clock", |
| 820 | 0, 0, &broken_system_clock); |
| 821 | |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 822 | /* |
Theodore Ts'o | 8dceb92 | 2005-09-24 21:59:45 -0400 | [diff] [blame] | 823 | * Check to see if the superblock last mount time or last |
| 824 | * write time is in the future. |
| 825 | */ |
Theodore Ts'o | 177839e | 2010-05-13 17:36:36 -0400 | [diff] [blame] | 826 | if (!broken_system_clock && |
| 827 | !(ctx->flags & E2F_FLAG_TIME_INSANE) && |
| 828 | fs->super->s_mtime > (__u32) ctx->now) { |
Theodore Ts'o | 8dceb92 | 2005-09-24 21:59:45 -0400 | [diff] [blame] | 829 | pctx.num = fs->super->s_mtime; |
Theodore Ts'o | 26ea489 | 2009-07-16 23:44:50 -0400 | [diff] [blame] | 830 | problem = PR_0_FUTURE_SB_LAST_MOUNT; |
| 831 | if (fs->super->s_mtime <= (__u32) ctx->now + ctx->time_fudge) |
| 832 | problem = PR_0_FUTURE_SB_LAST_MOUNT_FUDGED; |
| 833 | if (fix_problem(ctx, problem, &pctx)) { |
Theodore Ts'o | 8dceb92 | 2005-09-24 21:59:45 -0400 | [diff] [blame] | 834 | fs->super->s_mtime = ctx->now; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 835 | fs->flags |= EXT2_FLAG_DIRTY; |
Theodore Ts'o | 8dceb92 | 2005-09-24 21:59:45 -0400 | [diff] [blame] | 836 | } |
| 837 | } |
Theodore Ts'o | 177839e | 2010-05-13 17:36:36 -0400 | [diff] [blame] | 838 | if (!broken_system_clock && |
| 839 | !(ctx->flags & E2F_FLAG_TIME_INSANE) && |
| 840 | fs->super->s_wtime > (__u32) ctx->now) { |
Theodore Ts'o | 8dceb92 | 2005-09-24 21:59:45 -0400 | [diff] [blame] | 841 | pctx.num = fs->super->s_wtime; |
Theodore Ts'o | fe26a55 | 2009-08-08 10:14:48 -0400 | [diff] [blame] | 842 | problem = PR_0_FUTURE_SB_LAST_WRITE; |
Theodore Ts'o | 26ea489 | 2009-07-16 23:44:50 -0400 | [diff] [blame] | 843 | if (fs->super->s_wtime <= (__u32) ctx->now + ctx->time_fudge) |
Theodore Ts'o | 61a9d2b | 2009-10-03 23:17:45 -0400 | [diff] [blame] | 844 | problem = PR_0_FUTURE_SB_LAST_WRITE_FUDGED; |
Theodore Ts'o | 26ea489 | 2009-07-16 23:44:50 -0400 | [diff] [blame] | 845 | if (fix_problem(ctx, problem, &pctx)) { |
Theodore Ts'o | 8dceb92 | 2005-09-24 21:59:45 -0400 | [diff] [blame] | 846 | fs->super->s_wtime = ctx->now; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 847 | fs->flags |= EXT2_FLAG_DIRTY; |
Theodore Ts'o | 8dceb92 | 2005-09-24 21:59:45 -0400 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | |
Theodore Ts'o | 773fd8a | 2001-10-06 21:26:27 -0400 | [diff] [blame] | 851 | /* |
| 852 | * Move the ext3 journal file, if necessary. |
| 853 | */ |
| 854 | e2fsck_move_ext3_journal(ctx); |
Theodore Ts'o | b1c52b2 | 2006-03-10 15:25:59 -0500 | [diff] [blame] | 855 | |
| 856 | /* |
| 857 | * Fix journal hint, if necessary |
| 858 | */ |
| 859 | e2fsck_fix_ext3_journal_hint(ctx); |
| 860 | |
Theodore Ts'o | f77704e | 2006-11-11 22:32:35 -0500 | [diff] [blame] | 861 | /* |
| 862 | * Add dirhash hint if necessary |
| 863 | */ |
| 864 | e2fsck_fix_dirhash_hint(ctx); |
| 865 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 866 | /* |
| 867 | * Hide quota inodes if necessary. |
| 868 | */ |
| 869 | e2fsck_hide_quota(ctx); |
| 870 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 871 | return; |
| 872 | } |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 873 | |
| 874 | /* |
| 875 | * Check to see if we should backup the master sb to the backup super |
Theodore Ts'o | a8cde73 | 2008-01-26 23:17:50 -0500 | [diff] [blame] | 876 | * blocks. Returns non-zero if the sb should be backed up. |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 877 | */ |
Theodore Ts'o | a8cde73 | 2008-01-26 23:17:50 -0500 | [diff] [blame] | 878 | |
| 879 | /* |
| 880 | * A few flags are set on the fly by the kernel, but only in the |
| 881 | * primary superblock. This is actually a bad thing, and we should |
| 882 | * try to discourage it in the future. In particular, for the newer |
| 883 | * ext4 files, especially EXT4_FEATURE_RO_COMPAT_DIR_NLINK and |
| 884 | * EXT3_FEATURE_INCOMPAT_EXTENTS. So some of these may go away in the |
Eric Sandeen | d9ff474 | 2008-11-22 09:02:48 -0600 | [diff] [blame] | 885 | * future. EXT3_FEATURE_INCOMPAT_RECOVER may also get set when |
| 886 | * copying the primary superblock during online resize. |
Theodore Ts'o | a8cde73 | 2008-01-26 23:17:50 -0500 | [diff] [blame] | 887 | * |
| 888 | * The kernel will set EXT2_FEATURE_COMPAT_EXT_ATTR, but |
| 889 | * unfortunately, we shouldn't ignore it since if it's not set in the |
| 890 | * backup, the extended attributes in the filesystem will be stripped |
| 891 | * away. |
| 892 | */ |
| 893 | #define FEATURE_RO_COMPAT_IGNORE (EXT2_FEATURE_RO_COMPAT_LARGE_FILE| \ |
| 894 | EXT4_FEATURE_RO_COMPAT_DIR_NLINK) |
Eric Sandeen | d9ff474 | 2008-11-22 09:02:48 -0600 | [diff] [blame] | 895 | #define FEATURE_INCOMPAT_IGNORE (EXT3_FEATURE_INCOMPAT_EXTENTS| \ |
| 896 | EXT3_FEATURE_INCOMPAT_RECOVER) |
Theodore Ts'o | a8cde73 | 2008-01-26 23:17:50 -0500 | [diff] [blame] | 897 | |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 898 | int check_backup_super_block(e2fsck_t ctx) |
| 899 | { |
| 900 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 901 | errcode_t retval; |
| 902 | dgrp_t g; |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 903 | blk64_t sb; |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 904 | int ret = 0; |
Theodore Ts'o | 982dd30 | 2009-04-23 01:27:07 -0400 | [diff] [blame] | 905 | char buf[SUPERBLOCK_SIZE]; |
| 906 | struct ext2_super_block *backup_sb; |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 907 | |
| 908 | /* |
| 909 | * If we are already writing out the backup blocks, then we |
| 910 | * don't need to test. Also, if the filesystem is invalid, or |
| 911 | * the check was aborted or cancelled, we also don't want to |
| 912 | * do the backup. If the filesystem was opened read-only then |
| 913 | * we can't do the backup. |
| 914 | */ |
| 915 | if (((fs->flags & EXT2_FLAG_MASTER_SB_ONLY) == 0) || |
| 916 | !ext2fs_test_valid(fs) || |
| 917 | (fs->super->s_state & EXT2_ERROR_FS) || |
| 918 | (ctx->flags & (E2F_FLAG_ABORT | E2F_FLAG_CANCEL)) || |
| 919 | (ctx->options & E2F_OPT_READONLY)) |
| 920 | return 0; |
| 921 | |
| 922 | for (g = 1; g < fs->group_desc_count; g++) { |
| 923 | if (!ext2fs_bg_has_super(fs, g)) |
| 924 | continue; |
| 925 | |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 926 | sb = ext2fs_group_first_block2(fs, g); |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 927 | |
Theodore Ts'o | 982dd30 | 2009-04-23 01:27:07 -0400 | [diff] [blame] | 928 | retval = io_channel_read_blk(fs->io, sb, -SUPERBLOCK_SIZE, |
| 929 | buf); |
| 930 | if (retval) |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 931 | continue; |
Theodore Ts'o | 982dd30 | 2009-04-23 01:27:07 -0400 | [diff] [blame] | 932 | backup_sb = (struct ext2_super_block *) buf; |
| 933 | #ifdef WORDS_BIGENDIAN |
| 934 | ext2fs_swap_super(backup_sb); |
| 935 | #endif |
| 936 | if ((backup_sb->s_magic != EXT2_SUPER_MAGIC) || |
| 937 | (backup_sb->s_rev_level > EXT2_LIB_CURRENT_REV) || |
| 938 | ((backup_sb->s_log_block_size + EXT2_MIN_BLOCK_LOG_SIZE) > |
| 939 | EXT2_MAX_BLOCK_LOG_SIZE) || |
| 940 | (EXT2_INODE_SIZE(backup_sb) < EXT2_GOOD_OLD_INODE_SIZE)) |
| 941 | continue; |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 942 | |
Theodore Ts'o | a8cde73 | 2008-01-26 23:17:50 -0500 | [diff] [blame] | 943 | #define SUPER_INCOMPAT_DIFFERENT(x) \ |
Theodore Ts'o | 982dd30 | 2009-04-23 01:27:07 -0400 | [diff] [blame] | 944 | ((fs->super->x & ~FEATURE_INCOMPAT_IGNORE) != \ |
| 945 | (backup_sb->x & ~FEATURE_INCOMPAT_IGNORE)) |
Theodore Ts'o | a8cde73 | 2008-01-26 23:17:50 -0500 | [diff] [blame] | 946 | #define SUPER_RO_COMPAT_DIFFERENT(x) \ |
Theodore Ts'o | 982dd30 | 2009-04-23 01:27:07 -0400 | [diff] [blame] | 947 | ((fs->super->x & ~FEATURE_RO_COMPAT_IGNORE) != \ |
| 948 | (backup_sb->x & ~FEATURE_RO_COMPAT_IGNORE)) |
Theodore Ts'o | a8cde73 | 2008-01-26 23:17:50 -0500 | [diff] [blame] | 949 | #define SUPER_DIFFERENT(x) \ |
Theodore Ts'o | 982dd30 | 2009-04-23 01:27:07 -0400 | [diff] [blame] | 950 | (fs->super->x != backup_sb->x) |
Theodore Ts'o | a8cde73 | 2008-01-26 23:17:50 -0500 | [diff] [blame] | 951 | |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 952 | if (SUPER_DIFFERENT(s_feature_compat) || |
Theodore Ts'o | a8cde73 | 2008-01-26 23:17:50 -0500 | [diff] [blame] | 953 | SUPER_INCOMPAT_DIFFERENT(s_feature_incompat) || |
| 954 | SUPER_RO_COMPAT_DIFFERENT(s_feature_ro_compat) || |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 955 | SUPER_DIFFERENT(s_blocks_count) || |
JP Abgrall | e0ed740 | 2014-03-19 19:08:39 -0700 | [diff] [blame] | 956 | SUPER_DIFFERENT(s_blocks_count_hi) || |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 957 | SUPER_DIFFERENT(s_inodes_count) || |
Theodore Ts'o | 982dd30 | 2009-04-23 01:27:07 -0400 | [diff] [blame] | 958 | memcmp(fs->super->s_uuid, backup_sb->s_uuid, |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 959 | sizeof(fs->super->s_uuid))) |
| 960 | ret = 1; |
Theodore Ts'o | 0c37f45 | 2007-10-01 09:18:54 -0400 | [diff] [blame] | 961 | break; |
| 962 | } |
| 963 | return ret; |
| 964 | } |