2 * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3 * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /** @addtogroup lwext4
34 * @file ext4_journal.c
35 * @brief Journal handle functions
38 #include "ext4_config.h"
39 #include "ext4_types.h"
41 #include "ext4_super.h"
42 #include "ext4_journal.h"
43 #include "ext4_errno.h"
44 #include "ext4_blockdev.h"
45 #include "ext4_crc32.h"
46 #include "ext4_debug.h"
51 /**@brief Revoke entry during journal replay.*/
53 /**@brief Block number not to be replayed.*/
56 /**@brief For any transaction id smaller
57 * than trans_id, records of @block
58 * in those transactions should not
62 /**@brief Revoke tree node.*/
63 RB_ENTRY(revoke_entry) revoke_node;
66 /**@brief Valid journal replay information.*/
68 /**@brief Starting transaction id.*/
69 uint32_t start_trans_id;
71 /**@brief Ending transaction id.*/
72 uint32_t last_trans_id;
74 /**@brief Used as internal argument.*/
75 uint32_t this_trans_id;
77 /**@brief No of transactions went through.*/
80 /**@brief RB-Tree storing revoke entries.*/
81 RB_HEAD(jbd_revoke, revoke_entry) revoke_root;
84 /**@brief Journal replay internal arguments.*/
86 /**@brief Journal replay information.*/
87 struct recover_info *info;
89 /**@brief Current block we are on.*/
92 /**@brief Current trans_id we are on.*/
93 uint32_t this_trans_id;
97 jbd_revoke_entry_cmp(struct revoke_entry *a, struct revoke_entry *b)
99 if (a->block > b->block)
101 else if (a->block < b->block)
107 jbd_block_rec_cmp(struct jbd_block_rec *a, struct jbd_block_rec *b)
111 else if (a->lba < b->lba)
116 RB_GENERATE_INTERNAL(jbd_revoke, revoke_entry, revoke_node,
117 jbd_revoke_entry_cmp, static inline)
118 RB_GENERATE_INTERNAL(jbd_block, jbd_block_rec, block_rec_node,
119 jbd_block_rec_cmp, static inline)
121 #define jbd_alloc_revoke_entry() calloc(1, sizeof(struct revoke_entry))
122 #define jbd_free_revoke_entry(addr) free(addr)
124 static int jbd_has_csum(struct jbd_sb *jbd_sb)
126 if (JBD_HAS_INCOMPAT_FEATURE(jbd_sb, JBD_FEATURE_INCOMPAT_CSUM_V2))
129 if (JBD_HAS_INCOMPAT_FEATURE(jbd_sb, JBD_FEATURE_INCOMPAT_CSUM_V3))
135 #if CONFIG_META_CSUM_ENABLE
136 static uint32_t jbd_sb_csum(struct jbd_sb *jbd_sb)
138 uint32_t checksum = 0;
140 if (jbd_has_csum(jbd_sb)) {
141 uint32_t orig_checksum = jbd_sb->checksum;
142 jbd_set32(jbd_sb, checksum, 0);
143 /* Calculate crc32c checksum against tho whole superblock */
144 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_sb,
145 JBD_SUPERBLOCK_SIZE);
146 jbd_sb->checksum = orig_checksum;
151 #define jbd_sb_csum(...) 0
154 static void jbd_sb_csum_set(struct jbd_sb *jbd_sb)
156 if (!jbd_has_csum(jbd_sb))
159 jbd_set32(jbd_sb, checksum, jbd_sb_csum(jbd_sb));
162 #if CONFIG_META_CSUM_ENABLE
164 jbd_verify_sb_csum(struct jbd_sb *jbd_sb)
166 if (!jbd_has_csum(jbd_sb))
169 return jbd_sb_csum(jbd_sb) == jbd_get32(jbd_sb, checksum);
172 #define jbd_verify_sb_csum(...) true
175 #if CONFIG_META_CSUM_ENABLE
176 static uint32_t jbd_meta_csum(struct jbd_fs *jbd_fs,
177 struct jbd_bhdr *bhdr)
179 uint32_t checksum = 0;
181 if (jbd_has_csum(&jbd_fs->sb)) {
182 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
183 struct jbd_block_tail *tail =
184 (struct jbd_block_tail *)((char *)bhdr + block_size -
185 sizeof(struct jbd_block_tail));
186 uint32_t orig_checksum = tail->checksum;
189 /* First calculate crc32c checksum against fs uuid */
190 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
191 sizeof(jbd_fs->sb.uuid));
192 /* Calculate crc32c checksum against tho whole block */
193 checksum = ext4_crc32c(checksum, bhdr,
195 tail->checksum = orig_checksum;
200 #define jbd_meta_csum(...) 0
203 static void jbd_meta_csum_set(struct jbd_fs *jbd_fs,
204 struct jbd_bhdr *bhdr)
206 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
207 struct jbd_block_tail *tail = (struct jbd_block_tail *)
208 ((char *)bhdr + block_size -
209 sizeof(struct jbd_block_tail));
210 if (!jbd_has_csum(&jbd_fs->sb))
213 tail->checksum = to_be32(jbd_meta_csum(jbd_fs, bhdr));
216 #if CONFIG_META_CSUM_ENABLE
218 jbd_verify_meta_csum(struct jbd_fs *jbd_fs,
219 struct jbd_bhdr *bhdr)
221 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
222 struct jbd_block_tail *tail = (struct jbd_block_tail *)
223 ((char *)bhdr + block_size -
224 sizeof(struct jbd_block_tail));
225 if (!jbd_has_csum(&jbd_fs->sb))
228 return jbd_meta_csum(jbd_fs, bhdr) == to_be32(tail->checksum);
231 #define jbd_verify_meta_csum(...) true
234 #if CONFIG_META_CSUM_ENABLE
235 static uint32_t jbd_commit_csum(struct jbd_fs *jbd_fs,
236 struct jbd_commit_header *header)
238 uint32_t checksum = 0;
240 if (jbd_has_csum(&jbd_fs->sb)) {
241 uint32_t orig_checksum_type = header->chksum_type,
242 orig_checksum_size = header->chksum_size,
243 orig_checksum = header->chksum[0];
244 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
245 header->chksum_type = 0;
246 header->chksum_size = 0;
247 header->chksum[0] = 0;
249 /* First calculate crc32c checksum against fs uuid */
250 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
251 sizeof(jbd_fs->sb.uuid));
252 /* Calculate crc32c checksum against tho whole block */
253 checksum = ext4_crc32c(checksum, header,
256 header->chksum_type = orig_checksum_type;
257 header->chksum_size = orig_checksum_size;
258 header->chksum[0] = orig_checksum;
263 #define jbd_commit_csum(...) 0
266 static void jbd_commit_csum_set(struct jbd_fs *jbd_fs,
267 struct jbd_commit_header *header)
269 if (!jbd_has_csum(&jbd_fs->sb))
272 header->chksum_type = 0;
273 header->chksum_size = 0;
274 header->chksum[0] = jbd_commit_csum(jbd_fs, header);
277 #if CONFIG_META_CSUM_ENABLE
278 static bool jbd_verify_commit_csum(struct jbd_fs *jbd_fs,
279 struct jbd_commit_header *header)
281 if (!jbd_has_csum(&jbd_fs->sb))
284 return header->chksum[0] == to_be32(jbd_commit_csum(jbd_fs,
288 #define jbd_verify_commit_csum(...) true
291 #if CONFIG_META_CSUM_ENABLE
293 * NOTE: We only make use of @csum parameter when
294 * JBD_FEATURE_COMPAT_CHECKSUM is enabled.
296 static uint32_t jbd_block_csum(struct jbd_fs *jbd_fs, const void *buf,
300 uint32_t checksum = 0;
302 if (jbd_has_csum(&jbd_fs->sb)) {
303 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
304 /* First calculate crc32c checksum against fs uuid */
305 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
306 sizeof(jbd_fs->sb.uuid));
307 /* Then calculate crc32c checksum against sequence no. */
308 checksum = ext4_crc32c(checksum, &sequence,
310 /* Calculate crc32c checksum against tho whole block */
311 checksum = ext4_crc32c(checksum, buf,
313 } else if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
314 JBD_FEATURE_COMPAT_CHECKSUM)) {
315 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
316 /* Calculate crc32c checksum against tho whole block */
317 checksum = ext4_crc32(csum, buf,
323 #define jbd_block_csum(...) 0
326 static void jbd_block_tag_csum_set(struct jbd_fs *jbd_fs, void *__tag,
329 int ver = jbd_has_csum(&jbd_fs->sb);
334 struct jbd_block_tag *tag = __tag;
335 tag->checksum = (uint16_t)to_be32(checksum);
337 struct jbd_block_tag3 *tag = __tag;
338 tag->checksum = to_be32(checksum);
342 /**@brief Write jbd superblock to disk.
343 * @param jbd_fs jbd filesystem
344 * @param s jbd superblock
345 * @return standard error code*/
346 static int jbd_sb_write(struct jbd_fs *jbd_fs, struct jbd_sb *s)
349 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
352 rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
357 offset = fblock * ext4_sb_get_block_size(&fs->sb);
358 return ext4_block_writebytes(fs->bdev, offset, s,
359 EXT4_SUPERBLOCK_SIZE);
362 /**@brief Read jbd superblock from disk.
363 * @param jbd_fs jbd filesystem
364 * @param s jbd superblock
365 * @return standard error code*/
366 static int jbd_sb_read(struct jbd_fs *jbd_fs, struct jbd_sb *s)
369 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
372 rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
376 offset = fblock * ext4_sb_get_block_size(&fs->sb);
377 return ext4_block_readbytes(fs->bdev, offset, s,
378 EXT4_SUPERBLOCK_SIZE);
381 /**@brief Verify jbd superblock.
382 * @param sb jbd superblock
383 * @return true if jbd superblock is valid */
384 static bool jbd_verify_sb(struct jbd_sb *sb)
386 struct jbd_bhdr *header = &sb->header;
387 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER)
390 if (jbd_get32(header, blocktype) != JBD_SUPERBLOCK &&
391 jbd_get32(header, blocktype) != JBD_SUPERBLOCK_V2)
394 return jbd_verify_sb_csum(sb);
397 /**@brief Write back dirty jbd superblock to disk.
398 * @param jbd_fs jbd filesystem
399 * @return standard error code*/
400 static int jbd_write_sb(struct jbd_fs *jbd_fs)
404 rc = jbd_sb_write(jbd_fs, &jbd_fs->sb);
408 jbd_fs->dirty = false;
413 /**@brief Get reference to jbd filesystem.
414 * @param fs Filesystem to load journal of
415 * @param jbd_fs jbd filesystem
416 * @return standard error code*/
417 int jbd_get_fs(struct ext4_fs *fs,
418 struct jbd_fs *jbd_fs)
421 uint32_t journal_ino;
423 memset(jbd_fs, 0, sizeof(struct jbd_fs));
424 /* See if there is journal inode on this filesystem.*/
425 /* FIXME: detection on existance ofbkejournal bdev is
427 journal_ino = ext4_get32(&fs->sb, journal_inode_number);
429 rc = ext4_fs_get_inode_ref(fs,
433 memset(jbd_fs, 0, sizeof(struct jbd_fs));
436 rc = jbd_sb_read(jbd_fs, &jbd_fs->sb);
438 memset(jbd_fs, 0, sizeof(struct jbd_fs));
439 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
442 if (!jbd_verify_sb(&jbd_fs->sb)) {
443 memset(jbd_fs, 0, sizeof(struct jbd_fs));
444 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
451 /**@brief Put reference of jbd filesystem.
452 * @param jbd_fs jbd filesystem
453 * @return standard error code*/
454 int jbd_put_fs(struct jbd_fs *jbd_fs)
457 rc = jbd_write_sb(jbd_fs);
459 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
463 /**@brief Data block lookup helper.
464 * @param jbd_fs jbd filesystem
465 * @param iblock block index
466 * @param fblock logical block address
467 * @return standard error code*/
468 int jbd_inode_bmap(struct jbd_fs *jbd_fs,
470 ext4_fsblk_t *fblock)
472 int rc = ext4_fs_get_inode_dblk_idx(
480 /**@brief jbd block get function (through cache).
481 * @param jbd_fs jbd filesystem
482 * @param block block descriptor
483 * @param fblock jbd logical block address
484 * @return standard error code*/
485 static int jbd_block_get(struct jbd_fs *jbd_fs,
486 struct ext4_block *block,
489 /* TODO: journal device. */
491 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
493 /* Lookup the logical block address of
495 rc = jbd_inode_bmap(jbd_fs, iblock,
500 struct ext4_blockdev *bdev = jbd_fs->inode_ref.fs->bdev;
501 rc = ext4_block_get(bdev, block, fblock);
503 /* If succeeded, mark buffer as BC_FLUSH to indicate
504 * that data should be written to disk immediately.*/
506 ext4_bcache_set_flag(block->buf, BC_FLUSH);
507 /* As we don't want to occupy too much space
508 * in block cache, we set this buffer BC_TMP.*/
509 ext4_bcache_set_flag(block->buf, BC_TMP);
515 /**@brief jbd block get function (through cache, don't read).
516 * @param jbd_fs jbd filesystem
517 * @param block block descriptor
518 * @param fblock jbd logical block address
519 * @return standard error code*/
520 static int jbd_block_get_noread(struct jbd_fs *jbd_fs,
521 struct ext4_block *block,
524 /* TODO: journal device. */
526 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
527 rc = jbd_inode_bmap(jbd_fs, iblock,
532 struct ext4_blockdev *bdev = jbd_fs->inode_ref.fs->bdev;
533 rc = ext4_block_get_noread(bdev, block, fblock);
535 ext4_bcache_set_flag(block->buf, BC_FLUSH);
540 /**@brief jbd block set procedure (through cache).
541 * @param jbd_fs jbd filesystem
542 * @param block block descriptor
543 * @return standard error code*/
544 static int jbd_block_set(struct jbd_fs *jbd_fs,
545 struct ext4_block *block)
547 return ext4_block_set(jbd_fs->inode_ref.fs->bdev,
551 /**@brief helper functions to calculate
552 * block tag size, not including UUID part.
553 * @param jbd_fs jbd filesystem
554 * @return tag size in bytes*/
555 static int jbd_tag_bytes(struct jbd_fs *jbd_fs)
559 /* It is very easy to deal with the case which
560 * JBD_FEATURE_INCOMPAT_CSUM_V3 is enabled.*/
561 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
562 JBD_FEATURE_INCOMPAT_CSUM_V3))
563 return sizeof(struct jbd_block_tag3);
565 size = sizeof(struct jbd_block_tag);
567 /* If JBD_FEATURE_INCOMPAT_CSUM_V2 is enabled,
568 * add 2 bytes to size.*/
569 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
570 JBD_FEATURE_INCOMPAT_CSUM_V2))
571 size += sizeof(uint16_t);
573 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
574 JBD_FEATURE_INCOMPAT_64BIT))
577 /* If block number is 4 bytes in size,
578 * minus 4 bytes from size */
579 return size - sizeof(uint32_t);
582 /**@brief Tag information. */
584 /**@brief Tag size in bytes, including UUID part.*/
587 /**@brief block number stored in this tag.*/
590 /**@brief whether UUID part exists or not.*/
593 /**@brief UUID content if UUID part exists.*/
594 uint8_t uuid[UUID_SIZE];
596 /**@brief Is this the last tag? */
599 /**@brief crc32c checksum. */
603 /**@brief Extract information from a block tag.
604 * @param __tag pointer to the block tag
605 * @param tag_bytes block tag size of this jbd filesystem
606 * @param remaining size in buffer containing the block tag
607 * @param tag_info information of this tag.
608 * @return EOK when succeed, otherwise return EINVAL.*/
610 jbd_extract_block_tag(struct jbd_fs *jbd_fs,
613 int32_t remain_buf_size,
614 struct tag_info *tag_info)
617 tag_info->tag_bytes = tag_bytes;
618 tag_info->uuid_exist = false;
619 tag_info->last_tag = false;
621 /* See whether it is possible to hold a valid block tag.*/
622 if (remain_buf_size - tag_bytes < 0)
625 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
626 JBD_FEATURE_INCOMPAT_CSUM_V3)) {
627 struct jbd_block_tag3 *tag = __tag;
628 tag_info->block = jbd_get32(tag, blocknr);
629 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
630 JBD_FEATURE_INCOMPAT_64BIT))
632 (uint64_t)jbd_get32(tag, blocknr_high) << 32;
634 if (jbd_get32(tag, flags) & JBD_FLAG_ESCAPE)
637 if (!(jbd_get32(tag, flags) & JBD_FLAG_SAME_UUID)) {
638 /* See whether it is possible to hold UUID part.*/
639 if (remain_buf_size - tag_bytes < UUID_SIZE)
642 uuid_start = (char *)tag + tag_bytes;
643 tag_info->uuid_exist = true;
644 tag_info->tag_bytes += UUID_SIZE;
645 memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
648 if (jbd_get32(tag, flags) & JBD_FLAG_LAST_TAG)
649 tag_info->last_tag = true;
652 struct jbd_block_tag *tag = __tag;
653 tag_info->block = jbd_get32(tag, blocknr);
654 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
655 JBD_FEATURE_INCOMPAT_64BIT))
657 (uint64_t)jbd_get32(tag, blocknr_high) << 32;
659 if (jbd_get16(tag, flags) & JBD_FLAG_ESCAPE)
662 if (!(jbd_get16(tag, flags) & JBD_FLAG_SAME_UUID)) {
663 /* See whether it is possible to hold UUID part.*/
664 if (remain_buf_size - tag_bytes < UUID_SIZE)
667 uuid_start = (char *)tag + tag_bytes;
668 tag_info->uuid_exist = true;
669 tag_info->tag_bytes += UUID_SIZE;
670 memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
673 if (jbd_get16(tag, flags) & JBD_FLAG_LAST_TAG)
674 tag_info->last_tag = true;
680 /**@brief Write information to a block tag.
681 * @param __tag pointer to the block tag
682 * @param remaining size in buffer containing the block tag
683 * @param tag_info information of this tag.
684 * @return EOK when succeed, otherwise return EINVAL.*/
686 jbd_write_block_tag(struct jbd_fs *jbd_fs,
688 int32_t remain_buf_size,
689 struct tag_info *tag_info)
692 int tag_bytes = jbd_tag_bytes(jbd_fs);
694 tag_info->tag_bytes = tag_bytes;
696 /* See whether it is possible to hold a valid block tag.*/
697 if (remain_buf_size - tag_bytes < 0)
700 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
701 JBD_FEATURE_INCOMPAT_CSUM_V3)) {
702 struct jbd_block_tag3 *tag = __tag;
703 memset(tag, 0, sizeof(struct jbd_block_tag3));
704 jbd_set32(tag, blocknr, tag_info->block);
705 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
706 JBD_FEATURE_INCOMPAT_64BIT))
707 jbd_set32(tag, blocknr_high, tag_info->block >> 32);
709 if (tag_info->uuid_exist) {
710 /* See whether it is possible to hold UUID part.*/
711 if (remain_buf_size - tag_bytes < UUID_SIZE)
714 uuid_start = (char *)tag + tag_bytes;
715 tag_info->tag_bytes += UUID_SIZE;
716 memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
718 jbd_set32(tag, flags,
719 jbd_get32(tag, flags) | JBD_FLAG_SAME_UUID);
721 jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum);
723 if (tag_info->last_tag)
724 jbd_set32(tag, flags,
725 jbd_get32(tag, flags) | JBD_FLAG_LAST_TAG);
728 struct jbd_block_tag *tag = __tag;
729 memset(tag, 0, sizeof(struct jbd_block_tag));
730 jbd_set32(tag, blocknr, tag_info->block);
731 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
732 JBD_FEATURE_INCOMPAT_64BIT))
733 jbd_set32(tag, blocknr_high, tag_info->block >> 32);
735 if (tag_info->uuid_exist) {
736 /* See whether it is possible to hold UUID part.*/
737 if (remain_buf_size - tag_bytes < UUID_SIZE)
740 uuid_start = (char *)tag + tag_bytes;
741 tag_info->tag_bytes += UUID_SIZE;
742 memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
744 jbd_set16(tag, flags,
745 jbd_get16(tag, flags) | JBD_FLAG_SAME_UUID);
747 jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum);
749 if (tag_info->last_tag)
750 jbd_set16(tag, flags,
751 jbd_get16(tag, flags) | JBD_FLAG_LAST_TAG);
757 /**@brief Iterate all block tags in a block.
758 * @param jbd_fs jbd filesystem
759 * @param __tag_start pointer to the block
760 * @param tag_tbl_size size of the block
761 * @param func callback routine to indicate that
762 * a block tag is found
763 * @param arg additional argument to be passed to func */
765 jbd_iterate_block_table(struct jbd_fs *jbd_fs,
767 int32_t tag_tbl_size,
768 void (*func)(struct jbd_fs * jbd_fs,
774 char *tag_start, *tag_ptr;
775 int tag_bytes = jbd_tag_bytes(jbd_fs);
776 tag_start = __tag_start;
779 /* Cut off the size of block tail storing checksum. */
780 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
781 JBD_FEATURE_INCOMPAT_CSUM_V2) ||
782 JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
783 JBD_FEATURE_INCOMPAT_CSUM_V3))
784 tag_tbl_size -= sizeof(struct jbd_block_tail);
786 while (tag_tbl_size) {
787 struct tag_info tag_info;
788 int rc = jbd_extract_block_tag(jbd_fs,
797 func(jbd_fs, tag_info.block, tag_info.uuid, arg);
799 /* Stop the iteration when we reach the last tag. */
800 if (tag_info.last_tag)
803 tag_ptr += tag_info.tag_bytes;
804 tag_tbl_size -= tag_info.tag_bytes;
808 static void jbd_display_block_tags(struct jbd_fs *jbd_fs,
813 uint32_t *iblock = arg;
814 ext4_dbg(DEBUG_JBD, "Block in block_tag: %" PRIu64 "\n", block);
821 static struct revoke_entry *
822 jbd_revoke_entry_lookup(struct recover_info *info, ext4_fsblk_t block)
824 struct revoke_entry tmp = {
828 return RB_FIND(jbd_revoke, &info->revoke_root, &tmp);
831 /**@brief Replay a block in a transaction.
832 * @param jbd_fs jbd filesystem
833 * @param block block address to be replayed.*/
834 static void jbd_replay_block_tags(struct jbd_fs *jbd_fs,
836 uint8_t *uuid __unused,
840 struct replay_arg *arg = __arg;
841 struct recover_info *info = arg->info;
842 uint32_t *this_block = arg->this_block;
843 struct revoke_entry *revoke_entry;
844 struct ext4_block journal_block, ext4_block;
845 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
849 /* We replay this block only if the current transaction id
850 * is equal or greater than that in revoke entry.*/
851 revoke_entry = jbd_revoke_entry_lookup(info, block);
853 arg->this_trans_id < revoke_entry->trans_id)
857 "Replaying block in block_tag: %" PRIu64 "\n",
860 r = jbd_block_get(jbd_fs, &journal_block, *this_block);
864 /* We need special treatment for ext4 superblock. */
866 r = ext4_block_get_noread(fs->bdev, &ext4_block, block);
868 jbd_block_set(jbd_fs, &journal_block);
872 memcpy(ext4_block.data,
874 jbd_get32(&jbd_fs->sb, blocksize));
876 ext4_bcache_set_dirty(ext4_block.buf);
877 ext4_block_set(fs->bdev, &ext4_block);
879 uint16_t mount_count, state;
880 mount_count = ext4_get16(&fs->sb, mount_count);
881 state = ext4_get16(&fs->sb, state);
884 journal_block.data + EXT4_SUPERBLOCK_OFFSET,
885 EXT4_SUPERBLOCK_SIZE);
887 /* Mark system as mounted */
888 ext4_set16(&fs->sb, state, state);
889 r = ext4_sb_write(fs->bdev, &fs->sb);
893 /*Update mount count*/
894 ext4_set16(&fs->sb, mount_count, mount_count);
897 jbd_block_set(jbd_fs, &journal_block);
902 /**@brief Add block address to revoke tree, along with
903 * its transaction id.
904 * @param info journal replay info
905 * @param block block address to be replayed.*/
906 static void jbd_add_revoke_block_tags(struct recover_info *info,
909 struct revoke_entry *revoke_entry;
911 ext4_dbg(DEBUG_JBD, "Add block %" PRIu64 " to revoke tree\n", block);
912 /* If the revoke entry with respect to the block address
913 * exists already, update its transaction id.*/
914 revoke_entry = jbd_revoke_entry_lookup(info, block);
916 revoke_entry->trans_id = info->this_trans_id;
920 revoke_entry = jbd_alloc_revoke_entry();
921 ext4_assert(revoke_entry);
922 revoke_entry->block = block;
923 revoke_entry->trans_id = info->this_trans_id;
924 RB_INSERT(jbd_revoke, &info->revoke_root, revoke_entry);
929 static void jbd_destroy_revoke_tree(struct recover_info *info)
931 while (!RB_EMPTY(&info->revoke_root)) {
932 struct revoke_entry *revoke_entry =
933 RB_MIN(jbd_revoke, &info->revoke_root);
934 ext4_assert(revoke_entry);
935 RB_REMOVE(jbd_revoke, &info->revoke_root, revoke_entry);
936 jbd_free_revoke_entry(revoke_entry);
940 /* Make sure we wrap around the log correctly! */
941 #define wrap(sb, var) \
943 if (var >= jbd_get32((sb), maxlen)) \
944 var -= (jbd_get32((sb), maxlen) - jbd_get32((sb), first)); \
947 #define ACTION_SCAN 0
948 #define ACTION_REVOKE 1
949 #define ACTION_RECOVER 2
951 /**@brief Add entries in a revoke block to revoke tree.
952 * @param jbd_fs jbd filesystem
953 * @param header revoke block header
954 * @param recover_info journal replay info*/
955 static void jbd_build_revoke_tree(struct jbd_fs *jbd_fs,
956 struct jbd_bhdr *header,
957 struct recover_info *info)
960 struct jbd_revoke_header *revoke_hdr =
961 (struct jbd_revoke_header *)header;
962 uint32_t i, nr_entries, record_len = 4;
964 /* If we are working on a 64bit jbd filesystem, */
965 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
966 JBD_FEATURE_INCOMPAT_64BIT))
969 nr_entries = (jbd_get32(revoke_hdr, count) -
970 sizeof(struct jbd_revoke_header)) /
973 blocks_entry = (char *)(revoke_hdr + 1);
975 for (i = 0;i < nr_entries;i++) {
976 if (record_len == 8) {
978 (uint64_t *)blocks_entry;
979 jbd_add_revoke_block_tags(info, to_be64(*blocks));
982 (uint32_t *)blocks_entry;
983 jbd_add_revoke_block_tags(info, to_be32(*blocks));
985 blocks_entry += record_len;
989 static void jbd_debug_descriptor_block(struct jbd_fs *jbd_fs,
990 struct jbd_bhdr *header,
993 jbd_iterate_block_table(jbd_fs,
995 jbd_get32(&jbd_fs->sb, blocksize) -
996 sizeof(struct jbd_bhdr),
997 jbd_display_block_tags,
1001 static void jbd_replay_descriptor_block(struct jbd_fs *jbd_fs,
1002 struct jbd_bhdr *header,
1003 struct replay_arg *arg)
1005 jbd_iterate_block_table(jbd_fs,
1007 jbd_get32(&jbd_fs->sb, blocksize) -
1008 sizeof(struct jbd_bhdr),
1009 jbd_replay_block_tags,
1013 /**@brief The core routine of journal replay.
1014 * @param jbd_fs jbd filesystem
1015 * @param recover_info journal replay info
1016 * @param action action needed to be taken
1017 * @return standard error code*/
1018 static int jbd_iterate_log(struct jbd_fs *jbd_fs,
1019 struct recover_info *info,
1023 bool log_end = false;
1024 struct jbd_sb *sb = &jbd_fs->sb;
1025 uint32_t start_trans_id, this_trans_id;
1026 uint32_t start_block, this_block;
1028 /* We start iterating valid blocks in the whole journal.*/
1029 start_trans_id = this_trans_id = jbd_get32(sb, sequence);
1030 start_block = this_block = jbd_get32(sb, start);
1031 if (action == ACTION_SCAN)
1032 info->trans_cnt = 0;
1033 else if (!info->trans_cnt)
1036 ext4_dbg(DEBUG_JBD, "Start of journal at trans id: %" PRIu32 "\n",
1040 struct ext4_block block;
1041 struct jbd_bhdr *header;
1042 /* If we are not scanning for the last
1043 * valid transaction in the journal,
1044 * we will stop when we reach the end of
1046 if (action != ACTION_SCAN)
1047 if (this_trans_id > info->last_trans_id) {
1052 r = jbd_block_get(jbd_fs, &block, this_block);
1056 header = (struct jbd_bhdr *)block.data;
1057 /* This block does not have a valid magic number,
1058 * so we have reached the end of the journal.*/
1059 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER) {
1060 jbd_block_set(jbd_fs, &block);
1065 /* If the transaction id we found is not expected,
1066 * we may have reached the end of the journal.
1068 * If we are not scanning the journal, something
1069 * bad might have taken place. :-( */
1070 if (jbd_get32(header, sequence) != this_trans_id) {
1071 if (action != ACTION_SCAN)
1074 jbd_block_set(jbd_fs, &block);
1079 switch (jbd_get32(header, blocktype)) {
1080 case JBD_DESCRIPTOR_BLOCK:
1081 if (!jbd_verify_meta_csum(jbd_fs, header)) {
1083 DBG_WARN "Descriptor block checksum failed."
1084 "Journal block: %" PRIu32"\n",
1089 ext4_dbg(DEBUG_JBD, "Descriptor block: %" PRIu32", "
1090 "trans_id: %" PRIu32"\n",
1091 this_block, this_trans_id);
1092 if (action == ACTION_RECOVER) {
1093 struct replay_arg replay_arg;
1094 replay_arg.info = info;
1095 replay_arg.this_block = &this_block;
1096 replay_arg.this_trans_id = this_trans_id;
1098 jbd_replay_descriptor_block(jbd_fs,
1099 header, &replay_arg);
1101 jbd_debug_descriptor_block(jbd_fs,
1102 header, &this_block);
1105 case JBD_COMMIT_BLOCK:
1106 if (!jbd_verify_commit_csum(jbd_fs,
1107 (struct jbd_commit_header *)header)) {
1109 DBG_WARN "Commit block checksum failed."
1110 "Journal block: %" PRIu32"\n",
1115 ext4_dbg(DEBUG_JBD, "Commit block: %" PRIu32", "
1116 "trans_id: %" PRIu32"\n",
1117 this_block, this_trans_id);
1118 /* This is the end of a transaction,
1119 * we may now proceed to the next transaction.
1124 case JBD_REVOKE_BLOCK:
1125 if (!jbd_verify_meta_csum(jbd_fs, header)) {
1127 DBG_WARN "Revoke block checksum failed."
1128 "Journal block: %" PRIu32"\n",
1133 ext4_dbg(DEBUG_JBD, "Revoke block: %" PRIu32", "
1134 "trans_id: %" PRIu32"\n",
1135 this_block, this_trans_id);
1136 if (action == ACTION_REVOKE) {
1137 info->this_trans_id = this_trans_id;
1138 jbd_build_revoke_tree(jbd_fs,
1146 jbd_block_set(jbd_fs, &block);
1148 wrap(sb, this_block);
1149 if (this_block == start_block)
1153 ext4_dbg(DEBUG_JBD, "End of journal.\n");
1154 if (r == EOK && action == ACTION_SCAN) {
1155 /* We have finished scanning the journal. */
1156 info->start_trans_id = start_trans_id;
1157 if (this_trans_id > start_trans_id)
1158 info->last_trans_id = this_trans_id - 1;
1160 info->last_trans_id = this_trans_id;
1166 /**@brief Replay journal.
1167 * @param jbd_fs jbd filesystem
1168 * @return standard error code*/
1169 int jbd_recover(struct jbd_fs *jbd_fs)
1172 struct recover_info info;
1173 struct jbd_sb *sb = &jbd_fs->sb;
1177 RB_INIT(&info.revoke_root);
1179 r = jbd_iterate_log(jbd_fs, &info, ACTION_SCAN);
1183 r = jbd_iterate_log(jbd_fs, &info, ACTION_REVOKE);
1187 r = jbd_iterate_log(jbd_fs, &info, ACTION_RECOVER);
1189 /* If we successfully replay the journal,
1190 * clear EXT4_FINCOM_RECOVER flag on the
1191 * ext4 superblock, and set the start of
1193 uint32_t features_incompatible =
1194 ext4_get32(&jbd_fs->inode_ref.fs->sb,
1195 features_incompatible);
1196 jbd_set32(&jbd_fs->sb, start, 0);
1197 features_incompatible &= ~EXT4_FINCOM_RECOVER;
1198 ext4_set32(&jbd_fs->inode_ref.fs->sb,
1199 features_incompatible,
1200 features_incompatible);
1201 jbd_fs->dirty = true;
1202 r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
1203 &jbd_fs->inode_ref.fs->sb);
1205 jbd_destroy_revoke_tree(&info);
1209 static void jbd_journal_write_sb(struct jbd_journal *journal)
1211 struct jbd_fs *jbd_fs = journal->jbd_fs;
1212 jbd_set32(&jbd_fs->sb, start, journal->start);
1213 jbd_set32(&jbd_fs->sb, sequence, journal->trans_id);
1214 jbd_fs->dirty = true;
1217 /**@brief Start accessing the journal.
1218 * @param jbd_fs jbd filesystem
1219 * @param journal current journal session
1220 * @return standard error code*/
1221 int jbd_journal_start(struct jbd_fs *jbd_fs,
1222 struct jbd_journal *journal)
1225 uint32_t features_incompatible =
1226 ext4_get32(&jbd_fs->inode_ref.fs->sb,
1227 features_incompatible);
1228 struct ext4_block block = EXT4_BLOCK_ZERO();
1229 features_incompatible |= EXT4_FINCOM_RECOVER;
1230 ext4_set32(&jbd_fs->inode_ref.fs->sb,
1231 features_incompatible,
1232 features_incompatible);
1233 r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
1234 &jbd_fs->inode_ref.fs->sb);
1238 journal->first = jbd_get32(&jbd_fs->sb, first);
1239 journal->start = journal->first;
1240 journal->last = journal->first;
1241 journal->trans_id = 1;
1242 journal->alloc_trans_id = 1;
1244 journal->block_size = jbd_get32(&jbd_fs->sb, blocksize);
1246 r = jbd_block_get_noread(jbd_fs,
1250 memset(journal, 0, sizeof(struct jbd_journal));
1253 memset(block.data, 0, journal->block_size);
1254 ext4_bcache_set_dirty(block.buf);
1255 r = jbd_block_set(jbd_fs, &block);
1257 memset(journal, 0, sizeof(struct jbd_journal));
1261 TAILQ_INIT(&journal->trans_queue);
1262 TAILQ_INIT(&journal->cp_queue);
1263 RB_INIT(&journal->block_rec_root);
1264 journal->jbd_fs = jbd_fs;
1265 jbd_journal_write_sb(journal);
1266 return jbd_write_sb(jbd_fs);
1269 static void jbd_journal_flush_trans(struct jbd_trans *trans)
1271 struct jbd_buf *jbd_buf, *tmp;
1272 struct jbd_journal *journal = trans->journal;
1273 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1274 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
1276 struct ext4_block block = jbd_buf->block;
1277 ext4_block_flush_buf(fs->bdev, block.buf);
1282 jbd_journal_skip_pure_revoke(struct jbd_journal *journal,
1283 struct jbd_trans *trans)
1285 journal->start = trans->start_iblock +
1286 trans->alloc_blocks;
1287 wrap(&journal->jbd_fs->sb, journal->start);
1288 journal->trans_id = trans->trans_id + 1;
1289 jbd_journal_free_trans(journal,
1291 jbd_journal_write_sb(journal);
1295 jbd_journal_purge_cp_trans(struct jbd_journal *journal,
1298 struct jbd_trans *trans;
1299 while ((trans = TAILQ_FIRST(&journal->cp_queue))) {
1300 if (!trans->data_cnt) {
1301 TAILQ_REMOVE(&journal->cp_queue,
1304 jbd_journal_skip_pure_revoke(journal, trans);
1306 if (trans->data_cnt ==
1307 trans->written_cnt) {
1309 trans->start_iblock +
1310 trans->alloc_blocks;
1311 wrap(&journal->jbd_fs->sb,
1314 trans->trans_id + 1;
1315 TAILQ_REMOVE(&journal->cp_queue,
1318 jbd_journal_free_trans(journal,
1321 jbd_journal_write_sb(journal);
1322 } else if (!flush) {
1324 trans->start_iblock;
1325 wrap(&journal->jbd_fs->sb,
1329 jbd_journal_write_sb(journal);
1332 jbd_journal_flush_trans(trans);
1337 /**@brief Stop accessing the journal.
1338 * @param journal current journal session
1339 * @return standard error code*/
1340 int jbd_journal_stop(struct jbd_journal *journal)
1343 struct jbd_fs *jbd_fs = journal->jbd_fs;
1344 uint32_t features_incompatible;
1346 /* Commit all the transactions to the journal.*/
1347 jbd_journal_commit_all(journal);
1349 /* Make sure that journalled content have reached
1351 jbd_journal_purge_cp_trans(journal, true);
1353 /* There should be no block record in this journal
1355 if (!RB_EMPTY(&journal->block_rec_root))
1357 DBG_WARN "There are still block records "
1358 "in this journal session!\n");
1360 features_incompatible =
1361 ext4_get32(&jbd_fs->inode_ref.fs->sb,
1362 features_incompatible);
1363 features_incompatible &= ~EXT4_FINCOM_RECOVER;
1364 ext4_set32(&jbd_fs->inode_ref.fs->sb,
1365 features_incompatible,
1366 features_incompatible);
1367 r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
1368 &jbd_fs->inode_ref.fs->sb);
1373 journal->trans_id = 0;
1374 jbd_journal_write_sb(journal);
1375 return jbd_write_sb(journal->jbd_fs);
1378 /**@brief Allocate a block in the journal.
1379 * @param journal current journal session
1380 * @param trans transaction
1381 * @return allocated block address*/
1382 static uint32_t jbd_journal_alloc_block(struct jbd_journal *journal,
1383 struct jbd_trans *trans)
1385 uint32_t start_block;
1387 start_block = journal->last++;
1388 trans->alloc_blocks++;
1389 wrap(&journal->jbd_fs->sb, journal->last);
1391 /* If there is no space left, flush all journalled
1392 * blocks to disk first.*/
1393 if (journal->last == journal->start)
1394 jbd_journal_purge_cp_trans(journal, true);
1399 /**@brief Allocate a new transaction
1400 * @param journal current journal session
1401 * @return transaction allocated*/
1403 jbd_journal_new_trans(struct jbd_journal *journal)
1405 struct jbd_trans *trans = calloc(1, sizeof(struct jbd_trans));
1409 /* We will assign a trans_id to this transaction,
1410 * once it has been committed.*/
1411 trans->journal = journal;
1412 trans->data_csum = EXT4_CRC32_INIT;
1414 TAILQ_INIT(&trans->buf_queue);
1418 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
1419 struct ext4_buf *buf __unused,
1423 /**@brief gain access to it before making any modications.
1424 * @param journal current journal session
1425 * @param trans transaction
1426 * @param block descriptor
1427 * @return standard error code.*/
1428 int jbd_trans_get_access(struct jbd_journal *journal,
1429 struct jbd_trans *trans,
1430 struct ext4_block *block)
1433 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1434 struct jbd_buf *jbd_buf = block->buf->end_write_arg;
1436 /* If the buffer has already been modified, we should
1437 * flush dirty data in this buffer to disk.*/
1438 if (ext4_bcache_test_flag(block->buf, BC_DIRTY) &&
1439 block->buf->end_write == jbd_trans_end_write) {
1440 ext4_assert(jbd_buf);
1441 if (jbd_buf->trans != trans)
1442 r = ext4_block_flush_buf(fs->bdev, block->buf);
1448 static struct jbd_block_rec *
1449 jbd_trans_block_rec_lookup(struct jbd_journal *journal,
1452 struct jbd_block_rec tmp = {
1456 return RB_FIND(jbd_block,
1457 &journal->block_rec_root,
1461 static inline struct jbd_block_rec *
1462 jbd_trans_insert_block_rec(struct jbd_trans *trans,
1464 struct ext4_buf *buf)
1466 struct jbd_block_rec *block_rec;
1467 block_rec = jbd_trans_block_rec_lookup(trans->journal, lba);
1469 LIST_REMOVE(block_rec, tbrec_node);
1470 /* Data should be flushed to disk already. */
1471 ext4_assert(!block_rec->buf);
1472 /* Now this block record belongs to this transaction. */
1473 LIST_INSERT_HEAD(&trans->tbrec_list, block_rec, tbrec_node);
1474 block_rec->trans = trans;
1477 block_rec = calloc(1, sizeof(struct jbd_block_rec));
1481 block_rec->lba = lba;
1482 block_rec->buf = buf;
1483 block_rec->trans = trans;
1484 LIST_INSERT_HEAD(&trans->tbrec_list, block_rec, tbrec_node);
1485 RB_INSERT(jbd_block, &trans->journal->block_rec_root, block_rec);
1490 jbd_trans_remove_block_rec(struct jbd_journal *journal,
1491 struct jbd_block_rec *block_rec,
1492 struct jbd_trans *trans)
1494 /* If this block record doesn't belong to this transaction,
1496 if (block_rec->trans == trans) {
1497 LIST_REMOVE(block_rec, tbrec_node);
1498 RB_REMOVE(jbd_block,
1499 &journal->block_rec_root,
1505 /**@brief Add block to a transaction and mark it dirty.
1506 * @param trans transaction
1507 * @param block block descriptor
1508 * @return standard error code*/
1509 int jbd_trans_set_block_dirty(struct jbd_trans *trans,
1510 struct ext4_block *block)
1512 struct jbd_buf *buf;
1514 if (!ext4_bcache_test_flag(block->buf, BC_DIRTY) &&
1515 block->buf->end_write != jbd_trans_end_write) {
1516 struct jbd_block_rec *block_rec;
1517 buf = calloc(1, sizeof(struct jbd_buf));
1521 if ((block_rec = jbd_trans_insert_block_rec(trans,
1523 block->buf)) == NULL) {
1528 buf->block_rec = block_rec;
1530 buf->block = *block;
1531 ext4_bcache_inc_ref(block->buf);
1533 /* If the content reach the disk, notify us
1534 * so that we may do a checkpoint. */
1535 block->buf->end_write = jbd_trans_end_write;
1536 block->buf->end_write_arg = buf;
1539 TAILQ_INSERT_HEAD(&trans->buf_queue, buf, buf_node);
1541 ext4_bcache_set_dirty(block->buf);
1546 /**@brief Add block to be revoked to a transaction
1547 * @param trans transaction
1548 * @param lba logical block address
1549 * @return standard error code*/
1550 int jbd_trans_revoke_block(struct jbd_trans *trans,
1553 struct jbd_revoke_rec *rec =
1554 calloc(1, sizeof(struct jbd_revoke_rec));
1559 LIST_INSERT_HEAD(&trans->revoke_list, rec, revoke_node);
1563 /**@brief Try to add block to be revoked to a transaction.
1564 * If @lba still remains in an transaction on checkpoint
1565 * queue, add @lba as a revoked block to the transaction.
1566 * @param trans transaction
1567 * @param lba logical block address
1568 * @return standard error code*/
1569 int jbd_trans_try_revoke_block(struct jbd_trans *trans,
1573 struct jbd_journal *journal = trans->journal;
1574 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1575 struct jbd_block_rec *block_rec =
1576 jbd_trans_block_rec_lookup(journal, lba);
1578 /* Make sure we don't flush any buffers belong to this transaction. */
1579 if (block_rec && block_rec->trans != trans) {
1580 /* If the buffer has not been flushed yet, flush it now. */
1581 if (block_rec->buf) {
1582 r = ext4_block_flush_buf(fs->bdev, block_rec->buf);
1588 jbd_trans_revoke_block(trans, lba);
1594 /**@brief Free a transaction
1595 * @param journal current journal session
1596 * @param trans transaction
1597 * @param abort discard all the modifications on the block?
1598 * @return standard error code*/
1599 void jbd_journal_free_trans(struct jbd_journal *journal,
1600 struct jbd_trans *trans,
1603 struct jbd_buf *jbd_buf, *tmp;
1604 struct jbd_revoke_rec *rec, *tmp2;
1605 struct jbd_block_rec *block_rec, *tmp3;
1606 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1607 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
1610 jbd_buf->block.buf->end_write = NULL;
1611 jbd_buf->block.buf->end_write_arg = NULL;
1612 ext4_bcache_clear_dirty(jbd_buf->block.buf);
1613 ext4_block_set(fs->bdev, &jbd_buf->block);
1616 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1619 LIST_FOREACH_SAFE(rec, &trans->revoke_list, revoke_node,
1621 LIST_REMOVE(rec, revoke_node);
1624 LIST_FOREACH_SAFE(block_rec, &trans->tbrec_list, tbrec_node,
1626 jbd_trans_remove_block_rec(journal, block_rec, trans);
1632 /**@brief Write commit block for a transaction
1633 * @param trans transaction
1634 * @return standard error code*/
1635 static int jbd_trans_write_commit_block(struct jbd_trans *trans)
1638 struct jbd_commit_header *header;
1639 uint32_t commit_iblock = 0;
1640 struct ext4_block commit_block;
1641 struct jbd_journal *journal = trans->journal;
1643 commit_iblock = jbd_journal_alloc_block(journal, trans);
1644 rc = jbd_block_get_noread(journal->jbd_fs,
1645 &commit_block, commit_iblock);
1649 header = (struct jbd_commit_header *)commit_block.data;
1650 jbd_set32(&header->header, magic, JBD_MAGIC_NUMBER);
1651 jbd_set32(&header->header, blocktype, JBD_COMMIT_BLOCK);
1652 jbd_set32(&header->header, sequence, trans->trans_id);
1654 if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb,
1655 JBD_FEATURE_COMPAT_CHECKSUM)) {
1656 jbd_set32(header, chksum_type, JBD_CRC32_CHKSUM);
1657 jbd_set32(header, chksum_size, JBD_CRC32_CHKSUM_SIZE);
1658 jbd_set32(header, chksum[0], trans->data_csum);
1660 jbd_commit_csum_set(journal->jbd_fs, header);
1661 ext4_bcache_set_dirty(commit_block.buf);
1662 rc = jbd_block_set(journal->jbd_fs, &commit_block);
1669 /**@brief Write descriptor block for a transaction
1670 * @param journal current journal session
1671 * @param trans transaction
1672 * @return standard error code*/
1673 static int jbd_journal_prepare(struct jbd_journal *journal,
1674 struct jbd_trans *trans)
1676 int rc = EOK, i = 0;
1677 int32_t tag_tbl_size;
1678 uint32_t desc_iblock = 0;
1679 uint32_t data_iblock = 0;
1680 char *tag_start = NULL, *tag_ptr = NULL;
1681 struct jbd_buf *jbd_buf, *tmp;
1682 struct ext4_block desc_block, data_block;
1683 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1684 uint32_t checksum = EXT4_CRC32_INIT;
1686 /* Try to remove any non-dirty buffers from the tail of
1688 TAILQ_FOREACH_REVERSE_SAFE(jbd_buf, &trans->buf_queue,
1689 jbd_trans_buf, buf_node, tmp) {
1690 /* We stop the iteration when we find a dirty buffer. */
1691 if (ext4_bcache_test_flag(jbd_buf->block.buf,
1695 /* The buffer has not been modified, just release
1697 jbd_trans_remove_block_rec(journal,
1698 jbd_buf->block_rec, trans);
1701 jbd_buf->block.buf->end_write = NULL;
1702 jbd_buf->block.buf->end_write_arg = NULL;
1703 ext4_block_set(fs->bdev, &jbd_buf->block);
1704 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1708 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node, tmp) {
1709 struct tag_info tag_info;
1710 bool uuid_exist = false;
1711 if (!ext4_bcache_test_flag(jbd_buf->block.buf,
1713 /* The buffer has not been modified, just release
1715 jbd_trans_remove_block_rec(journal,
1716 jbd_buf->block_rec, trans);
1719 jbd_buf->block.buf->end_write = NULL;
1720 jbd_buf->block.buf->end_write_arg = NULL;
1721 ext4_block_set(fs->bdev, &jbd_buf->block);
1722 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1726 checksum = jbd_block_csum(journal->jbd_fs,
1727 jbd_buf->block.data,
1732 struct jbd_bhdr *bhdr;
1733 desc_iblock = jbd_journal_alloc_block(journal, trans);
1734 rc = jbd_block_get_noread(journal->jbd_fs,
1735 &desc_block, desc_iblock);
1739 ext4_bcache_set_dirty(desc_block.buf);
1741 bhdr = (struct jbd_bhdr *)desc_block.data;
1742 jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
1743 jbd_set32(bhdr, blocktype, JBD_DESCRIPTOR_BLOCK);
1744 jbd_set32(bhdr, sequence, trans->trans_id);
1746 tag_start = (char *)(bhdr + 1);
1747 tag_ptr = tag_start;
1749 tag_tbl_size = journal->block_size -
1750 sizeof(struct jbd_bhdr);
1752 if (jbd_has_csum(&journal->jbd_fs->sb))
1753 tag_tbl_size -= sizeof(struct jbd_block_tail);
1755 if (!trans->start_iblock)
1756 trans->start_iblock = desc_iblock;
1759 tag_info.block = jbd_buf->block.lb_id;
1760 tag_info.uuid_exist = uuid_exist;
1761 if (i == trans->data_cnt - 1)
1762 tag_info.last_tag = true;
1764 tag_info.last_tag = false;
1766 tag_info.checksum = checksum;
1769 memcpy(tag_info.uuid, journal->jbd_fs->sb.uuid,
1772 rc = jbd_write_block_tag(journal->jbd_fs,
1777 jbd_meta_csum_set(journal->jbd_fs,
1778 (struct jbd_bhdr *)desc_block.data);
1779 jbd_block_set(journal->jbd_fs, &desc_block);
1784 data_iblock = jbd_journal_alloc_block(journal, trans);
1785 rc = jbd_block_get_noread(journal->jbd_fs,
1786 &data_block, data_iblock);
1790 ext4_bcache_set_dirty(data_block.buf);
1792 memcpy(data_block.data, jbd_buf->block.data,
1793 journal->block_size);
1795 rc = jbd_block_set(journal->jbd_fs, &data_block);
1799 tag_ptr += tag_info.tag_bytes;
1800 tag_tbl_size -= tag_info.tag_bytes;
1804 if (rc == EOK && desc_iblock) {
1805 jbd_meta_csum_set(journal->jbd_fs,
1806 (struct jbd_bhdr *)desc_block.data);
1807 trans->data_csum = checksum;
1808 jbd_block_set(journal->jbd_fs, &desc_block);
1814 /**@brief Write revoke block for a transaction
1815 * @param journal current journal session
1816 * @param trans transaction
1817 * @return standard error code*/
1819 jbd_journal_prepare_revoke(struct jbd_journal *journal,
1820 struct jbd_trans *trans)
1822 int rc = EOK, i = 0;
1823 int32_t tag_tbl_size;
1824 uint32_t desc_iblock = 0;
1825 char *blocks_entry = NULL;
1826 struct jbd_revoke_rec *rec, *tmp;
1827 struct ext4_block desc_block;
1828 struct jbd_revoke_header *header = NULL;
1829 int32_t record_len = 4;
1831 if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb,
1832 JBD_FEATURE_INCOMPAT_64BIT))
1835 LIST_FOREACH_SAFE(rec, &trans->revoke_list, revoke_node,
1839 struct jbd_bhdr *bhdr;
1840 desc_iblock = jbd_journal_alloc_block(journal, trans);
1841 rc = jbd_block_get_noread(journal->jbd_fs,
1842 &desc_block, desc_iblock);
1847 ext4_bcache_set_dirty(desc_block.buf);
1849 bhdr = (struct jbd_bhdr *)desc_block.data;
1850 jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
1851 jbd_set32(bhdr, blocktype, JBD_REVOKE_BLOCK);
1852 jbd_set32(bhdr, sequence, trans->trans_id);
1854 header = (struct jbd_revoke_header *)bhdr;
1855 blocks_entry = (char *)(header + 1);
1856 tag_tbl_size = journal->block_size -
1857 sizeof(struct jbd_revoke_header);
1859 if (jbd_has_csum(&journal->jbd_fs->sb))
1860 tag_tbl_size -= sizeof(struct jbd_block_tail);
1862 if (!trans->start_iblock)
1863 trans->start_iblock = desc_iblock;
1867 if (tag_tbl_size < record_len) {
1868 jbd_set32(header, count,
1869 journal->block_size - tag_tbl_size);
1870 jbd_meta_csum_set(journal->jbd_fs,
1871 (struct jbd_bhdr *)desc_block.data);
1872 jbd_block_set(journal->jbd_fs, &desc_block);
1877 if (record_len == 8) {
1879 (uint64_t *)blocks_entry;
1880 *blocks = to_be64(rec->lba);
1883 (uint32_t *)blocks_entry;
1884 *blocks = to_be32(rec->lba);
1886 blocks_entry += record_len;
1887 tag_tbl_size -= record_len;
1891 if (rc == EOK && desc_iblock) {
1893 jbd_set32(header, count,
1894 journal->block_size - tag_tbl_size);
1896 jbd_meta_csum_set(journal->jbd_fs,
1897 (struct jbd_bhdr *)desc_block.data);
1898 jbd_block_set(journal->jbd_fs, &desc_block);
1904 /**@brief Submit the transaction to transaction queue.
1905 * @param journal current journal session
1906 * @param trans transaction*/
1908 jbd_journal_submit_trans(struct jbd_journal *journal,
1909 struct jbd_trans *trans)
1911 TAILQ_INSERT_TAIL(&journal->trans_queue,
1916 /**@brief Put references of block descriptors in a transaction.
1917 * @param journal current journal session
1918 * @param trans transaction*/
1919 void jbd_journal_cp_trans(struct jbd_journal *journal, struct jbd_trans *trans)
1921 struct jbd_buf *jbd_buf, *tmp;
1922 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1923 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
1925 struct ext4_block block = jbd_buf->block;
1926 ext4_block_set(fs->bdev, &block);
1930 /**@brief Update the start block of the journal when
1931 * all the contents in a transaction reach the disk.*/
1932 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
1933 struct ext4_buf *buf,
1937 struct jbd_buf *jbd_buf = arg;
1938 struct jbd_trans *trans = jbd_buf->trans;
1939 struct jbd_journal *journal = trans->journal;
1940 bool first_in_queue =
1941 trans == TAILQ_FIRST(&journal->cp_queue);
1945 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1946 jbd_buf->block_rec->buf = NULL;
1949 /* Clear the end_write and end_write_arg fields. */
1950 buf->end_write = NULL;
1951 buf->end_write_arg = NULL;
1953 trans->written_cnt++;
1954 if (trans->written_cnt == trans->data_cnt) {
1955 /* If it is the first transaction on checkpoint queue,
1956 * we will shift the start of the journal to the next
1957 * transaction, and remove subsequent written
1958 * transactions from checkpoint queue until we find
1959 * an unwritten one. */
1960 if (first_in_queue) {
1961 journal->start = trans->start_iblock +
1962 trans->alloc_blocks;
1963 wrap(&journal->jbd_fs->sb, journal->start);
1964 journal->trans_id = trans->trans_id + 1;
1965 TAILQ_REMOVE(&journal->cp_queue, trans, trans_node);
1966 jbd_journal_free_trans(journal, trans, false);
1968 jbd_journal_purge_cp_trans(journal, false);
1969 jbd_journal_write_sb(journal);
1970 jbd_write_sb(journal->jbd_fs);
1975 /**@brief Commit a transaction to the journal immediately.
1976 * @param journal current journal session
1977 * @param trans transaction
1978 * @return standard error code*/
1979 int jbd_journal_commit_trans(struct jbd_journal *journal,
1980 struct jbd_trans *trans)
1983 uint32_t last = journal->last;
1985 trans->trans_id = journal->alloc_trans_id;
1986 rc = jbd_journal_prepare(journal, trans);
1990 rc = jbd_journal_prepare_revoke(journal, trans);
1994 if (TAILQ_EMPTY(&trans->buf_queue) &&
1995 LIST_EMPTY(&trans->revoke_list)) {
1996 /* Since there are no entries in both buffer list
1997 * and revoke entry list, we do not consider trans as
1998 * complete transaction and just return EOK.*/
1999 jbd_journal_free_trans(journal, trans, false);
2003 rc = jbd_trans_write_commit_block(trans);
2007 journal->alloc_trans_id++;
2008 if (TAILQ_EMPTY(&journal->cp_queue)) {
2009 if (trans->data_cnt) {
2010 journal->start = trans->start_iblock;
2011 wrap(&journal->jbd_fs->sb, journal->start);
2012 journal->trans_id = trans->trans_id;
2013 jbd_journal_write_sb(journal);
2014 jbd_write_sb(journal->jbd_fs);
2015 TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
2017 jbd_journal_cp_trans(journal, trans);
2019 journal->start = trans->start_iblock +
2020 trans->alloc_blocks;
2021 wrap(&journal->jbd_fs->sb, journal->start);
2022 journal->trans_id = trans->trans_id + 1;
2023 jbd_journal_write_sb(journal);
2024 jbd_journal_free_trans(journal, trans, false);
2027 TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
2029 if (trans->data_cnt)
2030 jbd_journal_cp_trans(journal, trans);
2035 journal->last = last;
2036 jbd_journal_free_trans(journal, trans, true);
2041 /**@brief Commit one transaction on transaction queue
2043 * @param journal current journal session.*/
2044 void jbd_journal_commit_one(struct jbd_journal *journal)
2046 struct jbd_trans *trans;
2048 if ((trans = TAILQ_FIRST(&journal->trans_queue))) {
2049 TAILQ_REMOVE(&journal->trans_queue, trans, trans_node);
2050 jbd_journal_commit_trans(journal, trans);
2054 /**@brief Commit all the transactions on transaction queue
2056 * @param journal current journal session.*/
2057 void jbd_journal_commit_all(struct jbd_journal *journal)
2059 while (!TAILQ_EMPTY(&journal->trans_queue)) {
2060 jbd_journal_commit_one(journal);