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;
30 int journal_enable_debug = 2;
33 /* Kernel compatibility functions for handling the journal. These allow us
34 * to use the recovery.c file virtually unchanged from the kernel, so we
35 * don't have to do much to keep kernel and user recovery in sync.
37 int journal_bmap(journal_t *journal, blk_t block, unsigned long *phys)
39 struct inode *inode = journal->j_inode;
48 retval= ext2fs_bmap(inode->i_ctx->fs, inode->i_ino,
49 &inode->i_ext2, NULL, 0, block, &pblk);
54 struct buffer_head *getblk(kdev_t kdev, blk_t blocknr, int blocksize)
56 struct buffer_head *bh;
58 bh = e2fsck_allocate_memory(kdev->k_ctx, sizeof(*bh), "block buffer");
62 jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
63 (unsigned long) blocknr, blocksize, ++bh_count);
65 bh->b_ctx = kdev->k_ctx;
66 if (kdev->k_dev == K_DEV_FS)
67 bh->b_io = kdev->k_ctx->fs->io;
69 bh->b_io = kdev->k_ctx->journal_io;
70 bh->b_size = blocksize;
71 bh->b_blocknr = blocknr;
76 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
79 struct buffer_head *bh;
81 for (; nr > 0; --nr) {
83 if (rw == READ && !bh->b_uptodate) {
84 jfs_debug(3, "reading block %lu/%p\n",
85 (unsigned long) bh->b_blocknr, (void *) bh);
86 retval = io_channel_read_blk(bh->b_io,
90 com_err(bh->b_ctx->device_name, retval,
91 "while reading block %ld\n",
97 } else if (rw == WRITE && bh->b_dirty) {
98 jfs_debug(3, "writing block %lu/%p\n",
99 (unsigned long) bh->b_blocknr, (void *) bh);
100 retval = io_channel_write_blk(bh->b_io,
104 com_err(bh->b_ctx->device_name, retval,
105 "while writing block %ld\n",
113 jfs_debug(3, "no-op %s for block %lu\n",
114 rw == READ ? "read" : "write",
115 (unsigned long) bh->b_blocknr);
119 void mark_buffer_dirty(struct buffer_head *bh)
124 static void mark_buffer_clean(struct buffer_head * bh)
129 void brelse(struct buffer_head *bh)
132 ll_rw_block(WRITE, 1, &bh);
133 jfs_debug(3, "freeing block %lu/%p (total %d)\n",
134 (unsigned long) bh->b_blocknr, (void *) bh, --bh_count);
135 ext2fs_free_mem((void **) &bh);
138 int buffer_uptodate(struct buffer_head *bh)
140 return bh->b_uptodate;
143 void mark_buffer_uptodate(struct buffer_head *bh, int val)
145 bh->b_uptodate = val;
148 void wait_on_buffer(struct buffer_head *bh)
151 ll_rw_block(READ, 1, &bh);
155 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
157 ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
159 /* if we had an error doing journal recovery, we need a full fsck */
161 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
162 ext2fs_mark_super_dirty(ctx->fs);
165 static errcode_t e2fsck_journal_init_inode(e2fsck_t ctx,
166 struct ext2_super_block *s,
169 struct inode *inode = NULL;
170 struct buffer_head *bh;
173 struct kdev_s *dev_fs = NULL, *dev_journal;
175 jfs_debug(1, "Using journal inode %u\n", s->s_journal_inum);
176 *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
178 return EXT2_ET_NO_MEMORY;
181 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
183 retval = EXT2_ET_NO_MEMORY;
186 dev_journal = dev_fs+1;
188 inode = e2fsck_allocate_memory(ctx, sizeof(*inode), "journal inode");
190 retval = EXT2_ET_NO_MEMORY;
195 inode->i_ino = s->s_journal_inum;
196 retval = ext2fs_read_inode(ctx->fs, s->s_journal_inum, &inode->i_ext2);
200 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
201 dev_fs->k_dev = K_DEV_FS;
202 dev_journal->k_dev = K_DEV_JOURNAL;
204 (*journal)->j_dev = dev_journal;
205 (*journal)->j_fs_dev = dev_fs;
206 (*journal)->j_inode = inode;
207 (*journal)->j_blocksize = ctx->fs->blocksize;
208 (*journal)->j_maxlen = inode->i_ext2.i_size / (*journal)->j_blocksize;
209 ctx->journal_io = ctx->fs->io;
211 if (!inode->i_ext2.i_links_count ||
212 !LINUX_S_ISREG(inode->i_ext2.i_mode) ||
213 (*journal)->j_maxlen < JFS_MIN_JOURNAL_BLOCKS ||
214 (retval = journal_bmap(*journal, 0, &start)) != 0) {
218 bh = getblk(dev_journal, start, (*journal)->j_blocksize);
220 retval = EXT2_ET_NO_MEMORY;
223 (*journal)->j_sb_buffer = bh;
224 (*journal)->j_superblock = (journal_superblock_t *)bh->b_data;
230 ext2fs_free_mem((void **)&dev_fs);
232 ext2fs_free_mem((void **)&inode);
234 ext2fs_free_mem((void **)journal);
238 static errcode_t e2fsck_journal_init_dev(e2fsck_t ctx,
239 struct ext2_super_block *s,
242 struct buffer_head *bh;
246 int blocksize = ctx->fs->blocksize;
247 struct ext2_super_block jsuper;
248 struct problem_context pctx;
249 const char *journal_name;
250 struct kdev_s *dev_fs = NULL, *dev_journal;
252 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
254 return EXT2_ET_NO_MEMORY;
256 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 clear_problem_context(&pctx);
262 journal_name = ctx->journal_name;
264 journal_name = ext2fs_find_block_device(s->s_journal_dev);
267 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
268 return EXT2_ET_LOAD_EXT_JOURNAL;
271 jfs_debug(1, "Using journal file %s\n", journal_name);
274 io_ptr = unix_io_manager;
276 io_ptr = test_io_manager;
277 test_io_backing_manager = unix_io_manager;
279 retval = io_ptr->open(journal_name, IO_FLAG_RW, &ctx->journal_io);
280 if (!ctx->journal_name)
281 free((void *) journal_name);
285 io_channel_set_blksize(ctx->journal_io, blocksize);
286 start = (blocksize == 1024) ? 1 : 0;
287 bh = getblk(dev_journal, start, blocksize);
289 return EXT2_ET_NO_MEMORY;
290 ll_rw_block(READ, 1, &bh);
293 memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024,
296 #ifdef EXT2FS_ENABLE_SWAPFS
297 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
298 ext2fs_swap_super(&jsuper);
300 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
301 !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
302 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
303 return EXT2_ET_LOAD_EXT_JOURNAL;
305 /* Make sure the journal UUID is correct */
306 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
307 sizeof(jsuper.s_uuid))) {
308 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
309 return EXT2_ET_LOAD_EXT_JOURNAL;
312 *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
314 return EXT2_ET_NO_MEMORY;
317 (*journal)->j_dev = dev_journal;
318 (*journal)->j_fs_dev = dev_fs;
319 (*journal)->j_inode = NULL;
320 (*journal)->j_blocksize = ctx->fs->blocksize;
321 (*journal)->j_maxlen = jsuper.s_blocks_count;
323 bh = getblk(dev_journal, start+1, (*journal)->j_blocksize);
325 retval = EXT2_ET_NO_MEMORY;
328 (*journal)->j_sb_buffer = bh;
329 (*journal)->j_superblock = (journal_superblock_t *)bh->b_data;
334 ext2fs_free_mem((void **)&dev_fs);
335 ext2fs_free_mem((void **)journal);
339 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **journal)
341 struct ext2_super_block *sb = ctx->fs->super;
343 if (uuid_is_null(sb->s_journal_uuid)) {
344 if (!sb->s_journal_inum)
345 return EXT2_ET_BAD_INODE_NUM;
346 return e2fsck_journal_init_inode(ctx, sb, journal);
348 return e2fsck_journal_init_dev(ctx, sb, journal);
352 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
353 struct problem_context *pctx)
355 struct ext2_super_block *sb = ctx->fs->super;
356 int recover = ctx->fs->super->s_feature_incompat &
357 EXT3_FEATURE_INCOMPAT_RECOVER;
358 int has_journal = ctx->fs->super->s_feature_compat &
359 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
361 if (has_journal || sb->s_journal_inum) {
362 /* The journal inode is bogus, remove and force full fsck */
363 pctx->ino = sb->s_journal_inum;
364 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
365 if (has_journal && sb->s_journal_inum)
366 printf("*** ext3 journal has been deleted - "
367 "filesystem is now ext2 only ***\n\n");
368 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
369 sb->s_journal_inum = 0;
370 ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */
371 e2fsck_clear_recover(ctx, 1);
374 return EXT2_ET_BAD_INODE_NUM;
375 } else if (recover) {
376 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
377 e2fsck_clear_recover(ctx, 1);
380 return EXT2_ET_UNSUPP_FEATURE;
385 #define V1_SB_SIZE 0x0024
386 static void clear_v2_journal_fields(journal_t *journal)
388 e2fsck_t ctx = journal->j_dev->k_ctx;
389 struct problem_context pctx;
391 clear_problem_context(&pctx);
393 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
396 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
397 ctx->fs->blocksize-V1_SB_SIZE);
398 mark_buffer_dirty(journal->j_sb_buffer);
402 static errcode_t e2fsck_journal_load(journal_t *journal)
404 e2fsck_t ctx = journal->j_dev->k_ctx;
405 journal_superblock_t *jsb;
406 struct buffer_head *jbh = journal->j_sb_buffer;
407 struct problem_context pctx;
409 clear_problem_context(&pctx);
411 ll_rw_block(READ, 1, &jbh);
413 com_err(ctx->device_name, jbh->b_err,
414 _("reading journal superblock\n"));
418 jsb = journal->j_superblock;
419 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
420 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
421 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
423 switch (ntohl(jsb->s_header.h_blocktype)) {
424 case JFS_SUPERBLOCK_V1:
425 journal->j_format_version = 1;
426 if (jsb->s_feature_compat ||
427 jsb->s_feature_incompat ||
428 jsb->s_feature_ro_compat ||
430 clear_v2_journal_fields(journal);
433 case JFS_SUPERBLOCK_V2:
434 journal->j_format_version = 2;
435 if (ntohl(jsb->s_nr_users) > 1 &&
436 (ctx->fs->io == ctx->journal_io))
437 clear_v2_journal_fields(journal);
438 if (ntohl(jsb->s_nr_users) > 1) {
439 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
440 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
445 * These should never appear in a journal super block, so if
446 * they do, the journal is badly corrupted.
448 case JFS_DESCRIPTOR_BLOCK:
449 case JFS_COMMIT_BLOCK:
450 case JFS_REVOKE_BLOCK:
451 return EXT2_ET_CORRUPT_SUPERBLOCK;
453 /* If we don't understand the superblock major type, but there
454 * is a magic number, then it is likely to be a new format we
455 * just don't understand, so leave it alone. */
457 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
460 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
461 return EXT2_ET_UNSUPP_FEATURE;
463 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
464 return EXT2_ET_RO_UNSUPP_FEATURE;
466 /* We have now checked whether we know enough about the journal
467 * format to be able to proceed safely, so any other checks that
468 * fail we should attempt to recover from. */
469 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
470 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
471 _("%s: no valid journal superblock found\n"),
473 return EXT2_ET_CORRUPT_SUPERBLOCK;
476 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
477 journal->j_maxlen = ntohl(jsb->s_maxlen);
478 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
479 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
480 _("%s: journal too short\n"),
482 return EXT2_ET_CORRUPT_SUPERBLOCK;
485 journal->j_tail_sequence = ntohl(jsb->s_sequence);
486 journal->j_transaction_sequence = journal->j_tail_sequence;
487 journal->j_tail = ntohl(jsb->s_start);
488 journal->j_first = ntohl(jsb->s_first);
489 journal->j_last = ntohl(jsb->s_maxlen);
494 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
505 /* Leave a valid existing V1 superblock signature alone.
506 * Anything unrecognisable we overwrite with a new V2
509 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
510 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
511 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
512 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
515 /* Zero out everything else beyond the superblock header */
517 p = ((char *) jsb) + sizeof(journal_header_t);
518 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
520 jsb->s_blocksize = htonl(ctx->fs->blocksize);
521 jsb->s_maxlen = htonl(journal->j_maxlen);
522 jsb->s_first = htonl(1);
524 /* Initialize the journal sequence number so that there is "no"
525 * chance we will find old "valid" transactions in the journal.
526 * This avoids the need to zero the whole journal (slow to do,
527 * and risky when we are just recovering the filesystem).
529 uuid_generate(u.uuid);
530 for (i = 0; i < 4; i ++)
532 jsb->s_sequence = htonl(new_seq);
534 mark_buffer_dirty(journal->j_sb_buffer);
535 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
538 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
540 struct problem_context *pctx)
542 struct ext2_super_block *sb = ctx->fs->super;
543 int recover = ctx->fs->super->s_feature_incompat &
544 EXT3_FEATURE_INCOMPAT_RECOVER;
546 pctx->num = journal->j_inode->i_ino;
548 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
549 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
550 e2fsck_journal_reset_super(ctx, journal->j_superblock,
552 journal->j_transaction_sequence = 1;
553 e2fsck_clear_recover(ctx, recover);
556 return EXT2_ET_CORRUPT_SUPERBLOCK;
557 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
558 return EXT2_ET_CORRUPT_SUPERBLOCK;
563 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
566 journal_superblock_t *jsb;
569 mark_buffer_clean(journal->j_sb_buffer);
570 else if (!(ctx->options & E2F_OPT_READONLY)) {
571 jsb = journal->j_superblock;
572 jsb->s_sequence = htonl(journal->j_transaction_sequence);
574 jsb->s_start = 0; /* this marks the journal as empty */
575 mark_buffer_dirty(journal->j_sb_buffer);
577 brelse(journal->j_sb_buffer);
579 if (ctx->journal_io) {
580 if (ctx->fs && ctx->fs->io != ctx->journal_io)
581 io_channel_close(ctx->journal_io);
585 if (journal->j_inode)
586 ext2fs_free_mem((void **)&journal->j_inode);
587 ext2fs_free_mem((void **)&journal);
591 * This function makes sure that the superblock fields regarding the
592 * journal are consistent.
594 int e2fsck_check_ext3_journal(e2fsck_t ctx)
596 struct ext2_super_block *sb = ctx->fs->super;
598 int recover = ctx->fs->super->s_feature_incompat &
599 EXT3_FEATURE_INCOMPAT_RECOVER;
600 struct problem_context pctx;
601 int reset = 0, force_fsck = 0;
604 /* If we don't have any journal features, don't do anything more */
605 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
606 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
607 uuid_is_null(sb->s_journal_uuid))
610 clear_problem_context(&pctx);
611 pctx.num = sb->s_journal_inum;
613 retval = e2fsck_get_journal(ctx, &journal);
615 if (retval == EXT2_ET_BAD_INODE_NUM)
616 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
620 retval = e2fsck_journal_load(journal);
622 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
623 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
624 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
626 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
627 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
629 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
630 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
631 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
633 e2fsck_journal_release(ctx, journal, 0, 1);
638 * We want to make the flags consistent here. We will not leave with
639 * needs_recovery set but has_journal clear. We can't get in a loop
640 * with -y, -n, or -p, only if a user isn't making up their mind.
643 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
644 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
646 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
648 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
651 * Need a full fsck if we are releasing a
652 * journal stored on a reserved inode.
654 force_fsck = recover ||
655 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
656 /* Clear all of the journal fields */
657 sb->s_journal_inum = 0;
658 sb->s_journal_dev = 0;
659 memset(sb->s_journal_uuid, 0,
660 sizeof(sb->s_journal_uuid));
661 e2fsck_clear_recover(ctx, force_fsck);
662 } else if (!(ctx->options & E2F_OPT_READONLY)) {
663 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
664 ext2fs_mark_super_dirty(ctx->fs);
668 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
669 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
670 journal->j_superblock->s_start != 0) {
671 if (fix_problem(ctx, PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
673 sb->s_state &= ~EXT2_VALID_FS;
674 ext2fs_mark_super_dirty(ctx->fs);
677 * If the user answers no to the above question, we
678 * ignore the fact that journal apparently has data;
679 * accidentally replaying over valid data would be far
680 * worse than skipping a questionable recovery.
682 * XXX should we abort with a fatal error here? What
683 * will the ext3 kernel code do if a filesystem with
684 * !NEEDS_RECOVERY but with a non-zero
685 * journal->j_superblock->s_start is mounted?
689 e2fsck_journal_release(ctx, journal, reset, 0);
693 static errcode_t recover_ext3_journal(e2fsck_t ctx)
698 journal_init_revoke_caches();
699 retval = e2fsck_get_journal(ctx, &journal);
703 retval = e2fsck_journal_load(journal);
707 retval = journal_init_revoke(journal, 1024);
711 retval = -journal_recover(journal);
715 if (journal->j_superblock->s_errno) {
716 ctx->fs->super->s_state |= EXT2_ERROR_FS;
717 ext2fs_mark_super_dirty(ctx->fs);
718 journal->j_superblock->s_errno = 0;
719 mark_buffer_dirty(journal->j_sb_buffer);
723 journal_destroy_revoke_caches();
724 e2fsck_journal_release(ctx, journal, 1, 0);
728 int e2fsck_run_ext3_journal(e2fsck_t ctx)
730 io_manager io_ptr = ctx->fs->io->manager;
731 int blocksize = ctx->fs->blocksize;
732 errcode_t retval, recover_retval;
734 printf(_("%s: recovering journal\n"), ctx->device_name);
735 if (ctx->options & E2F_OPT_READONLY) {
736 printf(_("%s: won't do journal recovery while read-only\n"),
738 return EXT2_ET_FILE_RO;
741 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
742 ext2fs_flush(ctx->fs); /* Force out any modifications */
744 recover_retval = recover_ext3_journal(ctx);
747 * Reload the filesystem context to get up-to-date data from disk
748 * because journal recovery will change the filesystem under us.
750 ext2fs_close(ctx->fs);
751 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
752 ctx->superblock, blocksize, io_ptr,
756 com_err(ctx->program_name, retval,
757 _("while trying to re-open %s"),
761 ctx->fs->priv_data = ctx;
763 /* Set the superblock flags */
764 e2fsck_clear_recover(ctx, recover_retval);
765 return recover_retval;
769 * This function will move the journal inode from a visible file in
770 * the filesystem directory hierarchy to the reserved inode if necessary.
772 const static char * const journal_names[] = {
773 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
775 void e2fsck_move_ext3_journal(e2fsck_t ctx)
777 struct ext2_super_block *sb = ctx->fs->super;
778 struct problem_context pctx;
779 struct ext2_inode inode;
780 ext2_filsys fs = ctx->fs;
783 const char * const * cpp;
784 int group, mount_flags;
787 * If the filesystem is opened read-only, or there is no
788 * journal, or the journal is already in the hidden inode,
791 if ((ctx->options & E2F_OPT_READONLY) ||
792 (sb->s_journal_inum == 0) ||
793 (sb->s_journal_inum == EXT2_JOURNAL_INO) ||
794 !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
798 * If the filesystem is mounted, or we can't tell whether
799 * or not it's mounted, do nothing.
801 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
802 if (retval || (mount_flags & EXT2_MF_MOUNTED))
806 * If we can't find the name of the journal inode, then do
809 for (cpp = journal_names; *cpp; cpp++) {
810 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
811 strlen(*cpp), 0, &ino);
812 if ((retval == 0) && (ino == sb->s_journal_inum))
819 * The inode had better have only one link and not be readable.
821 if (ext2fs_read_inode(fs, ino, &inode) != 0)
823 if (inode.i_links_count != 1)
826 /* We need the inode bitmap to be loaded */
827 retval = ext2fs_read_bitmaps(fs);
831 clear_problem_context(&pctx);
833 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
837 * OK, we've done all the checks, let's actually move the
838 * journal inode. Errors at this point mean we need to force
839 * an ext2 filesystem check.
841 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
843 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
845 sb->s_journal_inum = EXT2_JOURNAL_INO;
846 ext2fs_mark_super_dirty(fs);
847 inode.i_links_count = 0;
848 inode.i_dtime = time(0);
849 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
852 group = ext2fs_group_of_ino(fs, ino);
853 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
854 ext2fs_mark_ib_dirty(fs);
855 fs->group_desc[group].bg_free_inodes_count++;
856 fs->super->s_free_inodes_count++;
860 pctx.errcode = retval;
861 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
862 fs->super->s_state &= ~EXT2_VALID_FS;
863 ext2fs_mark_super_dirty(fs);