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/mount.h>
17 #define MNT_FL (MS_MGC_VAL | MS_RDONLY)
19 #ifdef HAVE_SYS_STAT_H
23 #define E2FSCK_INCLUDE_INLINE_FUNCS
26 #include "uuid/uuid.h"
28 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
29 static int bh_count = 0;
33 * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths.
34 * This creates a larger static binary, and a smaller binary using
35 * shared libraries. It's also probably slightly less CPU-efficient,
36 * which is why it's not on by default. But, it's a good way of
37 * testing the functions in inode_io.c and fileio.c.
41 /* Kernel compatibility functions for handling the journal. These allow us
42 * to use the recovery.c file virtually unchanged from the kernel, so we
43 * don't have to do much to keep kernel and user recovery in sync.
45 int journal_bmap(journal_t *journal, blk_t block, unsigned long *phys)
51 struct inode *inode = journal->j_inode;
60 retval= ext2fs_bmap(inode->i_ctx->fs, inode->i_ino,
61 &inode->i_ext2, NULL, 0, block, &pblk);
67 struct buffer_head *getblk(kdev_t kdev, blk_t blocknr, int blocksize)
69 struct buffer_head *bh;
71 bh = e2fsck_allocate_memory(kdev->k_ctx, sizeof(*bh), "block buffer");
75 jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
76 (unsigned long) blocknr, blocksize, ++bh_count);
78 bh->b_ctx = kdev->k_ctx;
79 if (kdev->k_dev == K_DEV_FS)
80 bh->b_io = kdev->k_ctx->fs->io;
82 bh->b_io = kdev->k_ctx->journal_io;
83 bh->b_size = blocksize;
84 bh->b_blocknr = blocknr;
89 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
92 struct buffer_head *bh;
94 for (; nr > 0; --nr) {
96 if (rw == READ && !bh->b_uptodate) {
97 jfs_debug(3, "reading block %lu/%p\n",
98 (unsigned long) bh->b_blocknr, (void *) bh);
99 retval = io_channel_read_blk(bh->b_io,
103 com_err(bh->b_ctx->device_name, retval,
104 "while reading block %lu\n",
105 (unsigned long) bh->b_blocknr);
110 } else if (rw == WRITE && bh->b_dirty) {
111 jfs_debug(3, "writing block %lu/%p\n",
112 (unsigned long) bh->b_blocknr, (void *) bh);
113 retval = io_channel_write_blk(bh->b_io,
117 com_err(bh->b_ctx->device_name, retval,
118 "while writing block %lu\n",
119 (unsigned long) bh->b_blocknr);
126 jfs_debug(3, "no-op %s for block %lu\n",
127 rw == READ ? "read" : "write",
128 (unsigned long) bh->b_blocknr);
132 void mark_buffer_dirty(struct buffer_head *bh)
137 static void mark_buffer_clean(struct buffer_head * bh)
142 void brelse(struct buffer_head *bh)
145 ll_rw_block(WRITE, 1, &bh);
146 jfs_debug(3, "freeing block %lu/%p (total %d)\n",
147 (unsigned long) bh->b_blocknr, (void *) bh, --bh_count);
148 ext2fs_free_mem(&bh);
151 int buffer_uptodate(struct buffer_head *bh)
153 return bh->b_uptodate;
156 void mark_buffer_uptodate(struct buffer_head *bh, int val)
158 bh->b_uptodate = val;
161 void wait_on_buffer(struct buffer_head *bh)
164 ll_rw_block(READ, 1, &bh);
168 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
170 ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
172 /* if we had an error doing journal recovery, we need a full fsck */
174 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
175 ext2fs_mark_super_dirty(ctx->fs);
178 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
180 struct ext2_super_block *sb = ctx->fs->super;
181 struct ext2_super_block jsuper;
182 struct problem_context pctx;
183 struct buffer_head *bh;
184 struct inode *j_inode = NULL;
185 struct kdev_s *dev_fs = NULL, *dev_journal;
186 const char *journal_name = 0;
187 journal_t *journal = NULL;
188 errcode_t retval = 0;
189 io_manager io_ptr = 0;
190 unsigned long start = 0;
193 int tried_backup_jnl = 0;
196 clear_problem_context(&pctx);
198 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
200 return EXT2_ET_NO_MEMORY;
203 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
205 retval = EXT2_ET_NO_MEMORY;
208 dev_journal = dev_fs+1;
210 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
211 dev_fs->k_dev = K_DEV_FS;
212 dev_journal->k_dev = K_DEV_JOURNAL;
214 journal->j_dev = dev_journal;
215 journal->j_fs_dev = dev_fs;
216 journal->j_inode = NULL;
217 journal->j_blocksize = ctx->fs->blocksize;
219 if (uuid_is_null(sb->s_journal_uuid)) {
220 if (!sb->s_journal_inum)
221 return EXT2_ET_BAD_INODE_NUM;
222 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
225 retval = EXT2_ET_NO_MEMORY;
229 j_inode->i_ctx = ctx;
230 j_inode->i_ino = sb->s_journal_inum;
232 if ((retval = ext2fs_read_inode(ctx->fs,
234 &j_inode->i_ext2))) {
236 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
239 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
240 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
242 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
243 j_inode->i_ext2.i_links_count = 1;
244 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
247 if (!j_inode->i_ext2.i_links_count ||
248 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
249 retval = EXT2_ET_NO_JOURNAL;
250 goto try_backup_journal;
252 if (j_inode->i_ext2.i_size / journal->j_blocksize <
253 JFS_MIN_JOURNAL_BLOCKS) {
254 retval = EXT2_ET_JOURNAL_TOO_SMALL;
255 goto try_backup_journal;
257 for (i=0; i < EXT2_N_BLOCKS; i++) {
258 blk = j_inode->i_ext2.i_block[i];
260 if (i < EXT2_NDIR_BLOCKS) {
261 retval = EXT2_ET_JOURNAL_TOO_SMALL;
262 goto try_backup_journal;
266 if (blk < sb->s_first_data_block ||
267 blk >= sb->s_blocks_count) {
268 retval = EXT2_ET_BAD_BLOCK_NUM;
269 goto try_backup_journal;
272 journal->j_maxlen = j_inode->i_ext2.i_size / journal->j_blocksize;
275 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
281 io_ptr = inode_io_manager;
283 journal->j_inode = j_inode;
284 ctx->journal_io = ctx->fs->io;
285 if ((retval = journal_bmap(journal, 0, &start)) != 0)
290 if (!ctx->journal_name) {
293 uuid_unparse(sb->s_journal_uuid, uuid);
294 ctx->journal_name = blkid_get_devname(ctx->blkid,
296 if (!ctx->journal_name)
297 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
299 journal_name = ctx->journal_name;
302 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
303 return EXT2_ET_LOAD_EXT_JOURNAL;
306 jfs_debug(1, "Using journal file %s\n", journal_name);
307 io_ptr = unix_io_manager;
311 test_io_backing_manager = io_ptr;
312 io_ptr = test_io_manager;
317 retval = io_ptr->open(journal_name, IO_FLAG_RW,
322 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
325 if (ctx->fs->blocksize == 1024)
327 bh = getblk(dev_journal, start, ctx->fs->blocksize);
329 retval = EXT2_ET_NO_MEMORY;
332 ll_rw_block(READ, 1, &bh);
333 if ((retval = bh->b_err) != 0)
335 memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024,
338 #ifdef EXT2FS_ENABLE_SWAPFS
339 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
340 ext2fs_swap_super(&jsuper);
342 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
343 !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
344 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
345 retval = EXT2_ET_LOAD_EXT_JOURNAL;
348 /* Make sure the journal UUID is correct */
349 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
350 sizeof(jsuper.s_uuid))) {
351 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
352 retval = EXT2_ET_LOAD_EXT_JOURNAL;
356 journal->j_maxlen = jsuper.s_blocks_count;
360 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
361 retval = EXT2_ET_NO_MEMORY;
365 journal->j_sb_buffer = bh;
366 journal->j_superblock = (journal_superblock_t *)bh->b_data;
370 ext2fs_free_mem(&j_inode);
373 *ret_journal = journal;
378 ext2fs_free_mem(&dev_fs);
380 ext2fs_free_mem(&j_inode);
382 ext2fs_free_mem(&journal);
387 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
388 struct problem_context *pctx)
390 struct ext2_super_block *sb = ctx->fs->super;
391 int recover = ctx->fs->super->s_feature_incompat &
392 EXT3_FEATURE_INCOMPAT_RECOVER;
393 int has_journal = ctx->fs->super->s_feature_compat &
394 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
396 if (has_journal || sb->s_journal_inum) {
397 /* The journal inode is bogus, remove and force full fsck */
398 pctx->ino = sb->s_journal_inum;
399 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
400 if (has_journal && sb->s_journal_inum)
401 printf("*** ext3 journal has been deleted - "
402 "filesystem is now ext2 only ***\n\n");
403 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
404 sb->s_journal_inum = 0;
405 ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */
406 e2fsck_clear_recover(ctx, 1);
409 return EXT2_ET_BAD_INODE_NUM;
410 } else if (recover) {
411 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
412 e2fsck_clear_recover(ctx, 1);
415 return EXT2_ET_UNSUPP_FEATURE;
420 #define V1_SB_SIZE 0x0024
421 static void clear_v2_journal_fields(journal_t *journal)
423 e2fsck_t ctx = journal->j_dev->k_ctx;
424 struct problem_context pctx;
426 clear_problem_context(&pctx);
428 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
431 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
432 ctx->fs->blocksize-V1_SB_SIZE);
433 mark_buffer_dirty(journal->j_sb_buffer);
437 static errcode_t e2fsck_journal_load(journal_t *journal)
439 e2fsck_t ctx = journal->j_dev->k_ctx;
440 journal_superblock_t *jsb;
441 struct buffer_head *jbh = journal->j_sb_buffer;
442 struct problem_context pctx;
444 clear_problem_context(&pctx);
446 ll_rw_block(READ, 1, &jbh);
448 com_err(ctx->device_name, jbh->b_err,
449 _("reading journal superblock\n"));
453 jsb = journal->j_superblock;
454 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
455 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
456 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
458 switch (ntohl(jsb->s_header.h_blocktype)) {
459 case JFS_SUPERBLOCK_V1:
460 journal->j_format_version = 1;
461 if (jsb->s_feature_compat ||
462 jsb->s_feature_incompat ||
463 jsb->s_feature_ro_compat ||
465 clear_v2_journal_fields(journal);
468 case JFS_SUPERBLOCK_V2:
469 journal->j_format_version = 2;
470 if (ntohl(jsb->s_nr_users) > 1 &&
471 uuid_is_null(ctx->fs->super->s_journal_uuid))
472 clear_v2_journal_fields(journal);
473 if (ntohl(jsb->s_nr_users) > 1) {
474 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
475 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
480 * These should never appear in a journal super block, so if
481 * they do, the journal is badly corrupted.
483 case JFS_DESCRIPTOR_BLOCK:
484 case JFS_COMMIT_BLOCK:
485 case JFS_REVOKE_BLOCK:
486 return EXT2_ET_CORRUPT_SUPERBLOCK;
488 /* If we don't understand the superblock major type, but there
489 * is a magic number, then it is likely to be a new format we
490 * just don't understand, so leave it alone. */
492 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
495 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
496 return EXT2_ET_UNSUPP_FEATURE;
498 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
499 return EXT2_ET_RO_UNSUPP_FEATURE;
501 /* We have now checked whether we know enough about the journal
502 * format to be able to proceed safely, so any other checks that
503 * fail we should attempt to recover from. */
504 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
505 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
506 _("%s: no valid journal superblock found\n"),
508 return EXT2_ET_CORRUPT_SUPERBLOCK;
511 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
512 journal->j_maxlen = ntohl(jsb->s_maxlen);
513 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
514 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
515 _("%s: journal too short\n"),
517 return EXT2_ET_CORRUPT_SUPERBLOCK;
520 journal->j_tail_sequence = ntohl(jsb->s_sequence);
521 journal->j_transaction_sequence = journal->j_tail_sequence;
522 journal->j_tail = ntohl(jsb->s_start);
523 journal->j_first = ntohl(jsb->s_first);
524 journal->j_last = ntohl(jsb->s_maxlen);
529 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
540 /* Leave a valid existing V1 superblock signature alone.
541 * Anything unrecognisable we overwrite with a new V2
544 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
545 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
546 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
547 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
550 /* Zero out everything else beyond the superblock header */
552 p = ((char *) jsb) + sizeof(journal_header_t);
553 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
555 jsb->s_blocksize = htonl(ctx->fs->blocksize);
556 jsb->s_maxlen = htonl(journal->j_maxlen);
557 jsb->s_first = htonl(1);
559 /* Initialize the journal sequence number so that there is "no"
560 * chance we will find old "valid" transactions in the journal.
561 * This avoids the need to zero the whole journal (slow to do,
562 * and risky when we are just recovering the filesystem).
564 uuid_generate(u.uuid);
565 for (i = 0; i < 4; i ++)
567 jsb->s_sequence = htonl(new_seq);
569 mark_buffer_dirty(journal->j_sb_buffer);
570 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
573 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
575 struct problem_context *pctx)
577 struct ext2_super_block *sb = ctx->fs->super;
578 int recover = ctx->fs->super->s_feature_incompat &
579 EXT3_FEATURE_INCOMPAT_RECOVER;
581 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
582 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
583 e2fsck_journal_reset_super(ctx, journal->j_superblock,
585 journal->j_transaction_sequence = 1;
586 e2fsck_clear_recover(ctx, recover);
589 return EXT2_ET_CORRUPT_SUPERBLOCK;
590 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
591 return EXT2_ET_CORRUPT_SUPERBLOCK;
596 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
599 journal_superblock_t *jsb;
602 mark_buffer_clean(journal->j_sb_buffer);
603 else if (!(ctx->options & E2F_OPT_READONLY)) {
604 jsb = journal->j_superblock;
605 jsb->s_sequence = htonl(journal->j_transaction_sequence);
607 jsb->s_start = 0; /* this marks the journal as empty */
608 mark_buffer_dirty(journal->j_sb_buffer);
610 brelse(journal->j_sb_buffer);
612 if (ctx->journal_io) {
613 if (ctx->fs && ctx->fs->io != ctx->journal_io)
614 io_channel_close(ctx->journal_io);
619 if (journal->j_inode)
620 ext2fs_free_mem(&journal->j_inode);
622 if (journal->j_fs_dev)
623 ext2fs_free_mem(&journal->j_fs_dev);
624 ext2fs_free_mem(&journal);
628 * This function makes sure that the superblock fields regarding the
629 * journal are consistent.
631 int e2fsck_check_ext3_journal(e2fsck_t ctx)
633 struct ext2_super_block *sb = ctx->fs->super;
635 int recover = ctx->fs->super->s_feature_incompat &
636 EXT3_FEATURE_INCOMPAT_RECOVER;
637 struct problem_context pctx;
639 int reset = 0, force_fsck = 0;
642 /* If we don't have any journal features, don't do anything more */
643 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
644 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
645 uuid_is_null(sb->s_journal_uuid))
648 clear_problem_context(&pctx);
649 pctx.num = sb->s_journal_inum;
651 retval = e2fsck_get_journal(ctx, &journal);
653 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
654 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
655 (retval == EXT2_ET_NO_JOURNAL))
656 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
660 retval = e2fsck_journal_load(journal);
662 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
663 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
664 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
666 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
667 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
669 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
670 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
671 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
673 e2fsck_journal_release(ctx, journal, 0, 1);
678 * We want to make the flags consistent here. We will not leave with
679 * needs_recovery set but has_journal clear. We can't get in a loop
680 * with -y, -n, or -p, only if a user isn't making up their mind.
683 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
684 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
686 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
688 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
691 * Need a full fsck if we are releasing a
692 * journal stored on a reserved inode.
694 force_fsck = recover ||
695 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
696 /* Clear all of the journal fields */
697 sb->s_journal_inum = 0;
698 sb->s_journal_dev = 0;
699 memset(sb->s_journal_uuid, 0,
700 sizeof(sb->s_journal_uuid));
701 e2fsck_clear_recover(ctx, force_fsck);
702 } else if (!(ctx->options & E2F_OPT_READONLY)) {
703 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
704 ext2fs_mark_super_dirty(ctx->fs);
708 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
709 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
710 journal->j_superblock->s_start != 0) {
711 /* Print status information */
712 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
714 problem = PR_0_JOURNAL_RUN_DEFAULT;
716 problem = PR_0_JOURNAL_RUN;
717 if (fix_problem(ctx, problem, &pctx)) {
718 ctx->options |= E2F_OPT_FORCE;
719 sb->s_feature_incompat |=
720 EXT3_FEATURE_INCOMPAT_RECOVER;
721 ext2fs_mark_super_dirty(ctx->fs);
722 } else if (fix_problem(ctx,
723 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
725 sb->s_state &= ~EXT2_VALID_FS;
726 ext2fs_mark_super_dirty(ctx->fs);
729 * If the user answers no to the above question, we
730 * ignore the fact that journal apparently has data;
731 * accidentally replaying over valid data would be far
732 * worse than skipping a questionable recovery.
734 * XXX should we abort with a fatal error here? What
735 * will the ext3 kernel code do if a filesystem with
736 * !NEEDS_RECOVERY but with a non-zero
737 * journal->j_superblock->s_start is mounted?
741 e2fsck_journal_release(ctx, journal, reset, 0);
745 static errcode_t recover_ext3_journal(e2fsck_t ctx)
750 journal_init_revoke_caches();
751 retval = e2fsck_get_journal(ctx, &journal);
755 retval = e2fsck_journal_load(journal);
759 retval = journal_init_revoke(journal, 1024);
763 retval = -journal_recover(journal);
767 if (journal->j_superblock->s_errno) {
768 ctx->fs->super->s_state |= EXT2_ERROR_FS;
769 ext2fs_mark_super_dirty(ctx->fs);
770 journal->j_superblock->s_errno = 0;
771 mark_buffer_dirty(journal->j_sb_buffer);
775 journal_destroy_revoke(journal);
776 journal_destroy_revoke_caches();
777 e2fsck_journal_release(ctx, journal, 1, 0);
781 int e2fsck_run_ext3_journal(e2fsck_t ctx)
783 io_manager io_ptr = ctx->fs->io->manager;
784 int blocksize = ctx->fs->blocksize;
785 errcode_t retval, recover_retval;
787 printf(_("%s: recovering journal\n"), ctx->device_name);
788 if (ctx->options & E2F_OPT_READONLY) {
789 printf(_("%s: won't do journal recovery while read-only\n"),
791 return EXT2_ET_FILE_RO;
794 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
795 ext2fs_flush(ctx->fs); /* Force out any modifications */
797 recover_retval = recover_ext3_journal(ctx);
800 * Reload the filesystem context to get up-to-date data from disk
801 * because journal recovery will change the filesystem under us.
803 ext2fs_close(ctx->fs);
804 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
805 ctx->superblock, blocksize, io_ptr,
809 com_err(ctx->program_name, retval,
810 _("while trying to re-open %s"),
814 ctx->fs->priv_data = ctx;
816 /* Set the superblock flags */
817 e2fsck_clear_recover(ctx, recover_retval);
818 return recover_retval;
822 * This function will move the journal inode from a visible file in
823 * the filesystem directory hierarchy to the reserved inode if necessary.
825 static const char * const journal_names[] = {
826 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
828 void e2fsck_move_ext3_journal(e2fsck_t ctx)
830 struct ext2_super_block *sb = ctx->fs->super;
831 struct problem_context pctx;
832 struct ext2_inode inode;
833 ext2_filsys fs = ctx->fs;
836 const char * const * cpp;
837 int group, mount_flags;
839 clear_problem_context(&pctx);
842 * If the filesystem is opened read-only, or there is no
843 * journal, then do nothing.
845 if ((ctx->options & E2F_OPT_READONLY) ||
846 (sb->s_journal_inum == 0) ||
847 !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
851 * Read in the journal inode
853 if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
857 * If it's necessary to backup the journal inode, do so.
859 if ((sb->s_jnl_backup_type == 0) ||
860 ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
861 memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
862 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
863 memcpy(sb->s_jnl_blocks, inode.i_block,
865 sb->s_jnl_blocks[16] = inode.i_size;
866 sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
867 ext2fs_mark_super_dirty(fs);
868 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
873 * If the journal is already the hidden inode, then do nothing
875 if (sb->s_journal_inum == EXT2_JOURNAL_INO)
879 * The journal inode had better have only one link and not be readable.
881 if (inode.i_links_count != 1)
885 * If the filesystem is mounted, or we can't tell whether
886 * or not it's mounted, do nothing.
888 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
889 if (retval || (mount_flags & EXT2_MF_MOUNTED))
893 * If we can't find the name of the journal inode, then do
896 for (cpp = journal_names; *cpp; cpp++) {
897 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
898 strlen(*cpp), 0, &ino);
899 if ((retval == 0) && (ino == sb->s_journal_inum))
905 /* We need the inode bitmap to be loaded */
906 retval = ext2fs_read_bitmaps(fs);
911 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
915 * OK, we've done all the checks, let's actually move the
916 * journal inode. Errors at this point mean we need to force
917 * an ext2 filesystem check.
919 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
921 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
923 sb->s_journal_inum = EXT2_JOURNAL_INO;
924 ext2fs_mark_super_dirty(fs);
925 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
926 inode.i_links_count = 0;
927 inode.i_dtime = time(0);
928 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
931 group = ext2fs_group_of_ino(fs, ino);
932 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
933 ext2fs_mark_ib_dirty(fs);
934 fs->group_desc[group].bg_free_inodes_count++;
935 fs->super->s_free_inodes_count++;
939 pctx.errcode = retval;
940 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
941 fs->super->s_state &= ~EXT2_VALID_FS;
942 ext2fs_mark_super_dirty(fs);