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"
52 /**@brief Revoke entry during journal replay.*/
54 /**@brief Block number not to be replayed.*/
57 /**@brief For any transaction id smaller
58 * than trans_id, records of @block
59 * in those transactions should not
63 /**@brief Revoke tree node.*/
64 RB_ENTRY(revoke_entry) revoke_node;
67 /**@brief Valid journal replay information.*/
69 /**@brief Starting transaction id.*/
70 uint32_t start_trans_id;
72 /**@brief Ending transaction id.*/
73 uint32_t last_trans_id;
75 /**@brief Used as internal argument.*/
76 uint32_t this_trans_id;
78 /**@brief RB-Tree storing revoke entries.*/
79 RB_HEAD(jbd_revoke, revoke_entry) revoke_root;
82 /**@brief Journal replay internal arguments.*/
84 /**@brief Journal replay information.*/
85 struct recover_info *info;
87 /**@brief Current block we are on.*/
90 /**@brief Current trans_id we are on.*/
91 uint32_t this_trans_id;
95 jbd_revoke_entry_cmp(struct revoke_entry *a, struct revoke_entry *b)
97 if (a->block > b->block)
99 else if (a->block < b->block)
104 RB_GENERATE_INTERNAL(jbd_revoke, revoke_entry, revoke_node,
105 jbd_revoke_entry_cmp, static inline)
107 #define jbd_alloc_revoke_entry() calloc(1, sizeof(struct revoke_entry))
108 #define jbd_free_revoke_entry(addr) free(addr)
110 /**@brief Write jbd superblock to disk.
111 * @param jbd_fs jbd filesystem
112 * @param s jbd superblock
113 * @return standard error code*/
114 static int jbd_sb_write(struct jbd_fs *jbd_fs, struct jbd_sb *s)
117 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
120 rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
124 offset = fblock * ext4_sb_get_block_size(&fs->sb);
125 return ext4_block_writebytes(fs->bdev, offset, s,
126 EXT4_SUPERBLOCK_SIZE);
129 /**@brief Read jbd superblock from disk.
130 * @param jbd_fs jbd filesystem
131 * @param s jbd superblock
132 * @return standard error code*/
133 static int jbd_sb_read(struct jbd_fs *jbd_fs, struct jbd_sb *s)
136 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
139 rc = jbd_inode_bmap(jbd_fs, 0, &fblock);
143 offset = fblock * ext4_sb_get_block_size(&fs->sb);
144 return ext4_block_readbytes(fs->bdev, offset, s,
145 EXT4_SUPERBLOCK_SIZE);
148 /**@brief Verify jbd superblock.
149 * @param sb jbd superblock
150 * @return true if jbd superblock is valid */
151 static bool jbd_verify_sb(struct jbd_sb *sb)
153 struct jbd_bhdr *header = &sb->header;
154 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER)
157 if (jbd_get32(header, blocktype) != JBD_SUPERBLOCK &&
158 jbd_get32(header, blocktype) != JBD_SUPERBLOCK_V2)
164 /**@brief Write back dirty jbd superblock to disk.
165 * @param jbd_fs jbd filesystem
166 * @return standard error code*/
167 static int jbd_write_sb(struct jbd_fs *jbd_fs)
171 rc = jbd_sb_write(jbd_fs, &jbd_fs->sb);
175 jbd_fs->dirty = false;
180 /**@brief Get reference to jbd filesystem.
181 * @param fs Filesystem to load journal of
182 * @param jbd_fs jbd filesystem
183 * @return standard error code*/
184 int jbd_get_fs(struct ext4_fs *fs,
185 struct jbd_fs *jbd_fs)
188 uint32_t journal_ino;
190 memset(jbd_fs, 0, sizeof(struct jbd_fs));
191 /* See if there is journal inode on this filesystem.*/
192 /* FIXME: detection on existance ofbkejournal bdev is
194 journal_ino = ext4_get32(&fs->sb, journal_inode_number);
196 rc = ext4_fs_get_inode_ref(fs,
200 memset(jbd_fs, 0, sizeof(struct jbd_fs));
203 rc = jbd_sb_read(jbd_fs, &jbd_fs->sb);
205 memset(jbd_fs, 0, sizeof(struct jbd_fs));
206 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
209 if (!jbd_verify_sb(&jbd_fs->sb)) {
210 memset(jbd_fs, 0, sizeof(struct jbd_fs));
211 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
218 /**@brief Put reference of jbd filesystem.
219 * @param jbd_fs jbd filesystem
220 * @return standard error code*/
221 int jbd_put_fs(struct jbd_fs *jbd_fs)
224 rc = jbd_write_sb(jbd_fs);
226 ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
230 /**@brief Data block lookup helper.
231 * @param jbd_fs jbd filesystem
232 * @param iblock block index
233 * @param fblock logical block address
234 * @return standard error code*/
235 int jbd_inode_bmap(struct jbd_fs *jbd_fs,
237 ext4_fsblk_t *fblock)
239 int rc = ext4_fs_get_inode_dblk_idx(
247 /**@brief jbd block get function (through cache).
248 * @param jbd_fs jbd filesystem
249 * @param block block descriptor
250 * @param fblock jbd logical block address
251 * @return standard error code*/
252 static int jbd_block_get(struct jbd_fs *jbd_fs,
253 struct ext4_block *block,
256 /* TODO: journal device. */
258 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
260 /* Lookup the logical block address of
262 rc = jbd_inode_bmap(jbd_fs, iblock,
267 struct ext4_blockdev *bdev = jbd_fs->inode_ref.fs->bdev;
268 rc = ext4_block_get(bdev, block, fblock);
270 /* If succeeded, mark buffer as BC_FLUSH to indicate
271 * that data should be written to disk immediately.*/
273 ext4_bcache_set_flag(block->buf, BC_FLUSH);
278 /**@brief jbd block get function (through cache, don't read).
279 * @param jbd_fs jbd filesystem
280 * @param block block descriptor
281 * @param fblock jbd logical block address
282 * @return standard error code*/
283 static int jbd_block_get_noread(struct jbd_fs *jbd_fs,
284 struct ext4_block *block,
287 /* TODO: journal device. */
289 ext4_lblk_t iblock = (ext4_lblk_t)fblock;
290 rc = jbd_inode_bmap(jbd_fs, iblock,
295 struct ext4_blockdev *bdev = jbd_fs->inode_ref.fs->bdev;
296 rc = ext4_block_get_noread(bdev, block, fblock);
298 ext4_bcache_set_flag(block->buf, BC_FLUSH);
303 /**@brief jbd block set procedure (through cache).
304 * @param jbd_fs jbd filesystem
305 * @param block block descriptor
306 * @return standard error code*/
307 static int jbd_block_set(struct jbd_fs *jbd_fs,
308 struct ext4_block *block)
310 return ext4_block_set(jbd_fs->inode_ref.fs->bdev,
314 /**@brief helper functions to calculate
315 * block tag size, not including UUID part.
316 * @param jbd_fs jbd filesystem
317 * @return tag size in bytes*/
318 static int jbd_tag_bytes(struct jbd_fs *jbd_fs)
322 /* It is very easy to deal with the case which
323 * JBD_FEATURE_INCOMPAT_CSUM_V3 is enabled.*/
324 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
325 JBD_FEATURE_INCOMPAT_CSUM_V3))
326 return sizeof(struct jbd_block_tag3);
328 size = sizeof(struct jbd_block_tag);
330 /* If JBD_FEATURE_INCOMPAT_CSUM_V2 is enabled,
331 * add 2 bytes to size.*/
332 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
333 JBD_FEATURE_INCOMPAT_CSUM_V2))
334 size += sizeof(uint16_t);
336 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
337 JBD_FEATURE_INCOMPAT_64BIT))
340 /* If block number is 4 bytes in size,
341 * minus 4 bytes from size */
342 return size - sizeof(uint32_t);
345 /**@brief Tag information. */
347 /**@brief Tag size in bytes, including UUID part.*/
350 /**@brief block number stored in this tag.*/
353 /**@brief whether UUID part exists or not.*/
356 /**@brief UUID content if UUID part exists.*/
357 uint8_t uuid[UUID_SIZE];
359 /**@brief Is this the last tag? */
363 /**@brief Extract information from a block tag.
364 * @param __tag pointer to the block tag
365 * @param tag_bytes block tag size of this jbd filesystem
366 * @param remaining size in buffer containing the block tag
367 * @param tag_info information of this tag.
368 * @return EOK when succeed, otherwise return EINVAL.*/
370 jbd_extract_block_tag(struct jbd_fs *jbd_fs,
373 int32_t remain_buf_size,
374 struct tag_info *tag_info)
377 tag_info->tag_bytes = tag_bytes;
378 tag_info->uuid_exist = false;
379 tag_info->last_tag = false;
381 /* See whether it is possible to hold a valid block tag.*/
382 if (remain_buf_size - tag_bytes < 0)
385 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
386 JBD_FEATURE_INCOMPAT_CSUM_V3)) {
387 struct jbd_block_tag3 *tag = __tag;
388 tag_info->block = jbd_get32(tag, blocknr);
389 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
390 JBD_FEATURE_INCOMPAT_64BIT))
392 (uint64_t)jbd_get32(tag, blocknr_high) << 32;
394 if (jbd_get32(tag, flags) & JBD_FLAG_ESCAPE)
397 if (!(jbd_get32(tag, flags) & JBD_FLAG_SAME_UUID)) {
398 /* See whether it is possible to hold UUID part.*/
399 if (remain_buf_size - tag_bytes < UUID_SIZE)
402 uuid_start = (char *)tag + tag_bytes;
403 tag_info->uuid_exist = true;
404 tag_info->tag_bytes += UUID_SIZE;
405 memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
408 if (jbd_get32(tag, flags) & JBD_FLAG_LAST_TAG)
409 tag_info->last_tag = true;
412 struct jbd_block_tag *tag = __tag;
413 tag_info->block = jbd_get32(tag, blocknr);
414 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
415 JBD_FEATURE_INCOMPAT_64BIT))
417 (uint64_t)jbd_get32(tag, blocknr_high) << 32;
419 if (jbd_get16(tag, flags) & JBD_FLAG_ESCAPE)
422 if (!(jbd_get16(tag, flags) & JBD_FLAG_SAME_UUID)) {
423 /* See whether it is possible to hold UUID part.*/
424 if (remain_buf_size - tag_bytes < UUID_SIZE)
427 uuid_start = (char *)tag + tag_bytes;
428 tag_info->uuid_exist = true;
429 tag_info->tag_bytes += UUID_SIZE;
430 memcpy(tag_info->uuid, uuid_start, UUID_SIZE);
433 if (jbd_get16(tag, flags) & JBD_FLAG_LAST_TAG)
434 tag_info->last_tag = true;
440 /**@brief Write information to a block tag.
441 * @param __tag pointer to the block tag
442 * @param remaining size in buffer containing the block tag
443 * @param tag_info information of this tag.
444 * @return EOK when succeed, otherwise return EINVAL.*/
446 jbd_write_block_tag(struct jbd_fs *jbd_fs,
448 int32_t remain_buf_size,
449 struct tag_info *tag_info)
452 int tag_bytes = jbd_tag_bytes(jbd_fs);
454 tag_info->tag_bytes = tag_bytes;
456 /* See whether it is possible to hold a valid block tag.*/
457 if (remain_buf_size - tag_bytes < 0)
460 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
461 JBD_FEATURE_INCOMPAT_CSUM_V3)) {
462 struct jbd_block_tag3 *tag = __tag;
463 jbd_set32(tag, blocknr, tag_info->block);
464 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
465 JBD_FEATURE_INCOMPAT_64BIT))
466 jbd_set32(tag, blocknr_high, tag_info->block >> 32);
468 if (tag_info->uuid_exist) {
469 /* See whether it is possible to hold UUID part.*/
470 if (remain_buf_size - tag_bytes < UUID_SIZE)
473 uuid_start = (char *)tag + tag_bytes;
474 tag_info->tag_bytes += UUID_SIZE;
475 memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
477 jbd_set32(tag, flags,
478 jbd_get32(tag, flags) | JBD_FLAG_SAME_UUID);
480 if (tag_info->last_tag)
481 jbd_set32(tag, flags,
482 jbd_get32(tag, flags) | JBD_FLAG_LAST_TAG);
485 struct jbd_block_tag *tag = __tag;
486 jbd_set32(tag, blocknr, tag_info->block);
487 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
488 JBD_FEATURE_INCOMPAT_64BIT))
489 jbd_set32(tag, blocknr_high, tag_info->block >> 32);
491 if (tag_info->uuid_exist) {
492 /* See whether it is possible to hold UUID part.*/
493 if (remain_buf_size - tag_bytes < UUID_SIZE)
496 uuid_start = (char *)tag + tag_bytes;
497 tag_info->tag_bytes += UUID_SIZE;
498 memcpy(uuid_start, tag_info->uuid, UUID_SIZE);
500 jbd_set16(tag, flags,
501 jbd_get16(tag, flags) | JBD_FLAG_SAME_UUID);
503 if (tag_info->last_tag)
504 jbd_set16(tag, flags,
505 jbd_get16(tag, flags) | JBD_FLAG_LAST_TAG);
511 /**@brief Iterate all block tags in a block.
512 * @param jbd_fs jbd filesystem
513 * @param __tag_start pointer to the block
514 * @param tag_tbl_size size of the block
515 * @param func callback routine to indicate that
516 * a block tag is found
517 * @param arg additional argument to be passed to func */
519 jbd_iterate_block_table(struct jbd_fs *jbd_fs,
521 int32_t tag_tbl_size,
522 void (*func)(struct jbd_fs * jbd_fs,
528 char *tag_start, *tag_ptr;
529 int tag_bytes = jbd_tag_bytes(jbd_fs);
530 tag_start = __tag_start;
533 /* Cut off the size of block tail storing checksum. */
534 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
535 JBD_FEATURE_INCOMPAT_CSUM_V2) ||
536 JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
537 JBD_FEATURE_INCOMPAT_CSUM_V3))
538 tag_tbl_size -= sizeof(struct jbd_block_tail);
540 while (tag_tbl_size) {
541 struct tag_info tag_info;
542 int rc = jbd_extract_block_tag(jbd_fs,
551 func(jbd_fs, tag_info.block, tag_info.uuid, arg);
553 /* Stop the iteration when we reach the last tag. */
554 if (tag_info.last_tag)
557 tag_ptr += tag_info.tag_bytes;
558 tag_tbl_size -= tag_info.tag_bytes;
562 static void jbd_display_block_tags(struct jbd_fs *jbd_fs,
567 uint32_t *iblock = arg;
568 ext4_dbg(DEBUG_JBD, "Block in block_tag: %" PRIu64 "\n", block);
575 static struct revoke_entry *
576 jbd_revoke_entry_lookup(struct recover_info *info, ext4_fsblk_t block)
578 struct revoke_entry tmp = {
582 return RB_FIND(jbd_revoke, &info->revoke_root, &tmp);
585 /**@brief Replay a block in a transaction.
586 * @param jbd_fs jbd filesystem
587 * @param block block address to be replayed.*/
588 static void jbd_replay_block_tags(struct jbd_fs *jbd_fs,
590 uint8_t *uuid __unused,
594 struct replay_arg *arg = __arg;
595 struct recover_info *info = arg->info;
596 uint32_t *this_block = arg->this_block;
597 struct revoke_entry *revoke_entry;
598 struct ext4_block journal_block, ext4_block;
599 struct ext4_fs *fs = jbd_fs->inode_ref.fs;
603 /* We replay this block only if the current transaction id
604 * is equal or greater than that in revoke entry.*/
605 revoke_entry = jbd_revoke_entry_lookup(info, block);
607 arg->this_trans_id < revoke_entry->trans_id)
611 "Replaying block in block_tag: %" PRIu64 "\n",
614 r = jbd_block_get(jbd_fs, &journal_block, *this_block);
618 /* We need special treatment for ext4 superblock. */
620 r = ext4_block_get_noread(fs->bdev, &ext4_block, block);
622 jbd_block_set(jbd_fs, &journal_block);
626 memcpy(ext4_block.data,
628 jbd_get32(&jbd_fs->sb, blocksize));
630 ext4_bcache_set_dirty(ext4_block.buf);
631 ext4_block_set(fs->bdev, &ext4_block);
633 uint16_t mount_count, state;
634 mount_count = ext4_get16(&fs->sb, mount_count);
635 state = ext4_get16(&fs->sb, state);
638 journal_block.data + EXT4_SUPERBLOCK_OFFSET,
639 EXT4_SUPERBLOCK_SIZE);
641 /* Mark system as mounted */
642 ext4_set16(&fs->sb, state, state);
643 r = ext4_sb_write(fs->bdev, &fs->sb);
647 /*Update mount count*/
648 ext4_set16(&fs->sb, mount_count, mount_count);
651 jbd_block_set(jbd_fs, &journal_block);
656 /**@brief Add block address to revoke tree, along with
657 * its transaction id.
658 * @param info journal replay info
659 * @param block block address to be replayed.*/
660 static void jbd_add_revoke_block_tags(struct recover_info *info,
663 struct revoke_entry *revoke_entry;
665 ext4_dbg(DEBUG_JBD, "Add block %" PRIu64 " to revoke tree\n", block);
666 /* If the revoke entry with respect to the block address
667 * exists already, update its transaction id.*/
668 revoke_entry = jbd_revoke_entry_lookup(info, block);
670 revoke_entry->trans_id = info->this_trans_id;
674 revoke_entry = jbd_alloc_revoke_entry();
675 ext4_assert(revoke_entry);
676 revoke_entry->block = block;
677 revoke_entry->trans_id = info->this_trans_id;
678 RB_INSERT(jbd_revoke, &info->revoke_root, revoke_entry);
683 static void jbd_destroy_revoke_tree(struct recover_info *info)
685 while (!RB_EMPTY(&info->revoke_root)) {
686 struct revoke_entry *revoke_entry =
687 RB_MIN(jbd_revoke, &info->revoke_root);
688 ext4_assert(revoke_entry);
689 RB_REMOVE(jbd_revoke, &info->revoke_root, revoke_entry);
690 jbd_free_revoke_entry(revoke_entry);
694 /* Make sure we wrap around the log correctly! */
695 #define wrap(sb, var) \
697 if (var >= jbd_get32((sb), maxlen)) \
698 var -= (jbd_get32((sb), maxlen) - jbd_get32((sb), first)); \
701 #define ACTION_SCAN 0
702 #define ACTION_REVOKE 1
703 #define ACTION_RECOVER 2
705 /**@brief Add entries in a revoke block to revoke tree.
706 * @param jbd_fs jbd filesystem
707 * @param header revoke block header
708 * @param recover_info journal replay info*/
709 static void jbd_build_revoke_tree(struct jbd_fs *jbd_fs,
710 struct jbd_bhdr *header,
711 struct recover_info *info)
714 struct jbd_revoke_header *revoke_hdr =
715 (struct jbd_revoke_header *)header;
716 uint32_t i, nr_entries, record_len = 4;
718 /* If we are working on a 64bit jbd filesystem, */
719 if (JBD_HAS_INCOMPAT_FEATURE(&jbd_fs->sb,
720 JBD_FEATURE_INCOMPAT_64BIT))
723 nr_entries = (jbd_get32(revoke_hdr, count) -
724 sizeof(struct jbd_revoke_header)) /
727 blocks_entry = (char *)(revoke_hdr + 1);
729 for (i = 0;i < nr_entries;i++) {
730 if (record_len == 8) {
732 (uint64_t *)blocks_entry;
733 jbd_add_revoke_block_tags(info, to_be64(*blocks));
736 (uint32_t *)blocks_entry;
737 jbd_add_revoke_block_tags(info, to_be32(*blocks));
739 blocks_entry += record_len;
743 static void jbd_debug_descriptor_block(struct jbd_fs *jbd_fs,
744 struct jbd_bhdr *header,
747 jbd_iterate_block_table(jbd_fs,
749 jbd_get32(&jbd_fs->sb, blocksize) -
750 sizeof(struct jbd_bhdr),
751 jbd_display_block_tags,
755 static void jbd_replay_descriptor_block(struct jbd_fs *jbd_fs,
756 struct jbd_bhdr *header,
757 struct replay_arg *arg)
759 jbd_iterate_block_table(jbd_fs,
761 jbd_get32(&jbd_fs->sb, blocksize) -
762 sizeof(struct jbd_bhdr),
763 jbd_replay_block_tags,
767 /**@brief The core routine of journal replay.
768 * @param jbd_fs jbd filesystem
769 * @param recover_info journal replay info
770 * @param action action needed to be taken
771 * @return standard error code*/
772 static int jbd_iterate_log(struct jbd_fs *jbd_fs,
773 struct recover_info *info,
777 bool log_end = false;
778 struct jbd_sb *sb = &jbd_fs->sb;
779 uint32_t start_trans_id, this_trans_id;
780 uint32_t start_block, this_block;
782 /* We start iterating valid blocks in the whole journal.*/
783 start_trans_id = this_trans_id = jbd_get32(sb, sequence);
784 start_block = this_block = jbd_get32(sb, start);
786 ext4_dbg(DEBUG_JBD, "Start of journal at trans id: %" PRIu32 "\n",
790 struct ext4_block block;
791 struct jbd_bhdr *header;
792 /* If we are not scanning for the last
793 * valid transaction in the journal,
794 * we will stop when we reach the end of
796 if (action != ACTION_SCAN)
797 if (this_trans_id > info->last_trans_id) {
802 r = jbd_block_get(jbd_fs, &block, this_block);
806 header = (struct jbd_bhdr *)block.data;
807 /* This block does not have a valid magic number,
808 * so we have reached the end of the journal.*/
809 if (jbd_get32(header, magic) != JBD_MAGIC_NUMBER) {
810 jbd_block_set(jbd_fs, &block);
815 /* If the transaction id we found is not expected,
816 * we may have reached the end of the journal.
818 * If we are not scanning the journal, something
819 * bad might have taken place. :-( */
820 if (jbd_get32(header, sequence) != this_trans_id) {
821 if (action != ACTION_SCAN)
824 jbd_block_set(jbd_fs, &block);
829 switch (jbd_get32(header, blocktype)) {
830 case JBD_DESCRIPTOR_BLOCK:
831 ext4_dbg(DEBUG_JBD, "Descriptor block: %" PRIu32", "
832 "trans_id: %" PRIu32"\n",
833 this_block, this_trans_id);
834 if (action == ACTION_RECOVER) {
835 struct replay_arg replay_arg;
836 replay_arg.info = info;
837 replay_arg.this_block = &this_block;
838 replay_arg.this_trans_id = this_trans_id;
840 jbd_replay_descriptor_block(jbd_fs,
841 header, &replay_arg);
843 jbd_debug_descriptor_block(jbd_fs,
844 header, &this_block);
847 case JBD_COMMIT_BLOCK:
848 ext4_dbg(DEBUG_JBD, "Commit block: %" PRIu32", "
849 "trans_id: %" PRIu32"\n",
850 this_block, this_trans_id);
851 /* This is the end of a transaction,
852 * we may now proceed to the next transaction.
856 case JBD_REVOKE_BLOCK:
857 ext4_dbg(DEBUG_JBD, "Revoke block: %" PRIu32", "
858 "trans_id: %" PRIu32"\n",
859 this_block, this_trans_id);
860 if (action == ACTION_REVOKE) {
861 info->this_trans_id = this_trans_id;
862 jbd_build_revoke_tree(jbd_fs,
870 jbd_block_set(jbd_fs, &block);
872 wrap(sb, this_block);
873 if (this_block == start_block)
877 ext4_dbg(DEBUG_JBD, "End of journal.\n");
878 if (r == EOK && action == ACTION_SCAN) {
879 /* We have finished scanning the journal. */
880 info->start_trans_id = start_trans_id;
881 if (this_trans_id > start_trans_id)
882 info->last_trans_id = this_trans_id - 1;
884 info->last_trans_id = this_trans_id;
890 /**@brief Replay journal.
891 * @param jbd_fs jbd filesystem
892 * @return standard error code*/
893 int jbd_recover(struct jbd_fs *jbd_fs)
896 struct recover_info info;
897 struct jbd_sb *sb = &jbd_fs->sb;
901 RB_INIT(&info.revoke_root);
903 r = jbd_iterate_log(jbd_fs, &info, ACTION_SCAN);
907 r = jbd_iterate_log(jbd_fs, &info, ACTION_REVOKE);
911 r = jbd_iterate_log(jbd_fs, &info, ACTION_RECOVER);
913 /* If we successfully replay the journal,
914 * clear EXT4_FINCOM_RECOVER flag on the
915 * ext4 superblock, and set the start of
917 uint32_t features_incompatible =
918 ext4_get32(&jbd_fs->inode_ref.fs->sb,
919 features_incompatible);
920 jbd_set32(&jbd_fs->sb, start, 0);
921 features_incompatible &= ~EXT4_FINCOM_RECOVER;
922 ext4_set32(&jbd_fs->inode_ref.fs->sb,
923 features_incompatible,
924 features_incompatible);
925 jbd_fs->dirty = true;
926 r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
927 &jbd_fs->inode_ref.fs->sb);
929 jbd_destroy_revoke_tree(&info);
933 static void jbd_journal_write_sb(struct jbd_journal *journal)
935 struct jbd_fs *jbd_fs = journal->jbd_fs;
936 jbd_set32(&jbd_fs->sb, start, journal->start);
937 jbd_set32(&jbd_fs->sb, sequence, journal->trans_id);
938 jbd_fs->dirty = true;
941 /**@brief Start accessing the journal.
942 * @param jbd_fs jbd filesystem
943 * @param journal current journal session
944 * @return standard error code*/
945 int jbd_journal_start(struct jbd_fs *jbd_fs,
946 struct jbd_journal *journal)
949 uint32_t features_incompatible =
950 ext4_get32(&jbd_fs->inode_ref.fs->sb,
951 features_incompatible);
952 features_incompatible |= EXT4_FINCOM_RECOVER;
953 ext4_set32(&jbd_fs->inode_ref.fs->sb,
954 features_incompatible,
955 features_incompatible);
956 r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
957 &jbd_fs->inode_ref.fs->sb);
961 journal->first = jbd_get32(&jbd_fs->sb, first);
962 journal->start = journal->first;
963 journal->last = journal->first;
964 journal->trans_id = 1;
965 journal->alloc_trans_id = 1;
967 journal->block_size = jbd_get32(&jbd_fs->sb, blocksize);
969 TAILQ_INIT(&journal->trans_queue);
970 TAILQ_INIT(&journal->cp_queue);
971 journal->jbd_fs = jbd_fs;
972 jbd_journal_write_sb(journal);
973 return jbd_write_sb(jbd_fs);
976 /**@brief Stop accessing the journal.
977 * @param journal current journal session
978 * @return standard error code*/
979 int jbd_journal_stop(struct jbd_journal *journal)
982 struct jbd_fs *jbd_fs = journal->jbd_fs;
983 uint32_t features_incompatible;
985 /* Commit all the transactions to the journal.*/
986 jbd_journal_commit_all(journal);
987 /* Make sure that journalled content have reached
989 ext4_block_cache_flush(jbd_fs->inode_ref.fs->bdev);
991 features_incompatible =
992 ext4_get32(&jbd_fs->inode_ref.fs->sb,
993 features_incompatible);
994 features_incompatible &= ~EXT4_FINCOM_RECOVER;
995 ext4_set32(&jbd_fs->inode_ref.fs->sb,
996 features_incompatible,
997 features_incompatible);
998 r = ext4_sb_write(jbd_fs->inode_ref.fs->bdev,
999 &jbd_fs->inode_ref.fs->sb);
1004 journal->trans_id = 0;
1005 jbd_journal_write_sb(journal);
1006 return jbd_write_sb(journal->jbd_fs);
1009 /**@brief Allocate a block in the journal.
1010 * @param journal current journal session
1011 * @param trans transaction
1012 * @return allocated block address*/
1013 static uint32_t jbd_journal_alloc_block(struct jbd_journal *journal,
1014 struct jbd_trans *trans)
1016 uint32_t start_block;
1018 start_block = journal->last++;
1019 trans->alloc_blocks++;
1020 wrap(&journal->jbd_fs->sb, journal->last);
1022 /* If there is no space left, flush all journalled
1023 * blocks to disk first.*/
1024 if (journal->last == journal->start)
1025 ext4_block_cache_flush(journal->jbd_fs->inode_ref.fs->bdev);
1030 /**@brief Allocate a new transaction
1031 * @param journal current journal session
1032 * @return transaction allocated*/
1034 jbd_journal_new_trans(struct jbd_journal *journal)
1036 struct jbd_trans *trans = calloc(1, sizeof(struct jbd_trans));
1040 /* We will assign a trans_id to this transaction,
1041 * once it has been committed.*/
1042 trans->journal = journal;
1047 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
1048 struct ext4_buf *buf __unused,
1052 /**@brief Add block to a transaction
1053 * @param trans transaction
1054 * @param block block descriptor
1055 * @return standard error code*/
1056 int jbd_trans_add_block(struct jbd_trans *trans,
1057 struct ext4_block *block)
1059 struct jbd_buf *buf;
1060 /* We do not need to add those unmodified buffer to
1062 if (!ext4_bcache_test_flag(block->buf, BC_DIRTY))
1065 buf = calloc(1, sizeof(struct jbd_buf));
1070 buf->block = *block;
1071 ext4_bcache_inc_ref(block->buf);
1073 /* If the content reach the disk, notify us
1074 * so that we may do a checkpoint. */
1075 block->buf->end_write = jbd_trans_end_write;
1076 block->buf->end_write_arg = trans;
1079 LIST_INSERT_HEAD(&trans->buf_list, buf, buf_node);
1083 /**@brief Add block to be revoked to a transaction
1084 * @param trans transaction
1085 * @param lba logical block address
1086 * @return standard error code*/
1087 int jbd_trans_revoke_block(struct jbd_trans *trans,
1090 struct jbd_revoke_rec *rec =
1091 calloc(1, sizeof(struct jbd_revoke_rec));
1096 LIST_INSERT_HEAD(&trans->revoke_list, rec, revoke_node);
1100 /**@brief Free a transaction
1101 * @param journal current journal session
1102 * @param trans transaction
1103 * @param abort discard all the modifications on the block?
1104 * @return standard error code*/
1105 void jbd_journal_free_trans(struct jbd_journal *journal,
1106 struct jbd_trans *trans,
1109 struct jbd_buf *jbd_buf, *tmp;
1110 struct jbd_revoke_rec *rec, *tmp2;
1111 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1112 LIST_FOREACH_SAFE(jbd_buf, &trans->buf_list, buf_node,
1115 ext4_bcache_clear_dirty(jbd_buf->block.buf);
1116 ext4_block_set(fs->bdev, &jbd_buf->block);
1119 LIST_REMOVE(jbd_buf, buf_node);
1122 LIST_FOREACH_SAFE(rec, &trans->revoke_list, revoke_node,
1124 LIST_REMOVE(rec, revoke_node);
1131 /**@brief Write commit block for a transaction
1132 * @param trans transaction
1133 * @return standard error code*/
1134 static int jbd_trans_write_commit_block(struct jbd_trans *trans)
1137 struct jbd_commit_header *header;
1138 uint32_t commit_iblock = 0;
1139 struct ext4_block commit_block;
1140 struct jbd_journal *journal = trans->journal;
1142 commit_iblock = jbd_journal_alloc_block(journal, trans);
1143 rc = jbd_block_get_noread(journal->jbd_fs,
1144 &commit_block, commit_iblock);
1148 header = (struct jbd_commit_header *)commit_block.data;
1149 jbd_set32(&header->header, magic, JBD_MAGIC_NUMBER);
1150 jbd_set32(&header->header, blocktype, JBD_COMMIT_BLOCK);
1151 jbd_set32(&header->header, sequence, trans->trans_id);
1153 ext4_bcache_set_dirty(commit_block.buf);
1154 rc = jbd_block_set(journal->jbd_fs, &commit_block);
1161 /**@brief Write descriptor block for a transaction
1162 * @param journal current journal session
1163 * @param trans transaction
1164 * @return standard error code*/
1165 static int jbd_journal_prepare(struct jbd_journal *journal,
1166 struct jbd_trans *trans)
1168 int rc = EOK, i = 0;
1169 int32_t tag_tbl_size;
1170 uint32_t desc_iblock = 0;
1171 uint32_t data_iblock = 0;
1172 char *tag_start = NULL, *tag_ptr = NULL;
1173 struct jbd_buf *jbd_buf;
1174 struct ext4_block desc_block, data_block;
1176 LIST_FOREACH(jbd_buf, &trans->buf_list, buf_node) {
1177 struct tag_info tag_info;
1178 bool uuid_exist = false;
1181 struct jbd_bhdr *bhdr;
1182 desc_iblock = jbd_journal_alloc_block(journal, trans);
1183 rc = jbd_block_get_noread(journal->jbd_fs,
1184 &desc_block, desc_iblock);
1188 ext4_bcache_set_dirty(desc_block.buf);
1190 bhdr = (struct jbd_bhdr *)desc_block.data;
1191 jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
1192 jbd_set32(bhdr, blocktype, JBD_DESCRIPTOR_BLOCK);
1193 jbd_set32(bhdr, sequence, trans->trans_id);
1195 tag_start = (char *)(bhdr + 1);
1196 tag_ptr = tag_start;
1198 tag_tbl_size = journal->block_size -
1199 sizeof(struct jbd_bhdr);
1201 if (!trans->start_iblock)
1202 trans->start_iblock = desc_iblock;
1205 tag_info.block = jbd_buf->block.lb_id;
1206 tag_info.uuid_exist = uuid_exist;
1207 if (i == trans->data_cnt - 1)
1208 tag_info.last_tag = true;
1211 memcpy(tag_info.uuid, journal->jbd_fs->sb.uuid,
1214 rc = jbd_write_block_tag(journal->jbd_fs,
1219 jbd_block_set(journal->jbd_fs, &desc_block);
1224 data_iblock = jbd_journal_alloc_block(journal, trans);
1225 rc = jbd_block_get_noread(journal->jbd_fs,
1226 &data_block, data_iblock);
1230 ext4_bcache_set_dirty(data_block.buf);
1232 memcpy(data_block.data, jbd_buf->block.data,
1233 journal->block_size);
1235 rc = jbd_block_set(journal->jbd_fs, &data_block);
1239 tag_ptr += tag_info.tag_bytes;
1240 tag_tbl_size -= tag_info.tag_bytes;
1244 if (rc == EOK && desc_iblock)
1245 jbd_block_set(journal->jbd_fs, &desc_block);
1250 /**@brief Write revoke block for a transaction
1251 * @param journal current journal session
1252 * @param trans transaction
1253 * @return standard error code*/
1255 jbd_journal_prepare_revoke(struct jbd_journal *journal,
1256 struct jbd_trans *trans)
1258 int rc = EOK, i = 0;
1259 int32_t tag_tbl_size;
1260 uint32_t desc_iblock = 0;
1261 char *blocks_entry = NULL;
1262 struct jbd_revoke_rec *rec, *tmp;
1263 struct ext4_block desc_block;
1264 struct jbd_revoke_header *header = NULL;
1265 int32_t record_len = 4;
1267 if (JBD_HAS_INCOMPAT_FEATURE(&journal->jbd_fs->sb,
1268 JBD_FEATURE_INCOMPAT_64BIT))
1271 LIST_FOREACH_SAFE(rec, &trans->revoke_list, revoke_node,
1275 struct jbd_bhdr *bhdr;
1276 desc_iblock = jbd_journal_alloc_block(journal, trans);
1277 rc = jbd_block_get_noread(journal->jbd_fs,
1278 &desc_block, desc_iblock);
1283 ext4_bcache_set_dirty(desc_block.buf);
1285 bhdr = (struct jbd_bhdr *)desc_block.data;
1286 jbd_set32(bhdr, magic, JBD_MAGIC_NUMBER);
1287 jbd_set32(bhdr, blocktype, JBD_REVOKE_BLOCK);
1288 jbd_set32(bhdr, sequence, trans->trans_id);
1290 header = (struct jbd_revoke_header *)bhdr;
1291 blocks_entry = (char *)(header + 1);
1292 tag_tbl_size = journal->block_size -
1293 sizeof(struct jbd_revoke_header);
1295 if (!trans->start_iblock)
1296 trans->start_iblock = desc_iblock;
1300 if (tag_tbl_size < record_len) {
1301 jbd_set32(header, count,
1302 journal->block_size - tag_tbl_size);
1303 jbd_block_set(journal->jbd_fs, &desc_block);
1308 if (record_len == 8) {
1310 (uint64_t *)blocks_entry;
1311 *blocks = to_be64(rec->lba);
1314 (uint32_t *)blocks_entry;
1315 *blocks = to_be32(rec->lba);
1317 blocks_entry += record_len;
1318 tag_tbl_size -= record_len;
1322 if (rc == EOK && desc_iblock) {
1324 jbd_set32(header, count,
1325 journal->block_size - tag_tbl_size);
1327 jbd_block_set(journal->jbd_fs, &desc_block);
1333 /**@brief Submit the transaction to transaction queue.
1334 * @param journal current journal session
1335 * @param trans transaction*/
1337 jbd_journal_submit_trans(struct jbd_journal *journal,
1338 struct jbd_trans *trans)
1340 TAILQ_INSERT_TAIL(&journal->trans_queue,
1345 /**@brief Put references of block descriptors in a transaction.
1346 * @param journal current journal session
1347 * @param trans transaction*/
1348 void jbd_journal_cp_trans(struct jbd_journal *journal, struct jbd_trans *trans)
1350 struct jbd_buf *jbd_buf, *tmp;
1351 struct ext4_fs *fs = journal->jbd_fs->inode_ref.fs;
1352 LIST_FOREACH_SAFE(jbd_buf, &trans->buf_list, buf_node,
1354 struct ext4_block block = jbd_buf->block;
1355 ext4_block_set(fs->bdev, &block);
1359 /**@brief Update the start block of the journal when
1360 * all the contents in a transaction reach the disk.*/
1361 static void jbd_trans_end_write(struct ext4_bcache *bc __unused,
1362 struct ext4_buf *buf __unused,
1366 struct jbd_trans *trans = arg;
1367 struct jbd_journal *journal = trans->journal;
1368 bool first_in_queue =
1369 trans == TAILQ_FIRST(&journal->cp_queue);
1373 trans->written_cnt++;
1374 if (trans->written_cnt == trans->data_cnt) {
1375 TAILQ_REMOVE(&journal->cp_queue, trans, trans_node);
1377 if (first_in_queue) {
1378 journal->start = trans->start_iblock +
1379 trans->alloc_blocks;
1380 wrap(&journal->jbd_fs->sb, journal->start);
1381 journal->trans_id = trans->trans_id + 1;
1383 jbd_journal_free_trans(journal, trans, false);
1385 if (first_in_queue) {
1386 while ((trans = TAILQ_FIRST(&journal->cp_queue))) {
1387 if (!trans->data_cnt) {
1388 TAILQ_REMOVE(&journal->cp_queue,
1391 journal->start = trans->start_iblock +
1392 trans->alloc_blocks;
1393 wrap(&journal->jbd_fs->sb, journal->start);
1394 journal->trans_id = trans->trans_id + 1;
1395 jbd_journal_free_trans(journal,
1398 journal->start = trans->start_iblock;
1399 wrap(&journal->jbd_fs->sb, journal->start);
1400 journal->trans_id = trans->trans_id;
1404 jbd_journal_write_sb(journal);
1405 jbd_write_sb(journal->jbd_fs);
1410 /**@brief Commit a transaction to the journal immediately.
1411 * @param journal current journal session
1412 * @param trans transaction
1413 * @return standard error code*/
1414 int jbd_journal_commit_trans(struct jbd_journal *journal,
1415 struct jbd_trans *trans)
1418 uint32_t last = journal->last;
1420 trans->trans_id = journal->alloc_trans_id;
1421 rc = jbd_journal_prepare(journal, trans);
1425 rc = jbd_journal_prepare_revoke(journal, trans);
1429 rc = jbd_trans_write_commit_block(trans);
1433 journal->alloc_trans_id++;
1434 if (TAILQ_EMPTY(&journal->cp_queue)) {
1435 if (trans->data_cnt) {
1436 journal->start = trans->start_iblock;
1437 wrap(&journal->jbd_fs->sb, journal->start);
1438 journal->trans_id = trans->trans_id;
1439 jbd_journal_write_sb(journal);
1440 jbd_write_sb(journal->jbd_fs);
1441 TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
1443 jbd_journal_cp_trans(journal, trans);
1445 journal->start = trans->start_iblock +
1446 trans->alloc_blocks;
1447 wrap(&journal->jbd_fs->sb, journal->start);
1448 journal->trans_id = trans->trans_id + 1;
1449 jbd_journal_write_sb(journal);
1450 jbd_journal_free_trans(journal, trans, false);
1453 TAILQ_INSERT_TAIL(&journal->cp_queue, trans,
1455 if (trans->data_cnt)
1456 jbd_journal_cp_trans(journal, trans);
1461 journal->last = last;
1462 jbd_journal_free_trans(journal, trans, true);
1467 /**@brief Commit one transaction on transaction queue
1469 * @param journal current journal session.*/
1470 void jbd_journal_commit_one(struct jbd_journal *journal)
1472 struct jbd_trans *trans;
1474 if ((trans = TAILQ_FIRST(&journal->trans_queue))) {
1475 TAILQ_REMOVE(&journal->trans_queue, trans, trans_node);
1476 jbd_journal_commit_trans(journal, trans);
1480 /**@brief Commit all the transactions on transaction queue
1482 * @param journal current journal session.*/
1483 void jbd_journal_commit_all(struct jbd_journal *journal)
1485 while (!TAILQ_EMPTY(&journal->trans_queue)) {
1486 jbd_journal_commit_one(journal);