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 uint32_t orig_checksum_type = header->chksum_type,
271 orig_checksum_size = header->chksum_size,
272 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,
462 memset(jbd_fs, 0, sizeof(struct jbd_fs));
465 rc = jbd_sb_read(jbd_fs, &jbd_fs->sb);
467 memset(jbd_fs, 0, sizeof(struct jbd_fs));
468 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
471 if (!jbd_verify_sb(&jbd_fs->sb)) {
472 memset(jbd_fs, 0, sizeof(struct jbd_fs));
473 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
478 jbd_fs->bdev = fs->bdev;
483 /**@brief Put reference of jbd filesystem.
484 * @param jbd_fs jbd filesystem
485 * @return standard error code*/
486 int jbd_put_fs(struct jbd_fs *jbd_fs)
489 rc = jbd_write_sb(jbd_fs);
491 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
495 /**@brief Data block lookup helper.
496 * @param jbd_fs jbd filesystem
497 * @param iblock block index
498 * @param fblock logical block address
499 * @return standard error code*/
500 int jbd_inode_bmap(struct jbd_fs *jbd_fs,
502 ext4_fsblk_t *fblock)
504 int rc = ext4_fs_get_inode_dblk_idx(
512 /**@brief jbd block get function (through cache).
513 * @param jbd_fs jbd filesystem
514 * @param block block descriptor
515 * @param fblock jbd logical block address
516 * @return standard error code*/
517 static int jbd_block_get(struct jbd_fs *jbd_fs,
518 struct ext4_block *block,
521 /* TODO: journal device. */
523 struct ext4_blockdev *bdev = jbd_fs->bdev;
524 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
526 /* Lookup the logical block address of
528 rc = jbd_inode_bmap(jbd_fs, iblock,
533 rc = ext4_block_get(bdev, block, fblock);
535 /* If succeeded, mark buffer as BC_FLUSH to indicate
536 * that data should be written to disk immediately.*/
538 ext4_bcache_set_flag(block->buf, BC_FLUSH);
539 /* As we don't want to occupy too much space
540 * in block cache, we set this buffer BC_TMP.*/
541 ext4_bcache_set_flag(block->buf, BC_TMP);
547 /**@brief jbd block get function (through cache, don't read).
548 * @param jbd_fs jbd filesystem
549 * @param block block descriptor
550 * @param fblock jbd logical block address
551 * @return standard error code*/
552 static int jbd_block_get_noread(struct jbd_fs *jbd_fs,
553 struct ext4_block *block,
556 /* TODO: journal device. */
558 struct ext4_blockdev *bdev = jbd_fs->bdev;
559 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
560 rc = jbd_inode_bmap(jbd_fs, iblock,
565 rc = ext4_block_get_noread(bdev, block, fblock);
567 ext4_bcache_set_flag(block->buf, BC_FLUSH);
572 /**@brief jbd block set procedure (through cache).
573 * @param jbd_fs jbd filesystem
574 * @param block block descriptor
575 * @return standard error code*/
576 static int jbd_block_set(struct jbd_fs *jbd_fs,
577 struct ext4_block *block)
579 struct ext4_blockdev *bdev = jbd_fs->bdev;
580 return ext4_block_set(bdev, block);
583 /**@brief helper functions to calculate
584 * block tag size, not including UUID part.
585 * @param jbd_fs jbd filesystem
586 * @return tag size in bytes*/
587 static int jbd_tag_bytes(struct jbd_fs *jbd_fs)
591 /* It is very easy to deal with the case which
592 * JBD_FEATURE_INCOMPAT_CSUM_V3 is enabled.*/
593 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
594 JBD_FEATURE_INCOMPAT_CSUM_V3))
595 return sizeof(struct jbd_block_tag3);
597 size = sizeof(struct jbd_block_tag);
599 /* If JBD_FEATURE_INCOMPAT_CSUM_V2 is enabled,
600 * add 2 bytes to size.*/
601 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
602 JBD_FEATURE_INCOMPAT_CSUM_V2))
603 size += sizeof(uint16_t);
605 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
606 JBD_FEATURE_INCOMPAT_64BIT))
609 /* If block number is 4 bytes in size,
610 * minus 4 bytes from size */
611 return size - sizeof(uint32_t);
614 /**@brief Tag information. */
616 /**@brief Tag size in bytes, including UUID part.*/
619 /**@brief block number stored in this tag.*/
622 /**@brief Is the first 4 bytes of block equals to
623 * JBD_MAGIC_NUMBER? */
626 /**@brief whether UUID part exists or not.*/
629 /**@brief UUID content if UUID part exists.*/
630 uint8_t uuid[UUID_SIZE];
632 /**@brief Is this the last tag? */
635 /**@brief crc32c checksum. */
639 /**@brief Extract information from a block tag.
640 * @param __tag pointer to the block tag
641 * @param tag_bytes block tag size of this jbd filesystem
642 * @param remaining size in buffer containing the block tag
643 * @param tag_info information of this tag.
644 * @return EOK when succeed, otherwise return EINVAL.*/
646 jbd_extract_block_tag(struct jbd_fs *jbd_fs,
649 int32_t remain_buf_size,
650 struct tag_info *tag_info)
653 tag_info->tag_bytes = tag_bytes;
654 tag_info->uuid_exist = false;
655 tag_info->last_tag = false;
656 tag_info->is_escape = false;
658 /* See whether it is possible to hold a valid block tag.*/
659 if (remain_buf_size - tag_bytes < 0)
662 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
663 JBD_FEATURE_INCOMPAT_CSUM_V3)) {
664 struct jbd_block_tag3 *tag = __tag;
665 tag_info->block = jbd_get32(tag, blocknr);
666 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
667 JBD_FEATURE_INCOMPAT_64BIT))
669 (uint64_t)jbd_get32(tag, blocknr_high) << 32;
671 if (jbd_get32(tag, flags) & JBD_FLAG_ESCAPE)
672 tag_info->is_escape = true;
674 if (!(jbd_get32(tag, flags) & JBD_FLAG_SAME_UUID)) {
675 /* See whether it is possible to hold UUID part.*/
676 if (remain_buf_size - tag_bytes < UUID_SIZE)
679 uuid_start = (char *)tag + tag_bytes;
680 tag_info->uuid_exist = true;
681 tag_info->tag_bytes += UUID_SIZE;
682 memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
685 if (jbd_get32(tag, flags) & JBD_FLAG_LAST_TAG)
686 tag_info->last_tag = true;
689 struct jbd_block_tag *tag = __tag;
690 tag_info->block = jbd_get32(tag, blocknr);
691 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
692 JBD_FEATURE_INCOMPAT_64BIT))
694 (uint64_t)jbd_get32(tag, blocknr_high) << 32;
696 if (jbd_get16(tag, flags) & JBD_FLAG_ESCAPE)
697 tag_info->is_escape = true;
699 if (!(jbd_get16(tag, flags) & JBD_FLAG_SAME_UUID)) {
700 /* See whether it is possible to hold UUID part.*/
701 if (remain_buf_size - tag_bytes < UUID_SIZE)
704 uuid_start = (char *)tag + tag_bytes;
705 tag_info->uuid_exist = true;
706 tag_info->tag_bytes += UUID_SIZE;
707 memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
710 if (jbd_get16(tag, flags) & JBD_FLAG_LAST_TAG)
711 tag_info->last_tag = true;
717 /**@brief Write information to a block tag.
718 * @param __tag pointer to the block tag
719 * @param remaining size in buffer containing the block tag
720 * @param tag_info information of this tag.
721 * @return EOK when succeed, otherwise return EINVAL.*/
723 jbd_write_block_tag(struct jbd_fs *jbd_fs,
725 int32_t remain_buf_size,
726 struct tag_info *tag_info)
729 int tag_bytes = jbd_tag_bytes(jbd_fs);
731 tag_info->tag_bytes = tag_bytes;
733 /* See whether it is possible to hold a valid block tag.*/
734 if (remain_buf_size - tag_bytes < 0)
737 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
738 JBD_FEATURE_INCOMPAT_CSUM_V3)) {
739 struct jbd_block_tag3 *tag = __tag;
740 memset(tag, 0, sizeof(struct jbd_block_tag3));
741 jbd_set32(tag, blocknr, (uint32_t)tag_info->block);
742 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
743 JBD_FEATURE_INCOMPAT_64BIT))
744 jbd_set32(tag, blocknr_high, tag_info->block >> 32);
746 if (tag_info->uuid_exist) {
747 /* See whether it is possible to hold UUID part.*/
748 if (remain_buf_size - tag_bytes < UUID_SIZE)
751 uuid_start = (char *)tag + tag_bytes;
752 tag_info->tag_bytes += UUID_SIZE;
753 memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
755 jbd_set32(tag, flags,
756 jbd_get32(tag, flags) | JBD_FLAG_SAME_UUID);
758 jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum);
760 if (tag_info->last_tag)
761 jbd_set32(tag, flags,
762 jbd_get32(tag, flags) | JBD_FLAG_LAST_TAG);
764 if (tag_info->is_escape)
765 jbd_set32(tag, flags,
766 jbd_get32(tag, flags) | JBD_FLAG_ESCAPE);
769 struct jbd_block_tag *tag = __tag;
770 memset(tag, 0, sizeof(struct jbd_block_tag));
771 jbd_set32(tag, blocknr, (uint32_t)tag_info->block);
772 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
773 JBD_FEATURE_INCOMPAT_64BIT))
774 jbd_set32(tag, blocknr_high, tag_info->block >> 32);
776 if (tag_info->uuid_exist) {
777 /* See whether it is possible to hold UUID part.*/
778 if (remain_buf_size - tag_bytes < UUID_SIZE)
781 uuid_start = (char *)tag + tag_bytes;
782 tag_info->tag_bytes += UUID_SIZE;
783 memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
785 jbd_set16(tag, flags,
786 jbd_get16(tag, flags) | JBD_FLAG_SAME_UUID);
788 jbd_block_tag_csum_set(jbd_fs, __tag, tag_info->checksum);
790 if (tag_info->last_tag)
791 jbd_set16(tag, flags,
792 jbd_get16(tag, flags) | JBD_FLAG_LAST_TAG);
795 if (tag_info->is_escape)
796 jbd_set16(tag, flags,
797 jbd_get16(tag, flags) | JBD_FLAG_ESCAPE);
803 /**@brief Iterate all block tags in a block.
804 * @param jbd_fs jbd filesystem
805 * @param __tag_start pointer to the block
806 * @param tag_tbl_size size of the block
807 * @param func callback routine to indicate that
808 * a block tag is found
809 * @param arg additional argument to be passed to func */
811 jbd_iterate_block_table(struct jbd_fs *jbd_fs,
813 int32_t tag_tbl_size,
814 void (*func)(struct jbd_fs * jbd_fs,
815 struct tag_info *tag_info,
819 char *tag_start, *tag_ptr;
820 int tag_bytes = jbd_tag_bytes(jbd_fs);
821 tag_start = __tag_start;
824 /* Cut off the size of block tail storing checksum. */
825 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
826 JBD_FEATURE_INCOMPAT_CSUM_V2) ||
827 JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
828 JBD_FEATURE_INCOMPAT_CSUM_V3))
829 tag_tbl_size -= sizeof(struct jbd_block_tail);
831 while (tag_tbl_size) {
832 struct tag_info tag_info;
833 int rc = jbd_extract_block_tag(jbd_fs,
842 func(jbd_fs, &tag_info, arg);
844 /* Stop the iteration when we reach the last tag. */
845 if (tag_info.last_tag)
848 tag_ptr += tag_info.tag_bytes;
849 tag_tbl_size -= tag_info.tag_bytes;
853 static void jbd_display_block_tags(struct jbd_fs *jbd_fs,
854 struct tag_info *tag_info,
857 uint32_t *iblock = arg;
858 ext4_dbg(DEBUG_JBD, "Block in block_tag: %" PRIu64 "\n", tag_info->block);
860 wrap(&jbd_fs->sb, *iblock);
865 static struct revoke_entry *
866 jbd_revoke_entry_lookup(struct recover_info *info, ext4_fsblk_t block)
868 struct revoke_entry tmp = {
872 return RB_FIND(jbd_revoke, &info->revoke_root, &tmp);
875 /**@brief Replay a block in a transaction.
876 * @param jbd_fs jbd filesystem
877 * @param tag_info tag_info of the logged block.*/
878 static void jbd_replay_block_tags(struct jbd_fs *jbd_fs,
879 struct tag_info *tag_info,
883 struct replay_arg *arg = __arg;
884 struct recover_info *info = arg->info;
885 uint32_t *this_block = arg->this_block;
886 struct revoke_entry *revoke_entry;
887 struct ext4_block journal_block, ext4_block;
888 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
891 wrap(&jbd_fs->sb, *this_block);
893 /* We replay this block only if the current transaction id
894 * is equal or greater than that in revoke entry.*/
895 revoke_entry = jbd_revoke_entry_lookup(info, tag_info->block);
897 trans_id_diff(arg->this_trans_id, revoke_entry->trans_id) <= 0)
901 "Replaying block in block_tag: %" PRIu64 "\n",
904 r = jbd_block_get(jbd_fs, &journal_block, *this_block);
908 /* We need special treatment for ext4 superblock. */
909 if (tag_info->block) {
910 r = ext4_block_get_noread(fs->bdev, &ext4_block, tag_info->block);
912 jbd_block_set(jbd_fs, &journal_block);
916 memcpy(ext4_block.data,
918 jbd_get32(&jbd_fs->sb, blocksize));
920 if (tag_info->is_escape)
921 ((struct jbd_bhdr *)ext4_block.data)->magic =
922 to_be32(JBD_MAGIC_NUMBER);
924 ext4_bcache_set_dirty(ext4_block.buf);
925 ext4_block_set(fs->bdev, &ext4_block);
927 uint16_t mount_count, state;
928 mount_count = ext4_get16(&fs->sb, mount_count);
929 state = ext4_get16(&fs->sb, state);
932 journal_block.data + EXT4_SUPERBLOCK_OFFSET,
933 EXT4_SUPERBLOCK_SIZE);
935 /* Mark system as mounted */
936 ext4_set16(&fs->sb, state, state);
937 r = ext4_sb_write(fs->bdev, &fs->sb);
941 /*Update mount count*/
942 ext4_set16(&fs->sb, mount_count, mount_count);
945 jbd_block_set(jbd_fs, &journal_block);
950 /**@brief Add block address to revoke tree, along with
951 * its transaction id.
952 * @param info journal replay info
953 * @param block block address to be replayed.*/
954 static void jbd_add_revoke_block_tags(struct recover_info *info,
957 struct revoke_entry *revoke_entry;
959 ext4_dbg(DEBUG_JBD, "Add block %" PRIu64 " to revoke tree\n", block);
960 /* If the revoke entry with respect to the block address
961 * exists already, update its transaction id.*/
962 revoke_entry = jbd_revoke_entry_lookup(info, block);
964 revoke_entry->trans_id = info->this_trans_id;
968 revoke_entry = jbd_alloc_revoke_entry();
969 ext4_assert(revoke_entry);
970 revoke_entry->block = block;
971 revoke_entry->trans_id = info->this_trans_id;
972 RB_INSERT(jbd_revoke, &info->revoke_root, revoke_entry);
977 static void jbd_destroy_revoke_tree(struct recover_info *info)
979 while (!RB_EMPTY(&info->revoke_root)) {
980 struct revoke_entry *revoke_entry =
981 RB_MIN(jbd_revoke, &info->revoke_root);
982 ext4_assert(revoke_entry);
983 RB_REMOVE(jbd_revoke, &info->revoke_root, revoke_entry);
984 jbd_free_revoke_entry(revoke_entry);
989 #define ACTION_SCAN 0
990 #define ACTION_REVOKE 1
991 #define ACTION_RECOVER 2
993 /**@brief Add entries in a revoke block to revoke tree.
994 * @param jbd_fs jbd filesystem
995 * @param header revoke block header
996 * @param recover_info journal replay info*/
997 static void jbd_build_revoke_tree(struct jbd_fs *jbd_fs,
998 struct jbd_bhdr *header,
999 struct recover_info *info)
1002 struct jbd_revoke_header *revoke_hdr =
1003 (struct jbd_revoke_header *)header;
1004 uint32_t i, nr_entries, record_len = 4;
1006 /* If we are working on a 64bit jbd filesystem, */
1007 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
1008 JBD_FEATURE_INCOMPAT_64BIT))
1011 nr_entries = (jbd_get32(revoke_hdr, count) -
1012 sizeof(struct jbd_revoke_header)) /
1015 blocks_entry = (char *)(revoke_hdr + 1);
1017 for (i = 0;i < nr_entries;i++) {
1018 if (record_len == 8) {
1020 (uint64_t *)blocks_entry;
1021 jbd_add_revoke_block_tags(info, to_be64(*blocks));
1024 (uint32_t *)blocks_entry;
1025 jbd_add_revoke_block_tags(info, to_be32(*blocks));
1027 blocks_entry += record_len;
1031 static void jbd_debug_descriptor_block(struct jbd_fs *jbd_fs,
1032 struct jbd_bhdr *header,
1035 jbd_iterate_block_table(jbd_fs,
1037 jbd_get32(&jbd_fs->sb, blocksize) -
1038 sizeof(struct jbd_bhdr),
1039 jbd_display_block_tags,
1043 static void jbd_replay_descriptor_block(struct jbd_fs *jbd_fs,
1044 struct jbd_bhdr *header,
1045 struct replay_arg *arg)
1047 jbd_iterate_block_table(jbd_fs,
1049 jbd_get32(&jbd_fs->sb, blocksize) -
1050 sizeof(struct jbd_bhdr),
1051 jbd_replay_block_tags,
1055 /**@brief The core routine of journal replay.
1056 * @param jbd_fs jbd filesystem
1057 * @param recover_info journal replay info
1058 * @param action action needed to be taken
1059 * @return standard error code*/
1060 static int jbd_iterate_log(struct jbd_fs *jbd_fs,
1061 struct recover_info *info,
1065 bool log_end = false;
1066 struct jbd_sb *sb = &jbd_fs->sb;
1067 uint32_t start_trans_id, this_trans_id;
1068 uint32_t start_block, this_block;
1070 /* We start iterating valid blocks in the whole journal.*/
1071 start_trans_id = this_trans_id = jbd_get32(sb, sequence);
1072 start_block = this_block = jbd_get32(sb, start);
1073 if (action == ACTION_SCAN)
1074 info->trans_cnt = 0;
1075 else if (!info->trans_cnt)
1078 ext4_dbg(DEBUG_JBD, "Start of journal at trans id: %" PRIu32 "\n",
1082 struct ext4_block block;
1083 struct jbd_bhdr *header;
1084 /* If we are not scanning for the last
1085 * valid transaction in the journal,
1086 * we will stop when we reach the end of
1088 if (action != ACTION_SCAN)
1089 if (trans_id_diff(this_trans_id, info->last_trans_id) > 0) {
1094 r = jbd_block_get(jbd_fs, &block, this_block);
1098 header = (struct jbd_bhdr *)block.data;
1099 /* This block does not have a valid magic number,
1100 * so we have reached the end of the journal.*/
1101 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER) {
1102 jbd_block_set(jbd_fs, &block);
1107 /* If the transaction id we found is not expected,
1108 * we may have reached the end of the journal.
1110 * If we are not scanning the journal, something
1111 * bad might have taken place. :-( */
1112 if (jbd_get32(header, sequence) != this_trans_id) {
1113 if (action != ACTION_SCAN)
1116 jbd_block_set(jbd_fs, &block);
1121 switch (jbd_get32(header, blocktype)) {
1122 case JBD_DESCRIPTOR_BLOCK:
1123 if (!jbd_verify_meta_csum(jbd_fs, header)) {
1125 DBG_WARN "Descriptor block checksum failed."
1126 "Journal block: %" PRIu32"\n",
1131 ext4_dbg(DEBUG_JBD, "Descriptor block: %" PRIu32", "
1132 "trans_id: %" PRIu32"\n",
1133 this_block, this_trans_id);
1134 if (action == ACTION_RECOVER) {
1135 struct replay_arg replay_arg;
1136 replay_arg.info = info;
1137 replay_arg.this_block = &this_block;
1138 replay_arg.this_trans_id = this_trans_id;
1140 jbd_replay_descriptor_block(jbd_fs,
1141 header, &replay_arg);
1143 jbd_debug_descriptor_block(jbd_fs,
1144 header, &this_block);
1147 case JBD_COMMIT_BLOCK:
1148 if (!jbd_verify_commit_csum(jbd_fs,
1149 (struct jbd_commit_header *)header)) {
1151 DBG_WARN "Commit block checksum failed."
1152 "Journal block: %" PRIu32"\n",
1157 ext4_dbg(DEBUG_JBD, "Commit block: %" PRIu32", "
1158 "trans_id: %" PRIu32"\n",
1159 this_block, this_trans_id);
1161 * This is the end of a transaction,
1162 * we may now proceed to the next transaction.
1165 if (action == ACTION_SCAN)
1168 case JBD_REVOKE_BLOCK:
1169 if (!jbd_verify_meta_csum(jbd_fs, header)) {
1171 DBG_WARN "Revoke block checksum failed."
1172 "Journal block: %" PRIu32"\n",
1177 ext4_dbg(DEBUG_JBD, "Revoke block: %" PRIu32", "
1178 "trans_id: %" PRIu32"\n",
1179 this_block, this_trans_id);
1180 if (action == ACTION_REVOKE) {
1181 info->this_trans_id = this_trans_id;
1182 jbd_build_revoke_tree(jbd_fs,
1190 jbd_block_set(jbd_fs, &block);
1192 wrap(sb, this_block);
1193 if (this_block == start_block)
1197 ext4_dbg(DEBUG_JBD, "End of journal.\n");
1198 if (r == EOK && action == ACTION_SCAN) {
1199 /* We have finished scanning the journal. */
1200 info->start_trans_id = start_trans_id;
1201 if (trans_id_diff(this_trans_id, start_trans_id) > 0)
1202 info->last_trans_id = this_trans_id - 1;
1204 info->last_trans_id = this_trans_id;
1210 /**@brief Replay journal.
1211 * @param jbd_fs jbd filesystem
1212 * @return standard error code*/
1213 int jbd_recover(struct jbd_fs *jbd_fs)
1216 struct recover_info info;
1217 struct jbd_sb *sb = &jbd_fs->sb;
1221 RB_INIT(&info.revoke_root);
1223 r = jbd_iterate_log(jbd_fs, &info, ACTION_SCAN);
1227 r = jbd_iterate_log(jbd_fs, &info, ACTION_REVOKE);
1231 r = jbd_iterate_log(jbd_fs, &info, ACTION_RECOVER);
1233 /* If we successfully replay the journal,
1234 * clear EXT4_FINCOM_RECOVER flag on the
1235 * ext4 superblock, and set the start of
1237 uint32_t features_incompatible =
1238 ext4_get32(&jbd_fs->inode_ref.fs->sb,
1239 features_incompatible);
1240 jbd_set32(&jbd_fs->sb, start, 0);
1241 jbd_set32(&jbd_fs->sb, sequence, info.last_trans_id);
1242 features_incompatible &= ~EXT4_FINCOM_RECOVER;
1243 ext4_set32(&jbd_fs->inode_ref.fs->sb,
1244 features_incompatible,
1245 features_incompatible);
1246 jbd_fs->dirty = true;
1247 r = ext4_sb_write(jbd_fs->bdev,
1248 &jbd_fs->inode_ref.fs->sb);
1250 jbd_destroy_revoke_tree(&info);
1254 static void jbd_journal_write_sb(struct jbd_journal *journal)
1256 struct jbd_fs *jbd_fs = journal->jbd_fs;
1257 jbd_set32(&jbd_fs->sb, start, journal->start);
1258 jbd_set32(&jbd_fs->sb, sequence, journal->trans_id);
1259 jbd_fs->dirty = true;
1262 /**@brief Start accessing the journal.
1263 * @param jbd_fs jbd filesystem
1264 * @param journal current journal session
1265 * @return standard error code*/
1266 int jbd_journal_start(struct jbd_fs *jbd_fs,
1267 struct jbd_journal *journal)
1270 uint32_t features_incompatible =
1271 ext4_get32(&jbd_fs->inode_ref.fs->sb,
1272 features_incompatible);
1273 features_incompatible |= EXT4_FINCOM_RECOVER;
1274 ext4_set32(&jbd_fs->inode_ref.fs->sb,
1275 features_incompatible,
1276 features_incompatible);
1277 r = ext4_sb_write(jbd_fs->bdev,
1278 &jbd_fs->inode_ref.fs->sb);
1282 journal->first = jbd_get32(&jbd_fs->sb, first);
1283 journal->start = journal->first;
1284 journal->last = journal->first;
1286 * To invalidate any stale records we need to start from
1287 * the checkpoint transaction ID of the previous journalling session
1290 journal->trans_id = jbd_get32(&jbd_fs->sb, sequence) + 1;
1291 journal->alloc_trans_id = journal->trans_id;
1293 journal->block_size = jbd_get32(&jbd_fs->sb, blocksize);
1295 TAILQ_INIT(&journal->cp_queue);
1296 RB_INIT(&journal->block_rec_root);
1297 journal->jbd_fs = jbd_fs;
1298 jbd_journal_write_sb(journal);
1299 r = jbd_write_sb(jbd_fs);
1303 jbd_fs->bdev->journal = journal;
1307 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
1308 struct ext4_buf *buf __unused,
1313 * This routine is only suitable to committed transactions. */
1314 static void jbd_journal_flush_trans(struct jbd_trans *trans)
1316 struct jbd_buf *jbd_buf, *tmp;
1317 struct jbd_journal *journal = trans->journal;
1318 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1319 void *tmp_data = ext4_malloc(journal->block_size);
1320 ext4_assert(tmp_data);
1322 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
1324 struct ext4_buf *buf;
1325 struct ext4_block block;
1326 /* The buffer is not yet flushed. */
1327 buf = ext4_bcache_find_get(fs->bdev->bc, &block,
1328 jbd_buf->block_rec->lba);
1329 if (!(buf && ext4_bcache_test_flag(buf, BC_UPTODATE) &&
1330 jbd_buf->block_rec->trans == trans)) {
1332 struct ext4_block jbd_block = EXT4_BLOCK_ZERO();
1333 ext4_assert(jbd_block_get(journal->jbd_fs,
1335 jbd_buf->jbd_lba) == EOK);
1336 memcpy(tmp_data, jbd_block.data,
1337 journal->block_size);
1338 ext4_block_set(fs->bdev, &jbd_block);
1339 r = ext4_blocks_set_direct(fs->bdev, tmp_data,
1340 jbd_buf->block_rec->lba, 1);
1341 jbd_trans_end_write(fs->bdev->bc, buf, r, jbd_buf);
1343 ext4_block_flush_buf(fs->bdev, buf);
1346 ext4_block_set(fs->bdev, &block);
1349 ext4_free(tmp_data);
1353 jbd_journal_skip_pure_revoke(struct jbd_journal *journal,
1354 struct jbd_trans *trans)
1356 journal->start = trans->start_iblock +
1357 trans->alloc_blocks;
1358 wrap(&journal->jbd_fs->sb, journal->start);
1359 journal->trans_id = trans->trans_id + 1;
1360 jbd_journal_free_trans(journal,
1362 jbd_journal_write_sb(journal);
1366 jbd_journal_purge_cp_trans(struct jbd_journal *journal,
1370 struct jbd_trans *trans;
1371 while ((trans = TAILQ_FIRST(&journal->cp_queue))) {
1372 if (!trans->data_cnt) {
1373 TAILQ_REMOVE(&journal->cp_queue,
1376 jbd_journal_skip_pure_revoke(journal, trans);
1378 if (trans->data_cnt ==
1379 trans->written_cnt) {
1381 trans->start_iblock +
1382 trans->alloc_blocks;
1383 wrap(&journal->jbd_fs->sb,
1386 trans->trans_id + 1;
1387 TAILQ_REMOVE(&journal->cp_queue,
1390 jbd_journal_free_trans(journal,
1393 jbd_journal_write_sb(journal);
1394 } else if (!flush) {
1396 trans->start_iblock;
1397 wrap(&journal->jbd_fs->sb,
1401 jbd_journal_write_sb(journal);
1404 jbd_journal_flush_trans(trans);
1411 /**@brief Stop accessing the journal.
1412 * @param journal current journal session
1413 * @return standard error code*/
1414 int jbd_journal_stop(struct jbd_journal *journal)
1417 struct jbd_fs *jbd_fs = journal->jbd_fs;
1418 uint32_t features_incompatible;
1420 /* Make sure that journalled content have reached
1422 jbd_journal_purge_cp_trans(journal, true, false);
1424 /* There should be no block record in this journal
1426 if (!RB_EMPTY(&journal->block_rec_root))
1428 DBG_WARN "There are still block records "
1429 "in this journal session!\n");
1431 features_incompatible =
1432 ext4_get32(&jbd_fs->inode_ref.fs->sb,
1433 features_incompatible);
1434 features_incompatible &= ~EXT4_FINCOM_RECOVER;
1435 ext4_set32(&jbd_fs->inode_ref.fs->sb,
1436 features_incompatible,
1437 features_incompatible);
1438 r = ext4_sb_write(jbd_fs->bdev,
1439 &jbd_fs->inode_ref.fs->sb);
1444 journal->trans_id = 0;
1445 jbd_journal_write_sb(journal);
1446 return jbd_write_sb(journal->jbd_fs);
1449 /**@brief Allocate a block in the journal.
1450 * @param journal current journal session
1451 * @param trans transaction
1452 * @return allocated block address*/
1453 static uint32_t jbd_journal_alloc_block(struct jbd_journal *journal,
1454 struct jbd_trans *trans)
1456 uint32_t start_block;
1458 start_block = journal->last++;
1459 trans->alloc_blocks++;
1460 wrap(&journal->jbd_fs->sb, journal->last);
1462 /* If there is no space left, flush just one journalled
1464 if (journal->last == journal->start) {
1465 jbd_journal_purge_cp_trans(journal, true, true);
1466 ext4_assert(journal->last != journal->start);
1472 static struct jbd_block_rec *
1473 jbd_trans_block_rec_lookup(struct jbd_journal *journal,
1476 struct jbd_block_rec tmp = {
1480 return RB_FIND(jbd_block,
1481 &journal->block_rec_root,
1486 jbd_trans_change_ownership(struct jbd_block_rec *block_rec,
1487 struct jbd_trans *new_trans)
1489 LIST_REMOVE(block_rec, tbrec_node);
1491 /* Now this block record belongs to this transaction. */
1492 LIST_INSERT_HEAD(&new_trans->tbrec_list, block_rec, tbrec_node);
1494 block_rec->trans = new_trans;
1497 static inline struct jbd_block_rec *
1498 jbd_trans_insert_block_rec(struct jbd_trans *trans,
1501 struct jbd_block_rec *block_rec;
1502 block_rec = jbd_trans_block_rec_lookup(trans->journal, lba);
1504 jbd_trans_change_ownership(block_rec, trans);
1507 block_rec = ext4_calloc(1, sizeof(struct jbd_block_rec));
1511 block_rec->lba = lba;
1512 block_rec->trans = trans;
1513 TAILQ_INIT(&block_rec->dirty_buf_queue);
1514 LIST_INSERT_HEAD(&trans->tbrec_list, block_rec, tbrec_node);
1515 RB_INSERT(jbd_block, &trans->journal->block_rec_root, block_rec);
1520 * This routine will do the dirty works.
1523 jbd_trans_finish_callback(struct jbd_journal *journal,
1524 const struct jbd_trans *trans,
1525 struct jbd_block_rec *block_rec,
1529 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1530 if (block_rec->trans != trans)
1534 struct jbd_buf *jbd_buf, *tmp;
1535 TAILQ_FOREACH_SAFE(jbd_buf,
1536 &block_rec->dirty_buf_queue,
1539 jbd_trans_end_write(fs->bdev->bc,
1546 * We have to roll back data if the block is going to be
1549 struct jbd_buf *jbd_buf;
1550 struct ext4_block jbd_block = EXT4_BLOCK_ZERO(),
1551 block = EXT4_BLOCK_ZERO();
1552 jbd_buf = TAILQ_LAST(&block_rec->dirty_buf_queue,
1556 ext4_assert(ext4_block_get_noread(fs->bdev,
1558 block_rec->lba) == EOK);
1559 ext4_assert(jbd_block_get(journal->jbd_fs,
1561 jbd_buf->jbd_lba) == EOK);
1562 memcpy(block.data, jbd_block.data,
1563 journal->block_size);
1565 jbd_trans_change_ownership(block_rec,
1568 block.buf->end_write = jbd_trans_end_write;
1569 block.buf->end_write_arg = jbd_buf;
1571 ext4_bcache_set_flag(jbd_block.buf, BC_TMP);
1572 ext4_bcache_set_dirty(block.buf);
1574 ext4_block_set(fs->bdev, &jbd_block);
1575 ext4_block_set(fs->bdev, &block);
1578 /* The revoked buffer is yet written. */
1579 jbd_trans_change_ownership(block_rec,
1587 jbd_trans_remove_block_rec(struct jbd_journal *journal,
1588 struct jbd_block_rec *block_rec,
1589 struct jbd_trans *trans)
1591 /* If this block record doesn't belong to this transaction,
1593 if (block_rec->trans == trans) {
1594 LIST_REMOVE(block_rec, tbrec_node);
1595 RB_REMOVE(jbd_block,
1596 &journal->block_rec_root,
1598 ext4_free(block_rec);
1602 /**@brief Add block to a transaction and mark it dirty.
1603 * @param trans transaction
1604 * @param block block descriptor
1605 * @return standard error code*/
1606 int jbd_trans_set_block_dirty(struct jbd_trans *trans,
1607 struct ext4_block *block)
1609 struct jbd_buf *jbd_buf;
1610 struct jbd_revoke_rec *rec, tmp_rec = {
1613 struct jbd_block_rec *block_rec;
1615 if (block->buf->end_write == jbd_trans_end_write) {
1616 jbd_buf = block->buf->end_write_arg;
1617 if (jbd_buf && jbd_buf->trans == trans)
1620 jbd_buf = ext4_calloc(1, sizeof(struct jbd_buf));
1624 if ((block_rec = jbd_trans_insert_block_rec(trans,
1625 block->lb_id)) == NULL) {
1630 TAILQ_INSERT_TAIL(&block_rec->dirty_buf_queue,
1634 jbd_buf->block_rec = block_rec;
1635 jbd_buf->trans = trans;
1636 jbd_buf->block = *block;
1637 ext4_bcache_inc_ref(block->buf);
1639 /* If the content reach the disk, notify us
1640 * so that we may do a checkpoint. */
1641 block->buf->end_write = jbd_trans_end_write;
1642 block->buf->end_write_arg = jbd_buf;
1645 TAILQ_INSERT_HEAD(&trans->buf_queue, jbd_buf, buf_node);
1647 ext4_bcache_set_dirty(block->buf);
1648 rec = RB_FIND(jbd_revoke_tree,
1649 &trans->revoke_root,
1652 RB_REMOVE(jbd_revoke_tree, &trans->revoke_root,
1660 /**@brief Add block to be revoked to a transaction
1661 * @param trans transaction
1662 * @param lba logical block address
1663 * @return standard error code*/
1664 int jbd_trans_revoke_block(struct jbd_trans *trans,
1667 struct jbd_revoke_rec tmp_rec = {
1670 rec = RB_FIND(jbd_revoke_tree,
1671 &trans->revoke_root,
1676 rec = ext4_calloc(1, sizeof(struct jbd_revoke_rec));
1681 RB_INSERT(jbd_revoke_tree, &trans->revoke_root, rec);
1685 /**@brief Try to add block to be revoked to a transaction.
1686 * If @lba still remains in an transaction on checkpoint
1687 * queue, add @lba as a revoked block to the transaction.
1688 * @param trans transaction
1689 * @param lba logical block address
1690 * @return standard error code*/
1691 int jbd_trans_try_revoke_block(struct jbd_trans *trans,
1694 struct jbd_journal *journal = trans->journal;
1695 struct jbd_block_rec *block_rec =
1696 jbd_trans_block_rec_lookup(journal, lba);
1699 if (block_rec->trans == trans) {
1700 struct jbd_buf *jbd_buf =
1701 TAILQ_LAST(&block_rec->dirty_buf_queue,
1703 /* If there are still unwritten buffers. */
1704 if (TAILQ_FIRST(&block_rec->dirty_buf_queue) !=
1706 jbd_trans_revoke_block(trans, lba);
1709 jbd_trans_revoke_block(trans, lba);
1715 /**@brief Free a transaction
1716 * @param journal current journal session
1717 * @param trans transaction
1718 * @param abort discard all the modifications on the block?
1719 * @return standard error code*/
1720 void jbd_journal_free_trans(struct jbd_journal *journal,
1721 struct jbd_trans *trans,
1724 struct jbd_buf *jbd_buf, *tmp;
1725 struct jbd_revoke_rec *rec, *tmp2;
1726 struct jbd_block_rec *block_rec, *tmp3;
1727 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1728 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
1730 block_rec = jbd_buf->block_rec;
1732 jbd_buf->block.buf->end_write = NULL;
1733 jbd_buf->block.buf->end_write_arg = NULL;
1734 ext4_bcache_clear_dirty(jbd_buf->block.buf);
1735 ext4_block_set(fs->bdev, &jbd_buf->block);
1738 TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue,
1741 jbd_trans_finish_callback(journal,
1746 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1749 RB_FOREACH_SAFE(rec, jbd_revoke_tree, &trans->revoke_root,
1751 RB_REMOVE(jbd_revoke_tree, &trans->revoke_root, rec);
1754 LIST_FOREACH_SAFE(block_rec, &trans->tbrec_list, tbrec_node,
1756 jbd_trans_remove_block_rec(journal, block_rec, trans);
1762 /**@brief Write commit block for a transaction
1763 * @param trans transaction
1764 * @return standard error code*/
1765 static int jbd_trans_write_commit_block(struct jbd_trans *trans)
1768 struct ext4_block block;
1769 struct jbd_commit_header *header;
1770 uint32_t commit_iblock;
1771 struct jbd_journal *journal = trans->journal;
1773 commit_iblock = jbd_journal_alloc_block(journal, trans);
1775 rc = jbd_block_get_noread(journal->jbd_fs, &block, commit_iblock);
1779 header = (struct jbd_commit_header *)block.data;
1780 jbd_set32(&header->header, magic, JBD_MAGIC_NUMBER);
1781 jbd_set32(&header->header, blocktype, JBD_COMMIT_BLOCK);
1782 jbd_set32(&header->header, sequence, trans->trans_id);
1784 if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb,
1785 JBD_FEATURE_COMPAT_CHECKSUM)) {
1786 jbd_set32(header, chksum_type, JBD_CRC32_CHKSUM);
1787 jbd_set32(header, chksum_size, JBD_CRC32_CHKSUM_SIZE);
1788 jbd_set32(header, chksum[0], trans->data_csum);
1790 jbd_commit_csum_set(journal->jbd_fs, header);
1791 ext4_bcache_set_dirty(block.buf);
1792 ext4_bcache_set_flag(block.buf, BC_TMP);
1793 rc = jbd_block_set(journal->jbd_fs, &block);
1797 /**@brief Write descriptor block for a transaction
1798 * @param journal current journal session
1799 * @param trans transaction
1800 * @return standard error code*/
1801 static int jbd_journal_prepare(struct jbd_journal *journal,
1802 struct jbd_trans *trans)
1804 int rc = EOK, i = 0;
1805 struct ext4_block desc_block = EXT4_BLOCK_ZERO(),
1806 data_block = EXT4_BLOCK_ZERO();
1807 int32_t tag_tbl_size = 0;
1808 uint32_t desc_iblock = 0;
1809 uint32_t data_iblock = 0;
1810 char *tag_start = NULL, *tag_ptr = NULL;
1811 struct jbd_buf *jbd_buf, *tmp;
1812 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1813 uint32_t checksum = EXT4_CRC32_INIT;
1814 struct jbd_bhdr *bhdr = NULL;
1817 /* Try to remove any non-dirty buffers from the tail of
1819 TAILQ_FOREACH_REVERSE_SAFE(jbd_buf, &trans->buf_queue,
1820 jbd_trans_buf, buf_node, tmp) {
1821 struct jbd_revoke_rec tmp_rec = {
1822 .lba = jbd_buf->block_rec->lba
1824 /* We stop the iteration when we find a dirty buffer. */
1825 if (ext4_bcache_test_flag(jbd_buf->block.buf,
1829 TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue,
1833 jbd_buf->block.buf->end_write = NULL;
1834 jbd_buf->block.buf->end_write_arg = NULL;
1835 jbd_trans_finish_callback(journal,
1839 RB_FIND(jbd_revoke_tree,
1840 &trans->revoke_root,
1842 jbd_trans_remove_block_rec(journal,
1843 jbd_buf->block_rec, trans);
1846 ext4_block_set(fs->bdev, &jbd_buf->block);
1847 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1851 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node, tmp) {
1852 struct tag_info tag_info;
1853 bool uuid_exist = false;
1854 bool is_escape = false;
1855 struct jbd_revoke_rec tmp_rec = {
1856 .lba = jbd_buf->block_rec->lba
1858 if (!ext4_bcache_test_flag(jbd_buf->block.buf,
1860 TAILQ_REMOVE(&jbd_buf->block_rec->dirty_buf_queue,
1864 jbd_buf->block.buf->end_write = NULL;
1865 jbd_buf->block.buf->end_write_arg = NULL;
1867 /* The buffer has not been modified, just release
1869 jbd_trans_finish_callback(journal,
1873 RB_FIND(jbd_revoke_tree,
1874 &trans->revoke_root,
1876 jbd_trans_remove_block_rec(journal,
1877 jbd_buf->block_rec, trans);
1880 ext4_block_set(fs->bdev, &jbd_buf->block);
1881 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1885 checksum = jbd_block_csum(journal->jbd_fs,
1886 jbd_buf->block.data,
1889 if (((struct jbd_bhdr *)jbd_buf->block.data)->magic ==
1890 to_be32(JBD_MAGIC_NUMBER))
1895 desc_iblock = jbd_journal_alloc_block(journal, trans);
1896 rc = jbd_block_get_noread(journal->jbd_fs, &desc_block, desc_iblock);
1900 bhdr = (struct jbd_bhdr *)desc_block.data;
1901 jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
1902 jbd_set32(bhdr, blocktype, JBD_DESCRIPTOR_BLOCK);
1903 jbd_set32(bhdr, sequence, trans->trans_id);
1905 tag_start = (char *)(bhdr + 1);
1906 tag_ptr = tag_start;
1908 tag_tbl_size = journal->block_size -
1909 sizeof(struct jbd_bhdr);
1911 if (jbd_has_csum(&journal->jbd_fs->sb))
1912 tag_tbl_size -= sizeof(struct jbd_block_tail);
1914 if (!trans->start_iblock)
1915 trans->start_iblock = desc_iblock;
1917 ext4_bcache_set_dirty(desc_block.buf);
1918 ext4_bcache_set_flag(desc_block.buf, BC_TMP);
1920 tag_info.block = jbd_buf->block.lb_id;
1921 tag_info.uuid_exist = uuid_exist;
1922 tag_info.is_escape = is_escape;
1923 if (i == trans->data_cnt - 1)
1924 tag_info.last_tag = true;
1926 tag_info.last_tag = false;
1928 tag_info.checksum = checksum;
1931 memcpy(tag_info.uuid, journal->jbd_fs->sb.uuid,
1934 rc = jbd_write_block_tag(journal->jbd_fs,
1939 jbd_meta_csum_set(journal->jbd_fs, bhdr);
1941 rc = jbd_block_set(journal->jbd_fs, &desc_block);
1948 data_iblock = jbd_journal_alloc_block(journal, trans);
1949 rc = jbd_block_get_noread(journal->jbd_fs, &data_block, data_iblock);
1952 ext4_bcache_clear_dirty(desc_block.buf);
1953 jbd_block_set(journal->jbd_fs, &desc_block);
1957 data = data_block.data;
1958 memcpy(data, jbd_buf->block.data,
1959 journal->block_size);
1961 ((struct jbd_bhdr *)data)->magic = 0;
1963 ext4_bcache_set_dirty(data_block.buf);
1964 ext4_bcache_set_flag(data_block.buf, BC_TMP);
1965 rc = jbd_block_set(journal->jbd_fs, &data_block);
1968 ext4_bcache_clear_dirty(desc_block.buf);
1969 jbd_block_set(journal->jbd_fs, &desc_block);
1972 jbd_buf->jbd_lba = data_iblock;
1974 tag_ptr += tag_info.tag_bytes;
1975 tag_tbl_size -= tag_info.tag_bytes;
1979 if (rc == EOK && desc_iblock) {
1980 jbd_meta_csum_set(journal->jbd_fs,
1981 (struct jbd_bhdr *)bhdr);
1982 trans->data_csum = checksum;
1983 rc = jbd_block_set(journal->jbd_fs, &desc_block);
1989 /**@brief Write revoke block for a transaction
1990 * @param journal current journal session
1991 * @param trans transaction
1992 * @return standard error code*/
1994 jbd_journal_prepare_revoke(struct jbd_journal *journal,
1995 struct jbd_trans *trans)
1997 int rc = EOK, i = 0;
1998 struct ext4_block desc_block = EXT4_BLOCK_ZERO();
1999 int32_t tag_tbl_size = 0;
2000 uint32_t desc_iblock = 0;
2001 char *blocks_entry = NULL;
2002 struct jbd_revoke_rec *rec, *tmp;
2003 struct jbd_revoke_header *header = NULL;
2004 int32_t record_len = 4;
2005 struct jbd_bhdr *bhdr = NULL;
2007 if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb,
2008 JBD_FEATURE_INCOMPAT_64BIT))
2011 RB_FOREACH_SAFE(rec, jbd_revoke_tree, &trans->revoke_root,
2015 desc_iblock = jbd_journal_alloc_block(journal, trans);
2016 rc = jbd_block_get_noread(journal->jbd_fs, &desc_block,
2021 bhdr = (struct jbd_bhdr *)desc_block.data;
2022 jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
2023 jbd_set32(bhdr, blocktype, JBD_REVOKE_BLOCK);
2024 jbd_set32(bhdr, sequence, trans->trans_id);
2026 header = (struct jbd_revoke_header *)bhdr;
2027 blocks_entry = (char *)(header + 1);
2028 tag_tbl_size = journal->block_size -
2029 sizeof(struct jbd_revoke_header);
2031 if (jbd_has_csum(&journal->jbd_fs->sb))
2032 tag_tbl_size -= sizeof(struct jbd_block_tail);
2034 if (!trans->start_iblock)
2035 trans->start_iblock = desc_iblock;
2037 ext4_bcache_set_dirty(desc_block.buf);
2038 ext4_bcache_set_flag(desc_block.buf, BC_TMP);
2041 if (tag_tbl_size < record_len) {
2042 jbd_set32(header, count,
2043 journal->block_size - tag_tbl_size);
2044 jbd_meta_csum_set(journal->jbd_fs, bhdr);
2048 rc = jbd_block_set(journal->jbd_fs, &desc_block);
2054 if (record_len == 8) {
2056 (uint64_t *)blocks_entry;
2057 *blocks = to_be64(rec->lba);
2060 (uint32_t *)blocks_entry;
2061 *blocks = to_be32((uint32_t)rec->lba);
2063 blocks_entry += record_len;
2064 tag_tbl_size -= record_len;
2068 if (rc == EOK && desc_iblock) {
2070 jbd_set32(header, count,
2071 journal->block_size - tag_tbl_size);
2073 jbd_meta_csum_set(journal->jbd_fs, bhdr);
2074 rc = jbd_block_set(journal->jbd_fs, &desc_block);
2080 /**@brief Put references of block descriptors in a transaction.
2081 * @param journal current journal session
2082 * @param trans transaction*/
2083 void jbd_journal_cp_trans(struct jbd_journal *journal, struct jbd_trans *trans)
2085 struct jbd_buf *jbd_buf, *tmp;
2086 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
2087 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
2089 struct ext4_block block = jbd_buf->block;
2090 ext4_block_set(fs->bdev, &block);
2094 /**@brief Update the start block of the journal when
2095 * all the contents in a transaction reach the disk.*/
2096 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
2097 struct ext4_buf *buf,
2101 struct jbd_buf *jbd_buf = arg;
2102 struct jbd_trans *trans = jbd_buf->trans;
2103 struct jbd_block_rec *block_rec = jbd_buf->block_rec;
2104 struct jbd_journal *journal = trans->journal;
2105 bool first_in_queue =
2106 trans == TAILQ_FIRST(&journal->cp_queue);
2110 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
2111 TAILQ_REMOVE(&block_rec->dirty_buf_queue,
2115 jbd_trans_finish_callback(journal,
2120 if (block_rec->trans == trans && buf) {
2121 /* Clear the end_write and end_write_arg fields. */
2122 buf->end_write = NULL;
2123 buf->end_write_arg = NULL;
2128 trans->written_cnt++;
2129 if (trans->written_cnt == trans->data_cnt) {
2130 /* If it is the first transaction on checkpoint queue,
2131 * we will shift the start of the journal to the next
2132 * transaction, and remove subsequent written
2133 * transactions from checkpoint queue until we find
2134 * an unwritten one. */
2135 if (first_in_queue) {
2136 journal->start = trans->start_iblock +
2137 trans->alloc_blocks;
2138 wrap(&journal->jbd_fs->sb, journal->start);
2139 journal->trans_id = trans->trans_id + 1;
2140 TAILQ_REMOVE(&journal->cp_queue, trans, trans_node);
2141 jbd_journal_free_trans(journal, trans, false);
2143 jbd_journal_purge_cp_trans(journal, false, true);
2144 jbd_journal_write_sb(journal);
2145 jbd_write_sb(journal->jbd_fs);
2150 /**@brief Commit a transaction to the journal immediately.
2151 * @param journal current journal session
2152 * @param trans transaction
2153 * @return standard error code*/
2154 static int __jbd_journal_commit_trans(struct jbd_journal *journal,
2155 struct jbd_trans *trans)
2158 uint32_t last = journal->last;
2159 struct jbd_revoke_rec *rec, *tmp;
2161 trans->trans_id = journal->alloc_trans_id;
2162 rc = jbd_journal_prepare(journal, trans);
2166 rc = jbd_journal_prepare_revoke(journal, trans);
2170 if (TAILQ_EMPTY(&trans->buf_queue) &&
2171 RB_EMPTY(&trans->revoke_root)) {
2172 /* Since there are no entries in both buffer list
2173 * and revoke entry list, we do not consider trans as
2174 * complete transaction and just return EOK.*/
2175 jbd_journal_free_trans(journal, trans, false);
2179 rc = jbd_trans_write_commit_block(trans);
2183 journal->alloc_trans_id++;
2185 /* Complete the checkpoint of buffers which are revoked. */
2186 RB_FOREACH_SAFE(rec, jbd_revoke_tree, &trans->revoke_root,
2188 struct jbd_block_rec *block_rec =
2189 jbd_trans_block_rec_lookup(journal, rec->lba);
2190 struct jbd_buf *jbd_buf = NULL;
2192 jbd_buf = TAILQ_LAST(&block_rec->dirty_buf_queue,
2195 struct ext4_buf *buf;
2196 struct ext4_block block = EXT4_BLOCK_ZERO();
2198 * We do this to reset the ext4_buf::end_write and
2199 * ext4_buf::end_write_arg fields so that the checkpoint
2200 * callback won't be triggered again.
2202 buf = ext4_bcache_find_get(journal->jbd_fs->bdev->bc,
2204 jbd_buf->block_rec->lba);
2205 jbd_trans_end_write(journal->jbd_fs->bdev->bc,
2210 ext4_block_set(journal->jbd_fs->bdev, &block);
2214 if (TAILQ_EMPTY(&journal->cp_queue)) {
2216 * This transaction is going to be the first object in the
2218 * When the first transaction in checkpoint queue is completely
2219 * written to disk, we shift the tail of the log to right.
2221 if (trans->data_cnt) {
2222 journal->start = trans->start_iblock;
2223 wrap(&journal->jbd_fs->sb, journal->start);
2224 journal->trans_id = trans->trans_id;
2225 jbd_journal_write_sb(journal);
2226 jbd_write_sb(journal->jbd_fs);
2227 TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
2229 jbd_journal_cp_trans(journal, trans);
2231 journal->start = trans->start_iblock +
2232 trans->alloc_blocks;
2233 wrap(&journal->jbd_fs->sb, journal->start);
2234 journal->trans_id = trans->trans_id + 1;
2235 jbd_journal_write_sb(journal);
2236 jbd_journal_free_trans(journal, trans, false);
2239 /* No need to do anything to the JBD superblock. */
2240 TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
2242 if (trans->data_cnt)
2243 jbd_journal_cp_trans(journal, trans);
2246 if (rc != EOK && rc != ENOSPC) {
2247 journal->last = last;
2248 jbd_journal_free_trans(journal, trans, true);
2253 /**@brief Allocate a new transaction
2254 * @param journal current journal session
2255 * @return transaction allocated*/
2257 jbd_journal_new_trans(struct jbd_journal *journal)
2259 struct jbd_trans *trans = NULL;
2260 trans = ext4_calloc(1, sizeof(struct jbd_trans));
2264 /* We will assign a trans_id to this transaction,
2265 * once it has been committed.*/
2266 trans->journal = journal;
2267 trans->data_csum = EXT4_CRC32_INIT;
2269 TAILQ_INIT(&trans->buf_queue);
2273 /**@brief Commit a transaction to the journal immediately.
2274 * @param journal current journal session
2275 * @param trans transaction
2276 * @return standard error code*/
2277 int jbd_journal_commit_trans(struct jbd_journal *journal,
2278 struct jbd_trans *trans)
2281 r = __jbd_journal_commit_trans(journal, trans);