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
25 #include "uuid/uuid.h"
28 static int bh_count = 0;
29 int journal_enable_debug = 0;
32 int bmap(struct inode *inode, int block)
37 retval = ext2fs_bmap(inode->i_ctx->fs, inode->i_ino, &inode->i_ext2,
38 NULL, 0, block, &phys);
41 com_err(inode->i_ctx->device_name, retval,
42 _("bmap journal inode %ld, block %d\n"),
48 struct buffer_head *getblk(e2fsck_t ctx, blk_t blocknr, int blocksize)
50 struct buffer_head *bh;
52 bh = e2fsck_allocate_memory(ctx, sizeof(*bh), "block buffer");
56 jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
57 blocknr, blocksize, ++bh_count);
60 bh->b_size = blocksize;
61 bh->b_blocknr = blocknr;
66 void ll_rw_block(int rw, int nr, struct buffer_head *bhp[])
69 struct buffer_head *bh;
71 for (; nr > 0; --nr) {
73 if (rw == READ && !bh->b_uptodate) {
74 jfs_debug(3, "reading block %lu/%p\n",
76 retval = io_channel_read_blk(bh->b_ctx->fs->io,
80 com_err(bh->b_ctx->device_name, retval,
81 "while reading block %ld\n",
87 } else if (rw == WRITE && bh->b_dirty) {
88 jfs_debug(3, "writing block %lu/%p\n",
90 retval = io_channel_write_blk(bh->b_ctx->fs->io,
94 com_err(bh->b_ctx->device_name, retval,
95 "while writing block %ld\n",
103 jfs_debug(3, "no-op %s for block %lu\n",
104 rw == READ ? "read" : "write",
109 void mark_buffer_dirty(struct buffer_head *bh, int dummy)
111 bh->b_dirty = dummy | 1; /* use dummy to avoid unused variable */
114 void brelse(struct buffer_head *bh)
117 ll_rw_block(WRITE, 1, &bh);
118 jfs_debug(3, "freeing block %lu/%p (total %d)\n",
119 bh->b_blocknr, bh, --bh_count);
120 ext2fs_free_mem((void **) &bh);
123 int buffer_uptodate(struct buffer_head *bh)
125 return bh->b_uptodate;
128 void wait_on_buffer(struct buffer_head *bh)
131 ll_rw_block(READ, 1, &bh);
135 static void e2fsck_clear_recover(e2fsck_t ctx, int error)
137 struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
139 s->s_feature_incompat &= ~EXT3_FEATURE_INCOMPAT_RECOVER;
141 /* if we had an error doing journal recovery, we need a full fsck */
143 s->s_state &= ~EXT2_VALID_FS;
144 ext2fs_mark_super_dirty(ctx->fs);
147 static int e2fsck_journal_init_inode(e2fsck_t ctx, struct ext2fs_sb *s,
148 ino_t journal_inum, journal_t **journal)
151 const char *cmdname = ctx->program_name;
152 struct buffer_head *bh;
156 jfs_debug(1, "Using journal inode %lu\n", journal_inum);
157 *journal = e2fsck_allocate_memory(ctx, sizeof(journal_t), "journal");
159 return EXT2_ET_NO_MEMORY;
162 inode = e2fsck_allocate_memory(ctx, sizeof(*inode), "journal inode");
164 retval = EXT2_ET_NO_MEMORY;
169 inode->i_ino = journal_inum;
170 retval = ext2fs_read_inode(ctx->fs, journal_inum, &inode->i_ext2);
174 (*journal)->j_dev = ctx;
175 (*journal)->j_inode = inode;
176 (*journal)->j_blocksize = ctx->fs->blocksize;
177 (*journal)->j_maxlen = inode->i_ext2.i_size / (*journal)->j_blocksize;
179 if (!inode->i_ext2.i_links_count ||
180 !LINUX_S_ISREG(inode->i_ext2.i_mode) ||
181 (*journal)->j_maxlen < JFS_MIN_JOURNAL_BLOCKS ||
182 (start = bmap(inode, 0)) == 0) {
183 retval = EXT2_ET_BAD_INODE_NUM;
187 bh = getblk(ctx, start, (*journal)->j_blocksize);
189 retval = EXT2_ET_NO_MEMORY;
192 (*journal)->j_sb_buffer = bh;
193 (*journal)->j_superblock = (journal_superblock_t *)bh->b_data;
198 ext2fs_free_mem((void **)&inode);
200 ext2fs_free_mem((void **)journal);
205 static int e2fsck_get_journal(e2fsck_t ctx, journal_t **journal)
208 struct problem_context pctx;
209 struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
210 int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
212 clear_problem_context(&pctx);
214 if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
215 if (s->s_journal_dev) {
216 pctx.num = s->s_journal_dev;
217 /* this problem aborts on -y, -p, unsupported on -n */
218 if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_DEV, &pctx))
219 return EXT2_ET_UNSUPP_FEATURE;
220 s->s_journal_dev = 0;
221 s->s_state &= ~EXT2_VALID_FS;
222 ext2fs_mark_super_dirty(ctx->fs);
224 if (!uuid_is_null(s->s_journal_uuid)) {
225 uuid_unparse(s->s_journal_uuid, uuid_str);
227 /* this problem aborts on -y, -p, unsupported on -n */
228 if (!fix_problem(ctx, PR_0_JOURNAL_UNSUPP_UUID, &pctx))
229 return EXT2_ET_UNSUPP_FEATURE;
230 uuid_clear(s->s_journal_uuid);
231 s->s_state &= ~EXT2_VALID_FS;
232 ext2fs_mark_super_dirty(ctx->fs);
234 if (!s->s_journal_inum)
235 return EXT2_ET_BAD_INODE_NUM;
238 if (s->s_journal_dev) {
239 pctx.num = s->s_journal_dev;
240 if (!fix_problem(ctx, PR_0_JOURNAL_BAD_DEV, &pctx))
241 return EXT2_ET_UNSUPP_FEATURE;
242 s->s_journal_dev = 0;
243 s->s_state &= ~EXT2_VALID_FS;
244 ext2fs_mark_super_dirty(ctx->fs);
246 if (!uuid_is_null(s->s_journal_uuid)) {
247 uuid_unparse(s->s_journal_uuid, uuid_str);
249 if (!fix_problem(ctx, PR_0_JOURNAL_BAD_UUID, &pctx))
250 return EXT2_ET_UNSUPP_FEATURE;
251 uuid_clear(s->s_journal_uuid);
252 s->s_state &= ~EXT2_VALID_FS;
253 ext2fs_mark_super_dirty(ctx->fs);
256 return e2fsck_journal_init_inode(ctx, s, s->s_journal_inum, journal);
259 static int e2fsck_journal_fix_bad_inode(e2fsck_t ctx,
260 struct problem_context *pctx)
262 struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
263 int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
264 int has_journal = s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL;
266 if (has_journal || s->s_journal_inum) {
267 /* The journal inode is bogus, remove and force full fsck */
268 if (fix_problem(ctx, PR_0_JOURNAL_BAD_INODE, pctx)) {
269 struct ext2fs_sb *s =(struct ext2fs_sb *)ctx->fs->super;
271 if (has_journal && s->s_journal_inum)
272 printf("*** ext3 journal has been deleted - "
273 "filesystem is now ext2 only ***\n\n");
274 s->s_feature_compat &= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
275 s->s_journal_inum = 0;
276 e2fsck_clear_recover(ctx, 1);
279 return EXT2_ET_BAD_INODE_NUM;
280 } else if (recover) {
281 if (fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, pctx)) {
282 e2fsck_clear_recover(ctx, 1);
285 return EXT2_ET_UNSUPP_FEATURE;
290 static int e2fsck_journal_fix_unsupported_super(e2fsck_t ctx,
291 struct problem_context *pctx)
293 struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
295 /* Unsupported journal superblock - first choice is abort.
296 * Declining that gives the option to reset the superblock.
298 * Otherwise we get the chance to delete the journal, and
299 * failing that we abort because we can't handle this.
301 if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
302 fix_problem(ctx, PR_0_JOURNAL_UNSUPP_SUPER, pctx))
303 return EXT2_ET_CORRUPT_SUPERBLOCK;
305 if (e2fsck_journal_fix_bad_inode(ctx, pctx))
306 return EXT2_ET_UNSUPP_FEATURE;
311 static int e2fsck_journal_load(journal_t *journal)
313 e2fsck_t ctx = journal->j_dev;
314 journal_superblock_t *jsb;
315 struct buffer_head *jbh = journal->j_sb_buffer;
316 struct problem_context pctx;
318 clear_problem_context(&pctx);
320 ll_rw_block(READ, 1, &jbh);
322 com_err(ctx->device_name, jbh->b_err,
323 _("reading journal superblock\n"));
327 jsb = journal->j_superblock;
328 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
329 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER))
330 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
332 switch (ntohl(jsb->s_header.h_blocktype)) {
333 case JFS_SUPERBLOCK_V1:
334 journal->j_format_version = 1;
337 case JFS_SUPERBLOCK_V2:
338 journal->j_format_version = 2;
341 /* If we don't understand the superblock major type, but there
342 * is a magic number, then it is likely to be a new format we
343 * just don't understand, so leave it alone. */
345 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
346 _("%s: journal has unrecognised format\n"),
348 return EXT2_ET_UNSUPP_FEATURE;
351 if (JFS_HAS_INCOMPAT_FEATURE(journal, ~JFS_KNOWN_INCOMPAT_FEATURES)) {
352 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
353 _("%s: journal has incompatible features\n"),
355 return EXT2_ET_UNSUPP_FEATURE;
358 if (JFS_HAS_RO_COMPAT_FEATURE(journal, ~JFS_KNOWN_ROCOMPAT_FEATURES)) {
359 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
360 _("%s: journal has readonly-incompatible features\n"),
362 return EXT2_ET_RO_UNSUPP_FEATURE;
365 /* We have now checked whether we know enough about the journal
366 * format to be able to proceed safely, so any other checks that
367 * fail we should attempt to recover from. */
368 if (jsb->s_blocksize != htonl(journal->j_blocksize)) {
369 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
370 _("%s: no valid journal superblock found\n"),
372 return EXT2_ET_CORRUPT_SUPERBLOCK;
375 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
376 journal->j_maxlen = ntohl(jsb->s_maxlen);
377 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen) {
378 com_err(ctx->program_name, EXT2_ET_CORRUPT_SUPERBLOCK,
379 _("%s: journal too short\n"),
381 return EXT2_ET_CORRUPT_SUPERBLOCK;
384 journal->j_tail_sequence = ntohl(jsb->s_sequence);
385 journal->j_transaction_sequence = journal->j_tail_sequence;
386 journal->j_tail = ntohl(jsb->s_start);
387 journal->j_first = ntohl(jsb->s_first);
388 journal->j_last = ntohl(jsb->s_maxlen);
393 void e2fsck_journal_reset_super(e2fsck_t ctx, journal_superblock_t *jsb,
398 /* Leave a valid existing V1 superblock signature alone.
399 * Anything unrecognisable we overwrite with a new V2
402 if (jsb->s_header.h_magic != htonl(JFS_MAGIC_NUMBER) ||
403 jsb->s_header.h_blocktype != htonl(JFS_SUPERBLOCK_V1)) {
404 jsb->s_header.h_magic = htonl(JFS_MAGIC_NUMBER);
405 jsb->s_header.h_blocktype = htonl(JFS_SUPERBLOCK_V2);
408 /* Zero out everything else beyond the superblock header */
410 p = ((char *) jsb) + sizeof(journal_header_t);
411 memset (p, 0, ctx->fs->blocksize-sizeof(journal_header_t));
413 jsb->s_blocksize = htonl(ctx->fs->blocksize);
414 jsb->s_maxlen = htonl(journal->j_maxlen);
415 jsb->s_first = htonl(1);
416 jsb->s_sequence = htonl(1);
418 /* In theory we should also re-zero the entire journal here.
419 * Initialising s_sequence to a random value would be a
420 * reasonable compromise. */
422 ll_rw_block(WRITE, 1, &journal->j_sb_buffer);
425 static int e2fsck_journal_fix_corrupt_super(e2fsck_t ctx, journal_t *journal,
426 struct problem_context *pctx)
428 struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
429 int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
431 pctx->num = journal->j_inode->i_ino;
433 if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) {
434 if (fix_problem(ctx, PR_0_JOURNAL_BAD_SUPER, pctx)) {
435 journal_superblock_t *jsb = journal->j_superblock;
437 e2fsck_journal_reset_super(ctx, jsb, journal);
439 journal->j_transaction_sequence = 1;
440 e2fsck_clear_recover(ctx, recover);
443 return EXT2_ET_CORRUPT_SUPERBLOCK;
444 } else if (e2fsck_journal_fix_bad_inode(ctx, pctx))
445 return EXT2_ET_CORRUPT_SUPERBLOCK;
450 static void e2fsck_journal_release(e2fsck_t ctx, journal_t *journal, int reset)
452 journal_superblock_t *jsb;
454 if (!(ctx->options & E2F_OPT_READONLY)) {
455 jsb = journal->j_superblock;
456 jsb->s_sequence = htonl(journal->j_transaction_sequence);
458 jsb->s_start = 0; /* this marks the journal as empty */
459 mark_buffer_dirty(journal->j_sb_buffer, 1);
461 brelse(journal->j_sb_buffer);
463 if (journal->j_inode)
464 free(journal->j_inode);
465 ext2fs_free_mem((void **)&journal);
468 int e2fsck_check_ext3_journal(e2fsck_t ctx)
470 struct ext2fs_sb *s = (struct ext2fs_sb *)ctx->fs->super;
472 int recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
473 struct problem_context pctx;
477 /* If we don't have any journal features, don't do anything more */
478 if (!(s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL))
481 clear_problem_context(&pctx);
482 pctx.num = s->s_journal_inum;
484 retval = e2fsck_get_journal(ctx, &journal);
486 if (retval == EXT2_ET_BAD_INODE_NUM)
487 return e2fsck_journal_fix_bad_inode(ctx, &pctx);
491 retval = e2fsck_journal_load(journal);
493 if (retval == EXT2_ET_CORRUPT_SUPERBLOCK)
494 return e2fsck_journal_fix_corrupt_super(ctx, journal,
500 * We want to make the flags consistent here. We will not leave with
501 * needs_recovery set but has_journal clear. We can't get in a loop
502 * with -y, -n, or -p, only if a user isn't making up their mind.
505 if (!(s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
506 recover = s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER;
508 if (fix_problem(ctx, PR_0_JOURNAL_HAS_JOURNAL, &pctx)) {
510 !fix_problem(ctx, PR_0_JOURNAL_RECOVER_SET, &pctx))
512 s->s_journal_inum = 0;
513 e2fsck_clear_recover(ctx, recover);
514 } else if (!(ctx->options & E2F_OPT_READONLY)) {
515 s->s_feature_compat |= EXT3_FEATURE_COMPAT_HAS_JOURNAL;
516 ext2fs_mark_super_dirty(ctx->fs);
520 if (s->s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
521 !(s->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) &&
522 journal->j_superblock->s_start != 0) {
523 if (fix_problem(ctx, PR_0_JOURNAL_RESET_JOURNAL, &pctx))
525 /* I refuse to enable recovery for journal */
528 e2fsck_journal_release(ctx, journal, reset);
532 static int recover_ext3_journal(e2fsck_t ctx)
534 ext2_filsys fs = ctx->fs;
538 retval = e2fsck_get_journal(ctx, &journal);
542 retval = e2fsck_journal_load(journal);
546 retval = journal_init_revoke(journal, 1024);
550 retval = -journal_recover(journal);
551 e2fsck_journal_release(ctx, journal, 1);
557 #define TEMPLATE "/tmp/ext3.XXXXXX"
560 * This function attempts to mount and unmount an ext3 filesystem,
561 * which is a cheap way to force the kernel to run the journal and
562 * handle the recovery for us.
564 static int recover_ext3_journal_via_mount(e2fsck_t ctx)
566 ext2_filsys fs = ctx->fs;
567 char *dirlist[] = {"/mnt","/lost+found","/tmp","/root","/boot",0};
568 errcode_t retval, retval2;
570 char template[] = TEMPLATE;
574 if (ctx->options & E2F_OPT_READONLY) {
575 printf("%s: won't do journal recovery while read-only\n",
577 return EXT2_ET_FILE_RO;
580 printf(_("%s: trying for ext3 kernel journal recovery\n"),
583 * First try to make a temporary directory. This may fail if
584 * the root partition is still mounted read-only.
587 tmpdir = mktemp(template);
589 jfs_debug(2, "trying %s as ext3 temp mount point\n", tmpdir);
590 if (mkdir(template, 0700)) {
591 if (errno == EROFS) {
594 } else if (errno == EEXIST && count++ < 10) {
595 strcpy(template, TEMPLATE);
603 * OK, creating a temporary directory didn't work.
604 * Let's try a list of possible temporary mountpoints.
613 rootdev = buf.st_dev;
616 * Check that dir is on the same device as root (no other
617 * filesystem is mounted there), and it's a directory.
619 for (cpp = dirlist; (dir = *cpp); cpp++)
620 if (stat(dir, &buf) == 0 && buf.st_dev == rootdev &&
621 S_ISDIR(buf.st_mode)) {
628 io_manager io_ptr = fs->io->manager;
629 int blocksize = fs->blocksize;
631 jfs_debug(2, "using %s for ext3 mount\n", tmpdir);
632 /* FIXME - need to handle loop devices here */
633 if (mount(ctx->device_name, tmpdir, "ext3", MNT_FL, NULL)) {
635 com_err(ctx->program_name, errno,
636 "when mounting %s", ctx->device_name);
642 * Now that it mounted cleanly, the filesystem will have been
643 * recovered, so we can now unmount it.
649 * Remove the temporary directory, if it was created.
658 int e2fsck_run_ext3_journal(e2fsck_t ctx)
660 io_manager io_ptr = ctx->fs->io->manager;
661 int blocksize = ctx->fs->blocksize;
662 errcode_t retval, recover_retval;
664 printf(_("%s: recovering journal\n"), ctx->device_name);
665 if (ctx->options & E2F_OPT_READONLY) {
666 printf(_("%s: won't do journal recovery while read-only\n"),
668 return EXT2_ET_FILE_RO;
671 recover_retval = recover_ext3_journal(ctx);
674 * Reload the filesystem context to get up-to-date data from disk
675 * because journal recovery will change the filesystem under us.
677 ext2fs_close(ctx->fs);
678 retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW,
679 ctx->superblock, blocksize, io_ptr,
683 com_err(ctx->program_name, retval,
684 _("while trying to re-open %s"),
688 ctx->fs->priv_data = ctx;
690 /* Set the superblock flags */
691 e2fsck_clear_recover(ctx, recover_retval);
692 return recover_retval;