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>
40 #include <ext4_misc.h>
41 #include <ext4_errno.h>
42 #include <ext4_debug.h>
45 #include <ext4_super.h>
46 #include <ext4_journal.h>
47 #include <ext4_blockdev.h>
48 #include <ext4_crc32.h>
49 #include <ext4_journal.h>
54 /**@brief Revoke entry during journal replay.*/
56 /**@brief Block number not to be replayed.*/
59 /**@brief For any transaction id smaller
60 * than trans_id, records of @block
61 * in those transactions should not
65 /**@brief Revoke tree node.*/
66 RB_ENTRY(revoke_entry) revoke_node;
69 /**@brief Valid journal replay information.*/
71 /**@brief Starting transaction id.*/
72 uint32_t start_trans_id;
74 /**@brief Ending transaction id.*/
75 uint32_t last_trans_id;
77 /**@brief Used as internal argument.*/
78 uint32_t this_trans_id;
80 /**@brief No of transactions went through.*/
83 /**@brief RB-Tree storing revoke entries.*/
84 RB_HEAD(jbd_revoke, revoke_entry) revoke_root;
87 /**@brief Journal replay internal arguments.*/
89 /**@brief Journal replay information.*/
90 struct recover_info *info;
92 /**@brief Current block we are on.*/
95 /**@brief Current trans_id we are on.*/
96 uint32_t this_trans_id;
99 /* Make sure we wrap around the log correctly! */
100 #define wrap(sb, var) \
102 if (var >= jbd_get32((sb), maxlen)) \
103 var -= (jbd_get32((sb), maxlen) - jbd_get32((sb), first)); \
106 static inline int32_t
107 trans_id_diff(uint32_t x, uint32_t y)
109 int32_t diff = x - y;
114 jbd_revoke_entry_cmp(struct revoke_entry *a, struct revoke_entry *b)
116 if (a->block > b->block)
118 else if (a->block < b->block)
124 jbd_block_rec_cmp(struct jbd_block_rec *a, struct jbd_block_rec *b)
128 else if (a->lba < b->lba)
134 jbd_revoke_rec_cmp(struct jbd_revoke_rec *a, struct jbd_revoke_rec *b)
138 else if (a->lba < b->lba)
143 RB_GENERATE_INTERNAL(jbd_revoke, revoke_entry, revoke_node,
144 jbd_revoke_entry_cmp, static inline)
145 RB_GENERATE_INTERNAL(jbd_block, jbd_block_rec, block_rec_node,
146 jbd_block_rec_cmp, static inline)
147 RB_GENERATE_INTERNAL(jbd_revoke_tree, jbd_revoke_rec, revoke_node,
148 jbd_revoke_rec_cmp, static inline)
150 #define jbd_alloc_revoke_entry() ext4_calloc(1, sizeof(struct revoke_entry))
151 #define jbd_free_revoke_entry(addr) ext4_free(addr)
153 static int jbd_has_csum(struct jbd_sb *jbd_sb)
155 if (JBD_HAS_INCOMPAT_FEATURE(jbd_sb, JBD_FEATURE_INCOMPAT_CSUM_V2))
158 if (JBD_HAS_INCOMPAT_FEATURE(jbd_sb, JBD_FEATURE_INCOMPAT_CSUM_V3))
164 #if CONFIG_META_CSUM_ENABLE
165 static uint32_t jbd_sb_csum(struct jbd_sb *jbd_sb)
167 uint32_t checksum = 0;
169 if (jbd_has_csum(jbd_sb)) {
170 uint32_t orig_checksum = jbd_sb->checksum;
171 jbd_set32(jbd_sb, checksum, 0);
172 /* Calculate crc32c checksum against tho whole superblock */
173 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_sb,
174 JBD_SUPERBLOCK_SIZE);
175 jbd_sb->checksum = orig_checksum;
180 #define jbd_sb_csum(...) 0
183 static void jbd_sb_csum_set(struct jbd_sb *jbd_sb)
185 if (!jbd_has_csum(jbd_sb))
188 jbd_set32(jbd_sb, checksum, jbd_sb_csum(jbd_sb));
191 #if CONFIG_META_CSUM_ENABLE
193 jbd_verify_sb_csum(struct jbd_sb *jbd_sb)
195 if (!jbd_has_csum(jbd_sb))
198 return jbd_sb_csum(jbd_sb) == jbd_get32(jbd_sb, checksum);
201 #define jbd_verify_sb_csum(...) true
204 #if CONFIG_META_CSUM_ENABLE
205 static uint32_t jbd_meta_csum(struct jbd_fs *jbd_fs,
206 struct jbd_bhdr *bhdr)
208 uint32_t checksum = 0;
210 if (jbd_has_csum(&jbd_fs->sb)) {
211 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
212 struct jbd_block_tail *tail =
213 (struct jbd_block_tail *)((char *)bhdr + block_size -
214 sizeof(struct jbd_block_tail));
215 uint32_t orig_checksum = tail->checksum;
218 /* First calculate crc32c checksum against fs uuid */
219 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
220 sizeof(jbd_fs->sb.uuid));
221 /* Calculate crc32c checksum against tho whole block */
222 checksum = ext4_crc32c(checksum, bhdr,
224 tail->checksum = orig_checksum;
229 #define jbd_meta_csum(...) 0
232 static void jbd_meta_csum_set(struct jbd_fs *jbd_fs,
233 struct jbd_bhdr *bhdr)
235 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
236 struct jbd_block_tail *tail = (struct jbd_block_tail *)
237 ((char *)bhdr + block_size -
238 sizeof(struct jbd_block_tail));
239 if (!jbd_has_csum(&jbd_fs->sb))
242 tail->checksum = to_be32(jbd_meta_csum(jbd_fs, bhdr));
245 #if CONFIG_META_CSUM_ENABLE
247 jbd_verify_meta_csum(struct jbd_fs *jbd_fs,
248 struct jbd_bhdr *bhdr)
250 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
251 struct jbd_block_tail *tail = (struct jbd_block_tail *)
252 ((char *)bhdr + block_size -
253 sizeof(struct jbd_block_tail));
254 if (!jbd_has_csum(&jbd_fs->sb))
257 return jbd_meta_csum(jbd_fs, bhdr) == to_be32(tail->checksum);
260 #define jbd_verify_meta_csum(...) true
263 #if CONFIG_META_CSUM_ENABLE
264 static uint32_t jbd_commit_csum(struct jbd_fs *jbd_fs,
265 struct jbd_commit_header *header)
267 uint32_t checksum = 0;
269 if (jbd_has_csum(&jbd_fs->sb)) {
270 uint8_t orig_checksum_type = header->chksum_type,
271 orig_checksum_size = header->chksum_size;
272 uint32_t orig_checksum = header->chksum[0];
273 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
274 header->chksum_type = 0;
275 header->chksum_size = 0;
276 header->chksum[0] = 0;
278 /* First calculate crc32c checksum against fs uuid */
279 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
280 sizeof(jbd_fs->sb.uuid));
281 /* Calculate crc32c checksum against tho whole block */
282 checksum = ext4_crc32c(checksum, header,
285 header->chksum_type = orig_checksum_type;
286 header->chksum_size = orig_checksum_size;
287 header->chksum[0] = orig_checksum;
292 #define jbd_commit_csum(...) 0
295 static void jbd_commit_csum_set(struct jbd_fs *jbd_fs,
296 struct jbd_commit_header *header)
298 if (!jbd_has_csum(&jbd_fs->sb))
301 header->chksum_type = 0;
302 header->chksum_size = 0;
303 header->chksum[0] = jbd_commit_csum(jbd_fs, header);
306 #if CONFIG_META_CSUM_ENABLE
307 static bool jbd_verify_commit_csum(struct jbd_fs *jbd_fs,
308 struct jbd_commit_header *header)
310 if (!jbd_has_csum(&jbd_fs->sb))
313 return header->chksum[0] == to_be32(jbd_commit_csum(jbd_fs,
317 #define jbd_verify_commit_csum(...) true
320 #if CONFIG_META_CSUM_ENABLE
322 * NOTE: We only make use of @csum parameter when
323 * JBD_FEATURE_COMPAT_CHECKSUM is enabled.
325 static uint32_t jbd_block_csum(struct jbd_fs *jbd_fs, const void *buf,
329 uint32_t checksum = 0;
331 if (jbd_has_csum(&jbd_fs->sb)) {
332 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
333 /* First calculate crc32c checksum against fs uuid */
334 checksum = ext4_crc32c(EXT4_CRC32_INIT, jbd_fs->sb.uuid,
335 sizeof(jbd_fs->sb.uuid));
336 /* Then calculate crc32c checksum against sequence no. */
337 checksum = ext4_crc32c(checksum, &sequence,
339 /* Calculate crc32c checksum against tho whole block */
340 checksum = ext4_crc32c(checksum, buf,
342 } else if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
343 JBD_FEATURE_COMPAT_CHECKSUM)) {
344 uint32_t block_size = jbd_get32(&jbd_fs->sb, blocksize);
345 /* Calculate crc32c checksum against tho whole block */
346 checksum = ext4_crc32(csum, buf,
352 #define jbd_block_csum(...) 0
355 static void jbd_block_tag_csum_set(struct jbd_fs *jbd_fs, void *__tag,
358 int ver = jbd_has_csum(&jbd_fs->sb);
363 struct jbd_block_tag *tag = __tag;
364 tag->checksum = (uint16_t)to_be32(checksum);
366 struct jbd_block_tag3 *tag = __tag;
367 tag->checksum = to_be32(checksum);
371 /**@brief Write jbd superblock to disk.
372 * @param jbd_fs jbd filesystem
373 * @param s jbd superblock
374 * @return standard error code*/
375 static int jbd_sb_write(struct jbd_fs *jbd_fs, struct jbd_sb *s)
378 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
381 rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
386 offset = fblock * ext4_sb_get_block_size(&fs->sb);
387 return ext4_block_writebytes(fs->bdev, offset, s,
388 EXT4_SUPERBLOCK_SIZE);
391 /**@brief Read jbd superblock from disk.
392 * @param jbd_fs jbd filesystem
393 * @param s jbd superblock
394 * @return standard error code*/
395 static int jbd_sb_read(struct jbd_fs *jbd_fs, struct jbd_sb *s)
398 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
401 rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
405 offset = fblock * ext4_sb_get_block_size(&fs->sb);
406 return ext4_block_readbytes(fs->bdev, offset, s,
407 EXT4_SUPERBLOCK_SIZE);
410 /**@brief Verify jbd superblock.
411 * @param sb jbd superblock
412 * @return true if jbd superblock is valid */
413 static bool jbd_verify_sb(struct jbd_sb *sb)
415 struct jbd_bhdr *header = &sb->header;
416 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER)
419 if (jbd_get32(header, blocktype) != JBD_SUPERBLOCK &&
420 jbd_get32(header, blocktype) != JBD_SUPERBLOCK_V2)
423 return jbd_verify_sb_csum(sb);
426 /**@brief Write back dirty jbd superblock to disk.
427 * @param jbd_fs jbd filesystem
428 * @return standard error code*/
429 static int jbd_write_sb(struct jbd_fs *jbd_fs)
433 rc = jbd_sb_write(jbd_fs, &jbd_fs->sb);
437 jbd_fs->dirty = false;
442 /**@brief Get reference to jbd filesystem.
443 * @param fs Filesystem to load journal of
444 * @param jbd_fs jbd filesystem
445 * @return standard error code*/
446 int jbd_get_fs(struct ext4_fs *fs,
447 struct jbd_fs *jbd_fs)
450 uint32_t journal_ino;
452 memset(jbd_fs, 0, sizeof(struct jbd_fs));
453 /* See if there is journal inode on this filesystem.*/
454 /* FIXME: detection on existance ofbkejournal bdev is
456 journal_ino = ext4_get32(&fs->sb, journal_inode_number);
458 rc = ext4_fs_get_inode_ref(fs,
464 rc = jbd_sb_read(jbd_fs, &jbd_fs->sb);
468 if (!jbd_verify_sb(&jbd_fs->sb)) {
474 jbd_fs->bdev = fs->bdev;
478 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
479 memset(jbd_fs, 0, sizeof(struct jbd_fs));
484 /**@brief Put reference of jbd filesystem.
485 * @param jbd_fs jbd filesystem
486 * @return standard error code*/
487 int jbd_put_fs(struct jbd_fs *jbd_fs)
490 rc = jbd_write_sb(jbd_fs);
492 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
496 /**@brief Data block lookup helper.
497 * @param jbd_fs jbd filesystem
498 * @param iblock block index
499 * @param fblock logical block address
500 * @return standard error code*/
501 int jbd_inode_bmap(struct jbd_fs *jbd_fs,
503 ext4_fsblk_t *fblock)
505 int rc = ext4_fs_get_inode_dblk_idx(
513 /**@brief jbd block get function (through cache).
514 * @param jbd_fs jbd filesystem
515 * @param block block descriptor
516 * @param fblock jbd logical block address
517 * @return standard error code*/
518 static int jbd_block_get(struct jbd_fs *jbd_fs,
519 struct ext4_block *block,
522 /* TODO: journal device. */
524 struct ext4_blockdev *bdev = jbd_fs->bdev;
525 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
527 /* Lookup the logical block address of
529 rc = jbd_inode_bmap(jbd_fs, iblock,
534 rc = ext4_block_get(bdev, block, fblock);
536 /* If succeeded, mark buffer as BC_FLUSH to indicate
537 * that data should be written to disk immediately.*/
539 ext4_bcache_set_flag(block->buf, BC_FLUSH);
540 /* As we don't want to occupy too much space
541 * in block cache, we set this buffer BC_TMP.*/
542 ext4_bcache_set_flag(block->buf, BC_TMP);
548 /**@brief jbd block get function (through cache, don't read).
549 * @param jbd_fs jbd filesystem
550 * @param block block descriptor
551 * @param fblock jbd logical block address
552 * @return standard error code*/
553 static int jbd_block_get_noread(struct jbd_fs *jbd_fs,
554 struct ext4_block *block,
557 /* TODO: journal device. */
559 struct ext4_blockdev *bdev = jbd_fs->bdev;
560 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
561 rc = jbd_inode_bmap(jbd_fs, iblock,
566 rc = ext4_block_get_noread(bdev, block, fblock);
568 ext4_bcache_set_flag(block->buf, BC_FLUSH);
573 /**@brief jbd block set procedure (through cache).
574 * @param jbd_fs jbd filesystem
575 * @param block block descriptor
576 * @return standard error code*/
577 static int jbd_block_set(struct jbd_fs *jbd_fs,
578 struct ext4_block *block)
580 struct ext4_blockdev *bdev = jbd_fs->bdev;
581 return ext4_block_set(bdev, block);
584 /**@brief helper functions to calculate
585 * block tag size, not including UUID part.
586 * @param jbd_fs jbd filesystem
587 * @return tag size in bytes*/
588 static int jbd_tag_bytes(struct jbd_fs *jbd_fs)
592 /* It is very easy to deal with the case which
593 * JBD_FEATURE_INCOMPAT_CSUM_V3 is enabled.*/
594 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
595 JBD_FEATURE_INCOMPAT_CSUM_V3))
596 return sizeof(struct jbd_block_tag3);
598 size = sizeof(struct jbd_block_tag);
600 /* If JBD_FEATURE_INCOMPAT_CSUM_V2 is enabled,
601 * add 2 bytes to size.*/
602 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
603 JBD_FEATURE_INCOMPAT_CSUM_V2))
604 size += sizeof(uint16_t);
606 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
607 JBD_FEATURE_INCOMPAT_64BIT))
610 /* If block number is 4 bytes in size,
611 * minus 4 bytes from size */
612 return size - sizeof(uint32_t);
615 /**@brief Tag information. */
617 /**@brief Tag size in bytes, including UUID part.*/
620 /**@brief block number stored in this tag.*/
623 /**@brief Is the first 4 bytes of block equals to
624 * JBD_MAGIC_NUMBER? */
627 /**@brief whether UUID part exists or not.*/
630 /**@brief UUID content if UUID part exists.*/
631 uint8_t uuid[UUID_SIZE];
633 /**@brief Is this the last tag? */
636 /**@brief crc32c checksum. */
640 /**@brief Extract information from a block tag.
641 * @param __tag pointer to the block tag
642 * @param tag_bytes block tag size of this jbd filesystem
643 * @param remaining size in buffer containing the block tag
644 * @param tag_info information of this tag.
645 * @return EOK when succeed, otherwise return EINVAL.*/
647 jbd_extract_block_tag(struct jbd_fs *jbd_fs,
650 int32_t remain_buf_size,
651 struct tag_info *tag_info)
654 tag_info->tag_bytes = tag_bytes;
655 tag_info->uuid_exist = false;
656 tag_info->last_tag = false;
657 tag_info->is_escape = false;
659 /* See whether it is possible to hold a valid block tag.*/
660 if (remain_buf_size - tag_bytes < 0)
663 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
664 JBD_FEATURE_INCOMPAT_CSUM_V3)) {
665 struct jbd_block_tag3 *tag = __tag;
666 tag_info->block = jbd_get32(tag, blocknr);
667 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
668 JBD_FEATURE_INCOMPAT_64BIT))
670 (uint64_t)jbd_get32(tag, blocknr_high) << 32;
672 if (jbd_get32(tag, flags) & JBD_FLAG_ESCAPE)
673 tag_info->is_escape = true;
675 if (!(jbd_get32(tag, flags) & JBD_FLAG_SAME_UUID)) {
676 /* See whether it is possible to hold UUID part.*/
677 if (remain_buf_size - tag_bytes < UUID_SIZE)
680 uuid_start = (char *)tag + tag_bytes;
681 tag_info->uuid_exist = true;
682 tag_info->tag_bytes += UUID_SIZE;
683 memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
686 if (jbd_get32(tag, flags) & JBD_FLAG_LAST_TAG)
687 tag_info->last_tag = true;
690 struct jbd_block_tag *tag = __tag;
691 tag_info->block = jbd_get32(tag, blocknr);
692 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
693 JBD_FEATURE_INCOMPAT_64BIT))
695 (uint64_t)jbd_get32(tag, blocknr_high) << 32;
697 if (jbd_get16(tag, flags) & JBD_FLAG_ESCAPE)
698 tag_info->is_escape = true;
700 if (!(jbd_get16(tag, flags) & JBD_FLAG_SAME_UUID)) {
701 /* See whether it is possible to hold UUID part.*/
702 if (remain_buf_size - tag_bytes < UUID_SIZE)
705 uuid_start = (char *)tag + tag_bytes;
706 tag_info->uuid_exist = true;
707 tag_info->tag_bytes += UUID_SIZE;
708 memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
711 if (jbd_get16(tag, flags) & JBD_FLAG_LAST_TAG)
712 tag_info->last_tag = true;
718 /**@brief Write information to a block tag.
719 * @param __tag pointer to the block tag
720 * @param remaining size in buffer containing the block tag
721 * @param tag_info information of this tag.
722 * @return EOK when succeed, otherwise return EINVAL.*/
724 jbd_write_block_tag(struct jbd_fs *jbd_fs,
726 int32_t remain_buf_size,
727 struct tag_info *tag_info)
730 int tag_bytes = jbd_tag_bytes(jbd_fs);
732 tag_info->tag_bytes = tag_bytes;
734 /* See whether it is possible to hold a valid block tag.*/
735 if (remain_buf_size - tag_bytes < 0)
738 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
739 JBD_FEATURE_INCOMPAT_CSUM_V3)) {
740 struct jbd_block_tag3 *tag = __tag;
741 memset(tag, 0, sizeof(struct jbd_block_tag3));
742 jbd_set32(tag, blocknr, (uint32_t)tag_info->block);
743 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
744 JBD_FEATURE_INCOMPAT_64BIT))
745 jbd_set32(tag, blocknr_high, tag_info->block >> 32);
747 if (tag_info->uuid_exist) {
748 /* See whether it is possible to hold UUID part.*/
749 if (remain_buf_size - tag_bytes < UUID_SIZE)
752 uuid_start = (char *)tag + tag_bytes;
753 tag_info->tag_bytes += UUID_SIZE;
754 memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
756 jbd_set32(tag, flags,
757 jbd_get32(tag, flags) | JBD_FLAG_SAME_UUID);
759 jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum);
761 if (tag_info->last_tag)
762 jbd_set32(tag, flags,
763 jbd_get32(tag, flags) | JBD_FLAG_LAST_TAG);
765 if (tag_info->is_escape)
766 jbd_set32(tag, flags,
767 jbd_get32(tag, flags) | JBD_FLAG_ESCAPE);
770 struct jbd_block_tag *tag = __tag;
771 memset(tag, 0, sizeof(struct jbd_block_tag));
772 jbd_set32(tag, blocknr, (uint32_t)tag_info->block);
773 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
774 JBD_FEATURE_INCOMPAT_64BIT))
775 jbd_set32(tag, blocknr_high, tag_info->block >> 32);
777 if (tag_info->uuid_exist) {
778 /* See whether it is possible to hold UUID part.*/
779 if (remain_buf_size - tag_bytes < UUID_SIZE)
782 uuid_start = (char *)tag + tag_bytes;
783 tag_info->tag_bytes += UUID_SIZE;
784 memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
786 jbd_set16(tag, flags,
787 jbd_get16(tag, flags) | JBD_FLAG_SAME_UUID);
789 jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum);
791 if (tag_info->last_tag)
792 jbd_set16(tag, flags,
793 jbd_get16(tag, flags) | JBD_FLAG_LAST_TAG);
796 if (tag_info->is_escape)
797 jbd_set16(tag, flags,
798 jbd_get16(tag, flags) | JBD_FLAG_ESCAPE);
804 /**@brief Iterate all block tags in a block.
805 * @param jbd_fs jbd filesystem
806 * @param __tag_start pointer to the block
807 * @param tag_tbl_size size of the block
808 * @param func callback routine to indicate that
809 * a block tag is found
810 * @param arg additional argument to be passed to func */
812 jbd_iterate_block_table(struct jbd_fs *jbd_fs,
814 int32_t tag_tbl_size,
815 void (*func)(struct jbd_fs * jbd_fs,
816 struct tag_info *tag_info,
820 char *tag_start, *tag_ptr;
821 int tag_bytes = jbd_tag_bytes(jbd_fs);
822 tag_start = __tag_start;
825 /* Cut off the size of block tail storing checksum. */
826 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
827 JBD_FEATURE_INCOMPAT_CSUM_V2) ||
828 JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
829 JBD_FEATURE_INCOMPAT_CSUM_V3))
830 tag_tbl_size -= sizeof(struct jbd_block_tail);
832 while (tag_tbl_size) {
833 struct tag_info tag_info;
834 int rc = jbd_extract_block_tag(jbd_fs,
843 func(jbd_fs, &tag_info, arg);
845 /* Stop the iteration when we reach the last tag. */
846 if (tag_info.last_tag)
849 tag_ptr += tag_info.tag_bytes;
850 tag_tbl_size -= tag_info.tag_bytes;
854 static void jbd_display_block_tags(struct jbd_fs *jbd_fs,
855 struct tag_info *tag_info,
858 uint32_t *iblock = arg;
859 ext4_dbg(DEBUG_JBD, "Block in block_tag: %" PRIu64 "\n", tag_info->block);
861 wrap(&jbd_fs->sb, *iblock);
866 static struct revoke_entry *
867 jbd_revoke_entry_lookup(struct recover_info *info, ext4_fsblk_t block)
869 struct revoke_entry tmp = {
873 return RB_FIND(jbd_revoke, &info->revoke_root, &tmp);
876 /**@brief Replay a block in a transaction.
877 * @param jbd_fs jbd filesystem
878 * @param tag_info tag_info of the logged block.*/
879 static void jbd_replay_block_tags(struct jbd_fs *jbd_fs,
880 struct tag_info *tag_info,
884 struct replay_arg *arg = __arg;
885 struct recover_info *info = arg->info;
886 uint32_t *this_block = arg->this_block;
887 struct revoke_entry *revoke_entry;
888 struct ext4_block journal_block, ext4_block;
889 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
892 wrap(&jbd_fs->sb, *this_block);
894 /* We replay this block only if the current transaction id
895 * is equal or greater than that in revoke entry.*/
896 revoke_entry = jbd_revoke_entry_lookup(info, tag_info->block);
898 trans_id_diff(arg->this_trans_id, revoke_entry->trans_id) <= 0)
902 "Replaying block in block_tag: %" PRIu64 "\n",
905 r = jbd_block_get(jbd_fs, &journal_block, *this_block);
909 /* We need special treatment for ext4 superblock. */
910 if (tag_info->block) {
911 r = ext4_block_get_noread(fs->bdev, &ext4_block, tag_info->block);
913 jbd_block_set(jbd_fs, &journal_block);
917 memcpy(ext4_block.data,
919 jbd_get32(&jbd_fs->sb, blocksize));
921 if (tag_info->is_escape)
922 ((struct jbd_bhdr *)ext4_block.data)->magic =
923 to_be32(JBD_MAGIC_NUMBER);
925 ext4_bcache_set_dirty(ext4_block.buf);
926 ext4_block_set(fs->bdev, &ext4_block);
928 uint16_t mount_count, state;
929 mount_count = ext4_get16(&fs->sb, mount_count);
930 state = ext4_get16(&fs->sb, state);
933 journal_block.data + EXT4_SUPERBLOCK_OFFSET,
934 EXT4_SUPERBLOCK_SIZE);
936 /* Mark system as mounted */
937 ext4_set16(&fs->sb, state, state);
938 r = ext4_sb_write(fs->bdev, &fs->sb);
942 /*Update mount count*/
943 ext4_set16(&fs->sb, mount_count, mount_count);
946 jbd_block_set(jbd_fs, &journal_block);
951 /**@brief Add block address to revoke tree, along with
952 * its transaction id.
953 * @param info journal replay info
954 * @param block block address to be replayed.*/
955 static void jbd_add_revoke_block_tags(struct recover_info *info,
958 struct revoke_entry *revoke_entry;
960 ext4_dbg(DEBUG_JBD, "Add block %" PRIu64 " to revoke tree\n", block);
961 /* If the revoke entry with respect to the block address
962 * exists already, update its transaction id.*/
963 revoke_entry = jbd_revoke_entry_lookup(info, block);
965 revoke_entry->trans_id = info->this_trans_id;
969 revoke_entry = jbd_alloc_revoke_entry();
970 ext4_assert(revoke_entry);
971 revoke_entry->block = block;
972 revoke_entry->trans_id = info->this_trans_id;
973 RB_INSERT(jbd_revoke, &info->revoke_root, revoke_entry);
978 static void jbd_destroy_revoke_tree(struct recover_info *info)
980 while (!RB_EMPTY(&info->revoke_root)) {
981 struct revoke_entry *revoke_entry =
982 RB_MIN(jbd_revoke, &info->revoke_root);
983 ext4_assert(revoke_entry);
984 RB_REMOVE(jbd_revoke, &info->revoke_root, revoke_entry);
985 jbd_free_revoke_entry(revoke_entry);
990 #define ACTION_SCAN 0
991 #define ACTION_REVOKE 1
992 #define ACTION_RECOVER 2
994 /**@brief Add entries in a revoke block to revoke tree.
995 * @param jbd_fs jbd filesystem
996 * @param header revoke block header
997 * @param recover_info journal replay info*/
998 static void jbd_build_revoke_tree(struct jbd_fs *jbd_fs,
999 struct jbd_bhdr *header,
1000 struct recover_info *info)
1003 struct jbd_revoke_header *revoke_hdr =
1004 (struct jbd_revoke_header *)header;
1005 uint32_t i, nr_entries, record_len = 4;
1007 /* If we are working on a 64bit jbd filesystem, */
1008 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
1009 JBD_FEATURE_INCOMPAT_64BIT))
1012 nr_entries = (jbd_get32(revoke_hdr, count) -
1013 sizeof(struct jbd_revoke_header)) /
1016 blocks_entry = (char *)(revoke_hdr + 1);
1018 for (i = 0;i < nr_entries;i++) {
1019 if (record_len == 8) {
1021 (uint64_t *)blocks_entry;
1022 jbd_add_revoke_block_tags(info, to_be64(*blocks));
1025 (uint32_t *)blocks_entry;
1026 jbd_add_revoke_block_tags(info, to_be32(*blocks));
1028 blocks_entry += record_len;
1032 static void jbd_debug_descriptor_block(struct jbd_fs *jbd_fs,
1033 struct jbd_bhdr *header,
1036 jbd_iterate_block_table(jbd_fs,
1038 jbd_get32(&jbd_fs->sb, blocksize) -
1039 sizeof(struct jbd_bhdr),
1040 jbd_display_block_tags,
1044 static void jbd_replay_descriptor_block(struct jbd_fs *jbd_fs,
1045 struct jbd_bhdr *header,
1046 struct replay_arg *arg)
1048 jbd_iterate_block_table(jbd_fs,
1050 jbd_get32(&jbd_fs->sb, blocksize) -
1051 sizeof(struct jbd_bhdr),
1052 jbd_replay_block_tags,
1056 /**@brief The core routine of journal replay.
1057 * @param jbd_fs jbd filesystem
1058 * @param recover_info journal replay info
1059 * @param action action needed to be taken
1060 * @return standard error code*/
1061 static int jbd_iterate_log(struct jbd_fs *jbd_fs,
1062 struct recover_info *info,
1066 bool log_end = false;
1067 struct jbd_sb *sb = &jbd_fs->sb;
1068 uint32_t start_trans_id, this_trans_id;
1069 uint32_t start_block, this_block;
1071 /* We start iterating valid blocks in the whole journal.*/
1072 start_trans_id = this_trans_id = jbd_get32(sb, sequence);
1073 start_block = this_block = jbd_get32(sb, start);
1074 if (action == ACTION_SCAN)
1075 info->trans_cnt = 0;
1076 else if (!info->trans_cnt)
1079 ext4_dbg(DEBUG_JBD, "Start of journal at trans id: %" PRIu32 "\n",
1083 struct ext4_block block;
1084 struct jbd_bhdr *header;
1085 /* If we are not scanning for the last
1086 * valid transaction in the journal,
1087 * we will stop when we reach the end of
1089 if (action != ACTION_SCAN)
1090 if (trans_id_diff(this_trans_id, info->last_trans_id) > 0) {
1095 r = jbd_block_get(jbd_fs, &block, this_block);
1099 header = (struct jbd_bhdr *)block.data;
1100 /* This block does not have a valid magic number,
1101 * so we have reached the end of the journal.*/
1102 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER) {
1103 jbd_block_set(jbd_fs, &block);
1108 /* If the transaction id we found is not expected,
1109 * we may have reached the end of the journal.
1111 * If we are not scanning the journal, something
1112 * bad might have taken place. :-( */
1113 if (jbd_get32(header, sequence) != this_trans_id) {
1114 if (action != ACTION_SCAN)
1117 jbd_block_set(jbd_fs, &block);
1122 switch (jbd_get32(header, blocktype)) {
1123 case JBD_DESCRIPTOR_BLOCK:
1124 if (!jbd_verify_meta_csum(jbd_fs, header)) {
1126 DBG_WARN "Descriptor block checksum failed."
1127 "Journal block: %" PRIu32"\n",
1132 ext4_dbg(DEBUG_JBD, "Descriptor block: %" PRIu32", "
1133 "trans_id: %" PRIu32"\n",
1134 this_block, this_trans_id);
1135 if (action == ACTION_RECOVER) {
1136 struct replay_arg replay_arg;
1137 replay_arg.info = info;
1138 replay_arg.this_block = &this_block;
1139 replay_arg.this_trans_id = this_trans_id;
1141 jbd_replay_descriptor_block(jbd_fs,
1142 header, &replay_arg);
1144 jbd_debug_descriptor_block(jbd_fs,
1145 header, &this_block);
1148 case JBD_COMMIT_BLOCK:
1149 if (!jbd_verify_commit_csum(jbd_fs,
1150 (struct jbd_commit_header *)header)) {
1152 DBG_WARN "Commit block checksum failed."
1153 "Journal block: %" PRIu32"\n",
1158 ext4_dbg(DEBUG_JBD, "Commit block: %" PRIu32", "
1159 "trans_id: %" PRIu32"\n",
1160 this_block, this_trans_id);
1162 * This is the end of a transaction,
1163 * we may now proceed to the next transaction.
1166 if (action == ACTION_SCAN)
1169 case JBD_REVOKE_BLOCK:
1170 if (!jbd_verify_meta_csum(jbd_fs, header)) {
1172 DBG_WARN "Revoke block checksum failed."
1173 "Journal block: %" PRIu32"\n",
1178 ext4_dbg(DEBUG_JBD, "Revoke block: %" PRIu32", "
1179 "trans_id: %" PRIu32"\n",
1180 this_block, this_trans_id);
1181 if (action == ACTION_REVOKE) {
1182 info->this_trans_id = this_trans_id;
1183 jbd_build_revoke_tree(jbd_fs,
1191 jbd_block_set(jbd_fs, &block);
1193 wrap(sb, this_block);
1194 if (this_block == start_block)
1198 ext4_dbg(DEBUG_JBD, "End of journal.\n");
1199 if (r == EOK && action == ACTION_SCAN) {
1200 /* We have finished scanning the journal. */
1201 info->start_trans_id = start_trans_id;
1202 if (trans_id_diff(this_trans_id, start_trans_id) > 0)
1203 info->last_trans_id = this_trans_id - 1;
1205 info->last_trans_id = this_trans_id;
1211 /**@brief Replay journal.
1212 * @param jbd_fs jbd filesystem
1213 * @return standard error code*/
1214 int jbd_recover(struct jbd_fs *jbd_fs)
1217 struct recover_info info;
1218 struct jbd_sb *sb = &jbd_fs->sb;
1222 RB_INIT(&info.revoke_root);
1224 r = jbd_iterate_log(jbd_fs, &info, ACTION_SCAN);
1228 r = jbd_iterate_log(jbd_fs, &info, ACTION_REVOKE);
1232 r = jbd_iterate_log(jbd_fs, &info, ACTION_RECOVER);
1234 /* If we successfully replay the journal,
1235 * clear EXT4_FINCOM_RECOVER flag on the
1236 * ext4 superblock, and set the start of
1238 uint32_t features_incompatible =
1239 ext4_get32(&jbd_fs->inode_ref.fs->sb,
1240 features_incompatible);
1241 jbd_set32(&jbd_fs->sb, start, 0);
1242 jbd_set32(&jbd_fs->sb, sequence, info.last_trans_id);
1243 features_incompatible &= ~EXT4_FINCOM_RECOVER;
1244 ext4_set32(&jbd_fs->inode_ref.fs->sb,
1245 features_incompatible,
1246 features_incompatible);
1247 jbd_fs->dirty = true;
1248 r = ext4_sb_write(jbd_fs->bdev,
1249 &jbd_fs->inode_ref.fs->sb);
1251 jbd_destroy_revoke_tree(&info);
1255 static void jbd_journal_write_sb(struct jbd_journal *journal)
1257 struct jbd_fs *jbd_fs = journal->jbd_fs;
1258 jbd_set32(&jbd_fs->sb, start, journal->start);
1259 jbd_set32(&jbd_fs->sb, sequence, journal->trans_id);
1260 jbd_fs->dirty = true;
1263 /**@brief Start accessing the journal.
1264 * @param jbd_fs jbd filesystem
1265 * @param journal current journal session
1266 * @return standard error code*/
1267 int jbd_journal_start(struct jbd_fs *jbd_fs,
1268 struct jbd_journal *journal)
1271 uint32_t features_incompatible =
1272 ext4_get32(&jbd_fs->inode_ref.fs->sb,
1273 features_incompatible);
1274 features_incompatible |= EXT4_FINCOM_RECOVER;
1275 ext4_set32(&jbd_fs->inode_ref.fs->sb,
1276 features_incompatible,
1277 features_incompatible);
1278 r = ext4_sb_write(jbd_fs->bdev,
1279 &jbd_fs->inode_ref.fs->sb);
1283 journal->first = jbd_get32(&jbd_fs->sb, first);
1284 journal->start = journal->first;
1285 journal->last = journal->first;
1287 * To invalidate any stale records we need to start from
1288 * the checkpoint transaction ID of the previous journalling session
1291 journal->trans_id = jbd_get32(&jbd_fs->sb, sequence) + 1;
1292 journal->alloc_trans_id = journal->trans_id;
1294 journal->block_size = jbd_get32(&jbd_fs->sb, blocksize);
1296 TAILQ_INIT(&journal->cp_queue);
1297 RB_INIT(&journal->block_rec_root);
1298 journal->jbd_fs = jbd_fs;
1299 jbd_journal_write_sb(journal);
1300 r = jbd_write_sb(jbd_fs);
1304 jbd_fs->bdev->journal = journal;
1308 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
1309 struct ext4_buf *buf __unused,
1314 * This routine is only suitable to committed transactions. */
1315 static void jbd_journal_flush_trans(struct jbd_trans *trans)
1317 struct jbd_buf *jbd_buf, *tmp;
1318 struct jbd_journal *journal = trans->journal;
1319 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1320 void *tmp_data = ext4_malloc(journal->block_size);
1321 ext4_assert(tmp_data);
1323 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
1325 struct ext4_buf *buf;
1326 struct ext4_block block;
1327 /* The buffer is not yet flushed. */
1328 buf = ext4_bcache_find_get(fs->bdev->bc, &block,
1329 jbd_buf->block_rec->lba);
1330 if (!(buf && ext4_bcache_test_flag(buf, BC_UPTODATE) &&
1331 jbd_buf->block_rec->trans == trans)) {
1333 struct ext4_block jbd_block = EXT4_BLOCK_ZERO();
1334 r = jbd_block_get(journal->jbd_fs,
1337 ext4_assert(r == EOK);
1338 memcpy(tmp_data, jbd_block.data,
1339 journal->block_size);
1340 ext4_block_set(fs->bdev, &jbd_block);
1341 r = ext4_blocks_set_direct(fs->bdev, tmp_data,
1342 jbd_buf->block_rec->lba, 1);
1343 jbd_trans_end_write(fs->bdev->bc, buf, r, jbd_buf);
1345 ext4_block_flush_buf(fs->bdev, buf);
1348 ext4_block_set(fs->bdev, &block);
1351 ext4_free(tmp_data);
1355 jbd_journal_skip_pure_revoke(struct jbd_journal *journal,
1356 struct jbd_trans *trans)
1358 journal->start = trans->start_iblock +
1359 trans->alloc_blocks;
1360 wrap(&journal->jbd_fs->sb, journal->start);
1361 journal->trans_id = trans->trans_id + 1;
1362 jbd_journal_free_trans(journal,
1364 jbd_journal_write_sb(journal);
1368 jbd_journal_purge_cp_trans(struct jbd_journal *journal,
1372 struct jbd_trans *trans;
1373 while ((trans = TAILQ_FIRST(&journal->cp_queue))) {
1374 if (!trans->data_cnt) {
1375 TAILQ_REMOVE(&journal->cp_queue,
1378 jbd_journal_skip_pure_revoke(journal, trans);
1380 if (trans->data_cnt ==
1381 trans->written_cnt) {
1383 trans->start_iblock +
1384 trans->alloc_blocks;
1385 wrap(&journal->jbd_fs->sb,
1388 trans->trans_id + 1;
1389 TAILQ_REMOVE(&journal->cp_queue,
1392 jbd_journal_free_trans(journal,
1395 jbd_journal_write_sb(journal);
1396 } else if (!flush) {
1398 trans->start_iblock;
1399 wrap(&journal->jbd_fs->sb,
1403 jbd_journal_write_sb(journal);
1406 jbd_journal_flush_trans(trans);
1413 /**@brief Stop accessing the journal.
1414 * @param journal current journal session
1415 * @return standard error code*/
1416 int jbd_journal_stop(struct jbd_journal *journal)
1419 struct jbd_fs *jbd_fs = journal->jbd_fs;
1420 uint32_t features_incompatible;
1422 /* Make sure that journalled content have reached
1424 jbd_journal_purge_cp_trans(journal, true, false);
1426 /* There should be no block record in this journal
1428 if (!RB_EMPTY(&journal->block_rec_root))
1430 DBG_WARN "There are still block records "
1431 "in this journal session!\n");
1433 features_incompatible =
1434 ext4_get32(&jbd_fs->inode_ref.fs->sb,
1435 features_incompatible);
1436 features_incompatible &= ~EXT4_FINCOM_RECOVER;
1437 ext4_set32(&jbd_fs->inode_ref.fs->sb,
1438 features_incompatible,
1439 features_incompatible);
1440 r = ext4_sb_write(jbd_fs->bdev,
1441 &jbd_fs->inode_ref.fs->sb);
1446 journal->trans_id = 0;
1447 jbd_journal_write_sb(journal);
1448 return jbd_write_sb(journal->jbd_fs);
1451 /**@brief Allocate a block in the journal.
1452 * @param journal current journal session
1453 * @param trans transaction
1454 * @return allocated block address*/
1455 static uint32_t jbd_journal_alloc_block(struct jbd_journal *journal,
1456 struct jbd_trans *trans)
1458 uint32_t start_block;
1460 start_block = journal->last++;
1461 trans->alloc_blocks++;
1462 wrap(&journal->jbd_fs->sb, journal->last);
1464 /* If there is no space left, flush just one journalled
1466 if (journal->last == journal->start) {
1467 jbd_journal_purge_cp_trans(journal, true, true);
1468 ext4_assert(journal->last != journal->start);
1474 static struct jbd_block_rec *
1475 jbd_trans_block_rec_lookup(struct jbd_journal *journal,
1478 struct jbd_block_rec tmp = {
1482 return RB_FIND(jbd_block,
1483 &journal->block_rec_root,
1488 jbd_trans_change_ownership(struct jbd_block_rec *block_rec,
1489 struct jbd_trans *new_trans)
1491 LIST_REMOVE(block_rec, tbrec_node);
1493 /* Now this block record belongs to this transaction. */
1494 LIST_INSERT_HEAD(&new_trans->tbrec_list, block_rec, tbrec_node);
1496 block_rec->trans = new_trans;
1499 static inline struct jbd_block_rec *
1500 jbd_trans_insert_block_rec(struct jbd_trans *trans,
1503 struct jbd_block_rec *block_rec;
1504 block_rec = jbd_trans_block_rec_lookup(trans->journal, lba);
1506 jbd_trans_change_ownership(block_rec, trans);
1509 block_rec = ext4_calloc(1, sizeof(struct jbd_block_rec));
1513 block_rec->lba = lba;
1514 block_rec->trans = trans;
1515 TAILQ_INIT(&block_rec->dirty_buf_queue);
1516 LIST_INSERT_HEAD(&trans->tbrec_list, block_rec, tbrec_node);
1517 RB_INSERT(jbd_block, &trans->journal->block_rec_root, block_rec);
1522 * This routine will do the dirty works.
1525 jbd_trans_finish_callback(struct jbd_journal *journal,
1526 const struct jbd_trans *trans,
1527 struct jbd_block_rec *block_rec,
1531 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1532 if (block_rec->trans != trans)
1536 struct jbd_buf *jbd_buf, *tmp;
1537 TAILQ_FOREACH_SAFE(jbd_buf,
1538 &block_rec->dirty_buf_queue,
1541 jbd_trans_end_write(fs->bdev->bc,
1548 * We have to roll back data if the block is going to be
1551 struct jbd_buf *jbd_buf;
1552 struct ext4_block jbd_block = EXT4_BLOCK_ZERO(),
1553 block = EXT4_BLOCK_ZERO();
1554 jbd_buf = TAILQ_LAST(&block_rec->dirty_buf_queue,
1559 r = ext4_block_get_noread(fs->bdev,
1562 ext4_assert(r == EOK);
1563 r = jbd_block_get(journal->jbd_fs,
1566 ext4_assert(r == EOK);
1567 memcpy(block.data, jbd_block.data,
1568 journal->block_size);
1570 jbd_trans_change_ownership(block_rec,
1573 block.buf->end_write = jbd_trans_end_write;
1574 block.buf->end_write_arg = jbd_buf;
1576 ext4_bcache_set_flag(jbd_block.buf, BC_TMP);
1577 ext4_bcache_set_dirty(block.buf);
1579 ext4_block_set(fs->bdev, &jbd_block);
1580 ext4_block_set(fs->bdev, &block);
1583 /* The revoked buffer is yet written. */
1584 jbd_trans_change_ownership(block_rec,
1592 jbd_trans_remove_block_rec(struct jbd_journal *journal,
1593 struct jbd_block_rec *block_rec,
1594 struct jbd_trans *trans)
1596 /* If this block record doesn't belong to this transaction,
1598 if (block_rec->trans == trans) {
1599 LIST_REMOVE(block_rec, tbrec_node);
1600 RB_REMOVE(jbd_block,
1601 &journal->block_rec_root,
1603 ext4_free(block_rec);
1607 /**@brief Add block to a transaction and mark it dirty.
1608 * @param trans transaction
1609 * @param block block descriptor
1610 * @return standard error code*/
1611 int jbd_trans_set_block_dirty(struct jbd_trans *trans,
1612 struct ext4_block *block)
1614 struct jbd_buf *jbd_buf;
1615 struct jbd_revoke_rec *rec, tmp_rec = {
1618 struct jbd_block_rec *block_rec;
1620 if (block->buf->end_write == jbd_trans_end_write) {
1621 jbd_buf = block->buf->end_write_arg;
1622 if (jbd_buf && jbd_buf->trans == trans)
1625 jbd_buf = ext4_calloc(1, sizeof(struct jbd_buf));
1629 if ((block_rec = jbd_trans_insert_block_rec(trans,
1630 block->lb_id)) == NULL) {
1635 TAILQ_INSERT_TAIL(&block_rec->dirty_buf_queue,
1639 jbd_buf->block_rec = block_rec;
1640 jbd_buf->trans = trans;
1641 jbd_buf->block = *block;
1642 ext4_bcache_inc_ref(block->buf);
1644 /* If the content reach the disk, notify us
1645 * so that we may do a checkpoint. */
1646 block->buf->end_write = jbd_trans_end_write;
1647 block->buf->end_write_arg = jbd_buf;
1650 TAILQ_INSERT_HEAD(&trans->buf_queue, jbd_buf, buf_node);
1652 ext4_bcache_set_dirty(block->buf);
1653 rec = RB_FIND(jbd_revoke_tree,
1654 &trans->revoke_root,
1657 RB_REMOVE(jbd_revoke_tree, &trans->revoke_root,
1665 /**@brief Add block to be revoked to a transaction
1666 * @param trans transaction
1667 * @param lba logical block address
1668 * @return standard error code*/
1669 int jbd_trans_revoke_block(struct jbd_trans *trans,
1672 struct jbd_revoke_rec tmp_rec = {
1675 rec = RB_FIND(jbd_revoke_tree,
1676 &trans->revoke_root,
1681 rec = ext4_calloc(1, sizeof(struct jbd_revoke_rec));
1686 RB_INSERT(jbd_revoke_tree, &trans->revoke_root, rec);
1690 /**@brief Try to add block to be revoked to a transaction.
1691 * If @lba still remains in an transaction on checkpoint
1692 * queue, add @lba as a revoked block to the transaction.
1693 * @param trans transaction
1694 * @param lba logical block address
1695 * @return standard error code*/
1696 int jbd_trans_try_revoke_block(struct jbd_trans *trans,
1699 struct jbd_journal *journal = trans->journal;
1700 struct jbd_block_rec *block_rec =
1701 jbd_trans_block_rec_lookup(journal, lba);
1704 if (block_rec->trans == trans) {
1705 struct jbd_buf *jbd_buf =
1706 TAILQ_LAST(&block_rec->dirty_buf_queue,
1708 /* If there are still unwritten buffers. */
1709 if (TAILQ_FIRST(&block_rec->dirty_buf_queue) !=
1711 jbd_trans_revoke_block(trans, lba);
1714 jbd_trans_revoke_block(trans, lba);
1720 /**@brief Free a transaction
1721 * @param journal current journal session
1722 * @param trans transaction
1723 * @param abort discard all the modifications on the block?
1724 * @return standard error code*/
1725 void jbd_journal_free_trans(struct jbd_journal *journal,
1726 struct jbd_trans *trans,
1729 struct jbd_buf *jbd_buf, *tmp;
1730 struct jbd_revoke_rec *rec, *tmp2;
1731 struct jbd_block_rec *block_rec, *tmp3;
1732 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1733 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
1735 block_rec = jbd_buf->block_rec;
1737 jbd_buf->block.buf->end_write = NULL;
1738 jbd_buf->block.buf->end_write_arg = NULL;
1739 ext4_bcache_clear_dirty(jbd_buf->block.buf);
1740 ext4_block_set(fs->bdev, &jbd_buf->block);
1743 TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue,
1746 jbd_trans_finish_callback(journal,
1751 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1754 RB_FOREACH_SAFE(rec, jbd_revoke_tree, &trans->revoke_root,
1756 RB_REMOVE(jbd_revoke_tree, &trans->revoke_root, rec);
1759 LIST_FOREACH_SAFE(block_rec, &trans->tbrec_list, tbrec_node,
1761 jbd_trans_remove_block_rec(journal, block_rec, trans);
1767 /**@brief Write commit block for a transaction
1768 * @param trans transaction
1769 * @return standard error code*/
1770 static int jbd_trans_write_commit_block(struct jbd_trans *trans)
1773 struct ext4_block block;
1774 struct jbd_commit_header *header;
1775 uint32_t commit_iblock;
1776 struct jbd_journal *journal = trans->journal;
1778 commit_iblock = jbd_journal_alloc_block(journal, trans);
1780 rc = jbd_block_get_noread(journal->jbd_fs, &block, commit_iblock);
1784 header = (struct jbd_commit_header *)block.data;
1785 jbd_set32(&header->header, magic, JBD_MAGIC_NUMBER);
1786 jbd_set32(&header->header, blocktype, JBD_COMMIT_BLOCK);
1787 jbd_set32(&header->header, sequence, trans->trans_id);
1789 if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb,
1790 JBD_FEATURE_COMPAT_CHECKSUM)) {
1791 header->chksum_type = JBD_CRC32_CHKSUM;
1792 header->chksum_size = JBD_CRC32_CHKSUM_SIZE;
1793 jbd_set32(header, chksum[0], trans->data_csum);
1795 jbd_commit_csum_set(journal->jbd_fs, header);
1796 ext4_bcache_set_dirty(block.buf);
1797 ext4_bcache_set_flag(block.buf, BC_TMP);
1798 rc = jbd_block_set(journal->jbd_fs, &block);
1802 /**@brief Write descriptor block for a transaction
1803 * @param journal current journal session
1804 * @param trans transaction
1805 * @return standard error code*/
1806 static int jbd_journal_prepare(struct jbd_journal *journal,
1807 struct jbd_trans *trans)
1809 int rc = EOK, i = 0;
1810 struct ext4_block desc_block = EXT4_BLOCK_ZERO(),
1811 data_block = EXT4_BLOCK_ZERO();
1812 int32_t tag_tbl_size = 0;
1813 uint32_t desc_iblock = 0;
1814 uint32_t data_iblock = 0;
1815 char *tag_start = NULL, *tag_ptr = NULL;
1816 struct jbd_buf *jbd_buf, *tmp;
1817 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1818 uint32_t checksum = EXT4_CRC32_INIT;
1819 struct jbd_bhdr *bhdr = NULL;
1822 /* Try to remove any non-dirty buffers from the tail of
1824 TAILQ_FOREACH_REVERSE_SAFE(jbd_buf, &trans->buf_queue,
1825 jbd_trans_buf, buf_node, tmp) {
1826 struct jbd_revoke_rec tmp_rec = {
1827 .lba = jbd_buf->block_rec->lba
1829 /* We stop the iteration when we find a dirty buffer. */
1830 if (ext4_bcache_test_flag(jbd_buf->block.buf,
1834 TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue,
1838 jbd_buf->block.buf->end_write = NULL;
1839 jbd_buf->block.buf->end_write_arg = NULL;
1840 jbd_trans_finish_callback(journal,
1844 RB_FIND(jbd_revoke_tree,
1845 &trans->revoke_root,
1847 jbd_trans_remove_block_rec(journal,
1848 jbd_buf->block_rec, trans);
1851 ext4_block_set(fs->bdev, &jbd_buf->block);
1852 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1856 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node, tmp) {
1857 struct tag_info tag_info;
1858 bool uuid_exist = false;
1859 bool is_escape = false;
1860 struct jbd_revoke_rec tmp_rec = {
1861 .lba = jbd_buf->block_rec->lba
1863 if (!ext4_bcache_test_flag(jbd_buf->block.buf,
1865 TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue,
1869 jbd_buf->block.buf->end_write = NULL;
1870 jbd_buf->block.buf->end_write_arg = NULL;
1872 /* The buffer has not been modified, just release
1874 jbd_trans_finish_callback(journal,
1878 RB_FIND(jbd_revoke_tree,
1879 &trans->revoke_root,
1881 jbd_trans_remove_block_rec(journal,
1882 jbd_buf->block_rec, trans);
1885 ext4_block_set(fs->bdev, &jbd_buf->block);
1886 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1890 checksum = jbd_block_csum(journal->jbd_fs,
1891 jbd_buf->block.data,
1894 if (((struct jbd_bhdr *)jbd_buf->block.data)->magic ==
1895 to_be32(JBD_MAGIC_NUMBER))
1900 desc_iblock = jbd_journal_alloc_block(journal, trans);
1901 rc = jbd_block_get_noread(journal->jbd_fs, &desc_block, desc_iblock);
1905 bhdr = (struct jbd_bhdr *)desc_block.data;
1906 jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
1907 jbd_set32(bhdr, blocktype, JBD_DESCRIPTOR_BLOCK);
1908 jbd_set32(bhdr, sequence, trans->trans_id);
1910 tag_start = (char *)(bhdr + 1);
1911 tag_ptr = tag_start;
1913 tag_tbl_size = journal->block_size -
1914 sizeof(struct jbd_bhdr);
1916 if (jbd_has_csum(&journal->jbd_fs->sb))
1917 tag_tbl_size -= sizeof(struct jbd_block_tail);
1919 if (!trans->start_iblock)
1920 trans->start_iblock = desc_iblock;
1922 ext4_bcache_set_dirty(desc_block.buf);
1923 ext4_bcache_set_flag(desc_block.buf, BC_TMP);
1925 tag_info.block = jbd_buf->block.lb_id;
1926 tag_info.uuid_exist = uuid_exist;
1927 tag_info.is_escape = is_escape;
1928 if (i == trans->data_cnt - 1)
1929 tag_info.last_tag = true;
1931 tag_info.last_tag = false;
1933 tag_info.checksum = checksum;
1936 memcpy(tag_info.uuid, journal->jbd_fs->sb.uuid,
1939 rc = jbd_write_block_tag(journal->jbd_fs,
1944 jbd_meta_csum_set(journal->jbd_fs, bhdr);
1946 rc = jbd_block_set(journal->jbd_fs, &desc_block);
1953 data_iblock = jbd_journal_alloc_block(journal, trans);
1954 rc = jbd_block_get_noread(journal->jbd_fs, &data_block, data_iblock);
1957 ext4_bcache_clear_dirty(desc_block.buf);
1958 jbd_block_set(journal->jbd_fs, &desc_block);
1962 data = data_block.data;
1963 memcpy(data, jbd_buf->block.data,
1964 journal->block_size);
1966 ((struct jbd_bhdr *)data)->magic = 0;
1968 ext4_bcache_set_dirty(data_block.buf);
1969 ext4_bcache_set_flag(data_block.buf, BC_TMP);
1970 rc = jbd_block_set(journal->jbd_fs, &data_block);
1973 ext4_bcache_clear_dirty(desc_block.buf);
1974 jbd_block_set(journal->jbd_fs, &desc_block);
1977 jbd_buf->jbd_lba = data_iblock;
1979 tag_ptr += tag_info.tag_bytes;
1980 tag_tbl_size -= tag_info.tag_bytes;
1984 if (rc == EOK && desc_iblock) {
1985 jbd_meta_csum_set(journal->jbd_fs,
1986 (struct jbd_bhdr *)bhdr);
1987 trans->data_csum = checksum;
1988 rc = jbd_block_set(journal->jbd_fs, &desc_block);
1994 /**@brief Write revoke block for a transaction
1995 * @param journal current journal session
1996 * @param trans transaction
1997 * @return standard error code*/
1999 jbd_journal_prepare_revoke(struct jbd_journal *journal,
2000 struct jbd_trans *trans)
2002 int rc = EOK, i = 0;
2003 struct ext4_block desc_block = EXT4_BLOCK_ZERO();
2004 int32_t tag_tbl_size = 0;
2005 uint32_t desc_iblock = 0;
2006 char *blocks_entry = NULL;
2007 struct jbd_revoke_rec *rec, *tmp;
2008 struct jbd_revoke_header *header = NULL;
2009 int32_t record_len = 4;
2010 struct jbd_bhdr *bhdr = NULL;
2012 if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb,
2013 JBD_FEATURE_INCOMPAT_64BIT))
2016 RB_FOREACH_SAFE(rec, jbd_revoke_tree, &trans->revoke_root,
2020 desc_iblock = jbd_journal_alloc_block(journal, trans);
2021 rc = jbd_block_get_noread(journal->jbd_fs, &desc_block,
2026 bhdr = (struct jbd_bhdr *)desc_block.data;
2027 jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
2028 jbd_set32(bhdr, blocktype, JBD_REVOKE_BLOCK);
2029 jbd_set32(bhdr, sequence, trans->trans_id);
2031 header = (struct jbd_revoke_header *)bhdr;
2032 blocks_entry = (char *)(header + 1);
2033 tag_tbl_size = journal->block_size -
2034 sizeof(struct jbd_revoke_header);
2036 if (jbd_has_csum(&journal->jbd_fs->sb))
2037 tag_tbl_size -= sizeof(struct jbd_block_tail);
2039 if (!trans->start_iblock)
2040 trans->start_iblock = desc_iblock;
2042 ext4_bcache_set_dirty(desc_block.buf);
2043 ext4_bcache_set_flag(desc_block.buf, BC_TMP);
2046 if (tag_tbl_size < record_len) {
2047 jbd_set32(header, count,
2048 journal->block_size - tag_tbl_size);
2049 jbd_meta_csum_set(journal->jbd_fs, bhdr);
2053 rc = jbd_block_set(journal->jbd_fs, &desc_block);
2059 if (record_len == 8) {
2061 (uint64_t *)blocks_entry;
2062 *blocks = to_be64(rec->lba);
2065 (uint32_t *)blocks_entry;
2066 *blocks = to_be32((uint32_t)rec->lba);
2068 blocks_entry += record_len;
2069 tag_tbl_size -= record_len;
2073 if (rc == EOK && desc_iblock) {
2075 jbd_set32(header, count,
2076 journal->block_size - tag_tbl_size);
2078 jbd_meta_csum_set(journal->jbd_fs, bhdr);
2079 rc = jbd_block_set(journal->jbd_fs, &desc_block);
2085 /**@brief Put references of block descriptors in a transaction.
2086 * @param journal current journal session
2087 * @param trans transaction*/
2088 void jbd_journal_cp_trans(struct jbd_journal *journal, struct jbd_trans *trans)
2090 struct jbd_buf *jbd_buf, *tmp;
2091 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
2092 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
2094 struct ext4_block block = jbd_buf->block;
2095 ext4_block_set(fs->bdev, &block);
2099 /**@brief Update the start block of the journal when
2100 * all the contents in a transaction reach the disk.*/
2101 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
2102 struct ext4_buf *buf,
2106 struct jbd_buf *jbd_buf = arg;
2107 struct jbd_trans *trans = jbd_buf->trans;
2108 struct jbd_block_rec *block_rec = jbd_buf->block_rec;
2109 struct jbd_journal *journal = trans->journal;
2110 bool first_in_queue =
2111 trans == TAILQ_FIRST(&journal->cp_queue);
2115 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
2116 TAILQ_REMOVE(&block_rec->dirty_buf_queue,
2120 jbd_trans_finish_callback(journal,
2125 if (block_rec->trans == trans && buf) {
2126 /* Clear the end_write and end_write_arg fields. */
2127 buf->end_write = NULL;
2128 buf->end_write_arg = NULL;
2133 trans->written_cnt++;
2134 if (trans->written_cnt == trans->data_cnt) {
2135 /* If it is the first transaction on checkpoint queue,
2136 * we will shift the start of the journal to the next
2137 * transaction, and remove subsequent written
2138 * transactions from checkpoint queue until we find
2139 * an unwritten one. */
2140 if (first_in_queue) {
2141 journal->start = trans->start_iblock +
2142 trans->alloc_blocks;
2143 wrap(&journal->jbd_fs->sb, journal->start);
2144 journal->trans_id = trans->trans_id + 1;
2145 TAILQ_REMOVE(&journal->cp_queue, trans, trans_node);
2146 jbd_journal_free_trans(journal, trans, false);
2148 jbd_journal_purge_cp_trans(journal, false, false);
2149 jbd_journal_write_sb(journal);
2150 jbd_write_sb(journal->jbd_fs);
2155 /**@brief Commit a transaction to the journal immediately.
2156 * @param journal current journal session
2157 * @param trans transaction
2158 * @return standard error code*/
2159 static int __jbd_journal_commit_trans(struct jbd_journal *journal,
2160 struct jbd_trans *trans)
2163 uint32_t last = journal->last;
2164 struct jbd_revoke_rec *rec, *tmp;
2166 trans->trans_id = journal->alloc_trans_id;
2167 rc = jbd_journal_prepare(journal, trans);
2171 rc = jbd_journal_prepare_revoke(journal, trans);
2175 if (TAILQ_EMPTY(&trans->buf_queue) &&
2176 RB_EMPTY(&trans->revoke_root)) {
2177 /* Since there are no entries in both buffer list
2178 * and revoke entry list, we do not consider trans as
2179 * complete transaction and just return EOK.*/
2180 jbd_journal_free_trans(journal, trans, false);
2184 rc = jbd_trans_write_commit_block(trans);
2188 journal->alloc_trans_id++;
2190 /* Complete the checkpoint of buffers which are revoked. */
2191 RB_FOREACH_SAFE(rec, jbd_revoke_tree, &trans->revoke_root,
2193 struct jbd_block_rec *block_rec =
2194 jbd_trans_block_rec_lookup(journal, rec->lba);
2195 struct jbd_buf *jbd_buf = NULL;
2197 jbd_buf = TAILQ_LAST(&block_rec->dirty_buf_queue,
2200 struct ext4_buf *buf;
2201 struct ext4_block block = EXT4_BLOCK_ZERO();
2203 * We do this to reset the ext4_buf::end_write and
2204 * ext4_buf::end_write_arg fields so that the checkpoint
2205 * callback won't be triggered again.
2207 buf = ext4_bcache_find_get(journal->jbd_fs->bdev->bc,
2209 jbd_buf->block_rec->lba);
2210 jbd_trans_end_write(journal->jbd_fs->bdev->bc,
2215 ext4_block_set(journal->jbd_fs->bdev, &block);
2219 if (TAILQ_EMPTY(&journal->cp_queue)) {
2221 * This transaction is going to be the first object in the
2223 * When the first transaction in checkpoint queue is completely
2224 * written to disk, we shift the tail of the log to right.
2226 if (trans->data_cnt) {
2227 journal->start = trans->start_iblock;
2228 wrap(&journal->jbd_fs->sb, journal->start);
2229 journal->trans_id = trans->trans_id;
2230 jbd_journal_write_sb(journal);
2231 jbd_write_sb(journal->jbd_fs);
2232 TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
2234 jbd_journal_cp_trans(journal, trans);
2236 journal->start = trans->start_iblock +
2237 trans->alloc_blocks;
2238 wrap(&journal->jbd_fs->sb, journal->start);
2239 journal->trans_id = trans->trans_id + 1;
2240 jbd_journal_write_sb(journal);
2241 jbd_journal_free_trans(journal, trans, false);
2244 /* No need to do anything to the JBD superblock. */
2245 TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
2247 if (trans->data_cnt)
2248 jbd_journal_cp_trans(journal, trans);
2251 if (rc != EOK && rc != ENOSPC) {
2252 journal->last = last;
2253 jbd_journal_free_trans(journal, trans, true);
2258 /**@brief Allocate a new transaction
2259 * @param journal current journal session
2260 * @return transaction allocated*/
2262 jbd_journal_new_trans(struct jbd_journal *journal)
2264 struct jbd_trans *trans = NULL;
2265 trans = ext4_calloc(1, sizeof(struct jbd_trans));
2269 /* We will assign a trans_id to this transaction,
2270 * once it has been committed.*/
2271 trans->journal = journal;
2272 trans->data_csum = EXT4_CRC32_INIT;
2274 TAILQ_INIT(&trans->buf_queue);
2278 /**@brief Commit a transaction to the journal immediately.
2279 * @param journal current journal session
2280 * @param trans transaction
2281 * @return standard error code*/
2282 int jbd_journal_commit_trans(struct jbd_journal *journal,
2283 struct jbd_trans *trans)
2286 r = __jbd_journal_commit_trans(journal, trans);