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
15 #ifdef HAVE_SYS_MOUNT_H
16 #include <sys/param.h>
17 #include <sys/mount.h>
18 #define MNT_FL (MS_MGC_VAL | MS_RDONLY)
20 #ifdef HAVE_SYS_STAT_H
24 #define E2FSCK_INCLUDE_INLINE_FUNCS
27 #include "uuid/uuid.h"
29 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
30 static int bh_count = 0;
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.
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.
46 int journal_bmap(journal_t *journal, blk_t block, unsigned long *phys)
52 struct inode *inode = journal->j_inode;
61 retval= ext2fs_bmap(inode->i_ctx->fs, inode->i_ino,
62 &inode->i_ext2, NULL, 0, block, &pblk);
68 struct buffer_head *getblk(kdev_t kdev, blk_t blocknr, int blocksize)
70 struct buffer_head *bh;
71 int bufsize = sizeof(*bh) + kdev->k_ctx->fs->blocksize -
74 bh = e2fsck_allocate_memory(kdev->k_ctx, bufsize, "block buffer");
78 #ifdef CONFIG_JBD_DEBUG
79 if (journal_enable_debug >= 3)
82 jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
83 (unsigned long) blocknr, blocksize, bh_count);
85 bh->b_ctx = kdev->k_ctx;
86 if (kdev->k_dev == K_DEV_FS)
87 bh->b_io = kdev->k_ctx->fs->io;
89 bh->b_io = kdev->k_ctx->journal_io;
90 bh->b_size = blocksize;
91 bh->b_blocknr = blocknr;
96 void sync_blockdev(kdev_t kdev)
100 if (kdev->k_dev == K_DEV_FS)
101 io = kdev->k_ctx->fs->io;
103 io = kdev->k_ctx->journal_io;
105 io_channel_flush(io);
108 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
111 struct buffer_head *bh;
113 for (; nr > 0; --nr) {
115 if (rw == READ && !bh->b_uptodate) {
116 jfs_debug(3, "reading block %lu/%p\n",
117 (unsigned long) bh->b_blocknr, (void *) bh);
118 retval = io_channel_read_blk(bh->b_io,
122 com_err(bh->b_ctx->device_name, retval,
123 "while reading block %lu\n",
124 (unsigned long) bh->b_blocknr);
129 } else if (rw == WRITE && bh->b_dirty) {
130 jfs_debug(3, "writing block %lu/%p\n",
131 (unsigned long) bh->b_blocknr, (void *) bh);
132 retval = io_channel_write_blk(bh->b_io,
136 com_err(bh->b_ctx->device_name, retval,
137 "while writing block %lu\n",
138 (unsigned long) bh->b_blocknr);
145 jfs_debug(3, "no-op %s for block %lu\n",
146 rw == READ ? "read" : "write",
147 (unsigned long) bh->b_blocknr);
152 void mark_buffer_dirty(struct buffer_head *bh)
157 static void mark_buffer_clean(struct buffer_head * bh)
162 void brelse(struct buffer_head *bh)
165 ll_rw_block(WRITE, 1, &bh);
166 jfs_debug(3, "freeing block %lu/%p (total %d)\n",
167 (unsigned long) bh->b_blocknr, (void *) bh, --bh_count);
168 ext2fs_free_mem(&bh);
171 int buffer_uptodate(struct buffer_head *bh)
173 return bh->b_uptodate;
176 void mark_buffer_uptodate(struct buffer_head *bh, int val)
178 bh->b_uptodate = val;
181 void wait_on_buffer(struct buffer_head *bh)
184 ll_rw_block(READ, 1, &bh);
188 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
190 ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
192 /* if we had an error doing journal recovery, we need a full fsck */
194 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
195 ext2fs_mark_super_dirty(ctx->fs);
199 * This is a helper function to check the validity of the journal.
201 struct process_block_struct {
202 e2_blkcnt_t last_block;
205 static int process_journal_block(ext2_filsys fs,
207 e2_blkcnt_t blockcnt,
208 blk_t ref_block EXT2FS_ATTR((unused)),
209 int ref_offset EXT2FS_ATTR((unused)),
212 struct process_block_struct *p;
213 blk_t blk = *block_nr;
215 p = (struct process_block_struct *) priv_data;
217 if (!blk || blk < fs->super->s_first_data_block ||
218 blk >= fs->super->s_blocks_count)
222 p->last_block = blockcnt;
226 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
228 struct process_block_struct pb;
229 struct ext2_super_block *sb = ctx->fs->super;
230 struct ext2_super_block jsuper;
231 struct problem_context pctx;
232 struct buffer_head *bh;
233 struct inode *j_inode = NULL;
234 struct kdev_s *dev_fs = NULL, *dev_journal;
235 const char *journal_name = 0;
236 journal_t *journal = NULL;
237 errcode_t retval = 0;
238 io_manager io_ptr = 0;
239 unsigned long start = 0;
241 int tried_backup_jnl = 0;
243 clear_problem_context(&pctx);
245 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
247 return EXT2_ET_NO_MEMORY;
250 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
252 retval = EXT2_ET_NO_MEMORY;
255 dev_journal = dev_fs+1;
257 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
258 dev_fs->k_dev = K_DEV_FS;
259 dev_journal->k_dev = K_DEV_JOURNAL;
261 journal->j_dev = dev_journal;
262 journal->j_fs_dev = dev_fs;
263 journal->j_inode = NULL;
264 journal->j_blocksize = ctx->fs->blocksize;
266 if (uuid_is_null(sb->s_journal_uuid)) {
267 if (!sb->s_journal_inum) {
268 retval = EXT2_ET_BAD_INODE_NUM;
271 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
274 retval = EXT2_ET_NO_MEMORY;
278 j_inode->i_ctx = ctx;
279 j_inode->i_ino = sb->s_journal_inum;
281 if ((retval = ext2fs_read_inode(ctx->fs,
283 &j_inode->i_ext2))) {
285 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
288 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
289 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
291 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
292 j_inode->i_ext2.i_links_count = 1;
293 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
294 e2fsck_use_inode_shortcuts(ctx, 1);
295 ctx->stashed_ino = j_inode->i_ino;
296 ctx->stashed_inode = &j_inode->i_ext2;
299 if (!j_inode->i_ext2.i_links_count ||
300 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
301 retval = EXT2_ET_NO_JOURNAL;
302 goto try_backup_journal;
304 if (j_inode->i_ext2.i_size / journal->j_blocksize <
305 JFS_MIN_JOURNAL_BLOCKS) {
306 retval = EXT2_ET_JOURNAL_TOO_SMALL;
307 goto try_backup_journal;
310 retval = ext2fs_block_iterate2(ctx->fs, j_inode->i_ino,
312 process_journal_block, &pb);
313 if ((pb.last_block+1) * ctx->fs->blocksize <
314 j_inode->i_ext2.i_size) {
315 retval = EXT2_ET_JOURNAL_TOO_SMALL;
316 goto try_backup_journal;
318 if (tried_backup_jnl && !(ctx->options & E2F_OPT_READONLY)) {
319 retval = ext2fs_write_inode(ctx->fs, sb->s_journal_inum,
325 journal->j_maxlen = j_inode->i_ext2.i_size / journal->j_blocksize;
328 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
334 io_ptr = inode_io_manager;
336 journal->j_inode = j_inode;
337 ctx->journal_io = ctx->fs->io;
338 if ((retval = journal_bmap(journal, 0, &start)) != 0)
343 if (!ctx->journal_name) {
346 uuid_unparse(sb->s_journal_uuid, uuid);
347 ctx->journal_name = blkid_get_devname(ctx->blkid,
349 if (!ctx->journal_name)
350 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
352 journal_name = ctx->journal_name;
355 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
356 retval = EXT2_ET_LOAD_EXT_JOURNAL;
360 jfs_debug(1, "Using journal file %s\n", journal_name);
361 io_ptr = unix_io_manager;
365 test_io_backing_manager = io_ptr;
366 io_ptr = test_io_manager;
371 retval = io_ptr->open(journal_name,
372 IO_FLAG_RW | IO_FLAG_EXCLUSIVE,
377 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
380 if (ctx->fs->blocksize == 1024)
382 bh = getblk(dev_journal, start, ctx->fs->blocksize);
384 retval = EXT2_ET_NO_MEMORY;
387 ll_rw_block(READ, 1, &bh);
388 if ((retval = bh->b_err) != 0) {
392 memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024,
395 #ifdef WORDS_BIGENDIAN
396 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
397 ext2fs_swap_super(&jsuper);
399 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
400 !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
401 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
402 retval = EXT2_ET_LOAD_EXT_JOURNAL;
405 /* Make sure the journal UUID is correct */
406 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
407 sizeof(jsuper.s_uuid))) {
408 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
409 retval = EXT2_ET_LOAD_EXT_JOURNAL;
413 journal->j_maxlen = jsuper.s_blocks_count;
417 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
418 retval = EXT2_ET_NO_MEMORY;
422 journal->j_sb_buffer = bh;
423 journal->j_superblock = (journal_superblock_t *)bh->b_data;
427 ext2fs_free_mem(&j_inode);
430 *ret_journal = journal;
431 e2fsck_use_inode_shortcuts(ctx, 0);
435 e2fsck_use_inode_shortcuts(ctx, 0);
437 ext2fs_free_mem(&dev_fs);
439 ext2fs_free_mem(&j_inode);
441 ext2fs_free_mem(&journal);
445 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
446 struct problem_context *pctx)
448 struct ext2_super_block *sb = ctx->fs->super;
449 int recover = ctx->fs->super->s_feature_incompat &
450 EXT3_FEATURE_INCOMPAT_RECOVER;
451 int has_journal = ctx->fs->super->s_feature_compat &
452 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
454 if (has_journal || sb->s_journal_inum) {
455 /* The journal inode is bogus, remove and force full fsck */
456 pctx->ino = sb->s_journal_inum;
457 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
458 if (has_journal && sb->s_journal_inum)
459 printf("*** ext3 journal has been deleted - "
460 "filesystem is now ext2 only ***\n\n");
461 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
462 sb->s_journal_inum = 0;
463 ctx->flags |= E2F_FLAG_JOURNAL_INODE;
464 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
465 e2fsck_clear_recover(ctx, 1);
468 return EXT2_ET_BAD_INODE_NUM;
469 } else if (recover) {
470 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
471 e2fsck_clear_recover(ctx, 1);
474 return EXT2_ET_UNSUPP_FEATURE;
479 #define V1_SB_SIZE 0x0024
480 static void clear_v2_journal_fields(journal_t *journal)
482 e2fsck_t ctx = journal->j_dev->k_ctx;
483 struct problem_context pctx;
485 clear_problem_context(&pctx);
487 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
490 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
491 ctx->fs->blocksize-V1_SB_SIZE);
492 mark_buffer_dirty(journal->j_sb_buffer);
496 static errcode_t e2fsck_journal_load(journal_t *journal)
498 e2fsck_t ctx = journal->j_dev->k_ctx;
499 journal_superblock_t *jsb;
500 struct buffer_head *jbh = journal->j_sb_buffer;
501 struct problem_context pctx;
503 clear_problem_context(&pctx);
505 ll_rw_block(READ, 1, &jbh);
507 com_err(ctx->device_name, jbh->b_err,
508 _("reading journal superblock\n"));
512 jsb = journal->j_superblock;
513 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
514 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
515 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
517 switch (ntohl(jsb->s_header.h_blocktype)) {
518 case JFS_SUPERBLOCK_V1:
519 journal->j_format_version = 1;
520 if (jsb->s_feature_compat ||
521 jsb->s_feature_incompat ||
522 jsb->s_feature_ro_compat ||
524 clear_v2_journal_fields(journal);
527 case JFS_SUPERBLOCK_V2:
528 journal->j_format_version = 2;
529 if (ntohl(jsb->s_nr_users) > 1 &&
530 uuid_is_null(ctx->fs->super->s_journal_uuid))
531 clear_v2_journal_fields(journal);
532 if (ntohl(jsb->s_nr_users) > 1) {
533 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
534 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
539 * These should never appear in a journal super block, so if
540 * they do, the journal is badly corrupted.
542 case JFS_DESCRIPTOR_BLOCK:
543 case JFS_COMMIT_BLOCK:
544 case JFS_REVOKE_BLOCK:
545 return EXT2_ET_CORRUPT_SUPERBLOCK;
547 /* If we don't understand the superblock major type, but there
548 * is a magic number, then it is likely to be a new format we
549 * just don't understand, so leave it alone. */
551 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
554 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
555 return EXT2_ET_UNSUPP_FEATURE;
557 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
558 return EXT2_ET_RO_UNSUPP_FEATURE;
560 /* We have now checked whether we know enough about the journal
561 * format to be able to proceed safely, so any other checks that
562 * fail we should attempt to recover from. */
563 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
564 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
565 _("%s: no valid journal superblock found\n"),
567 return EXT2_ET_CORRUPT_SUPERBLOCK;
570 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
571 journal->j_maxlen = ntohl(jsb->s_maxlen);
572 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
573 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
574 _("%s: journal too short\n"),
576 return EXT2_ET_CORRUPT_SUPERBLOCK;
579 journal->j_tail_sequence = ntohl(jsb->s_sequence);
580 journal->j_transaction_sequence = journal->j_tail_sequence;
581 journal->j_tail = ntohl(jsb->s_start);
582 journal->j_first = ntohl(jsb->s_first);
583 journal->j_last = ntohl(jsb->s_maxlen);
588 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
599 /* Leave a valid existing V1 superblock signature alone.
600 * Anything unrecognisable we overwrite with a new V2
603 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
604 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
605 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
606 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
609 /* Zero out everything else beyond the superblock header */
611 p = ((char *) jsb) + sizeof(journal_header_t);
612 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
614 jsb->s_blocksize = htonl(ctx->fs->blocksize);
615 jsb->s_maxlen = htonl(journal->j_maxlen);
616 jsb->s_first = htonl(1);
618 /* Initialize the journal sequence number so that there is "no"
619 * chance we will find old "valid" transactions in the journal.
620 * This avoids the need to zero the whole journal (slow to do,
621 * and risky when we are just recovering the filesystem).
623 uuid_generate(u.uuid);
624 for (i = 0; i < 4; i ++)
626 jsb->s_sequence = htonl(new_seq);
628 mark_buffer_dirty(journal->j_sb_buffer);
629 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
632 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
634 struct problem_context *pctx)
636 struct ext2_super_block *sb = ctx->fs->super;
637 int recover = ctx->fs->super->s_feature_incompat &
638 EXT3_FEATURE_INCOMPAT_RECOVER;
640 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
641 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
642 e2fsck_journal_reset_super(ctx, journal->j_superblock,
644 journal->j_transaction_sequence = 1;
645 e2fsck_clear_recover(ctx, recover);
648 return EXT2_ET_CORRUPT_SUPERBLOCK;
649 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
650 return EXT2_ET_CORRUPT_SUPERBLOCK;
655 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
658 journal_superblock_t *jsb;
661 mark_buffer_clean(journal->j_sb_buffer);
662 else if (!(ctx->options & E2F_OPT_READONLY)) {
663 jsb = journal->j_superblock;
664 jsb->s_sequence = htonl(journal->j_transaction_sequence);
666 jsb->s_start = 0; /* this marks the journal as empty */
667 mark_buffer_dirty(journal->j_sb_buffer);
669 brelse(journal->j_sb_buffer);
671 if (ctx->journal_io) {
672 if (ctx->fs && ctx->fs->io != ctx->journal_io)
673 io_channel_close(ctx->journal_io);
678 if (journal->j_inode)
679 ext2fs_free_mem(&journal->j_inode);
681 if (journal->j_fs_dev)
682 ext2fs_free_mem(&journal->j_fs_dev);
683 ext2fs_free_mem(&journal);
687 * This function makes sure that the superblock fields regarding the
688 * journal are consistent.
690 int e2fsck_check_ext3_journal(e2fsck_t ctx)
692 struct ext2_super_block *sb = ctx->fs->super;
694 int recover = ctx->fs->super->s_feature_incompat &
695 EXT3_FEATURE_INCOMPAT_RECOVER;
696 struct problem_context pctx;
698 int reset = 0, force_fsck = 0;
701 /* If we don't have any journal features, don't do anything more */
702 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
703 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
704 uuid_is_null(sb->s_journal_uuid))
707 clear_problem_context(&pctx);
708 pctx.num = sb->s_journal_inum;
710 retval = e2fsck_get_journal(ctx, &journal);
712 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
713 (retval == EXT2_ET_BAD_BLOCK_NUM) ||
714 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
715 (retval == EXT2_ET_NO_JOURNAL))
716 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
720 retval = e2fsck_journal_load(journal);
722 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
723 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
724 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
726 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
727 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
729 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
730 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
731 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
733 e2fsck_journal_release(ctx, journal, 0, 1);
738 * We want to make the flags consistent here. We will not leave with
739 * needs_recovery set but has_journal clear. We can't get in a loop
740 * with -y, -n, or -p, only if a user isn't making up their mind.
743 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
744 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
746 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
748 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
751 * Need a full fsck if we are releasing a
752 * journal stored on a reserved inode.
754 force_fsck = recover ||
755 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
756 /* Clear all of the journal fields */
757 sb->s_journal_inum = 0;
758 sb->s_journal_dev = 0;
759 memset(sb->s_journal_uuid, 0,
760 sizeof(sb->s_journal_uuid));
761 e2fsck_clear_recover(ctx, force_fsck);
762 } else if (!(ctx->options & E2F_OPT_READONLY)) {
763 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
764 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
765 ext2fs_mark_super_dirty(ctx->fs);
769 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
770 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
771 journal->j_superblock->s_start != 0) {
772 /* Print status information */
773 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
775 problem = PR_0_JOURNAL_RUN_DEFAULT;
777 problem = PR_0_JOURNAL_RUN;
778 if (fix_problem(ctx, problem, &pctx)) {
779 ctx->options |= E2F_OPT_FORCE;
780 sb->s_feature_incompat |=
781 EXT3_FEATURE_INCOMPAT_RECOVER;
782 ext2fs_mark_super_dirty(ctx->fs);
783 } else if (fix_problem(ctx,
784 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
786 sb->s_state &= ~EXT2_VALID_FS;
787 ext2fs_mark_super_dirty(ctx->fs);
790 * If the user answers no to the above question, we
791 * ignore the fact that journal apparently has data;
792 * accidentally replaying over valid data would be far
793 * worse than skipping a questionable recovery.
795 * XXX should we abort with a fatal error here? What
796 * will the ext3 kernel code do if a filesystem with
797 * !NEEDS_RECOVERY but with a non-zero
798 * journal->j_superblock->s_start is mounted?
802 e2fsck_journal_release(ctx, journal, reset, 0);
806 static errcode_t recover_ext3_journal(e2fsck_t ctx)
808 struct problem_context pctx;
812 clear_problem_context(&pctx);
814 journal_init_revoke_caches();
815 retval = e2fsck_get_journal(ctx, &journal);
819 retval = e2fsck_journal_load(journal);
823 retval = journal_init_revoke(journal, 1024);
827 retval = -journal_recover(journal);
831 if (journal->j_failed_commit) {
832 pctx.ino = journal->j_failed_commit;
833 fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx);
834 ctx->fs->super->s_state |= EXT2_ERROR_FS;
835 ext2fs_mark_super_dirty(ctx->fs);
839 if (journal->j_superblock->s_errno) {
840 ctx->fs->super->s_state |= EXT2_ERROR_FS;
841 ext2fs_mark_super_dirty(ctx->fs);
842 journal->j_superblock->s_errno = 0;
843 mark_buffer_dirty(journal->j_sb_buffer);
847 journal_destroy_revoke(journal);
848 journal_destroy_revoke_caches();
849 e2fsck_journal_release(ctx, journal, 1, 0);
853 int e2fsck_run_ext3_journal(e2fsck_t ctx)
855 io_manager io_ptr = ctx->fs->io->manager;
856 int blocksize = ctx->fs->blocksize;
857 errcode_t retval, recover_retval;
859 unsigned long long kbytes_written = 0;
861 printf(_("%s: recovering journal\n"), ctx->device_name);
862 if (ctx->options & E2F_OPT_READONLY) {
863 printf(_("%s: won't do journal recovery while read-only\n"),
865 return EXT2_ET_FILE_RO;
868 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
869 ext2fs_flush(ctx->fs); /* Force out any modifications */
871 recover_retval = recover_ext3_journal(ctx);
874 * Reload the filesystem context to get up-to-date data from disk
875 * because journal recovery will change the filesystem under us.
877 if (ctx->fs->super->s_kbytes_written &&
878 ctx->fs->io->manager->get_stats)
879 ctx->fs->io->manager->get_stats(ctx->fs->io, &stats);
880 if (stats && stats->bytes_written)
881 kbytes_written = stats->bytes_written >> 10;
882 ext2fs_free(ctx->fs);
883 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
884 ctx->superblock, blocksize, io_ptr,
887 com_err(ctx->program_name, retval,
888 _("while trying to re-open %s"),
892 ctx->fs->priv_data = ctx;
893 ctx->fs->now = ctx->now;
894 ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
895 ctx->fs->super->s_kbytes_written += kbytes_written;
897 /* Set the superblock flags */
898 e2fsck_clear_recover(ctx, recover_retval);
899 return recover_retval;
903 * This function will move the journal inode from a visible file in
904 * the filesystem directory hierarchy to the reserved inode if necessary.
906 static const char * const journal_names[] = {
907 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
909 void e2fsck_move_ext3_journal(e2fsck_t ctx)
911 struct ext2_super_block *sb = ctx->fs->super;
912 struct problem_context pctx;
913 struct ext2_inode inode;
914 ext2_filsys fs = ctx->fs;
917 const char * const * cpp;
918 int group, mount_flags;
920 clear_problem_context(&pctx);
923 * If the filesystem is opened read-only, or there is no
924 * journal, then do nothing.
926 if ((ctx->options & E2F_OPT_READONLY) ||
927 (sb->s_journal_inum == 0) ||
928 !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
932 * Read in the journal inode
934 if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
938 * If it's necessary to backup the journal inode, do so.
940 if ((sb->s_jnl_backup_type == 0) ||
941 ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
942 memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
943 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
944 memcpy(sb->s_jnl_blocks, inode.i_block,
946 sb->s_jnl_blocks[16] = inode.i_size;
947 sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
948 ext2fs_mark_super_dirty(fs);
949 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
954 * If the journal is already the hidden inode, then do nothing
956 if (sb->s_journal_inum == EXT2_JOURNAL_INO)
960 * The journal inode had better have only one link and not be readable.
962 if (inode.i_links_count != 1)
966 * If the filesystem is mounted, or we can't tell whether
967 * or not it's mounted, do nothing.
969 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
970 if (retval || (mount_flags & EXT2_MF_MOUNTED))
974 * If we can't find the name of the journal inode, then do
977 for (cpp = journal_names; *cpp; cpp++) {
978 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
979 strlen(*cpp), 0, &ino);
980 if ((retval == 0) && (ino == sb->s_journal_inum))
986 /* We need the inode bitmap to be loaded */
987 retval = ext2fs_read_bitmaps(fs);
992 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
996 * OK, we've done all the checks, let's actually move the
997 * journal inode. Errors at this point mean we need to force
998 * an ext2 filesystem check.
1000 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
1002 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
1004 sb->s_journal_inum = EXT2_JOURNAL_INO;
1005 ext2fs_mark_super_dirty(fs);
1006 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1007 inode.i_links_count = 0;
1008 inode.i_dtime = ctx->now;
1009 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
1012 group = ext2fs_group_of_ino(fs, ino);
1013 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
1014 ext2fs_mark_ib_dirty(fs);
1015 fs->group_desc[group].bg_free_inodes_count++;
1016 ext2fs_group_desc_csum_set(fs, group);
1017 fs->super->s_free_inodes_count++;
1021 pctx.errcode = retval;
1022 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
1023 fs->super->s_state &= ~EXT2_VALID_FS;
1024 ext2fs_mark_super_dirty(fs);
1029 * This function makes sure the superblock hint for the external
1030 * journal is correct.
1032 int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
1034 struct ext2_super_block *sb = ctx->fs->super;
1035 struct problem_context pctx;
1036 char uuid[37], *journal_name;
1039 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
1040 uuid_is_null(sb->s_journal_uuid))
1043 uuid_unparse(sb->s_journal_uuid, uuid);
1044 journal_name = blkid_get_devname(ctx->blkid, "UUID", uuid);
1048 if (stat(journal_name, &st) < 0)
1051 if (st.st_rdev != sb->s_journal_dev) {
1052 clear_problem_context(&pctx);
1053 pctx.num = st.st_rdev;
1054 if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) {
1055 sb->s_journal_dev = st.st_rdev;
1056 ext2fs_mark_super_dirty(ctx->fs);