2 * journal.c --- code for handling the "ext3" journal
4 * Copyright (C) 2000 Andreas Dilger
5 * Copyright (C) 2000 Theodore Ts'o
7 * Parts of the code are based on fs/jfs/journal.c by Stephen C. Tweedie
8 * Copyright (C) 1999 Red Hat Software
10 * This file may be redistributed under the terms of the
11 * GNU General Public License version 2 or at your discretion
16 #ifdef HAVE_SYS_MOUNT_H
17 #include <sys/param.h>
18 #include <sys/mount.h>
19 #define MNT_FL (MS_MGC_VAL | MS_RDONLY)
21 #ifdef HAVE_SYS_STAT_H
25 #define E2FSCK_INCLUDE_INLINE_FUNCS
28 #include "uuid/uuid.h"
30 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
31 static int bh_count = 0;
35 * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths.
36 * This creates a larger static binary, and a smaller binary using
37 * shared libraries. It's also probably slightly less CPU-efficient,
38 * which is why it's not on by default. But, it's a good way of
39 * testing the functions in inode_io.c and fileio.c.
43 /* Kernel compatibility functions for handling the journal. These allow us
44 * to use the recovery.c file virtually unchanged from the kernel, so we
45 * don't have to do much to keep kernel and user recovery in sync.
47 int journal_bmap(journal_t *journal, blk64_t block, unsigned long long *phys)
53 struct inode *inode = journal->j_inode;
62 retval= ext2fs_bmap2(inode->i_ctx->fs, inode->i_ino,
63 &inode->i_ext2, NULL, 0, block, 0, &pblk);
69 struct buffer_head *getblk(kdev_t kdev, blk64_t blocknr, int blocksize)
71 struct buffer_head *bh;
72 int bufsize = sizeof(*bh) + kdev->k_ctx->fs->blocksize -
75 bh = e2fsck_allocate_memory(kdev->k_ctx, bufsize, "block buffer");
79 #ifdef CONFIG_JBD_DEBUG
80 if (journal_enable_debug >= 3)
83 jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
84 (unsigned long long) blocknr, blocksize, bh_count);
86 bh->b_ctx = kdev->k_ctx;
87 if (kdev->k_dev == K_DEV_FS)
88 bh->b_io = kdev->k_ctx->fs->io;
90 bh->b_io = kdev->k_ctx->journal_io;
91 bh->b_size = blocksize;
92 bh->b_blocknr = blocknr;
97 void sync_blockdev(kdev_t kdev)
101 if (kdev->k_dev == K_DEV_FS)
102 io = kdev->k_ctx->fs->io;
104 io = kdev->k_ctx->journal_io;
106 io_channel_flush(io);
109 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
112 struct buffer_head *bh;
114 for (; nr > 0; --nr) {
116 if (rw == READ && !bh->b_uptodate) {
117 jfs_debug(3, "reading block %llu/%p\n",
118 bh->b_blocknr, (void *) bh);
119 retval = io_channel_read_blk64(bh->b_io,
123 com_err(bh->b_ctx->device_name, retval,
124 "while reading block %llu\n",
130 } else if (rw == WRITE && bh->b_dirty) {
131 jfs_debug(3, "writing block %llu/%p\n",
134 retval = io_channel_write_blk64(bh->b_io,
138 com_err(bh->b_ctx->device_name, retval,
139 "while writing block %llu\n",
147 jfs_debug(3, "no-op %s for block %llu\n",
148 rw == READ ? "read" : "write",
154 void mark_buffer_dirty(struct buffer_head *bh)
159 static void mark_buffer_clean(struct buffer_head * bh)
164 void brelse(struct buffer_head *bh)
167 ll_rw_block(WRITE, 1, &bh);
168 jfs_debug(3, "freeing block %llu/%p (total %d)\n",
169 bh->b_blocknr, (void *) bh, --bh_count);
170 ext2fs_free_mem(&bh);
173 int buffer_uptodate(struct buffer_head *bh)
175 return bh->b_uptodate;
178 void mark_buffer_uptodate(struct buffer_head *bh, int val)
180 bh->b_uptodate = val;
183 void wait_on_buffer(struct buffer_head *bh)
186 ll_rw_block(READ, 1, &bh);
190 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
192 ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
194 /* if we had an error doing journal recovery, we need a full fsck */
196 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
197 ext2fs_mark_super_dirty(ctx->fs);
201 * This is a helper function to check the validity of the journal.
203 struct process_block_struct {
204 e2_blkcnt_t last_block;
207 static int process_journal_block(ext2_filsys fs,
209 e2_blkcnt_t blockcnt,
210 blk64_t ref_block EXT2FS_ATTR((unused)),
211 int ref_offset EXT2FS_ATTR((unused)),
214 struct process_block_struct *p;
215 blk64_t blk = *block_nr;
217 p = (struct process_block_struct *) priv_data;
219 if (!blk || blk < fs->super->s_first_data_block ||
220 blk >= ext2fs_blocks_count(fs->super))
224 p->last_block = blockcnt;
228 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
230 struct process_block_struct pb;
231 struct ext2_super_block *sb = ctx->fs->super;
232 struct ext2_super_block jsuper;
233 struct problem_context pctx;
234 struct buffer_head *bh;
235 struct inode *j_inode = NULL;
236 struct kdev_s *dev_fs = NULL, *dev_journal;
237 const char *journal_name = 0;
238 journal_t *journal = NULL;
239 errcode_t retval = 0;
240 io_manager io_ptr = 0;
241 unsigned long long start = 0;
243 int tried_backup_jnl = 0;
245 clear_problem_context(&pctx);
247 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
249 return EXT2_ET_NO_MEMORY;
252 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
254 retval = EXT2_ET_NO_MEMORY;
257 dev_journal = dev_fs+1;
259 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
260 dev_fs->k_dev = K_DEV_FS;
261 dev_journal->k_dev = K_DEV_JOURNAL;
263 journal->j_dev = dev_journal;
264 journal->j_fs_dev = dev_fs;
265 journal->j_inode = NULL;
266 journal->j_blocksize = ctx->fs->blocksize;
268 if (uuid_is_null(sb->s_journal_uuid)) {
269 if (!sb->s_journal_inum) {
270 retval = EXT2_ET_BAD_INODE_NUM;
273 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
276 retval = EXT2_ET_NO_MEMORY;
280 j_inode->i_ctx = ctx;
281 j_inode->i_ino = sb->s_journal_inum;
283 if ((retval = ext2fs_read_inode(ctx->fs,
285 &j_inode->i_ext2))) {
287 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
290 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
291 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
293 j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15];
294 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
295 j_inode->i_ext2.i_links_count = 1;
296 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
297 e2fsck_use_inode_shortcuts(ctx, 1);
298 ctx->stashed_ino = j_inode->i_ino;
299 ctx->stashed_inode = &j_inode->i_ext2;
302 if (!j_inode->i_ext2.i_links_count ||
303 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
304 retval = EXT2_ET_NO_JOURNAL;
305 goto try_backup_journal;
307 if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize <
308 JFS_MIN_JOURNAL_BLOCKS) {
309 retval = EXT2_ET_JOURNAL_TOO_SMALL;
310 goto try_backup_journal;
313 retval = ext2fs_block_iterate3(ctx->fs, j_inode->i_ino,
315 process_journal_block, &pb);
316 if ((pb.last_block + 1) * ctx->fs->blocksize <
317 EXT2_I_SIZE(&j_inode->i_ext2)) {
318 retval = EXT2_ET_JOURNAL_TOO_SMALL;
319 goto try_backup_journal;
321 if (tried_backup_jnl && !(ctx->options & E2F_OPT_READONLY)) {
322 retval = ext2fs_write_inode(ctx->fs, sb->s_journal_inum,
328 journal->j_maxlen = EXT2_I_SIZE(&j_inode->i_ext2) /
329 journal->j_blocksize;
332 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
338 io_ptr = inode_io_manager;
340 journal->j_inode = j_inode;
341 ctx->journal_io = ctx->fs->io;
342 if ((retval = journal_bmap(journal, 0, &start)) != 0)
347 if (!ctx->journal_name) {
350 uuid_unparse(sb->s_journal_uuid, uuid);
351 ctx->journal_name = blkid_get_devname(ctx->blkid,
353 if (!ctx->journal_name)
354 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
356 journal_name = ctx->journal_name;
359 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
360 retval = EXT2_ET_LOAD_EXT_JOURNAL;
364 jfs_debug(1, "Using journal file %s\n", journal_name);
365 io_ptr = unix_io_manager;
369 test_io_backing_manager = io_ptr;
370 io_ptr = test_io_manager;
375 retval = io_ptr->open(journal_name,
376 IO_FLAG_RW | IO_FLAG_EXCLUSIVE,
381 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
384 if (ctx->fs->blocksize == 1024)
386 bh = getblk(dev_journal, start, ctx->fs->blocksize);
388 retval = EXT2_ET_NO_MEMORY;
391 ll_rw_block(READ, 1, &bh);
392 if ((retval = bh->b_err) != 0) {
396 memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024,
399 #ifdef WORDS_BIGENDIAN
400 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
401 ext2fs_swap_super(&jsuper);
403 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
404 !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
405 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
406 retval = EXT2_ET_LOAD_EXT_JOURNAL;
409 /* Make sure the journal UUID is correct */
410 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
411 sizeof(jsuper.s_uuid))) {
412 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
413 retval = EXT2_ET_LOAD_EXT_JOURNAL;
417 journal->j_maxlen = ext2fs_blocks_count(&jsuper);
421 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
422 retval = EXT2_ET_NO_MEMORY;
426 journal->j_sb_buffer = bh;
427 journal->j_superblock = (journal_superblock_t *)bh->b_data;
431 ext2fs_free_mem(&j_inode);
434 *ret_journal = journal;
435 e2fsck_use_inode_shortcuts(ctx, 0);
439 e2fsck_use_inode_shortcuts(ctx, 0);
441 ext2fs_free_mem(&dev_fs);
443 ext2fs_free_mem(&j_inode);
445 ext2fs_free_mem(&journal);
449 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
450 struct problem_context *pctx)
452 struct ext2_super_block *sb = ctx->fs->super;
453 int recover = ctx->fs->super->s_feature_incompat &
454 EXT3_FEATURE_INCOMPAT_RECOVER;
455 int has_journal = ctx->fs->super->s_feature_compat &
456 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
458 if (has_journal || sb->s_journal_inum) {
459 /* The journal inode is bogus, remove and force full fsck */
460 pctx->ino = sb->s_journal_inum;
461 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
462 if (has_journal && sb->s_journal_inum)
463 printf("*** ext3 journal has been deleted - "
464 "filesystem is now ext2 only ***\n\n");
465 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
466 sb->s_journal_inum = 0;
467 ctx->flags |= E2F_FLAG_JOURNAL_INODE;
468 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
469 e2fsck_clear_recover(ctx, 1);
472 return EXT2_ET_BAD_INODE_NUM;
473 } else if (recover) {
474 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
475 e2fsck_clear_recover(ctx, 1);
478 return EXT2_ET_UNSUPP_FEATURE;
483 #define V1_SB_SIZE 0x0024
484 static void clear_v2_journal_fields(journal_t *journal)
486 e2fsck_t ctx = journal->j_dev->k_ctx;
487 struct problem_context pctx;
489 clear_problem_context(&pctx);
491 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
494 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
495 ctx->fs->blocksize-V1_SB_SIZE);
496 mark_buffer_dirty(journal->j_sb_buffer);
500 static errcode_t e2fsck_journal_load(journal_t *journal)
502 e2fsck_t ctx = journal->j_dev->k_ctx;
503 journal_superblock_t *jsb;
504 struct buffer_head *jbh = journal->j_sb_buffer;
505 struct problem_context pctx;
507 clear_problem_context(&pctx);
509 ll_rw_block(READ, 1, &jbh);
511 com_err(ctx->device_name, jbh->b_err,
512 _("reading journal superblock\n"));
516 jsb = journal->j_superblock;
517 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
518 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
519 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
521 switch (ntohl(jsb->s_header.h_blocktype)) {
522 case JFS_SUPERBLOCK_V1:
523 journal->j_format_version = 1;
524 if (jsb->s_feature_compat ||
525 jsb->s_feature_incompat ||
526 jsb->s_feature_ro_compat ||
528 clear_v2_journal_fields(journal);
531 case JFS_SUPERBLOCK_V2:
532 journal->j_format_version = 2;
533 if (ntohl(jsb->s_nr_users) > 1 &&
534 uuid_is_null(ctx->fs->super->s_journal_uuid))
535 clear_v2_journal_fields(journal);
536 if (ntohl(jsb->s_nr_users) > 1) {
537 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
538 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
543 * These should never appear in a journal super block, so if
544 * they do, the journal is badly corrupted.
546 case JFS_DESCRIPTOR_BLOCK:
547 case JFS_COMMIT_BLOCK:
548 case JFS_REVOKE_BLOCK:
549 return EXT2_ET_CORRUPT_SUPERBLOCK;
551 /* If we don't understand the superblock major type, but there
552 * is a magic number, then it is likely to be a new format we
553 * just don't understand, so leave it alone. */
555 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
558 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
559 return EXT2_ET_UNSUPP_FEATURE;
561 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
562 return EXT2_ET_RO_UNSUPP_FEATURE;
564 /* We have now checked whether we know enough about the journal
565 * format to be able to proceed safely, so any other checks that
566 * fail we should attempt to recover from. */
567 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
568 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
569 _("%s: no valid journal superblock found\n"),
571 return EXT2_ET_CORRUPT_SUPERBLOCK;
574 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
575 journal->j_maxlen = ntohl(jsb->s_maxlen);
576 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
577 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
578 _("%s: journal too short\n"),
580 return EXT2_ET_CORRUPT_SUPERBLOCK;
583 journal->j_tail_sequence = ntohl(jsb->s_sequence);
584 journal->j_transaction_sequence = journal->j_tail_sequence;
585 journal->j_tail = ntohl(jsb->s_start);
586 journal->j_first = ntohl(jsb->s_first);
587 journal->j_last = ntohl(jsb->s_maxlen);
592 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
603 /* Leave a valid existing V1 superblock signature alone.
604 * Anything unrecognisable we overwrite with a new V2
607 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
608 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
609 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
610 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
613 /* Zero out everything else beyond the superblock header */
615 p = ((char *) jsb) + sizeof(journal_header_t);
616 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
618 jsb->s_blocksize = htonl(ctx->fs->blocksize);
619 jsb->s_maxlen = htonl(journal->j_maxlen);
620 jsb->s_first = htonl(1);
622 /* Initialize the journal sequence number so that there is "no"
623 * chance we will find old "valid" transactions in the journal.
624 * This avoids the need to zero the whole journal (slow to do,
625 * and risky when we are just recovering the filesystem).
627 uuid_generate(u.uuid);
628 for (i = 0; i < 4; i ++)
630 jsb->s_sequence = htonl(new_seq);
632 mark_buffer_dirty(journal->j_sb_buffer);
633 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
636 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
638 struct problem_context *pctx)
640 struct ext2_super_block *sb = ctx->fs->super;
641 int recover = ctx->fs->super->s_feature_incompat &
642 EXT3_FEATURE_INCOMPAT_RECOVER;
644 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
645 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
646 e2fsck_journal_reset_super(ctx, journal->j_superblock,
648 journal->j_transaction_sequence = 1;
649 e2fsck_clear_recover(ctx, recover);
652 return EXT2_ET_CORRUPT_SUPERBLOCK;
653 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
654 return EXT2_ET_CORRUPT_SUPERBLOCK;
659 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
662 journal_superblock_t *jsb;
665 mark_buffer_clean(journal->j_sb_buffer);
666 else if (!(ctx->options & E2F_OPT_READONLY)) {
667 jsb = journal->j_superblock;
668 jsb->s_sequence = htonl(journal->j_transaction_sequence);
670 jsb->s_start = 0; /* this marks the journal as empty */
671 mark_buffer_dirty(journal->j_sb_buffer);
673 brelse(journal->j_sb_buffer);
675 if (ctx->journal_io) {
676 if (ctx->fs && ctx->fs->io != ctx->journal_io)
677 io_channel_close(ctx->journal_io);
682 if (journal->j_inode)
683 ext2fs_free_mem(&journal->j_inode);
685 if (journal->j_fs_dev)
686 ext2fs_free_mem(&journal->j_fs_dev);
687 ext2fs_free_mem(&journal);
691 * This function makes sure that the superblock fields regarding the
692 * journal are consistent.
694 int e2fsck_check_ext3_journal(e2fsck_t ctx)
696 struct ext2_super_block *sb = ctx->fs->super;
698 int recover = ctx->fs->super->s_feature_incompat &
699 EXT3_FEATURE_INCOMPAT_RECOVER;
700 struct problem_context pctx;
702 int reset = 0, force_fsck = 0;
705 /* If we don't have any journal features, don't do anything more */
706 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
707 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
708 uuid_is_null(sb->s_journal_uuid))
711 clear_problem_context(&pctx);
712 pctx.num = sb->s_journal_inum;
714 retval = e2fsck_get_journal(ctx, &journal);
716 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
717 (retval == EXT2_ET_BAD_BLOCK_NUM) ||
718 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
719 (retval == EXT2_ET_NO_JOURNAL))
720 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
724 retval = e2fsck_journal_load(journal);
726 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
727 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
728 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
730 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
731 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
733 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
734 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
735 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
737 e2fsck_journal_release(ctx, journal, 0, 1);
742 * We want to make the flags consistent here. We will not leave with
743 * needs_recovery set but has_journal clear. We can't get in a loop
744 * with -y, -n, or -p, only if a user isn't making up their mind.
747 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
748 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
750 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
752 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
755 * Need a full fsck if we are releasing a
756 * journal stored on a reserved inode.
758 force_fsck = recover ||
759 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
760 /* Clear all of the journal fields */
761 sb->s_journal_inum = 0;
762 sb->s_journal_dev = 0;
763 memset(sb->s_journal_uuid, 0,
764 sizeof(sb->s_journal_uuid));
765 e2fsck_clear_recover(ctx, force_fsck);
766 } else if (!(ctx->options & E2F_OPT_READONLY)) {
767 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
768 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
769 ext2fs_mark_super_dirty(ctx->fs);
773 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
774 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
775 journal->j_superblock->s_start != 0) {
776 /* Print status information */
777 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
779 problem = PR_0_JOURNAL_RUN_DEFAULT;
781 problem = PR_0_JOURNAL_RUN;
782 if (fix_problem(ctx, problem, &pctx)) {
783 ctx->options |= E2F_OPT_FORCE;
784 sb->s_feature_incompat |=
785 EXT3_FEATURE_INCOMPAT_RECOVER;
786 ext2fs_mark_super_dirty(ctx->fs);
787 } else if (fix_problem(ctx,
788 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
790 sb->s_state &= ~EXT2_VALID_FS;
791 ext2fs_mark_super_dirty(ctx->fs);
794 * If the user answers no to the above question, we
795 * ignore the fact that journal apparently has data;
796 * accidentally replaying over valid data would be far
797 * worse than skipping a questionable recovery.
799 * XXX should we abort with a fatal error here? What
800 * will the ext3 kernel code do if a filesystem with
801 * !NEEDS_RECOVERY but with a non-zero
802 * journal->j_superblock->s_start is mounted?
806 e2fsck_journal_release(ctx, journal, reset, 0);
810 static errcode_t recover_ext3_journal(e2fsck_t ctx)
812 struct problem_context pctx;
816 clear_problem_context(&pctx);
818 journal_init_revoke_caches();
819 retval = e2fsck_get_journal(ctx, &journal);
823 retval = e2fsck_journal_load(journal);
827 retval = journal_init_revoke(journal, 1024);
831 retval = -journal_recover(journal);
835 if (journal->j_failed_commit) {
836 pctx.ino = journal->j_failed_commit;
837 fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx);
838 ctx->fs->super->s_state |= EXT2_ERROR_FS;
839 ext2fs_mark_super_dirty(ctx->fs);
843 if (journal->j_superblock->s_errno) {
844 ctx->fs->super->s_state |= EXT2_ERROR_FS;
845 ext2fs_mark_super_dirty(ctx->fs);
846 journal->j_superblock->s_errno = 0;
847 mark_buffer_dirty(journal->j_sb_buffer);
851 journal_destroy_revoke(journal);
852 journal_destroy_revoke_caches();
853 e2fsck_journal_release(ctx, journal, 1, 0);
857 int e2fsck_run_ext3_journal(e2fsck_t ctx)
859 io_manager io_ptr = ctx->fs->io->manager;
860 int blocksize = ctx->fs->blocksize;
861 errcode_t retval, recover_retval;
863 unsigned long long kbytes_written = 0;
865 printf(_("%s: recovering journal\n"), ctx->device_name);
866 if (ctx->options & E2F_OPT_READONLY) {
867 printf(_("%s: won't do journal recovery while read-only\n"),
869 return EXT2_ET_FILE_RO;
872 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
873 ext2fs_flush(ctx->fs); /* Force out any modifications */
875 recover_retval = recover_ext3_journal(ctx);
878 * Reload the filesystem context to get up-to-date data from disk
879 * because journal recovery will change the filesystem under us.
881 if (ctx->fs->super->s_kbytes_written &&
882 ctx->fs->io->manager->get_stats)
883 ctx->fs->io->manager->get_stats(ctx->fs->io, &stats);
884 if (stats && stats->bytes_written)
885 kbytes_written = stats->bytes_written >> 10;
887 ext2fs_mmp_stop(ctx->fs);
888 ext2fs_free(ctx->fs);
889 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
890 ctx->superblock, blocksize, io_ptr,
893 com_err(ctx->program_name, retval,
894 _("while trying to re-open %s"),
898 ctx->fs->priv_data = ctx;
899 ctx->fs->now = ctx->now;
900 ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
901 ctx->fs->super->s_kbytes_written += kbytes_written;
903 /* Set the superblock flags */
904 e2fsck_clear_recover(ctx, recover_retval);
905 return recover_retval;
909 * This function will move the journal inode from a visible file in
910 * the filesystem directory hierarchy to the reserved inode if necessary.
912 static const char * const journal_names[] = {
913 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
915 void e2fsck_move_ext3_journal(e2fsck_t ctx)
917 struct ext2_super_block *sb = ctx->fs->super;
918 struct problem_context pctx;
919 struct ext2_inode inode;
920 ext2_filsys fs = ctx->fs;
923 const char * const * cpp;
924 int group, mount_flags;
926 clear_problem_context(&pctx);
929 * If the filesystem is opened read-only, or there is no
930 * journal, then do nothing.
932 if ((ctx->options & E2F_OPT_READONLY) ||
933 (sb->s_journal_inum == 0) ||
934 !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
938 * Read in the journal inode
940 if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
944 * If it's necessary to backup the journal inode, do so.
946 if ((sb->s_jnl_backup_type == 0) ||
947 ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
948 memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
949 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
950 memcpy(sb->s_jnl_blocks, inode.i_block,
952 sb->s_jnl_blocks[15] = inode.i_size_high;
953 sb->s_jnl_blocks[16] = inode.i_size;
954 sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
955 ext2fs_mark_super_dirty(fs);
956 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
961 * If the journal is already the hidden inode, then do nothing
963 if (sb->s_journal_inum == EXT2_JOURNAL_INO)
967 * The journal inode had better have only one link and not be readable.
969 if (inode.i_links_count != 1)
973 * If the filesystem is mounted, or we can't tell whether
974 * or not it's mounted, do nothing.
976 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
977 if (retval || (mount_flags & EXT2_MF_MOUNTED))
981 * If we can't find the name of the journal inode, then do
984 for (cpp = journal_names; *cpp; cpp++) {
985 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
986 strlen(*cpp), 0, &ino);
987 if ((retval == 0) && (ino == sb->s_journal_inum))
993 /* We need the inode bitmap to be loaded */
994 retval = ext2fs_read_bitmaps(fs);
999 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
1003 * OK, we've done all the checks, let's actually move the
1004 * journal inode. Errors at this point mean we need to force
1005 * an ext2 filesystem check.
1007 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
1009 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
1011 sb->s_journal_inum = EXT2_JOURNAL_INO;
1012 ext2fs_mark_super_dirty(fs);
1013 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1014 inode.i_links_count = 0;
1015 inode.i_dtime = ctx->now;
1016 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
1019 group = ext2fs_group_of_ino(fs, ino);
1020 ext2fs_unmark_inode_bitmap2(fs->inode_map, ino);
1021 ext2fs_mark_ib_dirty(fs);
1022 ext2fs_bg_free_inodes_count_set(fs, group, ext2fs_bg_free_inodes_count(fs, group) + 1);
1023 ext2fs_group_desc_csum_set(fs, group);
1024 fs->super->s_free_inodes_count++;
1028 pctx.errcode = retval;
1029 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
1030 fs->super->s_state &= ~EXT2_VALID_FS;
1031 ext2fs_mark_super_dirty(fs);
1036 * This function makes sure the superblock hint for the external
1037 * journal is correct.
1039 int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
1041 struct ext2_super_block *sb = ctx->fs->super;
1042 struct problem_context pctx;
1043 char uuid[37], *journal_name;
1046 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
1047 uuid_is_null(sb->s_journal_uuid))
1050 uuid_unparse(sb->s_journal_uuid, uuid);
1051 journal_name = blkid_get_devname(ctx->blkid, "UUID", uuid);
1055 if (stat(journal_name, &st) < 0)
1058 if (st.st_rdev != sb->s_journal_dev) {
1059 clear_problem_context(&pctx);
1060 pctx.num = st.st_rdev;
1061 if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) {
1062 sb->s_journal_dev = st.st_rdev;
1063 ext2fs_mark_super_dirty(ctx->fs);