Change include type policy
[lwext4.git] / src / ext4_journal.c
index 1d924a6668f4f4efbfb86800eeec8faeb4ae5e5a..aa3322b2a9295c0d26fb82437fa0639c410d9e92 100644 (file)
  * @brief Journal handle functions
  */
 
-#include "ext4_config.h"
-#include "ext4_types.h"
-#include "ext4_misc.h"
-#include "ext4_errno.h"
-#include "ext4_debug.h"
-
-#include "ext4_fs.h"
-#include "ext4_super.h"
-#include "ext4_journal.h"
-#include "ext4_blockdev.h"
-#include "ext4_crc32.h"
-#include "ext4_journal.h"
+#include <ext4_config.h>
+#include <ext4_types.h>
+#include <ext4_misc.h>
+#include <ext4_errno.h>
+#include <ext4_debug.h>
+
+#include <ext4_fs.h>
+#include <ext4_super.h>
+#include <ext4_journal.h>
+#include <ext4_blockdev.h>
+#include <ext4_crc32.h>
+#include <ext4_journal.h>
 
 #include <string.h>
 #include <stdlib.h>
@@ -1157,11 +1157,13 @@ static int jbd_iterate_log(struct jbd_fs *jbd_fs,
                        ext4_dbg(DEBUG_JBD, "Commit block: %" PRIu32", "
                                            "trans_id: %" PRIu32"\n",
                                            this_block, this_trans_id);
-                       /* This is the end of a transaction,
+                       /*
+                        * This is the end of a transaction,
                         * we may now proceed to the next transaction.
                         */
                        this_trans_id++;
-                       info->trans_cnt++;
+                       if (action == ACTION_SCAN)
+                               info->trans_cnt++;
                        break;
                case JBD_REVOKE_BLOCK:
                        if (!jbd_verify_meta_csum(jbd_fs, header)) {
@@ -1236,6 +1238,7 @@ int jbd_recover(struct jbd_fs *jbd_fs)
                        ext4_get32(&jbd_fs->inode_ref.fs->sb,
                                   features_incompatible);
                jbd_set32(&jbd_fs->sb, start, 0);
+               jbd_set32(&jbd_fs->sb, sequence, info.last_trans_id);
                features_incompatible &= ~EXT4_FINCOM_RECOVER;
                ext4_set32(&jbd_fs->inode_ref.fs->sb,
                           features_incompatible,
@@ -1267,7 +1270,6 @@ int jbd_journal_start(struct jbd_fs *jbd_fs,
        uint32_t features_incompatible =
                        ext4_get32(&jbd_fs->inode_ref.fs->sb,
                                   features_incompatible);
-       struct ext4_block block = EXT4_BLOCK_ZERO();
        features_incompatible |= EXT4_FINCOM_RECOVER;
        ext4_set32(&jbd_fs->inode_ref.fs->sb,
                        features_incompatible,
@@ -1280,26 +1282,16 @@ int jbd_journal_start(struct jbd_fs *jbd_fs,
        journal->first = jbd_get32(&jbd_fs->sb, first);
        journal->start = journal->first;
        journal->last = journal->first;
-       journal->trans_id = 1;
-       journal->alloc_trans_id = 1;
+       /*
+        * To invalidate any stale records we need to start from
+        * the checkpoint transaction ID of the previous journalling session
+        * plus 1.
+        */
+       journal->trans_id = jbd_get32(&jbd_fs->sb, sequence) + 1;
+       journal->alloc_trans_id = journal->trans_id;
 
        journal->block_size = jbd_get32(&jbd_fs->sb, blocksize);
 
-       r = jbd_block_get_noread(jbd_fs,
-                        &block,
-                        journal->start);
-       if (r != EOK) {
-               memset(journal, 0, sizeof(struct jbd_journal));
-               return r;
-       }
-       memset(block.data, 0, journal->block_size);
-       ext4_bcache_set_dirty(block.buf);
-       r = jbd_block_set(jbd_fs, &block);
-       if (r != EOK) {
-               memset(journal, 0, sizeof(struct jbd_journal));
-               return r;
-       }
-
        TAILQ_INIT(&journal->cp_queue);
        RB_INIT(&journal->block_rec_root);
        journal->jbd_fs = jbd_fs;
@@ -1467,10 +1459,12 @@ static uint32_t jbd_journal_alloc_block(struct jbd_journal *journal,
        trans->alloc_blocks++;
        wrap(&journal->jbd_fs->sb, journal->last);
        
-       /* If there is no space left, flush all journalled
-        * blocks to disk first.*/
-       if (journal->last == journal->start)
-               jbd_journal_purge_cp_trans(journal, true, false);
+       /* If there is no space left, flush just one journalled
+        * transaction.*/
+       if (journal->last == journal->start) {
+               jbd_journal_purge_cp_trans(journal, true, true);
+               ext4_assert(journal->last != journal->start);
+       }
 
        return start_block;
 }
@@ -1773,29 +1767,12 @@ static int jbd_trans_write_commit_block(struct jbd_trans *trans)
        int rc;
        struct ext4_block block;
        struct jbd_commit_header *header;
-       uint32_t commit_iblock, orig_commit_iblock;
+       uint32_t commit_iblock;
        struct jbd_journal *journal = trans->journal;
 
        commit_iblock = jbd_journal_alloc_block(journal, trans);
-       orig_commit_iblock = commit_iblock;
-       commit_iblock++;
-       wrap(&journal->jbd_fs->sb, commit_iblock);
-
-       /* To prevent accidental reference to stale journalling metadata. */
-       if (orig_commit_iblock < commit_iblock) {
-               rc = jbd_block_get_noread(journal->jbd_fs, &block, commit_iblock);
-               if (rc != EOK)
-                       return rc;
-
-               memset(block.data, 0, journal->block_size);
-               ext4_bcache_set_dirty(block.buf);
-               ext4_bcache_set_flag(block.buf, BC_TMP);
-               rc = jbd_block_set(journal->jbd_fs, &block);
-               if (rc != EOK)
-                       return rc;
-       }
 
-       rc = jbd_block_get_noread(journal->jbd_fs, &block, orig_commit_iblock);
+       rc = jbd_block_get_noread(journal->jbd_fs, &block, commit_iblock);
        if (rc != EOK)
                return rc;