ext4_blockdev: a number of changes below
[lwext4.git] / lwext4 / ext4_journal.c
index 61926ae973104fb8253bb3f91d90d1866a3b3312..832922f4d65b649cb4f0102d5c422d840a7696ed 100644 (file)
@@ -1,6 +1,38 @@
+/*
+ * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
+ * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup lwext4
+ * @{
+ */
 /**
  * @file  ext4_journal.c
- * @brief Journalling
+ * @brief Journal handle functions
  */
 
 #include "ext4_config.h"
@@ -14,7 +46,7 @@
 #include "tree.h"
 
 #include <string.h>
-#include <malloc.h>
+#include <stdlib.h>
 
 struct revoke_entry {
        ext4_fsblk_t block;
@@ -118,6 +150,12 @@ int jbd_get_fs(struct ext4_fs *fs,
        if (rc != EOK) {
                memset(jbd_fs, 0, sizeof(struct jbd_fs));
                ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
+               return rc;
+       }
+       if (!jbd_verify_sb(&jbd_fs->sb)) {
+               memset(jbd_fs, 0, sizeof(struct jbd_fs));
+               ext4_fs_put_inode_ref(&jbd_fs->inode_ref);
+               rc = EIO;
        }
 
        return rc;
@@ -137,7 +175,7 @@ int jbd_inode_bmap(struct jbd_fs *jbd_fs,
                   ext4_lblk_t iblock,
                   ext4_fsblk_t *fblock)
 {
-       int rc = ext4_fs_get_inode_data_block_index(
+       int rc = ext4_fs_get_inode_dblk_idx(
                        &jbd_fs->inode_ref,
                        iblock,
                        fblock,
@@ -346,9 +384,6 @@ static void jbd_replay_block_tags(struct jbd_fs *jbd_fs,
        struct ext4_block journal_block, ext4_block;
        struct ext4_fs *fs = jbd_fs->inode_ref.fs;
 
-       ext4_dbg(DEBUG_JBD,
-                "Replaying block in block_tag: %" PRIu64 "\n",
-                block);
        (*this_block)++;
 
        revoke_entry = jbd_revoke_entry_lookup(info, block);
@@ -356,6 +391,10 @@ static void jbd_replay_block_tags(struct jbd_fs *jbd_fs,
            arg->this_trans_id < revoke_entry->trans_id)
                return;
 
+       ext4_dbg(DEBUG_JBD,
+                "Replaying block in block_tag: %" PRIu64 "\n",
+                block);
+
        r = jbd_block_get(jbd_fs, &journal_block, *this_block);
        if (r != EOK)
                return;
@@ -453,7 +492,7 @@ static void jbd_build_revoke_tree(struct jbd_fs *jbd_fs,
                                     JBD_FEATURE_INCOMPAT_64BIT))
                record_len = 8;
 
-       nr_entries = (revoke_hdr->count -
+       nr_entries = (jbd_get32(revoke_hdr, count) -
                        sizeof(struct jbd_revoke_header)) /
                        record_len;
 
@@ -463,11 +502,11 @@ static void jbd_build_revoke_tree(struct jbd_fs *jbd_fs,
                if (record_len == 8) {
                        uint64_t *blocks =
                                (uint64_t *)blocks_entry;
-                       jbd_add_revoke_block_tags(info, *blocks);
+                       jbd_add_revoke_block_tags(info, to_be64(*blocks));
                } else {
                        uint32_t *blocks =
                                (uint32_t *)blocks_entry;
-                       jbd_add_revoke_block_tags(info, *blocks);
+                       jbd_add_revoke_block_tags(info, to_be32(*blocks));
                }
                blocks_entry += record_len;
        }
@@ -547,10 +586,7 @@ int jbd_iterate_log(struct jbd_fs *jbd_fs,
                        ext4_dbg(DEBUG_JBD, "Descriptor block: %" PRIu32", "
                                            "trans_id: %" PRIu32"\n",
                                            this_block, this_trans_id);
-                       if (action == ACTION_SCAN)
-                               jbd_debug_descriptor_block(jbd_fs,
-                                               header, &this_block);
-                       else if (action == ACTION_RECOVER) {
+                       if (action == ACTION_RECOVER) {
                                struct replay_arg replay_arg;
                                replay_arg.info = info;
                                replay_arg.this_block = &this_block;
@@ -558,7 +594,9 @@ int jbd_iterate_log(struct jbd_fs *jbd_fs,
 
                                jbd_replay_descriptor_block(jbd_fs,
                                                header, &replay_arg);
-                       }
+                       } else
+                               jbd_debug_descriptor_block(jbd_fs,
+                                               header, &this_block);
 
                        break;
                case JBD_COMMIT_BLOCK:
@@ -626,3 +664,7 @@ int jbd_recover(struct jbd_fs *jbd_fs)
        jbd_destroy_revoke_tree(&info);
        return r;
 }
+
+/**
+ * @}
+ */