2 * do_journal.c --- Scribble onto the journal!
4 * Copyright (C) 2014 Oracle. This file may be redistributed
5 * under the terms of the GNU Public License.
18 #ifdef HAVE_SYS_TIME_H
23 #include "ext2fs/kernel-jbd.h"
29 # define dbg_printf(f, a...) do {printf("JFS DEBUG: " f, ## a); \
33 # define dbg_printf(f, a...)
36 #define JOURNAL_CHECK_TRANS_MAGIC(x) \
38 if ((x)->magic != J_TRANS_MAGIC) \
39 return EXT2_ET_INVALID_ARGUMENT; \
42 #define J_TRANS_MAGIC 0xD15EA5ED
43 #define J_TRANS_OPEN 1
44 #define J_TRANS_COMMITTED 2
45 struct journal_transaction_s
{
55 typedef struct journal_transaction_s journal_transaction_t
;
57 static journal_t
*current_journal
= NULL
;
59 static void journal_dump_trans(journal_transaction_t
*trans
EXT2FS_ATTR((unused
)),
60 const char *tag
EXT2FS_ATTR((unused
)))
62 dbg_printf("TRANS %p(%s): tid=%d start=%llu block=%llu end=%llu "
63 "flags=0x%x\n", trans
, tag
, trans
->tid
, trans
->start
,
64 trans
->block
, trans
->end
, trans
->flags
);
67 static errcode_t
journal_commit_trans(journal_transaction_t
*trans
)
69 struct buffer_head
*bh
, *cbh
= NULL
;
70 struct commit_header
*commit
;
71 #ifdef HAVE_SYS_TIME_H
76 JOURNAL_CHECK_TRANS_MAGIC(trans
);
78 if ((trans
->flags
& J_TRANS_COMMITTED
) ||
79 !(trans
->flags
& J_TRANS_OPEN
))
80 return EXT2_ET_INVALID_ARGUMENT
;
82 bh
= getblk(trans
->journal
->j_dev
, 0, trans
->journal
->j_blocksize
);
86 /* write the descriptor block header */
87 commit
= (struct commit_header
*)bh
->b_data
;
88 commit
->h_magic
= ext2fs_cpu_to_be32(JFS_MAGIC_NUMBER
);
89 commit
->h_blocktype
= ext2fs_cpu_to_be32(JFS_COMMIT_BLOCK
);
90 commit
->h_sequence
= ext2fs_cpu_to_be32(trans
->tid
);
91 if (jfs_has_feature_checksum(trans
->journal
)) {
95 cbh
= getblk(trans
->journal
->j_dev
, 0,
96 trans
->journal
->j_blocksize
);
102 for (cblk
= trans
->start
; cblk
< trans
->block
; cblk
++) {
103 err
= journal_bmap(trans
->journal
, cblk
,
107 mark_buffer_uptodate(cbh
, 0);
108 ll_rw_block(READ
, 1, &cbh
);
112 csum_v1
= ext2fs_crc32_be(csum_v1
,
113 (unsigned char const *)cbh
->b_data
,
117 commit
->h_chksum_type
= JFS_CRC32_CHKSUM
;
118 commit
->h_chksum_size
= JFS_CRC32_CHKSUM_SIZE
;
119 commit
->h_chksum
[0] = ext2fs_cpu_to_be32(csum_v1
);
121 commit
->h_chksum_type
= 0;
122 commit
->h_chksum_size
= 0;
123 commit
->h_chksum
[0] = 0;
125 #ifdef HAVE_SYS_TIME_H
126 gettimeofday(&tv
, NULL
);
127 commit
->h_commit_sec
= ext2fs_cpu_to_be32(tv
.tv_sec
);
128 commit
->h_commit_nsec
= ext2fs_cpu_to_be32(tv
.tv_usec
* 1000);
130 commit
->h_commit_sec
= 0;
131 commit
->h_commit_nsec
= 0;
135 jbd2_commit_block_csum_set(trans
->journal
, bh
);
136 err
= journal_bmap(trans
->journal
, trans
->block
, &bh
->b_blocknr
);
140 dbg_printf("Writing commit block at %llu:%llu\n", trans
->block
,
142 mark_buffer_dirty(bh
);
143 ll_rw_block(WRITE
, 1, &bh
);
147 trans
->flags
|= J_TRANS_COMMITTED
;
148 trans
->flags
&= ~J_TRANS_OPEN
;
151 ext2fs_set_feature_journal_needs_recovery(trans
->fs
->super
);
152 ext2fs_mark_super_dirty(trans
->fs
);
160 static errcode_t
journal_add_revoke_to_trans(journal_transaction_t
*trans
,
161 blk64_t
*revoke_list
,
164 journal_revoke_header_t
*jrb
;
169 unsigned csum_size
= 0;
170 struct buffer_head
*bh
;
173 JOURNAL_CHECK_TRANS_MAGIC(trans
);
175 if ((trans
->flags
& J_TRANS_COMMITTED
) ||
176 !(trans
->flags
& J_TRANS_OPEN
))
177 return EXT2_ET_INVALID_ARGUMENT
;
182 /* Do we need to leave space at the end for a checksum? */
183 if (journal_has_csum_v2or3(trans
->journal
))
184 csum_size
= sizeof(struct journal_revoke_tail
);
186 curr_blk
= trans
->block
;
188 bh
= getblk(trans
->journal
->j_dev
, curr_blk
,
189 trans
->journal
->j_blocksize
);
192 jrb
= buf
= bh
->b_data
;
193 jrb
->r_header
.h_magic
= ext2fs_cpu_to_be32(JFS_MAGIC_NUMBER
);
194 jrb
->r_header
.h_blocktype
= ext2fs_cpu_to_be32(JFS_REVOKE_BLOCK
);
195 jrb
->r_header
.h_sequence
= ext2fs_cpu_to_be32(trans
->tid
);
196 offset
= sizeof(*jrb
);
198 if (jfs_has_feature_64bit(trans
->journal
))
203 for (i
= 0; i
< revoke_len
; i
++) {
204 /* Block full, write to journal */
205 if (offset
+ sz
> trans
->journal
->j_blocksize
- csum_size
) {
206 jrb
->r_count
= ext2fs_cpu_to_be32(offset
);
207 jbd2_revoke_csum_set(trans
->journal
, bh
);
209 err
= journal_bmap(trans
->journal
, curr_blk
,
213 dbg_printf("Writing revoke block at %llu:%llu\n",
214 curr_blk
, bh
->b_blocknr
);
215 mark_buffer_dirty(bh
);
216 ll_rw_block(WRITE
, 1, &bh
);
221 offset
= sizeof(*jrb
);
225 if (revoke_list
[i
] >=
226 ext2fs_blocks_count(trans
->journal
->j_fs_dev
->k_fs
->super
)) {
227 err
= EXT2_ET_BAD_BLOCK_NUM
;
231 if (jfs_has_feature_64bit(trans
->journal
))
232 *((__u64
*)(&((char *)buf
)[offset
])) =
233 ext2fs_cpu_to_be64(revoke_list
[i
]);
235 *((__u32
*)(&((char *)buf
)[offset
])) =
236 ext2fs_cpu_to_be32(revoke_list
[i
]);
241 jrb
->r_count
= ext2fs_cpu_to_be32(offset
);
242 jbd2_revoke_csum_set(trans
->journal
, bh
);
244 err
= journal_bmap(trans
->journal
, curr_blk
, &bh
->b_blocknr
);
247 dbg_printf("Writing revoke block at %llu:%llu\n",
248 curr_blk
, bh
->b_blocknr
);
249 mark_buffer_dirty(bh
);
250 ll_rw_block(WRITE
, 1, &bh
);
258 trans
->block
= curr_blk
;
263 static errcode_t
journal_add_blocks_to_trans(journal_transaction_t
*trans
,
264 blk64_t
*block_list
, size_t block_len
,
267 blk64_t curr_blk
, jdb_blk
;
270 journal_header_t
*jdb
;
271 journal_block_tag_t
*jdbt
;
273 void *buf
= NULL
, *jdb_buf
= NULL
;
274 struct buffer_head
*bh
= NULL
, *data_bh
;
277 JOURNAL_CHECK_TRANS_MAGIC(trans
);
279 if ((trans
->flags
& J_TRANS_COMMITTED
) ||
280 !(trans
->flags
& J_TRANS_OPEN
))
281 return EXT2_ET_INVALID_ARGUMENT
;
286 /* Do we need to leave space at the end for a checksum? */
287 if (journal_has_csum_v2or3(trans
->journal
))
288 csum_size
= sizeof(struct journal_block_tail
);
290 curr_blk
= jdb_blk
= trans
->block
;
292 data_bh
= getblk(trans
->journal
->j_dev
, curr_blk
,
293 trans
->journal
->j_blocksize
);
296 buf
= data_bh
->b_data
;
298 /* write the descriptor block header */
299 bh
= getblk(trans
->journal
->j_dev
, curr_blk
,
300 trans
->journal
->j_blocksize
);
305 jdb
= jdb_buf
= bh
->b_data
;
306 jdb
->h_magic
= ext2fs_cpu_to_be32(JFS_MAGIC_NUMBER
);
307 jdb
->h_blocktype
= ext2fs_cpu_to_be32(JFS_DESCRIPTOR_BLOCK
);
308 jdb
->h_sequence
= ext2fs_cpu_to_be32(trans
->tid
);
309 jdbt
= (journal_block_tag_t
*)(jdb
+ 1);
312 for (i
= 0; i
< block_len
; i
++) {
313 j
= fread(data_bh
->b_data
, trans
->journal
->j_blocksize
, 1, fp
);
319 tag_bytes
= journal_tag_bytes(trans
->journal
);
321 /* No space left in descriptor block, write it out */
322 if ((char *)jdbt
+ tag_bytes
>
323 (char *)jdb_buf
+ trans
->journal
->j_blocksize
- csum_size
) {
324 jbd2_descr_block_csum_set(trans
->journal
, bh
);
325 err
= journal_bmap(trans
->journal
, jdb_blk
,
329 dbg_printf("Writing descriptor block at %llu:%llu\n",
330 jdb_blk
, bh
->b_blocknr
);
331 mark_buffer_dirty(bh
);
332 ll_rw_block(WRITE
, 1, &bh
);
337 jdbt
= (journal_block_tag_t
*)(jdb
+ 1);
343 ext2fs_blocks_count(trans
->journal
->j_fs_dev
->k_fs
->super
)) {
344 err
= EXT2_ET_BAD_BLOCK_NUM
;
348 /* Fill out the block tag */
349 jdbt
->t_blocknr
= ext2fs_cpu_to_be32(block_list
[i
] & 0xFFFFFFFF);
351 if (jdbt
!= (journal_block_tag_t
*)(jdb
+ 1))
352 jdbt
->t_flags
|= ext2fs_cpu_to_be16(JFS_FLAG_SAME_UUID
);
354 memcpy(jdbt
+ tag_bytes
,
355 trans
->journal
->j_superblock
->s_uuid
,
356 sizeof(trans
->journal
->j_superblock
->s_uuid
));
359 if (i
== block_len
- 1)
360 jdbt
->t_flags
|= ext2fs_cpu_to_be16(JFS_FLAG_LAST_TAG
);
361 if (*((__u32
*)buf
) == ext2fs_cpu_to_be32(JFS_MAGIC_NUMBER
)) {
363 jdbt
->t_flags
|= ext2fs_cpu_to_be16(JFS_FLAG_ESCAPE
);
365 if (jfs_has_feature_64bit(trans
->journal
))
366 jdbt
->t_blocknr_high
= ext2fs_cpu_to_be32(block_list
[i
] >> 32);
367 jbd2_block_tag_csum_set(trans
->journal
, jdbt
, data_bh
,
370 /* Write the data block */
371 err
= journal_bmap(trans
->journal
, curr_blk
,
372 &data_bh
->b_blocknr
);
375 dbg_printf("Writing data block %llu at %llu:%llu tag %d\n",
376 block_list
[i
], curr_blk
, data_bh
->b_blocknr
,
378 mark_buffer_dirty(data_bh
);
379 ll_rw_block(WRITE
, 1, &data_bh
);
380 err
= data_bh
->b_err
;
385 jdbt
= (journal_block_tag_t
*)(((char *)jdbt
) + tag_bytes
);
388 /* Write out the last descriptor block */
389 if (jdbt
!= (journal_block_tag_t
*)(jdb
+ 1)) {
390 jbd2_descr_block_csum_set(trans
->journal
, bh
);
391 err
= journal_bmap(trans
->journal
, jdb_blk
, &bh
->b_blocknr
);
394 dbg_printf("Writing descriptor block at %llu:%llu\n",
395 jdb_blk
, bh
->b_blocknr
);
396 mark_buffer_dirty(bh
);
397 ll_rw_block(WRITE
, 1, &bh
);
404 trans
->block
= curr_blk
;
411 static blk64_t
journal_guess_blocks(journal_t
*journal
, blk64_t data_blocks
,
412 blk64_t revoke_blocks
)
417 /* Estimate # of revoke blocks */
418 bs
= journal
->j_blocksize
;
419 if (journal_has_csum_v2or3(journal
))
420 bs
-= sizeof(struct journal_revoke_tail
);
421 sz
= jfs_has_feature_64bit(journal
) ? sizeof(__u64
) : sizeof(__u32
);
422 ret
+= revoke_blocks
* sz
/ bs
;
424 /* Estimate # of data blocks */
425 bs
= journal
->j_blocksize
- 16;
426 if (journal_has_csum_v2or3(journal
))
427 bs
-= sizeof(struct journal_block_tail
);
428 sz
= journal_tag_bytes(journal
);
429 ret
+= data_blocks
* sz
/ bs
;
436 static errcode_t
journal_open_trans(journal_t
*journal
,
437 journal_transaction_t
*trans
,
440 trans
->fs
= journal
->j_fs_dev
->k_fs
;
441 trans
->journal
= journal
;
442 trans
->flags
= J_TRANS_OPEN
;
444 if (journal
->j_tail
== 0) {
445 /* Clean journal, start at the tail */
446 trans
->tid
= journal
->j_tail_sequence
;
447 trans
->start
= journal
->j_first
;
449 /* Put new transaction at the head of the list */
450 trans
->tid
= journal
->j_transaction_sequence
;
451 trans
->start
= journal
->j_head
;
454 trans
->block
= trans
->start
;
455 if (trans
->start
+ blocks
> journal
->j_last
)
457 trans
->end
= trans
->block
+ blocks
;
458 journal_dump_trans(trans
, "new transaction");
460 trans
->magic
= J_TRANS_MAGIC
;
464 static errcode_t
journal_close_trans(journal_transaction_t
*trans
)
468 JOURNAL_CHECK_TRANS_MAGIC(trans
);
470 if (!(trans
->flags
& J_TRANS_COMMITTED
))
473 journal
= trans
->journal
;
474 if (journal
->j_tail
== 0) {
475 /* Update the tail */
476 journal
->j_tail_sequence
= trans
->tid
;
477 journal
->j_tail
= trans
->start
;
478 journal
->j_superblock
->s_start
= ext2fs_cpu_to_be32(trans
->start
);
481 /* Update the head */
482 journal
->j_head
= trans
->end
+ 1;
483 journal
->j_transaction_sequence
= trans
->tid
+ 1;
487 /* Mark ourselves as needing recovery */
488 if (!ext2fs_has_feature_journal_needs_recovery(trans
->fs
->super
)) {
489 ext2fs_set_feature_journal_needs_recovery(trans
->fs
->super
);
490 ext2fs_mark_super_dirty(trans
->fs
);
496 #define JOURNAL_WRITE_NO_COMMIT 1
497 static errcode_t
journal_write(journal_t
*journal
,
498 int flags
, blk64_t
*block_list
,
499 size_t block_len
, blk64_t
*revoke_list
,
500 size_t revoke_len
, FILE *fp
)
503 journal_transaction_t trans
;
506 if (revoke_len
> 0) {
507 jfs_set_feature_revoke(journal
);
508 mark_buffer_dirty(journal
->j_sb_buffer
);
511 blocks
= journal_guess_blocks(journal
, block_len
, revoke_len
);
512 err
= journal_open_trans(journal
, &trans
, blocks
);
516 err
= journal_add_blocks_to_trans(&trans
, block_list
, block_len
, fp
);
520 err
= journal_add_revoke_to_trans(&trans
, revoke_list
, revoke_len
);
524 if (!(flags
& JOURNAL_WRITE_NO_COMMIT
)) {
525 err
= journal_commit_trans(&trans
);
530 err
= journal_close_trans(&trans
);
537 void do_journal_write(int argc
, char *argv
[])
539 blk64_t
*blist
= NULL
, *rlist
= NULL
;
540 size_t bn
= 0, rn
= 0;
546 if (current_journal
== NULL
) {
547 printf("Journal not open.\n");
552 while ((opt
= getopt(argc
, argv
, "b:r:c")) != -1) {
555 err
= read_list(optarg
, &blist
, &bn
);
557 com_err(argv
[0], err
,
558 "while reading block list");
561 err
= read_list(optarg
, &rlist
, &rn
);
563 com_err(argv
[0], err
,
564 "while reading revoke list");
567 flags
|= JOURNAL_WRITE_NO_COMMIT
;
570 printf("%s [-b blocks] [-r revoke] [-c] file\n",
572 printf("-b: Write these blocks into transaction.\n");
573 printf("-c: Do not commit transaction.\n");
574 printf("-r: Revoke these blocks from transaction.\n");
580 if (bn
> 0 && optind
!= argc
- 1) {
581 printf("Need a file to read blocks from.\n");
586 fp
= fopen(argv
[optind
], "r");
588 com_err(argv
[0], errno
,
589 "while opening journal data file");
594 err
= journal_write(current_journal
, flags
, blist
, bn
,
597 com_err("journal_write", err
, "while writing journal");
608 /* Make sure we wrap around the log correctly! */
609 #define wrap(journal, var) \
611 if (var >= (journal)->j_last) \
612 var -= ((journal)->j_last - (journal)->j_first); \
616 * Count the number of in-use tags in a journal descriptor block.
619 static int count_tags(journal_t
*journal
, char *buf
)
622 journal_block_tag_t
*tag
;
623 int nr
= 0, size
= journal
->j_blocksize
;
624 int tag_bytes
= journal_tag_bytes(journal
);
626 if (journal_has_csum_v2or3(journal
))
627 size
-= sizeof(struct journal_block_tail
);
629 tagp
= buf
+ sizeof(journal_header_t
);
631 while ((tagp
- buf
+ tag_bytes
) <= size
) {
632 tag
= (journal_block_tag_t
*) tagp
;
636 if (!(tag
->t_flags
& ext2fs_cpu_to_be16(JFS_FLAG_SAME_UUID
)))
639 if (tag
->t_flags
& ext2fs_cpu_to_be16(JFS_FLAG_LAST_TAG
))
646 static errcode_t
journal_find_head(journal_t
*journal
)
648 unsigned int next_commit_ID
;
649 blk64_t next_log_block
, head_block
;
651 journal_superblock_t
*sb
;
652 journal_header_t
*tmp
;
653 struct buffer_head
*bh
;
654 unsigned int sequence
;
658 * First thing is to establish what we expect to find in the log
659 * (in terms of transaction IDs), and where (in terms of log
660 * block offsets): query the superblock.
663 sb
= journal
->j_superblock
;
664 next_commit_ID
= ext2fs_be32_to_cpu(sb
->s_sequence
);
665 next_log_block
= ext2fs_be32_to_cpu(sb
->s_start
);
666 head_block
= next_log_block
;
668 if (next_log_block
== 0)
671 bh
= getblk(journal
->j_dev
, 0, journal
->j_blocksize
);
676 * Now we walk through the log, transaction by transaction,
677 * making sure that each transaction has a commit block in the
678 * expected place. Each complete transaction gets replayed back
679 * into the main filesystem.
682 dbg_printf("Scanning for sequence ID %u at %lu/%lu\n",
683 next_commit_ID
, (unsigned long)next_log_block
,
686 /* Skip over each chunk of the transaction looking
687 * either the next descriptor block or the final commit
689 err
= journal_bmap(journal
, next_log_block
, &bh
->b_blocknr
);
692 mark_buffer_uptodate(bh
, 0);
693 ll_rw_block(READ
, 1, &bh
);
699 wrap(journal
, next_log_block
);
701 /* What kind of buffer is it?
703 * If it is a descriptor block, check that it has the
704 * expected sequence number. Otherwise, we're all done
707 tmp
= (journal_header_t
*)bh
->b_data
;
709 if (tmp
->h_magic
!= ext2fs_cpu_to_be32(JFS_MAGIC_NUMBER
)) {
710 dbg_printf("JBD2: wrong magic 0x%x\n", tmp
->h_magic
);
714 blocktype
= ext2fs_be32_to_cpu(tmp
->h_blocktype
);
715 sequence
= ext2fs_be32_to_cpu(tmp
->h_sequence
);
716 dbg_printf("Found magic %d, sequence %d\n",
717 blocktype
, sequence
);
719 if (sequence
!= next_commit_ID
) {
720 dbg_printf("JBD2: Wrong sequence %d (wanted %d)\n",
721 sequence
, next_commit_ID
);
725 /* OK, we have a valid descriptor block which matches
726 * all of the sequence number checks. What are we going
727 * to do with it? That depends on the pass... */
730 case JFS_DESCRIPTOR_BLOCK
:
731 next_log_block
+= count_tags(journal
, bh
->b_data
);
732 wrap(journal
, next_log_block
);
735 case JFS_COMMIT_BLOCK
:
736 head_block
= next_log_block
;
740 case JFS_REVOKE_BLOCK
:
744 dbg_printf("Unrecognised magic %d, end of scan.\n",
753 dbg_printf("head seq=%d blk=%llu\n", next_commit_ID
,
755 journal
->j_transaction_sequence
= next_commit_ID
;
756 journal
->j_head
= head_block
;
762 static void update_journal_csum(journal_t
*journal
, int ver
)
764 journal_superblock_t
*jsb
;
766 if (journal
->j_format_version
< 2)
769 if (journal
->j_tail
!= 0 ||
770 ext2fs_has_feature_journal_needs_recovery(
771 journal
->j_fs_dev
->k_fs
->super
)) {
772 printf("Journal needs recovery, will not add csums.\n");
776 /* metadata_csum implies journal csum v3 */
777 jsb
= journal
->j_superblock
;
778 if (ext2fs_has_feature_metadata_csum(journal
->j_fs_dev
->k_fs
->super
)) {
779 printf("Setting csum v%d\n", ver
);
782 jfs_clear_feature_csum3(journal
);
783 jfs_set_feature_csum2(journal
);
784 jfs_clear_feature_checksum(journal
);
787 jfs_set_feature_csum3(journal
);
788 jfs_clear_feature_csum2(journal
);
789 jfs_clear_feature_checksum(journal
);
792 printf("Unknown checksum v%d\n", ver
);
795 journal
->j_superblock
->s_checksum_type
= JBD2_CRC32C_CHKSUM
;
796 journal
->j_csum_seed
= jbd2_chksum(journal
, ~0, jsb
->s_uuid
,
797 sizeof(jsb
->s_uuid
));
799 jfs_clear_feature_csum3(journal
);
800 jfs_clear_feature_csum2(journal
);
801 jfs_set_feature_checksum(journal
);
805 static void update_uuid(journal_t
*journal
)
810 if (journal
->j_format_version
< 2)
813 for (z
= 0; z
< sizeof(journal
->j_superblock
->s_uuid
); z
++)
814 if (journal
->j_superblock
->s_uuid
[z
])
819 fs
= journal
->j_fs_dev
->k_fs
;
820 if (!ext2fs_has_feature_64bit(fs
->super
))
823 if (jfs_has_feature_64bit(journal
) &&
824 ext2fs_has_feature_64bit(fs
->super
))
827 if (journal
->j_tail
!= 0 ||
828 ext2fs_has_feature_journal_needs_recovery(fs
->super
)) {
829 printf("Journal needs recovery, will not set 64bit.\n");
833 memcpy(journal
->j_superblock
->s_uuid
, fs
->super
->s_uuid
,
834 sizeof(fs
->super
->s_uuid
));
837 static void update_64bit_flag(journal_t
*journal
)
839 if (journal
->j_format_version
< 2)
842 if (!ext2fs_has_feature_64bit(journal
->j_fs_dev
->k_fs
->super
))
845 if (jfs_has_feature_64bit(journal
) &&
846 ext2fs_has_feature_64bit(journal
->j_fs_dev
->k_fs
->super
))
849 if (journal
->j_tail
!= 0 ||
850 ext2fs_has_feature_journal_needs_recovery(
851 journal
->j_fs_dev
->k_fs
->super
)) {
852 printf("Journal needs recovery, will not set 64bit.\n");
856 jfs_set_feature_64bit(journal
);
859 void do_journal_open(int argc
, char *argv
[])
861 int opt
, enable_csum
= 0, csum_ver
= 3;
865 if (check_fs_open(argv
[0]))
867 if (check_fs_read_write(argv
[0]))
869 if (check_fs_bitmaps(argv
[0]))
871 if (current_journal
) {
872 printf("Journal is already open.\n");
875 if (!ext2fs_has_feature_journal(current_fs
->super
)) {
876 printf("Journalling is not enabled on this filesystem.\n");
881 while ((opt
= getopt(argc
, argv
, "cv:f:")) != -1) {
887 if (current_fs
->journal_name
)
888 free(current_fs
->journal_name
);
889 current_fs
->journal_name
= strdup(optarg
);
892 csum_ver
= atoi(optarg
);
893 if (csum_ver
!= 2 && csum_ver
!= 3) {
894 printf("Unknown journal csum v%d\n", csum_ver
);
899 printf("%s: [-c] [-v ver] [-f ext_jnl]\n", argv
[0]);
900 printf("-c: Enable journal checksumming.\n");
901 printf("-v: Use this version checksum format.\n");
902 printf("-j: Load this external journal.\n");
906 err
= ext2fs_open_journal(current_fs
, ¤t_journal
);
908 com_err(argv
[0], err
, "while opening journal");
911 journal
= current_journal
;
913 dbg_printf("JOURNAL: seq=%d tailseq=%d start=%lu first=%lu "
914 "maxlen=%lu\n", journal
->j_tail_sequence
,
915 journal
->j_transaction_sequence
, journal
->j_tail
,
916 journal
->j_first
, journal
->j_last
);
918 update_uuid(journal
);
919 update_64bit_flag(journal
);
921 update_journal_csum(journal
, csum_ver
);
923 err
= journal_find_head(journal
);
925 com_err(argv
[0], err
, "while examining journal");
928 void do_journal_close(int argc
EXT2FS_ATTR((unused
)),
929 char *argv
[] EXT2FS_ATTR((unused
)))
931 if (current_journal
== NULL
) {
932 printf("Journal not open.\n");
936 ext2fs_close_journal(current_fs
, ¤t_journal
);
939 void do_journal_run(int argc
EXT2FS_ATTR((unused
)), char *argv
[])
943 if (check_fs_open(argv
[0]))
945 if (check_fs_read_write(argv
[0]))
947 if (check_fs_bitmaps(argv
[0]))
949 if (current_journal
) {
950 printf("Please close the journal before recovering it.\n");
954 err
= ext2fs_run_ext3_journal(¤t_fs
);
956 com_err("journal_run", err
, "while recovering journal");
958 ext2fs_clear_feature_journal_needs_recovery(current_fs
->super
);
959 ext2fs_mark_super_dirty(current_fs
);