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;
72 bh = e2fsck_allocate_memory(kdev->k_ctx, sizeof(*bh), "block buffer");
76 jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
77 (unsigned long) blocknr, blocksize, ++bh_count);
79 bh->b_ctx = kdev->k_ctx;
80 if (kdev->k_dev == K_DEV_FS)
81 bh->b_io = kdev->k_ctx->fs->io;
83 bh->b_io = kdev->k_ctx->journal_io;
84 bh->b_size = blocksize;
85 bh->b_blocknr = blocknr;
90 void sync_blockdev(kdev_t kdev)
94 if (kdev->k_dev == K_DEV_FS)
95 io = kdev->k_ctx->fs->io;
97 io = kdev->k_ctx->journal_io;
102 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
105 struct buffer_head *bh;
107 for (; nr > 0; --nr) {
109 if (rw == READ && !bh->b_uptodate) {
110 jfs_debug(3, "reading block %lu/%p\n",
111 (unsigned long) bh->b_blocknr, (void *) bh);
112 retval = io_channel_read_blk(bh->b_io,
116 com_err(bh->b_ctx->device_name, retval,
117 "while reading block %lu\n",
118 (unsigned long) bh->b_blocknr);
123 } else if (rw == WRITE && bh->b_dirty) {
124 jfs_debug(3, "writing block %lu/%p\n",
125 (unsigned long) bh->b_blocknr, (void *) bh);
126 retval = io_channel_write_blk(bh->b_io,
130 com_err(bh->b_ctx->device_name, retval,
131 "while writing block %lu\n",
132 (unsigned long) bh->b_blocknr);
139 jfs_debug(3, "no-op %s for block %lu\n",
140 rw == READ ? "read" : "write",
141 (unsigned long) bh->b_blocknr);
146 void mark_buffer_dirty(struct buffer_head *bh)
151 static void mark_buffer_clean(struct buffer_head * bh)
156 void brelse(struct buffer_head *bh)
159 ll_rw_block(WRITE, 1, &bh);
160 jfs_debug(3, "freeing block %lu/%p (total %d)\n",
161 (unsigned long) bh->b_blocknr, (void *) bh, --bh_count);
162 ext2fs_free_mem(&bh);
165 int buffer_uptodate(struct buffer_head *bh)
167 return bh->b_uptodate;
170 void mark_buffer_uptodate(struct buffer_head *bh, int val)
172 bh->b_uptodate = val;
175 void wait_on_buffer(struct buffer_head *bh)
178 ll_rw_block(READ, 1, &bh);
182 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
184 ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
186 /* if we had an error doing journal recovery, we need a full fsck */
188 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
189 ext2fs_mark_super_dirty(ctx->fs);
192 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
194 struct ext2_super_block *sb = ctx->fs->super;
195 struct ext2_super_block jsuper;
196 struct problem_context pctx;
197 struct buffer_head *bh;
198 struct inode *j_inode = NULL;
199 struct kdev_s *dev_fs = NULL, *dev_journal;
200 const char *journal_name = 0;
201 journal_t *journal = NULL;
202 errcode_t retval = 0;
203 io_manager io_ptr = 0;
204 unsigned long start = 0;
207 int tried_backup_jnl = 0;
210 clear_problem_context(&pctx);
212 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
214 return EXT2_ET_NO_MEMORY;
217 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
219 retval = EXT2_ET_NO_MEMORY;
222 dev_journal = dev_fs+1;
224 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
225 dev_fs->k_dev = K_DEV_FS;
226 dev_journal->k_dev = K_DEV_JOURNAL;
228 journal->j_dev = dev_journal;
229 journal->j_fs_dev = dev_fs;
230 journal->j_inode = NULL;
231 journal->j_blocksize = ctx->fs->blocksize;
233 if (uuid_is_null(sb->s_journal_uuid)) {
234 if (!sb->s_journal_inum)
235 return EXT2_ET_BAD_INODE_NUM;
236 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
239 retval = EXT2_ET_NO_MEMORY;
243 j_inode->i_ctx = ctx;
244 j_inode->i_ino = sb->s_journal_inum;
246 if ((retval = ext2fs_read_inode(ctx->fs,
248 &j_inode->i_ext2))) {
250 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
253 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
254 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
256 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
257 j_inode->i_ext2.i_links_count = 1;
258 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
261 if (!j_inode->i_ext2.i_links_count ||
262 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
263 retval = EXT2_ET_NO_JOURNAL;
264 goto try_backup_journal;
266 if (j_inode->i_ext2.i_size / journal->j_blocksize <
267 JFS_MIN_JOURNAL_BLOCKS) {
268 retval = EXT2_ET_JOURNAL_TOO_SMALL;
269 goto try_backup_journal;
271 for (i=0; i < EXT2_N_BLOCKS; i++) {
272 blk = j_inode->i_ext2.i_block[i];
274 if (i < EXT2_NDIR_BLOCKS) {
275 retval = EXT2_ET_JOURNAL_TOO_SMALL;
276 goto try_backup_journal;
280 if (blk < sb->s_first_data_block ||
281 blk >= sb->s_blocks_count) {
282 retval = EXT2_ET_BAD_BLOCK_NUM;
283 goto try_backup_journal;
286 journal->j_maxlen = j_inode->i_ext2.i_size / journal->j_blocksize;
289 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
295 io_ptr = inode_io_manager;
297 journal->j_inode = j_inode;
298 ctx->journal_io = ctx->fs->io;
299 if ((retval = journal_bmap(journal, 0, &start)) != 0)
304 if (!ctx->journal_name) {
307 uuid_unparse(sb->s_journal_uuid, uuid);
308 ctx->journal_name = blkid_get_devname(ctx->blkid,
310 if (!ctx->journal_name)
311 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
313 journal_name = ctx->journal_name;
316 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
317 return EXT2_ET_LOAD_EXT_JOURNAL;
320 jfs_debug(1, "Using journal file %s\n", journal_name);
321 io_ptr = unix_io_manager;
325 test_io_backing_manager = io_ptr;
326 io_ptr = test_io_manager;
331 retval = io_ptr->open(journal_name, IO_FLAG_RW,
336 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
339 if (ctx->fs->blocksize == 1024)
341 bh = getblk(dev_journal, start, ctx->fs->blocksize);
343 retval = EXT2_ET_NO_MEMORY;
346 ll_rw_block(READ, 1, &bh);
347 if ((retval = bh->b_err) != 0)
349 memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024,
352 #ifdef EXT2FS_ENABLE_SWAPFS
353 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
354 ext2fs_swap_super(&jsuper);
356 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
357 !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
358 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
359 retval = EXT2_ET_LOAD_EXT_JOURNAL;
362 /* Make sure the journal UUID is correct */
363 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
364 sizeof(jsuper.s_uuid))) {
365 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
366 retval = EXT2_ET_LOAD_EXT_JOURNAL;
370 journal->j_maxlen = jsuper.s_blocks_count;
374 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
375 retval = EXT2_ET_NO_MEMORY;
379 journal->j_sb_buffer = bh;
380 journal->j_superblock = (journal_superblock_t *)bh->b_data;
384 ext2fs_free_mem(&j_inode);
387 *ret_journal = journal;
392 ext2fs_free_mem(&dev_fs);
394 ext2fs_free_mem(&j_inode);
396 ext2fs_free_mem(&journal);
401 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
402 struct problem_context *pctx)
404 struct ext2_super_block *sb = ctx->fs->super;
405 int recover = ctx->fs->super->s_feature_incompat &
406 EXT3_FEATURE_INCOMPAT_RECOVER;
407 int has_journal = ctx->fs->super->s_feature_compat &
408 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
410 if (has_journal || sb->s_journal_inum) {
411 /* The journal inode is bogus, remove and force full fsck */
412 pctx->ino = sb->s_journal_inum;
413 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
414 if (has_journal && sb->s_journal_inum)
415 printf("*** ext3 journal has been deleted - "
416 "filesystem is now ext2 only ***\n\n");
417 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
418 sb->s_journal_inum = 0;
419 ctx->flags |= E2F_FLAG_JOURNAL_INODE; /* FIXME: todo */
420 e2fsck_clear_recover(ctx, 1);
423 return EXT2_ET_BAD_INODE_NUM;
424 } else if (recover) {
425 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
426 e2fsck_clear_recover(ctx, 1);
429 return EXT2_ET_UNSUPP_FEATURE;
434 #define V1_SB_SIZE 0x0024
435 static void clear_v2_journal_fields(journal_t *journal)
437 e2fsck_t ctx = journal->j_dev->k_ctx;
438 struct problem_context pctx;
440 clear_problem_context(&pctx);
442 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
445 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
446 ctx->fs->blocksize-V1_SB_SIZE);
447 mark_buffer_dirty(journal->j_sb_buffer);
451 static errcode_t e2fsck_journal_load(journal_t *journal)
453 e2fsck_t ctx = journal->j_dev->k_ctx;
454 journal_superblock_t *jsb;
455 struct buffer_head *jbh = journal->j_sb_buffer;
456 struct problem_context pctx;
458 clear_problem_context(&pctx);
460 ll_rw_block(READ, 1, &jbh);
462 com_err(ctx->device_name, jbh->b_err,
463 _("reading journal superblock\n"));
467 jsb = journal->j_superblock;
468 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
469 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
470 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
472 switch (ntohl(jsb->s_header.h_blocktype)) {
473 case JFS_SUPERBLOCK_V1:
474 journal->j_format_version = 1;
475 if (jsb->s_feature_compat ||
476 jsb->s_feature_incompat ||
477 jsb->s_feature_ro_compat ||
479 clear_v2_journal_fields(journal);
482 case JFS_SUPERBLOCK_V2:
483 journal->j_format_version = 2;
484 if (ntohl(jsb->s_nr_users) > 1 &&
485 uuid_is_null(ctx->fs->super->s_journal_uuid))
486 clear_v2_journal_fields(journal);
487 if (ntohl(jsb->s_nr_users) > 1) {
488 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
489 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
494 * These should never appear in a journal super block, so if
495 * they do, the journal is badly corrupted.
497 case JFS_DESCRIPTOR_BLOCK:
498 case JFS_COMMIT_BLOCK:
499 case JFS_REVOKE_BLOCK:
500 return EXT2_ET_CORRUPT_SUPERBLOCK;
502 /* If we don't understand the superblock major type, but there
503 * is a magic number, then it is likely to be a new format we
504 * just don't understand, so leave it alone. */
506 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
509 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
510 return EXT2_ET_UNSUPP_FEATURE;
512 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
513 return EXT2_ET_RO_UNSUPP_FEATURE;
515 /* We have now checked whether we know enough about the journal
516 * format to be able to proceed safely, so any other checks that
517 * fail we should attempt to recover from. */
518 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
519 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
520 _("%s: no valid journal superblock found\n"),
522 return EXT2_ET_CORRUPT_SUPERBLOCK;
525 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
526 journal->j_maxlen = ntohl(jsb->s_maxlen);
527 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
528 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
529 _("%s: journal too short\n"),
531 return EXT2_ET_CORRUPT_SUPERBLOCK;
534 journal->j_tail_sequence = ntohl(jsb->s_sequence);
535 journal->j_transaction_sequence = journal->j_tail_sequence;
536 journal->j_tail = ntohl(jsb->s_start);
537 journal->j_first = ntohl(jsb->s_first);
538 journal->j_last = ntohl(jsb->s_maxlen);
543 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
554 /* Leave a valid existing V1 superblock signature alone.
555 * Anything unrecognisable we overwrite with a new V2
558 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
559 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
560 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
561 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
564 /* Zero out everything else beyond the superblock header */
566 p = ((char *) jsb) + sizeof(journal_header_t);
567 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
569 jsb->s_blocksize = htonl(ctx->fs->blocksize);
570 jsb->s_maxlen = htonl(journal->j_maxlen);
571 jsb->s_first = htonl(1);
573 /* Initialize the journal sequence number so that there is "no"
574 * chance we will find old "valid" transactions in the journal.
575 * This avoids the need to zero the whole journal (slow to do,
576 * and risky when we are just recovering the filesystem).
578 uuid_generate(u.uuid);
579 for (i = 0; i < 4; i ++)
581 jsb->s_sequence = htonl(new_seq);
583 mark_buffer_dirty(journal->j_sb_buffer);
584 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
587 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
589 struct problem_context *pctx)
591 struct ext2_super_block *sb = ctx->fs->super;
592 int recover = ctx->fs->super->s_feature_incompat &
593 EXT3_FEATURE_INCOMPAT_RECOVER;
595 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
596 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
597 e2fsck_journal_reset_super(ctx, journal->j_superblock,
599 journal->j_transaction_sequence = 1;
600 e2fsck_clear_recover(ctx, recover);
603 return EXT2_ET_CORRUPT_SUPERBLOCK;
604 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
605 return EXT2_ET_CORRUPT_SUPERBLOCK;
610 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
613 journal_superblock_t *jsb;
616 mark_buffer_clean(journal->j_sb_buffer);
617 else if (!(ctx->options & E2F_OPT_READONLY)) {
618 jsb = journal->j_superblock;
619 jsb->s_sequence = htonl(journal->j_transaction_sequence);
621 jsb->s_start = 0; /* this marks the journal as empty */
622 mark_buffer_dirty(journal->j_sb_buffer);
624 brelse(journal->j_sb_buffer);
626 if (ctx->journal_io) {
627 if (ctx->fs && ctx->fs->io != ctx->journal_io)
628 io_channel_close(ctx->journal_io);
633 if (journal->j_inode)
634 ext2fs_free_mem(&journal->j_inode);
636 if (journal->j_fs_dev)
637 ext2fs_free_mem(&journal->j_fs_dev);
638 ext2fs_free_mem(&journal);
642 * This function makes sure that the superblock fields regarding the
643 * journal are consistent.
645 int e2fsck_check_ext3_journal(e2fsck_t ctx)
647 struct ext2_super_block *sb = ctx->fs->super;
649 int recover = ctx->fs->super->s_feature_incompat &
650 EXT3_FEATURE_INCOMPAT_RECOVER;
651 struct problem_context pctx;
653 int reset = 0, force_fsck = 0;
656 /* If we don't have any journal features, don't do anything more */
657 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
658 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
659 uuid_is_null(sb->s_journal_uuid))
662 clear_problem_context(&pctx);
663 pctx.num = sb->s_journal_inum;
665 retval = e2fsck_get_journal(ctx, &journal);
667 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
668 (retval == EXT2_ET_BAD_BLOCK_NUM) ||
669 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
670 (retval == EXT2_ET_NO_JOURNAL))
671 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
675 retval = e2fsck_journal_load(journal);
677 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
678 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
679 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
681 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
682 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
684 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
685 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
686 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
688 e2fsck_journal_release(ctx, journal, 0, 1);
693 * We want to make the flags consistent here. We will not leave with
694 * needs_recovery set but has_journal clear. We can't get in a loop
695 * with -y, -n, or -p, only if a user isn't making up their mind.
698 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
699 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
701 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
703 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
706 * Need a full fsck if we are releasing a
707 * journal stored on a reserved inode.
709 force_fsck = recover ||
710 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
711 /* Clear all of the journal fields */
712 sb->s_journal_inum = 0;
713 sb->s_journal_dev = 0;
714 memset(sb->s_journal_uuid, 0,
715 sizeof(sb->s_journal_uuid));
716 e2fsck_clear_recover(ctx, force_fsck);
717 } else if (!(ctx->options & E2F_OPT_READONLY)) {
718 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
719 ext2fs_mark_super_dirty(ctx->fs);
723 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
724 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
725 journal->j_superblock->s_start != 0) {
726 /* Print status information */
727 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
729 problem = PR_0_JOURNAL_RUN_DEFAULT;
731 problem = PR_0_JOURNAL_RUN;
732 if (fix_problem(ctx, problem, &pctx)) {
733 ctx->options |= E2F_OPT_FORCE;
734 sb->s_feature_incompat |=
735 EXT3_FEATURE_INCOMPAT_RECOVER;
736 ext2fs_mark_super_dirty(ctx->fs);
737 } else if (fix_problem(ctx,
738 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
740 sb->s_state &= ~EXT2_VALID_FS;
741 ext2fs_mark_super_dirty(ctx->fs);
744 * If the user answers no to the above question, we
745 * ignore the fact that journal apparently has data;
746 * accidentally replaying over valid data would be far
747 * worse than skipping a questionable recovery.
749 * XXX should we abort with a fatal error here? What
750 * will the ext3 kernel code do if a filesystem with
751 * !NEEDS_RECOVERY but with a non-zero
752 * journal->j_superblock->s_start is mounted?
756 e2fsck_journal_release(ctx, journal, reset, 0);
760 static errcode_t recover_ext3_journal(e2fsck_t ctx)
765 journal_init_revoke_caches();
766 retval = e2fsck_get_journal(ctx, &journal);
770 retval = e2fsck_journal_load(journal);
774 retval = journal_init_revoke(journal, 1024);
778 retval = -journal_recover(journal);
782 if (journal->j_superblock->s_errno) {
783 ctx->fs->super->s_state |= EXT2_ERROR_FS;
784 ext2fs_mark_super_dirty(ctx->fs);
785 journal->j_superblock->s_errno = 0;
786 mark_buffer_dirty(journal->j_sb_buffer);
790 journal_destroy_revoke(journal);
791 journal_destroy_revoke_caches();
792 e2fsck_journal_release(ctx, journal, 1, 0);
796 int e2fsck_run_ext3_journal(e2fsck_t ctx)
798 io_manager io_ptr = ctx->fs->io->manager;
799 int blocksize = ctx->fs->blocksize;
800 errcode_t retval, recover_retval;
802 printf(_("%s: recovering journal\n"), ctx->device_name);
803 if (ctx->options & E2F_OPT_READONLY) {
804 printf(_("%s: won't do journal recovery while read-only\n"),
806 return EXT2_ET_FILE_RO;
809 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
810 ext2fs_flush(ctx->fs); /* Force out any modifications */
812 recover_retval = recover_ext3_journal(ctx);
815 * Reload the filesystem context to get up-to-date data from disk
816 * because journal recovery will change the filesystem under us.
818 ext2fs_close(ctx->fs);
819 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
820 ctx->superblock, blocksize, io_ptr,
824 com_err(ctx->program_name, retval,
825 _("while trying to re-open %s"),
829 ctx->fs->priv_data = ctx;
831 /* Set the superblock flags */
832 e2fsck_clear_recover(ctx, recover_retval);
833 return recover_retval;
837 * This function will move the journal inode from a visible file in
838 * the filesystem directory hierarchy to the reserved inode if necessary.
840 static const char * const journal_names[] = {
841 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
843 void e2fsck_move_ext3_journal(e2fsck_t ctx)
845 struct ext2_super_block *sb = ctx->fs->super;
846 struct problem_context pctx;
847 struct ext2_inode inode;
848 ext2_filsys fs = ctx->fs;
851 const char * const * cpp;
852 int group, mount_flags;
854 clear_problem_context(&pctx);
857 * If the filesystem is opened read-only, or there is no
858 * journal, then do nothing.
860 if ((ctx->options & E2F_OPT_READONLY) ||
861 (sb->s_journal_inum == 0) ||
862 !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
866 * Read in the journal inode
868 if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
872 * If it's necessary to backup the journal inode, do so.
874 if ((sb->s_jnl_backup_type == 0) ||
875 ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
876 memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
877 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
878 memcpy(sb->s_jnl_blocks, inode.i_block,
880 sb->s_jnl_blocks[16] = inode.i_size;
881 sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
882 ext2fs_mark_super_dirty(fs);
883 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
888 * If the journal is already the hidden inode, then do nothing
890 if (sb->s_journal_inum == EXT2_JOURNAL_INO)
894 * The journal inode had better have only one link and not be readable.
896 if (inode.i_links_count != 1)
900 * If the filesystem is mounted, or we can't tell whether
901 * or not it's mounted, do nothing.
903 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
904 if (retval || (mount_flags & EXT2_MF_MOUNTED))
908 * If we can't find the name of the journal inode, then do
911 for (cpp = journal_names; *cpp; cpp++) {
912 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
913 strlen(*cpp), 0, &ino);
914 if ((retval == 0) && (ino == sb->s_journal_inum))
920 /* We need the inode bitmap to be loaded */
921 retval = ext2fs_read_bitmaps(fs);
926 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
930 * OK, we've done all the checks, let's actually move the
931 * journal inode. Errors at this point mean we need to force
932 * an ext2 filesystem check.
934 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
936 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
938 sb->s_journal_inum = EXT2_JOURNAL_INO;
939 ext2fs_mark_super_dirty(fs);
940 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
941 inode.i_links_count = 0;
942 inode.i_dtime = ctx->now;
943 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
946 group = ext2fs_group_of_ino(fs, ino);
947 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
948 ext2fs_mark_ib_dirty(fs);
949 fs->group_desc[group].bg_free_inodes_count++;
950 fs->super->s_free_inodes_count++;
954 pctx.errcode = retval;
955 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
956 fs->super->s_state &= ~EXT2_VALID_FS;
957 ext2fs_mark_super_dirty(fs);