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
16 #ifdef HAVE_SYS_MOUNT_H
17 #include <sys/param.h>
18 #include <sys/mount.h>
19 #define MNT_FL (MS_MGC_VAL | MS_RDONLY)
21 #ifdef HAVE_SYS_STAT_H
25 #define E2FSCK_INCLUDE_INLINE_FUNCS
26 #include "uuid/uuid.h"
29 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
30 static int bh_count = 0;
33 #if EXT2_FLAT_INCLUDES
36 #include "blkid/blkid.h"
40 * Define USE_INODE_IO to use the inode_io.c / fileio.c codepaths.
41 * This creates a larger static binary, and a smaller binary using
42 * shared libraries. It's also probably slightly less CPU-efficient,
43 * which is why it's not on by default. But, it's a good way of
44 * testing the functions in inode_io.c and fileio.c.
48 /* Checksumming functions */
49 static int ext2fs_journal_verify_csum_type(journal_t *j,
50 journal_superblock_t *jsb)
52 if (!jbd2_journal_has_csum_v2or3(j))
55 return jsb->s_checksum_type == JBD2_CRC32C_CHKSUM;
58 static __u32 ext2fs_journal_sb_csum(journal_superblock_t *jsb)
62 old_crc = jsb->s_checksum;
64 crc = ext2fs_crc32c_le(~0, (unsigned char *)jsb,
65 sizeof(journal_superblock_t));
66 jsb->s_checksum = old_crc;
71 static int ext2fs_journal_sb_csum_verify(journal_t *j,
72 journal_superblock_t *jsb)
74 __u32 provided, calculated;
76 if (!jbd2_journal_has_csum_v2or3(j))
79 provided = ext2fs_be32_to_cpu(jsb->s_checksum);
80 calculated = ext2fs_journal_sb_csum(jsb);
82 return provided == calculated;
85 static errcode_t ext2fs_journal_sb_csum_set(journal_t *j,
86 journal_superblock_t *jsb)
90 if (!jbd2_journal_has_csum_v2or3(j))
93 crc = ext2fs_journal_sb_csum(jsb);
94 jsb->s_checksum = ext2fs_cpu_to_be32(crc);
98 /* Kernel compatibility functions for handling the journal. These allow us
99 * to use the recovery.c file virtually unchanged from the kernel, so we
100 * don't have to do much to keep kernel and user recovery in sync.
102 int jbd2_journal_bmap(journal_t *journal, blk64_t block,
103 unsigned long long *phys)
109 struct inode *inode = journal->j_inode;
118 retval = ext2fs_bmap2(inode->i_fs, inode->i_ino,
119 &inode->i_ext2, NULL, 0, block, 0, &pblk);
125 struct buffer_head *getblk(kdev_t kdev, blk64_t blocknr, int blocksize)
127 struct buffer_head *bh;
128 int bufsize = sizeof(*bh) + kdev->k_fs->blocksize -
132 retval = ext2fs_get_memzero(bufsize, &bh);
136 #ifdef CONFIG_JBD_DEBUG
137 if (journal_enable_debug >= 3)
140 jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
141 (unsigned long long) blocknr, blocksize, bh_count);
143 bh->b_fs = kdev->k_fs;
144 if (kdev->k_dev == K_DEV_FS)
145 bh->b_io = kdev->k_fs->io;
147 bh->b_io = kdev->k_fs->journal_io;
148 bh->b_size = blocksize;
149 bh->b_blocknr = blocknr;
154 int sync_blockdev(kdev_t kdev)
158 if (kdev->k_dev == K_DEV_FS)
161 io = kdev->k_fs->journal_io;
163 return io_channel_flush(io) ? EIO : 0;
166 void ll_rw_block(int rw, int op_flags, int nr, struct buffer_head *bhp[])
169 struct buffer_head *bh;
171 for (; nr > 0; --nr) {
173 if (rw == REQ_OP_READ && !bh->b_uptodate) {
174 jfs_debug(3, "reading block %llu/%p\n",
175 bh->b_blocknr, (void *) bh);
176 retval = io_channel_read_blk64(bh->b_io,
180 com_err(bh->b_fs->device_name, retval,
181 "while reading block %llu\n",
183 bh->b_err = (int) retval;
187 } else if (rw == REQ_OP_WRITE && bh->b_dirty) {
188 jfs_debug(3, "writing block %llu/%p\n",
191 retval = io_channel_write_blk64(bh->b_io,
195 com_err(bh->b_fs->device_name, retval,
196 "while writing block %llu\n",
198 bh->b_err = (int) retval;
204 jfs_debug(3, "no-op %s for block %llu\n",
205 rw == REQ_OP_READ ? "read" : "write",
211 void mark_buffer_dirty(struct buffer_head *bh)
216 static void mark_buffer_clean(struct buffer_head *bh)
221 void brelse(struct buffer_head *bh)
224 ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
225 jfs_debug(3, "freeing block %llu/%p (total %d)\n",
226 bh->b_blocknr, (void *) bh, --bh_count);
227 ext2fs_free_mem(&bh);
230 int buffer_uptodate(struct buffer_head *bh)
232 return bh->b_uptodate;
235 void mark_buffer_uptodate(struct buffer_head *bh, int val)
237 bh->b_uptodate = val;
240 void wait_on_buffer(struct buffer_head *bh)
243 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
247 static void ext2fs_clear_recover(ext2_filsys fs, int error)
249 ext2fs_clear_feature_journal_needs_recovery(fs->super);
251 /* if we had an error doing journal recovery, we need a full fsck */
253 fs->super->s_state &= ~EXT2_VALID_FS;
255 * If we replayed the journal by definition the file system
256 * was mounted since the last time it was checked
258 if (fs->super->s_lastcheck >= fs->super->s_mtime)
259 fs->super->s_lastcheck = fs->super->s_mtime - 1;
260 ext2fs_mark_super_dirty(fs);
264 * This is a helper function to check the validity of the journal.
266 struct process_block_struct {
267 e2_blkcnt_t last_block;
270 static int process_journal_block(ext2_filsys fs,
272 e2_blkcnt_t blockcnt,
273 blk64_t ref_block EXT2FS_ATTR((unused)),
274 int ref_offset EXT2FS_ATTR((unused)),
277 struct process_block_struct *p;
278 blk64_t blk = *block_nr;
280 p = (struct process_block_struct *) priv_data;
282 if (!blk || blk < fs->super->s_first_data_block ||
283 blk >= ext2fs_blocks_count(fs->super))
287 p->last_block = blockcnt;
291 static errcode_t ext2fs_get_journal(ext2_filsys fs, journal_t **ret_journal)
293 struct process_block_struct pb;
294 struct ext2_super_block *sb = fs->super;
295 struct ext2_super_block jsuper;
296 struct buffer_head *bh;
297 struct inode *j_inode = NULL;
298 struct kdev_s *dev_fs = NULL, *dev_journal;
299 const char *journal_name = 0;
300 journal_t *journal = NULL;
301 errcode_t retval = 0;
302 io_manager io_ptr = 0;
303 unsigned long long start = 0;
305 int tried_backup_jnl = 0;
307 retval = ext2fs_get_memzero(sizeof(journal_t), &journal);
311 retval = ext2fs_get_memzero(2 * sizeof(struct kdev_s), &dev_fs);
314 dev_journal = dev_fs+1;
316 dev_fs->k_fs = dev_journal->k_fs = fs;
317 dev_fs->k_dev = K_DEV_FS;
318 dev_journal->k_dev = K_DEV_JOURNAL;
320 journal->j_dev = dev_journal;
321 journal->j_fs_dev = dev_fs;
322 journal->j_inode = NULL;
323 journal->j_blocksize = fs->blocksize;
325 if (uuid_is_null(sb->s_journal_uuid)) {
326 if (!sb->s_journal_inum) {
327 retval = EXT2_ET_BAD_INODE_NUM;
330 retval = ext2fs_get_memzero(sizeof(*j_inode), &j_inode);
335 j_inode->i_ino = sb->s_journal_inum;
337 retval = ext2fs_read_inode(fs, sb->s_journal_inum,
341 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
344 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
345 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
347 j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15];
348 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
349 j_inode->i_ext2.i_links_count = 1;
350 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
353 if (!j_inode->i_ext2.i_links_count ||
354 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
355 retval = EXT2_ET_NO_JOURNAL;
356 goto try_backup_journal;
358 if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize <
359 JBD2_MIN_JOURNAL_BLOCKS) {
360 retval = EXT2_ET_JOURNAL_TOO_SMALL;
361 goto try_backup_journal;
364 retval = ext2fs_block_iterate3(fs, j_inode->i_ino,
366 process_journal_block, &pb);
367 if ((pb.last_block + 1) * fs->blocksize <
368 (int) EXT2_I_SIZE(&j_inode->i_ext2)) {
369 retval = EXT2_ET_JOURNAL_TOO_SMALL;
370 goto try_backup_journal;
372 if (tried_backup_jnl && (fs->flags & EXT2_FLAG_RW)) {
373 retval = ext2fs_write_inode(fs, sb->s_journal_inum,
379 journal->j_maxlen = EXT2_I_SIZE(&j_inode->i_ext2) /
380 journal->j_blocksize;
383 retval = ext2fs_inode_io_intern2(fs, sb->s_journal_inum,
389 io_ptr = inode_io_manager;
391 journal->j_inode = j_inode;
392 fs->journal_io = fs->io;
393 retval = (errcode_t) jbd2_journal_bmap(journal, 0, &start);
399 if (!fs->journal_name) {
403 blkid_get_cache(&blkid, NULL);
404 uuid_unparse(sb->s_journal_uuid, uuid);
405 fs->journal_name = blkid_get_devname(blkid,
407 if (!fs->journal_name)
408 fs->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
409 blkid_put_cache(blkid);
411 journal_name = fs->journal_name;
414 retval = EXT2_ET_LOAD_EXT_JOURNAL;
418 jfs_debug(1, "Using journal file %s\n", journal_name);
419 io_ptr = unix_io_manager;
423 test_io_backing_manager = io_ptr;
424 io_ptr = test_io_manager;
430 retval = io_ptr->open(journal_name, fs->flags & EXT2_FLAG_RW,
436 io_channel_set_blksize(fs->journal_io, fs->blocksize);
441 start = ext2fs_journal_sb_start(fs->blocksize) - 1;
442 bh = getblk(dev_journal, start, fs->blocksize);
444 retval = EXT2_ET_NO_MEMORY;
447 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
453 memcpy(&jsuper, start ? bh->b_data :
454 bh->b_data + SUPERBLOCK_OFFSET,
456 #ifdef WORDS_BIGENDIAN
457 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
458 ext2fs_swap_super(&jsuper);
460 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
461 !ext2fs_has_feature_journal_dev(&jsuper)) {
462 retval = EXT2_ET_LOAD_EXT_JOURNAL;
466 /* Make sure the journal UUID is correct */
467 if (memcmp(jsuper.s_uuid, fs->super->s_journal_uuid,
468 sizeof(jsuper.s_uuid))) {
469 retval = EXT2_ET_LOAD_EXT_JOURNAL;
474 /* Check the superblock checksum */
475 if (ext2fs_has_feature_metadata_csum(&jsuper)) {
476 struct struct_ext2_filsys fsx;
477 struct ext2_super_block superx;
480 p = start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET;
481 memcpy(&fsx, fs, sizeof(fsx));
482 memcpy(&superx, fs->super, sizeof(superx));
484 ext2fs_set_feature_metadata_csum(fsx.super);
485 if (!ext2fs_superblock_csum_verify(&fsx, p)) {
486 retval = EXT2_ET_LOAD_EXT_JOURNAL;
493 maxlen = ext2fs_blocks_count(&jsuper);
494 journal->j_maxlen = (maxlen < 1ULL << 32) ? maxlen :
499 bh = getblk(dev_journal, start, journal->j_blocksize);
501 retval = EXT2_ET_NO_MEMORY;
505 journal->j_sb_buffer = bh;
506 journal->j_superblock = (journal_superblock_t *)bh->b_data;
510 ext2fs_free_mem(&j_inode);
513 *ret_journal = journal;
518 ext2fs_free_mem(&dev_fs);
520 ext2fs_free_mem(&j_inode);
522 ext2fs_free_mem(&journal);
526 static errcode_t ext2fs_journal_fix_bad_inode(ext2_filsys fs)
528 struct ext2_super_block *sb = fs->super;
529 int recover = ext2fs_has_feature_journal_needs_recovery(fs->super);
530 int has_journal = ext2fs_has_feature_journal(fs->super);
532 if (has_journal || sb->s_journal_inum) {
533 /* The journal inode is bogus, remove and force full fsck */
534 return EXT2_ET_BAD_INODE_NUM;
535 } else if (recover) {
536 return EXT2_ET_UNSUPP_FEATURE;
541 #define V1_SB_SIZE 0x0024
542 static void clear_v2_journal_fields(journal_t *journal)
544 ext2_filsys fs = journal->j_dev->k_fs;
546 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
547 fs->blocksize-V1_SB_SIZE);
548 mark_buffer_dirty(journal->j_sb_buffer);
552 static errcode_t ext2fs_journal_load(journal_t *journal)
554 ext2_filsys fs = journal->j_dev->k_fs;
555 journal_superblock_t *jsb;
556 struct buffer_head *jbh = journal->j_sb_buffer;
558 ll_rw_block(REQ_OP_READ, 0, 1, &jbh);
562 jsb = journal->j_superblock;
563 /* If we don't even have JBD2_MAGIC, we probably have a wrong inode */
564 if (jsb->s_header.h_magic != htonl(JBD2_MAGIC_NUMBER))
565 return ext2fs_journal_fix_bad_inode(fs);
567 switch (ntohl(jsb->s_header.h_blocktype)) {
568 case JBD2_SUPERBLOCK_V1:
569 journal->j_format_version = 1;
570 if (jsb->s_feature_compat ||
571 jsb->s_feature_incompat ||
572 jsb->s_feature_ro_compat ||
574 clear_v2_journal_fields(journal);
577 case JBD2_SUPERBLOCK_V2:
578 journal->j_format_version = 2;
579 if (ntohl(jsb->s_nr_users) > 1 &&
580 uuid_is_null(fs->super->s_journal_uuid))
581 clear_v2_journal_fields(journal);
582 if (ntohl(jsb->s_nr_users) > 1)
583 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
587 * These should never appear in a journal super block, so if
588 * they do, the journal is badly corrupted.
590 case JBD2_DESCRIPTOR_BLOCK:
591 case JBD2_COMMIT_BLOCK:
592 case JBD2_REVOKE_BLOCK:
593 return EXT2_ET_CORRUPT_JOURNAL_SB;
595 /* If we don't understand the superblock major type, but there
596 * is a magic number, then it is likely to be a new format we
597 * just don't understand, so leave it alone. */
599 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
602 if (JBD2_HAS_INCOMPAT_FEATURE(journal, ~JBD2_KNOWN_INCOMPAT_FEATURES))
603 return EXT2_ET_UNSUPP_FEATURE;
605 if (JBD2_HAS_RO_COMPAT_FEATURE(journal, ~JBD2_KNOWN_ROCOMPAT_FEATURES))
606 return EXT2_ET_RO_UNSUPP_FEATURE;
608 /* Checksum v1-3 are mutually exclusive features. */
609 if (jbd2_has_feature_csum2(journal) && jbd2_has_feature_csum3(journal))
610 return EXT2_ET_CORRUPT_JOURNAL_SB;
612 if (jbd2_journal_has_csum_v2or3(journal) &&
613 jbd2_has_feature_checksum(journal))
614 return EXT2_ET_CORRUPT_JOURNAL_SB;
616 if (!ext2fs_journal_verify_csum_type(journal, jsb) ||
617 !ext2fs_journal_sb_csum_verify(journal, jsb))
618 return EXT2_ET_CORRUPT_JOURNAL_SB;
620 if (jbd2_journal_has_csum_v2or3(journal))
621 journal->j_csum_seed = jbd2_chksum(journal, ~0, jsb->s_uuid,
622 sizeof(jsb->s_uuid));
624 /* We have now checked whether we know enough about the journal
625 * format to be able to proceed safely, so any other checks that
626 * fail we should attempt to recover from. */
627 if (jsb->s_blocksize != htonl(journal->j_blocksize))
628 return EXT2_ET_CORRUPT_JOURNAL_SB;
630 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
631 journal->j_maxlen = ntohl(jsb->s_maxlen);
632 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen)
633 return EXT2_ET_CORRUPT_JOURNAL_SB;
635 journal->j_tail_sequence = ntohl(jsb->s_sequence);
636 journal->j_transaction_sequence = journal->j_tail_sequence;
637 journal->j_tail = ntohl(jsb->s_start);
638 journal->j_first = ntohl(jsb->s_first);
639 journal->j_last = ntohl(jsb->s_maxlen);
644 static void ext2fs_journal_release(ext2_filsys fs, journal_t *journal,
647 journal_superblock_t *jsb;
650 mark_buffer_clean(journal->j_sb_buffer);
651 else if (fs->flags & EXT2_FLAG_RW) {
652 jsb = journal->j_superblock;
653 jsb->s_sequence = htonl(journal->j_tail_sequence);
655 jsb->s_start = 0; /* this marks the journal as empty */
656 ext2fs_journal_sb_csum_set(journal, jsb);
657 mark_buffer_dirty(journal->j_sb_buffer);
659 brelse(journal->j_sb_buffer);
661 if (fs && fs->journal_io) {
662 if (fs->io != fs->journal_io)
663 io_channel_close(fs->journal_io);
664 fs->journal_io = NULL;
665 free(fs->journal_name);
666 fs->journal_name = NULL;
670 if (journal->j_inode)
671 ext2fs_free_mem(&journal->j_inode);
673 if (journal->j_fs_dev)
674 ext2fs_free_mem(&journal->j_fs_dev);
675 ext2fs_free_mem(&journal);
679 * This function makes sure that the superblock fields regarding the
680 * journal are consistent.
682 static errcode_t ext2fs_check_ext3_journal(ext2_filsys fs)
684 struct ext2_super_block *sb = fs->super;
686 int recover = ext2fs_has_feature_journal_needs_recovery(fs->super);
689 /* If we don't have any journal features, don't do anything more */
690 if (!ext2fs_has_feature_journal(sb) &&
691 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
692 uuid_is_null(sb->s_journal_uuid))
695 retval = ext2fs_get_journal(fs, &journal);
699 retval = ext2fs_journal_load(journal);
704 * We want to make the flags consistent here. We will not leave with
705 * needs_recovery set but has_journal clear. We can't get in a loop
706 * with -y, -n, or -p, only if a user isn't making up their mind.
708 if (!ext2fs_has_feature_journal(sb)) {
709 retval = EXT2_ET_JOURNAL_FLAGS_WRONG;
713 if (ext2fs_has_feature_journal(sb) &&
714 !ext2fs_has_feature_journal_needs_recovery(sb) &&
715 journal->j_superblock->s_start != 0) {
716 retval = EXT2_ET_JOURNAL_FLAGS_WRONG;
721 * If we don't need to do replay the journal, check to see if
722 * the journal's errno is set; if so, we need to mark the file
723 * system as being corrupt and clear the journal's s_errno.
725 if (!ext2fs_has_feature_journal_needs_recovery(sb) &&
726 journal->j_superblock->s_errno) {
727 fs->super->s_state |= EXT2_ERROR_FS;
728 ext2fs_mark_super_dirty(fs);
729 journal->j_superblock->s_errno = 0;
730 ext2fs_journal_sb_csum_set(journal, journal->j_superblock);
731 mark_buffer_dirty(journal->j_sb_buffer);
735 ext2fs_journal_release(fs, journal, 0, retval ? 1 : 0);
739 static errcode_t recover_ext3_journal(ext2_filsys fs)
744 retval = jbd2_journal_init_revoke_record_cache();
748 retval = jbd2_journal_init_revoke_table_cache();
752 retval = ext2fs_get_journal(fs, &journal);
756 retval = ext2fs_journal_load(journal);
760 retval = jbd2_journal_init_revoke(journal, 1024);
764 retval = -jbd2_journal_recover(journal);
768 if (journal->j_failed_commit) {
769 journal->j_superblock->s_errno = -EINVAL;
770 mark_buffer_dirty(journal->j_sb_buffer);
774 jbd2_journal_destroy_revoke(journal);
775 jbd2_journal_destroy_revoke_record_cache();
776 jbd2_journal_destroy_revoke_table_cache();
777 ext2fs_journal_release(fs, journal, 1, 0);
781 errcode_t ext2fs_run_ext3_journal(ext2_filsys *fsp)
783 ext2_filsys fs = *fsp;
784 io_manager io_ptr = fs->io->manager;
785 errcode_t retval, recover_retval;
787 unsigned long long kbytes_written = 0;
792 if (!(fs->flags & EXT2_FLAG_RW))
793 return EXT2_ET_FILE_RO;
795 if (fs->flags & EXT2_FLAG_DIRTY)
796 ext2fs_flush(fs); /* Force out any modifications */
798 recover_retval = recover_ext3_journal(fs);
801 * Reload the filesystem context to get up-to-date data from disk
802 * because journal recovery will change the filesystem under us.
804 if (fs->super->s_kbytes_written &&
805 fs->io->manager->get_stats)
806 fs->io->manager->get_stats(fs->io, &stats);
807 if (stats && stats->bytes_written)
808 kbytes_written = stats->bytes_written >> 10;
811 fsname = fs->device_name;
812 fs->device_name = NULL;
814 fsblocksize = fs->blocksize;
817 retval = ext2fs_open(fsname, fsflags, 0, fsblocksize, io_ptr, fsp);
818 ext2fs_free_mem(&fsname);
823 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
824 fs->super->s_kbytes_written += kbytes_written;
826 /* Set the superblock flags */
827 ext2fs_clear_recover(fs, recover_retval != 0);
830 * Do one last sanity check, and propagate journal->s_errno to
831 * the EXT2_ERROR_FS flag in the fs superblock if needed.
833 retval = ext2fs_check_ext3_journal(fs);
834 return retval ? retval : recover_retval;
837 errcode_t ext2fs_open_journal(ext2_filsys fs, journal_t **j)
842 retval = jbd2_journal_init_revoke_record_cache();
846 retval = jbd2_journal_init_revoke_table_cache();
850 retval = ext2fs_get_journal(fs, &journal);
854 retval = ext2fs_journal_load(journal);
858 retval = jbd2_journal_init_revoke(journal, 1024);
862 if (journal->j_failed_commit) {
863 journal->j_superblock->s_errno = -EINVAL;
864 mark_buffer_dirty(journal->j_sb_buffer);
871 jbd2_journal_destroy_revoke(journal);
872 jbd2_journal_destroy_revoke_record_cache();
873 jbd2_journal_destroy_revoke_table_cache();
874 ext2fs_journal_release(fs, journal, 1, 0);
878 errcode_t ext2fs_close_journal(ext2_filsys fs, journal_t **j)
880 journal_t *journal = *j;
882 jbd2_journal_destroy_revoke(journal);
883 jbd2_journal_destroy_revoke_record_cache();
884 jbd2_journal_destroy_revoke_table_cache();
885 ext2fs_journal_release(fs, journal, 0, 0);
891 void jbd2_commit_block_csum_set(journal_t *j, struct buffer_head *bh)
893 struct commit_header *h;
896 if (!jbd2_journal_has_csum_v2or3(j))
899 h = (struct commit_header *)(bh->b_data);
900 h->h_chksum_type = 0;
901 h->h_chksum_size = 0;
903 csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
904 h->h_chksum[0] = ext2fs_cpu_to_be32(csum);
907 void jbd2_revoke_csum_set(journal_t *j, struct buffer_head *bh)
909 jbd2_descr_block_csum_set(j, bh);
912 void jbd2_descr_block_csum_set(journal_t *j, struct buffer_head *bh)
914 struct jbd2_journal_block_tail *tail;
917 if (!jbd2_journal_has_csum_v2or3(j))
920 tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize -
921 sizeof(struct jbd2_journal_block_tail));
922 tail->t_checksum = 0;
923 csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
924 tail->t_checksum = ext2fs_cpu_to_be32(csum);
927 void jbd2_block_tag_csum_set(journal_t *j, journal_block_tag_t *tag,
928 struct buffer_head *bh, __u32 sequence)
930 journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
934 if (!jbd2_journal_has_csum_v2or3(j))
937 seq = ext2fs_cpu_to_be32(sequence);
938 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
939 csum32 = jbd2_chksum(j, csum32, bh->b_data, bh->b_size);
941 if (jbd2_has_feature_csum3(j))
942 tag3->t_checksum = ext2fs_cpu_to_be32(csum32);
944 tag->t_checksum = ext2fs_cpu_to_be16(csum32);