Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * e2fsck.c - superblock checks |
| 3 | * |
| 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 |
| 24 | |
| 25 | static void check_super_value(e2fsck_t ctx, const char *descr, |
| 26 | unsigned long value, int flags, |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 27 | unsigned long min_val, unsigned long max_val) |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 28 | { |
| 29 | struct problem_context pctx; |
| 30 | |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 31 | if (((flags & MIN_CHECK) && (value < min_val)) || |
| 32 | ((flags & MAX_CHECK) && (value > max_val))) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 33 | clear_problem_context(&pctx); |
| 34 | pctx.num = value; |
| 35 | pctx.str = descr; |
| 36 | fix_problem(ctx, PR_0_MISC_CORRUPT_SUPER, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 37 | ctx->flags |= E2F_FLAG_ABORT; /* never get here! */ |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
Theodore Ts'o | 4313932 | 1998-02-16 22:34:46 +0000 | [diff] [blame] | 41 | /* |
| 42 | * This routine may get stubbed out in special compilations of the |
| 43 | * e2fsck code.. |
| 44 | */ |
| 45 | #ifndef EXT2_SPECIAL_DEVICE_SIZE |
| 46 | errcode_t e2fsck_get_device_size(e2fsck_t ctx) |
| 47 | { |
| 48 | return (ext2fs_get_device_size(ctx->filesystem_name, |
| 49 | EXT2_BLOCK_SIZE(ctx->fs->super), |
| 50 | &ctx->num_blocks)); |
| 51 | } |
Theodore Ts'o | 4313932 | 1998-02-16 22:34:46 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 54 | /* |
| 55 | * helper function to release an inode |
| 56 | */ |
| 57 | struct process_block_struct { |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 58 | e2fsck_t ctx; |
| 59 | char *buf; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 60 | struct problem_context *pctx; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 61 | int truncating; |
| 62 | int truncate_offset; |
| 63 | blk_t truncate_block; |
| 64 | int truncated_blocks; |
| 65 | int abort; |
| 66 | errcode_t errcode; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | static int release_inode_block(ext2_filsys fs, |
| 70 | blk_t *block_nr, |
| 71 | int blockcnt, |
| 72 | void *priv_data) |
| 73 | { |
| 74 | struct process_block_struct *pb; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 75 | e2fsck_t ctx; |
| 76 | struct problem_context *pctx; |
| 77 | blk_t blk = *block_nr; |
| 78 | int retval = 0; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 79 | |
| 80 | pb = (struct process_block_struct *) priv_data; |
| 81 | ctx = pb->ctx; |
| 82 | pctx = pb->pctx; |
| 83 | |
| 84 | pctx->blk = blk; |
| 85 | pctx->blkcount = blockcnt; |
| 86 | |
| 87 | if (HOLE_BLKADDR(blk)) |
| 88 | return 0; |
| 89 | |
| 90 | if ((blk < fs->super->s_first_data_block) || |
| 91 | (blk >= fs->super->s_blocks_count)) { |
| 92 | fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_BLOCK_NUM, pctx); |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 93 | return_abort: |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 94 | pb->abort = 1; |
| 95 | return BLOCK_ABORT; |
| 96 | } |
| 97 | |
| 98 | if (!ext2fs_test_block_bitmap(fs->block_map, blk)) { |
| 99 | fix_problem(ctx, PR_0_ORPHAN_ALREADY_CLEARED_BLOCK, pctx); |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 100 | goto return_abort; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
| 104 | * If we are deleting an orphan, then we leave the fields alone. |
| 105 | * If we are truncating an orphan, then update the inode fields |
| 106 | * and clean up any partial block data. |
| 107 | */ |
| 108 | if (pb->truncating) { |
| 109 | /* |
| 110 | * We only remove indirect blocks if they are |
| 111 | * completely empty. |
| 112 | */ |
| 113 | if (blockcnt < 0) { |
| 114 | int i, limit; |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 115 | blk_t *bp; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 116 | |
| 117 | pb->errcode = io_channel_read_blk(fs->io, blk, 1, |
| 118 | pb->buf); |
| 119 | if (pb->errcode) |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 120 | goto return_abort; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 121 | |
| 122 | limit = fs->blocksize >> 2; |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 123 | for (i = 0, bp = (blk_t *) pb->buf; |
| 124 | i < limit; i++, bp++) |
| 125 | if (*bp) |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | /* |
| 129 | * We don't remove direct blocks until we've reached |
| 130 | * the truncation block. |
| 131 | */ |
| 132 | if (blockcnt >= 0 && blockcnt < pb->truncate_block) |
| 133 | return 0; |
| 134 | /* |
| 135 | * If part of the last block needs truncating, we do |
| 136 | * it here. |
| 137 | */ |
| 138 | if ((blockcnt == pb->truncate_block) && pb->truncate_offset) { |
| 139 | pb->errcode = io_channel_read_blk(fs->io, blk, 1, |
| 140 | pb->buf); |
| 141 | if (pb->errcode) |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 142 | goto return_abort; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 143 | memset(pb->buf + pb->truncate_offset, 0, |
| 144 | fs->blocksize - pb->truncate_offset); |
| 145 | pb->errcode = io_channel_write_blk(fs->io, blk, 1, |
| 146 | pb->buf); |
| 147 | if (pb->errcode) |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 148 | goto return_abort; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 149 | } |
| 150 | pb->truncated_blocks++; |
| 151 | *block_nr = 0; |
| 152 | retval |= BLOCK_CHANGED; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | ext2fs_unmark_block_bitmap(fs->block_map, blk); |
Theodore Ts'o | ecf1b77 | 2000-08-20 22:06:31 +0000 | [diff] [blame] | 156 | fs->group_desc[ext2fs_group_of_blk(fs, blk)].bg_free_blocks_count++; |
| 157 | fs->super->s_free_blocks_count++; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 158 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 159 | return retval; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
| 163 | * This function releases an inode. Returns 1 if an inconsistency was |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 164 | * found. If the inode has a link count, then it is being truncated and |
| 165 | * not deleted. |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 166 | */ |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 167 | 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] | 168 | struct ext2_inode *inode, char *block_buf, |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 169 | struct problem_context *pctx) |
| 170 | { |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 171 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 172 | errcode_t retval; |
| 173 | struct process_block_struct pb; |
| 174 | |
Theodore Ts'o | 42475e2 | 2001-06-22 20:35:38 -0400 | [diff] [blame] | 175 | if (!ext2fs_inode_has_valid_blocks(inode)) |
| 176 | return 0; |
| 177 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 178 | pb.buf = block_buf + 3 * ctx->fs->blocksize; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 179 | pb.ctx = ctx; |
| 180 | pb.abort = 0; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 181 | pb.errcode = 0; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 182 | pb.pctx = pctx; |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 183 | if (inode->i_links_count) { |
| 184 | pb.truncating = 1; |
| 185 | pb.truncate_block = (blk_t) |
| 186 | ((((long long)inode->i_size_high << 32) + |
| 187 | inode->i_size + fs->blocksize - 1) / |
| 188 | fs->blocksize); |
| 189 | pb.truncate_offset = inode->i_size % fs->blocksize; |
| 190 | } else { |
| 191 | pb.truncating = 0; |
| 192 | pb.truncate_block = 0; |
| 193 | pb.truncate_offset = 0; |
| 194 | } |
| 195 | pb.truncated_blocks = 0; |
| 196 | retval = ext2fs_block_iterate(fs, ino, BLOCK_FLAG_DEPTH_TRAVERSE, |
| 197 | block_buf, release_inode_block, &pb); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 198 | if (retval) { |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 199 | com_err("release_inode_blocks", retval, |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 200 | _("while calling ext2fs_block_iterate for inode %d"), |
| 201 | ino); |
| 202 | return 1; |
| 203 | } |
| 204 | if (pb.abort) |
| 205 | return 1; |
| 206 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 207 | /* Refresh the inode since ext2fs_block_iterate may have changed it */ |
| 208 | e2fsck_read_inode(ctx, ino, inode, "release_inode_blocks"); |
| 209 | |
| 210 | if (pb.truncated_blocks) |
| 211 | inode->i_blocks -= pb.truncated_blocks * |
| 212 | (fs->blocksize / 512); |
| 213 | |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 214 | ext2fs_mark_bb_dirty(fs); |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * This function releases all of the orphan inodes. It returns 1 if |
| 220 | * it hit some error, and 0 on success. |
| 221 | */ |
| 222 | static int release_orphan_inodes(e2fsck_t ctx) |
| 223 | { |
| 224 | ext2_filsys fs = ctx->fs; |
Theodore Ts'o | ecf1b77 | 2000-08-20 22:06:31 +0000 | [diff] [blame] | 225 | int group; |
Theodore Ts'o | 86c627e | 2001-01-11 15:12:14 +0000 | [diff] [blame] | 226 | ext2_ino_t ino, next_ino; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 227 | struct ext2_inode inode; |
| 228 | struct problem_context pctx; |
| 229 | char *block_buf; |
| 230 | |
| 231 | if ((ino = fs->super->s_last_orphan) == 0) |
| 232 | return 0; |
| 233 | |
Theodore Ts'o | 25c63ba | 2000-08-18 15:31:37 +0000 | [diff] [blame] | 234 | /* |
| 235 | * Win or lose, we won't be using the head of the orphan inode |
| 236 | * list again. |
| 237 | */ |
| 238 | fs->super->s_last_orphan = 0; |
| 239 | ext2fs_mark_super_dirty(fs); |
Theodore Ts'o | eb4ab51 | 2001-08-13 10:58:41 -0400 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * If the filesystem contains errors, don't run the orphan |
| 243 | * list, since the orphan list can't be trusted; and we're |
| 244 | * going to be running a full e2fsck run anyway... |
| 245 | */ |
| 246 | if (fs->super->s_state & EXT2_ERROR_FS) |
| 247 | return 0; |
Theodore Ts'o | 25c63ba | 2000-08-18 15:31:37 +0000 | [diff] [blame] | 248 | |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 249 | if ((ino < EXT2_FIRST_INODE(fs->super)) || |
| 250 | (ino > fs->super->s_inodes_count)) { |
| 251 | clear_problem_context(&pctx); |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 252 | pctx.ino = ino; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 253 | fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_HEAD_INODE, &pctx); |
| 254 | return 1; |
| 255 | } |
| 256 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 257 | block_buf = (char *) e2fsck_allocate_memory(ctx, fs->blocksize * 4, |
Theodore Ts'o | 9b56575 | 2000-12-13 18:50:22 +0000 | [diff] [blame] | 258 | "block iterate buffer"); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 259 | e2fsck_read_bitmaps(ctx); |
| 260 | |
| 261 | while (ino) { |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 262 | e2fsck_read_inode(ctx, ino, &inode, "release_orphan_inodes"); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 263 | clear_problem_context(&pctx); |
Theodore Ts'o | ecf1b77 | 2000-08-20 22:06:31 +0000 | [diff] [blame] | 264 | pctx.ino = ino; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 265 | pctx.inode = &inode; |
Theodore Ts'o | f0b8c87 | 2001-05-09 06:03:58 +0000 | [diff] [blame] | 266 | pctx.str = inode.i_links_count ? _("Truncating") : |
| 267 | _("Clearing"); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 268 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 269 | fix_problem(ctx, PR_0_ORPHAN_CLEAR_INODE, &pctx); |
Theodore Ts'o | ecf1b77 | 2000-08-20 22:06:31 +0000 | [diff] [blame] | 270 | |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 271 | next_ino = inode.i_dtime; |
| 272 | if (next_ino && |
Theodore Ts'o | 99a2cc9 | 2000-08-22 21:41:52 +0000 | [diff] [blame] | 273 | ((next_ino < EXT2_FIRST_INODE(fs->super)) || |
| 274 | (next_ino > fs->super->s_inodes_count))) { |
| 275 | pctx.ino = next_ino; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 276 | fix_problem(ctx, PR_0_ORPHAN_ILLEGAL_INODE, &pctx); |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 277 | goto return_abort; |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 280 | if (release_inode_blocks(ctx, ino, &inode, block_buf, &pctx)) |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 281 | goto return_abort; |
Theodore Ts'o | ecf1b77 | 2000-08-20 22:06:31 +0000 | [diff] [blame] | 282 | |
Theodore Ts'o | 8394902 | 2000-10-25 01:38:50 +0000 | [diff] [blame] | 283 | if (!inode.i_links_count) { |
| 284 | ext2fs_unmark_inode_bitmap(fs->inode_map, ino); |
| 285 | ext2fs_mark_ib_dirty(fs); |
| 286 | group = ext2fs_group_of_ino(fs, ino); |
| 287 | fs->group_desc[group].bg_free_inodes_count++; |
| 288 | fs->super->s_free_inodes_count++; |
| 289 | if (LINUX_S_ISDIR(inode.i_mode)) |
| 290 | fs->group_desc[group].bg_used_dirs_count--; |
| 291 | |
| 292 | inode.i_dtime = time(0); |
| 293 | } |
| 294 | e2fsck_write_inode(ctx, ino, &inode, "delete_file"); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 295 | ino = next_ino; |
| 296 | } |
| 297 | return 0; |
Theodore Ts'o | 53ef44c | 2001-01-06 05:55:58 +0000 | [diff] [blame] | 298 | return_abort: |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 299 | ext2fs_free_mem((void **) &block_buf); |
| 300 | return 1; |
| 301 | } |
| 302 | |
| 303 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 304 | void check_super_block(e2fsck_t ctx) |
| 305 | { |
| 306 | ext2_filsys fs = ctx->fs; |
| 307 | blk_t first_block, last_block; |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 308 | struct ext2_super_block *sb = fs->super; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 309 | blk_t blocks_per_group = fs->super->s_blocks_per_group; |
Theodore Ts'o | 78cf054 | 1999-10-20 18:29:18 +0000 | [diff] [blame] | 310 | int inodes_per_block; |
Theodore Ts'o | 7f813ba | 1998-09-03 01:26:03 +0000 | [diff] [blame] | 311 | dgrp_t i; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 312 | blk_t should_be; |
| 313 | struct problem_context pctx; |
Theodore Ts'o | 78cf054 | 1999-10-20 18:29:18 +0000 | [diff] [blame] | 314 | |
Theodore Ts'o | ef05987 | 2001-04-17 00:40:02 +0000 | [diff] [blame] | 315 | inodes_per_block = (EXT2_BLOCK_SIZE(fs->super) / |
| 316 | EXT2_INODE_SIZE(fs->super)); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 317 | |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 318 | ctx->invalid_inode_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 319 | sizeof(int) * fs->group_desc_count, "invalid_inode_bitmap"); |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 320 | ctx->invalid_block_bitmap_flag = (int *) e2fsck_allocate_memory(ctx, |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 321 | sizeof(int) * fs->group_desc_count, "invalid_block_bitmap"); |
Theodore Ts'o | 54dc7ca | 1998-01-19 14:50:49 +0000 | [diff] [blame] | 322 | ctx->invalid_inode_table_flag = (int *) e2fsck_allocate_memory(ctx, |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 323 | sizeof(int) * fs->group_desc_count, "invalid_inode_table"); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 324 | |
| 325 | clear_problem_context(&pctx); |
| 326 | |
| 327 | /* |
| 328 | * Verify the super block constants... |
| 329 | */ |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 330 | check_super_value(ctx, "inodes_count", sb->s_inodes_count, |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 331 | MIN_CHECK, 1, 0); |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 332 | check_super_value(ctx, "blocks_count", sb->s_blocks_count, |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 333 | MIN_CHECK, 1, 0); |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 334 | check_super_value(ctx, "first_data_block", sb->s_first_data_block, |
| 335 | MAX_CHECK, 0, sb->s_blocks_count); |
| 336 | check_super_value(ctx, "log_frag_size", sb->s_log_frag_size, |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 337 | MAX_CHECK, 0, 2); |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 338 | check_super_value(ctx, "log_block_size", sb->s_log_block_size, |
| 339 | MIN_CHECK | MAX_CHECK, sb->s_log_frag_size, |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 340 | 2); |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 341 | check_super_value(ctx, "frags_per_group", sb->s_frags_per_group, |
Theodore Ts'o | ef05987 | 2001-04-17 00:40:02 +0000 | [diff] [blame] | 342 | MIN_CHECK | MAX_CHECK, sb->s_blocks_per_group, |
| 343 | 8 * EXT2_BLOCK_SIZE(sb)); |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 344 | check_super_value(ctx, "blocks_per_group", sb->s_blocks_per_group, |
Theodore Ts'o | ef05987 | 2001-04-17 00:40:02 +0000 | [diff] [blame] | 345 | MIN_CHECK | MAX_CHECK, 8, 8 * EXT2_BLOCK_SIZE(sb)); |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 346 | check_super_value(ctx, "inodes_per_group", sb->s_inodes_per_group, |
Theodore Ts'o | ef05987 | 2001-04-17 00:40:02 +0000 | [diff] [blame] | 347 | MIN_CHECK | MAX_CHECK, inodes_per_block, |
| 348 | inodes_per_block * (blocks_per_group-4)); |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 349 | check_super_value(ctx, "r_blocks_count", sb->s_r_blocks_count, |
Theodore Ts'o | 424cd2b | 2001-05-14 04:06:56 +0000 | [diff] [blame] | 350 | MAX_CHECK, 0, sb->s_blocks_count / 4); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 351 | |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 352 | if (!ctx->num_blocks) { |
Theodore Ts'o | 4313932 | 1998-02-16 22:34:46 +0000 | [diff] [blame] | 353 | pctx.errcode = e2fsck_get_device_size(ctx); |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 354 | if (pctx.errcode && pctx.errcode != EXT2_ET_UNIMPLEMENTED) { |
| 355 | fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 356 | ctx->flags |= E2F_FLAG_ABORT; |
| 357 | return; |
| 358 | } |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 359 | if ((pctx.errcode != EXT2_ET_UNIMPLEMENTED) && |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 360 | (ctx->num_blocks < sb->s_blocks_count)) { |
| 361 | pctx.blk = sb->s_blocks_count; |
Theodore Ts'o | f8188ff | 1997-11-14 05:23:04 +0000 | [diff] [blame] | 362 | pctx.blk2 = ctx->num_blocks; |
| 363 | if (fix_problem(ctx, PR_0_FS_SIZE_WRONG, &pctx)) { |
| 364 | ctx->flags |= E2F_FLAG_ABORT; |
| 365 | return; |
| 366 | } |
| 367 | } |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 370 | if (sb->s_log_block_size != sb->s_log_frag_size) { |
| 371 | pctx.blk = EXT2_BLOCK_SIZE(sb); |
| 372 | pctx.blk2 = EXT2_FRAG_SIZE(sb); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 373 | fix_problem(ctx, PR_0_NO_FRAGMENTS, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 374 | ctx->flags |= E2F_FLAG_ABORT; |
| 375 | return; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 378 | should_be = sb->s_frags_per_group >> |
| 379 | (sb->s_log_block_size - sb->s_log_frag_size); |
| 380 | if (sb->s_blocks_per_group != should_be) { |
| 381 | pctx.blk = sb->s_blocks_per_group; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 382 | pctx.blk2 = should_be; |
| 383 | fix_problem(ctx, PR_0_BLOCKS_PER_GROUP, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 384 | ctx->flags |= E2F_FLAG_ABORT; |
| 385 | return; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 388 | should_be = (sb->s_log_block_size == 0) ? 1 : 0; |
| 389 | if (sb->s_first_data_block != should_be) { |
| 390 | pctx.blk = sb->s_first_data_block; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 391 | pctx.blk2 = should_be; |
| 392 | fix_problem(ctx, PR_0_FIRST_DATA_BLOCK, &pctx); |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 393 | ctx->flags |= E2F_FLAG_ABORT; |
| 394 | return; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 397 | should_be = sb->s_inodes_per_group * fs->group_desc_count; |
| 398 | if (sb->s_inodes_count != should_be) { |
| 399 | pctx.ino = sb->s_inodes_count; |
Theodore Ts'o | d4b0ce0 | 1999-06-18 01:09:29 +0000 | [diff] [blame] | 400 | pctx.ino2 = should_be; |
| 401 | if (fix_problem(ctx, PR_0_INODE_COUNT_WRONG, &pctx)) { |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 402 | sb->s_inodes_count = should_be; |
Theodore Ts'o | d4b0ce0 | 1999-06-18 01:09:29 +0000 | [diff] [blame] | 403 | ext2fs_mark_super_dirty(fs); |
| 404 | } |
| 405 | } |
| 406 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 407 | /* |
| 408 | * Verify the group descriptors.... |
| 409 | */ |
| 410 | first_block = fs->super->s_first_data_block; |
| 411 | last_block = first_block + blocks_per_group; |
| 412 | |
| 413 | for (i = 0; i < fs->group_desc_count; i++) { |
| 414 | pctx.group = i; |
| 415 | |
| 416 | if (i == fs->group_desc_count - 1) |
| 417 | last_block = fs->super->s_blocks_count; |
| 418 | if ((fs->group_desc[i].bg_block_bitmap < first_block) || |
| 419 | (fs->group_desc[i].bg_block_bitmap >= last_block)) { |
| 420 | pctx.blk = fs->group_desc[i].bg_block_bitmap; |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 421 | if (fix_problem(ctx, PR_0_BB_NOT_GROUP, &pctx)) |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 422 | fs->group_desc[i].bg_block_bitmap = 0; |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 423 | } |
| 424 | if (fs->group_desc[i].bg_block_bitmap == 0) { |
| 425 | ctx->invalid_block_bitmap_flag[i]++; |
| 426 | ctx->invalid_bitmaps++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 427 | } |
| 428 | if ((fs->group_desc[i].bg_inode_bitmap < first_block) || |
| 429 | (fs->group_desc[i].bg_inode_bitmap >= last_block)) { |
| 430 | pctx.blk = fs->group_desc[i].bg_inode_bitmap; |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 431 | if (fix_problem(ctx, PR_0_IB_NOT_GROUP, &pctx)) |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 432 | fs->group_desc[i].bg_inode_bitmap = 0; |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 433 | } |
| 434 | if (fs->group_desc[i].bg_inode_bitmap == 0) { |
| 435 | ctx->invalid_inode_bitmap_flag[i]++; |
| 436 | ctx->invalid_bitmaps++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 437 | } |
| 438 | if ((fs->group_desc[i].bg_inode_table < first_block) || |
| 439 | ((fs->group_desc[i].bg_inode_table + |
| 440 | fs->inode_blocks_per_group - 1) >= last_block)) { |
| 441 | pctx.blk = fs->group_desc[i].bg_inode_table; |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 442 | if (fix_problem(ctx, PR_0_ITABLE_NOT_GROUP, &pctx)) |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 443 | fs->group_desc[i].bg_inode_table = 0; |
Theodore Ts'o | 24bfb44 | 2001-11-26 15:51:14 -0500 | [diff] [blame] | 444 | } |
| 445 | if (fs->group_desc[i].bg_inode_table == 0) { |
| 446 | ctx->invalid_inode_table_flag[i]++; |
| 447 | ctx->invalid_bitmaps++; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 448 | } |
| 449 | first_block += fs->super->s_blocks_per_group; |
| 450 | last_block += fs->super->s_blocks_per_group; |
| 451 | } |
| 452 | /* |
| 453 | * If we have invalid bitmaps, set the error state of the |
| 454 | * filesystem. |
| 455 | */ |
| 456 | if (ctx->invalid_bitmaps && !(ctx->options & E2F_OPT_READONLY)) { |
| 457 | fs->super->s_state &= ~EXT2_VALID_FS; |
| 458 | ext2fs_mark_super_dirty(fs); |
| 459 | } |
| 460 | |
Theodore Ts'o | 4ea0a11 | 2000-05-08 13:33:17 +0000 | [diff] [blame] | 461 | clear_problem_context(&pctx); |
| 462 | |
Theodore Ts'o | 54be2cc | 1998-02-01 12:58:48 +0000 | [diff] [blame] | 463 | #ifndef EXT2_SKIP_UUID |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 464 | /* |
| 465 | * If the UUID field isn't assigned, assign it. |
| 466 | */ |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 467 | 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] | 468 | if (fix_problem(ctx, PR_0_ADD_UUID, &pctx)) { |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 469 | uuid_generate(sb->s_uuid); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 470 | ext2fs_mark_super_dirty(fs); |
| 471 | } |
| 472 | } |
Theodore Ts'o | 54be2cc | 1998-02-01 12:58:48 +0000 | [diff] [blame] | 473 | #endif |
Theodore Ts'o | 4ea0a11 | 2000-05-08 13:33:17 +0000 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | * For the Hurd, check to see if the filetype option is set, |
| 477 | * since it doesn't support it. |
| 478 | */ |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 479 | if (!(ctx->options & E2F_OPT_READONLY) && |
| 480 | fs->super->s_creator_os == EXT2_OS_HURD && |
Theodore Ts'o | 4ea0a11 | 2000-05-08 13:33:17 +0000 | [diff] [blame] | 481 | (fs->super->s_feature_incompat & |
| 482 | EXT2_FEATURE_INCOMPAT_FILETYPE)) { |
| 483 | if (fix_problem(ctx, PR_0_HURD_CLEAR_FILETYPE, &pctx)) { |
| 484 | fs->super->s_feature_incompat &= |
| 485 | ~EXT2_FEATURE_INCOMPAT_FILETYPE; |
| 486 | ext2fs_mark_super_dirty(fs); |
| 487 | |
| 488 | } |
| 489 | } |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 490 | |
| 491 | /* |
Theodore Ts'o | 7b59f1e | 2000-12-13 18:11:44 +0000 | [diff] [blame] | 492 | * If we have any of the compatibility flags set, we need to have a |
| 493 | * revision 1 filesystem. Most kernels will not check the flags on |
| 494 | * a rev 0 filesystem and we may have corruption issues because of |
| 495 | * the incompatible changes to the filesystem. |
| 496 | */ |
| 497 | if (!(ctx->options & E2F_OPT_READONLY) && |
| 498 | fs->super->s_rev_level == EXT2_GOOD_OLD_REV && |
| 499 | (fs->super->s_feature_compat || |
| 500 | fs->super->s_feature_ro_compat || |
| 501 | fs->super->s_feature_incompat) && |
| 502 | fix_problem(ctx, PR_0_FS_REV_LEVEL, &pctx)) { |
Theodore Ts'o | a917d1c | 2000-12-13 18:36:23 +0000 | [diff] [blame] | 503 | ext2fs_update_dynamic_rev(fs); |
Theodore Ts'o | 7b59f1e | 2000-12-13 18:11:44 +0000 | [diff] [blame] | 504 | ext2fs_mark_super_dirty(fs); |
| 505 | } |
| 506 | |
| 507 | /* |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 508 | * Clean up any orphan inodes, if present. |
| 509 | */ |
| 510 | if (!(ctx->options & E2F_OPT_READONLY) && release_orphan_inodes(ctx)) { |
| 511 | fs->super->s_state &= ~EXT2_VALID_FS; |
Theodore Ts'o | 7b59f1e | 2000-12-13 18:11:44 +0000 | [diff] [blame] | 512 | ext2fs_mark_super_dirty(fs); |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Theodore Ts'o | 773fd8a | 2001-10-06 21:26:27 -0400 | [diff] [blame] | 515 | /* |
| 516 | * Move the ext3 journal file, if necessary. |
| 517 | */ |
| 518 | e2fsck_move_ext3_journal(ctx); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 519 | return; |
| 520 | } |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 521 | |
| 522 | |