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 CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
29 static int bh_count
= 0;
33 * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths.
34 * This creates a larger static binary, and a smaller binary using
35 * shared libraries. It's also probably slightly less CPU-efficient,
36 * which is why it's not on by default. But, it's a good way of
37 * testing the functions in inode_io.c and fileio.c.
41 /* Kernel compatibility functions for handling the journal. These allow us
42 * to use the recovery.c file virtually unchanged from the kernel, so we
43 * don't have to do much to keep kernel and user recovery in sync.
45 int journal_bmap(journal_t
*journal
, blk_t block
, unsigned long *phys
)
51 struct inode
*inode
= journal
->j_inode
;
60 retval
= ext2fs_bmap(inode
->i_ctx
->fs
, inode
->i_ino
,
61 &inode
->i_ext2
, NULL
, 0, block
, &pblk
);
67 struct buffer_head
*getblk(kdev_t kdev
, blk_t blocknr
, int blocksize
)
69 struct buffer_head
*bh
;
71 bh
= e2fsck_allocate_memory(kdev
->k_ctx
, sizeof(*bh
), "block buffer");
75 jfs_debug(4, "getblk for block %lu (%d bytes)(total %d)\n",
76 (unsigned long) blocknr
, blocksize
, ++bh_count
);
78 bh
->b_ctx
= kdev
->k_ctx
;
79 if (kdev
->k_dev
== K_DEV_FS
)
80 bh
->b_io
= kdev
->k_ctx
->fs
->io
;
82 bh
->b_io
= kdev
->k_ctx
->journal_io
;
83 bh
->b_size
= blocksize
;
84 bh
->b_blocknr
= blocknr
;
89 void ll_rw_block(int rw
, int nr
, struct buffer_head
*bhp
[])
92 struct buffer_head
*bh
;
94 for (; nr
> 0; --nr
) {
96 if (rw
== READ
&& !bh
->b_uptodate
) {
97 jfs_debug(3, "reading block %lu/%p\n",
98 (unsigned long) bh
->b_blocknr
, (void *) bh
);
99 retval
= io_channel_read_blk(bh
->b_io
,
103 com_err(bh
->b_ctx
->device_name
, retval
,
104 "while reading block %lu\n",
105 (unsigned long) bh
->b_blocknr
);
110 } else if (rw
== WRITE
&& bh
->b_dirty
) {
111 jfs_debug(3, "writing block %lu/%p\n",
112 (unsigned long) bh
->b_blocknr
, (void *) bh
);
113 retval
= io_channel_write_blk(bh
->b_io
,
117 com_err(bh
->b_ctx
->device_name
, retval
,
118 "while writing block %lu\n",
119 (unsigned long) bh
->b_blocknr
);
126 jfs_debug(3, "no-op %s for block %lu\n",
127 rw
== READ
? "read" : "write",
128 (unsigned long) bh
->b_blocknr
);
133 void mark_buffer_dirty(struct buffer_head
*bh
)
138 static void mark_buffer_clean(struct buffer_head
* bh
)
143 void brelse(struct buffer_head
*bh
)
146 ll_rw_block(WRITE
, 1, &bh
);
147 jfs_debug(3, "freeing block %lu/%p (total %d)\n",
148 (unsigned long) bh
->b_blocknr
, (void *) bh
, --bh_count
);
149 ext2fs_free_mem(&bh
);
152 int buffer_uptodate(struct buffer_head
*bh
)
154 return bh
->b_uptodate
;
157 void mark_buffer_uptodate(struct buffer_head
*bh
, int val
)
159 bh
->b_uptodate
= val
;
162 void wait_on_buffer(struct buffer_head
*bh
)
165 ll_rw_block(READ
, 1, &bh
);
169 static void e2fsck_clear_recover(e2fsck_t ctx
, int error
)
171 ctx
->fs
->super
->s_feature_incompat
&= ~EXT3_FEATURE_INCOMPAT_RECOVER
;
173 /* if we had an error doing journal recovery, we need a full fsck */
175 ctx
->fs
->super
->s_state
&= ~EXT2_VALID_FS
;
176 ext2fs_mark_super_dirty(ctx
->fs
);
179 static errcode_t
e2fsck_get_journal(e2fsck_t ctx
, journal_t
**ret_journal
)
181 struct ext2_super_block
*sb
= ctx
->fs
->super
;
182 struct ext2_super_block jsuper
;
183 struct problem_context pctx
;
184 struct buffer_head
*bh
;
185 struct inode
*j_inode
= NULL
;
186 struct kdev_s
*dev_fs
= NULL
, *dev_journal
;
187 const char *journal_name
= 0;
188 journal_t
*journal
= NULL
;
189 errcode_t retval
= 0;
190 io_manager io_ptr
= 0;
191 unsigned long start
= 0;
194 int tried_backup_jnl
= 0;
197 clear_problem_context(&pctx
);
199 journal
= e2fsck_allocate_memory(ctx
, sizeof(journal_t
), "journal");
201 return EXT2_ET_NO_MEMORY
;
204 dev_fs
= e2fsck_allocate_memory(ctx
, 2*sizeof(struct kdev_s
), "kdev");
206 retval
= EXT2_ET_NO_MEMORY
;
209 dev_journal
= dev_fs
+1;
211 dev_fs
->k_ctx
= dev_journal
->k_ctx
= ctx
;
212 dev_fs
->k_dev
= K_DEV_FS
;
213 dev_journal
->k_dev
= K_DEV_JOURNAL
;
215 journal
->j_dev
= dev_journal
;
216 journal
->j_fs_dev
= dev_fs
;
217 journal
->j_inode
= NULL
;
218 journal
->j_blocksize
= ctx
->fs
->blocksize
;
220 if (uuid_is_null(sb
->s_journal_uuid
)) {
221 if (!sb
->s_journal_inum
)
222 return EXT2_ET_BAD_INODE_NUM
;
223 j_inode
= e2fsck_allocate_memory(ctx
, sizeof(*j_inode
),
226 retval
= EXT2_ET_NO_MEMORY
;
230 j_inode
->i_ctx
= ctx
;
231 j_inode
->i_ino
= sb
->s_journal_inum
;
233 if ((retval
= ext2fs_read_inode(ctx
->fs
,
235 &j_inode
->i_ext2
))) {
237 if (sb
->s_jnl_backup_type
!= EXT3_JNL_BACKUP_BLOCKS
||
240 memset(&j_inode
->i_ext2
, 0, sizeof(struct ext2_inode
));
241 memcpy(&j_inode
->i_ext2
.i_block
[0], sb
->s_jnl_blocks
,
243 j_inode
->i_ext2
.i_size
= sb
->s_jnl_blocks
[16];
244 j_inode
->i_ext2
.i_links_count
= 1;
245 j_inode
->i_ext2
.i_mode
= LINUX_S_IFREG
| 0600;
248 if (!j_inode
->i_ext2
.i_links_count
||
249 !LINUX_S_ISREG(j_inode
->i_ext2
.i_mode
)) {
250 retval
= EXT2_ET_NO_JOURNAL
;
251 goto try_backup_journal
;
253 if (j_inode
->i_ext2
.i_size
/ journal
->j_blocksize
<
254 JFS_MIN_JOURNAL_BLOCKS
) {
255 retval
= EXT2_ET_JOURNAL_TOO_SMALL
;
256 goto try_backup_journal
;
258 for (i
=0; i
< EXT2_N_BLOCKS
; i
++) {
259 blk
= j_inode
->i_ext2
.i_block
[i
];
261 if (i
< EXT2_NDIR_BLOCKS
) {
262 retval
= EXT2_ET_JOURNAL_TOO_SMALL
;
263 goto try_backup_journal
;
267 if (blk
< sb
->s_first_data_block
||
268 blk
>= sb
->s_blocks_count
) {
269 retval
= EXT2_ET_BAD_BLOCK_NUM
;
270 goto try_backup_journal
;
273 journal
->j_maxlen
= j_inode
->i_ext2
.i_size
/ journal
->j_blocksize
;
276 retval
= ext2fs_inode_io_intern2(ctx
->fs
, sb
->s_journal_inum
,
282 io_ptr
= inode_io_manager
;
284 journal
->j_inode
= j_inode
;
285 ctx
->journal_io
= ctx
->fs
->io
;
286 if ((retval
= journal_bmap(journal
, 0, &start
)) != 0)
291 if (!ctx
->journal_name
) {
294 uuid_unparse(sb
->s_journal_uuid
, uuid
);
295 ctx
->journal_name
= blkid_get_devname(ctx
->blkid
,
297 if (!ctx
->journal_name
)
298 ctx
->journal_name
= blkid_devno_to_devname(sb
->s_journal_dev
);
300 journal_name
= ctx
->journal_name
;
303 fix_problem(ctx
, PR_0_CANT_FIND_JOURNAL
, &pctx
);
304 return EXT2_ET_LOAD_EXT_JOURNAL
;
307 jfs_debug(1, "Using journal file %s\n", journal_name
);
308 io_ptr
= unix_io_manager
;
312 test_io_backing_manager
= io_ptr
;
313 io_ptr
= test_io_manager
;
318 retval
= io_ptr
->open(journal_name
, IO_FLAG_RW
,
323 io_channel_set_blksize(ctx
->journal_io
, ctx
->fs
->blocksize
);
326 if (ctx
->fs
->blocksize
== 1024)
328 bh
= getblk(dev_journal
, start
, ctx
->fs
->blocksize
);
330 retval
= EXT2_ET_NO_MEMORY
;
333 ll_rw_block(READ
, 1, &bh
);
334 if ((retval
= bh
->b_err
) != 0)
336 memcpy(&jsuper
, start
? bh
->b_data
: bh
->b_data
+ 1024,
339 #ifdef EXT2FS_ENABLE_SWAPFS
340 if (jsuper
.s_magic
== ext2fs_swab16(EXT2_SUPER_MAGIC
))
341 ext2fs_swap_super(&jsuper
);
343 if (jsuper
.s_magic
!= EXT2_SUPER_MAGIC
||
344 !(jsuper
.s_feature_incompat
& EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
)) {
345 fix_problem(ctx
, PR_0_EXT_JOURNAL_BAD_SUPER
, &pctx
);
346 retval
= EXT2_ET_LOAD_EXT_JOURNAL
;
349 /* Make sure the journal UUID is correct */
350 if (memcmp(jsuper
.s_uuid
, ctx
->fs
->super
->s_journal_uuid
,
351 sizeof(jsuper
.s_uuid
))) {
352 fix_problem(ctx
, PR_0_JOURNAL_BAD_UUID
, &pctx
);
353 retval
= EXT2_ET_LOAD_EXT_JOURNAL
;
357 journal
->j_maxlen
= jsuper
.s_blocks_count
;
361 if (!(bh
= getblk(dev_journal
, start
, journal
->j_blocksize
))) {
362 retval
= EXT2_ET_NO_MEMORY
;
366 journal
->j_sb_buffer
= bh
;
367 journal
->j_superblock
= (journal_superblock_t
*)bh
->b_data
;
371 ext2fs_free_mem(&j_inode
);
374 *ret_journal
= journal
;
379 ext2fs_free_mem(&dev_fs
);
381 ext2fs_free_mem(&j_inode
);
383 ext2fs_free_mem(&journal
);
388 static errcode_t
e2fsck_journal_fix_bad_inode(e2fsck_t ctx
,
389 struct problem_context
*pctx
)
391 struct ext2_super_block
*sb
= ctx
->fs
->super
;
392 int recover
= ctx
->fs
->super
->s_feature_incompat
&
393 EXT3_FEATURE_INCOMPAT_RECOVER
;
394 int has_journal
= ctx
->fs
->super
->s_feature_compat
&
395 EXT3_FEATURE_COMPAT_HAS_JOURNAL
;
397 if (has_journal
|| sb
->s_journal_inum
) {
398 /* The journal inode is bogus, remove and force full fsck */
399 pctx
->ino
= sb
->s_journal_inum
;
400 if (fix_problem(ctx
, PR_0_JOURNAL_BAD_INODE
, pctx
)) {
401 if (has_journal
&& sb
->s_journal_inum
)
402 printf("*** ext3 journal has been deleted - "
403 "filesystem is now ext2 only ***\n\n");
404 sb
->s_feature_compat
&= ~EXT3_FEATURE_COMPAT_HAS_JOURNAL
;
405 sb
->s_journal_inum
= 0;
406 ctx
->flags
|= E2F_FLAG_JOURNAL_INODE
; /* FIXME: todo */
407 e2fsck_clear_recover(ctx
, 1);
410 return EXT2_ET_BAD_INODE_NUM
;
411 } else if (recover
) {
412 if (fix_problem(ctx
, PR_0_JOURNAL_RECOVER_SET
, pctx
)) {
413 e2fsck_clear_recover(ctx
, 1);
416 return EXT2_ET_UNSUPP_FEATURE
;
421 #define V1_SB_SIZE 0x0024
422 static void clear_v2_journal_fields(journal_t
*journal
)
424 e2fsck_t ctx
= journal
->j_dev
->k_ctx
;
425 struct problem_context pctx
;
427 clear_problem_context(&pctx
);
429 if (!fix_problem(ctx
, PR_0_CLEAR_V2_JOURNAL
, &pctx
))
432 memset(((char *) journal
->j_superblock
) + V1_SB_SIZE
, 0,
433 ctx
->fs
->blocksize
-V1_SB_SIZE
);
434 mark_buffer_dirty(journal
->j_sb_buffer
);
438 static errcode_t
e2fsck_journal_load(journal_t
*journal
)
440 e2fsck_t ctx
= journal
->j_dev
->k_ctx
;
441 journal_superblock_t
*jsb
;
442 struct buffer_head
*jbh
= journal
->j_sb_buffer
;
443 struct problem_context pctx
;
445 clear_problem_context(&pctx
);
447 ll_rw_block(READ
, 1, &jbh
);
449 com_err(ctx
->device_name
, jbh
->b_err
,
450 _("reading journal superblock\n"));
454 jsb
= journal
->j_superblock
;
455 /* If we don't even have JFS_MAGIC, we probably have a wrong inode */
456 if (jsb
->s_header
.h_magic
!= htonl(JFS_MAGIC_NUMBER
))
457 return e2fsck_journal_fix_bad_inode(ctx
, &pctx
);
459 switch (ntohl(jsb
->s_header
.h_blocktype
)) {
460 case JFS_SUPERBLOCK_V1
:
461 journal
->j_format_version
= 1;
462 if (jsb
->s_feature_compat
||
463 jsb
->s_feature_incompat
||
464 jsb
->s_feature_ro_compat
||
466 clear_v2_journal_fields(journal
);
469 case JFS_SUPERBLOCK_V2
:
470 journal
->j_format_version
= 2;
471 if (ntohl(jsb
->s_nr_users
) > 1 &&
472 uuid_is_null(ctx
->fs
->super
->s_journal_uuid
))
473 clear_v2_journal_fields(journal
);
474 if (ntohl(jsb
->s_nr_users
) > 1) {
475 fix_problem(ctx
, PR_0_JOURNAL_UNSUPP_MULTIFS
, &pctx
);
476 return EXT2_ET_JOURNAL_UNSUPP_VERSION
;
481 * These should never appear in a journal super block, so if
482 * they do, the journal is badly corrupted.
484 case JFS_DESCRIPTOR_BLOCK
:
485 case JFS_COMMIT_BLOCK
:
486 case JFS_REVOKE_BLOCK
:
487 return EXT2_ET_CORRUPT_SUPERBLOCK
;
489 /* If we don't understand the superblock major type, but there
490 * is a magic number, then it is likely to be a new format we
491 * just don't understand, so leave it alone. */
493 return EXT2_ET_JOURNAL_UNSUPP_VERSION
;
496 if (JFS_HAS_INCOMPAT_FEATURE(journal
, ~JFS_KNOWN_INCOMPAT_FEATURES
))
497 return EXT2_ET_UNSUPP_FEATURE
;
499 if (JFS_HAS_RO_COMPAT_FEATURE(journal
, ~JFS_KNOWN_ROCOMPAT_FEATURES
))
500 return EXT2_ET_RO_UNSUPP_FEATURE
;
502 /* We have now checked whether we know enough about the journal
503 * format to be able to proceed safely, so any other checks that
504 * fail we should attempt to recover from. */
505 if (jsb
->s_blocksize
!= htonl(journal
->j_blocksize
)) {
506 com_err(ctx
->program_name
, EXT2_ET_CORRUPT_SUPERBLOCK
,
507 _("%s: no valid journal superblock found\n"),
509 return EXT2_ET_CORRUPT_SUPERBLOCK
;
512 if (ntohl(jsb
->s_maxlen
) < journal
->j_maxlen
)
513 journal
->j_maxlen
= ntohl(jsb
->s_maxlen
);
514 else if (ntohl(jsb
->s_maxlen
) > journal
->j_maxlen
) {
515 com_err(ctx
->program_name
, EXT2_ET_CORRUPT_SUPERBLOCK
,
516 _("%s: journal too short\n"),
518 return EXT2_ET_CORRUPT_SUPERBLOCK
;
521 journal
->j_tail_sequence
= ntohl(jsb
->s_sequence
);
522 journal
->j_transaction_sequence
= journal
->j_tail_sequence
;
523 journal
->j_tail
= ntohl(jsb
->s_start
);
524 journal
->j_first
= ntohl(jsb
->s_first
);
525 journal
->j_last
= ntohl(jsb
->s_maxlen
);
530 static void e2fsck_journal_reset_super(e2fsck_t ctx
, journal_superblock_t
*jsb
,
541 /* Leave a valid existing V1 superblock signature alone.
542 * Anything unrecognisable we overwrite with a new V2
545 if (jsb
->s_header
.h_magic
!= htonl(JFS_MAGIC_NUMBER
) ||
546 jsb
->s_header
.h_blocktype
!= htonl(JFS_SUPERBLOCK_V1
)) {
547 jsb
->s_header
.h_magic
= htonl(JFS_MAGIC_NUMBER
);
548 jsb
->s_header
.h_blocktype
= htonl(JFS_SUPERBLOCK_V2
);
551 /* Zero out everything else beyond the superblock header */
553 p
= ((char *) jsb
) + sizeof(journal_header_t
);
554 memset (p
, 0, ctx
->fs
->blocksize
-sizeof(journal_header_t
));
556 jsb
->s_blocksize
= htonl(ctx
->fs
->blocksize
);
557 jsb
->s_maxlen
= htonl(journal
->j_maxlen
);
558 jsb
->s_first
= htonl(1);
560 /* Initialize the journal sequence number so that there is "no"
561 * chance we will find old "valid" transactions in the journal.
562 * This avoids the need to zero the whole journal (slow to do,
563 * and risky when we are just recovering the filesystem).
565 uuid_generate(u
.uuid
);
566 for (i
= 0; i
< 4; i
++)
568 jsb
->s_sequence
= htonl(new_seq
);
570 mark_buffer_dirty(journal
->j_sb_buffer
);
571 ll_rw_block(WRITE
, 1, &journal
->j_sb_buffer
);
574 static errcode_t
e2fsck_journal_fix_corrupt_super(e2fsck_t ctx
,
576 struct problem_context
*pctx
)
578 struct ext2_super_block
*sb
= ctx
->fs
->super
;
579 int recover
= ctx
->fs
->super
->s_feature_incompat
&
580 EXT3_FEATURE_INCOMPAT_RECOVER
;
582 if (sb
->s_feature_compat
& EXT3_FEATURE_COMPAT_HAS_JOURNAL
) {
583 if (fix_problem(ctx
, PR_0_JOURNAL_BAD_SUPER
, pctx
)) {
584 e2fsck_journal_reset_super(ctx
, journal
->j_superblock
,
586 journal
->j_transaction_sequence
= 1;
587 e2fsck_clear_recover(ctx
, recover
);
590 return EXT2_ET_CORRUPT_SUPERBLOCK
;
591 } else if (e2fsck_journal_fix_bad_inode(ctx
, pctx
))
592 return EXT2_ET_CORRUPT_SUPERBLOCK
;
597 static void e2fsck_journal_release(e2fsck_t ctx
, journal_t
*journal
,
600 journal_superblock_t
*jsb
;
603 mark_buffer_clean(journal
->j_sb_buffer
);
604 else if (!(ctx
->options
& E2F_OPT_READONLY
)) {
605 jsb
= journal
->j_superblock
;
606 jsb
->s_sequence
= htonl(journal
->j_transaction_sequence
);
608 jsb
->s_start
= 0; /* this marks the journal as empty */
609 mark_buffer_dirty(journal
->j_sb_buffer
);
611 brelse(journal
->j_sb_buffer
);
613 if (ctx
->journal_io
) {
614 if (ctx
->fs
&& ctx
->fs
->io
!= ctx
->journal_io
)
615 io_channel_close(ctx
->journal_io
);
620 if (journal
->j_inode
)
621 ext2fs_free_mem(&journal
->j_inode
);
623 if (journal
->j_fs_dev
)
624 ext2fs_free_mem(&journal
->j_fs_dev
);
625 ext2fs_free_mem(&journal
);
629 * This function makes sure that the superblock fields regarding the
630 * journal are consistent.
632 int e2fsck_check_ext3_journal(e2fsck_t ctx
)
634 struct ext2_super_block
*sb
= ctx
->fs
->super
;
636 int recover
= ctx
->fs
->super
->s_feature_incompat
&
637 EXT3_FEATURE_INCOMPAT_RECOVER
;
638 struct problem_context pctx
;
640 int reset
= 0, force_fsck
= 0;
643 /* If we don't have any journal features, don't do anything more */
644 if (!(sb
->s_feature_compat
& EXT3_FEATURE_COMPAT_HAS_JOURNAL
) &&
645 !recover
&& sb
->s_journal_inum
== 0 && sb
->s_journal_dev
== 0 &&
646 uuid_is_null(sb
->s_journal_uuid
))
649 clear_problem_context(&pctx
);
650 pctx
.num
= sb
->s_journal_inum
;
652 retval
= e2fsck_get_journal(ctx
, &journal
);
654 if ((retval
== EXT2_ET_BAD_INODE_NUM
) ||
655 (retval
== EXT2_ET_JOURNAL_TOO_SMALL
) ||
656 (retval
== EXT2_ET_NO_JOURNAL
))
657 return e2fsck_journal_fix_bad_inode(ctx
, &pctx
);
661 retval
= e2fsck_journal_load(journal
);
663 if ((retval
== EXT2_ET_CORRUPT_SUPERBLOCK
) ||
664 ((retval
== EXT2_ET_UNSUPP_FEATURE
) &&
665 (!fix_problem(ctx
, PR_0_JOURNAL_UNSUPP_INCOMPAT
,
667 ((retval
== EXT2_ET_RO_UNSUPP_FEATURE
) &&
668 (!fix_problem(ctx
, PR_0_JOURNAL_UNSUPP_ROCOMPAT
,
670 ((retval
== EXT2_ET_JOURNAL_UNSUPP_VERSION
) &&
671 (!fix_problem(ctx
, PR_0_JOURNAL_UNSUPP_VERSION
, &pctx
))))
672 retval
= e2fsck_journal_fix_corrupt_super(ctx
, journal
,
674 e2fsck_journal_release(ctx
, journal
, 0, 1);
679 * We want to make the flags consistent here. We will not leave with
680 * needs_recovery set but has_journal clear. We can't get in a loop
681 * with -y, -n, or -p, only if a user isn't making up their mind.
684 if (!(sb
->s_feature_compat
& EXT3_FEATURE_COMPAT_HAS_JOURNAL
)) {
685 recover
= sb
->s_feature_incompat
& EXT3_FEATURE_INCOMPAT_RECOVER
;
687 if (fix_problem(ctx
, PR_0_JOURNAL_HAS_JOURNAL
, &pctx
)) {
689 !fix_problem(ctx
, PR_0_JOURNAL_RECOVER_SET
, &pctx
))
692 * Need a full fsck if we are releasing a
693 * journal stored on a reserved inode.
695 force_fsck
= recover
||
696 (sb
->s_journal_inum
< EXT2_FIRST_INODE(sb
));
697 /* Clear all of the journal fields */
698 sb
->s_journal_inum
= 0;
699 sb
->s_journal_dev
= 0;
700 memset(sb
->s_journal_uuid
, 0,
701 sizeof(sb
->s_journal_uuid
));
702 e2fsck_clear_recover(ctx
, force_fsck
);
703 } else if (!(ctx
->options
& E2F_OPT_READONLY
)) {
704 sb
->s_feature_compat
|= EXT3_FEATURE_COMPAT_HAS_JOURNAL
;
705 ext2fs_mark_super_dirty(ctx
->fs
);
709 if (sb
->s_feature_compat
& EXT3_FEATURE_COMPAT_HAS_JOURNAL
&&
710 !(sb
->s_feature_incompat
& EXT3_FEATURE_INCOMPAT_RECOVER
) &&
711 journal
->j_superblock
->s_start
!= 0) {
712 /* Print status information */
713 fix_problem(ctx
, PR_0_JOURNAL_RECOVERY_CLEAR
, &pctx
);
715 problem
= PR_0_JOURNAL_RUN_DEFAULT
;
717 problem
= PR_0_JOURNAL_RUN
;
718 if (fix_problem(ctx
, problem
, &pctx
)) {
719 ctx
->options
|= E2F_OPT_FORCE
;
720 sb
->s_feature_incompat
|=
721 EXT3_FEATURE_INCOMPAT_RECOVER
;
722 ext2fs_mark_super_dirty(ctx
->fs
);
723 } else if (fix_problem(ctx
,
724 PR_0_JOURNAL_RESET_JOURNAL
, &pctx
)) {
726 sb
->s_state
&= ~EXT2_VALID_FS
;
727 ext2fs_mark_super_dirty(ctx
->fs
);
730 * If the user answers no to the above question, we
731 * ignore the fact that journal apparently has data;
732 * accidentally replaying over valid data would be far
733 * worse than skipping a questionable recovery.
735 * XXX should we abort with a fatal error here? What
736 * will the ext3 kernel code do if a filesystem with
737 * !NEEDS_RECOVERY but with a non-zero
738 * journal->j_superblock->s_start is mounted?
742 e2fsck_journal_release(ctx
, journal
, reset
, 0);
746 static errcode_t
recover_ext3_journal(e2fsck_t ctx
)
751 journal_init_revoke_caches();
752 retval
= e2fsck_get_journal(ctx
, &journal
);
756 retval
= e2fsck_journal_load(journal
);
760 retval
= journal_init_revoke(journal
, 1024);
764 retval
= -journal_recover(journal
);
768 if (journal
->j_superblock
->s_errno
) {
769 ctx
->fs
->super
->s_state
|= EXT2_ERROR_FS
;
770 ext2fs_mark_super_dirty(ctx
->fs
);
771 journal
->j_superblock
->s_errno
= 0;
772 mark_buffer_dirty(journal
->j_sb_buffer
);
776 journal_destroy_revoke(journal
);
777 journal_destroy_revoke_caches();
778 e2fsck_journal_release(ctx
, journal
, 1, 0);
782 int e2fsck_run_ext3_journal(e2fsck_t ctx
)
784 io_manager io_ptr
= ctx
->fs
->io
->manager
;
785 int blocksize
= ctx
->fs
->blocksize
;
786 errcode_t retval
, recover_retval
;
788 printf(_("%s: recovering journal\n"), ctx
->device_name
);
789 if (ctx
->options
& E2F_OPT_READONLY
) {
790 printf(_("%s: won't do journal recovery while read-only\n"),
792 return EXT2_ET_FILE_RO
;
795 if (ctx
->fs
->flags
& EXT2_FLAG_DIRTY
)
796 ext2fs_flush(ctx
->fs
); /* Force out any modifications */
798 recover_retval
= recover_ext3_journal(ctx
);
801 * Reload the filesystem context to get up-to-date data from disk
802 * because journal recovery will change the filesystem under us.
804 ext2fs_close(ctx
->fs
);
805 retval
= ext2fs_open(ctx
->filesystem_name
, EXT2_FLAG_RW
,
806 ctx
->superblock
, blocksize
, io_ptr
,
810 com_err(ctx
->program_name
, retval
,
811 _("while trying to re-open %s"),
815 ctx
->fs
->priv_data
= ctx
;
817 /* Set the superblock flags */
818 e2fsck_clear_recover(ctx
, recover_retval
);
819 return recover_retval
;
823 * This function will move the journal inode from a visible file in
824 * the filesystem directory hierarchy to the reserved inode if necessary.
826 static const char * const journal_names
[] = {
827 ".journal", "journal", ".journal.dat", "journal.dat", 0 };
829 void e2fsck_move_ext3_journal(e2fsck_t ctx
)
831 struct ext2_super_block
*sb
= ctx
->fs
->super
;
832 struct problem_context pctx
;
833 struct ext2_inode inode
;
834 ext2_filsys fs
= ctx
->fs
;
837 const char * const * cpp
;
838 int group
, mount_flags
;
840 clear_problem_context(&pctx
);
843 * If the filesystem is opened read-only, or there is no
844 * journal, then do nothing.
846 if ((ctx
->options
& E2F_OPT_READONLY
) ||
847 (sb
->s_journal_inum
== 0) ||
848 !(sb
->s_feature_compat
& EXT3_FEATURE_COMPAT_HAS_JOURNAL
))
852 * Read in the journal inode
854 if (ext2fs_read_inode(fs
, sb
->s_journal_inum
, &inode
) != 0)
858 * If it's necessary to backup the journal inode, do so.
860 if ((sb
->s_jnl_backup_type
== 0) ||
861 ((sb
->s_jnl_backup_type
== EXT3_JNL_BACKUP_BLOCKS
) &&
862 memcmp(inode
.i_block
, sb
->s_jnl_blocks
, EXT2_N_BLOCKS
*4))) {
863 if (fix_problem(ctx
, PR_0_BACKUP_JNL
, &pctx
)) {
864 memcpy(sb
->s_jnl_blocks
, inode
.i_block
,
866 sb
->s_jnl_blocks
[16] = inode
.i_size
;
867 sb
->s_jnl_backup_type
= EXT3_JNL_BACKUP_BLOCKS
;
868 ext2fs_mark_super_dirty(fs
);
869 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
874 * If the journal is already the hidden inode, then do nothing
876 if (sb
->s_journal_inum
== EXT2_JOURNAL_INO
)
880 * The journal inode had better have only one link and not be readable.
882 if (inode
.i_links_count
!= 1)
886 * If the filesystem is mounted, or we can't tell whether
887 * or not it's mounted, do nothing.
889 retval
= ext2fs_check_if_mounted(ctx
->filesystem_name
, &mount_flags
);
890 if (retval
|| (mount_flags
& EXT2_MF_MOUNTED
))
894 * If we can't find the name of the journal inode, then do
897 for (cpp
= journal_names
; *cpp
; cpp
++) {
898 retval
= ext2fs_lookup(fs
, EXT2_ROOT_INO
, *cpp
,
899 strlen(*cpp
), 0, &ino
);
900 if ((retval
== 0) && (ino
== sb
->s_journal_inum
))
906 /* We need the inode bitmap to be loaded */
907 retval
= ext2fs_read_bitmaps(fs
);
912 if (!fix_problem(ctx
, PR_0_MOVE_JOURNAL
, &pctx
))
916 * OK, we've done all the checks, let's actually move the
917 * journal inode. Errors at this point mean we need to force
918 * an ext2 filesystem check.
920 if ((retval
= ext2fs_unlink(fs
, EXT2_ROOT_INO
, *cpp
, ino
, 0)) != 0)
922 if ((retval
= ext2fs_write_inode(fs
, EXT2_JOURNAL_INO
, &inode
)) != 0)
924 sb
->s_journal_inum
= EXT2_JOURNAL_INO
;
925 ext2fs_mark_super_dirty(fs
);
926 fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
927 inode
.i_links_count
= 0;
928 inode
.i_dtime
= time(0);
929 if ((retval
= ext2fs_write_inode(fs
, ino
, &inode
)) != 0)
932 group
= ext2fs_group_of_ino(fs
, ino
);
933 ext2fs_unmark_inode_bitmap(fs
->inode_map
, ino
);
934 ext2fs_mark_ib_dirty(fs
);
935 fs
->group_desc
[group
].bg_free_inodes_count
++;
936 fs
->super
->s_free_inodes_count
++;
940 pctx
.errcode
= retval
;
941 fix_problem(ctx
, PR_0_ERR_MOVE_JOURNAL
, &pctx
);
942 fs
->super
->s_state
&= ~EXT2_VALID_FS
;
943 ext2fs_mark_super_dirty(fs
);