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 %ld\n",
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 %ld\n",
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 clear_problem_context(&pctx);
195 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
197 return EXT2_ET_NO_MEMORY;
200 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
202 retval = EXT2_ET_NO_MEMORY;
205 dev_journal = dev_fs+1;
207 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
208 dev_fs->k_dev = K_DEV_FS;
209 dev_journal->k_dev = K_DEV_JOURNAL;
211 journal->j_dev = dev_journal;
212 journal->j_fs_dev = dev_fs;
213 journal->j_inode = NULL;
214 journal->j_blocksize = ctx->fs->blocksize;
216 if (uuid_is_null(sb->s_journal_uuid)) {
217 if (!sb->s_journal_inum)
218 return EXT2_ET_BAD_INODE_NUM;
219 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
222 retval = EXT2_ET_NO_MEMORY;
226 j_inode->i_ctx = ctx;
227 j_inode->i_ino = sb->s_journal_inum;
229 if ((retval = ext2fs_read_inode(ctx->fs,
233 if (!j_inode->i_ext2.i_links_count ||
234 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
235 retval = EXT2_ET_NO_JOURNAL;
238 if (j_inode->i_ext2.i_size / journal->j_blocksize <
239 JFS_MIN_JOURNAL_BLOCKS) {
240 retval = EXT2_ET_JOURNAL_TOO_SMALL;
244 journal->j_maxlen = j_inode->i_ext2.i_size / journal->j_blocksize;
247 retval = ext2fs_inode_io_intern(ctx->fs, sb->s_journal_inum,
252 io_ptr = inode_io_manager;
254 journal->j_inode = j_inode;
255 ctx->journal_io = ctx->fs->io;
256 if ((retval = journal_bmap(journal, 0, &start)) != 0)
261 if (!ctx->journal_name) {
264 uuid_unparse(sb->s_journal_uuid, uuid);
265 ctx->journal_name = blkid_get_devname(ctx->blkid,
267 if (!ctx->journal_name)
268 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
270 journal_name = ctx->journal_name;
273 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
274 return EXT2_ET_LOAD_EXT_JOURNAL;
277 jfs_debug(1, "Using journal file %s\n", journal_name);
278 io_ptr = unix_io_manager;
282 test_io_backing_manager = io_ptr;
283 io_ptr = test_io_manager;
288 retval = io_ptr->open(journal_name, IO_FLAG_RW,
293 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
296 if (ctx->fs->blocksize == 1024)
298 bh = getblk(dev_journal, start, ctx->fs->blocksize);
300 retval = EXT2_ET_NO_MEMORY;
303 ll_rw_block(READ, 1, &bh);
304 if ((retval = bh->b_err) != 0)
306 memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024,
309 #ifdef EXT2FS_ENABLE_SWAPFS
310 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
311 ext2fs_swap_super(&jsuper);
313 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
314 !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
315 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
316 retval = EXT2_ET_LOAD_EXT_JOURNAL;
319 /* Make sure the journal UUID is correct */
320 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
321 sizeof(jsuper.s_uuid))) {
322 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
323 retval = EXT2_ET_LOAD_EXT_JOURNAL;
327 journal->j_maxlen = jsuper.s_blocks_count;
331 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
332 retval = EXT2_ET_NO_MEMORY;
336 journal->j_sb_buffer = bh;
337 journal->j_superblock = (journal_superblock_t *)bh->b_data;
341 ext2fs_free_mem(&j_inode);
344 *ret_journal = journal;
349 ext2fs_free_mem(&dev_fs);
351 ext2fs_free_mem(&j_inode);
353 ext2fs_free_mem(&journal);
358 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
359 struct problem_context *pctx)
361 struct ext2_super_block *sb = ctx->fs->super;
362 int recover = ctx->fs->super->s_feature_incompat &
363 EXT3_FEATURE_INCOMPAT_RECOVER;
364 int has_journal = ctx->fs->super->s_feature_compat &
365 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
367 if (has_journal || sb->s_journal_inum) {
368 /* The journal inode is bogus, remove and force full fsck */
369 pctx->ino = sb->s_journal_inum;
370 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
371 if (has_journal && sb->s_journal_inum)
372 printf("*** ext3 journal has been deleted - "
373 "filesystem is now ext2 only ***\n\n");
374 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
375 sb->s_journal_inum = 0;
376 ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */
377 e2fsck_clear_recover(ctx, 1);
380 return EXT2_ET_BAD_INODE_NUM;
381 } else if (recover) {
382 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
383 e2fsck_clear_recover(ctx, 1);
386 return EXT2_ET_UNSUPP_FEATURE;
391 #define V1_SB_SIZE 0x0024
392 static void clear_v2_journal_fields(journal_t *journal)
394 e2fsck_t ctx = journal->j_dev->k_ctx;
395 struct problem_context pctx;
397 clear_problem_context(&pctx);
399 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
402 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
403 ctx->fs->blocksize-V1_SB_SIZE);
404 mark_buffer_dirty(journal->j_sb_buffer);
408 static errcode_t e2fsck_journal_load(journal_t *journal)
410 e2fsck_t ctx = journal->j_dev->k_ctx;
411 journal_superblock_t *jsb;
412 struct buffer_head *jbh = journal->j_sb_buffer;
413 struct problem_context pctx;
415 clear_problem_context(&pctx);
417 ll_rw_block(READ, 1, &jbh);
419 com_err(ctx->device_name, jbh->b_err,
420 _("reading journal superblock\n"));
424 jsb = journal->j_superblock;
425 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
426 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
427 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
429 switch (ntohl(jsb->s_header.h_blocktype)) {
430 case JFS_SUPERBLOCK_V1:
431 journal->j_format_version = 1;
432 if (jsb->s_feature_compat ||
433 jsb->s_feature_incompat ||
434 jsb->s_feature_ro_compat ||
436 clear_v2_journal_fields(journal);
439 case JFS_SUPERBLOCK_V2:
440 journal->j_format_version = 2;
441 if (ntohl(jsb->s_nr_users) > 1 &&
442 (ctx->fs->io == ctx->journal_io))
443 clear_v2_journal_fields(journal);
444 if (ntohl(jsb->s_nr_users) > 1) {
445 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
446 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
451 * These should never appear in a journal super block, so if
452 * they do, the journal is badly corrupted.
454 case JFS_DESCRIPTOR_BLOCK:
455 case JFS_COMMIT_BLOCK:
456 case JFS_REVOKE_BLOCK:
457 return EXT2_ET_CORRUPT_SUPERBLOCK;
459 /* If we don't understand the superblock major type, but there
460 * is a magic number, then it is likely to be a new format we
461 * just don't understand, so leave it alone. */
463 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
466 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
467 return EXT2_ET_UNSUPP_FEATURE;
469 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
470 return EXT2_ET_RO_UNSUPP_FEATURE;
472 /* We have now checked whether we know enough about the journal
473 * format to be able to proceed safely, so any other checks that
474 * fail we should attempt to recover from. */
475 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
476 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
477 _("%s: no valid journal superblock found\n"),
479 return EXT2_ET_CORRUPT_SUPERBLOCK;
482 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
483 journal->j_maxlen = ntohl(jsb->s_maxlen);
484 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
485 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
486 _("%s: journal too short\n"),
488 return EXT2_ET_CORRUPT_SUPERBLOCK;
491 journal->j_tail_sequence = ntohl(jsb->s_sequence);
492 journal->j_transaction_sequence = journal->j_tail_sequence;
493 journal->j_tail = ntohl(jsb->s_start);
494 journal->j_first = ntohl(jsb->s_first);
495 journal->j_last = ntohl(jsb->s_maxlen);
500 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
511 /* Leave a valid existing V1 superblock signature alone.
512 * Anything unrecognisable we overwrite with a new V2
515 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
516 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
517 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
518 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
521 /* Zero out everything else beyond the superblock header */
523 p = ((char *) jsb) + sizeof(journal_header_t);
524 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
526 jsb->s_blocksize = htonl(ctx->fs->blocksize);
527 jsb->s_maxlen = htonl(journal->j_maxlen);
528 jsb->s_first = htonl(1);
530 /* Initialize the journal sequence number so that there is "no"
531 * chance we will find old "valid" transactions in the journal.
532 * This avoids the need to zero the whole journal (slow to do,
533 * and risky when we are just recovering the filesystem).
535 uuid_generate(u.uuid);
536 for (i = 0; i < 4; i ++)
538 jsb->s_sequence = htonl(new_seq);
540 mark_buffer_dirty(journal->j_sb_buffer);
541 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
544 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
546 struct problem_context *pctx)
548 struct ext2_super_block *sb = ctx->fs->super;
549 int recover = ctx->fs->super->s_feature_incompat &
550 EXT3_FEATURE_INCOMPAT_RECOVER;
552 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
553 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
554 e2fsck_journal_reset_super(ctx, journal->j_superblock,
556 journal->j_transaction_sequence = 1;
557 e2fsck_clear_recover(ctx, recover);
560 return EXT2_ET_CORRUPT_SUPERBLOCK;
561 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
562 return EXT2_ET_CORRUPT_SUPERBLOCK;
567 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
570 journal_superblock_t *jsb;
573 mark_buffer_clean(journal->j_sb_buffer);
574 else if (!(ctx->options & E2F_OPT_READONLY)) {
575 jsb = journal->j_superblock;
576 jsb->s_sequence = htonl(journal->j_transaction_sequence);
578 jsb->s_start = 0; /* this marks the journal as empty */
579 mark_buffer_dirty(journal->j_sb_buffer);
581 brelse(journal->j_sb_buffer);
583 if (ctx->journal_io) {
584 if (ctx->fs && ctx->fs->io != ctx->journal_io)
585 io_channel_close(ctx->journal_io);
590 if (journal->j_inode)
591 ext2fs_free_mem(&journal->j_inode);
593 if (journal->j_fs_dev)
594 ext2fs_free_mem(&journal->j_fs_dev);
595 ext2fs_free_mem(&journal);
599 * This function makes sure that the superblock fields regarding the
600 * journal are consistent.
602 int e2fsck_check_ext3_journal(e2fsck_t ctx)
604 struct ext2_super_block *sb = ctx->fs->super;
606 int recover = ctx->fs->super->s_feature_incompat &
607 EXT3_FEATURE_INCOMPAT_RECOVER;
608 struct problem_context pctx;
610 int reset = 0, force_fsck = 0;
613 /* If we don't have any journal features, don't do anything more */
614 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
615 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
616 uuid_is_null(sb->s_journal_uuid))
619 clear_problem_context(&pctx);
620 pctx.num = sb->s_journal_inum;
622 retval = e2fsck_get_journal(ctx, &journal);
624 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
625 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
626 (retval == EXT2_ET_NO_JOURNAL))
627 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
631 retval = e2fsck_journal_load(journal);
633 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
634 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
635 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
637 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
638 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
640 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
641 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
642 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
644 e2fsck_journal_release(ctx, journal, 0, 1);
649 * We want to make the flags consistent here. We will not leave with
650 * needs_recovery set but has_journal clear. We can't get in a loop
651 * with -y, -n, or -p, only if a user isn't making up their mind.
654 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
655 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
657 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
659 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
662 * Need a full fsck if we are releasing a
663 * journal stored on a reserved inode.
665 force_fsck = recover ||
666 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
667 /* Clear all of the journal fields */
668 sb->s_journal_inum = 0;
669 sb->s_journal_dev = 0;
670 memset(sb->s_journal_uuid, 0,
671 sizeof(sb->s_journal_uuid));
672 e2fsck_clear_recover(ctx, force_fsck);
673 } else if (!(ctx->options & E2F_OPT_READONLY)) {
674 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
675 ext2fs_mark_super_dirty(ctx->fs);
679 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
680 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
681 journal->j_superblock->s_start != 0) {
682 /* Print status information */
683 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
685 problem = PR_0_JOURNAL_RUN_DEFAULT;
687 problem = PR_0_JOURNAL_RUN;
688 if (fix_problem(ctx, problem, &pctx)) {
689 ctx->options |= E2F_OPT_FORCE;
690 sb->s_feature_incompat |=
691 EXT3_FEATURE_INCOMPAT_RECOVER;
692 ext2fs_mark_super_dirty(ctx->fs);
693 } else if (fix_problem(ctx,
694 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
696 sb->s_state &= ~EXT2_VALID_FS;
697 ext2fs_mark_super_dirty(ctx->fs);
700 * If the user answers no to the above question, we
701 * ignore the fact that journal apparently has data;
702 * accidentally replaying over valid data would be far
703 * worse than skipping a questionable recovery.
705 * XXX should we abort with a fatal error here? What
706 * will the ext3 kernel code do if a filesystem with
707 * !NEEDS_RECOVERY but with a non-zero
708 * journal->j_superblock->s_start is mounted?
712 e2fsck_journal_release(ctx, journal, reset, 0);
716 static errcode_t recover_ext3_journal(e2fsck_t ctx)
721 journal_init_revoke_caches();
722 retval = e2fsck_get_journal(ctx, &journal);
726 retval = e2fsck_journal_load(journal);
730 retval = journal_init_revoke(journal, 1024);
734 retval = -journal_recover(journal);
738 if (journal->j_superblock->s_errno) {
739 ctx->fs->super->s_state |= EXT2_ERROR_FS;
740 ext2fs_mark_super_dirty(ctx->fs);
741 journal->j_superblock->s_errno = 0;
742 mark_buffer_dirty(journal->j_sb_buffer);
746 journal_destroy_revoke(journal);
747 journal_destroy_revoke_caches();
748 e2fsck_journal_release(ctx, journal, 1, 0);
752 int e2fsck_run_ext3_journal(e2fsck_t ctx)
754 io_manager io_ptr = ctx->fs->io->manager;
755 int blocksize = ctx->fs->blocksize;
756 errcode_t retval, recover_retval;
758 printf(_("%s: recovering journal\n"), ctx->device_name);
759 if (ctx->options & E2F_OPT_READONLY) {
760 printf(_("%s: won't do journal recovery while read-only\n"),
762 return EXT2_ET_FILE_RO;
765 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
766 ext2fs_flush(ctx->fs); /* Force out any modifications */
768 recover_retval = recover_ext3_journal(ctx);
771 * Reload the filesystem context to get up-to-date data from disk
772 * because journal recovery will change the filesystem under us.
774 ext2fs_close(ctx->fs);
775 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
776 ctx->superblock, blocksize, io_ptr,
780 com_err(ctx->program_name, retval,
781 _("while trying to re-open %s"),
785 ctx->fs->priv_data = ctx;
787 /* Set the superblock flags */
788 e2fsck_clear_recover(ctx, recover_retval);
789 return recover_retval;
793 * This function will move the journal inode from a visible file in
794 * the filesystem directory hierarchy to the reserved inode if necessary.
796 static const char * const journal_names[] = {
797 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
799 void e2fsck_move_ext3_journal(e2fsck_t ctx)
801 struct ext2_super_block *sb = ctx->fs->super;
802 struct problem_context pctx;
803 struct ext2_inode inode;
804 ext2_filsys fs = ctx->fs;
807 const char * const * cpp;
808 int group, mount_flags;
811 * If the filesystem is opened read-only, or there is no
812 * journal, or the journal is already in the hidden inode,
815 if ((ctx->options & E2F_OPT_READONLY) ||
816 (sb->s_journal_inum == 0) ||
817 (sb->s_journal_inum == EXT2_JOURNAL_INO) ||
818 !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
822 * If the filesystem is mounted, or we can't tell whether
823 * or not it's mounted, do nothing.
825 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
826 if (retval || (mount_flags & EXT2_MF_MOUNTED))
830 * If we can't find the name of the journal inode, then do
833 for (cpp = journal_names; *cpp; cpp++) {
834 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
835 strlen(*cpp), 0, &ino);
836 if ((retval == 0) && (ino == sb->s_journal_inum))
843 * The inode had better have only one link and not be readable.
845 if (ext2fs_read_inode(fs, ino, &inode) != 0)
847 if (inode.i_links_count != 1)
850 /* We need the inode bitmap to be loaded */
851 retval = ext2fs_read_bitmaps(fs);
855 clear_problem_context(&pctx);
857 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
861 * OK, we've done all the checks, let's actually move the
862 * journal inode. Errors at this point mean we need to force
863 * an ext2 filesystem check.
865 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
867 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
869 sb->s_journal_inum = EXT2_JOURNAL_INO;
870 ext2fs_mark_super_dirty(fs);
871 inode.i_links_count = 0;
872 inode.i_dtime = time(0);
873 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
876 group = ext2fs_group_of_ino(fs, ino);
877 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
878 ext2fs_mark_ib_dirty(fs);
879 fs->group_desc[group].bg_free_inodes_count++;
880 fs->super->s_free_inodes_count++;
884 pctx.errcode = retval;
885 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
886 fs->super->s_state &= ~EXT2_VALID_FS;
887 ext2fs_mark_super_dirty(fs);