s4:samdb: Make sure value is initialized with 0
[samba.git] / source4 / dsdb / samdb / ldb_modules / partition_metadata.c
index b3b57447ecdd863ff2ea0e8ab8b13f59f30f648a..cf44c5d1218e61d77cf857d639c6f912a6497f7d 100644 (file)
@@ -18,6 +18,7 @@
 */
 
 #include "dsdb/samdb/ldb_modules/partition.h"
+#include "lib/ldb-samba/ldb_wrap.h"
 #include "system/filesys.h"
 
 #define LDB_METADATA_SEQ_NUM   "SEQ_NUM"
@@ -35,6 +36,7 @@ static int partition_metadata_get_uint64(struct ldb_module *module,
        TDB_DATA tdb_key, tdb_data;
        char *value_str;
        TALLOC_CTX *tmp_ctx;
+       int error = 0;
 
        data = talloc_get_type_abort(ldb_module_get_private(module),
                                     struct partition_private_data);
@@ -72,7 +74,11 @@ static int partition_metadata_get_uint64(struct ldb_module *module,
                return ldb_module_oom(module);
        }
 
-       *value = strtoull(value_str, NULL, 10);
+       *value = strtoull_err(value_str, NULL, 10, &error);
+       if (error != 0) {
+               return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
+                                       "partition_metadata: converision failed");
+       }
 
        SAFE_FREE(tdb_data.dptr);
        talloc_free(tmp_ctx);
@@ -147,7 +153,7 @@ int partition_metadata_inc_schema_sequence(struct ldb_module *module)
 {
        struct partition_private_data *data;
        int ret;
-       uint64_t value;
+       uint64_t value = 0;
 
        data = talloc_get_type_abort(ldb_module_get_private(module),
                                    struct partition_private_data);
@@ -185,9 +191,8 @@ static int partition_metadata_open(struct ldb_module *module, bool create)
        TALLOC_CTX *tmp_ctx;
        struct partition_private_data *data;
        struct loadparm_context *lp_ctx;
-       const char *sam_name;
        char *filename, *dirname;
-       int open_flags;
+       int open_flags, tdb_flags, ldb_flags;
        struct stat statbuf;
 
        data = talloc_get_type_abort(ldb_module_get_private(module),
@@ -202,15 +207,10 @@ static int partition_metadata_open(struct ldb_module *module, bool create)
                return ldb_module_oom(module);
        }
 
-       sam_name = (const char *)ldb_get_opaque(ldb, "ldb_url");
-       if (!sam_name) {
-               talloc_free(tmp_ctx);
-               return ldb_operr(ldb);
-       }
-       if (strncmp("tdb://", sam_name, 6) == 0) {
-               sam_name += 6;
-       }
-       filename = talloc_asprintf(tmp_ctx, "%s.d/metadata.tdb", sam_name);
+       filename = ldb_relative_path(ldb,
+                                    tmp_ctx,
+                                    "sam.ldb.d/metadata.tdb");
+
        if (!filename) {
                talloc_free(tmp_ctx);
                return ldb_oom(ldb);
@@ -222,7 +222,9 @@ static int partition_metadata_open(struct ldb_module *module, bool create)
 
                /* While provisioning, sam.ldb.d directory may not exist,
                 * so create it. Ignore errors, if it already exists. */
-               dirname = talloc_asprintf(tmp_ctx, "%s.d", sam_name);
+               dirname = ldb_relative_path(ldb,
+                                           tmp_ctx,
+                                           "sam.ldb.d");
                if (!dirname) {
                        talloc_free(tmp_ctx);
                        return ldb_oom(ldb);
@@ -240,9 +242,17 @@ static int partition_metadata_open(struct ldb_module *module, bool create)
        lp_ctx = talloc_get_type_abort(ldb_get_opaque(ldb, "loadparm"),
                                       struct loadparm_context);
 
-       data->metadata->db = tdb_wrap_open(data->metadata, filename, 10,
-                                             TDB_DEFAULT, open_flags, 0660,
-                                             lp_ctx);
+       tdb_flags = lpcfg_tdb_flags(lp_ctx, TDB_DEFAULT|TDB_SEQNUM);
+
+       ldb_flags = ldb_module_flags(ldb);
+
+       if (ldb_flags & LDB_FLG_NOSYNC) {
+               tdb_flags |= TDB_NOSYNC;
+       }
+
+       data->metadata->db = tdb_wrap_open(
+               data->metadata, filename, 10,
+               tdb_flags, open_flags, 0660);
        if (data->metadata->db == NULL) {
                talloc_free(tmp_ctx);
                if (create) {
@@ -300,7 +310,8 @@ int partition_metadata_init(struct ldb_module *module)
 
        ret = partition_metadata_open(module, false);
        if (ret == LDB_SUCCESS) {
-               goto end;
+               /* Great, we got the DB open */
+               return LDB_SUCCESS;
        }
 
        /* metadata.tdb does not exist, create it */
@@ -314,18 +325,10 @@ int partition_metadata_init(struct ldb_module *module)
                                       "Migrating partition metadata: "
                                       "create of metadata.tdb gave: %s\n",
                                       ldb_errstring(ldb_module_get_ctx(module)));
-               talloc_free(data->metadata);
-               data->metadata = NULL;
-               goto end;
-       }
-
-       ret = partition_metadata_set_sequence_number(module);
-       if (ret != LDB_SUCCESS) {
-               talloc_free(data->metadata);
-               data->metadata = NULL;
+               TALLOC_FREE(data->metadata);
+               return ret;
        }
 
-end:
        return ret;
 }
 
@@ -335,10 +338,35 @@ end:
  */
 int partition_metadata_sequence_number(struct ldb_module *module, uint64_t *value)
 {
-       return partition_metadata_get_uint64(module,
-                                            LDB_METADATA_SEQ_NUM,
-                                            value,
-                                            0);
+
+       /* We have to lock all the databases as otherwise we can
+        * return a sequence number that is higher than the DB values
+        * that we can see, as those transactions close after the
+        * metadata.tdb transaction closes */
+       int ret = partition_read_lock(module);
+       if (ret != LDB_SUCCESS) {
+               return ret;
+       }
+
+       /*
+        * This means we will give a 0 until the first write
+        * tranaction, which is actually pretty reasonable.
+        *
+        * All modern databases will have the metadata.tdb from
+        * the time of the first transaction in provision anyway.
+        */
+       ret = partition_metadata_get_uint64(module,
+                                           LDB_METADATA_SEQ_NUM,
+                                           value,
+                                           0);
+       if (ret == LDB_SUCCESS) {
+               ret = partition_read_unlock(module);
+       } else {
+               /* Don't overwrite the error code */
+               partition_read_unlock(module);
+       }
+       return ret;
+
 }
 
 
@@ -367,6 +395,26 @@ int partition_metadata_sequence_number_increment(struct ldb_module *module, uint
                return ret;
        }
 
+       if (*value == 0) {
+               /*
+                * We are in a transaction now, so we can get the
+                * sequence number from the partitions.
+                */
+               ret = partition_metadata_set_sequence_number(module);
+               if (ret != LDB_SUCCESS) {
+                       TALLOC_FREE(data->metadata);
+                       partition_del_trans(module);
+                       return ret;
+               }
+
+               ret = partition_metadata_get_uint64(module,
+                                                   LDB_METADATA_SEQ_NUM,
+                                                   value, 0);
+               if (ret != LDB_SUCCESS) {
+                       return ret;
+               }
+       }
+
        (*value)++;
        ret = partition_metadata_set_uint64(module, LDB_METADATA_SEQ_NUM, *value, false);
        return ret;