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 < 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, IO_FLAG_RW,
376 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
379 if (ctx->fs->blocksize == 1024)
381 bh = getblk(dev_journal, start, ctx->fs->blocksize);
383 retval = EXT2_ET_NO_MEMORY;
386 ll_rw_block(READ, 1, &bh);
387 if ((retval = bh->b_err) != 0) {
391 memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024,
394 #ifdef WORDS_BIGENDIAN
395 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
396 ext2fs_swap_super(&jsuper);
398 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
399 !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
400 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
401 retval = EXT2_ET_LOAD_EXT_JOURNAL;
404 /* Make sure the journal UUID is correct */
405 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
406 sizeof(jsuper.s_uuid))) {
407 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
408 retval = EXT2_ET_LOAD_EXT_JOURNAL;
412 journal->j_maxlen = jsuper.s_blocks_count;
416 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
417 retval = EXT2_ET_NO_MEMORY;
421 journal->j_sb_buffer = bh;
422 journal->j_superblock = (journal_superblock_t *)bh->b_data;
426 ext2fs_free_mem(&j_inode);
429 *ret_journal = journal;
430 e2fsck_use_inode_shortcuts(ctx, 0);
434 e2fsck_use_inode_shortcuts(ctx, 0);
436 ext2fs_free_mem(&dev_fs);
438 ext2fs_free_mem(&j_inode);
440 ext2fs_free_mem(&journal);
444 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
445 struct problem_context *pctx)
447 struct ext2_super_block *sb = ctx->fs->super;
448 int recover = ctx->fs->super->s_feature_incompat &
449 EXT3_FEATURE_INCOMPAT_RECOVER;
450 int has_journal = ctx->fs->super->s_feature_compat &
451 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
453 if (has_journal || sb->s_journal_inum) {
454 /* The journal inode is bogus, remove and force full fsck */
455 pctx->ino = sb->s_journal_inum;
456 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
457 if (has_journal && sb->s_journal_inum)
458 printf("*** ext3 journal has been deleted - "
459 "filesystem is now ext2 only ***\n\n");
460 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
461 sb->s_journal_inum = 0;
462 ctx->flags |= E2F_FLAG_JOURNAL_INODE;
463 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
464 e2fsck_clear_recover(ctx, 1);
467 return EXT2_ET_BAD_INODE_NUM;
468 } else if (recover) {
469 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
470 e2fsck_clear_recover(ctx, 1);
473 return EXT2_ET_UNSUPP_FEATURE;
478 #define V1_SB_SIZE 0x0024
479 static void clear_v2_journal_fields(journal_t *journal)
481 e2fsck_t ctx = journal->j_dev->k_ctx;
482 struct problem_context pctx;
484 clear_problem_context(&pctx);
486 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
489 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
490 ctx->fs->blocksize-V1_SB_SIZE);
491 mark_buffer_dirty(journal->j_sb_buffer);
495 static errcode_t e2fsck_journal_load(journal_t *journal)
497 e2fsck_t ctx = journal->j_dev->k_ctx;
498 journal_superblock_t *jsb;
499 struct buffer_head *jbh = journal->j_sb_buffer;
500 struct problem_context pctx;
502 clear_problem_context(&pctx);
504 ll_rw_block(READ, 1, &jbh);
506 com_err(ctx->device_name, jbh->b_err,
507 _("reading journal superblock\n"));
511 jsb = journal->j_superblock;
512 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
513 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
514 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
516 switch (ntohl(jsb->s_header.h_blocktype)) {
517 case JFS_SUPERBLOCK_V1:
518 journal->j_format_version = 1;
519 if (jsb->s_feature_compat ||
520 jsb->s_feature_incompat ||
521 jsb->s_feature_ro_compat ||
523 clear_v2_journal_fields(journal);
526 case JFS_SUPERBLOCK_V2:
527 journal->j_format_version = 2;
528 if (ntohl(jsb->s_nr_users) > 1 &&
529 uuid_is_null(ctx->fs->super->s_journal_uuid))
530 clear_v2_journal_fields(journal);
531 if (ntohl(jsb->s_nr_users) > 1) {
532 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
533 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
538 * These should never appear in a journal super block, so if
539 * they do, the journal is badly corrupted.
541 case JFS_DESCRIPTOR_BLOCK:
542 case JFS_COMMIT_BLOCK:
543 case JFS_REVOKE_BLOCK:
544 return EXT2_ET_CORRUPT_SUPERBLOCK;
546 /* If we don't understand the superblock major type, but there
547 * is a magic number, then it is likely to be a new format we
548 * just don't understand, so leave it alone. */
550 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
553 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
554 return EXT2_ET_UNSUPP_FEATURE;
556 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
557 return EXT2_ET_RO_UNSUPP_FEATURE;
559 /* We have now checked whether we know enough about the journal
560 * format to be able to proceed safely, so any other checks that
561 * fail we should attempt to recover from. */
562 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
563 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
564 _("%s: no valid journal superblock found\n"),
566 return EXT2_ET_CORRUPT_SUPERBLOCK;
569 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
570 journal->j_maxlen = ntohl(jsb->s_maxlen);
571 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
572 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
573 _("%s: journal too short\n"),
575 return EXT2_ET_CORRUPT_SUPERBLOCK;
578 journal->j_tail_sequence = ntohl(jsb->s_sequence);
579 journal->j_transaction_sequence = journal->j_tail_sequence;
580 journal->j_tail = ntohl(jsb->s_start);
581 journal->j_first = ntohl(jsb->s_first);
582 journal->j_last = ntohl(jsb->s_maxlen);
587 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
598 /* Leave a valid existing V1 superblock signature alone.
599 * Anything unrecognisable we overwrite with a new V2
602 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
603 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
604 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
605 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
608 /* Zero out everything else beyond the superblock header */
610 p = ((char *) jsb) + sizeof(journal_header_t);
611 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
613 jsb->s_blocksize = htonl(ctx->fs->blocksize);
614 jsb->s_maxlen = htonl(journal->j_maxlen);
615 jsb->s_first = htonl(1);
617 /* Initialize the journal sequence number so that there is "no"
618 * chance we will find old "valid" transactions in the journal.
619 * This avoids the need to zero the whole journal (slow to do,
620 * and risky when we are just recovering the filesystem).
622 uuid_generate(u.uuid);
623 for (i = 0; i < 4; i ++)
625 jsb->s_sequence = htonl(new_seq);
627 mark_buffer_dirty(journal->j_sb_buffer);
628 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
631 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
633 struct problem_context *pctx)
635 struct ext2_super_block *sb = ctx->fs->super;
636 int recover = ctx->fs->super->s_feature_incompat &
637 EXT3_FEATURE_INCOMPAT_RECOVER;
639 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
640 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
641 e2fsck_journal_reset_super(ctx, journal->j_superblock,
643 journal->j_transaction_sequence = 1;
644 e2fsck_clear_recover(ctx, recover);
647 return EXT2_ET_CORRUPT_SUPERBLOCK;
648 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
649 return EXT2_ET_CORRUPT_SUPERBLOCK;
654 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
657 journal_superblock_t *jsb;
660 mark_buffer_clean(journal->j_sb_buffer);
661 else if (!(ctx->options & E2F_OPT_READONLY)) {
662 jsb = journal->j_superblock;
663 jsb->s_sequence = htonl(journal->j_transaction_sequence);
665 jsb->s_start = 0; /* this marks the journal as empty */
666 mark_buffer_dirty(journal->j_sb_buffer);
668 brelse(journal->j_sb_buffer);
670 if (ctx->journal_io) {
671 if (ctx->fs && ctx->fs->io != ctx->journal_io)
672 io_channel_close(ctx->journal_io);
677 if (journal->j_inode)
678 ext2fs_free_mem(&journal->j_inode);
680 if (journal->j_fs_dev)
681 ext2fs_free_mem(&journal->j_fs_dev);
682 ext2fs_free_mem(&journal);
686 * This function makes sure that the superblock fields regarding the
687 * journal are consistent.
689 int e2fsck_check_ext3_journal(e2fsck_t ctx)
691 struct ext2_super_block *sb = ctx->fs->super;
693 int recover = ctx->fs->super->s_feature_incompat &
694 EXT3_FEATURE_INCOMPAT_RECOVER;
695 struct problem_context pctx;
697 int reset = 0, force_fsck = 0;
700 /* If we don't have any journal features, don't do anything more */
701 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
702 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
703 uuid_is_null(sb->s_journal_uuid))
706 clear_problem_context(&pctx);
707 pctx.num = sb->s_journal_inum;
709 retval = e2fsck_get_journal(ctx, &journal);
711 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
712 (retval == EXT2_ET_BAD_BLOCK_NUM) ||
713 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
714 (retval == EXT2_ET_NO_JOURNAL))
715 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
719 retval = e2fsck_journal_load(journal);
721 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
722 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
723 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
725 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
726 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
728 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
729 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
730 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
732 e2fsck_journal_release(ctx, journal, 0, 1);
737 * We want to make the flags consistent here. We will not leave with
738 * needs_recovery set but has_journal clear. We can't get in a loop
739 * with -y, -n, or -p, only if a user isn't making up their mind.
742 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
743 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
745 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
747 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
750 * Need a full fsck if we are releasing a
751 * journal stored on a reserved inode.
753 force_fsck = recover ||
754 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
755 /* Clear all of the journal fields */
756 sb->s_journal_inum = 0;
757 sb->s_journal_dev = 0;
758 memset(sb->s_journal_uuid, 0,
759 sizeof(sb->s_journal_uuid));
760 e2fsck_clear_recover(ctx, force_fsck);
761 } else if (!(ctx->options & E2F_OPT_READONLY)) {
762 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
763 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
764 ext2fs_mark_super_dirty(ctx->fs);
768 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
769 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
770 journal->j_superblock->s_start != 0) {
771 /* Print status information */
772 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
774 problem = PR_0_JOURNAL_RUN_DEFAULT;
776 problem = PR_0_JOURNAL_RUN;
777 if (fix_problem(ctx, problem, &pctx)) {
778 ctx->options |= E2F_OPT_FORCE;
779 sb->s_feature_incompat |=
780 EXT3_FEATURE_INCOMPAT_RECOVER;
781 ext2fs_mark_super_dirty(ctx->fs);
782 } else if (fix_problem(ctx,
783 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
785 sb->s_state &= ~EXT2_VALID_FS;
786 ext2fs_mark_super_dirty(ctx->fs);
789 * If the user answers no to the above question, we
790 * ignore the fact that journal apparently has data;
791 * accidentally replaying over valid data would be far
792 * worse than skipping a questionable recovery.
794 * XXX should we abort with a fatal error here? What
795 * will the ext3 kernel code do if a filesystem with
796 * !NEEDS_RECOVERY but with a non-zero
797 * journal->j_superblock->s_start is mounted?
801 e2fsck_journal_release(ctx, journal, reset, 0);
805 static errcode_t recover_ext3_journal(e2fsck_t ctx)
807 struct problem_context pctx;
811 clear_problem_context(&pctx);
813 journal_init_revoke_caches();
814 retval = e2fsck_get_journal(ctx, &journal);
818 retval = e2fsck_journal_load(journal);
822 retval = journal_init_revoke(journal, 1024);
826 retval = -journal_recover(journal);
830 if (journal->j_failed_commit) {
831 pctx.ino = journal->j_failed_commit;
832 fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx);
833 ctx->fs->super->s_state |= EXT2_ERROR_FS;
834 ext2fs_mark_super_dirty(ctx->fs);
838 if (journal->j_superblock->s_errno) {
839 ctx->fs->super->s_state |= EXT2_ERROR_FS;
840 ext2fs_mark_super_dirty(ctx->fs);
841 journal->j_superblock->s_errno = 0;
842 mark_buffer_dirty(journal->j_sb_buffer);
846 journal_destroy_revoke(journal);
847 journal_destroy_revoke_caches();
848 e2fsck_journal_release(ctx, journal, 1, 0);
852 int e2fsck_run_ext3_journal(e2fsck_t ctx)
854 io_manager io_ptr = ctx->fs->io->manager;
855 int blocksize = ctx->fs->blocksize;
856 errcode_t retval, recover_retval;
858 unsigned long long kbytes_written = 0;
860 printf(_("%s: recovering journal\n"), ctx->device_name);
861 if (ctx->options & E2F_OPT_READONLY) {
862 printf(_("%s: won't do journal recovery while read-only\n"),
864 return EXT2_ET_FILE_RO;
867 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
868 ext2fs_flush(ctx->fs); /* Force out any modifications */
870 recover_retval = recover_ext3_journal(ctx);
873 * Reload the filesystem context to get up-to-date data from disk
874 * because journal recovery will change the filesystem under us.
876 if (ctx->fs->super->s_kbytes_written &&
877 ctx->fs->io->manager->get_stats)
878 ctx->fs->io->manager->get_stats(ctx->fs->io, &stats);
879 if (stats && stats->bytes_written)
880 kbytes_written = stats->bytes_written >> 10;
881 ext2fs_free(ctx->fs);
882 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
883 ctx->superblock, blocksize, io_ptr,
886 com_err(ctx->program_name, retval,
887 _("while trying to re-open %s"),
891 ctx->fs->priv_data = ctx;
892 ctx->fs->now = ctx->now;
893 ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
894 ctx->fs->super->s_kbytes_written += kbytes_written;
896 /* Set the superblock flags */
897 e2fsck_clear_recover(ctx, recover_retval);
898 return recover_retval;
902 * This function will move the journal inode from a visible file in
903 * the filesystem directory hierarchy to the reserved inode if necessary.
905 static const char * const journal_names[] = {
906 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
908 void e2fsck_move_ext3_journal(e2fsck_t ctx)
910 struct ext2_super_block *sb = ctx->fs->super;
911 struct problem_context pctx;
912 struct ext2_inode inode;
913 ext2_filsys fs = ctx->fs;
916 const char * const * cpp;
917 int group, mount_flags;
919 clear_problem_context(&pctx);
922 * If the filesystem is opened read-only, or there is no
923 * journal, then do nothing.
925 if ((ctx->options & E2F_OPT_READONLY) ||
926 (sb->s_journal_inum == 0) ||
927 !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
931 * Read in the journal inode
933 if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
937 * If it's necessary to backup the journal inode, do so.
939 if ((sb->s_jnl_backup_type == 0) ||
940 ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
941 memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
942 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
943 memcpy(sb->s_jnl_blocks, inode.i_block,
945 sb->s_jnl_blocks[16] = inode.i_size;
946 sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
947 ext2fs_mark_super_dirty(fs);
948 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
953 * If the journal is already the hidden inode, then do nothing
955 if (sb->s_journal_inum == EXT2_JOURNAL_INO)
959 * The journal inode had better have only one link and not be readable.
961 if (inode.i_links_count != 1)
965 * If the filesystem is mounted, or we can't tell whether
966 * or not it's mounted, do nothing.
968 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
969 if (retval || (mount_flags & EXT2_MF_MOUNTED))
973 * If we can't find the name of the journal inode, then do
976 for (cpp = journal_names; *cpp; cpp++) {
977 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
978 strlen(*cpp), 0, &ino);
979 if ((retval == 0) && (ino == sb->s_journal_inum))
985 /* We need the inode bitmap to be loaded */
986 retval = ext2fs_read_bitmaps(fs);
991 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
995 * OK, we've done all the checks, let's actually move the
996 * journal inode. Errors at this point mean we need to force
997 * an ext2 filesystem check.
999 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
1001 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
1003 sb->s_journal_inum = EXT2_JOURNAL_INO;
1004 ext2fs_mark_super_dirty(fs);
1005 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1006 inode.i_links_count = 0;
1007 inode.i_dtime = ctx->now;
1008 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
1011 group = ext2fs_group_of_ino(fs, ino);
1012 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
1013 ext2fs_mark_ib_dirty(fs);
1014 fs->group_desc[group].bg_free_inodes_count++;
1015 ext2fs_group_desc_csum_set(fs, group);
1016 fs->super->s_free_inodes_count++;
1020 pctx.errcode = retval;
1021 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
1022 fs->super->s_state &= ~EXT2_VALID_FS;
1023 ext2fs_mark_super_dirty(fs);
1028 * This function makes sure the superblock hint for the external
1029 * journal is correct.
1031 int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
1033 struct ext2_super_block *sb = ctx->fs->super;
1034 struct problem_context pctx;
1035 char uuid[37], *journal_name;
1038 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
1039 uuid_is_null(sb->s_journal_uuid))
1042 uuid_unparse(sb->s_journal_uuid, uuid);
1043 journal_name = blkid_get_devname(ctx->blkid, "UUID", uuid);
1047 if (stat(journal_name, &st) < 0)
1050 if (st.st_rdev != sb->s_journal_dev) {
1051 clear_problem_context(&pctx);
1052 pctx.num = st.st_rdev;
1053 if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) {
1054 sb->s_journal_dev = st.st_rdev;
1055 ext2fs_mark_super_dirty(ctx->fs);