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 /* Checksumming functions */
44 static int e2fsck_journal_verify_csum_type(journal_t *j,
45 journal_superblock_t *jsb)
47 if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2))
50 return jsb->s_checksum_type == JBD2_CRC32C_CHKSUM;
53 static __u32 e2fsck_journal_sb_csum(journal_superblock_t *jsb)
57 old_crc = jsb->s_checksum;
59 crc = ext2fs_crc32c_le(~0, (unsigned char *)jsb,
60 sizeof(journal_superblock_t));
61 jsb->s_checksum = old_crc;
66 static int e2fsck_journal_sb_csum_verify(journal_t *j,
67 journal_superblock_t *jsb)
69 __u32 provided, calculated;
71 if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2))
74 provided = ext2fs_be32_to_cpu(jsb->s_checksum);
75 calculated = e2fsck_journal_sb_csum(jsb);
77 return provided == calculated;
80 static errcode_t e2fsck_journal_sb_csum_set(journal_t *j,
81 journal_superblock_t *jsb)
85 if (!JFS_HAS_INCOMPAT_FEATURE(j, JFS_FEATURE_INCOMPAT_CSUM_V2))
88 crc = e2fsck_journal_sb_csum(jsb);
89 jsb->s_checksum = ext2fs_cpu_to_be32(crc);
93 /* Kernel compatibility functions for handling the journal. These allow us
94 * to use the recovery.c file virtually unchanged from the kernel, so we
95 * don't have to do much to keep kernel and user recovery in sync.
97 int journal_bmap(journal_t *journal, blk64_t block, unsigned long long *phys)
103 struct inode *inode = journal->j_inode;
112 retval= ext2fs_bmap2(inode->i_ctx->fs, inode->i_ino,
113 &inode->i_ext2, NULL, 0, block, 0, &pblk);
119 struct buffer_head *getblk(kdev_t kdev, blk64_t blocknr, int blocksize)
121 struct buffer_head *bh;
122 int bufsize = sizeof(*bh) + kdev->k_ctx->fs->blocksize -
125 bh = e2fsck_allocate_memory(kdev->k_ctx, bufsize, "block buffer");
129 #ifdef CONFIG_JBD_DEBUG
130 if (journal_enable_debug >= 3)
133 jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
134 (unsigned long long) blocknr, blocksize, bh_count);
136 bh->b_ctx = kdev->k_ctx;
137 if (kdev->k_dev == K_DEV_FS)
138 bh->b_io = kdev->k_ctx->fs->io;
140 bh->b_io = kdev->k_ctx->journal_io;
141 bh->b_size = blocksize;
142 bh->b_blocknr = blocknr;
147 void sync_blockdev(kdev_t kdev)
151 if (kdev->k_dev == K_DEV_FS)
152 io = kdev->k_ctx->fs->io;
154 io = kdev->k_ctx->journal_io;
156 io_channel_flush(io);
159 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
162 struct buffer_head *bh;
164 for (; nr > 0; --nr) {
166 if (rw == READ && !bh->b_uptodate) {
167 jfs_debug(3, "reading block %llu/%p\n",
168 bh->b_blocknr, (void *) bh);
169 retval = io_channel_read_blk64(bh->b_io,
173 com_err(bh->b_ctx->device_name, retval,
174 "while reading block %llu\n",
176 bh->b_err = (int) retval;
180 } else if (rw == WRITE && bh->b_dirty) {
181 jfs_debug(3, "writing block %llu/%p\n",
184 retval = io_channel_write_blk64(bh->b_io,
188 com_err(bh->b_ctx->device_name, retval,
189 "while writing block %llu\n",
191 bh->b_err = (int) retval;
197 jfs_debug(3, "no-op %s for block %llu\n",
198 rw == READ ? "read" : "write",
204 void mark_buffer_dirty(struct buffer_head *bh)
209 static void mark_buffer_clean(struct buffer_head * bh)
214 void brelse(struct buffer_head *bh)
217 ll_rw_block(WRITE, 1, &bh);
218 jfs_debug(3, "freeing block %llu/%p (total %d)\n",
219 bh->b_blocknr, (void *) bh, --bh_count);
220 ext2fs_free_mem(&bh);
223 int buffer_uptodate(struct buffer_head *bh)
225 return bh->b_uptodate;
228 void mark_buffer_uptodate(struct buffer_head *bh, int val)
230 bh->b_uptodate = val;
233 void wait_on_buffer(struct buffer_head *bh)
236 ll_rw_block(READ, 1, &bh);
240 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
242 ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
244 /* if we had an error doing journal recovery, we need a full fsck */
246 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
247 ext2fs_mark_super_dirty(ctx->fs);
251 * This is a helper function to check the validity of the journal.
253 struct process_block_struct {
254 e2_blkcnt_t last_block;
257 static int process_journal_block(ext2_filsys fs,
259 e2_blkcnt_t blockcnt,
260 blk64_t ref_block EXT2FS_ATTR((unused)),
261 int ref_offset EXT2FS_ATTR((unused)),
264 struct process_block_struct *p;
265 blk64_t blk = *block_nr;
267 p = (struct process_block_struct *) priv_data;
269 if (!blk || blk < fs->super->s_first_data_block ||
270 blk >= ext2fs_blocks_count(fs->super))
274 p->last_block = blockcnt;
278 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
280 struct process_block_struct pb;
281 struct ext2_super_block *sb = ctx->fs->super;
282 struct ext2_super_block jsuper;
283 struct problem_context pctx;
284 struct buffer_head *bh;
285 struct inode *j_inode = NULL;
286 struct kdev_s *dev_fs = NULL, *dev_journal;
287 const char *journal_name = 0;
288 journal_t *journal = NULL;
289 errcode_t retval = 0;
290 io_manager io_ptr = 0;
291 unsigned long long start = 0;
293 int tried_backup_jnl = 0;
295 clear_problem_context(&pctx);
297 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
299 return EXT2_ET_NO_MEMORY;
302 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
304 retval = EXT2_ET_NO_MEMORY;
307 dev_journal = dev_fs+1;
309 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
310 dev_fs->k_dev = K_DEV_FS;
311 dev_journal->k_dev = K_DEV_JOURNAL;
313 journal->j_dev = dev_journal;
314 journal->j_fs_dev = dev_fs;
315 journal->j_inode = NULL;
316 journal->j_blocksize = ctx->fs->blocksize;
318 if (uuid_is_null(sb->s_journal_uuid)) {
319 if (!sb->s_journal_inum) {
320 retval = EXT2_ET_BAD_INODE_NUM;
323 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
326 retval = EXT2_ET_NO_MEMORY;
330 j_inode->i_ctx = ctx;
331 j_inode->i_ino = sb->s_journal_inum;
333 if ((retval = ext2fs_read_inode(ctx->fs,
335 &j_inode->i_ext2))) {
337 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
340 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
341 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
343 j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15];
344 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
345 j_inode->i_ext2.i_links_count = 1;
346 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
347 e2fsck_use_inode_shortcuts(ctx, 1);
348 ctx->stashed_ino = j_inode->i_ino;
349 ctx->stashed_inode = &j_inode->i_ext2;
352 if (!j_inode->i_ext2.i_links_count ||
353 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
354 retval = EXT2_ET_NO_JOURNAL;
355 goto try_backup_journal;
357 if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize <
358 JFS_MIN_JOURNAL_BLOCKS) {
359 retval = EXT2_ET_JOURNAL_TOO_SMALL;
360 goto try_backup_journal;
363 retval = ext2fs_block_iterate3(ctx->fs, j_inode->i_ino,
365 process_journal_block, &pb);
366 if ((pb.last_block + 1) * ctx->fs->blocksize <
367 (int) EXT2_I_SIZE(&j_inode->i_ext2)) {
368 retval = EXT2_ET_JOURNAL_TOO_SMALL;
369 goto try_backup_journal;
371 if (tried_backup_jnl && !(ctx->options & E2F_OPT_READONLY)) {
372 retval = ext2fs_write_inode(ctx->fs, sb->s_journal_inum,
378 journal->j_maxlen = EXT2_I_SIZE(&j_inode->i_ext2) /
379 journal->j_blocksize;
382 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
388 io_ptr = inode_io_manager;
390 journal->j_inode = j_inode;
391 ctx->journal_io = ctx->fs->io;
392 if ((retval = (errcode_t) journal_bmap(journal, 0, &start)) != 0)
397 if (!ctx->journal_name) {
400 uuid_unparse(sb->s_journal_uuid, uuid);
401 ctx->journal_name = blkid_get_devname(ctx->blkid,
403 if (!ctx->journal_name)
404 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
406 journal_name = ctx->journal_name;
409 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
410 retval = EXT2_ET_LOAD_EXT_JOURNAL;
414 jfs_debug(1, "Using journal file %s\n", journal_name);
415 io_ptr = unix_io_manager;
419 test_io_backing_manager = io_ptr;
420 io_ptr = test_io_manager;
426 int flags = IO_FLAG_RW;
427 if (!(ctx->mount_flags & EXT2_MF_ISROOT &&
428 ctx->mount_flags & EXT2_MF_READONLY))
429 flags |= IO_FLAG_EXCLUSIVE;
430 if ((ctx->mount_flags & EXT2_MF_READONLY) &&
431 (ctx->options & E2F_OPT_FORCE))
432 flags &= ~IO_FLAG_EXCLUSIVE;
435 retval = io_ptr->open(journal_name, flags,
441 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
446 start = ext2fs_journal_sb_start(ctx->fs->blocksize) - 1;
447 bh = getblk(dev_journal, start, ctx->fs->blocksize);
449 retval = EXT2_ET_NO_MEMORY;
452 ll_rw_block(READ, 1, &bh);
453 if ((retval = bh->b_err) != 0) {
457 memcpy(&jsuper, start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET,
460 #ifdef WORDS_BIGENDIAN
461 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
462 ext2fs_swap_super(&jsuper);
464 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
465 !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
466 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
467 retval = EXT2_ET_LOAD_EXT_JOURNAL;
470 /* Make sure the journal UUID is correct */
471 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
472 sizeof(jsuper.s_uuid))) {
473 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
474 retval = EXT2_ET_LOAD_EXT_JOURNAL;
478 maxlen = ext2fs_blocks_count(&jsuper);
479 journal->j_maxlen = (maxlen < 1ULL << 32) ? maxlen : (1ULL << 32) - 1;
483 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
484 retval = EXT2_ET_NO_MEMORY;
488 journal->j_sb_buffer = bh;
489 journal->j_superblock = (journal_superblock_t *)bh->b_data;
493 ext2fs_free_mem(&j_inode);
496 *ret_journal = journal;
497 e2fsck_use_inode_shortcuts(ctx, 0);
501 e2fsck_use_inode_shortcuts(ctx, 0);
503 ext2fs_free_mem(&dev_fs);
505 ext2fs_free_mem(&j_inode);
507 ext2fs_free_mem(&journal);
511 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
512 struct problem_context *pctx)
514 struct ext2_super_block *sb = ctx->fs->super;
515 int recover = ctx->fs->super->s_feature_incompat &
516 EXT3_FEATURE_INCOMPAT_RECOVER;
517 int has_journal = ctx->fs->super->s_feature_compat &
518 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
520 if (has_journal || sb->s_journal_inum) {
521 /* The journal inode is bogus, remove and force full fsck */
522 pctx->ino = sb->s_journal_inum;
523 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
524 if (has_journal && sb->s_journal_inum)
525 printf("*** ext3 journal has been deleted - "
526 "filesystem is now ext2 only ***\n\n");
527 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
528 sb->s_journal_inum = 0;
529 ctx->flags |= E2F_FLAG_JOURNAL_INODE;
530 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
531 e2fsck_clear_recover(ctx, 1);
534 return EXT2_ET_BAD_INODE_NUM;
535 } else if (recover) {
536 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
537 e2fsck_clear_recover(ctx, 1);
540 return EXT2_ET_UNSUPP_FEATURE;
545 #define V1_SB_SIZE 0x0024
546 static void clear_v2_journal_fields(journal_t *journal)
548 e2fsck_t ctx = journal->j_dev->k_ctx;
549 struct problem_context pctx;
551 clear_problem_context(&pctx);
553 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
556 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
557 ctx->fs->blocksize-V1_SB_SIZE);
558 mark_buffer_dirty(journal->j_sb_buffer);
562 static errcode_t e2fsck_journal_load(journal_t *journal)
564 e2fsck_t ctx = journal->j_dev->k_ctx;
565 journal_superblock_t *jsb;
566 struct buffer_head *jbh = journal->j_sb_buffer;
567 struct problem_context pctx;
569 clear_problem_context(&pctx);
571 ll_rw_block(READ, 1, &jbh);
573 com_err(ctx->device_name, jbh->b_err, "%s",
574 _("reading journal superblock\n"));
578 jsb = journal->j_superblock;
579 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
580 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
581 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
583 switch (ntohl(jsb->s_header.h_blocktype)) {
584 case JFS_SUPERBLOCK_V1:
585 journal->j_format_version = 1;
586 if (jsb->s_feature_compat ||
587 jsb->s_feature_incompat ||
588 jsb->s_feature_ro_compat ||
590 clear_v2_journal_fields(journal);
593 case JFS_SUPERBLOCK_V2:
594 journal->j_format_version = 2;
595 if (ntohl(jsb->s_nr_users) > 1 &&
596 uuid_is_null(ctx->fs->super->s_journal_uuid))
597 clear_v2_journal_fields(journal);
598 if (ntohl(jsb->s_nr_users) > 1) {
599 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
600 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
605 * These should never appear in a journal super block, so if
606 * they do, the journal is badly corrupted.
608 case JFS_DESCRIPTOR_BLOCK:
609 case JFS_COMMIT_BLOCK:
610 case JFS_REVOKE_BLOCK:
611 return EXT2_ET_CORRUPT_SUPERBLOCK;
613 /* If we don't understand the superblock major type, but there
614 * is a magic number, then it is likely to be a new format we
615 * just don't understand, so leave it alone. */
617 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
620 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
621 return EXT2_ET_UNSUPP_FEATURE;
623 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
624 return EXT2_ET_RO_UNSUPP_FEATURE;
626 /* Checksum v1 and v2 are mutually exclusive features. */
627 if (JFS_HAS_INCOMPAT_FEATURE(journal, JFS_FEATURE_INCOMPAT_CSUM_V2) &&
628 JFS_HAS_COMPAT_FEATURE(journal, JFS_FEATURE_COMPAT_CHECKSUM))
629 return EXT2_ET_CORRUPT_SUPERBLOCK;
631 if (!e2fsck_journal_verify_csum_type(journal, jsb) ||
632 !e2fsck_journal_sb_csum_verify(journal, jsb))
633 return EXT2_ET_CORRUPT_SUPERBLOCK;
635 /* We have now checked whether we know enough about the journal
636 * format to be able to proceed safely, so any other checks that
637 * fail we should attempt to recover from. */
638 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
639 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
640 _("%s: no valid journal superblock found\n"),
642 return EXT2_ET_CORRUPT_SUPERBLOCK;
645 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
646 journal->j_maxlen = ntohl(jsb->s_maxlen);
647 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
648 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
649 _("%s: journal too short\n"),
651 return EXT2_ET_CORRUPT_SUPERBLOCK;
654 journal->j_tail_sequence = ntohl(jsb->s_sequence);
655 journal->j_transaction_sequence = journal->j_tail_sequence;
656 journal->j_tail = ntohl(jsb->s_start);
657 journal->j_first = ntohl(jsb->s_first);
658 journal->j_last = ntohl(jsb->s_maxlen);
663 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
674 /* Leave a valid existing V1 superblock signature alone.
675 * Anything unrecognisable we overwrite with a new V2
678 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
679 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
680 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
681 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
684 /* Zero out everything else beyond the superblock header */
686 p = ((char *) jsb) + sizeof(journal_header_t);
687 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
689 jsb->s_blocksize = htonl(ctx->fs->blocksize);
690 jsb->s_maxlen = htonl(journal->j_maxlen);
691 jsb->s_first = htonl(1);
693 /* Initialize the journal sequence number so that there is "no"
694 * chance we will find old "valid" transactions in the journal.
695 * This avoids the need to zero the whole journal (slow to do,
696 * and risky when we are just recovering the filesystem).
698 uuid_generate(u.uuid);
699 for (i = 0; i < 4; i ++)
701 jsb->s_sequence = htonl(new_seq);
702 e2fsck_journal_sb_csum_set(journal, jsb);
704 mark_buffer_dirty(journal->j_sb_buffer);
705 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
708 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
710 struct problem_context *pctx)
712 struct ext2_super_block *sb = ctx->fs->super;
713 int recover = ctx->fs->super->s_feature_incompat &
714 EXT3_FEATURE_INCOMPAT_RECOVER;
716 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
717 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
718 e2fsck_journal_reset_super(ctx, journal->j_superblock,
720 journal->j_transaction_sequence = 1;
721 e2fsck_clear_recover(ctx, recover);
724 return EXT2_ET_CORRUPT_SUPERBLOCK;
725 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
726 return EXT2_ET_CORRUPT_SUPERBLOCK;
731 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
734 journal_superblock_t *jsb;
737 mark_buffer_clean(journal->j_sb_buffer);
738 else if (!(ctx->options & E2F_OPT_READONLY)) {
739 jsb = journal->j_superblock;
740 jsb->s_sequence = htonl(journal->j_transaction_sequence);
742 jsb->s_start = 0; /* this marks the journal as empty */
743 e2fsck_journal_sb_csum_set(journal, jsb);
744 mark_buffer_dirty(journal->j_sb_buffer);
746 brelse(journal->j_sb_buffer);
748 if (ctx->journal_io) {
749 if (ctx->fs && ctx->fs->io != ctx->journal_io)
750 io_channel_close(ctx->journal_io);
755 if (journal->j_inode)
756 ext2fs_free_mem(&journal->j_inode);
758 if (journal->j_fs_dev)
759 ext2fs_free_mem(&journal->j_fs_dev);
760 ext2fs_free_mem(&journal);
764 * This function makes sure that the superblock fields regarding the
765 * journal are consistent.
767 errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx)
769 struct ext2_super_block *sb = ctx->fs->super;
771 int recover = ctx->fs->super->s_feature_incompat &
772 EXT3_FEATURE_INCOMPAT_RECOVER;
773 struct problem_context pctx;
775 int reset = 0, force_fsck = 0;
778 /* If we don't have any journal features, don't do anything more */
779 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
780 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
781 uuid_is_null(sb->s_journal_uuid))
784 clear_problem_context(&pctx);
785 pctx.num = sb->s_journal_inum;
787 retval = e2fsck_get_journal(ctx, &journal);
789 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
790 (retval == EXT2_ET_BAD_BLOCK_NUM) ||
791 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
792 (retval == EXT2_ET_NO_JOURNAL))
793 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
797 retval = e2fsck_journal_load(journal);
799 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
800 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
801 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
803 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
804 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
806 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
807 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
808 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
810 e2fsck_journal_release(ctx, journal, 0, 1);
815 * We want to make the flags consistent here. We will not leave with
816 * needs_recovery set but has_journal clear. We can't get in a loop
817 * with -y, -n, or -p, only if a user isn't making up their mind.
820 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
821 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
822 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
824 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
827 * Need a full fsck if we are releasing a
828 * journal stored on a reserved inode.
830 force_fsck = recover ||
831 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
832 /* Clear all of the journal fields */
833 sb->s_journal_inum = 0;
834 sb->s_journal_dev = 0;
835 memset(sb->s_journal_uuid, 0,
836 sizeof(sb->s_journal_uuid));
837 e2fsck_clear_recover(ctx, force_fsck);
838 } else if (!(ctx->options & E2F_OPT_READONLY)) {
839 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
840 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
841 ext2fs_mark_super_dirty(ctx->fs);
845 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
846 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
847 journal->j_superblock->s_start != 0) {
848 /* Print status information */
849 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
851 problem = PR_0_JOURNAL_RUN_DEFAULT;
853 problem = PR_0_JOURNAL_RUN;
854 if (fix_problem(ctx, problem, &pctx)) {
855 ctx->options |= E2F_OPT_FORCE;
856 sb->s_feature_incompat |=
857 EXT3_FEATURE_INCOMPAT_RECOVER;
858 ext2fs_mark_super_dirty(ctx->fs);
859 } else if (fix_problem(ctx,
860 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
862 sb->s_state &= ~EXT2_VALID_FS;
863 ext2fs_mark_super_dirty(ctx->fs);
866 * If the user answers no to the above question, we
867 * ignore the fact that journal apparently has data;
868 * accidentally replaying over valid data would be far
869 * worse than skipping a questionable recovery.
871 * XXX should we abort with a fatal error here? What
872 * will the ext3 kernel code do if a filesystem with
873 * !NEEDS_RECOVERY but with a non-zero
874 * journal->j_superblock->s_start is mounted?
879 * If we don't need to do replay the journal, check to see if
880 * the journal's errno is set; if so, we need to mark the file
881 * system as being corrupt and clear the journal's s_errno.
883 if (!(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
884 journal->j_superblock->s_errno) {
885 ctx->fs->super->s_state |= EXT2_ERROR_FS;
886 ext2fs_mark_super_dirty(ctx->fs);
887 journal->j_superblock->s_errno = 0;
888 e2fsck_journal_sb_csum_set(journal, journal->j_superblock);
889 mark_buffer_dirty(journal->j_sb_buffer);
892 e2fsck_journal_release(ctx, journal, reset, 0);
896 static errcode_t recover_ext3_journal(e2fsck_t ctx)
898 struct problem_context pctx;
902 clear_problem_context(&pctx);
904 journal_init_revoke_caches();
905 retval = e2fsck_get_journal(ctx, &journal);
909 retval = e2fsck_journal_load(journal);
913 retval = journal_init_revoke(journal, 1024);
917 retval = -journal_recover(journal);
921 if (journal->j_failed_commit) {
922 pctx.ino = journal->j_failed_commit;
923 fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx);
924 journal->j_superblock->s_errno = -EINVAL;
925 mark_buffer_dirty(journal->j_sb_buffer);
929 journal_destroy_revoke(journal);
930 journal_destroy_revoke_caches();
931 e2fsck_journal_release(ctx, journal, 1, 0);
935 errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx)
937 io_manager io_ptr = ctx->fs->io->manager;
938 int blocksize = ctx->fs->blocksize;
939 errcode_t retval, recover_retval;
941 unsigned long long kbytes_written = 0;
943 printf(_("%s: recovering journal\n"), ctx->device_name);
944 if (ctx->options & E2F_OPT_READONLY) {
945 printf(_("%s: won't do journal recovery while read-only\n"),
947 return EXT2_ET_FILE_RO;
950 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
951 ext2fs_flush(ctx->fs); /* Force out any modifications */
953 recover_retval = recover_ext3_journal(ctx);
956 * Reload the filesystem context to get up-to-date data from disk
957 * because journal recovery will change the filesystem under us.
959 if (ctx->fs->super->s_kbytes_written &&
960 ctx->fs->io->manager->get_stats)
961 ctx->fs->io->manager->get_stats(ctx->fs->io, &stats);
962 if (stats && stats->bytes_written)
963 kbytes_written = stats->bytes_written >> 10;
965 ext2fs_mmp_stop(ctx->fs);
966 ext2fs_free(ctx->fs);
967 retval = ext2fs_open(ctx->filesystem_name, ctx->openfs_flags,
968 ctx->superblock, blocksize, io_ptr,
971 com_err(ctx->program_name, retval,
972 _("while trying to re-open %s"),
976 ctx->fs->priv_data = ctx;
977 ctx->fs->now = ctx->now;
978 ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
979 ctx->fs->super->s_kbytes_written += kbytes_written;
981 /* Set the superblock flags */
982 e2fsck_clear_recover(ctx, recover_retval != 0);
985 * Do one last sanity check, and propagate journal->s_errno to
986 * the EXT2_ERROR_FS flag in the fs superblock if needed.
988 retval = e2fsck_check_ext3_journal(ctx);
989 return retval ? retval : recover_retval;
993 * This function will move the journal inode from a visible file in
994 * the filesystem directory hierarchy to the reserved inode if necessary.
996 static const char * const journal_names[] = {
997 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
999 void e2fsck_move_ext3_journal(e2fsck_t ctx)
1001 struct ext2_super_block *sb = ctx->fs->super;
1002 struct problem_context pctx;
1003 struct ext2_inode inode;
1004 ext2_filsys fs = ctx->fs;
1007 const char * const * cpp;
1011 clear_problem_context(&pctx);
1014 * If the filesystem is opened read-only, or there is no
1015 * journal, then do nothing.
1017 if ((ctx->options & E2F_OPT_READONLY) ||
1018 (sb->s_journal_inum == 0) ||
1019 !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
1023 * Read in the journal inode
1025 if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
1029 * If it's necessary to backup the journal inode, do so.
1031 if ((sb->s_jnl_backup_type == 0) ||
1032 ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
1033 memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
1034 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
1035 memcpy(sb->s_jnl_blocks, inode.i_block,
1037 sb->s_jnl_blocks[15] = inode.i_size_high;
1038 sb->s_jnl_blocks[16] = inode.i_size;
1039 sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1040 ext2fs_mark_super_dirty(fs);
1041 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1046 * If the journal is already the hidden inode, then do nothing
1048 if (sb->s_journal_inum == EXT2_JOURNAL_INO)
1052 * The journal inode had better have only one link and not be readable.
1054 if (inode.i_links_count != 1)
1058 * If the filesystem is mounted, or we can't tell whether
1059 * or not it's mounted, do nothing.
1061 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
1062 if (retval || (mount_flags & EXT2_MF_MOUNTED))
1066 * If we can't find the name of the journal inode, then do
1069 for (cpp = journal_names; *cpp; cpp++) {
1070 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
1071 strlen(*cpp), 0, &ino);
1072 if ((retval == 0) && (ino == sb->s_journal_inum))
1078 /* We need the inode bitmap to be loaded */
1079 retval = ext2fs_read_bitmaps(fs);
1084 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
1088 * OK, we've done all the checks, let's actually move the
1089 * journal inode. Errors at this point mean we need to force
1090 * an ext2 filesystem check.
1092 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
1094 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
1096 sb->s_journal_inum = EXT2_JOURNAL_INO;
1097 ext2fs_mark_super_dirty(fs);
1098 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1099 inode.i_links_count = 0;
1100 inode.i_dtime = ctx->now;
1101 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
1104 group = ext2fs_group_of_ino(fs, ino);
1105 ext2fs_unmark_inode_bitmap2(fs->inode_map, ino);
1106 ext2fs_mark_ib_dirty(fs);
1107 ext2fs_bg_free_inodes_count_set(fs, group, ext2fs_bg_free_inodes_count(fs, group) + 1);
1108 ext2fs_group_desc_csum_set(fs, group);
1109 fs->super->s_free_inodes_count++;
1113 pctx.errcode = retval;
1114 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
1115 fs->super->s_state &= ~EXT2_VALID_FS;
1116 ext2fs_mark_super_dirty(fs);
1121 * This function makes sure the superblock hint for the external
1122 * journal is correct.
1124 int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
1126 struct ext2_super_block *sb = ctx->fs->super;
1127 struct problem_context pctx;
1128 char uuid[37], *journal_name;
1131 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
1132 uuid_is_null(sb->s_journal_uuid))
1135 uuid_unparse(sb->s_journal_uuid, uuid);
1136 journal_name = blkid_get_devname(ctx->blkid, "UUID", uuid);
1140 if (stat(journal_name, &st) < 0) {
1145 if (st.st_rdev != sb->s_journal_dev) {
1146 clear_problem_context(&pctx);
1147 pctx.num = st.st_rdev;
1148 if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) {
1149 sb->s_journal_dev = st.st_rdev;
1150 ext2fs_mark_super_dirty(ctx->fs);