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 (!jbd2_journal_has_csum_v2or3(j))
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 (!jbd2_journal_has_csum_v2or3(j))
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 (!jbd2_journal_has_csum_v2or3(j))
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 jbd2_journal_bmap(journal_t *journal, blk64_t block,
98 unsigned long long *phys)
104 struct inode *inode = journal->j_inode;
113 retval= ext2fs_bmap2(inode->i_ctx->fs, inode->i_ino,
114 &inode->i_ext2, NULL, 0, block, 0, &pblk);
116 return -1 * ((int) retval);
120 struct buffer_head *getblk(kdev_t kdev, blk64_t blocknr, int blocksize)
122 struct buffer_head *bh;
123 int bufsize = sizeof(*bh) + kdev->k_ctx->fs->blocksize -
126 bh = e2fsck_allocate_memory(kdev->k_ctx, bufsize, "block buffer");
130 #ifdef CONFIG_JBD_DEBUG
131 if (journal_enable_debug >= 3)
134 jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
135 (unsigned long long) blocknr, blocksize, bh_count);
137 bh->b_ctx = kdev->k_ctx;
138 if (kdev->k_dev == K_DEV_FS)
139 bh->b_io = kdev->k_ctx->fs->io;
141 bh->b_io = kdev->k_ctx->journal_io;
142 bh->b_size = blocksize;
143 bh->b_blocknr = blocknr;
148 int sync_blockdev(kdev_t kdev)
152 if (kdev->k_dev == K_DEV_FS)
153 io = kdev->k_ctx->fs->io;
155 io = kdev->k_ctx->journal_io;
157 return io_channel_flush(io) ? -EIO : 0;
160 void ll_rw_block(int rw, int op_flags, int nr, struct buffer_head *bhp[])
163 struct buffer_head *bh;
165 for (; nr > 0; --nr) {
167 if (rw == REQ_OP_READ && !bh->b_uptodate) {
168 jfs_debug(3, "reading block %llu/%p\n",
169 bh->b_blocknr, (void *) bh);
170 retval = io_channel_read_blk64(bh->b_io,
174 com_err(bh->b_ctx->device_name, retval,
175 "while reading block %llu\n",
177 bh->b_err = (int) retval;
181 } else if (rw == REQ_OP_WRITE && bh->b_dirty) {
182 jfs_debug(3, "writing block %llu/%p\n",
185 retval = io_channel_write_blk64(bh->b_io,
189 com_err(bh->b_ctx->device_name, retval,
190 "while writing block %llu\n",
192 bh->b_err = (int) retval;
198 jfs_debug(3, "no-op %s for block %llu\n",
199 rw == REQ_OP_READ ? "read" : "write",
205 void mark_buffer_dirty(struct buffer_head *bh)
210 static void mark_buffer_clean(struct buffer_head * bh)
215 void brelse(struct buffer_head *bh)
218 ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
219 jfs_debug(3, "freeing block %llu/%p (total %d)\n",
220 bh->b_blocknr, (void *) bh, --bh_count);
221 ext2fs_free_mem(&bh);
224 int buffer_uptodate(struct buffer_head *bh)
226 return bh->b_uptodate;
229 void mark_buffer_uptodate(struct buffer_head *bh, int val)
231 bh->b_uptodate = val;
234 void wait_on_buffer(struct buffer_head *bh)
237 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
241 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
243 ext2fs_clear_feature_journal_needs_recovery(ctx->fs->super);
245 /* if we had an error doing journal recovery, we need a full fsck */
247 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
248 ext2fs_mark_super_dirty(ctx->fs);
252 * This is a helper function to check the validity of the journal.
254 struct process_block_struct {
255 e2_blkcnt_t last_block;
258 static int process_journal_block(ext2_filsys fs,
260 e2_blkcnt_t blockcnt,
261 blk64_t ref_block EXT2FS_ATTR((unused)),
262 int ref_offset EXT2FS_ATTR((unused)),
265 struct process_block_struct *p;
266 blk64_t blk = *block_nr;
268 p = (struct process_block_struct *) priv_data;
270 if (!blk || blk < fs->super->s_first_data_block ||
271 blk >= ext2fs_blocks_count(fs->super))
275 p->last_block = blockcnt;
279 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
281 struct process_block_struct pb;
282 struct ext2_super_block *sb = ctx->fs->super;
283 struct ext2_super_block jsuper;
284 struct problem_context pctx;
285 struct buffer_head *bh;
286 struct inode *j_inode = NULL;
287 struct kdev_s *dev_fs = NULL, *dev_journal;
288 const char *journal_name = 0;
289 journal_t *journal = NULL;
290 errcode_t retval = 0;
291 io_manager io_ptr = 0;
292 unsigned long long start = 0;
295 int tried_backup_jnl = 0;
297 clear_problem_context(&pctx);
299 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
301 return EXT2_ET_NO_MEMORY;
304 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
306 retval = EXT2_ET_NO_MEMORY;
309 dev_journal = dev_fs+1;
311 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
312 dev_fs->k_dev = K_DEV_FS;
313 dev_journal->k_dev = K_DEV_JOURNAL;
315 journal->j_dev = dev_journal;
316 journal->j_fs_dev = dev_fs;
317 journal->j_inode = NULL;
318 journal->j_blocksize = ctx->fs->blocksize;
320 if (uuid_is_null(sb->s_journal_uuid)) {
321 if (!sb->s_journal_inum) {
322 retval = EXT2_ET_BAD_INODE_NUM;
325 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
328 retval = EXT2_ET_NO_MEMORY;
332 j_inode->i_ctx = ctx;
333 j_inode->i_ino = sb->s_journal_inum;
335 if ((retval = ext2fs_read_inode(ctx->fs,
337 &j_inode->i_ext2))) {
339 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
342 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
343 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
345 j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15];
346 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
347 j_inode->i_ext2.i_links_count = 1;
348 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
349 e2fsck_use_inode_shortcuts(ctx, 1);
350 ctx->stashed_ino = j_inode->i_ino;
351 ctx->stashed_inode = &j_inode->i_ext2;
354 if (!j_inode->i_ext2.i_links_count ||
355 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
356 retval = EXT2_ET_NO_JOURNAL;
357 goto try_backup_journal;
359 if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize <
360 JBD2_MIN_JOURNAL_BLOCKS) {
361 retval = EXT2_ET_JOURNAL_TOO_SMALL;
362 goto try_backup_journal;
365 retval = ext2fs_block_iterate3(ctx->fs, j_inode->i_ino,
367 process_journal_block, &pb);
368 if ((pb.last_block + 1) * ctx->fs->blocksize <
369 (int) EXT2_I_SIZE(&j_inode->i_ext2)) {
370 retval = EXT2_ET_JOURNAL_TOO_SMALL;
371 goto try_backup_journal;
373 if (tried_backup_jnl && !(ctx->options & E2F_OPT_READONLY)) {
374 retval = ext2fs_write_inode(ctx->fs, sb->s_journal_inum,
380 journal->j_maxlen = EXT2_I_SIZE(&j_inode->i_ext2) /
381 journal->j_blocksize;
384 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
390 io_ptr = inode_io_manager;
392 journal->j_inode = j_inode;
393 ctx->journal_io = ctx->fs->io;
394 if ((ret = jbd2_journal_bmap(journal, 0, &start)) != 0) {
395 retval = (errcode_t) (-1 * ret);
401 if (!ctx->journal_name) {
404 uuid_unparse(sb->s_journal_uuid, uuid);
405 ctx->journal_name = blkid_get_devname(ctx->blkid,
407 if (!ctx->journal_name)
408 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
410 journal_name = ctx->journal_name;
413 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
414 retval = EXT2_ET_LOAD_EXT_JOURNAL;
418 jfs_debug(1, "Using journal file %s\n", journal_name);
419 io_ptr = unix_io_manager;
423 test_io_backing_manager = io_ptr;
424 io_ptr = test_io_manager;
430 int flags = IO_FLAG_RW;
431 if (!(ctx->mount_flags & EXT2_MF_ISROOT &&
432 ctx->mount_flags & EXT2_MF_READONLY))
433 flags |= IO_FLAG_EXCLUSIVE;
434 if ((ctx->mount_flags & EXT2_MF_READONLY) &&
435 (ctx->options & E2F_OPT_FORCE))
436 flags &= ~IO_FLAG_EXCLUSIVE;
439 retval = io_ptr->open(journal_name, flags,
445 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
450 start = ext2fs_journal_sb_start(ctx->fs->blocksize) - 1;
451 bh = getblk(dev_journal, start, ctx->fs->blocksize);
453 retval = EXT2_ET_NO_MEMORY;
456 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
457 if ((retval = bh->b_err) != 0) {
461 memcpy(&jsuper, start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET,
463 #ifdef WORDS_BIGENDIAN
464 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
465 ext2fs_swap_super(&jsuper);
467 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
468 !ext2fs_has_feature_journal_dev(&jsuper)) {
469 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
470 retval = EXT2_ET_LOAD_EXT_JOURNAL;
474 /* Make sure the journal UUID is correct */
475 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
476 sizeof(jsuper.s_uuid))) {
477 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
478 retval = EXT2_ET_LOAD_EXT_JOURNAL;
483 /* Check the superblock checksum */
484 if (ext2fs_has_feature_metadata_csum(&jsuper)) {
485 struct struct_ext2_filsys fsx;
486 struct ext2_super_block superx;
489 p = start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET;
490 memcpy(&fsx, ctx->fs, sizeof(fsx));
491 memcpy(&superx, ctx->fs->super, sizeof(superx));
493 ext2fs_set_feature_metadata_csum(fsx.super);
494 if (!ext2fs_superblock_csum_verify(&fsx, p) &&
495 fix_problem(ctx, PR_0_EXT_JOURNAL_SUPER_CSUM_INVALID,
497 ext2fs_superblock_csum_set(&fsx, p);
498 mark_buffer_dirty(bh);
503 maxlen = ext2fs_blocks_count(&jsuper);
504 journal->j_maxlen = (maxlen < 1ULL << 32) ? maxlen : (1ULL << 32) - 1;
508 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
509 retval = EXT2_ET_NO_MEMORY;
513 journal->j_sb_buffer = bh;
514 journal->j_superblock = (journal_superblock_t *)bh->b_data;
518 ext2fs_free_mem(&j_inode);
521 *ret_journal = journal;
522 e2fsck_use_inode_shortcuts(ctx, 0);
526 e2fsck_use_inode_shortcuts(ctx, 0);
528 ext2fs_free_mem(&dev_fs);
530 ext2fs_free_mem(&j_inode);
532 ext2fs_free_mem(&journal);
536 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
537 struct problem_context *pctx)
539 struct ext2_super_block *sb = ctx->fs->super;
540 int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
541 int has_journal = ext2fs_has_feature_journal(ctx->fs->super);
543 if (has_journal || sb->s_journal_inum) {
544 /* The journal inode is bogus, remove and force full fsck */
545 pctx->ino = sb->s_journal_inum;
546 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
547 if (has_journal && sb->s_journal_inum)
548 printf("*** journal has been deleted ***\n\n");
549 ext2fs_clear_feature_journal(sb);
550 sb->s_journal_inum = 0;
551 memset(sb->s_jnl_blocks, 0, sizeof(sb->s_jnl_blocks));
552 ctx->flags |= E2F_FLAG_JOURNAL_INODE;
553 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
554 e2fsck_clear_recover(ctx, 1);
557 return EXT2_ET_CORRUPT_JOURNAL_SB;
558 } else if (recover) {
559 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
560 e2fsck_clear_recover(ctx, 1);
563 return EXT2_ET_UNSUPP_FEATURE;
568 #define V1_SB_SIZE 0x0024
569 static void clear_v2_journal_fields(journal_t *journal)
571 e2fsck_t ctx = journal->j_dev->k_ctx;
572 struct problem_context pctx;
574 clear_problem_context(&pctx);
576 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
579 ctx->flags |= E2F_FLAG_PROBLEMS_FIXED;
580 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
581 ctx->fs->blocksize-V1_SB_SIZE);
582 mark_buffer_dirty(journal->j_sb_buffer);
586 static errcode_t e2fsck_journal_load(journal_t *journal)
588 e2fsck_t ctx = journal->j_dev->k_ctx;
589 journal_superblock_t *jsb;
590 struct buffer_head *jbh = journal->j_sb_buffer;
591 struct problem_context pctx;
593 clear_problem_context(&pctx);
595 ll_rw_block(REQ_OP_READ, 0, 1, &jbh);
597 com_err(ctx->device_name, jbh->b_err, "%s",
598 _("reading journal superblock\n"));
602 jsb = journal->j_superblock;
603 /* If we don't even have JBD2_MAGIC, we probably have a wrong inode */
604 if (jsb->s_header.h_magic != htonl(JBD2_MAGIC_NUMBER))
605 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
607 switch (ntohl(jsb->s_header.h_blocktype)) {
608 case JBD2_SUPERBLOCK_V1:
609 journal->j_format_version = 1;
610 if (jsb->s_feature_compat ||
611 jsb->s_feature_incompat ||
612 jsb->s_feature_ro_compat ||
614 clear_v2_journal_fields(journal);
617 case JBD2_SUPERBLOCK_V2:
618 journal->j_format_version = 2;
619 if (ntohl(jsb->s_nr_users) > 1 &&
620 uuid_is_null(ctx->fs->super->s_journal_uuid))
621 clear_v2_journal_fields(journal);
622 if (ntohl(jsb->s_nr_users) > 1) {
623 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
624 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
629 * These should never appear in a journal super block, so if
630 * they do, the journal is badly corrupted.
632 case JBD2_DESCRIPTOR_BLOCK:
633 case JBD2_COMMIT_BLOCK:
634 case JBD2_REVOKE_BLOCK:
635 return EXT2_ET_CORRUPT_JOURNAL_SB;
637 /* If we don't understand the superblock major type, but there
638 * is a magic number, then it is likely to be a new format we
639 * just don't understand, so leave it alone. */
641 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
644 if (JBD2_HAS_INCOMPAT_FEATURE(journal, ~JBD2_KNOWN_INCOMPAT_FEATURES))
645 return EXT2_ET_UNSUPP_FEATURE;
647 if (JBD2_HAS_RO_COMPAT_FEATURE(journal, ~JBD2_KNOWN_ROCOMPAT_FEATURES))
648 return EXT2_ET_RO_UNSUPP_FEATURE;
650 /* Checksum v1-3 are mutually exclusive features. */
651 if (jbd2_has_feature_csum2(journal) && jbd2_has_feature_csum3(journal))
652 return EXT2_ET_CORRUPT_JOURNAL_SB;
654 if (jbd2_journal_has_csum_v2or3(journal) &&
655 jbd2_has_feature_checksum(journal))
656 return EXT2_ET_CORRUPT_JOURNAL_SB;
658 if (!e2fsck_journal_verify_csum_type(journal, jsb) ||
659 !e2fsck_journal_sb_csum_verify(journal, jsb))
660 return EXT2_ET_CORRUPT_JOURNAL_SB;
662 if (jbd2_journal_has_csum_v2or3(journal))
663 journal->j_csum_seed = jbd2_chksum(journal, ~0, jsb->s_uuid,
664 sizeof(jsb->s_uuid));
666 /* We have now checked whether we know enough about the journal
667 * format to be able to proceed safely, so any other checks that
668 * fail we should attempt to recover from. */
669 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
670 com_err(ctx->program_name, EXT2_ET_CORRUPT_JOURNAL_SB,
671 _("%s: no valid journal superblock found\n"),
673 return EXT2_ET_CORRUPT_JOURNAL_SB;
676 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
677 journal->j_maxlen = ntohl(jsb->s_maxlen);
678 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
679 com_err(ctx->program_name, EXT2_ET_CORRUPT_JOURNAL_SB,
680 _("%s: journal too short\n"),
682 return EXT2_ET_CORRUPT_JOURNAL_SB;
685 journal->j_tail_sequence = ntohl(jsb->s_sequence);
686 journal->j_transaction_sequence = journal->j_tail_sequence;
687 journal->j_tail = ntohl(jsb->s_start);
688 journal->j_first = ntohl(jsb->s_first);
689 journal->j_last = ntohl(jsb->s_maxlen);
694 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
705 /* Leave a valid existing V1 superblock signature alone.
706 * Anything unrecognisable we overwrite with a new V2
709 if (jsb->s_header.h_magic != htonl(JBD2_MAGIC_NUMBER) ||
710 jsb->s_header.h_blocktype != htonl(JBD2_SUPERBLOCK_V1)) {
711 jsb->s_header.h_magic = htonl(JBD2_MAGIC_NUMBER);
712 jsb->s_header.h_blocktype = htonl(JBD2_SUPERBLOCK_V2);
715 /* Zero out everything else beyond the superblock header */
717 p = ((char *) jsb) + sizeof(journal_header_t);
718 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
720 jsb->s_blocksize = htonl(ctx->fs->blocksize);
721 jsb->s_maxlen = htonl(journal->j_maxlen);
722 jsb->s_first = htonl(1);
724 /* Initialize the journal sequence number so that there is "no"
725 * chance we will find old "valid" transactions in the journal.
726 * This avoids the need to zero the whole journal (slow to do,
727 * and risky when we are just recovering the filesystem).
729 uuid_generate(u.uuid);
730 for (i = 0; i < 4; i ++)
732 jsb->s_sequence = htonl(new_seq);
733 e2fsck_journal_sb_csum_set(journal, jsb);
735 mark_buffer_dirty(journal->j_sb_buffer);
736 ll_rw_block(REQ_OP_WRITE, 0, 1, &journal->j_sb_buffer);
739 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
741 struct problem_context *pctx)
743 struct ext2_super_block *sb = ctx->fs->super;
744 int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
746 if (ext2fs_has_feature_journal(sb)) {
747 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
748 e2fsck_journal_reset_super(ctx, journal->j_superblock,
750 journal->j_transaction_sequence = 1;
751 e2fsck_clear_recover(ctx, recover);
754 return EXT2_ET_CORRUPT_JOURNAL_SB;
755 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
756 return EXT2_ET_CORRUPT_JOURNAL_SB;
761 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
764 journal_superblock_t *jsb;
767 mark_buffer_clean(journal->j_sb_buffer);
768 else if (!(ctx->options & E2F_OPT_READONLY)) {
769 jsb = journal->j_superblock;
770 jsb->s_sequence = htonl(journal->j_tail_sequence);
772 jsb->s_start = 0; /* this marks the journal as empty */
773 e2fsck_journal_sb_csum_set(journal, jsb);
774 mark_buffer_dirty(journal->j_sb_buffer);
776 brelse(journal->j_sb_buffer);
778 if (ctx->journal_io) {
779 if (ctx->fs && ctx->fs->io != ctx->journal_io)
780 io_channel_close(ctx->journal_io);
785 if (journal->j_inode)
786 ext2fs_free_mem(&journal->j_inode);
788 if (journal->j_fs_dev)
789 ext2fs_free_mem(&journal->j_fs_dev);
790 ext2fs_free_mem(&journal);
794 * This function makes sure that the superblock fields regarding the
795 * journal are consistent.
797 errcode_t e2fsck_check_ext3_journal(e2fsck_t ctx)
799 struct ext2_super_block *sb = ctx->fs->super;
801 int recover = ext2fs_has_feature_journal_needs_recovery(ctx->fs->super);
802 struct problem_context pctx;
804 int reset = 0, force_fsck = 0;
807 /* If we don't have any journal features, don't do anything more */
808 if (!ext2fs_has_feature_journal(sb) &&
809 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
810 uuid_is_null(sb->s_journal_uuid))
813 clear_problem_context(&pctx);
814 pctx.num = sb->s_journal_inum;
816 retval = e2fsck_get_journal(ctx, &journal);
818 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
819 (retval == EXT2_ET_BAD_BLOCK_NUM) ||
820 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
821 (retval == EXT2_ET_NO_JOURNAL))
822 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
826 retval = e2fsck_journal_load(journal);
828 if ((retval == EXT2_ET_CORRUPT_JOURNAL_SB) ||
829 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
830 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
832 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
833 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
835 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
836 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
837 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
839 e2fsck_journal_release(ctx, journal, 0, 1);
844 * We want to make the flags consistent here. We will not leave with
845 * needs_recovery set but has_journal clear. We can't get in a loop
846 * with -y, -n, or -p, only if a user isn't making up their mind.
849 if (!ext2fs_has_feature_journal(sb)) {
850 recover = ext2fs_has_feature_journal_needs_recovery(sb);
851 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
853 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
856 * Need a full fsck if we are releasing a
857 * journal stored on a reserved inode.
859 force_fsck = recover ||
860 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
861 /* Clear all of the journal fields */
862 sb->s_journal_inum = 0;
863 sb->s_journal_dev = 0;
864 memset(sb->s_journal_uuid, 0,
865 sizeof(sb->s_journal_uuid));
866 e2fsck_clear_recover(ctx, force_fsck);
867 } else if (!(ctx->options & E2F_OPT_READONLY)) {
868 ext2fs_set_feature_journal(sb);
869 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
870 ext2fs_mark_super_dirty(ctx->fs);
874 if (ext2fs_has_feature_journal(sb) &&
875 !ext2fs_has_feature_journal_needs_recovery(sb) &&
876 journal->j_superblock->s_start != 0) {
877 /* Print status information */
878 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
880 problem = PR_0_JOURNAL_RUN_DEFAULT;
882 problem = PR_0_JOURNAL_RUN;
883 if (fix_problem(ctx, problem, &pctx)) {
884 ctx->options |= E2F_OPT_FORCE;
885 ext2fs_set_feature_journal_needs_recovery(sb);
886 ext2fs_mark_super_dirty(ctx->fs);
887 } else if (fix_problem(ctx,
888 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
890 sb->s_state &= ~EXT2_VALID_FS;
891 ext2fs_mark_super_dirty(ctx->fs);
894 * If the user answers no to the above question, we
895 * ignore the fact that journal apparently has data;
896 * accidentally replaying over valid data would be far
897 * worse than skipping a questionable recovery.
899 * XXX should we abort with a fatal error here? What
900 * will the ext3 kernel code do if a filesystem with
901 * !NEEDS_RECOVERY but with a non-zero
902 * journal->j_superblock->s_start is mounted?
907 * If we don't need to do replay the journal, check to see if
908 * the journal's errno is set; if so, we need to mark the file
909 * system as being corrupt and clear the journal's s_errno.
911 if (!ext2fs_has_feature_journal_needs_recovery(sb) &&
912 journal->j_superblock->s_errno) {
913 ctx->fs->super->s_state |= EXT2_ERROR_FS;
914 ext2fs_mark_super_dirty(ctx->fs);
915 journal->j_superblock->s_errno = 0;
916 e2fsck_journal_sb_csum_set(journal, journal->j_superblock);
917 mark_buffer_dirty(journal->j_sb_buffer);
920 e2fsck_journal_release(ctx, journal, reset, 0);
924 static errcode_t recover_ext3_journal(e2fsck_t ctx)
926 struct problem_context pctx;
930 clear_problem_context(&pctx);
932 jbd2_journal_init_revoke_caches();
933 retval = e2fsck_get_journal(ctx, &journal);
937 retval = e2fsck_journal_load(journal);
941 retval = jbd2_journal_init_revoke(journal, 1024);
945 retval = -jbd2_journal_recover(journal);
949 if (journal->j_failed_commit) {
950 pctx.ino = journal->j_failed_commit;
951 fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx);
952 journal->j_superblock->s_errno = -EINVAL;
953 mark_buffer_dirty(journal->j_sb_buffer);
956 journal->j_tail_sequence = journal->j_transaction_sequence;
959 jbd2_journal_destroy_revoke(journal);
960 jbd2_journal_destroy_revoke_caches();
961 e2fsck_journal_release(ctx, journal, 1, 0);
965 errcode_t e2fsck_run_ext3_journal(e2fsck_t ctx)
967 io_manager io_ptr = ctx->fs->io->manager;
968 int blocksize = ctx->fs->blocksize;
969 errcode_t retval, recover_retval;
971 unsigned long long kbytes_written = 0;
973 printf(_("%s: recovering journal\n"), ctx->device_name);
974 if (ctx->options & E2F_OPT_READONLY) {
975 printf(_("%s: won't do journal recovery while read-only\n"),
977 return EXT2_ET_FILE_RO;
980 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
981 ext2fs_flush(ctx->fs); /* Force out any modifications */
983 recover_retval = recover_ext3_journal(ctx);
986 * Reload the filesystem context to get up-to-date data from disk
987 * because journal recovery will change the filesystem under us.
989 if (ctx->fs->super->s_kbytes_written &&
990 ctx->fs->io->manager->get_stats)
991 ctx->fs->io->manager->get_stats(ctx->fs->io, &stats);
992 if (stats && stats->bytes_written)
993 kbytes_written = stats->bytes_written >> 10;
995 ext2fs_mmp_stop(ctx->fs);
996 ext2fs_free(ctx->fs);
997 retval = ext2fs_open(ctx->filesystem_name, ctx->openfs_flags,
998 ctx->superblock, blocksize, io_ptr,
1001 com_err(ctx->program_name, retval,
1002 _("while trying to re-open %s"),
1004 fatal_error(ctx, 0);
1006 ctx->fs->priv_data = ctx;
1007 ctx->fs->now = ctx->now;
1008 ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1009 ctx->fs->super->s_kbytes_written += kbytes_written;
1011 /* Set the superblock flags */
1012 e2fsck_clear_recover(ctx, recover_retval != 0);
1015 * Do one last sanity check, and propagate journal->s_errno to
1016 * the EXT2_ERROR_FS flag in the fs superblock if needed.
1018 retval = e2fsck_check_ext3_journal(ctx);
1019 return retval ? retval : recover_retval;
1023 * This function will move the journal inode from a visible file in
1024 * the filesystem directory hierarchy to the reserved inode if necessary.
1026 static const char * const journal_names[] = {
1027 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
1029 void e2fsck_move_ext3_journal(e2fsck_t ctx)
1031 struct ext2_super_block *sb = ctx->fs->super;
1032 struct problem_context pctx;
1033 struct ext2_inode inode;
1034 ext2_filsys fs = ctx->fs;
1037 const char * const * cpp;
1041 clear_problem_context(&pctx);
1044 * If the filesystem is opened read-only, or there is no
1045 * journal, then do nothing.
1047 if ((ctx->options & E2F_OPT_READONLY) ||
1048 (sb->s_journal_inum == 0) ||
1049 !ext2fs_has_feature_journal(sb))
1053 * Read in the journal inode
1055 if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
1059 * If it's necessary to backup the journal inode, do so.
1061 if ((sb->s_jnl_backup_type == 0) ||
1062 ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
1063 memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
1064 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
1065 memcpy(sb->s_jnl_blocks, inode.i_block,
1067 sb->s_jnl_blocks[15] = inode.i_size_high;
1068 sb->s_jnl_blocks[16] = inode.i_size;
1069 sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
1070 ext2fs_mark_super_dirty(fs);
1071 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1076 * If the journal is already the hidden inode, then do nothing
1078 if (sb->s_journal_inum == EXT2_JOURNAL_INO)
1082 * The journal inode had better have only one link and not be readable.
1084 if (inode.i_links_count != 1)
1088 * If the filesystem is mounted, or we can't tell whether
1089 * or not it's mounted, do nothing.
1091 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
1092 if (retval || (mount_flags & EXT2_MF_MOUNTED))
1096 * If we can't find the name of the journal inode, then do
1099 for (cpp = journal_names; *cpp; cpp++) {
1100 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
1101 strlen(*cpp), 0, &ino);
1102 if ((retval == 0) && (ino == sb->s_journal_inum))
1108 /* We need the inode bitmap to be loaded */
1109 retval = ext2fs_read_bitmaps(fs);
1114 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
1118 * OK, we've done all the checks, let's actually move the
1119 * journal inode. Errors at this point mean we need to force
1120 * an ext2 filesystem check.
1122 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
1124 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
1126 sb->s_journal_inum = EXT2_JOURNAL_INO;
1127 ext2fs_mark_super_dirty(fs);
1128 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1129 inode.i_links_count = 0;
1130 inode.i_dtime = ctx->now;
1131 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
1134 group = ext2fs_group_of_ino(fs, ino);
1135 ext2fs_unmark_inode_bitmap2(fs->inode_map, ino);
1136 ext2fs_mark_ib_dirty(fs);
1137 ext2fs_bg_free_inodes_count_set(fs, group, ext2fs_bg_free_inodes_count(fs, group) + 1);
1138 ext2fs_group_desc_csum_set(fs, group);
1139 fs->super->s_free_inodes_count++;
1143 pctx.errcode = retval;
1144 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
1145 fs->super->s_state &= ~EXT2_VALID_FS;
1146 ext2fs_mark_super_dirty(fs);
1151 * This function makes sure the superblock hint for the external
1152 * journal is correct.
1154 int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
1156 struct ext2_super_block *sb = ctx->fs->super;
1157 struct problem_context pctx;
1158 char uuid[37], *journal_name;
1161 if (!ext2fs_has_feature_journal(sb) ||
1162 uuid_is_null(sb->s_journal_uuid))
1165 uuid_unparse(sb->s_journal_uuid, uuid);
1166 journal_name = blkid_get_devname(ctx->blkid, "UUID", uuid);
1170 if (stat(journal_name, &st) < 0) {
1175 if (st.st_rdev != sb->s_journal_dev) {
1176 clear_problem_context(&pctx);
1177 pctx.num = st.st_rdev;
1178 if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) {
1179 sb->s_journal_dev = st.st_rdev;
1180 ext2fs_mark_super_dirty(ctx->fs);