From 773fd8a1d4202af708a37bdc7754134e6fdcfe22 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Sat, 6 Oct 2001 21:26:27 -0400 Subject: [PATCH] e2fsck.h, journal.c (e2fsck_move_ext3_journal): Add new function which will automatically relocate the ext3 journal from a visible file to an invisible journal file if the filesystem has been opened read/write. super.c (check_super_block): Add call to e2fsck_move_ext3_journal problem.c, problem.h (PR_0_MOVE_JOURNAL, PR_0_ERR_MOVE_JOURNAL): Add new problem codes. --- e2fsck/ChangeLog | 12 +++++++ e2fsck/e2fsck.h | 1 + e2fsck/journal.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ e2fsck/problem.c | 12 ++++++- e2fsck/problem.h | 6 ++++ e2fsck/super.c | 4 +++ 6 files changed, 134 insertions(+), 1 deletion(-) diff --git a/e2fsck/ChangeLog b/e2fsck/ChangeLog index 2a0481c..ad944da 100644 --- a/e2fsck/ChangeLog +++ b/e2fsck/ChangeLog @@ -1,3 +1,15 @@ +2001-09-20 Theodore Tso + + * e2fsck.h, journal.c (e2fsck_move_ext3_journal): Add new function + which will automatically relocate the ext3 journal from a + visible file to an invisible journal file if the + filesystem has been opened read/write. + + * super.c (check_super_block): Add call to e2fsck_move_ext3_journal + + * problem.c, problem.h (PR_0_MOVE_JOURNAL, PR_0_ERR_MOVE_JOURNAL): + Add new problem codes. + 2001-09-20 Theodore Tso * Release of E2fsprogs 1.25 diff --git a/e2fsck/e2fsck.h b/e2fsck/e2fsck.h index 0541ab1..35de50b 100644 --- a/e2fsck/e2fsck.h +++ b/e2fsck/e2fsck.h @@ -314,6 +314,7 @@ extern void ehandler_init(io_channel channel); /* journal.c */ extern int e2fsck_check_ext3_journal(e2fsck_t ctx); extern int e2fsck_run_ext3_journal(e2fsck_t ctx); +extern void e2fsck_move_ext3_journal(e2fsck_t ctx); /* pass1.c */ extern void e2fsck_use_inode_shortcuts(e2fsck_t ctx, int bool); diff --git a/e2fsck/journal.c b/e2fsck/journal.c index fd71921..0bff025 100644 --- a/e2fsck/journal.c +++ b/e2fsck/journal.c @@ -706,3 +706,103 @@ int e2fsck_run_ext3_journal(e2fsck_t ctx) e2fsck_clear_recover(ctx, recover_retval); return recover_retval; } + +/* + * This function will move the journal inode from a visible file in + * the filesystem directory hierarchy to the reserved inode if necessary. + */ +const static char * const journal_names[] = { + ".journal", "journal", ".journal.dat", "journal.dat", 0 }; + +void e2fsck_move_ext3_journal(e2fsck_t ctx) +{ + struct ext2_super_block *sb = ctx->fs->super; + struct problem_context pctx; + struct ext2_inode inode; + ext2_filsys fs = ctx->fs; + ext2_ino_t ino; + errcode_t retval; + const char * const * cpp; + int group, mount_flags; + + /* + * If the filesystem is opened read-only, or there is no + * journal, or the journal is already in the hidden inode, + * then do nothing. + */ + if ((ctx->options & E2F_OPT_READONLY) || + (sb->s_journal_inum == 0) || + (sb->s_journal_inum == EXT2_JOURNAL_INO) || + !(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) + return; + + /* + * If the filesystem is mounted, or we can't tell whether + * or not it's mounted, do nothing. + */ + retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags); + if (retval || (mount_flags & EXT2_MF_MOUNTED)) + return; + + /* + * If we can't find the name of the journal inode, then do + * nothing. + */ + for (cpp = journal_names; *cpp; cpp++) { + retval = ext2fs_lookup(fs, EXT2_ROOT_INO, *cpp, + strlen(*cpp), 0, &ino); + if ((retval == 0) && (ino == sb->s_journal_inum)) + break; + } + if (*cpp == 0) + return; + + /* + * The inode had better have only one link and not be readable. + */ + if (ext2fs_read_inode(fs, ino, &inode) != 0) + return; + if (inode.i_links_count != 1) + return; + + /* We need the inode bitmap to be loaded */ + retval = ext2fs_read_bitmaps(fs); + if (retval) + return; + + clear_problem_context(&pctx); + pctx.str = *cpp; + if (!fix_problem(ctx, PR_0_MOVE_JOURNAL, &pctx)) + return; + + /* + * OK, we've done all the checks, let's actually move the + * journal inode. Errors at this point mean we need to force + * an ext2 filesystem check. + */ + if ((retval = ext2fs_unlink(fs, EXT2_ROOT_INO, *cpp, ino, 0)) != 0) + goto err_out; + if ((retval = ext2fs_write_inode(fs, EXT2_JOURNAL_INO, &inode)) != 0) + goto err_out; + sb->s_journal_inum = EXT2_JOURNAL_INO; + ext2fs_mark_super_dirty(fs); + inode.i_links_count = 0; + inode.i_dtime = time(0); + if ((retval = ext2fs_write_inode(fs, ino, &inode)) != 0) + goto err_out; + + group = ext2fs_group_of_ino(fs, ino); + ext2fs_unmark_inode_bitmap(fs->inode_map, ino); + ext2fs_mark_ib_dirty(fs); + fs->group_desc[group].bg_free_inodes_count++; + fs->super->s_free_inodes_count++; + return; + +err_out: + pctx.errcode = retval; + fix_problem(ctx, PR_0_ERR_MOVE_JOURNAL, &pctx); + fs->super->s_state &= ~EXT2_VALID_FS; + ext2fs_mark_super_dirty(fs); + return; +} + diff --git a/e2fsck/problem.c b/e2fsck/problem.c index 8044e67..34f4f30 100644 --- a/e2fsck/problem.c +++ b/e2fsck/problem.c @@ -276,7 +276,17 @@ static const struct e2fsck_problem problem_table[] = { { PR_0_JOURNAL_UNSUPP_VERSION, N_("@j version not supported by this e2fsck.\n"), PROMPT_ABORT, 0 }, - + + /* Moving journal to hidden file */ + { PR_0_MOVE_JOURNAL, + N_("Moving @j from /%s to hidden inode.\n\n"), + PROMPT_NONE, 0 }, + + /* Error moving journal to hidden file */ + { PR_0_ERR_MOVE_JOURNAL, + N_("Error moving @j: %m\n\n"), + PROMPT_NONE, 0 }, + /* Pass 1 errors */ /* Pass 1: Checking inodes, blocks, and sizes */ diff --git a/e2fsck/problem.h b/e2fsck/problem.h index 6792ecb..e7ddf76 100644 --- a/e2fsck/problem.h +++ b/e2fsck/problem.h @@ -155,6 +155,12 @@ struct problem_context { /* Journal has unsupported version number */ #define PR_0_JOURNAL_UNSUPP_VERSION 0x000027 +/* Moving journal to hidden file */ +#define PR_0_MOVE_JOURNAL 0x000028 + +/* Error moving journal */ +#define PR_0_ERR_MOVE_JOURNAL 0x000029 + /* * Pass 1 errors */ diff --git a/e2fsck/super.c b/e2fsck/super.c index ceac78b..43773ae 100644 --- a/e2fsck/super.c +++ b/e2fsck/super.c @@ -509,6 +509,10 @@ void check_super_block(e2fsck_t ctx) ext2fs_mark_super_dirty(fs); } + /* + * Move the ext3 journal file, if necessary. + */ + e2fsck_move_ext3_journal(ctx); return; } -- 1.8.3.1