2 * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3 * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 /** @addtogroup lwext4
34 * @file ext4_journal.c
35 * @brief Journal handle functions
38 #include "ext4_config.h"
39 #include "ext4_types.h"
41 #include "ext4_super.h"
42 #include "ext4_journal.h"
43 #include "ext4_errno.h"
44 #include "ext4_blockdev.h"
45 #include "ext4_crc32c.h"
46 #include "ext4_debug.h"
51 /**@brief Revoke entry during journal replay.*/
53 /**@brief Block number not to be replayed.*/
56 /**@brief For any transaction id smaller
57 * than trans_id, records of @block
58 * in those transactions should not
62 /**@brief Revoke tree node.*/
63 RB_ENTRY(revoke_entry) revoke_node;
66 /**@brief Valid journal replay information.*/
68 /**@brief Starting transaction id.*/
69 uint32_t start_trans_id;
71 /**@brief Ending transaction id.*/
72 uint32_t last_trans_id;
74 /**@brief Used as internal argument.*/
75 uint32_t this_trans_id;
77 /**@brief RB-Tree storing revoke entries.*/
78 RB_HEAD(jbd_revoke, revoke_entry) revoke_root;
81 /**@brief Journal replay internal arguments.*/
83 /**@brief Journal replay information.*/
84 struct recover_info *info;
86 /**@brief Current block we are on.*/
89 /**@brief Current trans_id we are on.*/
90 uint32_t this_trans_id;
94 jbd_revoke_entry_cmp(struct revoke_entry *a, struct revoke_entry *b)
96 if (a->block > b->block)
98 else if (a->block < b->block)
104 jbd_block_rec_cmp(struct jbd_block_rec *a, struct jbd_block_rec *b)
108 else if (a->lba < b->lba)
113 RB_GENERATE_INTERNAL(jbd_revoke, revoke_entry, revoke_node,
114 jbd_revoke_entry_cmp, static inline)
115 RB_GENERATE_INTERNAL(jbd_block, jbd_block_rec, block_rec_node,
116 jbd_block_rec_cmp, static inline)
118 #define jbd_alloc_revoke_entry() calloc(1, sizeof(struct revoke_entry))
119 #define jbd_free_revoke_entry(addr) free(addr)
121 /**@brief Write jbd superblock to disk.
122 * @param jbd_fs jbd filesystem
123 * @param s jbd superblock
124 * @return standard error code*/
125 static int jbd_sb_write(struct jbd_fs *jbd_fs, struct jbd_sb *s)
128 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
131 rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
135 offset = fblock * ext4_sb_get_block_size(&fs->sb);
136 return ext4_block_writebytes(fs->bdev, offset, s,
137 EXT4_SUPERBLOCK_SIZE);
140 /**@brief Read jbd superblock from disk.
141 * @param jbd_fs jbd filesystem
142 * @param s jbd superblock
143 * @return standard error code*/
144 static int jbd_sb_read(struct jbd_fs *jbd_fs, struct jbd_sb *s)
147 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
150 rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
154 offset = fblock * ext4_sb_get_block_size(&fs->sb);
155 return ext4_block_readbytes(fs->bdev, offset, s,
156 EXT4_SUPERBLOCK_SIZE);
159 /**@brief Verify jbd superblock.
160 * @param sb jbd superblock
161 * @return true if jbd superblock is valid */
162 static bool jbd_verify_sb(struct jbd_sb *sb)
164 struct jbd_bhdr *header = &sb->header;
165 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER)
168 if (jbd_get32(header, blocktype) != JBD_SUPERBLOCK &&
169 jbd_get32(header, blocktype) != JBD_SUPERBLOCK_V2)
175 /**@brief Write back dirty jbd superblock to disk.
176 * @param jbd_fs jbd filesystem
177 * @return standard error code*/
178 static int jbd_write_sb(struct jbd_fs *jbd_fs)
182 rc = jbd_sb_write(jbd_fs, &jbd_fs->sb);
186 jbd_fs->dirty = false;
191 /**@brief Get reference to jbd filesystem.
192 * @param fs Filesystem to load journal of
193 * @param jbd_fs jbd filesystem
194 * @return standard error code*/
195 int jbd_get_fs(struct ext4_fs *fs,
196 struct jbd_fs *jbd_fs)
199 uint32_t journal_ino;
201 memset(jbd_fs, 0, sizeof(struct jbd_fs));
202 /* See if there is journal inode on this filesystem.*/
203 /* FIXME: detection on existance ofbkejournal bdev is
205 journal_ino = ext4_get32(&fs->sb, journal_inode_number);
207 rc = ext4_fs_get_inode_ref(fs,
211 memset(jbd_fs, 0, sizeof(struct jbd_fs));
214 rc = jbd_sb_read(jbd_fs, &jbd_fs->sb);
216 memset(jbd_fs, 0, sizeof(struct jbd_fs));
217 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
220 if (!jbd_verify_sb(&jbd_fs->sb)) {
221 memset(jbd_fs, 0, sizeof(struct jbd_fs));
222 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
229 /**@brief Put reference of jbd filesystem.
230 * @param jbd_fs jbd filesystem
231 * @return standard error code*/
232 int jbd_put_fs(struct jbd_fs *jbd_fs)
235 rc = jbd_write_sb(jbd_fs);
237 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
241 /**@brief Data block lookup helper.
242 * @param jbd_fs jbd filesystem
243 * @param iblock block index
244 * @param fblock logical block address
245 * @return standard error code*/
246 int jbd_inode_bmap(struct jbd_fs *jbd_fs,
248 ext4_fsblk_t *fblock)
250 int rc = ext4_fs_get_inode_dblk_idx(
258 /**@brief jbd block get function (through cache).
259 * @param jbd_fs jbd filesystem
260 * @param block block descriptor
261 * @param fblock jbd logical block address
262 * @return standard error code*/
263 static int jbd_block_get(struct jbd_fs *jbd_fs,
264 struct ext4_block *block,
267 /* TODO: journal device. */
269 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
271 /* Lookup the logical block address of
273 rc = jbd_inode_bmap(jbd_fs, iblock,
278 struct ext4_blockdev *bdev = jbd_fs->inode_ref.fs->bdev;
279 rc = ext4_block_get(bdev, block, fblock);
281 /* If succeeded, mark buffer as BC_FLUSH to indicate
282 * that data should be written to disk immediately.*/
284 ext4_bcache_set_flag(block->buf, BC_FLUSH);
289 /**@brief jbd block get function (through cache, don't read).
290 * @param jbd_fs jbd filesystem
291 * @param block block descriptor
292 * @param fblock jbd logical block address
293 * @return standard error code*/
294 static int jbd_block_get_noread(struct jbd_fs *jbd_fs,
295 struct ext4_block *block,
298 /* TODO: journal device. */
300 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
301 rc = jbd_inode_bmap(jbd_fs, iblock,
306 struct ext4_blockdev *bdev = jbd_fs->inode_ref.fs->bdev;
307 rc = ext4_block_get_noread(bdev, block, fblock);
309 ext4_bcache_set_flag(block->buf, BC_FLUSH);
314 /**@brief jbd block set procedure (through cache).
315 * @param jbd_fs jbd filesystem
316 * @param block block descriptor
317 * @return standard error code*/
318 static int jbd_block_set(struct jbd_fs *jbd_fs,
319 struct ext4_block *block)
321 return ext4_block_set(jbd_fs->inode_ref.fs->bdev,
325 /**@brief helper functions to calculate
326 * block tag size, not including UUID part.
327 * @param jbd_fs jbd filesystem
328 * @return tag size in bytes*/
329 static int jbd_tag_bytes(struct jbd_fs *jbd_fs)
333 /* It is very easy to deal with the case which
334 * JBD_FEATURE_INCOMPAT_CSUM_V3 is enabled.*/
335 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
336 JBD_FEATURE_INCOMPAT_CSUM_V3))
337 return sizeof(struct jbd_block_tag3);
339 size = sizeof(struct jbd_block_tag);
341 /* If JBD_FEATURE_INCOMPAT_CSUM_V2 is enabled,
342 * add 2 bytes to size.*/
343 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
344 JBD_FEATURE_INCOMPAT_CSUM_V2))
345 size += sizeof(uint16_t);
347 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
348 JBD_FEATURE_INCOMPAT_64BIT))
351 /* If block number is 4 bytes in size,
352 * minus 4 bytes from size */
353 return size - sizeof(uint32_t);
356 /**@brief Tag information. */
358 /**@brief Tag size in bytes, including UUID part.*/
361 /**@brief block number stored in this tag.*/
364 /**@brief whether UUID part exists or not.*/
367 /**@brief UUID content if UUID part exists.*/
368 uint8_t uuid[UUID_SIZE];
370 /**@brief Is this the last tag? */
374 /**@brief Extract information from a block tag.
375 * @param __tag pointer to the block tag
376 * @param tag_bytes block tag size of this jbd filesystem
377 * @param remaining size in buffer containing the block tag
378 * @param tag_info information of this tag.
379 * @return EOK when succeed, otherwise return EINVAL.*/
381 jbd_extract_block_tag(struct jbd_fs *jbd_fs,
384 int32_t remain_buf_size,
385 struct tag_info *tag_info)
388 tag_info->tag_bytes = tag_bytes;
389 tag_info->uuid_exist = false;
390 tag_info->last_tag = false;
392 /* See whether it is possible to hold a valid block tag.*/
393 if (remain_buf_size - tag_bytes < 0)
396 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
397 JBD_FEATURE_INCOMPAT_CSUM_V3)) {
398 struct jbd_block_tag3 *tag = __tag;
399 tag_info->block = jbd_get32(tag, blocknr);
400 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
401 JBD_FEATURE_INCOMPAT_64BIT))
403 (uint64_t)jbd_get32(tag, blocknr_high) << 32;
405 if (jbd_get32(tag, flags) & JBD_FLAG_ESCAPE)
408 if (!(jbd_get32(tag, flags) & JBD_FLAG_SAME_UUID)) {
409 /* See whether it is possible to hold UUID part.*/
410 if (remain_buf_size - tag_bytes < UUID_SIZE)
413 uuid_start = (char *)tag + tag_bytes;
414 tag_info->uuid_exist = true;
415 tag_info->tag_bytes += UUID_SIZE;
416 memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
419 if (jbd_get32(tag, flags) & JBD_FLAG_LAST_TAG)
420 tag_info->last_tag = true;
423 struct jbd_block_tag *tag = __tag;
424 tag_info->block = jbd_get32(tag, blocknr);
425 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
426 JBD_FEATURE_INCOMPAT_64BIT))
428 (uint64_t)jbd_get32(tag, blocknr_high) << 32;
430 if (jbd_get16(tag, flags) & JBD_FLAG_ESCAPE)
433 if (!(jbd_get16(tag, flags) & JBD_FLAG_SAME_UUID)) {
434 /* See whether it is possible to hold UUID part.*/
435 if (remain_buf_size - tag_bytes < UUID_SIZE)
438 uuid_start = (char *)tag + tag_bytes;
439 tag_info->uuid_exist = true;
440 tag_info->tag_bytes += UUID_SIZE;
441 memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
444 if (jbd_get16(tag, flags) & JBD_FLAG_LAST_TAG)
445 tag_info->last_tag = true;
451 /**@brief Write information to a block tag.
452 * @param __tag pointer to the block tag
453 * @param remaining size in buffer containing the block tag
454 * @param tag_info information of this tag.
455 * @return EOK when succeed, otherwise return EINVAL.*/
457 jbd_write_block_tag(struct jbd_fs *jbd_fs,
459 int32_t remain_buf_size,
460 struct tag_info *tag_info)
463 int tag_bytes = jbd_tag_bytes(jbd_fs);
465 tag_info->tag_bytes = tag_bytes;
467 /* See whether it is possible to hold a valid block tag.*/
468 if (remain_buf_size - tag_bytes < 0)
471 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
472 JBD_FEATURE_INCOMPAT_CSUM_V3)) {
473 struct jbd_block_tag3 *tag = __tag;
474 memset(tag, 0, sizeof(struct jbd_block_tag3));
475 jbd_set32(tag, blocknr, tag_info->block);
476 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
477 JBD_FEATURE_INCOMPAT_64BIT))
478 jbd_set32(tag, blocknr_high, tag_info->block >> 32);
480 if (tag_info->uuid_exist) {
481 /* See whether it is possible to hold UUID part.*/
482 if (remain_buf_size - tag_bytes < UUID_SIZE)
485 uuid_start = (char *)tag + tag_bytes;
486 tag_info->tag_bytes += UUID_SIZE;
487 memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
489 jbd_set32(tag, flags,
490 jbd_get32(tag, flags) | JBD_FLAG_SAME_UUID);
492 if (tag_info->last_tag)
493 jbd_set32(tag, flags,
494 jbd_get32(tag, flags) | JBD_FLAG_LAST_TAG);
497 struct jbd_block_tag *tag = __tag;
498 memset(tag, 0, sizeof(struct jbd_block_tag));
499 jbd_set32(tag, blocknr, tag_info->block);
500 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
501 JBD_FEATURE_INCOMPAT_64BIT))
502 jbd_set32(tag, blocknr_high, tag_info->block >> 32);
504 if (tag_info->uuid_exist) {
505 /* See whether it is possible to hold UUID part.*/
506 if (remain_buf_size - tag_bytes < UUID_SIZE)
509 uuid_start = (char *)tag + tag_bytes;
510 tag_info->tag_bytes += UUID_SIZE;
511 memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
513 jbd_set16(tag, flags,
514 jbd_get16(tag, flags) | JBD_FLAG_SAME_UUID);
516 if (tag_info->last_tag)
517 jbd_set16(tag, flags,
518 jbd_get16(tag, flags) | JBD_FLAG_LAST_TAG);
524 /**@brief Iterate all block tags in a block.
525 * @param jbd_fs jbd filesystem
526 * @param __tag_start pointer to the block
527 * @param tag_tbl_size size of the block
528 * @param func callback routine to indicate that
529 * a block tag is found
530 * @param arg additional argument to be passed to func */
532 jbd_iterate_block_table(struct jbd_fs *jbd_fs,
534 int32_t tag_tbl_size,
535 void (*func)(struct jbd_fs * jbd_fs,
541 char *tag_start, *tag_ptr;
542 int tag_bytes = jbd_tag_bytes(jbd_fs);
543 tag_start = __tag_start;
546 /* Cut off the size of block tail storing checksum. */
547 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
548 JBD_FEATURE_INCOMPAT_CSUM_V2) ||
549 JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
550 JBD_FEATURE_INCOMPAT_CSUM_V3))
551 tag_tbl_size -= sizeof(struct jbd_block_tail);
553 while (tag_tbl_size) {
554 struct tag_info tag_info;
555 int rc = jbd_extract_block_tag(jbd_fs,
564 func(jbd_fs, tag_info.block, tag_info.uuid, arg);
566 /* Stop the iteration when we reach the last tag. */
567 if (tag_info.last_tag)
570 tag_ptr += tag_info.tag_bytes;
571 tag_tbl_size -= tag_info.tag_bytes;
575 static void jbd_display_block_tags(struct jbd_fs *jbd_fs,
580 uint32_t *iblock = arg;
581 ext4_dbg(DEBUG_JBD, "Block in block_tag: %" PRIu64 "\n", block);
588 static struct revoke_entry *
589 jbd_revoke_entry_lookup(struct recover_info *info, ext4_fsblk_t block)
591 struct revoke_entry tmp = {
595 return RB_FIND(jbd_revoke, &info->revoke_root, &tmp);
598 /**@brief Replay a block in a transaction.
599 * @param jbd_fs jbd filesystem
600 * @param block block address to be replayed.*/
601 static void jbd_replay_block_tags(struct jbd_fs *jbd_fs,
603 uint8_t *uuid __unused,
607 struct replay_arg *arg = __arg;
608 struct recover_info *info = arg->info;
609 uint32_t *this_block = arg->this_block;
610 struct revoke_entry *revoke_entry;
611 struct ext4_block journal_block, ext4_block;
612 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
616 /* We replay this block only if the current transaction id
617 * is equal or greater than that in revoke entry.*/
618 revoke_entry = jbd_revoke_entry_lookup(info, block);
620 arg->this_trans_id < revoke_entry->trans_id)
624 "Replaying block in block_tag: %" PRIu64 "\n",
627 r = jbd_block_get(jbd_fs, &journal_block, *this_block);
631 /* We need special treatment for ext4 superblock. */
633 r = ext4_block_get_noread(fs->bdev, &ext4_block, block);
635 jbd_block_set(jbd_fs, &journal_block);
639 memcpy(ext4_block.data,
641 jbd_get32(&jbd_fs->sb, blocksize));
643 ext4_bcache_set_dirty(ext4_block.buf);
644 ext4_block_set(fs->bdev, &ext4_block);
646 uint16_t mount_count, state;
647 mount_count = ext4_get16(&fs->sb, mount_count);
648 state = ext4_get16(&fs->sb, state);
651 journal_block.data + EXT4_SUPERBLOCK_OFFSET,
652 EXT4_SUPERBLOCK_SIZE);
654 /* Mark system as mounted */
655 ext4_set16(&fs->sb, state, state);
656 r = ext4_sb_write(fs->bdev, &fs->sb);
660 /*Update mount count*/
661 ext4_set16(&fs->sb, mount_count, mount_count);
664 jbd_block_set(jbd_fs, &journal_block);
669 /**@brief Add block address to revoke tree, along with
670 * its transaction id.
671 * @param info journal replay info
672 * @param block block address to be replayed.*/
673 static void jbd_add_revoke_block_tags(struct recover_info *info,
676 struct revoke_entry *revoke_entry;
678 ext4_dbg(DEBUG_JBD, "Add block %" PRIu64 " to revoke tree\n", block);
679 /* If the revoke entry with respect to the block address
680 * exists already, update its transaction id.*/
681 revoke_entry = jbd_revoke_entry_lookup(info, block);
683 revoke_entry->trans_id = info->this_trans_id;
687 revoke_entry = jbd_alloc_revoke_entry();
688 ext4_assert(revoke_entry);
689 revoke_entry->block = block;
690 revoke_entry->trans_id = info->this_trans_id;
691 RB_INSERT(jbd_revoke, &info->revoke_root, revoke_entry);
696 static void jbd_destroy_revoke_tree(struct recover_info *info)
698 while (!RB_EMPTY(&info->revoke_root)) {
699 struct revoke_entry *revoke_entry =
700 RB_MIN(jbd_revoke, &info->revoke_root);
701 ext4_assert(revoke_entry);
702 RB_REMOVE(jbd_revoke, &info->revoke_root, revoke_entry);
703 jbd_free_revoke_entry(revoke_entry);
707 /* Make sure we wrap around the log correctly! */
708 #define wrap(sb, var) \
710 if (var >= jbd_get32((sb), maxlen)) \
711 var -= (jbd_get32((sb), maxlen) - jbd_get32((sb), first)); \
714 #define ACTION_SCAN 0
715 #define ACTION_REVOKE 1
716 #define ACTION_RECOVER 2
718 /**@brief Add entries in a revoke block to revoke tree.
719 * @param jbd_fs jbd filesystem
720 * @param header revoke block header
721 * @param recover_info journal replay info*/
722 static void jbd_build_revoke_tree(struct jbd_fs *jbd_fs,
723 struct jbd_bhdr *header,
724 struct recover_info *info)
727 struct jbd_revoke_header *revoke_hdr =
728 (struct jbd_revoke_header *)header;
729 uint32_t i, nr_entries, record_len = 4;
731 /* If we are working on a 64bit jbd filesystem, */
732 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
733 JBD_FEATURE_INCOMPAT_64BIT))
736 nr_entries = (jbd_get32(revoke_hdr, count) -
737 sizeof(struct jbd_revoke_header)) /
740 blocks_entry = (char *)(revoke_hdr + 1);
742 for (i = 0;i < nr_entries;i++) {
743 if (record_len == 8) {
745 (uint64_t *)blocks_entry;
746 jbd_add_revoke_block_tags(info, to_be64(*blocks));
749 (uint32_t *)blocks_entry;
750 jbd_add_revoke_block_tags(info, to_be32(*blocks));
752 blocks_entry += record_len;
756 static void jbd_debug_descriptor_block(struct jbd_fs *jbd_fs,
757 struct jbd_bhdr *header,
760 jbd_iterate_block_table(jbd_fs,
762 jbd_get32(&jbd_fs->sb, blocksize) -
763 sizeof(struct jbd_bhdr),
764 jbd_display_block_tags,
768 static void jbd_replay_descriptor_block(struct jbd_fs *jbd_fs,
769 struct jbd_bhdr *header,
770 struct replay_arg *arg)
772 jbd_iterate_block_table(jbd_fs,
774 jbd_get32(&jbd_fs->sb, blocksize) -
775 sizeof(struct jbd_bhdr),
776 jbd_replay_block_tags,
780 /**@brief The core routine of journal replay.
781 * @param jbd_fs jbd filesystem
782 * @param recover_info journal replay info
783 * @param action action needed to be taken
784 * @return standard error code*/
785 static int jbd_iterate_log(struct jbd_fs *jbd_fs,
786 struct recover_info *info,
790 bool log_end = false;
791 struct jbd_sb *sb = &jbd_fs->sb;
792 uint32_t start_trans_id, this_trans_id;
793 uint32_t start_block, this_block;
795 /* We start iterating valid blocks in the whole journal.*/
796 start_trans_id = this_trans_id = jbd_get32(sb, sequence);
797 start_block = this_block = jbd_get32(sb, start);
799 ext4_dbg(DEBUG_JBD, "Start of journal at trans id: %" PRIu32 "\n",
803 struct ext4_block block;
804 struct jbd_bhdr *header;
805 /* If we are not scanning for the last
806 * valid transaction in the journal,
807 * we will stop when we reach the end of
809 if (action != ACTION_SCAN)
810 if (this_trans_id > info->last_trans_id) {
815 r = jbd_block_get(jbd_fs, &block, this_block);
819 header = (struct jbd_bhdr *)block.data;
820 /* This block does not have a valid magic number,
821 * so we have reached the end of the journal.*/
822 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER) {
823 jbd_block_set(jbd_fs, &block);
828 /* If the transaction id we found is not expected,
829 * we may have reached the end of the journal.
831 * If we are not scanning the journal, something
832 * bad might have taken place. :-( */
833 if (jbd_get32(header, sequence) != this_trans_id) {
834 if (action != ACTION_SCAN)
837 jbd_block_set(jbd_fs, &block);
842 switch (jbd_get32(header, blocktype)) {
843 case JBD_DESCRIPTOR_BLOCK:
844 ext4_dbg(DEBUG_JBD, "Descriptor block: %" PRIu32", "
845 "trans_id: %" PRIu32"\n",
846 this_block, this_trans_id);
847 if (action == ACTION_RECOVER) {
848 struct replay_arg replay_arg;
849 replay_arg.info = info;
850 replay_arg.this_block = &this_block;
851 replay_arg.this_trans_id = this_trans_id;
853 jbd_replay_descriptor_block(jbd_fs,
854 header, &replay_arg);
856 jbd_debug_descriptor_block(jbd_fs,
857 header, &this_block);
860 case JBD_COMMIT_BLOCK:
861 ext4_dbg(DEBUG_JBD, "Commit block: %" PRIu32", "
862 "trans_id: %" PRIu32"\n",
863 this_block, this_trans_id);
864 /* This is the end of a transaction,
865 * we may now proceed to the next transaction.
869 case JBD_REVOKE_BLOCK:
870 ext4_dbg(DEBUG_JBD, "Revoke block: %" PRIu32", "
871 "trans_id: %" PRIu32"\n",
872 this_block, this_trans_id);
873 if (action == ACTION_REVOKE) {
874 info->this_trans_id = this_trans_id;
875 jbd_build_revoke_tree(jbd_fs,
883 jbd_block_set(jbd_fs, &block);
885 wrap(sb, this_block);
886 if (this_block == start_block)
890 ext4_dbg(DEBUG_JBD, "End of journal.\n");
891 if (r == EOK && action == ACTION_SCAN) {
892 /* We have finished scanning the journal. */
893 info->start_trans_id = start_trans_id;
894 if (this_trans_id > start_trans_id)
895 info->last_trans_id = this_trans_id - 1;
897 info->last_trans_id = this_trans_id;
903 /**@brief Replay journal.
904 * @param jbd_fs jbd filesystem
905 * @return standard error code*/
906 int jbd_recover(struct jbd_fs *jbd_fs)
909 struct recover_info info;
910 struct jbd_sb *sb = &jbd_fs->sb;
914 RB_INIT(&info.revoke_root);
916 r = jbd_iterate_log(jbd_fs, &info, ACTION_SCAN);
920 r = jbd_iterate_log(jbd_fs, &info, ACTION_REVOKE);
924 r = jbd_iterate_log(jbd_fs, &info, ACTION_RECOVER);
926 /* If we successfully replay the journal,
927 * clear EXT4_FINCOM_RECOVER flag on the
928 * ext4 superblock, and set the start of
930 uint32_t features_incompatible =
931 ext4_get32(&jbd_fs->inode_ref.fs->sb,
932 features_incompatible);
933 jbd_set32(&jbd_fs->sb, start, 0);
934 features_incompatible &= ~EXT4_FINCOM_RECOVER;
935 ext4_set32(&jbd_fs->inode_ref.fs->sb,
936 features_incompatible,
937 features_incompatible);
938 jbd_fs->dirty = true;
939 r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
940 &jbd_fs->inode_ref.fs->sb);
942 jbd_destroy_revoke_tree(&info);
946 static void jbd_journal_write_sb(struct jbd_journal *journal)
948 struct jbd_fs *jbd_fs = journal->jbd_fs;
949 jbd_set32(&jbd_fs->sb, start, journal->start);
950 jbd_set32(&jbd_fs->sb, sequence, journal->trans_id);
951 jbd_fs->dirty = true;
954 /**@brief Start accessing the journal.
955 * @param jbd_fs jbd filesystem
956 * @param journal current journal session
957 * @return standard error code*/
958 int jbd_journal_start(struct jbd_fs *jbd_fs,
959 struct jbd_journal *journal)
962 uint32_t features_incompatible =
963 ext4_get32(&jbd_fs->inode_ref.fs->sb,
964 features_incompatible);
965 features_incompatible |= EXT4_FINCOM_RECOVER;
966 ext4_set32(&jbd_fs->inode_ref.fs->sb,
967 features_incompatible,
968 features_incompatible);
969 r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
970 &jbd_fs->inode_ref.fs->sb);
974 journal->first = jbd_get32(&jbd_fs->sb, first);
975 journal->start = journal->first;
976 journal->last = journal->first;
977 journal->trans_id = 1;
978 journal->alloc_trans_id = 1;
980 journal->block_size = jbd_get32(&jbd_fs->sb, blocksize);
982 TAILQ_INIT(&journal->trans_queue);
983 TAILQ_INIT(&journal->cp_queue);
984 RB_INIT(&journal->block_rec_root);
985 journal->jbd_fs = jbd_fs;
986 jbd_journal_write_sb(journal);
987 return jbd_write_sb(jbd_fs);
990 static void jbd_journal_flush_trans(struct jbd_trans *trans)
992 struct jbd_buf *jbd_buf, *tmp;
993 struct jbd_journal *journal = trans->journal;
994 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
995 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
997 struct ext4_block block = jbd_buf->block;
998 ext4_block_flush_buf(fs->bdev, block.buf);
1003 jbd_journal_skip_pure_revoke(struct jbd_journal *journal,
1004 struct jbd_trans *trans)
1006 journal->start = trans->start_iblock +
1007 trans->alloc_blocks;
1008 wrap(&journal->jbd_fs->sb, journal->start);
1009 journal->trans_id = trans->trans_id + 1;
1010 jbd_journal_free_trans(journal,
1012 jbd_journal_write_sb(journal);
1015 static void jbd_journal_flush_all_trans(struct jbd_journal *journal)
1017 struct jbd_trans *trans;
1018 while ((trans = TAILQ_FIRST(&journal->cp_queue))) {
1019 if (!trans->data_cnt) {
1020 TAILQ_REMOVE(&journal->cp_queue,
1023 jbd_journal_skip_pure_revoke(journal, trans);
1025 jbd_journal_flush_trans(trans);
1030 /**@brief Stop accessing the journal.
1031 * @param journal current journal session
1032 * @return standard error code*/
1033 int jbd_journal_stop(struct jbd_journal *journal)
1036 struct jbd_fs *jbd_fs = journal->jbd_fs;
1037 uint32_t features_incompatible;
1039 /* Commit all the transactions to the journal.*/
1040 jbd_journal_commit_all(journal);
1042 /* Make sure that journalled content have reached
1044 jbd_journal_flush_all_trans(journal);
1046 /* There should be no block record in this journal
1048 if (!RB_EMPTY(&journal->block_rec_root))
1050 DBG_WARN "There are still block records "
1051 "in this journal session!\n");
1053 features_incompatible =
1054 ext4_get32(&jbd_fs->inode_ref.fs->sb,
1055 features_incompatible);
1056 features_incompatible &= ~EXT4_FINCOM_RECOVER;
1057 ext4_set32(&jbd_fs->inode_ref.fs->sb,
1058 features_incompatible,
1059 features_incompatible);
1060 r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
1061 &jbd_fs->inode_ref.fs->sb);
1066 journal->trans_id = 0;
1067 jbd_journal_write_sb(journal);
1068 return jbd_write_sb(journal->jbd_fs);
1071 /**@brief Allocate a block in the journal.
1072 * @param journal current journal session
1073 * @param trans transaction
1074 * @return allocated block address*/
1075 static uint32_t jbd_journal_alloc_block(struct jbd_journal *journal,
1076 struct jbd_trans *trans)
1078 uint32_t start_block;
1080 start_block = journal->last++;
1081 trans->alloc_blocks++;
1082 wrap(&journal->jbd_fs->sb, journal->last);
1084 /* If there is no space left, flush all journalled
1085 * blocks to disk first.*/
1086 if (journal->last == journal->start)
1087 jbd_journal_flush_all_trans(journal);
1092 /**@brief Allocate a new transaction
1093 * @param journal current journal session
1094 * @return transaction allocated*/
1096 jbd_journal_new_trans(struct jbd_journal *journal)
1098 struct jbd_trans *trans = calloc(1, sizeof(struct jbd_trans));
1102 /* We will assign a trans_id to this transaction,
1103 * once it has been committed.*/
1104 trans->journal = journal;
1109 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
1110 struct ext4_buf *buf __unused,
1114 /**@brief gain access to it before making any modications.
1115 * @param journal current journal session
1116 * @param trans transaction
1117 * @param block descriptor
1118 * @return standard error code.*/
1119 int jbd_trans_get_access(struct jbd_journal *journal,
1120 struct jbd_trans *trans,
1121 struct ext4_block *block)
1124 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1125 struct jbd_buf *jbd_buf = block->buf->end_write_arg;
1127 /* If the buffer has already been modified, we should
1128 * flush dirty data in this buffer to disk.*/
1129 if (ext4_bcache_test_flag(block->buf, BC_DIRTY) &&
1130 block->buf->end_write == jbd_trans_end_write) {
1131 ext4_assert(jbd_buf);
1132 if (jbd_buf->trans != trans)
1133 r = ext4_block_flush_buf(fs->bdev, block->buf);
1139 static struct jbd_block_rec *
1140 jbd_trans_block_rec_lookup(struct jbd_journal *journal,
1143 struct jbd_block_rec tmp = {
1147 return RB_FIND(jbd_block,
1148 &journal->block_rec_root,
1152 static inline struct jbd_block_rec *
1153 jbd_trans_insert_block_rec(struct jbd_trans *trans,
1155 struct ext4_buf *buf)
1157 struct jbd_block_rec *block_rec;
1158 block_rec = jbd_trans_block_rec_lookup(trans->journal, lba);
1160 /* Data should be flushed to disk already. */
1161 ext4_assert(!block_rec->buf);
1162 /* Now this block record belongs to this transaction. */
1163 block_rec->trans = trans;
1166 block_rec = calloc(1, sizeof(struct jbd_block_rec));
1170 block_rec->lba = lba;
1171 block_rec->buf = buf;
1172 block_rec->trans = trans;
1173 RB_INSERT(jbd_block, &trans->journal->block_rec_root, block_rec);
1178 jbd_trans_remove_block_rec(struct jbd_journal *journal,
1179 struct jbd_buf *jbd_buf)
1181 struct jbd_block_rec *block_rec = jbd_buf->block_rec;
1182 /* If this block record doesn't belong to this transaction,
1184 if (block_rec->trans == jbd_buf->trans) {
1185 RB_REMOVE(jbd_block,
1186 &journal->block_rec_root,
1192 /**@brief Add block to a transaction and mark it dirty.
1193 * @param trans transaction
1194 * @param block block descriptor
1195 * @return standard error code*/
1196 int jbd_trans_set_block_dirty(struct jbd_trans *trans,
1197 struct ext4_block *block)
1199 struct jbd_buf *buf;
1201 if (!ext4_bcache_test_flag(block->buf, BC_DIRTY) &&
1202 block->buf->end_write != jbd_trans_end_write) {
1203 struct jbd_block_rec *block_rec;
1204 buf = calloc(1, sizeof(struct jbd_buf));
1208 if ((block_rec = jbd_trans_insert_block_rec(trans,
1210 block->buf)) == NULL) {
1215 buf->block_rec = block_rec;
1217 buf->block = *block;
1218 ext4_bcache_inc_ref(block->buf);
1220 /* If the content reach the disk, notify us
1221 * so that we may do a checkpoint. */
1222 block->buf->end_write = jbd_trans_end_write;
1223 block->buf->end_write_arg = buf;
1226 TAILQ_INSERT_HEAD(&trans->buf_queue, buf, buf_node);
1228 ext4_bcache_set_dirty(block->buf);
1233 /**@brief Add block to be revoked to a transaction
1234 * @param trans transaction
1235 * @param lba logical block address
1236 * @return standard error code*/
1237 int jbd_trans_revoke_block(struct jbd_trans *trans,
1240 struct jbd_revoke_rec *rec =
1241 calloc(1, sizeof(struct jbd_revoke_rec));
1246 LIST_INSERT_HEAD(&trans->revoke_list, rec, revoke_node);
1250 /**@brief Try to add block to be revoked to a transaction.
1251 * If @lba still remains in an transaction on checkpoint
1252 * queue, add @lba as a revoked block to the transaction.
1253 * @param trans transaction
1254 * @param lba logical block address
1255 * @return standard error code*/
1256 int jbd_trans_try_revoke_block(struct jbd_trans *trans,
1260 struct jbd_journal *journal = trans->journal;
1261 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1262 struct jbd_block_rec *block_rec =
1263 jbd_trans_block_rec_lookup(journal, lba);
1265 /* Make sure we don't flush any buffers belong to this transaction. */
1266 if (block_rec && block_rec->trans != trans) {
1267 /* If the buffer has not been flushed yet, flush it now. */
1268 if (block_rec->buf) {
1269 r = ext4_block_flush_buf(fs->bdev, block_rec->buf);
1275 jbd_trans_revoke_block(trans, lba);
1281 /**@brief Free a transaction
1282 * @param journal current journal session
1283 * @param trans transaction
1284 * @param abort discard all the modifications on the block?
1285 * @return standard error code*/
1286 void jbd_journal_free_trans(struct jbd_journal *journal,
1287 struct jbd_trans *trans,
1290 struct jbd_buf *jbd_buf, *tmp;
1291 struct jbd_revoke_rec *rec, *tmp2;
1292 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1293 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
1296 jbd_buf->block.buf->end_write = NULL;
1297 jbd_buf->block.buf->end_write_arg = NULL;
1298 ext4_bcache_clear_dirty(jbd_buf->block.buf);
1299 ext4_block_set(fs->bdev, &jbd_buf->block);
1302 jbd_trans_remove_block_rec(journal, jbd_buf);
1303 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1306 LIST_FOREACH_SAFE(rec, &trans->revoke_list, revoke_node,
1308 LIST_REMOVE(rec, revoke_node);
1315 /**@brief Write commit block for a transaction
1316 * @param trans transaction
1317 * @return standard error code*/
1318 static int jbd_trans_write_commit_block(struct jbd_trans *trans)
1321 struct jbd_commit_header *header;
1322 uint32_t commit_iblock = 0;
1323 struct ext4_block commit_block;
1324 struct jbd_journal *journal = trans->journal;
1326 commit_iblock = jbd_journal_alloc_block(journal, trans);
1327 rc = jbd_block_get_noread(journal->jbd_fs,
1328 &commit_block, commit_iblock);
1332 header = (struct jbd_commit_header *)commit_block.data;
1333 jbd_set32(&header->header, magic, JBD_MAGIC_NUMBER);
1334 jbd_set32(&header->header, blocktype, JBD_COMMIT_BLOCK);
1335 jbd_set32(&header->header, sequence, trans->trans_id);
1337 ext4_bcache_set_dirty(commit_block.buf);
1338 rc = jbd_block_set(journal->jbd_fs, &commit_block);
1345 /**@brief Write descriptor block for a transaction
1346 * @param journal current journal session
1347 * @param trans transaction
1348 * @return standard error code*/
1349 static int jbd_journal_prepare(struct jbd_journal *journal,
1350 struct jbd_trans *trans)
1352 int rc = EOK, i = 0;
1353 int32_t tag_tbl_size;
1354 uint32_t desc_iblock = 0;
1355 uint32_t data_iblock = 0;
1356 char *tag_start = NULL, *tag_ptr = NULL;
1357 struct jbd_buf *jbd_buf, *tmp;
1358 struct ext4_block desc_block, data_block;
1359 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1361 /* Try to remove any non-dirty buffers from the tail of
1363 TAILQ_FOREACH_REVERSE_SAFE(jbd_buf, &trans->buf_queue,
1364 jbd_trans_buf, buf_node, tmp) {
1365 /* We stop the iteration when we find a dirty buffer. */
1366 if (ext4_bcache_test_flag(jbd_buf->block.buf,
1370 /* The buffer has not been modified, just release
1372 jbd_trans_remove_block_rec(journal, jbd_buf);
1375 jbd_buf->block.buf->end_write = NULL;
1376 jbd_buf->block.buf->end_write_arg = NULL;
1377 ext4_block_set(fs->bdev, &jbd_buf->block);
1378 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1382 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node, tmp) {
1383 struct tag_info tag_info;
1384 bool uuid_exist = false;
1385 if (!ext4_bcache_test_flag(jbd_buf->block.buf,
1387 /* The buffer has not been modified, just release
1389 jbd_trans_remove_block_rec(journal, jbd_buf);
1392 jbd_buf->block.buf->end_write = NULL;
1393 jbd_buf->block.buf->end_write_arg = NULL;
1394 ext4_block_set(fs->bdev, &jbd_buf->block);
1395 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1401 struct jbd_bhdr *bhdr;
1402 desc_iblock = jbd_journal_alloc_block(journal, trans);
1403 rc = jbd_block_get_noread(journal->jbd_fs,
1404 &desc_block, desc_iblock);
1408 ext4_bcache_set_dirty(desc_block.buf);
1410 bhdr = (struct jbd_bhdr *)desc_block.data;
1411 jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
1412 jbd_set32(bhdr, blocktype, JBD_DESCRIPTOR_BLOCK);
1413 jbd_set32(bhdr, sequence, trans->trans_id);
1415 tag_start = (char *)(bhdr + 1);
1416 tag_ptr = tag_start;
1418 tag_tbl_size = journal->block_size -
1419 sizeof(struct jbd_bhdr);
1421 if (!trans->start_iblock)
1422 trans->start_iblock = desc_iblock;
1425 tag_info.block = jbd_buf->block.lb_id;
1426 tag_info.uuid_exist = uuid_exist;
1427 if (i == trans->data_cnt - 1)
1428 tag_info.last_tag = true;
1430 tag_info.last_tag = false;
1433 memcpy(tag_info.uuid, journal->jbd_fs->sb.uuid,
1436 rc = jbd_write_block_tag(journal->jbd_fs,
1441 jbd_block_set(journal->jbd_fs, &desc_block);
1446 data_iblock = jbd_journal_alloc_block(journal, trans);
1447 rc = jbd_block_get_noread(journal->jbd_fs,
1448 &data_block, data_iblock);
1452 ext4_bcache_set_dirty(data_block.buf);
1454 memcpy(data_block.data, jbd_buf->block.data,
1455 journal->block_size);
1457 rc = jbd_block_set(journal->jbd_fs, &data_block);
1461 tag_ptr += tag_info.tag_bytes;
1462 tag_tbl_size -= tag_info.tag_bytes;
1466 if (rc == EOK && desc_iblock)
1467 jbd_block_set(journal->jbd_fs, &desc_block);
1472 /**@brief Write revoke block for a transaction
1473 * @param journal current journal session
1474 * @param trans transaction
1475 * @return standard error code*/
1477 jbd_journal_prepare_revoke(struct jbd_journal *journal,
1478 struct jbd_trans *trans)
1480 int rc = EOK, i = 0;
1481 int32_t tag_tbl_size;
1482 uint32_t desc_iblock = 0;
1483 char *blocks_entry = NULL;
1484 struct jbd_revoke_rec *rec, *tmp;
1485 struct ext4_block desc_block;
1486 struct jbd_revoke_header *header = NULL;
1487 int32_t record_len = 4;
1489 if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb,
1490 JBD_FEATURE_INCOMPAT_64BIT))
1493 LIST_FOREACH_SAFE(rec, &trans->revoke_list, revoke_node,
1497 struct jbd_bhdr *bhdr;
1498 desc_iblock = jbd_journal_alloc_block(journal, trans);
1499 rc = jbd_block_get_noread(journal->jbd_fs,
1500 &desc_block, desc_iblock);
1505 ext4_bcache_set_dirty(desc_block.buf);
1507 bhdr = (struct jbd_bhdr *)desc_block.data;
1508 jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
1509 jbd_set32(bhdr, blocktype, JBD_REVOKE_BLOCK);
1510 jbd_set32(bhdr, sequence, trans->trans_id);
1512 header = (struct jbd_revoke_header *)bhdr;
1513 blocks_entry = (char *)(header + 1);
1514 tag_tbl_size = journal->block_size -
1515 sizeof(struct jbd_revoke_header);
1517 if (!trans->start_iblock)
1518 trans->start_iblock = desc_iblock;
1522 if (tag_tbl_size < record_len) {
1523 jbd_set32(header, count,
1524 journal->block_size - tag_tbl_size);
1525 jbd_block_set(journal->jbd_fs, &desc_block);
1530 if (record_len == 8) {
1532 (uint64_t *)blocks_entry;
1533 *blocks = to_be64(rec->lba);
1536 (uint32_t *)blocks_entry;
1537 *blocks = to_be32(rec->lba);
1539 blocks_entry += record_len;
1540 tag_tbl_size -= record_len;
1544 if (rc == EOK && desc_iblock) {
1546 jbd_set32(header, count,
1547 journal->block_size - tag_tbl_size);
1549 jbd_block_set(journal->jbd_fs, &desc_block);
1555 /**@brief Submit the transaction to transaction queue.
1556 * @param journal current journal session
1557 * @param trans transaction*/
1559 jbd_journal_submit_trans(struct jbd_journal *journal,
1560 struct jbd_trans *trans)
1562 TAILQ_INSERT_TAIL(&journal->trans_queue,
1567 /**@brief Put references of block descriptors in a transaction.
1568 * @param journal current journal session
1569 * @param trans transaction*/
1570 void jbd_journal_cp_trans(struct jbd_journal *journal, struct jbd_trans *trans)
1572 struct jbd_buf *jbd_buf, *tmp;
1573 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1574 TAILQ_FOREACH_SAFE(jbd_buf, &trans->buf_queue, buf_node,
1576 struct ext4_block block = jbd_buf->block;
1577 ext4_block_set(fs->bdev, &block);
1581 /**@brief Update the start block of the journal when
1582 * all the contents in a transaction reach the disk.*/
1583 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
1584 struct ext4_buf *buf,
1588 struct jbd_buf *jbd_buf = arg;
1589 struct jbd_trans *trans = jbd_buf->trans;
1590 struct jbd_journal *journal = trans->journal;
1591 bool first_in_queue =
1592 trans == TAILQ_FIRST(&journal->cp_queue);
1596 TAILQ_REMOVE(&trans->buf_queue, jbd_buf, buf_node);
1597 jbd_buf->block_rec->buf = NULL;
1598 jbd_trans_remove_block_rec(journal, jbd_buf);
1601 /* Clear the end_write and end_write_arg fields. */
1602 buf->end_write = NULL;
1603 buf->end_write_arg = NULL;
1605 trans->written_cnt++;
1606 if (trans->written_cnt == trans->data_cnt) {
1607 TAILQ_REMOVE(&journal->cp_queue, trans, trans_node);
1609 if (first_in_queue) {
1610 journal->start = trans->start_iblock +
1611 trans->alloc_blocks;
1612 wrap(&journal->jbd_fs->sb, journal->start);
1613 journal->trans_id = trans->trans_id + 1;
1615 jbd_journal_free_trans(journal, trans, false);
1617 if (first_in_queue) {
1618 while ((trans = TAILQ_FIRST(&journal->cp_queue))) {
1619 if (!trans->data_cnt) {
1620 TAILQ_REMOVE(&journal->cp_queue,
1623 jbd_journal_skip_pure_revoke(journal,
1626 journal->start = trans->start_iblock;
1627 wrap(&journal->jbd_fs->sb, journal->start);
1628 journal->trans_id = trans->trans_id;
1632 jbd_journal_write_sb(journal);
1633 jbd_write_sb(journal->jbd_fs);
1638 /**@brief Commit a transaction to the journal immediately.
1639 * @param journal current journal session
1640 * @param trans transaction
1641 * @return standard error code*/
1642 int jbd_journal_commit_trans(struct jbd_journal *journal,
1643 struct jbd_trans *trans)
1646 uint32_t last = journal->last;
1648 trans->trans_id = journal->alloc_trans_id;
1649 rc = jbd_journal_prepare(journal, trans);
1653 rc = jbd_journal_prepare_revoke(journal, trans);
1657 if (TAILQ_EMPTY(&trans->buf_queue) &&
1658 LIST_EMPTY(&trans->revoke_list)) {
1659 /* Since there are no entries in both buffer list
1660 * and revoke entry list, we do not consider trans as
1661 * complete transaction and just return EOK.*/
1662 jbd_journal_free_trans(journal, trans, false);
1666 rc = jbd_trans_write_commit_block(trans);
1670 journal->alloc_trans_id++;
1671 if (TAILQ_EMPTY(&journal->cp_queue)) {
1672 if (trans->data_cnt) {
1673 journal->start = trans->start_iblock;
1674 wrap(&journal->jbd_fs->sb, journal->start);
1675 journal->trans_id = trans->trans_id;
1676 jbd_journal_write_sb(journal);
1677 jbd_write_sb(journal->jbd_fs);
1678 TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
1680 jbd_journal_cp_trans(journal, trans);
1682 journal->start = trans->start_iblock +
1683 trans->alloc_blocks;
1684 wrap(&journal->jbd_fs->sb, journal->start);
1685 journal->trans_id = trans->trans_id + 1;
1686 jbd_journal_write_sb(journal);
1687 jbd_journal_free_trans(journal, trans, false);
1690 TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
1692 if (trans->data_cnt)
1693 jbd_journal_cp_trans(journal, trans);
1698 journal->last = last;
1699 jbd_journal_free_trans(journal, trans, true);
1704 /**@brief Commit one transaction on transaction queue
1706 * @param journal current journal session.*/
1707 void jbd_journal_commit_one(struct jbd_journal *journal)
1709 struct jbd_trans *trans;
1711 if ((trans = TAILQ_FIRST(&journal->trans_queue))) {
1712 TAILQ_REMOVE(&journal->trans_queue, trans, trans_node);
1713 jbd_journal_commit_trans(journal, trans);
1717 /**@brief Commit all the transactions on transaction queue
1719 * @param journal current journal session.*/
1720 void jbd_journal_commit_all(struct jbd_journal *journal)
1722 while (!TAILQ_EMPTY(&journal->trans_queue)) {
1723 jbd_journal_commit_one(journal);