journal.c revision 546a1ff18cc912003883ff67ba3e87c69f700fc4
1/* 2 * journal.c --- code for handling the "ext3" journal 3 * 4 * Copyright (C) 2000 Andreas Dilger 5 * Copyright (C) 2000 Theodore Ts'o 6 * 7 * Parts of the code are based on fs/jfs/journal.c by Stephen C. Tweedie 8 * Copyright (C) 1999 Red Hat Software 9 * 10 * This file may be redistributed under the terms of the 11 * GNU General Public License version 2 or at your discretion 12 * any later version. 13 */ 14 15#ifdef HAVE_SYS_MOUNT_H 16#include <sys/mount.h> 17#define MNT_FL (MS_MGC_VAL | MS_RDONLY) 18#endif 19#ifdef HAVE_SYS_STAT_H 20#include <sys/stat.h> 21#endif 22 23#define E2FSCK_INCLUDE_INLINE_FUNCS 24#include "jfs_user.h" 25#include "problem.h" 26#include "uuid/uuid.h" 27 28#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */ 29static int bh_count = 0; 30int journal_enable_debug = 2; 31#endif 32 33/* 34 * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths. 35 * This creates a larger static binary, and a smaller binary using 36 * shared libraries. It's also probably slightly less CPU-efficient, 37 * which is why it's not on by default. But, it's a good way of 38 * testing the functions in inode_io.c and fileio.c. 39 */ 40#undef USE_INODE_IO 41 42/* Kernel compatibility functions for handling the journal. These allow us 43 * to use the recovery.c file virtually unchanged from the kernel, so we 44 * don't have to do much to keep kernel and user recovery in sync. 45 */ 46int journal_bmap(journal_t *journal, blk_t block, unsigned long *phys) 47{ 48#ifdef USE_INODE_IO 49 *phys = block; 50 return 0; 51#else 52 struct inode *inode = journal->j_inode; 53 errcode_t retval; 54 blk_t pblk; 55 56 if (!inode) { 57 *phys = block; 58 return 0; 59 } 60 61 retval= ext2fs_bmap(inode->i_ctx->fs, inode->i_ino, 62 &inode->i_ext2, NULL, 0, block, &pblk); 63 *phys = pblk; 64 return (retval); 65#endif 66} 67 68struct buffer_head *getblk(kdev_t kdev, blk_t blocknr, int blocksize) 69{ 70 struct buffer_head *bh; 71 72 bh = e2fsck_allocate_memory(kdev->k_ctx, sizeof(*bh), "block buffer"); 73 if (!bh) 74 return NULL; 75 76 jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n", 77 (unsigned long) blocknr, blocksize, ++bh_count); 78 79 bh->b_ctx = kdev->k_ctx; 80 if (kdev->k_dev == K_DEV_FS) 81 bh->b_io = kdev->k_ctx->fs->io; 82 else 83 bh->b_io = kdev->k_ctx->journal_io; 84 bh->b_size = blocksize; 85 bh->b_blocknr = blocknr; 86 87 return bh; 88} 89 90void ll_rw_block(int rw, int nr, struct buffer_head *bhp[]) 91{ 92 int retval; 93 struct buffer_head *bh; 94 95 for (; nr > 0; --nr) { 96 bh = *bhp++; 97 if (rw == READ && !bh->b_uptodate) { 98 jfs_debug(3, "reading block %lu/%p\n", 99 (unsigned long) bh->b_blocknr, (void *) bh); 100 retval = io_channel_read_blk(bh->b_io, 101 bh->b_blocknr, 102 1, bh->b_data); 103 if (retval) { 104 com_err(bh->b_ctx->device_name, retval, 105 "while reading block %ld\n", 106 bh->b_blocknr); 107 bh->b_err = retval; 108 continue; 109 } 110 bh->b_uptodate = 1; 111 } else if (rw == WRITE && bh->b_dirty) { 112 jfs_debug(3, "writing block %lu/%p\n", 113 (unsigned long) bh->b_blocknr, (void *) bh); 114 retval = io_channel_write_blk(bh->b_io, 115 bh->b_blocknr, 116 1, bh->b_data); 117 if (retval) { 118 com_err(bh->b_ctx->device_name, retval, 119 "while writing block %ld\n", 120 bh->b_blocknr); 121 bh->b_err = retval; 122 continue; 123 } 124 bh->b_dirty = 0; 125 bh->b_uptodate = 1; 126 } else 127 jfs_debug(3, "no-op %s for block %lu\n", 128 rw == READ ? "read" : "write", 129 (unsigned long) bh->b_blocknr); 130 } 131} 132 133void mark_buffer_dirty(struct buffer_head *bh) 134{ 135 bh->b_dirty = 1; 136} 137 138static void mark_buffer_clean(struct buffer_head * bh) 139{ 140 bh->b_dirty = 0; 141} 142 143void brelse(struct buffer_head *bh) 144{ 145 if (bh->b_dirty) 146 ll_rw_block(WRITE, 1, &bh); 147 jfs_debug(3, "freeing block %lu/%p (total %d)\n", 148 (unsigned long) bh->b_blocknr, (void *) bh, --bh_count); 149 ext2fs_free_mem((void **) &bh); 150} 151 152int buffer_uptodate(struct buffer_head *bh) 153{ 154 return bh->b_uptodate; 155} 156 157void mark_buffer_uptodate(struct buffer_head *bh, int val) 158{ 159 bh->b_uptodate = val; 160} 161 162void wait_on_buffer(struct buffer_head *bh) 163{ 164 if (!bh->b_uptodate) 165 ll_rw_block(READ, 1, &bh); 166} 167 168 169static void e2fsck_clear_recover(e2fsck_t ctx, int error) 170{ 171 ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER; 172 173 /* if we had an error doing journal recovery, we need a full fsck */ 174 if (error) 175 ctx->fs->super->s_state &= ~EXT2_VALID_FS; 176 ext2fs_mark_super_dirty(ctx->fs); 177} 178 179static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal) 180{ 181 struct ext2_super_block *sb = ctx->fs->super; 182 struct ext2_super_block jsuper; 183 struct problem_context pctx; 184 struct buffer_head *bh; 185 struct inode *j_inode = NULL; 186 struct kdev_s *dev_fs = NULL, *dev_journal; 187 char *journal_name = 0; 188 journal_t *journal = NULL; 189 errcode_t retval; 190 io_manager io_ptr; 191 unsigned long start = 0; 192 int free_journal_name = 0; 193 int ext_journal = 0; 194 195 clear_problem_context(&pctx); 196 197 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal"); 198 if (!journal) { 199 return EXT2_ET_NO_MEMORY; 200 } 201 202 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev"); 203 if (!dev_fs) { 204 retval = EXT2_ET_NO_MEMORY; 205 goto errout; 206 } 207 dev_journal = dev_fs+1; 208 209 dev_fs->k_ctx = dev_journal->k_ctx = ctx; 210 dev_fs->k_dev = K_DEV_FS; 211 dev_journal->k_dev = K_DEV_JOURNAL; 212 213 journal->j_dev = dev_journal; 214 journal->j_fs_dev = dev_fs; 215 journal->j_inode = NULL; 216 journal->j_blocksize = ctx->fs->blocksize; 217 218 if (uuid_is_null(sb->s_journal_uuid)) { 219 if (!sb->s_journal_inum) 220 return EXT2_ET_BAD_INODE_NUM; 221 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode), 222 "journal inode"); 223 if (!j_inode) { 224 retval = EXT2_ET_NO_MEMORY; 225 goto errout; 226 } 227 228 j_inode->i_ctx = ctx; 229 j_inode->i_ino = sb->s_journal_inum; 230 231 if ((retval = ext2fs_read_inode(ctx->fs, 232 sb->s_journal_inum, 233 &j_inode->i_ext2))) 234 goto errout; 235 if (!j_inode->i_ext2.i_links_count || 236 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) { 237 retval = EXT2_ET_NO_JOURNAL; 238 goto errout; 239 } 240 if (j_inode->i_ext2.i_size / journal->j_blocksize < 241 JFS_MIN_JOURNAL_BLOCKS) { 242 retval = EXT2_ET_JOURNAL_TOO_SMALL; 243 goto errout; 244 } 245 246 journal->j_maxlen = j_inode->i_ext2.i_size / journal->j_blocksize; 247 248#ifdef USE_INODE_IO 249 retval = ext2fs_inode_io_intern(ctx->fs, sb->s_journal_inum, 250 &journal_name); 251 if (retval) 252 goto errout; 253 254 io_ptr = inode_io_manager; 255#else 256 journal->j_inode = j_inode; 257 ctx->journal_io = ctx->fs->io; 258 if ((retval = journal_bmap(journal, 0, &start)) != 0) 259 goto errout; 260#endif 261 } else { 262 ext_journal = 1; 263 journal_name = ctx->journal_name; 264 if (!journal_name) { 265 journal_name = ext2fs_find_block_device(sb->s_journal_dev); 266 free_journal_name = 1; 267 } 268 269 if (!journal_name) { 270 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx); 271 return EXT2_ET_LOAD_EXT_JOURNAL; 272 } 273 274 jfs_debug(1, "Using journal file %s\n", journal_name); 275 io_ptr = unix_io_manager; 276 } 277 278#if 0 279 test_io_backing_manager = io_ptr; 280 io_ptr = test_io_manager; 281#endif 282#ifndef USE_INODE_IO 283 if (ext_journal) 284#endif 285 retval = io_ptr->open(journal_name, IO_FLAG_RW, 286 &ctx->journal_io); 287 if (free_journal_name) 288 free((void *) journal_name); 289 if (retval) 290 goto errout; 291 292 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize); 293 294 if (ext_journal) { 295 if (ctx->fs->blocksize == 1024) 296 start = 1; 297 bh = getblk(dev_journal, start, ctx->fs->blocksize); 298 if (!bh) { 299 retval = EXT2_ET_NO_MEMORY; 300 goto errout; 301 } 302 ll_rw_block(READ, 1, &bh); 303 if ((retval = bh->b_err) != 0) 304 goto errout; 305 memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024, 306 sizeof(jsuper)); 307 brelse(bh); 308#ifdef EXT2FS_ENABLE_SWAPFS 309 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) 310 ext2fs_swap_super(&jsuper); 311#endif 312 if (jsuper.s_magic != EXT2_SUPER_MAGIC || 313 !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) { 314 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx); 315 retval = EXT2_ET_LOAD_EXT_JOURNAL; 316 goto errout; 317 } 318 /* Make sure the journal UUID is correct */ 319 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid, 320 sizeof(jsuper.s_uuid))) { 321 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx); 322 retval = EXT2_ET_LOAD_EXT_JOURNAL; 323 goto errout; 324 } 325 326 journal->j_maxlen = jsuper.s_blocks_count; 327 start++; 328 } 329 330 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) { 331 retval = EXT2_ET_NO_MEMORY; 332 goto errout; 333 } 334 335 journal->j_sb_buffer = bh; 336 journal->j_superblock = (journal_superblock_t *)bh->b_data; 337 338#ifdef USE_INODE_IO 339 if (j_inode) 340 ext2fs_free_mem((void **)&j_inode); 341#endif 342 343 *ret_journal = journal; 344 return 0; 345 346errout: 347 if (dev_fs) 348 ext2fs_free_mem((void **)&dev_fs); 349 if (j_inode) 350 ext2fs_free_mem((void **)&j_inode); 351 if (journal) 352 ext2fs_free_mem((void **)&journal); 353 return retval; 354 355} 356 357static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx, 358 struct problem_context *pctx) 359{ 360 struct ext2_super_block *sb = ctx->fs->super; 361 int recover = ctx->fs->super->s_feature_incompat & 362 EXT3_FEATURE_INCOMPAT_RECOVER; 363 int has_journal = ctx->fs->super->s_feature_compat & 364 EXT3_FEATURE_COMPAT_HAS_JOURNAL; 365 366 if (has_journal || sb->s_journal_inum) { 367 /* The journal inode is bogus, remove and force full fsck */ 368 pctx->ino = sb->s_journal_inum; 369 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) { 370 if (has_journal && sb->s_journal_inum) 371 printf("*** ext3 journal has been deleted - " 372 "filesystem is now ext2 only ***\n\n"); 373 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL; 374 sb->s_journal_inum = 0; 375 ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */ 376 e2fsck_clear_recover(ctx, 1); 377 return 0; 378 } 379 return EXT2_ET_BAD_INODE_NUM; 380 } else if (recover) { 381 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) { 382 e2fsck_clear_recover(ctx, 1); 383 return 0; 384 } 385 return EXT2_ET_UNSUPP_FEATURE; 386 } 387 return 0; 388} 389 390#define V1_SB_SIZE 0x0024 391static void clear_v2_journal_fields(journal_t *journal) 392{ 393 e2fsck_t ctx = journal->j_dev->k_ctx; 394 struct problem_context pctx; 395 396 clear_problem_context(&pctx); 397 398 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx)) 399 return; 400 401 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0, 402 ctx->fs->blocksize-V1_SB_SIZE); 403 mark_buffer_dirty(journal->j_sb_buffer); 404} 405 406 407static errcode_t e2fsck_journal_load(journal_t *journal) 408{ 409 e2fsck_t ctx = journal->j_dev->k_ctx; 410 journal_superblock_t *jsb; 411 struct buffer_head *jbh = journal->j_sb_buffer; 412 struct problem_context pctx; 413 414 clear_problem_context(&pctx); 415 416 ll_rw_block(READ, 1, &jbh); 417 if (jbh->b_err) { 418 com_err(ctx->device_name, jbh->b_err, 419 _("reading journal superblock\n")); 420 return jbh->b_err; 421 } 422 423 jsb = journal->j_superblock; 424 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */ 425 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER)) 426 return e2fsck_journal_fix_bad_inode(ctx, &pctx); 427 428 switch (ntohl(jsb->s_header.h_blocktype)) { 429 case JFS_SUPERBLOCK_V1: 430 journal->j_format_version = 1; 431 if (jsb->s_feature_compat || 432 jsb->s_feature_incompat || 433 jsb->s_feature_ro_compat || 434 jsb->s_nr_users) 435 clear_v2_journal_fields(journal); 436 break; 437 438 case JFS_SUPERBLOCK_V2: 439 journal->j_format_version = 2; 440 if (ntohl(jsb->s_nr_users) > 1 && 441 (ctx->fs->io == ctx->journal_io)) 442 clear_v2_journal_fields(journal); 443 if (ntohl(jsb->s_nr_users) > 1) { 444 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx); 445 return EXT2_ET_JOURNAL_UNSUPP_VERSION; 446 } 447 break; 448 449 /* 450 * These should never appear in a journal super block, so if 451 * they do, the journal is badly corrupted. 452 */ 453 case JFS_DESCRIPTOR_BLOCK: 454 case JFS_COMMIT_BLOCK: 455 case JFS_REVOKE_BLOCK: 456 return EXT2_ET_CORRUPT_SUPERBLOCK; 457 458 /* If we don't understand the superblock major type, but there 459 * is a magic number, then it is likely to be a new format we 460 * just don't understand, so leave it alone. */ 461 default: 462 return EXT2_ET_JOURNAL_UNSUPP_VERSION; 463 } 464 465 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES)) 466 return EXT2_ET_UNSUPP_FEATURE; 467 468 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES)) 469 return EXT2_ET_RO_UNSUPP_FEATURE; 470 471 /* We have now checked whether we know enough about the journal 472 * format to be able to proceed safely, so any other checks that 473 * fail we should attempt to recover from. */ 474 if (jsb->s_blocksize != htonl(journal->j_blocksize)) { 475 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK, 476 _("%s: no valid journal superblock found\n"), 477 ctx->device_name); 478 return EXT2_ET_CORRUPT_SUPERBLOCK; 479 } 480 481 if (ntohl(jsb->s_maxlen) < journal->j_maxlen) 482 journal->j_maxlen = ntohl(jsb->s_maxlen); 483 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) { 484 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK, 485 _("%s: journal too short\n"), 486 ctx->device_name); 487 return EXT2_ET_CORRUPT_SUPERBLOCK; 488 } 489 490 journal->j_tail_sequence = ntohl(jsb->s_sequence); 491 journal->j_transaction_sequence = journal->j_tail_sequence; 492 journal->j_tail = ntohl(jsb->s_start); 493 journal->j_first = ntohl(jsb->s_first); 494 journal->j_last = ntohl(jsb->s_maxlen); 495 496 return 0; 497} 498 499static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb, 500 journal_t *journal) 501{ 502 char *p; 503 union { 504 uuid_t uuid; 505 __u32 val[4]; 506 } u; 507 __u32 new_seq = 0; 508 int i; 509 510 /* Leave a valid existing V1 superblock signature alone. 511 * Anything unrecognisable we overwrite with a new V2 512 * signature. */ 513 514 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) || 515 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) { 516 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER); 517 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2); 518 } 519 520 /* Zero out everything else beyond the superblock header */ 521 522 p = ((char *) jsb) + sizeof(journal_header_t); 523 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t)); 524 525 jsb->s_blocksize = htonl(ctx->fs->blocksize); 526 jsb->s_maxlen = htonl(journal->j_maxlen); 527 jsb->s_first = htonl(1); 528 529 /* Initialize the journal sequence number so that there is "no" 530 * chance we will find old "valid" transactions in the journal. 531 * This avoids the need to zero the whole journal (slow to do, 532 * and risky when we are just recovering the filesystem). 533 */ 534 uuid_generate(u.uuid); 535 for (i = 0; i < 4; i ++) 536 new_seq ^= u.val[i]; 537 jsb->s_sequence = htonl(new_seq); 538 539 mark_buffer_dirty(journal->j_sb_buffer); 540 ll_rw_block(WRITE, 1, &journal->j_sb_buffer); 541} 542 543static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx, 544 journal_t *journal, 545 struct problem_context *pctx) 546{ 547 struct ext2_super_block *sb = ctx->fs->super; 548 int recover = ctx->fs->super->s_feature_incompat & 549 EXT3_FEATURE_INCOMPAT_RECOVER; 550 551 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) { 552 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) { 553 e2fsck_journal_reset_super(ctx, journal->j_superblock, 554 journal); 555 journal->j_transaction_sequence = 1; 556 e2fsck_clear_recover(ctx, recover); 557 return 0; 558 } 559 return EXT2_ET_CORRUPT_SUPERBLOCK; 560 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx)) 561 return EXT2_ET_CORRUPT_SUPERBLOCK; 562 563 return 0; 564} 565 566static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal, 567 int reset, int drop) 568{ 569 journal_superblock_t *jsb; 570 571 if (drop) 572 mark_buffer_clean(journal->j_sb_buffer); 573 else if (!(ctx->options & E2F_OPT_READONLY)) { 574 jsb = journal->j_superblock; 575 jsb->s_sequence = htonl(journal->j_transaction_sequence); 576 if (reset) 577 jsb->s_start = 0; /* this marks the journal as empty */ 578 mark_buffer_dirty(journal->j_sb_buffer); 579 } 580 brelse(journal->j_sb_buffer); 581 582 if (ctx->journal_io) { 583 if (ctx->fs && ctx->fs->io != ctx->journal_io) 584 io_channel_close(ctx->journal_io); 585 ctx->journal_io = 0; 586 } 587 588#ifndef USE_INODE_IO 589 if (journal->j_inode) 590 ext2fs_free_mem((void **)&journal->j_inode); 591#endif 592 if (journal->j_fs_dev) 593 ext2fs_free_mem((void **)&journal->j_fs_dev); 594 ext2fs_free_mem((void **)&journal); 595} 596 597/* 598 * This function makes sure that the superblock fields regarding the 599 * journal are consistent. 600 */ 601int e2fsck_check_ext3_journal(e2fsck_t ctx) 602{ 603 struct ext2_super_block *sb = ctx->fs->super; 604 journal_t *journal; 605 int recover = ctx->fs->super->s_feature_incompat & 606 EXT3_FEATURE_INCOMPAT_RECOVER; 607 struct problem_context pctx; 608 problem_t problem; 609 int reset = 0, force_fsck = 0; 610 int retval; 611 612 /* If we don't have any journal features, don't do anything more */ 613 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) && 614 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 && 615 uuid_is_null(sb->s_journal_uuid)) 616 return 0; 617 618 clear_problem_context(&pctx); 619 pctx.num = sb->s_journal_inum; 620 621 retval = e2fsck_get_journal(ctx, &journal); 622 if (retval) { 623 if ((retval == EXT2_ET_BAD_INODE_NUM) || 624 (retval == EXT2_ET_JOURNAL_TOO_SMALL) || 625 (retval == EXT2_ET_NO_JOURNAL)) 626 return e2fsck_journal_fix_bad_inode(ctx, &pctx); 627 return retval; 628 } 629 630 retval = e2fsck_journal_load(journal); 631 if (retval) { 632 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) || 633 ((retval == EXT2_ET_UNSUPP_FEATURE) && 634 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT, 635 &pctx))) || 636 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) && 637 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT, 638 &pctx))) || 639 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) && 640 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx)))) 641 retval = e2fsck_journal_fix_corrupt_super(ctx, journal, 642 &pctx); 643 e2fsck_journal_release(ctx, journal, 0, 1); 644 return retval; 645 } 646 647 /* 648 * We want to make the flags consistent here. We will not leave with 649 * needs_recovery set but has_journal clear. We can't get in a loop 650 * with -y, -n, or -p, only if a user isn't making up their mind. 651 */ 652no_has_journal: 653 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) { 654 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER; 655 pctx.str = "inode"; 656 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) { 657 if (recover && 658 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx)) 659 goto no_has_journal; 660 /* 661 * Need a full fsck if we are releasing a 662 * journal stored on a reserved inode. 663 */ 664 force_fsck = recover || 665 (sb->s_journal_inum < EXT2_FIRST_INODE(sb)); 666 /* Clear all of the journal fields */ 667 sb->s_journal_inum = 0; 668 sb->s_journal_dev = 0; 669 memset(sb->s_journal_uuid, 0, 670 sizeof(sb->s_journal_uuid)); 671 e2fsck_clear_recover(ctx, force_fsck); 672 } else if (!(ctx->options & E2F_OPT_READONLY)) { 673 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL; 674 ext2fs_mark_super_dirty(ctx->fs); 675 } 676 } 677 678 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL && 679 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) && 680 journal->j_superblock->s_start != 0) { 681 /* Print status information */ 682 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx); 683 if (ctx->superblock) 684 problem = PR_0_JOURNAL_RUN_DEFAULT; 685 else 686 problem = PR_0_JOURNAL_RUN; 687 if (fix_problem(ctx, problem, &pctx)) { 688 ctx->options |= E2F_OPT_FORCE; 689 sb->s_feature_incompat |= 690 EXT3_FEATURE_INCOMPAT_RECOVER; 691 ext2fs_mark_super_dirty(ctx->fs); 692 } else if (fix_problem(ctx, 693 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) { 694 reset = 1; 695 sb->s_state &= ~EXT2_VALID_FS; 696 ext2fs_mark_super_dirty(ctx->fs); 697 } 698 /* 699 * If the user answers no to the above question, we 700 * ignore the fact that journal apparently has data; 701 * accidentally replaying over valid data would be far 702 * worse than skipping a questionable recovery. 703 * 704 * XXX should we abort with a fatal error here? What 705 * will the ext3 kernel code do if a filesystem with 706 * !NEEDS_RECOVERY but with a non-zero 707 * journal->j_superblock->s_start is mounted? 708 */ 709 } 710 711 e2fsck_journal_release(ctx, journal, reset, 0); 712 return retval; 713} 714 715static errcode_t recover_ext3_journal(e2fsck_t ctx) 716{ 717 journal_t *journal; 718 int retval; 719 720 journal_init_revoke_caches(); 721 retval = e2fsck_get_journal(ctx, &journal); 722 if (retval) 723 return retval; 724 725 retval = e2fsck_journal_load(journal); 726 if (retval) 727 goto errout; 728 729 retval = journal_init_revoke(journal, 1024); 730 if (retval) 731 goto errout; 732 733 retval = -journal_recover(journal); 734 if (retval) 735 goto errout; 736 737 if (journal->j_superblock->s_errno) { 738 ctx->fs->super->s_state |= EXT2_ERROR_FS; 739 ext2fs_mark_super_dirty(ctx->fs); 740 journal->j_superblock->s_errno = 0; 741 mark_buffer_dirty(journal->j_sb_buffer); 742 } 743 744errout: 745 journal_destroy_revoke(journal); 746 journal_destroy_revoke_caches(); 747 e2fsck_journal_release(ctx, journal, 1, 0); 748 return retval; 749} 750 751int e2fsck_run_ext3_journal(e2fsck_t ctx) 752{ 753 io_manager io_ptr = ctx->fs->io->manager; 754 int blocksize = ctx->fs->blocksize; 755 errcode_t retval, recover_retval; 756 757 printf(_("%s: recovering journal\n"), ctx->device_name); 758 if (ctx->options & E2F_OPT_READONLY) { 759 printf(_("%s: won't do journal recovery while read-only\n"), 760 ctx->device_name); 761 return EXT2_ET_FILE_RO; 762 } 763 764 if (ctx->fs->flags & EXT2_FLAG_DIRTY) 765 ext2fs_flush(ctx->fs); /* Force out any modifications */ 766 767 recover_retval = recover_ext3_journal(ctx); 768 769 /* 770 * Reload the filesystem context to get up-to-date data from disk 771 * because journal recovery will change the filesystem under us. 772 */ 773 ext2fs_close(ctx->fs); 774 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW, 775 ctx->superblock, blocksize, io_ptr, 776 &ctx->fs); 777 778 if (retval) { 779 com_err(ctx->program_name, retval, 780 _("while trying to re-open %s"), 781 ctx->device_name); 782 fatal_error(ctx, 0); 783 } 784 ctx->fs->priv_data = ctx; 785 786 /* Set the superblock flags */ 787 e2fsck_clear_recover(ctx, recover_retval); 788 return recover_retval; 789} 790 791/* 792 * This function will move the journal inode from a visible file in 793 * the filesystem directory hierarchy to the reserved inode if necessary. 794 */ 795static const char * const journal_names[] = { 796 ".journal", "journal", ".journal.dat", "journal.dat", 0 }; 797 798void e2fsck_move_ext3_journal(e2fsck_t ctx) 799{ 800 struct ext2_super_block *sb = ctx->fs->super; 801 struct problem_context pctx; 802 struct ext2_inode inode; 803 ext2_filsys fs = ctx->fs; 804 ext2_ino_t ino; 805 errcode_t retval; 806 const char * const * cpp; 807 int group, mount_flags; 808 809 /* 810 * If the filesystem is opened read-only, or there is no 811 * journal, or the journal is already in the hidden inode, 812 * then do nothing. 813 */ 814 if ((ctx->options & E2F_OPT_READONLY) || 815 (sb->s_journal_inum == 0) || 816 (sb->s_journal_inum == EXT2_JOURNAL_INO) || 817 !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) 818 return; 819 820 /* 821 * If the filesystem is mounted, or we can't tell whether 822 * or not it's mounted, do nothing. 823 */ 824 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags); 825 if (retval || (mount_flags & EXT2_MF_MOUNTED)) 826 return; 827 828 /* 829 * If we can't find the name of the journal inode, then do 830 * nothing. 831 */ 832 for (cpp = journal_names; *cpp; cpp++) { 833 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp, 834 strlen(*cpp), 0, &ino); 835 if ((retval == 0) && (ino == sb->s_journal_inum)) 836 break; 837 } 838 if (*cpp == 0) 839 return; 840 841 /* 842 * The inode had better have only one link and not be readable. 843 */ 844 if (ext2fs_read_inode(fs, ino, &inode) != 0) 845 return; 846 if (inode.i_links_count != 1) 847 return; 848 849 /* We need the inode bitmap to be loaded */ 850 retval = ext2fs_read_bitmaps(fs); 851 if (retval) 852 return; 853 854 clear_problem_context(&pctx); 855 pctx.str = *cpp; 856 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx)) 857 return; 858 859 /* 860 * OK, we've done all the checks, let's actually move the 861 * journal inode. Errors at this point mean we need to force 862 * an ext2 filesystem check. 863 */ 864 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0) 865 goto err_out; 866 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0) 867 goto err_out; 868 sb->s_journal_inum = EXT2_JOURNAL_INO; 869 ext2fs_mark_super_dirty(fs); 870 inode.i_links_count = 0; 871 inode.i_dtime = time(0); 872 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0) 873 goto err_out; 874 875 group = ext2fs_group_of_ino(fs, ino); 876 ext2fs_unmark_inode_bitmap(fs->inode_map, ino); 877 ext2fs_mark_ib_dirty(fs); 878 fs->group_desc[group].bg_free_inodes_count++; 879 fs->super->s_free_inodes_count++; 880 return; 881 882err_out: 883 pctx.errcode = retval; 884 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx); 885 fs->super->s_state &= ~EXT2_VALID_FS; 886 ext2fs_mark_super_dirty(fs); 887 return; 888} 889 890