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 #ifdef CONFIG_JBD_DEBUG
77 if (journal_enable_debug >= 3)
80 jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
81 (unsigned long) blocknr, blocksize, bh_count);
83 bh->b_ctx = kdev->k_ctx;
84 if (kdev->k_dev == K_DEV_FS)
85 bh->b_io = kdev->k_ctx->fs->io;
87 bh->b_io = kdev->k_ctx->journal_io;
88 bh->b_size = blocksize;
89 bh->b_blocknr = blocknr;
94 void sync_blockdev(kdev_t kdev)
98 if (kdev->k_dev == K_DEV_FS)
99 io = kdev->k_ctx->fs->io;
101 io = kdev->k_ctx->journal_io;
103 io_channel_flush(io);
106 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
109 struct buffer_head *bh;
111 for (; nr > 0; --nr) {
113 if (rw == READ && !bh->b_uptodate) {
114 jfs_debug(3, "reading block %lu/%p\n",
115 (unsigned long) bh->b_blocknr, (void *) bh);
116 retval = io_channel_read_blk(bh->b_io,
120 com_err(bh->b_ctx->device_name, retval,
121 "while reading block %lu\n",
122 (unsigned long) bh->b_blocknr);
127 } else if (rw == WRITE && bh->b_dirty) {
128 jfs_debug(3, "writing block %lu/%p\n",
129 (unsigned long) bh->b_blocknr, (void *) bh);
130 retval = io_channel_write_blk(bh->b_io,
134 com_err(bh->b_ctx->device_name, retval,
135 "while writing block %lu\n",
136 (unsigned long) bh->b_blocknr);
143 jfs_debug(3, "no-op %s for block %lu\n",
144 rw == READ ? "read" : "write",
145 (unsigned long) bh->b_blocknr);
150 void mark_buffer_dirty(struct buffer_head *bh)
155 static void mark_buffer_clean(struct buffer_head * bh)
160 void brelse(struct buffer_head *bh)
163 ll_rw_block(WRITE, 1, &bh);
164 jfs_debug(3, "freeing block %lu/%p (total %d)\n",
165 (unsigned long) bh->b_blocknr, (void *) bh, --bh_count);
166 ext2fs_free_mem(&bh);
169 int buffer_uptodate(struct buffer_head *bh)
171 return bh->b_uptodate;
174 void mark_buffer_uptodate(struct buffer_head *bh, int val)
176 bh->b_uptodate = val;
179 void wait_on_buffer(struct buffer_head *bh)
182 ll_rw_block(READ, 1, &bh);
186 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
188 ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
190 /* if we had an error doing journal recovery, we need a full fsck */
192 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
193 ext2fs_mark_super_dirty(ctx->fs);
197 * This is a helper function to check the validity of the journal.
199 struct process_block_struct {
200 e2_blkcnt_t last_block;
203 static int process_journal_block(ext2_filsys fs,
205 e2_blkcnt_t blockcnt,
206 blk_t ref_block EXT2FS_ATTR((unused)),
207 int ref_offset EXT2FS_ATTR((unused)),
210 struct process_block_struct *p;
211 blk_t blk = *block_nr;
213 p = (struct process_block_struct *) priv_data;
215 if (blk < fs->super->s_first_data_block ||
216 blk >= fs->super->s_blocks_count)
220 p->last_block = blockcnt;
224 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **ret_journal)
226 struct process_block_struct pb;
227 struct ext2_super_block *sb = ctx->fs->super;
228 struct ext2_super_block jsuper;
229 struct problem_context pctx;
230 struct buffer_head *bh;
231 struct inode *j_inode = NULL;
232 struct kdev_s *dev_fs = NULL, *dev_journal;
233 const char *journal_name = 0;
234 journal_t *journal = NULL;
235 errcode_t retval = 0;
236 io_manager io_ptr = 0;
237 unsigned long start = 0;
239 int tried_backup_jnl = 0;
241 clear_problem_context(&pctx);
243 journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
245 return EXT2_ET_NO_MEMORY;
248 dev_fs = e2fsck_allocate_memory(ctx, 2*sizeof(struct kdev_s), "kdev");
250 retval = EXT2_ET_NO_MEMORY;
253 dev_journal = dev_fs+1;
255 dev_fs->k_ctx = dev_journal->k_ctx = ctx;
256 dev_fs->k_dev = K_DEV_FS;
257 dev_journal->k_dev = K_DEV_JOURNAL;
259 journal->j_dev = dev_journal;
260 journal->j_fs_dev = dev_fs;
261 journal->j_inode = NULL;
262 journal->j_blocksize = ctx->fs->blocksize;
264 if (uuid_is_null(sb->s_journal_uuid)) {
265 if (!sb->s_journal_inum) {
266 retval = EXT2_ET_BAD_INODE_NUM;
269 j_inode = e2fsck_allocate_memory(ctx, sizeof(*j_inode),
272 retval = EXT2_ET_NO_MEMORY;
276 j_inode->i_ctx = ctx;
277 j_inode->i_ino = sb->s_journal_inum;
279 if ((retval = ext2fs_read_inode(ctx->fs,
281 &j_inode->i_ext2))) {
283 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
286 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
287 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
289 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
290 j_inode->i_ext2.i_links_count = 1;
291 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
292 e2fsck_use_inode_shortcuts(ctx, 1);
293 ctx->stashed_ino = j_inode->i_ino;
294 ctx->stashed_inode = &j_inode->i_ext2;
297 if (!j_inode->i_ext2.i_links_count ||
298 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
299 retval = EXT2_ET_NO_JOURNAL;
300 goto try_backup_journal;
302 if (j_inode->i_ext2.i_size / journal->j_blocksize <
303 JFS_MIN_JOURNAL_BLOCKS) {
304 retval = EXT2_ET_JOURNAL_TOO_SMALL;
305 goto try_backup_journal;
308 retval = ext2fs_block_iterate2(ctx->fs, j_inode->i_ino,
310 process_journal_block, &pb);
311 if ((pb.last_block+1) * ctx->fs->blocksize <
312 j_inode->i_ext2.i_size) {
313 retval = EXT2_ET_JOURNAL_TOO_SMALL;
314 goto try_backup_journal;
316 if (tried_backup_jnl && !(ctx->options & E2F_OPT_READONLY)) {
317 retval = ext2fs_write_inode(ctx->fs, sb->s_journal_inum,
323 journal->j_maxlen = j_inode->i_ext2.i_size / journal->j_blocksize;
326 retval = ext2fs_inode_io_intern2(ctx->fs, sb->s_journal_inum,
332 io_ptr = inode_io_manager;
334 journal->j_inode = j_inode;
335 ctx->journal_io = ctx->fs->io;
336 if ((retval = journal_bmap(journal, 0, &start)) != 0)
341 if (!ctx->journal_name) {
344 uuid_unparse(sb->s_journal_uuid, uuid);
345 ctx->journal_name = blkid_get_devname(ctx->blkid,
347 if (!ctx->journal_name)
348 ctx->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
350 journal_name = ctx->journal_name;
353 fix_problem(ctx, PR_0_CANT_FIND_JOURNAL, &pctx);
354 retval = EXT2_ET_LOAD_EXT_JOURNAL;
358 jfs_debug(1, "Using journal file %s\n", journal_name);
359 io_ptr = unix_io_manager;
363 test_io_backing_manager = io_ptr;
364 io_ptr = test_io_manager;
369 retval = io_ptr->open(journal_name, IO_FLAG_RW,
374 io_channel_set_blksize(ctx->journal_io, ctx->fs->blocksize);
377 if (ctx->fs->blocksize == 1024)
379 bh = getblk(dev_journal, start, ctx->fs->blocksize);
381 retval = EXT2_ET_NO_MEMORY;
384 ll_rw_block(READ, 1, &bh);
385 if ((retval = bh->b_err) != 0) {
389 memcpy(&jsuper, start ? bh->b_data : bh->b_data + 1024,
392 #ifdef WORDS_BIGENDIAN
393 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
394 ext2fs_swap_super(&jsuper);
396 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
397 !(jsuper.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
398 fix_problem(ctx, PR_0_EXT_JOURNAL_BAD_SUPER, &pctx);
399 retval = EXT2_ET_LOAD_EXT_JOURNAL;
402 /* Make sure the journal UUID is correct */
403 if (memcmp(jsuper.s_uuid, ctx->fs->super->s_journal_uuid,
404 sizeof(jsuper.s_uuid))) {
405 fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx);
406 retval = EXT2_ET_LOAD_EXT_JOURNAL;
410 journal->j_maxlen = jsuper.s_blocks_count;
414 if (!(bh = getblk(dev_journal, start, journal->j_blocksize))) {
415 retval = EXT2_ET_NO_MEMORY;
419 journal->j_sb_buffer = bh;
420 journal->j_superblock = (journal_superblock_t *)bh->b_data;
424 ext2fs_free_mem(&j_inode);
427 *ret_journal = journal;
428 e2fsck_use_inode_shortcuts(ctx, 0);
432 e2fsck_use_inode_shortcuts(ctx, 0);
434 ext2fs_free_mem(&dev_fs);
436 ext2fs_free_mem(&j_inode);
438 ext2fs_free_mem(&journal);
442 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
443 struct problem_context *pctx)
445 struct ext2_super_block *sb = ctx->fs->super;
446 int recover = ctx->fs->super->s_feature_incompat &
447 EXT3_FEATURE_INCOMPAT_RECOVER;
448 int has_journal = ctx->fs->super->s_feature_compat &
449 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
451 if (has_journal || sb->s_journal_inum) {
452 /* The journal inode is bogus, remove and force full fsck */
453 pctx->ino = sb->s_journal_inum;
454 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
455 if (has_journal && sb->s_journal_inum)
456 printf("*** ext3 journal has been deleted - "
457 "filesystem is now ext2 only ***\n\n");
458 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
459 sb->s_journal_inum = 0;
460 ctx->flags |= E2F_FLAG_JOURNAL_INODE;
461 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
462 e2fsck_clear_recover(ctx, 1);
465 return EXT2_ET_BAD_INODE_NUM;
466 } else if (recover) {
467 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
468 e2fsck_clear_recover(ctx, 1);
471 return EXT2_ET_UNSUPP_FEATURE;
476 #define V1_SB_SIZE 0x0024
477 static void clear_v2_journal_fields(journal_t *journal)
479 e2fsck_t ctx = journal->j_dev->k_ctx;
480 struct problem_context pctx;
482 clear_problem_context(&pctx);
484 if (!fix_problem(ctx, PR_0_CLEAR_V2_JOURNAL, &pctx))
487 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
488 ctx->fs->blocksize-V1_SB_SIZE);
489 mark_buffer_dirty(journal->j_sb_buffer);
493 static errcode_t e2fsck_journal_load(journal_t *journal)
495 e2fsck_t ctx = journal->j_dev->k_ctx;
496 journal_superblock_t *jsb;
497 struct buffer_head *jbh = journal->j_sb_buffer;
498 struct problem_context pctx;
500 clear_problem_context(&pctx);
502 ll_rw_block(READ, 1, &jbh);
504 com_err(ctx->device_name, jbh->b_err,
505 _("reading journal superblock\n"));
509 jsb = journal->j_superblock;
510 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
511 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
512 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
514 switch (ntohl(jsb->s_header.h_blocktype)) {
515 case JFS_SUPERBLOCK_V1:
516 journal->j_format_version = 1;
517 if (jsb->s_feature_compat ||
518 jsb->s_feature_incompat ||
519 jsb->s_feature_ro_compat ||
521 clear_v2_journal_fields(journal);
524 case JFS_SUPERBLOCK_V2:
525 journal->j_format_version = 2;
526 if (ntohl(jsb->s_nr_users) > 1 &&
527 uuid_is_null(ctx->fs->super->s_journal_uuid))
528 clear_v2_journal_fields(journal);
529 if (ntohl(jsb->s_nr_users) > 1) {
530 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_MULTIFS, &pctx);
531 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
536 * These should never appear in a journal super block, so if
537 * they do, the journal is badly corrupted.
539 case JFS_DESCRIPTOR_BLOCK:
540 case JFS_COMMIT_BLOCK:
541 case JFS_REVOKE_BLOCK:
542 return EXT2_ET_CORRUPT_SUPERBLOCK;
544 /* If we don't understand the superblock major type, but there
545 * is a magic number, then it is likely to be a new format we
546 * just don't understand, so leave it alone. */
548 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
551 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES))
552 return EXT2_ET_UNSUPP_FEATURE;
554 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES))
555 return EXT2_ET_RO_UNSUPP_FEATURE;
557 /* We have now checked whether we know enough about the journal
558 * format to be able to proceed safely, so any other checks that
559 * fail we should attempt to recover from. */
560 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
561 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
562 _("%s: no valid journal superblock found\n"),
564 return EXT2_ET_CORRUPT_SUPERBLOCK;
567 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
568 journal->j_maxlen = ntohl(jsb->s_maxlen);
569 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
570 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
571 _("%s: journal too short\n"),
573 return EXT2_ET_CORRUPT_SUPERBLOCK;
576 journal->j_tail_sequence = ntohl(jsb->s_sequence);
577 journal->j_transaction_sequence = journal->j_tail_sequence;
578 journal->j_tail = ntohl(jsb->s_start);
579 journal->j_first = ntohl(jsb->s_first);
580 journal->j_last = ntohl(jsb->s_maxlen);
585 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
596 /* Leave a valid existing V1 superblock signature alone.
597 * Anything unrecognisable we overwrite with a new V2
600 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
601 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
602 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
603 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
606 /* Zero out everything else beyond the superblock header */
608 p = ((char *) jsb) + sizeof(journal_header_t);
609 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
611 jsb->s_blocksize = htonl(ctx->fs->blocksize);
612 jsb->s_maxlen = htonl(journal->j_maxlen);
613 jsb->s_first = htonl(1);
615 /* Initialize the journal sequence number so that there is "no"
616 * chance we will find old "valid" transactions in the journal.
617 * This avoids the need to zero the whole journal (slow to do,
618 * and risky when we are just recovering the filesystem).
620 uuid_generate(u.uuid);
621 for (i = 0; i < 4; i ++)
623 jsb->s_sequence = htonl(new_seq);
625 mark_buffer_dirty(journal->j_sb_buffer);
626 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
629 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
631 struct problem_context *pctx)
633 struct ext2_super_block *sb = ctx->fs->super;
634 int recover = ctx->fs->super->s_feature_incompat &
635 EXT3_FEATURE_INCOMPAT_RECOVER;
637 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
638 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
639 e2fsck_journal_reset_super(ctx, journal->j_superblock,
641 journal->j_transaction_sequence = 1;
642 e2fsck_clear_recover(ctx, recover);
645 return EXT2_ET_CORRUPT_SUPERBLOCK;
646 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
647 return EXT2_ET_CORRUPT_SUPERBLOCK;
652 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
655 journal_superblock_t *jsb;
658 mark_buffer_clean(journal->j_sb_buffer);
659 else if (!(ctx->options & E2F_OPT_READONLY)) {
660 jsb = journal->j_superblock;
661 jsb->s_sequence = htonl(journal->j_transaction_sequence);
663 jsb->s_start = 0; /* this marks the journal as empty */
664 mark_buffer_dirty(journal->j_sb_buffer);
666 brelse(journal->j_sb_buffer);
668 if (ctx->journal_io) {
669 if (ctx->fs && ctx->fs->io != ctx->journal_io)
670 io_channel_close(ctx->journal_io);
675 if (journal->j_inode)
676 ext2fs_free_mem(&journal->j_inode);
678 if (journal->j_fs_dev)
679 ext2fs_free_mem(&journal->j_fs_dev);
680 ext2fs_free_mem(&journal);
684 * This function makes sure that the superblock fields regarding the
685 * journal are consistent.
687 int e2fsck_check_ext3_journal(e2fsck_t ctx)
689 struct ext2_super_block *sb = ctx->fs->super;
691 int recover = ctx->fs->super->s_feature_incompat &
692 EXT3_FEATURE_INCOMPAT_RECOVER;
693 struct problem_context pctx;
695 int reset = 0, force_fsck = 0;
698 /* If we don't have any journal features, don't do anything more */
699 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
700 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
701 uuid_is_null(sb->s_journal_uuid))
704 clear_problem_context(&pctx);
705 pctx.num = sb->s_journal_inum;
707 retval = e2fsck_get_journal(ctx, &journal);
709 if ((retval == EXT2_ET_BAD_INODE_NUM) ||
710 (retval == EXT2_ET_BAD_BLOCK_NUM) ||
711 (retval == EXT2_ET_JOURNAL_TOO_SMALL) ||
712 (retval == EXT2_ET_NO_JOURNAL))
713 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
717 retval = e2fsck_journal_load(journal);
719 if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
720 ((retval == EXT2_ET_UNSUPP_FEATURE) &&
721 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_INCOMPAT,
723 ((retval == EXT2_ET_RO_UNSUPP_FEATURE) &&
724 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_ROCOMPAT,
726 ((retval == EXT2_ET_JOURNAL_UNSUPP_VERSION) &&
727 (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_VERSION, &pctx))))
728 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
730 e2fsck_journal_release(ctx, journal, 0, 1);
735 * We want to make the flags consistent here. We will not leave with
736 * needs_recovery set but has_journal clear. We can't get in a loop
737 * with -y, -n, or -p, only if a user isn't making up their mind.
740 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
741 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
743 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
745 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
748 * Need a full fsck if we are releasing a
749 * journal stored on a reserved inode.
751 force_fsck = recover ||
752 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
753 /* Clear all of the journal fields */
754 sb->s_journal_inum = 0;
755 sb->s_journal_dev = 0;
756 memset(sb->s_journal_uuid, 0,
757 sizeof(sb->s_journal_uuid));
758 e2fsck_clear_recover(ctx, force_fsck);
759 } else if (!(ctx->options & E2F_OPT_READONLY)) {
760 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
761 ctx->fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
762 ext2fs_mark_super_dirty(ctx->fs);
766 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
767 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
768 journal->j_superblock->s_start != 0) {
769 /* Print status information */
770 fix_problem(ctx, PR_0_JOURNAL_RECOVERY_CLEAR, &pctx);
772 problem = PR_0_JOURNAL_RUN_DEFAULT;
774 problem = PR_0_JOURNAL_RUN;
775 if (fix_problem(ctx, problem, &pctx)) {
776 ctx->options |= E2F_OPT_FORCE;
777 sb->s_feature_incompat |=
778 EXT3_FEATURE_INCOMPAT_RECOVER;
779 ext2fs_mark_super_dirty(ctx->fs);
780 } else if (fix_problem(ctx,
781 PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
783 sb->s_state &= ~EXT2_VALID_FS;
784 ext2fs_mark_super_dirty(ctx->fs);
787 * If the user answers no to the above question, we
788 * ignore the fact that journal apparently has data;
789 * accidentally replaying over valid data would be far
790 * worse than skipping a questionable recovery.
792 * XXX should we abort with a fatal error here? What
793 * will the ext3 kernel code do if a filesystem with
794 * !NEEDS_RECOVERY but with a non-zero
795 * journal->j_superblock->s_start is mounted?
799 e2fsck_journal_release(ctx, journal, reset, 0);
803 static errcode_t recover_ext3_journal(e2fsck_t ctx)
805 struct problem_context pctx;
809 clear_problem_context(&pctx);
811 journal_init_revoke_caches();
812 retval = e2fsck_get_journal(ctx, &journal);
816 retval = e2fsck_journal_load(journal);
820 retval = journal_init_revoke(journal, 1024);
824 retval = -journal_recover(journal);
828 if (journal->j_failed_commit) {
829 pctx.ino = journal->j_failed_commit;
830 fix_problem(ctx, PR_0_JNL_TXN_CORRUPT, &pctx);
831 ctx->fs->super->s_state |= EXT2_ERROR_FS;
832 ext2fs_mark_super_dirty(ctx->fs);
836 if (journal->j_superblock->s_errno) {
837 ctx->fs->super->s_state |= EXT2_ERROR_FS;
838 ext2fs_mark_super_dirty(ctx->fs);
839 journal->j_superblock->s_errno = 0;
840 mark_buffer_dirty(journal->j_sb_buffer);
844 journal_destroy_revoke(journal);
845 journal_destroy_revoke_caches();
846 e2fsck_journal_release(ctx, journal, 1, 0);
850 int e2fsck_run_ext3_journal(e2fsck_t ctx)
852 io_manager io_ptr = ctx->fs->io->manager;
853 int blocksize = ctx->fs->blocksize;
854 errcode_t retval, recover_retval;
856 printf(_("%s: recovering journal\n"), ctx->device_name);
857 if (ctx->options & E2F_OPT_READONLY) {
858 printf(_("%s: won't do journal recovery while read-only\n"),
860 return EXT2_ET_FILE_RO;
863 if (ctx->fs->flags & EXT2_FLAG_DIRTY)
864 ext2fs_flush(ctx->fs); /* Force out any modifications */
866 recover_retval = recover_ext3_journal(ctx);
869 * Reload the filesystem context to get up-to-date data from disk
870 * because journal recovery will change the filesystem under us.
872 ext2fs_close(ctx->fs);
873 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
874 ctx->superblock, blocksize, io_ptr,
878 com_err(ctx->program_name, retval,
879 _("while trying to re-open %s"),
883 ctx->fs->priv_data = ctx;
884 ctx->fs->now = ctx->now;
885 ctx->fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
887 /* Set the superblock flags */
888 e2fsck_clear_recover(ctx, recover_retval);
889 return recover_retval;
893 * This function will move the journal inode from a visible file in
894 * the filesystem directory hierarchy to the reserved inode if necessary.
896 static const char * const journal_names[] = {
897 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
899 void e2fsck_move_ext3_journal(e2fsck_t ctx)
901 struct ext2_super_block *sb = ctx->fs->super;
902 struct problem_context pctx;
903 struct ext2_inode inode;
904 ext2_filsys fs = ctx->fs;
907 const char * const * cpp;
908 int group, mount_flags;
910 clear_problem_context(&pctx);
913 * If the filesystem is opened read-only, or there is no
914 * journal, then do nothing.
916 if ((ctx->options & E2F_OPT_READONLY) ||
917 (sb->s_journal_inum == 0) ||
918 !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
922 * Read in the journal inode
924 if (ext2fs_read_inode(fs, sb->s_journal_inum, &inode) != 0)
928 * If it's necessary to backup the journal inode, do so.
930 if ((sb->s_jnl_backup_type == 0) ||
931 ((sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS) &&
932 memcmp(inode.i_block, sb->s_jnl_blocks, EXT2_N_BLOCKS*4))) {
933 if (fix_problem(ctx, PR_0_BACKUP_JNL, &pctx)) {
934 memcpy(sb->s_jnl_blocks, inode.i_block,
936 sb->s_jnl_blocks[16] = inode.i_size;
937 sb->s_jnl_backup_type = EXT3_JNL_BACKUP_BLOCKS;
938 ext2fs_mark_super_dirty(fs);
939 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
944 * If the journal is already the hidden inode, then do nothing
946 if (sb->s_journal_inum == EXT2_JOURNAL_INO)
950 * The journal inode had better have only one link and not be readable.
952 if (inode.i_links_count != 1)
956 * If the filesystem is mounted, or we can't tell whether
957 * or not it's mounted, do nothing.
959 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
960 if (retval || (mount_flags & EXT2_MF_MOUNTED))
964 * If we can't find the name of the journal inode, then do
967 for (cpp = journal_names; *cpp; cpp++) {
968 retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp,
969 strlen(*cpp), 0, &ino);
970 if ((retval == 0) && (ino == sb->s_journal_inum))
976 /* We need the inode bitmap to be loaded */
977 retval = ext2fs_read_bitmaps(fs);
982 if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx))
986 * OK, we've done all the checks, let's actually move the
987 * journal inode. Errors at this point mean we need to force
988 * an ext2 filesystem check.
990 if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0)
992 if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0)
994 sb->s_journal_inum = EXT2_JOURNAL_INO;
995 ext2fs_mark_super_dirty(fs);
996 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
997 inode.i_links_count = 0;
998 inode.i_dtime = ctx->now;
999 if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0)
1002 group = ext2fs_group_of_ino(fs, ino);
1003 ext2fs_unmark_inode_bitmap(fs->inode_map, ino);
1004 ext2fs_mark_ib_dirty(fs);
1005 fs->group_desc[group].bg_free_inodes_count++;
1006 ext2fs_group_desc_csum_set(fs, group);
1007 fs->super->s_free_inodes_count++;
1011 pctx.errcode = retval;
1012 fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx);
1013 fs->super->s_state &= ~EXT2_VALID_FS;
1014 ext2fs_mark_super_dirty(fs);
1019 * This function makes sure the superblock hint for the external
1020 * journal is correct.
1022 int e2fsck_fix_ext3_journal_hint(e2fsck_t ctx)
1024 struct ext2_super_block *sb = ctx->fs->super;
1025 struct problem_context pctx;
1026 char uuid[37], *journal_name;
1029 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) ||
1030 uuid_is_null(sb->s_journal_uuid))
1033 uuid_unparse(sb->s_journal_uuid, uuid);
1034 journal_name = blkid_get_devname(ctx->blkid, "UUID", uuid);
1038 if (stat(journal_name, &st) < 0)
1041 if (st.st_rdev != sb->s_journal_dev) {
1042 clear_problem_context(&pctx);
1043 pctx.num = st.st_rdev;
1044 if (fix_problem(ctx, PR_0_EXTERNAL_JOURNAL_HINT, &pctx)) {
1045 sb->s_journal_dev = st.st_rdev;
1046 ext2fs_mark_super_dirty(ctx->fs);