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 JFS_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 bmap(struct inode *inode, int block)
42 retval = ext2fs_bmap(inode->i_ctx->fs, inode->i_ino, &inode->i_ext2,
43 NULL, 0, block, &phys);
46 com_err(inode->i_ctx->device_name, retval,
47 _("bmap journal inode %ld, block %d\n"),
53 struct buffer_head *getblk(e2fsck_t ctx, blk_t blocknr, int blocksize)
55 struct buffer_head *bh;
57 bh = e2fsck_allocate_memory(ctx, sizeof(*bh), "block buffer");
61 jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
62 (unsigned long) blocknr, blocksize, ++bh_count);
65 bh->b_size = blocksize;
66 bh->b_blocknr = blocknr;
71 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
74 struct buffer_head *bh;
76 for (; nr > 0; --nr) {
78 if (rw == READ && !bh->b_uptodate) {
79 jfs_debug(3, "reading block %lu/%p\n",
80 (unsigned long) bh->b_blocknr, (void *) bh);
81 retval = io_channel_read_blk(bh->b_ctx->fs->io,
85 com_err(bh->b_ctx->device_name, retval,
86 "while reading block %ld\n",
92 } else if (rw == WRITE && bh->b_dirty) {
93 jfs_debug(3, "writing block %lu/%p\n",
94 (unsigned long) bh->b_blocknr, (void *) bh);
95 retval = io_channel_write_blk(bh->b_ctx->fs->io,
99 com_err(bh->b_ctx->device_name, retval,
100 "while writing block %ld\n",
108 jfs_debug(3, "no-op %s for block %lu\n",
109 rw == READ ? "read" : "write",
110 (unsigned long) bh->b_blocknr);
114 void mark_buffer_dirty(struct buffer_head *bh, int dummy)
116 bh->b_dirty = dummy | 1; /* use dummy to avoid unused variable */
119 static void mark_buffer_clean(struct buffer_head * bh)
124 void brelse(struct buffer_head *bh)
127 ll_rw_block(WRITE, 1, &bh);
128 jfs_debug(3, "freeing block %lu/%p (total %d)\n",
129 (unsigned long) bh->b_blocknr, (void *) bh, --bh_count);
130 ext2fs_free_mem((void **) &bh);
133 int buffer_uptodate(struct buffer_head *bh)
135 return bh->b_uptodate;
138 void mark_buffer_uptodate(struct buffer_head *bh, int val)
140 bh->b_uptodate = val;
143 void wait_on_buffer(struct buffer_head *bh)
146 ll_rw_block(READ, 1, &bh);
150 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
152 ctx->fs->super->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
154 /* if we had an error doing journal recovery, we need a full fsck */
156 ctx->fs->super->s_state &= ~EXT2_VALID_FS;
157 ext2fs_mark_super_dirty(ctx->fs);
160 static errcode_t e2fsck_journal_init_inode(e2fsck_t ctx,
161 struct ext2_super_block *s,
162 ext2_ino_t journal_inum,
166 struct buffer_head *bh;
170 jfs_debug(1, "Using journal inode %u\n", journal_inum);
171 *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
173 return EXT2_ET_NO_MEMORY;
176 inode = e2fsck_allocate_memory(ctx, sizeof(*inode), "journal inode");
178 retval = EXT2_ET_NO_MEMORY;
183 inode->i_ino = journal_inum;
184 retval = ext2fs_read_inode(ctx->fs, journal_inum, &inode->i_ext2);
188 (*journal)->j_dev = ctx;
189 (*journal)->j_inode = inode;
190 (*journal)->j_blocksize = ctx->fs->blocksize;
191 (*journal)->j_maxlen = inode->i_ext2.i_size / (*journal)->j_blocksize;
193 if (!inode->i_ext2.i_links_count ||
194 !LINUX_S_ISREG(inode->i_ext2.i_mode) ||
195 (*journal)->j_maxlen < JFS_MIN_JOURNAL_BLOCKS ||
196 (start = bmap(inode, 0)) == 0) {
197 retval = EXT2_ET_BAD_INODE_NUM;
201 bh = getblk(ctx, start, (*journal)->j_blocksize);
203 retval = EXT2_ET_NO_MEMORY;
206 (*journal)->j_sb_buffer = bh;
207 (*journal)->j_superblock = (journal_superblock_t *)bh->b_data;
212 ext2fs_free_mem((void **)&inode);
214 ext2fs_free_mem((void **)journal);
219 static errcode_t e2fsck_get_journal(e2fsck_t ctx, journal_t **journal)
222 struct problem_context pctx;
223 struct ext2_super_block *sb = ctx->fs->super;
225 clear_problem_context(&pctx);
227 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
228 /* FIXME: check if dev is valid block dev, has a journal */
229 if (sb->s_journal_dev) {
230 pctx.num = sb->s_journal_dev;
231 /* this problem aborts on -y, -p, unsupported on -n */
232 if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_DEV, &pctx))
233 return EXT2_ET_UNSUPP_FEATURE;
234 sb->s_journal_dev = 0;
235 sb->s_state &= ~EXT2_VALID_FS;
236 ext2fs_mark_super_dirty(ctx->fs);
238 /* FIXME: check if UUID is valid block dev, has a journal */
239 if (!uuid_is_null(sb->s_journal_uuid)) {
240 uuid_unparse(sb->s_journal_uuid, uuid_str);
242 /* this problem aborts on -y, -p, unsupported on -n */
243 if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_UUID, &pctx))
244 return EXT2_ET_UNSUPP_FEATURE;
245 uuid_clear(sb->s_journal_uuid);
246 sb->s_state &= ~EXT2_VALID_FS;
247 ext2fs_mark_super_dirty(ctx->fs);
249 if (!sb->s_journal_inum)
250 return EXT2_ET_BAD_INODE_NUM;
253 if (sb->s_journal_dev) {
254 pctx.num = sb->s_journal_dev;
255 if (!fix_problem(ctx, PR_0_JOURNAL_BAD_DEV, &pctx))
256 return EXT2_ET_UNSUPP_FEATURE;
257 sb->s_journal_dev = 0;
258 sb->s_state &= ~EXT2_VALID_FS;
259 ext2fs_mark_super_dirty(ctx->fs);
261 if (!uuid_is_null(sb->s_journal_uuid)) {
262 uuid_unparse(sb->s_journal_uuid, uuid_str);
264 if (!fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx))
265 return EXT2_ET_UNSUPP_FEATURE;
266 uuid_clear(sb->s_journal_uuid);
267 sb->s_state &= ~EXT2_VALID_FS;
268 ext2fs_mark_super_dirty(ctx->fs);
271 return e2fsck_journal_init_inode(ctx, sb, sb->s_journal_inum, journal);
274 static errcode_t e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
275 struct problem_context *pctx)
277 struct ext2_super_block *sb = ctx->fs->super;
278 int recover = ctx->fs->super->s_feature_incompat &
279 EXT3_FEATURE_INCOMPAT_RECOVER;
280 int has_journal = ctx->fs->super->s_feature_compat &
281 EXT3_FEATURE_COMPAT_HAS_JOURNAL;
283 if (has_journal || sb->s_journal_inum) {
284 /* The journal inode is bogus, remove and force full fsck */
285 pctx->ino = sb->s_journal_inum;
286 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
287 if (has_journal && sb->s_journal_inum)
288 printf("*** ext3 journal has been deleted - "
289 "filesystem is now ext2 only ***\n\n");
290 sb->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
291 sb->s_journal_inum = 0;
292 e2fsck_clear_recover(ctx, 1);
295 return EXT2_ET_BAD_INODE_NUM;
296 } else if (recover) {
297 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
298 e2fsck_clear_recover(ctx, 1);
301 return EXT2_ET_UNSUPP_FEATURE;
306 static errcode_t e2fsck_journal_load(journal_t *journal)
308 e2fsck_t ctx = journal->j_dev;
309 journal_superblock_t *jsb;
310 struct buffer_head *jbh = journal->j_sb_buffer;
311 struct problem_context pctx;
313 clear_problem_context(&pctx);
315 ll_rw_block(READ, 1, &jbh);
317 com_err(ctx->device_name, jbh->b_err,
318 _("reading journal superblock\n"));
322 jsb = journal->j_superblock;
323 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
324 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
325 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
327 switch (ntohl(jsb->s_header.h_blocktype)) {
328 case JFS_SUPERBLOCK_V1:
329 journal->j_format_version = 1;
332 case JFS_SUPERBLOCK_V2:
333 journal->j_format_version = 2;
336 /* If we don't understand the superblock major type, but there
337 * is a magic number, then it is likely to be a new format we
338 * just don't understand, so leave it alone. */
340 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
341 _("%s: journal has unrecognised format\n"),
343 return EXT2_ET_UNSUPP_FEATURE;
346 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES)) {
347 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
348 _("%s: journal has incompatible features\n"),
350 return EXT2_ET_UNSUPP_FEATURE;
353 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES)) {
354 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
355 _("%s: journal has readonly-incompatible features\n"),
357 return EXT2_ET_RO_UNSUPP_FEATURE;
360 /* We have now checked whether we know enough about the journal
361 * format to be able to proceed safely, so any other checks that
362 * fail we should attempt to recover from. */
363 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
364 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
365 _("%s: no valid journal superblock found\n"),
367 return EXT2_ET_CORRUPT_SUPERBLOCK;
370 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
371 journal->j_maxlen = ntohl(jsb->s_maxlen);
372 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
373 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
374 _("%s: journal too short\n"),
376 return EXT2_ET_CORRUPT_SUPERBLOCK;
379 journal->j_tail_sequence = ntohl(jsb->s_sequence);
380 journal->j_transaction_sequence = journal->j_tail_sequence;
381 journal->j_tail = ntohl(jsb->s_start);
382 journal->j_first = ntohl(jsb->s_first);
383 journal->j_last = ntohl(jsb->s_maxlen);
388 static void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
393 /* Leave a valid existing V1 superblock signature alone.
394 * Anything unrecognisable we overwrite with a new V2
397 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
398 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
399 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
400 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
403 /* Zero out everything else beyond the superblock header */
405 p = ((char *) jsb) + sizeof(journal_header_t);
406 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
408 jsb->s_blocksize = htonl(ctx->fs->blocksize);
409 jsb->s_maxlen = htonl(journal->j_maxlen);
410 jsb->s_first = htonl(1);
411 jsb->s_sequence = htonl(1);
413 /* In theory we should also re-zero the entire journal here.
414 * Initialising s_sequence to a random value would be a
415 * reasonable compromise. */
417 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
420 static errcode_t e2fsck_journal_fix_corrupt_super(e2fsck_t ctx,
422 struct problem_context *pctx)
424 struct ext2_super_block *sb = ctx->fs->super;
425 int recover = ctx->fs->super->s_feature_incompat &
426 EXT3_FEATURE_INCOMPAT_RECOVER;
428 pctx->num = journal->j_inode->i_ino;
430 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
431 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
432 e2fsck_journal_reset_super(ctx, journal->j_superblock,
434 journal->j_transaction_sequence = 1;
435 e2fsck_clear_recover(ctx, recover);
438 return EXT2_ET_CORRUPT_SUPERBLOCK;
439 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
440 return EXT2_ET_CORRUPT_SUPERBLOCK;
445 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal,
448 journal_superblock_t *jsb;
451 mark_buffer_clean(journal->j_sb_buffer);
452 else if (!(ctx->options & E2F_OPT_READONLY)) {
453 jsb = journal->j_superblock;
454 jsb->s_sequence = htonl(journal->j_transaction_sequence);
456 jsb->s_start = 0; /* this marks the journal as empty */
457 mark_buffer_dirty(journal->j_sb_buffer, 1);
459 brelse(journal->j_sb_buffer);
461 if (journal->j_inode)
462 ext2fs_free_mem((void **)&journal->j_inode);
463 ext2fs_free_mem((void **)&journal);
467 * This function makes sure that the superblock fields regarding the
468 * journal are consistent.
470 int e2fsck_check_ext3_journal(e2fsck_t ctx)
472 struct ext2_super_block *sb = ctx->fs->super;
474 int recover = ctx->fs->super->s_feature_incompat &
475 EXT3_FEATURE_INCOMPAT_RECOVER;
476 struct problem_context pctx;
477 int reset = 0, force_fsck = 0;
480 /* If we don't have any journal features, don't do anything more */
481 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) &&
482 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
483 uuid_is_null(sb->s_journal_uuid))
486 clear_problem_context(&pctx);
487 pctx.num = sb->s_journal_inum;
489 retval = e2fsck_get_journal(ctx, &journal);
491 if (retval == EXT2_ET_BAD_INODE_NUM)
492 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
496 retval = e2fsck_journal_load(journal);
498 if (retval == EXT2_ET_CORRUPT_SUPERBLOCK)
499 retval = e2fsck_journal_fix_corrupt_super(ctx, journal,
501 e2fsck_journal_release(ctx, journal, 0, 1);
506 * We want to make the flags consistent here. We will not leave with
507 * needs_recovery set but has_journal clear. We can't get in a loop
508 * with -y, -n, or -p, only if a user isn't making up their mind.
511 if (!(sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
512 recover = sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
514 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
516 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
519 * Need a full fsck if we are releasing a
520 * journal stored on a reserved inode.
522 force_fsck = recover ||
523 (sb->s_journal_inum < EXT2_FIRST_INODE(sb));
524 /* Clear all of the journal fields */
525 sb->s_journal_inum = 0;
526 sb->s_journal_dev = 0;
527 memset(sb->s_journal_uuid, 0,
528 sizeof(sb->s_journal_uuid));
529 e2fsck_clear_recover(ctx, force_fsck);
530 } else if (!(ctx->options & E2F_OPT_READONLY)) {
531 sb->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
532 ext2fs_mark_super_dirty(ctx->fs);
536 if (sb->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
537 !(sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
538 journal->j_superblock->s_start != 0) {
539 if (fix_problem(ctx, PR_0_JOURNAL_RESET_JOURNAL, &pctx)) {
541 sb->s_state &= ~EXT2_VALID_FS;
542 ext2fs_mark_super_dirty(ctx->fs);
545 * If the user answers no to the above question, we
546 * ignore the fact that journal apparently has data;
547 * accidentally replaying over valid data would be far
548 * worse than skipping a questionable recovery.
550 * XXX should we abort with a fatal error here? What
551 * will the ext3 kernel code do if a filesystem with
552 * !NEEDS_RECOVERY but with a non-zero
553 * journal->j_superblock->s_start is mounted?
557 e2fsck_journal_release(ctx, journal, reset, 0);
561 static errcode_t recover_ext3_journal(e2fsck_t ctx)
566 retval = e2fsck_get_journal(ctx, &journal);
570 retval = e2fsck_journal_load(journal);
574 retval = journal_init_revoke(journal, 1024);
578 retval = -journal_recover(journal);
580 e2fsck_journal_release(ctx, journal, 1, 0);
586 #define TEMPLATE "/tmp/ext3.XXXXXX"
589 * This function attempts to mount and unmount an ext3 filesystem,
590 * which is a cheap way to force the kernel to run the journal and
591 * handle the recovery for us.
593 static errcode_t recover_ext3_journal_via_mount(e2fsck_t ctx)
595 ext2_filsys fs = ctx->fs;
596 char *dirlist[] = {"/mnt","/lost+found","/tmp","/root","/boot",0};
597 errcode_t retval, retval2;
599 char template[] = TEMPLATE;
603 if (ctx->options & E2F_OPT_READONLY) {
604 printf("%s: won't do journal recovery while read-only\n",
606 return EXT2_ET_FILE_RO;
609 printf(_("%s: trying for ext3 kernel journal recovery\n"),
612 * First try to make a temporary directory. This may fail if
613 * the root partition is still mounted read-only.
616 tmpdir = mktemp(template);
618 jfs_debug(2, "trying %s as ext3 temp mount point\n", tmpdir);
619 if (mkdir(template, 0700)) {
620 if (errno == EROFS) {
623 } else if (errno == EEXIST && count++ < 10) {
624 strcpy(template, TEMPLATE);
632 * OK, creating a temporary directory didn't work.
633 * Let's try a list of possible temporary mountpoints.
642 rootdev = buf.st_dev;
645 * Check that dir is on the same device as root (no other
646 * filesystem is mounted there), and it's a directory.
648 for (cpp = dirlist; (dir = *cpp); cpp++)
649 if (stat(dir, &buf) == 0 && buf.st_dev == rootdev &&
650 S_ISDIR(buf.st_mode)) {
657 io_manager io_ptr = fs->io->manager;
658 int blocksize = fs->blocksize;
660 jfs_debug(2, "using %s for ext3 mount\n", tmpdir);
661 /* FIXME - need to handle loop devices here */
662 if (mount(ctx->device_name, tmpdir, "ext3", MNT_FL, NULL)) {
664 com_err(ctx->program_name, errno,
665 "when mounting %s", ctx->device_name);
671 * Now that it mounted cleanly, the filesystem will have been
672 * recovered, so we can now unmount it.
678 * Remove the temporary directory, if it was created.
687 int e2fsck_run_ext3_journal(e2fsck_t ctx)
689 io_manager io_ptr = ctx->fs->io->manager;
690 int blocksize = ctx->fs->blocksize;
691 errcode_t retval, recover_retval;
693 printf(_("%s: recovering journal\n"), ctx->device_name);
694 if (ctx->options & E2F_OPT_READONLY) {
695 printf(_("%s: won't do journal recovery while read-only\n"),
697 return EXT2_ET_FILE_RO;
700 ext2fs_flush(ctx->fs); /* Force out any modifications */
702 recover_retval = recover_ext3_journal(ctx);
705 * Reload the filesystem context to get up-to-date data from disk
706 * because journal recovery will change the filesystem under us.
708 ext2fs_close(ctx->fs);
709 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
710 ctx->superblock, blocksize, io_ptr,
714 com_err(ctx->program_name, retval,
715 _("while trying to re-open %s"),
719 ctx->fs->priv_data = ctx;
721 /* Set the superblock flags */
722 e2fsck_clear_recover(ctx, recover_retval);
723 return recover_retval;