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, unsigned long 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, (blk64_t) block,
126 struct buffer_head *getblk(kdev_t kdev, unsigned long long blocknr,
129 struct buffer_head *bh;
130 int bufsize = sizeof(*bh) + kdev->k_fs->blocksize -
134 retval = ext2fs_get_memzero(bufsize, &bh);
138 #ifdef CONFIG_JBD_DEBUG
139 if (journal_enable_debug >= 3)
142 jfs_debug(4, "getblk for block %llu (%d bytes)(total %d)\n",
143 blocknr, blocksize, bh_count);
145 bh->b_fs = kdev->k_fs;
146 if (kdev->k_dev == K_DEV_FS)
147 bh->b_io = kdev->k_fs->io;
149 bh->b_io = kdev->k_fs->journal_io;
150 bh->b_size = blocksize;
151 bh->b_blocknr = blocknr;
156 int sync_blockdev(kdev_t kdev)
160 if (kdev->k_dev == K_DEV_FS)
163 io = kdev->k_fs->journal_io;
165 return io_channel_flush(io) ? EIO : 0;
168 void ll_rw_block(int rw, int op_flags, int nr, struct buffer_head *bhp[])
171 struct buffer_head *bh;
173 for (; nr > 0; --nr) {
175 if (rw == REQ_OP_READ && !bh->b_uptodate) {
176 jfs_debug(3, "reading block %llu/%p\n",
177 bh->b_blocknr, (void *) bh);
178 retval = io_channel_read_blk64(bh->b_io,
182 com_err(bh->b_fs->device_name, retval,
183 "while reading block %llu\n",
185 bh->b_err = (int) retval;
189 } else if (rw == REQ_OP_WRITE && bh->b_dirty) {
190 jfs_debug(3, "writing block %llu/%p\n",
193 retval = io_channel_write_blk64(bh->b_io,
197 com_err(bh->b_fs->device_name, retval,
198 "while writing block %llu\n",
200 bh->b_err = (int) retval;
206 jfs_debug(3, "no-op %s for block %llu\n",
207 rw == REQ_OP_READ ? "read" : "write",
213 void mark_buffer_dirty(struct buffer_head *bh)
218 static void mark_buffer_clean(struct buffer_head *bh)
223 void brelse(struct buffer_head *bh)
226 ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
227 jfs_debug(3, "freeing block %llu/%p (total %d)\n",
228 bh->b_blocknr, (void *) bh, --bh_count);
229 ext2fs_free_mem(&bh);
232 int buffer_uptodate(struct buffer_head *bh)
234 return bh->b_uptodate;
237 void mark_buffer_uptodate(struct buffer_head *bh, int val)
239 bh->b_uptodate = val;
242 void wait_on_buffer(struct buffer_head *bh)
245 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
249 static void ext2fs_clear_recover(ext2_filsys fs, int error)
251 ext2fs_clear_feature_journal_needs_recovery(fs->super);
253 /* if we had an error doing journal recovery, we need a full fsck */
255 fs->super->s_state &= ~EXT2_VALID_FS;
257 * If we replayed the journal by definition the file system
258 * was mounted since the last time it was checked
260 if (fs->super->s_lastcheck >= fs->super->s_mtime)
261 fs->super->s_lastcheck = fs->super->s_mtime - 1;
262 ext2fs_mark_super_dirty(fs);
266 * This is a helper function to check the validity of the journal.
268 struct process_block_struct {
269 e2_blkcnt_t last_block;
272 static int process_journal_block(ext2_filsys fs,
274 e2_blkcnt_t blockcnt,
275 blk64_t ref_block EXT2FS_ATTR((unused)),
276 int ref_offset EXT2FS_ATTR((unused)),
279 struct process_block_struct *p;
280 blk64_t blk = *block_nr;
282 p = (struct process_block_struct *) priv_data;
284 if (!blk || blk < fs->super->s_first_data_block ||
285 blk >= ext2fs_blocks_count(fs->super))
289 p->last_block = blockcnt;
293 static errcode_t ext2fs_get_journal(ext2_filsys fs, journal_t **ret_journal)
295 struct process_block_struct pb;
296 struct ext2_super_block *sb = fs->super;
297 struct ext2_super_block jsuper;
298 struct buffer_head *bh;
299 struct inode *j_inode = NULL;
300 struct kdev_s *dev_fs = NULL, *dev_journal;
301 const char *journal_name = 0;
302 journal_t *journal = NULL;
303 errcode_t retval = 0;
304 io_manager io_ptr = 0;
305 unsigned long long start = 0;
307 int tried_backup_jnl = 0;
309 retval = ext2fs_get_memzero(sizeof(journal_t), &journal);
313 retval = ext2fs_get_memzero(2 * sizeof(struct kdev_s), &dev_fs);
316 dev_journal = dev_fs+1;
318 dev_fs->k_fs = dev_journal->k_fs = fs;
319 dev_fs->k_dev = K_DEV_FS;
320 dev_journal->k_dev = K_DEV_JOURNAL;
322 journal->j_dev = dev_journal;
323 journal->j_fs_dev = dev_fs;
324 journal->j_inode = NULL;
325 journal->j_blocksize = fs->blocksize;
327 if (uuid_is_null(sb->s_journal_uuid)) {
328 if (!sb->s_journal_inum) {
329 retval = EXT2_ET_BAD_INODE_NUM;
332 retval = ext2fs_get_memzero(sizeof(*j_inode), &j_inode);
337 j_inode->i_ino = sb->s_journal_inum;
339 retval = ext2fs_read_inode(fs, sb->s_journal_inum,
343 if (sb->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS ||
346 memset(&j_inode->i_ext2, 0, sizeof(struct ext2_inode));
347 memcpy(&j_inode->i_ext2.i_block[0], sb->s_jnl_blocks,
349 j_inode->i_ext2.i_size_high = sb->s_jnl_blocks[15];
350 j_inode->i_ext2.i_size = sb->s_jnl_blocks[16];
351 j_inode->i_ext2.i_links_count = 1;
352 j_inode->i_ext2.i_mode = LINUX_S_IFREG | 0600;
355 if (!j_inode->i_ext2.i_links_count ||
356 !LINUX_S_ISREG(j_inode->i_ext2.i_mode)) {
357 retval = EXT2_ET_NO_JOURNAL;
358 goto try_backup_journal;
360 if (EXT2_I_SIZE(&j_inode->i_ext2) / journal->j_blocksize <
361 JBD2_MIN_JOURNAL_BLOCKS) {
362 retval = EXT2_ET_JOURNAL_TOO_SMALL;
363 goto try_backup_journal;
366 retval = ext2fs_block_iterate3(fs, j_inode->i_ino,
368 process_journal_block, &pb);
369 if ((pb.last_block + 1) * fs->blocksize <
370 (int) EXT2_I_SIZE(&j_inode->i_ext2)) {
371 retval = EXT2_ET_JOURNAL_TOO_SMALL;
372 goto try_backup_journal;
374 if (tried_backup_jnl && (fs->flags & EXT2_FLAG_RW)) {
375 retval = ext2fs_write_inode(fs, sb->s_journal_inum,
381 journal->j_maxlen = EXT2_I_SIZE(&j_inode->i_ext2) /
382 journal->j_blocksize;
385 retval = ext2fs_inode_io_intern2(fs, sb->s_journal_inum,
391 io_ptr = inode_io_manager;
393 journal->j_inode = j_inode;
394 fs->journal_io = fs->io;
395 retval = (errcode_t) jbd2_journal_bmap(journal, 0, &start);
401 if (!fs->journal_name) {
405 blkid_get_cache(&blkid, NULL);
406 uuid_unparse(sb->s_journal_uuid, uuid);
407 fs->journal_name = blkid_get_devname(blkid,
409 if (!fs->journal_name)
410 fs->journal_name = blkid_devno_to_devname(sb->s_journal_dev);
411 blkid_put_cache(blkid);
413 journal_name = fs->journal_name;
416 retval = EXT2_ET_LOAD_EXT_JOURNAL;
420 jfs_debug(1, "Using journal file %s\n", journal_name);
421 io_ptr = unix_io_manager;
425 test_io_backing_manager = io_ptr;
426 io_ptr = test_io_manager;
432 retval = io_ptr->open(journal_name, fs->flags & EXT2_FLAG_RW,
438 io_channel_set_blksize(fs->journal_io, fs->blocksize);
443 start = ext2fs_journal_sb_start(fs->blocksize) - 1;
444 bh = getblk(dev_journal, start, fs->blocksize);
446 retval = EXT2_ET_NO_MEMORY;
449 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
455 memcpy(&jsuper, start ? bh->b_data :
456 bh->b_data + SUPERBLOCK_OFFSET,
458 #ifdef WORDS_BIGENDIAN
459 if (jsuper.s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC))
460 ext2fs_swap_super(&jsuper);
462 if (jsuper.s_magic != EXT2_SUPER_MAGIC ||
463 !ext2fs_has_feature_journal_dev(&jsuper)) {
464 retval = EXT2_ET_LOAD_EXT_JOURNAL;
468 /* Make sure the journal UUID is correct */
469 if (memcmp(jsuper.s_uuid, fs->super->s_journal_uuid,
470 sizeof(jsuper.s_uuid))) {
471 retval = EXT2_ET_LOAD_EXT_JOURNAL;
476 /* Check the superblock checksum */
477 if (ext2fs_has_feature_metadata_csum(&jsuper)) {
478 struct struct_ext2_filsys fsx;
479 struct ext2_super_block superx;
482 p = start ? bh->b_data : bh->b_data + SUPERBLOCK_OFFSET;
483 memcpy(&fsx, fs, sizeof(fsx));
484 memcpy(&superx, fs->super, sizeof(superx));
486 ext2fs_set_feature_metadata_csum(fsx.super);
487 if (!ext2fs_superblock_csum_verify(&fsx, p)) {
488 retval = EXT2_ET_LOAD_EXT_JOURNAL;
495 maxlen = ext2fs_blocks_count(&jsuper);
496 journal->j_maxlen = (maxlen < 1ULL << 32) ? maxlen :
501 bh = getblk(dev_journal, start, journal->j_blocksize);
503 retval = EXT2_ET_NO_MEMORY;
507 journal->j_sb_buffer = bh;
508 journal->j_superblock = (journal_superblock_t *)bh->b_data;
512 ext2fs_free_mem(&j_inode);
515 *ret_journal = journal;
520 ext2fs_free_mem(&dev_fs);
522 ext2fs_free_mem(&j_inode);
524 ext2fs_free_mem(&journal);
528 static errcode_t ext2fs_journal_fix_bad_inode(ext2_filsys fs)
530 struct ext2_super_block *sb = fs->super;
531 int recover = ext2fs_has_feature_journal_needs_recovery(fs->super);
532 int has_journal = ext2fs_has_feature_journal(fs->super);
534 if (has_journal || sb->s_journal_inum) {
535 /* The journal inode is bogus, remove and force full fsck */
536 return EXT2_ET_BAD_INODE_NUM;
537 } else if (recover) {
538 return EXT2_ET_UNSUPP_FEATURE;
543 #define V1_SB_SIZE 0x0024
544 static void clear_v2_journal_fields(journal_t *journal)
546 ext2_filsys fs = journal->j_dev->k_fs;
548 memset(((char *) journal->j_superblock) + V1_SB_SIZE, 0,
549 fs->blocksize-V1_SB_SIZE);
550 mark_buffer_dirty(journal->j_sb_buffer);
554 static errcode_t ext2fs_journal_load(journal_t *journal)
556 ext2_filsys fs = journal->j_dev->k_fs;
557 journal_superblock_t *jsb;
558 struct buffer_head *jbh = journal->j_sb_buffer;
560 ll_rw_block(REQ_OP_READ, 0, 1, &jbh);
564 jsb = journal->j_superblock;
565 /* If we don't even have JBD2_MAGIC, we probably have a wrong inode */
566 if (jsb->s_header.h_magic != htonl(JBD2_MAGIC_NUMBER))
567 return ext2fs_journal_fix_bad_inode(fs);
569 switch (ntohl(jsb->s_header.h_blocktype)) {
570 case JBD2_SUPERBLOCK_V1:
571 journal->j_format_version = 1;
572 if (jsb->s_feature_compat ||
573 jsb->s_feature_incompat ||
574 jsb->s_feature_ro_compat ||
576 clear_v2_journal_fields(journal);
579 case JBD2_SUPERBLOCK_V2:
580 journal->j_format_version = 2;
581 if (ntohl(jsb->s_nr_users) > 1 &&
582 uuid_is_null(fs->super->s_journal_uuid))
583 clear_v2_journal_fields(journal);
584 if (ntohl(jsb->s_nr_users) > 1)
585 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
589 * These should never appear in a journal super block, so if
590 * they do, the journal is badly corrupted.
592 case JBD2_DESCRIPTOR_BLOCK:
593 case JBD2_COMMIT_BLOCK:
594 case JBD2_REVOKE_BLOCK:
595 return EXT2_ET_CORRUPT_JOURNAL_SB;
597 /* If we don't understand the superblock major type, but there
598 * is a magic number, then it is likely to be a new format we
599 * just don't understand, so leave it alone. */
601 return EXT2_ET_JOURNAL_UNSUPP_VERSION;
604 if (JBD2_HAS_INCOMPAT_FEATURE(journal, ~JBD2_KNOWN_INCOMPAT_FEATURES))
605 return EXT2_ET_UNSUPP_FEATURE;
607 if (JBD2_HAS_RO_COMPAT_FEATURE(journal, ~JBD2_KNOWN_ROCOMPAT_FEATURES))
608 return EXT2_ET_RO_UNSUPP_FEATURE;
610 /* Checksum v1-3 are mutually exclusive features. */
611 if (jbd2_has_feature_csum2(journal) && jbd2_has_feature_csum3(journal))
612 return EXT2_ET_CORRUPT_JOURNAL_SB;
614 if (jbd2_journal_has_csum_v2or3(journal) &&
615 jbd2_has_feature_checksum(journal))
616 return EXT2_ET_CORRUPT_JOURNAL_SB;
618 if (!ext2fs_journal_verify_csum_type(journal, jsb) ||
619 !ext2fs_journal_sb_csum_verify(journal, jsb))
620 return EXT2_ET_CORRUPT_JOURNAL_SB;
622 if (jbd2_journal_has_csum_v2or3(journal))
623 journal->j_csum_seed = jbd2_chksum(journal, ~0, jsb->s_uuid,
624 sizeof(jsb->s_uuid));
626 /* We have now checked whether we know enough about the journal
627 * format to be able to proceed safely, so any other checks that
628 * fail we should attempt to recover from. */
629 if (jsb->s_blocksize != htonl(journal->j_blocksize))
630 return EXT2_ET_CORRUPT_JOURNAL_SB;
632 if (ntohl(jsb->s_maxlen) < journal->j_maxlen)
633 journal->j_maxlen = ntohl(jsb->s_maxlen);
634 else if (ntohl(jsb->s_maxlen) > journal->j_maxlen)
635 return EXT2_ET_CORRUPT_JOURNAL_SB;
637 journal->j_tail_sequence = ntohl(jsb->s_sequence);
638 journal->j_transaction_sequence = journal->j_tail_sequence;
639 journal->j_tail = ntohl(jsb->s_start);
640 journal->j_first = ntohl(jsb->s_first);
641 journal->j_last = ntohl(jsb->s_maxlen);
646 static void ext2fs_journal_release(ext2_filsys fs, journal_t *journal,
649 journal_superblock_t *jsb;
652 mark_buffer_clean(journal->j_sb_buffer);
653 else if (fs->flags & EXT2_FLAG_RW) {
654 jsb = journal->j_superblock;
655 jsb->s_sequence = htonl(journal->j_tail_sequence);
657 jsb->s_start = 0; /* this marks the journal as empty */
658 ext2fs_journal_sb_csum_set(journal, jsb);
659 mark_buffer_dirty(journal->j_sb_buffer);
661 brelse(journal->j_sb_buffer);
663 if (fs && fs->journal_io) {
664 if (fs->io != fs->journal_io)
665 io_channel_close(fs->journal_io);
666 fs->journal_io = NULL;
667 free(fs->journal_name);
668 fs->journal_name = NULL;
672 if (journal->j_inode)
673 ext2fs_free_mem(&journal->j_inode);
675 if (journal->j_fs_dev)
676 ext2fs_free_mem(&journal->j_fs_dev);
677 ext2fs_free_mem(&journal);
681 * This function makes sure that the superblock fields regarding the
682 * journal are consistent.
684 static errcode_t ext2fs_check_ext3_journal(ext2_filsys fs)
686 struct ext2_super_block *sb = fs->super;
688 int recover = ext2fs_has_feature_journal_needs_recovery(fs->super);
691 /* If we don't have any journal features, don't do anything more */
692 if (!ext2fs_has_feature_journal(sb) &&
693 !recover && sb->s_journal_inum == 0 && sb->s_journal_dev == 0 &&
694 uuid_is_null(sb->s_journal_uuid))
697 retval = ext2fs_get_journal(fs, &journal);
701 retval = ext2fs_journal_load(journal);
706 * We want to make the flags consistent here. We will not leave with
707 * needs_recovery set but has_journal clear. We can't get in a loop
708 * with -y, -n, or -p, only if a user isn't making up their mind.
710 if (!ext2fs_has_feature_journal(sb)) {
711 retval = EXT2_ET_JOURNAL_FLAGS_WRONG;
715 if (ext2fs_has_feature_journal(sb) &&
716 !ext2fs_has_feature_journal_needs_recovery(sb) &&
717 journal->j_superblock->s_start != 0) {
718 retval = EXT2_ET_JOURNAL_FLAGS_WRONG;
723 * If we don't need to do replay the journal, check to see if
724 * the journal's errno is set; if so, we need to mark the file
725 * system as being corrupt and clear the journal's s_errno.
727 if (!ext2fs_has_feature_journal_needs_recovery(sb) &&
728 journal->j_superblock->s_errno) {
729 fs->super->s_state |= EXT2_ERROR_FS;
730 ext2fs_mark_super_dirty(fs);
731 journal->j_superblock->s_errno = 0;
732 ext2fs_journal_sb_csum_set(journal, journal->j_superblock);
733 mark_buffer_dirty(journal->j_sb_buffer);
737 ext2fs_journal_release(fs, journal, 0, retval ? 1 : 0);
741 static errcode_t recover_ext3_journal(ext2_filsys fs)
746 retval = jbd2_journal_init_revoke_record_cache();
750 retval = jbd2_journal_init_revoke_table_cache();
754 retval = ext2fs_get_journal(fs, &journal);
758 retval = ext2fs_journal_load(journal);
762 retval = jbd2_journal_init_revoke(journal, 1024);
766 retval = -jbd2_journal_recover(journal);
770 if (journal->j_failed_commit) {
771 journal->j_superblock->s_errno = -EINVAL;
772 mark_buffer_dirty(journal->j_sb_buffer);
776 jbd2_journal_destroy_revoke(journal);
777 jbd2_journal_destroy_revoke_record_cache();
778 jbd2_journal_destroy_revoke_table_cache();
779 ext2fs_journal_release(fs, journal, 1, 0);
783 errcode_t ext2fs_run_ext3_journal(ext2_filsys *fsp)
785 ext2_filsys fs = *fsp;
786 io_manager io_ptr = fs->io->manager;
787 errcode_t retval, recover_retval;
789 unsigned long long kbytes_written = 0;
794 if (!(fs->flags & EXT2_FLAG_RW))
795 return EXT2_ET_FILE_RO;
797 if (fs->flags & EXT2_FLAG_DIRTY)
798 ext2fs_flush(fs); /* Force out any modifications */
800 recover_retval = recover_ext3_journal(fs);
803 * Reload the filesystem context to get up-to-date data from disk
804 * because journal recovery will change the filesystem under us.
806 if (fs->super->s_kbytes_written &&
807 fs->io->manager->get_stats)
808 fs->io->manager->get_stats(fs->io, &stats);
809 if (stats && stats->bytes_written)
810 kbytes_written = stats->bytes_written >> 10;
813 fsname = fs->device_name;
814 fs->device_name = NULL;
816 fsblocksize = fs->blocksize;
819 retval = ext2fs_open(fsname, fsflags, 0, fsblocksize, io_ptr, fsp);
820 ext2fs_free_mem(&fsname);
825 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
826 fs->super->s_kbytes_written += kbytes_written;
828 /* Set the superblock flags */
829 ext2fs_clear_recover(fs, recover_retval != 0);
832 * Do one last sanity check, and propagate journal->s_errno to
833 * the EXT2_ERROR_FS flag in the fs superblock if needed.
835 retval = ext2fs_check_ext3_journal(fs);
836 return retval ? retval : recover_retval;
839 errcode_t ext2fs_open_journal(ext2_filsys fs, journal_t **j)
844 retval = jbd2_journal_init_revoke_record_cache();
848 retval = jbd2_journal_init_revoke_table_cache();
852 retval = ext2fs_get_journal(fs, &journal);
856 retval = ext2fs_journal_load(journal);
860 retval = jbd2_journal_init_revoke(journal, 1024);
864 if (journal->j_failed_commit) {
865 journal->j_superblock->s_errno = -EINVAL;
866 mark_buffer_dirty(journal->j_sb_buffer);
873 jbd2_journal_destroy_revoke(journal);
874 jbd2_journal_destroy_revoke_record_cache();
875 jbd2_journal_destroy_revoke_table_cache();
876 ext2fs_journal_release(fs, journal, 1, 0);
880 errcode_t ext2fs_close_journal(ext2_filsys fs, journal_t **j)
882 journal_t *journal = *j;
884 jbd2_journal_destroy_revoke(journal);
885 jbd2_journal_destroy_revoke_record_cache();
886 jbd2_journal_destroy_revoke_table_cache();
887 ext2fs_journal_release(fs, journal, 0, 0);
893 void jbd2_commit_block_csum_set(journal_t *j, struct buffer_head *bh)
895 struct commit_header *h;
898 if (!jbd2_journal_has_csum_v2or3(j))
901 h = (struct commit_header *)(bh->b_data);
902 h->h_chksum_type = 0;
903 h->h_chksum_size = 0;
905 csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
906 h->h_chksum[0] = ext2fs_cpu_to_be32(csum);
909 void jbd2_revoke_csum_set(journal_t *j, struct buffer_head *bh)
911 jbd2_descr_block_csum_set(j, bh);
914 void jbd2_descr_block_csum_set(journal_t *j, struct buffer_head *bh)
916 struct jbd2_journal_block_tail *tail;
919 if (!jbd2_journal_has_csum_v2or3(j))
922 tail = (struct jbd2_journal_block_tail *)(bh->b_data + j->j_blocksize -
923 sizeof(struct jbd2_journal_block_tail));
924 tail->t_checksum = 0;
925 csum = jbd2_chksum(j, j->j_csum_seed, bh->b_data, j->j_blocksize);
926 tail->t_checksum = ext2fs_cpu_to_be32(csum);
929 void jbd2_block_tag_csum_set(journal_t *j, journal_block_tag_t *tag,
930 struct buffer_head *bh, __u32 sequence)
932 journal_block_tag3_t *tag3 = (journal_block_tag3_t *)tag;
936 if (!jbd2_journal_has_csum_v2or3(j))
939 seq = ext2fs_cpu_to_be32(sequence);
940 csum32 = jbd2_chksum(j, j->j_csum_seed, (__u8 *)&seq, sizeof(seq));
941 csum32 = jbd2_chksum(j, csum32, bh->b_data, bh->b_size);
943 if (jbd2_has_feature_csum3(j))
944 tag3->t_checksum = ext2fs_cpu_to_be32(csum32);
946 tag->t_checksum = ext2fs_cpu_to_be16(csum32);