[PATCH] LDB Rename and extract the key value code from the TDB specific code.

Gary Lockyer gary at catalyst.net.nz
Tue Jul 24 21:53:20 UTC 2018


As promised after the commit of the lmdb code, a series of patches
renaming and separating the key value code in lib/ldb/ldb_tdb from the
tdb specific code. Moving the generic key value code to
lib/ldb/ldb_key_value, leaving the TDB specific code in lib/ldb/ldb_tdb.

The patch set consists of a series of rename commits, each followed by a
reformat commit.

After the renames the key value code is moved to ldb_key_value and
reformatted.

CI currently running:
 https://gitlab.com/catalyst-samba/samba/pipelines/26428309

Comments and or review appreciated.

Thanks
Gary.
-------------- next part --------------
From 29bb89edf4ba86fb4710d2599b5a03b8f340ea8c Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 07:23:10 +1200
Subject: [PATCH 01/18] lib ldb: Rename functions to ldb_kv

Rename the ldb key value functions from ltdb_* to ldb_kv_*. The renaming
is preparation for the separation of the tdb specific code from the key
value code.  This work is a follow on from the addition of the lmdb
backend.

Note that the next commit tidies up the code formatting.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_mdb/ldb_mdb.c    |   3 +-
 lib/ldb/ldb_tdb/ldb_cache.c  |  88 ++++++------
 lib/ldb/ldb_tdb/ldb_index.c  | 267 ++++++++++++++++++------------------
 lib/ldb/ldb_tdb/ldb_search.c |  44 +++---
 lib/ldb/ldb_tdb/ldb_tdb.c    | 318 ++++++++++++++++++++++---------------------
 lib/ldb/ldb_tdb/ldb_tdb.h    |  62 ++++-----
 6 files changed, 397 insertions(+), 385 deletions(-)

diff --git a/lib/ldb/ldb_mdb/ldb_mdb.c b/lib/ldb/ldb_mdb/ldb_mdb.c
index af552fe..74b598d 100644
--- a/lib/ldb/ldb_mdb/ldb_mdb.c
+++ b/lib/ldb/ldb_mdb/ldb_mdb.c
@@ -883,5 +883,6 @@ int lmdb_connect(struct ldb_context *ldb,
 	 */
 	ltdb->max_key_length = LDB_MDB_MAX_KEY_LENGTH;
 
-	return init_store(ltdb, "ldb_mdb backend", ldb, options, _module);
+	return ldb_kv_init_store(
+	    ltdb, "ldb_mdb backend", ldb, options, _module);
 }
diff --git a/lib/ldb/ldb_tdb/ldb_cache.c b/lib/ldb/ldb_tdb/ldb_cache.c
index 1856fb1..e9da45d 100644
--- a/lib/ldb/ldb_tdb/ldb_cache.c
+++ b/lib/ldb/ldb_tdb/ldb_cache.c
@@ -42,7 +42,7 @@
 static const struct {
 	const char *name;
 	int value;
-} ltdb_valid_attr_flags[] = {
+} ldb_kv_valid_attr_flags[] = {
 	{ "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
 	{ "INTEGER", LTDB_FLAG_INTEGER },
 	{ "HIDDEN", 0 },
@@ -51,11 +51,10 @@ static const struct {
 	{ NULL, 0 }
 };
 
-
 /*
   de-register any special handlers for @ATTRIBUTES
 */
-static void ltdb_attributes_unload(struct ldb_module *module)
+static void ldb_kv_attributes_unload(struct ldb_module *module)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 
@@ -66,20 +65,20 @@ static void ltdb_attributes_unload(struct ldb_module *module)
 /*
   add up the attrib flags for a @ATTRIBUTES element
 */
-static int ltdb_attributes_flags(struct ldb_message_element *el, unsigned *v)
+static int ldb_kv_attributes_flags(struct ldb_message_element *el, unsigned *v)
 {
 	unsigned int i;
 	unsigned value = 0;
 	for (i=0;i<el->num_values;i++) {
 		unsigned int j;
-		for (j=0;ltdb_valid_attr_flags[j].name;j++) {
-			if (strcmp(ltdb_valid_attr_flags[j].name, 
+		for (j = 0; ldb_kv_valid_attr_flags[j].name; j++) {
+			if (strcmp(ldb_kv_valid_attr_flags[j].name,
 				   (char *)el->values[i].data) == 0) {
-				value |= ltdb_valid_attr_flags[j].value;
+				value |= ldb_kv_valid_attr_flags[j].value;
 				break;
 			}
 		}
-		if (ltdb_valid_attr_flags[j].name == NULL) {
+		if (ldb_kv_valid_attr_flags[j].name == NULL) {
 			return -1;
 		}
 	}
@@ -97,7 +96,7 @@ static int ldb_schema_attribute_compare(const void *p1, const void *p2)
 /*
   register any special handlers from @ATTRIBUTES
 */
-static int ltdb_attributes_load(struct ldb_module *module)
+static int ldb_kv_attributes_load(struct ldb_module *module)
 {
 	struct ldb_schema_attribute *attrs;
 	struct ldb_context *ldb;
@@ -123,10 +122,12 @@ static int ltdb_attributes_load(struct ldb_module *module)
 	dn = ldb_dn_new(module, ldb, LTDB_ATTRIBUTES);
 	if (dn == NULL) goto failed;
 
-	r = ltdb_search_dn1(module, dn, attrs_msg,
-			    LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
-			    |LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
-			    |LDB_UNPACK_DATA_FLAG_NO_DN);
+	r = ldb_kv_search_dn1(module,
+			      dn,
+			      attrs_msg,
+			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
+				  LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
+				  LDB_UNPACK_DATA_FLAG_NO_DN);
 	talloc_free(dn);
 	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
 		goto failed;
@@ -162,7 +163,8 @@ static int ltdb_attributes_load(struct ldb_module *module)
 			continue;
 		}
 
-		if (ltdb_attributes_flags(&attrs_msg->elements[i], &flags) != 0) {
+		if (ldb_kv_attributes_flags(&attrs_msg->elements[i], &flags) !=
+		    0) {
 			ldb_debug(ldb, LDB_DEBUG_ERROR,
 				  "Invalid @ATTRIBUTES element for '%s'",
 				  attrs_msg->elements[i].name);
@@ -233,8 +235,8 @@ failed:
 /*
   register any index records we find for the DB
 */
-static int ltdb_index_load(struct ldb_module *module,
-			   struct ltdb_private *ltdb)
+static int ldb_kv_index_load(struct ldb_module *module,
+			     struct ltdb_private *ltdb)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	struct ldb_dn *indexlist_dn;
@@ -268,10 +270,12 @@ static int ltdb_index_load(struct ldb_module *module,
 		return -1;
 	}
 
-	r = ltdb_search_dn1(module, indexlist_dn, ltdb->cache->indexlist,
-			    LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
-			    |LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
-			    |LDB_UNPACK_DATA_FLAG_NO_DN);
+	r = ldb_kv_search_dn1(module,
+			      indexlist_dn,
+			      ltdb->cache->indexlist,
+			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
+				  LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
+				  LDB_UNPACK_DATA_FLAG_NO_DN);
 	TALLOC_FREE(indexlist_dn);
 
 	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
@@ -311,7 +315,7 @@ static int ltdb_index_load(struct ldb_module *module,
 /*
   initialise the baseinfo record
 */
-static int ltdb_baseinfo_init(struct ldb_module *module)
+static int ldb_kv_baseinfo_init(struct ldb_module *module)
 {
 	struct ldb_context *ldb;
 	void *data = ldb_module_get_private(module);
@@ -352,8 +356,8 @@ static int ltdb_baseinfo_init(struct ldb_module *module)
 		goto failed;
 	}
 	val.length = 1;
-	
-	ret = ltdb_store(module, msg, TDB_INSERT);
+
+	ret = ldb_kv_store(module, msg, TDB_INSERT);
 
 	talloc_free(msg);
 
@@ -368,7 +372,7 @@ failed:
 /*
   free any cache records
  */
-static void ltdb_cache_free(struct ldb_module *module)
+static void ldb_kv_cache_free(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
@@ -381,17 +385,17 @@ static void ltdb_cache_free(struct ldb_module *module)
 /*
   force a cache reload
 */
-int ltdb_cache_reload(struct ldb_module *module)
+int ldb_kv_cache_reload(struct ldb_module *module)
 {
-	ltdb_attributes_unload(module);
-	ltdb_cache_free(module);
-	return ltdb_cache_load(module);
+	ldb_kv_attributes_unload(module);
+	ldb_kv_cache_free(module);
+	return ldb_kv_cache_load(module);
 }
 
 /*
   load the cache records
 */
-int ltdb_cache_load(struct ldb_module *module)
+int ldb_kv_cache_load(struct ldb_module *module)
 {
 	struct ldb_context *ldb;
 	void *data = ldb_module_get_private(module);
@@ -425,7 +429,7 @@ int ltdb_cache_load(struct ldb_module *module)
 	if (r != LDB_SUCCESS) {
 		goto failed;
 	}
-	r= ltdb_search_dn1(module, baseinfo_dn, baseinfo, 0);
+	r = ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0);
 	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
 		goto failed_and_unlock;
 	}
@@ -447,9 +451,10 @@ int ltdb_cache_load(struct ldb_module *module)
 
 		/* error handling for ltdb_baseinfo_init() is by
 		   looking for the record again. */
-		ltdb_baseinfo_init(module);
+		ldb_kv_baseinfo_init(module);
 
-		if (ltdb_search_dn1(module, baseinfo_dn, baseinfo, 0) != LDB_SUCCESS) {
+		if (ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0) !=
+		    LDB_SUCCESS) {
 			goto failed_and_unlock;
 		}
 
@@ -474,7 +479,7 @@ int ltdb_cache_load(struct ldb_module *module)
 	options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
 	if (options_dn == NULL) goto failed_and_unlock;
 
-	r= ltdb_search_dn1(module, options_dn, options, 0);
+	r = ldb_kv_search_dn1(module, options_dn, options, 0);
 	talloc_free(options_dn);
 	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
 		goto failed_and_unlock;
@@ -501,9 +506,9 @@ int ltdb_cache_load(struct ldb_module *module)
 	 * the handlers across all databases when used under Samba's
 	 * partition module.
 	 */
-	ltdb_attributes_unload(module);
+	ldb_kv_attributes_unload(module);
 
-	if (ltdb_index_load(module, ltdb) == -1) {
+	if (ldb_kv_index_load(module, ltdb) == -1) {
 		goto failed_and_unlock;
 	}
 
@@ -512,7 +517,7 @@ int ltdb_cache_load(struct ldb_module *module)
 	 * the handlers across all databases when used under Samba's
 	 * partition module.
 	 */
-	if (ltdb_attributes_load(module) == -1) {
+	if (ldb_kv_attributes_load(module) == -1) {
 		goto failed_and_unlock;
 	}
 
@@ -557,7 +562,7 @@ failed:
 /*
   increase the sequence number to indicate a database change
 */
-int ltdb_increase_sequence_number(struct ldb_module *module)
+int ldb_kv_increase_sequence_number(struct ldb_module *module)
 {
 	struct ldb_context *ldb;
 	void *data = ldb_module_get_private(module);
@@ -624,7 +629,7 @@ int ltdb_increase_sequence_number(struct ldb_module *module)
 	val_time.data = (uint8_t *)s;
 	val_time.length = strlen(s);
 
-	ret = ltdb_modify_internal(module, msg, NULL);
+	ret = ldb_kv_modify_internal(module, msg, NULL);
 
 	talloc_free(msg);
 
@@ -639,12 +644,13 @@ int ltdb_increase_sequence_number(struct ldb_module *module)
 	return ret;
 }
 
-int ltdb_check_at_attributes_values(const struct ldb_val *value)
+int ldb_kv_check_at_attributes_values(const struct ldb_val *value)
 {
 	unsigned int i;
 
-	for (i = 0; ltdb_valid_attr_flags[i].name != NULL; i++) {
-		if ((strcmp(ltdb_valid_attr_flags[i].name, (char *)value->data) == 0)) {
+	for (i = 0; ldb_kv_valid_attr_flags[i].name != NULL; i++) {
+		if ((strcmp(ldb_kv_valid_attr_flags[i].name,
+			    (char *)value->data) == 0)) {
 			return 0;
 		}
 	}
diff --git a/lib/ldb/ldb_tdb/ldb_index.c b/lib/ldb/ldb_tdb/ldb_index.c
index fb60612..75073a1 100644
--- a/lib/ldb/ldb_tdb/ldb_index.c
+++ b/lib/ldb/ldb_tdb/ldb_index.c
@@ -170,17 +170,17 @@ enum key_truncation {
 	KEY_TRUNCATED,
 };
 
-static int ltdb_write_index_dn_guid(struct ldb_module *module,
-				    const struct ldb_message *msg,
-				    int add);
-static int ltdb_index_dn_base_dn(struct ldb_module *module,
-				 struct ltdb_private *ltdb,
-				 struct ldb_dn *base_dn,
-				 struct dn_list *dn_list,
-				 enum key_truncation *truncation);
+static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
+				      const struct ldb_message *msg,
+				      int add);
+static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
+				   struct ltdb_private *ltdb,
+				   struct ldb_dn *base_dn,
+				   struct dn_list *dn_list,
+				   enum key_truncation *truncation);
 
-static void ltdb_dn_list_sort(struct ltdb_private *ltdb,
-			      struct dn_list *list);
+static void ldb_kv_dn_list_sort(struct ltdb_private *ltdb,
+				struct dn_list *list);
 
 /* we put a @IDXVERSION attribute on index entries. This
    allows us to tell if it was written by an older version
@@ -189,7 +189,8 @@ static void ltdb_dn_list_sort(struct ltdb_private *ltdb,
 
 #define LTDB_GUID_INDEXING_VERSION 3
 
-static unsigned ltdb_max_key_length(struct ltdb_private *ltdb) {
+static unsigned ldb_kv_max_key_length(struct ltdb_private *ltdb)
+{
 	if (ltdb->max_key_length == 0){
 		return UINT_MAX;
 	}
@@ -197,7 +198,7 @@ static unsigned ltdb_max_key_length(struct ltdb_private *ltdb) {
 }
 
 /* enable the idxptr mode when transactions start */
-int ltdb_index_transaction_start(struct ldb_module *module)
+int ldb_kv_index_transaction_start(struct ldb_module *module)
 {
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
 	ltdb->idxptr = talloc_zero(ltdb, struct ltdb_idxptr);
@@ -247,9 +248,9 @@ static int ldb_val_equal_exact_ordered(const struct ldb_val v1,
 
   This is therefore safe when the value is a GUID in the future
  */
-static int ltdb_dn_list_find_val(struct ltdb_private *ltdb,
-				 const struct dn_list *list,
-				 const struct ldb_val *v)
+static int ldb_kv_dn_list_find_val(struct ltdb_private *ltdb,
+				   const struct dn_list *list,
+				   const struct ldb_val *v)
 {
 	unsigned int i;
 	struct ldb_val *exact = NULL, *next = NULL;
@@ -282,9 +283,9 @@ static int ltdb_dn_list_find_val(struct ltdb_private *ltdb,
   find a entry in a dn_list. Uses a case sensitive comparison with the dn
   returns -1 if not found
  */
-static int ltdb_dn_list_find_msg(struct ltdb_private *ltdb,
-				 struct dn_list *list,
-				 const struct ldb_message *msg)
+static int ldb_kv_dn_list_find_msg(struct ltdb_private *ltdb,
+				   struct dn_list *list,
+				   const struct ldb_message *msg)
 {
 	struct ldb_val v;
 	const struct ldb_val *key_val;
@@ -300,7 +301,7 @@ static int ltdb_dn_list_find_msg(struct ltdb_private *ltdb,
 		}
 		v = *key_val;
 	}
-	return ltdb_dn_list_find_val(ltdb, list, &v);
+	return ldb_kv_dn_list_find_val(ltdb, list, &v);
 }
 
 /*
@@ -308,7 +309,7 @@ static int ltdb_dn_list_find_msg(struct ltdb_private *ltdb,
   checks and also copes with CPUs that are fussy about pointer
   alignment
  */
-static struct dn_list *ltdb_index_idxptr(struct ldb_module *module, TDB_DATA rec, bool check_parent)
+static struct dn_list *ldb_kv_index_idxptr(struct ldb_module *module, TDB_DATA rec, bool check_parent)
 {
 	struct dn_list *list;
 	if (rec.dsize != sizeof(void *)) {
@@ -340,7 +341,7 @@ static struct dn_list *ltdb_index_idxptr(struct ldb_module *module, TDB_DATA rec
   return the @IDX list in an index entry for a dn as a
   struct dn_list
  */
-static int ltdb_dn_list_load(struct ldb_module *module,
+static int ldb_kv_dn_list_load(struct ldb_module *module,
 			     struct ltdb_private *ltdb,
 			     struct ldb_dn *dn, struct dn_list *list)
 {
@@ -369,7 +370,7 @@ static int ltdb_dn_list_load(struct ldb_module *module,
 	}
 
 	/* we've found an in-memory index entry */
-	list2 = ltdb_index_idxptr(module, rec, true);
+	list2 = ldb_kv_index_idxptr(module, rec, true);
 	if (list2 == NULL) {
 		free(rec.dptr);
 		return LDB_ERR_OPERATIONS_ERROR;
@@ -385,7 +386,7 @@ normal_index:
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ltdb_search_dn1(module, dn, msg,
+	ret = ldb_kv_search_dn1(module, dn, msg,
 			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
 			      |LDB_UNPACK_DATA_FLAG_NO_DN);
 	if (ret != LDB_SUCCESS) {
@@ -472,7 +473,7 @@ normal_index:
 	return LDB_SUCCESS;
 }
 
-int ltdb_key_dn_from_idx(struct ldb_module *module,
+int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 			 struct ltdb_private *ltdb,
 			 TALLOC_CTX *mem_ctx,
 			 struct ldb_dn *dn,
@@ -489,7 +490,7 @@ int ltdb_key_dn_from_idx(struct ldb_module *module,
 	}
 
 
-	ret = ltdb_index_dn_base_dn(module, ltdb, dn, list, &truncation);
+	ret = ldb_kv_index_dn_base_dn(module, ltdb, dn, list, &truncation);
 	if (ret != LDB_SUCCESS) {
 		TALLOC_FREE(list);
 		return ret;
@@ -533,7 +534,7 @@ int ltdb_key_dn_from_idx(struct ldb_module *module,
 				return LDB_ERR_OPERATIONS_ERROR;
 			}
 
-			ret = ltdb_idx_to_key(module, ltdb,
+			ret = ldb_kv_idx_to_key(module, ltdb,
 					      ldb, &list->dn[i],
 					      &key);
 			if (ret != LDB_SUCCESS) {
@@ -542,7 +543,7 @@ int ltdb_key_dn_from_idx(struct ldb_module *module,
 				return ret;
 			}
 
-			ret = ltdb_search_key(module, ltdb, key,
+			ret = ldb_kv_search_key(module, ltdb, key,
 					      rec, flags);
 			if (key.dptr != guid_key) {
 				TALLOC_FREE(key.dptr);
@@ -587,7 +588,7 @@ int ltdb_key_dn_from_idx(struct ldb_module *module,
 	}
 
 	/* The tdb_key memory is allocated by the caller */
-	ret = ltdb_guid_to_key(module, ltdb,
+	ret = ldb_kv_guid_to_key(module, ltdb,
 			       &list->dn[index], tdb_key);
 	TALLOC_FREE(list);
 
@@ -603,7 +604,7 @@ int ltdb_key_dn_from_idx(struct ldb_module *module,
 /*
   save a dn_list into a full @IDX style record
  */
-static int ltdb_dn_list_store_full(struct ldb_module *module,
+static int ldb_kv_dn_list_store_full(struct ldb_module *module,
 				   struct ltdb_private *ltdb,
 				   struct ldb_dn *dn,
 				   struct dn_list *list)
@@ -619,7 +620,7 @@ static int ltdb_dn_list_store_full(struct ldb_module *module,
 	msg->dn = dn;
 
 	if (list->count == 0) {
-		ret = ltdb_delete_noindex(module, msg);
+		ret = ldb_kv_delete_noindex(module, msg);
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 			ret = LDB_SUCCESS;
 		}
@@ -690,7 +691,7 @@ static int ltdb_dn_list_store_full(struct ldb_module *module,
 		}
 	}
 
-	ret = ltdb_store(module, msg, TDB_REPLACE);
+	ret = ldb_kv_store(module, msg, TDB_REPLACE);
 	talloc_free(msg);
 	return ret;
 }
@@ -698,7 +699,7 @@ static int ltdb_dn_list_store_full(struct ldb_module *module,
 /*
   save a dn_list into the database, in either @IDX or internal format
  */
-static int ltdb_dn_list_store(struct ldb_module *module, struct ldb_dn *dn,
+static int ldb_kv_dn_list_store(struct ldb_module *module, struct ldb_dn *dn,
 			      struct dn_list *list)
 {
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
@@ -707,7 +708,7 @@ static int ltdb_dn_list_store(struct ldb_module *module, struct ldb_dn *dn,
 	struct dn_list *list2;
 
 	if (ltdb->idxptr == NULL) {
-		return ltdb_dn_list_store_full(module, ltdb,
+		return ldb_kv_dn_list_store_full(module, ltdb,
 					       dn, list);
 	}
 
@@ -726,7 +727,7 @@ static int ltdb_dn_list_store(struct ldb_module *module, struct ldb_dn *dn,
 
 	rec = tdb_fetch(ltdb->idxptr->itdb, key);
 	if (rec.dptr != NULL) {
-		list2 = ltdb_index_idxptr(module, rec, false);
+		list2 = ldb_kv_index_idxptr(module, rec, false);
 		if (list2 == NULL) {
 			free(rec.dptr);
 			return LDB_ERR_OPERATIONS_ERROR;
@@ -762,7 +763,7 @@ static int ltdb_dn_list_store(struct ldb_module *module, struct ldb_dn *dn,
 /*
   traverse function for storing the in-memory index entries on disk
  */
-static int ltdb_index_traverse_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
+static int ldb_kv_index_traverse_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
 {
 	struct ldb_module *module = state;
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
@@ -771,7 +772,7 @@ static int ltdb_index_traverse_store(struct tdb_context *tdb, TDB_DATA key, TDB_
 	struct ldb_val v;
 	struct dn_list *list;
 
-	list = ltdb_index_idxptr(module, data, true);
+	list = ldb_kv_index_idxptr(module, data, true);
 	if (list == NULL) {
 		ltdb->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
 		return -1;
@@ -787,7 +788,7 @@ static int ltdb_index_traverse_store(struct tdb_context *tdb, TDB_DATA key, TDB_
 		return -1;
 	}
 
-	ltdb->idxptr->error = ltdb_dn_list_store_full(module, ltdb,
+	ltdb->idxptr->error = ldb_kv_dn_list_store_full(module, ltdb,
 						      dn, list);
 	talloc_free(dn);
 	if (ltdb->idxptr->error != 0) {
@@ -797,7 +798,7 @@ static int ltdb_index_traverse_store(struct tdb_context *tdb, TDB_DATA key, TDB_
 }
 
 /* cleanup the idxptr mode when transaction commits */
-int ltdb_index_transaction_commit(struct ldb_module *module)
+int ldb_kv_index_transaction_commit(struct ldb_module *module)
 {
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
 	int ret;
@@ -807,7 +808,7 @@ int ltdb_index_transaction_commit(struct ldb_module *module)
 	ldb_reset_err_string(ldb);
 
 	if (ltdb->idxptr->itdb) {
-		tdb_traverse(ltdb->idxptr->itdb, ltdb_index_traverse_store, module);
+		tdb_traverse(ltdb->idxptr->itdb, ldb_kv_index_traverse_store, module);
 		tdb_close(ltdb->idxptr->itdb);
 	}
 
@@ -825,7 +826,7 @@ int ltdb_index_transaction_commit(struct ldb_module *module)
 }
 
 /* cleanup the idxptr mode when transaction cancels */
-int ltdb_index_transaction_cancel(struct ldb_module *module)
+int ldb_kv_index_transaction_cancel(struct ldb_module *module)
 {
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
 	if (ltdb->idxptr && ltdb->idxptr->itdb) {
@@ -841,7 +842,7 @@ int ltdb_index_transaction_cancel(struct ldb_module *module)
   return the dn key to be used for an index
   the caller is responsible for freeing
 */
-static struct ldb_dn *ltdb_index_key(struct ldb_context *ldb,
+static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 				     struct ltdb_private *ltdb,
 				     const char *attr, const struct ldb_val *value,
 				     const struct ldb_schema_attribute **ap,
@@ -855,7 +856,7 @@ static struct ldb_dn *ltdb_index_key(struct ldb_context *ldb,
 	int r;
 	bool should_b64_encode;
 
-	unsigned int max_key_length = ltdb_max_key_length(ltdb);
+	unsigned int max_key_length = ldb_kv_max_key_length(ltdb);
 	size_t key_len = 0;
 	size_t attr_len = 0;
 	const size_t indx_len = sizeof(LTDB_INDEX) - 1;
@@ -1026,7 +1027,7 @@ static struct ldb_dn *ltdb_index_key(struct ldb_context *ldb,
 /*
   see if a attribute value is in the list of indexed attributes
 */
-static bool ltdb_is_indexed(struct ldb_module *module,
+static bool ldb_kv_is_indexed(struct ldb_module *module,
 			    struct ltdb_private *ltdb,
 			    const char *attr)
 {
@@ -1089,7 +1090,7 @@ static bool ltdb_is_indexed(struct ldb_module *module,
   return a list of dn's that might match a simple indexed search (an
   equality search only)
  */
-static int ltdb_index_dn_simple(struct ldb_module *module,
+static int ldb_kv_index_dn_simple(struct ldb_module *module,
 				struct ltdb_private *ltdb,
 				const struct ldb_parse_tree *tree,
 				struct dn_list *list)
@@ -1106,13 +1107,13 @@ static int ltdb_index_dn_simple(struct ldb_module *module,
 
 	/* if the attribute isn't in the list of indexed attributes then
 	   this node needs a full search */
-	if (!ltdb_is_indexed(module, ltdb, tree->u.equality.attr)) {
+	if (!ldb_kv_is_indexed(module, ltdb, tree->u.equality.attr)) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
 	/* the attribute is indexed. Pull the list of DNs that match the
 	   search criterion */
-	dn = ltdb_index_key(ldb, ltdb,
+	dn = ldb_kv_index_key(ldb, ltdb,
 			    tree->u.equality.attr,
 			    &tree->u.equality.value, NULL, &truncation);
 	/*
@@ -1122,7 +1123,7 @@ static int ltdb_index_dn_simple(struct ldb_module *module,
 	 */
 	if (!dn) return LDB_ERR_OPERATIONS_ERROR;
 
-	ret = ltdb_dn_list_load(module, ltdb, dn, list);
+	ret = ldb_kv_dn_list_load(module, ltdb, dn, list);
 	talloc_free(dn);
 	return ret;
 }
@@ -1135,7 +1136,7 @@ static bool list_union(struct ldb_context *ldb,
 /*
   return a list of dn's that might match a leaf indexed search
  */
-static int ltdb_index_dn_leaf(struct ldb_module *module,
+static int ldb_kv_index_dn_leaf(struct ldb_module *module,
 			      struct ltdb_private *ltdb,
 			      const struct ldb_parse_tree *tree,
 			      struct dn_list *list)
@@ -1173,7 +1174,7 @@ static int ltdb_index_dn_leaf(struct ldb_module *module,
 		 * We can't call TALLOC_FREE(dn) as this must belong
 		 * to list for the memory to remain valid.
 		 */
-		return ltdb_index_dn_base_dn(module, ltdb, dn, list,
+		return ldb_kv_index_dn_base_dn(module, ltdb, dn, list,
 					     &truncation);
 		/*
 		 * We ignore truncation here and allow multi-valued matches
@@ -1207,7 +1208,7 @@ static int ltdb_index_dn_leaf(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	return ltdb_index_dn_simple(module, ltdb, tree, list);
+	return ldb_kv_index_dn_simple(module, ltdb, tree, list);
 }
 
 
@@ -1275,7 +1276,7 @@ static bool list_intersect(struct ldb_context *ldb,
 
 	for (i=0;i<short_list->count;i++) {
 		/* For the GUID index case, this is a binary search */
-		if (ltdb_dn_list_find_val(ltdb, long_list,
+		if (ldb_kv_dn_list_find_val(ltdb, long_list,
 					  &short_list->dn[i]) != -1) {
 			list3->dn[list3->count] = short_list->dn[i];
 			list3->count++;
@@ -1326,8 +1327,8 @@ static bool list_union(struct ldb_context *ldb,
 	 * NOTE: This can sort the in-memory index values, as list or
 	 * list2 might not be a copy!
 	 */
-	ltdb_dn_list_sort(ltdb, list);
-	ltdb_dn_list_sort(ltdb, list2);
+	ldb_kv_dn_list_sort(ltdb, list);
+	ldb_kv_dn_list_sort(ltdb, list2);
 
 	dn3 = talloc_array(list, struct ldb_val, list->count + list2->count);
 	if (!dn3) {
@@ -1371,7 +1372,7 @@ static bool list_union(struct ldb_context *ldb,
 	return true;
 }
 
-static int ltdb_index_dn(struct ldb_module *module,
+static int ldb_kv_index_dn(struct ldb_module *module,
 			 struct ltdb_private *ltdb,
 			 const struct ldb_parse_tree *tree,
 			 struct dn_list *list);
@@ -1380,7 +1381,7 @@ static int ltdb_index_dn(struct ldb_module *module,
 /*
   process an OR list (a union)
  */
-static int ltdb_index_dn_or(struct ldb_module *module,
+static int ldb_kv_index_dn_or(struct ldb_module *module,
 			    struct ltdb_private *ltdb,
 			    const struct ldb_parse_tree *tree,
 			    struct dn_list *list)
@@ -1402,7 +1403,7 @@ static int ltdb_index_dn_or(struct ldb_module *module,
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
 
-		ret = ltdb_index_dn(module, ltdb,
+		ret = ldb_kv_index_dn(module, ltdb,
 				    tree->u.list.elements[i], list2);
 
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
@@ -1434,7 +1435,7 @@ static int ltdb_index_dn_or(struct ldb_module *module,
 /*
   NOT an index results
  */
-static int ltdb_index_dn_not(struct ldb_module *module,
+static int ldb_kv_index_dn_not(struct ldb_module *module,
 			     struct ltdb_private *ltdb,
 			     const struct ldb_parse_tree *tree,
 			     struct dn_list *list)
@@ -1454,7 +1455,7 @@ static int ltdb_index_dn_not(struct ldb_module *module,
  * These things are unique, so avoid a full scan if this is a search
  * by GUID, DN or a unique attribute
  */
-static bool ltdb_index_unique(struct ldb_context *ldb,
+static bool ldb_kv_index_unique(struct ldb_context *ldb,
 			      struct ltdb_private *ltdb,
 			      const char *attr)
 {
@@ -1478,7 +1479,7 @@ static bool ltdb_index_unique(struct ldb_context *ldb,
 /*
   process an AND expression (intersection)
  */
-static int ltdb_index_dn_and(struct ldb_module *module,
+static int ldb_kv_index_dn_and(struct ldb_module *module,
 			     struct ltdb_private *ltdb,
 			     const struct ldb_parse_tree *tree,
 			     struct dn_list *list)
@@ -1500,12 +1501,12 @@ static int ltdb_index_dn_and(struct ldb_module *module,
 		int ret;
 
 		if (subtree->operation != LDB_OP_EQUALITY ||
-		    !ltdb_index_unique(ldb, ltdb,
+		    !ldb_kv_index_unique(ldb, ltdb,
 				       subtree->u.equality.attr)) {
 			continue;
 		}
 
-		ret = ltdb_index_dn(module, ltdb, subtree, list);
+		ret = ldb_kv_index_dn(module, ltdb, subtree, list);
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 			/* 0 && X == 0 */
 			return LDB_ERR_NO_SUCH_OBJECT;
@@ -1532,7 +1533,7 @@ static int ltdb_index_dn_and(struct ldb_module *module,
 			return ldb_module_oom(module);
 		}
 
-		ret = ltdb_index_dn(module, ltdb, subtree, list2);
+		ret = ldb_kv_index_dn(module, ltdb, subtree, list2);
 
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 			/* X && 0 == 0 */
@@ -1581,7 +1582,7 @@ static int ltdb_index_dn_and(struct ldb_module *module,
 /*
   return a list of matching objects using a one-level index
  */
-static int ltdb_index_dn_attr(struct ldb_module *module,
+static int ldb_kv_index_dn_attr(struct ldb_module *module,
 			      struct ltdb_private *ltdb,
 			      const char *attr,
 			      struct ldb_dn *dn,
@@ -1598,13 +1599,13 @@ static int ltdb_index_dn_attr(struct ldb_module *module,
 	/* work out the index key from the parent DN */
 	val.data = (uint8_t *)((uintptr_t)ldb_dn_get_casefold(dn));
 	val.length = strlen((char *)val.data);
-	key = ltdb_index_key(ldb, ltdb, attr, &val, NULL, truncation);
+	key = ldb_kv_index_key(ldb, ltdb, attr, &val, NULL, truncation);
 	if (!key) {
 		ldb_oom(ldb);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ltdb_dn_list_load(module, ltdb, key, list);
+	ret = ldb_kv_dn_list_load(module, ltdb, key, list);
 	talloc_free(key);
 	if (ret != LDB_SUCCESS) {
 		return ret;
@@ -1620,7 +1621,7 @@ static int ltdb_index_dn_attr(struct ldb_module *module,
 /*
   return a list of matching objects using a one-level index
  */
-static int ltdb_index_dn_one(struct ldb_module *module,
+static int ldb_kv_index_dn_one(struct ldb_module *module,
 			     struct ltdb_private *ltdb,
 			     struct ldb_dn *parent_dn,
 			     struct dn_list *list,
@@ -1628,7 +1629,7 @@ static int ltdb_index_dn_one(struct ldb_module *module,
 {
 	/* Ensure we do not shortcut on intersection for this list */
 	list->strict = true;
-	return ltdb_index_dn_attr(module, ltdb,
+	return ldb_kv_index_dn_attr(module, ltdb,
 				  LTDB_IDXONE, parent_dn, list, truncation);
 
 }
@@ -1636,7 +1637,7 @@ static int ltdb_index_dn_one(struct ldb_module *module,
 /*
   return a list of matching objects using the DN index
  */
-static int ltdb_index_dn_base_dn(struct ldb_module *module,
+static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
 				 struct ltdb_private *ltdb,
 				 struct ldb_dn *base_dn,
 				 struct dn_list *dn_list,
@@ -1676,7 +1677,7 @@ static int ltdb_index_dn_base_dn(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	return ltdb_index_dn_attr(module, ltdb,
+	return ldb_kv_index_dn_attr(module, ltdb,
 				  LTDB_IDXDN, base_dn, dn_list, truncation);
 }
 
@@ -1684,7 +1685,7 @@ static int ltdb_index_dn_base_dn(struct ldb_module *module,
   return a list of dn's that might match a indexed search or
   an error. return LDB_ERR_NO_SUCH_OBJECT for no matches, or LDB_SUCCESS for matches
  */
-static int ltdb_index_dn(struct ldb_module *module,
+static int ldb_kv_index_dn(struct ldb_module *module,
 			 struct ltdb_private *ltdb,
 			 const struct ldb_parse_tree *tree,
 			 struct dn_list *list)
@@ -1693,19 +1694,19 @@ static int ltdb_index_dn(struct ldb_module *module,
 
 	switch (tree->operation) {
 	case LDB_OP_AND:
-		ret = ltdb_index_dn_and(module, ltdb, tree, list);
+		ret = ldb_kv_index_dn_and(module, ltdb, tree, list);
 		break;
 
 	case LDB_OP_OR:
-		ret = ltdb_index_dn_or(module, ltdb, tree, list);
+		ret = ldb_kv_index_dn_or(module, ltdb, tree, list);
 		break;
 
 	case LDB_OP_NOT:
-		ret = ltdb_index_dn_not(module, ltdb, tree, list);
+		ret = ldb_kv_index_dn_not(module, ltdb, tree, list);
 		break;
 
 	case LDB_OP_EQUALITY:
-		ret = ltdb_index_dn_leaf(module, ltdb, tree, list);
+		ret = ldb_kv_index_dn_leaf(module, ltdb, tree, list);
 		break;
 
 	case LDB_OP_SUBSTRING:
@@ -1726,7 +1727,7 @@ static int ltdb_index_dn(struct ldb_module *module,
   filter a candidate dn_list from an indexed search into a set of results
   extracting just the given attributes
 */
-static int ltdb_index_filter(struct ltdb_private *ltdb,
+static int ldb_kv_index_filter(struct ltdb_private *ltdb,
 			     const struct dn_list *dn_list,
 			     struct ltdb_context *ac,
 			     uint32_t *match_count,
@@ -1783,7 +1784,7 @@ static int ltdb_index_filter(struct ltdb_private *ltdb,
 	for (i = 0; i < dn_list->count; i++) {
 		int ret;
 
-		ret = ltdb_idx_to_key(ac->module,
+		ret = ldb_kv_idx_to_key(ac->module,
 				      ltdb,
 				      keys,
 				      &dn_list->dn[i],
@@ -1831,7 +1832,7 @@ static int ltdb_index_filter(struct ltdb_private *ltdb,
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
 
-		ret = ltdb_search_key(ac->module, ltdb,
+		ret = ldb_kv_search_key(ac->module, ltdb,
 				      keys[i], msg,
 				      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC|
 				      LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC);
@@ -1881,7 +1882,7 @@ static int ltdb_index_filter(struct ltdb_private *ltdb,
 		}
 
 		/* filter the attributes that the user wants */
-		ret = ltdb_filter_attrs(ac, msg, ac->attrs, &filtered_msg);
+		ret = ldb_kv_filter_attrs(ac, msg, ac->attrs, &filtered_msg);
 
 		talloc_free(msg);
 
@@ -1910,7 +1911,7 @@ static int ltdb_index_filter(struct ltdb_private *ltdb,
 /*
   sort a DN list
  */
-static void ltdb_dn_list_sort(struct ltdb_private *ltdb,
+static void ldb_kv_dn_list_sort(struct ltdb_private *ltdb,
 			      struct dn_list *list)
 {
 	if (list->count < 2) {
@@ -1931,7 +1932,7 @@ static void ltdb_dn_list_sort(struct ltdb_private *ltdb,
   returns -1 if an indexed search is not possible, in which
   case the caller should call ltdb_search_full()
 */
-int ltdb_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
+int ldb_kv_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(ac->module), struct ltdb_private);
@@ -1979,7 +1980,7 @@ int ltdb_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
 		 * the tree, we must ensure we strictly intersect with
 		 * this list, as we trust the ONELEVEL index
 		 */
-		ret = ltdb_index_dn_one(ac->module, ltdb, ac->base, dn_list,
+		ret = ldb_kv_index_dn_one(ac->module, ltdb, ac->base, dn_list,
 				        &scope_one_truncation);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(dn_list);
@@ -2018,7 +2019,7 @@ int ltdb_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
 			 * index can't trim the result list down then
 			 * the ONELEVEL index is still good enough.
 			 */
-			ret = ltdb_index_dn(ac->module, ltdb, ac->tree,
+			ret = ldb_kv_index_dn(ac->module, ltdb, ac->tree,
 					    idx_one_tree_list);
 			if (ret == LDB_SUCCESS) {
 				if (!list_intersect(ldb, ltdb,
@@ -2042,7 +2043,7 @@ int ltdb_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
 		 * Here we load the index for the tree.  We have no
 		 * index for the subtree.
 		 */
-		ret = ltdb_index_dn(ac->module, ltdb, ac->tree, dn_list);
+		ret = ldb_kv_index_dn(ac->module, ltdb, ac->tree, dn_list);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(dn_list);
 			return ret;
@@ -2061,7 +2062,7 @@ int ltdb_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
 	 * processing as the truncation here refers only to the
 	 * SCOPE_ONELEVEL index.
 	 */
-	ret = ltdb_index_filter(ltdb, dn_list, ac, match_count,
+	ret = ldb_kv_index_filter(ltdb, dn_list, ac, match_count,
 				scope_one_truncation);
 	talloc_free(dn_list);
 	return ret;
@@ -2087,7 +2088,7 @@ int ltdb_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
  *
  * @return                  An ldb error code
  */
-static int ltdb_index_add1(struct ldb_module *module,
+static int ldb_kv_index_add1(struct ldb_module *module,
 			   struct ltdb_private *ltdb,
 			   const struct ldb_message *msg,
 			   struct ldb_message_element *el, int v_idx)
@@ -2108,7 +2109,7 @@ static int ltdb_index_add1(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	dn_key = ltdb_index_key(ldb, ltdb,
+	dn_key = ldb_kv_index_key(ldb, ltdb,
 				el->name, &el->values[v_idx], &a, &truncation);
 	if (!dn_key) {
 		talloc_free(list);
@@ -2135,7 +2136,7 @@ static int ltdb_index_add1(struct ldb_module *module,
 	}
 	talloc_steal(list, dn_key);
 
-	ret = ltdb_dn_list_load(module, ltdb, dn_key, list);
+	ret = ldb_kv_dn_list_load(module, ltdb, dn_key, list);
 	if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
 		talloc_free(list);
 		return ret;
@@ -2177,7 +2178,7 @@ static int ltdb_index_add1(struct ldb_module *module,
 				return LDB_ERR_OPERATIONS_ERROR;
 			}
 
-			ret = ltdb_idx_to_key(module, ltdb,
+			ret = ldb_kv_idx_to_key(module, ltdb,
 					      ldb, &list->dn[i],
 					      &key);
 			if (ret != LDB_SUCCESS) {
@@ -2186,7 +2187,7 @@ static int ltdb_index_add1(struct ldb_module *module,
 				return ret;
 			}
 
-			ret = ltdb_search_key(module, ltdb, key,
+			ret = ldb_kv_search_key(module, ltdb, key,
 					      rec, flags);
 			if (key.dptr != guid_key) {
 				TALLOC_FREE(key.dptr);
@@ -2359,7 +2360,7 @@ static int ltdb_index_add1(struct ldb_module *module,
 	}
 	list->count++;
 
-	ret = ltdb_dn_list_store(module, dn_key, list);
+	ret = ldb_kv_dn_list_store(module, dn_key, list);
 
 	talloc_free(list);
 
@@ -2369,14 +2370,14 @@ static int ltdb_index_add1(struct ldb_module *module,
 /*
   add index entries for one elements in a message
  */
-static int ltdb_index_add_el(struct ldb_module *module,
+static int ldb_kv_index_add_el(struct ldb_module *module,
 			     struct ltdb_private *ltdb,
 			     const struct ldb_message *msg,
 			     struct ldb_message_element *el)
 {
 	unsigned int i;
 	for (i = 0; i < el->num_values; i++) {
-		int ret = ltdb_index_add1(module, ltdb,
+		int ret = ldb_kv_index_add1(module, ltdb,
 					  msg, el, i);
 		if (ret != LDB_SUCCESS) {
 			return ret;
@@ -2389,7 +2390,7 @@ static int ltdb_index_add_el(struct ldb_module *module,
 /*
   add index entries for all elements in a message
  */
-static int ltdb_index_add_all(struct ldb_module *module,
+static int ldb_kv_index_add_all(struct ldb_module *module,
 			      struct ltdb_private *ltdb,
 			      const struct ldb_message *msg)
 {
@@ -2407,7 +2408,7 @@ static int ltdb_index_add_all(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ltdb_write_index_dn_guid(module, msg, 1);
+	ret = ldb_kv_write_index_dn_guid(module, msg, 1);
 	if (ret != LDB_SUCCESS) {
 		return ret;
 	}
@@ -2418,10 +2419,10 @@ static int ltdb_index_add_all(struct ldb_module *module,
 	}
 
 	for (i = 0; i < msg->num_elements; i++) {
-		if (!ltdb_is_indexed(module, ltdb, elements[i].name)) {
+		if (!ldb_kv_is_indexed(module, ltdb, elements[i].name)) {
 			continue;
 		}
-		ret = ltdb_index_add_el(module, ltdb,
+		ret = ldb_kv_index_add_el(module, ltdb,
 					msg, &elements[i]);
 		if (ret != LDB_SUCCESS) {
 			struct ldb_context *ldb = ldb_module_get_ctx(module);
@@ -2440,7 +2441,7 @@ static int ltdb_index_add_all(struct ldb_module *module,
 /*
   insert a DN index for a message
 */
-static int ltdb_modify_index_dn(struct ldb_module *module,
+static int ldb_kv_modify_index_dn(struct ldb_module *module,
 				struct ltdb_private *ltdb,
 				const struct ldb_message *msg,
 				struct ldb_dn *dn,
@@ -2470,9 +2471,9 @@ static int ltdb_modify_index_dn(struct ldb_module *module,
 	el.num_values = 1;
 
 	if (add) {
-		ret = ltdb_index_add1(module, ltdb, msg, &el, 0);
+		ret = ldb_kv_index_add1(module, ltdb, msg, &el, 0);
 	} else { /* delete */
-		ret = ltdb_index_del_value(module, ltdb, msg, &el, 0);
+		ret = ldb_kv_index_del_value(module, ltdb, msg, &el, 0);
 	}
 
 	if (ret != LDB_SUCCESS) {
@@ -2493,7 +2494,7 @@ static int ltdb_modify_index_dn(struct ldb_module *module,
 /*
   insert a one level index for a message
 */
-static int ltdb_index_onelevel(struct ldb_module *module,
+static int ldb_kv_index_onelevel(struct ldb_module *module,
 			       const struct ldb_message *msg, int add)
 {
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module),
@@ -2510,7 +2511,7 @@ static int ltdb_index_onelevel(struct ldb_module *module,
 	if (pdn == NULL) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
-	ret = ltdb_modify_index_dn(module, ltdb,
+	ret = ldb_kv_modify_index_dn(module, ltdb,
 				   msg, pdn, LTDB_IDXONE, add);
 
 	talloc_free(pdn);
@@ -2521,7 +2522,7 @@ static int ltdb_index_onelevel(struct ldb_module *module,
 /*
   insert a one level index for a message
 */
-static int ltdb_write_index_dn_guid(struct ldb_module *module,
+static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
 				    const struct ldb_message *msg,
 				    int add)
 {
@@ -2534,7 +2535,7 @@ static int ltdb_write_index_dn_guid(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	ret = ltdb_modify_index_dn(module, ltdb, msg, msg->dn,
+	ret = ldb_kv_modify_index_dn(module, ltdb, msg, msg->dn,
 				   LTDB_IDXDN, add);
 
 	if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
@@ -2550,7 +2551,7 @@ static int ltdb_write_index_dn_guid(struct ldb_module *module,
   add the index entries for a new element in a record
   The caller guarantees that these element values are not yet indexed
 */
-int ltdb_index_add_element(struct ldb_module *module,
+int ldb_kv_index_add_element(struct ldb_module *module,
 			   struct ltdb_private *ltdb,
 			   const struct ldb_message *msg,
 			   struct ldb_message_element *el)
@@ -2558,16 +2559,16 @@ int ltdb_index_add_element(struct ldb_module *module,
 	if (ldb_dn_is_special(msg->dn)) {
 		return LDB_SUCCESS;
 	}
-	if (!ltdb_is_indexed(module, ltdb, el->name)) {
+	if (!ldb_kv_is_indexed(module, ltdb, el->name)) {
 		return LDB_SUCCESS;
 	}
-	return ltdb_index_add_el(module, ltdb, msg, el);
+	return ldb_kv_index_add_el(module, ltdb, msg, el);
 }
 
 /*
   add the index entries for a new record
 */
-int ltdb_index_add_new(struct ldb_module *module,
+int ldb_kv_index_add_new(struct ldb_module *module,
 		       struct ltdb_private *ltdb,
 		       const struct ldb_message *msg)
 {
@@ -2577,7 +2578,7 @@ int ltdb_index_add_new(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	ret = ltdb_index_add_all(module, ltdb, msg);
+	ret = ldb_kv_index_add_all(module, ltdb, msg);
 	if (ret != LDB_SUCCESS) {
 		/*
 		 * Because we can't trust the caller to be doing
@@ -2586,11 +2587,11 @@ int ltdb_index_add_new(struct ldb_module *module,
 		 * cleanup
 		 */
 
-		ltdb_index_delete(module, msg);
+		ldb_kv_index_delete(module, msg);
 		return ret;
 	}
 
-	ret = ltdb_index_onelevel(module, msg, 1);
+	ret = ldb_kv_index_onelevel(module, msg, 1);
 	if (ret != LDB_SUCCESS) {
 		/*
 		 * Because we can't trust the caller to be doing
@@ -2598,7 +2599,7 @@ int ltdb_index_add_new(struct ldb_module *module,
 		 * entry rather than relying on a transaction
 		 * cleanup
 		 */
-		ltdb_index_delete(module, msg);
+		ldb_kv_index_delete(module, msg);
 		return ret;
 	}
 	return ret;
@@ -2608,7 +2609,7 @@ int ltdb_index_add_new(struct ldb_module *module,
 /*
   delete an index entry for one message element
 */
-int ltdb_index_del_value(struct ldb_module *module,
+int ldb_kv_index_del_value(struct ldb_module *module,
 			 struct ltdb_private *ltdb,
 			 const struct ldb_message *msg,
 			 struct ldb_message_element *el, unsigned int v_idx)
@@ -2633,7 +2634,7 @@ int ltdb_index_del_value(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	dn_key = ltdb_index_key(ldb, ltdb,
+	dn_key = ldb_kv_index_key(ldb, ltdb,
 				el->name, &el->values[v_idx],
 				NULL, &truncation);
 	/*
@@ -2652,7 +2653,7 @@ int ltdb_index_del_value(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ltdb_dn_list_load(module, ltdb, dn_key, list);
+	ret = ldb_kv_dn_list_load(module, ltdb, dn_key, list);
 	if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 		/* it wasn't indexed. Did we have an earlier error? If we did then
 		   its gone now */
@@ -2668,7 +2669,7 @@ int ltdb_index_del_value(struct ldb_module *module,
 	/*
 	 * Find one of the values matching this message to remove
 	 */
-	i = ltdb_dn_list_find_msg(ltdb, list, msg);
+	i = ldb_kv_dn_list_find_msg(ltdb, list, msg);
 	if (i == -1) {
 		/* nothing to delete */
 		talloc_free(dn_key);
@@ -2687,7 +2688,7 @@ int ltdb_index_del_value(struct ldb_module *module,
 		list->dn = talloc_realloc(list, list->dn, struct ldb_val, list->count);
 	}
 
-	ret = ltdb_dn_list_store(module, dn_key, list);
+	ret = ldb_kv_dn_list_store(module, dn_key, list);
 
 	talloc_free(dn_key);
 
@@ -2698,7 +2699,7 @@ int ltdb_index_del_value(struct ldb_module *module,
   delete the index entries for a element
   return -1 on failure
 */
-int ltdb_index_del_element(struct ldb_module *module,
+int ldb_kv_index_del_element(struct ldb_module *module,
 			   struct ltdb_private *ltdb,
 			   const struct ldb_message *msg,
 			   struct ldb_message_element *el)
@@ -2721,11 +2722,11 @@ int ltdb_index_del_element(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	if (!ltdb_is_indexed(module, ltdb, el->name)) {
+	if (!ldb_kv_is_indexed(module, ltdb, el->name)) {
 		return LDB_SUCCESS;
 	}
 	for (i = 0; i < el->num_values; i++) {
-		ret = ltdb_index_del_value(module, ltdb, msg, el, i);
+		ret = ldb_kv_index_del_value(module, ltdb, msg, el, i);
 		if (ret != LDB_SUCCESS) {
 			return ret;
 		}
@@ -2738,7 +2739,7 @@ int ltdb_index_del_element(struct ldb_module *module,
   delete the index entries for a record
   return -1 on failure
 */
-int ltdb_index_delete(struct ldb_module *module, const struct ldb_message *msg)
+int ldb_kv_index_delete(struct ldb_module *module, const struct ldb_message *msg)
 {
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
 	int ret;
@@ -2748,12 +2749,12 @@ int ltdb_index_delete(struct ldb_module *module, const struct ldb_message *msg)
 		return LDB_SUCCESS;
 	}
 
-	ret = ltdb_index_onelevel(module, msg, 0);
+	ret = ldb_kv_index_onelevel(module, msg, 0);
 	if (ret != LDB_SUCCESS) {
 		return ret;
 	}
 
-	ret = ltdb_write_index_dn_guid(module, msg, 0);
+	ret = ldb_kv_write_index_dn_guid(module, msg, 0);
 	if (ret != LDB_SUCCESS) {
 		return ret;
 	}
@@ -2764,7 +2765,7 @@ int ltdb_index_delete(struct ldb_module *module, const struct ldb_message *msg)
 	}
 
 	for (i = 0; i < msg->num_elements; i++) {
-		ret = ltdb_index_del_element(module, ltdb,
+		ret = ldb_kv_index_del_element(module, ltdb,
 					     msg, &msg->elements[i]);
 		if (ret != LDB_SUCCESS) {
 			return ret;
@@ -2810,7 +2811,7 @@ static int delete_index(struct ltdb_private *ltdb, struct ldb_val key, struct ld
 	 * This does not actually touch the DB quite yet, just
          * the in-memory index cache
 	 */
-	ret = ltdb_dn_list_store(module, dn, &list);
+	ret = ldb_kv_dn_list_store(module, dn, &list);
 	if (ret != LDB_SUCCESS) {
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
 				       "Unable to store null index for %s\n",
@@ -2847,7 +2848,7 @@ static int re_key(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_
 		return 0;
 	}
 
-	is_record = ltdb_key_is_record(key);
+	is_record = ldb_kv_key_is_record(key);
 	if (is_record == false) {
 		return 0;
 	}
@@ -2883,7 +2884,7 @@ static int re_key(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_
 	/* check if the DN key has changed, perhaps due to the case
 	   insensitivity of an element changing, or a change from DN
 	   to GUID keys */
-	key2 = ltdb_key_msg(module, msg, msg);
+	key2 = ldb_kv_key_msg(module, msg, msg);
 	if (key2.dptr == NULL) {
 		/* probably a corrupt record ... darn */
 		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid DN in re_index: %s",
@@ -2937,7 +2938,7 @@ static int re_index(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ld
 		return 0;
 	}
 
-	is_record = ltdb_key_is_record(key);
+	is_record = ldb_kv_key_is_record(key);
 	if (is_record == false) {
 		return 0;
 	}
@@ -2970,7 +2971,7 @@ static int re_index(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ld
 		return -1;
 	}
 
-	ret = ltdb_index_onelevel(module, msg, 1);
+	ret = ldb_kv_index_onelevel(module, msg, 1);
 	if (ret != LDB_SUCCESS) {
 		ldb_debug(ldb, LDB_DEBUG_ERROR,
 			  "Adding special ONE LEVEL index failed (%s)!",
@@ -2979,7 +2980,7 @@ static int re_index(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ld
 		return -1;
 	}
 
-	ret = ltdb_index_add_all(module, ltdb, msg);
+	ret = ldb_kv_index_add_all(module, ltdb, msg);
 
 	if (ret != LDB_SUCCESS) {
 		ctx->error = ret;
@@ -3002,7 +3003,7 @@ static int re_index(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ld
 /*
   force a complete reindex of the database
 */
-int ltdb_reindex(struct ldb_module *module)
+int ldb_kv_reindex(struct ldb_module *module)
 {
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
 	int ret;
@@ -3016,7 +3017,7 @@ int ltdb_reindex(struct ldb_module *module)
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
-	if (ltdb_cache_reload(module) != 0) {
+	if (ldb_kv_cache_reload(module) != 0) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
@@ -3025,9 +3026,9 @@ int ltdb_reindex(struct ldb_module *module)
 	 * DB, no values stored so far are any use as we want to do a
 	 * re-index
 	 */
-	ltdb_index_transaction_cancel(module);
+	ldb_kv_index_transaction_cancel(module);
 
-	ret = ltdb_index_transaction_start(module);
+	ret = ldb_kv_index_transaction_start(module);
 	if (ret != LDB_SUCCESS) {
 		return ret;
 	}
diff --git a/lib/ldb/ldb_tdb/ldb_search.c b/lib/ldb/ldb_tdb/ldb_search.c
index 832be9a..4f67047 100644
--- a/lib/ldb/ldb_tdb/ldb_search.c
+++ b/lib/ldb/ldb_tdb/ldb_search.c
@@ -116,7 +116,7 @@ static int msg_add_distinguished_name(struct ldb_message *msg)
   return LDB_ERR_NO_SUCH_OBJECT on record-not-found
   and LDB_SUCCESS on success
 */
-int ltdb_search_base(struct ldb_module *module,
+int ldb_kv_search_base(struct ldb_module *module,
 		     TALLOC_CTX *mem_ctx,
 		     struct ldb_dn *dn,
 		     struct ldb_dn **ret_dn)
@@ -141,7 +141,7 @@ int ltdb_search_base(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ltdb_search_dn1(module, dn,
+	ret = ldb_kv_search_dn1(module, dn,
 			      msg,
 			      LDB_UNPACK_DATA_FLAG_NO_ATTRS);
 	if (ret == LDB_SUCCESS) {
@@ -183,7 +183,7 @@ struct ltdb_parse_data_unpack_ctx {
 	unsigned int unpack_flags;
 };
 
-static int ltdb_parse_data_unpack(struct ldb_val key,
+static int ldb_kv_parse_data_unpack(struct ldb_val key,
 				  struct ldb_val data,
 				  void *private_data)
 {
@@ -236,7 +236,7 @@ static int ltdb_parse_data_unpack(struct ldb_val key,
   return LDB_ERR_NO_SUCH_OBJECT on record-not-found
   and LDB_SUCCESS on success
 */
-int ltdb_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
+int ldb_kv_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
 		    const struct TDB_DATA tdb_key,
 		    struct ldb_message *msg,
 		    unsigned int unpack_flags)
@@ -258,7 +258,7 @@ int ltdb_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
 	msg->elements = NULL;
 
 	ret = ltdb->kv_ops->fetch_and_parse(ltdb, ldb_key,
-					    ltdb_parse_data_unpack, &ctx);
+					    ldb_kv_parse_data_unpack, &ctx);
 
 	if (ret == -1) {
 		ret = ltdb->kv_ops->error(ltdb);
@@ -284,7 +284,7 @@ int ltdb_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
   return LDB_ERR_NO_SUCH_OBJECT on record-not-found
   and LDB_SUCCESS on success
 */
-int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg,
+int ldb_kv_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg,
 		    unsigned int unpack_flags)
 {
 	void *data = ldb_module_get_private(module);
@@ -306,7 +306,7 @@ int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_mes
 		}
 
 		/* form the key */
-		tdb_key = ltdb_key_dn(module, tdb_key_ctx, dn);
+		tdb_key = ldb_kv_key_dn(module, tdb_key_ctx, dn);
 		if (!tdb_key.dptr) {
 			TALLOC_FREE(tdb_key_ctx);
 			return LDB_ERR_OPERATIONS_ERROR;
@@ -319,7 +319,7 @@ int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_mes
 		 * used for internal memory.
 		 *
 		 */
-		ret = ltdb_key_dn_from_idx(module, ltdb,
+		ret = ldb_kv_key_dn_from_idx(module, ltdb,
 					   msg,
 					   dn, &tdb_key);
 		if (ret != LDB_SUCCESS) {
@@ -327,7 +327,7 @@ int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_mes
 		}
 	}
 
-	ret = ltdb_search_key(module, ltdb, tdb_key, msg, unpack_flags);
+	ret = ldb_kv_search_key(module, ltdb, tdb_key, msg, unpack_flags);
 
 	TALLOC_FREE(tdb_key_ctx);
 
@@ -355,7 +355,7 @@ int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_mes
   individually allocated, which is what our callers expect.
 
  */
-int ltdb_filter_attrs(TALLOC_CTX *mem_ctx,
+int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
 		      const struct ldb_message *msg, const char * const *attrs,
 		      struct ldb_message **filtered_msg)
 {
@@ -509,7 +509,7 @@ static int search_func(struct ltdb_private *ltdb, struct ldb_val key, struct ldb
 	ac = talloc_get_type(state, struct ltdb_context);
 	ldb = ldb_module_get_ctx(ac->module);
 
-	if (ltdb_key_is_record(tdb_key) == false) {
+	if (ldb_kv_key_is_record(tdb_key) == false) {
 		return 0;
 	}
 
@@ -556,7 +556,7 @@ static int search_func(struct ltdb_private *ltdb, struct ldb_val key, struct ldb
 	}
 
 	/* filter the attributes that the user wants */
-	ret = ltdb_filter_attrs(ac, msg, ac->attrs, &filtered_msg);
+	ret = ldb_kv_filter_attrs(ac, msg, ac->attrs, &filtered_msg);
 	talloc_free(msg);
 
 	if (ret == -1) {
@@ -580,7 +580,7 @@ static int search_func(struct ltdb_private *ltdb, struct ldb_val key, struct ldb
   search the database with a LDAP-like expression.
   this is the "full search" non-indexed variant
 */
-static int ltdb_search_full(struct ltdb_context *ctx)
+static int ldb_kv_search_full(struct ltdb_context *ctx)
 {
 	void *data = ldb_module_get_private(ctx->module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
@@ -596,7 +596,7 @@ static int ltdb_search_full(struct ltdb_context *ctx)
 	return ctx->error;
 }
 
-static int ltdb_search_and_return_base(struct ltdb_private *ltdb,
+static int ldb_kv_search_and_return_base(struct ltdb_private *ltdb,
 				       struct ltdb_context *ctx)
 {
 	struct ldb_message *msg, *filtered_msg;
@@ -610,7 +610,7 @@ static int ltdb_search_and_return_base(struct ltdb_private *ltdb,
 	if (!msg) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
-	ret = ltdb_search_dn1(ctx->module, ctx->base, msg,
+	ret = ldb_kv_search_dn1(ctx->module, ctx->base, msg,
 			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC|
 			      LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC);
 
@@ -671,7 +671,7 @@ static int ltdb_search_and_return_base(struct ltdb_private *ltdb,
 	 * This copies msg->dn including the casefolding, so the above
 	 * assignment is safe
 	 */
-	ret = ltdb_filter_attrs(ctx, msg, ctx->attrs, &filtered_msg);
+	ret = ldb_kv_filter_attrs(ctx, msg, ctx->attrs, &filtered_msg);
 
 	/*
 	 * Remove any extended components possibly copied in from
@@ -700,7 +700,7 @@ static int ltdb_search_and_return_base(struct ltdb_private *ltdb,
   search the database with a LDAP-like expression.
   choses a search method
 */
-int ltdb_search(struct ltdb_context *ctx)
+int ldb_kv_search(struct ltdb_context *ctx)
 {
 	struct ldb_context *ldb;
 	struct ldb_module *module = ctx->module;
@@ -717,7 +717,7 @@ int ltdb_search(struct ltdb_context *ctx)
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	if (ltdb_cache_load(module) != 0) {
+	if (ldb_kv_cache_load(module) != 0) {
 		ltdb->kv_ops->unlock_read(module);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
@@ -768,7 +768,7 @@ int ltdb_search(struct ltdb_context *ctx)
 		 * will try to look up an index record for a special
 		 * record (which doesn't exist).
 		 */
-		ret = ltdb_search_and_return_base(ltdb, ctx);
+		ret = ldb_kv_search_and_return_base(ltdb, ctx);
 
 		ltdb->kv_ops->unlock_read(module);
 
@@ -781,7 +781,7 @@ int ltdb_search(struct ltdb_context *ctx)
 		 * dn.  Also optimise the subsequent filter by filling
 		 * in the ctx->base to be exactly case correct
 		 */
-		ret = ltdb_search_base(module, ctx,
+		ret = ldb_kv_search_base(module, ctx,
 				       req->op.search.base,
 				       &ctx->base);
 		
@@ -799,7 +799,7 @@ int ltdb_search(struct ltdb_context *ctx)
 	if (ret == LDB_SUCCESS) {
 		uint32_t match_count = 0;
 
-		ret = ltdb_search_indexed(ctx, &match_count);
+		ret = ldb_kv_search_indexed(ctx, &match_count);
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 			/* Not in the index, therefore OK! */
 			ret = LDB_SUCCESS;
@@ -844,7 +844,7 @@ int ltdb_search(struct ltdb_context *ctx)
 				return LDB_ERR_INAPPROPRIATE_MATCHING;
 			}
 
-			ret = ltdb_search_full(ctx);
+			ret = ldb_kv_search_full(ctx);
 			if (ret != LDB_SUCCESS) {
 				ldb_set_errstring(ldb, "Indexed and full searches both failed!\n");
 			}
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.c b/lib/ldb/ldb_tdb/ldb_tdb.c
index daf9a77..a18e613 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -164,7 +164,7 @@ static int ltdb_unlock_read(struct ldb_module *module)
  * Determine if this key could hold a record.  We allow the new GUID
  * index, the old DN index and a possible future ID=
  */
-bool ltdb_key_is_record(TDB_DATA key)
+bool ldb_kv_key_is_record(TDB_DATA key)
 {
 	if (key.dsize < 4) {
 		return false;
@@ -197,7 +197,7 @@ bool ltdb_key_is_record(TDB_DATA key)
   note that the key for a record can depend on whether the
   dn refers to a case sensitive index record or not
 */
-TDB_DATA ltdb_key_dn(struct ldb_module *module, TALLOC_CTX *mem_ctx,
+TDB_DATA ldb_kv_key_dn(struct ldb_module *module, TALLOC_CTX *mem_ctx,
 		     struct ldb_dn *dn)
 {
 	TDB_DATA key;
@@ -244,7 +244,7 @@ failed:
 }
 
 /* The caller is to provide a correctly sized key */
-int ltdb_guid_to_key(struct ldb_module *module,
+int ldb_kv_guid_to_key(struct ldb_module *module,
 		     struct ltdb_private *ltdb,
 		     const struct ldb_val *GUID_val,
 		     TDB_DATA *key)
@@ -266,7 +266,7 @@ int ltdb_guid_to_key(struct ldb_module *module,
  * The caller is to provide a correctly sized key, used only in
  * the GUID index mode
  */
-int ltdb_idx_to_key(struct ldb_module *module,
+int ldb_kv_idx_to_key(struct ldb_module *module,
 		    struct ltdb_private *ltdb,
 		    TALLOC_CTX *mem_ctx,
 		    const struct ldb_val *idx_val,
@@ -276,7 +276,7 @@ int ltdb_idx_to_key(struct ldb_module *module,
 	struct ldb_dn *dn;
 
 	if (ltdb->cache->GUID_index_attribute != NULL) {
-		return ltdb_guid_to_key(module, ltdb,
+		return ldb_kv_guid_to_key(module, ltdb,
 					idx_val, key);
 	}
 
@@ -289,7 +289,7 @@ int ltdb_idx_to_key(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 	/* form the key */
-	*key = ltdb_key_dn(module, mem_ctx, dn);
+	*key = ldb_kv_key_dn(module, mem_ctx, dn);
 	TALLOC_FREE(dn);
 	if (!key->dptr) {
 		return ldb_module_oom(module);
@@ -305,7 +305,7 @@ int ltdb_idx_to_key(struct ldb_module *module,
   note that the key for a record can depend on whether a
   GUID index is in use, or the DN is used as the key
 */
-TDB_DATA ltdb_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
+TDB_DATA ldb_kv_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
 		      const struct ldb_message *msg)
 {
 	void *data = ldb_module_get_private(module);
@@ -315,11 +315,11 @@ TDB_DATA ltdb_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
 	int ret;
 
 	if (ltdb->cache->GUID_index_attribute == NULL) {
-		return ltdb_key_dn(module, mem_ctx, msg->dn);
+		return ldb_kv_key_dn(module, mem_ctx, msg->dn);
 	}
 
 	if (ldb_dn_is_special(msg->dn)) {
-		return ltdb_key_dn(module, mem_ctx, msg->dn);
+		return ldb_kv_key_dn(module, mem_ctx, msg->dn);
 	}
 
 	guid_val = ldb_msg_find_ldb_val(msg,
@@ -347,7 +347,7 @@ TDB_DATA ltdb_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
 	}
 	key.dsize = talloc_get_size(key.dptr);
 
-	ret = ltdb_guid_to_key(module, ltdb, guid_val, &key);
+	ret = ldb_kv_guid_to_key(module, ltdb, guid_val, &key);
 
 	if (ret != LDB_SUCCESS) {
 		errno = EINVAL;
@@ -362,7 +362,7 @@ TDB_DATA ltdb_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
   check special dn's have valid attributes
   currently only @ATTRIBUTES is checked
 */
-static int ltdb_check_special_dn(struct ldb_module *module,
+static int ldb_kv_check_special_dn(struct ldb_module *module,
 				 const struct ldb_message *msg)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
@@ -379,7 +379,7 @@ static int ltdb_check_special_dn(struct ldb_module *module,
 		if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) continue;
 
 		for (j = 0; j < msg->elements[i].num_values; j++) {
-			if (ltdb_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
+			if (ldb_kv_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
 				ldb_set_errstring(ldb, "Invalid attribute value in an @ATTRIBUTES entry");
 				return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
 			}
@@ -394,7 +394,7 @@ static int ltdb_check_special_dn(struct ldb_module *module,
   we've made a modification to a dn - possibly reindex and
   update sequence number
 */
-static int ltdb_modified(struct ldb_module *module, struct ldb_dn *dn)
+static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
 {
 	int ret = LDB_SUCCESS;
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
@@ -415,21 +415,21 @@ static int ltdb_modified(struct ldb_module *module, struct ldb_dn *dn)
 				LDB_DEBUG_ERROR, "Reindexing %s due to modification on %s",
 				ltdb->kv_ops->name(ltdb), ldb_dn_get_linearized(dn));
 		}
-		ret = ltdb_reindex(module);
+		ret = ldb_kv_reindex(module);
 	}
 
 	/* If the modify was to a normal record, or any special except @BASEINFO, update the seq number */
 	if (ret == LDB_SUCCESS &&
 	    !(ldb_dn_is_special(dn) &&
 	      ldb_dn_check_special(dn, LTDB_BASEINFO)) ) {
-		ret = ltdb_increase_sequence_number(module);
+		ret = ldb_kv_increase_sequence_number(module);
 	}
 
 	/* If the modify was to @OPTIONS, reload the cache */
 	if (ret == LDB_SUCCESS &&
 	    ldb_dn_is_special(dn) &&
 	    (ldb_dn_check_special(dn, LTDB_OPTIONS)) ) {
-		ret = ltdb_cache_reload(module);
+		ret = ldb_kv_cache_reload(module);
 	}
 
 	if (ret != LDB_SUCCESS) {
@@ -439,7 +439,7 @@ static int ltdb_modified(struct ldb_module *module, struct ldb_dn *dn)
 	return ret;
 }
 
-static int ltdb_tdb_store(struct ltdb_private *ltdb, struct ldb_val ldb_key,
+static int ltdb_store(struct ltdb_private *ltdb, struct ldb_val ldb_key,
 			  struct ldb_val ldb_data, int flags)
 {
 	TDB_DATA key = {
@@ -470,7 +470,10 @@ static const char *ltdb_errorstr(struct ltdb_private *ltdb)
 /*
   store a record into the db
 */
-int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs)
+int ldb_kv_store(
+	struct ldb_module *module,
+	const struct ldb_message *msg,
+	int flgs)
 {
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
@@ -489,7 +492,7 @@ int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flg
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
-	tdb_key = ltdb_key_msg(module, tdb_key_ctx, msg);
+	tdb_key = ldb_kv_key_msg(module, tdb_key_ctx, msg);
 	if (tdb_key.dptr == NULL) {
 		TALLOC_FREE(tdb_key_ctx);
 		return LDB_ERR_OTHER;
@@ -533,7 +536,7 @@ done:
 /*
   check if a attribute is a single valued, for a given element
  */
-static bool ldb_tdb_single_valued(const struct ldb_schema_attribute *a,
+static bool ldb_kv_single_valued(const struct ldb_schema_attribute *a,
 				  struct ldb_message_element *el)
 {
 	if (!a) return false;
@@ -558,7 +561,7 @@ static bool ldb_tdb_single_valued(const struct ldb_schema_attribute *a,
 	return false;
 }
 
-static int ltdb_add_internal(struct ldb_module *module,
+static int ldb_kv_add_internal(struct ldb_module *module,
 			     struct ltdb_private *ltdb,
 			     const struct ldb_message *msg,
 			     bool check_single_value)
@@ -578,7 +581,7 @@ static int ltdb_add_internal(struct ldb_module *module,
 		}
 		if (check_single_value &&
 				el->num_values > 1 &&
-				ldb_tdb_single_valued(a, el)) {
+				ldb_kv_single_valued(a, el)) {
 			ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
 					       el->name, ldb_dn_get_linearized(msg->dn));
 			return LDB_ERR_CONSTRAINT_VIOLATION;
@@ -614,7 +617,7 @@ static int ltdb_add_internal(struct ldb_module *module,
 		}
 	}
 
-	ret = ltdb_store(module, msg, TDB_INSERT);
+	ret = ldb_kv_store(module, msg, TDB_INSERT);
 	if (ret != LDB_SUCCESS) {
 		/*
 		 * Try really hard to get the right error code for
@@ -627,7 +630,7 @@ static int ltdb_add_internal(struct ldb_module *module,
 			if (mem_ctx == NULL) {
 				return ldb_module_operr(module);
 			}
-			ret2 = ltdb_search_base(module, mem_ctx,
+			ret2 = ldb_kv_search_base(module, mem_ctx,
 						msg->dn, &dn2);
 			TALLOC_FREE(mem_ctx);
 			if (ret2 == LDB_SUCCESS) {
@@ -642,7 +645,7 @@ static int ltdb_add_internal(struct ldb_module *module,
 		return ret;
 	}
 
-	ret = ltdb_index_add_new(module, ltdb, msg);
+	ret = ldb_kv_index_add_new(module, ltdb, msg);
 	if (ret != LDB_SUCCESS) {
 		/*
 		 * If we failed to index, delete the message again.
@@ -654,11 +657,11 @@ static int ltdb_add_internal(struct ldb_module *module,
 		 * Note that the caller may not cancel the transation
 		 * and this means the above add might really show up!
 		 */
-		ltdb_delete_noindex(module, msg);
+		ldb_kv_delete_noindex(module, msg);
 		return ret;
 	}
 
-	ret = ltdb_modified(module, msg->dn);
+	ret = ldb_kv_modified(module, msg->dn);
 
 	return ret;
 }
@@ -666,7 +669,7 @@ static int ltdb_add_internal(struct ldb_module *module,
 /*
   add a record to the database
 */
-static int ltdb_add(struct ltdb_context *ctx)
+static int ldb_kv_add(struct ltdb_context *ctx)
 {
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
@@ -684,24 +687,24 @@ static int ltdb_add(struct ltdb_context *ctx)
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
-	ret = ltdb_check_special_dn(module, req->op.add.message);
+	ret = ldb_kv_check_special_dn(module, req->op.add.message);
 	if (ret != LDB_SUCCESS) {
 		return ret;
 	}
 
 	ldb_request_set_state(req, LDB_ASYNC_PENDING);
 
-	if (ltdb_cache_load(module) != 0) {
+	if (ldb_kv_cache_load(module) != 0) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ltdb_add_internal(module, ltdb,
+	ret = ldb_kv_add_internal(module, ltdb,
 				req->op.add.message, true);
 
 	return ret;
 }
 
-static int ltdb_tdb_delete(struct ltdb_private *ltdb, struct ldb_val ldb_key)
+static int ltdb_delete(struct ltdb_private *ltdb, struct ldb_val ldb_key)
 {
 	TDB_DATA tdb_key = {
 		.dptr = ldb_key.data,
@@ -718,7 +721,7 @@ static int ltdb_tdb_delete(struct ltdb_private *ltdb, struct ldb_val ldb_key)
   delete a record from the database, not updating indexes (used for deleting
   index records)
 */
-int ltdb_delete_noindex(struct ldb_module *module,
+int ldb_kv_delete_noindex(struct ldb_module *module,
 			const struct ldb_message *msg)
 {
 	void *data = ldb_module_get_private(module);
@@ -737,7 +740,7 @@ int ltdb_delete_noindex(struct ldb_module *module,
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
-	tdb_key = ltdb_key_msg(module, tdb_key_ctx, msg);
+	tdb_key = ldb_kv_key_msg(module, tdb_key_ctx, msg);
 	if (!tdb_key.dptr) {
 		TALLOC_FREE(tdb_key_ctx);
 		return LDB_ERR_OTHER;
@@ -756,7 +759,7 @@ int ltdb_delete_noindex(struct ldb_module *module,
 	return ret;
 }
 
-static int ltdb_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
+static int ldb_kv_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
 {
 	struct ldb_message *msg;
 	int ret = LDB_SUCCESS;
@@ -768,24 +771,24 @@ static int ltdb_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
 
 	/* in case any attribute of the message was indexed, we need
 	   to fetch the old record */
-	ret = ltdb_search_dn1(module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
+	ret = ldb_kv_search_dn1(module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
 	if (ret != LDB_SUCCESS) {
 		/* not finding the old record is an error */
 		goto done;
 	}
 
-	ret = ltdb_delete_noindex(module, msg);
+	ret = ldb_kv_delete_noindex(module, msg);
 	if (ret != LDB_SUCCESS) {
 		goto done;
 	}
 
 	/* remove any indexed attributes */
-	ret = ltdb_index_delete(module, msg);
+	ret = ldb_kv_index_delete(module, msg);
 	if (ret != LDB_SUCCESS) {
 		goto done;
 	}
 
-	ret = ltdb_modified(module, dn);
+	ret = ldb_kv_modified(module, dn);
 	if (ret != LDB_SUCCESS) {
 		goto done;
 	}
@@ -798,7 +801,7 @@ done:
 /*
   delete a record from the database
 */
-static int ltdb_delete(struct ltdb_context *ctx)
+static int ldb_kv_delete(struct ltdb_context *ctx)
 {
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
@@ -806,11 +809,11 @@ static int ltdb_delete(struct ltdb_context *ctx)
 
 	ldb_request_set_state(req, LDB_ASYNC_PENDING);
 
-	if (ltdb_cache_load(module) != 0) {
+	if (ldb_kv_cache_load(module) != 0) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ltdb_delete_internal(module, req->op.del.dn);
+	ret = ldb_kv_delete_internal(module, req->op.del.dn);
 
 	return ret;
 }
@@ -822,7 +825,7 @@ static int ltdb_delete(struct ltdb_context *ctx)
 
   return the index of the first matching element if found, otherwise -1
 */
-static int find_element(const struct ldb_message *msg, const char *name)
+static int ldb_kv_find_element(const struct ldb_message *msg, const char *name)
 {
 	unsigned int i;
 	for (i=0;i<msg->num_elements;i++) {
@@ -841,7 +844,7 @@ static int find_element(const struct ldb_message *msg, const char *name)
 
   returns 0 on success, -1 on failure (and sets errno)
 */
-static int ltdb_msg_add_element(struct ldb_message *msg,
+static int ldb_kv_msg_add_element(struct ldb_message *msg,
 				struct ldb_message_element *el)
 {
 	struct ldb_message_element *e2;
@@ -884,7 +887,7 @@ static int ltdb_msg_add_element(struct ldb_message *msg,
 /*
   delete all elements having a specified attribute name
 */
-static int msg_delete_attribute(struct ldb_module *module,
+static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
 				struct ltdb_private *ltdb,
 				struct ldb_message *msg, const char *name)
 {
@@ -909,7 +912,7 @@ static int msg_delete_attribute(struct ldb_module *module,
 	}
 	i = el - msg->elements;
 
-	ret = ltdb_index_del_element(module, ltdb, msg, el);
+	ret = ldb_kv_index_del_element(module, ltdb, msg, el);
 	if (ret != LDB_SUCCESS) {
 		return ret;
 	}
@@ -930,7 +933,7 @@ static int msg_delete_attribute(struct ldb_module *module,
 
   return LDB Error on failure
 */
-static int msg_delete_element(struct ldb_module *module,
+static int ldb_kv_msg_delete_element(struct ldb_module *module,
 			      struct ltdb_private *ltdb,
 			      struct ldb_message *msg,
 			      const char *name,
@@ -942,7 +945,7 @@ static int msg_delete_element(struct ldb_module *module,
 	struct ldb_message_element *el;
 	const struct ldb_schema_attribute *a;
 
-	found = find_element(msg, name);
+	found = ldb_kv_find_element(msg, name);
 	if (found == -1) {
 		return LDB_ERR_NO_SUCH_ATTRIBUTE;
 	}
@@ -964,11 +967,11 @@ static int msg_delete_element(struct ldb_module *module,
 		}
 		if (matched) {
 			if (el->num_values == 1) {
-				return msg_delete_attribute(module,
+				return ldb_kv_msg_delete_attribute(module,
 							    ltdb, msg, name);
 			}
 
-			ret = ltdb_index_del_value(module, ltdb, msg, el, i);
+			ret = ldb_kv_index_del_value(module, ltdb, msg, el, i);
 			if (ret != LDB_SUCCESS) {
 				return ret;
 			}
@@ -999,7 +1002,7 @@ static int msg_delete_element(struct ldb_module *module,
 
   'req' is optional, and is used to specify controls if supplied
 */
-int ltdb_modify_internal(struct ldb_module *module,
+int ldb_kv_modify_internal(struct ldb_module *module,
 			 const struct ldb_message *msg,
 			 struct ldb_request *req)
 {
@@ -1027,7 +1030,7 @@ int ltdb_modify_internal(struct ldb_module *module,
 		goto done;
 	}
 
-	ret = ltdb_search_dn1(module, msg->dn,
+	ret = ldb_kv_search_dn1(module, msg->dn,
 			      msg2,
 			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
 	if (ret != LDB_SUCCESS) {
@@ -1077,7 +1080,7 @@ int ltdb_modify_internal(struct ldb_module *module,
 				}
 			}
 
-			if (el->num_values > 1 && ldb_tdb_single_valued(a, el)) {
+			if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
 				ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
 						       el->name, ldb_dn_get_linearized(msg2->dn));
 				ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
@@ -1085,13 +1088,13 @@ int ltdb_modify_internal(struct ldb_module *module,
 			}
 
 			/* Checks if element already exists */
-			idx = find_element(msg2, el->name);
+			idx = ldb_kv_find_element(msg2, el->name);
 			if (idx == -1) {
-				if (ltdb_msg_add_element(msg2, el) != 0) {
+				if (ldb_kv_msg_add_element(msg2, el) != 0) {
 					ret = LDB_ERR_OTHER;
 					goto done;
 				}
-				ret = ltdb_index_add_element(module, ltdb,
+				ret = ldb_kv_index_add_element(module, ltdb,
 							     msg2,
 							     el);
 				if (ret != LDB_SUCCESS) {
@@ -1103,7 +1106,7 @@ int ltdb_modify_internal(struct ldb_module *module,
 
 				/* We cannot add another value on a existing one
 				   if the attribute is single-valued */
-				if (ldb_tdb_single_valued(a, el)) {
+				if (ldb_kv_single_valued(a, el)) {
 					ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
 						               el->name, ldb_dn_get_linearized(msg2->dn));
 					ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
@@ -1173,7 +1176,7 @@ int ltdb_modify_internal(struct ldb_module *module,
 				el2->values = vals;
 				el2->num_values += el->num_values;
 
-				ret = ltdb_index_add_element(module, ltdb,
+				ret = ldb_kv_index_add_element(module, ltdb,
 							     msg2, el);
 				if (ret != LDB_SUCCESS) {
 					goto done;
@@ -1184,7 +1187,7 @@ int ltdb_modify_internal(struct ldb_module *module,
 
 		case LDB_FLAG_MOD_REPLACE:
 
-			if (el->num_values > 1 && ldb_tdb_single_valued(a, el)) {
+			if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
 				ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
 						       el->name, ldb_dn_get_linearized(msg2->dn));
 				ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
@@ -1221,7 +1224,7 @@ int ltdb_modify_internal(struct ldb_module *module,
 			}
 
 			/* Checks if element already exists */
-			idx = find_element(msg2, el->name);
+			idx = ldb_kv_find_element(msg2, el->name);
 			if (idx != -1) {
 				j = (unsigned int) idx;
 				el2 = &(msg2->elements[j]);
@@ -1238,7 +1241,7 @@ int ltdb_modify_internal(struct ldb_module *module,
 				}
 
 				/* Delete the attribute if it exists in the DB */
-				if (msg_delete_attribute(module, ltdb,
+				if (ldb_kv_msg_delete_attribute(module, ltdb,
 							 msg2,
 							 el->name) != 0) {
 					ret = LDB_ERR_OTHER;
@@ -1247,12 +1250,12 @@ int ltdb_modify_internal(struct ldb_module *module,
 			}
 
 			/* Recreate it with the new values */
-			if (ltdb_msg_add_element(msg2, el) != 0) {
+			if (ldb_kv_msg_add_element(msg2, el) != 0) {
 				ret = LDB_ERR_OTHER;
 				goto done;
 			}
 
-			ret = ltdb_index_add_element(module, ltdb,
+			ret = ldb_kv_index_add_element(module, ltdb,
 						     msg2, el);
 			if (ret != LDB_SUCCESS) {
 				goto done;
@@ -1269,7 +1272,7 @@ int ltdb_modify_internal(struct ldb_module *module,
 
 			if (msg->elements[i].num_values == 0) {
 				/* Delete the whole attribute */
-				ret = msg_delete_attribute(module,
+				ret = ldb_kv_msg_delete_attribute(module,
 							   ltdb,
 							   msg2,
 							   msg->elements[i].name);
@@ -1287,7 +1290,7 @@ int ltdb_modify_internal(struct ldb_module *module,
 			} else {
 				/* Delete specified values from an attribute */
 				for (j=0; j < msg->elements[i].num_values; j++) {
-					ret = msg_delete_element(module,
+					ret = ldb_kv_msg_delete_element(module,
 								 ltdb,
 							         msg2,
 							         msg->elements[i].name,
@@ -1316,12 +1319,12 @@ int ltdb_modify_internal(struct ldb_module *module,
 		}
 	}
 
-	ret = ltdb_store(module, msg2, TDB_MODIFY);
+	ret = ldb_kv_store(module, msg2, TDB_MODIFY);
 	if (ret != LDB_SUCCESS) {
 		goto done;
 	}
 
-	ret = ltdb_modified(module, msg2->dn);
+	ret = ldb_kv_modified(module, msg2->dn);
 	if (ret != LDB_SUCCESS) {
 		goto done;
 	}
@@ -1334,24 +1337,24 @@ done:
 /*
   modify a record
 */
-static int ltdb_modify(struct ltdb_context *ctx)
+static int ldb_kv_modify(struct ltdb_context *ctx)
 {
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
 	int ret = LDB_SUCCESS;
 
-	ret = ltdb_check_special_dn(module, req->op.mod.message);
+	ret = ldb_kv_check_special_dn(module, req->op.mod.message);
 	if (ret != LDB_SUCCESS) {
 		return ret;
 	}
 
 	ldb_request_set_state(req, LDB_ASYNC_PENDING);
 
-	if (ltdb_cache_load(module) != 0) {
+	if (ldb_kv_cache_load(module) != 0) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ltdb_modify_internal(module, req->op.mod.message, req);
+	ret = ldb_kv_modify_internal(module, req->op.mod.message, req);
 
 	return ret;
 }
@@ -1359,7 +1362,7 @@ static int ltdb_modify(struct ltdb_context *ctx)
 /*
   rename a record
 */
-static int ltdb_rename(struct ltdb_context *ctx)
+static int ldb_kv_rename(struct ltdb_context *ctx)
 {
 	struct ldb_module *module = ctx->module;
 	void *data = ldb_module_get_private(module);
@@ -1372,7 +1375,7 @@ static int ltdb_rename(struct ltdb_context *ctx)
 
 	ldb_request_set_state(req, LDB_ASYNC_PENDING);
 
-	if (ltdb_cache_load(ctx->module) != 0) {
+	if (ldb_kv_cache_load(ctx->module) != 0) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
@@ -1382,7 +1385,7 @@ static int ltdb_rename(struct ltdb_context *ctx)
 	}
 
 	/* we need to fetch the old record to re-add under the new name */
-	ret = ltdb_search_dn1(module, req->op.rename.olddn, msg,
+	ret = ldb_kv_search_dn1(module, req->op.rename.olddn, msg,
 			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
 	if (ret != LDB_SUCCESS) {
 		/* not finding the old record is an error */
@@ -1396,13 +1399,13 @@ static int ltdb_rename(struct ltdb_context *ctx)
 	 * Even in GUID index mode we use ltdb_key_dn() as we are
 	 * trying to figure out if this is just a case rename
 	 */
-	tdb_key = ltdb_key_dn(module, msg, req->op.rename.newdn);
+	tdb_key = ldb_kv_key_dn(module, msg, req->op.rename.newdn);
 	if (!tdb_key.dptr) {
 		talloc_free(msg);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	tdb_key_old = ltdb_key_dn(module, msg, req->op.rename.olddn);
+	tdb_key_old = ldb_kv_key_dn(module, msg, req->op.rename.olddn);
 	if (!tdb_key_old.dptr) {
 		talloc_free(msg);
 		talloc_free(tdb_key.dptr);
@@ -1415,7 +1418,7 @@ static int ltdb_rename(struct ltdb_context *ctx)
 	 */
 	if (tdb_key_old.dsize != tdb_key.dsize
 	    || memcmp(tdb_key.dptr, tdb_key_old.dptr, tdb_key.dsize) != 0) {
-		ret = ltdb_search_base(module, msg,
+		ret = ldb_kv_search_base(module, msg,
 				       req->op.rename.newdn,
 				       &db_dn);
 		if (ret == LDB_SUCCESS) {
@@ -1446,7 +1449,7 @@ static int ltdb_rename(struct ltdb_context *ctx)
 	 * unique indexes. We rely on the transaction to make this
 	 * atomic
 	 */
-	ret = ltdb_delete_internal(module, msg->dn);
+	ret = ldb_kv_delete_internal(module, msg->dn);
 	if (ret != LDB_SUCCESS) {
 		talloc_free(msg);
 		return ret;
@@ -1462,14 +1465,14 @@ static int ltdb_rename(struct ltdb_context *ctx)
 	 * deleted attributes. We could go through all elements but that's
 	 * maybe not the most efficient way
 	 */
-	ret = ltdb_add_internal(module, ltdb, msg, false);
+	ret = ldb_kv_add_internal(module, ltdb, msg, false);
 
 	talloc_free(msg);
 
 	return ret;
 }
 
-static int ltdb_tdb_transaction_start(struct ltdb_private *ltdb)
+static int ltdb_transaction_start(struct ltdb_private *ltdb)
 {
 	pid_t pid = getpid();
 
@@ -1486,7 +1489,7 @@ static int ltdb_tdb_transaction_start(struct ltdb_private *ltdb)
 	return tdb_transaction_start(ltdb->tdb);
 }
 
-static int ltdb_tdb_transaction_cancel(struct ltdb_private *ltdb)
+static int ltdb_transaction_cancel(struct ltdb_private *ltdb)
 {
 	pid_t pid = getpid();
 
@@ -1503,7 +1506,7 @@ static int ltdb_tdb_transaction_cancel(struct ltdb_private *ltdb)
 	return tdb_transaction_cancel(ltdb->tdb);
 }
 
-static int ltdb_tdb_transaction_prepare_commit(struct ltdb_private *ltdb)
+static int ltdb_transaction_prepare_commit(struct ltdb_private *ltdb)
 {
 	pid_t pid = getpid();
 
@@ -1520,7 +1523,7 @@ static int ltdb_tdb_transaction_prepare_commit(struct ltdb_private *ltdb)
 	return tdb_transaction_prepare_commit(ltdb->tdb);
 }
 
-static int ltdb_tdb_transaction_commit(struct ltdb_private *ltdb)
+static int ltdb_transaction_commit(struct ltdb_private *ltdb)
 {
 	pid_t pid = getpid();
 
@@ -1537,7 +1540,7 @@ static int ltdb_tdb_transaction_commit(struct ltdb_private *ltdb)
 	return tdb_transaction_commit(ltdb->tdb);
 }
 
-static int ltdb_start_trans(struct ldb_module *module)
+static int ldb_kv_start_trans(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
@@ -1564,7 +1567,7 @@ static int ltdb_start_trans(struct ldb_module *module)
 	}
 
 
-	ltdb_index_transaction_start(module);
+	ldb_kv_index_transaction_start(module);
 
 	ltdb->reindex_failed = false;
 
@@ -1575,9 +1578,9 @@ static int ltdb_start_trans(struct ldb_module *module)
  * Forward declaration to allow prepare_commit to in fact abort the
  * transaction
  */
-static int ltdb_del_trans(struct ldb_module *module);
+static int ldb_kv_del_trans(struct ldb_module *module);
 
-static int ltdb_prepare_commit(struct ldb_module *module)
+static int ldb_kv_prepare_commit(struct ldb_module *module)
 {
 	int ret;
 	void *data = ldb_module_get_private(module);
@@ -1612,14 +1615,14 @@ static int ltdb_prepare_commit(struct ldb_module *module)
 		 * We must instead abort the transaction so we get the
 		 * old values and old index back
 		 */
-		ltdb_del_trans(module);
+		ldb_kv_del_trans(module);
 		ldb_set_errstring(ldb_module_get_ctx(module),
 				  "Failure during re-index, so "
 				  "transaction must be aborted.");
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ltdb_index_transaction_commit(module);
+	ret = ldb_kv_index_transaction_commit(module);
 	if (ret != LDB_SUCCESS) {
 		ltdb->kv_ops->abort_write(ltdb);
 		return ret;
@@ -1641,14 +1644,14 @@ static int ltdb_prepare_commit(struct ldb_module *module)
 	return LDB_SUCCESS;
 }
 
-static int ltdb_end_trans(struct ldb_module *module)
+static int ldb_kv_end_trans(struct ldb_module *module)
 {
 	int ret;
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
 
 	if (!ltdb->prepared_commit) {
-		ret = ltdb_prepare_commit(module);
+		ret = ldb_kv_prepare_commit(module);
 		if (ret != LDB_SUCCESS) {
 			return ret;
 		}
@@ -1668,13 +1671,13 @@ static int ltdb_end_trans(struct ldb_module *module)
 	return LDB_SUCCESS;
 }
 
-static int ltdb_del_trans(struct ldb_module *module)
+static int ldb_kv_del_trans(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
 
 
-	if (ltdb_index_transaction_cancel(module) != 0) {
+	if (ldb_kv_index_transaction_cancel(module) != 0) {
 		ltdb->kv_ops->abort_write(ltdb);
 		return ltdb->kv_ops->error(ltdb);
 	}
@@ -1686,7 +1689,7 @@ static int ltdb_del_trans(struct ldb_module *module)
 /*
   return sequenceNumber from @BASEINFO
 */
-static int ltdb_sequence_number(struct ltdb_context *ctx,
+static int ldb_kv_sequence_number(struct ltdb_context *ctx,
 				struct ldb_extended **ext)
 {
 	struct ldb_context *ldb;
@@ -1740,7 +1743,7 @@ static int ltdb_sequence_number(struct ltdb_context *ctx,
 		goto done;
 	}
 
-	ret = ltdb_search_dn1(module, dn, msg, 0);
+	ret = ldb_kv_search_dn1(module, dn, msg, 0);
 	if (ret != LDB_SUCCESS) {
 		goto done;
 	}
@@ -1779,7 +1782,7 @@ done:
 	return ret;
 }
 
-static void ltdb_request_done(struct ltdb_context *ctx, int error)
+static void ldb_kv_request_done(struct ltdb_context *ctx, int error)
 {
 	struct ldb_context *ldb;
 	struct ldb_request *req;
@@ -1805,7 +1808,7 @@ static void ltdb_request_done(struct ltdb_context *ctx, int error)
 	req->callback(req, ares);
 }
 
-static void ltdb_timeout(struct tevent_context *ev,
+static void ldb_kv_timeout(struct tevent_context *ev,
 			  struct tevent_timer *te,
 			  struct timeval t,
 			  void *private_data)
@@ -1815,7 +1818,7 @@ static void ltdb_timeout(struct tevent_context *ev,
 
 	if (!ctx->request_terminated) {
 		/* request is done now */
-		ltdb_request_done(ctx, LDB_ERR_TIME_LIMIT_EXCEEDED);
+		ldb_kv_request_done(ctx, LDB_ERR_TIME_LIMIT_EXCEEDED);
 	}
 
 	if (ctx->spy) {
@@ -1826,7 +1829,7 @@ static void ltdb_timeout(struct tevent_context *ev,
 	talloc_free(ctx);
 }
 
-static void ltdb_request_extended_done(struct ltdb_context *ctx,
+static void ldb_kv_request_extended_done(struct ltdb_context *ctx,
 					struct ldb_extended *ext,
 					int error)
 {
@@ -1855,7 +1858,7 @@ static void ltdb_request_extended_done(struct ltdb_context *ctx,
 	req->callback(req, ares);
 }
 
-static void ltdb_handle_extended(struct ltdb_context *ctx)
+static void ldb_kv_handle_extended(struct ltdb_context *ctx)
 {
 	struct ldb_extended *ext = NULL;
 	int ret;
@@ -1863,13 +1866,13 @@ static void ltdb_handle_extended(struct ltdb_context *ctx)
 	if (strcmp(ctx->req->op.extended.oid,
 		   LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
 		/* get sequence number */
-		ret = ltdb_sequence_number(ctx, &ext);
+		ret = ldb_kv_sequence_number(ctx, &ext);
 	} else {
 		/* not recognized */
 		ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
 	}
 
-	ltdb_request_extended_done(ctx, ext, ret);
+	ldb_kv_request_extended_done(ctx, ext, ret);
 }
 
 struct kv_ctx {
@@ -1881,7 +1884,7 @@ struct kv_ctx {
 		      void *private_data);
 };
 
-static int ldb_tdb_traverse_fn_wrapper(struct tdb_context *tdb, TDB_DATA tdb_key, TDB_DATA tdb_data, void *ctx)
+static int ltdb_traverse_fn_wrapper(struct tdb_context *tdb, TDB_DATA tdb_key, TDB_DATA tdb_data, void *ctx)
 {
 	struct kv_ctx *kv_ctx = ctx;
 	struct ldb_val key = {
@@ -1895,7 +1898,7 @@ static int ldb_tdb_traverse_fn_wrapper(struct tdb_context *tdb, TDB_DATA tdb_key
 	return kv_ctx->kv_traverse_fn(kv_ctx->ltdb, key, data, kv_ctx->ctx);
 }
 
-static int ltdb_tdb_traverse_fn(struct ltdb_private *ltdb, ldb_kv_traverse_fn fn, void *ctx)
+static int ltdb_traverse_fn(struct ltdb_private *ltdb, ldb_kv_traverse_fn fn, void *ctx)
 {
 	struct kv_ctx kv_ctx = {
 		.kv_traverse_fn = fn,
@@ -1903,13 +1906,13 @@ static int ltdb_tdb_traverse_fn(struct ltdb_private *ltdb, ldb_kv_traverse_fn fn
 		.ltdb = ltdb
 	};
 	if (tdb_transaction_active(ltdb->tdb)) {
-		return tdb_traverse(ltdb->tdb, ldb_tdb_traverse_fn_wrapper, &kv_ctx);
+		return tdb_traverse(ltdb->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
 	} else {
-		return tdb_traverse_read(ltdb->tdb, ldb_tdb_traverse_fn_wrapper, &kv_ctx);
+		return tdb_traverse_read(ltdb->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
 	}
 }
 
-static int ltdb_tdb_update_in_iterate(struct ltdb_private *ltdb,
+static int ltdb_update_in_iterate(struct ltdb_private *ltdb,
 				      struct ldb_val ldb_key,
 				      struct ldb_val ldb_key2,
 				      struct ldb_val ldb_data, void *state)
@@ -1961,7 +1964,7 @@ static int ltdb_tdb_update_in_iterate(struct ltdb_private *ltdb,
 	return tdb_ret;
 }
 
-static int ltdb_tdb_parse_record_wrapper(TDB_DATA tdb_key, TDB_DATA tdb_data,
+static int ltdb_parse_record_wrapper(TDB_DATA tdb_key, TDB_DATA tdb_data,
 					 void *ctx)
 {
 	struct kv_ctx *kv_ctx = ctx;
@@ -1977,7 +1980,7 @@ static int ltdb_tdb_parse_record_wrapper(TDB_DATA tdb_key, TDB_DATA tdb_data,
 	return kv_ctx->parser(key, data, kv_ctx->ctx);
 }
 
-static int ltdb_tdb_parse_record(struct ltdb_private *ltdb,
+static int ltdb_parse_record(struct ltdb_private *ltdb,
 				 struct ldb_val ldb_key,
 				 int (*parser)(struct ldb_val key,
 					       struct ldb_val data,
@@ -2000,7 +2003,7 @@ static int ltdb_tdb_parse_record(struct ltdb_private *ltdb,
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	ret = tdb_parse_record(ltdb->tdb, key, ltdb_tdb_parse_record_wrapper,
+	ret = tdb_parse_record(ltdb->tdb, key, ltdb_parse_record_wrapper,
 			       &kv_ctx);
 	if (ret == 0) {
 		return LDB_SUCCESS;
@@ -2008,12 +2011,12 @@ static int ltdb_tdb_parse_record(struct ltdb_private *ltdb,
 	return ltdb_err_map(tdb_error(ltdb->tdb));
 }
 
-static const char * ltdb_tdb_name(struct ltdb_private *ltdb)
+static const char * ltdb_name(struct ltdb_private *ltdb)
 {
 	return tdb_name(ltdb->tdb);
 }
 
-static bool ltdb_tdb_changed(struct ltdb_private *ltdb)
+static bool ltdb_changed(struct ltdb_private *ltdb)
 {
 	int seq = tdb_get_seqnum(ltdb->tdb);
 	bool has_changed = (seq != ltdb->tdb_seqnum);
@@ -2029,25 +2032,25 @@ static bool ltdb_transaction_active(struct ltdb_private *ltdb)
 }
 
 static const struct kv_db_ops key_value_ops = {
-	.store = ltdb_tdb_store,
-	.delete = ltdb_tdb_delete,
-	.iterate = ltdb_tdb_traverse_fn,
-	.update_in_iterate = ltdb_tdb_update_in_iterate,
-	.fetch_and_parse = ltdb_tdb_parse_record,
+	.store = ltdb_store,
+	.delete = ltdb_delete,
+	.iterate = ltdb_traverse_fn,
+	.update_in_iterate = ltdb_update_in_iterate,
+	.fetch_and_parse = ltdb_parse_record,
 	.lock_read = ltdb_lock_read,
 	.unlock_read = ltdb_unlock_read,
-	.begin_write = ltdb_tdb_transaction_start,
-	.prepare_write = ltdb_tdb_transaction_prepare_commit,
-	.finish_write = ltdb_tdb_transaction_commit,
-	.abort_write = ltdb_tdb_transaction_cancel,
+	.begin_write = ltdb_transaction_start,
+	.prepare_write = ltdb_transaction_prepare_commit,
+	.finish_write = ltdb_transaction_commit,
+	.abort_write = ltdb_transaction_cancel,
 	.error = ltdb_error,
 	.errorstr = ltdb_errorstr,
-	.name = ltdb_tdb_name,
-	.has_changed = ltdb_tdb_changed,
+	.name = ltdb_name,
+	.has_changed = ltdb_changed,
 	.transaction_active = ltdb_transaction_active,
 };
 
-static void ltdb_callback(struct tevent_context *ev,
+static void ldb_kv_callback(struct tevent_context *ev,
 			  struct tevent_timer *te,
 			  struct timeval t,
 			  void *private_data)
@@ -2063,22 +2066,22 @@ static void ltdb_callback(struct tevent_context *ev,
 
 	switch (ctx->req->operation) {
 	case LDB_SEARCH:
-		ret = ltdb_search(ctx);
+		ret = ldb_kv_search(ctx);
 		break;
 	case LDB_ADD:
-		ret = ltdb_add(ctx);
+		ret = ldb_kv_add(ctx);
 		break;
 	case LDB_MODIFY:
-		ret = ltdb_modify(ctx);
+		ret = ldb_kv_modify(ctx);
 		break;
 	case LDB_DELETE:
-		ret = ltdb_delete(ctx);
+		ret = ldb_kv_delete(ctx);
 		break;
 	case LDB_RENAME:
-		ret = ltdb_rename(ctx);
+		ret = ldb_kv_rename(ctx);
 		break;
 	case LDB_EXTENDED:
-		ltdb_handle_extended(ctx);
+		ldb_kv_handle_extended(ctx);
 		goto done;
 	default:
 		/* no other op supported */
@@ -2087,7 +2090,7 @@ static void ltdb_callback(struct tevent_context *ev,
 
 	if (!ctx->request_terminated) {
 		/* request is done now */
-		ltdb_request_done(ctx, ret);
+		ldb_kv_request_done(ctx, ret);
 	}
 
 done:
@@ -2099,7 +2102,7 @@ done:
 	talloc_free(ctx);
 }
 
-static int ltdb_request_destructor(void *ptr)
+static int ldb_kv_request_destructor(void *ptr)
 {
 	struct ltdb_req_spy *spy = talloc_get_type(ptr, struct ltdb_req_spy);
 
@@ -2112,7 +2115,7 @@ static int ltdb_request_destructor(void *ptr)
 	return 0;
 }
 
-static int ltdb_handle_request(struct ldb_module *module,
+static int ldb_kv_handle_request(struct ldb_module *module,
 			       struct ldb_request *req)
 {
 	struct ldb_control *control_permissive;
@@ -2155,7 +2158,7 @@ static int ltdb_handle_request(struct ldb_module *module,
 
 	tv.tv_sec = 0;
 	tv.tv_usec = 0;
-	te = tevent_add_timer(ev, ac, tv, ltdb_callback, ac);
+	te = tevent_add_timer(ev, ac, tv, ldb_kv_callback, ac);
 	if (NULL == te) {
 		talloc_free(ac);
 		return LDB_ERR_OPERATIONS_ERROR;
@@ -2165,7 +2168,7 @@ static int ltdb_handle_request(struct ldb_module *module,
 		tv.tv_sec = req->starttime + req->timeout;
 		tv.tv_usec = 0;
 		ac->timeout_event = tevent_add_timer(ev, ac, tv,
-						     ltdb_timeout, ac);
+						     ldb_kv_timeout, ac);
 		if (NULL == ac->timeout_event) {
 			talloc_free(ac);
 			return LDB_ERR_OPERATIONS_ERROR;
@@ -2181,12 +2184,12 @@ static int ltdb_handle_request(struct ldb_module *module,
 	}
 	ac->spy->ctx = ac;
 
-	talloc_set_destructor((TALLOC_CTX *)ac->spy, ltdb_request_destructor);
+	talloc_set_destructor((TALLOC_CTX *)ac->spy, ldb_kv_request_destructor);
 
 	return LDB_SUCCESS;
 }
 
-static int ltdb_init_rootdse(struct ldb_module *module)
+static int ldb_kv_init_rootdse(struct ldb_module *module)
 {
 	/* ignore errors on this - we expect it for non-sam databases */
 	ldb_mod_register_control(module, LDB_CONTROL_PERMISSIVE_MODIFY_OID);
@@ -2196,38 +2199,38 @@ static int ltdb_init_rootdse(struct ldb_module *module)
 }
 
 
-static int generic_lock_read(struct ldb_module *module)
+static int ldb_kv_lock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
 	return ltdb->kv_ops->lock_read(module);
 }
 
-static int generic_unlock_read(struct ldb_module *module)
+static int ldb_kv_unlock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
 	return ltdb->kv_ops->unlock_read(module);
 }
 
-static const struct ldb_module_ops ltdb_ops = {
+static const struct ldb_module_ops ldb_kv_ops = {
 	.name              = "tdb",
-	.init_context      = ltdb_init_rootdse,
-	.search            = ltdb_handle_request,
-	.add               = ltdb_handle_request,
-	.modify            = ltdb_handle_request,
-	.del               = ltdb_handle_request,
-	.rename            = ltdb_handle_request,
-	.extended          = ltdb_handle_request,
-	.start_transaction = ltdb_start_trans,
-	.end_transaction   = ltdb_end_trans,
-	.prepare_commit    = ltdb_prepare_commit,
-	.del_transaction   = ltdb_del_trans,
-	.read_lock         = generic_lock_read,
-	.read_unlock       = generic_unlock_read,
+	.init_context      = ldb_kv_init_rootdse,
+	.search            = ldb_kv_handle_request,
+	.add               = ldb_kv_handle_request,
+	.modify            = ldb_kv_handle_request,
+	.del               = ldb_kv_handle_request,
+	.rename            = ldb_kv_handle_request,
+	.extended          = ldb_kv_handle_request,
+	.start_transaction = ldb_kv_start_trans,
+	.end_transaction   = ldb_kv_end_trans,
+	.prepare_commit    = ldb_kv_prepare_commit,
+	.del_transaction   = ldb_kv_del_trans,
+	.read_lock         = ldb_kv_lock_read,
+	.read_unlock       = ldb_kv_unlock_read,
 };
 
-int init_store(struct ltdb_private *ltdb,
+int ldb_kv_init_store(struct ltdb_private *ltdb,
 		      const char *name,
 		      struct ldb_context *ldb,
 		      const char *options[],
@@ -2245,7 +2248,7 @@ int init_store(struct ltdb_private *ltdb,
 
 	ltdb->pid = getpid();
 
-	ltdb->module = ldb_module_new(ldb, ldb, name, &ltdb_ops);
+	ltdb->module = ldb_module_new(ldb, ldb, name, &ldb_kv_ops);
 	if (!ltdb->module) {
 		ldb_oom(ldb);
 		talloc_free(ltdb);
@@ -2254,7 +2257,7 @@ int init_store(struct ltdb_private *ltdb,
 	ldb_module_set_private(ltdb->module, ltdb);
 	talloc_steal(ltdb->module, ltdb);
 
-	if (ltdb_cache_load(ltdb->module) != 0) {
+	if (ldb_kv_cache_load(ltdb->module) != 0) {
 		ldb_asprintf_errstring(ldb, "Unable to load ltdb cache "
 				       "records for backend '%s'", name);
 		talloc_free(ltdb->module);
@@ -2390,5 +2393,6 @@ int ltdb_connect(struct ldb_context *ldb, const char *url,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	return init_store(ltdb, "ldb_tdb backend", ldb, options, _module);
+	return ldb_kv_init_store(
+	    ltdb, "ldb_tdb backend", ldb, options, _module);
 }
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.h b/lib/ldb/ldb_tdb/ldb_tdb.h
index 2896c63..675355e 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -153,37 +153,37 @@ struct ltdb_reindex_context {
 
 /* The following definitions come from lib/ldb/ldb_tdb/ldb_cache.c  */
 
-int ltdb_cache_reload(struct ldb_module *module);
-int ltdb_cache_load(struct ldb_module *module);
-int ltdb_increase_sequence_number(struct ldb_module *module);
-int ltdb_check_at_attributes_values(const struct ldb_val *value);
+int ldb_kv_cache_reload(struct ldb_module *module);
+int ldb_kv_cache_load(struct ldb_module *module);
+int ldb_kv_increase_sequence_number(struct ldb_module *module);
+int ldb_kv_check_at_attributes_values(const struct ldb_val *value);
 
 /* The following definitions come from lib/ldb/ldb_tdb/ldb_index.c  */
 
 struct ldb_parse_tree;
 
-int ltdb_search_indexed(struct ltdb_context *ctx, uint32_t *);
-int ltdb_index_add_new(struct ldb_module *module,
+int ldb_kv_search_indexed(struct ltdb_context *ctx, uint32_t *);
+int ldb_kv_index_add_new(struct ldb_module *module,
 		       struct ltdb_private *ltdb,
 		       const struct ldb_message *msg);
-int ltdb_index_delete(struct ldb_module *module, const struct ldb_message *msg);
-int ltdb_index_del_element(struct ldb_module *module,
+int ldb_kv_index_delete(struct ldb_module *module, const struct ldb_message *msg);
+int ldb_kv_index_del_element(struct ldb_module *module,
 			   struct ltdb_private *ltdb,
 			   const struct ldb_message *msg,
 			   struct ldb_message_element *el);
-int ltdb_index_add_element(struct ldb_module *module,
+int ldb_kv_index_add_element(struct ldb_module *module,
 			   struct ltdb_private *ltdb,
 			   const struct ldb_message *msg,
 			   struct ldb_message_element *el);
-int ltdb_index_del_value(struct ldb_module *module,
+int ldb_kv_index_del_value(struct ldb_module *module,
 			 struct ltdb_private *ltdb,
 			 const struct ldb_message *msg,
 			 struct ldb_message_element *el, unsigned int v_idx);
-int ltdb_reindex(struct ldb_module *module);
-int ltdb_index_transaction_start(struct ldb_module *module);
-int ltdb_index_transaction_commit(struct ldb_module *module);
-int ltdb_index_transaction_cancel(struct ldb_module *module);
-int ltdb_key_dn_from_idx(struct ldb_module *module,
+int ldb_kv_reindex(struct ldb_module *module);
+int ldb_kv_index_transaction_start(struct ldb_module *module);
+int ldb_kv_index_transaction_commit(struct ldb_module *module);
+int ldb_kv_index_transaction_cancel(struct ldb_module *module);
+int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 			 struct ltdb_private *ltdb,
 			 TALLOC_CTX *mem_ctx,
 			 struct ldb_dn *dn,
@@ -194,44 +194,44 @@ int ltdb_key_dn_from_idx(struct ldb_module *module,
 int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name, 
 		      const struct ldb_val *val);
 void ltdb_search_dn1_free(struct ldb_module *module, struct ldb_message *msg);
-int ltdb_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg,
+int ldb_kv_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg,
 		    unsigned int unpack_flags);
-int ltdb_search_base(struct ldb_module *module,
+int ldb_kv_search_base(struct ldb_module *module,
 		     TALLOC_CTX *mem_ctx,
 		     struct ldb_dn *dn,
 		     struct ldb_dn **ret_dn);
-int ltdb_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
+int ldb_kv_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
 		    struct TDB_DATA tdb_key,
 		    struct ldb_message *msg,
 		    unsigned int unpack_flags);
-int ltdb_filter_attrs(TALLOC_CTX *mem_ctx,
+int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
 		      const struct ldb_message *msg, const char * const *attrs,
 		      struct ldb_message **filtered_msg);
-int ltdb_search(struct ltdb_context *ctx);
+int ldb_kv_search(struct ltdb_context *ctx);
 
 /* The following definitions come from lib/ldb/ldb_tdb/ldb_tdb.c  */
 /* 
  * Determine if this key could hold a record.  We allow the new GUID
  * index, the old DN index and a possible future ID=
  */
-bool ltdb_key_is_record(TDB_DATA key);
-TDB_DATA ltdb_key_dn(struct ldb_module *module, TALLOC_CTX *mem_ctx,
+bool ldb_kv_key_is_record(TDB_DATA key);
+TDB_DATA ldb_kv_key_dn(struct ldb_module *module, TALLOC_CTX *mem_ctx,
 		     struct ldb_dn *dn);
-TDB_DATA ltdb_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
+TDB_DATA ldb_kv_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
 		      const struct ldb_message *msg);
-int ltdb_guid_to_key(struct ldb_module *module,
+int ldb_kv_guid_to_key(struct ldb_module *module,
 		     struct ltdb_private *ltdb,
 		     const struct ldb_val *GUID_val,
 		     TDB_DATA *key);
-int ltdb_idx_to_key(struct ldb_module *module,
+int ldb_kv_idx_to_key(struct ldb_module *module,
 		    struct ltdb_private *ltdb,
 		    TALLOC_CTX *mem_ctx,
 		    const struct ldb_val *idx_val,
 		    TDB_DATA *key);
 TDB_DATA ltdb_key(struct ldb_module *module, struct ldb_dn *dn);
-int ltdb_store(struct ldb_module *module, const struct ldb_message *msg, int flgs);
-int ltdb_modify_internal(struct ldb_module *module, const struct ldb_message *msg, struct ldb_request *req);
-int ltdb_delete_noindex(struct ldb_module *module,
+int ldb_kv_store(struct ldb_module *module, const struct ldb_message *msg, int flgs);
+int ldb_kv_modify_internal(struct ldb_module *module, const struct ldb_message *msg, struct ldb_request *req);
+int ldb_kv_delete_noindex(struct ldb_module *module,
 			const struct ldb_message *msg);
 int ltdb_err_map(enum TDB_ERROR tdb_code);
 
@@ -239,9 +239,9 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
 				   const char *path, int hash_size, int tdb_flags,
 				   int open_flags, mode_t mode,
 				   struct ldb_context *ldb);
-int init_store(struct ltdb_private *ltdb, const char *name,
-	       struct ldb_context *ldb, const char *options[],
-	       struct ldb_module **_module);
+int ldb_kv_init_store(struct ltdb_private *ltdb, const char *name,
+		      struct ldb_context *ldb, const char *options[],
+		      struct ldb_module **_module);
 
 int ltdb_connect(struct ldb_context *ldb, const char *url,
 		 unsigned int flags, const char *options[],
-- 
2.7.4


From ee7c16944bad7d5f890c32464b762a60eb3e2bf6 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 07:25:32 +1200
Subject: [PATCH 02/18] lib ldb: fix formatting of ldb_kv rename.

Clean up the code format after the rename in the previous commit.
Hopefully doing a rename commit followed by a reformat commit makes the
code easier to review.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_tdb/ldb_index.c  | 315 ++++++++++++++++++++++---------------------
 lib/ldb/ldb_tdb/ldb_search.c |  59 ++++----
 lib/ldb/ldb_tdb/ldb_tdb.c    | 273 +++++++++++++++++++------------------
 lib/ldb/ldb_tdb/ldb_tdb.h    |  98 ++++++++------
 4 files changed, 384 insertions(+), 361 deletions(-)

diff --git a/lib/ldb/ldb_tdb/ldb_index.c b/lib/ldb/ldb_tdb/ldb_index.c
index 75073a1..23b8aa0 100644
--- a/lib/ldb/ldb_tdb/ldb_index.c
+++ b/lib/ldb/ldb_tdb/ldb_index.c
@@ -309,7 +309,9 @@ static int ldb_kv_dn_list_find_msg(struct ltdb_private *ltdb,
   checks and also copes with CPUs that are fussy about pointer
   alignment
  */
-static struct dn_list *ldb_kv_index_idxptr(struct ldb_module *module, TDB_DATA rec, bool check_parent)
+static struct dn_list *ldb_kv_index_idxptr(struct ldb_module *module,
+					   TDB_DATA rec,
+					   bool check_parent)
 {
 	struct dn_list *list;
 	if (rec.dsize != sizeof(void *)) {
@@ -342,8 +344,9 @@ static struct dn_list *ldb_kv_index_idxptr(struct ldb_module *module, TDB_DATA r
   struct dn_list
  */
 static int ldb_kv_dn_list_load(struct ldb_module *module,
-			     struct ltdb_private *ltdb,
-			     struct ldb_dn *dn, struct dn_list *list)
+			       struct ltdb_private *ltdb,
+			       struct ldb_dn *dn,
+			       struct dn_list *list)
 {
 	struct ldb_message *msg;
 	int ret, version;
@@ -386,9 +389,11 @@ normal_index:
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ldb_kv_search_dn1(module, dn, msg,
-			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
-			      |LDB_UNPACK_DATA_FLAG_NO_DN);
+	ret = ldb_kv_search_dn1(module,
+				dn,
+				msg,
+				LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
+				    LDB_UNPACK_DATA_FLAG_NO_DN);
 	if (ret != LDB_SUCCESS) {
 		talloc_free(msg);
 		return ret;
@@ -474,10 +479,10 @@ normal_index:
 }
 
 int ldb_kv_key_dn_from_idx(struct ldb_module *module,
-			 struct ltdb_private *ltdb,
-			 TALLOC_CTX *mem_ctx,
-			 struct ldb_dn *dn,
-			 TDB_DATA *tdb_key)
+			   struct ltdb_private *ltdb,
+			   TALLOC_CTX *mem_ctx,
+			   struct ldb_dn *dn,
+			   TDB_DATA *tdb_key)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	int ret;
@@ -489,7 +494,6 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-
 	ret = ldb_kv_index_dn_base_dn(module, ltdb, dn, list, &truncation);
 	if (ret != LDB_SUCCESS) {
 		TALLOC_FREE(list);
@@ -534,17 +538,15 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 				return LDB_ERR_OPERATIONS_ERROR;
 			}
 
-			ret = ldb_kv_idx_to_key(module, ltdb,
-					      ldb, &list->dn[i],
-					      &key);
+			ret = ldb_kv_idx_to_key(
+			    module, ltdb, ldb, &list->dn[i], &key);
 			if (ret != LDB_SUCCESS) {
 				TALLOC_FREE(list);
 				TALLOC_FREE(rec);
 				return ret;
 			}
 
-			ret = ldb_kv_search_key(module, ltdb, key,
-					      rec, flags);
+			ret = ldb_kv_search_key(module, ltdb, key, rec, flags);
 			if (key.dptr != guid_key) {
 				TALLOC_FREE(key.dptr);
 			}
@@ -588,8 +590,7 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 	}
 
 	/* The tdb_key memory is allocated by the caller */
-	ret = ldb_kv_guid_to_key(module, ltdb,
-			       &list->dn[index], tdb_key);
+	ret = ldb_kv_guid_to_key(module, ltdb, &list->dn[index], tdb_key);
 	TALLOC_FREE(list);
 
 	if (ret != LDB_SUCCESS) {
@@ -605,9 +606,9 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
   save a dn_list into a full @IDX style record
  */
 static int ldb_kv_dn_list_store_full(struct ldb_module *module,
-				   struct ltdb_private *ltdb,
-				   struct ldb_dn *dn,
-				   struct dn_list *list)
+				     struct ltdb_private *ltdb,
+				     struct ldb_dn *dn,
+				     struct dn_list *list)
 {
 	struct ldb_message *msg;
 	int ret;
@@ -699,8 +700,9 @@ static int ldb_kv_dn_list_store_full(struct ldb_module *module,
 /*
   save a dn_list into the database, in either @IDX or internal format
  */
-static int ldb_kv_dn_list_store(struct ldb_module *module, struct ldb_dn *dn,
-			      struct dn_list *list)
+static int ldb_kv_dn_list_store(struct ldb_module *module,
+				struct ldb_dn *dn,
+				struct dn_list *list)
 {
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
 	TDB_DATA rec, key;
@@ -708,8 +710,7 @@ static int ldb_kv_dn_list_store(struct ldb_module *module, struct ldb_dn *dn,
 	struct dn_list *list2;
 
 	if (ltdb->idxptr == NULL) {
-		return ldb_kv_dn_list_store_full(module, ltdb,
-					       dn, list);
+		return ldb_kv_dn_list_store_full(module, ltdb, dn, list);
 	}
 
 	if (ltdb->idxptr->itdb == NULL) {
@@ -763,7 +764,10 @@ static int ldb_kv_dn_list_store(struct ldb_module *module, struct ldb_dn *dn,
 /*
   traverse function for storing the in-memory index entries on disk
  */
-static int ldb_kv_index_traverse_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *state)
+static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
+				       TDB_DATA key,
+				       TDB_DATA data,
+				       void *state)
 {
 	struct ldb_module *module = state;
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
@@ -788,8 +792,7 @@ static int ldb_kv_index_traverse_store(struct tdb_context *tdb, TDB_DATA key, TD
 		return -1;
 	}
 
-	ltdb->idxptr->error = ldb_kv_dn_list_store_full(module, ltdb,
-						      dn, list);
+	ltdb->idxptr->error = ldb_kv_dn_list_store_full(module, ltdb, dn, list);
 	talloc_free(dn);
 	if (ltdb->idxptr->error != 0) {
 		return -1;
@@ -808,7 +811,8 @@ int ldb_kv_index_transaction_commit(struct ldb_module *module)
 	ldb_reset_err_string(ldb);
 
 	if (ltdb->idxptr->itdb) {
-		tdb_traverse(ltdb->idxptr->itdb, ldb_kv_index_traverse_store, module);
+		tdb_traverse(
+		    ltdb->idxptr->itdb, ldb_kv_index_traverse_store, module);
 		tdb_close(ltdb->idxptr->itdb);
 	}
 
@@ -843,10 +847,11 @@ int ldb_kv_index_transaction_cancel(struct ldb_module *module)
   the caller is responsible for freeing
 */
 static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
-				     struct ltdb_private *ltdb,
-				     const char *attr, const struct ldb_val *value,
-				     const struct ldb_schema_attribute **ap,
-				     enum key_truncation *truncation)
+				       struct ltdb_private *ltdb,
+				       const char *attr,
+				       const struct ldb_val *value,
+				       const struct ldb_schema_attribute **ap,
+				       enum key_truncation *truncation)
 {
 	struct ldb_dn *ret;
 	struct ldb_val v;
@@ -1028,8 +1033,8 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
   see if a attribute value is in the list of indexed attributes
 */
 static bool ldb_kv_is_indexed(struct ldb_module *module,
-			    struct ltdb_private *ltdb,
-			    const char *attr)
+			      struct ltdb_private *ltdb,
+			      const char *attr)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	unsigned int i;
@@ -1091,9 +1096,9 @@ static bool ldb_kv_is_indexed(struct ldb_module *module,
   equality search only)
  */
 static int ldb_kv_index_dn_simple(struct ldb_module *module,
-				struct ltdb_private *ltdb,
-				const struct ldb_parse_tree *tree,
-				struct dn_list *list)
+				  struct ltdb_private *ltdb,
+				  const struct ldb_parse_tree *tree,
+				  struct dn_list *list)
 {
 	struct ldb_context *ldb;
 	struct ldb_dn *dn;
@@ -1113,9 +1118,12 @@ static int ldb_kv_index_dn_simple(struct ldb_module *module,
 
 	/* the attribute is indexed. Pull the list of DNs that match the
 	   search criterion */
-	dn = ldb_kv_index_key(ldb, ltdb,
-			    tree->u.equality.attr,
-			    &tree->u.equality.value, NULL, &truncation);
+	dn = ldb_kv_index_key(ldb,
+			      ltdb,
+			      tree->u.equality.attr,
+			      &tree->u.equality.value,
+			      NULL,
+			      &truncation);
 	/*
 	 * We ignore truncation here and allow multi-valued matches
 	 * as ltdb_search_indexed will filter out the wrong one in
@@ -1137,9 +1145,9 @@ static bool list_union(struct ldb_context *ldb,
   return a list of dn's that might match a leaf indexed search
  */
 static int ldb_kv_index_dn_leaf(struct ldb_module *module,
-			      struct ltdb_private *ltdb,
-			      const struct ldb_parse_tree *tree,
-			      struct dn_list *list)
+				struct ltdb_private *ltdb,
+				const struct ldb_parse_tree *tree,
+				struct dn_list *list)
 {
 	if (ltdb->disallow_dn_filter &&
 	    (ldb_attr_cmp(tree->u.equality.attr, "dn") == 0)) {
@@ -1174,8 +1182,8 @@ static int ldb_kv_index_dn_leaf(struct ldb_module *module,
 		 * We can't call TALLOC_FREE(dn) as this must belong
 		 * to list for the memory to remain valid.
 		 */
-		return ldb_kv_index_dn_base_dn(module, ltdb, dn, list,
-					     &truncation);
+		return ldb_kv_index_dn_base_dn(
+		    module, ltdb, dn, list, &truncation);
 		/*
 		 * We ignore truncation here and allow multi-valued matches
 		 * as ltdb_search_indexed will filter out the wrong one in
@@ -1276,8 +1284,8 @@ static bool list_intersect(struct ldb_context *ldb,
 
 	for (i=0;i<short_list->count;i++) {
 		/* For the GUID index case, this is a binary search */
-		if (ldb_kv_dn_list_find_val(ltdb, long_list,
-					  &short_list->dn[i]) != -1) {
+		if (ldb_kv_dn_list_find_val(
+			ltdb, long_list, &short_list->dn[i]) != -1) {
 			list3->dn[list3->count] = short_list->dn[i];
 			list3->count++;
 		}
@@ -1373,18 +1381,17 @@ static bool list_union(struct ldb_context *ldb,
 }
 
 static int ldb_kv_index_dn(struct ldb_module *module,
-			 struct ltdb_private *ltdb,
-			 const struct ldb_parse_tree *tree,
-			 struct dn_list *list);
-
+			   struct ltdb_private *ltdb,
+			   const struct ldb_parse_tree *tree,
+			   struct dn_list *list);
 
 /*
   process an OR list (a union)
  */
 static int ldb_kv_index_dn_or(struct ldb_module *module,
-			    struct ltdb_private *ltdb,
-			    const struct ldb_parse_tree *tree,
-			    struct dn_list *list)
+			      struct ltdb_private *ltdb,
+			      const struct ldb_parse_tree *tree,
+			      struct dn_list *list)
 {
 	struct ldb_context *ldb;
 	unsigned int i;
@@ -1403,8 +1410,8 @@ static int ldb_kv_index_dn_or(struct ldb_module *module,
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
 
-		ret = ldb_kv_index_dn(module, ltdb,
-				    tree->u.list.elements[i], list2);
+		ret = ldb_kv_index_dn(
+		    module, ltdb, tree->u.list.elements[i], list2);
 
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 			/* X || 0 == X */
@@ -1436,9 +1443,9 @@ static int ldb_kv_index_dn_or(struct ldb_module *module,
   NOT an index results
  */
 static int ldb_kv_index_dn_not(struct ldb_module *module,
-			     struct ltdb_private *ltdb,
-			     const struct ldb_parse_tree *tree,
-			     struct dn_list *list)
+			       struct ltdb_private *ltdb,
+			       const struct ldb_parse_tree *tree,
+			       struct dn_list *list)
 {
 	/* the only way to do an indexed not would be if we could
 	   negate the not via another not or if we knew the total
@@ -1456,8 +1463,8 @@ static int ldb_kv_index_dn_not(struct ldb_module *module,
  * by GUID, DN or a unique attribute
  */
 static bool ldb_kv_index_unique(struct ldb_context *ldb,
-			      struct ltdb_private *ltdb,
-			      const char *attr)
+				struct ltdb_private *ltdb,
+				const char *attr)
 {
 	const struct ldb_schema_attribute *a;
 	if (ltdb->cache->GUID_index_attribute != NULL) {
@@ -1480,9 +1487,9 @@ static bool ldb_kv_index_unique(struct ldb_context *ldb,
   process an AND expression (intersection)
  */
 static int ldb_kv_index_dn_and(struct ldb_module *module,
-			     struct ltdb_private *ltdb,
-			     const struct ldb_parse_tree *tree,
-			     struct dn_list *list)
+			       struct ltdb_private *ltdb,
+			       const struct ldb_parse_tree *tree,
+			       struct dn_list *list)
 {
 	struct ldb_context *ldb;
 	unsigned int i;
@@ -1501,8 +1508,7 @@ static int ldb_kv_index_dn_and(struct ldb_module *module,
 		int ret;
 
 		if (subtree->operation != LDB_OP_EQUALITY ||
-		    !ldb_kv_index_unique(ldb, ltdb,
-				       subtree->u.equality.attr)) {
+		    !ldb_kv_index_unique(ldb, ltdb, subtree->u.equality.attr)) {
 			continue;
 		}
 
@@ -1583,11 +1589,11 @@ static int ldb_kv_index_dn_and(struct ldb_module *module,
   return a list of matching objects using a one-level index
  */
 static int ldb_kv_index_dn_attr(struct ldb_module *module,
-			      struct ltdb_private *ltdb,
-			      const char *attr,
-			      struct ldb_dn *dn,
-			      struct dn_list *list,
-			      enum key_truncation *truncation)
+				struct ltdb_private *ltdb,
+				const char *attr,
+				struct ldb_dn *dn,
+				struct dn_list *list,
+				enum key_truncation *truncation)
 {
 	struct ldb_context *ldb;
 	struct ldb_dn *key;
@@ -1622,26 +1628,25 @@ static int ldb_kv_index_dn_attr(struct ldb_module *module,
   return a list of matching objects using a one-level index
  */
 static int ldb_kv_index_dn_one(struct ldb_module *module,
-			     struct ltdb_private *ltdb,
-			     struct ldb_dn *parent_dn,
-			     struct dn_list *list,
-			     enum key_truncation *truncation)
+			       struct ltdb_private *ltdb,
+			       struct ldb_dn *parent_dn,
+			       struct dn_list *list,
+			       enum key_truncation *truncation)
 {
 	/* Ensure we do not shortcut on intersection for this list */
 	list->strict = true;
-	return ldb_kv_index_dn_attr(module, ltdb,
-				  LTDB_IDXONE, parent_dn, list, truncation);
-
+	return ldb_kv_index_dn_attr(
+	    module, ltdb, LTDB_IDXONE, parent_dn, list, truncation);
 }
 
 /*
   return a list of matching objects using the DN index
  */
 static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
-				 struct ltdb_private *ltdb,
-				 struct ldb_dn *base_dn,
-				 struct dn_list *dn_list,
-				 enum key_truncation *truncation)
+				   struct ltdb_private *ltdb,
+				   struct ldb_dn *base_dn,
+				   struct dn_list *dn_list,
+				   enum key_truncation *truncation)
 {
 	const struct ldb_val *guid_val = NULL;
 	if (ltdb->cache->GUID_index_attribute == NULL) {
@@ -1677,8 +1682,8 @@ static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	return ldb_kv_index_dn_attr(module, ltdb,
-				  LTDB_IDXDN, base_dn, dn_list, truncation);
+	return ldb_kv_index_dn_attr(
+	    module, ltdb, LTDB_IDXDN, base_dn, dn_list, truncation);
 }
 
 /*
@@ -1686,9 +1691,9 @@ static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
   an error. return LDB_ERR_NO_SUCH_OBJECT for no matches, or LDB_SUCCESS for matches
  */
 static int ldb_kv_index_dn(struct ldb_module *module,
-			 struct ltdb_private *ltdb,
-			 const struct ldb_parse_tree *tree,
-			 struct dn_list *list)
+			   struct ltdb_private *ltdb,
+			   const struct ldb_parse_tree *tree,
+			   struct dn_list *list)
 {
 	int ret = LDB_ERR_OPERATIONS_ERROR;
 
@@ -1728,10 +1733,10 @@ static int ldb_kv_index_dn(struct ldb_module *module,
   extracting just the given attributes
 */
 static int ldb_kv_index_filter(struct ltdb_private *ltdb,
-			     const struct dn_list *dn_list,
-			     struct ltdb_context *ac,
-			     uint32_t *match_count,
-			     enum key_truncation scope_one_truncation)
+			       const struct dn_list *dn_list,
+			       struct ltdb_context *ac,
+			       uint32_t *match_count,
+			       enum key_truncation scope_one_truncation)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
 	struct ldb_message *msg;
@@ -1784,11 +1789,8 @@ static int ldb_kv_index_filter(struct ltdb_private *ltdb,
 	for (i = 0; i < dn_list->count; i++) {
 		int ret;
 
-		ret = ldb_kv_idx_to_key(ac->module,
-				      ltdb,
-				      keys,
-				      &dn_list->dn[i],
-				      &keys[num_keys]);
+		ret = ldb_kv_idx_to_key(
+		    ac->module, ltdb, keys, &dn_list->dn[i], &keys[num_keys]);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(keys);
 			return ret;
@@ -1832,10 +1834,13 @@ static int ldb_kv_index_filter(struct ltdb_private *ltdb,
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
 
-		ret = ldb_kv_search_key(ac->module, ltdb,
-				      keys[i], msg,
-				      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC|
-				      LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC);
+		ret =
+		    ldb_kv_search_key(ac->module,
+				      ltdb,
+				      keys[i],
+				      msg,
+				      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
+					  LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC);
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 			/*
 			 * the record has disappeared? yes, this can
@@ -1911,8 +1916,7 @@ static int ldb_kv_index_filter(struct ltdb_private *ltdb,
 /*
   sort a DN list
  */
-static void ldb_kv_dn_list_sort(struct ltdb_private *ltdb,
-			      struct dn_list *list)
+static void ldb_kv_dn_list_sort(struct ltdb_private *ltdb, struct dn_list *list)
 {
 	if (list->count < 2) {
 		return;
@@ -1980,8 +1984,8 @@ int ldb_kv_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
 		 * the tree, we must ensure we strictly intersect with
 		 * this list, as we trust the ONELEVEL index
 		 */
-		ret = ldb_kv_index_dn_one(ac->module, ltdb, ac->base, dn_list,
-				        &scope_one_truncation);
+		ret = ldb_kv_index_dn_one(
+		    ac->module, ltdb, ac->base, dn_list, &scope_one_truncation);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(dn_list);
 			return ret;
@@ -2019,8 +2023,8 @@ int ldb_kv_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
 			 * index can't trim the result list down then
 			 * the ONELEVEL index is still good enough.
 			 */
-			ret = ldb_kv_index_dn(ac->module, ltdb, ac->tree,
-					    idx_one_tree_list);
+			ret = ldb_kv_index_dn(
+			    ac->module, ltdb, ac->tree, idx_one_tree_list);
 			if (ret == LDB_SUCCESS) {
 				if (!list_intersect(ldb, ltdb,
 						    dn_list,
@@ -2062,8 +2066,8 @@ int ldb_kv_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
 	 * processing as the truncation here refers only to the
 	 * SCOPE_ONELEVEL index.
 	 */
-	ret = ldb_kv_index_filter(ltdb, dn_list, ac, match_count,
-				scope_one_truncation);
+	ret = ldb_kv_index_filter(
+	    ltdb, dn_list, ac, match_count, scope_one_truncation);
 	talloc_free(dn_list);
 	return ret;
 }
@@ -2089,9 +2093,10 @@ int ldb_kv_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
  * @return                  An ldb error code
  */
 static int ldb_kv_index_add1(struct ldb_module *module,
-			   struct ltdb_private *ltdb,
-			   const struct ldb_message *msg,
-			   struct ldb_message_element *el, int v_idx)
+			     struct ltdb_private *ltdb,
+			     const struct ldb_message *msg,
+			     struct ldb_message_element *el,
+			     int v_idx)
 {
 	struct ldb_context *ldb;
 	struct ldb_dn *dn_key;
@@ -2109,8 +2114,8 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	dn_key = ldb_kv_index_key(ldb, ltdb,
-				el->name, &el->values[v_idx], &a, &truncation);
+	dn_key = ldb_kv_index_key(
+	    ldb, ltdb, el->name, &el->values[v_idx], &a, &truncation);
 	if (!dn_key) {
 		talloc_free(list);
 		return LDB_ERR_OPERATIONS_ERROR;
@@ -2178,17 +2183,15 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 				return LDB_ERR_OPERATIONS_ERROR;
 			}
 
-			ret = ldb_kv_idx_to_key(module, ltdb,
-					      ldb, &list->dn[i],
-					      &key);
+			ret = ldb_kv_idx_to_key(
+			    module, ltdb, ldb, &list->dn[i], &key);
 			if (ret != LDB_SUCCESS) {
 				TALLOC_FREE(list);
 				TALLOC_FREE(rec);
 				return ret;
 			}
 
-			ret = ldb_kv_search_key(module, ltdb, key,
-					      rec, flags);
+			ret = ldb_kv_search_key(module, ltdb, key, rec, flags);
 			if (key.dptr != guid_key) {
 				TALLOC_FREE(key.dptr);
 			}
@@ -2371,14 +2374,13 @@ static int ldb_kv_index_add1(struct ldb_module *module,
   add index entries for one elements in a message
  */
 static int ldb_kv_index_add_el(struct ldb_module *module,
-			     struct ltdb_private *ltdb,
-			     const struct ldb_message *msg,
-			     struct ldb_message_element *el)
+			       struct ltdb_private *ltdb,
+			       const struct ldb_message *msg,
+			       struct ldb_message_element *el)
 {
 	unsigned int i;
 	for (i = 0; i < el->num_values; i++) {
-		int ret = ldb_kv_index_add1(module, ltdb,
-					  msg, el, i);
+		int ret = ldb_kv_index_add1(module, ltdb, msg, el, i);
 		if (ret != LDB_SUCCESS) {
 			return ret;
 		}
@@ -2391,8 +2393,8 @@ static int ldb_kv_index_add_el(struct ldb_module *module,
   add index entries for all elements in a message
  */
 static int ldb_kv_index_add_all(struct ldb_module *module,
-			      struct ltdb_private *ltdb,
-			      const struct ldb_message *msg)
+				struct ltdb_private *ltdb,
+				const struct ldb_message *msg)
 {
 	struct ldb_message_element *elements = msg->elements;
 	unsigned int i;
@@ -2422,8 +2424,7 @@ static int ldb_kv_index_add_all(struct ldb_module *module,
 		if (!ldb_kv_is_indexed(module, ltdb, elements[i].name)) {
 			continue;
 		}
-		ret = ldb_kv_index_add_el(module, ltdb,
-					msg, &elements[i]);
+		ret = ldb_kv_index_add_el(module, ltdb, msg, &elements[i]);
 		if (ret != LDB_SUCCESS) {
 			struct ldb_context *ldb = ldb_module_get_ctx(module);
 			ldb_asprintf_errstring(ldb,
@@ -2442,10 +2443,11 @@ static int ldb_kv_index_add_all(struct ldb_module *module,
   insert a DN index for a message
 */
 static int ldb_kv_modify_index_dn(struct ldb_module *module,
-				struct ltdb_private *ltdb,
-				const struct ldb_message *msg,
-				struct ldb_dn *dn,
-				const char *index, int add)
+				  struct ltdb_private *ltdb,
+				  const struct ldb_message *msg,
+				  struct ldb_dn *dn,
+				  const char *index,
+				  int add)
 {
 	struct ldb_message_element el;
 	struct ldb_val val;
@@ -2495,7 +2497,8 @@ static int ldb_kv_modify_index_dn(struct ldb_module *module,
   insert a one level index for a message
 */
 static int ldb_kv_index_onelevel(struct ldb_module *module,
-			       const struct ldb_message *msg, int add)
+				 const struct ldb_message *msg,
+				 int add)
 {
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module),
 						    struct ltdb_private);
@@ -2511,8 +2514,7 @@ static int ldb_kv_index_onelevel(struct ldb_module *module,
 	if (pdn == NULL) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
-	ret = ldb_kv_modify_index_dn(module, ltdb,
-				   msg, pdn, LTDB_IDXONE, add);
+	ret = ldb_kv_modify_index_dn(module, ltdb, msg, pdn, LTDB_IDXONE, add);
 
 	talloc_free(pdn);
 
@@ -2523,8 +2525,8 @@ static int ldb_kv_index_onelevel(struct ldb_module *module,
   insert a one level index for a message
 */
 static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
-				    const struct ldb_message *msg,
-				    int add)
+				      const struct ldb_message *msg,
+				      int add)
 {
 	int ret;
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module),
@@ -2535,8 +2537,8 @@ static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	ret = ldb_kv_modify_index_dn(module, ltdb, msg, msg->dn,
-				   LTDB_IDXDN, add);
+	ret =
+	    ldb_kv_modify_index_dn(module, ltdb, msg, msg->dn, LTDB_IDXDN, add);
 
 	if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
@@ -2552,9 +2554,9 @@ static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
   The caller guarantees that these element values are not yet indexed
 */
 int ldb_kv_index_add_element(struct ldb_module *module,
-			   struct ltdb_private *ltdb,
-			   const struct ldb_message *msg,
-			   struct ldb_message_element *el)
+			     struct ltdb_private *ltdb,
+			     const struct ldb_message *msg,
+			     struct ldb_message_element *el)
 {
 	if (ldb_dn_is_special(msg->dn)) {
 		return LDB_SUCCESS;
@@ -2569,8 +2571,8 @@ int ldb_kv_index_add_element(struct ldb_module *module,
   add the index entries for a new record
 */
 int ldb_kv_index_add_new(struct ldb_module *module,
-		       struct ltdb_private *ltdb,
-		       const struct ldb_message *msg)
+			 struct ltdb_private *ltdb,
+			 const struct ldb_message *msg)
 {
 	int ret;
 
@@ -2610,9 +2612,10 @@ int ldb_kv_index_add_new(struct ldb_module *module,
   delete an index entry for one message element
 */
 int ldb_kv_index_del_value(struct ldb_module *module,
-			 struct ltdb_private *ltdb,
-			 const struct ldb_message *msg,
-			 struct ldb_message_element *el, unsigned int v_idx)
+			   struct ltdb_private *ltdb,
+			   const struct ldb_message *msg,
+			   struct ldb_message_element *el,
+			   unsigned int v_idx)
 {
 	struct ldb_context *ldb;
 	struct ldb_dn *dn_key;
@@ -2634,9 +2637,8 @@ int ldb_kv_index_del_value(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	dn_key = ldb_kv_index_key(ldb, ltdb,
-				el->name, &el->values[v_idx],
-				NULL, &truncation);
+	dn_key = ldb_kv_index_key(
+	    ldb, ltdb, el->name, &el->values[v_idx], NULL, &truncation);
 	/*
 	 * We ignore key truncation in ltdb_index_add1() so
 	 * match that by ignoring it here as well
@@ -2700,9 +2702,9 @@ int ldb_kv_index_del_value(struct ldb_module *module,
   return -1 on failure
 */
 int ldb_kv_index_del_element(struct ldb_module *module,
-			   struct ltdb_private *ltdb,
-			   const struct ldb_message *msg,
-			   struct ldb_message_element *el)
+			     struct ltdb_private *ltdb,
+			     const struct ldb_message *msg,
+			     struct ldb_message_element *el)
 {
 	const char *dn_str;
 	int ret;
@@ -2739,7 +2741,8 @@ int ldb_kv_index_del_element(struct ldb_module *module,
   delete the index entries for a record
   return -1 on failure
 */
-int ldb_kv_index_delete(struct ldb_module *module, const struct ldb_message *msg)
+int ldb_kv_index_delete(struct ldb_module *module,
+			const struct ldb_message *msg)
 {
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
 	int ret;
@@ -2765,8 +2768,8 @@ int ldb_kv_index_delete(struct ldb_module *module, const struct ldb_message *msg
 	}
 
 	for (i = 0; i < msg->num_elements; i++) {
-		ret = ldb_kv_index_del_element(module, ltdb,
-					     msg, &msg->elements[i]);
+		ret = ldb_kv_index_del_element(
+		    module, ltdb, msg, &msg->elements[i]);
 		if (ret != LDB_SUCCESS) {
 			return ret;
 		}
diff --git a/lib/ldb/ldb_tdb/ldb_search.c b/lib/ldb/ldb_tdb/ldb_search.c
index 4f67047..a0df5d0 100644
--- a/lib/ldb/ldb_tdb/ldb_search.c
+++ b/lib/ldb/ldb_tdb/ldb_search.c
@@ -117,9 +117,9 @@ static int msg_add_distinguished_name(struct ldb_message *msg)
   and LDB_SUCCESS on success
 */
 int ldb_kv_search_base(struct ldb_module *module,
-		     TALLOC_CTX *mem_ctx,
-		     struct ldb_dn *dn,
-		     struct ldb_dn **ret_dn)
+		       TALLOC_CTX *mem_ctx,
+		       struct ldb_dn *dn,
+		       struct ldb_dn **ret_dn)
 {
 	int exists;
 	int ret;
@@ -141,9 +141,7 @@ int ldb_kv_search_base(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ldb_kv_search_dn1(module, dn,
-			      msg,
-			      LDB_UNPACK_DATA_FLAG_NO_ATTRS);
+	ret = ldb_kv_search_dn1(module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_ATTRS);
 	if (ret == LDB_SUCCESS) {
 		const char *dn_linearized
 			= ldb_dn_get_linearized(dn);
@@ -184,8 +182,8 @@ struct ltdb_parse_data_unpack_ctx {
 };
 
 static int ldb_kv_parse_data_unpack(struct ldb_val key,
-				  struct ldb_val data,
-				  void *private_data)
+				    struct ldb_val data,
+				    void *private_data)
 {
 	struct ltdb_parse_data_unpack_ctx *ctx = private_data;
 	unsigned int nb_elements_in_db;
@@ -236,10 +234,11 @@ static int ldb_kv_parse_data_unpack(struct ldb_val key,
   return LDB_ERR_NO_SUCH_OBJECT on record-not-found
   and LDB_SUCCESS on success
 */
-int ldb_kv_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
-		    const struct TDB_DATA tdb_key,
-		    struct ldb_message *msg,
-		    unsigned int unpack_flags)
+int ldb_kv_search_key(struct ldb_module *module,
+		      struct ltdb_private *ltdb,
+		      const struct TDB_DATA tdb_key,
+		      struct ldb_message *msg,
+		      unsigned int unpack_flags)
 {
 	int ret;
 	struct ltdb_parse_data_unpack_ctx ctx = {
@@ -257,8 +256,8 @@ int ldb_kv_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
 	msg->num_elements = 0;
 	msg->elements = NULL;
 
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, ldb_key,
-					    ldb_kv_parse_data_unpack, &ctx);
+	ret = ltdb->kv_ops->fetch_and_parse(
+	    ltdb, ldb_key, ldb_kv_parse_data_unpack, &ctx);
 
 	if (ret == -1) {
 		ret = ltdb->kv_ops->error(ltdb);
@@ -284,8 +283,10 @@ int ldb_kv_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
   return LDB_ERR_NO_SUCH_OBJECT on record-not-found
   and LDB_SUCCESS on success
 */
-int ldb_kv_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg,
-		    unsigned int unpack_flags)
+int ldb_kv_search_dn1(struct ldb_module *module,
+		      struct ldb_dn *dn,
+		      struct ldb_message *msg,
+		      unsigned int unpack_flags)
 {
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
@@ -319,9 +320,7 @@ int ldb_kv_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_m
 		 * used for internal memory.
 		 *
 		 */
-		ret = ldb_kv_key_dn_from_idx(module, ltdb,
-					   msg,
-					   dn, &tdb_key);
+		ret = ldb_kv_key_dn_from_idx(module, ltdb, msg, dn, &tdb_key);
 		if (ret != LDB_SUCCESS) {
 			return ret;
 		}
@@ -356,8 +355,9 @@ int ldb_kv_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_m
 
  */
 int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
-		      const struct ldb_message *msg, const char * const *attrs,
-		      struct ldb_message **filtered_msg)
+			const struct ldb_message *msg,
+			const char *const *attrs,
+			struct ldb_message **filtered_msg)
 {
 	unsigned int i;
 	bool keep_all = false;
@@ -597,7 +597,7 @@ static int ldb_kv_search_full(struct ltdb_context *ctx)
 }
 
 static int ldb_kv_search_and_return_base(struct ltdb_private *ltdb,
-				       struct ltdb_context *ctx)
+					 struct ltdb_context *ctx)
 {
 	struct ldb_message *msg, *filtered_msg;
 	struct ldb_context *ldb = ldb_module_get_ctx(ctx->module);
@@ -610,9 +610,11 @@ static int ldb_kv_search_and_return_base(struct ltdb_private *ltdb,
 	if (!msg) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
-	ret = ldb_kv_search_dn1(ctx->module, ctx->base, msg,
-			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC|
-			      LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC);
+	ret = ldb_kv_search_dn1(ctx->module,
+				ctx->base,
+				msg,
+				LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
+				    LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC);
 
 	if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 		if (ltdb->check_base == false) {
@@ -781,10 +783,9 @@ int ldb_kv_search(struct ltdb_context *ctx)
 		 * dn.  Also optimise the subsequent filter by filling
 		 * in the ctx->base to be exactly case correct
 		 */
-		ret = ldb_kv_search_base(module, ctx,
-				       req->op.search.base,
-				       &ctx->base);
-		
+		ret = ldb_kv_search_base(
+		    module, ctx, req->op.search.base, &ctx->base);
+
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 			ldb_asprintf_errstring(ldb, 
 					       "No such Base DN: %s", 
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.c b/lib/ldb/ldb_tdb/ldb_tdb.c
index a18e613..c0938c0 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -197,8 +197,9 @@ bool ldb_kv_key_is_record(TDB_DATA key)
   note that the key for a record can depend on whether the
   dn refers to a case sensitive index record or not
 */
-TDB_DATA ldb_kv_key_dn(struct ldb_module *module, TALLOC_CTX *mem_ctx,
-		     struct ldb_dn *dn)
+TDB_DATA ldb_kv_key_dn(struct ldb_module *module,
+		       TALLOC_CTX *mem_ctx,
+		       struct ldb_dn *dn)
 {
 	TDB_DATA key;
 	char *key_str = NULL;
@@ -245,9 +246,9 @@ failed:
 
 /* The caller is to provide a correctly sized key */
 int ldb_kv_guid_to_key(struct ldb_module *module,
-		     struct ltdb_private *ltdb,
-		     const struct ldb_val *GUID_val,
-		     TDB_DATA *key)
+		       struct ltdb_private *ltdb,
+		       const struct ldb_val *GUID_val,
+		       TDB_DATA *key)
 {
 	const char *GUID_prefix = LTDB_GUID_KEY_PREFIX;
 	const int GUID_prefix_len = sizeof(LTDB_GUID_KEY_PREFIX) - 1;
@@ -267,17 +268,16 @@ int ldb_kv_guid_to_key(struct ldb_module *module,
  * the GUID index mode
  */
 int ldb_kv_idx_to_key(struct ldb_module *module,
-		    struct ltdb_private *ltdb,
-		    TALLOC_CTX *mem_ctx,
-		    const struct ldb_val *idx_val,
-		    TDB_DATA *key)
+		      struct ltdb_private *ltdb,
+		      TALLOC_CTX *mem_ctx,
+		      const struct ldb_val *idx_val,
+		      TDB_DATA *key)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	struct ldb_dn *dn;
 
 	if (ltdb->cache->GUID_index_attribute != NULL) {
-		return ldb_kv_guid_to_key(module, ltdb,
-					idx_val, key);
+		return ldb_kv_guid_to_key(module, ltdb, idx_val, key);
 	}
 
 	dn = ldb_dn_from_ldb_val(mem_ctx, ldb, idx_val);
@@ -305,8 +305,9 @@ int ldb_kv_idx_to_key(struct ldb_module *module,
   note that the key for a record can depend on whether a
   GUID index is in use, or the DN is used as the key
 */
-TDB_DATA ldb_kv_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
-		      const struct ldb_message *msg)
+TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
+			TALLOC_CTX *mem_ctx,
+			const struct ldb_message *msg)
 {
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
@@ -363,7 +364,7 @@ TDB_DATA ldb_kv_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
   currently only @ATTRIBUTES is checked
 */
 static int ldb_kv_check_special_dn(struct ldb_module *module,
-				 const struct ldb_message *msg)
+				   const struct ldb_message *msg)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	unsigned int i, j;
@@ -379,7 +380,8 @@ static int ldb_kv_check_special_dn(struct ldb_module *module,
 		if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) continue;
 
 		for (j = 0; j < msg->elements[i].num_values; j++) {
-			if (ldb_kv_check_at_attributes_values(&msg->elements[i].values[j]) != 0) {
+			if (ldb_kv_check_at_attributes_values(
+				&msg->elements[i].values[j]) != 0) {
 				ldb_set_errstring(ldb, "Invalid attribute value in an @ATTRIBUTES entry");
 				return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
 			}
@@ -439,8 +441,10 @@ static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
 	return ret;
 }
 
-static int ltdb_store(struct ltdb_private *ltdb, struct ldb_val ldb_key,
-			  struct ldb_val ldb_data, int flags)
+static int ltdb_store(struct ltdb_private *ltdb,
+		      struct ldb_val ldb_key,
+		      struct ldb_val ldb_data,
+		      int flags)
 {
 	TDB_DATA key = {
 		.dptr = ldb_key.data,
@@ -470,10 +474,9 @@ static const char *ltdb_errorstr(struct ltdb_private *ltdb)
 /*
   store a record into the db
 */
-int ldb_kv_store(
-	struct ldb_module *module,
-	const struct ldb_message *msg,
-	int flgs)
+int ldb_kv_store(struct ldb_module *module,
+		 const struct ldb_message *msg,
+		 int flgs)
 {
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
@@ -537,7 +540,7 @@ done:
   check if a attribute is a single valued, for a given element
  */
 static bool ldb_kv_single_valued(const struct ldb_schema_attribute *a,
-				  struct ldb_message_element *el)
+				 struct ldb_message_element *el)
 {
 	if (!a) return false;
 	if (el != NULL) {
@@ -562,9 +565,9 @@ static bool ldb_kv_single_valued(const struct ldb_schema_attribute *a,
 }
 
 static int ldb_kv_add_internal(struct ldb_module *module,
-			     struct ltdb_private *ltdb,
-			     const struct ldb_message *msg,
-			     bool check_single_value)
+			       struct ltdb_private *ltdb,
+			       const struct ldb_message *msg,
+			       bool check_single_value)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	int ret = LDB_SUCCESS;
@@ -579,9 +582,8 @@ static int ldb_kv_add_internal(struct ldb_module *module,
 					       el->name, ldb_dn_get_linearized(msg->dn));
 			return LDB_ERR_CONSTRAINT_VIOLATION;
 		}
-		if (check_single_value &&
-				el->num_values > 1 &&
-				ldb_kv_single_valued(a, el)) {
+		if (check_single_value && el->num_values > 1 &&
+		    ldb_kv_single_valued(a, el)) {
 			ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
 					       el->name, ldb_dn_get_linearized(msg->dn));
 			return LDB_ERR_CONSTRAINT_VIOLATION;
@@ -630,8 +632,8 @@ static int ldb_kv_add_internal(struct ldb_module *module,
 			if (mem_ctx == NULL) {
 				return ldb_module_operr(module);
 			}
-			ret2 = ldb_kv_search_base(module, mem_ctx,
-						msg->dn, &dn2);
+			ret2 =
+			    ldb_kv_search_base(module, mem_ctx, msg->dn, &dn2);
 			TALLOC_FREE(mem_ctx);
 			if (ret2 == LDB_SUCCESS) {
 				ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
@@ -698,8 +700,7 @@ static int ldb_kv_add(struct ltdb_context *ctx)
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ldb_kv_add_internal(module, ltdb,
-				req->op.add.message, true);
+	ret = ldb_kv_add_internal(module, ltdb, req->op.add.message, true);
 
 	return ret;
 }
@@ -722,7 +723,7 @@ static int ltdb_delete(struct ltdb_private *ltdb, struct ldb_val ldb_key)
   index records)
 */
 int ldb_kv_delete_noindex(struct ldb_module *module,
-			const struct ldb_message *msg)
+			  const struct ldb_message *msg)
 {
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
@@ -771,7 +772,8 @@ static int ldb_kv_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
 
 	/* in case any attribute of the message was indexed, we need
 	   to fetch the old record */
-	ret = ldb_kv_search_dn1(module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
+	ret = ldb_kv_search_dn1(
+	    module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
 	if (ret != LDB_SUCCESS) {
 		/* not finding the old record is an error */
 		goto done;
@@ -845,7 +847,7 @@ static int ldb_kv_find_element(const struct ldb_message *msg, const char *name)
   returns 0 on success, -1 on failure (and sets errno)
 */
 static int ldb_kv_msg_add_element(struct ldb_message *msg,
-				struct ldb_message_element *el)
+				  struct ldb_message_element *el)
 {
 	struct ldb_message_element *e2;
 	unsigned int i;
@@ -888,8 +890,9 @@ static int ldb_kv_msg_add_element(struct ldb_message *msg,
   delete all elements having a specified attribute name
 */
 static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
-				struct ltdb_private *ltdb,
-				struct ldb_message *msg, const char *name)
+				       struct ltdb_private *ltdb,
+				       struct ldb_message *msg,
+				       const char *name)
 {
 	unsigned int i;
 	int ret;
@@ -934,10 +937,10 @@ static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
   return LDB Error on failure
 */
 static int ldb_kv_msg_delete_element(struct ldb_module *module,
-			      struct ltdb_private *ltdb,
-			      struct ldb_message *msg,
-			      const char *name,
-			      const struct ldb_val *val)
+				     struct ltdb_private *ltdb,
+				     struct ldb_message *msg,
+				     const char *name,
+				     const struct ldb_val *val)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	unsigned int i;
@@ -967,8 +970,8 @@ static int ldb_kv_msg_delete_element(struct ldb_module *module,
 		}
 		if (matched) {
 			if (el->num_values == 1) {
-				return ldb_kv_msg_delete_attribute(module,
-							    ltdb, msg, name);
+				return ldb_kv_msg_delete_attribute(
+				    module, ltdb, msg, name);
 			}
 
 			ret = ldb_kv_index_del_value(module, ltdb, msg, el, i);
@@ -1003,8 +1006,8 @@ static int ldb_kv_msg_delete_element(struct ldb_module *module,
   'req' is optional, and is used to specify controls if supplied
 */
 int ldb_kv_modify_internal(struct ldb_module *module,
-			 const struct ldb_message *msg,
-			 struct ldb_request *req)
+			   const struct ldb_message *msg,
+			   struct ldb_request *req)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	void *data = ldb_module_get_private(module);
@@ -1030,9 +1033,8 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 		goto done;
 	}
 
-	ret = ldb_kv_search_dn1(module, msg->dn,
-			      msg2,
-			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
+	ret = ldb_kv_search_dn1(
+	    module, msg->dn, msg2, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
 	if (ret != LDB_SUCCESS) {
 		goto done;
 	}
@@ -1094,9 +1096,8 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 					ret = LDB_ERR_OTHER;
 					goto done;
 				}
-				ret = ldb_kv_index_add_element(module, ltdb,
-							     msg2,
-							     el);
+				ret = ldb_kv_index_add_element(
+				    module, ltdb, msg2, el);
 				if (ret != LDB_SUCCESS) {
 					goto done;
 				}
@@ -1176,8 +1177,8 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 				el2->values = vals;
 				el2->num_values += el->num_values;
 
-				ret = ldb_kv_index_add_element(module, ltdb,
-							     msg2, el);
+				ret = ldb_kv_index_add_element(
+				    module, ltdb, msg2, el);
 				if (ret != LDB_SUCCESS) {
 					goto done;
 				}
@@ -1241,9 +1242,8 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 				}
 
 				/* Delete the attribute if it exists in the DB */
-				if (ldb_kv_msg_delete_attribute(module, ltdb,
-							 msg2,
-							 el->name) != 0) {
+				if (ldb_kv_msg_delete_attribute(
+					module, ltdb, msg2, el->name) != 0) {
 					ret = LDB_ERR_OTHER;
 					goto done;
 				}
@@ -1255,8 +1255,7 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 				goto done;
 			}
 
-			ret = ldb_kv_index_add_element(module, ltdb,
-						     msg2, el);
+			ret = ldb_kv_index_add_element(module, ltdb, msg2, el);
 			if (ret != LDB_SUCCESS) {
 				goto done;
 			}
@@ -1272,10 +1271,8 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 
 			if (msg->elements[i].num_values == 0) {
 				/* Delete the whole attribute */
-				ret = ldb_kv_msg_delete_attribute(module,
-							   ltdb,
-							   msg2,
-							   msg->elements[i].name);
+				ret = ldb_kv_msg_delete_attribute(
+				    module, ltdb, msg2, msg->elements[i].name);
 				if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
 				    control_permissive) {
 					ret = LDB_SUCCESS;
@@ -1290,11 +1287,12 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 			} else {
 				/* Delete specified values from an attribute */
 				for (j=0; j < msg->elements[i].num_values; j++) {
-					ret = ldb_kv_msg_delete_element(module,
-								 ltdb,
-							         msg2,
-							         msg->elements[i].name,
-							         &msg->elements[i].values[j]);
+					ret = ldb_kv_msg_delete_element(
+					    module,
+					    ltdb,
+					    msg2,
+					    msg->elements[i].name,
+					    &msg->elements[i].values[j]);
 					if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
 					    control_permissive) {
 						ret = LDB_SUCCESS;
@@ -1385,8 +1383,10 @@ static int ldb_kv_rename(struct ltdb_context *ctx)
 	}
 
 	/* we need to fetch the old record to re-add under the new name */
-	ret = ldb_kv_search_dn1(module, req->op.rename.olddn, msg,
-			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
+	ret = ldb_kv_search_dn1(module,
+				req->op.rename.olddn,
+				msg,
+				LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
 	if (ret != LDB_SUCCESS) {
 		/* not finding the old record is an error */
 		return ret;
@@ -1418,9 +1418,8 @@ static int ldb_kv_rename(struct ltdb_context *ctx)
 	 */
 	if (tdb_key_old.dsize != tdb_key.dsize
 	    || memcmp(tdb_key.dptr, tdb_key_old.dptr, tdb_key.dsize) != 0) {
-		ret = ldb_kv_search_base(module, msg,
-				       req->op.rename.newdn,
-				       &db_dn);
+		ret = ldb_kv_search_base(
+		    module, msg, req->op.rename.newdn, &db_dn);
 		if (ret == LDB_SUCCESS) {
 			ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
 		} else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
@@ -1566,7 +1565,6 @@ static int ldb_kv_start_trans(struct ldb_module *module)
 		return ltdb->kv_ops->error(ltdb);
 	}
 
-
 	ldb_kv_index_transaction_start(module);
 
 	ltdb->reindex_failed = false;
@@ -1676,7 +1674,6 @@ static int ldb_kv_del_trans(struct ldb_module *module)
 	void *data = ldb_module_get_private(module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
 
-
 	if (ldb_kv_index_transaction_cancel(module) != 0) {
 		ltdb->kv_ops->abort_write(ltdb);
 		return ltdb->kv_ops->error(ltdb);
@@ -1690,7 +1687,7 @@ static int ldb_kv_del_trans(struct ldb_module *module)
   return sequenceNumber from @BASEINFO
 */
 static int ldb_kv_sequence_number(struct ltdb_context *ctx,
-				struct ldb_extended **ext)
+				  struct ldb_extended **ext)
 {
 	struct ldb_context *ldb;
 	struct ldb_module *module = ctx->module;
@@ -1809,9 +1806,9 @@ static void ldb_kv_request_done(struct ltdb_context *ctx, int error)
 }
 
 static void ldb_kv_timeout(struct tevent_context *ev,
-			  struct tevent_timer *te,
-			  struct timeval t,
-			  void *private_data)
+			   struct tevent_timer *te,
+			   struct timeval t,
+			   void *private_data)
 {
 	struct ltdb_context *ctx;
 	ctx = talloc_get_type(private_data, struct ltdb_context);
@@ -1830,8 +1827,8 @@ static void ldb_kv_timeout(struct tevent_context *ev,
 }
 
 static void ldb_kv_request_extended_done(struct ltdb_context *ctx,
-					struct ldb_extended *ext,
-					int error)
+					 struct ldb_extended *ext,
+					 int error)
 {
 	struct ldb_context *ldb;
 	struct ldb_request *req;
@@ -1884,7 +1881,10 @@ struct kv_ctx {
 		      void *private_data);
 };
 
-static int ltdb_traverse_fn_wrapper(struct tdb_context *tdb, TDB_DATA tdb_key, TDB_DATA tdb_data, void *ctx)
+static int ltdb_traverse_fn_wrapper(struct tdb_context *tdb,
+				    TDB_DATA tdb_key,
+				    TDB_DATA tdb_data,
+				    void *ctx)
 {
 	struct kv_ctx *kv_ctx = ctx;
 	struct ldb_val key = {
@@ -1898,7 +1898,9 @@ static int ltdb_traverse_fn_wrapper(struct tdb_context *tdb, TDB_DATA tdb_key, T
 	return kv_ctx->kv_traverse_fn(kv_ctx->ltdb, key, data, kv_ctx->ctx);
 }
 
-static int ltdb_traverse_fn(struct ltdb_private *ltdb, ldb_kv_traverse_fn fn, void *ctx)
+static int ltdb_traverse_fn(struct ltdb_private *ltdb,
+			    ldb_kv_traverse_fn fn,
+			    void *ctx)
 {
 	struct kv_ctx kv_ctx = {
 		.kv_traverse_fn = fn,
@@ -1906,16 +1908,19 @@ static int ltdb_traverse_fn(struct ltdb_private *ltdb, ldb_kv_traverse_fn fn, vo
 		.ltdb = ltdb
 	};
 	if (tdb_transaction_active(ltdb->tdb)) {
-		return tdb_traverse(ltdb->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
+		return tdb_traverse(
+		    ltdb->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
 	} else {
-		return tdb_traverse_read(ltdb->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
+		return tdb_traverse_read(
+		    ltdb->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
 	}
 }
 
 static int ltdb_update_in_iterate(struct ltdb_private *ltdb,
-				      struct ldb_val ldb_key,
-				      struct ldb_val ldb_key2,
-				      struct ldb_val ldb_data, void *state)
+				  struct ldb_val ldb_key,
+				  struct ldb_val ldb_key2,
+				  struct ldb_val ldb_data,
+				  void *state)
 {
 	int tdb_ret;
 	struct ldb_context *ldb;
@@ -1964,8 +1969,9 @@ static int ltdb_update_in_iterate(struct ltdb_private *ltdb,
 	return tdb_ret;
 }
 
-static int ltdb_parse_record_wrapper(TDB_DATA tdb_key, TDB_DATA tdb_data,
-					 void *ctx)
+static int ltdb_parse_record_wrapper(TDB_DATA tdb_key,
+				     TDB_DATA tdb_data,
+				     void *ctx)
 {
 	struct kv_ctx *kv_ctx = ctx;
 	struct ldb_val key = {
@@ -1981,11 +1987,11 @@ static int ltdb_parse_record_wrapper(TDB_DATA tdb_key, TDB_DATA tdb_data,
 }
 
 static int ltdb_parse_record(struct ltdb_private *ltdb,
-				 struct ldb_val ldb_key,
-				 int (*parser)(struct ldb_val key,
-					       struct ldb_val data,
-					       void *private_data),
-				 void *ctx)
+			     struct ldb_val ldb_key,
+			     int (*parser)(struct ldb_val key,
+					   struct ldb_val data,
+					   void *private_data),
+			     void *ctx)
 {
 	struct kv_ctx kv_ctx = {
 		.parser = parser,
@@ -2003,15 +2009,15 @@ static int ltdb_parse_record(struct ltdb_private *ltdb,
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	ret = tdb_parse_record(ltdb->tdb, key, ltdb_parse_record_wrapper,
-			       &kv_ctx);
+	ret = tdb_parse_record(
+	    ltdb->tdb, key, ltdb_parse_record_wrapper, &kv_ctx);
 	if (ret == 0) {
 		return LDB_SUCCESS;
 	}
 	return ltdb_err_map(tdb_error(ltdb->tdb));
 }
 
-static const char * ltdb_name(struct ltdb_private *ltdb)
+static const char *ltdb_name(struct ltdb_private *ltdb)
 {
 	return tdb_name(ltdb->tdb);
 }
@@ -2032,28 +2038,28 @@ static bool ltdb_transaction_active(struct ltdb_private *ltdb)
 }
 
 static const struct kv_db_ops key_value_ops = {
-	.store = ltdb_store,
-	.delete = ltdb_delete,
-	.iterate = ltdb_traverse_fn,
-	.update_in_iterate = ltdb_update_in_iterate,
-	.fetch_and_parse = ltdb_parse_record,
-	.lock_read = ltdb_lock_read,
-	.unlock_read = ltdb_unlock_read,
-	.begin_write = ltdb_transaction_start,
-	.prepare_write = ltdb_transaction_prepare_commit,
-	.finish_write = ltdb_transaction_commit,
-	.abort_write = ltdb_transaction_cancel,
-	.error = ltdb_error,
-	.errorstr = ltdb_errorstr,
-	.name = ltdb_name,
-	.has_changed = ltdb_changed,
-	.transaction_active = ltdb_transaction_active,
+    .store = ltdb_store,
+    .delete = ltdb_delete,
+    .iterate = ltdb_traverse_fn,
+    .update_in_iterate = ltdb_update_in_iterate,
+    .fetch_and_parse = ltdb_parse_record,
+    .lock_read = ltdb_lock_read,
+    .unlock_read = ltdb_unlock_read,
+    .begin_write = ltdb_transaction_start,
+    .prepare_write = ltdb_transaction_prepare_commit,
+    .finish_write = ltdb_transaction_commit,
+    .abort_write = ltdb_transaction_cancel,
+    .error = ltdb_error,
+    .errorstr = ltdb_errorstr,
+    .name = ltdb_name,
+    .has_changed = ltdb_changed,
+    .transaction_active = ltdb_transaction_active,
 };
 
 static void ldb_kv_callback(struct tevent_context *ev,
-			  struct tevent_timer *te,
-			  struct timeval t,
-			  void *private_data)
+			    struct tevent_timer *te,
+			    struct timeval t,
+			    void *private_data)
 {
 	struct ltdb_context *ctx;
 	int ret;
@@ -2116,7 +2122,7 @@ static int ldb_kv_request_destructor(void *ptr)
 }
 
 static int ldb_kv_handle_request(struct ldb_module *module,
-			       struct ldb_request *req)
+				 struct ldb_request *req)
 {
 	struct ldb_control *control_permissive;
 	struct ldb_context *ldb;
@@ -2167,8 +2173,8 @@ static int ldb_kv_handle_request(struct ldb_module *module,
 	if (req->timeout > 0) {
 		tv.tv_sec = req->starttime + req->timeout;
 		tv.tv_usec = 0;
-		ac->timeout_event = tevent_add_timer(ev, ac, tv,
-						     ldb_kv_timeout, ac);
+		ac->timeout_event =
+		    tevent_add_timer(ev, ac, tv, ldb_kv_timeout, ac);
 		if (NULL == ac->timeout_event) {
 			talloc_free(ac);
 			return LDB_ERR_OPERATIONS_ERROR;
@@ -2198,7 +2204,6 @@ static int ldb_kv_init_rootdse(struct ldb_module *module)
 	return LDB_SUCCESS;
 }
 
-
 static int ldb_kv_lock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
@@ -2214,20 +2219,20 @@ static int ldb_kv_unlock_read(struct ldb_module *module)
 }
 
 static const struct ldb_module_ops ldb_kv_ops = {
-	.name              = "tdb",
-	.init_context      = ldb_kv_init_rootdse,
-	.search            = ldb_kv_handle_request,
-	.add               = ldb_kv_handle_request,
-	.modify            = ldb_kv_handle_request,
-	.del               = ldb_kv_handle_request,
-	.rename            = ldb_kv_handle_request,
-	.extended          = ldb_kv_handle_request,
-	.start_transaction = ldb_kv_start_trans,
-	.end_transaction   = ldb_kv_end_trans,
-	.prepare_commit    = ldb_kv_prepare_commit,
-	.del_transaction   = ldb_kv_del_trans,
-	.read_lock         = ldb_kv_lock_read,
-	.read_unlock       = ldb_kv_unlock_read,
+    .name = "tdb",
+    .init_context = ldb_kv_init_rootdse,
+    .search = ldb_kv_handle_request,
+    .add = ldb_kv_handle_request,
+    .modify = ldb_kv_handle_request,
+    .del = ldb_kv_handle_request,
+    .rename = ldb_kv_handle_request,
+    .extended = ldb_kv_handle_request,
+    .start_transaction = ldb_kv_start_trans,
+    .end_transaction = ldb_kv_end_trans,
+    .prepare_commit = ldb_kv_prepare_commit,
+    .del_transaction = ldb_kv_del_trans,
+    .read_lock = ldb_kv_lock_read,
+    .read_unlock = ldb_kv_unlock_read,
 };
 
 int ldb_kv_init_store(struct ltdb_private *ltdb,
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.h b/lib/ldb/ldb_tdb/ldb_tdb.h
index 675355e..861d94a 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -164,49 +164,55 @@ struct ldb_parse_tree;
 
 int ldb_kv_search_indexed(struct ltdb_context *ctx, uint32_t *);
 int ldb_kv_index_add_new(struct ldb_module *module,
-		       struct ltdb_private *ltdb,
-		       const struct ldb_message *msg);
-int ldb_kv_index_delete(struct ldb_module *module, const struct ldb_message *msg);
+			 struct ltdb_private *ltdb,
+			 const struct ldb_message *msg);
+int ldb_kv_index_delete(struct ldb_module *module,
+			const struct ldb_message *msg);
 int ldb_kv_index_del_element(struct ldb_module *module,
-			   struct ltdb_private *ltdb,
-			   const struct ldb_message *msg,
-			   struct ldb_message_element *el);
+			     struct ltdb_private *ltdb,
+			     const struct ldb_message *msg,
+			     struct ldb_message_element *el);
 int ldb_kv_index_add_element(struct ldb_module *module,
+			     struct ltdb_private *ltdb,
+			     const struct ldb_message *msg,
+			     struct ldb_message_element *el);
+int ldb_kv_index_del_value(struct ldb_module *module,
 			   struct ltdb_private *ltdb,
 			   const struct ldb_message *msg,
-			   struct ldb_message_element *el);
-int ldb_kv_index_del_value(struct ldb_module *module,
-			 struct ltdb_private *ltdb,
-			 const struct ldb_message *msg,
-			 struct ldb_message_element *el, unsigned int v_idx);
+			   struct ldb_message_element *el,
+			   unsigned int v_idx);
 int ldb_kv_reindex(struct ldb_module *module);
 int ldb_kv_index_transaction_start(struct ldb_module *module);
 int ldb_kv_index_transaction_commit(struct ldb_module *module);
 int ldb_kv_index_transaction_cancel(struct ldb_module *module);
 int ldb_kv_key_dn_from_idx(struct ldb_module *module,
-			 struct ltdb_private *ltdb,
-			 TALLOC_CTX *mem_ctx,
-			 struct ldb_dn *dn,
-			 TDB_DATA *tdb_key);
+			   struct ltdb_private *ltdb,
+			   TALLOC_CTX *mem_ctx,
+			   struct ldb_dn *dn,
+			   TDB_DATA *tdb_key);
 
 /* The following definitions come from lib/ldb/ldb_tdb/ldb_search.c  */
 
 int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name, 
 		      const struct ldb_val *val);
 void ltdb_search_dn1_free(struct ldb_module *module, struct ldb_message *msg);
-int ldb_kv_search_dn1(struct ldb_module *module, struct ldb_dn *dn, struct ldb_message *msg,
-		    unsigned int unpack_flags);
+int ldb_kv_search_dn1(struct ldb_module *module,
+		      struct ldb_dn *dn,
+		      struct ldb_message *msg,
+		      unsigned int unpack_flags);
 int ldb_kv_search_base(struct ldb_module *module,
-		     TALLOC_CTX *mem_ctx,
-		     struct ldb_dn *dn,
-		     struct ldb_dn **ret_dn);
-int ldb_kv_search_key(struct ldb_module *module, struct ltdb_private *ltdb,
-		    struct TDB_DATA tdb_key,
-		    struct ldb_message *msg,
-		    unsigned int unpack_flags);
+		       TALLOC_CTX *mem_ctx,
+		       struct ldb_dn *dn,
+		       struct ldb_dn **ret_dn);
+int ldb_kv_search_key(struct ldb_module *module,
+		      struct ltdb_private *ltdb,
+		      struct TDB_DATA tdb_key,
+		      struct ldb_message *msg,
+		      unsigned int unpack_flags);
 int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
-		      const struct ldb_message *msg, const char * const *attrs,
-		      struct ldb_message **filtered_msg);
+			const struct ldb_message *msg,
+			const char *const *attrs,
+			struct ldb_message **filtered_msg);
 int ldb_kv_search(struct ltdb_context *ctx);
 
 /* The following definitions come from lib/ldb/ldb_tdb/ldb_tdb.c  */
@@ -215,32 +221,40 @@ int ldb_kv_search(struct ltdb_context *ctx);
  * index, the old DN index and a possible future ID=
  */
 bool ldb_kv_key_is_record(TDB_DATA key);
-TDB_DATA ldb_kv_key_dn(struct ldb_module *module, TALLOC_CTX *mem_ctx,
-		     struct ldb_dn *dn);
-TDB_DATA ldb_kv_key_msg(struct ldb_module *module, TALLOC_CTX *mem_ctx,
-		      const struct ldb_message *msg);
+TDB_DATA ldb_kv_key_dn(struct ldb_module *module,
+		       TALLOC_CTX *mem_ctx,
+		       struct ldb_dn *dn);
+TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
+			TALLOC_CTX *mem_ctx,
+			const struct ldb_message *msg);
 int ldb_kv_guid_to_key(struct ldb_module *module,
-		     struct ltdb_private *ltdb,
-		     const struct ldb_val *GUID_val,
-		     TDB_DATA *key);
+		       struct ltdb_private *ltdb,
+		       const struct ldb_val *GUID_val,
+		       TDB_DATA *key);
 int ldb_kv_idx_to_key(struct ldb_module *module,
-		    struct ltdb_private *ltdb,
-		    TALLOC_CTX *mem_ctx,
-		    const struct ldb_val *idx_val,
-		    TDB_DATA *key);
+		      struct ltdb_private *ltdb,
+		      TALLOC_CTX *mem_ctx,
+		      const struct ldb_val *idx_val,
+		      TDB_DATA *key);
 TDB_DATA ltdb_key(struct ldb_module *module, struct ldb_dn *dn);
-int ldb_kv_store(struct ldb_module *module, const struct ldb_message *msg, int flgs);
-int ldb_kv_modify_internal(struct ldb_module *module, const struct ldb_message *msg, struct ldb_request *req);
+int ldb_kv_store(struct ldb_module *module,
+		 const struct ldb_message *msg,
+		 int flgs);
+int ldb_kv_modify_internal(struct ldb_module *module,
+			   const struct ldb_message *msg,
+			   struct ldb_request *req);
 int ldb_kv_delete_noindex(struct ldb_module *module,
-			const struct ldb_message *msg);
+			  const struct ldb_message *msg);
 int ltdb_err_map(enum TDB_ERROR tdb_code);
 
 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
 				   const char *path, int hash_size, int tdb_flags,
 				   int open_flags, mode_t mode,
 				   struct ldb_context *ldb);
-int ldb_kv_init_store(struct ltdb_private *ltdb, const char *name,
-		      struct ldb_context *ldb, const char *options[],
+int ldb_kv_init_store(struct ltdb_private *ltdb,
+		      const char *name,
+		      struct ldb_context *ldb,
+		      const char *options[],
 		      struct ldb_module **_module);
 
 int ltdb_connect(struct ldb_context *ldb, const char *url,
-- 
2.7.4


From c612cc5e89310c6884437bb13f267dc768bfa622 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 08:52:48 +1200
Subject: [PATCH 03/18] lib ldb: rename struct ltdb_reindex_context

Rename struct ltdb_reindex_context to ldb_kv_reindex_context, as this is
a key value level structure and not a tdb specific structure.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_tdb/ldb_index.c | 6 +++---
 lib/ldb/ldb_tdb/ldb_tdb.c   | 2 +-
 lib/ldb/ldb_tdb/ldb_tdb.h   | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/ldb/ldb_tdb/ldb_index.c b/lib/ldb/ldb_tdb/ldb_index.c
index 23b8aa0..4d6c57b 100644
--- a/lib/ldb/ldb_tdb/ldb_index.c
+++ b/lib/ldb/ldb_tdb/ldb_index.c
@@ -2832,7 +2832,7 @@ static int delete_index(struct ltdb_private *ltdb, struct ldb_val key, struct ld
 static int re_key(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_val val, void *state)
 {
 	struct ldb_context *ldb;
-	struct ltdb_reindex_context *ctx = (struct ltdb_reindex_context *)state;
+	struct ldb_kv_reindex_context *ctx = (struct ldb_kv_reindex_context *)state;
 	struct ldb_module *module = ctx->module;
 	struct ldb_message *msg;
 	unsigned int nb_elements_in_db;
@@ -2923,7 +2923,7 @@ static int re_key(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_
 static int re_index(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_val val, void *state)
 {
 	struct ldb_context *ldb;
-	struct ltdb_reindex_context *ctx = (struct ltdb_reindex_context *)state;
+	struct ldb_kv_reindex_context *ctx = (struct ldb_kv_reindex_context *)state;
 	struct ldb_module *module = ctx->module;
 	struct ldb_message *msg;
 	unsigned int nb_elements_in_db;
@@ -3010,7 +3010,7 @@ int ldb_kv_reindex(struct ldb_module *module)
 {
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
 	int ret;
-	struct ltdb_reindex_context ctx;
+	struct ldb_kv_reindex_context ctx;
 
 	/*
 	 * Only triggered after a modification, but make clear we do
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.c b/lib/ldb/ldb_tdb/ldb_tdb.c
index c0938c0..348b5fc 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -1924,7 +1924,7 @@ static int ltdb_update_in_iterate(struct ltdb_private *ltdb,
 {
 	int tdb_ret;
 	struct ldb_context *ldb;
-	struct ltdb_reindex_context *ctx = (struct ltdb_reindex_context *)state;
+	struct ldb_kv_reindex_context *ctx = (struct ldb_kv_reindex_context *)state;
 	struct ldb_module *module = ctx->module;
 	TDB_DATA key = {
 		.dptr = ldb_key.data,
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.h b/lib/ldb/ldb_tdb/ldb_tdb.h
index 861d94a..d7f6073 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -109,7 +109,7 @@ struct ltdb_context {
 	int error;
 };
 
-struct ltdb_reindex_context {
+struct ldb_kv_reindex_context {
 	struct ldb_module *module;
 	int error;
 	uint32_t count;
-- 
2.7.4


From d02fa44014694b8870750781867826d5ef4b36ae Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 08:53:28 +1200
Subject: [PATCH 04/18] lib ldb: reformat ltdb_reindex_context rename

Fix up the formatting after the rename of ltdb_reindex_context to
ldb_kv_reindex_context.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_tdb/ldb_index.c | 6 ++++--
 lib/ldb/ldb_tdb/ldb_tdb.c   | 3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/lib/ldb/ldb_tdb/ldb_index.c b/lib/ldb/ldb_tdb/ldb_index.c
index 4d6c57b..1399f69 100644
--- a/lib/ldb/ldb_tdb/ldb_index.c
+++ b/lib/ldb/ldb_tdb/ldb_index.c
@@ -2832,7 +2832,8 @@ static int delete_index(struct ltdb_private *ltdb, struct ldb_val key, struct ld
 static int re_key(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_val val, void *state)
 {
 	struct ldb_context *ldb;
-	struct ldb_kv_reindex_context *ctx = (struct ldb_kv_reindex_context *)state;
+	struct ldb_kv_reindex_context *ctx =
+	    (struct ldb_kv_reindex_context *)state;
 	struct ldb_module *module = ctx->module;
 	struct ldb_message *msg;
 	unsigned int nb_elements_in_db;
@@ -2923,7 +2924,8 @@ static int re_key(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_
 static int re_index(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_val val, void *state)
 {
 	struct ldb_context *ldb;
-	struct ldb_kv_reindex_context *ctx = (struct ldb_kv_reindex_context *)state;
+	struct ldb_kv_reindex_context *ctx =
+	    (struct ldb_kv_reindex_context *)state;
 	struct ldb_module *module = ctx->module;
 	struct ldb_message *msg;
 	unsigned int nb_elements_in_db;
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.c b/lib/ldb/ldb_tdb/ldb_tdb.c
index 348b5fc..29c0ead 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -1924,7 +1924,8 @@ static int ltdb_update_in_iterate(struct ltdb_private *ltdb,
 {
 	int tdb_ret;
 	struct ldb_context *ldb;
-	struct ldb_kv_reindex_context *ctx = (struct ldb_kv_reindex_context *)state;
+	struct ldb_kv_reindex_context *ctx =
+	    (struct ldb_kv_reindex_context *)state;
 	struct ldb_module *module = ctx->module;
 	TDB_DATA key = {
 		.dptr = ldb_key.data,
-- 
2.7.4


From 883f1a03cc7439c1ce21e7d6f512036fdc645410 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 09:07:47 +1200
Subject: [PATCH 05/18] lib ldb: rename ltdb_context to ldb_kv_context

Rename ltdb_context to ldb_kv_context as it is a key value level
structure and not tdb specific.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_tdb/ldb_index.c  |  4 ++--
 lib/ldb/ldb_tdb/ldb_search.c | 10 +++++-----
 lib/ldb/ldb_tdb/ldb_tdb.c    | 30 +++++++++++++++---------------
 lib/ldb/ldb_tdb/ldb_tdb.h    |  6 +++---
 4 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/lib/ldb/ldb_tdb/ldb_index.c b/lib/ldb/ldb_tdb/ldb_index.c
index 1399f69..a04431e 100644
--- a/lib/ldb/ldb_tdb/ldb_index.c
+++ b/lib/ldb/ldb_tdb/ldb_index.c
@@ -1734,7 +1734,7 @@ static int ldb_kv_index_dn(struct ldb_module *module,
 */
 static int ldb_kv_index_filter(struct ltdb_private *ltdb,
 			       const struct dn_list *dn_list,
-			       struct ltdb_context *ac,
+			       struct ldb_kv_context *ac,
 			       uint32_t *match_count,
 			       enum key_truncation scope_one_truncation)
 {
@@ -1936,7 +1936,7 @@ static void ldb_kv_dn_list_sort(struct ltdb_private *ltdb, struct dn_list *list)
   returns -1 if an indexed search is not possible, in which
   case the caller should call ltdb_search_full()
 */
-int ldb_kv_search_indexed(struct ltdb_context *ac, uint32_t *match_count)
+int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
 	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(ac->module), struct ltdb_private);
diff --git a/lib/ldb/ldb_tdb/ldb_search.c b/lib/ldb/ldb_tdb/ldb_search.c
index a0df5d0..77938b7 100644
--- a/lib/ldb/ldb_tdb/ldb_search.c
+++ b/lib/ldb/ldb_tdb/ldb_search.c
@@ -496,7 +496,7 @@ failed:
 static int search_func(struct ltdb_private *ltdb, struct ldb_val key, struct ldb_val val, void *state)
 {
 	struct ldb_context *ldb;
-	struct ltdb_context *ac;
+	struct ldb_kv_context *ac;
 	struct ldb_message *msg, *filtered_msg;
 	int ret;
 	bool matched;
@@ -506,7 +506,7 @@ static int search_func(struct ltdb_private *ltdb, struct ldb_val key, struct ldb
 		.dsize = key.length
 	};
 
-	ac = talloc_get_type(state, struct ltdb_context);
+	ac = talloc_get_type(state, struct ldb_kv_context);
 	ldb = ldb_module_get_ctx(ac->module);
 
 	if (ldb_kv_key_is_record(tdb_key) == false) {
@@ -580,7 +580,7 @@ static int search_func(struct ltdb_private *ltdb, struct ldb_val key, struct ldb
   search the database with a LDAP-like expression.
   this is the "full search" non-indexed variant
 */
-static int ldb_kv_search_full(struct ltdb_context *ctx)
+static int ldb_kv_search_full(struct ldb_kv_context *ctx)
 {
 	void *data = ldb_module_get_private(ctx->module);
 	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
@@ -597,7 +597,7 @@ static int ldb_kv_search_full(struct ltdb_context *ctx)
 }
 
 static int ldb_kv_search_and_return_base(struct ltdb_private *ltdb,
-					 struct ltdb_context *ctx)
+					 struct ldb_kv_context *ctx)
 {
 	struct ldb_message *msg, *filtered_msg;
 	struct ldb_context *ldb = ldb_module_get_ctx(ctx->module);
@@ -702,7 +702,7 @@ static int ldb_kv_search_and_return_base(struct ltdb_private *ltdb,
   search the database with a LDAP-like expression.
   choses a search method
 */
-int ldb_kv_search(struct ltdb_context *ctx)
+int ldb_kv_search(struct ldb_kv_context *ctx)
 {
 	struct ldb_context *ldb;
 	struct ldb_module *module = ctx->module;
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.c b/lib/ldb/ldb_tdb/ldb_tdb.c
index 29c0ead..1f0f0fc 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -57,7 +57,7 @@
   prevent memory errors on callbacks
 */
 struct ltdb_req_spy {
-	struct ltdb_context *ctx;
+	struct ldb_kv_context *ctx;
 };
 
 /*
@@ -671,7 +671,7 @@ static int ldb_kv_add_internal(struct ldb_module *module,
 /*
   add a record to the database
 */
-static int ldb_kv_add(struct ltdb_context *ctx)
+static int ldb_kv_add(struct ldb_kv_context *ctx)
 {
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
@@ -803,7 +803,7 @@ done:
 /*
   delete a record from the database
 */
-static int ldb_kv_delete(struct ltdb_context *ctx)
+static int ldb_kv_delete(struct ldb_kv_context *ctx)
 {
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
@@ -1335,7 +1335,7 @@ done:
 /*
   modify a record
 */
-static int ldb_kv_modify(struct ltdb_context *ctx)
+static int ldb_kv_modify(struct ldb_kv_context *ctx)
 {
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
@@ -1360,7 +1360,7 @@ static int ldb_kv_modify(struct ltdb_context *ctx)
 /*
   rename a record
 */
-static int ldb_kv_rename(struct ltdb_context *ctx)
+static int ldb_kv_rename(struct ldb_kv_context *ctx)
 {
 	struct ldb_module *module = ctx->module;
 	void *data = ldb_module_get_private(module);
@@ -1686,7 +1686,7 @@ static int ldb_kv_del_trans(struct ldb_module *module)
 /*
   return sequenceNumber from @BASEINFO
 */
-static int ldb_kv_sequence_number(struct ltdb_context *ctx,
+static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
 				  struct ldb_extended **ext)
 {
 	struct ldb_context *ldb;
@@ -1779,7 +1779,7 @@ done:
 	return ret;
 }
 
-static void ldb_kv_request_done(struct ltdb_context *ctx, int error)
+static void ldb_kv_request_done(struct ldb_kv_context *ctx, int error)
 {
 	struct ldb_context *ldb;
 	struct ldb_request *req;
@@ -1810,8 +1810,8 @@ static void ldb_kv_timeout(struct tevent_context *ev,
 			   struct timeval t,
 			   void *private_data)
 {
-	struct ltdb_context *ctx;
-	ctx = talloc_get_type(private_data, struct ltdb_context);
+	struct ldb_kv_context *ctx;
+	ctx = talloc_get_type(private_data, struct ldb_kv_context);
 
 	if (!ctx->request_terminated) {
 		/* request is done now */
@@ -1826,7 +1826,7 @@ static void ldb_kv_timeout(struct tevent_context *ev,
 	talloc_free(ctx);
 }
 
-static void ldb_kv_request_extended_done(struct ltdb_context *ctx,
+static void ldb_kv_request_extended_done(struct ldb_kv_context *ctx,
 					 struct ldb_extended *ext,
 					 int error)
 {
@@ -1855,7 +1855,7 @@ static void ldb_kv_request_extended_done(struct ltdb_context *ctx,
 	req->callback(req, ares);
 }
 
-static void ldb_kv_handle_extended(struct ltdb_context *ctx)
+static void ldb_kv_handle_extended(struct ldb_kv_context *ctx)
 {
 	struct ldb_extended *ext = NULL;
 	int ret;
@@ -2062,10 +2062,10 @@ static void ldb_kv_callback(struct tevent_context *ev,
 			    struct timeval t,
 			    void *private_data)
 {
-	struct ltdb_context *ctx;
+	struct ldb_kv_context *ctx;
 	int ret;
 
-	ctx = talloc_get_type(private_data, struct ltdb_context);
+	ctx = talloc_get_type(private_data, struct ldb_kv_context);
 
 	if (ctx->request_terminated) {
 		goto done;
@@ -2128,7 +2128,7 @@ static int ldb_kv_handle_request(struct ldb_module *module,
 	struct ldb_control *control_permissive;
 	struct ldb_context *ldb;
 	struct tevent_context *ev;
-	struct ltdb_context *ac;
+	struct ldb_kv_context *ac;
 	struct tevent_timer *te;
 	struct timeval tv;
 	unsigned int i;
@@ -2154,7 +2154,7 @@ static int ldb_kv_handle_request(struct ldb_module *module,
 
 	ev = ldb_handle_get_event_context(req->handle);
 
-	ac = talloc_zero(ldb, struct ltdb_context);
+	ac = talloc_zero(ldb, struct ldb_kv_context);
 	if (ac == NULL) {
 		ldb_oom(ldb);
 		return LDB_ERR_OPERATIONS_ERROR;
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.h b/lib/ldb/ldb_tdb/ldb_tdb.h
index d7f6073..5c6cf98 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -91,7 +91,7 @@ struct ltdb_private {
 	pid_t pid;
 };
 
-struct ltdb_context {
+struct ldb_kv_context {
 	struct ldb_module *module;
 	struct ldb_request *req;
 
@@ -162,7 +162,7 @@ int ldb_kv_check_at_attributes_values(const struct ldb_val *value);
 
 struct ldb_parse_tree;
 
-int ldb_kv_search_indexed(struct ltdb_context *ctx, uint32_t *);
+int ldb_kv_search_indexed(struct ldb_kv_context *ctx, uint32_t *);
 int ldb_kv_index_add_new(struct ldb_module *module,
 			 struct ltdb_private *ltdb,
 			 const struct ldb_message *msg);
@@ -213,7 +213,7 @@ int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
 			const struct ldb_message *msg,
 			const char *const *attrs,
 			struct ldb_message **filtered_msg);
-int ldb_kv_search(struct ltdb_context *ctx);
+int ldb_kv_search(struct ldb_kv_context *ctx);
 
 /* The following definitions come from lib/ldb/ldb_tdb/ldb_tdb.c  */
 /* 
-- 
2.7.4


From 75ada210d6be6ef1e9312b14a8dd4031bf8039b8 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 09:14:52 +1200
Subject: [PATCH 06/18] lib ldb: rename ltdb_req_spy to ldb_kv_req_spy

Rename ltdb_req_spy to ldb_kv_req_spy, as it is key value level and not
tdb specific.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_tdb/ldb_tdb.c | 6 +++---
 lib/ldb/ldb_tdb/ldb_tdb.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/ldb/ldb_tdb/ldb_tdb.c b/lib/ldb/ldb_tdb/ldb_tdb.c
index 1f0f0fc..339d274 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -56,7 +56,7 @@
 /*
   prevent memory errors on callbacks
 */
-struct ltdb_req_spy {
+struct ldb_kv_req_spy {
 	struct ldb_kv_context *ctx;
 };
 
@@ -2111,7 +2111,7 @@ done:
 
 static int ldb_kv_request_destructor(void *ptr)
 {
-	struct ltdb_req_spy *spy = talloc_get_type(ptr, struct ltdb_req_spy);
+	struct ldb_kv_req_spy *spy = talloc_get_type(ptr, struct ldb_kv_req_spy);
 
 	if (spy->ctx != NULL) {
 		spy->ctx->spy = NULL;
@@ -2184,7 +2184,7 @@ static int ldb_kv_handle_request(struct ldb_module *module,
 
 	/* set a spy so that we do not try to use the request context
 	 * if it is freed before ltdb_callback fires */
-	ac->spy = talloc(req, struct ltdb_req_spy);
+	ac->spy = talloc(req, struct ldb_kv_req_spy);
 	if (NULL == ac->spy) {
 		talloc_free(ac);
 		return LDB_ERR_OPERATIONS_ERROR;
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.h b/lib/ldb/ldb_tdb/ldb_tdb.h
index 5c6cf98..9a9471e 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -96,7 +96,7 @@ struct ldb_kv_context {
 	struct ldb_request *req;
 
 	bool request_terminated;
-	struct ltdb_req_spy *spy;
+	struct ldb_kv_req_spy *spy;
 
 	/* search stuff */
 	const struct ldb_parse_tree *tree;
-- 
2.7.4


From 2b2481f35e1a1c6de2fcc04a12269dd50cb31c54 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 09:15:24 +1200
Subject: [PATCH 07/18] lib ldb: format rename of  ltdb_req_spy

Fix up the code formatting after the rename of ltdb_req_spy to
ldb_kv_req_spy

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_tdb/ldb_tdb.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/ldb/ldb_tdb/ldb_tdb.c b/lib/ldb/ldb_tdb/ldb_tdb.c
index 339d274..d01fa9e 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -2111,7 +2111,8 @@ done:
 
 static int ldb_kv_request_destructor(void *ptr)
 {
-	struct ldb_kv_req_spy *spy = talloc_get_type(ptr, struct ldb_kv_req_spy);
+	struct ldb_kv_req_spy *spy =
+	    talloc_get_type(ptr, struct ldb_kv_req_spy);
 
 	if (spy->ctx != NULL) {
 		spy->ctx->spy = NULL;
-- 
2.7.4


From 3e6cd8e6b17ae7a47430f4834ba2aed3fea23af2 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 11:53:21 +1200
Subject: [PATCH 08/18] lib ldb: rename ltdb_private to ldb_kv_private

Rename ltdb_private to ldb_kv_private as it contains key value operation
context.

Note there is still some tdb specific context that can be refactored into a
separate structure along the lines of the lmdb context.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_mdb/ldb_mdb.c       | 106 ++++++------
 lib/ldb/ldb_tdb/ldb_cache.c     | 118 ++++++-------
 lib/ldb/ldb_tdb/ldb_index.c     | 374 ++++++++++++++++++++--------------------
 lib/ldb/ldb_tdb/ldb_search.c    |  50 +++---
 lib/ldb/ldb_tdb/ldb_tdb.c       | 358 +++++++++++++++++++-------------------
 lib/ldb/ldb_tdb/ldb_tdb.h       |  54 +++---
 lib/ldb/tests/ldb_kv_ops_test.c | 242 +++++++++++++-------------
 lib/ldb/tests/ldb_lmdb_test.c   |   8 +-
 lib/ldb/tests/ldb_tdb_test.c    |   8 +-
 9 files changed, 659 insertions(+), 659 deletions(-)

diff --git a/lib/ldb/ldb_mdb/ldb_mdb.c b/lib/ldb/ldb_mdb/ldb_mdb.c
index 74b598d..7fbe044 100644
--- a/lib/ldb/ldb_mdb/ldb_mdb.c
+++ b/lib/ldb/ldb_mdb/ldb_mdb.c
@@ -91,9 +91,9 @@ static int lmdb_error_at(struct ldb_context *ldb,
 }
 
 
-static bool lmdb_transaction_active(struct ltdb_private *ltdb)
+static bool lmdb_transaction_active(struct ldb_kv_private *ldb_kv)
 {
-	return ltdb->lmdb_private->txlist != NULL;
+	return ldb_kv->lmdb_private->txlist != NULL;
 }
 
 static MDB_txn *lmdb_trans_get_tx(struct lmdb_trans *ltx)
@@ -146,18 +146,18 @@ static MDB_txn *get_current_txn(struct lmdb_private *lmdb)
 	return NULL;
 }
 
-static int lmdb_store(struct ltdb_private *ltdb,
+static int lmdb_store(struct ldb_kv_private *ldb_kv,
 		      struct ldb_val key,
 		      struct ldb_val data, int flags)
 {
-	struct lmdb_private *lmdb = ltdb->lmdb_private;
+	struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 	MDB_val mdb_key;
 	MDB_val mdb_data;
 	int mdb_flags;
 	MDB_txn *txn = NULL;
 	MDB_dbi dbi = 0;
 
-	if (ltdb->read_only) {
+	if (ldb_kv->read_only) {
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
@@ -204,14 +204,14 @@ static int lmdb_store(struct ltdb_private *ltdb,
 	return ldb_mdb_err_map(lmdb->error);
 }
 
-static int lmdb_delete(struct ltdb_private *ltdb, struct ldb_val key)
+static int lmdb_delete(struct ldb_kv_private *ldb_kv, struct ldb_val key)
 {
-	struct lmdb_private *lmdb = ltdb->lmdb_private;
+	struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 	MDB_val mdb_key;
 	MDB_txn *txn = NULL;
 	MDB_dbi dbi = 0;
 
-	if (ltdb->read_only) {
+	if (ldb_kv->read_only) {
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
@@ -237,11 +237,11 @@ static int lmdb_delete(struct ltdb_private *ltdb, struct ldb_val key)
 	return ldb_mdb_err_map(lmdb->error);
 }
 
-static int lmdb_traverse_fn(struct ltdb_private *ltdb,
+static int lmdb_traverse_fn(struct ldb_kv_private *ldb_kv,
 			    ldb_kv_traverse_fn fn,
 			    void *ctx)
 {
-	struct lmdb_private *lmdb = ltdb->lmdb_private;
+	struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 	MDB_val mdb_key;
 	MDB_val mdb_data;
 	MDB_txn *txn = NULL;
@@ -279,7 +279,7 @@ static int lmdb_traverse_fn(struct ltdb_private *ltdb,
 			.data = mdb_data.mv_data,
 		};
 
-		ret = fn(ltdb, key, data, ctx);
+		ret = fn(ldb_kv, key, data, ctx);
 		if (ret != 0) {
 			goto done;
 		}
@@ -298,13 +298,13 @@ done:
 	return ldb_mdb_err_map(lmdb->error);
 }
 
-static int lmdb_update_in_iterate(struct ltdb_private *ltdb,
+static int lmdb_update_in_iterate(struct ldb_kv_private *ldb_kv,
 				  struct ldb_val key,
 				  struct ldb_val key2,
 				  struct ldb_val data,
 				  void *state)
 {
-	struct lmdb_private *lmdb = ltdb->lmdb_private;
+	struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 	struct ldb_val copy;
 	int ret = LDB_SUCCESS;
 
@@ -313,13 +313,13 @@ static int lmdb_update_in_iterate(struct ltdb_private *ltdb,
 	 * data, as it is in private lmdb memory.
 	 */
 	copy.length = data.length;
-	copy.data = talloc_memdup(ltdb, data.data, data.length);
+	copy.data = talloc_memdup(ldb_kv, data.data, data.length);
 	if (copy.data == NULL) {
 		lmdb->error = MDB_PANIC;
 		return ldb_oom(lmdb->ldb);
 	}
 
-	lmdb->error = lmdb_delete(ltdb, key);
+	lmdb->error = lmdb_delete(ldb_kv, key);
 	if (lmdb->error != MDB_SUCCESS) {
 		ldb_debug(
 			lmdb->ldb,
@@ -335,7 +335,7 @@ static int lmdb_update_in_iterate(struct ltdb_private *ltdb,
 		goto done;
 	}
 
-	lmdb->error = lmdb_store(ltdb, key2, copy, 0);
+	lmdb->error = lmdb_store(ldb_kv, key2, copy, 0);
 	if (lmdb->error != MDB_SUCCESS) {
 		ldb_debug(
 			lmdb->ldb,
@@ -366,12 +366,12 @@ done:
 }
 
 /* Handles only a single record */
-static int lmdb_parse_record(struct ltdb_private *ltdb, struct ldb_val key,
+static int lmdb_parse_record(struct ldb_kv_private *ldb_kv, struct ldb_val key,
 			     int (*parser)(struct ldb_val key, struct ldb_val data,
 					   void *private_data),
 			     void *ctx)
 {
-	struct lmdb_private *lmdb = ltdb->lmdb_private;
+	struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 	MDB_val mdb_key;
 	MDB_val mdb_data;
 	MDB_txn *txn = NULL;
@@ -415,8 +415,8 @@ static int lmdb_parse_record(struct ltdb_private *ltdb, struct ldb_val key,
 static int lmdb_lock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
-	struct lmdb_private *lmdb = ltdb->lmdb_private;
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 	pid_t pid = getpid();
 
 	if (pid != lmdb->pid) {
@@ -431,8 +431,8 @@ static int lmdb_lock_read(struct ldb_module *module)
 	}
 
 	lmdb->error = MDB_SUCCESS;
-	if (lmdb_transaction_active(ltdb) == false &&
-	    ltdb->read_lock_count == 0) {
+	if (lmdb_transaction_active(ldb_kv) == false &&
+	    ldb_kv->read_lock_count == 0) {
 		lmdb->error = mdb_txn_begin(lmdb->env,
 					    NULL,
 					    MDB_RDONLY,
@@ -442,36 +442,36 @@ static int lmdb_lock_read(struct ldb_module *module)
 		return ldb_mdb_error(lmdb->ldb, lmdb->error);
 	}
 
-	ltdb->read_lock_count++;
+	ldb_kv->read_lock_count++;
 	return ldb_mdb_err_map(lmdb->error);
 }
 
 static int lmdb_unlock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 
-	if (lmdb_transaction_active(ltdb) == false && ltdb->read_lock_count == 1) {
-		struct lmdb_private *lmdb = ltdb->lmdb_private;
+	if (lmdb_transaction_active(ldb_kv) == false && ldb_kv->read_lock_count == 1) {
+		struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 		mdb_txn_commit(lmdb->read_txn);
 		lmdb->read_txn = NULL;
-		ltdb->read_lock_count--;
+		ldb_kv->read_lock_count--;
 		return LDB_SUCCESS;
 	}
-	ltdb->read_lock_count--;
+	ldb_kv->read_lock_count--;
 	return LDB_SUCCESS;
 }
 
-static int lmdb_transaction_start(struct ltdb_private *ltdb)
+static int lmdb_transaction_start(struct ldb_kv_private *ldb_kv)
 {
-	struct lmdb_private *lmdb = ltdb->lmdb_private;
+	struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 	struct lmdb_trans *ltx;
 	struct lmdb_trans *ltx_head;
 	MDB_txn *tx_parent;
 	pid_t pid = getpid();
 
 	/* Do not take out the transaction lock on a read-only DB */
-	if (ltdb->read_only) {
+	if (ldb_kv->read_only) {
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
@@ -505,10 +505,10 @@ static int lmdb_transaction_start(struct ltdb_private *ltdb)
 	return ldb_mdb_err_map(lmdb->error);
 }
 
-static int lmdb_transaction_cancel(struct ltdb_private *ltdb)
+static int lmdb_transaction_cancel(struct ldb_kv_private *ldb_kv)
 {
 	struct lmdb_trans *ltx;
-	struct lmdb_private *lmdb = ltdb->lmdb_private;
+	struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 
 	ltx = lmdb_private_trans_head(lmdb);
 	if (ltx == NULL) {
@@ -520,16 +520,16 @@ static int lmdb_transaction_cancel(struct ltdb_private *ltdb)
 	return LDB_SUCCESS;
 }
 
-static int lmdb_transaction_prepare_commit(struct ltdb_private *ltdb)
+static int lmdb_transaction_prepare_commit(struct ldb_kv_private *ldb_kv)
 {
 	/* No need to prepare a commit */
 	return LDB_SUCCESS;
 }
 
-static int lmdb_transaction_commit(struct ltdb_private *ltdb)
+static int lmdb_transaction_commit(struct ldb_kv_private *ldb_kv)
 {
 	struct lmdb_trans *ltx;
-	struct lmdb_private *lmdb = ltdb->lmdb_private;
+	struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 
 	ltx = lmdb_private_trans_head(lmdb);
 	if (ltx == NULL) {
@@ -542,22 +542,22 @@ static int lmdb_transaction_commit(struct ltdb_private *ltdb)
 	return lmdb->error;
 }
 
-static int lmdb_error(struct ltdb_private *ltdb)
+static int lmdb_error(struct ldb_kv_private *ldb_kv)
 {
-	return ldb_mdb_err_map(ltdb->lmdb_private->error);
+	return ldb_mdb_err_map(ldb_kv->lmdb_private->error);
 }
 
-static const char *lmdb_errorstr(struct ltdb_private *ltdb)
+static const char *lmdb_errorstr(struct ldb_kv_private *ldb_kv)
 {
-	return mdb_strerror(ltdb->lmdb_private->error);
+	return mdb_strerror(ldb_kv->lmdb_private->error);
 }
 
-static const char * lmdb_name(struct ltdb_private *ltdb)
+static const char * lmdb_name(struct ldb_kv_private *ldb_kv)
 {
 	return "lmdb";
 }
 
-static bool lmdb_changed(struct ltdb_private *ltdb)
+static bool lmdb_changed(struct ldb_kv_private *ldb_kv)
 {
 	/*
 	 * lmdb does no provide a quick way to determine if the database
@@ -835,7 +835,7 @@ int lmdb_connect(struct ldb_context *ldb,
 {
 	const char *path = NULL;
 	struct lmdb_private *lmdb = NULL;
-	struct ltdb_private *ltdb = NULL;
+	struct ldb_kv_private *ldb_kv = NULL;
 	int ret;
 
 	/*
@@ -850,29 +850,29 @@ int lmdb_connect(struct ldb_context *ldb,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ltdb = talloc_zero(ldb, struct ltdb_private);
-	if (!ltdb) {
+	ldb_kv = talloc_zero(ldb, struct ldb_kv_private);
+	if (!ldb_kv) {
 		ldb_oom(ldb);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	lmdb = talloc_zero(ltdb, struct lmdb_private);
+	lmdb = talloc_zero(ldb_kv, struct lmdb_private);
 	if (lmdb == NULL) {
-		TALLOC_FREE(ltdb);
+		TALLOC_FREE(ldb_kv);
 		return ldb_oom(ldb);
 	}
 	lmdb->ldb = ldb;
-	ltdb->kv_ops = &lmdb_key_value_ops;
+	ldb_kv->kv_ops = &lmdb_key_value_ops;
 
 	ret = lmdb_pvt_open(lmdb, ldb, path, flags);
 	if (ret != LDB_SUCCESS) {
-		TALLOC_FREE(ltdb);
+		TALLOC_FREE(ldb_kv);
 		return ret;
 	}
 
-	ltdb->lmdb_private = lmdb;
+	ldb_kv->lmdb_private = lmdb;
 	if (flags & LDB_FLG_RDONLY) {
-		ltdb->read_only = true;
+		ldb_kv->read_only = true;
 	}
 
 	/*
@@ -881,8 +881,8 @@ int lmdb_connect(struct ldb_context *ldb,
 	 * The override option is max_key_len_for_self_test, and is
 	 * used for testing only.
 	 */
-	ltdb->max_key_length = LDB_MDB_MAX_KEY_LENGTH;
+	ldb_kv->max_key_length = LDB_MDB_MAX_KEY_LENGTH;
 
 	return ldb_kv_init_store(
-	    ltdb, "ldb_mdb backend", ldb, options, _module);
+	    ldb_kv, "ldb_mdb backend", ldb, options, _module);
 }
diff --git a/lib/ldb/ldb_tdb/ldb_cache.c b/lib/ldb/ldb_tdb/ldb_cache.c
index e9da45d..1a07f99 100644
--- a/lib/ldb/ldb_tdb/ldb_cache.c
+++ b/lib/ldb/ldb_tdb/ldb_cache.c
@@ -236,7 +236,7 @@ failed:
   register any index records we find for the DB
 */
 static int ldb_kv_index_load(struct ldb_module *module,
-			     struct ltdb_private *ltdb)
+			     struct ldb_kv_private *ldb_kv)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	struct ldb_dn *indexlist_dn;
@@ -247,32 +247,32 @@ static int ldb_kv_index_load(struct ldb_module *module,
 		 * we skip loading the @INDEXLIST record when a module is
 		 * supplying its own attribute handling
 		 */
-		ltdb->cache->attribute_indexes = true;
-		ltdb->cache->one_level_indexes = ldb->schema.one_level_indexes;
-		ltdb->cache->GUID_index_attribute
+		ldb_kv->cache->attribute_indexes = true;
+		ldb_kv->cache->one_level_indexes = ldb->schema.one_level_indexes;
+		ldb_kv->cache->GUID_index_attribute
 			= ldb->schema.GUID_index_attribute;
-		ltdb->cache->GUID_index_dn_component
+		ldb_kv->cache->GUID_index_dn_component
 			= ldb->schema.GUID_index_dn_component;
 		return 0;
 	}
 
-	talloc_free(ltdb->cache->indexlist);
+	talloc_free(ldb_kv->cache->indexlist);
 
-	ltdb->cache->indexlist = ldb_msg_new(ltdb->cache);
-	if (ltdb->cache->indexlist == NULL) {
+	ldb_kv->cache->indexlist = ldb_msg_new(ldb_kv->cache);
+	if (ldb_kv->cache->indexlist == NULL) {
 		return -1;
 	}
-	ltdb->cache->one_level_indexes = false;
-	ltdb->cache->attribute_indexes = false;
+	ldb_kv->cache->one_level_indexes = false;
+	ldb_kv->cache->attribute_indexes = false;
 
-	indexlist_dn = ldb_dn_new(ltdb, ldb, LTDB_INDEXLIST);
+	indexlist_dn = ldb_dn_new(ldb_kv, ldb, LTDB_INDEXLIST);
 	if (indexlist_dn == NULL) {
 		return -1;
 	}
 
 	r = ldb_kv_search_dn1(module,
 			      indexlist_dn,
-			      ltdb->cache->indexlist,
+			      ldb_kv->cache->indexlist,
 			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
 				  LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
 				  LDB_UNPACK_DATA_FLAG_NO_DN);
@@ -282,21 +282,21 @@ static int ldb_kv_index_load(struct ldb_module *module,
 		return -1;
 	}
 
-	if (ldb_msg_find_element(ltdb->cache->indexlist, LTDB_IDXONE) != NULL) {
-		ltdb->cache->one_level_indexes = true;
+	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXONE) != NULL) {
+		ldb_kv->cache->one_level_indexes = true;
 	}
-	if (ldb_msg_find_element(ltdb->cache->indexlist, LTDB_IDXATTR) != NULL) {
-		ltdb->cache->attribute_indexes = true;
+	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR) != NULL) {
+		ldb_kv->cache->attribute_indexes = true;
 	}
-	ltdb->cache->GUID_index_attribute
-		= ldb_msg_find_attr_as_string(ltdb->cache->indexlist,
+	ldb_kv->cache->GUID_index_attribute
+		= ldb_msg_find_attr_as_string(ldb_kv->cache->indexlist,
 					      LTDB_IDXGUID, NULL);
-	ltdb->cache->GUID_index_dn_component
-		= ldb_msg_find_attr_as_string(ltdb->cache->indexlist,
+	ldb_kv->cache->GUID_index_dn_component
+		= ldb_msg_find_attr_as_string(ldb_kv->cache->indexlist,
 					      LTDB_IDX_DN_GUID, NULL);
 
 	lmdb_subdb_version
-		= ldb_msg_find_attr_as_int(ltdb->cache->indexlist,
+		= ldb_msg_find_attr_as_int(ldb_kv->cache->indexlist,
 					   LTDB_IDX_LMDB_SUBDB, 0);
 
 	if (lmdb_subdb_version != 0) {
@@ -319,7 +319,7 @@ static int ldb_kv_baseinfo_init(struct ldb_module *module)
 {
 	struct ldb_context *ldb;
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_message *msg;
 	struct ldb_message_element el;
 	struct ldb_val val;
@@ -331,9 +331,9 @@ static int ldb_kv_baseinfo_init(struct ldb_module *module)
 
 	ldb = ldb_module_get_ctx(module);
 
-	ltdb->sequence_number = atof(initial_sequence_number);
+	ldb_kv->sequence_number = atof(initial_sequence_number);
 
-	msg = ldb_msg_new(ltdb);
+	msg = ldb_msg_new(ldb_kv);
 	if (msg == NULL) {
 		goto failed;
 	}
@@ -375,11 +375,11 @@ failed:
 static void ldb_kv_cache_free(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 
-	ltdb->sequence_number = 0;
-	talloc_free(ltdb->cache);
-	ltdb->cache = NULL;
+	ldb_kv->sequence_number = 0;
+	talloc_free(ldb_kv->cache);
+	ldb_kv->cache = NULL;
 }
 
 /*
@@ -399,7 +399,7 @@ int ldb_kv_cache_load(struct ldb_module *module)
 {
 	struct ldb_context *ldb;
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
 	uint64_t seq;
 	struct ldb_message *baseinfo = NULL, *options = NULL;
@@ -410,22 +410,22 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	ldb = ldb_module_get_ctx(module);
 
 	/* a very fast check to avoid extra database reads */
-	if (ltdb->cache != NULL && !ltdb->kv_ops->has_changed(ltdb)) {
+	if (ldb_kv->cache != NULL && !ldb_kv->kv_ops->has_changed(ldb_kv)) {
 		return 0;
 	}
 
-	if (ltdb->cache == NULL) {
-		ltdb->cache = talloc_zero(ltdb, struct ltdb_cache);
-		if (ltdb->cache == NULL) goto failed;
+	if (ldb_kv->cache == NULL) {
+		ldb_kv->cache = talloc_zero(ldb_kv, struct ltdb_cache);
+		if (ldb_kv->cache == NULL) goto failed;
 	}
 
-	baseinfo = ldb_msg_new(ltdb->cache);
+	baseinfo = ldb_msg_new(ldb_kv->cache);
 	if (baseinfo == NULL) goto failed;
 
 	baseinfo_dn = ldb_dn_new(baseinfo, ldb, LTDB_BASEINFO);
 	if (baseinfo_dn == NULL) goto failed;
 
-	r = ltdb->kv_ops->lock_read(module);
+	r = ldb_kv->kv_ops->lock_read(module);
 	if (r != LDB_SUCCESS) {
 		goto failed;
 	}
@@ -438,12 +438,12 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	if (r == LDB_ERR_NO_SUCH_OBJECT) {
 
 		/* Give up the read lock, try again with a write lock */
-		r = ltdb->kv_ops->unlock_read(module);
+		r = ldb_kv->kv_ops->unlock_read(module);
 		if (r != LDB_SUCCESS) {
 			goto failed;
 		}
 
-		if (ltdb->kv_ops->begin_write(ltdb) != 0) {
+		if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
 			goto failed;
 		}
 
@@ -461,19 +461,19 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	}
 
 	/* Ignore the result, and update the sequence number */
-	ltdb->kv_ops->has_changed(ltdb);
+	ldb_kv->kv_ops->has_changed(ldb_kv);
 
 	/* if the current internal sequence number is the same as the one
 	   in the database then assume the rest of the cache is OK */
 	seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
-	if (seq == ltdb->sequence_number) {
+	if (seq == ldb_kv->sequence_number) {
 		goto done;
 	}
-	ltdb->sequence_number = seq;
+	ldb_kv->sequence_number = seq;
 
 	/* Read an interpret database options */
 
-	options = ldb_msg_new(ltdb->cache);
+	options = ldb_msg_new(ldb_kv->cache);
 	if (options == NULL) goto failed_and_unlock;
 
 	options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
@@ -487,15 +487,15 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	
 	/* set flags if they do exist */
 	if (r == LDB_SUCCESS) {
-		ltdb->check_base = ldb_msg_find_attr_as_bool(options,
+		ldb_kv->check_base = ldb_msg_find_attr_as_bool(options,
 							     LTDB_CHECK_BASE,
 							     false);
-		ltdb->disallow_dn_filter = ldb_msg_find_attr_as_bool(options,
+		ldb_kv->disallow_dn_filter = ldb_msg_find_attr_as_bool(options,
 								     LTDB_DISALLOW_DN_FILTER,
 								     false);
 	} else {
-		ltdb->check_base = false;
-		ltdb->disallow_dn_filter = false;
+		ldb_kv->check_base = false;
+		ldb_kv->disallow_dn_filter = false;
 	}
 
 	/*
@@ -508,7 +508,7 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	 */
 	ldb_kv_attributes_unload(module);
 
-	if (ldb_kv_index_load(module, ltdb) == -1) {
+	if (ldb_kv_index_load(module, ldb_kv) == -1) {
 		goto failed_and_unlock;
 	}
 
@@ -521,24 +521,24 @@ int ldb_kv_cache_load(struct ldb_module *module)
 		goto failed_and_unlock;
 	}
 
-	ltdb->GUID_index_syntax = NULL;
-	if (ltdb->cache->GUID_index_attribute != NULL) {
+	ldb_kv->GUID_index_syntax = NULL;
+	if (ldb_kv->cache->GUID_index_attribute != NULL) {
 		/*
 		 * Now the attributes are loaded, set the guid_index_syntax.
 		 * This can't fail, it will return a default at worst
 		 */
 		a = ldb_schema_attribute_by_name(ldb,
-						 ltdb->cache->GUID_index_attribute);
-		ltdb->GUID_index_syntax = a->syntax;
+						 ldb_kv->cache->GUID_index_attribute);
+		ldb_kv->GUID_index_syntax = a->syntax;
 	}
 
 done:
 	if (have_write_txn) {
-		if (ltdb->kv_ops->finish_write(ltdb) != 0) {
+		if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
 			goto failed;
 		}
 	} else {
-		ltdb->kv_ops->unlock_read(module);
+		ldb_kv->kv_ops->unlock_read(module);
 	}
 
 	talloc_free(options);
@@ -547,9 +547,9 @@ done:
 
 failed_and_unlock:
 	if (have_write_txn) {
-		ltdb->kv_ops->abort_write(ltdb);
+		ldb_kv->kv_ops->abort_write(ldb_kv);
 	} else {
-		ltdb->kv_ops->unlock_read(module);
+		ldb_kv->kv_ops->unlock_read(module);
 	}
 
 failed:
@@ -566,7 +566,7 @@ int ldb_kv_increase_sequence_number(struct ldb_module *module)
 {
 	struct ldb_context *ldb;
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_message *msg;
 	struct ldb_message_element el[2];
 	struct ldb_val val;
@@ -577,13 +577,13 @@ int ldb_kv_increase_sequence_number(struct ldb_module *module)
 
 	ldb = ldb_module_get_ctx(module);
 
-	msg = ldb_msg_new(ltdb);
+	msg = ldb_msg_new(ldb_kv);
 	if (msg == NULL) {
 		errno = ENOMEM;
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	s = talloc_asprintf(msg, "%llu", ltdb->sequence_number+1);
+	s = talloc_asprintf(msg, "%llu", ldb_kv->sequence_number+1);
 	if (!s) {
 		talloc_free(msg);
 		errno = ENOMEM;
@@ -634,12 +634,12 @@ int ldb_kv_increase_sequence_number(struct ldb_module *module)
 	talloc_free(msg);
 
 	if (ret == LDB_SUCCESS) {
-		ltdb->sequence_number += 1;
+		ldb_kv->sequence_number += 1;
 	}
 
 	/* updating the tdb_seqnum here avoids us reloading the cache
 	   records due to our own modification */
-	ltdb->kv_ops->has_changed(ltdb);
+	ldb_kv->kv_ops->has_changed(ldb_kv);
 
 	return ret;
 }
diff --git a/lib/ldb/ldb_tdb/ldb_index.c b/lib/ldb/ldb_tdb/ldb_index.c
index a04431e..e0a5b62 100644
--- a/lib/ldb/ldb_tdb/ldb_index.c
+++ b/lib/ldb/ldb_tdb/ldb_index.c
@@ -174,12 +174,12 @@ static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
 				      const struct ldb_message *msg,
 				      int add);
 static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
-				   struct ltdb_private *ltdb,
+				   struct ldb_kv_private *ldb_kv,
 				   struct ldb_dn *base_dn,
 				   struct dn_list *dn_list,
 				   enum key_truncation *truncation);
 
-static void ldb_kv_dn_list_sort(struct ltdb_private *ltdb,
+static void ldb_kv_dn_list_sort(struct ldb_kv_private *ldb_kv,
 				struct dn_list *list);
 
 /* we put a @IDXVERSION attribute on index entries. This
@@ -189,20 +189,20 @@ static void ldb_kv_dn_list_sort(struct ltdb_private *ltdb,
 
 #define LTDB_GUID_INDEXING_VERSION 3
 
-static unsigned ldb_kv_max_key_length(struct ltdb_private *ltdb)
+static unsigned ldb_kv_max_key_length(struct ldb_kv_private *ldb_kv)
 {
-	if (ltdb->max_key_length == 0){
+	if (ldb_kv->max_key_length == 0){
 		return UINT_MAX;
 	}
-	return ltdb->max_key_length;
+	return ldb_kv->max_key_length;
 }
 
 /* enable the idxptr mode when transactions start */
 int ldb_kv_index_transaction_start(struct ldb_module *module)
 {
-	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
-	ltdb->idxptr = talloc_zero(ltdb, struct ltdb_idxptr);
-	if (ltdb->idxptr == NULL) {
+	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
+	ldb_kv->idxptr = talloc_zero(ldb_kv, struct ltdb_idxptr);
+	if (ldb_kv->idxptr == NULL) {
 		return ldb_oom(ldb_module_get_ctx(module));
 	}
 
@@ -248,14 +248,14 @@ static int ldb_val_equal_exact_ordered(const struct ldb_val v1,
 
   This is therefore safe when the value is a GUID in the future
  */
-static int ldb_kv_dn_list_find_val(struct ltdb_private *ltdb,
+static int ldb_kv_dn_list_find_val(struct ldb_kv_private *ldb_kv,
 				   const struct dn_list *list,
 				   const struct ldb_val *v)
 {
 	unsigned int i;
 	struct ldb_val *exact = NULL, *next = NULL;
 
-	if (ltdb->cache->GUID_index_attribute == NULL) {
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
 		for (i=0; i<list->count; i++) {
 			if (ldb_val_equal_exact(&list->dn[i], v) == 1) {
 				return i;
@@ -283,25 +283,25 @@ static int ldb_kv_dn_list_find_val(struct ltdb_private *ltdb,
   find a entry in a dn_list. Uses a case sensitive comparison with the dn
   returns -1 if not found
  */
-static int ldb_kv_dn_list_find_msg(struct ltdb_private *ltdb,
+static int ldb_kv_dn_list_find_msg(struct ldb_kv_private *ldb_kv,
 				   struct dn_list *list,
 				   const struct ldb_message *msg)
 {
 	struct ldb_val v;
 	const struct ldb_val *key_val;
-	if (ltdb->cache->GUID_index_attribute == NULL) {
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
 		const char *dn_str = ldb_dn_get_linearized(msg->dn);
 		v.data = discard_const_p(unsigned char, dn_str);
 		v.length = strlen(dn_str);
 	} else {
 		key_val = ldb_msg_find_ldb_val(msg,
-					       ltdb->cache->GUID_index_attribute);
+					       ldb_kv->cache->GUID_index_attribute);
 		if (key_val == NULL) {
 			return -1;
 		}
 		v = *key_val;
 	}
-	return ldb_kv_dn_list_find_val(ltdb, list, &v);
+	return ldb_kv_dn_list_find_val(ldb_kv, list, &v);
 }
 
 /*
@@ -344,7 +344,7 @@ static struct dn_list *ldb_kv_index_idxptr(struct ldb_module *module,
   struct dn_list
  */
 static int ldb_kv_dn_list_load(struct ldb_module *module,
-			       struct ltdb_private *ltdb,
+			       struct ldb_kv_private *ldb_kv,
 			       struct ldb_dn *dn,
 			       struct dn_list *list)
 {
@@ -359,15 +359,15 @@ static int ldb_kv_dn_list_load(struct ldb_module *module,
 	list->count = 0;
 
 	/* see if we have any in-memory index entries */
-	if (ltdb->idxptr == NULL ||
-	    ltdb->idxptr->itdb == NULL) {
+	if (ldb_kv->idxptr == NULL ||
+	    ldb_kv->idxptr->itdb == NULL) {
 		goto normal_index;
 	}
 
 	key.dptr = discard_const_p(unsigned char, ldb_dn_get_linearized(dn));
 	key.dsize = strlen((char *)key.dptr);
 
-	rec = tdb_fetch(ltdb->idxptr->itdb, key);
+	rec = tdb_fetch(ldb_kv->idxptr->itdb, key);
 	if (rec.dptr == NULL) {
 		goto normal_index;
 	}
@@ -413,7 +413,7 @@ normal_index:
 	 * asked for the memory to be allocated on msg, not on each
 	 * value with LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC above
 	 */
-	if (ltdb->cache->GUID_index_attribute == NULL) {
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
 		/* check indexing version number */
 		if (version != LTDB_INDEXING_VERSION) {
 			ldb_debug_set(ldb_module_get_ctx(module),
@@ -479,7 +479,7 @@ normal_index:
 }
 
 int ldb_kv_key_dn_from_idx(struct ldb_module *module,
-			   struct ltdb_private *ltdb,
+			   struct ldb_kv_private *ldb_kv,
 			   TALLOC_CTX *mem_ctx,
 			   struct ldb_dn *dn,
 			   TDB_DATA *tdb_key)
@@ -494,7 +494,7 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ldb_kv_index_dn_base_dn(module, ltdb, dn, list, &truncation);
+	ret = ldb_kv_index_dn_base_dn(module, ldb_kv, dn, list, &truncation);
 	if (ret != LDB_SUCCESS) {
 		TALLOC_FREE(list);
 		return ret;
@@ -512,7 +512,7 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 				       ": Failed to read DN index "
 				       "against %s for %s: too many "
 				       "values (%u > 1)",
-				       ltdb->cache->GUID_index_attribute,
+				       ldb_kv->cache->GUID_index_attribute,
 				       dn_str, list->count);
 		TALLOC_FREE(list);
 		return LDB_ERR_CONSTRAINT_VIOLATION;
@@ -539,14 +539,14 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 			}
 
 			ret = ldb_kv_idx_to_key(
-			    module, ltdb, ldb, &list->dn[i], &key);
+			    module, ldb_kv, ldb, &list->dn[i], &key);
 			if (ret != LDB_SUCCESS) {
 				TALLOC_FREE(list);
 				TALLOC_FREE(rec);
 				return ret;
 			}
 
-			ret = ldb_kv_search_key(module, ltdb, key, rec, flags);
+			ret = ldb_kv_search_key(module, ldb_kv, key, rec, flags);
 			if (key.dptr != guid_key) {
 				TALLOC_FREE(key.dptr);
 			}
@@ -590,7 +590,7 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 	}
 
 	/* The tdb_key memory is allocated by the caller */
-	ret = ldb_kv_guid_to_key(module, ltdb, &list->dn[index], tdb_key);
+	ret = ldb_kv_guid_to_key(module, ldb_kv, &list->dn[index], tdb_key);
 	TALLOC_FREE(list);
 
 	if (ret != LDB_SUCCESS) {
@@ -606,7 +606,7 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
   save a dn_list into a full @IDX style record
  */
 static int ldb_kv_dn_list_store_full(struct ldb_module *module,
-				     struct ltdb_private *ltdb,
+				     struct ldb_kv_private *ldb_kv,
 				     struct ldb_dn *dn,
 				     struct dn_list *list)
 {
@@ -629,7 +629,7 @@ static int ldb_kv_dn_list_store_full(struct ldb_module *module,
 		return ret;
 	}
 
-	if (ltdb->cache->GUID_index_attribute == NULL) {
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
 		ret = ldb_msg_add_fmt(msg, LTDB_IDXVERSION, "%u",
 				      LTDB_INDEXING_VERSION);
 		if (ret != LDB_SUCCESS) {
@@ -654,7 +654,7 @@ static int ldb_kv_dn_list_store_full(struct ldb_module *module,
 			return ldb_module_oom(module);
 		}
 
-		if (ltdb->cache->GUID_index_attribute == NULL) {
+		if (ldb_kv->cache->GUID_index_attribute == NULL) {
 			el->values = list->dn;
 			el->num_values = list->count;
 		} else {
@@ -704,18 +704,18 @@ static int ldb_kv_dn_list_store(struct ldb_module *module,
 				struct ldb_dn *dn,
 				struct dn_list *list)
 {
-	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
 	TDB_DATA rec, key;
 	int ret;
 	struct dn_list *list2;
 
-	if (ltdb->idxptr == NULL) {
-		return ldb_kv_dn_list_store_full(module, ltdb, dn, list);
+	if (ldb_kv->idxptr == NULL) {
+		return ldb_kv_dn_list_store_full(module, ldb_kv, dn, list);
 	}
 
-	if (ltdb->idxptr->itdb == NULL) {
-		ltdb->idxptr->itdb = tdb_open(NULL, 1000, TDB_INTERNAL, O_RDWR, 0);
-		if (ltdb->idxptr->itdb == NULL) {
+	if (ldb_kv->idxptr->itdb == NULL) {
+		ldb_kv->idxptr->itdb = tdb_open(NULL, 1000, TDB_INTERNAL, O_RDWR, 0);
+		if (ldb_kv->idxptr->itdb == NULL) {
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
 	}
@@ -726,7 +726,7 @@ static int ldb_kv_dn_list_store(struct ldb_module *module,
 	}
 	key.dsize = strlen((char *)key.dptr);
 
-	rec = tdb_fetch(ltdb->idxptr->itdb, key);
+	rec = tdb_fetch(ldb_kv->idxptr->itdb, key);
 	if (rec.dptr != NULL) {
 		list2 = ldb_kv_index_idxptr(module, rec, false);
 		if (list2 == NULL) {
@@ -739,7 +739,7 @@ static int ldb_kv_dn_list_store(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	list2 = talloc(ltdb->idxptr, struct dn_list);
+	list2 = talloc(ldb_kv->idxptr, struct dn_list);
 	if (list2 == NULL) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
@@ -754,9 +754,9 @@ static int ldb_kv_dn_list_store(struct ldb_module *module,
 	 * This is not a store into the main DB, but into an in-memory
 	 * TDB, so we don't need a guard on ltdb->read_only
 	 */
-	ret = tdb_store(ltdb->idxptr->itdb, key, rec, TDB_INSERT);
+	ret = tdb_store(ldb_kv->idxptr->itdb, key, rec, TDB_INSERT);
 	if (ret != 0) {
-		return ltdb_err_map(tdb_error(ltdb->idxptr->itdb));
+		return ltdb_err_map(tdb_error(ldb_kv->idxptr->itdb));
 	}
 	return LDB_SUCCESS;
 }
@@ -770,7 +770,7 @@ static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
 				       void *state)
 {
 	struct ldb_module *module = state;
-	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
 	struct ldb_dn *dn;
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	struct ldb_val v;
@@ -778,7 +778,7 @@ static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
 
 	list = ldb_kv_index_idxptr(module, data, true);
 	if (list == NULL) {
-		ltdb->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
+		ldb_kv->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
 		return -1;
 	}
 
@@ -788,13 +788,13 @@ static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
 	dn = ldb_dn_from_ldb_val(module, ldb, &v);
 	if (dn == NULL) {
 		ldb_asprintf_errstring(ldb, "Failed to parse index key %*.*s as an LDB DN", (int)v.length, (int)v.length, (const char *)v.data);
-		ltdb->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
+		ldb_kv->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
 		return -1;
 	}
 
-	ltdb->idxptr->error = ldb_kv_dn_list_store_full(module, ltdb, dn, list);
+	ldb_kv->idxptr->error = ldb_kv_dn_list_store_full(module, ldb_kv, dn, list);
 	talloc_free(dn);
-	if (ltdb->idxptr->error != 0) {
+	if (ldb_kv->idxptr->error != 0) {
 		return -1;
 	}
 	return 0;
@@ -803,20 +803,20 @@ static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
 /* cleanup the idxptr mode when transaction commits */
 int ldb_kv_index_transaction_commit(struct ldb_module *module)
 {
-	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
 	int ret;
 
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 
 	ldb_reset_err_string(ldb);
 
-	if (ltdb->idxptr->itdb) {
+	if (ldb_kv->idxptr->itdb) {
 		tdb_traverse(
-		    ltdb->idxptr->itdb, ldb_kv_index_traverse_store, module);
-		tdb_close(ltdb->idxptr->itdb);
+		    ldb_kv->idxptr->itdb, ldb_kv_index_traverse_store, module);
+		tdb_close(ldb_kv->idxptr->itdb);
 	}
 
-	ret = ltdb->idxptr->error;
+	ret = ldb_kv->idxptr->error;
 	if (ret != LDB_SUCCESS) {
 		if (!ldb_errstring(ldb)) {
 			ldb_set_errstring(ldb, ldb_strerror(ret));
@@ -824,20 +824,20 @@ int ldb_kv_index_transaction_commit(struct ldb_module *module)
 		ldb_asprintf_errstring(ldb, "Failed to store index records in transaction commit: %s", ldb_errstring(ldb));
 	}
 
-	talloc_free(ltdb->idxptr);
-	ltdb->idxptr = NULL;
+	talloc_free(ldb_kv->idxptr);
+	ldb_kv->idxptr = NULL;
 	return ret;
 }
 
 /* cleanup the idxptr mode when transaction cancels */
 int ldb_kv_index_transaction_cancel(struct ldb_module *module)
 {
-	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
-	if (ltdb->idxptr && ltdb->idxptr->itdb) {
-		tdb_close(ltdb->idxptr->itdb);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
+	if (ldb_kv->idxptr && ldb_kv->idxptr->itdb) {
+		tdb_close(ldb_kv->idxptr->itdb);
 	}
-	talloc_free(ltdb->idxptr);
-	ltdb->idxptr = NULL;
+	talloc_free(ldb_kv->idxptr);
+	ldb_kv->idxptr = NULL;
 	return LDB_SUCCESS;
 }
 
@@ -847,7 +847,7 @@ int ldb_kv_index_transaction_cancel(struct ldb_module *module)
   the caller is responsible for freeing
 */
 static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
-				       struct ltdb_private *ltdb,
+				       struct ldb_kv_private *ldb_kv,
 				       const char *attr,
 				       const struct ldb_val *value,
 				       const struct ldb_schema_attribute **ap,
@@ -861,7 +861,7 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 	int r;
 	bool should_b64_encode;
 
-	unsigned int max_key_length = ldb_kv_max_key_length(ltdb);
+	unsigned int max_key_length = ldb_kv_max_key_length(ldb_kv);
 	size_t key_len = 0;
 	size_t attr_len = 0;
 	const size_t indx_len = sizeof(LTDB_INDEX) - 1;
@@ -937,7 +937,7 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 	 * casefold and lineraized, that is good enough.  That already
 	 * avoids embedded NUL etc.
 	 */
-	if (ltdb->cache->GUID_index_attribute != NULL) {
+	if (ldb_kv->cache->GUID_index_attribute != NULL) {
 		if (strcmp(attr, LTDB_IDXDN) == 0) {
 			should_b64_encode = false;
 		} else if (strcmp(attr, LTDB_IDXONE) == 0) {
@@ -1033,16 +1033,16 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
   see if a attribute value is in the list of indexed attributes
 */
 static bool ldb_kv_is_indexed(struct ldb_module *module,
-			      struct ltdb_private *ltdb,
+			      struct ldb_kv_private *ldb_kv,
 			      const char *attr)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	unsigned int i;
 	struct ldb_message_element *el;
 
-	if ((ltdb->cache->GUID_index_attribute != NULL) &&
+	if ((ldb_kv->cache->GUID_index_attribute != NULL) &&
 	    (ldb_attr_cmp(attr,
-			  ltdb->cache->GUID_index_attribute) == 0)) {
+			  ldb_kv->cache->GUID_index_attribute) == 0)) {
 		/* Implicity covered, this is the index key */
 		return false;
 	}
@@ -1061,11 +1061,11 @@ static bool ldb_kv_is_indexed(struct ldb_module *module,
 		}
 	}
 
-	if (!ltdb->cache->attribute_indexes) {
+	if (!ldb_kv->cache->attribute_indexes) {
 		return false;
 	}
 
-	el = ldb_msg_find_element(ltdb->cache->indexlist, LTDB_IDXATTR);
+	el = ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR);
 	if (el == NULL) {
 		return false;
 	}
@@ -1096,7 +1096,7 @@ static bool ldb_kv_is_indexed(struct ldb_module *module,
   equality search only)
  */
 static int ldb_kv_index_dn_simple(struct ldb_module *module,
-				  struct ltdb_private *ltdb,
+				  struct ldb_kv_private *ldb_kv,
 				  const struct ldb_parse_tree *tree,
 				  struct dn_list *list)
 {
@@ -1112,14 +1112,14 @@ static int ldb_kv_index_dn_simple(struct ldb_module *module,
 
 	/* if the attribute isn't in the list of indexed attributes then
 	   this node needs a full search */
-	if (!ldb_kv_is_indexed(module, ltdb, tree->u.equality.attr)) {
+	if (!ldb_kv_is_indexed(module, ldb_kv, tree->u.equality.attr)) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
 	/* the attribute is indexed. Pull the list of DNs that match the
 	   search criterion */
 	dn = ldb_kv_index_key(ldb,
-			      ltdb,
+			      ldb_kv,
 			      tree->u.equality.attr,
 			      &tree->u.equality.value,
 			      NULL,
@@ -1131,25 +1131,25 @@ static int ldb_kv_index_dn_simple(struct ldb_module *module,
 	 */
 	if (!dn) return LDB_ERR_OPERATIONS_ERROR;
 
-	ret = ldb_kv_dn_list_load(module, ltdb, dn, list);
+	ret = ldb_kv_dn_list_load(module, ldb_kv, dn, list);
 	talloc_free(dn);
 	return ret;
 }
 
 
 static bool list_union(struct ldb_context *ldb,
-		       struct ltdb_private *ltdb,
+		       struct ldb_kv_private *ldb_kv,
 		       struct dn_list *list, struct dn_list *list2);
 
 /*
   return a list of dn's that might match a leaf indexed search
  */
 static int ldb_kv_index_dn_leaf(struct ldb_module *module,
-				struct ltdb_private *ltdb,
+				struct ldb_kv_private *ldb_kv,
 				const struct ldb_parse_tree *tree,
 				struct dn_list *list)
 {
-	if (ltdb->disallow_dn_filter &&
+	if (ldb_kv->disallow_dn_filter &&
 	    (ldb_attr_cmp(tree->u.equality.attr, "dn") == 0)) {
 		/* in AD mode we do not support "(dn=...)" search filters */
 		list->dn = NULL;
@@ -1183,16 +1183,16 @@ static int ldb_kv_index_dn_leaf(struct ldb_module *module,
 		 * to list for the memory to remain valid.
 		 */
 		return ldb_kv_index_dn_base_dn(
-		    module, ltdb, dn, list, &truncation);
+		    module, ldb_kv, dn, list, &truncation);
 		/*
 		 * We ignore truncation here and allow multi-valued matches
 		 * as ltdb_search_indexed will filter out the wrong one in
 		 * ltdb_index_filter() which calls ldb_match_message().
 		 */
 
-	} else if ((ltdb->cache->GUID_index_attribute != NULL) &&
+	} else if ((ldb_kv->cache->GUID_index_attribute != NULL) &&
 		   (ldb_attr_cmp(tree->u.equality.attr,
-				 ltdb->cache->GUID_index_attribute) == 0)) {
+				 ldb_kv->cache->GUID_index_attribute) == 0)) {
 		int ret;
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
 		list->dn = talloc_array(list, struct ldb_val, 1);
@@ -1205,7 +1205,7 @@ static int ldb_kv_index_dn_leaf(struct ldb_module *module,
 		 * ensure we get the index in binary, rather
 		 * than a string
 		 */
-		ret = ltdb->GUID_index_syntax->canonicalise_fn(ldb,
+		ret = ldb_kv->GUID_index_syntax->canonicalise_fn(ldb,
 							       list->dn,
 							       &tree->u.equality.value,
 							       &list->dn[0]);
@@ -1216,7 +1216,7 @@ static int ldb_kv_index_dn_leaf(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	return ldb_kv_index_dn_simple(module, ltdb, tree, list);
+	return ldb_kv_index_dn_simple(module, ldb_kv, tree, list);
 }
 
 
@@ -1225,7 +1225,7 @@ static int ldb_kv_index_dn_leaf(struct ldb_module *module,
   list = list & list2
 */
 static bool list_intersect(struct ldb_context *ldb,
-			   struct ltdb_private *ltdb,
+			   struct ldb_kv_private *ldb_kv,
 			   struct dn_list *list, const struct dn_list *list2)
 {
 	const struct dn_list *short_list, *long_list;
@@ -1285,7 +1285,7 @@ static bool list_intersect(struct ldb_context *ldb,
 	for (i=0;i<short_list->count;i++) {
 		/* For the GUID index case, this is a binary search */
 		if (ldb_kv_dn_list_find_val(
-			ltdb, long_list, &short_list->dn[i]) != -1) {
+			ldb_kv, long_list, &short_list->dn[i]) != -1) {
 			list3->dn[list3->count] = short_list->dn[i];
 			list3->count++;
 		}
@@ -1305,7 +1305,7 @@ static bool list_intersect(struct ldb_context *ldb,
   list = list | list2
 */
 static bool list_union(struct ldb_context *ldb,
-		       struct ltdb_private *ltdb,
+		       struct ldb_kv_private *ldb_kv,
 		       struct dn_list *list, struct dn_list *list2)
 {
 	struct ldb_val *dn3;
@@ -1335,8 +1335,8 @@ static bool list_union(struct ldb_context *ldb,
 	 * NOTE: This can sort the in-memory index values, as list or
 	 * list2 might not be a copy!
 	 */
-	ldb_kv_dn_list_sort(ltdb, list);
-	ldb_kv_dn_list_sort(ltdb, list2);
+	ldb_kv_dn_list_sort(ldb_kv, list);
+	ldb_kv_dn_list_sort(ldb_kv, list2);
 
 	dn3 = talloc_array(list, struct ldb_val, list->count + list2->count);
 	if (!dn3) {
@@ -1381,7 +1381,7 @@ static bool list_union(struct ldb_context *ldb,
 }
 
 static int ldb_kv_index_dn(struct ldb_module *module,
-			   struct ltdb_private *ltdb,
+			   struct ldb_kv_private *ldb_kv,
 			   const struct ldb_parse_tree *tree,
 			   struct dn_list *list);
 
@@ -1389,7 +1389,7 @@ static int ldb_kv_index_dn(struct ldb_module *module,
   process an OR list (a union)
  */
 static int ldb_kv_index_dn_or(struct ldb_module *module,
-			      struct ltdb_private *ltdb,
+			      struct ldb_kv_private *ldb_kv,
 			      const struct ldb_parse_tree *tree,
 			      struct dn_list *list)
 {
@@ -1411,7 +1411,7 @@ static int ldb_kv_index_dn_or(struct ldb_module *module,
 		}
 
 		ret = ldb_kv_index_dn(
-		    module, ltdb, tree->u.list.elements[i], list2);
+		    module, ldb_kv, tree->u.list.elements[i], list2);
 
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 			/* X || 0 == X */
@@ -1425,7 +1425,7 @@ static int ldb_kv_index_dn_or(struct ldb_module *module,
 			return ret;
 		}
 
-		if (!list_union(ldb, ltdb, list, list2)) {
+		if (!list_union(ldb, ldb_kv, list, list2)) {
 			talloc_free(list2);
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
@@ -1443,7 +1443,7 @@ static int ldb_kv_index_dn_or(struct ldb_module *module,
   NOT an index results
  */
 static int ldb_kv_index_dn_not(struct ldb_module *module,
-			       struct ltdb_private *ltdb,
+			       struct ldb_kv_private *ldb_kv,
 			       const struct ldb_parse_tree *tree,
 			       struct dn_list *list)
 {
@@ -1463,12 +1463,12 @@ static int ldb_kv_index_dn_not(struct ldb_module *module,
  * by GUID, DN or a unique attribute
  */
 static bool ldb_kv_index_unique(struct ldb_context *ldb,
-				struct ltdb_private *ltdb,
+				struct ldb_kv_private *ldb_kv,
 				const char *attr)
 {
 	const struct ldb_schema_attribute *a;
-	if (ltdb->cache->GUID_index_attribute != NULL) {
-		if (ldb_attr_cmp(attr, ltdb->cache->GUID_index_attribute) == 0) {
+	if (ldb_kv->cache->GUID_index_attribute != NULL) {
+		if (ldb_attr_cmp(attr, ldb_kv->cache->GUID_index_attribute) == 0) {
 			return true;
 		}
 	}
@@ -1487,7 +1487,7 @@ static bool ldb_kv_index_unique(struct ldb_context *ldb,
   process an AND expression (intersection)
  */
 static int ldb_kv_index_dn_and(struct ldb_module *module,
-			       struct ltdb_private *ltdb,
+			       struct ldb_kv_private *ldb_kv,
 			       const struct ldb_parse_tree *tree,
 			       struct dn_list *list)
 {
@@ -1508,11 +1508,11 @@ static int ldb_kv_index_dn_and(struct ldb_module *module,
 		int ret;
 
 		if (subtree->operation != LDB_OP_EQUALITY ||
-		    !ldb_kv_index_unique(ldb, ltdb, subtree->u.equality.attr)) {
+		    !ldb_kv_index_unique(ldb, ldb_kv, subtree->u.equality.attr)) {
 			continue;
 		}
 
-		ret = ldb_kv_index_dn(module, ltdb, subtree, list);
+		ret = ldb_kv_index_dn(module, ldb_kv, subtree, list);
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 			/* 0 && X == 0 */
 			return LDB_ERR_NO_SUCH_OBJECT;
@@ -1539,7 +1539,7 @@ static int ldb_kv_index_dn_and(struct ldb_module *module,
 			return ldb_module_oom(module);
 		}
 
-		ret = ldb_kv_index_dn(module, ltdb, subtree, list2);
+		ret = ldb_kv_index_dn(module, ldb_kv, subtree, list2);
 
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 			/* X && 0 == 0 */
@@ -1560,7 +1560,7 @@ static int ldb_kv_index_dn_and(struct ldb_module *module,
 			list->dn = list2->dn;
 			list->count = list2->count;
 			found = true;
-		} else if (!list_intersect(ldb, ltdb,
+		} else if (!list_intersect(ldb, ldb_kv,
 					   list, list2)) {
 			talloc_free(list2);
 			return LDB_ERR_OPERATIONS_ERROR;
@@ -1589,7 +1589,7 @@ static int ldb_kv_index_dn_and(struct ldb_module *module,
   return a list of matching objects using a one-level index
  */
 static int ldb_kv_index_dn_attr(struct ldb_module *module,
-				struct ltdb_private *ltdb,
+				struct ldb_kv_private *ldb_kv,
 				const char *attr,
 				struct ldb_dn *dn,
 				struct dn_list *list,
@@ -1605,13 +1605,13 @@ static int ldb_kv_index_dn_attr(struct ldb_module *module,
 	/* work out the index key from the parent DN */
 	val.data = (uint8_t *)((uintptr_t)ldb_dn_get_casefold(dn));
 	val.length = strlen((char *)val.data);
-	key = ldb_kv_index_key(ldb, ltdb, attr, &val, NULL, truncation);
+	key = ldb_kv_index_key(ldb, ldb_kv, attr, &val, NULL, truncation);
 	if (!key) {
 		ldb_oom(ldb);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ldb_kv_dn_list_load(module, ltdb, key, list);
+	ret = ldb_kv_dn_list_load(module, ldb_kv, key, list);
 	talloc_free(key);
 	if (ret != LDB_SUCCESS) {
 		return ret;
@@ -1628,7 +1628,7 @@ static int ldb_kv_index_dn_attr(struct ldb_module *module,
   return a list of matching objects using a one-level index
  */
 static int ldb_kv_index_dn_one(struct ldb_module *module,
-			       struct ltdb_private *ltdb,
+			       struct ldb_kv_private *ldb_kv,
 			       struct ldb_dn *parent_dn,
 			       struct dn_list *list,
 			       enum key_truncation *truncation)
@@ -1636,20 +1636,20 @@ static int ldb_kv_index_dn_one(struct ldb_module *module,
 	/* Ensure we do not shortcut on intersection for this list */
 	list->strict = true;
 	return ldb_kv_index_dn_attr(
-	    module, ltdb, LTDB_IDXONE, parent_dn, list, truncation);
+	    module, ldb_kv, LTDB_IDXONE, parent_dn, list, truncation);
 }
 
 /*
   return a list of matching objects using the DN index
  */
 static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
-				   struct ltdb_private *ltdb,
+				   struct ldb_kv_private *ldb_kv,
 				   struct ldb_dn *base_dn,
 				   struct dn_list *dn_list,
 				   enum key_truncation *truncation)
 {
 	const struct ldb_val *guid_val = NULL;
-	if (ltdb->cache->GUID_index_attribute == NULL) {
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
 		dn_list->dn = talloc_array(dn_list, struct ldb_val, 1);
 		if (dn_list->dn == NULL) {
 			return ldb_module_oom(module);
@@ -1665,9 +1665,9 @@ static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	if (ltdb->cache->GUID_index_dn_component != NULL) {
+	if (ldb_kv->cache->GUID_index_dn_component != NULL) {
 		guid_val = ldb_dn_get_extended_component(base_dn,
-							 ltdb->cache->GUID_index_dn_component);
+							 ldb_kv->cache->GUID_index_dn_component);
 	}
 
 	if (guid_val != NULL) {
@@ -1683,7 +1683,7 @@ static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
 	}
 
 	return ldb_kv_index_dn_attr(
-	    module, ltdb, LTDB_IDXDN, base_dn, dn_list, truncation);
+	    module, ldb_kv, LTDB_IDXDN, base_dn, dn_list, truncation);
 }
 
 /*
@@ -1691,7 +1691,7 @@ static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
   an error. return LDB_ERR_NO_SUCH_OBJECT for no matches, or LDB_SUCCESS for matches
  */
 static int ldb_kv_index_dn(struct ldb_module *module,
-			   struct ltdb_private *ltdb,
+			   struct ldb_kv_private *ldb_kv,
 			   const struct ldb_parse_tree *tree,
 			   struct dn_list *list)
 {
@@ -1699,19 +1699,19 @@ static int ldb_kv_index_dn(struct ldb_module *module,
 
 	switch (tree->operation) {
 	case LDB_OP_AND:
-		ret = ldb_kv_index_dn_and(module, ltdb, tree, list);
+		ret = ldb_kv_index_dn_and(module, ldb_kv, tree, list);
 		break;
 
 	case LDB_OP_OR:
-		ret = ldb_kv_index_dn_or(module, ltdb, tree, list);
+		ret = ldb_kv_index_dn_or(module, ldb_kv, tree, list);
 		break;
 
 	case LDB_OP_NOT:
-		ret = ldb_kv_index_dn_not(module, ltdb, tree, list);
+		ret = ldb_kv_index_dn_not(module, ldb_kv, tree, list);
 		break;
 
 	case LDB_OP_EQUALITY:
-		ret = ldb_kv_index_dn_leaf(module, ltdb, tree, list);
+		ret = ldb_kv_index_dn_leaf(module, ldb_kv, tree, list);
 		break;
 
 	case LDB_OP_SUBSTRING:
@@ -1732,7 +1732,7 @@ static int ldb_kv_index_dn(struct ldb_module *module,
   filter a candidate dn_list from an indexed search into a set of results
   extracting just the given attributes
 */
-static int ldb_kv_index_filter(struct ltdb_private *ltdb,
+static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 			       const struct dn_list *dn_list,
 			       struct ldb_kv_context *ac,
 			       uint32_t *match_count,
@@ -1757,7 +1757,7 @@ static int ldb_kv_index_filter(struct ltdb_private *ltdb,
 		return ldb_module_oom(ac->module);
 	}
 
-	if (ltdb->cache->GUID_index_attribute != NULL) {
+	if (ldb_kv->cache->GUID_index_attribute != NULL) {
 		/*
 		 * We speculate that the keys will be GUID based and so
 		 * pre-fill in enough space for a GUID (avoiding a pile of
@@ -1790,13 +1790,13 @@ static int ldb_kv_index_filter(struct ltdb_private *ltdb,
 		int ret;
 
 		ret = ldb_kv_idx_to_key(
-		    ac->module, ltdb, keys, &dn_list->dn[i], &keys[num_keys]);
+		    ac->module, ldb_kv, keys, &dn_list->dn[i], &keys[num_keys]);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(keys);
 			return ret;
 		}
 
-		if (ltdb->cache->GUID_index_attribute != NULL) {
+		if (ldb_kv->cache->GUID_index_attribute != NULL) {
 			/*
 			 * If we are in GUID index mode, then the dn_list is
 			 * sorted.  If we got a duplicate, forget about it, as
@@ -1836,7 +1836,7 @@ static int ldb_kv_index_filter(struct ltdb_private *ltdb,
 
 		ret =
 		    ldb_kv_search_key(ac->module,
-				      ltdb,
+				      ldb_kv,
 				      keys[i],
 				      msg,
 				      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
@@ -1866,7 +1866,7 @@ static int ldb_kv_index_filter(struct ltdb_private *ltdb,
 		 * LDB_SCOPE_BASE is not passed in by our only caller.
 		 */
 		if (ac->scope == LDB_SCOPE_ONELEVEL
-		    && ltdb->cache->one_level_indexes
+		    && ldb_kv->cache->one_level_indexes
 		    && scope_one_truncation == KEY_NOT_TRUNCATED) {
 			ret = ldb_match_message(ldb, msg, ac->tree,
 						ac->scope, &matched);
@@ -1916,7 +1916,7 @@ static int ldb_kv_index_filter(struct ltdb_private *ltdb,
 /*
   sort a DN list
  */
-static void ldb_kv_dn_list_sort(struct ltdb_private *ltdb, struct dn_list *list)
+static void ldb_kv_dn_list_sort(struct ldb_kv_private *ltdb, struct dn_list *list)
 {
 	if (list->count < 2) {
 		return;
@@ -1939,15 +1939,15 @@ static void ldb_kv_dn_list_sort(struct ltdb_private *ltdb, struct dn_list *list)
 int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
-	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(ac->module), struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(ac->module), struct ldb_kv_private);
 	struct dn_list *dn_list;
 	int ret;
 	enum ldb_scope index_scope;
 	enum key_truncation scope_one_truncation = KEY_NOT_TRUNCATED;
 
 	/* see if indexing is enabled */
-	if (!ltdb->cache->attribute_indexes &&
-	    !ltdb->cache->one_level_indexes &&
+	if (!ldb_kv->cache->attribute_indexes &&
+	    !ldb_kv->cache->one_level_indexes &&
 	    ac->scope != LDB_SCOPE_BASE) {
 		/* fallback to a full search */
 		return LDB_ERR_OPERATIONS_ERROR;
@@ -1964,7 +1964,7 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 	 * search
 	 */
 	if (ac->scope == LDB_SCOPE_ONELEVEL &&
-	    !ltdb->cache->one_level_indexes) {
+	    !ldb_kv->cache->one_level_indexes) {
 		index_scope = LDB_SCOPE_SUBTREE;
 	} else {
 		index_scope = ac->scope;
@@ -1985,7 +1985,7 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 		 * this list, as we trust the ONELEVEL index
 		 */
 		ret = ldb_kv_index_dn_one(
-		    ac->module, ltdb, ac->base, dn_list, &scope_one_truncation);
+		    ac->module, ldb_kv, ac->base, dn_list, &scope_one_truncation);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(dn_list);
 			return ret;
@@ -2004,14 +2004,14 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 		 * do it always and rely on the index lookup being
 		 * fast enough in the small case.
 		 */
-		if (ltdb->cache->GUID_index_attribute != NULL) {
+		if (ldb_kv->cache->GUID_index_attribute != NULL) {
 			struct dn_list *idx_one_tree_list
 				= talloc_zero(ac, struct dn_list);
 			if (idx_one_tree_list == NULL) {
 				return ldb_module_oom(ac->module);
 			}
 
-			if (!ltdb->cache->attribute_indexes) {
+			if (!ldb_kv->cache->attribute_indexes) {
 				talloc_free(idx_one_tree_list);
 				talloc_free(dn_list);
 				return LDB_ERR_OPERATIONS_ERROR;
@@ -2024,9 +2024,9 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 			 * the ONELEVEL index is still good enough.
 			 */
 			ret = ldb_kv_index_dn(
-			    ac->module, ltdb, ac->tree, idx_one_tree_list);
+			    ac->module, ldb_kv, ac->tree, idx_one_tree_list);
 			if (ret == LDB_SUCCESS) {
-				if (!list_intersect(ldb, ltdb,
+				if (!list_intersect(ldb, ldb_kv,
 						    dn_list,
 						    idx_one_tree_list)) {
 					talloc_free(idx_one_tree_list);
@@ -2039,7 +2039,7 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 
 	case LDB_SCOPE_SUBTREE:
 	case LDB_SCOPE_DEFAULT:
-		if (!ltdb->cache->attribute_indexes) {
+		if (!ldb_kv->cache->attribute_indexes) {
 			talloc_free(dn_list);
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
@@ -2047,7 +2047,7 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 		 * Here we load the index for the tree.  We have no
 		 * index for the subtree.
 		 */
-		ret = ldb_kv_index_dn(ac->module, ltdb, ac->tree, dn_list);
+		ret = ldb_kv_index_dn(ac->module, ldb_kv, ac->tree, dn_list);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(dn_list);
 			return ret;
@@ -2067,7 +2067,7 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 	 * SCOPE_ONELEVEL index.
 	 */
 	ret = ldb_kv_index_filter(
-	    ltdb, dn_list, ac, match_count, scope_one_truncation);
+	    ldb_kv, dn_list, ac, match_count, scope_one_truncation);
 	talloc_free(dn_list);
 	return ret;
 }
@@ -2093,7 +2093,7 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
  * @return                  An ldb error code
  */
 static int ldb_kv_index_add1(struct ldb_module *module,
-			     struct ltdb_private *ltdb,
+			     struct ldb_kv_private *ldb_kv,
 			     const struct ldb_message *msg,
 			     struct ldb_message_element *el,
 			     int v_idx)
@@ -2115,7 +2115,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 	}
 
 	dn_key = ldb_kv_index_key(
-	    ldb, ltdb, el->name, &el->values[v_idx], &a, &truncation);
+	    ldb, ldb_kv, el->name, &el->values[v_idx], &a, &truncation);
 	if (!dn_key) {
 		talloc_free(list);
 		return LDB_ERR_OPERATIONS_ERROR;
@@ -2135,13 +2135,13 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 			"exceeds maximum key length of %u (encoded).",
 			el->name,
 			ldb_dn_get_linearized(msg->dn),
-			ltdb->max_key_length);
+			ldb_kv->max_key_length);
 		talloc_free(list);
 		return LDB_ERR_CONSTRAINT_VIOLATION;
 	}
 	talloc_steal(list, dn_key);
 
-	ret = ldb_kv_dn_list_load(module, ltdb, dn_key, list);
+	ret = ldb_kv_dn_list_load(module, ldb_kv, dn_key, list);
 	if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
 		talloc_free(list);
 		return ret;
@@ -2184,14 +2184,14 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 			}
 
 			ret = ldb_kv_idx_to_key(
-			    module, ltdb, ldb, &list->dn[i], &key);
+			    module, ldb_kv, ldb, &list->dn[i], &key);
 			if (ret != LDB_SUCCESS) {
 				TALLOC_FREE(list);
 				TALLOC_FREE(rec);
 				return ret;
 			}
 
-			ret = ldb_kv_search_key(module, ltdb, key, rec, flags);
+			ret = ldb_kv_search_key(module, ldb_kv, key, rec, flags);
 			if (key.dptr != guid_key) {
 				TALLOC_FREE(key.dptr);
 			}
@@ -2239,7 +2239,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		 * user-visible error string
 		 */
 
-		if (ltdb->cache->GUID_index_attribute == NULL) {
+		if (ldb_kv->cache->GUID_index_attribute == NULL) {
 			ldb_debug(ldb, LDB_DEBUG_WARNING,
 				  __location__
 				  ": unique index violation on %s in %s, "
@@ -2254,7 +2254,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 			const struct ldb_schema_attribute *attr
 				= ldb_schema_attribute_by_name(
 					ldb,
-					ltdb->cache->GUID_index_attribute);
+					ldb_kv->cache->GUID_index_attribute);
 			struct ldb_val v;
 			ret = attr->syntax->ldif_write_fn(ldb, list,
 							  &list->dn[0], &v);
@@ -2265,7 +2265,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 					  "%s, conficts with %s %*.*s in %s",
 					  el->name,
 					  ldb_dn_get_linearized(msg->dn),
-					  ltdb->cache->GUID_index_attribute,
+					  ldb_kv->cache->GUID_index_attribute,
 					  (int)v.length,
 					  (int)v.length,
 					  v.data,
@@ -2290,7 +2290,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	if (ltdb->cache->GUID_index_attribute == NULL) {
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
 		const char *dn_str = ldb_dn_get_linearized(msg->dn);
 		list->dn[list->count].data
 			= (uint8_t *)talloc_strdup(list->dn, dn_str);
@@ -2303,7 +2303,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		const struct ldb_val *key_val;
 		struct ldb_val *exact = NULL, *next = NULL;
 		key_val = ldb_msg_find_ldb_val(msg,
-					       ltdb->cache->GUID_index_attribute);
+					       ldb_kv->cache->GUID_index_attribute);
 		if (key_val == NULL) {
 			talloc_free(list);
 			return ldb_module_operr(module);
@@ -2329,7 +2329,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 			const struct ldb_schema_attribute *attr
 				= ldb_schema_attribute_by_name(
 					ldb,
-					ltdb->cache->GUID_index_attribute);
+					ldb_kv->cache->GUID_index_attribute);
 			struct ldb_val v;
 			ret = attr->syntax->ldif_write_fn(ldb, list,
 							  exact, &v);
@@ -2341,7 +2341,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 					  "duplicate of %s %*.*s in %s",
 					  ldb_dn_get_linearized(msg->dn),
 					  el->name,
-					  ltdb->cache->GUID_index_attribute,
+					  ldb_kv->cache->GUID_index_attribute,
 					  (int)v.length,
 					  (int)v.length,
 					  v.data,
@@ -2374,13 +2374,13 @@ static int ldb_kv_index_add1(struct ldb_module *module,
   add index entries for one elements in a message
  */
 static int ldb_kv_index_add_el(struct ldb_module *module,
-			       struct ltdb_private *ltdb,
+			       struct ldb_kv_private *ldb_kv,
 			       const struct ldb_message *msg,
 			       struct ldb_message_element *el)
 {
 	unsigned int i;
 	for (i = 0; i < el->num_values; i++) {
-		int ret = ldb_kv_index_add1(module, ltdb, msg, el, i);
+		int ret = ldb_kv_index_add1(module, ldb_kv, msg, el, i);
 		if (ret != LDB_SUCCESS) {
 			return ret;
 		}
@@ -2393,7 +2393,7 @@ static int ldb_kv_index_add_el(struct ldb_module *module,
   add index entries for all elements in a message
  */
 static int ldb_kv_index_add_all(struct ldb_module *module,
-				struct ltdb_private *ltdb,
+				struct ldb_kv_private *ldb_kv,
 				const struct ldb_message *msg)
 {
 	struct ldb_message_element *elements = msg->elements;
@@ -2415,16 +2415,16 @@ static int ldb_kv_index_add_all(struct ldb_module *module,
 		return ret;
 	}
 
-	if (!ltdb->cache->attribute_indexes) {
+	if (!ldb_kv->cache->attribute_indexes) {
 		/* no indexed fields */
 		return LDB_SUCCESS;
 	}
 
 	for (i = 0; i < msg->num_elements; i++) {
-		if (!ldb_kv_is_indexed(module, ltdb, elements[i].name)) {
+		if (!ldb_kv_is_indexed(module, ldb_kv, elements[i].name)) {
 			continue;
 		}
-		ret = ldb_kv_index_add_el(module, ltdb, msg, &elements[i]);
+		ret = ldb_kv_index_add_el(module, ldb_kv, msg, &elements[i]);
 		if (ret != LDB_SUCCESS) {
 			struct ldb_context *ldb = ldb_module_get_ctx(module);
 			ldb_asprintf_errstring(ldb,
@@ -2443,7 +2443,7 @@ static int ldb_kv_index_add_all(struct ldb_module *module,
   insert a DN index for a message
 */
 static int ldb_kv_modify_index_dn(struct ldb_module *module,
-				  struct ltdb_private *ltdb,
+				  struct ldb_kv_private *ldb_kv,
 				  const struct ldb_message *msg,
 				  struct ldb_dn *dn,
 				  const char *index,
@@ -2462,7 +2462,7 @@ static int ldb_kv_modify_index_dn(struct ldb_module *module,
 				       "against %s in %s: failed "
 				       "to get casefold DN",
 				       index,
-				       ltdb->cache->GUID_index_attribute,
+				       ldb_kv->cache->GUID_index_attribute,
 				       dn_str);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
@@ -2473,9 +2473,9 @@ static int ldb_kv_modify_index_dn(struct ldb_module *module,
 	el.num_values = 1;
 
 	if (add) {
-		ret = ldb_kv_index_add1(module, ltdb, msg, &el, 0);
+		ret = ldb_kv_index_add1(module, ldb_kv, msg, &el, 0);
 	} else { /* delete */
-		ret = ldb_kv_index_del_value(module, ltdb, msg, &el, 0);
+		ret = ldb_kv_index_del_value(module, ldb_kv, msg, &el, 0);
 	}
 
 	if (ret != LDB_SUCCESS) {
@@ -2486,7 +2486,7 @@ static int ldb_kv_modify_index_dn(struct ldb_module *module,
 				       ": Failed to modify %s "
 				       "against %s in %s - %s",
 				       index,
-				       ltdb->cache->GUID_index_attribute,
+				       ldb_kv->cache->GUID_index_attribute,
 				       dn_str, ldb_errstring(ldb));
 		return ret;
 	}
@@ -2500,13 +2500,13 @@ static int ldb_kv_index_onelevel(struct ldb_module *module,
 				 const struct ldb_message *msg,
 				 int add)
 {
-	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module),
-						    struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module),
+						    struct ldb_kv_private);
 	struct ldb_dn *pdn;
 	int ret;
 
 	/* We index for ONE Level only if requested */
-	if (!ltdb->cache->one_level_indexes) {
+	if (!ldb_kv->cache->one_level_indexes) {
 		return LDB_SUCCESS;
 	}
 
@@ -2514,7 +2514,7 @@ static int ldb_kv_index_onelevel(struct ldb_module *module,
 	if (pdn == NULL) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
-	ret = ldb_kv_modify_index_dn(module, ltdb, msg, pdn, LTDB_IDXONE, add);
+	ret = ldb_kv_modify_index_dn(module, ldb_kv, msg, pdn, LTDB_IDXONE, add);
 
 	talloc_free(pdn);
 
@@ -2529,16 +2529,16 @@ static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
 				      int add)
 {
 	int ret;
-	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module),
-						    struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module),
+						    struct ldb_kv_private);
 
 	/* We index for DN only if using a GUID index */
-	if (ltdb->cache->GUID_index_attribute == NULL) {
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
 		return LDB_SUCCESS;
 	}
 
 	ret =
-	    ldb_kv_modify_index_dn(module, ltdb, msg, msg->dn, LTDB_IDXDN, add);
+	    ldb_kv_modify_index_dn(module, ldb_kv, msg, msg->dn, LTDB_IDXDN, add);
 
 	if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
@@ -2554,24 +2554,24 @@ static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
   The caller guarantees that these element values are not yet indexed
 */
 int ldb_kv_index_add_element(struct ldb_module *module,
-			     struct ltdb_private *ltdb,
+			     struct ldb_kv_private *ldb_kv,
 			     const struct ldb_message *msg,
 			     struct ldb_message_element *el)
 {
 	if (ldb_dn_is_special(msg->dn)) {
 		return LDB_SUCCESS;
 	}
-	if (!ldb_kv_is_indexed(module, ltdb, el->name)) {
+	if (!ldb_kv_is_indexed(module, ldb_kv, el->name)) {
 		return LDB_SUCCESS;
 	}
-	return ldb_kv_index_add_el(module, ltdb, msg, el);
+	return ldb_kv_index_add_el(module, ldb_kv, msg, el);
 }
 
 /*
   add the index entries for a new record
 */
 int ldb_kv_index_add_new(struct ldb_module *module,
-			 struct ltdb_private *ltdb,
+			 struct ldb_kv_private *ldb_kv,
 			 const struct ldb_message *msg)
 {
 	int ret;
@@ -2580,7 +2580,7 @@ int ldb_kv_index_add_new(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	ret = ldb_kv_index_add_all(module, ltdb, msg);
+	ret = ldb_kv_index_add_all(module, ldb_kv, msg);
 	if (ret != LDB_SUCCESS) {
 		/*
 		 * Because we can't trust the caller to be doing
@@ -2612,7 +2612,7 @@ int ldb_kv_index_add_new(struct ldb_module *module,
   delete an index entry for one message element
 */
 int ldb_kv_index_del_value(struct ldb_module *module,
-			   struct ltdb_private *ltdb,
+			   struct ldb_kv_private *ldb_kv,
 			   const struct ldb_message *msg,
 			   struct ldb_message_element *el,
 			   unsigned int v_idx)
@@ -2638,7 +2638,7 @@ int ldb_kv_index_del_value(struct ldb_module *module,
 	}
 
 	dn_key = ldb_kv_index_key(
-	    ldb, ltdb, el->name, &el->values[v_idx], NULL, &truncation);
+	    ldb, ldb_kv, el->name, &el->values[v_idx], NULL, &truncation);
 	/*
 	 * We ignore key truncation in ltdb_index_add1() so
 	 * match that by ignoring it here as well
@@ -2655,7 +2655,7 @@ int ldb_kv_index_del_value(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ldb_kv_dn_list_load(module, ltdb, dn_key, list);
+	ret = ldb_kv_dn_list_load(module, ldb_kv, dn_key, list);
 	if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 		/* it wasn't indexed. Did we have an earlier error? If we did then
 		   its gone now */
@@ -2671,7 +2671,7 @@ int ldb_kv_index_del_value(struct ldb_module *module,
 	/*
 	 * Find one of the values matching this message to remove
 	 */
-	i = ldb_kv_dn_list_find_msg(ltdb, list, msg);
+	i = ldb_kv_dn_list_find_msg(ldb_kv, list, msg);
 	if (i == -1) {
 		/* nothing to delete */
 		talloc_free(dn_key);
@@ -2702,7 +2702,7 @@ int ldb_kv_index_del_value(struct ldb_module *module,
   return -1 on failure
 */
 int ldb_kv_index_del_element(struct ldb_module *module,
-			     struct ltdb_private *ltdb,
+			     struct ldb_kv_private *ldb_kv,
 			     const struct ldb_message *msg,
 			     struct ldb_message_element *el)
 {
@@ -2710,7 +2710,7 @@ int ldb_kv_index_del_element(struct ldb_module *module,
 	int ret;
 	unsigned int i;
 
-	if (!ltdb->cache->attribute_indexes) {
+	if (!ldb_kv->cache->attribute_indexes) {
 		/* no indexed fields */
 		return LDB_SUCCESS;
 	}
@@ -2724,11 +2724,11 @@ int ldb_kv_index_del_element(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	if (!ldb_kv_is_indexed(module, ltdb, el->name)) {
+	if (!ldb_kv_is_indexed(module, ldb_kv, el->name)) {
 		return LDB_SUCCESS;
 	}
 	for (i = 0; i < el->num_values; i++) {
-		ret = ldb_kv_index_del_value(module, ltdb, msg, el, i);
+		ret = ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
 		if (ret != LDB_SUCCESS) {
 			return ret;
 		}
@@ -2744,7 +2744,7 @@ int ldb_kv_index_del_element(struct ldb_module *module,
 int ldb_kv_index_delete(struct ldb_module *module,
 			const struct ldb_message *msg)
 {
-	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
 	int ret;
 	unsigned int i;
 
@@ -2762,14 +2762,14 @@ int ldb_kv_index_delete(struct ldb_module *module,
 		return ret;
 	}
 
-	if (!ltdb->cache->attribute_indexes) {
+	if (!ldb_kv->cache->attribute_indexes) {
 		/* no indexed fields */
 		return LDB_SUCCESS;
 	}
 
 	for (i = 0; i < msg->num_elements; i++) {
 		ret = ldb_kv_index_del_element(
-		    module, ltdb, msg, &msg->elements[i]);
+		    module, ldb_kv, msg, &msg->elements[i]);
 		if (ret != LDB_SUCCESS) {
 			return ret;
 		}
@@ -2787,7 +2787,7 @@ int ldb_kv_index_delete(struct ldb_module *module,
   commit, which in turn greatly reduces DB churn as we will likely
   be able to do a direct update into the old record.
 */
-static int delete_index(struct ltdb_private *ltdb, struct ldb_val key, struct ldb_val data, void *state)
+static int delete_index(struct ldb_kv_private *ldb_kv, struct ldb_val key, struct ldb_val data, void *state)
 {
 	struct ldb_module *module = state;
 	const char *dnstr = "DN=" LTDB_INDEX ":";
@@ -2808,7 +2808,7 @@ static int delete_index(struct ltdb_private *ltdb, struct ldb_val key, struct ld
 	v.data = key.data + 3;
 	v.length = strnlen((char *)key.data, key.length) - 3;
 
-	dn = ldb_dn_from_ldb_val(ltdb, ldb_module_get_ctx(module), &v);
+	dn = ldb_dn_from_ldb_val(ldb_kv, ldb_module_get_ctx(module), &v);
 
 	/*
 	 * This does not actually touch the DB quite yet, just
@@ -2829,7 +2829,7 @@ static int delete_index(struct ltdb_private *ltdb, struct ldb_val key, struct ld
 /*
   traversal function that adds @INDEX records during a re index TODO wrong comment
 */
-static int re_key(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_val val, void *state)
+static int re_key(struct ldb_kv_private *ldb_kv, struct ldb_val ldb_key, struct ldb_val val, void *state)
 {
 	struct ldb_context *ldb;
 	struct ldb_kv_reindex_context *ctx =
@@ -2902,7 +2902,7 @@ static int re_key(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_
 			.data = key2.dptr,
 			.length = key2.dsize
 		};
-		ltdb->kv_ops->update_in_iterate(ltdb, ldb_key, ldb_key2, val, ctx);
+		ldb_kv->kv_ops->update_in_iterate(ldb_kv, ldb_key, ldb_key2, val, ctx);
 	}
 	talloc_free(key2.dptr);
 
@@ -2921,7 +2921,7 @@ static int re_key(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_
 /*
   traversal function that adds @INDEX records during a re index
 */
-static int re_index(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ldb_val val, void *state)
+static int re_index(struct ldb_kv_private *ldb_kv, struct ldb_val ldb_key, struct ldb_val val, void *state)
 {
 	struct ldb_context *ldb;
 	struct ldb_kv_reindex_context *ctx =
@@ -2985,7 +2985,7 @@ static int re_index(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ld
 		return -1;
 	}
 
-	ret = ldb_kv_index_add_all(module, ltdb, msg);
+	ret = ldb_kv_index_add_all(module, ldb_kv, msg);
 
 	if (ret != LDB_SUCCESS) {
 		ctx->error = ret;
@@ -3010,7 +3010,7 @@ static int re_index(struct ltdb_private *ltdb, struct ldb_val ldb_key, struct ld
 */
 int ldb_kv_reindex(struct ldb_module *module)
 {
-	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
 	int ret;
 	struct ldb_kv_reindex_context ctx;
 
@@ -3018,7 +3018,7 @@ int ldb_kv_reindex(struct ldb_module *module)
 	 * Only triggered after a modification, but make clear we do
 	 * not re-index a read-only DB
 	 */
-	if (ltdb->read_only) {
+	if (ldb_kv->read_only) {
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
@@ -3041,7 +3041,7 @@ int ldb_kv_reindex(struct ldb_module *module)
 	/* first traverse the database deleting any @INDEX records by
 	 * putting NULL entries in the in-memory tdb
 	 */
-	ret = ltdb->kv_ops->iterate(ltdb, delete_index, module);
+	ret = ldb_kv->kv_ops->iterate(ldb_kv, delete_index, module);
 	if (ret < 0) {
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
 		ldb_asprintf_errstring(ldb, "index deletion traverse failed: %s",
@@ -3053,7 +3053,7 @@ int ldb_kv_reindex(struct ldb_module *module)
 	ctx.error = 0;
 	ctx.count = 0;
 
-	ret = ltdb->kv_ops->iterate(ltdb, re_key, &ctx);
+	ret = ldb_kv->kv_ops->iterate(ldb_kv, re_key, &ctx);
 	if (ret < 0) {
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
 		ldb_asprintf_errstring(ldb, "key correction traverse failed: %s",
@@ -3071,7 +3071,7 @@ int ldb_kv_reindex(struct ldb_module *module)
 	ctx.count = 0;
 
 	/* now traverse adding any indexes for normal LDB records */
-	ret = ltdb->kv_ops->iterate(ltdb, re_index, &ctx);
+	ret = ldb_kv->kv_ops->iterate(ldb_kv, re_index, &ctx);
 	if (ret < 0) {
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
 		ldb_asprintf_errstring(ldb, "reindexing traverse failed: %s",
@@ -3089,7 +3089,7 @@ int ldb_kv_reindex(struct ldb_module *module)
 		ldb_debug(ldb_module_get_ctx(module),
 			  LDB_DEBUG_WARNING, "Reindexing: re_index successful on %s, "
 			  "final index write-out will be in transaction commit",
-			  ltdb->kv_ops->name(ltdb));
+			  ldb_kv->kv_ops->name(ldb_kv));
 	}
 	return LDB_SUCCESS;
 }
diff --git a/lib/ldb/ldb_tdb/ldb_search.c b/lib/ldb/ldb_tdb/ldb_search.c
index 77938b7..fd8b64e 100644
--- a/lib/ldb/ldb_tdb/ldb_search.c
+++ b/lib/ldb/ldb_tdb/ldb_search.c
@@ -235,7 +235,7 @@ static int ldb_kv_parse_data_unpack(struct ldb_val key,
   and LDB_SUCCESS on success
 */
 int ldb_kv_search_key(struct ldb_module *module,
-		      struct ltdb_private *ltdb,
+		      struct ldb_kv_private *ldb_kv,
 		      const struct TDB_DATA tdb_key,
 		      struct ldb_message *msg,
 		      unsigned int unpack_flags)
@@ -256,11 +256,11 @@ int ldb_kv_search_key(struct ldb_module *module,
 	msg->num_elements = 0;
 	msg->elements = NULL;
 
-	ret = ltdb->kv_ops->fetch_and_parse(
-	    ltdb, ldb_key, ldb_kv_parse_data_unpack, &ctx);
+	ret = ldb_kv->kv_ops->fetch_and_parse(
+	    ldb_kv, ldb_key, ldb_kv_parse_data_unpack, &ctx);
 
 	if (ret == -1) {
-		ret = ltdb->kv_ops->error(ltdb);
+		ret = ldb_kv->kv_ops->error(ldb_kv);
 		if (ret == LDB_SUCCESS) {
 			/*
 			 * Just to be sure we don't turn errors
@@ -289,7 +289,7 @@ int ldb_kv_search_dn1(struct ldb_module *module,
 		      unsigned int unpack_flags)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	int ret;
 	uint8_t guid_key[LTDB_GUID_KEY_SIZE];
 	TDB_DATA tdb_key = {
@@ -298,7 +298,7 @@ int ldb_kv_search_dn1(struct ldb_module *module,
 	};
 	TALLOC_CTX *tdb_key_ctx = NULL;
 
-	if (ltdb->cache->GUID_index_attribute == NULL ||
+	if (ldb_kv->cache->GUID_index_attribute == NULL ||
 		ldb_dn_is_special(dn)) {
 
 		tdb_key_ctx = talloc_new(msg);
@@ -320,13 +320,13 @@ int ldb_kv_search_dn1(struct ldb_module *module,
 		 * used for internal memory.
 		 *
 		 */
-		ret = ldb_kv_key_dn_from_idx(module, ltdb, msg, dn, &tdb_key);
+		ret = ldb_kv_key_dn_from_idx(module, ldb_kv, msg, dn, &tdb_key);
 		if (ret != LDB_SUCCESS) {
 			return ret;
 		}
 	}
 
-	ret = ldb_kv_search_key(module, ltdb, tdb_key, msg, unpack_flags);
+	ret = ldb_kv_search_key(module, ldb_kv, tdb_key, msg, unpack_flags);
 
 	TALLOC_FREE(tdb_key_ctx);
 
@@ -493,7 +493,7 @@ failed:
 /*
   search function for a non-indexed search
  */
-static int search_func(struct ltdb_private *ltdb, struct ldb_val key, struct ldb_val val, void *state)
+static int search_func(struct ldb_kv_private *ldb_kv, struct ldb_val key, struct ldb_val val, void *state)
 {
 	struct ldb_context *ldb;
 	struct ldb_kv_context *ac;
@@ -583,11 +583,11 @@ static int search_func(struct ltdb_private *ltdb, struct ldb_val key, struct ldb
 static int ldb_kv_search_full(struct ldb_kv_context *ctx)
 {
 	void *data = ldb_module_get_private(ctx->module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	int ret;
 
 	ctx->error = LDB_SUCCESS;
-	ret = ltdb->kv_ops->iterate(ltdb, search_func, ctx);
+	ret = ldb_kv->kv_ops->iterate(ldb_kv, search_func, ctx);
 
 	if (ret < 0) {
 		return LDB_ERR_OPERATIONS_ERROR;
@@ -596,7 +596,7 @@ static int ldb_kv_search_full(struct ldb_kv_context *ctx)
 	return ctx->error;
 }
 
-static int ldb_kv_search_and_return_base(struct ltdb_private *ltdb,
+static int ldb_kv_search_and_return_base(struct ldb_kv_private *ldb_kv,
 					 struct ldb_kv_context *ctx)
 {
 	struct ldb_message *msg, *filtered_msg;
@@ -617,7 +617,7 @@ static int ldb_kv_search_and_return_base(struct ltdb_private *ltdb,
 				    LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC);
 
 	if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-		if (ltdb->check_base == false) {
+		if (ldb_kv->check_base == false) {
 			/*
 			 * In this case, we are done, as no base
 			 * checking is allowed in this DB
@@ -708,24 +708,24 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	int ret;
 
 	ldb = ldb_module_get_ctx(module);
 
 	ldb_request_set_state(req, LDB_ASYNC_PENDING);
 
-	if (ltdb->kv_ops->lock_read(module) != 0) {
+	if (ldb_kv->kv_ops->lock_read(module) != 0) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
 	if (ldb_kv_cache_load(module) != 0) {
-		ltdb->kv_ops->unlock_read(module);
+		ldb_kv->kv_ops->unlock_read(module);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
 	if (req->op.search.tree == NULL) {
-		ltdb->kv_ops->unlock_read(module);
+		ldb_kv->kv_ops->unlock_read(module);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
@@ -770,13 +770,13 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 		 * will try to look up an index record for a special
 		 * record (which doesn't exist).
 		 */
-		ret = ldb_kv_search_and_return_base(ltdb, ctx);
+		ret = ldb_kv_search_and_return_base(ldb_kv, ctx);
 
-		ltdb->kv_ops->unlock_read(module);
+		ldb_kv->kv_ops->unlock_read(module);
 
 		return ret;
 
-	} else if (ltdb->check_base) {
+	} else if (ldb_kv->check_base) {
 		/*
 		 * This database has been marked as
 		 * 'checkBaseOnSearch', so do a spot check of the base
@@ -811,7 +811,7 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 		 * callback error */
 		if ( ! ctx->request_terminated && ret != LDB_SUCCESS) {
 			/* Not indexed, so we need to do a full scan */
-			if (ltdb->warn_unindexed || ltdb->disable_full_db_scan) {
+			if (ldb_kv->warn_unindexed || ldb_kv->disable_full_db_scan) {
 				/* useful for debugging when slow performance
 				 * is caused by unindexed searches */
 				char *expression = ldb_filter_from_tree(ctx, ctx->tree);
@@ -834,14 +834,14 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 				 * full search or we may return
 				 * duplicate entries
 				 */
-				ltdb->kv_ops->unlock_read(module);
+				ldb_kv->kv_ops->unlock_read(module);
 				return LDB_ERR_OPERATIONS_ERROR;
 			}
 
-			if (ltdb->disable_full_db_scan) {
+			if (ldb_kv->disable_full_db_scan) {
 				ldb_set_errstring(ldb,
 						  "ldb FULL SEARCH disabled");
-				ltdb->kv_ops->unlock_read(module);
+				ldb_kv->kv_ops->unlock_read(module);
 				return LDB_ERR_INAPPROPRIATE_MATCHING;
 			}
 
@@ -852,7 +852,7 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 		}
 	}
 
-	ltdb->kv_ops->unlock_read(module);
+	ldb_kv->kv_ops->unlock_read(module);
 
 	return ret;
 }
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.c b/lib/ldb/ldb_tdb/ldb_tdb.c
index d01fa9e..c1f5fa3 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -97,37 +97,37 @@ int ltdb_err_map(enum TDB_ERROR tdb_code)
 static int ltdb_lock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	int tdb_ret = 0;
 	int ret;
 	pid_t pid = getpid();
 
-	if (ltdb->pid != pid) {
+	if (ldb_kv->pid != pid) {
 		ldb_asprintf_errstring(
 			ldb_module_get_ctx(module),
 			__location__": Reusing ldb opend by pid %d in "
 			"process %d\n",
-			ltdb->pid,
+			ldb_kv->pid,
 			pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	if (tdb_transaction_active(ltdb->tdb) == false &&
-	    ltdb->read_lock_count == 0) {
-		tdb_ret = tdb_lockall_read(ltdb->tdb);
+	if (tdb_transaction_active(ldb_kv->tdb) == false &&
+	    ldb_kv->read_lock_count == 0) {
+		tdb_ret = tdb_lockall_read(ldb_kv->tdb);
 	}
 	if (tdb_ret == 0) {
-		ltdb->read_lock_count++;
+		ldb_kv->read_lock_count++;
 		return LDB_SUCCESS;
 	}
-	ret = ltdb_err_map(tdb_error(ltdb->tdb));
+	ret = ltdb_err_map(tdb_error(ldb_kv->tdb));
 	if (ret == LDB_SUCCESS) {
 		ret = LDB_ERR_OPERATIONS_ERROR;
 	}
 	ldb_debug_set(ldb_module_get_ctx(module),
 		      LDB_DEBUG_FATAL,
 		      "Failure during ltdb_lock_read(): %s -> %s",
-		      tdb_errorstr(ltdb->tdb),
+		      tdb_errorstr(ldb_kv->tdb),
 		      ldb_strerror(ret));
 	return ret;
 }
@@ -138,24 +138,24 @@ static int ltdb_lock_read(struct ldb_module *module)
 static int ltdb_unlock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	pid_t pid = getpid();
 
-	if (ltdb->pid != pid) {
+	if (ldb_kv->pid != pid) {
 		ldb_asprintf_errstring(
 			ldb_module_get_ctx(module),
 			__location__": Reusing ldb opend by pid %d in "
 			"process %d\n",
-			ltdb->pid,
+			ldb_kv->pid,
 			pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
-	if (!tdb_transaction_active(ltdb->tdb) && ltdb->read_lock_count == 1) {
-		tdb_unlockall_read(ltdb->tdb);
-		ltdb->read_lock_count--;
+	if (!tdb_transaction_active(ldb_kv->tdb) && ldb_kv->read_lock_count == 1) {
+		tdb_unlockall_read(ldb_kv->tdb);
+		ldb_kv->read_lock_count--;
 		return 0;
 	}
-	ltdb->read_lock_count--;
+	ldb_kv->read_lock_count--;
 	return 0;
 }
 
@@ -246,7 +246,7 @@ failed:
 
 /* The caller is to provide a correctly sized key */
 int ldb_kv_guid_to_key(struct ldb_module *module,
-		       struct ltdb_private *ltdb,
+		       struct ldb_kv_private *ldb_kv,
 		       const struct ldb_val *GUID_val,
 		       TDB_DATA *key)
 {
@@ -268,7 +268,7 @@ int ldb_kv_guid_to_key(struct ldb_module *module,
  * the GUID index mode
  */
 int ldb_kv_idx_to_key(struct ldb_module *module,
-		      struct ltdb_private *ltdb,
+		      struct ldb_kv_private *ldb_kv,
 		      TALLOC_CTX *mem_ctx,
 		      const struct ldb_val *idx_val,
 		      TDB_DATA *key)
@@ -276,8 +276,8 @@ int ldb_kv_idx_to_key(struct ldb_module *module,
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	struct ldb_dn *dn;
 
-	if (ltdb->cache->GUID_index_attribute != NULL) {
-		return ldb_kv_guid_to_key(module, ltdb, idx_val, key);
+	if (ldb_kv->cache->GUID_index_attribute != NULL) {
+		return ldb_kv_guid_to_key(module, ldb_kv, idx_val, key);
 	}
 
 	dn = ldb_dn_from_ldb_val(mem_ctx, ldb, idx_val);
@@ -310,12 +310,12 @@ TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
 			const struct ldb_message *msg)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	TDB_DATA key;
 	const struct ldb_val *guid_val;
 	int ret;
 
-	if (ltdb->cache->GUID_index_attribute == NULL) {
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
 		return ldb_kv_key_dn(module, mem_ctx, msg->dn);
 	}
 
@@ -324,13 +324,13 @@ TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
 	}
 
 	guid_val = ldb_msg_find_ldb_val(msg,
-				       ltdb->cache->GUID_index_attribute);
+				       ldb_kv->cache->GUID_index_attribute);
 	if (guid_val == NULL) {
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
 				       "Did not find GUID attribute %s "
 				       "in %s, required for TDB record "
 				       "key in " LTDB_IDXGUID " mode.",
-				       ltdb->cache->GUID_index_attribute,
+				       ldb_kv->cache->GUID_index_attribute,
 				       ldb_dn_get_linearized(msg->dn));
 		errno = EINVAL;
 		key.dptr = NULL;
@@ -348,7 +348,7 @@ TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
 	}
 	key.dsize = talloc_get_size(key.dptr);
 
-	ret = ldb_kv_guid_to_key(module, ltdb, guid_val, &key);
+	ret = ldb_kv_guid_to_key(module, ldb_kv, guid_val, &key);
 
 	if (ret != LDB_SUCCESS) {
 		errno = EINVAL;
@@ -399,11 +399,11 @@ static int ldb_kv_check_special_dn(struct ldb_module *module,
 static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
 {
 	int ret = LDB_SUCCESS;
-	struct ltdb_private *ltdb = talloc_get_type(ldb_module_get_private(module), struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
 
 	/* only allow modifies inside a transaction, otherwise the
 	 * ldb is unsafe */
-	if (ltdb->kv_ops->transaction_active(ltdb) == false) {
+	if (ldb_kv->kv_ops->transaction_active(ldb_kv) == false) {
 		ldb_set_errstring(ldb_module_get_ctx(module), "ltdb modify without transaction");
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
@@ -412,10 +412,10 @@ static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
 	    (ldb_dn_check_special(dn, LTDB_INDEXLIST) ||
 	     ldb_dn_check_special(dn, LTDB_ATTRIBUTES)) )
 	{
-		if (ltdb->warn_reindex) {
+		if (ldb_kv->warn_reindex) {
 			ldb_debug(ldb_module_get_ctx(module),
 				LDB_DEBUG_ERROR, "Reindexing %s due to modification on %s",
-				ltdb->kv_ops->name(ltdb), ldb_dn_get_linearized(dn));
+				ldb_kv->kv_ops->name(ldb_kv), ldb_dn_get_linearized(dn));
 		}
 		ret = ldb_kv_reindex(module);
 	}
@@ -435,13 +435,13 @@ static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
 	}
 
 	if (ret != LDB_SUCCESS) {
-		ltdb->reindex_failed = true;
+		ldb_kv->reindex_failed = true;
 	}
 
 	return ret;
 }
 
-static int ltdb_store(struct ltdb_private *ltdb,
+static int ltdb_store(struct ldb_kv_private *ldb_kv,
 		      struct ldb_val ldb_key,
 		      struct ldb_val ldb_data,
 		      int flags)
@@ -454,21 +454,21 @@ static int ltdb_store(struct ltdb_private *ltdb,
 		.dptr = ldb_data.data,
 		.dsize = ldb_data.length
 	};
-	bool transaction_active = tdb_transaction_active(ltdb->tdb);
+	bool transaction_active = tdb_transaction_active(ldb_kv->tdb);
 	if (transaction_active == false){
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
-	return tdb_store(ltdb->tdb, key, data, flags);
+	return tdb_store(ldb_kv->tdb, key, data, flags);
 }
 
-static int ltdb_error(struct ltdb_private *ltdb)
+static int ltdb_error(struct ldb_kv_private *ldb_kv)
 {
-	return ltdb_err_map(tdb_error(ltdb->tdb));
+	return ltdb_err_map(tdb_error(ldb_kv->tdb));
 }
 
-static const char *ltdb_errorstr(struct ltdb_private *ltdb)
+static const char *ltdb_errorstr(struct ldb_kv_private *ldb_kv)
 {
-	return tdb_errorstr(ltdb->tdb);
+	return tdb_errorstr(ldb_kv->tdb);
 }
 
 /*
@@ -479,7 +479,7 @@ int ldb_kv_store(struct ldb_module *module,
 		 int flgs)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	TDB_DATA tdb_key;
 	struct ldb_val ldb_key;
 	struct ldb_val ldb_data;
@@ -490,7 +490,7 @@ int ldb_kv_store(struct ldb_module *module,
 		return ldb_module_oom(module);
 	}
 
-	if (ltdb->read_only) {
+	if (ldb_kv->read_only) {
 		talloc_free(tdb_key_ctx);
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
@@ -511,10 +511,10 @@ int ldb_kv_store(struct ldb_module *module,
 	ldb_key.data = tdb_key.dptr;
 	ldb_key.length = tdb_key.dsize;
 
-	ret = ltdb->kv_ops->store(ltdb, ldb_key, ldb_data, flgs);
+	ret = ldb_kv->kv_ops->store(ldb_kv, ldb_key, ldb_data, flgs);
 	if (ret != 0) {
 		bool is_special = ldb_dn_is_special(msg->dn);
-		ret = ltdb->kv_ops->error(ltdb);
+		ret = ldb_kv->kv_ops->error(ldb_kv);
 
 		/*
 		 * LDB_ERR_ENTRY_ALREADY_EXISTS means the DN, not
@@ -522,7 +522,7 @@ int ldb_kv_store(struct ldb_module *module,
 		 */
 		if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS
 		    && !is_special
-		    && ltdb->cache->GUID_index_attribute != NULL) {
+		    && ldb_kv->cache->GUID_index_attribute != NULL) {
 			ret = LDB_ERR_CONSTRAINT_VIOLATION;
 		}
 		goto done;
@@ -565,7 +565,7 @@ static bool ldb_kv_single_valued(const struct ldb_schema_attribute *a,
 }
 
 static int ldb_kv_add_internal(struct ldb_module *module,
-			       struct ltdb_private *ltdb,
+			       struct ldb_kv_private *ldb_kv,
 			       const struct ldb_message *msg,
 			       bool check_single_value)
 {
@@ -647,7 +647,7 @@ static int ldb_kv_add_internal(struct ldb_module *module,
 		return ret;
 	}
 
-	ret = ldb_kv_index_add_new(module, ltdb, msg);
+	ret = ldb_kv_index_add_new(module, ldb_kv, msg);
 	if (ret != LDB_SUCCESS) {
 		/*
 		 * If we failed to index, delete the message again.
@@ -676,11 +676,11 @@ static int ldb_kv_add(struct ldb_kv_context *ctx)
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	int ret = LDB_SUCCESS;
 
-	if (ltdb->max_key_length != 0 &&
-	    ltdb->cache->GUID_index_attribute == NULL &&
+	if (ldb_kv->max_key_length != 0 &&
+	    ldb_kv->cache->GUID_index_attribute == NULL &&
 	    !ldb_dn_is_special(req->op.add.message->dn))
 	{
 		ldb_set_errstring(ldb_module_get_ctx(module),
@@ -700,22 +700,22 @@ static int ldb_kv_add(struct ldb_kv_context *ctx)
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	ret = ldb_kv_add_internal(module, ltdb, req->op.add.message, true);
+	ret = ldb_kv_add_internal(module, ldb_kv, req->op.add.message, true);
 
 	return ret;
 }
 
-static int ltdb_delete(struct ltdb_private *ltdb, struct ldb_val ldb_key)
+static int ltdb_delete(struct ldb_kv_private *ldb_kv, struct ldb_val ldb_key)
 {
 	TDB_DATA tdb_key = {
 		.dptr = ldb_key.data,
 		.dsize = ldb_key.length
 	};
-	bool transaction_active = tdb_transaction_active(ltdb->tdb);
+	bool transaction_active = tdb_transaction_active(ldb_kv->tdb);
 	if (transaction_active == false){
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
-	return tdb_delete(ltdb->tdb, tdb_key);
+	return tdb_delete(ldb_kv->tdb, tdb_key);
 }
 
 /*
@@ -726,7 +726,7 @@ int ldb_kv_delete_noindex(struct ldb_module *module,
 			  const struct ldb_message *msg)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_val ldb_key;
 	TDB_DATA tdb_key;
 	int ret;
@@ -736,7 +736,7 @@ int ldb_kv_delete_noindex(struct ldb_module *module,
 		return ldb_module_oom(module);
 	}
 
-	if (ltdb->read_only) {
+	if (ldb_kv->read_only) {
 		talloc_free(tdb_key_ctx);
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
@@ -750,11 +750,11 @@ int ldb_kv_delete_noindex(struct ldb_module *module,
 	ldb_key.data = tdb_key.dptr;
 	ldb_key.length = tdb_key.dsize;
 
-	ret = ltdb->kv_ops->delete(ltdb, ldb_key);
+	ret = ldb_kv->kv_ops->delete(ldb_kv, ldb_key);
 	TALLOC_FREE(tdb_key_ctx);
 
 	if (ret != 0) {
-		ret = ltdb->kv_ops->error(ltdb);
+		ret = ldb_kv->kv_ops->error(ldb_kv);
 	}
 
 	return ret;
@@ -890,7 +890,7 @@ static int ldb_kv_msg_add_element(struct ldb_message *msg,
   delete all elements having a specified attribute name
 */
 static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
-				       struct ltdb_private *ltdb,
+				       struct ldb_kv_private *ldb_kv,
 				       struct ldb_message *msg,
 				       const char *name)
 {
@@ -900,12 +900,12 @@ static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
 	bool is_special = ldb_dn_is_special(msg->dn);
 
 	if (!is_special
-	    && ltdb->cache->GUID_index_attribute != NULL
-	    && ldb_attr_cmp(name, ltdb->cache->GUID_index_attribute) == 0) {
+	    && ldb_kv->cache->GUID_index_attribute != NULL
+	    && ldb_attr_cmp(name, ldb_kv->cache->GUID_index_attribute) == 0) {
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
 		ldb_asprintf_errstring(ldb, "Must not modify GUID "
 				       "attribute %s (used as DB index)",
-				       ltdb->cache->GUID_index_attribute);
+				       ldb_kv->cache->GUID_index_attribute);
 		return LDB_ERR_CONSTRAINT_VIOLATION;
 	}
 
@@ -915,7 +915,7 @@ static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
 	}
 	i = el - msg->elements;
 
-	ret = ldb_kv_index_del_element(module, ltdb, msg, el);
+	ret = ldb_kv_index_del_element(module, ldb_kv, msg, el);
 	if (ret != LDB_SUCCESS) {
 		return ret;
 	}
@@ -937,7 +937,7 @@ static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
   return LDB Error on failure
 */
 static int ldb_kv_msg_delete_element(struct ldb_module *module,
-				     struct ltdb_private *ltdb,
+				     struct ldb_kv_private *ldb_kv,
 				     struct ldb_message *msg,
 				     const char *name,
 				     const struct ldb_val *val)
@@ -971,10 +971,10 @@ static int ldb_kv_msg_delete_element(struct ldb_module *module,
 		if (matched) {
 			if (el->num_values == 1) {
 				return ldb_kv_msg_delete_attribute(
-				    module, ltdb, msg, name);
+				    module, ldb_kv, msg, name);
 			}
 
-			ret = ldb_kv_index_del_value(module, ltdb, msg, el, i);
+			ret = ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
 			if (ret != LDB_SUCCESS) {
 				return ret;
 			}
@@ -1011,7 +1011,7 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_message *msg2;
 	unsigned int i, j;
 	int ret = LDB_SUCCESS, idx;
@@ -1097,7 +1097,7 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 					goto done;
 				}
 				ret = ldb_kv_index_add_element(
-				    module, ltdb, msg2, el);
+				    module, ldb_kv, msg2, el);
 				if (ret != LDB_SUCCESS) {
 					goto done;
 				}
@@ -1178,7 +1178,7 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 				el2->num_values += el->num_values;
 
 				ret = ldb_kv_index_add_element(
-				    module, ltdb, msg2, el);
+				    module, ldb_kv, msg2, el);
 				if (ret != LDB_SUCCESS) {
 					goto done;
 				}
@@ -1243,7 +1243,7 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 
 				/* Delete the attribute if it exists in the DB */
 				if (ldb_kv_msg_delete_attribute(
-					module, ltdb, msg2, el->name) != 0) {
+					module, ldb_kv, msg2, el->name) != 0) {
 					ret = LDB_ERR_OTHER;
 					goto done;
 				}
@@ -1255,7 +1255,7 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 				goto done;
 			}
 
-			ret = ldb_kv_index_add_element(module, ltdb, msg2, el);
+			ret = ldb_kv_index_add_element(module, ldb_kv, msg2, el);
 			if (ret != LDB_SUCCESS) {
 				goto done;
 			}
@@ -1272,7 +1272,7 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 			if (msg->elements[i].num_values == 0) {
 				/* Delete the whole attribute */
 				ret = ldb_kv_msg_delete_attribute(
-				    module, ltdb, msg2, msg->elements[i].name);
+				    module, ldb_kv, msg2, msg->elements[i].name);
 				if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
 				    control_permissive) {
 					ret = LDB_SUCCESS;
@@ -1289,7 +1289,7 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 				for (j=0; j < msg->elements[i].num_values; j++) {
 					ret = ldb_kv_msg_delete_element(
 					    module,
-					    ltdb,
+					    ldb_kv,
 					    msg2,
 					    msg->elements[i].name,
 					    &msg->elements[i].values[j]);
@@ -1364,7 +1364,7 @@ static int ldb_kv_rename(struct ldb_kv_context *ctx)
 {
 	struct ldb_module *module = ctx->module;
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_request *req = ctx->req;
 	struct ldb_message *msg;
 	int ret = LDB_SUCCESS;
@@ -1464,110 +1464,110 @@ static int ldb_kv_rename(struct ldb_kv_context *ctx)
 	 * deleted attributes. We could go through all elements but that's
 	 * maybe not the most efficient way
 	 */
-	ret = ldb_kv_add_internal(module, ltdb, msg, false);
+	ret = ldb_kv_add_internal(module, ldb_kv, msg, false);
 
 	talloc_free(msg);
 
 	return ret;
 }
 
-static int ltdb_transaction_start(struct ltdb_private *ltdb)
+static int ltdb_transaction_start(struct ldb_kv_private *ldb_kv)
 {
 	pid_t pid = getpid();
 
-	if (ltdb->pid != pid) {
+	if (ldb_kv->pid != pid) {
 		ldb_asprintf_errstring(
-			ldb_module_get_ctx(ltdb->module),
+			ldb_module_get_ctx(ldb_kv->module),
 			__location__": Reusing ldb opend by pid %d in "
 			"process %d\n",
-			ltdb->pid,
+			ldb_kv->pid,
 			pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	return tdb_transaction_start(ltdb->tdb);
+	return tdb_transaction_start(ldb_kv->tdb);
 }
 
-static int ltdb_transaction_cancel(struct ltdb_private *ltdb)
+static int ltdb_transaction_cancel(struct ldb_kv_private *ldb_kv)
 {
 	pid_t pid = getpid();
 
-	if (ltdb->pid != pid) {
+	if (ldb_kv->pid != pid) {
 		ldb_asprintf_errstring(
-			ldb_module_get_ctx(ltdb->module),
+			ldb_module_get_ctx(ldb_kv->module),
 			__location__": Reusing ldb opend by pid %d in "
 			"process %d\n",
-			ltdb->pid,
+			ldb_kv->pid,
 			pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	return tdb_transaction_cancel(ltdb->tdb);
+	return tdb_transaction_cancel(ldb_kv->tdb);
 }
 
-static int ltdb_transaction_prepare_commit(struct ltdb_private *ltdb)
+static int ltdb_transaction_prepare_commit(struct ldb_kv_private *ldb_kv)
 {
 	pid_t pid = getpid();
 
-	if (ltdb->pid != pid) {
+	if (ldb_kv->pid != pid) {
 		ldb_asprintf_errstring(
-			ldb_module_get_ctx(ltdb->module),
+			ldb_module_get_ctx(ldb_kv->module),
 			__location__": Reusing ldb opend by pid %d in "
 			"process %d\n",
-			ltdb->pid,
+			ldb_kv->pid,
 			pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	return tdb_transaction_prepare_commit(ltdb->tdb);
+	return tdb_transaction_prepare_commit(ldb_kv->tdb);
 }
 
-static int ltdb_transaction_commit(struct ltdb_private *ltdb)
+static int ltdb_transaction_commit(struct ldb_kv_private *ldb_kv)
 {
 	pid_t pid = getpid();
 
-	if (ltdb->pid != pid) {
+	if (ldb_kv->pid != pid) {
 		ldb_asprintf_errstring(
-			ldb_module_get_ctx(ltdb->module),
+			ldb_module_get_ctx(ldb_kv->module),
 			__location__": Reusing ldb opend by pid %d in "
 			"process %d\n",
-			ltdb->pid,
+			ldb_kv->pid,
 			pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	return tdb_transaction_commit(ltdb->tdb);
+	return tdb_transaction_commit(ldb_kv->tdb);
 }
 
 static int ldb_kv_start_trans(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 
 	pid_t pid = getpid();
 
-	if (ltdb->pid != pid) {
+	if (ldb_kv->pid != pid) {
 		ldb_asprintf_errstring(
-			ldb_module_get_ctx(ltdb->module),
+			ldb_module_get_ctx(ldb_kv->module),
 			__location__": Reusing ldb opend by pid %d in "
 			"process %d\n",
-			ltdb->pid,
+			ldb_kv->pid,
 			pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
 	/* Do not take out the transaction lock on a read-only DB */
-	if (ltdb->read_only) {
+	if (ldb_kv->read_only) {
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
-	if (ltdb->kv_ops->begin_write(ltdb) != 0) {
-		return ltdb->kv_ops->error(ltdb);
+	if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
+		return ldb_kv->kv_ops->error(ldb_kv);
 	}
 
 	ldb_kv_index_transaction_start(module);
 
-	ltdb->reindex_failed = false;
+	ldb_kv->reindex_failed = false;
 
 	return LDB_SUCCESS;
 }
@@ -1582,20 +1582,20 @@ static int ldb_kv_prepare_commit(struct ldb_module *module)
 {
 	int ret;
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	pid_t pid = getpid();
 
-	if (ltdb->pid != pid) {
+	if (ldb_kv->pid != pid) {
 		ldb_asprintf_errstring(
 			ldb_module_get_ctx(module),
 			__location__": Reusing ldb opend by pid %d in "
 			"process %d\n",
-			ltdb->pid,
+			ldb_kv->pid,
 			pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	if (!ltdb->kv_ops->transaction_active(ltdb)) {
+	if (!ldb_kv->kv_ops->transaction_active(ldb_kv)) {
 		ldb_set_errstring(ldb_module_get_ctx(module),
 				  "ltdb_prepare_commit() called "
 				  "without transaction active");
@@ -1608,7 +1608,7 @@ static int ldb_kv_prepare_commit(struct ldb_module *module)
 	 * This can happen if for example a duplicate value was marked
 	 * unique.  We must not write a partial re-index into the DB.
 	 */
-	if (ltdb->reindex_failed) {
+	if (ldb_kv->reindex_failed) {
 		/*
 		 * We must instead abort the transaction so we get the
 		 * old values and old index back
@@ -1622,22 +1622,22 @@ static int ldb_kv_prepare_commit(struct ldb_module *module)
 
 	ret = ldb_kv_index_transaction_commit(module);
 	if (ret != LDB_SUCCESS) {
-		ltdb->kv_ops->abort_write(ltdb);
+		ldb_kv->kv_ops->abort_write(ldb_kv);
 		return ret;
 	}
 
-	if (ltdb->kv_ops->prepare_write(ltdb) != 0) {
-		ret = ltdb->kv_ops->error(ltdb);
+	if (ldb_kv->kv_ops->prepare_write(ldb_kv) != 0) {
+		ret = ldb_kv->kv_ops->error(ldb_kv);
 		ldb_debug_set(ldb_module_get_ctx(module),
 			      LDB_DEBUG_FATAL,
 			      "Failure during "
 			      "prepare_write): %s -> %s",
-			      ltdb->kv_ops->errorstr(ltdb),
+			      ldb_kv->kv_ops->errorstr(ldb_kv),
 			      ldb_strerror(ret));
 		return ret;
 	}
 
-	ltdb->prepared_commit = true;
+	ldb_kv->prepared_commit = true;
 
 	return LDB_SUCCESS;
 }
@@ -1646,22 +1646,22 @@ static int ldb_kv_end_trans(struct ldb_module *module)
 {
 	int ret;
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 
-	if (!ltdb->prepared_commit) {
+	if (!ldb_kv->prepared_commit) {
 		ret = ldb_kv_prepare_commit(module);
 		if (ret != LDB_SUCCESS) {
 			return ret;
 		}
 	}
 
-	ltdb->prepared_commit = false;
+	ldb_kv->prepared_commit = false;
 
-	if (ltdb->kv_ops->finish_write(ltdb) != 0) {
-		ret = ltdb->kv_ops->error(ltdb);
+	if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
+		ret = ldb_kv->kv_ops->error(ldb_kv);
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
 				       "Failure during tdb_transaction_commit(): %s -> %s",
-				       ltdb->kv_ops->errorstr(ltdb),
+				       ldb_kv->kv_ops->errorstr(ldb_kv),
 				       ldb_strerror(ret));
 		return ret;
 	}
@@ -1672,14 +1672,14 @@ static int ldb_kv_end_trans(struct ldb_module *module)
 static int ldb_kv_del_trans(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 
 	if (ldb_kv_index_transaction_cancel(module) != 0) {
-		ltdb->kv_ops->abort_write(ltdb);
-		return ltdb->kv_ops->error(ltdb);
+		ldb_kv->kv_ops->abort_write(ldb_kv);
+		return ldb_kv->kv_ops->error(ldb_kv);
 	}
 
-	ltdb->kv_ops->abort_write(ltdb);
+	ldb_kv->kv_ops->abort_write(ldb_kv);
 	return LDB_SUCCESS;
 }
 
@@ -1693,7 +1693,7 @@ static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
 	TALLOC_CTX *tmp_ctx = NULL;
 	struct ldb_seqnum_request *seq;
 	struct ldb_seqnum_result *res;
@@ -1712,7 +1712,7 @@ static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
 
 	ldb_request_set_state(req, LDB_ASYNC_PENDING);
 
-	if (ltdb->kv_ops->lock_read(module) != 0) {
+	if (ldb_kv->kv_ops->lock_read(module) != 0) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
@@ -1775,7 +1775,7 @@ static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
 done:
 	talloc_free(tmp_ctx);
 
-	ltdb->kv_ops->unlock_read(module);
+	ldb_kv->kv_ops->unlock_read(module);
 	return ret;
 }
 
@@ -1875,7 +1875,7 @@ static void ldb_kv_handle_extended(struct ldb_kv_context *ctx)
 struct kv_ctx {
 	ldb_kv_traverse_fn kv_traverse_fn;
 	void *ctx;
-	struct ltdb_private *ltdb;
+	struct ldb_kv_private *ldb_kv;
 	int (*parser)(struct ldb_val key,
 		      struct ldb_val data,
 		      void *private_data);
@@ -1895,28 +1895,28 @@ static int ltdb_traverse_fn_wrapper(struct tdb_context *tdb,
 		.length = tdb_data.dsize,
 		.data = tdb_data.dptr,
 	};
-	return kv_ctx->kv_traverse_fn(kv_ctx->ltdb, key, data, kv_ctx->ctx);
+	return kv_ctx->kv_traverse_fn(kv_ctx->ldb_kv, key, data, kv_ctx->ctx);
 }
 
-static int ltdb_traverse_fn(struct ltdb_private *ltdb,
+static int ltdb_traverse_fn(struct ldb_kv_private *ldb_kv,
 			    ldb_kv_traverse_fn fn,
 			    void *ctx)
 {
 	struct kv_ctx kv_ctx = {
 		.kv_traverse_fn = fn,
 		.ctx = ctx,
-		.ltdb = ltdb
+		.ldb_kv = ldb_kv
 	};
-	if (tdb_transaction_active(ltdb->tdb)) {
+	if (tdb_transaction_active(ldb_kv->tdb)) {
 		return tdb_traverse(
-		    ltdb->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
+		    ldb_kv->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
 	} else {
 		return tdb_traverse_read(
-		    ltdb->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
+		    ldb_kv->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
 	}
 }
 
-static int ltdb_update_in_iterate(struct ltdb_private *ltdb,
+static int ltdb_update_in_iterate(struct ldb_kv_private *ldb_kv,
 				  struct ldb_val ldb_key,
 				  struct ldb_val ldb_key2,
 				  struct ldb_val ldb_data,
@@ -1942,7 +1942,7 @@ static int ltdb_update_in_iterate(struct ltdb_private *ltdb,
 
 	ldb = ldb_module_get_ctx(module);
 
-	tdb_ret = tdb_delete(ltdb->tdb, key);
+	tdb_ret = tdb_delete(ldb_kv->tdb, key);
 	if (tdb_ret != 0) {
 		ldb_debug(ldb, LDB_DEBUG_ERROR,
 			  "Failed to delete %*.*s "
@@ -1951,11 +1951,11 @@ static int ltdb_update_in_iterate(struct ltdb_private *ltdb,
 			  (const char *)key.dptr,
 			  (int)key2.dsize, (int)key2.dsize,
 			  (const char *)key.dptr,
-			  tdb_errorstr(ltdb->tdb));
-		ctx->error = ltdb_err_map(tdb_error(ltdb->tdb));
+			  tdb_errorstr(ldb_kv->tdb));
+		ctx->error = ltdb_err_map(tdb_error(ldb_kv->tdb));
 		return -1;
 	}
-	tdb_ret = tdb_store(ltdb->tdb, key2, data, 0);
+	tdb_ret = tdb_store(ldb_kv->tdb, key2, data, 0);
 	if (tdb_ret != 0) {
 		ldb_debug(ldb, LDB_DEBUG_ERROR,
 			  "Failed to rekey %*.*s as %*.*s: %s",
@@ -1963,8 +1963,8 @@ static int ltdb_update_in_iterate(struct ltdb_private *ltdb,
 			  (const char *)key.dptr,
 			  (int)key2.dsize, (int)key2.dsize,
 			  (const char *)key.dptr,
-			  tdb_errorstr(ltdb->tdb));
-		ctx->error = ltdb_err_map(tdb_error(ltdb->tdb));
+			  tdb_errorstr(ldb_kv->tdb));
+		ctx->error = ltdb_err_map(tdb_error(ldb_kv->tdb));
 		return -1;
 	}
 	return tdb_ret;
@@ -1987,7 +1987,7 @@ static int ltdb_parse_record_wrapper(TDB_DATA tdb_key,
 	return kv_ctx->parser(key, data, kv_ctx->ctx);
 }
 
-static int ltdb_parse_record(struct ltdb_private *ltdb,
+static int ltdb_parse_record(struct ldb_kv_private *ldb_kv,
 			     struct ldb_val ldb_key,
 			     int (*parser)(struct ldb_val key,
 					   struct ldb_val data,
@@ -1997,7 +1997,7 @@ static int ltdb_parse_record(struct ltdb_private *ltdb,
 	struct kv_ctx kv_ctx = {
 		.parser = parser,
 		.ctx = ctx,
-		.ltdb = ltdb
+		.ldb_kv = ldb_kv
 	};
 	TDB_DATA key = {
 		.dptr = ldb_key.data,
@@ -2005,37 +2005,37 @@ static int ltdb_parse_record(struct ltdb_private *ltdb,
 	};
 	int ret;
 
-	if (tdb_transaction_active(ltdb->tdb) == false &&
-	    ltdb->read_lock_count == 0) {
+	if (tdb_transaction_active(ldb_kv->tdb) == false &&
+	    ldb_kv->read_lock_count == 0) {
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
 	ret = tdb_parse_record(
-	    ltdb->tdb, key, ltdb_parse_record_wrapper, &kv_ctx);
+	    ldb_kv->tdb, key, ltdb_parse_record_wrapper, &kv_ctx);
 	if (ret == 0) {
 		return LDB_SUCCESS;
 	}
-	return ltdb_err_map(tdb_error(ltdb->tdb));
+	return ltdb_err_map(tdb_error(ldb_kv->tdb));
 }
 
-static const char *ltdb_name(struct ltdb_private *ltdb)
+static const char *ltdb_name(struct ldb_kv_private *ldb_kv)
 {
-	return tdb_name(ltdb->tdb);
+	return tdb_name(ldb_kv->tdb);
 }
 
-static bool ltdb_changed(struct ltdb_private *ltdb)
+static bool ltdb_changed(struct ldb_kv_private *ldb_kv)
 {
-	int seq = tdb_get_seqnum(ltdb->tdb);
-	bool has_changed = (seq != ltdb->tdb_seqnum);
+	int seq = tdb_get_seqnum(ldb_kv->tdb);
+	bool has_changed = (seq != ldb_kv->tdb_seqnum);
 
-	ltdb->tdb_seqnum = seq;
+	ldb_kv->tdb_seqnum = seq;
 
 	return has_changed;
 }
 
-static bool ltdb_transaction_active(struct ltdb_private *ltdb)
+static bool ltdb_transaction_active(struct ldb_kv_private *ldb_kv)
 {
-	return tdb_transaction_active(ltdb->tdb);
+	return tdb_transaction_active(ldb_kv->tdb);
 }
 
 static const struct kv_db_ops key_value_ops = {
@@ -2209,15 +2209,15 @@ static int ldb_kv_init_rootdse(struct ldb_module *module)
 static int ldb_kv_lock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
-	return ltdb->kv_ops->lock_read(module);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	return ldb_kv->kv_ops->lock_read(module);
 }
 
 static int ldb_kv_unlock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ltdb_private *ltdb = talloc_get_type(data, struct ltdb_private);
-	return ltdb->kv_ops->unlock_read(module);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	return ldb_kv->kv_ops->unlock_read(module);
 }
 
 static const struct ldb_module_ops ldb_kv_ops = {
@@ -2237,41 +2237,41 @@ static const struct ldb_module_ops ldb_kv_ops = {
     .read_unlock = ldb_kv_unlock_read,
 };
 
-int ldb_kv_init_store(struct ltdb_private *ltdb,
+int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
 		      const char *name,
 		      struct ldb_context *ldb,
 		      const char *options[],
 		      struct ldb_module **_module)
 {
 	if (getenv("LDB_WARN_UNINDEXED")) {
-		ltdb->warn_unindexed = true;
+		ldb_kv->warn_unindexed = true;
 	}
 
 	if (getenv("LDB_WARN_REINDEX")) {
-		ltdb->warn_reindex = true;
+		ldb_kv->warn_reindex = true;
 	}
 
-	ltdb->sequence_number = 0;
+	ldb_kv->sequence_number = 0;
 
-	ltdb->pid = getpid();
+	ldb_kv->pid = getpid();
 
-	ltdb->module = ldb_module_new(ldb, ldb, name, &ldb_kv_ops);
-	if (!ltdb->module) {
+	ldb_kv->module = ldb_module_new(ldb, ldb, name, &ldb_kv_ops);
+	if (!ldb_kv->module) {
 		ldb_oom(ldb);
-		talloc_free(ltdb);
+		talloc_free(ldb_kv);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
-	ldb_module_set_private(ltdb->module, ltdb);
-	talloc_steal(ltdb->module, ltdb);
+	ldb_module_set_private(ldb_kv->module, ldb_kv);
+	talloc_steal(ldb_kv->module, ldb_kv);
 
-	if (ldb_kv_cache_load(ltdb->module) != 0) {
+	if (ldb_kv_cache_load(ldb_kv->module) != 0) {
 		ldb_asprintf_errstring(ldb, "Unable to load ltdb cache "
 				       "records for backend '%s'", name);
-		talloc_free(ltdb->module);
+		talloc_free(ldb_kv->module);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	*_module = ltdb->module;
+	*_module = ldb_kv->module;
 	/*
 	 * Set or override the maximum key length
 	 *
@@ -2287,7 +2287,7 @@ int ldb_kv_init_store(struct ltdb_private *ltdb,
 					 "max_key_len_for_self_test");
 		if (len_str != NULL) {
 			unsigned len = strtoul(len_str, NULL, 0);
-			ltdb->max_key_length = len;
+			ldb_kv->max_key_length = len;
 		}
 	}
 
@@ -2303,7 +2303,7 @@ int ldb_kv_init_store(struct ltdb_private *ltdb,
 			ldb_options_find(ldb, options,
 					 "disable_full_db_scan_for_self_test");
 		if (len_str != NULL) {
-			ltdb->disable_full_db_scan = true;
+			ldb_kv->disable_full_db_scan = true;
 		}
 	}
 
@@ -2319,7 +2319,7 @@ int ltdb_connect(struct ldb_context *ldb, const char *url,
 {
 	const char *path;
 	int tdb_flags, open_flags;
-	struct ltdb_private *ltdb;
+	struct ldb_kv_private *ldb_kv;
 
 	/*
 	 * We hold locks, so we must use a private event context
@@ -2351,8 +2351,8 @@ int ltdb_connect(struct ldb_context *ldb, const char *url,
 		tdb_flags |= TDB_NOMMAP;
 	}
 
-	ltdb = talloc_zero(ldb, struct ltdb_private);
-	if (!ltdb) {
+	ldb_kv = talloc_zero(ldb, struct ldb_kv_private);
+	if (!ldb_kv) {
 		ldb_oom(ldb);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
@@ -2366,7 +2366,7 @@ int ltdb_connect(struct ldb_context *ldb, const char *url,
 		 */
 		open_flags = O_RDWR;
 
-		ltdb->read_only = true;
+		ldb_kv->read_only = true;
 
 	} else if (flags & LDB_FLG_DONT_CREATE_DB) {
 		/*
@@ -2381,19 +2381,19 @@ int ltdb_connect(struct ldb_context *ldb, const char *url,
 		open_flags = O_CREAT | O_RDWR;
 	}
 
-	ltdb->kv_ops = &key_value_ops;
+	ldb_kv->kv_ops = &key_value_ops;
 
 	errno = 0;
 	/* note that we use quite a large default hash size */
-	ltdb->tdb = ltdb_wrap_open(ltdb, path, 10000,
+	ldb_kv->tdb = ltdb_wrap_open(ldb_kv, path, 10000,
 				   tdb_flags, open_flags,
 				   ldb_get_create_perms(ldb), ldb);
-	if (!ltdb->tdb) {
+	if (!ldb_kv->tdb) {
 		ldb_asprintf_errstring(ldb,
 				       "Unable to open tdb '%s': %s", path, strerror(errno));
 		ldb_debug(ldb, LDB_DEBUG_ERROR,
 			  "Unable to open tdb '%s': %s", path, strerror(errno));
-		talloc_free(ltdb);
+		talloc_free(ldb_kv);
 		if (errno == EACCES || errno == EPERM) {
 			return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
 		}
@@ -2401,5 +2401,5 @@ int ltdb_connect(struct ldb_context *ldb, const char *url,
 	}
 
 	return ldb_kv_init_store(
-	    ltdb, "ldb_tdb backend", ldb, options, _module);
+	    ldb_kv, "ldb_tdb backend", ldb, options, _module);
 }
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.h b/lib/ldb/ldb_tdb/ldb_tdb.h
index 9a9471e..930405d 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -4,37 +4,37 @@
 #include "tdb.h"
 #include "ldb_module.h"
 
-struct ltdb_private;
-typedef int (*ldb_kv_traverse_fn)(struct ltdb_private *ltdb,
+struct ldb_kv_private;
+typedef int (*ldb_kv_traverse_fn)(struct ldb_kv_private *ldb_kv,
 				  struct ldb_val key, struct ldb_val data,
 				  void *ctx);
 
 struct kv_db_ops {
-	int (*store)(struct ltdb_private *ltdb, struct ldb_val key, struct ldb_val data, int flags);
-	int (*delete)(struct ltdb_private *ltdb, struct ldb_val key);
-	int (*iterate)(struct ltdb_private *ltdb, ldb_kv_traverse_fn fn, void *ctx);
-	int (*update_in_iterate)(struct ltdb_private *ltdb, struct ldb_val key,
+	int (*store)(struct ldb_kv_private *ldb_kv, struct ldb_val key, struct ldb_val data, int flags);
+	int (*delete)(struct ldb_kv_private *ldb_kv, struct ldb_val key);
+	int (*iterate)(struct ldb_kv_private *ldb_kv, ldb_kv_traverse_fn fn, void *ctx);
+	int (*update_in_iterate)(struct ldb_kv_private *ldb_kv, struct ldb_val key,
 				 struct ldb_val key2, struct ldb_val data, void *ctx);
-	int (*fetch_and_parse)(struct ltdb_private *ltdb, struct ldb_val key,
+	int (*fetch_and_parse)(struct ldb_kv_private *ldb_kv, struct ldb_val key,
                                int (*parser)(struct ldb_val key, struct ldb_val data,
                                              void *private_data),
                                void *ctx);
 	int (*lock_read)(struct ldb_module *);
 	int (*unlock_read)(struct ldb_module *);
-	int (*begin_write)(struct ltdb_private *);
-	int (*prepare_write)(struct ltdb_private *);
-	int (*abort_write)(struct ltdb_private *);
-	int (*finish_write)(struct ltdb_private *);
-	int (*error)(struct ltdb_private *ltdb);
-	const char * (*errorstr)(struct ltdb_private *ltdb);
-	const char * (*name)(struct ltdb_private *ltdb);
-	bool (*has_changed)(struct ltdb_private *ltdb);
-	bool (*transaction_active)(struct ltdb_private *ltdb);
+	int (*begin_write)(struct ldb_kv_private *);
+	int (*prepare_write)(struct ldb_kv_private *);
+	int (*abort_write)(struct ldb_kv_private *);
+	int (*finish_write)(struct ldb_kv_private *);
+	int (*error)(struct ldb_kv_private *ldb_kv);
+	const char * (*errorstr)(struct ldb_kv_private *ldb_kv);
+	const char * (*name)(struct ldb_kv_private *ldb_kv);
+	bool (*has_changed)(struct ldb_kv_private *ldb_kv);
+	bool (*transaction_active)(struct ldb_kv_private *ldb_kv);
 };
 
-/* this private structure is used by the ltdb backend in the
+/* this private structure is used by the key value backends in the
    ldb_context */
-struct ltdb_private {
+struct ldb_kv_private {
 	const struct kv_db_ops *kv_ops;
 	struct ldb_module *module;
 	TDB_CONTEXT *tdb;
@@ -164,20 +164,20 @@ struct ldb_parse_tree;
 
 int ldb_kv_search_indexed(struct ldb_kv_context *ctx, uint32_t *);
 int ldb_kv_index_add_new(struct ldb_module *module,
-			 struct ltdb_private *ltdb,
+			 struct ldb_kv_private *ldb_kv,
 			 const struct ldb_message *msg);
 int ldb_kv_index_delete(struct ldb_module *module,
 			const struct ldb_message *msg);
 int ldb_kv_index_del_element(struct ldb_module *module,
-			     struct ltdb_private *ltdb,
+			     struct ldb_kv_private *ldb_kv,
 			     const struct ldb_message *msg,
 			     struct ldb_message_element *el);
 int ldb_kv_index_add_element(struct ldb_module *module,
-			     struct ltdb_private *ltdb,
+			     struct ldb_kv_private *ldb_kv,
 			     const struct ldb_message *msg,
 			     struct ldb_message_element *el);
 int ldb_kv_index_del_value(struct ldb_module *module,
-			   struct ltdb_private *ltdb,
+			   struct ldb_kv_private *ldb_kv,
 			   const struct ldb_message *msg,
 			   struct ldb_message_element *el,
 			   unsigned int v_idx);
@@ -186,7 +186,7 @@ int ldb_kv_index_transaction_start(struct ldb_module *module);
 int ldb_kv_index_transaction_commit(struct ldb_module *module);
 int ldb_kv_index_transaction_cancel(struct ldb_module *module);
 int ldb_kv_key_dn_from_idx(struct ldb_module *module,
-			   struct ltdb_private *ltdb,
+			   struct ldb_kv_private *ldb_kv,
 			   TALLOC_CTX *mem_ctx,
 			   struct ldb_dn *dn,
 			   TDB_DATA *tdb_key);
@@ -205,7 +205,7 @@ int ldb_kv_search_base(struct ldb_module *module,
 		       struct ldb_dn *dn,
 		       struct ldb_dn **ret_dn);
 int ldb_kv_search_key(struct ldb_module *module,
-		      struct ltdb_private *ltdb,
+		      struct ldb_kv_private *ldb_kv,
 		      struct TDB_DATA tdb_key,
 		      struct ldb_message *msg,
 		      unsigned int unpack_flags);
@@ -228,11 +228,11 @@ TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
 			TALLOC_CTX *mem_ctx,
 			const struct ldb_message *msg);
 int ldb_kv_guid_to_key(struct ldb_module *module,
-		       struct ltdb_private *ltdb,
+		       struct ldb_kv_private *ldb_kv,
 		       const struct ldb_val *GUID_val,
 		       TDB_DATA *key);
 int ldb_kv_idx_to_key(struct ldb_module *module,
-		      struct ltdb_private *ltdb,
+		      struct ldb_kv_private *ldb_kv,
 		      TALLOC_CTX *mem_ctx,
 		      const struct ldb_val *idx_val,
 		      TDB_DATA *key);
@@ -251,7 +251,7 @@ struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
 				   const char *path, int hash_size, int tdb_flags,
 				   int open_flags, mode_t mode,
 				   struct ldb_context *ldb);
-int ldb_kv_init_store(struct ltdb_private *ltdb,
+int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
 		      const char *name,
 		      struct ldb_context *ldb,
 		      const char *options[],
diff --git a/lib/ldb/tests/ldb_kv_ops_test.c b/lib/ldb/tests/ldb_kv_ops_test.c
index 30ce019..74aaf03 100644
--- a/lib/ldb/tests/ldb_kv_ops_test.c
+++ b/lib/ldb/tests/ldb_kv_ops_test.c
@@ -171,18 +171,18 @@ static int teardown(void **state)
 	return 0;
 }
 
-static struct ltdb_private *get_ltdb(struct ldb_context *ldb)
+static struct ldb_kv_private *get_ldb_kv(struct ldb_context *ldb)
 {
 	void *data = NULL;
-	struct ltdb_private *ltdb = NULL;
+	struct ldb_kv_private *ldb_kv = NULL;
 
 	data = ldb_module_get_private(ldb->modules);
 	assert_non_null(data);
 
-	ltdb = talloc_get_type(data, struct ltdb_private);
-	assert_non_null(ltdb);
+	ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	assert_non_null(ldb_kv);
 
-	return ltdb;
+	return ldb_kv;
 }
 
 static int parse(struct ldb_val key,
@@ -209,7 +209,7 @@ static void test_add_get(void **state)
 	int ret;
 	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
 							  struct test_ctx);
-	struct ltdb_private *ltdb = get_ltdb(test_ctx->ldb);
+	struct ldb_kv_private *ldb_kv = get_ldb_kv(test_ctx->ldb);
 	uint8_t key_val[] = "TheKey";
 	struct ldb_val key = {
 		.data   = key_val,
@@ -233,34 +233,34 @@ static void test_add_get(void **state)
 	/*
 	 * Begin a transaction
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Write the record
 	 */
-	ret = ltdb->kv_ops->store(ltdb, key, data, flags);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, data, flags);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Commit the transaction
 	 */
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * And now read it back
 	 */
-	ret = ltdb->kv_ops->lock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->lock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
 
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &read);
+	ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &read);
 	assert_int_equal(ret, 0);
 
 	assert_int_equal(sizeof(value), read.length);
 	assert_memory_equal(value, read.data, sizeof(value));
 
-	ret = ltdb->kv_ops->unlock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->unlock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
 	talloc_free(tmp_ctx);
 }
@@ -273,7 +273,7 @@ static void test_read_outside_transaction(void **state)
 	int ret;
 	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
 							  struct test_ctx);
-	struct ltdb_private *ltdb = get_ltdb(test_ctx->ldb);
+	struct ldb_kv_private *ldb_kv = get_ldb_kv(test_ctx->ldb);
 	uint8_t key_val[] = "TheKey";
 	struct ldb_val key = {
 		.data   = key_val,
@@ -297,26 +297,26 @@ static void test_read_outside_transaction(void **state)
 	/*
 	 * Begin a transaction
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Write the record
 	 */
-	ret = ltdb->kv_ops->store(ltdb, key, data, flags);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, data, flags);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Commit the transaction
 	 */
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * And now read it back
 	 * Note there is no read transaction active
 	 */
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &read);
+	ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &read);
 	assert_int_equal(ret, LDB_ERR_PROTOCOL_ERROR);
 
 	talloc_free(tmp_ctx);
@@ -330,7 +330,7 @@ static void test_delete(void **state)
 	int ret;
 	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
 							  struct test_ctx);
-	struct ltdb_private *ltdb = get_ltdb(test_ctx->ldb);
+	struct ldb_kv_private *ldb_kv = get_ldb_kv(test_ctx->ldb);
 	uint8_t key_val[] = "TheKey";
 	struct ldb_val key = {
 		.data   = key_val,
@@ -354,59 +354,59 @@ static void test_delete(void **state)
 	/*
 	 * Begin a transaction
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Write the record
 	 */
-	ret = ltdb->kv_ops->store(ltdb, key, data, flags);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, data, flags);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Commit the transaction
 	 */
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * And now read it back
 	 */
-	ret = ltdb->kv_ops->lock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->lock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &read);
+	ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &read);
 	assert_int_equal(ret, 0);
 	assert_int_equal(sizeof(value), read.length);
 	assert_memory_equal(value, read.data, sizeof(value));
-	ret = ltdb->kv_ops->unlock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->unlock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Begin a transaction
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Now delete it.
 	 */
-	ret = ltdb->kv_ops->delete(ltdb, key);
+	ret = ldb_kv->kv_ops->delete(ldb_kv, key);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Commit the transaction
 	 */
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * And now try to read it back
 	 */
-	ret = ltdb->kv_ops->lock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->lock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &read);
+	ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &read);
 	assert_int_equal(ret, LDB_ERR_NO_SUCH_OBJECT);
-	ret = ltdb->kv_ops->unlock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->unlock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
 
 	talloc_free(tmp_ctx);
@@ -421,7 +421,7 @@ static void test_transaction_abort_write(void **state)
 	int ret;
 	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
 							  struct test_ctx);
-	struct ltdb_private *ltdb = get_ltdb(test_ctx->ldb);
+	struct ldb_kv_private *ldb_kv = get_ldb_kv(test_ctx->ldb);
 	uint8_t key_val[] = "TheKey";
 	struct ldb_val key = {
 		.data   = key_val,
@@ -445,19 +445,19 @@ static void test_transaction_abort_write(void **state)
 	/*
 	 * Begin a transaction
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Write the record
 	 */
-	ret = ltdb->kv_ops->store(ltdb, key, data, flags);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, data, flags);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * And now read it back
 	 */
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &read);
+	ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &read);
 	assert_int_equal(ret, 0);
 	assert_int_equal(sizeof(value), read.length);
 	assert_memory_equal(value, read.data, sizeof(value));
@@ -466,17 +466,17 @@ static void test_transaction_abort_write(void **state)
 	/*
 	 * Now abort the transaction
 	 */
-	ret = ltdb->kv_ops->abort_write(ltdb);
+	ret = ldb_kv->kv_ops->abort_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * And now read it back, should not be there
 	 */
-	ret = ltdb->kv_ops->lock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->lock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &read);
+	ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &read);
 	assert_int_equal(ret, LDB_ERR_NO_SUCH_OBJECT);
-	ret = ltdb->kv_ops->unlock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->unlock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
 
 	talloc_free(tmp_ctx);
@@ -491,7 +491,7 @@ static void test_transaction_abort_delete(void **state)
 	int ret;
 	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
 							  struct test_ctx);
-	struct ltdb_private *ltdb = get_ltdb(test_ctx->ldb);
+	struct ldb_kv_private *ldb_kv = get_ldb_kv(test_ctx->ldb);
 	uint8_t key_val[] = "TheKey";
 	struct ldb_val key = {
 		.data   = key_val,
@@ -515,67 +515,67 @@ static void test_transaction_abort_delete(void **state)
 	/*
 	 * Begin a transaction
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Write the record
 	 */
-	ret = ltdb->kv_ops->store(ltdb, key, data, flags);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, data, flags);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Commit the transaction
 	 */
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * And now read it back
 	 */
-	ret = ltdb->kv_ops->lock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->lock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &read);
+	ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &read);
 	assert_int_equal(ret, 0);
 	assert_int_equal(sizeof(value), read.length);
 	assert_memory_equal(value, read.data, sizeof(value));
-	ret = ltdb->kv_ops->unlock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->unlock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Begin a transaction
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Now delete it.
 	 */
-	ret = ltdb->kv_ops->delete(ltdb, key);
+	ret = ldb_kv->kv_ops->delete(ldb_kv, key);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * And now read it back
 	 */
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &read);
+	ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &read);
 	assert_int_equal(ret, LDB_ERR_NO_SUCH_OBJECT);
 
 	/*
 	 * Abort the transaction
 	 */
-	ret = ltdb->kv_ops->abort_write(ltdb);
+	ret = ldb_kv->kv_ops->abort_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * And now try to read it back
 	 */
-	ret = ltdb->kv_ops->lock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->lock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &read);
+	ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &read);
 	assert_int_equal(ret, 0);
 	assert_int_equal(sizeof(value), read.length);
 	assert_memory_equal(value, read.data, sizeof(value));
-	ret = ltdb->kv_ops->unlock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->unlock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
 
 	talloc_free(tmp_ctx);
@@ -589,7 +589,7 @@ static void test_write_outside_transaction(void **state)
 	int ret;
 	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
 							  struct test_ctx);
-	struct ltdb_private *ltdb = get_ltdb(test_ctx->ldb);
+	struct ldb_kv_private *ldb_kv = get_ldb_kv(test_ctx->ldb);
 	uint8_t key_val[] = "TheKey";
 	struct ldb_val key = {
 		.data   = key_val,
@@ -612,7 +612,7 @@ static void test_write_outside_transaction(void **state)
 	/*
 	 * Attempt to write the record
 	 */
-	ret = ltdb->kv_ops->store(ltdb, key, data, flags);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, data, flags);
 	assert_int_equal(ret, LDB_ERR_PROTOCOL_ERROR);
 
 	talloc_free(tmp_ctx);
@@ -626,7 +626,7 @@ static void test_delete_outside_transaction(void **state)
 	int ret;
 	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
 							  struct test_ctx);
-	struct ltdb_private *ltdb = get_ltdb(test_ctx->ldb);
+	struct ldb_kv_private *ldb_kv = get_ldb_kv(test_ctx->ldb);
 	uint8_t key_val[] = "TheKey";
 	struct ldb_val key = {
 		.data   = key_val,
@@ -650,55 +650,55 @@ static void test_delete_outside_transaction(void **state)
 	/*
 	 * Begin a transaction
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Write the record
 	 */
-	ret = ltdb->kv_ops->store(ltdb, key, data, flags);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, data, flags);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Commit the transaction
 	 */
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * And now read it back
 	 */
-	ret = ltdb->kv_ops->lock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->lock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &read);
+	ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &read);
 	assert_int_equal(ret, 0);
 	assert_int_equal(sizeof(value), read.length);
 	assert_memory_equal(value, read.data, sizeof(value));
-	ret = ltdb->kv_ops->unlock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->unlock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Now attempt to delete a record
 	 */
-	ret = ltdb->kv_ops->delete(ltdb, key);
+	ret = ldb_kv->kv_ops->delete(ldb_kv, key);
 	assert_int_equal(ret, LDB_ERR_PROTOCOL_ERROR);
 
 	/*
 	 * And now read it back
 	 */
-	ret = ltdb->kv_ops->lock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->lock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
-	ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &read);
+	ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &read);
 	assert_int_equal(ret, 0);
 	assert_int_equal(sizeof(value), read.length);
 	assert_memory_equal(value, read.data, sizeof(value));
-	ret = ltdb->kv_ops->unlock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->unlock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
 
 	talloc_free(tmp_ctx);
 }
 
-static int traverse_fn(struct ltdb_private *ltdb,
+static int traverse_fn(struct ldb_kv_private *ldb_kv,
 		       struct ldb_val key,
 		       struct ldb_val data,
 		       void *ctx) {
@@ -722,7 +722,7 @@ static void test_iterate(void **state)
 	int ret;
 	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
 							  struct test_ctx);
-	struct ltdb_private *ltdb = get_ltdb(test_ctx->ldb);
+	struct ldb_kv_private *ldb_kv = get_ldb_kv(test_ctx->ldb);
 	int i;
 	int num_recs = 1024;
 	int visits[num_recs];
@@ -735,7 +735,7 @@ static void test_iterate(void **state)
 	/*
 	 * Begin a transaction
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
@@ -755,7 +755,7 @@ static void test_iterate(void **state)
 						       i);
 		rec.length = strlen((char *)rec.data) + 1;
 
-		ret = ltdb->kv_ops->store(ltdb, key, rec, flags);
+		ret = ldb_kv->kv_ops->store(ldb_kv, key, rec, flags);
 		assert_int_equal(ret, 0);
 
 		TALLOC_FREE(key.data);
@@ -765,20 +765,20 @@ static void test_iterate(void **state)
 	/*
 	 * Commit the transaction
 	 */
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
 	 * Now iterate over the kv store and ensure that all the
 	 * records are visited.
 	 */
-	ret = ltdb->kv_ops->lock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->lock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
-	ret = ltdb->kv_ops->iterate(ltdb, traverse_fn, visits);
+	ret = ldb_kv->kv_ops->iterate(ldb_kv, traverse_fn, visits);
 	for (i = 0; i <num_recs; i++) {
 		assert_int_equal(1, visits[i]);
 	}
-	ret = ltdb->kv_ops->unlock_read(test_ctx->ldb->modules);
+	ret = ldb_kv->kv_ops->unlock_read(test_ctx->ldb->modules);
 	assert_int_equal(ret, 0);
 
 	TALLOC_FREE(tmp_ctx);
@@ -789,7 +789,7 @@ struct update_context {
 	int visits[NUM_RECS];
 };
 
-static int update_fn(struct ltdb_private *ltdb,
+static int update_fn(struct ldb_kv_private *ldb_kv,
 		     struct ldb_val key,
 		     struct ldb_val data,
 		     void *ctx) {
@@ -800,7 +800,7 @@ static int update_fn(struct ltdb_private *ltdb,
 	int ret = LDB_SUCCESS;
 	TALLOC_CTX *tmp_ctx;
 
-	tmp_ctx = talloc_new(ltdb);
+	tmp_ctx = talloc_new(ldb_kv);
 	assert_non_null(tmp_ctx);
 
 	context = talloc_get_type_abort(ctx, struct update_context);
@@ -815,7 +815,7 @@ static int update_fn(struct ltdb_private *ltdb,
 		new_key.length  = key.length;
 		new_key.data[0] = 'K';
 
-		ret = ltdb->kv_ops->update_in_iterate(ltdb,
+		ret = ldb_kv->kv_ops->update_in_iterate(ldb_kv,
 						      key,
 						      new_key,
 						      data,
@@ -833,7 +833,7 @@ static void test_update_in_iterate(void **state)
 	int ret;
 	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
 							  struct test_ctx);
-	struct ltdb_private *ltdb = get_ltdb(test_ctx->ldb);
+	struct ldb_kv_private *ldb_kv = get_ldb_kv(test_ctx->ldb);
 	int i;
 	struct update_context *context = NULL;
 
@@ -849,7 +849,7 @@ static void test_update_in_iterate(void **state)
 	/*
 	 * Begin a transaction
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
@@ -868,7 +868,7 @@ static void test_update_in_iterate(void **state)
 							 i);
 		rec.length = strlen((char *)rec.data) + 1;
 
-		ret = ltdb->kv_ops->store(ltdb, key, rec, flags);
+		ret = ldb_kv->kv_ops->store(ldb_kv, key, rec, flags);
 		assert_int_equal(ret, 0);
 
 		TALLOC_FREE(key.data);
@@ -878,7 +878,7 @@ static void test_update_in_iterate(void **state)
 	/*
 	 * Commit the transaction
 	 */
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	/*
@@ -889,15 +889,15 @@ static void test_update_in_iterate(void **state)
 	/*
 	 * Needs to be done inside a transaction
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
-	ret = ltdb->kv_ops->iterate(ltdb, update_fn, context);
+	ret = ldb_kv->kv_ops->iterate(ldb_kv, update_fn, context);
 	for (i = 0; i < NUM_RECS; i++) {
 		assert_int_equal(1, context->visits[i]);
 	}
 
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	TALLOC_FREE(tmp_ctx);
@@ -912,7 +912,7 @@ static void test_write_transaction_isolation(void **state)
 	int ret;
 	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
 							  struct test_ctx);
-	struct ltdb_private *ltdb = get_ltdb(test_ctx->ldb);
+	struct ldb_kv_private *ldb_kv = get_ldb_kv(test_ctx->ldb);
 	struct ldb_val key;
 	struct ldb_val val;
 
@@ -939,7 +939,7 @@ static void test_write_transaction_isolation(void **state)
 	/*
 	 * Add a record to the database
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY1);
@@ -948,10 +948,10 @@ static void test_write_transaction_isolation(void **state)
 	val.data = (uint8_t *)talloc_strdup(tmp_ctx, VAL1);
 	val.length = strlen(VAL1) + 1;
 
-	ret = ltdb->kv_ops->store(ltdb, key, val, 0);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, val, 0);
 	assert_int_equal(ret, 0);
 
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 
@@ -987,9 +987,9 @@ static void test_write_transaction_isolation(void **state)
 			exit(ret);
 		}
 
-		ltdb = get_ltdb(ldb);
+		ldb_kv = get_ldb_kv(ldb);
 
-		ret = ltdb->kv_ops->lock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->lock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": lock_read returned (%d)\n",
 				    ret);
@@ -1002,7 +1002,7 @@ static void test_write_transaction_isolation(void **state)
 		key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY1);
 		key.length = strlen(KEY1) + 1;
 
-		ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &val);
+		ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &val);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": fetch_and_parse returned "
 				    "(%d)\n",
@@ -1024,7 +1024,7 @@ static void test_write_transaction_isolation(void **state)
 			exit(LDB_ERR_OPERATIONS_ERROR);
 		}
 
-		ret = ltdb->kv_ops->unlock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->unlock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": unlock_read returned (%d)\n",
 				    ret);
@@ -1037,14 +1037,14 @@ static void test_write_transaction_isolation(void **state)
 		key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY2);
 		key.length = strlen(KEY2 + 1);
 
-		ret = ltdb->kv_ops->lock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->lock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": lock_read returned (%d)\n",
 				    ret);
 			exit(ret);
 		}
 
-		ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &val);
+		ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &val);
 		if (ret != LDB_ERR_NO_SUCH_OBJECT) {
 			print_error(__location__": fetch_and_parse returned "
 				    "(%d)\n",
@@ -1052,7 +1052,7 @@ static void test_write_transaction_isolation(void **state)
 			exit(ret);
 		}
 
-		ret = ltdb->kv_ops->unlock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->unlock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": unlock_read returned (%d)\n",
 				    ret);
@@ -1082,7 +1082,7 @@ static void test_write_transaction_isolation(void **state)
 		/*
 		 * Check that KEY1 is there
 		 */
-		ret = ltdb->kv_ops->lock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->lock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": unlock_read returned (%d)\n",
 				    ret);
@@ -1091,7 +1091,7 @@ static void test_write_transaction_isolation(void **state)
 		key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY1);
 		key.length = strlen(KEY1) + 1;
 
-		ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &val);
+		ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &val);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": fetch_and_parse returned "
 				    "(%d)\n",
@@ -1113,7 +1113,7 @@ static void test_write_transaction_isolation(void **state)
 			exit(LDB_ERR_OPERATIONS_ERROR);
 		}
 
-		ret = ltdb->kv_ops->unlock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->unlock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": unlock_read returned (%d)\n",
 				    ret);
@@ -1124,7 +1124,7 @@ static void test_write_transaction_isolation(void **state)
 		/*
 		 * Check that KEY2 is there
 		 */
-		ret = ltdb->kv_ops->lock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->lock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": unlock_read returned (%d)\n",
 				    ret);
@@ -1134,7 +1134,7 @@ static void test_write_transaction_isolation(void **state)
 		key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY2);
 		key.length = strlen(KEY2) + 1;
 
-		ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &val);
+		ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &val);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": fetch_and_parse returned "
 				    "(%d)\n",
@@ -1156,7 +1156,7 @@ static void test_write_transaction_isolation(void **state)
 			exit(LDB_ERR_OPERATIONS_ERROR);
 		}
 
-		ret = ltdb->kv_ops->unlock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->unlock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": unlock_read returned (%d)\n",
 				    ret);
@@ -1172,7 +1172,7 @@ static void test_write_transaction_isolation(void **state)
 	 * Begin a transaction and add a record to the database
 	 * but leave the transaction open
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY2);
@@ -1181,7 +1181,7 @@ static void test_write_transaction_isolation(void **state)
 	val.data = (uint8_t *)talloc_strdup(tmp_ctx, VAL2);
 	val.length = strlen(VAL2) + 1;
 
-	ret = ltdb->kv_ops->store(ltdb, key, val, 0);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, val, 0);
 	assert_int_equal(ret, 0);
 
 	/*
@@ -1200,7 +1200,7 @@ static void test_write_transaction_isolation(void **state)
 	/*
 	 * commit the transaction
 	 */
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(0, ret);
 
 	/*
@@ -1229,7 +1229,7 @@ static void test_delete_transaction_isolation(void **state)
 	int ret;
 	struct test_ctx *test_ctx = talloc_get_type_abort(*state,
 							  struct test_ctx);
-	struct ltdb_private *ltdb = get_ltdb(test_ctx->ldb);
+	struct ldb_kv_private *ldb_kv = get_ldb_kv(test_ctx->ldb);
 	struct ldb_val key;
 	struct ldb_val val;
 
@@ -1256,7 +1256,7 @@ static void test_delete_transaction_isolation(void **state)
 	/*
 	 * Add records to the database
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY1);
@@ -1265,7 +1265,7 @@ static void test_delete_transaction_isolation(void **state)
 	val.data = (uint8_t *)talloc_strdup(tmp_ctx, VAL1);
 	val.length = strlen(VAL1) + 1;
 
-	ret = ltdb->kv_ops->store(ltdb, key, val, 0);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, val, 0);
 	assert_int_equal(ret, 0);
 
 	key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY2);
@@ -1274,10 +1274,10 @@ static void test_delete_transaction_isolation(void **state)
 	val.data = (uint8_t *)talloc_strdup(tmp_ctx, VAL2);
 	val.length = strlen(VAL2) + 1;
 
-	ret = ltdb->kv_ops->store(ltdb, key, val, 0);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, val, 0);
 	assert_int_equal(ret, 0);
 
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 
@@ -1314,9 +1314,9 @@ static void test_delete_transaction_isolation(void **state)
 			exit(ret);
 		}
 
-		ltdb = get_ltdb(ldb);
+		ldb_kv = get_ldb_kv(ldb);
 
-		ret = ltdb->kv_ops->lock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->lock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": lock_read returned (%d)\n",
 				    ret);
@@ -1329,7 +1329,7 @@ static void test_delete_transaction_isolation(void **state)
 		key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY1);
 		key.length = strlen(KEY1) + 1;
 
-		ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &val);
+		ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &val);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": fetch_and_parse returned "
 				    "(%d)\n",
@@ -1358,7 +1358,7 @@ static void test_delete_transaction_isolation(void **state)
 		key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY2);
 		key.length = strlen(KEY2) + 1;
 
-		ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &val);
+		ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &val);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": fetch_and_parse returned "
 				    "(%d)\n",
@@ -1380,7 +1380,7 @@ static void test_delete_transaction_isolation(void **state)
 			exit(LDB_ERR_OPERATIONS_ERROR);
 		}
 
-		ret = ltdb->kv_ops->unlock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->unlock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": unlock_read returned (%d)\n",
 				    ret);
@@ -1410,7 +1410,7 @@ static void test_delete_transaction_isolation(void **state)
 		/*
 		 * Check that KEY1 is there
 		 */
-		ret = ltdb->kv_ops->lock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->lock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": unlock_read returned (%d)\n",
 				    ret);
@@ -1419,7 +1419,7 @@ static void test_delete_transaction_isolation(void **state)
 		key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY1);
 		key.length = strlen(KEY1) + 1;
 
-		ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &val);
+		ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &val);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": fetch_and_parse returned "
 				    "(%d)\n",
@@ -1447,14 +1447,14 @@ static void test_delete_transaction_isolation(void **state)
 		key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY2);
 		key.length = strlen(KEY2 + 1);
 
-		ret = ltdb->kv_ops->lock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->lock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": lock_read returned (%d)\n",
 				    ret);
 			exit(ret);
 		}
 
-		ret = ltdb->kv_ops->fetch_and_parse(ltdb, key, parse, &val);
+		ret = ldb_kv->kv_ops->fetch_and_parse(ldb_kv, key, parse, &val);
 		if (ret != LDB_ERR_NO_SUCH_OBJECT) {
 			print_error(__location__": fetch_and_parse returned "
 				    "(%d)\n",
@@ -1462,7 +1462,7 @@ static void test_delete_transaction_isolation(void **state)
 			exit(ret);
 		}
 
-		ret = ltdb->kv_ops->unlock_read(ldb->modules);
+		ret = ldb_kv->kv_ops->unlock_read(ldb->modules);
 		if (ret != LDB_SUCCESS) {
 			print_error(__location__": unlock_read returned (%d)\n",
 				    ret);
@@ -1478,13 +1478,13 @@ static void test_delete_transaction_isolation(void **state)
 	 * Begin a transaction and delete a record from the database
 	 * but leave the transaction open
 	 */
-	ret = ltdb->kv_ops->begin_write(ltdb);
+	ret = ldb_kv->kv_ops->begin_write(ldb_kv);
 	assert_int_equal(ret, 0);
 
 	key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY2);
 	key.length = strlen(KEY2) + 1;
 
-	ret = ltdb->kv_ops->delete(ltdb, key);
+	ret = ldb_kv->kv_ops->delete(ldb_kv, key);
 	assert_int_equal(ret, 0);
 	/*
 	 * Signal the child process
@@ -1502,7 +1502,7 @@ static void test_delete_transaction_isolation(void **state)
 	/*
 	 * commit the transaction
 	 */
-	ret = ltdb->kv_ops->finish_write(ltdb);
+	ret = ldb_kv->kv_ops->finish_write(ldb_kv);
 	assert_int_equal(0, ret);
 
 	/*
diff --git a/lib/ldb/tests/ldb_lmdb_test.c b/lib/ldb/tests/ldb_lmdb_test.c
index a254a84..364a4f6 100644
--- a/lib/ldb/tests/ldb_lmdb_test.c
+++ b/lib/ldb/tests/ldb_lmdb_test.c
@@ -414,17 +414,17 @@ static void test_ldb_add_dn_no_guid_mode(void **state)
 static struct MDB_env *get_mdb_env(struct ldb_context *ldb)
 {
 	void *data = NULL;
-	struct ltdb_private *ltdb = NULL;
+	struct ldb_kv_private *ldb_kv = NULL;
 	struct lmdb_private *lmdb = NULL;
 	struct MDB_env *env = NULL;
 
 	data = ldb_module_get_private(ldb->modules);
 	assert_non_null(data);
 
-	ltdb = talloc_get_type(data, struct ltdb_private);
-	assert_non_null(ltdb);
+	ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	assert_non_null(ldb_kv);
 
-	lmdb = ltdb->lmdb_private;
+	lmdb = ldb_kv->lmdb_private;
 	assert_non_null(lmdb);
 
 	env = lmdb->env;
diff --git a/lib/ldb/tests/ldb_tdb_test.c b/lib/ldb/tests/ldb_tdb_test.c
index 686a351..226fcb1 100644
--- a/lib/ldb/tests/ldb_tdb_test.c
+++ b/lib/ldb/tests/ldb_tdb_test.c
@@ -150,16 +150,16 @@ static int ldbtest_teardown(void **state)
 static TDB_CONTEXT *get_tdb_context(struct ldb_context *ldb)
 {
 	void *data = NULL;
-	struct ltdb_private *ltdb = NULL;
+	struct ldb_kv_private *ldb_kv = NULL;
 	TDB_CONTEXT *tdb = NULL;
 
 	data = ldb_module_get_private(ldb->modules);
 	assert_non_null(data);
 
-	ltdb = talloc_get_type(data, struct ltdb_private);
-	assert_non_null(ltdb);
+	ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	assert_non_null(ldb_kv);
 
-	tdb = ltdb->tdb;
+	tdb = ldb_kv->tdb;
 	assert_non_null(tdb);
 
 	return tdb;
-- 
2.7.4


From d2df087a3f0f99d65fa580f78eb47d659f880389 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 11:54:39 +1200
Subject: [PATCH 09/18] lib ldb: format rename ldb_kv_private

Tidy up the code format after the rename of ltdb_private to
ldb_kv_private

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_mdb/ldb_mdb.c       |  21 ++--
 lib/ldb/ldb_tdb/ldb_cache.c     |  63 +++++------
 lib/ldb/ldb_tdb/ldb_index.c     | 189 ++++++++++++++++++--------------
 lib/ldb/ldb_tdb/ldb_search.c    |  19 ++--
 lib/ldb/ldb_tdb/ldb_tdb.c       | 234 ++++++++++++++++++++++------------------
 lib/ldb/ldb_tdb/ldb_tdb.h       |  33 ++++--
 lib/ldb/tests/ldb_kv_ops_test.c |  21 ++--
 7 files changed, 329 insertions(+), 251 deletions(-)

diff --git a/lib/ldb/ldb_mdb/ldb_mdb.c b/lib/ldb/ldb_mdb/ldb_mdb.c
index 7fbe044..9f47746 100644
--- a/lib/ldb/ldb_mdb/ldb_mdb.c
+++ b/lib/ldb/ldb_mdb/ldb_mdb.c
@@ -90,7 +90,6 @@ static int lmdb_error_at(struct ldb_context *ldb,
 	return ldb_err;
 }
 
-
 static bool lmdb_transaction_active(struct ldb_kv_private *ldb_kv)
 {
 	return ldb_kv->lmdb_private->txlist != NULL;
@@ -148,7 +147,8 @@ static MDB_txn *get_current_txn(struct lmdb_private *lmdb)
 
 static int lmdb_store(struct ldb_kv_private *ldb_kv,
 		      struct ldb_val key,
-		      struct ldb_val data, int flags)
+		      struct ldb_val data,
+		      int flags)
 {
 	struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 	MDB_val mdb_key;
@@ -366,8 +366,10 @@ done:
 }
 
 /* Handles only a single record */
-static int lmdb_parse_record(struct ldb_kv_private *ldb_kv, struct ldb_val key,
-			     int (*parser)(struct ldb_val key, struct ldb_val data,
+static int lmdb_parse_record(struct ldb_kv_private *ldb_kv,
+			     struct ldb_val key,
+			     int (*parser)(struct ldb_val key,
+					   struct ldb_val data,
 					   void *private_data),
 			     void *ctx)
 {
@@ -415,7 +417,8 @@ static int lmdb_parse_record(struct ldb_kv_private *ldb_kv, struct ldb_val key,
 static int lmdb_lock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 	pid_t pid = getpid();
 
@@ -449,9 +452,11 @@ static int lmdb_lock_read(struct ldb_module *module)
 static int lmdb_unlock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 
-	if (lmdb_transaction_active(ldb_kv) == false && ldb_kv->read_lock_count == 1) {
+	if (lmdb_transaction_active(ldb_kv) == false &&
+	    ldb_kv->read_lock_count == 1) {
 		struct lmdb_private *lmdb = ldb_kv->lmdb_private;
 		mdb_txn_commit(lmdb->read_txn);
 		lmdb->read_txn = NULL;
@@ -552,7 +557,7 @@ static const char *lmdb_errorstr(struct ldb_kv_private *ldb_kv)
 	return mdb_strerror(ldb_kv->lmdb_private->error);
 }
 
-static const char * lmdb_name(struct ldb_kv_private *ldb_kv)
+static const char *lmdb_name(struct ldb_kv_private *ldb_kv)
 {
 	return "lmdb";
 }
diff --git a/lib/ldb/ldb_tdb/ldb_cache.c b/lib/ldb/ldb_tdb/ldb_cache.c
index 1a07f99..6c61e59 100644
--- a/lib/ldb/ldb_tdb/ldb_cache.c
+++ b/lib/ldb/ldb_tdb/ldb_cache.c
@@ -248,11 +248,12 @@ static int ldb_kv_index_load(struct ldb_module *module,
 		 * supplying its own attribute handling
 		 */
 		ldb_kv->cache->attribute_indexes = true;
-		ldb_kv->cache->one_level_indexes = ldb->schema.one_level_indexes;
-		ldb_kv->cache->GUID_index_attribute
-			= ldb->schema.GUID_index_attribute;
-		ldb_kv->cache->GUID_index_dn_component
-			= ldb->schema.GUID_index_dn_component;
+		ldb_kv->cache->one_level_indexes =
+		    ldb->schema.one_level_indexes;
+		ldb_kv->cache->GUID_index_attribute =
+		    ldb->schema.GUID_index_attribute;
+		ldb_kv->cache->GUID_index_dn_component =
+		    ldb->schema.GUID_index_dn_component;
 		return 0;
 	}
 
@@ -282,22 +283,21 @@ static int ldb_kv_index_load(struct ldb_module *module,
 		return -1;
 	}
 
-	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXONE) != NULL) {
+	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXONE) !=
+	    NULL) {
 		ldb_kv->cache->one_level_indexes = true;
 	}
-	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR) != NULL) {
+	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR) !=
+	    NULL) {
 		ldb_kv->cache->attribute_indexes = true;
 	}
-	ldb_kv->cache->GUID_index_attribute
-		= ldb_msg_find_attr_as_string(ldb_kv->cache->indexlist,
-					      LTDB_IDXGUID, NULL);
-	ldb_kv->cache->GUID_index_dn_component
-		= ldb_msg_find_attr_as_string(ldb_kv->cache->indexlist,
-					      LTDB_IDX_DN_GUID, NULL);
+	ldb_kv->cache->GUID_index_attribute = ldb_msg_find_attr_as_string(
+	    ldb_kv->cache->indexlist, LTDB_IDXGUID, NULL);
+	ldb_kv->cache->GUID_index_dn_component = ldb_msg_find_attr_as_string(
+	    ldb_kv->cache->indexlist, LTDB_IDX_DN_GUID, NULL);
 
-	lmdb_subdb_version
-		= ldb_msg_find_attr_as_int(ldb_kv->cache->indexlist,
-					   LTDB_IDX_LMDB_SUBDB, 0);
+	lmdb_subdb_version = ldb_msg_find_attr_as_int(
+	    ldb_kv->cache->indexlist, LTDB_IDX_LMDB_SUBDB, 0);
 
 	if (lmdb_subdb_version != 0) {
 		ldb_set_errstring(ldb,
@@ -319,7 +319,8 @@ static int ldb_kv_baseinfo_init(struct ldb_module *module)
 {
 	struct ldb_context *ldb;
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_message *msg;
 	struct ldb_message_element el;
 	struct ldb_val val;
@@ -375,7 +376,8 @@ failed:
 static void ldb_kv_cache_free(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 
 	ldb_kv->sequence_number = 0;
 	talloc_free(ldb_kv->cache);
@@ -399,7 +401,8 @@ int ldb_kv_cache_load(struct ldb_module *module)
 {
 	struct ldb_context *ldb;
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
 	uint64_t seq;
 	struct ldb_message *baseinfo = NULL, *options = NULL;
@@ -416,7 +419,8 @@ int ldb_kv_cache_load(struct ldb_module *module)
 
 	if (ldb_kv->cache == NULL) {
 		ldb_kv->cache = talloc_zero(ldb_kv, struct ltdb_cache);
-		if (ldb_kv->cache == NULL) goto failed;
+		if (ldb_kv->cache == NULL)
+			goto failed;
 	}
 
 	baseinfo = ldb_msg_new(ldb_kv->cache);
@@ -487,12 +491,10 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	
 	/* set flags if they do exist */
 	if (r == LDB_SUCCESS) {
-		ldb_kv->check_base = ldb_msg_find_attr_as_bool(options,
-							     LTDB_CHECK_BASE,
-							     false);
-		ldb_kv->disallow_dn_filter = ldb_msg_find_attr_as_bool(options,
-								     LTDB_DISALLOW_DN_FILTER,
-								     false);
+		ldb_kv->check_base =
+		    ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
+		ldb_kv->disallow_dn_filter = ldb_msg_find_attr_as_bool(
+		    options, LTDB_DISALLOW_DN_FILTER, false);
 	} else {
 		ldb_kv->check_base = false;
 		ldb_kv->disallow_dn_filter = false;
@@ -527,8 +529,8 @@ int ldb_kv_cache_load(struct ldb_module *module)
 		 * Now the attributes are loaded, set the guid_index_syntax.
 		 * This can't fail, it will return a default at worst
 		 */
-		a = ldb_schema_attribute_by_name(ldb,
-						 ldb_kv->cache->GUID_index_attribute);
+		a = ldb_schema_attribute_by_name(
+		    ldb, ldb_kv->cache->GUID_index_attribute);
 		ldb_kv->GUID_index_syntax = a->syntax;
 	}
 
@@ -566,7 +568,8 @@ int ldb_kv_increase_sequence_number(struct ldb_module *module)
 {
 	struct ldb_context *ldb;
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_message *msg;
 	struct ldb_message_element el[2];
 	struct ldb_val val;
@@ -583,7 +586,7 @@ int ldb_kv_increase_sequence_number(struct ldb_module *module)
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	s = talloc_asprintf(msg, "%llu", ldb_kv->sequence_number+1);
+	s = talloc_asprintf(msg, "%llu", ldb_kv->sequence_number + 1);
 	if (!s) {
 		talloc_free(msg);
 		errno = ENOMEM;
diff --git a/lib/ldb/ldb_tdb/ldb_index.c b/lib/ldb/ldb_tdb/ldb_index.c
index e0a5b62..1c3a7a0 100644
--- a/lib/ldb/ldb_tdb/ldb_index.c
+++ b/lib/ldb/ldb_tdb/ldb_index.c
@@ -191,7 +191,7 @@ static void ldb_kv_dn_list_sort(struct ldb_kv_private *ldb_kv,
 
 static unsigned ldb_kv_max_key_length(struct ldb_kv_private *ldb_kv)
 {
-	if (ldb_kv->max_key_length == 0){
+	if (ldb_kv->max_key_length == 0) {
 		return UINT_MAX;
 	}
 	return ldb_kv->max_key_length;
@@ -200,7 +200,8 @@ static unsigned ldb_kv_max_key_length(struct ldb_kv_private *ldb_kv)
 /* enable the idxptr mode when transactions start */
 int ldb_kv_index_transaction_start(struct ldb_module *module)
 {
-	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
 	ldb_kv->idxptr = talloc_zero(ldb_kv, struct ltdb_idxptr);
 	if (ldb_kv->idxptr == NULL) {
 		return ldb_oom(ldb_module_get_ctx(module));
@@ -294,8 +295,8 @@ static int ldb_kv_dn_list_find_msg(struct ldb_kv_private *ldb_kv,
 		v.data = discard_const_p(unsigned char, dn_str);
 		v.length = strlen(dn_str);
 	} else {
-		key_val = ldb_msg_find_ldb_val(msg,
-					       ldb_kv->cache->GUID_index_attribute);
+		key_val = ldb_msg_find_ldb_val(
+		    msg, ldb_kv->cache->GUID_index_attribute);
 		if (key_val == NULL) {
 			return -1;
 		}
@@ -359,8 +360,7 @@ static int ldb_kv_dn_list_load(struct ldb_module *module,
 	list->count = 0;
 
 	/* see if we have any in-memory index entries */
-	if (ldb_kv->idxptr == NULL ||
-	    ldb_kv->idxptr->itdb == NULL) {
+	if (ldb_kv->idxptr == NULL || ldb_kv->idxptr->itdb == NULL) {
 		goto normal_index;
 	}
 
@@ -513,7 +513,8 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 				       "against %s for %s: too many "
 				       "values (%u > 1)",
 				       ldb_kv->cache->GUID_index_attribute,
-				       dn_str, list->count);
+				       dn_str,
+				       list->count);
 		TALLOC_FREE(list);
 		return LDB_ERR_CONSTRAINT_VIOLATION;
 	}
@@ -546,7 +547,8 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 				return ret;
 			}
 
-			ret = ldb_kv_search_key(module, ldb_kv, key, rec, flags);
+			ret =
+			    ldb_kv_search_key(module, ldb_kv, key, rec, flags);
 			if (key.dptr != guid_key) {
 				TALLOC_FREE(key.dptr);
 			}
@@ -704,7 +706,8 @@ static int ldb_kv_dn_list_store(struct ldb_module *module,
 				struct ldb_dn *dn,
 				struct dn_list *list)
 {
-	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
 	TDB_DATA rec, key;
 	int ret;
 	struct dn_list *list2;
@@ -714,7 +717,8 @@ static int ldb_kv_dn_list_store(struct ldb_module *module,
 	}
 
 	if (ldb_kv->idxptr->itdb == NULL) {
-		ldb_kv->idxptr->itdb = tdb_open(NULL, 1000, TDB_INTERNAL, O_RDWR, 0);
+		ldb_kv->idxptr->itdb =
+		    tdb_open(NULL, 1000, TDB_INTERNAL, O_RDWR, 0);
 		if (ldb_kv->idxptr->itdb == NULL) {
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
@@ -770,7 +774,8 @@ static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
 				       void *state)
 {
 	struct ldb_module *module = state;
-	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
 	struct ldb_dn *dn;
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	struct ldb_val v;
@@ -792,7 +797,8 @@ static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
 		return -1;
 	}
 
-	ldb_kv->idxptr->error = ldb_kv_dn_list_store_full(module, ldb_kv, dn, list);
+	ldb_kv->idxptr->error =
+	    ldb_kv_dn_list_store_full(module, ldb_kv, dn, list);
 	talloc_free(dn);
 	if (ldb_kv->idxptr->error != 0) {
 		return -1;
@@ -803,7 +809,8 @@ static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
 /* cleanup the idxptr mode when transaction commits */
 int ldb_kv_index_transaction_commit(struct ldb_module *module)
 {
-	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
 	int ret;
 
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
@@ -832,7 +839,8 @@ int ldb_kv_index_transaction_commit(struct ldb_module *module)
 /* cleanup the idxptr mode when transaction cancels */
 int ldb_kv_index_transaction_cancel(struct ldb_module *module)
 {
-	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
 	if (ldb_kv->idxptr && ldb_kv->idxptr->itdb) {
 		tdb_close(ldb_kv->idxptr->itdb);
 	}
@@ -1041,8 +1049,7 @@ static bool ldb_kv_is_indexed(struct ldb_module *module,
 	struct ldb_message_element *el;
 
 	if ((ldb_kv->cache->GUID_index_attribute != NULL) &&
-	    (ldb_attr_cmp(attr,
-			  ldb_kv->cache->GUID_index_attribute) == 0)) {
+	    (ldb_attr_cmp(attr, ldb_kv->cache->GUID_index_attribute) == 0)) {
 		/* Implicity covered, this is the index key */
 		return false;
 	}
@@ -1136,10 +1143,10 @@ static int ldb_kv_index_dn_simple(struct ldb_module *module,
 	return ret;
 }
 
-
 static bool list_union(struct ldb_context *ldb,
 		       struct ldb_kv_private *ldb_kv,
-		       struct dn_list *list, struct dn_list *list2);
+		       struct dn_list *list,
+		       struct dn_list *list2);
 
 /*
   return a list of dn's that might match a leaf indexed search
@@ -1205,10 +1212,8 @@ static int ldb_kv_index_dn_leaf(struct ldb_module *module,
 		 * ensure we get the index in binary, rather
 		 * than a string
 		 */
-		ret = ldb_kv->GUID_index_syntax->canonicalise_fn(ldb,
-							       list->dn,
-							       &tree->u.equality.value,
-							       &list->dn[0]);
+		ret = ldb_kv->GUID_index_syntax->canonicalise_fn(
+		    ldb, list->dn, &tree->u.equality.value, &list->dn[0]);
 		if (ret != LDB_SUCCESS) {
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
@@ -1226,7 +1231,8 @@ static int ldb_kv_index_dn_leaf(struct ldb_module *module,
 */
 static bool list_intersect(struct ldb_context *ldb,
 			   struct ldb_kv_private *ldb_kv,
-			   struct dn_list *list, const struct dn_list *list2)
+			   struct dn_list *list,
+			   const struct dn_list *list2)
 {
 	const struct dn_list *short_list, *long_list;
 	struct dn_list *list3;
@@ -1306,7 +1312,8 @@ static bool list_intersect(struct ldb_context *ldb,
 */
 static bool list_union(struct ldb_context *ldb,
 		       struct ldb_kv_private *ldb_kv,
-		       struct dn_list *list, struct dn_list *list2)
+		       struct dn_list *list,
+		       struct dn_list *list2)
 {
 	struct ldb_val *dn3;
 	unsigned int i = 0, j = 0, k = 0;
@@ -1468,7 +1475,8 @@ static bool ldb_kv_index_unique(struct ldb_context *ldb,
 {
 	const struct ldb_schema_attribute *a;
 	if (ldb_kv->cache->GUID_index_attribute != NULL) {
-		if (ldb_attr_cmp(attr, ldb_kv->cache->GUID_index_attribute) == 0) {
+		if (ldb_attr_cmp(attr, ldb_kv->cache->GUID_index_attribute) ==
+		    0) {
 			return true;
 		}
 	}
@@ -1508,7 +1516,8 @@ static int ldb_kv_index_dn_and(struct ldb_module *module,
 		int ret;
 
 		if (subtree->operation != LDB_OP_EQUALITY ||
-		    !ldb_kv_index_unique(ldb, ldb_kv, subtree->u.equality.attr)) {
+		    !ldb_kv_index_unique(
+			ldb, ldb_kv, subtree->u.equality.attr)) {
 			continue;
 		}
 
@@ -1560,8 +1569,7 @@ static int ldb_kv_index_dn_and(struct ldb_module *module,
 			list->dn = list2->dn;
 			list->count = list2->count;
 			found = true;
-		} else if (!list_intersect(ldb, ldb_kv,
-					   list, list2)) {
+		} else if (!list_intersect(ldb, ldb_kv, list, list2)) {
 			talloc_free(list2);
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
@@ -1666,8 +1674,8 @@ static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
 	}
 
 	if (ldb_kv->cache->GUID_index_dn_component != NULL) {
-		guid_val = ldb_dn_get_extended_component(base_dn,
-							 ldb_kv->cache->GUID_index_dn_component);
+		guid_val = ldb_dn_get_extended_component(
+		    base_dn, ldb_kv->cache->GUID_index_dn_component);
 	}
 
 	if (guid_val != NULL) {
@@ -1865,9 +1873,9 @@ static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 		 *
 		 * LDB_SCOPE_BASE is not passed in by our only caller.
 		 */
-		if (ac->scope == LDB_SCOPE_ONELEVEL
-		    && ldb_kv->cache->one_level_indexes
-		    && scope_one_truncation == KEY_NOT_TRUNCATED) {
+		if (ac->scope == LDB_SCOPE_ONELEVEL &&
+		    ldb_kv->cache->one_level_indexes &&
+		    scope_one_truncation == KEY_NOT_TRUNCATED) {
 			ret = ldb_match_message(ldb, msg, ac->tree,
 						ac->scope, &matched);
 		} else {
@@ -1916,7 +1924,8 @@ static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 /*
   sort a DN list
  */
-static void ldb_kv_dn_list_sort(struct ldb_kv_private *ltdb, struct dn_list *list)
+static void ldb_kv_dn_list_sort(struct ldb_kv_private *ltdb,
+				struct dn_list *list)
 {
 	if (list->count < 2) {
 		return;
@@ -1939,7 +1948,8 @@ static void ldb_kv_dn_list_sort(struct ldb_kv_private *ltdb, struct dn_list *lis
 int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(ac->module), struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(ac->module), struct ldb_kv_private);
 	struct dn_list *dn_list;
 	int ret;
 	enum ldb_scope index_scope;
@@ -1947,8 +1957,7 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 
 	/* see if indexing is enabled */
 	if (!ldb_kv->cache->attribute_indexes &&
-	    !ldb_kv->cache->one_level_indexes &&
-	    ac->scope != LDB_SCOPE_BASE) {
+	    !ldb_kv->cache->one_level_indexes && ac->scope != LDB_SCOPE_BASE) {
 		/* fallback to a full search */
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
@@ -1984,8 +1993,11 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 		 * the tree, we must ensure we strictly intersect with
 		 * this list, as we trust the ONELEVEL index
 		 */
-		ret = ldb_kv_index_dn_one(
-		    ac->module, ldb_kv, ac->base, dn_list, &scope_one_truncation);
+		ret = ldb_kv_index_dn_one(ac->module,
+					  ldb_kv,
+					  ac->base,
+					  dn_list,
+					  &scope_one_truncation);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(dn_list);
 			return ret;
@@ -2026,7 +2038,8 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 			ret = ldb_kv_index_dn(
 			    ac->module, ldb_kv, ac->tree, idx_one_tree_list);
 			if (ret == LDB_SUCCESS) {
-				if (!list_intersect(ldb, ldb_kv,
+				if (!list_intersect(ldb,
+						    ldb_kv,
 						    dn_list,
 						    idx_one_tree_list)) {
 					talloc_free(idx_one_tree_list);
@@ -2130,12 +2143,12 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		(el->flags & LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX)))) {
 
 		ldb_asprintf_errstring(
-			ldb,
-			__location__ ": unique index key on %s in %s, "
-			"exceeds maximum key length of %u (encoded).",
-			el->name,
-			ldb_dn_get_linearized(msg->dn),
-			ldb_kv->max_key_length);
+		    ldb,
+		    __location__ ": unique index key on %s in %s, "
+				 "exceeds maximum key length of %u (encoded).",
+		    el->name,
+		    ldb_dn_get_linearized(msg->dn),
+		    ldb_kv->max_key_length);
 		talloc_free(list);
 		return LDB_ERR_CONSTRAINT_VIOLATION;
 	}
@@ -2191,7 +2204,8 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 				return ret;
 			}
 
-			ret = ldb_kv_search_key(module, ldb_kv, key, rec, flags);
+			ret =
+			    ldb_kv_search_key(module, ldb_kv, key, rec, flags);
 			if (key.dptr != guid_key) {
 				TALLOC_FREE(key.dptr);
 			}
@@ -2251,15 +2265,15 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 				  ldb_dn_get_linearized(dn_key));
 		} else {
 			/* This can't fail, gives a default at worst */
-			const struct ldb_schema_attribute *attr
-				= ldb_schema_attribute_by_name(
-					ldb,
-					ldb_kv->cache->GUID_index_attribute);
+			const struct ldb_schema_attribute *attr =
+			    ldb_schema_attribute_by_name(
+				ldb, ldb_kv->cache->GUID_index_attribute);
 			struct ldb_val v;
 			ret = attr->syntax->ldif_write_fn(ldb, list,
 							  &list->dn[0], &v);
 			if (ret == LDB_SUCCESS) {
-				ldb_debug(ldb, LDB_DEBUG_WARNING,
+				ldb_debug(ldb,
+					  LDB_DEBUG_WARNING,
 					  __location__
 					  ": unique index violation on %s in "
 					  "%s, conficts with %s %*.*s in %s",
@@ -2302,8 +2316,8 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 	} else {
 		const struct ldb_val *key_val;
 		struct ldb_val *exact = NULL, *next = NULL;
-		key_val = ldb_msg_find_ldb_val(msg,
-					       ldb_kv->cache->GUID_index_attribute);
+		key_val = ldb_msg_find_ldb_val(
+		    msg, ldb_kv->cache->GUID_index_attribute);
 		if (key_val == NULL) {
 			talloc_free(list);
 			return ldb_module_operr(module);
@@ -2326,15 +2340,15 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		 */
 		if (exact != NULL && truncation == KEY_NOT_TRUNCATED) {
 			/* This can't fail, gives a default at worst */
-			const struct ldb_schema_attribute *attr
-				= ldb_schema_attribute_by_name(
-					ldb,
-					ldb_kv->cache->GUID_index_attribute);
+			const struct ldb_schema_attribute *attr =
+			    ldb_schema_attribute_by_name(
+				ldb, ldb_kv->cache->GUID_index_attribute);
 			struct ldb_val v;
 			ret = attr->syntax->ldif_write_fn(ldb, list,
 							  exact, &v);
 			if (ret == LDB_SUCCESS) {
-				ldb_debug(ldb, LDB_DEBUG_WARNING,
+				ldb_debug(ldb,
+					  LDB_DEBUG_WARNING,
 					  __location__
 					  ": duplicate attribute value in %s "
 					  "for index on %s, "
@@ -2457,10 +2471,9 @@ static int ldb_kv_modify_index_dn(struct ldb_module *module,
 	if (val.data == NULL) {
 		const char *dn_str = ldb_dn_get_linearized(dn);
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       __location__
-				       ": Failed to modify %s "
-				       "against %s in %s: failed "
-				       "to get casefold DN",
+				       __location__ ": Failed to modify %s "
+						    "against %s in %s: failed "
+						    "to get casefold DN",
 				       index,
 				       ldb_kv->cache->GUID_index_attribute,
 				       dn_str);
@@ -2482,12 +2495,12 @@ static int ldb_kv_modify_index_dn(struct ldb_module *module,
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
 		const char *dn_str = ldb_dn_get_linearized(dn);
 		ldb_asprintf_errstring(ldb,
-				       __location__
-				       ": Failed to modify %s "
-				       "against %s in %s - %s",
+				       __location__ ": Failed to modify %s "
+						    "against %s in %s - %s",
 				       index,
 				       ldb_kv->cache->GUID_index_attribute,
-				       dn_str, ldb_errstring(ldb));
+				       dn_str,
+				       ldb_errstring(ldb));
 		return ret;
 	}
 	return ret;
@@ -2500,8 +2513,8 @@ static int ldb_kv_index_onelevel(struct ldb_module *module,
 				 const struct ldb_message *msg,
 				 int add)
 {
-	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module),
-						    struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
 	struct ldb_dn *pdn;
 	int ret;
 
@@ -2514,7 +2527,8 @@ static int ldb_kv_index_onelevel(struct ldb_module *module,
 	if (pdn == NULL) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
-	ret = ldb_kv_modify_index_dn(module, ldb_kv, msg, pdn, LTDB_IDXONE, add);
+	ret =
+	    ldb_kv_modify_index_dn(module, ldb_kv, msg, pdn, LTDB_IDXONE, add);
 
 	talloc_free(pdn);
 
@@ -2529,16 +2543,16 @@ static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
 				      int add)
 {
 	int ret;
-	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module),
-						    struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
 
 	/* We index for DN only if using a GUID index */
 	if (ldb_kv->cache->GUID_index_attribute == NULL) {
 		return LDB_SUCCESS;
 	}
 
-	ret =
-	    ldb_kv_modify_index_dn(module, ldb_kv, msg, msg->dn, LTDB_IDXDN, add);
+	ret = ldb_kv_modify_index_dn(
+	    module, ldb_kv, msg, msg->dn, LTDB_IDXDN, add);
 
 	if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
@@ -2744,7 +2758,8 @@ int ldb_kv_index_del_element(struct ldb_module *module,
 int ldb_kv_index_delete(struct ldb_module *module,
 			const struct ldb_message *msg)
 {
-	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
 	int ret;
 	unsigned int i;
 
@@ -2787,7 +2802,10 @@ int ldb_kv_index_delete(struct ldb_module *module,
   commit, which in turn greatly reduces DB churn as we will likely
   be able to do a direct update into the old record.
 */
-static int delete_index(struct ldb_kv_private *ldb_kv, struct ldb_val key, struct ldb_val data, void *state)
+static int delete_index(struct ldb_kv_private *ldb_kv,
+			struct ldb_val key,
+			struct ldb_val data,
+			void *state)
 {
 	struct ldb_module *module = state;
 	const char *dnstr = "DN=" LTDB_INDEX ":";
@@ -2829,7 +2847,10 @@ static int delete_index(struct ldb_kv_private *ldb_kv, struct ldb_val key, struc
 /*
   traversal function that adds @INDEX records during a re index TODO wrong comment
 */
-static int re_key(struct ldb_kv_private *ldb_kv, struct ldb_val ldb_key, struct ldb_val val, void *state)
+static int re_key(struct ldb_kv_private *ldb_kv,
+		  struct ldb_val ldb_key,
+		  struct ldb_val val,
+		  void *state)
 {
 	struct ldb_context *ldb;
 	struct ldb_kv_reindex_context *ctx =
@@ -2902,7 +2923,8 @@ static int re_key(struct ldb_kv_private *ldb_kv, struct ldb_val ldb_key, struct
 			.data = key2.dptr,
 			.length = key2.dsize
 		};
-		ldb_kv->kv_ops->update_in_iterate(ldb_kv, ldb_key, ldb_key2, val, ctx);
+		ldb_kv->kv_ops->update_in_iterate(
+		    ldb_kv, ldb_key, ldb_key2, val, ctx);
 	}
 	talloc_free(key2.dptr);
 
@@ -2921,7 +2943,10 @@ static int re_key(struct ldb_kv_private *ldb_kv, struct ldb_val ldb_key, struct
 /*
   traversal function that adds @INDEX records during a re index
 */
-static int re_index(struct ldb_kv_private *ldb_kv, struct ldb_val ldb_key, struct ldb_val val, void *state)
+static int re_index(struct ldb_kv_private *ldb_kv,
+		    struct ldb_val ldb_key,
+		    struct ldb_val val,
+		    void *state)
 {
 	struct ldb_context *ldb;
 	struct ldb_kv_reindex_context *ctx =
@@ -3010,7 +3035,8 @@ static int re_index(struct ldb_kv_private *ldb_kv, struct ldb_val ldb_key, struc
 */
 int ldb_kv_reindex(struct ldb_module *module)
 {
-	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
 	int ret;
 	struct ldb_kv_reindex_context ctx;
 
@@ -3087,7 +3113,8 @@ int ldb_kv_reindex(struct ldb_module *module)
 
 	if (ctx.count > 10000) {
 		ldb_debug(ldb_module_get_ctx(module),
-			  LDB_DEBUG_WARNING, "Reindexing: re_index successful on %s, "
+			  LDB_DEBUG_WARNING,
+			  "Reindexing: re_index successful on %s, "
 			  "final index write-out will be in transaction commit",
 			  ldb_kv->kv_ops->name(ldb_kv));
 	}
diff --git a/lib/ldb/ldb_tdb/ldb_search.c b/lib/ldb/ldb_tdb/ldb_search.c
index fd8b64e..b3eafeb 100644
--- a/lib/ldb/ldb_tdb/ldb_search.c
+++ b/lib/ldb/ldb_tdb/ldb_search.c
@@ -289,7 +289,8 @@ int ldb_kv_search_dn1(struct ldb_module *module,
 		      unsigned int unpack_flags)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	int ret;
 	uint8_t guid_key[LTDB_GUID_KEY_SIZE];
 	TDB_DATA tdb_key = {
@@ -299,7 +300,7 @@ int ldb_kv_search_dn1(struct ldb_module *module,
 	TALLOC_CTX *tdb_key_ctx = NULL;
 
 	if (ldb_kv->cache->GUID_index_attribute == NULL ||
-		ldb_dn_is_special(dn)) {
+	    ldb_dn_is_special(dn)) {
 
 		tdb_key_ctx = talloc_new(msg);
 		if (!tdb_key_ctx) {
@@ -493,7 +494,10 @@ failed:
 /*
   search function for a non-indexed search
  */
-static int search_func(struct ldb_kv_private *ldb_kv, struct ldb_val key, struct ldb_val val, void *state)
+static int search_func(struct ldb_kv_private *ldb_kv,
+		       struct ldb_val key,
+		       struct ldb_val val,
+		       void *state)
 {
 	struct ldb_context *ldb;
 	struct ldb_kv_context *ac;
@@ -583,7 +587,8 @@ static int search_func(struct ldb_kv_private *ldb_kv, struct ldb_val key, struct
 static int ldb_kv_search_full(struct ldb_kv_context *ctx)
 {
 	void *data = ldb_module_get_private(ctx->module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	int ret;
 
 	ctx->error = LDB_SUCCESS;
@@ -708,7 +713,8 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	int ret;
 
 	ldb = ldb_module_get_ctx(module);
@@ -811,7 +817,8 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 		 * callback error */
 		if ( ! ctx->request_terminated && ret != LDB_SUCCESS) {
 			/* Not indexed, so we need to do a full scan */
-			if (ldb_kv->warn_unindexed || ldb_kv->disable_full_db_scan) {
+			if (ldb_kv->warn_unindexed ||
+			    ldb_kv->disable_full_db_scan) {
 				/* useful for debugging when slow performance
 				 * is caused by unindexed searches */
 				char *expression = ldb_filter_from_tree(ctx, ctx->tree);
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.c b/lib/ldb/ldb_tdb/ldb_tdb.c
index c1f5fa3..b963123 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -97,18 +97,19 @@ int ltdb_err_map(enum TDB_ERROR tdb_code)
 static int ltdb_lock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	int tdb_ret = 0;
 	int ret;
 	pid_t pid = getpid();
 
 	if (ldb_kv->pid != pid) {
-		ldb_asprintf_errstring(
-			ldb_module_get_ctx(module),
-			__location__": Reusing ldb opend by pid %d in "
-			"process %d\n",
-			ldb_kv->pid,
-			pid);
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       __location__
+				       ": Reusing ldb opend by pid %d in "
+				       "process %d\n",
+				       ldb_kv->pid,
+				       pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
@@ -138,19 +139,21 @@ static int ltdb_lock_read(struct ldb_module *module)
 static int ltdb_unlock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	pid_t pid = getpid();
 
 	if (ldb_kv->pid != pid) {
-		ldb_asprintf_errstring(
-			ldb_module_get_ctx(module),
-			__location__": Reusing ldb opend by pid %d in "
-			"process %d\n",
-			ldb_kv->pid,
-			pid);
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       __location__
+				       ": Reusing ldb opend by pid %d in "
+				       "process %d\n",
+				       ldb_kv->pid,
+				       pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
-	if (!tdb_transaction_active(ldb_kv->tdb) && ldb_kv->read_lock_count == 1) {
+	if (!tdb_transaction_active(ldb_kv->tdb) &&
+	    ldb_kv->read_lock_count == 1) {
 		tdb_unlockall_read(ldb_kv->tdb);
 		ldb_kv->read_lock_count--;
 		return 0;
@@ -310,7 +313,8 @@ TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
 			const struct ldb_message *msg)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	TDB_DATA key;
 	const struct ldb_val *guid_val;
 	int ret;
@@ -323,8 +327,8 @@ TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
 		return ldb_kv_key_dn(module, mem_ctx, msg->dn);
 	}
 
-	guid_val = ldb_msg_find_ldb_val(msg,
-				       ldb_kv->cache->GUID_index_attribute);
+	guid_val =
+	    ldb_msg_find_ldb_val(msg, ldb_kv->cache->GUID_index_attribute);
 	if (guid_val == NULL) {
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
 				       "Did not find GUID attribute %s "
@@ -399,7 +403,8 @@ static int ldb_kv_check_special_dn(struct ldb_module *module,
 static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
 {
 	int ret = LDB_SUCCESS;
-	struct ldb_kv_private *ldb_kv = talloc_get_type(ldb_module_get_private(module), struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
 
 	/* only allow modifies inside a transaction, otherwise the
 	 * ldb is unsafe */
@@ -414,8 +419,10 @@ static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
 	{
 		if (ldb_kv->warn_reindex) {
 			ldb_debug(ldb_module_get_ctx(module),
-				LDB_DEBUG_ERROR, "Reindexing %s due to modification on %s",
-				ldb_kv->kv_ops->name(ldb_kv), ldb_dn_get_linearized(dn));
+				  LDB_DEBUG_ERROR,
+				  "Reindexing %s due to modification on %s",
+				  ldb_kv->kv_ops->name(ldb_kv),
+				  ldb_dn_get_linearized(dn));
 		}
 		ret = ldb_kv_reindex(module);
 	}
@@ -479,7 +486,8 @@ int ldb_kv_store(struct ldb_module *module,
 		 int flgs)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	TDB_DATA tdb_key;
 	struct ldb_val ldb_key;
 	struct ldb_val ldb_data;
@@ -520,9 +528,8 @@ int ldb_kv_store(struct ldb_module *module,
 		 * LDB_ERR_ENTRY_ALREADY_EXISTS means the DN, not
 		 * the GUID, so re-map
 		 */
-		if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS
-		    && !is_special
-		    && ldb_kv->cache->GUID_index_attribute != NULL) {
+		if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS && !is_special &&
+		    ldb_kv->cache->GUID_index_attribute != NULL) {
 			ret = LDB_ERR_CONSTRAINT_VIOLATION;
 		}
 		goto done;
@@ -676,13 +683,13 @@ static int ldb_kv_add(struct ldb_kv_context *ctx)
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	int ret = LDB_SUCCESS;
 
 	if (ldb_kv->max_key_length != 0 &&
 	    ldb_kv->cache->GUID_index_attribute == NULL &&
-	    !ldb_dn_is_special(req->op.add.message->dn))
-	{
+	    !ldb_dn_is_special(req->op.add.message->dn)) {
 		ldb_set_errstring(ldb_module_get_ctx(module),
 				  "Must operate ldb_mdb in GUID "
 				  "index mode, but " LTDB_IDXGUID " not set.");
@@ -726,7 +733,8 @@ int ldb_kv_delete_noindex(struct ldb_module *module,
 			  const struct ldb_message *msg)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_val ldb_key;
 	TDB_DATA tdb_key;
 	int ret;
@@ -750,7 +758,7 @@ int ldb_kv_delete_noindex(struct ldb_module *module,
 	ldb_key.data = tdb_key.dptr;
 	ldb_key.length = tdb_key.dsize;
 
-	ret = ldb_kv->kv_ops->delete(ldb_kv, ldb_key);
+	ret = ldb_kv->kv_ops->delete (ldb_kv, ldb_key);
 	TALLOC_FREE(tdb_key_ctx);
 
 	if (ret != 0) {
@@ -899,11 +907,11 @@ static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
 	struct ldb_message_element *el;
 	bool is_special = ldb_dn_is_special(msg->dn);
 
-	if (!is_special
-	    && ldb_kv->cache->GUID_index_attribute != NULL
-	    && ldb_attr_cmp(name, ldb_kv->cache->GUID_index_attribute) == 0) {
+	if (!is_special && ldb_kv->cache->GUID_index_attribute != NULL &&
+	    ldb_attr_cmp(name, ldb_kv->cache->GUID_index_attribute) == 0) {
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb, "Must not modify GUID "
+		ldb_asprintf_errstring(ldb,
+				       "Must not modify GUID "
 				       "attribute %s (used as DB index)",
 				       ldb_kv->cache->GUID_index_attribute);
 		return LDB_ERR_CONSTRAINT_VIOLATION;
@@ -974,7 +982,8 @@ static int ldb_kv_msg_delete_element(struct ldb_module *module,
 				    module, ldb_kv, msg, name);
 			}
 
-			ret = ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
+			ret =
+			    ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
 			if (ret != LDB_SUCCESS) {
 				return ret;
 			}
@@ -1011,7 +1020,8 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_message *msg2;
 	unsigned int i, j;
 	int ret = LDB_SUCCESS, idx;
@@ -1255,7 +1265,8 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 				goto done;
 			}
 
-			ret = ldb_kv_index_add_element(module, ldb_kv, msg2, el);
+			ret =
+			    ldb_kv_index_add_element(module, ldb_kv, msg2, el);
 			if (ret != LDB_SUCCESS) {
 				goto done;
 			}
@@ -1272,7 +1283,10 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 			if (msg->elements[i].num_values == 0) {
 				/* Delete the whole attribute */
 				ret = ldb_kv_msg_delete_attribute(
-				    module, ldb_kv, msg2, msg->elements[i].name);
+				    module,
+				    ldb_kv,
+				    msg2,
+				    msg->elements[i].name);
 				if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
 				    control_permissive) {
 					ret = LDB_SUCCESS;
@@ -1364,7 +1378,8 @@ static int ldb_kv_rename(struct ldb_kv_context *ctx)
 {
 	struct ldb_module *module = ctx->module;
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	struct ldb_request *req = ctx->req;
 	struct ldb_message *msg;
 	int ret = LDB_SUCCESS;
@@ -1476,12 +1491,12 @@ static int ltdb_transaction_start(struct ldb_kv_private *ldb_kv)
 	pid_t pid = getpid();
 
 	if (ldb_kv->pid != pid) {
-		ldb_asprintf_errstring(
-			ldb_module_get_ctx(ldb_kv->module),
-			__location__": Reusing ldb opend by pid %d in "
-			"process %d\n",
-			ldb_kv->pid,
-			pid);
+		ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
+				       __location__
+				       ": Reusing ldb opend by pid %d in "
+				       "process %d\n",
+				       ldb_kv->pid,
+				       pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
@@ -1493,12 +1508,12 @@ static int ltdb_transaction_cancel(struct ldb_kv_private *ldb_kv)
 	pid_t pid = getpid();
 
 	if (ldb_kv->pid != pid) {
-		ldb_asprintf_errstring(
-			ldb_module_get_ctx(ldb_kv->module),
-			__location__": Reusing ldb opend by pid %d in "
-			"process %d\n",
-			ldb_kv->pid,
-			pid);
+		ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
+				       __location__
+				       ": Reusing ldb opend by pid %d in "
+				       "process %d\n",
+				       ldb_kv->pid,
+				       pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
@@ -1510,12 +1525,12 @@ static int ltdb_transaction_prepare_commit(struct ldb_kv_private *ldb_kv)
 	pid_t pid = getpid();
 
 	if (ldb_kv->pid != pid) {
-		ldb_asprintf_errstring(
-			ldb_module_get_ctx(ldb_kv->module),
-			__location__": Reusing ldb opend by pid %d in "
-			"process %d\n",
-			ldb_kv->pid,
-			pid);
+		ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
+				       __location__
+				       ": Reusing ldb opend by pid %d in "
+				       "process %d\n",
+				       ldb_kv->pid,
+				       pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
@@ -1527,12 +1542,12 @@ static int ltdb_transaction_commit(struct ldb_kv_private *ldb_kv)
 	pid_t pid = getpid();
 
 	if (ldb_kv->pid != pid) {
-		ldb_asprintf_errstring(
-			ldb_module_get_ctx(ldb_kv->module),
-			__location__": Reusing ldb opend by pid %d in "
-			"process %d\n",
-			ldb_kv->pid,
-			pid);
+		ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
+				       __location__
+				       ": Reusing ldb opend by pid %d in "
+				       "process %d\n",
+				       ldb_kv->pid,
+				       pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
@@ -1542,17 +1557,18 @@ static int ltdb_transaction_commit(struct ldb_kv_private *ldb_kv)
 static int ldb_kv_start_trans(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 
 	pid_t pid = getpid();
 
 	if (ldb_kv->pid != pid) {
-		ldb_asprintf_errstring(
-			ldb_module_get_ctx(ldb_kv->module),
-			__location__": Reusing ldb opend by pid %d in "
-			"process %d\n",
-			ldb_kv->pid,
-			pid);
+		ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
+				       __location__
+				       ": Reusing ldb opend by pid %d in "
+				       "process %d\n",
+				       ldb_kv->pid,
+				       pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
@@ -1582,16 +1598,17 @@ static int ldb_kv_prepare_commit(struct ldb_module *module)
 {
 	int ret;
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	pid_t pid = getpid();
 
 	if (ldb_kv->pid != pid) {
-		ldb_asprintf_errstring(
-			ldb_module_get_ctx(module),
-			__location__": Reusing ldb opend by pid %d in "
-			"process %d\n",
-			ldb_kv->pid,
-			pid);
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       __location__
+				       ": Reusing ldb opend by pid %d in "
+				       "process %d\n",
+				       ldb_kv->pid,
+				       pid);
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
@@ -1646,7 +1663,8 @@ static int ldb_kv_end_trans(struct ldb_module *module)
 {
 	int ret;
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 
 	if (!ldb_kv->prepared_commit) {
 		ret = ldb_kv_prepare_commit(module);
@@ -1659,10 +1677,11 @@ static int ldb_kv_end_trans(struct ldb_module *module)
 
 	if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
 		ret = ldb_kv->kv_ops->error(ldb_kv);
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       "Failure during tdb_transaction_commit(): %s -> %s",
-				       ldb_kv->kv_ops->errorstr(ldb_kv),
-				       ldb_strerror(ret));
+		ldb_asprintf_errstring(
+		    ldb_module_get_ctx(module),
+		    "Failure during tdb_transaction_commit(): %s -> %s",
+		    ldb_kv->kv_ops->errorstr(ldb_kv),
+		    ldb_strerror(ret));
 		return ret;
 	}
 
@@ -1672,7 +1691,8 @@ static int ldb_kv_end_trans(struct ldb_module *module)
 static int ldb_kv_del_trans(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 
 	if (ldb_kv_index_transaction_cancel(module) != 0) {
 		ldb_kv->kv_ops->abort_write(ldb_kv);
@@ -1693,7 +1713,8 @@ static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
 	struct ldb_module *module = ctx->module;
 	struct ldb_request *req = ctx->req;
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	TALLOC_CTX *tmp_ctx = NULL;
 	struct ldb_seqnum_request *seq;
 	struct ldb_seqnum_result *res;
@@ -1903,10 +1924,7 @@ static int ltdb_traverse_fn(struct ldb_kv_private *ldb_kv,
 			    void *ctx)
 {
 	struct kv_ctx kv_ctx = {
-		.kv_traverse_fn = fn,
-		.ctx = ctx,
-		.ldb_kv = ldb_kv
-	};
+	    .kv_traverse_fn = fn, .ctx = ctx, .ldb_kv = ldb_kv};
 	if (tdb_transaction_active(ldb_kv->tdb)) {
 		return tdb_traverse(
 		    ldb_kv->tdb, ltdb_traverse_fn_wrapper, &kv_ctx);
@@ -1944,12 +1962,15 @@ static int ltdb_update_in_iterate(struct ldb_kv_private *ldb_kv,
 
 	tdb_ret = tdb_delete(ldb_kv->tdb, key);
 	if (tdb_ret != 0) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR,
+		ldb_debug(ldb,
+			  LDB_DEBUG_ERROR,
 			  "Failed to delete %*.*s "
 			  "for rekey as %*.*s: %s",
-			  (int)key.dsize, (int)key.dsize,
+			  (int)key.dsize,
+			  (int)key.dsize,
 			  (const char *)key.dptr,
-			  (int)key2.dsize, (int)key2.dsize,
+			  (int)key2.dsize,
+			  (int)key2.dsize,
 			  (const char *)key.dptr,
 			  tdb_errorstr(ldb_kv->tdb));
 		ctx->error = ltdb_err_map(tdb_error(ldb_kv->tdb));
@@ -1957,11 +1978,14 @@ static int ltdb_update_in_iterate(struct ldb_kv_private *ldb_kv,
 	}
 	tdb_ret = tdb_store(ldb_kv->tdb, key2, data, 0);
 	if (tdb_ret != 0) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR,
+		ldb_debug(ldb,
+			  LDB_DEBUG_ERROR,
 			  "Failed to rekey %*.*s as %*.*s: %s",
-			  (int)key.dsize, (int)key.dsize,
+			  (int)key.dsize,
+			  (int)key.dsize,
 			  (const char *)key.dptr,
-			  (int)key2.dsize, (int)key2.dsize,
+			  (int)key2.dsize,
+			  (int)key2.dsize,
 			  (const char *)key.dptr,
 			  tdb_errorstr(ldb_kv->tdb));
 		ctx->error = ltdb_err_map(tdb_error(ldb_kv->tdb));
@@ -1994,11 +2018,7 @@ static int ltdb_parse_record(struct ldb_kv_private *ldb_kv,
 					   void *private_data),
 			     void *ctx)
 {
-	struct kv_ctx kv_ctx = {
-		.parser = parser,
-		.ctx = ctx,
-		.ldb_kv = ldb_kv
-	};
+	struct kv_ctx kv_ctx = {.parser = parser, .ctx = ctx, .ldb_kv = ldb_kv};
 	TDB_DATA key = {
 		.dptr = ldb_key.data,
 		.dsize = ldb_key.length
@@ -2209,14 +2229,16 @@ static int ldb_kv_init_rootdse(struct ldb_module *module)
 static int ldb_kv_lock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	return ldb_kv->kv_ops->lock_read(module);
 }
 
 static int ldb_kv_unlock_read(struct ldb_module *module)
 {
 	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
 	return ldb_kv->kv_ops->unlock_read(module);
 }
 
@@ -2385,9 +2407,13 @@ int ltdb_connect(struct ldb_context *ldb, const char *url,
 
 	errno = 0;
 	/* note that we use quite a large default hash size */
-	ldb_kv->tdb = ltdb_wrap_open(ldb_kv, path, 10000,
-				   tdb_flags, open_flags,
-				   ldb_get_create_perms(ldb), ldb);
+	ldb_kv->tdb = ltdb_wrap_open(ldb_kv,
+				     path,
+				     10000,
+				     tdb_flags,
+				     open_flags,
+				     ldb_get_create_perms(ldb),
+				     ldb);
 	if (!ldb_kv->tdb) {
 		ldb_asprintf_errstring(ldb,
 				       "Unable to open tdb '%s': %s", path, strerror(errno));
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.h b/lib/ldb/ldb_tdb/ldb_tdb.h
index 930405d..ae9ce1f 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -6,19 +6,30 @@
 
 struct ldb_kv_private;
 typedef int (*ldb_kv_traverse_fn)(struct ldb_kv_private *ldb_kv,
-				  struct ldb_val key, struct ldb_val data,
+				  struct ldb_val key,
+				  struct ldb_val data,
 				  void *ctx);
 
 struct kv_db_ops {
-	int (*store)(struct ldb_kv_private *ldb_kv, struct ldb_val key, struct ldb_val data, int flags);
+	int (*store)(struct ldb_kv_private *ldb_kv,
+		     struct ldb_val key,
+		     struct ldb_val data,
+		     int flags);
 	int (*delete)(struct ldb_kv_private *ldb_kv, struct ldb_val key);
-	int (*iterate)(struct ldb_kv_private *ldb_kv, ldb_kv_traverse_fn fn, void *ctx);
-	int (*update_in_iterate)(struct ldb_kv_private *ldb_kv, struct ldb_val key,
-				 struct ldb_val key2, struct ldb_val data, void *ctx);
-	int (*fetch_and_parse)(struct ldb_kv_private *ldb_kv, struct ldb_val key,
-                               int (*parser)(struct ldb_val key, struct ldb_val data,
-                                             void *private_data),
-                               void *ctx);
+	int (*iterate)(struct ldb_kv_private *ldb_kv,
+		       ldb_kv_traverse_fn fn,
+		       void *ctx);
+	int (*update_in_iterate)(struct ldb_kv_private *ldb_kv,
+				 struct ldb_val key,
+				 struct ldb_val key2,
+				 struct ldb_val data,
+				 void *ctx);
+	int (*fetch_and_parse)(struct ldb_kv_private *ldb_kv,
+			       struct ldb_val key,
+			       int (*parser)(struct ldb_val key,
+					     struct ldb_val data,
+					     void *private_data),
+			       void *ctx);
 	int (*lock_read)(struct ldb_module *);
 	int (*unlock_read)(struct ldb_module *);
 	int (*begin_write)(struct ldb_kv_private *);
@@ -26,8 +37,8 @@ struct kv_db_ops {
 	int (*abort_write)(struct ldb_kv_private *);
 	int (*finish_write)(struct ldb_kv_private *);
 	int (*error)(struct ldb_kv_private *ldb_kv);
-	const char * (*errorstr)(struct ldb_kv_private *ldb_kv);
-	const char * (*name)(struct ldb_kv_private *ldb_kv);
+	const char *(*errorstr)(struct ldb_kv_private *ldb_kv);
+	const char *(*name)(struct ldb_kv_private *ldb_kv);
 	bool (*has_changed)(struct ldb_kv_private *ldb_kv);
 	bool (*transaction_active)(struct ldb_kv_private *ldb_kv);
 };
diff --git a/lib/ldb/tests/ldb_kv_ops_test.c b/lib/ldb/tests/ldb_kv_ops_test.c
index 74aaf03..36ea3bf 100644
--- a/lib/ldb/tests/ldb_kv_ops_test.c
+++ b/lib/ldb/tests/ldb_kv_ops_test.c
@@ -390,7 +390,7 @@ static void test_delete(void **state)
 	/*
 	 * Now delete it.
 	 */
-	ret = ldb_kv->kv_ops->delete(ldb_kv, key);
+	ret = ldb_kv->kv_ops->delete (ldb_kv, key);
 	assert_int_equal(ret, 0);
 
 	/*
@@ -551,7 +551,7 @@ static void test_transaction_abort_delete(void **state)
 	/*
 	 * Now delete it.
 	 */
-	ret = ldb_kv->kv_ops->delete(ldb_kv, key);
+	ret = ldb_kv->kv_ops->delete (ldb_kv, key);
 	assert_int_equal(ret, 0);
 
 	/*
@@ -680,7 +680,7 @@ static void test_delete_outside_transaction(void **state)
 	/*
 	 * Now attempt to delete a record
 	 */
-	ret = ldb_kv->kv_ops->delete(ldb_kv, key);
+	ret = ldb_kv->kv_ops->delete (ldb_kv, key);
 	assert_int_equal(ret, LDB_ERR_PROTOCOL_ERROR);
 
 	/*
@@ -701,7 +701,8 @@ static void test_delete_outside_transaction(void **state)
 static int traverse_fn(struct ldb_kv_private *ldb_kv,
 		       struct ldb_val key,
 		       struct ldb_val data,
-		       void *ctx) {
+		       void *ctx)
+{
 
 	int *visits = ctx;
 	int i;
@@ -792,7 +793,8 @@ struct update_context {
 static int update_fn(struct ldb_kv_private *ldb_kv,
 		     struct ldb_val key,
 		     struct ldb_val data,
-		     void *ctx) {
+		     void *ctx)
+{
 
 	struct ldb_val new_key;
 	struct ldb_module *module = NULL;
@@ -815,11 +817,8 @@ static int update_fn(struct ldb_kv_private *ldb_kv,
 		new_key.length  = key.length;
 		new_key.data[0] = 'K';
 
-		ret = ldb_kv->kv_ops->update_in_iterate(ldb_kv,
-						      key,
-						      new_key,
-						      data,
-						      &module);
+		ret = ldb_kv->kv_ops->update_in_iterate(
+		    ldb_kv, key, new_key, data, &module);
 	}
 	TALLOC_FREE(tmp_ctx);
 	return ret;
@@ -1484,7 +1483,7 @@ static void test_delete_transaction_isolation(void **state)
 	key.data = (uint8_t *)talloc_strdup(tmp_ctx, KEY2);
 	key.length = strlen(KEY2) + 1;
 
-	ret = ldb_kv->kv_ops->delete(ldb_kv, key);
+	ret = ldb_kv->kv_ops->delete (ldb_kv, key);
 	assert_int_equal(ret, 0);
 	/*
 	 * Signal the child process
-- 
2.7.4


From 85d5018547afcf7bdbbdfddf9701f83cba3393de Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 13:31:27 +1200
Subject: [PATCH 10/18] lib ldb: rename ltdb_cache to ldb_kv_cache

Rename ltdb_cache to ldb_kv_cache as it's key value level and not tdb
specific

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_tdb/ldb_cache.c | 2 +-
 lib/ldb/ldb_tdb/ldb_tdb.h   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/ldb/ldb_tdb/ldb_cache.c b/lib/ldb/ldb_tdb/ldb_cache.c
index 6c61e59..9e81b61 100644
--- a/lib/ldb/ldb_tdb/ldb_cache.c
+++ b/lib/ldb/ldb_tdb/ldb_cache.c
@@ -418,7 +418,7 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	}
 
 	if (ldb_kv->cache == NULL) {
-		ldb_kv->cache = talloc_zero(ldb_kv, struct ltdb_cache);
+		ldb_kv->cache = talloc_zero(ldb_kv, struct ldb_kv_cache);
 		if (ldb_kv->cache == NULL)
 			goto failed;
 	}
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.h b/lib/ldb/ldb_tdb/ldb_tdb.h
index ae9ce1f..f2a091b 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -58,7 +58,7 @@ struct ldb_kv_private {
 	   possible */
 	int tdb_seqnum;
 
-	struct ltdb_cache {
+	struct ldb_kv_cache {
 		struct ldb_message *indexlist;
 		bool one_level_indexes;
 		bool attribute_indexes;
-- 
2.7.4


From fda8f564ef139d2d5ddcf5226acebeeb1cbcd72a Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 13:36:07 +1200
Subject: [PATCH 11/18] lib ldb: rename tdb_key_ctx to key_ctx

Rename tdb_key_ctx to key_ctx, as it's key value level and not tdb
specific.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_tdb/ldb_tdb.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/lib/ldb/ldb_tdb/ldb_tdb.c b/lib/ldb/ldb_tdb/ldb_tdb.c
index b963123..9519090 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -492,27 +492,27 @@ int ldb_kv_store(struct ldb_module *module,
 	struct ldb_val ldb_key;
 	struct ldb_val ldb_data;
 	int ret = LDB_SUCCESS;
-	TALLOC_CTX *tdb_key_ctx = talloc_new(module);
+	TALLOC_CTX *key_ctx = talloc_new(module);
 
-	if (tdb_key_ctx == NULL) {
+	if (key_ctx == NULL) {
 		return ldb_module_oom(module);
 	}
 
 	if (ldb_kv->read_only) {
-		talloc_free(tdb_key_ctx);
+		talloc_free(key_ctx);
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
-	tdb_key = ldb_kv_key_msg(module, tdb_key_ctx, msg);
+	tdb_key = ldb_kv_key_msg(module, key_ctx, msg);
 	if (tdb_key.dptr == NULL) {
-		TALLOC_FREE(tdb_key_ctx);
+		TALLOC_FREE(key_ctx);
 		return LDB_ERR_OTHER;
 	}
 
 	ret = ldb_pack_data(ldb_module_get_ctx(module),
 			    msg, &ldb_data);
 	if (ret == -1) {
-		TALLOC_FREE(tdb_key_ctx);
+		TALLOC_FREE(key_ctx);
 		return LDB_ERR_OTHER;
 	}
 
@@ -536,7 +536,7 @@ int ldb_kv_store(struct ldb_module *module,
 	}
 
 done:
-	TALLOC_FREE(tdb_key_ctx);
+	TALLOC_FREE(key_ctx);
 	talloc_free(ldb_data.data);
 
 	return ret;
-- 
2.7.4


From d04e9288245e639f1c41b7792f5a82fb35eeaf14 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 13:40:02 +1200
Subject: [PATCH 12/18] lib ldb: rename ltdb_idxptr to ldb_kv_idxptr

Rename ltdb_idxptr to ldb_kv_idxptr as it's key value level and not tdb
specific.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_tdb/ldb_index.c | 4 ++--
 lib/ldb/ldb_tdb/ldb_tdb.h   | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/ldb/ldb_tdb/ldb_index.c b/lib/ldb/ldb_tdb/ldb_index.c
index 1c3a7a0..ce107da 100644
--- a/lib/ldb/ldb_tdb/ldb_index.c
+++ b/lib/ldb/ldb_tdb/ldb_index.c
@@ -160,7 +160,7 @@ struct dn_list {
 	bool strict;
 };
 
-struct ltdb_idxptr {
+struct ldb_kv_idxptr {
 	struct tdb_context *itdb;
 	int error;
 };
@@ -202,7 +202,7 @@ int ldb_kv_index_transaction_start(struct ldb_module *module)
 {
 	struct ldb_kv_private *ldb_kv = talloc_get_type(
 	    ldb_module_get_private(module), struct ldb_kv_private);
-	ldb_kv->idxptr = talloc_zero(ldb_kv, struct ltdb_idxptr);
+	ldb_kv->idxptr = talloc_zero(ldb_kv, struct ldb_kv_idxptr);
 	if (ldb_kv->idxptr == NULL) {
 		return ldb_oom(ldb_module_get_ctx(module));
 	}
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.h b/lib/ldb/ldb_tdb/ldb_tdb.h
index f2a091b..2ee14f7 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -69,7 +69,7 @@ struct ldb_kv_private {
 
 	bool check_base;
 	bool disallow_dn_filter;
-	struct ltdb_idxptr *idxptr;
+	struct ldb_kv_idxptr *idxptr;
 	bool prepared_commit;
 	int read_lock_count;
 
-- 
2.7.4


From f3f538fdcb581aa8ed7b8b5505d6db361fd32805 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 13:47:16 +1200
Subject: [PATCH 13/18] lib ldb: remove unused function prototypes

Remove unused function prototypes

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_tdb/ldb_tdb.h | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/lib/ldb/ldb_tdb/ldb_tdb.h b/lib/ldb/ldb_tdb/ldb_tdb.h
index 2ee14f7..4cbc7c2 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -203,10 +203,6 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 			   TDB_DATA *tdb_key);
 
 /* The following definitions come from lib/ldb/ldb_tdb/ldb_search.c  */
-
-int ltdb_has_wildcard(struct ldb_module *module, const char *attr_name, 
-		      const struct ldb_val *val);
-void ltdb_search_dn1_free(struct ldb_module *module, struct ldb_message *msg);
 int ldb_kv_search_dn1(struct ldb_module *module,
 		      struct ldb_dn *dn,
 		      struct ldb_message *msg,
-- 
2.7.4


From 2e05d57dd70ef03ec6846268e4c81341f8ac2a72 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Fri, 20 Jul 2018 13:50:00 +1200
Subject: [PATCH 14/18] lib ldb: rename ltdb_parse_data_unpack_ctx

Rename ltdb_parse_data_unpack_ctx to ldb_kv_parse_data_unpack_ctx, as
it's a key value level structure and not ltdb specific.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_tdb/ldb_search.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/ldb/ldb_tdb/ldb_search.c b/lib/ldb/ldb_tdb/ldb_search.c
index b3eafeb..0138f92 100644
--- a/lib/ldb/ldb_tdb/ldb_search.c
+++ b/lib/ldb/ldb_tdb/ldb_search.c
@@ -175,7 +175,7 @@ int ldb_kv_search_base(struct ldb_module *module,
 	return LDB_ERR_NO_SUCH_OBJECT;
 }
 
-struct ltdb_parse_data_unpack_ctx {
+struct ldb_kv_parse_data_unpack_ctx {
 	struct ldb_message *msg;
 	struct ldb_module *module;
 	unsigned int unpack_flags;
@@ -185,7 +185,7 @@ static int ldb_kv_parse_data_unpack(struct ldb_val key,
 				    struct ldb_val data,
 				    void *private_data)
 {
-	struct ltdb_parse_data_unpack_ctx *ctx = private_data;
+	struct ldb_kv_parse_data_unpack_ctx *ctx = private_data;
 	unsigned int nb_elements_in_db;
 	int ret;
 	struct ldb_context *ldb = ldb_module_get_ctx(ctx->module);
@@ -241,7 +241,7 @@ int ldb_kv_search_key(struct ldb_module *module,
 		      unsigned int unpack_flags)
 {
 	int ret;
-	struct ltdb_parse_data_unpack_ctx ctx = {
+	struct ldb_kv_parse_data_unpack_ctx ctx = {
 		.msg = msg,
 		.module = module,
 		.unpack_flags = unpack_flags
-- 
2.7.4


From 6157729babf91b13694725f7bc266171fe1656f1 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Mon, 23 Jul 2018 10:02:16 +1200
Subject: [PATCH 15/18] lib ldb: move key value code to lib/ldb/ldb_key_value

Move the key value code to a separate subdirectory.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_key_value/ldb_kv.c        | 1933 ++++++++++++++++++++
 lib/ldb/ldb_key_value/ldb_kv.h        |  265 +++
 lib/ldb/ldb_key_value/ldb_kv_cache.c  |  662 +++++++
 lib/ldb/ldb_key_value/ldb_kv_index.c  | 3123 +++++++++++++++++++++++++++++++++
 lib/ldb/ldb_key_value/ldb_kv_search.c |  864 +++++++++
 lib/ldb/ldb_mdb/ldb_mdb.c             |    2 +-
 lib/ldb/ldb_tdb/ldb_cache.c           |  663 -------
 lib/ldb/ldb_tdb/ldb_index.c           | 3122 --------------------------------
 lib/ldb/ldb_tdb/ldb_search.c          |  866 ---------
 lib/ldb/ldb_tdb/ldb_tdb.c             | 1953 +--------------------
 lib/ldb/ldb_tdb/ldb_tdb.h             |  253 ---
 lib/ldb/ldb_tdb/ldb_tdb_err_map.c     |   84 +
 lib/ldb/tests/ldb_kv_ops_test.c       |    1 +
 lib/ldb/tests/ldb_lmdb_test.c         |    1 +
 lib/ldb/tests/ldb_tdb_test.c          |    1 +
 lib/ldb/wscript                       |   24 +-
 16 files changed, 6973 insertions(+), 6844 deletions(-)
 create mode 100644 lib/ldb/ldb_key_value/ldb_kv.c
 create mode 100644 lib/ldb/ldb_key_value/ldb_kv.h
 create mode 100644 lib/ldb/ldb_key_value/ldb_kv_cache.c
 create mode 100644 lib/ldb/ldb_key_value/ldb_kv_index.c
 create mode 100644 lib/ldb/ldb_key_value/ldb_kv_search.c
 delete mode 100644 lib/ldb/ldb_tdb/ldb_cache.c
 delete mode 100644 lib/ldb/ldb_tdb/ldb_index.c
 delete mode 100644 lib/ldb/ldb_tdb/ldb_search.c
 create mode 100644 lib/ldb/ldb_tdb/ldb_tdb_err_map.c

diff --git a/lib/ldb/ldb_key_value/ldb_kv.c b/lib/ldb/ldb_key_value/ldb_kv.c
new file mode 100644
index 0000000..2449ec2
--- /dev/null
+++ b/lib/ldb/ldb_key_value/ldb_kv.c
@@ -0,0 +1,1933 @@
+/*
+   ldb database library
+
+   Copyright (C) Andrew Tridgell 2004
+   Copyright (C) Stefan Metzmacher 2004
+   Copyright (C) Simo Sorce 2006-2008
+   Copyright (C) Matthias Dieter Wallnöfer 2009-2010
+
+     ** NOTE! The following LGPL license applies to the ldb
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ *  Name: ldb_kv
+ *
+ *  Component: ldb key value backend
+ *
+ *  Description: core functions for ldb key value backend
+ *
+ *  Author: Andrew Tridgell
+ *  Author: Stefan Metzmacher
+ *
+ *  Modifications:
+ *
+ *  - description: make the module use asynchronous calls
+ *    date: Feb 2006
+ *    Author: Simo Sorce
+ *
+ *  - description: make it possible to use event contexts
+ *    date: Jan 2008
+ *    Author: Simo Sorce
+ *
+ *  - description: fix up memory leaks and small bugs
+ *    date: Oct 2009
+ *    Author: Matthias Dieter Wallnöfer
+ */
+
+#include "ldb_kv.h"
+#include "ldb_private.h"
+
+/*
+  prevent memory errors on callbacks
+*/
+struct ldb_kv_req_spy {
+	struct ldb_kv_context *ctx;
+};
+
+/*
+ * Determine if this key could hold a record.  We allow the new GUID
+ * index, the old DN index and a possible future ID=
+ */
+bool ldb_kv_key_is_record(TDB_DATA key)
+{
+	if (key.dsize < 4) {
+		return false;
+	}
+
+	if (memcmp(key.dptr, "DN=", 3) == 0) {
+		return true;
+	}
+
+	if (memcmp(key.dptr, "ID=", 3) == 0) {
+		return true;
+	}
+
+	if (key.dsize < sizeof(LTDB_GUID_KEY_PREFIX)) {
+		return false;
+	}
+
+	if (memcmp(key.dptr, LTDB_GUID_KEY_PREFIX,
+		   sizeof(LTDB_GUID_KEY_PREFIX) - 1) == 0) {
+		return true;
+	}
+
+	return false;
+}
+
+/*
+  form a TDB_DATA for a record key
+  caller frees
+
+  note that the key for a record can depend on whether the
+  dn refers to a case sensitive index record or not
+*/
+TDB_DATA ldb_kv_key_dn(struct ldb_module *module,
+		       TALLOC_CTX *mem_ctx,
+		       struct ldb_dn *dn)
+{
+	TDB_DATA key;
+	char *key_str = NULL;
+	const char *dn_folded = NULL;
+
+	/*
+	  most DNs are case insensitive. The exception is index DNs for
+	  case sensitive attributes
+
+	  there are 3 cases dealt with in this code:
+
+	  1) if the dn doesn't start with @ then uppercase the attribute
+             names and the attributes values of case insensitive attributes
+	  2) if the dn starts with @ then leave it alone -
+	     the indexing code handles the rest
+	*/
+
+	dn_folded = ldb_dn_get_casefold(dn);
+	if (!dn_folded) {
+		goto failed;
+	}
+
+	key_str = talloc_strdup(mem_ctx, "DN=");
+	if (!key_str) {
+		goto failed;
+	}
+
+	key_str = talloc_strdup_append_buffer(key_str, dn_folded);
+	if (!key_str) {
+		goto failed;
+	}
+
+	key.dptr = (uint8_t *)key_str;
+	key.dsize = strlen(key_str) + 1;
+
+	return key;
+
+failed:
+	errno = ENOMEM;
+	key.dptr = NULL;
+	key.dsize = 0;
+	return key;
+}
+
+/* The caller is to provide a correctly sized key */
+int ldb_kv_guid_to_key(struct ldb_module *module,
+		       struct ldb_kv_private *ldb_kv,
+		       const struct ldb_val *GUID_val,
+		       TDB_DATA *key)
+{
+	const char *GUID_prefix = LTDB_GUID_KEY_PREFIX;
+	const int GUID_prefix_len = sizeof(LTDB_GUID_KEY_PREFIX) - 1;
+
+	if (key->dsize != (GUID_val->length+GUID_prefix_len)) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	memcpy(key->dptr, GUID_prefix, GUID_prefix_len);
+	memcpy(&key->dptr[GUID_prefix_len],
+	       GUID_val->data, GUID_val->length);
+	return LDB_SUCCESS;
+}
+
+/*
+ * The caller is to provide a correctly sized key, used only in
+ * the GUID index mode
+ */
+int ldb_kv_idx_to_key(struct ldb_module *module,
+		      struct ldb_kv_private *ldb_kv,
+		      TALLOC_CTX *mem_ctx,
+		      const struct ldb_val *idx_val,
+		      TDB_DATA *key)
+{
+	struct ldb_context *ldb = ldb_module_get_ctx(module);
+	struct ldb_dn *dn;
+
+	if (ldb_kv->cache->GUID_index_attribute != NULL) {
+		return ldb_kv_guid_to_key(module, ldb_kv, idx_val, key);
+	}
+
+	dn = ldb_dn_from_ldb_val(mem_ctx, ldb, idx_val);
+	if (dn == NULL) {
+		/*
+		 * LDB_ERR_INVALID_DN_SYNTAX would just be confusing
+		 * to the caller, as this in an invalid index value
+		 */
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	/* form the key */
+	*key = ldb_kv_key_dn(module, mem_ctx, dn);
+	TALLOC_FREE(dn);
+	if (!key->dptr) {
+		return ldb_module_oom(module);
+	}
+	return LDB_SUCCESS;
+}
+
+/*
+  form a TDB_DATA for a record key
+  caller frees mem_ctx, which may or may not have the key
+  as a child.
+
+  note that the key for a record can depend on whether a
+  GUID index is in use, or the DN is used as the key
+*/
+TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
+			TALLOC_CTX *mem_ctx,
+			const struct ldb_message *msg)
+{
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	TDB_DATA key;
+	const struct ldb_val *guid_val;
+	int ret;
+
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
+		return ldb_kv_key_dn(module, mem_ctx, msg->dn);
+	}
+
+	if (ldb_dn_is_special(msg->dn)) {
+		return ldb_kv_key_dn(module, mem_ctx, msg->dn);
+	}
+
+	guid_val =
+	    ldb_msg_find_ldb_val(msg, ldb_kv->cache->GUID_index_attribute);
+	if (guid_val == NULL) {
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       "Did not find GUID attribute %s "
+				       "in %s, required for TDB record "
+				       "key in " LTDB_IDXGUID " mode.",
+				       ldb_kv->cache->GUID_index_attribute,
+				       ldb_dn_get_linearized(msg->dn));
+		errno = EINVAL;
+		key.dptr = NULL;
+		key.dsize = 0;
+		return key;
+	}
+
+	/* In this case, allocate with talloc */
+	key.dptr = talloc_size(mem_ctx, LTDB_GUID_KEY_SIZE);
+	if (key.dptr == NULL) {
+		errno = ENOMEM;
+		key.dptr = NULL;
+		key.dsize = 0;
+		return key;
+	}
+	key.dsize = talloc_get_size(key.dptr);
+
+	ret = ldb_kv_guid_to_key(module, ldb_kv, guid_val, &key);
+
+	if (ret != LDB_SUCCESS) {
+		errno = EINVAL;
+		key.dptr = NULL;
+		key.dsize = 0;
+		return key;
+	}
+	return key;
+}
+
+/*
+  check special dn's have valid attributes
+  currently only @ATTRIBUTES is checked
+*/
+static int ldb_kv_check_special_dn(struct ldb_module *module,
+				   const struct ldb_message *msg)
+{
+	struct ldb_context *ldb = ldb_module_get_ctx(module);
+	unsigned int i, j;
+
+	if (! ldb_dn_is_special(msg->dn) ||
+	    ! ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
+		return LDB_SUCCESS;
+	}
+
+	/* we have @ATTRIBUTES, let's check attributes are fine */
+	/* should we check that we deny multivalued attributes ? */
+	for (i = 0; i < msg->num_elements; i++) {
+		if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) continue;
+
+		for (j = 0; j < msg->elements[i].num_values; j++) {
+			if (ldb_kv_check_at_attributes_values(
+				&msg->elements[i].values[j]) != 0) {
+				ldb_set_errstring(ldb, "Invalid attribute value in an @ATTRIBUTES entry");
+				return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
+			}
+		}
+	}
+
+	return LDB_SUCCESS;
+}
+
+
+/*
+  we've made a modification to a dn - possibly reindex and
+  update sequence number
+*/
+static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
+{
+	int ret = LDB_SUCCESS;
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
+
+	/* only allow modifies inside a transaction, otherwise the
+	 * ldb is unsafe */
+	if (ldb_kv->kv_ops->transaction_active(ldb_kv) == false) {
+		ldb_set_errstring(ldb_module_get_ctx(module), "ltdb modify without transaction");
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	if (ldb_dn_is_special(dn) &&
+	    (ldb_dn_check_special(dn, LTDB_INDEXLIST) ||
+	     ldb_dn_check_special(dn, LTDB_ATTRIBUTES)) )
+	{
+		if (ldb_kv->warn_reindex) {
+			ldb_debug(ldb_module_get_ctx(module),
+				  LDB_DEBUG_ERROR,
+				  "Reindexing %s due to modification on %s",
+				  ldb_kv->kv_ops->name(ldb_kv),
+				  ldb_dn_get_linearized(dn));
+		}
+		ret = ldb_kv_reindex(module);
+	}
+
+	/* If the modify was to a normal record, or any special except @BASEINFO, update the seq number */
+	if (ret == LDB_SUCCESS &&
+	    !(ldb_dn_is_special(dn) &&
+	      ldb_dn_check_special(dn, LTDB_BASEINFO)) ) {
+		ret = ldb_kv_increase_sequence_number(module);
+	}
+
+	/* If the modify was to @OPTIONS, reload the cache */
+	if (ret == LDB_SUCCESS &&
+	    ldb_dn_is_special(dn) &&
+	    (ldb_dn_check_special(dn, LTDB_OPTIONS)) ) {
+		ret = ldb_kv_cache_reload(module);
+	}
+
+	if (ret != LDB_SUCCESS) {
+		ldb_kv->reindex_failed = true;
+	}
+
+	return ret;
+}
+/*
+  store a record into the db
+*/
+int ldb_kv_store(struct ldb_module *module,
+		 const struct ldb_message *msg,
+		 int flgs)
+{
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	TDB_DATA tdb_key;
+	struct ldb_val ldb_key;
+	struct ldb_val ldb_data;
+	int ret = LDB_SUCCESS;
+	TALLOC_CTX *key_ctx = talloc_new(module);
+
+	if (key_ctx == NULL) {
+		return ldb_module_oom(module);
+	}
+
+	if (ldb_kv->read_only) {
+		talloc_free(key_ctx);
+		return LDB_ERR_UNWILLING_TO_PERFORM;
+	}
+
+	tdb_key = ldb_kv_key_msg(module, key_ctx, msg);
+	if (tdb_key.dptr == NULL) {
+		TALLOC_FREE(key_ctx);
+		return LDB_ERR_OTHER;
+	}
+
+	ret = ldb_pack_data(ldb_module_get_ctx(module),
+			    msg, &ldb_data);
+	if (ret == -1) {
+		TALLOC_FREE(key_ctx);
+		return LDB_ERR_OTHER;
+	}
+
+	ldb_key.data = tdb_key.dptr;
+	ldb_key.length = tdb_key.dsize;
+
+	ret = ldb_kv->kv_ops->store(ldb_kv, ldb_key, ldb_data, flgs);
+	if (ret != 0) {
+		bool is_special = ldb_dn_is_special(msg->dn);
+		ret = ldb_kv->kv_ops->error(ldb_kv);
+
+		/*
+		 * LDB_ERR_ENTRY_ALREADY_EXISTS means the DN, not
+		 * the GUID, so re-map
+		 */
+		if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS && !is_special &&
+		    ldb_kv->cache->GUID_index_attribute != NULL) {
+			ret = LDB_ERR_CONSTRAINT_VIOLATION;
+		}
+		goto done;
+	}
+
+done:
+	TALLOC_FREE(key_ctx);
+	talloc_free(ldb_data.data);
+
+	return ret;
+}
+
+
+/*
+  check if a attribute is a single valued, for a given element
+ */
+static bool ldb_kv_single_valued(const struct ldb_schema_attribute *a,
+				 struct ldb_message_element *el)
+{
+	if (!a) return false;
+	if (el != NULL) {
+		if (el->flags & LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK) {
+			/* override from a ldb module, for example
+			   used for the description field, which is
+			   marked multi-valued in the schema but which
+			   should not actually accept multiple
+			   values */
+			return true;
+		}
+		if (el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK) {
+			/* override from a ldb module, for example used for
+			   deleted linked attribute entries */
+			return false;
+		}
+	}
+	if (a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
+		return true;
+	}
+	return false;
+}
+
+static int ldb_kv_add_internal(struct ldb_module *module,
+			       struct ldb_kv_private *ldb_kv,
+			       const struct ldb_message *msg,
+			       bool check_single_value)
+{
+	struct ldb_context *ldb = ldb_module_get_ctx(module);
+	int ret = LDB_SUCCESS;
+	unsigned int i;
+
+	for (i=0;i<msg->num_elements;i++) {
+		struct ldb_message_element *el = &msg->elements[i];
+		const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
+
+		if (el->num_values == 0) {
+			ldb_asprintf_errstring(ldb, "attribute '%s' on '%s' specified, but with 0 values (illegal)",
+					       el->name, ldb_dn_get_linearized(msg->dn));
+			return LDB_ERR_CONSTRAINT_VIOLATION;
+		}
+		if (check_single_value && el->num_values > 1 &&
+		    ldb_kv_single_valued(a, el)) {
+			ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
+					       el->name, ldb_dn_get_linearized(msg->dn));
+			return LDB_ERR_CONSTRAINT_VIOLATION;
+		}
+
+		/* Do not check "@ATTRIBUTES" for duplicated values */
+		if (ldb_dn_is_special(msg->dn) &&
+		    ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
+			continue;
+		}
+
+		if (check_single_value &&
+		    !(el->flags &
+		      LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
+			struct ldb_val *duplicate = NULL;
+
+			ret = ldb_msg_find_duplicate_val(ldb, discard_const(msg),
+							 el, &duplicate, 0);
+			if (ret != LDB_SUCCESS) {
+				return ret;
+			}
+			if (duplicate != NULL) {
+				ldb_asprintf_errstring(
+					ldb,
+					"attribute '%s': value '%.*s' on '%s' "
+					"provided more than once in ADD object",
+					el->name,
+					(int)duplicate->length,
+					duplicate->data,
+					ldb_dn_get_linearized(msg->dn));
+				return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
+			}
+		}
+	}
+
+	ret = ldb_kv_store(module, msg, TDB_INSERT);
+	if (ret != LDB_SUCCESS) {
+		/*
+		 * Try really hard to get the right error code for
+		 * a re-add situation, as this can matter!
+		 */
+		if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
+			int ret2;
+			struct ldb_dn *dn2 = NULL;
+			TALLOC_CTX *mem_ctx = talloc_new(module);
+			if (mem_ctx == NULL) {
+				return ldb_module_operr(module);
+			}
+			ret2 =
+			    ldb_kv_search_base(module, mem_ctx, msg->dn, &dn2);
+			TALLOC_FREE(mem_ctx);
+			if (ret2 == LDB_SUCCESS) {
+				ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
+			}
+		}
+		if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
+			ldb_asprintf_errstring(ldb,
+					       "Entry %s already exists",
+					       ldb_dn_get_linearized(msg->dn));
+		}
+		return ret;
+	}
+
+	ret = ldb_kv_index_add_new(module, ldb_kv, msg);
+	if (ret != LDB_SUCCESS) {
+		/*
+		 * If we failed to index, delete the message again.
+		 *
+		 * This is particularly important for the GUID index
+		 * case, which will only fail for a duplicate DN
+		 * in the index add.
+		 *
+		 * Note that the caller may not cancel the transation
+		 * and this means the above add might really show up!
+		 */
+		ldb_kv_delete_noindex(module, msg);
+		return ret;
+	}
+
+	ret = ldb_kv_modified(module, msg->dn);
+
+	return ret;
+}
+
+/*
+  add a record to the database
+*/
+static int ldb_kv_add(struct ldb_kv_context *ctx)
+{
+	struct ldb_module *module = ctx->module;
+	struct ldb_request *req = ctx->req;
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	int ret = LDB_SUCCESS;
+
+	if (ldb_kv->max_key_length != 0 &&
+	    ldb_kv->cache->GUID_index_attribute == NULL &&
+	    !ldb_dn_is_special(req->op.add.message->dn)) {
+		ldb_set_errstring(ldb_module_get_ctx(module),
+				  "Must operate ldb_mdb in GUID "
+				  "index mode, but " LTDB_IDXGUID " not set.");
+		return LDB_ERR_UNWILLING_TO_PERFORM;
+	}
+
+	ret = ldb_kv_check_special_dn(module, req->op.add.message);
+	if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	ldb_request_set_state(req, LDB_ASYNC_PENDING);
+
+	if (ldb_kv_cache_load(module) != 0) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ret = ldb_kv_add_internal(module, ldb_kv, req->op.add.message, true);
+
+	return ret;
+}
+
+/*
+  delete a record from the database, not updating indexes (used for deleting
+  index records)
+*/
+int ldb_kv_delete_noindex(struct ldb_module *module,
+			  const struct ldb_message *msg)
+{
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_val ldb_key;
+	TDB_DATA tdb_key;
+	int ret;
+	TALLOC_CTX *tdb_key_ctx = talloc_new(module);
+
+	if (tdb_key_ctx == NULL) {
+		return ldb_module_oom(module);
+	}
+
+	if (ldb_kv->read_only) {
+		talloc_free(tdb_key_ctx);
+		return LDB_ERR_UNWILLING_TO_PERFORM;
+	}
+
+	tdb_key = ldb_kv_key_msg(module, tdb_key_ctx, msg);
+	if (!tdb_key.dptr) {
+		TALLOC_FREE(tdb_key_ctx);
+		return LDB_ERR_OTHER;
+	}
+
+	ldb_key.data = tdb_key.dptr;
+	ldb_key.length = tdb_key.dsize;
+
+	ret = ldb_kv->kv_ops->delete (ldb_kv, ldb_key);
+	TALLOC_FREE(tdb_key_ctx);
+
+	if (ret != 0) {
+		ret = ldb_kv->kv_ops->error(ldb_kv);
+	}
+
+	return ret;
+}
+
+static int ldb_kv_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
+{
+	struct ldb_message *msg;
+	int ret = LDB_SUCCESS;
+
+	msg = ldb_msg_new(module);
+	if (msg == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	/* in case any attribute of the message was indexed, we need
+	   to fetch the old record */
+	ret = ldb_kv_search_dn1(
+	    module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
+	if (ret != LDB_SUCCESS) {
+		/* not finding the old record is an error */
+		goto done;
+	}
+
+	ret = ldb_kv_delete_noindex(module, msg);
+	if (ret != LDB_SUCCESS) {
+		goto done;
+	}
+
+	/* remove any indexed attributes */
+	ret = ldb_kv_index_delete(module, msg);
+	if (ret != LDB_SUCCESS) {
+		goto done;
+	}
+
+	ret = ldb_kv_modified(module, dn);
+	if (ret != LDB_SUCCESS) {
+		goto done;
+	}
+
+done:
+	talloc_free(msg);
+	return ret;
+}
+
+/*
+  delete a record from the database
+*/
+static int ldb_kv_delete(struct ldb_kv_context *ctx)
+{
+	struct ldb_module *module = ctx->module;
+	struct ldb_request *req = ctx->req;
+	int ret = LDB_SUCCESS;
+
+	ldb_request_set_state(req, LDB_ASYNC_PENDING);
+
+	if (ldb_kv_cache_load(module) != 0) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ret = ldb_kv_delete_internal(module, req->op.del.dn);
+
+	return ret;
+}
+
+/*
+  find an element by attribute name. At the moment this does a linear search,
+  it should be re-coded to use a binary search once all places that modify
+  records guarantee sorted order
+
+  return the index of the first matching element if found, otherwise -1
+*/
+static int ldb_kv_find_element(const struct ldb_message *msg, const char *name)
+{
+	unsigned int i;
+	for (i=0;i<msg->num_elements;i++) {
+		if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
+			return i;
+		}
+	}
+	return -1;
+}
+
+
+/*
+  add an element to an existing record. Assumes a elements array that we
+  can call re-alloc on, and assumed that we can re-use the data pointers from
+  the passed in additional values. Use with care!
+
+  returns 0 on success, -1 on failure (and sets errno)
+*/
+static int ldb_kv_msg_add_element(struct ldb_message *msg,
+				  struct ldb_message_element *el)
+{
+	struct ldb_message_element *e2;
+	unsigned int i;
+
+	if (el->num_values == 0) {
+		/* nothing to do here - we don't add empty elements */
+		return 0;
+	}
+
+	e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
+			      msg->num_elements+1);
+	if (!e2) {
+		errno = ENOMEM;
+		return -1;
+	}
+
+	msg->elements = e2;
+
+	e2 = &msg->elements[msg->num_elements];
+
+	e2->name = el->name;
+	e2->flags = el->flags;
+	e2->values = talloc_array(msg->elements,
+				  struct ldb_val, el->num_values);
+	if (!e2->values) {
+		errno = ENOMEM;
+		return -1;
+	}
+	for (i=0;i<el->num_values;i++) {
+		e2->values[i] = el->values[i];
+	}
+	e2->num_values = el->num_values;
+
+	++msg->num_elements;
+
+	return 0;
+}
+
+/*
+  delete all elements having a specified attribute name
+*/
+static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
+				       struct ldb_kv_private *ldb_kv,
+				       struct ldb_message *msg,
+				       const char *name)
+{
+	unsigned int i;
+	int ret;
+	struct ldb_message_element *el;
+	bool is_special = ldb_dn_is_special(msg->dn);
+
+	if (!is_special && ldb_kv->cache->GUID_index_attribute != NULL &&
+	    ldb_attr_cmp(name, ldb_kv->cache->GUID_index_attribute) == 0) {
+		struct ldb_context *ldb = ldb_module_get_ctx(module);
+		ldb_asprintf_errstring(ldb,
+				       "Must not modify GUID "
+				       "attribute %s (used as DB index)",
+				       ldb_kv->cache->GUID_index_attribute);
+		return LDB_ERR_CONSTRAINT_VIOLATION;
+	}
+
+	el = ldb_msg_find_element(msg, name);
+	if (el == NULL) {
+		return LDB_ERR_NO_SUCH_ATTRIBUTE;
+	}
+	i = el - msg->elements;
+
+	ret = ldb_kv_index_del_element(module, ldb_kv, msg, el);
+	if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	talloc_free(el->values);
+	if (msg->num_elements > (i+1)) {
+		memmove(el, el+1, sizeof(*el) * (msg->num_elements - (i+1)));
+	}
+	msg->num_elements--;
+	msg->elements = talloc_realloc(msg, msg->elements,
+				       struct ldb_message_element,
+				       msg->num_elements);
+	return LDB_SUCCESS;
+}
+
+/*
+  delete all elements matching an attribute name/value
+
+  return LDB Error on failure
+*/
+static int ldb_kv_msg_delete_element(struct ldb_module *module,
+				     struct ldb_kv_private *ldb_kv,
+				     struct ldb_message *msg,
+				     const char *name,
+				     const struct ldb_val *val)
+{
+	struct ldb_context *ldb = ldb_module_get_ctx(module);
+	unsigned int i;
+	int found, ret;
+	struct ldb_message_element *el;
+	const struct ldb_schema_attribute *a;
+
+	found = ldb_kv_find_element(msg, name);
+	if (found == -1) {
+		return LDB_ERR_NO_SUCH_ATTRIBUTE;
+	}
+
+	i = (unsigned int) found;
+	el = &(msg->elements[i]);
+
+	a = ldb_schema_attribute_by_name(ldb, el->name);
+
+	for (i=0;i<el->num_values;i++) {
+		bool matched;
+		if (a->syntax->operator_fn) {
+			ret = a->syntax->operator_fn(ldb, LDB_OP_EQUALITY, a,
+						     &el->values[i], val, &matched);
+			if (ret != LDB_SUCCESS) return ret;
+		} else {
+			matched = (a->syntax->comparison_fn(ldb, ldb,
+							    &el->values[i], val) == 0);
+		}
+		if (matched) {
+			if (el->num_values == 1) {
+				return ldb_kv_msg_delete_attribute(
+				    module, ldb_kv, msg, name);
+			}
+
+			ret =
+			    ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
+			if (ret != LDB_SUCCESS) {
+				return ret;
+			}
+
+			if (i<el->num_values-1) {
+				memmove(&el->values[i], &el->values[i+1],
+					sizeof(el->values[i])*
+						(el->num_values-(i+1)));
+			}
+			el->num_values--;
+
+			/* per definition we find in a canonicalised message an
+			   attribute value only once. So we are finished here */
+			return LDB_SUCCESS;
+		}
+	}
+
+	/* Not found */
+	return LDB_ERR_NO_SUCH_ATTRIBUTE;
+}
+
+/*
+  modify a record - internal interface
+
+  yuck - this is O(n^2). Luckily n is usually small so we probably
+  get away with it, but if we ever have really large attribute lists
+  then we'll need to look at this again
+
+  'req' is optional, and is used to specify controls if supplied
+*/
+int ldb_kv_modify_internal(struct ldb_module *module,
+			   const struct ldb_message *msg,
+			   struct ldb_request *req)
+{
+	struct ldb_context *ldb = ldb_module_get_ctx(module);
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_message *msg2;
+	unsigned int i, j;
+	int ret = LDB_SUCCESS, idx;
+	struct ldb_control *control_permissive = NULL;
+	TALLOC_CTX *mem_ctx = talloc_new(req);
+
+	if (mem_ctx == NULL) {
+		return ldb_module_oom(module);
+	}
+
+	if (req) {
+		control_permissive = ldb_request_get_control(req,
+					LDB_CONTROL_PERMISSIVE_MODIFY_OID);
+	}
+
+	msg2 = ldb_msg_new(mem_ctx);
+	if (msg2 == NULL) {
+		ret = LDB_ERR_OTHER;
+		goto done;
+	}
+
+	ret = ldb_kv_search_dn1(
+	    module, msg->dn, msg2, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
+	if (ret != LDB_SUCCESS) {
+		goto done;
+	}
+
+	for (i=0; i<msg->num_elements; i++) {
+		struct ldb_message_element *el = &msg->elements[i], *el2;
+		struct ldb_val *vals;
+		const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
+		const char *dn;
+		uint32_t options = 0;
+		if (control_permissive != NULL) {
+			options |= LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES;
+		}
+
+		switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
+		case LDB_FLAG_MOD_ADD:
+
+			if (el->num_values == 0) {
+				ldb_asprintf_errstring(ldb,
+						       "attribute '%s': attribute on '%s' specified, but with 0 values (illegal)",
+						       el->name, ldb_dn_get_linearized(msg2->dn));
+				ret = LDB_ERR_CONSTRAINT_VIOLATION;
+				goto done;
+			}
+
+			/* make a copy of the array so that a permissive
+			 * control can remove duplicates without changing the
+			 * original values, but do not copy data as we do not
+			 * need to keep it around once the operation is
+			 * finished */
+			if (control_permissive) {
+				el = talloc(msg2, struct ldb_message_element);
+				if (!el) {
+					ret = LDB_ERR_OTHER;
+					goto done;
+				}
+				*el = msg->elements[i];
+				el->values = talloc_array(el, struct ldb_val, el->num_values);
+				if (el->values == NULL) {
+					ret = LDB_ERR_OTHER;
+					goto done;
+				}
+				for (j = 0; j < el->num_values; j++) {
+					el->values[j] = msg->elements[i].values[j];
+				}
+			}
+
+			if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
+				ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
+						       el->name, ldb_dn_get_linearized(msg2->dn));
+				ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
+				goto done;
+			}
+
+			/* Checks if element already exists */
+			idx = ldb_kv_find_element(msg2, el->name);
+			if (idx == -1) {
+				if (ldb_kv_msg_add_element(msg2, el) != 0) {
+					ret = LDB_ERR_OTHER;
+					goto done;
+				}
+				ret = ldb_kv_index_add_element(
+				    module, ldb_kv, msg2, el);
+				if (ret != LDB_SUCCESS) {
+					goto done;
+				}
+			} else {
+				j = (unsigned int) idx;
+				el2 = &(msg2->elements[j]);
+
+				/* We cannot add another value on a existing one
+				   if the attribute is single-valued */
+				if (ldb_kv_single_valued(a, el)) {
+					ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
+						               el->name, ldb_dn_get_linearized(msg2->dn));
+					ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
+					goto done;
+				}
+
+				/* Check that values don't exist yet on multi-
+				   valued attributes or aren't provided twice */
+				if (!(el->flags &
+				      LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
+					struct ldb_val *duplicate = NULL;
+					ret = ldb_msg_find_common_values(ldb,
+									 msg2,
+									 el,
+									 el2,
+									 options);
+
+					if (ret ==
+					    LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
+						ldb_asprintf_errstring(ldb,
+							"attribute '%s': value "
+							"#%u on '%s' already "
+							"exists", el->name, j,
+							ldb_dn_get_linearized(msg2->dn));
+						goto done;
+					} else if (ret != LDB_SUCCESS) {
+						goto done;
+					}
+
+					ret = ldb_msg_find_duplicate_val(
+						ldb, msg2, el, &duplicate, 0);
+					if (ret != LDB_SUCCESS) {
+						goto done;
+					}
+					if (duplicate != NULL) {
+						ldb_asprintf_errstring(
+							ldb,
+							"attribute '%s': value "
+							"'%.*s' on '%s' "
+							"provided more than "
+							"once in ADD",
+							el->name,
+							(int)duplicate->length,
+							duplicate->data,
+							ldb_dn_get_linearized(msg->dn));
+						ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
+						goto done;
+					}
+				}
+
+				/* Now combine existing and new values to a new
+				   attribute record */
+				vals = talloc_realloc(msg2->elements,
+						      el2->values, struct ldb_val,
+						      el2->num_values + el->num_values);
+				if (vals == NULL) {
+					ldb_oom(ldb);
+					ret = LDB_ERR_OTHER;
+					goto done;
+				}
+
+				for (j=0; j<el->num_values; j++) {
+					vals[el2->num_values + j] =
+						ldb_val_dup(vals, &el->values[j]);
+				}
+
+				el2->values = vals;
+				el2->num_values += el->num_values;
+
+				ret = ldb_kv_index_add_element(
+				    module, ldb_kv, msg2, el);
+				if (ret != LDB_SUCCESS) {
+					goto done;
+				}
+			}
+
+			break;
+
+		case LDB_FLAG_MOD_REPLACE:
+
+			if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
+				ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
+						       el->name, ldb_dn_get_linearized(msg2->dn));
+				ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
+				goto done;
+			}
+
+			/*
+			 * We don't need to check this if we have been
+			 * pre-screened by the repl_meta_data module
+			 * in Samba, or someone else who can claim to
+			 * know what they are doing.
+			 */
+			if (!(el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
+				struct ldb_val *duplicate = NULL;
+
+				ret = ldb_msg_find_duplicate_val(ldb, msg2, el,
+								 &duplicate, 0);
+				if (ret != LDB_SUCCESS) {
+					goto done;
+				}
+				if (duplicate != NULL) {
+					ldb_asprintf_errstring(
+						ldb,
+						"attribute '%s': value '%.*s' "
+						"on '%s' provided more than "
+						"once in REPLACE",
+						el->name,
+						(int)duplicate->length,
+						duplicate->data,
+						ldb_dn_get_linearized(msg2->dn));
+					ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
+					goto done;
+				}
+			}
+
+			/* Checks if element already exists */
+			idx = ldb_kv_find_element(msg2, el->name);
+			if (idx != -1) {
+				j = (unsigned int) idx;
+				el2 = &(msg2->elements[j]);
+
+				/* we consider two elements to be
+				 * equal only if the order
+				 * matches. This allows dbcheck to
+				 * fix the ordering on attributes
+				 * where order matters, such as
+				 * objectClass
+				 */
+				if (ldb_msg_element_equal_ordered(el, el2)) {
+					continue;
+				}
+
+				/* Delete the attribute if it exists in the DB */
+				if (ldb_kv_msg_delete_attribute(
+					module, ldb_kv, msg2, el->name) != 0) {
+					ret = LDB_ERR_OTHER;
+					goto done;
+				}
+			}
+
+			/* Recreate it with the new values */
+			if (ldb_kv_msg_add_element(msg2, el) != 0) {
+				ret = LDB_ERR_OTHER;
+				goto done;
+			}
+
+			ret =
+			    ldb_kv_index_add_element(module, ldb_kv, msg2, el);
+			if (ret != LDB_SUCCESS) {
+				goto done;
+			}
+
+			break;
+
+		case LDB_FLAG_MOD_DELETE:
+			dn = ldb_dn_get_linearized(msg2->dn);
+			if (dn == NULL) {
+				ret = LDB_ERR_OTHER;
+				goto done;
+			}
+
+			if (msg->elements[i].num_values == 0) {
+				/* Delete the whole attribute */
+				ret = ldb_kv_msg_delete_attribute(
+				    module,
+				    ldb_kv,
+				    msg2,
+				    msg->elements[i].name);
+				if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
+				    control_permissive) {
+					ret = LDB_SUCCESS;
+				} else {
+					ldb_asprintf_errstring(ldb,
+							       "attribute '%s': no such attribute for delete on '%s'",
+							       msg->elements[i].name, dn);
+				}
+				if (ret != LDB_SUCCESS) {
+					goto done;
+				}
+			} else {
+				/* Delete specified values from an attribute */
+				for (j=0; j < msg->elements[i].num_values; j++) {
+					ret = ldb_kv_msg_delete_element(
+					    module,
+					    ldb_kv,
+					    msg2,
+					    msg->elements[i].name,
+					    &msg->elements[i].values[j]);
+					if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
+					    control_permissive) {
+						ret = LDB_SUCCESS;
+					} else if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
+						ldb_asprintf_errstring(ldb,
+								       "attribute '%s': no matching attribute value while deleting attribute on '%s'",
+								       msg->elements[i].name, dn);
+					}
+					if (ret != LDB_SUCCESS) {
+						goto done;
+					}
+				}
+			}
+			break;
+		default:
+			ldb_asprintf_errstring(ldb,
+					       "attribute '%s': invalid modify flags on '%s': 0x%x",
+					       msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
+					       msg->elements[i].flags & LDB_FLAG_MOD_MASK);
+			ret = LDB_ERR_PROTOCOL_ERROR;
+			goto done;
+		}
+	}
+
+	ret = ldb_kv_store(module, msg2, TDB_MODIFY);
+	if (ret != LDB_SUCCESS) {
+		goto done;
+	}
+
+	ret = ldb_kv_modified(module, msg2->dn);
+	if (ret != LDB_SUCCESS) {
+		goto done;
+	}
+
+done:
+	TALLOC_FREE(mem_ctx);
+	return ret;
+}
+
+/*
+  modify a record
+*/
+static int ldb_kv_modify(struct ldb_kv_context *ctx)
+{
+	struct ldb_module *module = ctx->module;
+	struct ldb_request *req = ctx->req;
+	int ret = LDB_SUCCESS;
+
+	ret = ldb_kv_check_special_dn(module, req->op.mod.message);
+	if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	ldb_request_set_state(req, LDB_ASYNC_PENDING);
+
+	if (ldb_kv_cache_load(module) != 0) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ret = ldb_kv_modify_internal(module, req->op.mod.message, req);
+
+	return ret;
+}
+
+/*
+  rename a record
+*/
+static int ldb_kv_rename(struct ldb_kv_context *ctx)
+{
+	struct ldb_module *module = ctx->module;
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_request *req = ctx->req;
+	struct ldb_message *msg;
+	int ret = LDB_SUCCESS;
+	TDB_DATA tdb_key, tdb_key_old;
+	struct ldb_dn *db_dn;
+
+	ldb_request_set_state(req, LDB_ASYNC_PENDING);
+
+	if (ldb_kv_cache_load(ctx->module) != 0) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	msg = ldb_msg_new(ctx);
+	if (msg == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	/* we need to fetch the old record to re-add under the new name */
+	ret = ldb_kv_search_dn1(module,
+				req->op.rename.olddn,
+				msg,
+				LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
+	if (ret != LDB_SUCCESS) {
+		/* not finding the old record is an error */
+		return ret;
+	}
+
+	/* We need to, before changing the DB, check if the new DN
+	 * exists, so we can return this error to the caller with an
+	 * unmodified DB
+	 *
+	 * Even in GUID index mode we use ltdb_key_dn() as we are
+	 * trying to figure out if this is just a case rename
+	 */
+	tdb_key = ldb_kv_key_dn(module, msg, req->op.rename.newdn);
+	if (!tdb_key.dptr) {
+		talloc_free(msg);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	tdb_key_old = ldb_kv_key_dn(module, msg, req->op.rename.olddn);
+	if (!tdb_key_old.dptr) {
+		talloc_free(msg);
+		talloc_free(tdb_key.dptr);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	/*
+	 * Only declare a conflict if the new DN already exists,
+	 * and it isn't a case change on the old DN
+	 */
+	if (tdb_key_old.dsize != tdb_key.dsize
+	    || memcmp(tdb_key.dptr, tdb_key_old.dptr, tdb_key.dsize) != 0) {
+		ret = ldb_kv_search_base(
+		    module, msg, req->op.rename.newdn, &db_dn);
+		if (ret == LDB_SUCCESS) {
+			ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
+		} else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+			ret = LDB_SUCCESS;
+		}
+	}
+
+	/* finding the new record already in the DB is an error */
+
+	if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       "Entry %s already exists",
+				       ldb_dn_get_linearized(req->op.rename.newdn));
+	}
+	if (ret != LDB_SUCCESS) {
+		talloc_free(tdb_key_old.dptr);
+		talloc_free(tdb_key.dptr);
+		talloc_free(msg);
+		return ret;
+	}
+
+	talloc_free(tdb_key_old.dptr);
+	talloc_free(tdb_key.dptr);
+
+	/* Always delete first then add, to avoid conflicts with
+	 * unique indexes. We rely on the transaction to make this
+	 * atomic
+	 */
+	ret = ldb_kv_delete_internal(module, msg->dn);
+	if (ret != LDB_SUCCESS) {
+		talloc_free(msg);
+		return ret;
+	}
+
+	msg->dn = ldb_dn_copy(msg, req->op.rename.newdn);
+	if (msg->dn == NULL) {
+		talloc_free(msg);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	/* We don't check single value as we can have more than 1 with
+	 * deleted attributes. We could go through all elements but that's
+	 * maybe not the most efficient way
+	 */
+	ret = ldb_kv_add_internal(module, ldb_kv, msg, false);
+
+	talloc_free(msg);
+
+	return ret;
+}
+
+static int ldb_kv_start_trans(struct ldb_module *module)
+{
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+
+	pid_t pid = getpid();
+
+	if (ldb_kv->pid != pid) {
+		ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
+				       __location__
+				       ": Reusing ldb opend by pid %d in "
+				       "process %d\n",
+				       ldb_kv->pid,
+				       pid);
+		return LDB_ERR_PROTOCOL_ERROR;
+	}
+
+	/* Do not take out the transaction lock on a read-only DB */
+	if (ldb_kv->read_only) {
+		return LDB_ERR_UNWILLING_TO_PERFORM;
+	}
+
+	if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
+		return ldb_kv->kv_ops->error(ldb_kv);
+	}
+
+	ldb_kv_index_transaction_start(module);
+
+	ldb_kv->reindex_failed = false;
+
+	return LDB_SUCCESS;
+}
+
+/*
+ * Forward declaration to allow prepare_commit to in fact abort the
+ * transaction
+ */
+static int ldb_kv_del_trans(struct ldb_module *module);
+
+static int ldb_kv_prepare_commit(struct ldb_module *module)
+{
+	int ret;
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	pid_t pid = getpid();
+
+	if (ldb_kv->pid != pid) {
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       __location__
+				       ": Reusing ldb opend by pid %d in "
+				       "process %d\n",
+				       ldb_kv->pid,
+				       pid);
+		return LDB_ERR_PROTOCOL_ERROR;
+	}
+
+	if (!ldb_kv->kv_ops->transaction_active(ldb_kv)) {
+		ldb_set_errstring(ldb_module_get_ctx(module),
+				  "ltdb_prepare_commit() called "
+				  "without transaction active");
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	/*
+	 * Check if the last re-index failed.
+	 *
+	 * This can happen if for example a duplicate value was marked
+	 * unique.  We must not write a partial re-index into the DB.
+	 */
+	if (ldb_kv->reindex_failed) {
+		/*
+		 * We must instead abort the transaction so we get the
+		 * old values and old index back
+		 */
+		ldb_kv_del_trans(module);
+		ldb_set_errstring(ldb_module_get_ctx(module),
+				  "Failure during re-index, so "
+				  "transaction must be aborted.");
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ret = ldb_kv_index_transaction_commit(module);
+	if (ret != LDB_SUCCESS) {
+		ldb_kv->kv_ops->abort_write(ldb_kv);
+		return ret;
+	}
+
+	if (ldb_kv->kv_ops->prepare_write(ldb_kv) != 0) {
+		ret = ldb_kv->kv_ops->error(ldb_kv);
+		ldb_debug_set(ldb_module_get_ctx(module),
+			      LDB_DEBUG_FATAL,
+			      "Failure during "
+			      "prepare_write): %s -> %s",
+			      ldb_kv->kv_ops->errorstr(ldb_kv),
+			      ldb_strerror(ret));
+		return ret;
+	}
+
+	ldb_kv->prepared_commit = true;
+
+	return LDB_SUCCESS;
+}
+
+static int ldb_kv_end_trans(struct ldb_module *module)
+{
+	int ret;
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+
+	if (!ldb_kv->prepared_commit) {
+		ret = ldb_kv_prepare_commit(module);
+		if (ret != LDB_SUCCESS) {
+			return ret;
+		}
+	}
+
+	ldb_kv->prepared_commit = false;
+
+	if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
+		ret = ldb_kv->kv_ops->error(ldb_kv);
+		ldb_asprintf_errstring(
+		    ldb_module_get_ctx(module),
+		    "Failure during tdb_transaction_commit(): %s -> %s",
+		    ldb_kv->kv_ops->errorstr(ldb_kv),
+		    ldb_strerror(ret));
+		return ret;
+	}
+
+	return LDB_SUCCESS;
+}
+
+static int ldb_kv_del_trans(struct ldb_module *module)
+{
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+
+	if (ldb_kv_index_transaction_cancel(module) != 0) {
+		ldb_kv->kv_ops->abort_write(ldb_kv);
+		return ldb_kv->kv_ops->error(ldb_kv);
+	}
+
+	ldb_kv->kv_ops->abort_write(ldb_kv);
+	return LDB_SUCCESS;
+}
+
+/*
+  return sequenceNumber from @BASEINFO
+*/
+static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
+				  struct ldb_extended **ext)
+{
+	struct ldb_context *ldb;
+	struct ldb_module *module = ctx->module;
+	struct ldb_request *req = ctx->req;
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	TALLOC_CTX *tmp_ctx = NULL;
+	struct ldb_seqnum_request *seq;
+	struct ldb_seqnum_result *res;
+	struct ldb_message *msg = NULL;
+	struct ldb_dn *dn;
+	const char *date;
+	int ret = LDB_SUCCESS;
+
+	ldb = ldb_module_get_ctx(module);
+
+	seq = talloc_get_type(req->op.extended.data,
+				struct ldb_seqnum_request);
+	if (seq == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ldb_request_set_state(req, LDB_ASYNC_PENDING);
+
+	if (ldb_kv->kv_ops->lock_read(module) != 0) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	res = talloc_zero(req, struct ldb_seqnum_result);
+	if (res == NULL) {
+		ret = LDB_ERR_OPERATIONS_ERROR;
+		goto done;
+	}
+
+	tmp_ctx = talloc_new(req);
+	if (tmp_ctx == NULL) {
+		ret = LDB_ERR_OPERATIONS_ERROR;
+		goto done;
+	}
+
+	dn = ldb_dn_new(tmp_ctx, ldb, LTDB_BASEINFO);
+	if (dn == NULL) {
+		ret = LDB_ERR_OPERATIONS_ERROR;
+		goto done;
+	}
+
+	msg = ldb_msg_new(tmp_ctx);
+	if (msg == NULL) {
+		ret = LDB_ERR_OPERATIONS_ERROR;
+		goto done;
+	}
+
+	ret = ldb_kv_search_dn1(module, dn, msg, 0);
+	if (ret != LDB_SUCCESS) {
+		goto done;
+	}
+
+	switch (seq->type) {
+	case LDB_SEQ_HIGHEST_SEQ:
+		res->seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
+		break;
+	case LDB_SEQ_NEXT:
+		res->seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
+		res->seq_num++;
+		break;
+	case LDB_SEQ_HIGHEST_TIMESTAMP:
+		date = ldb_msg_find_attr_as_string(msg, LTDB_MOD_TIMESTAMP, NULL);
+		if (date) {
+			res->seq_num = ldb_string_to_time(date);
+		} else {
+			res->seq_num = 0;
+			/* zero is as good as anything when we don't know */
+		}
+		break;
+	}
+
+	*ext = talloc_zero(req, struct ldb_extended);
+	if (*ext == NULL) {
+		ret = LDB_ERR_OPERATIONS_ERROR;
+		goto done;
+	}
+	(*ext)->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
+	(*ext)->data = talloc_steal(*ext, res);
+
+done:
+	talloc_free(tmp_ctx);
+
+	ldb_kv->kv_ops->unlock_read(module);
+	return ret;
+}
+
+static void ldb_kv_request_done(struct ldb_kv_context *ctx, int error)
+{
+	struct ldb_context *ldb;
+	struct ldb_request *req;
+	struct ldb_reply *ares;
+
+	ldb = ldb_module_get_ctx(ctx->module);
+	req = ctx->req;
+
+	/* if we already returned an error just return */
+	if (ldb_request_get_status(req) != LDB_SUCCESS) {
+		return;
+	}
+
+	ares = talloc_zero(req, struct ldb_reply);
+	if (!ares) {
+		ldb_oom(ldb);
+		req->callback(req, NULL);
+		return;
+	}
+	ares->type = LDB_REPLY_DONE;
+	ares->error = error;
+
+	req->callback(req, ares);
+}
+
+static void ldb_kv_timeout(struct tevent_context *ev,
+			   struct tevent_timer *te,
+			   struct timeval t,
+			   void *private_data)
+{
+	struct ldb_kv_context *ctx;
+	ctx = talloc_get_type(private_data, struct ldb_kv_context);
+
+	if (!ctx->request_terminated) {
+		/* request is done now */
+		ldb_kv_request_done(ctx, LDB_ERR_TIME_LIMIT_EXCEEDED);
+	}
+
+	if (ctx->spy) {
+		/* neutralize the spy */
+		ctx->spy->ctx = NULL;
+		ctx->spy = NULL;
+	}
+	talloc_free(ctx);
+}
+
+static void ldb_kv_request_extended_done(struct ldb_kv_context *ctx,
+					 struct ldb_extended *ext,
+					 int error)
+{
+	struct ldb_context *ldb;
+	struct ldb_request *req;
+	struct ldb_reply *ares;
+
+	ldb = ldb_module_get_ctx(ctx->module);
+	req = ctx->req;
+
+	/* if we already returned an error just return */
+	if (ldb_request_get_status(req) != LDB_SUCCESS) {
+		return;
+	}
+
+	ares = talloc_zero(req, struct ldb_reply);
+	if (!ares) {
+		ldb_oom(ldb);
+		req->callback(req, NULL);
+		return;
+	}
+	ares->type = LDB_REPLY_DONE;
+	ares->response = ext;
+	ares->error = error;
+
+	req->callback(req, ares);
+}
+
+static void ldb_kv_handle_extended(struct ldb_kv_context *ctx)
+{
+	struct ldb_extended *ext = NULL;
+	int ret;
+
+	if (strcmp(ctx->req->op.extended.oid,
+		   LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
+		/* get sequence number */
+		ret = ldb_kv_sequence_number(ctx, &ext);
+	} else {
+		/* not recognized */
+		ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
+	}
+
+	ldb_kv_request_extended_done(ctx, ext, ret);
+}
+
+static void ldb_kv_callback(struct tevent_context *ev,
+			    struct tevent_timer *te,
+			    struct timeval t,
+			    void *private_data)
+{
+	struct ldb_kv_context *ctx;
+	int ret;
+
+	ctx = talloc_get_type(private_data, struct ldb_kv_context);
+
+	if (ctx->request_terminated) {
+		goto done;
+	}
+
+	switch (ctx->req->operation) {
+	case LDB_SEARCH:
+		ret = ldb_kv_search(ctx);
+		break;
+	case LDB_ADD:
+		ret = ldb_kv_add(ctx);
+		break;
+	case LDB_MODIFY:
+		ret = ldb_kv_modify(ctx);
+		break;
+	case LDB_DELETE:
+		ret = ldb_kv_delete(ctx);
+		break;
+	case LDB_RENAME:
+		ret = ldb_kv_rename(ctx);
+		break;
+	case LDB_EXTENDED:
+		ldb_kv_handle_extended(ctx);
+		goto done;
+	default:
+		/* no other op supported */
+		ret = LDB_ERR_PROTOCOL_ERROR;
+	}
+
+	if (!ctx->request_terminated) {
+		/* request is done now */
+		ldb_kv_request_done(ctx, ret);
+	}
+
+done:
+	if (ctx->spy) {
+		/* neutralize the spy */
+		ctx->spy->ctx = NULL;
+		ctx->spy = NULL;
+	}
+	talloc_free(ctx);
+}
+
+static int ldb_kv_request_destructor(void *ptr)
+{
+	struct ldb_kv_req_spy *spy =
+	    talloc_get_type(ptr, struct ldb_kv_req_spy);
+
+	if (spy->ctx != NULL) {
+		spy->ctx->spy = NULL;
+		spy->ctx->request_terminated = true;
+		spy->ctx = NULL;
+	}
+
+	return 0;
+}
+
+static int ldb_kv_handle_request(struct ldb_module *module,
+				 struct ldb_request *req)
+{
+	struct ldb_control *control_permissive;
+	struct ldb_context *ldb;
+	struct tevent_context *ev;
+	struct ldb_kv_context *ac;
+	struct tevent_timer *te;
+	struct timeval tv;
+	unsigned int i;
+
+	ldb = ldb_module_get_ctx(module);
+
+	control_permissive = ldb_request_get_control(req,
+					LDB_CONTROL_PERMISSIVE_MODIFY_OID);
+
+	for (i = 0; req->controls && req->controls[i]; i++) {
+		if (req->controls[i]->critical &&
+		    req->controls[i] != control_permissive) {
+			ldb_asprintf_errstring(ldb, "Unsupported critical extension %s",
+					       req->controls[i]->oid);
+			return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
+		}
+	}
+
+	if (req->starttime == 0 || req->timeout == 0) {
+		ldb_set_errstring(ldb, "Invalid timeout settings");
+		return LDB_ERR_TIME_LIMIT_EXCEEDED;
+	}
+
+	ev = ldb_handle_get_event_context(req->handle);
+
+	ac = talloc_zero(ldb, struct ldb_kv_context);
+	if (ac == NULL) {
+		ldb_oom(ldb);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ac->module = module;
+	ac->req = req;
+
+	tv.tv_sec = 0;
+	tv.tv_usec = 0;
+	te = tevent_add_timer(ev, ac, tv, ldb_kv_callback, ac);
+	if (NULL == te) {
+		talloc_free(ac);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	if (req->timeout > 0) {
+		tv.tv_sec = req->starttime + req->timeout;
+		tv.tv_usec = 0;
+		ac->timeout_event =
+		    tevent_add_timer(ev, ac, tv, ldb_kv_timeout, ac);
+		if (NULL == ac->timeout_event) {
+			talloc_free(ac);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+	}
+
+	/* set a spy so that we do not try to use the request context
+	 * if it is freed before ltdb_callback fires */
+	ac->spy = talloc(req, struct ldb_kv_req_spy);
+	if (NULL == ac->spy) {
+		talloc_free(ac);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	ac->spy->ctx = ac;
+
+	talloc_set_destructor((TALLOC_CTX *)ac->spy, ldb_kv_request_destructor);
+
+	return LDB_SUCCESS;
+}
+
+static int ldb_kv_init_rootdse(struct ldb_module *module)
+{
+	/* ignore errors on this - we expect it for non-sam databases */
+	ldb_mod_register_control(module, LDB_CONTROL_PERMISSIVE_MODIFY_OID);
+
+	/* there can be no module beyond the backend, just return */
+	return LDB_SUCCESS;
+}
+
+static int ldb_kv_lock_read(struct ldb_module *module)
+{
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	return ldb_kv->kv_ops->lock_read(module);
+}
+
+static int ldb_kv_unlock_read(struct ldb_module *module)
+{
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	return ldb_kv->kv_ops->unlock_read(module);
+}
+
+static const struct ldb_module_ops ldb_kv_ops = {
+    .name = "tdb",
+    .init_context = ldb_kv_init_rootdse,
+    .search = ldb_kv_handle_request,
+    .add = ldb_kv_handle_request,
+    .modify = ldb_kv_handle_request,
+    .del = ldb_kv_handle_request,
+    .rename = ldb_kv_handle_request,
+    .extended = ldb_kv_handle_request,
+    .start_transaction = ldb_kv_start_trans,
+    .end_transaction = ldb_kv_end_trans,
+    .prepare_commit = ldb_kv_prepare_commit,
+    .del_transaction = ldb_kv_del_trans,
+    .read_lock = ldb_kv_lock_read,
+    .read_unlock = ldb_kv_unlock_read,
+};
+
+int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
+		      const char *name,
+		      struct ldb_context *ldb,
+		      const char *options[],
+		      struct ldb_module **_module)
+{
+	if (getenv("LDB_WARN_UNINDEXED")) {
+		ldb_kv->warn_unindexed = true;
+	}
+
+	if (getenv("LDB_WARN_REINDEX")) {
+		ldb_kv->warn_reindex = true;
+	}
+
+	ldb_kv->sequence_number = 0;
+
+	ldb_kv->pid = getpid();
+
+	ldb_kv->module = ldb_module_new(ldb, ldb, name, &ldb_kv_ops);
+	if (!ldb_kv->module) {
+		ldb_oom(ldb);
+		talloc_free(ldb_kv);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	ldb_module_set_private(ldb_kv->module, ldb_kv);
+	talloc_steal(ldb_kv->module, ldb_kv);
+
+	if (ldb_kv_cache_load(ldb_kv->module) != 0) {
+		ldb_asprintf_errstring(ldb, "Unable to load ltdb cache "
+				       "records for backend '%s'", name);
+		talloc_free(ldb_kv->module);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	*_module = ldb_kv->module;
+	/*
+	 * Set or override the maximum key length
+	 *
+	 * The ldb_mdb code will have set this to 511, but our tests
+	 * set this even smaller (to make the tests more practical).
+	 *
+	 * This must only be used for the selftest as the length
+	 * becomes encoded in the index keys.
+	 */
+	{
+		const char *len_str =
+			ldb_options_find(ldb, options,
+					 "max_key_len_for_self_test");
+		if (len_str != NULL) {
+			unsigned len = strtoul(len_str, NULL, 0);
+			ldb_kv->max_key_length = len;
+		}
+	}
+
+	/*
+	 * Override full DB scans
+	 *
+	 * A full DB scan is expensive on a large database.  This
+	 * option is for testing to show that the full DB scan is not
+	 * triggered.
+	 */
+	{
+		const char *len_str =
+			ldb_options_find(ldb, options,
+					 "disable_full_db_scan_for_self_test");
+		if (len_str != NULL) {
+			ldb_kv->disable_full_db_scan = true;
+		}
+	}
+
+	return LDB_SUCCESS;
+}
diff --git a/lib/ldb/ldb_key_value/ldb_kv.h b/lib/ldb/ldb_key_value/ldb_kv.h
new file mode 100644
index 0000000..13fe125
--- /dev/null
+++ b/lib/ldb/ldb_key_value/ldb_kv.h
@@ -0,0 +1,265 @@
+#include "replace.h"
+#include "system/filesys.h"
+#include "system/time.h"
+#include "tdb.h"
+#include "ldb_module.h"
+
+struct ldb_kv_private;
+typedef int (*ldb_kv_traverse_fn)(struct ldb_kv_private *ldb_kv,
+				  struct ldb_val key,
+				  struct ldb_val data,
+				  void *ctx);
+
+struct kv_db_ops {
+	int (*store)(struct ldb_kv_private *ldb_kv,
+		     struct ldb_val key,
+		     struct ldb_val data,
+		     int flags);
+	int (*delete)(struct ldb_kv_private *ldb_kv, struct ldb_val key);
+	int (*iterate)(struct ldb_kv_private *ldb_kv,
+		       ldb_kv_traverse_fn fn,
+		       void *ctx);
+	int (*update_in_iterate)(struct ldb_kv_private *ldb_kv,
+				 struct ldb_val key,
+				 struct ldb_val key2,
+				 struct ldb_val data,
+				 void *ctx);
+	int (*fetch_and_parse)(struct ldb_kv_private *ldb_kv,
+			       struct ldb_val key,
+			       int (*parser)(struct ldb_val key,
+					     struct ldb_val data,
+					     void *private_data),
+			       void *ctx);
+	int (*lock_read)(struct ldb_module *);
+	int (*unlock_read)(struct ldb_module *);
+	int (*begin_write)(struct ldb_kv_private *);
+	int (*prepare_write)(struct ldb_kv_private *);
+	int (*abort_write)(struct ldb_kv_private *);
+	int (*finish_write)(struct ldb_kv_private *);
+	int (*error)(struct ldb_kv_private *ldb_kv);
+	const char *(*errorstr)(struct ldb_kv_private *ldb_kv);
+	const char *(*name)(struct ldb_kv_private *ldb_kv);
+	bool (*has_changed)(struct ldb_kv_private *ldb_kv);
+	bool (*transaction_active)(struct ldb_kv_private *ldb_kv);
+};
+
+/* this private structure is used by the key value backends in the
+   ldb_context */
+struct ldb_kv_private {
+	const struct kv_db_ops *kv_ops;
+	struct ldb_module *module;
+	TDB_CONTEXT *tdb;
+	struct lmdb_private *lmdb_private;
+	unsigned int connect_flags;
+
+	unsigned long long sequence_number;
+
+	/* the low level tdb seqnum - used to avoid loading BASEINFO when
+	   possible */
+	int tdb_seqnum;
+
+	struct ldb_kv_cache {
+		struct ldb_message *indexlist;
+		bool one_level_indexes;
+		bool attribute_indexes;
+		const char *GUID_index_attribute;
+		const char *GUID_index_dn_component;
+	} *cache;
+
+
+	bool check_base;
+	bool disallow_dn_filter;
+	struct ldb_kv_idxptr *idxptr;
+	bool prepared_commit;
+	int read_lock_count;
+
+	bool warn_unindexed;
+	bool warn_reindex;
+
+	bool read_only;
+
+	bool reindex_failed;
+
+	const struct ldb_schema_syntax *GUID_index_syntax;
+
+	/*
+	 * Maximum index key length.  If non zero keys longer than this length
+	 * will be truncated for non unique indexes. Keys for unique indexes
+	 * greater than this length will be rejected.
+	 */
+	unsigned max_key_length;
+
+	/*
+	 * To allow testing that ensures the DB does not fall back
+	 * to a full scan
+	 */
+	bool disable_full_db_scan;
+
+	/*
+	 * The PID that opened this database so we don't work in a
+	 * fork()ed child.
+	 */
+	pid_t pid;
+};
+
+struct ldb_kv_context {
+	struct ldb_module *module;
+	struct ldb_request *req;
+
+	bool request_terminated;
+	struct ldb_kv_req_spy *spy;
+
+	/* search stuff */
+	const struct ldb_parse_tree *tree;
+	struct ldb_dn *base;
+	enum ldb_scope scope;
+	const char * const *attrs;
+	struct tevent_timer *timeout_event;
+
+	/* error handling */
+	int error;
+};
+
+struct ldb_kv_reindex_context {
+	struct ldb_module *module;
+	int error;
+	uint32_t count;
+};
+
+
+/* special record types */
+#define LTDB_INDEX      "@INDEX"
+#define LTDB_INDEXLIST  "@INDEXLIST"
+#define LTDB_IDX        "@IDX"
+#define LTDB_IDXVERSION "@IDXVERSION"
+#define LTDB_IDXATTR    "@IDXATTR"
+#define LTDB_IDXONE     "@IDXONE"
+#define LTDB_IDXDN     "@IDXDN"
+#define LTDB_IDXGUID    "@IDXGUID"
+#define LTDB_IDX_DN_GUID "@IDX_DN_GUID"
+
+/*
+ * This will be used to indicate when a new, yet to be developed
+ * sub-database version of the indicies are in use, to ensure we do
+ * not load future databases unintentionally.
+ */
+
+#define LTDB_IDX_LMDB_SUBDB "@IDX_LMDB_SUBDB"
+
+#define LTDB_BASEINFO   "@BASEINFO"
+#define LTDB_OPTIONS    "@OPTIONS"
+#define LTDB_ATTRIBUTES "@ATTRIBUTES"
+
+/* special attribute types */
+#define LTDB_SEQUENCE_NUMBER "sequenceNumber"
+#define LTDB_CHECK_BASE "checkBaseOnSearch"
+#define LTDB_DISALLOW_DN_FILTER "disallowDNFilter"
+#define LTDB_MOD_TIMESTAMP "whenChanged"
+#define LTDB_OBJECTCLASS "objectClass"
+
+/* DB keys */
+#define LTDB_GUID_KEY_PREFIX "GUID="
+#define LTDB_GUID_SIZE 16
+#define LTDB_GUID_KEY_SIZE (LTDB_GUID_SIZE + sizeof(LTDB_GUID_KEY_PREFIX) - 1)
+
+/*
+ * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_cache.c
+ */
+
+int ldb_kv_cache_reload(struct ldb_module *module);
+int ldb_kv_cache_load(struct ldb_module *module);
+int ldb_kv_increase_sequence_number(struct ldb_module *module);
+int ldb_kv_check_at_attributes_values(const struct ldb_val *value);
+
+/*
+ * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_index.c
+ */
+
+struct ldb_parse_tree;
+
+int ldb_kv_search_indexed(struct ldb_kv_context *ctx, uint32_t *);
+int ldb_kv_index_add_new(struct ldb_module *module,
+			 struct ldb_kv_private *ldb_kv,
+			 const struct ldb_message *msg);
+int ldb_kv_index_delete(struct ldb_module *module,
+			const struct ldb_message *msg);
+int ldb_kv_index_del_element(struct ldb_module *module,
+			     struct ldb_kv_private *ldb_kv,
+			     const struct ldb_message *msg,
+			     struct ldb_message_element *el);
+int ldb_kv_index_add_element(struct ldb_module *module,
+			     struct ldb_kv_private *ldb_kv,
+			     const struct ldb_message *msg,
+			     struct ldb_message_element *el);
+int ldb_kv_index_del_value(struct ldb_module *module,
+			   struct ldb_kv_private *ldb_kv,
+			   const struct ldb_message *msg,
+			   struct ldb_message_element *el,
+			   unsigned int v_idx);
+int ldb_kv_reindex(struct ldb_module *module);
+int ldb_kv_index_transaction_start(struct ldb_module *module);
+int ldb_kv_index_transaction_commit(struct ldb_module *module);
+int ldb_kv_index_transaction_cancel(struct ldb_module *module);
+int ldb_kv_key_dn_from_idx(struct ldb_module *module,
+			   struct ldb_kv_private *ldb_kv,
+			   TALLOC_CTX *mem_ctx,
+			   struct ldb_dn *dn,
+			   TDB_DATA *tdb_key);
+
+/*
+ * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_search.c
+ */
+int ldb_kv_search_dn1(struct ldb_module *module,
+		      struct ldb_dn *dn,
+		      struct ldb_message *msg,
+		      unsigned int unpack_flags);
+int ldb_kv_search_base(struct ldb_module *module,
+		       TALLOC_CTX *mem_ctx,
+		       struct ldb_dn *dn,
+		       struct ldb_dn **ret_dn);
+int ldb_kv_search_key(struct ldb_module *module,
+		      struct ldb_kv_private *ldb_kv,
+		      struct TDB_DATA tdb_key,
+		      struct ldb_message *msg,
+		      unsigned int unpack_flags);
+int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
+			const struct ldb_message *msg,
+			const char *const *attrs,
+			struct ldb_message **filtered_msg);
+int ldb_kv_search(struct ldb_kv_context *ctx);
+
+/*
+ * The following definitions come from lib/ldb/ldb_key_value/ldb_kv.c  */
+/*
+ * Determine if this key could hold a record.  We allow the new GUID
+ * index, the old DN index and a possible future ID=
+ */
+bool ldb_kv_key_is_record(TDB_DATA key);
+TDB_DATA ldb_kv_key_dn(struct ldb_module *module,
+		       TALLOC_CTX *mem_ctx,
+		       struct ldb_dn *dn);
+TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
+			TALLOC_CTX *mem_ctx,
+			const struct ldb_message *msg);
+int ldb_kv_guid_to_key(struct ldb_module *module,
+		       struct ldb_kv_private *ldb_kv,
+		       const struct ldb_val *GUID_val,
+		       TDB_DATA *key);
+int ldb_kv_idx_to_key(struct ldb_module *module,
+		      struct ldb_kv_private *ldb_kv,
+		      TALLOC_CTX *mem_ctx,
+		      const struct ldb_val *idx_val,
+		      TDB_DATA *key);
+int ldb_kv_store(struct ldb_module *module,
+		 const struct ldb_message *msg,
+		 int flgs);
+int ldb_kv_modify_internal(struct ldb_module *module,
+			   const struct ldb_message *msg,
+			   struct ldb_request *req);
+int ldb_kv_delete_noindex(struct ldb_module *module,
+			  const struct ldb_message *msg);
+int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
+		      const char *name,
+		      struct ldb_context *ldb,
+		      const char *options[],
+		      struct ldb_module **_module);
diff --git a/lib/ldb/ldb_key_value/ldb_kv_cache.c b/lib/ldb/ldb_key_value/ldb_kv_cache.c
new file mode 100644
index 0000000..f0bc31b
--- /dev/null
+++ b/lib/ldb/ldb_key_value/ldb_kv_cache.c
@@ -0,0 +1,662 @@
+/*
+   ldb database library
+
+   Copyright (C) Andrew Tridgell  2004
+
+     ** NOTE! The following LGPL license applies to the ldb
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ *  Name: ldb
+ *
+ *  Component: ldb key value cache functions
+ *
+ *  Description: cache special records in a ldb/tdb
+ *
+ *  Author: Andrew Tridgell
+ */
+
+#include "ldb_kv.h"
+#include "ldb_private.h"
+
+#define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
+#define LTDB_FLAG_INTEGER          (1<<1)
+#define LTDB_FLAG_UNIQUE_INDEX     (1<<2)
+
+/* valid attribute flags */
+static const struct {
+	const char *name;
+	int value;
+} ldb_kv_valid_attr_flags[] = {
+	{ "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
+	{ "INTEGER", LTDB_FLAG_INTEGER },
+	{ "HIDDEN", 0 },
+	{ "UNIQUE_INDEX",  LTDB_FLAG_UNIQUE_INDEX},
+	{ "NONE", 0 },
+	{ NULL, 0 }
+};
+
+/*
+  de-register any special handlers for @ATTRIBUTES
+*/
+static void ldb_kv_attributes_unload(struct ldb_module *module)
+{
+	struct ldb_context *ldb = ldb_module_get_ctx(module);
+
+	ldb_schema_attribute_remove_flagged(ldb, LDB_ATTR_FLAG_FROM_DB);
+
+}
+
+/*
+  add up the attrib flags for a @ATTRIBUTES element
+*/
+static int ldb_kv_attributes_flags(struct ldb_message_element *el, unsigned *v)
+{
+	unsigned int i;
+	unsigned value = 0;
+	for (i=0;i<el->num_values;i++) {
+		unsigned int j;
+		for (j = 0; ldb_kv_valid_attr_flags[j].name; j++) {
+			if (strcmp(ldb_kv_valid_attr_flags[j].name,
+				   (char *)el->values[i].data) == 0) {
+				value |= ldb_kv_valid_attr_flags[j].value;
+				break;
+			}
+		}
+		if (ldb_kv_valid_attr_flags[j].name == NULL) {
+			return -1;
+		}
+	}
+	*v = value;
+	return 0;
+}
+
+static int ldb_schema_attribute_compare(const void *p1, const void *p2)
+{
+	const struct ldb_schema_attribute *sa1 = (const struct ldb_schema_attribute *)p1;
+	const struct ldb_schema_attribute *sa2 = (const struct ldb_schema_attribute *)p2;
+	return ldb_attr_cmp(sa1->name, sa2->name);
+}
+
+/*
+  register any special handlers from @ATTRIBUTES
+*/
+static int ldb_kv_attributes_load(struct ldb_module *module)
+{
+	struct ldb_schema_attribute *attrs;
+	struct ldb_context *ldb;
+	struct ldb_message *attrs_msg = NULL;
+	struct ldb_dn *dn;
+	unsigned int i;
+	unsigned int num_loaded_attrs = 0;
+	int r;
+
+	ldb = ldb_module_get_ctx(module);
+
+	if (ldb->schema.attribute_handler_override) {
+		/* we skip loading the @ATTRIBUTES record when a module is supplying
+		   its own attribute handling */
+		return 0;
+	}
+
+	attrs_msg = ldb_msg_new(module);
+	if (attrs_msg == NULL) {
+		goto failed;
+	}
+
+	dn = ldb_dn_new(module, ldb, LTDB_ATTRIBUTES);
+	if (dn == NULL) goto failed;
+
+	r = ldb_kv_search_dn1(module,
+			      dn,
+			      attrs_msg,
+			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
+				  LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
+				  LDB_UNPACK_DATA_FLAG_NO_DN);
+	talloc_free(dn);
+	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
+		goto failed;
+	}
+	if (r == LDB_ERR_NO_SUCH_OBJECT || attrs_msg->num_elements == 0) {
+		TALLOC_FREE(attrs_msg);
+		return 0;
+	}
+
+	attrs = talloc_array(attrs_msg,
+			     struct ldb_schema_attribute,
+			     attrs_msg->num_elements
+			     + ldb->schema.num_attributes);
+	if (attrs == NULL) {
+		goto failed;
+	}
+
+	memcpy(attrs,
+	       ldb->schema.attributes,
+	       sizeof(ldb->schema.attributes[0]) * ldb->schema.num_attributes);
+
+	/* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
+	   but its close enough for now */
+	for (i=0;i<attrs_msg->num_elements;i++) {
+		unsigned flags = 0, attr_flags = 0;
+		const char *syntax;
+		const struct ldb_schema_syntax *s;
+		const struct ldb_schema_attribute *a =
+			ldb_schema_attribute_by_name(ldb,
+						     attrs_msg->elements[i].name);
+		if (a != NULL && a->flags & LDB_ATTR_FLAG_FIXED) {
+			/* Must already be set in the array, and kept */
+			continue;
+		}
+
+		if (ldb_kv_attributes_flags(&attrs_msg->elements[i], &flags) !=
+		    0) {
+			ldb_debug(ldb, LDB_DEBUG_ERROR,
+				  "Invalid @ATTRIBUTES element for '%s'",
+				  attrs_msg->elements[i].name);
+			goto failed;
+		}
+
+		if (flags & LTDB_FLAG_UNIQUE_INDEX) {
+			attr_flags = LDB_ATTR_FLAG_UNIQUE_INDEX;
+		}
+		flags &= ~LTDB_FLAG_UNIQUE_INDEX;
+
+		/* These are not currently flags, each is exclusive */
+		if (flags == LTDB_FLAG_CASE_INSENSITIVE) {
+			syntax = LDB_SYNTAX_DIRECTORY_STRING;
+		} else if (flags == LTDB_FLAG_INTEGER) {
+			syntax = LDB_SYNTAX_INTEGER;
+		} else if (flags == 0) {
+			syntax = LDB_SYNTAX_OCTET_STRING;
+		} else {
+			ldb_debug(ldb, LDB_DEBUG_ERROR,
+				  "Invalid flag combination 0x%x for '%s' "
+				  "in @ATTRIBUTES",
+				  flags, attrs_msg->elements[i].name);
+			goto failed;
+		}
+
+		s = ldb_standard_syntax_by_name(ldb, syntax);
+		if (s == NULL) {
+			ldb_debug(ldb, LDB_DEBUG_ERROR,
+				  "Invalid attribute syntax '%s' for '%s' "
+				  "in @ATTRIBUTES",
+				  syntax, attrs_msg->elements[i].name);
+			goto failed;
+		}
+
+		attr_flags |= LDB_ATTR_FLAG_ALLOCATED | LDB_ATTR_FLAG_FROM_DB;
+
+		r = ldb_schema_attribute_fill_with_syntax(ldb,
+							  attrs,
+							  attrs_msg->elements[i].name,
+							  attr_flags, s,
+							  &attrs[num_loaded_attrs + ldb->schema.num_attributes]);
+		if (r != 0) {
+			goto failed;
+		}
+		num_loaded_attrs++;
+	}
+
+	attrs = talloc_realloc(attrs_msg,
+			       attrs, struct ldb_schema_attribute,
+			       num_loaded_attrs + ldb->schema.num_attributes);
+	if (attrs == NULL) {
+		goto failed;
+	}
+	TYPESAFE_QSORT(attrs, num_loaded_attrs + ldb->schema.num_attributes,
+		       ldb_schema_attribute_compare);
+	talloc_unlink(ldb, ldb->schema.attributes);
+	ldb->schema.attributes = talloc_steal(ldb, attrs);
+	ldb->schema.num_attributes = num_loaded_attrs + ldb->schema.num_attributes;
+	TALLOC_FREE(attrs_msg);
+
+	return 0;
+failed:
+	TALLOC_FREE(attrs_msg);
+	return -1;
+}
+
+/*
+  register any index records we find for the DB
+*/
+static int ldb_kv_index_load(struct ldb_module *module,
+			     struct ldb_kv_private *ldb_kv)
+{
+	struct ldb_context *ldb = ldb_module_get_ctx(module);
+	struct ldb_dn *indexlist_dn;
+	int r, lmdb_subdb_version;
+
+	if (ldb->schema.index_handler_override) {
+		/*
+		 * we skip loading the @INDEXLIST record when a module is
+		 * supplying its own attribute handling
+		 */
+		ldb_kv->cache->attribute_indexes = true;
+		ldb_kv->cache->one_level_indexes =
+		    ldb->schema.one_level_indexes;
+		ldb_kv->cache->GUID_index_attribute =
+		    ldb->schema.GUID_index_attribute;
+		ldb_kv->cache->GUID_index_dn_component =
+		    ldb->schema.GUID_index_dn_component;
+		return 0;
+	}
+
+	talloc_free(ldb_kv->cache->indexlist);
+
+	ldb_kv->cache->indexlist = ldb_msg_new(ldb_kv->cache);
+	if (ldb_kv->cache->indexlist == NULL) {
+		return -1;
+	}
+	ldb_kv->cache->one_level_indexes = false;
+	ldb_kv->cache->attribute_indexes = false;
+
+	indexlist_dn = ldb_dn_new(ldb_kv, ldb, LTDB_INDEXLIST);
+	if (indexlist_dn == NULL) {
+		return -1;
+	}
+
+	r = ldb_kv_search_dn1(module,
+			      indexlist_dn,
+			      ldb_kv->cache->indexlist,
+			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
+				  LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
+				  LDB_UNPACK_DATA_FLAG_NO_DN);
+	TALLOC_FREE(indexlist_dn);
+
+	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
+		return -1;
+	}
+
+	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXONE) !=
+	    NULL) {
+		ldb_kv->cache->one_level_indexes = true;
+	}
+	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR) !=
+	    NULL) {
+		ldb_kv->cache->attribute_indexes = true;
+	}
+	ldb_kv->cache->GUID_index_attribute = ldb_msg_find_attr_as_string(
+	    ldb_kv->cache->indexlist, LTDB_IDXGUID, NULL);
+	ldb_kv->cache->GUID_index_dn_component = ldb_msg_find_attr_as_string(
+	    ldb_kv->cache->indexlist, LTDB_IDX_DN_GUID, NULL);
+
+	lmdb_subdb_version = ldb_msg_find_attr_as_int(
+	    ldb_kv->cache->indexlist, LTDB_IDX_LMDB_SUBDB, 0);
+
+	if (lmdb_subdb_version != 0) {
+		ldb_set_errstring(ldb,
+				  "FATAL: This ldb_mdb database has "
+				  "been written in a new verson of LDB "
+				  "using a sub-database index that "
+				  "is not understood by ldb "
+				  LDB_VERSION);
+		return -1;
+	}
+
+	return 0;
+}
+
+/*
+  initialise the baseinfo record
+*/
+static int ldb_kv_baseinfo_init(struct ldb_module *module)
+{
+	struct ldb_context *ldb;
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_message *msg;
+	struct ldb_message_element el;
+	struct ldb_val val;
+	int ret;
+	/* the initial sequence number must be different from the one
+	   set in ltdb_cache_free(). Thanks to Jon for pointing this
+	   out. */
+	const char *initial_sequence_number = "1";
+
+	ldb = ldb_module_get_ctx(module);
+
+	ldb_kv->sequence_number = atof(initial_sequence_number);
+
+	msg = ldb_msg_new(ldb_kv);
+	if (msg == NULL) {
+		goto failed;
+	}
+
+	msg->num_elements = 1;
+	msg->elements = ⪙
+	msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
+	if (!msg->dn) {
+		goto failed;
+	}
+	el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
+	if (!el.name) {
+		goto failed;
+	}
+	el.values = &val;
+	el.num_values = 1;
+	el.flags = 0;
+	val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
+	if (!val.data) {
+		goto failed;
+	}
+	val.length = 1;
+
+	ret = ldb_kv_store(module, msg, TDB_INSERT);
+
+	talloc_free(msg);
+
+	return ret;
+
+failed:
+	talloc_free(msg);
+	errno = ENOMEM;
+	return LDB_ERR_OPERATIONS_ERROR;
+}
+
+/*
+  free any cache records
+ */
+static void ldb_kv_cache_free(struct ldb_module *module)
+{
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+
+	ldb_kv->sequence_number = 0;
+	talloc_free(ldb_kv->cache);
+	ldb_kv->cache = NULL;
+}
+
+/*
+  force a cache reload
+*/
+int ldb_kv_cache_reload(struct ldb_module *module)
+{
+	ldb_kv_attributes_unload(module);
+	ldb_kv_cache_free(module);
+	return ldb_kv_cache_load(module);
+}
+
+/*
+  load the cache records
+*/
+int ldb_kv_cache_load(struct ldb_module *module)
+{
+	struct ldb_context *ldb;
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
+	uint64_t seq;
+	struct ldb_message *baseinfo = NULL, *options = NULL;
+	const struct ldb_schema_attribute *a;
+	bool have_write_txn = false;
+	int r;
+
+	ldb = ldb_module_get_ctx(module);
+
+	/* a very fast check to avoid extra database reads */
+	if (ldb_kv->cache != NULL && !ldb_kv->kv_ops->has_changed(ldb_kv)) {
+		return 0;
+	}
+
+	if (ldb_kv->cache == NULL) {
+		ldb_kv->cache = talloc_zero(ldb_kv, struct ldb_kv_cache);
+		if (ldb_kv->cache == NULL)
+			goto failed;
+	}
+
+	baseinfo = ldb_msg_new(ldb_kv->cache);
+	if (baseinfo == NULL) goto failed;
+
+	baseinfo_dn = ldb_dn_new(baseinfo, ldb, LTDB_BASEINFO);
+	if (baseinfo_dn == NULL) goto failed;
+
+	r = ldb_kv->kv_ops->lock_read(module);
+	if (r != LDB_SUCCESS) {
+		goto failed;
+	}
+	r = ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0);
+	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
+		goto failed_and_unlock;
+	}
+
+	/* possibly initialise the baseinfo */
+	if (r == LDB_ERR_NO_SUCH_OBJECT) {
+
+		/* Give up the read lock, try again with a write lock */
+		r = ldb_kv->kv_ops->unlock_read(module);
+		if (r != LDB_SUCCESS) {
+			goto failed;
+		}
+
+		if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
+			goto failed;
+		}
+
+		have_write_txn = true;
+
+		/* error handling for ltdb_baseinfo_init() is by
+		   looking for the record again. */
+		ldb_kv_baseinfo_init(module);
+
+		if (ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0) !=
+		    LDB_SUCCESS) {
+			goto failed_and_unlock;
+		}
+
+	}
+
+	/* Ignore the result, and update the sequence number */
+	ldb_kv->kv_ops->has_changed(ldb_kv);
+
+	/* if the current internal sequence number is the same as the one
+	   in the database then assume the rest of the cache is OK */
+	seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
+	if (seq == ldb_kv->sequence_number) {
+		goto done;
+	}
+	ldb_kv->sequence_number = seq;
+
+	/* Read an interpret database options */
+
+	options = ldb_msg_new(ldb_kv->cache);
+	if (options == NULL) goto failed_and_unlock;
+
+	options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
+	if (options_dn == NULL) goto failed_and_unlock;
+
+	r = ldb_kv_search_dn1(module, options_dn, options, 0);
+	talloc_free(options_dn);
+	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
+		goto failed_and_unlock;
+	}
+
+	/* set flags if they do exist */
+	if (r == LDB_SUCCESS) {
+		ldb_kv->check_base =
+		    ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
+		ldb_kv->disallow_dn_filter = ldb_msg_find_attr_as_bool(
+		    options, LTDB_DISALLOW_DN_FILTER, false);
+	} else {
+		ldb_kv->check_base = false;
+		ldb_kv->disallow_dn_filter = false;
+	}
+
+	/*
+	 * ltdb_attributes_unload() calls internally talloc_free() on
+	 * any non-fixed elemnts in ldb->schema.attributes.
+	 *
+	 * NOTE WELL: This is per-ldb, not per module, so overwrites
+	 * the handlers across all databases when used under Samba's
+	 * partition module.
+	 */
+	ldb_kv_attributes_unload(module);
+
+	if (ldb_kv_index_load(module, ldb_kv) == -1) {
+		goto failed_and_unlock;
+	}
+
+	/*
+	 * NOTE WELL: This is per-ldb, not per module, so overwrites
+	 * the handlers across all databases when used under Samba's
+	 * partition module.
+	 */
+	if (ldb_kv_attributes_load(module) == -1) {
+		goto failed_and_unlock;
+	}
+
+	ldb_kv->GUID_index_syntax = NULL;
+	if (ldb_kv->cache->GUID_index_attribute != NULL) {
+		/*
+		 * Now the attributes are loaded, set the guid_index_syntax.
+		 * This can't fail, it will return a default at worst
+		 */
+		a = ldb_schema_attribute_by_name(
+		    ldb, ldb_kv->cache->GUID_index_attribute);
+		ldb_kv->GUID_index_syntax = a->syntax;
+	}
+
+done:
+	if (have_write_txn) {
+		if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
+			goto failed;
+		}
+	} else {
+		ldb_kv->kv_ops->unlock_read(module);
+	}
+
+	talloc_free(options);
+	talloc_free(baseinfo);
+	return 0;
+
+failed_and_unlock:
+	if (have_write_txn) {
+		ldb_kv->kv_ops->abort_write(ldb_kv);
+	} else {
+		ldb_kv->kv_ops->unlock_read(module);
+	}
+
+failed:
+	talloc_free(options);
+	talloc_free(baseinfo);
+	return -1;
+}
+
+
+/*
+  increase the sequence number to indicate a database change
+*/
+int ldb_kv_increase_sequence_number(struct ldb_module *module)
+{
+	struct ldb_context *ldb;
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	struct ldb_message *msg;
+	struct ldb_message_element el[2];
+	struct ldb_val val;
+	struct ldb_val val_time;
+	time_t t = time(NULL);
+	char *s = NULL;
+	int ret;
+
+	ldb = ldb_module_get_ctx(module);
+
+	msg = ldb_msg_new(ldb_kv);
+	if (msg == NULL) {
+		errno = ENOMEM;
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	s = talloc_asprintf(msg, "%llu", ldb_kv->sequence_number + 1);
+	if (!s) {
+		talloc_free(msg);
+		errno = ENOMEM;
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	msg->num_elements = ARRAY_SIZE(el);
+	msg->elements = el;
+	msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
+	if (msg->dn == NULL) {
+		talloc_free(msg);
+		errno = ENOMEM;
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
+	if (el[0].name == NULL) {
+		talloc_free(msg);
+		errno = ENOMEM;
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	el[0].values = &val;
+	el[0].num_values = 1;
+	el[0].flags = LDB_FLAG_MOD_REPLACE;
+	val.data = (uint8_t *)s;
+	val.length = strlen(s);
+
+	el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
+	if (el[1].name == NULL) {
+		talloc_free(msg);
+		errno = ENOMEM;
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	el[1].values = &val_time;
+	el[1].num_values = 1;
+	el[1].flags = LDB_FLAG_MOD_REPLACE;
+
+	s = ldb_timestring(msg, t);
+	if (s == NULL) {
+		talloc_free(msg);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	val_time.data = (uint8_t *)s;
+	val_time.length = strlen(s);
+
+	ret = ldb_kv_modify_internal(module, msg, NULL);
+
+	talloc_free(msg);
+
+	if (ret == LDB_SUCCESS) {
+		ldb_kv->sequence_number += 1;
+	}
+
+	/* updating the tdb_seqnum here avoids us reloading the cache
+	   records due to our own modification */
+	ldb_kv->kv_ops->has_changed(ldb_kv);
+
+	return ret;
+}
+
+int ldb_kv_check_at_attributes_values(const struct ldb_val *value)
+{
+	unsigned int i;
+
+	for (i = 0; ldb_kv_valid_attr_flags[i].name != NULL; i++) {
+		if ((strcmp(ldb_kv_valid_attr_flags[i].name,
+			    (char *)value->data) == 0)) {
+			return 0;
+		}
+	}
+
+	return -1;
+}
diff --git a/lib/ldb/ldb_key_value/ldb_kv_index.c b/lib/ldb/ldb_key_value/ldb_kv_index.c
new file mode 100644
index 0000000..4cefe0a
--- /dev/null
+++ b/lib/ldb/ldb_key_value/ldb_kv_index.c
@@ -0,0 +1,3123 @@
+/*
+   ldb database library
+
+   Copyright (C) Andrew Tridgell  2004-2009
+
+     ** NOTE! The following LGPL license applies to the ldb
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ *  Name: ldb
+ *
+ *  Component: ldb key value backend - indexing
+ *
+ *  Description: indexing routines for ldb key value backend
+ *
+ *  Author: Andrew Tridgell
+ */
+
+/*
+
+LDB Index design and choice of key:
+=======================================
+
+LDB has index records held as LDB objects with a special record like:
+
+dn: @INDEX:attr:value
+
+value may be base64 encoded, if it is deemed not printable:
+
+dn: @INDEX:attr::base64-value
+
+In each record, there is two possible formats:
+
+The original format is:
+-----------------------
+
+dn: @INDEX:NAME:DNSUPDATEPROXY
+ at IDXVERSION: 2
+ at IDX: CN=DnsUpdateProxy,CN=Users,DC=addom,DC=samba,DC=example,DC=com
+
+In this format, @IDX is multi-valued, one entry for each match
+
+The corrosponding entry is stored in a TDB record with key:
+
+DN=CN=DNSUPDATEPROXY,CN=USERS,DC=ADDOM,DC=SAMBA,DC=EXAMPLE,DC=COM
+
+(This allows a scope BASE search to directly find the record via
+a simple casefold of the DN).
+
+The original mixed-case DN is stored in the entry iself.
+
+
+The new 'GUID index' format is:
+-------------------------------
+
+dn: @INDEX:NAME:DNSUPDATEPROXY
+ at IDXVERSION: 3
+ at IDX: <binary GUID>[<binary GUID>[...]]
+
+The binary guid is 16 bytes, as bytes and not expanded as hexidecimal
+or pretty-printed.  The GUID is chosen from the message to be stored
+by the @IDXGUID attribute on @INDEXLIST.
+
+If there are multiple values the @IDX value simply becomes longer,
+in multiples of 16.
+
+The corrosponding entry is stored in a TDB record with key:
+
+GUID=<binary GUID>
+
+This allows a very quick translation between the fixed-length index
+values and the TDB key, while seperating entries from other data
+in the TDB, should they be unlucky enough to start with the bytes of
+the 'DN=' prefix.
+
+Additionally, this allows a scope BASE search to directly find the
+record via a simple match on a GUID= extended DN, controlled via
+ at IDX_DN_GUID on @INDEXLIST
+
+Exception for special @ DNs:
+
+ at BASEINFO, @INDEXLIST and all other special DNs are stored as per the
+original format, as they are never referenced in an index and are used
+to bootstrap the database.
+
+
+Control points for choice of index mode
+---------------------------------------
+
+The choice of index and TDB key mode is made based (for example, from
+Samba) on entries in the @INDEXLIST DN:
+
+dn: @INDEXLIST
+ at IDXGUID: objectGUID
+ at IDX_DN_GUID: GUID
+
+By default, the original DN format is used.
+
+
+Control points for choosing indexed attributes
+----------------------------------------------
+
+ at IDXATTR controls if an attribute is indexed
+
+dn: @INDEXLIST
+ at IDXATTR: samAccountName
+ at IDXATTR: nETBIOSName
+
+
+C Override functions
+--------------------
+
+void ldb_schema_set_override_GUID_index(struct ldb_context *ldb,
+                                        const char *GUID_index_attribute,
+                                        const char *GUID_index_dn_component)
+
+This is used, particularly in combination with the below, instead of
+the @IDXGUID and @IDX_DN_GUID values in @INDEXLIST.
+
+void ldb_schema_set_override_indexlist(struct ldb_context *ldb,
+                                       bool one_level_indexes);
+void ldb_schema_attribute_set_override_handler(struct ldb_context *ldb,
+                                               ldb_attribute_handler_override_fn_t override,
+                                               void *private_data);
+
+When the above two functions are called in combination, the @INDEXLIST
+values are not read from the DB, so
+ldb_schema_set_override_GUID_index() must be called.
+
+*/
+
+#include "ldb_kv.h"
+#include "../ldb_tdb/ldb_tdb.h"
+#include "ldb_private.h"
+#include "lib/util/binsearch.h"
+
+struct dn_list {
+	unsigned int count;
+	struct ldb_val *dn;
+	/*
+	 * Do not optimise the intersection of this list,
+	 * we must never return an entry not in this
+	 * list.  This allows the index for
+	 * SCOPE_ONELEVEL to be trusted.
+	 */
+	bool strict;
+};
+
+struct ldb_kv_idxptr {
+	struct tdb_context *itdb;
+	int error;
+};
+
+enum key_truncation {
+	KEY_NOT_TRUNCATED,
+	KEY_TRUNCATED,
+};
+
+static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
+				      const struct ldb_message *msg,
+				      int add);
+static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
+				   struct ldb_kv_private *ldb_kv,
+				   struct ldb_dn *base_dn,
+				   struct dn_list *dn_list,
+				   enum key_truncation *truncation);
+
+static void ldb_kv_dn_list_sort(struct ldb_kv_private *ldb_kv,
+				struct dn_list *list);
+
+/* we put a @IDXVERSION attribute on index entries. This
+   allows us to tell if it was written by an older version
+*/
+#define LTDB_INDEXING_VERSION 2
+
+#define LTDB_GUID_INDEXING_VERSION 3
+
+static unsigned ldb_kv_max_key_length(struct ldb_kv_private *ldb_kv)
+{
+	if (ldb_kv->max_key_length == 0) {
+		return UINT_MAX;
+	}
+	return ldb_kv->max_key_length;
+}
+
+/* enable the idxptr mode when transactions start */
+int ldb_kv_index_transaction_start(struct ldb_module *module)
+{
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
+	ldb_kv->idxptr = talloc_zero(ldb_kv, struct ldb_kv_idxptr);
+	if (ldb_kv->idxptr == NULL) {
+		return ldb_oom(ldb_module_get_ctx(module));
+	}
+
+	return LDB_SUCCESS;
+}
+
+/*
+  see if two ldb_val structures contain exactly the same data
+  return -1 or 1 for a mismatch, 0 for match
+*/
+static int ldb_val_equal_exact_for_qsort(const struct ldb_val *v1,
+					 const struct ldb_val *v2)
+{
+	if (v1->length > v2->length) {
+		return -1;
+	}
+	if (v1->length < v2->length) {
+		return 1;
+	}
+	return memcmp(v1->data, v2->data, v1->length);
+}
+
+/*
+  see if two ldb_val structures contain exactly the same data
+  return -1 or 1 for a mismatch, 0 for match
+*/
+static int ldb_val_equal_exact_ordered(const struct ldb_val v1,
+				       const struct ldb_val *v2)
+{
+	if (v1.length > v2->length) {
+		return -1;
+	}
+	if (v1.length < v2->length) {
+		return 1;
+	}
+	return memcmp(v1.data, v2->data, v1.length);
+}
+
+
+/*
+  find a entry in a dn_list, using a ldb_val. Uses a case sensitive
+  binary-safe comparison for the 'dn' returns -1 if not found
+
+  This is therefore safe when the value is a GUID in the future
+ */
+static int ldb_kv_dn_list_find_val(struct ldb_kv_private *ldb_kv,
+				   const struct dn_list *list,
+				   const struct ldb_val *v)
+{
+	unsigned int i;
+	struct ldb_val *exact = NULL, *next = NULL;
+
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
+		for (i=0; i<list->count; i++) {
+			if (ldb_val_equal_exact(&list->dn[i], v) == 1) {
+				return i;
+			}
+		}
+		return -1;
+	}
+
+	BINARY_ARRAY_SEARCH_GTE(list->dn, list->count,
+				*v, ldb_val_equal_exact_ordered,
+				exact, next);
+	if (exact == NULL) {
+		return -1;
+	}
+	/* Not required, but keeps the compiler quiet */
+	if (next != NULL) {
+		return -1;
+	}
+
+	i = exact - list->dn;
+	return i;
+}
+
+/*
+  find a entry in a dn_list. Uses a case sensitive comparison with the dn
+  returns -1 if not found
+ */
+static int ldb_kv_dn_list_find_msg(struct ldb_kv_private *ldb_kv,
+				   struct dn_list *list,
+				   const struct ldb_message *msg)
+{
+	struct ldb_val v;
+	const struct ldb_val *key_val;
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
+		const char *dn_str = ldb_dn_get_linearized(msg->dn);
+		v.data = discard_const_p(unsigned char, dn_str);
+		v.length = strlen(dn_str);
+	} else {
+		key_val = ldb_msg_find_ldb_val(
+		    msg, ldb_kv->cache->GUID_index_attribute);
+		if (key_val == NULL) {
+			return -1;
+		}
+		v = *key_val;
+	}
+	return ldb_kv_dn_list_find_val(ldb_kv, list, &v);
+}
+
+/*
+  this is effectively a cast function, but with lots of paranoia
+  checks and also copes with CPUs that are fussy about pointer
+  alignment
+ */
+static struct dn_list *ldb_kv_index_idxptr(struct ldb_module *module,
+					   TDB_DATA rec,
+					   bool check_parent)
+{
+	struct dn_list *list;
+	if (rec.dsize != sizeof(void *)) {
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       "Bad data size for idxptr %u", (unsigned)rec.dsize);
+		return NULL;
+	}
+	/* note that we can't just use a cast here, as rec.dptr may
+	   not be aligned sufficiently for a pointer. A cast would cause
+	   platforms like some ARM CPUs to crash */
+	memcpy(&list, rec.dptr, sizeof(void *));
+	list = talloc_get_type(list, struct dn_list);
+	if (list == NULL) {
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       "Bad type '%s' for idxptr",
+				       talloc_get_name(list));
+		return NULL;
+	}
+	if (check_parent && list->dn && talloc_parent(list->dn) != list) {
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       "Bad parent '%s' for idxptr",
+				       talloc_get_name(talloc_parent(list->dn)));
+		return NULL;
+	}
+	return list;
+}
+
+/*
+  return the @IDX list in an index entry for a dn as a
+  struct dn_list
+ */
+static int ldb_kv_dn_list_load(struct ldb_module *module,
+			       struct ldb_kv_private *ldb_kv,
+			       struct ldb_dn *dn,
+			       struct dn_list *list)
+{
+	struct ldb_message *msg;
+	int ret, version;
+	struct ldb_message_element *el;
+	TDB_DATA rec;
+	struct dn_list *list2;
+	TDB_DATA key;
+
+	list->dn = NULL;
+	list->count = 0;
+
+	/* see if we have any in-memory index entries */
+	if (ldb_kv->idxptr == NULL || ldb_kv->idxptr->itdb == NULL) {
+		goto normal_index;
+	}
+
+	key.dptr = discard_const_p(unsigned char, ldb_dn_get_linearized(dn));
+	key.dsize = strlen((char *)key.dptr);
+
+	rec = tdb_fetch(ldb_kv->idxptr->itdb, key);
+	if (rec.dptr == NULL) {
+		goto normal_index;
+	}
+
+	/* we've found an in-memory index entry */
+	list2 = ldb_kv_index_idxptr(module, rec, true);
+	if (list2 == NULL) {
+		free(rec.dptr);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	free(rec.dptr);
+
+	*list = *list2;
+	return LDB_SUCCESS;
+
+normal_index:
+	msg = ldb_msg_new(list);
+	if (msg == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ret = ldb_kv_search_dn1(module,
+				dn,
+				msg,
+				LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
+				    LDB_UNPACK_DATA_FLAG_NO_DN);
+	if (ret != LDB_SUCCESS) {
+		talloc_free(msg);
+		return ret;
+	}
+
+	el = ldb_msg_find_element(msg, LTDB_IDX);
+	if (!el) {
+		talloc_free(msg);
+		return LDB_SUCCESS;
+	}
+
+	version = ldb_msg_find_attr_as_int(msg, LTDB_IDXVERSION, 0);
+
+	/*
+	 * we avoid copying the strings by stealing the list.  We have
+	 * to steal msg onto el->values (which looks odd) because we
+	 * asked for the memory to be allocated on msg, not on each
+	 * value with LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC above
+	 */
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
+		/* check indexing version number */
+		if (version != LTDB_INDEXING_VERSION) {
+			ldb_debug_set(ldb_module_get_ctx(module),
+				      LDB_DEBUG_ERROR,
+				      "Wrong DN index version %d "
+				      "expected %d for %s",
+				      version, LTDB_INDEXING_VERSION,
+				      ldb_dn_get_linearized(dn));
+			talloc_free(msg);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+
+		talloc_steal(el->values, msg);
+		list->dn = talloc_steal(list, el->values);
+		list->count = el->num_values;
+	} else {
+		unsigned int i;
+		if (version != LTDB_GUID_INDEXING_VERSION) {
+			/* This is quite likely during the DB startup
+			   on first upgrade to using a GUID index */
+			ldb_debug_set(ldb_module_get_ctx(module),
+				      LDB_DEBUG_ERROR,
+				      "Wrong GUID index version %d "
+				      "expected %d for %s",
+				      version, LTDB_GUID_INDEXING_VERSION,
+				      ldb_dn_get_linearized(dn));
+			talloc_free(msg);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+
+		if (el->num_values == 0) {
+			talloc_free(msg);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+
+		if ((el->values[0].length % LTDB_GUID_SIZE) != 0) {
+			talloc_free(msg);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+
+		list->count = el->values[0].length / LTDB_GUID_SIZE;
+		list->dn = talloc_array(list, struct ldb_val, list->count);
+		if (list->dn == NULL) {
+			talloc_free(msg);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+
+		/*
+		 * The actual data is on msg, due to
+		 * LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
+		 */
+		talloc_steal(list->dn, msg);
+		for (i = 0; i < list->count; i++) {
+			list->dn[i].data
+				= &el->values[0].data[i * LTDB_GUID_SIZE];
+			list->dn[i].length = LTDB_GUID_SIZE;
+		}
+	}
+
+	/* We don't need msg->elements any more */
+	talloc_free(msg->elements);
+	return LDB_SUCCESS;
+}
+
+int ldb_kv_key_dn_from_idx(struct ldb_module *module,
+			   struct ldb_kv_private *ldb_kv,
+			   TALLOC_CTX *mem_ctx,
+			   struct ldb_dn *dn,
+			   TDB_DATA *tdb_key)
+{
+	struct ldb_context *ldb = ldb_module_get_ctx(module);
+	int ret;
+	int index = 0;
+	enum key_truncation truncation = KEY_NOT_TRUNCATED;
+	struct dn_list *list = talloc(mem_ctx, struct dn_list);
+	if (list == NULL) {
+		ldb_oom(ldb);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ret = ldb_kv_index_dn_base_dn(module, ldb_kv, dn, list, &truncation);
+	if (ret != LDB_SUCCESS) {
+		TALLOC_FREE(list);
+		return ret;
+	}
+
+	if (list->count == 0) {
+		TALLOC_FREE(list);
+		return LDB_ERR_NO_SUCH_OBJECT;
+	}
+
+	if (list->count > 1 && truncation == KEY_NOT_TRUNCATED)  {
+		const char *dn_str = ldb_dn_get_linearized(dn);
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       __location__
+				       ": Failed to read DN index "
+				       "against %s for %s: too many "
+				       "values (%u > 1)",
+				       ldb_kv->cache->GUID_index_attribute,
+				       dn_str,
+				       list->count);
+		TALLOC_FREE(list);
+		return LDB_ERR_CONSTRAINT_VIOLATION;
+	}
+
+	if (list->count > 0 && truncation == KEY_TRUNCATED)  {
+		/*
+		 * DN key has been truncated, need to inspect the actual
+		 * records to locate the actual DN
+		 */
+		int i;
+		index = -1;
+		for (i=0; i < list->count; i++) {
+			uint8_t guid_key[LTDB_GUID_KEY_SIZE];
+			TDB_DATA key = {
+				.dptr = guid_key,
+				.dsize = sizeof(guid_key)
+			};
+			const int flags = LDB_UNPACK_DATA_FLAG_NO_ATTRS;
+			struct ldb_message *rec = ldb_msg_new(ldb);
+			if (rec == NULL) {
+				TALLOC_FREE(list);
+				return LDB_ERR_OPERATIONS_ERROR;
+			}
+
+			ret = ldb_kv_idx_to_key(
+			    module, ldb_kv, ldb, &list->dn[i], &key);
+			if (ret != LDB_SUCCESS) {
+				TALLOC_FREE(list);
+				TALLOC_FREE(rec);
+				return ret;
+			}
+
+			ret =
+			    ldb_kv_search_key(module, ldb_kv, key, rec, flags);
+			if (key.dptr != guid_key) {
+				TALLOC_FREE(key.dptr);
+			}
+			if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+				/*
+				 * the record has disappeared?
+				 * yes, this can happen
+				 */
+				TALLOC_FREE(rec);
+				continue;
+			}
+
+			if (ret != LDB_SUCCESS) {
+				/* an internal error */
+				TALLOC_FREE(rec);
+				TALLOC_FREE(list);
+				return LDB_ERR_OPERATIONS_ERROR;
+			}
+
+			/*
+			 * We found the actual DN that we wanted from in the
+			 * multiple values that matched the index
+			 * (due to truncation), so return that.
+			 *
+			 */
+			if (ldb_dn_compare(dn, rec->dn) == 0) {
+				index = i;
+				TALLOC_FREE(rec);
+				break;
+			}
+		}
+
+		/*
+		 * We matched the index but the actual DN we wanted
+		 * was not here.
+		 */
+		if (index == -1) {
+			TALLOC_FREE(list);
+			return LDB_ERR_NO_SUCH_OBJECT;
+		}
+	}
+
+	/* The tdb_key memory is allocated by the caller */
+	ret = ldb_kv_guid_to_key(module, ldb_kv, &list->dn[index], tdb_key);
+	TALLOC_FREE(list);
+
+	if (ret != LDB_SUCCESS) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	return LDB_SUCCESS;
+}
+
+
+
+/*
+  save a dn_list into a full @IDX style record
+ */
+static int ldb_kv_dn_list_store_full(struct ldb_module *module,
+				     struct ldb_kv_private *ldb_kv,
+				     struct ldb_dn *dn,
+				     struct dn_list *list)
+{
+	struct ldb_message *msg;
+	int ret;
+
+	msg = ldb_msg_new(module);
+	if (!msg) {
+		return ldb_module_oom(module);
+	}
+
+	msg->dn = dn;
+
+	if (list->count == 0) {
+		ret = ldb_kv_delete_noindex(module, msg);
+		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+			ret = LDB_SUCCESS;
+		}
+		talloc_free(msg);
+		return ret;
+	}
+
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
+		ret = ldb_msg_add_fmt(msg, LTDB_IDXVERSION, "%u",
+				      LTDB_INDEXING_VERSION);
+		if (ret != LDB_SUCCESS) {
+			talloc_free(msg);
+			return ldb_module_oom(module);
+		}
+	} else {
+		ret = ldb_msg_add_fmt(msg, LTDB_IDXVERSION, "%u",
+				      LTDB_GUID_INDEXING_VERSION);
+		if (ret != LDB_SUCCESS) {
+			talloc_free(msg);
+			return ldb_module_oom(module);
+		}
+	}
+
+	if (list->count > 0) {
+		struct ldb_message_element *el;
+
+		ret = ldb_msg_add_empty(msg, LTDB_IDX, LDB_FLAG_MOD_ADD, &el);
+		if (ret != LDB_SUCCESS) {
+			talloc_free(msg);
+			return ldb_module_oom(module);
+		}
+
+		if (ldb_kv->cache->GUID_index_attribute == NULL) {
+			el->values = list->dn;
+			el->num_values = list->count;
+		} else {
+			struct ldb_val v;
+			unsigned int i;
+			el->values = talloc_array(msg,
+						  struct ldb_val, 1);
+			if (el->values == NULL) {
+				talloc_free(msg);
+				return ldb_module_oom(module);
+			}
+
+			v.data = talloc_array_size(el->values,
+						   list->count,
+						   LTDB_GUID_SIZE);
+			if (v.data == NULL) {
+				talloc_free(msg);
+				return ldb_module_oom(module);
+			}
+
+			v.length = talloc_get_size(v.data);
+
+			for (i = 0; i < list->count; i++) {
+				if (list->dn[i].length !=
+				    LTDB_GUID_SIZE) {
+					talloc_free(msg);
+					return ldb_module_operr(module);
+				}
+				memcpy(&v.data[LTDB_GUID_SIZE*i],
+				       list->dn[i].data,
+				       LTDB_GUID_SIZE);
+			}
+			el->values[0] = v;
+			el->num_values = 1;
+		}
+	}
+
+	ret = ldb_kv_store(module, msg, TDB_REPLACE);
+	talloc_free(msg);
+	return ret;
+}
+
+/*
+  save a dn_list into the database, in either @IDX or internal format
+ */
+static int ldb_kv_dn_list_store(struct ldb_module *module,
+				struct ldb_dn *dn,
+				struct dn_list *list)
+{
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
+	TDB_DATA rec, key;
+	int ret;
+	struct dn_list *list2;
+
+	if (ldb_kv->idxptr == NULL) {
+		return ldb_kv_dn_list_store_full(module, ldb_kv, dn, list);
+	}
+
+	if (ldb_kv->idxptr->itdb == NULL) {
+		ldb_kv->idxptr->itdb =
+		    tdb_open(NULL, 1000, TDB_INTERNAL, O_RDWR, 0);
+		if (ldb_kv->idxptr->itdb == NULL) {
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+	}
+
+	key.dptr = discard_const_p(unsigned char, ldb_dn_get_linearized(dn));
+	if (key.dptr == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	key.dsize = strlen((char *)key.dptr);
+
+	rec = tdb_fetch(ldb_kv->idxptr->itdb, key);
+	if (rec.dptr != NULL) {
+		list2 = ldb_kv_index_idxptr(module, rec, false);
+		if (list2 == NULL) {
+			free(rec.dptr);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+		free(rec.dptr);
+		list2->dn = talloc_steal(list2, list->dn);
+		list2->count = list->count;
+		return LDB_SUCCESS;
+	}
+
+	list2 = talloc(ldb_kv->idxptr, struct dn_list);
+	if (list2 == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	list2->dn = talloc_steal(list2, list->dn);
+	list2->count = list->count;
+
+	rec.dptr = (uint8_t *)&list2;
+	rec.dsize = sizeof(void *);
+
+
+	/*
+	 * This is not a store into the main DB, but into an in-memory
+	 * TDB, so we don't need a guard on ltdb->read_only
+	 */
+	ret = tdb_store(ldb_kv->idxptr->itdb, key, rec, TDB_INSERT);
+	if (ret != 0) {
+		return ltdb_err_map(tdb_error(ldb_kv->idxptr->itdb));
+	}
+	return LDB_SUCCESS;
+}
+
+/*
+  traverse function for storing the in-memory index entries on disk
+ */
+static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
+				       TDB_DATA key,
+				       TDB_DATA data,
+				       void *state)
+{
+	struct ldb_module *module = state;
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
+	struct ldb_dn *dn;
+	struct ldb_context *ldb = ldb_module_get_ctx(module);
+	struct ldb_val v;
+	struct dn_list *list;
+
+	list = ldb_kv_index_idxptr(module, data, true);
+	if (list == NULL) {
+		ldb_kv->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
+		return -1;
+	}
+
+	v.data = key.dptr;
+	v.length = strnlen((char *)key.dptr, key.dsize);
+
+	dn = ldb_dn_from_ldb_val(module, ldb, &v);
+	if (dn == NULL) {
+		ldb_asprintf_errstring(ldb, "Failed to parse index key %*.*s as an LDB DN", (int)v.length, (int)v.length, (const char *)v.data);
+		ldb_kv->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
+		return -1;
+	}
+
+	ldb_kv->idxptr->error =
+	    ldb_kv_dn_list_store_full(module, ldb_kv, dn, list);
+	talloc_free(dn);
+	if (ldb_kv->idxptr->error != 0) {
+		return -1;
+	}
+	return 0;
+}
+
+/* cleanup the idxptr mode when transaction commits */
+int ldb_kv_index_transaction_commit(struct ldb_module *module)
+{
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
+	int ret;
+
+	struct ldb_context *ldb = ldb_module_get_ctx(module);
+
+	ldb_reset_err_string(ldb);
+
+	if (ldb_kv->idxptr->itdb) {
+		tdb_traverse(
+		    ldb_kv->idxptr->itdb, ldb_kv_index_traverse_store, module);
+		tdb_close(ldb_kv->idxptr->itdb);
+	}
+
+	ret = ldb_kv->idxptr->error;
+	if (ret != LDB_SUCCESS) {
+		if (!ldb_errstring(ldb)) {
+			ldb_set_errstring(ldb, ldb_strerror(ret));
+		}
+		ldb_asprintf_errstring(ldb, "Failed to store index records in transaction commit: %s", ldb_errstring(ldb));
+	}
+
+	talloc_free(ldb_kv->idxptr);
+	ldb_kv->idxptr = NULL;
+	return ret;
+}
+
+/* cleanup the idxptr mode when transaction cancels */
+int ldb_kv_index_transaction_cancel(struct ldb_module *module)
+{
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
+	if (ldb_kv->idxptr && ldb_kv->idxptr->itdb) {
+		tdb_close(ldb_kv->idxptr->itdb);
+	}
+	talloc_free(ldb_kv->idxptr);
+	ldb_kv->idxptr = NULL;
+	return LDB_SUCCESS;
+}
+
+
+/*
+  return the dn key to be used for an index
+  the caller is responsible for freeing
+*/
+static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
+				       struct ldb_kv_private *ldb_kv,
+				       const char *attr,
+				       const struct ldb_val *value,
+				       const struct ldb_schema_attribute **ap,
+				       enum key_truncation *truncation)
+{
+	struct ldb_dn *ret;
+	struct ldb_val v;
+	const struct ldb_schema_attribute *a = NULL;
+	char *attr_folded = NULL;
+	const char *attr_for_dn = NULL;
+	int r;
+	bool should_b64_encode;
+
+	unsigned int max_key_length = ldb_kv_max_key_length(ldb_kv);
+	size_t key_len = 0;
+	size_t attr_len = 0;
+	const size_t indx_len = sizeof(LTDB_INDEX) - 1;
+	unsigned frmt_len = 0;
+	const size_t additional_key_length = 4;
+	unsigned int num_separators = 3; /* Estimate for overflow check */
+	const size_t min_data = 1;
+	const size_t min_key_length = additional_key_length
+		+ indx_len + num_separators + min_data;
+
+	if (attr[0] == '@') {
+		attr_for_dn = attr;
+		v = *value;
+		if (ap != NULL) {
+			*ap = NULL;
+		}
+	} else {
+		attr_folded = ldb_attr_casefold(ldb, attr);
+		if (!attr_folded) {
+			return NULL;
+		}
+
+		attr_for_dn = attr_folded;
+
+		a = ldb_schema_attribute_by_name(ldb, attr);
+		if (ap) {
+			*ap = a;
+		}
+		r = a->syntax->canonicalise_fn(ldb, ldb, value, &v);
+		if (r != LDB_SUCCESS) {
+			const char *errstr = ldb_errstring(ldb);
+			/* canonicalisation can be refused. For
+			   example, a attribute that takes wildcards
+			   will refuse to canonicalise if the value
+			   contains a wildcard */
+			ldb_asprintf_errstring(ldb,
+					       "Failed to create index "
+					       "key for attribute '%s':%s%s%s",
+					       attr, ldb_strerror(r),
+					       (errstr?":":""),
+					       (errstr?errstr:""));
+			talloc_free(attr_folded);
+			return NULL;
+		}
+	}
+	attr_len = strlen(attr_for_dn);
+
+	/*
+	 * Check if there is any hope this will fit into the DB.
+	 * Overflow here is not actually critical the code below
+	 * checks again to make the printf and the DB does another
+	 * check for too long keys
+	 */
+	if (max_key_length - attr_len < min_key_length) {
+		ldb_asprintf_errstring(
+			ldb,
+			__location__ ": max_key_length "
+			"is too small (%u) < (%u)",
+			max_key_length,
+			(unsigned)(min_key_length + attr_len));
+		talloc_free(attr_folded);
+		return NULL;
+	}
+
+	/*
+	 * ltdb_key_dn() makes something 4 bytes longer, it adds a leading
+	 * "DN=" and a trailing string terminator
+	 */
+	max_key_length -= additional_key_length;
+
+	/*
+	 * We do not base 64 encode a DN in a key, it has already been
+	 * casefold and lineraized, that is good enough.  That already
+	 * avoids embedded NUL etc.
+	 */
+	if (ldb_kv->cache->GUID_index_attribute != NULL) {
+		if (strcmp(attr, LTDB_IDXDN) == 0) {
+			should_b64_encode = false;
+		} else if (strcmp(attr, LTDB_IDXONE) == 0) {
+			/*
+			 * We can only change the behaviour for IDXONE
+			 * when the GUID index is enabled
+			 */
+			should_b64_encode = false;
+		} else {
+			should_b64_encode
+				= ldb_should_b64_encode(ldb, &v);
+		}
+	} else {
+		should_b64_encode = ldb_should_b64_encode(ldb, &v);
+	}
+
+	if (should_b64_encode) {
+		size_t vstr_len = 0;
+		char *vstr = ldb_base64_encode(ldb, (char *)v.data, v.length);
+		if (!vstr) {
+			talloc_free(attr_folded);
+			return NULL;
+		}
+		vstr_len = strlen(vstr);
+		/*
+		 * Overflow here is not critical as we only use this
+		 * to choose the printf truncation
+		 */
+		key_len = num_separators + indx_len + attr_len + vstr_len;
+		if (key_len > max_key_length) {
+			size_t excess = key_len - max_key_length;
+			frmt_len = vstr_len - excess;
+			*truncation = KEY_TRUNCATED;
+			/*
+			* Truncated keys are placed in a separate key space
+			* from the non truncated keys
+			* Note: the double hash "##" is not a typo and
+			* indicates that the following value is base64 encoded
+			*/
+			ret = ldb_dn_new_fmt(ldb, ldb, "%s#%s##%.*s",
+					     LTDB_INDEX, attr_for_dn,
+					     frmt_len, vstr);
+		} else {
+			frmt_len = vstr_len;
+			*truncation = KEY_NOT_TRUNCATED;
+			/*
+			 * Note: the double colon "::" is not a typo and
+			 * indicates that the following value is base64 encoded
+			 */
+			ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s::%.*s",
+					     LTDB_INDEX, attr_for_dn,
+					     frmt_len, vstr);
+		}
+		talloc_free(vstr);
+	} else {
+		/* Only need two seperators */
+		num_separators = 2;
+
+		/*
+		 * Overflow here is not critical as we only use this
+		 * to choose the printf truncation
+		 */
+		key_len = num_separators + indx_len + attr_len + (int)v.length;
+		if (key_len > max_key_length) {
+			size_t excess = key_len - max_key_length;
+			frmt_len = v.length - excess;
+			*truncation = KEY_TRUNCATED;
+			/*
+			 * Truncated keys are placed in a separate key space
+			 * from the non truncated keys
+			 */
+			ret = ldb_dn_new_fmt(ldb, ldb, "%s#%s#%.*s",
+					     LTDB_INDEX, attr_for_dn,
+					     frmt_len, (char *)v.data);
+		} else {
+			frmt_len = v.length;
+			*truncation = KEY_NOT_TRUNCATED;
+			ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s:%.*s",
+					     LTDB_INDEX, attr_for_dn,
+					     frmt_len, (char *)v.data);
+		}
+	}
+
+	if (v.data != value->data) {
+		talloc_free(v.data);
+	}
+	talloc_free(attr_folded);
+
+	return ret;
+}
+
+/*
+  see if a attribute value is in the list of indexed attributes
+*/
+static bool ldb_kv_is_indexed(struct ldb_module *module,
+			      struct ldb_kv_private *ldb_kv,
+			      const char *attr)
+{
+	struct ldb_context *ldb = ldb_module_get_ctx(module);
+	unsigned int i;
+	struct ldb_message_element *el;
+
+	if ((ldb_kv->cache->GUID_index_attribute != NULL) &&
+	    (ldb_attr_cmp(attr, ldb_kv->cache->GUID_index_attribute) == 0)) {
+		/* Implicity covered, this is the index key */
+		return false;
+	}
+	if (ldb->schema.index_handler_override) {
+		const struct ldb_schema_attribute *a
+			= ldb_schema_attribute_by_name(ldb, attr);
+
+		if (a == NULL) {
+			return false;
+		}
+
+		if (a->flags & LDB_ATTR_FLAG_INDEXED) {
+			return true;
+		} else {
+			return false;
+		}
+	}
+
+	if (!ldb_kv->cache->attribute_indexes) {
+		return false;
+	}
+
+	el = ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR);
+	if (el == NULL) {
+		return false;
+	}
+
+	/* TODO: this is too expensive! At least use a binary search */
+	for (i=0; i<el->num_values; i++) {
+		if (ldb_attr_cmp((char *)el->values[i].data, attr) == 0) {
+			return true;
+		}
+	}
+	return false;
+}
+
+/*
+  in the following logic functions, the return value is treated as
+  follows:
+
+     LDB_SUCCESS: we found some matching index values
+
+     LDB_ERR_NO_SUCH_OBJECT: we know for sure that no object matches
+
+     LDB_ERR_OPERATIONS_ERROR: indexing could not answer the call,
+                               we'll need a full search
+ */
+
+/*
+  return a list of dn's that might match a simple indexed search (an
+  equality search only)
+ */
+static int ldb_kv_index_dn_simple(struct ldb_module *module,
+				  struct ldb_kv_private *ldb_kv,
+				  const struct ldb_parse_tree *tree,
+				  struct dn_list *list)
+{
+	struct ldb_context *ldb;
+	struct ldb_dn *dn;
+	int ret;
+	enum key_truncation truncation = KEY_NOT_TRUNCATED;
+
+	ldb = ldb_module_get_ctx(module);
+
+	list->count = 0;
+	list->dn = NULL;
+
+	/* if the attribute isn't in the list of indexed attributes then
+	   this node needs a full search */
+	if (!ldb_kv_is_indexed(module, ldb_kv, tree->u.equality.attr)) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	/* the attribute is indexed. Pull the list of DNs that match the
+	   search criterion */
+	dn = ldb_kv_index_key(ldb,
+			      ldb_kv,
+			      tree->u.equality.attr,
+			      &tree->u.equality.value,
+			      NULL,
+			      &truncation);
+	/*
+	 * We ignore truncation here and allow multi-valued matches
+	 * as ltdb_search_indexed will filter out the wrong one in
+	 * ltdb_index_filter() which calls ldb_match_message().
+	 */
+	if (!dn) return LDB_ERR_OPERATIONS_ERROR;
+
+	ret = ldb_kv_dn_list_load(module, ldb_kv, dn, list);
+	talloc_free(dn);
+	return ret;
+}
+
+static bool list_union(struct ldb_context *ldb,
+		       struct ldb_kv_private *ldb_kv,
+		       struct dn_list *list,
+		       struct dn_list *list2);
+
+/*
+  return a list of dn's that might match a leaf indexed search
+ */
+static int ldb_kv_index_dn_leaf(struct ldb_module *module,
+				struct ldb_kv_private *ldb_kv,
+				const struct ldb_parse_tree *tree,
+				struct dn_list *list)
+{
+	if (ldb_kv->disallow_dn_filter &&
+	    (ldb_attr_cmp(tree->u.equality.attr, "dn") == 0)) {
+		/* in AD mode we do not support "(dn=...)" search filters */
+		list->dn = NULL;
+		list->count = 0;
+		return LDB_SUCCESS;
+	}
+	if (tree->u.equality.attr[0] == '@') {
+		/* Do not allow a indexed search against an @ */
+		list->dn = NULL;
+		list->count = 0;
+		return LDB_SUCCESS;
+	}
+	if (ldb_attr_dn(tree->u.equality.attr) == 0) {
+		enum key_truncation truncation = KEY_NOT_TRUNCATED;
+		struct ldb_dn *dn
+			= ldb_dn_from_ldb_val(list,
+					      ldb_module_get_ctx(module),
+					      &tree->u.equality.value);
+		if (dn == NULL) {
+			/* If we can't parse it, no match */
+			list->dn = NULL;
+			list->count = 0;
+			return LDB_SUCCESS;
+		}
+
+		/*
+		 * Re-use the same code we use for a SCOPE_BASE
+		 * search
+		 *
+		 * We can't call TALLOC_FREE(dn) as this must belong
+		 * to list for the memory to remain valid.
+		 */
+		return ldb_kv_index_dn_base_dn(
+		    module, ldb_kv, dn, list, &truncation);
+		/*
+		 * We ignore truncation here and allow multi-valued matches
+		 * as ltdb_search_indexed will filter out the wrong one in
+		 * ltdb_index_filter() which calls ldb_match_message().
+		 */
+
+	} else if ((ldb_kv->cache->GUID_index_attribute != NULL) &&
+		   (ldb_attr_cmp(tree->u.equality.attr,
+				 ldb_kv->cache->GUID_index_attribute) == 0)) {
+		int ret;
+		struct ldb_context *ldb = ldb_module_get_ctx(module);
+		list->dn = talloc_array(list, struct ldb_val, 1);
+		if (list->dn == NULL) {
+			ldb_module_oom(module);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+		/*
+		 * We need to go via the canonicalise_fn() to
+		 * ensure we get the index in binary, rather
+		 * than a string
+		 */
+		ret = ldb_kv->GUID_index_syntax->canonicalise_fn(
+		    ldb, list->dn, &tree->u.equality.value, &list->dn[0]);
+		if (ret != LDB_SUCCESS) {
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+		list->count = 1;
+		return LDB_SUCCESS;
+	}
+
+	return ldb_kv_index_dn_simple(module, ldb_kv, tree, list);
+}
+
+
+/*
+  list intersection
+  list = list & list2
+*/
+static bool list_intersect(struct ldb_context *ldb,
+			   struct ldb_kv_private *ldb_kv,
+			   struct dn_list *list,
+			   const struct dn_list *list2)
+{
+	const struct dn_list *short_list, *long_list;
+	struct dn_list *list3;
+	unsigned int i;
+
+	if (list->count == 0) {
+		/* 0 & X == 0 */
+		return true;
+	}
+	if (list2->count == 0) {
+		/* X & 0 == 0 */
+		list->count = 0;
+		list->dn = NULL;
+		return true;
+	}
+
+	/* the indexing code is allowed to return a longer list than
+	   what really matches, as all results are filtered by the
+	   full expression at the end - this shortcut avoids a lot of
+	   work in some cases */
+	if (list->count < 2 && list2->count > 10 && list2->strict == false) {
+		return true;
+	}
+	if (list2->count < 2 && list->count > 10 && list->strict == false) {
+		list->count = list2->count;
+		list->dn = list2->dn;
+		/* note that list2 may not be the parent of list2->dn,
+		   as list2->dn may be owned by ltdb->idxptr. In that
+		   case we expect this reparent call to fail, which is
+		   OK */
+		talloc_reparent(list2, list, list2->dn);
+		return true;
+	}
+
+	if (list->count > list2->count) {
+		short_list = list2;
+		long_list = list;
+	} else {
+		short_list = list;
+		long_list = list2;
+	}
+
+	list3 = talloc_zero(list, struct dn_list);
+	if (list3 == NULL) {
+		return false;
+	}
+
+	list3->dn = talloc_array(list3, struct ldb_val,
+				 MIN(list->count, list2->count));
+	if (!list3->dn) {
+		talloc_free(list3);
+		return false;
+	}
+	list3->count = 0;
+
+	for (i=0;i<short_list->count;i++) {
+		/* For the GUID index case, this is a binary search */
+		if (ldb_kv_dn_list_find_val(
+			ldb_kv, long_list, &short_list->dn[i]) != -1) {
+			list3->dn[list3->count] = short_list->dn[i];
+			list3->count++;
+		}
+	}
+
+	list->strict |= list2->strict;
+	list->dn = talloc_steal(list, list3->dn);
+	list->count = list3->count;
+	talloc_free(list3);
+
+	return true;
+}
+
+
+/*
+  list union
+  list = list | list2
+*/
+static bool list_union(struct ldb_context *ldb,
+		       struct ldb_kv_private *ldb_kv,
+		       struct dn_list *list,
+		       struct dn_list *list2)
+{
+	struct ldb_val *dn3;
+	unsigned int i = 0, j = 0, k = 0;
+
+	if (list2->count == 0) {
+		/* X | 0 == X */
+		return true;
+	}
+
+	if (list->count == 0) {
+		/* 0 | X == X */
+		list->count = list2->count;
+		list->dn = list2->dn;
+		/* note that list2 may not be the parent of list2->dn,
+		   as list2->dn may be owned by ltdb->idxptr. In that
+		   case we expect this reparent call to fail, which is
+		   OK */
+		talloc_reparent(list2, list, list2->dn);
+		return true;
+	}
+
+	/*
+	 * Sort the lists (if not in GUID DN mode) so we can do
+	 * the de-duplication during the merge
+	 *
+	 * NOTE: This can sort the in-memory index values, as list or
+	 * list2 might not be a copy!
+	 */
+	ldb_kv_dn_list_sort(ldb_kv, list);
+	ldb_kv_dn_list_sort(ldb_kv, list2);
+
+	dn3 = talloc_array(list, struct ldb_val, list->count + list2->count);
+	if (!dn3) {
+		ldb_oom(ldb);
+		return false;
+	}
+
+	while (i < list->count || j < list2->count) {
+		int cmp;
+		if (i >= list->count) {
+			cmp = 1;
+		} else if (j >= list2->count) {
+			cmp = -1;
+		} else {
+			cmp = ldb_val_equal_exact_ordered(list->dn[i],
+							  &list2->dn[j]);
+		}
+
+		if (cmp < 0) {
+			/* Take list */
+			dn3[k] = list->dn[i];
+			i++;
+			k++;
+		} else if (cmp > 0) {
+			/* Take list2 */
+			dn3[k] = list2->dn[j];
+			j++;
+			k++;
+		} else {
+			/* Equal, take list */
+			dn3[k] = list->dn[i];
+			i++;
+			j++;
+			k++;
+		}
+	}
+
+	list->dn = dn3;
+	list->count = k;
+
+	return true;
+}
+
+static int ldb_kv_index_dn(struct ldb_module *module,
+			   struct ldb_kv_private *ldb_kv,
+			   const struct ldb_parse_tree *tree,
+			   struct dn_list *list);
+
+/*
+  process an OR list (a union)
+ */
+static int ldb_kv_index_dn_or(struct ldb_module *module,
+			      struct ldb_kv_private *ldb_kv,
+			      const struct ldb_parse_tree *tree,
+			      struct dn_list *list)
+{
+	struct ldb_context *ldb;
+	unsigned int i;
+
+	ldb = ldb_module_get_ctx(module);
+
+	list->dn = NULL;
+	list->count = 0;
+
+	for (i=0; i<tree->u.list.num_elements; i++) {
+		struct dn_list *list2;
+		int ret;
+
+		list2 = talloc_zero(list, struct dn_list);
+		if (list2 == NULL) {
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+
+		ret = ldb_kv_index_dn(
+		    module, ldb_kv, tree->u.list.elements[i], list2);
+
+		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+			/* X || 0 == X */
+			talloc_free(list2);
+			continue;
+		}
+
+		if (ret != LDB_SUCCESS) {
+			/* X || * == * */
+			talloc_free(list2);
+			return ret;
+		}
+
+		if (!list_union(ldb, ldb_kv, list, list2)) {
+			talloc_free(list2);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+	}
+
+	if (list->count == 0) {
+		return LDB_ERR_NO_SUCH_OBJECT;
+	}
+
+	return LDB_SUCCESS;
+}
+
+
+/*
+  NOT an index results
+ */
+static int ldb_kv_index_dn_not(struct ldb_module *module,
+			       struct ldb_kv_private *ldb_kv,
+			       const struct ldb_parse_tree *tree,
+			       struct dn_list *list)
+{
+	/* the only way to do an indexed not would be if we could
+	   negate the not via another not or if we knew the total
+	   number of database elements so we could know that the
+	   existing expression covered the whole database.
+
+	   instead, we just give up, and rely on a full index scan
+	   (unless an outer & manages to reduce the list)
+	*/
+	return LDB_ERR_OPERATIONS_ERROR;
+}
+
+/*
+ * These things are unique, so avoid a full scan if this is a search
+ * by GUID, DN or a unique attribute
+ */
+static bool ldb_kv_index_unique(struct ldb_context *ldb,
+				struct ldb_kv_private *ldb_kv,
+				const char *attr)
+{
+	const struct ldb_schema_attribute *a;
+	if (ldb_kv->cache->GUID_index_attribute != NULL) {
+		if (ldb_attr_cmp(attr, ldb_kv->cache->GUID_index_attribute) ==
+		    0) {
+			return true;
+		}
+	}
+	if (ldb_attr_dn(attr) == 0) {
+		return true;
+	}
+
+	a = ldb_schema_attribute_by_name(ldb, attr);
+	if (a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX) {
+		return true;
+	}
+	return false;
+}
+
+/*
+  process an AND expression (intersection)
+ */
+static int ldb_kv_index_dn_and(struct ldb_module *module,
+			       struct ldb_kv_private *ldb_kv,
+			       const struct ldb_parse_tree *tree,
+			       struct dn_list *list)
+{
+	struct ldb_context *ldb;
+	unsigned int i;
+	bool found;
+
+	ldb = ldb_module_get_ctx(module);
+
+	list->dn = NULL;
+	list->count = 0;
+
+	/* in the first pass we only look for unique simple
+	   equality tests, in the hope of avoiding having to look
+	   at any others */
+	for (i=0; i<tree->u.list.num_elements; i++) {
+		const struct ldb_parse_tree *subtree = tree->u.list.elements[i];
+		int ret;
+
+		if (subtree->operation != LDB_OP_EQUALITY ||
+		    !ldb_kv_index_unique(
+			ldb, ldb_kv, subtree->u.equality.attr)) {
+			continue;
+		}
+
+		ret = ldb_kv_index_dn(module, ldb_kv, subtree, list);
+		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+			/* 0 && X == 0 */
+			return LDB_ERR_NO_SUCH_OBJECT;
+		}
+		if (ret == LDB_SUCCESS) {
+			/* a unique index match means we can
+			 * stop. Note that we don't care if we return
+			 * a few too many objects, due to later
+			 * filtering */
+			return LDB_SUCCESS;
+		}
+	}
+
+	/* now do a full intersection */
+	found = false;
+
+	for (i=0; i<tree->u.list.num_elements; i++) {
+		const struct ldb_parse_tree *subtree = tree->u.list.elements[i];
+		struct dn_list *list2;
+		int ret;
+
+		list2 = talloc_zero(list, struct dn_list);
+		if (list2 == NULL) {
+			return ldb_module_oom(module);
+		}
+
+		ret = ldb_kv_index_dn(module, ldb_kv, subtree, list2);
+
+		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+			/* X && 0 == 0 */
+			list->dn = NULL;
+			list->count = 0;
+			talloc_free(list2);
+			return LDB_ERR_NO_SUCH_OBJECT;
+		}
+
+		if (ret != LDB_SUCCESS) {
+			/* this didn't adding anything */
+			talloc_free(list2);
+			continue;
+		}
+
+		if (!found) {
+			talloc_reparent(list2, list, list->dn);
+			list->dn = list2->dn;
+			list->count = list2->count;
+			found = true;
+		} else if (!list_intersect(ldb, ldb_kv, list, list2)) {
+			talloc_free(list2);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+
+		if (list->count == 0) {
+			list->dn = NULL;
+			return LDB_ERR_NO_SUCH_OBJECT;
+		}
+
+		if (list->count < 2) {
+			/* it isn't worth loading the next part of the tree */
+			return LDB_SUCCESS;
+		}
+	}
+
+	if (!found) {
+		/* none of the attributes were indexed */
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	return LDB_SUCCESS;
+}
+
+/*
+  return a list of matching objects using a one-level index
+ */
+static int ldb_kv_index_dn_attr(struct ldb_module *module,
+				struct ldb_kv_private *ldb_kv,
+				const char *attr,
+				struct ldb_dn *dn,
+				struct dn_list *list,
+				enum key_truncation *truncation)
+{
+	struct ldb_context *ldb;
+	struct ldb_dn *key;
+	struct ldb_val val;
+	int ret;
+
+	ldb = ldb_module_get_ctx(module);
+
+	/* work out the index key from the parent DN */
+	val.data = (uint8_t *)((uintptr_t)ldb_dn_get_casefold(dn));
+	val.length = strlen((char *)val.data);
+	key = ldb_kv_index_key(ldb, ldb_kv, attr, &val, NULL, truncation);
+	if (!key) {
+		ldb_oom(ldb);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ret = ldb_kv_dn_list_load(module, ldb_kv, key, list);
+	talloc_free(key);
+	if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	if (list->count == 0) {
+		return LDB_ERR_NO_SUCH_OBJECT;
+	}
+
+	return LDB_SUCCESS;
+}
+
+/*
+  return a list of matching objects using a one-level index
+ */
+static int ldb_kv_index_dn_one(struct ldb_module *module,
+			       struct ldb_kv_private *ldb_kv,
+			       struct ldb_dn *parent_dn,
+			       struct dn_list *list,
+			       enum key_truncation *truncation)
+{
+	/* Ensure we do not shortcut on intersection for this list */
+	list->strict = true;
+	return ldb_kv_index_dn_attr(
+	    module, ldb_kv, LTDB_IDXONE, parent_dn, list, truncation);
+}
+
+/*
+  return a list of matching objects using the DN index
+ */
+static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
+				   struct ldb_kv_private *ldb_kv,
+				   struct ldb_dn *base_dn,
+				   struct dn_list *dn_list,
+				   enum key_truncation *truncation)
+{
+	const struct ldb_val *guid_val = NULL;
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
+		dn_list->dn = talloc_array(dn_list, struct ldb_val, 1);
+		if (dn_list->dn == NULL) {
+			return ldb_module_oom(module);
+		}
+		dn_list->dn[0].data = discard_const_p(unsigned char,
+						      ldb_dn_get_linearized(base_dn));
+		if (dn_list->dn[0].data == NULL) {
+			return ldb_module_oom(module);
+		}
+		dn_list->dn[0].length = strlen((char *)dn_list->dn[0].data);
+		dn_list->count = 1;
+
+		return LDB_SUCCESS;
+	}
+
+	if (ldb_kv->cache->GUID_index_dn_component != NULL) {
+		guid_val = ldb_dn_get_extended_component(
+		    base_dn, ldb_kv->cache->GUID_index_dn_component);
+	}
+
+	if (guid_val != NULL) {
+		dn_list->dn = talloc_array(dn_list, struct ldb_val, 1);
+		if (dn_list->dn == NULL) {
+			return ldb_module_oom(module);
+		}
+		dn_list->dn[0].data = guid_val->data;
+		dn_list->dn[0].length = guid_val->length;
+		dn_list->count = 1;
+
+		return LDB_SUCCESS;
+	}
+
+	return ldb_kv_index_dn_attr(
+	    module, ldb_kv, LTDB_IDXDN, base_dn, dn_list, truncation);
+}
+
+/*
+  return a list of dn's that might match a indexed search or
+  an error. return LDB_ERR_NO_SUCH_OBJECT for no matches, or LDB_SUCCESS for matches
+ */
+static int ldb_kv_index_dn(struct ldb_module *module,
+			   struct ldb_kv_private *ldb_kv,
+			   const struct ldb_parse_tree *tree,
+			   struct dn_list *list)
+{
+	int ret = LDB_ERR_OPERATIONS_ERROR;
+
+	switch (tree->operation) {
+	case LDB_OP_AND:
+		ret = ldb_kv_index_dn_and(module, ldb_kv, tree, list);
+		break;
+
+	case LDB_OP_OR:
+		ret = ldb_kv_index_dn_or(module, ldb_kv, tree, list);
+		break;
+
+	case LDB_OP_NOT:
+		ret = ldb_kv_index_dn_not(module, ldb_kv, tree, list);
+		break;
+
+	case LDB_OP_EQUALITY:
+		ret = ldb_kv_index_dn_leaf(module, ldb_kv, tree, list);
+		break;
+
+	case LDB_OP_SUBSTRING:
+	case LDB_OP_GREATER:
+	case LDB_OP_LESS:
+	case LDB_OP_PRESENT:
+	case LDB_OP_APPROX:
+	case LDB_OP_EXTENDED:
+		/* we can't index with fancy bitops yet */
+		ret = LDB_ERR_OPERATIONS_ERROR;
+		break;
+	}
+
+	return ret;
+}
+
+/*
+  filter a candidate dn_list from an indexed search into a set of results
+  extracting just the given attributes
+*/
+static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
+			       const struct dn_list *dn_list,
+			       struct ldb_kv_context *ac,
+			       uint32_t *match_count,
+			       enum key_truncation scope_one_truncation)
+{
+	struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
+	struct ldb_message *msg;
+	struct ldb_message *filtered_msg;
+	unsigned int i;
+	unsigned int num_keys = 0;
+	uint8_t previous_guid_key[LTDB_GUID_KEY_SIZE] = {};
+	TDB_DATA *keys = NULL;
+
+	/*
+	 * We have to allocate the key list (rather than just walk the
+	 * caller supplied list) as the callback could change the list
+	 * (by modifying an indexed attribute hosted in the in-memory
+	 * index cache!)
+	 */
+	keys = talloc_array(ac, TDB_DATA, dn_list->count);
+	if (keys == NULL) {
+		return ldb_module_oom(ac->module);
+	}
+
+	if (ldb_kv->cache->GUID_index_attribute != NULL) {
+		/*
+		 * We speculate that the keys will be GUID based and so
+		 * pre-fill in enough space for a GUID (avoiding a pile of
+		 * small allocations)
+		 */
+		struct guid_tdb_key {
+			uint8_t guid_key[LTDB_GUID_KEY_SIZE];
+		} *key_values = NULL;
+
+		key_values = talloc_array(keys,
+					  struct guid_tdb_key,
+					  dn_list->count);
+
+		if (key_values == NULL) {
+			talloc_free(keys);
+			return ldb_module_oom(ac->module);
+		}
+		for (i = 0; i < dn_list->count; i++) {
+			keys[i].dptr = key_values[i].guid_key;
+			keys[i].dsize = sizeof(key_values[i].guid_key);
+		}
+	} else {
+		for (i = 0; i < dn_list->count; i++) {
+			keys[i].dptr = NULL;
+			keys[i].dsize = 0;
+		}
+	}
+
+	for (i = 0; i < dn_list->count; i++) {
+		int ret;
+
+		ret = ldb_kv_idx_to_key(
+		    ac->module, ldb_kv, keys, &dn_list->dn[i], &keys[num_keys]);
+		if (ret != LDB_SUCCESS) {
+			talloc_free(keys);
+			return ret;
+		}
+
+		if (ldb_kv->cache->GUID_index_attribute != NULL) {
+			/*
+			 * If we are in GUID index mode, then the dn_list is
+			 * sorted.  If we got a duplicate, forget about it, as
+			 * otherwise we would send the same entry back more
+			 * than once.
+			 *
+			 * This is needed in the truncated DN case, or if a
+			 * duplicate was forced in via
+			 * LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK
+			 */
+
+			if (memcmp(previous_guid_key,
+				   keys[num_keys].dptr,
+				   sizeof(previous_guid_key)) == 0) {
+				continue;
+			}
+
+			memcpy(previous_guid_key,
+			       keys[num_keys].dptr,
+			       sizeof(previous_guid_key));
+		}
+		num_keys++;
+	}
+
+
+	/*
+	 * Now that the list is a safe copy, send the callbacks
+	 */
+	for (i = 0; i < num_keys; i++) {
+		int ret;
+		bool matched;
+		msg = ldb_msg_new(ac);
+		if (!msg) {
+			talloc_free(keys);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+
+		ret =
+		    ldb_kv_search_key(ac->module,
+				      ldb_kv,
+				      keys[i],
+				      msg,
+				      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
+					  LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC);
+		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+			/*
+			 * the record has disappeared? yes, this can
+			 * happen if the entry is deleted by something
+			 * operating in the callback (not another
+			 * process, as we have a read lock)
+			 */
+			talloc_free(msg);
+			continue;
+		}
+
+		if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
+			/* an internal error */
+			talloc_free(keys);
+			talloc_free(msg);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+
+		/*
+		 * We trust the index for LDB_SCOPE_ONELEVEL
+		 * unless the index key has been truncated.
+		 *
+		 * LDB_SCOPE_BASE is not passed in by our only caller.
+		 */
+		if (ac->scope == LDB_SCOPE_ONELEVEL &&
+		    ldb_kv->cache->one_level_indexes &&
+		    scope_one_truncation == KEY_NOT_TRUNCATED) {
+			ret = ldb_match_message(ldb, msg, ac->tree,
+						ac->scope, &matched);
+		} else {
+			ret = ldb_match_msg_error(ldb, msg,
+						  ac->tree, ac->base,
+						  ac->scope, &matched);
+		}
+
+		if (ret != LDB_SUCCESS) {
+			talloc_free(keys);
+			talloc_free(msg);
+			return ret;
+		}
+		if (!matched) {
+			talloc_free(msg);
+			continue;
+		}
+
+		/* filter the attributes that the user wants */
+		ret = ldb_kv_filter_attrs(ac, msg, ac->attrs, &filtered_msg);
+
+		talloc_free(msg);
+
+		if (ret == -1) {
+			talloc_free(keys);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+
+		ret = ldb_module_send_entry(ac->req, filtered_msg, NULL);
+		if (ret != LDB_SUCCESS) {
+			/* Regardless of success or failure, the msg
+			 * is the callbacks responsiblity, and should
+			 * not be talloc_free()'ed */
+			ac->request_terminated = true;
+			talloc_free(keys);
+			return ret;
+		}
+
+		(*match_count)++;
+	}
+
+	TALLOC_FREE(keys);
+	return LDB_SUCCESS;
+}
+
+/*
+  sort a DN list
+ */
+static void ldb_kv_dn_list_sort(struct ldb_kv_private *ltdb,
+				struct dn_list *list)
+{
+	if (list->count < 2) {
+		return;
+	}
+
+	/* We know the list is sorted when using the GUID index */
+	if (ltdb->cache->GUID_index_attribute != NULL) {
+		return;
+	}
+
+	TYPESAFE_QSORT(list->dn, list->count,
+		       ldb_val_equal_exact_for_qsort);
+}
+
+/*
+  search the database with a LDAP-like expression using indexes
+  returns -1 if an indexed search is not possible, in which
+  case the caller should call ltdb_search_full()
+*/
+int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
+{
+	struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(ac->module), struct ldb_kv_private);
+	struct dn_list *dn_list;
+	int ret;
+	enum ldb_scope index_scope;
+	enum key_truncation scope_one_truncation = KEY_NOT_TRUNCATED;
+
+	/* see if indexing is enabled */
+	if (!ldb_kv->cache->attribute_indexes &&
+	    !ldb_kv->cache->one_level_indexes && ac->scope != LDB_SCOPE_BASE) {
+		/* fallback to a full search */
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	dn_list = talloc_zero(ac, struct dn_list);
+	if (dn_list == NULL) {
+		return ldb_module_oom(ac->module);
+	}
+
+	/*
+	 * For the purposes of selecting the switch arm below, if we
+	 * don't have a one-level index then treat it like a subtree
+	 * search
+	 */
+	if (ac->scope == LDB_SCOPE_ONELEVEL &&
+	    !ldb_kv->cache->one_level_indexes) {
+		index_scope = LDB_SCOPE_SUBTREE;
+	} else {
+		index_scope = ac->scope;
+	}
+
+	switch (index_scope) {
+	case LDB_SCOPE_BASE:
+		/*
+		 * The only caller will have filtered the operation out
+		 * so we should never get here
+		 */
+		return ldb_operr(ldb);
+
+	case LDB_SCOPE_ONELEVEL:
+		/*
+		 * If we ever start to also load the index values for
+		 * the tree, we must ensure we strictly intersect with
+		 * this list, as we trust the ONELEVEL index
+		 */
+		ret = ldb_kv_index_dn_one(ac->module,
+					  ldb_kv,
+					  ac->base,
+					  dn_list,
+					  &scope_one_truncation);
+		if (ret != LDB_SUCCESS) {
+			talloc_free(dn_list);
+			return ret;
+		}
+
+		/*
+		 * If we have too many matches, running the filter
+		 * tree over the SCOPE_ONELEVEL can be quite expensive
+		 * so we now check the filter tree index as well.
+		 *
+		 * We only do this in the GUID index mode, which is
+		 * O(n*log(m)) otherwise the intersection below will
+		 * be too costly at O(n*m).
+		 *
+		 * We don't set a heuristic for 'too many' but instead
+		 * do it always and rely on the index lookup being
+		 * fast enough in the small case.
+		 */
+		if (ldb_kv->cache->GUID_index_attribute != NULL) {
+			struct dn_list *idx_one_tree_list
+				= talloc_zero(ac, struct dn_list);
+			if (idx_one_tree_list == NULL) {
+				return ldb_module_oom(ac->module);
+			}
+
+			if (!ldb_kv->cache->attribute_indexes) {
+				talloc_free(idx_one_tree_list);
+				talloc_free(dn_list);
+				return LDB_ERR_OPERATIONS_ERROR;
+			}
+			/*
+			 * Here we load the index for the tree.
+			 *
+			 * We only care if this is successful, if the
+			 * index can't trim the result list down then
+			 * the ONELEVEL index is still good enough.
+			 */
+			ret = ldb_kv_index_dn(
+			    ac->module, ldb_kv, ac->tree, idx_one_tree_list);
+			if (ret == LDB_SUCCESS) {
+				if (!list_intersect(ldb,
+						    ldb_kv,
+						    dn_list,
+						    idx_one_tree_list)) {
+					talloc_free(idx_one_tree_list);
+					talloc_free(dn_list);
+					return LDB_ERR_OPERATIONS_ERROR;
+				}
+			}
+		}
+		break;
+
+	case LDB_SCOPE_SUBTREE:
+	case LDB_SCOPE_DEFAULT:
+		if (!ldb_kv->cache->attribute_indexes) {
+			talloc_free(dn_list);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+		/*
+		 * Here we load the index for the tree.  We have no
+		 * index for the subtree.
+		 */
+		ret = ldb_kv_index_dn(ac->module, ldb_kv, ac->tree, dn_list);
+		if (ret != LDB_SUCCESS) {
+			talloc_free(dn_list);
+			return ret;
+		}
+		break;
+	}
+
+	/*
+	 * It is critical that this function do the re-filter even
+	 * on things found by the index as the index can over-match
+	 * in cases of truncation (as well as when it decides it is
+	 * not worth further filtering)
+	 *
+	 * If this changes, then the index code above would need to
+	 * pass up a flag to say if any index was truncated during
+	 * processing as the truncation here refers only to the
+	 * SCOPE_ONELEVEL index.
+	 */
+	ret = ldb_kv_index_filter(
+	    ldb_kv, dn_list, ac, match_count, scope_one_truncation);
+	talloc_free(dn_list);
+	return ret;
+}
+
+/**
+ * @brief Add a DN in the index list of a given attribute name/value pair
+ *
+ * This function will add the DN in the index list for the index for
+ * the given attribute name and value.
+ *
+ * @param[in]  module       A ldb_module structure
+ *
+ * @param[in]  dn           The string representation of the DN as it
+ *                          will be stored in the index entry
+ *
+ * @param[in]  el           A ldb_message_element array, one of the entry
+ *                          referred by the v_idx is the attribute name and
+ *                          value pair which will be used to construct the
+ *                          index name
+ *
+ * @param[in]  v_idx        The index of element in the el array to use
+ *
+ * @return                  An ldb error code
+ */
+static int ldb_kv_index_add1(struct ldb_module *module,
+			     struct ldb_kv_private *ldb_kv,
+			     const struct ldb_message *msg,
+			     struct ldb_message_element *el,
+			     int v_idx)
+{
+	struct ldb_context *ldb;
+	struct ldb_dn *dn_key;
+	int ret;
+	const struct ldb_schema_attribute *a;
+	struct dn_list *list;
+	unsigned alloc_len;
+	enum key_truncation truncation = KEY_TRUNCATED;
+
+
+	ldb = ldb_module_get_ctx(module);
+
+	list = talloc_zero(module, struct dn_list);
+	if (list == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	dn_key = ldb_kv_index_key(
+	    ldb, ldb_kv, el->name, &el->values[v_idx], &a, &truncation);
+	if (!dn_key) {
+		talloc_free(list);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	/*
+	 * Samba only maintains unique indexes on the objectSID and objectGUID
+	 * so if a unique index key exceeds the maximum length there is a
+	 * problem.
+	 */
+	if ((truncation == KEY_TRUNCATED) && (a != NULL &&
+		(a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX ||
+		(el->flags & LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX)))) {
+
+		ldb_asprintf_errstring(
+		    ldb,
+		    __location__ ": unique index key on %s in %s, "
+				 "exceeds maximum key length of %u (encoded).",
+		    el->name,
+		    ldb_dn_get_linearized(msg->dn),
+		    ldb_kv->max_key_length);
+		talloc_free(list);
+		return LDB_ERR_CONSTRAINT_VIOLATION;
+	}
+	talloc_steal(list, dn_key);
+
+	ret = ldb_kv_dn_list_load(module, ldb_kv, dn_key, list);
+	if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
+		talloc_free(list);
+		return ret;
+	}
+
+	/*
+	 * Check for duplicates in the @IDXDN DN -> GUID record
+	 *
+	 * This is very normal, it just means a duplicate DN creation
+	 * was attempted, so don't set the error string or print scary
+	 * messages.
+	 */
+	if (list->count > 0 &&
+	    ldb_attr_cmp(el->name, LTDB_IDXDN) == 0 &&
+	    truncation == KEY_NOT_TRUNCATED) {
+
+		talloc_free(list);
+		return LDB_ERR_CONSTRAINT_VIOLATION;
+
+	} else if (list->count > 0
+		   && ldb_attr_cmp(el->name, LTDB_IDXDN) == 0) {
+
+		/*
+		 * At least one existing entry in the DN->GUID index, which
+		 * arises when the DN indexes have been truncated
+		 *
+		 * So need to pull the DN's to check if it's really a duplicate
+		 */
+		int i;
+		for (i=0; i < list->count; i++) {
+			uint8_t guid_key[LTDB_GUID_KEY_SIZE];
+			TDB_DATA key = {
+				.dptr = guid_key,
+				.dsize = sizeof(guid_key)
+			};
+			const int flags = LDB_UNPACK_DATA_FLAG_NO_ATTRS;
+			struct ldb_message *rec = ldb_msg_new(ldb);
+			if (rec == NULL) {
+				return LDB_ERR_OPERATIONS_ERROR;
+			}
+
+			ret = ldb_kv_idx_to_key(
+			    module, ldb_kv, ldb, &list->dn[i], &key);
+			if (ret != LDB_SUCCESS) {
+				TALLOC_FREE(list);
+				TALLOC_FREE(rec);
+				return ret;
+			}
+
+			ret =
+			    ldb_kv_search_key(module, ldb_kv, key, rec, flags);
+			if (key.dptr != guid_key) {
+				TALLOC_FREE(key.dptr);
+			}
+			if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+				/*
+				 * the record has disappeared?
+				 * yes, this can happen
+				 */
+				talloc_free(rec);
+				continue;
+			}
+
+			if (ret != LDB_SUCCESS) {
+				/* an internal error */
+				TALLOC_FREE(rec);
+				TALLOC_FREE(list);
+				return LDB_ERR_OPERATIONS_ERROR;
+			}
+			/*
+			 * The DN we are trying to add to the DB and index
+			 * is already here, so we must deny the addition
+			 */
+			if (ldb_dn_compare(msg->dn, rec->dn) == 0) {
+				TALLOC_FREE(rec);
+				TALLOC_FREE(list);
+				return LDB_ERR_CONSTRAINT_VIOLATION;
+			}
+		}
+	}
+
+	/*
+	 * Check for duplicates in unique indexes
+	 *
+	 * We don't need to do a loop test like the @IDXDN case
+	 * above as we have a ban on long unique index values
+	 * at the start of this function.
+	 */
+	if (list->count > 0 &&
+	    ((a != NULL
+	      && (a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX ||
+		  (el->flags & LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX))))) {
+		/*
+		 * We do not want to print info about a possibly
+		 * confidential DN that the conflict was with in the
+		 * user-visible error string
+		 */
+
+		if (ldb_kv->cache->GUID_index_attribute == NULL) {
+			ldb_debug(ldb, LDB_DEBUG_WARNING,
+				  __location__
+				  ": unique index violation on %s in %s, "
+				  "conficts with %*.*s in %s",
+				  el->name, ldb_dn_get_linearized(msg->dn),
+				  (int)list->dn[0].length,
+				  (int)list->dn[0].length,
+				  list->dn[0].data,
+				  ldb_dn_get_linearized(dn_key));
+		} else {
+			/* This can't fail, gives a default at worst */
+			const struct ldb_schema_attribute *attr =
+			    ldb_schema_attribute_by_name(
+				ldb, ldb_kv->cache->GUID_index_attribute);
+			struct ldb_val v;
+			ret = attr->syntax->ldif_write_fn(ldb, list,
+							  &list->dn[0], &v);
+			if (ret == LDB_SUCCESS) {
+				ldb_debug(ldb,
+					  LDB_DEBUG_WARNING,
+					  __location__
+					  ": unique index violation on %s in "
+					  "%s, conficts with %s %*.*s in %s",
+					  el->name,
+					  ldb_dn_get_linearized(msg->dn),
+					  ldb_kv->cache->GUID_index_attribute,
+					  (int)v.length,
+					  (int)v.length,
+					  v.data,
+					  ldb_dn_get_linearized(dn_key));
+			}
+		}
+		ldb_asprintf_errstring(ldb,
+				       __location__ ": unique index violation "
+				       "on %s in %s",
+				       el->name,
+				       ldb_dn_get_linearized(msg->dn));
+		talloc_free(list);
+		return LDB_ERR_CONSTRAINT_VIOLATION;
+	}
+
+	/* overallocate the list a bit, to reduce the number of
+	 * realloc trigered copies */
+	alloc_len = ((list->count+1)+7) & ~7;
+	list->dn = talloc_realloc(list, list->dn, struct ldb_val, alloc_len);
+	if (list->dn == NULL) {
+		talloc_free(list);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
+		const char *dn_str = ldb_dn_get_linearized(msg->dn);
+		list->dn[list->count].data
+			= (uint8_t *)talloc_strdup(list->dn, dn_str);
+		if (list->dn[list->count].data == NULL) {
+			talloc_free(list);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+		list->dn[list->count].length = strlen(dn_str);
+	} else {
+		const struct ldb_val *key_val;
+		struct ldb_val *exact = NULL, *next = NULL;
+		key_val = ldb_msg_find_ldb_val(
+		    msg, ldb_kv->cache->GUID_index_attribute);
+		if (key_val == NULL) {
+			talloc_free(list);
+			return ldb_module_operr(module);
+		}
+
+		if (key_val->length != LTDB_GUID_SIZE) {
+			talloc_free(list);
+			return ldb_module_operr(module);
+		}
+
+		BINARY_ARRAY_SEARCH_GTE(list->dn, list->count,
+					*key_val, ldb_val_equal_exact_ordered,
+					exact, next);
+
+		/*
+		 * Give a warning rather than fail, this could be a
+		 * duplicate value in the record allowed by a caller
+		 * forcing in the value with
+		 * LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK
+		 */
+		if (exact != NULL && truncation == KEY_NOT_TRUNCATED) {
+			/* This can't fail, gives a default at worst */
+			const struct ldb_schema_attribute *attr =
+			    ldb_schema_attribute_by_name(
+				ldb, ldb_kv->cache->GUID_index_attribute);
+			struct ldb_val v;
+			ret = attr->syntax->ldif_write_fn(ldb, list,
+							  exact, &v);
+			if (ret == LDB_SUCCESS) {
+				ldb_debug(ldb,
+					  LDB_DEBUG_WARNING,
+					  __location__
+					  ": duplicate attribute value in %s "
+					  "for index on %s, "
+					  "duplicate of %s %*.*s in %s",
+					  ldb_dn_get_linearized(msg->dn),
+					  el->name,
+					  ldb_kv->cache->GUID_index_attribute,
+					  (int)v.length,
+					  (int)v.length,
+					  v.data,
+					  ldb_dn_get_linearized(dn_key));
+			}
+		}
+
+		if (next == NULL) {
+			next = &list->dn[list->count];
+		} else {
+			memmove(&next[1], next,
+				sizeof(*next) * (list->count - (next - list->dn)));
+		}
+		*next = ldb_val_dup(list->dn, key_val);
+		if (next->data == NULL) {
+			talloc_free(list);
+			return ldb_module_operr(module);
+		}
+	}
+	list->count++;
+
+	ret = ldb_kv_dn_list_store(module, dn_key, list);
+
+	talloc_free(list);
+
+	return ret;
+}
+
+/*
+  add index entries for one elements in a message
+ */
+static int ldb_kv_index_add_el(struct ldb_module *module,
+			       struct ldb_kv_private *ldb_kv,
+			       const struct ldb_message *msg,
+			       struct ldb_message_element *el)
+{
+	unsigned int i;
+	for (i = 0; i < el->num_values; i++) {
+		int ret = ldb_kv_index_add1(module, ldb_kv, msg, el, i);
+		if (ret != LDB_SUCCESS) {
+			return ret;
+		}
+	}
+
+	return LDB_SUCCESS;
+}
+
+/*
+  add index entries for all elements in a message
+ */
+static int ldb_kv_index_add_all(struct ldb_module *module,
+				struct ldb_kv_private *ldb_kv,
+				const struct ldb_message *msg)
+{
+	struct ldb_message_element *elements = msg->elements;
+	unsigned int i;
+	const char *dn_str;
+	int ret;
+
+	if (ldb_dn_is_special(msg->dn)) {
+		return LDB_SUCCESS;
+	}
+
+	dn_str = ldb_dn_get_linearized(msg->dn);
+	if (dn_str == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ret = ldb_kv_write_index_dn_guid(module, msg, 1);
+	if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	if (!ldb_kv->cache->attribute_indexes) {
+		/* no indexed fields */
+		return LDB_SUCCESS;
+	}
+
+	for (i = 0; i < msg->num_elements; i++) {
+		if (!ldb_kv_is_indexed(module, ldb_kv, elements[i].name)) {
+			continue;
+		}
+		ret = ldb_kv_index_add_el(module, ldb_kv, msg, &elements[i]);
+		if (ret != LDB_SUCCESS) {
+			struct ldb_context *ldb = ldb_module_get_ctx(module);
+			ldb_asprintf_errstring(ldb,
+					       __location__ ": Failed to re-index %s in %s - %s",
+					       elements[i].name, dn_str,
+					       ldb_errstring(ldb));
+			return ret;
+		}
+	}
+
+	return LDB_SUCCESS;
+}
+
+
+/*
+  insert a DN index for a message
+*/
+static int ldb_kv_modify_index_dn(struct ldb_module *module,
+				  struct ldb_kv_private *ldb_kv,
+				  const struct ldb_message *msg,
+				  struct ldb_dn *dn,
+				  const char *index,
+				  int add)
+{
+	struct ldb_message_element el;
+	struct ldb_val val;
+	int ret;
+
+	val.data = (uint8_t *)((uintptr_t)ldb_dn_get_casefold(dn));
+	if (val.data == NULL) {
+		const char *dn_str = ldb_dn_get_linearized(dn);
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       __location__ ": Failed to modify %s "
+						    "against %s in %s: failed "
+						    "to get casefold DN",
+				       index,
+				       ldb_kv->cache->GUID_index_attribute,
+				       dn_str);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	val.length = strlen((char *)val.data);
+	el.name = index;
+	el.values = &val;
+	el.num_values = 1;
+
+	if (add) {
+		ret = ldb_kv_index_add1(module, ldb_kv, msg, &el, 0);
+	} else { /* delete */
+		ret = ldb_kv_index_del_value(module, ldb_kv, msg, &el, 0);
+	}
+
+	if (ret != LDB_SUCCESS) {
+		struct ldb_context *ldb = ldb_module_get_ctx(module);
+		const char *dn_str = ldb_dn_get_linearized(dn);
+		ldb_asprintf_errstring(ldb,
+				       __location__ ": Failed to modify %s "
+						    "against %s in %s - %s",
+				       index,
+				       ldb_kv->cache->GUID_index_attribute,
+				       dn_str,
+				       ldb_errstring(ldb));
+		return ret;
+	}
+	return ret;
+}
+
+/*
+  insert a one level index for a message
+*/
+static int ldb_kv_index_onelevel(struct ldb_module *module,
+				 const struct ldb_message *msg,
+				 int add)
+{
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
+	struct ldb_dn *pdn;
+	int ret;
+
+	/* We index for ONE Level only if requested */
+	if (!ldb_kv->cache->one_level_indexes) {
+		return LDB_SUCCESS;
+	}
+
+	pdn = ldb_dn_get_parent(module, msg->dn);
+	if (pdn == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	ret =
+	    ldb_kv_modify_index_dn(module, ldb_kv, msg, pdn, LTDB_IDXONE, add);
+
+	talloc_free(pdn);
+
+	return ret;
+}
+
+/*
+  insert a one level index for a message
+*/
+static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
+				      const struct ldb_message *msg,
+				      int add)
+{
+	int ret;
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
+
+	/* We index for DN only if using a GUID index */
+	if (ldb_kv->cache->GUID_index_attribute == NULL) {
+		return LDB_SUCCESS;
+	}
+
+	ret = ldb_kv_modify_index_dn(
+	    module, ldb_kv, msg, msg->dn, LTDB_IDXDN, add);
+
+	if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       "Entry %s already exists",
+				       ldb_dn_get_linearized(msg->dn));
+		ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
+	}
+	return ret;
+}
+
+/*
+  add the index entries for a new element in a record
+  The caller guarantees that these element values are not yet indexed
+*/
+int ldb_kv_index_add_element(struct ldb_module *module,
+			     struct ldb_kv_private *ldb_kv,
+			     const struct ldb_message *msg,
+			     struct ldb_message_element *el)
+{
+	if (ldb_dn_is_special(msg->dn)) {
+		return LDB_SUCCESS;
+	}
+	if (!ldb_kv_is_indexed(module, ldb_kv, el->name)) {
+		return LDB_SUCCESS;
+	}
+	return ldb_kv_index_add_el(module, ldb_kv, msg, el);
+}
+
+/*
+  add the index entries for a new record
+*/
+int ldb_kv_index_add_new(struct ldb_module *module,
+			 struct ldb_kv_private *ldb_kv,
+			 const struct ldb_message *msg)
+{
+	int ret;
+
+	if (ldb_dn_is_special(msg->dn)) {
+		return LDB_SUCCESS;
+	}
+
+	ret = ldb_kv_index_add_all(module, ldb_kv, msg);
+	if (ret != LDB_SUCCESS) {
+		/*
+		 * Because we can't trust the caller to be doing
+		 * transactions properly, clean up any index for this
+		 * entry rather than relying on a transaction
+		 * cleanup
+		 */
+
+		ldb_kv_index_delete(module, msg);
+		return ret;
+	}
+
+	ret = ldb_kv_index_onelevel(module, msg, 1);
+	if (ret != LDB_SUCCESS) {
+		/*
+		 * Because we can't trust the caller to be doing
+		 * transactions properly, clean up any index for this
+		 * entry rather than relying on a transaction
+		 * cleanup
+		 */
+		ldb_kv_index_delete(module, msg);
+		return ret;
+	}
+	return ret;
+}
+
+
+/*
+  delete an index entry for one message element
+*/
+int ldb_kv_index_del_value(struct ldb_module *module,
+			   struct ldb_kv_private *ldb_kv,
+			   const struct ldb_message *msg,
+			   struct ldb_message_element *el,
+			   unsigned int v_idx)
+{
+	struct ldb_context *ldb;
+	struct ldb_dn *dn_key;
+	const char *dn_str;
+	int ret, i;
+	unsigned int j;
+	struct dn_list *list;
+	struct ldb_dn *dn = msg->dn;
+	enum key_truncation truncation = KEY_NOT_TRUNCATED;
+
+	ldb = ldb_module_get_ctx(module);
+
+	dn_str = ldb_dn_get_linearized(dn);
+	if (dn_str == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	if (dn_str[0] == '@') {
+		return LDB_SUCCESS;
+	}
+
+	dn_key = ldb_kv_index_key(
+	    ldb, ldb_kv, el->name, &el->values[v_idx], NULL, &truncation);
+	/*
+	 * We ignore key truncation in ltdb_index_add1() so
+	 * match that by ignoring it here as well
+	 *
+	 * Multiple values are legitimate and accepted
+	 */
+	if (!dn_key) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	list = talloc_zero(dn_key, struct dn_list);
+	if (list == NULL) {
+		talloc_free(dn_key);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ret = ldb_kv_dn_list_load(module, ldb_kv, dn_key, list);
+	if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+		/* it wasn't indexed. Did we have an earlier error? If we did then
+		   its gone now */
+		talloc_free(dn_key);
+		return LDB_SUCCESS;
+	}
+
+	if (ret != LDB_SUCCESS) {
+		talloc_free(dn_key);
+		return ret;
+	}
+
+	/*
+	 * Find one of the values matching this message to remove
+	 */
+	i = ldb_kv_dn_list_find_msg(ldb_kv, list, msg);
+	if (i == -1) {
+		/* nothing to delete */
+		talloc_free(dn_key);
+		return LDB_SUCCESS;
+	}
+
+	j = (unsigned int) i;
+	if (j != list->count - 1) {
+		memmove(&list->dn[j], &list->dn[j+1], sizeof(list->dn[0])*(list->count - (j+1)));
+	}
+	list->count--;
+	if (list->count == 0) {
+		talloc_free(list->dn);
+		list->dn = NULL;
+	} else {
+		list->dn = talloc_realloc(list, list->dn, struct ldb_val, list->count);
+	}
+
+	ret = ldb_kv_dn_list_store(module, dn_key, list);
+
+	talloc_free(dn_key);
+
+	return ret;
+}
+
+/*
+  delete the index entries for a element
+  return -1 on failure
+*/
+int ldb_kv_index_del_element(struct ldb_module *module,
+			     struct ldb_kv_private *ldb_kv,
+			     const struct ldb_message *msg,
+			     struct ldb_message_element *el)
+{
+	const char *dn_str;
+	int ret;
+	unsigned int i;
+
+	if (!ldb_kv->cache->attribute_indexes) {
+		/* no indexed fields */
+		return LDB_SUCCESS;
+	}
+
+	dn_str = ldb_dn_get_linearized(msg->dn);
+	if (dn_str == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	if (dn_str[0] == '@') {
+		return LDB_SUCCESS;
+	}
+
+	if (!ldb_kv_is_indexed(module, ldb_kv, el->name)) {
+		return LDB_SUCCESS;
+	}
+	for (i = 0; i < el->num_values; i++) {
+		ret = ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
+		if (ret != LDB_SUCCESS) {
+			return ret;
+		}
+	}
+
+	return LDB_SUCCESS;
+}
+
+/*
+  delete the index entries for a record
+  return -1 on failure
+*/
+int ldb_kv_index_delete(struct ldb_module *module,
+			const struct ldb_message *msg)
+{
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
+	int ret;
+	unsigned int i;
+
+	if (ldb_dn_is_special(msg->dn)) {
+		return LDB_SUCCESS;
+	}
+
+	ret = ldb_kv_index_onelevel(module, msg, 0);
+	if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	ret = ldb_kv_write_index_dn_guid(module, msg, 0);
+	if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	if (!ldb_kv->cache->attribute_indexes) {
+		/* no indexed fields */
+		return LDB_SUCCESS;
+	}
+
+	for (i = 0; i < msg->num_elements; i++) {
+		ret = ldb_kv_index_del_element(
+		    module, ldb_kv, msg, &msg->elements[i]);
+		if (ret != LDB_SUCCESS) {
+			return ret;
+		}
+	}
+
+	return LDB_SUCCESS;
+}
+
+
+/*
+  traversal function that deletes all @INDEX records in the in-memory
+  TDB.
+
+  This does not touch the actual DB, that is done at transaction
+  commit, which in turn greatly reduces DB churn as we will likely
+  be able to do a direct update into the old record.
+*/
+static int delete_index(struct ldb_kv_private *ldb_kv,
+			struct ldb_val key,
+			struct ldb_val data,
+			void *state)
+{
+	struct ldb_module *module = state;
+	const char *dnstr = "DN=" LTDB_INDEX ":";
+	struct dn_list list;
+	struct ldb_dn *dn;
+	struct ldb_val v;
+	int ret;
+
+	if (strncmp((char *)key.data, dnstr, strlen(dnstr)) != 0) {
+		return 0;
+	}
+	/* we need to put a empty list in the internal tdb for this
+	 * index entry */
+	list.dn = NULL;
+	list.count = 0;
+
+	/* the offset of 3 is to remove the DN= prefix. */
+	v.data = key.data + 3;
+	v.length = strnlen((char *)key.data, key.length) - 3;
+
+	dn = ldb_dn_from_ldb_val(ldb_kv, ldb_module_get_ctx(module), &v);
+
+	/*
+	 * This does not actually touch the DB quite yet, just
+         * the in-memory index cache
+	 */
+	ret = ldb_kv_dn_list_store(module, dn, &list);
+	if (ret != LDB_SUCCESS) {
+		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+				       "Unable to store null index for %s\n",
+						ldb_dn_get_linearized(dn));
+		talloc_free(dn);
+		return -1;
+	}
+	talloc_free(dn);
+	return 0;
+}
+
+/*
+  traversal function that adds @INDEX records during a re index TODO wrong comment
+*/
+static int re_key(struct ldb_kv_private *ldb_kv,
+		  struct ldb_val ldb_key,
+		  struct ldb_val val,
+		  void *state)
+{
+	struct ldb_context *ldb;
+	struct ldb_kv_reindex_context *ctx =
+	    (struct ldb_kv_reindex_context *)state;
+	struct ldb_module *module = ctx->module;
+	struct ldb_message *msg;
+	unsigned int nb_elements_in_db;
+	int ret;
+	TDB_DATA key2;
+	bool is_record;
+	TDB_DATA key = {
+		.dptr = ldb_key.data,
+		.dsize = ldb_key.length
+	};
+
+	ldb = ldb_module_get_ctx(module);
+
+	if (key.dsize > 4 &&
+	    memcmp(key.dptr, "DN=@", 4) == 0) {
+		return 0;
+	}
+
+	is_record = ldb_kv_key_is_record(key);
+	if (is_record == false) {
+		return 0;
+	}
+
+	msg = ldb_msg_new(module);
+	if (msg == NULL) {
+		return -1;
+	}
+
+	ret = ldb_unpack_data_only_attr_list_flags(ldb, &val,
+						   msg,
+						   NULL, 0,
+						   LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC,
+						   &nb_elements_in_db);
+	if (ret != 0) {
+		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %s\n",
+						ldb_dn_get_linearized(msg->dn));
+		ctx->error = ret;
+		talloc_free(msg);
+		return -1;
+	}
+
+	if (msg->dn == NULL) {
+		ldb_debug(ldb, LDB_DEBUG_ERROR,
+			  "Refusing to re-index as GUID "
+			  "key %*.*s with no DN\n",
+			  (int)key.dsize, (int)key.dsize,
+			  (char *)key.dptr);
+		talloc_free(msg);
+		return -1;
+	}
+
+	/* check if the DN key has changed, perhaps due to the case
+	   insensitivity of an element changing, or a change from DN
+	   to GUID keys */
+	key2 = ldb_kv_key_msg(module, msg, msg);
+	if (key2.dptr == NULL) {
+		/* probably a corrupt record ... darn */
+		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid DN in re_index: %s",
+						ldb_dn_get_linearized(msg->dn));
+		talloc_free(msg);
+		return 0;
+	}
+	if (key.dsize != key2.dsize ||
+	    (memcmp(key.dptr, key2.dptr, key.dsize) != 0)) {
+		struct ldb_val ldb_key2 = {
+			.data = key2.dptr,
+			.length = key2.dsize
+		};
+		ldb_kv->kv_ops->update_in_iterate(
+		    ldb_kv, ldb_key, ldb_key2, val, ctx);
+	}
+	talloc_free(key2.dptr);
+
+	talloc_free(msg);
+
+	ctx->count++;
+	if (ctx->count % 10000 == 0) {
+		ldb_debug(ldb, LDB_DEBUG_WARNING,
+			  "Reindexing: re-keyed %u records so far",
+			  ctx->count);
+	}
+
+	return 0;
+}
+
+/*
+  traversal function that adds @INDEX records during a re index
+*/
+static int re_index(struct ldb_kv_private *ldb_kv,
+		    struct ldb_val ldb_key,
+		    struct ldb_val val,
+		    void *state)
+{
+	struct ldb_context *ldb;
+	struct ldb_kv_reindex_context *ctx =
+	    (struct ldb_kv_reindex_context *)state;
+	struct ldb_module *module = ctx->module;
+	struct ldb_message *msg;
+	unsigned int nb_elements_in_db;
+	TDB_DATA key = {
+		.dptr = ldb_key.data,
+		.dsize = ldb_key.length
+	};
+	int ret;
+	bool is_record;
+
+	ldb = ldb_module_get_ctx(module);
+
+	if (key.dsize > 4 &&
+	    memcmp(key.dptr, "DN=@", 4) == 0) {
+		return 0;
+	}
+
+	is_record = ldb_kv_key_is_record(key);
+	if (is_record == false) {
+		return 0;
+	}
+
+	msg = ldb_msg_new(module);
+	if (msg == NULL) {
+		return -1;
+	}
+
+	ret = ldb_unpack_data_only_attr_list_flags(ldb, &val,
+						   msg,
+						   NULL, 0,
+						   LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC,
+						   &nb_elements_in_db);
+	if (ret != 0) {
+		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %s\n",
+						ldb_dn_get_linearized(msg->dn));
+		ctx->error = ret;
+		talloc_free(msg);
+		return -1;
+	}
+
+	if (msg->dn == NULL) {
+		ldb_debug(ldb, LDB_DEBUG_ERROR,
+			  "Refusing to re-index as GUID "
+			  "key %*.*s with no DN\n",
+			  (int)key.dsize, (int)key.dsize,
+			  (char *)key.dptr);
+		talloc_free(msg);
+		return -1;
+	}
+
+	ret = ldb_kv_index_onelevel(module, msg, 1);
+	if (ret != LDB_SUCCESS) {
+		ldb_debug(ldb, LDB_DEBUG_ERROR,
+			  "Adding special ONE LEVEL index failed (%s)!",
+						ldb_dn_get_linearized(msg->dn));
+		talloc_free(msg);
+		return -1;
+	}
+
+	ret = ldb_kv_index_add_all(module, ldb_kv, msg);
+
+	if (ret != LDB_SUCCESS) {
+		ctx->error = ret;
+		talloc_free(msg);
+		return -1;
+	}
+
+	talloc_free(msg);
+
+	ctx->count++;
+	if (ctx->count % 10000 == 0) {
+		ldb_debug(ldb, LDB_DEBUG_WARNING,
+			  "Reindexing: re-indexed %u records so far",
+			  ctx->count);
+	}
+
+	return 0;
+}
+
+/*
+  force a complete reindex of the database
+*/
+int ldb_kv_reindex(struct ldb_module *module)
+{
+	struct ldb_kv_private *ldb_kv = talloc_get_type(
+	    ldb_module_get_private(module), struct ldb_kv_private);
+	int ret;
+	struct ldb_kv_reindex_context ctx;
+
+	/*
+	 * Only triggered after a modification, but make clear we do
+	 * not re-index a read-only DB
+	 */
+	if (ldb_kv->read_only) {
+		return LDB_ERR_UNWILLING_TO_PERFORM;
+	}
+
+	if (ldb_kv_cache_reload(module) != 0) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	/*
+	 * Ensure we read (and so remove) the entries from the real
+	 * DB, no values stored so far are any use as we want to do a
+	 * re-index
+	 */
+	ldb_kv_index_transaction_cancel(module);
+
+	ret = ldb_kv_index_transaction_start(module);
+	if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	/* first traverse the database deleting any @INDEX records by
+	 * putting NULL entries in the in-memory tdb
+	 */
+	ret = ldb_kv->kv_ops->iterate(ldb_kv, delete_index, module);
+	if (ret < 0) {
+		struct ldb_context *ldb = ldb_module_get_ctx(module);
+		ldb_asprintf_errstring(ldb, "index deletion traverse failed: %s",
+				       ldb_errstring(ldb));
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ctx.module = module;
+	ctx.error = 0;
+	ctx.count = 0;
+
+	ret = ldb_kv->kv_ops->iterate(ldb_kv, re_key, &ctx);
+	if (ret < 0) {
+		struct ldb_context *ldb = ldb_module_get_ctx(module);
+		ldb_asprintf_errstring(ldb, "key correction traverse failed: %s",
+				       ldb_errstring(ldb));
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	if (ctx.error != LDB_SUCCESS) {
+		struct ldb_context *ldb = ldb_module_get_ctx(module);
+		ldb_asprintf_errstring(ldb, "reindexing failed: %s", ldb_errstring(ldb));
+		return ctx.error;
+	}
+
+	ctx.error = 0;
+	ctx.count = 0;
+
+	/* now traverse adding any indexes for normal LDB records */
+	ret = ldb_kv->kv_ops->iterate(ldb_kv, re_index, &ctx);
+	if (ret < 0) {
+		struct ldb_context *ldb = ldb_module_get_ctx(module);
+		ldb_asprintf_errstring(ldb, "reindexing traverse failed: %s",
+				       ldb_errstring(ldb));
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	if (ctx.error != LDB_SUCCESS) {
+		struct ldb_context *ldb = ldb_module_get_ctx(module);
+		ldb_asprintf_errstring(ldb, "reindexing failed: %s", ldb_errstring(ldb));
+		return ctx.error;
+	}
+
+	if (ctx.count > 10000) {
+		ldb_debug(ldb_module_get_ctx(module),
+			  LDB_DEBUG_WARNING,
+			  "Reindexing: re_index successful on %s, "
+			  "final index write-out will be in transaction commit",
+			  ldb_kv->kv_ops->name(ldb_kv));
+	}
+	return LDB_SUCCESS;
+}
diff --git a/lib/ldb/ldb_key_value/ldb_kv_search.c b/lib/ldb/ldb_key_value/ldb_kv_search.c
new file mode 100644
index 0000000..5bb780a
--- /dev/null
+++ b/lib/ldb/ldb_key_value/ldb_kv_search.c
@@ -0,0 +1,864 @@
+/*
+   ldb database library
+
+   Copyright (C) Andrew Tridgell  2004
+
+     ** NOTE! The following LGPL license applies to the ldb
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ *  Name: ldb
+ *
+ *  Component: ldb search functions
+ *
+ *  Description: functions to search ldb+tdb databases
+ *
+ *  Author: Andrew Tridgell
+ */
+
+#include "ldb_kv.h"
+#include "ldb_private.h"
+
+/*
+  add one element to a message
+*/
+static int msg_add_element(struct ldb_message *ret,
+			   const struct ldb_message_element *el,
+			   int check_duplicates)
+{
+	unsigned int i;
+	struct ldb_message_element *e2, *elnew;
+
+	if (check_duplicates && ldb_msg_find_element(ret, el->name)) {
+		/* its already there */
+		return 0;
+	}
+
+	e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
+	if (!e2) {
+		return -1;
+	}
+	ret->elements = e2;
+
+	elnew = &e2[ret->num_elements];
+
+	elnew->name = talloc_strdup(ret->elements, el->name);
+	if (!elnew->name) {
+		return -1;
+	}
+
+	if (el->num_values) {
+		elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
+		if (!elnew->values) {
+			return -1;
+		}
+	} else {
+		elnew->values = NULL;
+	}
+
+	for (i=0;i<el->num_values;i++) {
+		elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
+		if (elnew->values[i].length != el->values[i].length) {
+			return -1;
+		}
+	}
+
+	elnew->num_values = el->num_values;
+	elnew->flags = el->flags;
+
+	ret->num_elements++;
+
+	return 0;
+}
+
+/*
+  add the special distinguishedName element
+*/
+static int msg_add_distinguished_name(struct ldb_message *msg)
+{
+	struct ldb_message_element el;
+	struct ldb_val val;
+	int ret;
+
+	el.flags = 0;
+	el.name = "distinguishedName";
+	el.num_values = 1;
+	el.values = &val;
+	el.flags = 0;
+	val.data = (uint8_t *)ldb_dn_alloc_linearized(msg, msg->dn);
+	if (val.data == NULL) {
+		return -1;
+	}
+	val.length = strlen((char *)val.data);
+
+	ret = msg_add_element(msg, &el, 1);
+	return ret;
+}
+
+/*
+  search the database for a single simple dn.
+  return LDB_ERR_NO_SUCH_OBJECT on record-not-found
+  and LDB_SUCCESS on success
+*/
+int ldb_kv_search_base(struct ldb_module *module,
+		       TALLOC_CTX *mem_ctx,
+		       struct ldb_dn *dn,
+		       struct ldb_dn **ret_dn)
+{
+	int exists;
+	int ret;
+	struct ldb_message *msg = NULL;
+
+	if (ldb_dn_is_null(dn)) {
+		return LDB_ERR_NO_SUCH_OBJECT;
+	}
+
+	/*
+	 * We can't use tdb_exists() directly on a key when the TDB
+	 * key is the GUID one, not the DN based one.  So we just do a
+	 * normal search and avoid most of the allocation with the
+	 * LDB_UNPACK_DATA_FLAG_NO_DN and
+	 * LDB_UNPACK_DATA_FLAG_NO_ATTRS flags
+	 */
+	msg = ldb_msg_new(module);
+	if (msg == NULL) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ret = ldb_kv_search_dn1(module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_ATTRS);
+	if (ret == LDB_SUCCESS) {
+		const char *dn_linearized
+			= ldb_dn_get_linearized(dn);
+		const char *msg_dn_linearlized
+			= ldb_dn_get_linearized(msg->dn);
+
+		if (strcmp(dn_linearized, msg_dn_linearlized) == 0) {
+			/*
+			 * Re-use the full incoming DN for
+			 * subtree checks
+			 */
+			*ret_dn = dn;
+		} else {
+			/*
+			 * Use the string DN from the unpack, so that
+			 * we have a case-exact match of the base
+			 */
+			*ret_dn = talloc_steal(mem_ctx, msg->dn);
+		}
+		exists = true;
+	} else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+		exists = false;
+	} else {
+		talloc_free(msg);
+		return ret;
+	}
+	talloc_free(msg);
+	if (exists) {
+		return LDB_SUCCESS;
+	}
+	return LDB_ERR_NO_SUCH_OBJECT;
+}
+
+struct ldb_kv_parse_data_unpack_ctx {
+	struct ldb_message *msg;
+	struct ldb_module *module;
+	unsigned int unpack_flags;
+};
+
+static int ldb_kv_parse_data_unpack(struct ldb_val key,
+				    struct ldb_val data,
+				    void *private_data)
+{
+	struct ldb_kv_parse_data_unpack_ctx *ctx = private_data;
+	unsigned int nb_elements_in_db;
+	int ret;
+	struct ldb_context *ldb = ldb_module_get_ctx(ctx->module);
+	struct ldb_val data_parse = data;
+
+	if (ctx->unpack_flags & LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC) {
+		/*
+		 * If we got LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
+		 * we need at least do a memdup on the whole
+		 * data buffer as that may change later
+		 * and the caller needs a stable result.
+		 */
+		data_parse.data = talloc_memdup(ctx->msg,
+						data.data,
+						data.length);
+		if (data_parse.data == NULL) {
+			ldb_debug(ldb, LDB_DEBUG_ERROR,
+				  "Unable to allocate data(%d) for %*.*s\n",
+				  (int)data.length,
+				  (int)key.length, (int)key.length, key.data);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+	}
+
+	ret = ldb_unpack_data_only_attr_list_flags(ldb, &data_parse,
+						   ctx->msg,
+						   NULL, 0,
+						   ctx->unpack_flags,
+						   &nb_elements_in_db);
+	if (ret == -1) {
+		if (data_parse.data != data.data) {
+			talloc_free(data_parse.data);
+		}
+
+		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %*.*s\n",
+			  (int)key.length, (int)key.length, key.data);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	return ret;
+}
+
+/*
+  search the database for a single simple dn, returning all attributes
+  in a single message
+
+  return LDB_ERR_NO_SUCH_OBJECT on record-not-found
+  and LDB_SUCCESS on success
+*/
+int ldb_kv_search_key(struct ldb_module *module,
+		      struct ldb_kv_private *ldb_kv,
+		      const struct TDB_DATA tdb_key,
+		      struct ldb_message *msg,
+		      unsigned int unpack_flags)
+{
+	int ret;
+	struct ldb_kv_parse_data_unpack_ctx ctx = {
+		.msg = msg,
+		.module = module,
+		.unpack_flags = unpack_flags
+	};
+	struct ldb_val ldb_key = {
+		.data = tdb_key.dptr,
+		.length = tdb_key.dsize
+	};
+
+	memset(msg, 0, sizeof(*msg));
+
+	msg->num_elements = 0;
+	msg->elements = NULL;
+
+	ret = ldb_kv->kv_ops->fetch_and_parse(
+	    ldb_kv, ldb_key, ldb_kv_parse_data_unpack, &ctx);
+
+	if (ret == -1) {
+		ret = ldb_kv->kv_ops->error(ldb_kv);
+		if (ret == LDB_SUCCESS) {
+			/*
+			 * Just to be sure we don't turn errors
+			 * into success
+			 */
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+		return ret;
+	} else if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	return LDB_SUCCESS;
+}
+
+/*
+  search the database for a single simple dn, returning all attributes
+  in a single message
+
+  return LDB_ERR_NO_SUCH_OBJECT on record-not-found
+  and LDB_SUCCESS on success
+*/
+int ldb_kv_search_dn1(struct ldb_module *module,
+		      struct ldb_dn *dn,
+		      struct ldb_message *msg,
+		      unsigned int unpack_flags)
+{
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	int ret;
+	uint8_t guid_key[LTDB_GUID_KEY_SIZE];
+	TDB_DATA tdb_key = {
+		.dptr = guid_key,
+		.dsize = sizeof(guid_key)
+	};
+	TALLOC_CTX *tdb_key_ctx = NULL;
+
+	if (ldb_kv->cache->GUID_index_attribute == NULL ||
+	    ldb_dn_is_special(dn)) {
+
+		tdb_key_ctx = talloc_new(msg);
+		if (!tdb_key_ctx) {
+			return ldb_module_oom(module);
+		}
+
+		/* form the key */
+		tdb_key = ldb_kv_key_dn(module, tdb_key_ctx, dn);
+		if (!tdb_key.dptr) {
+			TALLOC_FREE(tdb_key_ctx);
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+	} else {
+		/*
+		 * Look in the index to find the key for this DN.
+		 *
+		 * the tdb_key memory is allocated above, msg is just
+		 * used for internal memory.
+		 *
+		 */
+		ret = ldb_kv_key_dn_from_idx(module, ldb_kv, msg, dn, &tdb_key);
+		if (ret != LDB_SUCCESS) {
+			return ret;
+		}
+	}
+
+	ret = ldb_kv_search_key(module, ldb_kv, tdb_key, msg, unpack_flags);
+
+	TALLOC_FREE(tdb_key_ctx);
+
+	if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	if ((unpack_flags & LDB_UNPACK_DATA_FLAG_NO_DN) == 0) {
+		if (!msg->dn) {
+			msg->dn = ldb_dn_copy(msg, dn);
+		}
+		if (!msg->dn) {
+			return LDB_ERR_OPERATIONS_ERROR;
+		}
+	}
+
+	return LDB_SUCCESS;
+}
+
+/*
+  filter the specified list of attributes from a message
+  removing not requested attrs from the new message constructed.
+
+  The reason this makes a new message is that the old one may not be
+  individually allocated, which is what our callers expect.
+
+ */
+int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
+			const struct ldb_message *msg,
+			const char *const *attrs,
+			struct ldb_message **filtered_msg)
+{
+	unsigned int i;
+	bool keep_all = false;
+	bool add_dn = false;
+	uint32_t num_elements;
+	uint32_t elements_size;
+	struct ldb_message *msg2;
+
+	msg2 = ldb_msg_new(mem_ctx);
+	if (msg2 == NULL) {
+		goto failed;
+	}
+
+	msg2->dn = ldb_dn_copy(msg2, msg->dn);
+	if (msg2->dn == NULL) {
+		goto failed;
+	}
+
+	if (attrs) {
+		/* check for special attrs */
+		for (i = 0; attrs[i]; i++) {
+			int cmp = strcmp(attrs[i], "*");
+			if (cmp == 0) {
+				keep_all = true;
+				break;
+			}
+			cmp = ldb_attr_cmp(attrs[i], "distinguishedName");
+			if (cmp == 0) {
+				add_dn = true;
+			}
+		}
+	} else {
+		keep_all = true;
+	}
+
+	if (keep_all) {
+		add_dn = true;
+		elements_size = msg->num_elements + 1;
+
+	/* Shortcuts for the simple cases */
+	} else if (add_dn && i == 1) {
+		if (msg_add_distinguished_name(msg2) != 0) {
+			goto failed;
+		}
+		*filtered_msg = msg2;
+		return 0;
+	} else if (i == 0) {
+		*filtered_msg = msg2;
+		return 0;
+
+	/* Otherwise we are copying at most as many element as we have attributes */
+	} else {
+		elements_size = i;
+	}
+
+	msg2->elements = talloc_array(msg2, struct ldb_message_element,
+				      elements_size);
+	if (msg2->elements == NULL) goto failed;
+
+	num_elements = 0;
+
+	for (i = 0; i < msg->num_elements; i++) {
+		struct ldb_message_element *el = &msg->elements[i];
+		struct ldb_message_element *el2 = &msg2->elements[num_elements];
+		unsigned int j;
+
+		if (keep_all == false) {
+			bool found = false;
+			for (j = 0; attrs[j]; j++) {
+				int cmp = ldb_attr_cmp(el->name, attrs[j]);
+				if (cmp == 0) {
+					found = true;
+					break;
+				}
+			}
+			if (found == false) {
+				continue;
+			}
+		}
+		*el2 = *el;
+		el2->name = talloc_strdup(msg2->elements, el->name);
+		if (el2->name == NULL) {
+			goto failed;
+		}
+		el2->values = talloc_array(msg2->elements, struct ldb_val, el->num_values);
+		if (el2->values == NULL) {
+			goto failed;
+		}
+		for (j=0;j<el->num_values;j++) {
+			el2->values[j] = ldb_val_dup(el2->values, &el->values[j]);
+			if (el2->values[j].data == NULL && el->values[j].length != 0) {
+				goto failed;
+			}
+		}
+		num_elements++;
+
+		/* Pidginhole principle: we can't have more elements
+		 * than the number of attributes if they are unique in
+		 * the DB */
+		if (num_elements > elements_size) {
+			goto failed;
+		}
+	}
+
+	msg2->num_elements = num_elements;
+
+	if (add_dn) {
+		if (msg_add_distinguished_name(msg2) != 0) {
+			goto failed;
+		}
+	}
+
+	if (msg2->num_elements > 0) {
+		msg2->elements = talloc_realloc(msg2, msg2->elements,
+						struct ldb_message_element,
+						msg2->num_elements);
+		if (msg2->elements == NULL) {
+			goto failed;
+		}
+	} else {
+		talloc_free(msg2->elements);
+		msg2->elements = NULL;
+	}
+
+	*filtered_msg = msg2;
+
+	return 0;
+failed:
+	TALLOC_FREE(msg2);
+	return -1;
+}
+
+/*
+  search function for a non-indexed search
+ */
+static int search_func(struct ldb_kv_private *ldb_kv,
+		       struct ldb_val key,
+		       struct ldb_val val,
+		       void *state)
+{
+	struct ldb_context *ldb;
+	struct ldb_kv_context *ac;
+	struct ldb_message *msg, *filtered_msg;
+	int ret;
+	bool matched;
+	unsigned int nb_elements_in_db;
+	TDB_DATA tdb_key = {
+		.dptr = key.data,
+		.dsize = key.length
+	};
+
+	ac = talloc_get_type(state, struct ldb_kv_context);
+	ldb = ldb_module_get_ctx(ac->module);
+
+	if (ldb_kv_key_is_record(tdb_key) == false) {
+		return 0;
+	}
+
+	msg = ldb_msg_new(ac);
+	if (!msg) {
+		ac->error = LDB_ERR_OPERATIONS_ERROR;
+		return -1;
+	}
+
+	/* unpack the record */
+	ret = ldb_unpack_data_only_attr_list_flags(ldb, &val,
+						   msg,
+						   NULL, 0,
+						   LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC|
+						   LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC,
+						   &nb_elements_in_db);
+	if (ret == -1) {
+		talloc_free(msg);
+		ac->error = LDB_ERR_OPERATIONS_ERROR;
+		return -1;
+	}
+
+	if (!msg->dn) {
+		msg->dn = ldb_dn_new(msg, ldb,
+				     (char *)key.data + 3);
+		if (msg->dn == NULL) {
+			talloc_free(msg);
+			ac->error = LDB_ERR_OPERATIONS_ERROR;
+			return -1;
+		}
+	}
+
+	/* see if it matches the given expression */
+	ret = ldb_match_msg_error(ldb, msg,
+				  ac->tree, ac->base, ac->scope, &matched);
+	if (ret != LDB_SUCCESS) {
+		talloc_free(msg);
+		ac->error = LDB_ERR_OPERATIONS_ERROR;
+		return -1;
+	}
+	if (!matched) {
+		talloc_free(msg);
+		return 0;
+	}
+
+	/* filter the attributes that the user wants */
+	ret = ldb_kv_filter_attrs(ac, msg, ac->attrs, &filtered_msg);
+	talloc_free(msg);
+
+	if (ret == -1) {
+		ac->error = LDB_ERR_OPERATIONS_ERROR;
+		return -1;
+	}
+
+	ret = ldb_module_send_entry(ac->req, filtered_msg, NULL);
+	if (ret != LDB_SUCCESS) {
+		ac->request_terminated = true;
+		/* the callback failed, abort the operation */
+		ac->error = LDB_ERR_OPERATIONS_ERROR;
+		return -1;
+	}
+
+	return 0;
+}
+
+
+/*
+  search the database with a LDAP-like expression.
+  this is the "full search" non-indexed variant
+*/
+static int ldb_kv_search_full(struct ldb_kv_context *ctx)
+{
+	void *data = ldb_module_get_private(ctx->module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	int ret;
+
+	ctx->error = LDB_SUCCESS;
+	ret = ldb_kv->kv_ops->iterate(ldb_kv, search_func, ctx);
+
+	if (ret < 0) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	return ctx->error;
+}
+
+static int ldb_kv_search_and_return_base(struct ldb_kv_private *ldb_kv,
+					 struct ldb_kv_context *ctx)
+{
+	struct ldb_message *msg, *filtered_msg;
+	struct ldb_context *ldb = ldb_module_get_ctx(ctx->module);
+	const char *dn_linearized;
+	const char *msg_dn_linearlized;
+	int ret;
+	bool matched;
+
+	msg = ldb_msg_new(ctx);
+	if (!msg) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+	ret = ldb_kv_search_dn1(ctx->module,
+				ctx->base,
+				msg,
+				LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
+				    LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC);
+
+	if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+		if (ldb_kv->check_base == false) {
+			/*
+			 * In this case, we are done, as no base
+			 * checking is allowed in this DB
+			 */
+			talloc_free(msg);
+			return LDB_SUCCESS;
+		}
+		ldb_asprintf_errstring(ldb,
+				       "No such Base DN: %s",
+				       ldb_dn_get_linearized(ctx->base));
+	}
+	if (ret != LDB_SUCCESS) {
+		talloc_free(msg);
+		return ret;
+	}
+
+
+	/*
+	 * We use this, not ldb_match_msg_error() as we know
+	 * we matched on the scope BASE, as we just fetched
+	 * the base DN
+	 */
+
+	ret = ldb_match_message(ldb, msg,
+				ctx->tree,
+				ctx->scope,
+				&matched);
+	if (ret != LDB_SUCCESS) {
+		talloc_free(msg);
+		return ret;
+	}
+	if (!matched) {
+		talloc_free(msg);
+		return LDB_SUCCESS;
+	}
+
+	dn_linearized = ldb_dn_get_linearized(ctx->base);
+	msg_dn_linearlized = ldb_dn_get_linearized(msg->dn);
+
+	if (strcmp(dn_linearized, msg_dn_linearlized) == 0) {
+		/*
+		 * If the DN is exactly the same string, then
+		 * re-use the full incoming DN for the
+		 * returned result, as it has already been
+		 * casefolded
+		 */
+		msg->dn = ctx->base;
+	}
+
+	/*
+	 * filter the attributes that the user wants.
+	 *
+	 * This copies msg->dn including the casefolding, so the above
+	 * assignment is safe
+	 */
+	ret = ldb_kv_filter_attrs(ctx, msg, ctx->attrs, &filtered_msg);
+
+	/*
+	 * Remove any extended components possibly copied in from
+	 * msg->dn, we just want the casefold components
+	 */
+	ldb_dn_remove_extended_components(filtered_msg->dn);
+	talloc_free(msg);
+
+	if (ret == -1) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ret = ldb_module_send_entry(ctx->req, filtered_msg, NULL);
+	if (ret != LDB_SUCCESS) {
+		/* Regardless of success or failure, the msg
+		 * is the callbacks responsiblity, and should
+		 * not be talloc_free()'ed */
+		ctx->request_terminated = true;
+		return ret;
+	}
+
+	return LDB_SUCCESS;
+}
+
+/*
+  search the database with a LDAP-like expression.
+  choses a search method
+*/
+int ldb_kv_search(struct ldb_kv_context *ctx)
+{
+	struct ldb_context *ldb;
+	struct ldb_module *module = ctx->module;
+	struct ldb_request *req = ctx->req;
+	void *data = ldb_module_get_private(module);
+	struct ldb_kv_private *ldb_kv =
+	    talloc_get_type(data, struct ldb_kv_private);
+	int ret;
+
+	ldb = ldb_module_get_ctx(module);
+
+	ldb_request_set_state(req, LDB_ASYNC_PENDING);
+
+	if (ldb_kv->kv_ops->lock_read(module) != 0) {
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	if (ldb_kv_cache_load(module) != 0) {
+		ldb_kv->kv_ops->unlock_read(module);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	if (req->op.search.tree == NULL) {
+		ldb_kv->kv_ops->unlock_read(module);
+		return LDB_ERR_OPERATIONS_ERROR;
+	}
+
+	ctx->tree = req->op.search.tree;
+	ctx->scope = req->op.search.scope;
+	ctx->base = req->op.search.base;
+	ctx->attrs = req->op.search.attrs;
+
+	if ((req->op.search.base == NULL) || (ldb_dn_is_null(req->op.search.base) == true)) {
+
+		/* Check what we should do with a NULL dn */
+		switch (req->op.search.scope) {
+		case LDB_SCOPE_BASE:
+			ldb_asprintf_errstring(ldb,
+					       "NULL Base DN invalid for a base search");
+			ret = LDB_ERR_INVALID_DN_SYNTAX;
+			break;
+		case LDB_SCOPE_ONELEVEL:
+			ldb_asprintf_errstring(ldb,
+					       "NULL Base DN invalid for a one-level search");
+			ret = LDB_ERR_INVALID_DN_SYNTAX;
+			break;
+		case LDB_SCOPE_SUBTREE:
+		default:
+			/* We accept subtree searches from a NULL base DN, ie over the whole DB */
+			ret = LDB_SUCCESS;
+		}
+	} else if (ldb_dn_is_valid(req->op.search.base) == false) {
+
+		/* We don't want invalid base DNs here */
+		ldb_asprintf_errstring(ldb,
+				       "Invalid Base DN: %s",
+				       ldb_dn_get_linearized(req->op.search.base));
+		ret = LDB_ERR_INVALID_DN_SYNTAX;
+
+	} else if (req->op.search.scope == LDB_SCOPE_BASE) {
+
+		/*
+		 * If we are LDB_SCOPE_BASE, do just one search and
+		 * return early.  This is critical to ensure we do not
+		 * go into the index code for special DNs, as that
+		 * will try to look up an index record for a special
+		 * record (which doesn't exist).
+		 */
+		ret = ldb_kv_search_and_return_base(ldb_kv, ctx);
+
+		ldb_kv->kv_ops->unlock_read(module);
+
+		return ret;
+
+	} else if (ldb_kv->check_base) {
+		/*
+		 * This database has been marked as
+		 * 'checkBaseOnSearch', so do a spot check of the base
+		 * dn.  Also optimise the subsequent filter by filling
+		 * in the ctx->base to be exactly case correct
+		 */
+		ret = ldb_kv_search_base(
+		    module, ctx, req->op.search.base, &ctx->base);
+
+		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+			ldb_asprintf_errstring(ldb,
+					       "No such Base DN: %s",
+					       ldb_dn_get_linearized(req->op.search.base));
+		}
+
+	} else {
+		/* If we are not checking the base DN life is easy */
+		ret = LDB_SUCCESS;
+	}
+
+	if (ret == LDB_SUCCESS) {
+		uint32_t match_count = 0;
+
+		ret = ldb_kv_search_indexed(ctx, &match_count);
+		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+			/* Not in the index, therefore OK! */
+			ret = LDB_SUCCESS;
+
+		}
+		/* Check if we got just a normal error.
+		 * In that case proceed to a full search unless we got a
+		 * callback error */
+		if (!ctx->request_terminated && ret != LDB_SUCCESS) {
+			/* Not indexed, so we need to do a full scan */
+			if (ldb_kv->warn_unindexed ||
+			    ldb_kv->disable_full_db_scan) {
+				/* useful for debugging when slow performance
+				 * is caused by unindexed searches */
+				char *expression = ldb_filter_from_tree(ctx, ctx->tree);
+				ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb FULL SEARCH: %s SCOPE: %s DN: %s",
+							expression,
+							req->op.search.scope==LDB_SCOPE_BASE?"base":
+							req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
+							req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN",
+							ldb_dn_get_linearized(req->op.search.base));
+
+				talloc_free(expression);
+			}
+
+			if (match_count != 0) {
+				/* the indexing code gave an error
+				 * after having returned at least one
+				 * entry. This means the indexes are
+				 * corrupt or a database record is
+				 * corrupt. We cannot continue with a
+				 * full search or we may return
+				 * duplicate entries
+				 */
+				ldb_kv->kv_ops->unlock_read(module);
+				return LDB_ERR_OPERATIONS_ERROR;
+			}
+
+			if (ldb_kv->disable_full_db_scan) {
+				ldb_set_errstring(ldb,
+						  "ldb FULL SEARCH disabled");
+				ldb_kv->kv_ops->unlock_read(module);
+				return LDB_ERR_INAPPROPRIATE_MATCHING;
+			}
+
+			ret = ldb_kv_search_full(ctx);
+			if (ret != LDB_SUCCESS) {
+				ldb_set_errstring(ldb, "Indexed and full searches both failed!\n");
+			}
+		}
+	}
+
+	ldb_kv->kv_ops->unlock_read(module);
+
+	return ret;
+}
diff --git a/lib/ldb/ldb_mdb/ldb_mdb.c b/lib/ldb/ldb_mdb/ldb_mdb.c
index 9f47746..3dc790a 100644
--- a/lib/ldb/ldb_mdb/ldb_mdb.c
+++ b/lib/ldb/ldb_mdb/ldb_mdb.c
@@ -23,7 +23,7 @@
 */
 
 #include "ldb_mdb.h"
-#include "../ldb_tdb/ldb_tdb.h"
+#include "../ldb_key_value/ldb_kv.h"
 #include "include/dlinklist.h"
 
 #define MDB_URL_PREFIX		"mdb://"
diff --git a/lib/ldb/ldb_tdb/ldb_cache.c b/lib/ldb/ldb_tdb/ldb_cache.c
deleted file mode 100644
index 9e81b61..0000000
--- a/lib/ldb/ldb_tdb/ldb_cache.c
+++ /dev/null
@@ -1,663 +0,0 @@
-/* 
-   ldb database library
-
-   Copyright (C) Andrew Tridgell  2004
-
-     ** NOTE! The following LGPL license applies to the ldb
-     ** library. This does NOT imply that all of Samba is released
-     ** under the LGPL
-   
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 3 of the License, or (at your option) any later version.
-
-   This library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, see <http://www.gnu.org/licenses/>.
-*/
-
-/*
- *  Name: ldb
- *
- *  Component: ldb tdb cache functions
- *
- *  Description: cache special records in a ldb/tdb
- *
- *  Author: Andrew Tridgell
- */
-
-#include "ldb_tdb.h"
-#include "ldb_private.h"
-
-#define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
-#define LTDB_FLAG_INTEGER          (1<<1)
-#define LTDB_FLAG_UNIQUE_INDEX     (1<<2)
-
-/* valid attribute flags */
-static const struct {
-	const char *name;
-	int value;
-} ldb_kv_valid_attr_flags[] = {
-	{ "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
-	{ "INTEGER", LTDB_FLAG_INTEGER },
-	{ "HIDDEN", 0 },
-	{ "UNIQUE_INDEX",  LTDB_FLAG_UNIQUE_INDEX},
-	{ "NONE", 0 },
-	{ NULL, 0 }
-};
-
-/*
-  de-register any special handlers for @ATTRIBUTES
-*/
-static void ldb_kv_attributes_unload(struct ldb_module *module)
-{
-	struct ldb_context *ldb = ldb_module_get_ctx(module);
-
-	ldb_schema_attribute_remove_flagged(ldb, LDB_ATTR_FLAG_FROM_DB);
-
-}
-
-/*
-  add up the attrib flags for a @ATTRIBUTES element
-*/
-static int ldb_kv_attributes_flags(struct ldb_message_element *el, unsigned *v)
-{
-	unsigned int i;
-	unsigned value = 0;
-	for (i=0;i<el->num_values;i++) {
-		unsigned int j;
-		for (j = 0; ldb_kv_valid_attr_flags[j].name; j++) {
-			if (strcmp(ldb_kv_valid_attr_flags[j].name,
-				   (char *)el->values[i].data) == 0) {
-				value |= ldb_kv_valid_attr_flags[j].value;
-				break;
-			}
-		}
-		if (ldb_kv_valid_attr_flags[j].name == NULL) {
-			return -1;
-		}
-	}
-	*v = value;
-	return 0;
-}
-
-static int ldb_schema_attribute_compare(const void *p1, const void *p2)
-{
-	const struct ldb_schema_attribute *sa1 = (const struct ldb_schema_attribute *)p1;
-	const struct ldb_schema_attribute *sa2 = (const struct ldb_schema_attribute *)p2;
-	return ldb_attr_cmp(sa1->name, sa2->name);
-}
-
-/*
-  register any special handlers from @ATTRIBUTES
-*/
-static int ldb_kv_attributes_load(struct ldb_module *module)
-{
-	struct ldb_schema_attribute *attrs;
-	struct ldb_context *ldb;
-	struct ldb_message *attrs_msg = NULL;
-	struct ldb_dn *dn;
-	unsigned int i;
-	unsigned int num_loaded_attrs = 0;
-	int r;
-
-	ldb = ldb_module_get_ctx(module);
-
-	if (ldb->schema.attribute_handler_override) {
-		/* we skip loading the @ATTRIBUTES record when a module is supplying
-		   its own attribute handling */
-		return 0;
-	}
-
-	attrs_msg = ldb_msg_new(module);
-	if (attrs_msg == NULL) {
-		goto failed;
-	}
-
-	dn = ldb_dn_new(module, ldb, LTDB_ATTRIBUTES);
-	if (dn == NULL) goto failed;
-
-	r = ldb_kv_search_dn1(module,
-			      dn,
-			      attrs_msg,
-			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
-				  LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
-				  LDB_UNPACK_DATA_FLAG_NO_DN);
-	talloc_free(dn);
-	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
-		goto failed;
-	}
-	if (r == LDB_ERR_NO_SUCH_OBJECT || attrs_msg->num_elements == 0) {
-		TALLOC_FREE(attrs_msg);
-		return 0;
-	}
-
-	attrs = talloc_array(attrs_msg,
-			     struct ldb_schema_attribute,
-			     attrs_msg->num_elements
-			     + ldb->schema.num_attributes);
-	if (attrs == NULL) {
-		goto failed;
-	}
-
-	memcpy(attrs,
-	       ldb->schema.attributes,
-	       sizeof(ldb->schema.attributes[0]) * ldb->schema.num_attributes);
-
-	/* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
-	   but its close enough for now */
-	for (i=0;i<attrs_msg->num_elements;i++) {
-		unsigned flags = 0, attr_flags = 0;
-		const char *syntax;
-		const struct ldb_schema_syntax *s;
-		const struct ldb_schema_attribute *a =
-			ldb_schema_attribute_by_name(ldb,
-						     attrs_msg->elements[i].name);
-		if (a != NULL && a->flags & LDB_ATTR_FLAG_FIXED) {
-			/* Must already be set in the array, and kept */
-			continue;
-		}
-
-		if (ldb_kv_attributes_flags(&attrs_msg->elements[i], &flags) !=
-		    0) {
-			ldb_debug(ldb, LDB_DEBUG_ERROR,
-				  "Invalid @ATTRIBUTES element for '%s'",
-				  attrs_msg->elements[i].name);
-			goto failed;
-		}
-
-		if (flags & LTDB_FLAG_UNIQUE_INDEX) {
-			attr_flags = LDB_ATTR_FLAG_UNIQUE_INDEX;
-		}
-		flags &= ~LTDB_FLAG_UNIQUE_INDEX;
-
-		/* These are not currently flags, each is exclusive */
-		if (flags == LTDB_FLAG_CASE_INSENSITIVE) {
-			syntax = LDB_SYNTAX_DIRECTORY_STRING;
-		} else if (flags == LTDB_FLAG_INTEGER) {
-			syntax = LDB_SYNTAX_INTEGER;
-		} else if (flags == 0) {
-			syntax = LDB_SYNTAX_OCTET_STRING;
-		} else {
-			ldb_debug(ldb, LDB_DEBUG_ERROR, 
-				  "Invalid flag combination 0x%x for '%s' "
-				  "in @ATTRIBUTES",
-				  flags, attrs_msg->elements[i].name);
-			goto failed;
-		}
-
-		s = ldb_standard_syntax_by_name(ldb, syntax);
-		if (s == NULL) {
-			ldb_debug(ldb, LDB_DEBUG_ERROR, 
-				  "Invalid attribute syntax '%s' for '%s' "
-				  "in @ATTRIBUTES",
-				  syntax, attrs_msg->elements[i].name);
-			goto failed;
-		}
-
-		attr_flags |= LDB_ATTR_FLAG_ALLOCATED | LDB_ATTR_FLAG_FROM_DB;
-
-		r = ldb_schema_attribute_fill_with_syntax(ldb,
-							  attrs,
-							  attrs_msg->elements[i].name,
-							  attr_flags, s,
-							  &attrs[num_loaded_attrs + ldb->schema.num_attributes]);
-		if (r != 0) {
-			goto failed;
-		}
-		num_loaded_attrs++;
-	}
-
-	attrs = talloc_realloc(attrs_msg,
-			       attrs, struct ldb_schema_attribute,
-			       num_loaded_attrs + ldb->schema.num_attributes);
-	if (attrs == NULL) {
-		goto failed;
-	}
-	TYPESAFE_QSORT(attrs, num_loaded_attrs + ldb->schema.num_attributes,
-		       ldb_schema_attribute_compare);
-	talloc_unlink(ldb, ldb->schema.attributes);
-	ldb->schema.attributes = talloc_steal(ldb, attrs);
-	ldb->schema.num_attributes = num_loaded_attrs + ldb->schema.num_attributes;
-	TALLOC_FREE(attrs_msg);
-
-	return 0;
-failed:
-	TALLOC_FREE(attrs_msg);
-	return -1;
-}
-
-/*
-  register any index records we find for the DB
-*/
-static int ldb_kv_index_load(struct ldb_module *module,
-			     struct ldb_kv_private *ldb_kv)
-{
-	struct ldb_context *ldb = ldb_module_get_ctx(module);
-	struct ldb_dn *indexlist_dn;
-	int r, lmdb_subdb_version;
-
-	if (ldb->schema.index_handler_override) {
-		/*
-		 * we skip loading the @INDEXLIST record when a module is
-		 * supplying its own attribute handling
-		 */
-		ldb_kv->cache->attribute_indexes = true;
-		ldb_kv->cache->one_level_indexes =
-		    ldb->schema.one_level_indexes;
-		ldb_kv->cache->GUID_index_attribute =
-		    ldb->schema.GUID_index_attribute;
-		ldb_kv->cache->GUID_index_dn_component =
-		    ldb->schema.GUID_index_dn_component;
-		return 0;
-	}
-
-	talloc_free(ldb_kv->cache->indexlist);
-
-	ldb_kv->cache->indexlist = ldb_msg_new(ldb_kv->cache);
-	if (ldb_kv->cache->indexlist == NULL) {
-		return -1;
-	}
-	ldb_kv->cache->one_level_indexes = false;
-	ldb_kv->cache->attribute_indexes = false;
-
-	indexlist_dn = ldb_dn_new(ldb_kv, ldb, LTDB_INDEXLIST);
-	if (indexlist_dn == NULL) {
-		return -1;
-	}
-
-	r = ldb_kv_search_dn1(module,
-			      indexlist_dn,
-			      ldb_kv->cache->indexlist,
-			      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
-				  LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC |
-				  LDB_UNPACK_DATA_FLAG_NO_DN);
-	TALLOC_FREE(indexlist_dn);
-
-	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
-		return -1;
-	}
-
-	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXONE) !=
-	    NULL) {
-		ldb_kv->cache->one_level_indexes = true;
-	}
-	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR) !=
-	    NULL) {
-		ldb_kv->cache->attribute_indexes = true;
-	}
-	ldb_kv->cache->GUID_index_attribute = ldb_msg_find_attr_as_string(
-	    ldb_kv->cache->indexlist, LTDB_IDXGUID, NULL);
-	ldb_kv->cache->GUID_index_dn_component = ldb_msg_find_attr_as_string(
-	    ldb_kv->cache->indexlist, LTDB_IDX_DN_GUID, NULL);
-
-	lmdb_subdb_version = ldb_msg_find_attr_as_int(
-	    ldb_kv->cache->indexlist, LTDB_IDX_LMDB_SUBDB, 0);
-
-	if (lmdb_subdb_version != 0) {
-		ldb_set_errstring(ldb,
-				  "FATAL: This ldb_mdb database has "
-				  "been written in a new verson of LDB "
-				  "using a sub-database index that "
-				  "is not understood by ldb "
-				  LDB_VERSION);
-		return -1;
-	}
-
-	return 0;
-}
-
-/*
-  initialise the baseinfo record
-*/
-static int ldb_kv_baseinfo_init(struct ldb_module *module)
-{
-	struct ldb_context *ldb;
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	struct ldb_message *msg;
-	struct ldb_message_element el;
-	struct ldb_val val;
-	int ret;
-	/* the initial sequence number must be different from the one
-	   set in ltdb_cache_free(). Thanks to Jon for pointing this
-	   out. */
-	const char *initial_sequence_number = "1";
-
-	ldb = ldb_module_get_ctx(module);
-
-	ldb_kv->sequence_number = atof(initial_sequence_number);
-
-	msg = ldb_msg_new(ldb_kv);
-	if (msg == NULL) {
-		goto failed;
-	}
-
-	msg->num_elements = 1;
-	msg->elements = ⪙
-	msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
-	if (!msg->dn) {
-		goto failed;
-	}
-	el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
-	if (!el.name) {
-		goto failed;
-	}
-	el.values = &val;
-	el.num_values = 1;
-	el.flags = 0;
-	val.data = (uint8_t *)talloc_strdup(msg, initial_sequence_number);
-	if (!val.data) {
-		goto failed;
-	}
-	val.length = 1;
-
-	ret = ldb_kv_store(module, msg, TDB_INSERT);
-
-	talloc_free(msg);
-
-	return ret;
-
-failed:
-	talloc_free(msg);
-	errno = ENOMEM;
-	return LDB_ERR_OPERATIONS_ERROR;
-}
-
-/*
-  free any cache records
- */
-static void ldb_kv_cache_free(struct ldb_module *module)
-{
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-
-	ldb_kv->sequence_number = 0;
-	talloc_free(ldb_kv->cache);
-	ldb_kv->cache = NULL;
-}
-
-/*
-  force a cache reload
-*/
-int ldb_kv_cache_reload(struct ldb_module *module)
-{
-	ldb_kv_attributes_unload(module);
-	ldb_kv_cache_free(module);
-	return ldb_kv_cache_load(module);
-}
-
-/*
-  load the cache records
-*/
-int ldb_kv_cache_load(struct ldb_module *module)
-{
-	struct ldb_context *ldb;
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	struct ldb_dn *baseinfo_dn = NULL, *options_dn = NULL;
-	uint64_t seq;
-	struct ldb_message *baseinfo = NULL, *options = NULL;
-	const struct ldb_schema_attribute *a;
-	bool have_write_txn = false;
-	int r;
-
-	ldb = ldb_module_get_ctx(module);
-
-	/* a very fast check to avoid extra database reads */
-	if (ldb_kv->cache != NULL && !ldb_kv->kv_ops->has_changed(ldb_kv)) {
-		return 0;
-	}
-
-	if (ldb_kv->cache == NULL) {
-		ldb_kv->cache = talloc_zero(ldb_kv, struct ldb_kv_cache);
-		if (ldb_kv->cache == NULL)
-			goto failed;
-	}
-
-	baseinfo = ldb_msg_new(ldb_kv->cache);
-	if (baseinfo == NULL) goto failed;
-
-	baseinfo_dn = ldb_dn_new(baseinfo, ldb, LTDB_BASEINFO);
-	if (baseinfo_dn == NULL) goto failed;
-
-	r = ldb_kv->kv_ops->lock_read(module);
-	if (r != LDB_SUCCESS) {
-		goto failed;
-	}
-	r = ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0);
-	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
-		goto failed_and_unlock;
-	}
-
-	/* possibly initialise the baseinfo */
-	if (r == LDB_ERR_NO_SUCH_OBJECT) {
-
-		/* Give up the read lock, try again with a write lock */
-		r = ldb_kv->kv_ops->unlock_read(module);
-		if (r != LDB_SUCCESS) {
-			goto failed;
-		}
-
-		if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
-			goto failed;
-		}
-
-		have_write_txn = true;
-
-		/* error handling for ltdb_baseinfo_init() is by
-		   looking for the record again. */
-		ldb_kv_baseinfo_init(module);
-
-		if (ldb_kv_search_dn1(module, baseinfo_dn, baseinfo, 0) !=
-		    LDB_SUCCESS) {
-			goto failed_and_unlock;
-		}
-
-	}
-
-	/* Ignore the result, and update the sequence number */
-	ldb_kv->kv_ops->has_changed(ldb_kv);
-
-	/* if the current internal sequence number is the same as the one
-	   in the database then assume the rest of the cache is OK */
-	seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
-	if (seq == ldb_kv->sequence_number) {
-		goto done;
-	}
-	ldb_kv->sequence_number = seq;
-
-	/* Read an interpret database options */
-
-	options = ldb_msg_new(ldb_kv->cache);
-	if (options == NULL) goto failed_and_unlock;
-
-	options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
-	if (options_dn == NULL) goto failed_and_unlock;
-
-	r = ldb_kv_search_dn1(module, options_dn, options, 0);
-	talloc_free(options_dn);
-	if (r != LDB_SUCCESS && r != LDB_ERR_NO_SUCH_OBJECT) {
-		goto failed_and_unlock;
-	}
-	
-	/* set flags if they do exist */
-	if (r == LDB_SUCCESS) {
-		ldb_kv->check_base =
-		    ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
-		ldb_kv->disallow_dn_filter = ldb_msg_find_attr_as_bool(
-		    options, LTDB_DISALLOW_DN_FILTER, false);
-	} else {
-		ldb_kv->check_base = false;
-		ldb_kv->disallow_dn_filter = false;
-	}
-
-	/*
-	 * ltdb_attributes_unload() calls internally talloc_free() on
-	 * any non-fixed elemnts in ldb->schema.attributes.
-	 *
-	 * NOTE WELL: This is per-ldb, not per module, so overwrites
-	 * the handlers across all databases when used under Samba's
-	 * partition module.
-	 */
-	ldb_kv_attributes_unload(module);
-
-	if (ldb_kv_index_load(module, ldb_kv) == -1) {
-		goto failed_and_unlock;
-	}
-
-	/*
-	 * NOTE WELL: This is per-ldb, not per module, so overwrites
-	 * the handlers across all databases when used under Samba's
-	 * partition module.
-	 */
-	if (ldb_kv_attributes_load(module) == -1) {
-		goto failed_and_unlock;
-	}
-
-	ldb_kv->GUID_index_syntax = NULL;
-	if (ldb_kv->cache->GUID_index_attribute != NULL) {
-		/*
-		 * Now the attributes are loaded, set the guid_index_syntax.
-		 * This can't fail, it will return a default at worst
-		 */
-		a = ldb_schema_attribute_by_name(
-		    ldb, ldb_kv->cache->GUID_index_attribute);
-		ldb_kv->GUID_index_syntax = a->syntax;
-	}
-
-done:
-	if (have_write_txn) {
-		if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
-			goto failed;
-		}
-	} else {
-		ldb_kv->kv_ops->unlock_read(module);
-	}
-
-	talloc_free(options);
-	talloc_free(baseinfo);
-	return 0;
-
-failed_and_unlock:
-	if (have_write_txn) {
-		ldb_kv->kv_ops->abort_write(ldb_kv);
-	} else {
-		ldb_kv->kv_ops->unlock_read(module);
-	}
-
-failed:
-	talloc_free(options);
-	talloc_free(baseinfo);
-	return -1;
-}
-
-
-/*
-  increase the sequence number to indicate a database change
-*/
-int ldb_kv_increase_sequence_number(struct ldb_module *module)
-{
-	struct ldb_context *ldb;
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	struct ldb_message *msg;
-	struct ldb_message_element el[2];
-	struct ldb_val val;
-	struct ldb_val val_time;
-	time_t t = time(NULL);
-	char *s = NULL;
-	int ret;
-
-	ldb = ldb_module_get_ctx(module);
-
-	msg = ldb_msg_new(ldb_kv);
-	if (msg == NULL) {
-		errno = ENOMEM;
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	s = talloc_asprintf(msg, "%llu", ldb_kv->sequence_number + 1);
-	if (!s) {
-		talloc_free(msg);
-		errno = ENOMEM;
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	msg->num_elements = ARRAY_SIZE(el);
-	msg->elements = el;
-	msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
-	if (msg->dn == NULL) {
-		talloc_free(msg);
-		errno = ENOMEM;
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
-	if (el[0].name == NULL) {
-		talloc_free(msg);
-		errno = ENOMEM;
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	el[0].values = &val;
-	el[0].num_values = 1;
-	el[0].flags = LDB_FLAG_MOD_REPLACE;
-	val.data = (uint8_t *)s;
-	val.length = strlen(s);
-
-	el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
-	if (el[1].name == NULL) {
-		talloc_free(msg);
-		errno = ENOMEM;
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	el[1].values = &val_time;
-	el[1].num_values = 1;
-	el[1].flags = LDB_FLAG_MOD_REPLACE;
-
-	s = ldb_timestring(msg, t);
-	if (s == NULL) {
-		talloc_free(msg);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	val_time.data = (uint8_t *)s;
-	val_time.length = strlen(s);
-
-	ret = ldb_kv_modify_internal(module, msg, NULL);
-
-	talloc_free(msg);
-
-	if (ret == LDB_SUCCESS) {
-		ldb_kv->sequence_number += 1;
-	}
-
-	/* updating the tdb_seqnum here avoids us reloading the cache
-	   records due to our own modification */
-	ldb_kv->kv_ops->has_changed(ldb_kv);
-
-	return ret;
-}
-
-int ldb_kv_check_at_attributes_values(const struct ldb_val *value)
-{
-	unsigned int i;
-
-	for (i = 0; ldb_kv_valid_attr_flags[i].name != NULL; i++) {
-		if ((strcmp(ldb_kv_valid_attr_flags[i].name,
-			    (char *)value->data) == 0)) {
-			return 0;
-		}
-	}
-
-	return -1;
-}
-
diff --git a/lib/ldb/ldb_tdb/ldb_index.c b/lib/ldb/ldb_tdb/ldb_index.c
deleted file mode 100644
index ce107da..0000000
--- a/lib/ldb/ldb_tdb/ldb_index.c
+++ /dev/null
@@ -1,3122 +0,0 @@
-/*
-   ldb database library
-
-   Copyright (C) Andrew Tridgell  2004-2009
-
-     ** NOTE! The following LGPL license applies to the ldb
-     ** library. This does NOT imply that all of Samba is released
-     ** under the LGPL
-
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 3 of the License, or (at your option) any later version.
-
-   This library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, see <http://www.gnu.org/licenses/>.
-*/
-
-/*
- *  Name: ldb
- *
- *  Component: ldb tdb backend - indexing
- *
- *  Description: indexing routines for ldb tdb backend
- *
- *  Author: Andrew Tridgell
- */
-
-/*
-
-LDB Index design and choice of TDB key:
-=======================================
-
-LDB has index records held as LDB objects with a special record like:
-
-dn: @INDEX:attr:value
-
-value may be base64 encoded, if it is deemed not printable:
-
-dn: @INDEX:attr::base64-value
-
-In each record, there is two possible formats:
-
-The original format is:
------------------------
-
-dn: @INDEX:NAME:DNSUPDATEPROXY
- at IDXVERSION: 2
- at IDX: CN=DnsUpdateProxy,CN=Users,DC=addom,DC=samba,DC=example,DC=com
-
-In this format, @IDX is multi-valued, one entry for each match
-
-The corrosponding entry is stored in a TDB record with key:
-
-DN=CN=DNSUPDATEPROXY,CN=USERS,DC=ADDOM,DC=SAMBA,DC=EXAMPLE,DC=COM
-
-(This allows a scope BASE search to directly find the record via
-a simple casefold of the DN).
-
-The original mixed-case DN is stored in the entry iself.
-
-
-The new 'GUID index' format is:
--------------------------------
-
-dn: @INDEX:NAME:DNSUPDATEPROXY
- at IDXVERSION: 3
- at IDX: <binary GUID>[<binary GUID>[...]]
-
-The binary guid is 16 bytes, as bytes and not expanded as hexidecimal
-or pretty-printed.  The GUID is chosen from the message to be stored
-by the @IDXGUID attribute on @INDEXLIST.
-
-If there are multiple values the @IDX value simply becomes longer,
-in multiples of 16.
-
-The corrosponding entry is stored in a TDB record with key:
-
-GUID=<binary GUID>
-
-This allows a very quick translation between the fixed-length index
-values and the TDB key, while seperating entries from other data
-in the TDB, should they be unlucky enough to start with the bytes of
-the 'DN=' prefix.
-
-Additionally, this allows a scope BASE search to directly find the
-record via a simple match on a GUID= extended DN, controlled via
- at IDX_DN_GUID on @INDEXLIST
-
-Exception for special @ DNs:
-
- at BASEINFO, @INDEXLIST and all other special DNs are stored as per the
-original format, as they are never referenced in an index and are used
-to bootstrap the database.
-
-
-Control points for choice of index mode
----------------------------------------
-
-The choice of index and TDB key mode is made based (for example, from
-Samba) on entries in the @INDEXLIST DN:
-
-dn: @INDEXLIST
- at IDXGUID: objectGUID
- at IDX_DN_GUID: GUID
-
-By default, the original DN format is used.
-
-
-Control points for choosing indexed attributes
-----------------------------------------------
-
- at IDXATTR controls if an attribute is indexed
-
-dn: @INDEXLIST
- at IDXATTR: samAccountName
- at IDXATTR: nETBIOSName
-
-
-C Override functions
---------------------
-
-void ldb_schema_set_override_GUID_index(struct ldb_context *ldb,
-                                        const char *GUID_index_attribute,
-                                        const char *GUID_index_dn_component)
-
-This is used, particularly in combination with the below, instead of
-the @IDXGUID and @IDX_DN_GUID values in @INDEXLIST.
-
-void ldb_schema_set_override_indexlist(struct ldb_context *ldb,
-                                       bool one_level_indexes);
-void ldb_schema_attribute_set_override_handler(struct ldb_context *ldb,
-                                               ldb_attribute_handler_override_fn_t override,
-                                               void *private_data);
-
-When the above two functions are called in combination, the @INDEXLIST
-values are not read from the DB, so
-ldb_schema_set_override_GUID_index() must be called.
-
-*/
-
-#include "ldb_tdb.h"
-#include "ldb_private.h"
-#include "lib/util/binsearch.h"
-
-struct dn_list {
-	unsigned int count;
-	struct ldb_val *dn;
-	/*
-	 * Do not optimise the intersection of this list,
-	 * we must never return an entry not in this
-	 * list.  This allows the index for
-	 * SCOPE_ONELEVEL to be trusted.
-	 */
-	bool strict;
-};
-
-struct ldb_kv_idxptr {
-	struct tdb_context *itdb;
-	int error;
-};
-
-enum key_truncation {
-	KEY_NOT_TRUNCATED,
-	KEY_TRUNCATED,
-};
-
-static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
-				      const struct ldb_message *msg,
-				      int add);
-static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
-				   struct ldb_kv_private *ldb_kv,
-				   struct ldb_dn *base_dn,
-				   struct dn_list *dn_list,
-				   enum key_truncation *truncation);
-
-static void ldb_kv_dn_list_sort(struct ldb_kv_private *ldb_kv,
-				struct dn_list *list);
-
-/* we put a @IDXVERSION attribute on index entries. This
-   allows us to tell if it was written by an older version
-*/
-#define LTDB_INDEXING_VERSION 2
-
-#define LTDB_GUID_INDEXING_VERSION 3
-
-static unsigned ldb_kv_max_key_length(struct ldb_kv_private *ldb_kv)
-{
-	if (ldb_kv->max_key_length == 0) {
-		return UINT_MAX;
-	}
-	return ldb_kv->max_key_length;
-}
-
-/* enable the idxptr mode when transactions start */
-int ldb_kv_index_transaction_start(struct ldb_module *module)
-{
-	struct ldb_kv_private *ldb_kv = talloc_get_type(
-	    ldb_module_get_private(module), struct ldb_kv_private);
-	ldb_kv->idxptr = talloc_zero(ldb_kv, struct ldb_kv_idxptr);
-	if (ldb_kv->idxptr == NULL) {
-		return ldb_oom(ldb_module_get_ctx(module));
-	}
-
-	return LDB_SUCCESS;
-}
-
-/*
-  see if two ldb_val structures contain exactly the same data
-  return -1 or 1 for a mismatch, 0 for match
-*/
-static int ldb_val_equal_exact_for_qsort(const struct ldb_val *v1,
-					 const struct ldb_val *v2)
-{
-	if (v1->length > v2->length) {
-		return -1;
-	}
-	if (v1->length < v2->length) {
-		return 1;
-	}
-	return memcmp(v1->data, v2->data, v1->length);
-}
-
-/*
-  see if two ldb_val structures contain exactly the same data
-  return -1 or 1 for a mismatch, 0 for match
-*/
-static int ldb_val_equal_exact_ordered(const struct ldb_val v1,
-				       const struct ldb_val *v2)
-{
-	if (v1.length > v2->length) {
-		return -1;
-	}
-	if (v1.length < v2->length) {
-		return 1;
-	}
-	return memcmp(v1.data, v2->data, v1.length);
-}
-
-
-/*
-  find a entry in a dn_list, using a ldb_val. Uses a case sensitive
-  binary-safe comparison for the 'dn' returns -1 if not found
-
-  This is therefore safe when the value is a GUID in the future
- */
-static int ldb_kv_dn_list_find_val(struct ldb_kv_private *ldb_kv,
-				   const struct dn_list *list,
-				   const struct ldb_val *v)
-{
-	unsigned int i;
-	struct ldb_val *exact = NULL, *next = NULL;
-
-	if (ldb_kv->cache->GUID_index_attribute == NULL) {
-		for (i=0; i<list->count; i++) {
-			if (ldb_val_equal_exact(&list->dn[i], v) == 1) {
-				return i;
-			}
-		}
-		return -1;
-	}
-
-	BINARY_ARRAY_SEARCH_GTE(list->dn, list->count,
-				*v, ldb_val_equal_exact_ordered,
-				exact, next);
-	if (exact == NULL) {
-		return -1;
-	}
-	/* Not required, but keeps the compiler quiet */
-	if (next != NULL) {
-		return -1;
-	}
-
-	i = exact - list->dn;
-	return i;
-}
-
-/*
-  find a entry in a dn_list. Uses a case sensitive comparison with the dn
-  returns -1 if not found
- */
-static int ldb_kv_dn_list_find_msg(struct ldb_kv_private *ldb_kv,
-				   struct dn_list *list,
-				   const struct ldb_message *msg)
-{
-	struct ldb_val v;
-	const struct ldb_val *key_val;
-	if (ldb_kv->cache->GUID_index_attribute == NULL) {
-		const char *dn_str = ldb_dn_get_linearized(msg->dn);
-		v.data = discard_const_p(unsigned char, dn_str);
-		v.length = strlen(dn_str);
-	} else {
-		key_val = ldb_msg_find_ldb_val(
-		    msg, ldb_kv->cache->GUID_index_attribute);
-		if (key_val == NULL) {
-			return -1;
-		}
-		v = *key_val;
-	}
-	return ldb_kv_dn_list_find_val(ldb_kv, list, &v);
-}
-
-/*
-  this is effectively a cast function, but with lots of paranoia
-  checks and also copes with CPUs that are fussy about pointer
-  alignment
- */
-static struct dn_list *ldb_kv_index_idxptr(struct ldb_module *module,
-					   TDB_DATA rec,
-					   bool check_parent)
-{
-	struct dn_list *list;
-	if (rec.dsize != sizeof(void *)) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       "Bad data size for idxptr %u", (unsigned)rec.dsize);
-		return NULL;
-	}
-	/* note that we can't just use a cast here, as rec.dptr may
-	   not be aligned sufficiently for a pointer. A cast would cause
-	   platforms like some ARM CPUs to crash */
-	memcpy(&list, rec.dptr, sizeof(void *));
-	list = talloc_get_type(list, struct dn_list);
-	if (list == NULL) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       "Bad type '%s' for idxptr",
-				       talloc_get_name(list));
-		return NULL;
-	}
-	if (check_parent && list->dn && talloc_parent(list->dn) != list) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       "Bad parent '%s' for idxptr",
-				       talloc_get_name(talloc_parent(list->dn)));
-		return NULL;
-	}
-	return list;
-}
-
-/*
-  return the @IDX list in an index entry for a dn as a
-  struct dn_list
- */
-static int ldb_kv_dn_list_load(struct ldb_module *module,
-			       struct ldb_kv_private *ldb_kv,
-			       struct ldb_dn *dn,
-			       struct dn_list *list)
-{
-	struct ldb_message *msg;
-	int ret, version;
-	struct ldb_message_element *el;
-	TDB_DATA rec;
-	struct dn_list *list2;
-	TDB_DATA key;
-
-	list->dn = NULL;
-	list->count = 0;
-
-	/* see if we have any in-memory index entries */
-	if (ldb_kv->idxptr == NULL || ldb_kv->idxptr->itdb == NULL) {
-		goto normal_index;
-	}
-
-	key.dptr = discard_const_p(unsigned char, ldb_dn_get_linearized(dn));
-	key.dsize = strlen((char *)key.dptr);
-
-	rec = tdb_fetch(ldb_kv->idxptr->itdb, key);
-	if (rec.dptr == NULL) {
-		goto normal_index;
-	}
-
-	/* we've found an in-memory index entry */
-	list2 = ldb_kv_index_idxptr(module, rec, true);
-	if (list2 == NULL) {
-		free(rec.dptr);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	free(rec.dptr);
-
-	*list = *list2;
-	return LDB_SUCCESS;
-
-normal_index:
-	msg = ldb_msg_new(list);
-	if (msg == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ret = ldb_kv_search_dn1(module,
-				dn,
-				msg,
-				LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
-				    LDB_UNPACK_DATA_FLAG_NO_DN);
-	if (ret != LDB_SUCCESS) {
-		talloc_free(msg);
-		return ret;
-	}
-
-	el = ldb_msg_find_element(msg, LTDB_IDX);
-	if (!el) {
-		talloc_free(msg);
-		return LDB_SUCCESS;
-	}
-
-	version = ldb_msg_find_attr_as_int(msg, LTDB_IDXVERSION, 0);
-
-	/*
-	 * we avoid copying the strings by stealing the list.  We have
-	 * to steal msg onto el->values (which looks odd) because we
-	 * asked for the memory to be allocated on msg, not on each
-	 * value with LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC above
-	 */
-	if (ldb_kv->cache->GUID_index_attribute == NULL) {
-		/* check indexing version number */
-		if (version != LTDB_INDEXING_VERSION) {
-			ldb_debug_set(ldb_module_get_ctx(module),
-				      LDB_DEBUG_ERROR,
-				      "Wrong DN index version %d "
-				      "expected %d for %s",
-				      version, LTDB_INDEXING_VERSION,
-				      ldb_dn_get_linearized(dn));
-			talloc_free(msg);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-
-		talloc_steal(el->values, msg);
-		list->dn = talloc_steal(list, el->values);
-		list->count = el->num_values;
-	} else {
-		unsigned int i;
-		if (version != LTDB_GUID_INDEXING_VERSION) {
-			/* This is quite likely during the DB startup
-			   on first upgrade to using a GUID index */
-			ldb_debug_set(ldb_module_get_ctx(module),
-				      LDB_DEBUG_ERROR,
-				      "Wrong GUID index version %d "
-				      "expected %d for %s",
-				      version, LTDB_GUID_INDEXING_VERSION,
-				      ldb_dn_get_linearized(dn));
-			talloc_free(msg);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-
-		if (el->num_values == 0) {
-			talloc_free(msg);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-
-		if ((el->values[0].length % LTDB_GUID_SIZE) != 0) {
-			talloc_free(msg);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-
-		list->count = el->values[0].length / LTDB_GUID_SIZE;
-		list->dn = talloc_array(list, struct ldb_val, list->count);
-		if (list->dn == NULL) {
-			talloc_free(msg);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-
-		/*
-		 * The actual data is on msg, due to
-		 * LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
-		 */
-		talloc_steal(list->dn, msg);
-		for (i = 0; i < list->count; i++) {
-			list->dn[i].data
-				= &el->values[0].data[i * LTDB_GUID_SIZE];
-			list->dn[i].length = LTDB_GUID_SIZE;
-		}
-	}
-
-	/* We don't need msg->elements any more */
-	talloc_free(msg->elements);
-	return LDB_SUCCESS;
-}
-
-int ldb_kv_key_dn_from_idx(struct ldb_module *module,
-			   struct ldb_kv_private *ldb_kv,
-			   TALLOC_CTX *mem_ctx,
-			   struct ldb_dn *dn,
-			   TDB_DATA *tdb_key)
-{
-	struct ldb_context *ldb = ldb_module_get_ctx(module);
-	int ret;
-	int index = 0;
-	enum key_truncation truncation = KEY_NOT_TRUNCATED;
-	struct dn_list *list = talloc(mem_ctx, struct dn_list);
-	if (list == NULL) {
-		ldb_oom(ldb);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ret = ldb_kv_index_dn_base_dn(module, ldb_kv, dn, list, &truncation);
-	if (ret != LDB_SUCCESS) {
-		TALLOC_FREE(list);
-		return ret;
-	}
-
-	if (list->count == 0) {
-		TALLOC_FREE(list);
-		return LDB_ERR_NO_SUCH_OBJECT;
-	}
-
-	if (list->count > 1 && truncation == KEY_NOT_TRUNCATED)  {
-		const char *dn_str = ldb_dn_get_linearized(dn);
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       __location__
-				       ": Failed to read DN index "
-				       "against %s for %s: too many "
-				       "values (%u > 1)",
-				       ldb_kv->cache->GUID_index_attribute,
-				       dn_str,
-				       list->count);
-		TALLOC_FREE(list);
-		return LDB_ERR_CONSTRAINT_VIOLATION;
-	}
-
-	if (list->count > 0 && truncation == KEY_TRUNCATED)  {
-		/*
-		 * DN key has been truncated, need to inspect the actual
-		 * records to locate the actual DN
-		 */
-		int i;
-		index = -1;
-		for (i=0; i < list->count; i++) {
-			uint8_t guid_key[LTDB_GUID_KEY_SIZE];
-			TDB_DATA key = {
-				.dptr = guid_key,
-				.dsize = sizeof(guid_key)
-			};
-			const int flags = LDB_UNPACK_DATA_FLAG_NO_ATTRS;
-			struct ldb_message *rec = ldb_msg_new(ldb);
-			if (rec == NULL) {
-				TALLOC_FREE(list);
-				return LDB_ERR_OPERATIONS_ERROR;
-			}
-
-			ret = ldb_kv_idx_to_key(
-			    module, ldb_kv, ldb, &list->dn[i], &key);
-			if (ret != LDB_SUCCESS) {
-				TALLOC_FREE(list);
-				TALLOC_FREE(rec);
-				return ret;
-			}
-
-			ret =
-			    ldb_kv_search_key(module, ldb_kv, key, rec, flags);
-			if (key.dptr != guid_key) {
-				TALLOC_FREE(key.dptr);
-			}
-			if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-				/*
-				 * the record has disappeared?
-				 * yes, this can happen
-				 */
-				TALLOC_FREE(rec);
-				continue;
-			}
-
-			if (ret != LDB_SUCCESS) {
-				/* an internal error */
-				TALLOC_FREE(rec);
-				TALLOC_FREE(list);
-				return LDB_ERR_OPERATIONS_ERROR;
-			}
-
-			/*
-			 * We found the actual DN that we wanted from in the
-			 * multiple values that matched the index
-			 * (due to truncation), so return that.
-			 *
-			 */
-			if (ldb_dn_compare(dn, rec->dn) == 0) {
-				index = i;
-				TALLOC_FREE(rec);
-				break;
-			}
-		}
-
-		/*
-		 * We matched the index but the actual DN we wanted
-		 * was not here.
-		 */
-		if (index == -1) {
-			TALLOC_FREE(list);
-			return LDB_ERR_NO_SUCH_OBJECT;
-		}
-	}
-
-	/* The tdb_key memory is allocated by the caller */
-	ret = ldb_kv_guid_to_key(module, ldb_kv, &list->dn[index], tdb_key);
-	TALLOC_FREE(list);
-
-	if (ret != LDB_SUCCESS) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	return LDB_SUCCESS;
-}
-
-
-
-/*
-  save a dn_list into a full @IDX style record
- */
-static int ldb_kv_dn_list_store_full(struct ldb_module *module,
-				     struct ldb_kv_private *ldb_kv,
-				     struct ldb_dn *dn,
-				     struct dn_list *list)
-{
-	struct ldb_message *msg;
-	int ret;
-
-	msg = ldb_msg_new(module);
-	if (!msg) {
-		return ldb_module_oom(module);
-	}
-
-	msg->dn = dn;
-
-	if (list->count == 0) {
-		ret = ldb_kv_delete_noindex(module, msg);
-		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-			ret = LDB_SUCCESS;
-		}
-		talloc_free(msg);
-		return ret;
-	}
-
-	if (ldb_kv->cache->GUID_index_attribute == NULL) {
-		ret = ldb_msg_add_fmt(msg, LTDB_IDXVERSION, "%u",
-				      LTDB_INDEXING_VERSION);
-		if (ret != LDB_SUCCESS) {
-			talloc_free(msg);
-			return ldb_module_oom(module);
-		}
-	} else {
-		ret = ldb_msg_add_fmt(msg, LTDB_IDXVERSION, "%u",
-				      LTDB_GUID_INDEXING_VERSION);
-		if (ret != LDB_SUCCESS) {
-			talloc_free(msg);
-			return ldb_module_oom(module);
-		}
-	}
-
-	if (list->count > 0) {
-		struct ldb_message_element *el;
-
-		ret = ldb_msg_add_empty(msg, LTDB_IDX, LDB_FLAG_MOD_ADD, &el);
-		if (ret != LDB_SUCCESS) {
-			talloc_free(msg);
-			return ldb_module_oom(module);
-		}
-
-		if (ldb_kv->cache->GUID_index_attribute == NULL) {
-			el->values = list->dn;
-			el->num_values = list->count;
-		} else {
-			struct ldb_val v;
-			unsigned int i;
-			el->values = talloc_array(msg,
-						  struct ldb_val, 1);
-			if (el->values == NULL) {
-				talloc_free(msg);
-				return ldb_module_oom(module);
-			}
-
-			v.data = talloc_array_size(el->values,
-						   list->count,
-						   LTDB_GUID_SIZE);
-			if (v.data == NULL) {
-				talloc_free(msg);
-				return ldb_module_oom(module);
-			}
-
-			v.length = talloc_get_size(v.data);
-
-			for (i = 0; i < list->count; i++) {
-				if (list->dn[i].length !=
-				    LTDB_GUID_SIZE) {
-					talloc_free(msg);
-					return ldb_module_operr(module);
-				}
-				memcpy(&v.data[LTDB_GUID_SIZE*i],
-				       list->dn[i].data,
-				       LTDB_GUID_SIZE);
-			}
-			el->values[0] = v;
-			el->num_values = 1;
-		}
-	}
-
-	ret = ldb_kv_store(module, msg, TDB_REPLACE);
-	talloc_free(msg);
-	return ret;
-}
-
-/*
-  save a dn_list into the database, in either @IDX or internal format
- */
-static int ldb_kv_dn_list_store(struct ldb_module *module,
-				struct ldb_dn *dn,
-				struct dn_list *list)
-{
-	struct ldb_kv_private *ldb_kv = talloc_get_type(
-	    ldb_module_get_private(module), struct ldb_kv_private);
-	TDB_DATA rec, key;
-	int ret;
-	struct dn_list *list2;
-
-	if (ldb_kv->idxptr == NULL) {
-		return ldb_kv_dn_list_store_full(module, ldb_kv, dn, list);
-	}
-
-	if (ldb_kv->idxptr->itdb == NULL) {
-		ldb_kv->idxptr->itdb =
-		    tdb_open(NULL, 1000, TDB_INTERNAL, O_RDWR, 0);
-		if (ldb_kv->idxptr->itdb == NULL) {
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-	}
-
-	key.dptr = discard_const_p(unsigned char, ldb_dn_get_linearized(dn));
-	if (key.dptr == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	key.dsize = strlen((char *)key.dptr);
-
-	rec = tdb_fetch(ldb_kv->idxptr->itdb, key);
-	if (rec.dptr != NULL) {
-		list2 = ldb_kv_index_idxptr(module, rec, false);
-		if (list2 == NULL) {
-			free(rec.dptr);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-		free(rec.dptr);
-		list2->dn = talloc_steal(list2, list->dn);
-		list2->count = list->count;
-		return LDB_SUCCESS;
-	}
-
-	list2 = talloc(ldb_kv->idxptr, struct dn_list);
-	if (list2 == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	list2->dn = talloc_steal(list2, list->dn);
-	list2->count = list->count;
-
-	rec.dptr = (uint8_t *)&list2;
-	rec.dsize = sizeof(void *);
-
-
-	/*
-	 * This is not a store into the main DB, but into an in-memory
-	 * TDB, so we don't need a guard on ltdb->read_only
-	 */
-	ret = tdb_store(ldb_kv->idxptr->itdb, key, rec, TDB_INSERT);
-	if (ret != 0) {
-		return ltdb_err_map(tdb_error(ldb_kv->idxptr->itdb));
-	}
-	return LDB_SUCCESS;
-}
-
-/*
-  traverse function for storing the in-memory index entries on disk
- */
-static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
-				       TDB_DATA key,
-				       TDB_DATA data,
-				       void *state)
-{
-	struct ldb_module *module = state;
-	struct ldb_kv_private *ldb_kv = talloc_get_type(
-	    ldb_module_get_private(module), struct ldb_kv_private);
-	struct ldb_dn *dn;
-	struct ldb_context *ldb = ldb_module_get_ctx(module);
-	struct ldb_val v;
-	struct dn_list *list;
-
-	list = ldb_kv_index_idxptr(module, data, true);
-	if (list == NULL) {
-		ldb_kv->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
-		return -1;
-	}
-
-	v.data = key.dptr;
-	v.length = strnlen((char *)key.dptr, key.dsize);
-
-	dn = ldb_dn_from_ldb_val(module, ldb, &v);
-	if (dn == NULL) {
-		ldb_asprintf_errstring(ldb, "Failed to parse index key %*.*s as an LDB DN", (int)v.length, (int)v.length, (const char *)v.data);
-		ldb_kv->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
-		return -1;
-	}
-
-	ldb_kv->idxptr->error =
-	    ldb_kv_dn_list_store_full(module, ldb_kv, dn, list);
-	talloc_free(dn);
-	if (ldb_kv->idxptr->error != 0) {
-		return -1;
-	}
-	return 0;
-}
-
-/* cleanup the idxptr mode when transaction commits */
-int ldb_kv_index_transaction_commit(struct ldb_module *module)
-{
-	struct ldb_kv_private *ldb_kv = talloc_get_type(
-	    ldb_module_get_private(module), struct ldb_kv_private);
-	int ret;
-
-	struct ldb_context *ldb = ldb_module_get_ctx(module);
-
-	ldb_reset_err_string(ldb);
-
-	if (ldb_kv->idxptr->itdb) {
-		tdb_traverse(
-		    ldb_kv->idxptr->itdb, ldb_kv_index_traverse_store, module);
-		tdb_close(ldb_kv->idxptr->itdb);
-	}
-
-	ret = ldb_kv->idxptr->error;
-	if (ret != LDB_SUCCESS) {
-		if (!ldb_errstring(ldb)) {
-			ldb_set_errstring(ldb, ldb_strerror(ret));
-		}
-		ldb_asprintf_errstring(ldb, "Failed to store index records in transaction commit: %s", ldb_errstring(ldb));
-	}
-
-	talloc_free(ldb_kv->idxptr);
-	ldb_kv->idxptr = NULL;
-	return ret;
-}
-
-/* cleanup the idxptr mode when transaction cancels */
-int ldb_kv_index_transaction_cancel(struct ldb_module *module)
-{
-	struct ldb_kv_private *ldb_kv = talloc_get_type(
-	    ldb_module_get_private(module), struct ldb_kv_private);
-	if (ldb_kv->idxptr && ldb_kv->idxptr->itdb) {
-		tdb_close(ldb_kv->idxptr->itdb);
-	}
-	talloc_free(ldb_kv->idxptr);
-	ldb_kv->idxptr = NULL;
-	return LDB_SUCCESS;
-}
-
-
-/*
-  return the dn key to be used for an index
-  the caller is responsible for freeing
-*/
-static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
-				       struct ldb_kv_private *ldb_kv,
-				       const char *attr,
-				       const struct ldb_val *value,
-				       const struct ldb_schema_attribute **ap,
-				       enum key_truncation *truncation)
-{
-	struct ldb_dn *ret;
-	struct ldb_val v;
-	const struct ldb_schema_attribute *a = NULL;
-	char *attr_folded = NULL;
-	const char *attr_for_dn = NULL;
-	int r;
-	bool should_b64_encode;
-
-	unsigned int max_key_length = ldb_kv_max_key_length(ldb_kv);
-	size_t key_len = 0;
-	size_t attr_len = 0;
-	const size_t indx_len = sizeof(LTDB_INDEX) - 1;
-	unsigned frmt_len = 0;
-	const size_t additional_key_length = 4;
-	unsigned int num_separators = 3; /* Estimate for overflow check */
-	const size_t min_data = 1;
-	const size_t min_key_length = additional_key_length
-		+ indx_len + num_separators + min_data;
-
-	if (attr[0] == '@') {
-		attr_for_dn = attr;
-		v = *value;
-		if (ap != NULL) {
-			*ap = NULL;
-		}
-	} else {
-		attr_folded = ldb_attr_casefold(ldb, attr);
-		if (!attr_folded) {
-			return NULL;
-		}
-
-		attr_for_dn = attr_folded;
-
-		a = ldb_schema_attribute_by_name(ldb, attr);
-		if (ap) {
-			*ap = a;
-		}
-		r = a->syntax->canonicalise_fn(ldb, ldb, value, &v);
-		if (r != LDB_SUCCESS) {
-			const char *errstr = ldb_errstring(ldb);
-			/* canonicalisation can be refused. For
-			   example, a attribute that takes wildcards
-			   will refuse to canonicalise if the value
-			   contains a wildcard */
-			ldb_asprintf_errstring(ldb,
-					       "Failed to create index "
-					       "key for attribute '%s':%s%s%s",
-					       attr, ldb_strerror(r),
-					       (errstr?":":""),
-					       (errstr?errstr:""));
-			talloc_free(attr_folded);
-			return NULL;
-		}
-	}
-	attr_len = strlen(attr_for_dn);
-
-	/*
-	 * Check if there is any hope this will fit into the DB.
-	 * Overflow here is not actually critical the code below
-	 * checks again to make the printf and the DB does another
-	 * check for too long keys
-	 */
-	if (max_key_length - attr_len < min_key_length) {
-		ldb_asprintf_errstring(
-			ldb,
-			__location__ ": max_key_length "
-			"is too small (%u) < (%u)",
-			max_key_length,
-			(unsigned)(min_key_length + attr_len));
-		talloc_free(attr_folded);
-		return NULL;
-	}
-
-	/*
-	 * ltdb_key_dn() makes something 4 bytes longer, it adds a leading
-	 * "DN=" and a trailing string terminator
-	 */
-	max_key_length -= additional_key_length;
-
-	/*
-	 * We do not base 64 encode a DN in a key, it has already been
-	 * casefold and lineraized, that is good enough.  That already
-	 * avoids embedded NUL etc.
-	 */
-	if (ldb_kv->cache->GUID_index_attribute != NULL) {
-		if (strcmp(attr, LTDB_IDXDN) == 0) {
-			should_b64_encode = false;
-		} else if (strcmp(attr, LTDB_IDXONE) == 0) {
-			/*
-			 * We can only change the behaviour for IDXONE
-			 * when the GUID index is enabled
-			 */
-			should_b64_encode = false;
-		} else {
-			should_b64_encode
-				= ldb_should_b64_encode(ldb, &v);
-		}
-	} else {
-		should_b64_encode = ldb_should_b64_encode(ldb, &v);
-	}
-
-	if (should_b64_encode) {
-		size_t vstr_len = 0;
-		char *vstr = ldb_base64_encode(ldb, (char *)v.data, v.length);
-		if (!vstr) {
-			talloc_free(attr_folded);
-			return NULL;
-		}
-		vstr_len = strlen(vstr);
-		/* 
-		 * Overflow here is not critical as we only use this
-		 * to choose the printf truncation
-		 */
-		key_len = num_separators + indx_len + attr_len + vstr_len;
-		if (key_len > max_key_length) {
-			size_t excess = key_len - max_key_length;
-			frmt_len = vstr_len - excess;
-			*truncation = KEY_TRUNCATED;
-			/*
-			* Truncated keys are placed in a separate key space
-			* from the non truncated keys
-			* Note: the double hash "##" is not a typo and
-			* indicates that the following value is base64 encoded
-			*/
-			ret = ldb_dn_new_fmt(ldb, ldb, "%s#%s##%.*s",
-					     LTDB_INDEX, attr_for_dn,
-					     frmt_len, vstr);
-		} else {
-			frmt_len = vstr_len;
-			*truncation = KEY_NOT_TRUNCATED;
-			/*
-			 * Note: the double colon "::" is not a typo and
-			 * indicates that the following value is base64 encoded
-			 */
-			ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s::%.*s",
-					     LTDB_INDEX, attr_for_dn,
-					     frmt_len, vstr);
-		}
-		talloc_free(vstr);
-	} else {
-		/* Only need two seperators */
-		num_separators = 2;
-
-		/*
-		 * Overflow here is not critical as we only use this
-		 * to choose the printf truncation
-		 */
-		key_len = num_separators + indx_len + attr_len + (int)v.length;
-		if (key_len > max_key_length) {
-			size_t excess = key_len - max_key_length;
-			frmt_len = v.length - excess;
-			*truncation = KEY_TRUNCATED;
-			/*
-			 * Truncated keys are placed in a separate key space
-			 * from the non truncated keys
-			 */
-			ret = ldb_dn_new_fmt(ldb, ldb, "%s#%s#%.*s",
-					     LTDB_INDEX, attr_for_dn,
-					     frmt_len, (char *)v.data);
-		} else {
-			frmt_len = v.length;
-			*truncation = KEY_NOT_TRUNCATED;
-			ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s:%.*s",
-					     LTDB_INDEX, attr_for_dn,
-					     frmt_len, (char *)v.data);
-		}
-	}
-
-	if (v.data != value->data) {
-		talloc_free(v.data);
-	}
-	talloc_free(attr_folded);
-
-	return ret;
-}
-
-/*
-  see if a attribute value is in the list of indexed attributes
-*/
-static bool ldb_kv_is_indexed(struct ldb_module *module,
-			      struct ldb_kv_private *ldb_kv,
-			      const char *attr)
-{
-	struct ldb_context *ldb = ldb_module_get_ctx(module);
-	unsigned int i;
-	struct ldb_message_element *el;
-
-	if ((ldb_kv->cache->GUID_index_attribute != NULL) &&
-	    (ldb_attr_cmp(attr, ldb_kv->cache->GUID_index_attribute) == 0)) {
-		/* Implicity covered, this is the index key */
-		return false;
-	}
-	if (ldb->schema.index_handler_override) {
-		const struct ldb_schema_attribute *a
-			= ldb_schema_attribute_by_name(ldb, attr);
-
-		if (a == NULL) {
-			return false;
-		}
-
-		if (a->flags & LDB_ATTR_FLAG_INDEXED) {
-			return true;
-		} else {
-			return false;
-		}
-	}
-
-	if (!ldb_kv->cache->attribute_indexes) {
-		return false;
-	}
-
-	el = ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR);
-	if (el == NULL) {
-		return false;
-	}
-
-	/* TODO: this is too expensive! At least use a binary search */
-	for (i=0; i<el->num_values; i++) {
-		if (ldb_attr_cmp((char *)el->values[i].data, attr) == 0) {
-			return true;
-		}
-	}
-	return false;
-}
-
-/*
-  in the following logic functions, the return value is treated as
-  follows:
-
-     LDB_SUCCESS: we found some matching index values
-
-     LDB_ERR_NO_SUCH_OBJECT: we know for sure that no object matches
-
-     LDB_ERR_OPERATIONS_ERROR: indexing could not answer the call,
-                               we'll need a full search
- */
-
-/*
-  return a list of dn's that might match a simple indexed search (an
-  equality search only)
- */
-static int ldb_kv_index_dn_simple(struct ldb_module *module,
-				  struct ldb_kv_private *ldb_kv,
-				  const struct ldb_parse_tree *tree,
-				  struct dn_list *list)
-{
-	struct ldb_context *ldb;
-	struct ldb_dn *dn;
-	int ret;
-	enum key_truncation truncation = KEY_NOT_TRUNCATED;
-
-	ldb = ldb_module_get_ctx(module);
-
-	list->count = 0;
-	list->dn = NULL;
-
-	/* if the attribute isn't in the list of indexed attributes then
-	   this node needs a full search */
-	if (!ldb_kv_is_indexed(module, ldb_kv, tree->u.equality.attr)) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	/* the attribute is indexed. Pull the list of DNs that match the
-	   search criterion */
-	dn = ldb_kv_index_key(ldb,
-			      ldb_kv,
-			      tree->u.equality.attr,
-			      &tree->u.equality.value,
-			      NULL,
-			      &truncation);
-	/*
-	 * We ignore truncation here and allow multi-valued matches
-	 * as ltdb_search_indexed will filter out the wrong one in
-	 * ltdb_index_filter() which calls ldb_match_message().
-	 */
-	if (!dn) return LDB_ERR_OPERATIONS_ERROR;
-
-	ret = ldb_kv_dn_list_load(module, ldb_kv, dn, list);
-	talloc_free(dn);
-	return ret;
-}
-
-static bool list_union(struct ldb_context *ldb,
-		       struct ldb_kv_private *ldb_kv,
-		       struct dn_list *list,
-		       struct dn_list *list2);
-
-/*
-  return a list of dn's that might match a leaf indexed search
- */
-static int ldb_kv_index_dn_leaf(struct ldb_module *module,
-				struct ldb_kv_private *ldb_kv,
-				const struct ldb_parse_tree *tree,
-				struct dn_list *list)
-{
-	if (ldb_kv->disallow_dn_filter &&
-	    (ldb_attr_cmp(tree->u.equality.attr, "dn") == 0)) {
-		/* in AD mode we do not support "(dn=...)" search filters */
-		list->dn = NULL;
-		list->count = 0;
-		return LDB_SUCCESS;
-	}
-	if (tree->u.equality.attr[0] == '@') {
-		/* Do not allow a indexed search against an @ */
-		list->dn = NULL;
-		list->count = 0;
-		return LDB_SUCCESS;
-	}
-	if (ldb_attr_dn(tree->u.equality.attr) == 0) {
-		enum key_truncation truncation = KEY_NOT_TRUNCATED;
-		struct ldb_dn *dn
-			= ldb_dn_from_ldb_val(list,
-					      ldb_module_get_ctx(module),
-					      &tree->u.equality.value);
-		if (dn == NULL) {
-			/* If we can't parse it, no match */
-			list->dn = NULL;
-			list->count = 0;
-			return LDB_SUCCESS;
-		}
-
-		/*
-		 * Re-use the same code we use for a SCOPE_BASE
-		 * search
-		 *
-		 * We can't call TALLOC_FREE(dn) as this must belong
-		 * to list for the memory to remain valid.
-		 */
-		return ldb_kv_index_dn_base_dn(
-		    module, ldb_kv, dn, list, &truncation);
-		/*
-		 * We ignore truncation here and allow multi-valued matches
-		 * as ltdb_search_indexed will filter out the wrong one in
-		 * ltdb_index_filter() which calls ldb_match_message().
-		 */
-
-	} else if ((ldb_kv->cache->GUID_index_attribute != NULL) &&
-		   (ldb_attr_cmp(tree->u.equality.attr,
-				 ldb_kv->cache->GUID_index_attribute) == 0)) {
-		int ret;
-		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		list->dn = talloc_array(list, struct ldb_val, 1);
-		if (list->dn == NULL) {
-			ldb_module_oom(module);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-		/*
-		 * We need to go via the canonicalise_fn() to
-		 * ensure we get the index in binary, rather
-		 * than a string
-		 */
-		ret = ldb_kv->GUID_index_syntax->canonicalise_fn(
-		    ldb, list->dn, &tree->u.equality.value, &list->dn[0]);
-		if (ret != LDB_SUCCESS) {
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-		list->count = 1;
-		return LDB_SUCCESS;
-	}
-
-	return ldb_kv_index_dn_simple(module, ldb_kv, tree, list);
-}
-
-
-/*
-  list intersection
-  list = list & list2
-*/
-static bool list_intersect(struct ldb_context *ldb,
-			   struct ldb_kv_private *ldb_kv,
-			   struct dn_list *list,
-			   const struct dn_list *list2)
-{
-	const struct dn_list *short_list, *long_list;
-	struct dn_list *list3;
-	unsigned int i;
-
-	if (list->count == 0) {
-		/* 0 & X == 0 */
-		return true;
-	}
-	if (list2->count == 0) {
-		/* X & 0 == 0 */
-		list->count = 0;
-		list->dn = NULL;
-		return true;
-	}
-
-	/* the indexing code is allowed to return a longer list than
-	   what really matches, as all results are filtered by the
-	   full expression at the end - this shortcut avoids a lot of
-	   work in some cases */
-	if (list->count < 2 && list2->count > 10 && list2->strict == false) {
-		return true;
-	}
-	if (list2->count < 2 && list->count > 10 && list->strict == false) {
-		list->count = list2->count;
-		list->dn = list2->dn;
-		/* note that list2 may not be the parent of list2->dn,
-		   as list2->dn may be owned by ltdb->idxptr. In that
-		   case we expect this reparent call to fail, which is
-		   OK */
-		talloc_reparent(list2, list, list2->dn);
-		return true;
-	}
-
-	if (list->count > list2->count) {
-		short_list = list2;
-		long_list = list;
-	} else {
-		short_list = list;
-		long_list = list2;
-	}
-
-	list3 = talloc_zero(list, struct dn_list);
-	if (list3 == NULL) {
-		return false;
-	}
-
-	list3->dn = talloc_array(list3, struct ldb_val,
-				 MIN(list->count, list2->count));
-	if (!list3->dn) {
-		talloc_free(list3);
-		return false;
-	}
-	list3->count = 0;
-
-	for (i=0;i<short_list->count;i++) {
-		/* For the GUID index case, this is a binary search */
-		if (ldb_kv_dn_list_find_val(
-			ldb_kv, long_list, &short_list->dn[i]) != -1) {
-			list3->dn[list3->count] = short_list->dn[i];
-			list3->count++;
-		}
-	}
-
-	list->strict |= list2->strict;
-	list->dn = talloc_steal(list, list3->dn);
-	list->count = list3->count;
-	talloc_free(list3);
-
-	return true;
-}
-
-
-/*
-  list union
-  list = list | list2
-*/
-static bool list_union(struct ldb_context *ldb,
-		       struct ldb_kv_private *ldb_kv,
-		       struct dn_list *list,
-		       struct dn_list *list2)
-{
-	struct ldb_val *dn3;
-	unsigned int i = 0, j = 0, k = 0;
-
-	if (list2->count == 0) {
-		/* X | 0 == X */
-		return true;
-	}
-
-	if (list->count == 0) {
-		/* 0 | X == X */
-		list->count = list2->count;
-		list->dn = list2->dn;
-		/* note that list2 may not be the parent of list2->dn,
-		   as list2->dn may be owned by ltdb->idxptr. In that
-		   case we expect this reparent call to fail, which is
-		   OK */
-		talloc_reparent(list2, list, list2->dn);
-		return true;
-	}
-
-	/*
-	 * Sort the lists (if not in GUID DN mode) so we can do
-	 * the de-duplication during the merge
-	 *
-	 * NOTE: This can sort the in-memory index values, as list or
-	 * list2 might not be a copy!
-	 */
-	ldb_kv_dn_list_sort(ldb_kv, list);
-	ldb_kv_dn_list_sort(ldb_kv, list2);
-
-	dn3 = talloc_array(list, struct ldb_val, list->count + list2->count);
-	if (!dn3) {
-		ldb_oom(ldb);
-		return false;
-	}
-
-	while (i < list->count || j < list2->count) {
-		int cmp;
-		if (i >= list->count) {
-			cmp = 1;
-		} else if (j >= list2->count) {
-			cmp = -1;
-		} else {
-			cmp = ldb_val_equal_exact_ordered(list->dn[i],
-							  &list2->dn[j]);
-		}
-
-		if (cmp < 0) {
-			/* Take list */
-			dn3[k] = list->dn[i];
-			i++;
-			k++;
-		} else if (cmp > 0) {
-			/* Take list2 */
-			dn3[k] = list2->dn[j];
-			j++;
-			k++;
-		} else {
-			/* Equal, take list */
-			dn3[k] = list->dn[i];
-			i++;
-			j++;
-			k++;
-		}
-	}
-
-	list->dn = dn3;
-	list->count = k;
-
-	return true;
-}
-
-static int ldb_kv_index_dn(struct ldb_module *module,
-			   struct ldb_kv_private *ldb_kv,
-			   const struct ldb_parse_tree *tree,
-			   struct dn_list *list);
-
-/*
-  process an OR list (a union)
- */
-static int ldb_kv_index_dn_or(struct ldb_module *module,
-			      struct ldb_kv_private *ldb_kv,
-			      const struct ldb_parse_tree *tree,
-			      struct dn_list *list)
-{
-	struct ldb_context *ldb;
-	unsigned int i;
-
-	ldb = ldb_module_get_ctx(module);
-
-	list->dn = NULL;
-	list->count = 0;
-
-	for (i=0; i<tree->u.list.num_elements; i++) {
-		struct dn_list *list2;
-		int ret;
-
-		list2 = talloc_zero(list, struct dn_list);
-		if (list2 == NULL) {
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-
-		ret = ldb_kv_index_dn(
-		    module, ldb_kv, tree->u.list.elements[i], list2);
-
-		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-			/* X || 0 == X */
-			talloc_free(list2);
-			continue;
-		}
-
-		if (ret != LDB_SUCCESS) {
-			/* X || * == * */
-			talloc_free(list2);
-			return ret;
-		}
-
-		if (!list_union(ldb, ldb_kv, list, list2)) {
-			talloc_free(list2);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-	}
-
-	if (list->count == 0) {
-		return LDB_ERR_NO_SUCH_OBJECT;
-	}
-
-	return LDB_SUCCESS;
-}
-
-
-/*
-  NOT an index results
- */
-static int ldb_kv_index_dn_not(struct ldb_module *module,
-			       struct ldb_kv_private *ldb_kv,
-			       const struct ldb_parse_tree *tree,
-			       struct dn_list *list)
-{
-	/* the only way to do an indexed not would be if we could
-	   negate the not via another not or if we knew the total
-	   number of database elements so we could know that the
-	   existing expression covered the whole database.
-
-	   instead, we just give up, and rely on a full index scan
-	   (unless an outer & manages to reduce the list)
-	*/
-	return LDB_ERR_OPERATIONS_ERROR;
-}
-
-/*
- * These things are unique, so avoid a full scan if this is a search
- * by GUID, DN or a unique attribute
- */
-static bool ldb_kv_index_unique(struct ldb_context *ldb,
-				struct ldb_kv_private *ldb_kv,
-				const char *attr)
-{
-	const struct ldb_schema_attribute *a;
-	if (ldb_kv->cache->GUID_index_attribute != NULL) {
-		if (ldb_attr_cmp(attr, ldb_kv->cache->GUID_index_attribute) ==
-		    0) {
-			return true;
-		}
-	}
-	if (ldb_attr_dn(attr) == 0) {
-		return true;
-	}
-
-	a = ldb_schema_attribute_by_name(ldb, attr);
-	if (a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX) {
-		return true;
-	}
-	return false;
-}
-
-/*
-  process an AND expression (intersection)
- */
-static int ldb_kv_index_dn_and(struct ldb_module *module,
-			       struct ldb_kv_private *ldb_kv,
-			       const struct ldb_parse_tree *tree,
-			       struct dn_list *list)
-{
-	struct ldb_context *ldb;
-	unsigned int i;
-	bool found;
-
-	ldb = ldb_module_get_ctx(module);
-
-	list->dn = NULL;
-	list->count = 0;
-
-	/* in the first pass we only look for unique simple
-	   equality tests, in the hope of avoiding having to look
-	   at any others */
-	for (i=0; i<tree->u.list.num_elements; i++) {
-		const struct ldb_parse_tree *subtree = tree->u.list.elements[i];
-		int ret;
-
-		if (subtree->operation != LDB_OP_EQUALITY ||
-		    !ldb_kv_index_unique(
-			ldb, ldb_kv, subtree->u.equality.attr)) {
-			continue;
-		}
-
-		ret = ldb_kv_index_dn(module, ldb_kv, subtree, list);
-		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-			/* 0 && X == 0 */
-			return LDB_ERR_NO_SUCH_OBJECT;
-		}
-		if (ret == LDB_SUCCESS) {
-			/* a unique index match means we can
-			 * stop. Note that we don't care if we return
-			 * a few too many objects, due to later
-			 * filtering */
-			return LDB_SUCCESS;
-		}
-	}
-
-	/* now do a full intersection */
-	found = false;
-
-	for (i=0; i<tree->u.list.num_elements; i++) {
-		const struct ldb_parse_tree *subtree = tree->u.list.elements[i];
-		struct dn_list *list2;
-		int ret;
-
-		list2 = talloc_zero(list, struct dn_list);
-		if (list2 == NULL) {
-			return ldb_module_oom(module);
-		}
-
-		ret = ldb_kv_index_dn(module, ldb_kv, subtree, list2);
-
-		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-			/* X && 0 == 0 */
-			list->dn = NULL;
-			list->count = 0;
-			talloc_free(list2);
-			return LDB_ERR_NO_SUCH_OBJECT;
-		}
-
-		if (ret != LDB_SUCCESS) {
-			/* this didn't adding anything */
-			talloc_free(list2);
-			continue;
-		}
-
-		if (!found) {
-			talloc_reparent(list2, list, list->dn);
-			list->dn = list2->dn;
-			list->count = list2->count;
-			found = true;
-		} else if (!list_intersect(ldb, ldb_kv, list, list2)) {
-			talloc_free(list2);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-
-		if (list->count == 0) {
-			list->dn = NULL;
-			return LDB_ERR_NO_SUCH_OBJECT;
-		}
-
-		if (list->count < 2) {
-			/* it isn't worth loading the next part of the tree */
-			return LDB_SUCCESS;
-		}
-	}
-
-	if (!found) {
-		/* none of the attributes were indexed */
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	return LDB_SUCCESS;
-}
-
-/*
-  return a list of matching objects using a one-level index
- */
-static int ldb_kv_index_dn_attr(struct ldb_module *module,
-				struct ldb_kv_private *ldb_kv,
-				const char *attr,
-				struct ldb_dn *dn,
-				struct dn_list *list,
-				enum key_truncation *truncation)
-{
-	struct ldb_context *ldb;
-	struct ldb_dn *key;
-	struct ldb_val val;
-	int ret;
-
-	ldb = ldb_module_get_ctx(module);
-
-	/* work out the index key from the parent DN */
-	val.data = (uint8_t *)((uintptr_t)ldb_dn_get_casefold(dn));
-	val.length = strlen((char *)val.data);
-	key = ldb_kv_index_key(ldb, ldb_kv, attr, &val, NULL, truncation);
-	if (!key) {
-		ldb_oom(ldb);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ret = ldb_kv_dn_list_load(module, ldb_kv, key, list);
-	talloc_free(key);
-	if (ret != LDB_SUCCESS) {
-		return ret;
-	}
-
-	if (list->count == 0) {
-		return LDB_ERR_NO_SUCH_OBJECT;
-	}
-
-	return LDB_SUCCESS;
-}
-
-/*
-  return a list of matching objects using a one-level index
- */
-static int ldb_kv_index_dn_one(struct ldb_module *module,
-			       struct ldb_kv_private *ldb_kv,
-			       struct ldb_dn *parent_dn,
-			       struct dn_list *list,
-			       enum key_truncation *truncation)
-{
-	/* Ensure we do not shortcut on intersection for this list */
-	list->strict = true;
-	return ldb_kv_index_dn_attr(
-	    module, ldb_kv, LTDB_IDXONE, parent_dn, list, truncation);
-}
-
-/*
-  return a list of matching objects using the DN index
- */
-static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
-				   struct ldb_kv_private *ldb_kv,
-				   struct ldb_dn *base_dn,
-				   struct dn_list *dn_list,
-				   enum key_truncation *truncation)
-{
-	const struct ldb_val *guid_val = NULL;
-	if (ldb_kv->cache->GUID_index_attribute == NULL) {
-		dn_list->dn = talloc_array(dn_list, struct ldb_val, 1);
-		if (dn_list->dn == NULL) {
-			return ldb_module_oom(module);
-		}
-		dn_list->dn[0].data = discard_const_p(unsigned char,
-						      ldb_dn_get_linearized(base_dn));
-		if (dn_list->dn[0].data == NULL) {
-			return ldb_module_oom(module);
-		}
-		dn_list->dn[0].length = strlen((char *)dn_list->dn[0].data);
-		dn_list->count = 1;
-
-		return LDB_SUCCESS;
-	}
-
-	if (ldb_kv->cache->GUID_index_dn_component != NULL) {
-		guid_val = ldb_dn_get_extended_component(
-		    base_dn, ldb_kv->cache->GUID_index_dn_component);
-	}
-
-	if (guid_val != NULL) {
-		dn_list->dn = talloc_array(dn_list, struct ldb_val, 1);
-		if (dn_list->dn == NULL) {
-			return ldb_module_oom(module);
-		}
-		dn_list->dn[0].data = guid_val->data;
-		dn_list->dn[0].length = guid_val->length;
-		dn_list->count = 1;
-
-		return LDB_SUCCESS;
-	}
-
-	return ldb_kv_index_dn_attr(
-	    module, ldb_kv, LTDB_IDXDN, base_dn, dn_list, truncation);
-}
-
-/*
-  return a list of dn's that might match a indexed search or
-  an error. return LDB_ERR_NO_SUCH_OBJECT for no matches, or LDB_SUCCESS for matches
- */
-static int ldb_kv_index_dn(struct ldb_module *module,
-			   struct ldb_kv_private *ldb_kv,
-			   const struct ldb_parse_tree *tree,
-			   struct dn_list *list)
-{
-	int ret = LDB_ERR_OPERATIONS_ERROR;
-
-	switch (tree->operation) {
-	case LDB_OP_AND:
-		ret = ldb_kv_index_dn_and(module, ldb_kv, tree, list);
-		break;
-
-	case LDB_OP_OR:
-		ret = ldb_kv_index_dn_or(module, ldb_kv, tree, list);
-		break;
-
-	case LDB_OP_NOT:
-		ret = ldb_kv_index_dn_not(module, ldb_kv, tree, list);
-		break;
-
-	case LDB_OP_EQUALITY:
-		ret = ldb_kv_index_dn_leaf(module, ldb_kv, tree, list);
-		break;
-
-	case LDB_OP_SUBSTRING:
-	case LDB_OP_GREATER:
-	case LDB_OP_LESS:
-	case LDB_OP_PRESENT:
-	case LDB_OP_APPROX:
-	case LDB_OP_EXTENDED:
-		/* we can't index with fancy bitops yet */
-		ret = LDB_ERR_OPERATIONS_ERROR;
-		break;
-	}
-
-	return ret;
-}
-
-/*
-  filter a candidate dn_list from an indexed search into a set of results
-  extracting just the given attributes
-*/
-static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
-			       const struct dn_list *dn_list,
-			       struct ldb_kv_context *ac,
-			       uint32_t *match_count,
-			       enum key_truncation scope_one_truncation)
-{
-	struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
-	struct ldb_message *msg;
-	struct ldb_message *filtered_msg;
-	unsigned int i;
-	unsigned int num_keys = 0;
-	uint8_t previous_guid_key[LTDB_GUID_KEY_SIZE] = {};
-	TDB_DATA *keys = NULL;
-
-	/*
-	 * We have to allocate the key list (rather than just walk the
-	 * caller supplied list) as the callback could change the list
-	 * (by modifying an indexed attribute hosted in the in-memory
-	 * index cache!)
-	 */
-	keys = talloc_array(ac, TDB_DATA, dn_list->count);
-	if (keys == NULL) {
-		return ldb_module_oom(ac->module);
-	}
-
-	if (ldb_kv->cache->GUID_index_attribute != NULL) {
-		/*
-		 * We speculate that the keys will be GUID based and so
-		 * pre-fill in enough space for a GUID (avoiding a pile of
-		 * small allocations)
-		 */
-		struct guid_tdb_key {
-			uint8_t guid_key[LTDB_GUID_KEY_SIZE];
-		} *key_values = NULL;
-
-		key_values = talloc_array(keys,
-					  struct guid_tdb_key,
-					  dn_list->count);
-
-		if (key_values == NULL) {
-			talloc_free(keys);
-			return ldb_module_oom(ac->module);
-		}
-		for (i = 0; i < dn_list->count; i++) {
-			keys[i].dptr = key_values[i].guid_key;
-			keys[i].dsize = sizeof(key_values[i].guid_key);
-		}
-	} else {
-		for (i = 0; i < dn_list->count; i++) {
-			keys[i].dptr = NULL;
-			keys[i].dsize = 0;
-		}
-	}
-
-	for (i = 0; i < dn_list->count; i++) {
-		int ret;
-
-		ret = ldb_kv_idx_to_key(
-		    ac->module, ldb_kv, keys, &dn_list->dn[i], &keys[num_keys]);
-		if (ret != LDB_SUCCESS) {
-			talloc_free(keys);
-			return ret;
-		}
-
-		if (ldb_kv->cache->GUID_index_attribute != NULL) {
-			/*
-			 * If we are in GUID index mode, then the dn_list is
-			 * sorted.  If we got a duplicate, forget about it, as
-			 * otherwise we would send the same entry back more
-			 * than once.
-			 *
-			 * This is needed in the truncated DN case, or if a
-			 * duplicate was forced in via
-			 * LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK
-			 */
-
-			if (memcmp(previous_guid_key,
-				   keys[num_keys].dptr,
-				   sizeof(previous_guid_key)) == 0) {
-				continue;
-			}
-
-			memcpy(previous_guid_key,
-			       keys[num_keys].dptr,
-			       sizeof(previous_guid_key));
-		}
-		num_keys++;
-	}
-
-
-	/*
-	 * Now that the list is a safe copy, send the callbacks
-	 */
-	for (i = 0; i < num_keys; i++) {
-		int ret;
-		bool matched;
-		msg = ldb_msg_new(ac);
-		if (!msg) {
-			talloc_free(keys);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-
-		ret =
-		    ldb_kv_search_key(ac->module,
-				      ldb_kv,
-				      keys[i],
-				      msg,
-				      LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
-					  LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC);
-		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-			/*
-			 * the record has disappeared? yes, this can
-			 * happen if the entry is deleted by something
-			 * operating in the callback (not another
-			 * process, as we have a read lock)
-			 */
-			talloc_free(msg);
-			continue;
-		}
-
-		if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
-			/* an internal error */
-			talloc_free(keys);
-			talloc_free(msg);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-
-		/*
-		 * We trust the index for LDB_SCOPE_ONELEVEL
-		 * unless the index key has been truncated.
-		 *
-		 * LDB_SCOPE_BASE is not passed in by our only caller.
-		 */
-		if (ac->scope == LDB_SCOPE_ONELEVEL &&
-		    ldb_kv->cache->one_level_indexes &&
-		    scope_one_truncation == KEY_NOT_TRUNCATED) {
-			ret = ldb_match_message(ldb, msg, ac->tree,
-						ac->scope, &matched);
-		} else {
-			ret = ldb_match_msg_error(ldb, msg,
-						  ac->tree, ac->base,
-						  ac->scope, &matched);
-		}
-
-		if (ret != LDB_SUCCESS) {
-			talloc_free(keys);
-			talloc_free(msg);
-			return ret;
-		}
-		if (!matched) {
-			talloc_free(msg);
-			continue;
-		}
-
-		/* filter the attributes that the user wants */
-		ret = ldb_kv_filter_attrs(ac, msg, ac->attrs, &filtered_msg);
-
-		talloc_free(msg);
-
-		if (ret == -1) {
-			talloc_free(keys);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-
-		ret = ldb_module_send_entry(ac->req, filtered_msg, NULL);
-		if (ret != LDB_SUCCESS) {
-			/* Regardless of success or failure, the msg
-			 * is the callbacks responsiblity, and should
-			 * not be talloc_free()'ed */
-			ac->request_terminated = true;
-			talloc_free(keys);
-			return ret;
-		}
-
-		(*match_count)++;
-	}
-
-	TALLOC_FREE(keys);
-	return LDB_SUCCESS;
-}
-
-/*
-  sort a DN list
- */
-static void ldb_kv_dn_list_sort(struct ldb_kv_private *ltdb,
-				struct dn_list *list)
-{
-	if (list->count < 2) {
-		return;
-	}
-
-	/* We know the list is sorted when using the GUID index */
-	if (ltdb->cache->GUID_index_attribute != NULL) {
-		return;
-	}
-
-	TYPESAFE_QSORT(list->dn, list->count,
-		       ldb_val_equal_exact_for_qsort);
-}
-
-/*
-  search the database with a LDAP-like expression using indexes
-  returns -1 if an indexed search is not possible, in which
-  case the caller should call ltdb_search_full()
-*/
-int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
-{
-	struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
-	struct ldb_kv_private *ldb_kv = talloc_get_type(
-	    ldb_module_get_private(ac->module), struct ldb_kv_private);
-	struct dn_list *dn_list;
-	int ret;
-	enum ldb_scope index_scope;
-	enum key_truncation scope_one_truncation = KEY_NOT_TRUNCATED;
-
-	/* see if indexing is enabled */
-	if (!ldb_kv->cache->attribute_indexes &&
-	    !ldb_kv->cache->one_level_indexes && ac->scope != LDB_SCOPE_BASE) {
-		/* fallback to a full search */
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	dn_list = talloc_zero(ac, struct dn_list);
-	if (dn_list == NULL) {
-		return ldb_module_oom(ac->module);
-	}
-
-	/*
-	 * For the purposes of selecting the switch arm below, if we
-	 * don't have a one-level index then treat it like a subtree
-	 * search
-	 */
-	if (ac->scope == LDB_SCOPE_ONELEVEL &&
-	    !ldb_kv->cache->one_level_indexes) {
-		index_scope = LDB_SCOPE_SUBTREE;
-	} else {
-		index_scope = ac->scope;
-	}
-
-	switch (index_scope) {
-	case LDB_SCOPE_BASE:
-		/*
-		 * The only caller will have filtered the operation out
-		 * so we should never get here
-		 */
-		return ldb_operr(ldb);
-
-	case LDB_SCOPE_ONELEVEL:
-		/*
-		 * If we ever start to also load the index values for
-		 * the tree, we must ensure we strictly intersect with
-		 * this list, as we trust the ONELEVEL index
-		 */
-		ret = ldb_kv_index_dn_one(ac->module,
-					  ldb_kv,
-					  ac->base,
-					  dn_list,
-					  &scope_one_truncation);
-		if (ret != LDB_SUCCESS) {
-			talloc_free(dn_list);
-			return ret;
-		}
-
-		/*
-		 * If we have too many matches, running the filter
-		 * tree over the SCOPE_ONELEVEL can be quite expensive
-		 * so we now check the filter tree index as well.
-		 *
-		 * We only do this in the GUID index mode, which is
-		 * O(n*log(m)) otherwise the intersection below will
-		 * be too costly at O(n*m).
-		 *
-		 * We don't set a heuristic for 'too many' but instead
-		 * do it always and rely on the index lookup being
-		 * fast enough in the small case.
-		 */
-		if (ldb_kv->cache->GUID_index_attribute != NULL) {
-			struct dn_list *idx_one_tree_list
-				= talloc_zero(ac, struct dn_list);
-			if (idx_one_tree_list == NULL) {
-				return ldb_module_oom(ac->module);
-			}
-
-			if (!ldb_kv->cache->attribute_indexes) {
-				talloc_free(idx_one_tree_list);
-				talloc_free(dn_list);
-				return LDB_ERR_OPERATIONS_ERROR;
-			}
-			/*
-			 * Here we load the index for the tree.
-			 *
-			 * We only care if this is successful, if the
-			 * index can't trim the result list down then
-			 * the ONELEVEL index is still good enough.
-			 */
-			ret = ldb_kv_index_dn(
-			    ac->module, ldb_kv, ac->tree, idx_one_tree_list);
-			if (ret == LDB_SUCCESS) {
-				if (!list_intersect(ldb,
-						    ldb_kv,
-						    dn_list,
-						    idx_one_tree_list)) {
-					talloc_free(idx_one_tree_list);
-					talloc_free(dn_list);
-					return LDB_ERR_OPERATIONS_ERROR;
-				}
-			}
-		}
-		break;
-
-	case LDB_SCOPE_SUBTREE:
-	case LDB_SCOPE_DEFAULT:
-		if (!ldb_kv->cache->attribute_indexes) {
-			talloc_free(dn_list);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-		/*
-		 * Here we load the index for the tree.  We have no
-		 * index for the subtree.
-		 */
-		ret = ldb_kv_index_dn(ac->module, ldb_kv, ac->tree, dn_list);
-		if (ret != LDB_SUCCESS) {
-			talloc_free(dn_list);
-			return ret;
-		}
-		break;
-	}
-
-	/*
-	 * It is critical that this function do the re-filter even
-	 * on things found by the index as the index can over-match
-	 * in cases of truncation (as well as when it decides it is
-	 * not worth further filtering)
-	 *
-	 * If this changes, then the index code above would need to
-	 * pass up a flag to say if any index was truncated during
-	 * processing as the truncation here refers only to the
-	 * SCOPE_ONELEVEL index.
-	 */
-	ret = ldb_kv_index_filter(
-	    ldb_kv, dn_list, ac, match_count, scope_one_truncation);
-	talloc_free(dn_list);
-	return ret;
-}
-
-/**
- * @brief Add a DN in the index list of a given attribute name/value pair
- *
- * This function will add the DN in the index list for the index for
- * the given attribute name and value.
- *
- * @param[in]  module       A ldb_module structure
- *
- * @param[in]  dn           The string representation of the DN as it
- *                          will be stored in the index entry
- *
- * @param[in]  el           A ldb_message_element array, one of the entry
- *                          referred by the v_idx is the attribute name and
- *                          value pair which will be used to construct the
- *                          index name
- *
- * @param[in]  v_idx        The index of element in the el array to use
- *
- * @return                  An ldb error code
- */
-static int ldb_kv_index_add1(struct ldb_module *module,
-			     struct ldb_kv_private *ldb_kv,
-			     const struct ldb_message *msg,
-			     struct ldb_message_element *el,
-			     int v_idx)
-{
-	struct ldb_context *ldb;
-	struct ldb_dn *dn_key;
-	int ret;
-	const struct ldb_schema_attribute *a;
-	struct dn_list *list;
-	unsigned alloc_len;
-	enum key_truncation truncation = KEY_TRUNCATED;
-
-
-	ldb = ldb_module_get_ctx(module);
-
-	list = talloc_zero(module, struct dn_list);
-	if (list == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	dn_key = ldb_kv_index_key(
-	    ldb, ldb_kv, el->name, &el->values[v_idx], &a, &truncation);
-	if (!dn_key) {
-		talloc_free(list);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	/*
-	 * Samba only maintains unique indexes on the objectSID and objectGUID
-	 * so if a unique index key exceeds the maximum length there is a
-	 * problem.
-	 */
-	if ((truncation == KEY_TRUNCATED) && (a != NULL &&
-		(a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX ||
-		(el->flags & LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX)))) {
-
-		ldb_asprintf_errstring(
-		    ldb,
-		    __location__ ": unique index key on %s in %s, "
-				 "exceeds maximum key length of %u (encoded).",
-		    el->name,
-		    ldb_dn_get_linearized(msg->dn),
-		    ldb_kv->max_key_length);
-		talloc_free(list);
-		return LDB_ERR_CONSTRAINT_VIOLATION;
-	}
-	talloc_steal(list, dn_key);
-
-	ret = ldb_kv_dn_list_load(module, ldb_kv, dn_key, list);
-	if (ret != LDB_SUCCESS && ret != LDB_ERR_NO_SUCH_OBJECT) {
-		talloc_free(list);
-		return ret;
-	}
-
-	/*
-	 * Check for duplicates in the @IDXDN DN -> GUID record
-	 *
-	 * This is very normal, it just means a duplicate DN creation
-	 * was attempted, so don't set the error string or print scary
-	 * messages.
-	 */
-	if (list->count > 0 &&
-	    ldb_attr_cmp(el->name, LTDB_IDXDN) == 0 &&
-	    truncation == KEY_NOT_TRUNCATED) {
-
-		talloc_free(list);
-		return LDB_ERR_CONSTRAINT_VIOLATION;
-
-	} else if (list->count > 0
-		   && ldb_attr_cmp(el->name, LTDB_IDXDN) == 0) {
-
-		/*
-		 * At least one existing entry in the DN->GUID index, which
-		 * arises when the DN indexes have been truncated
-		 *
-		 * So need to pull the DN's to check if it's really a duplicate
-		 */
-		int i;
-		for (i=0; i < list->count; i++) {
-			uint8_t guid_key[LTDB_GUID_KEY_SIZE];
-			TDB_DATA key = {
-				.dptr = guid_key,
-				.dsize = sizeof(guid_key)
-			};
-			const int flags = LDB_UNPACK_DATA_FLAG_NO_ATTRS;
-			struct ldb_message *rec = ldb_msg_new(ldb);
-			if (rec == NULL) {
-				return LDB_ERR_OPERATIONS_ERROR;
-			}
-
-			ret = ldb_kv_idx_to_key(
-			    module, ldb_kv, ldb, &list->dn[i], &key);
-			if (ret != LDB_SUCCESS) {
-				TALLOC_FREE(list);
-				TALLOC_FREE(rec);
-				return ret;
-			}
-
-			ret =
-			    ldb_kv_search_key(module, ldb_kv, key, rec, flags);
-			if (key.dptr != guid_key) {
-				TALLOC_FREE(key.dptr);
-			}
-			if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-				/*
-				 * the record has disappeared?
-				 * yes, this can happen
-				 */
-				talloc_free(rec);
-				continue;
-			}
-
-			if (ret != LDB_SUCCESS) {
-				/* an internal error */
-				TALLOC_FREE(rec);
-				TALLOC_FREE(list);
-				return LDB_ERR_OPERATIONS_ERROR;
-			}
-			/*
-			 * The DN we are trying to add to the DB and index
-			 * is already here, so we must deny the addition
-			 */
-			if (ldb_dn_compare(msg->dn, rec->dn) == 0) {
-				TALLOC_FREE(rec);
-				TALLOC_FREE(list);
-				return LDB_ERR_CONSTRAINT_VIOLATION;
-			}
-		}
-	}
-
-	/*
-	 * Check for duplicates in unique indexes
-	 *
-	 * We don't need to do a loop test like the @IDXDN case
-	 * above as we have a ban on long unique index values
-	 * at the start of this function.
-	 */
-	if (list->count > 0 &&
-	    ((a != NULL
-	      && (a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX ||
-		  (el->flags & LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX))))) {
-		/*
-		 * We do not want to print info about a possibly
-		 * confidential DN that the conflict was with in the
-		 * user-visible error string
-		 */
-
-		if (ldb_kv->cache->GUID_index_attribute == NULL) {
-			ldb_debug(ldb, LDB_DEBUG_WARNING,
-				  __location__
-				  ": unique index violation on %s in %s, "
-				  "conficts with %*.*s in %s",
-				  el->name, ldb_dn_get_linearized(msg->dn),
-				  (int)list->dn[0].length,
-				  (int)list->dn[0].length,
-				  list->dn[0].data,
-				  ldb_dn_get_linearized(dn_key));
-		} else {
-			/* This can't fail, gives a default at worst */
-			const struct ldb_schema_attribute *attr =
-			    ldb_schema_attribute_by_name(
-				ldb, ldb_kv->cache->GUID_index_attribute);
-			struct ldb_val v;
-			ret = attr->syntax->ldif_write_fn(ldb, list,
-							  &list->dn[0], &v);
-			if (ret == LDB_SUCCESS) {
-				ldb_debug(ldb,
-					  LDB_DEBUG_WARNING,
-					  __location__
-					  ": unique index violation on %s in "
-					  "%s, conficts with %s %*.*s in %s",
-					  el->name,
-					  ldb_dn_get_linearized(msg->dn),
-					  ldb_kv->cache->GUID_index_attribute,
-					  (int)v.length,
-					  (int)v.length,
-					  v.data,
-					  ldb_dn_get_linearized(dn_key));
-			}
-		}
-		ldb_asprintf_errstring(ldb,
-				       __location__ ": unique index violation "
-				       "on %s in %s",
-				       el->name,
-				       ldb_dn_get_linearized(msg->dn));
-		talloc_free(list);
-		return LDB_ERR_CONSTRAINT_VIOLATION;
-	}
-
-	/* overallocate the list a bit, to reduce the number of
-	 * realloc trigered copies */
-	alloc_len = ((list->count+1)+7) & ~7;
-	list->dn = talloc_realloc(list, list->dn, struct ldb_val, alloc_len);
-	if (list->dn == NULL) {
-		talloc_free(list);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	if (ldb_kv->cache->GUID_index_attribute == NULL) {
-		const char *dn_str = ldb_dn_get_linearized(msg->dn);
-		list->dn[list->count].data
-			= (uint8_t *)talloc_strdup(list->dn, dn_str);
-		if (list->dn[list->count].data == NULL) {
-			talloc_free(list);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-		list->dn[list->count].length = strlen(dn_str);
-	} else {
-		const struct ldb_val *key_val;
-		struct ldb_val *exact = NULL, *next = NULL;
-		key_val = ldb_msg_find_ldb_val(
-		    msg, ldb_kv->cache->GUID_index_attribute);
-		if (key_val == NULL) {
-			talloc_free(list);
-			return ldb_module_operr(module);
-		}
-
-		if (key_val->length != LTDB_GUID_SIZE) {
-			talloc_free(list);
-			return ldb_module_operr(module);
-		}
-
-		BINARY_ARRAY_SEARCH_GTE(list->dn, list->count,
-					*key_val, ldb_val_equal_exact_ordered,
-					exact, next);
-
-		/*
-		 * Give a warning rather than fail, this could be a
-		 * duplicate value in the record allowed by a caller
-		 * forcing in the value with
-		 * LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK
-		 */
-		if (exact != NULL && truncation == KEY_NOT_TRUNCATED) {
-			/* This can't fail, gives a default at worst */
-			const struct ldb_schema_attribute *attr =
-			    ldb_schema_attribute_by_name(
-				ldb, ldb_kv->cache->GUID_index_attribute);
-			struct ldb_val v;
-			ret = attr->syntax->ldif_write_fn(ldb, list,
-							  exact, &v);
-			if (ret == LDB_SUCCESS) {
-				ldb_debug(ldb,
-					  LDB_DEBUG_WARNING,
-					  __location__
-					  ": duplicate attribute value in %s "
-					  "for index on %s, "
-					  "duplicate of %s %*.*s in %s",
-					  ldb_dn_get_linearized(msg->dn),
-					  el->name,
-					  ldb_kv->cache->GUID_index_attribute,
-					  (int)v.length,
-					  (int)v.length,
-					  v.data,
-					  ldb_dn_get_linearized(dn_key));
-			}
-		}
-
-		if (next == NULL) {
-			next = &list->dn[list->count];
-		} else {
-			memmove(&next[1], next,
-				sizeof(*next) * (list->count - (next - list->dn)));
-		}
-		*next = ldb_val_dup(list->dn, key_val);
-		if (next->data == NULL) {
-			talloc_free(list);
-			return ldb_module_operr(module);
-		}
-	}
-	list->count++;
-
-	ret = ldb_kv_dn_list_store(module, dn_key, list);
-
-	talloc_free(list);
-
-	return ret;
-}
-
-/*
-  add index entries for one elements in a message
- */
-static int ldb_kv_index_add_el(struct ldb_module *module,
-			       struct ldb_kv_private *ldb_kv,
-			       const struct ldb_message *msg,
-			       struct ldb_message_element *el)
-{
-	unsigned int i;
-	for (i = 0; i < el->num_values; i++) {
-		int ret = ldb_kv_index_add1(module, ldb_kv, msg, el, i);
-		if (ret != LDB_SUCCESS) {
-			return ret;
-		}
-	}
-
-	return LDB_SUCCESS;
-}
-
-/*
-  add index entries for all elements in a message
- */
-static int ldb_kv_index_add_all(struct ldb_module *module,
-				struct ldb_kv_private *ldb_kv,
-				const struct ldb_message *msg)
-{
-	struct ldb_message_element *elements = msg->elements;
-	unsigned int i;
-	const char *dn_str;
-	int ret;
-
-	if (ldb_dn_is_special(msg->dn)) {
-		return LDB_SUCCESS;
-	}
-
-	dn_str = ldb_dn_get_linearized(msg->dn);
-	if (dn_str == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ret = ldb_kv_write_index_dn_guid(module, msg, 1);
-	if (ret != LDB_SUCCESS) {
-		return ret;
-	}
-
-	if (!ldb_kv->cache->attribute_indexes) {
-		/* no indexed fields */
-		return LDB_SUCCESS;
-	}
-
-	for (i = 0; i < msg->num_elements; i++) {
-		if (!ldb_kv_is_indexed(module, ldb_kv, elements[i].name)) {
-			continue;
-		}
-		ret = ldb_kv_index_add_el(module, ldb_kv, msg, &elements[i]);
-		if (ret != LDB_SUCCESS) {
-			struct ldb_context *ldb = ldb_module_get_ctx(module);
-			ldb_asprintf_errstring(ldb,
-					       __location__ ": Failed to re-index %s in %s - %s",
-					       elements[i].name, dn_str,
-					       ldb_errstring(ldb));
-			return ret;
-		}
-	}
-
-	return LDB_SUCCESS;
-}
-
-
-/*
-  insert a DN index for a message
-*/
-static int ldb_kv_modify_index_dn(struct ldb_module *module,
-				  struct ldb_kv_private *ldb_kv,
-				  const struct ldb_message *msg,
-				  struct ldb_dn *dn,
-				  const char *index,
-				  int add)
-{
-	struct ldb_message_element el;
-	struct ldb_val val;
-	int ret;
-
-	val.data = (uint8_t *)((uintptr_t)ldb_dn_get_casefold(dn));
-	if (val.data == NULL) {
-		const char *dn_str = ldb_dn_get_linearized(dn);
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       __location__ ": Failed to modify %s "
-						    "against %s in %s: failed "
-						    "to get casefold DN",
-				       index,
-				       ldb_kv->cache->GUID_index_attribute,
-				       dn_str);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	val.length = strlen((char *)val.data);
-	el.name = index;
-	el.values = &val;
-	el.num_values = 1;
-
-	if (add) {
-		ret = ldb_kv_index_add1(module, ldb_kv, msg, &el, 0);
-	} else { /* delete */
-		ret = ldb_kv_index_del_value(module, ldb_kv, msg, &el, 0);
-	}
-
-	if (ret != LDB_SUCCESS) {
-		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		const char *dn_str = ldb_dn_get_linearized(dn);
-		ldb_asprintf_errstring(ldb,
-				       __location__ ": Failed to modify %s "
-						    "against %s in %s - %s",
-				       index,
-				       ldb_kv->cache->GUID_index_attribute,
-				       dn_str,
-				       ldb_errstring(ldb));
-		return ret;
-	}
-	return ret;
-}
-
-/*
-  insert a one level index for a message
-*/
-static int ldb_kv_index_onelevel(struct ldb_module *module,
-				 const struct ldb_message *msg,
-				 int add)
-{
-	struct ldb_kv_private *ldb_kv = talloc_get_type(
-	    ldb_module_get_private(module), struct ldb_kv_private);
-	struct ldb_dn *pdn;
-	int ret;
-
-	/* We index for ONE Level only if requested */
-	if (!ldb_kv->cache->one_level_indexes) {
-		return LDB_SUCCESS;
-	}
-
-	pdn = ldb_dn_get_parent(module, msg->dn);
-	if (pdn == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	ret =
-	    ldb_kv_modify_index_dn(module, ldb_kv, msg, pdn, LTDB_IDXONE, add);
-
-	talloc_free(pdn);
-
-	return ret;
-}
-
-/*
-  insert a one level index for a message
-*/
-static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
-				      const struct ldb_message *msg,
-				      int add)
-{
-	int ret;
-	struct ldb_kv_private *ldb_kv = talloc_get_type(
-	    ldb_module_get_private(module), struct ldb_kv_private);
-
-	/* We index for DN only if using a GUID index */
-	if (ldb_kv->cache->GUID_index_attribute == NULL) {
-		return LDB_SUCCESS;
-	}
-
-	ret = ldb_kv_modify_index_dn(
-	    module, ldb_kv, msg, msg->dn, LTDB_IDXDN, add);
-
-	if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       "Entry %s already exists",
-				       ldb_dn_get_linearized(msg->dn));
-		ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
-	}
-	return ret;
-}
-
-/*
-  add the index entries for a new element in a record
-  The caller guarantees that these element values are not yet indexed
-*/
-int ldb_kv_index_add_element(struct ldb_module *module,
-			     struct ldb_kv_private *ldb_kv,
-			     const struct ldb_message *msg,
-			     struct ldb_message_element *el)
-{
-	if (ldb_dn_is_special(msg->dn)) {
-		return LDB_SUCCESS;
-	}
-	if (!ldb_kv_is_indexed(module, ldb_kv, el->name)) {
-		return LDB_SUCCESS;
-	}
-	return ldb_kv_index_add_el(module, ldb_kv, msg, el);
-}
-
-/*
-  add the index entries for a new record
-*/
-int ldb_kv_index_add_new(struct ldb_module *module,
-			 struct ldb_kv_private *ldb_kv,
-			 const struct ldb_message *msg)
-{
-	int ret;
-
-	if (ldb_dn_is_special(msg->dn)) {
-		return LDB_SUCCESS;
-	}
-
-	ret = ldb_kv_index_add_all(module, ldb_kv, msg);
-	if (ret != LDB_SUCCESS) {
-		/*
-		 * Because we can't trust the caller to be doing
-		 * transactions properly, clean up any index for this
-		 * entry rather than relying on a transaction
-		 * cleanup
-		 */
-
-		ldb_kv_index_delete(module, msg);
-		return ret;
-	}
-
-	ret = ldb_kv_index_onelevel(module, msg, 1);
-	if (ret != LDB_SUCCESS) {
-		/*
-		 * Because we can't trust the caller to be doing
-		 * transactions properly, clean up any index for this
-		 * entry rather than relying on a transaction
-		 * cleanup
-		 */
-		ldb_kv_index_delete(module, msg);
-		return ret;
-	}
-	return ret;
-}
-
-
-/*
-  delete an index entry for one message element
-*/
-int ldb_kv_index_del_value(struct ldb_module *module,
-			   struct ldb_kv_private *ldb_kv,
-			   const struct ldb_message *msg,
-			   struct ldb_message_element *el,
-			   unsigned int v_idx)
-{
-	struct ldb_context *ldb;
-	struct ldb_dn *dn_key;
-	const char *dn_str;
-	int ret, i;
-	unsigned int j;
-	struct dn_list *list;
-	struct ldb_dn *dn = msg->dn;
-	enum key_truncation truncation = KEY_NOT_TRUNCATED;
-
-	ldb = ldb_module_get_ctx(module);
-
-	dn_str = ldb_dn_get_linearized(dn);
-	if (dn_str == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	if (dn_str[0] == '@') {
-		return LDB_SUCCESS;
-	}
-
-	dn_key = ldb_kv_index_key(
-	    ldb, ldb_kv, el->name, &el->values[v_idx], NULL, &truncation);
-	/*
-	 * We ignore key truncation in ltdb_index_add1() so
-	 * match that by ignoring it here as well
-	 *
-	 * Multiple values are legitimate and accepted
-	 */
-	if (!dn_key) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	list = talloc_zero(dn_key, struct dn_list);
-	if (list == NULL) {
-		talloc_free(dn_key);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ret = ldb_kv_dn_list_load(module, ldb_kv, dn_key, list);
-	if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-		/* it wasn't indexed. Did we have an earlier error? If we did then
-		   its gone now */
-		talloc_free(dn_key);
-		return LDB_SUCCESS;
-	}
-
-	if (ret != LDB_SUCCESS) {
-		talloc_free(dn_key);
-		return ret;
-	}
-
-	/*
-	 * Find one of the values matching this message to remove
-	 */
-	i = ldb_kv_dn_list_find_msg(ldb_kv, list, msg);
-	if (i == -1) {
-		/* nothing to delete */
-		talloc_free(dn_key);
-		return LDB_SUCCESS;
-	}
-
-	j = (unsigned int) i;
-	if (j != list->count - 1) {
-		memmove(&list->dn[j], &list->dn[j+1], sizeof(list->dn[0])*(list->count - (j+1)));
-	}
-	list->count--;
-	if (list->count == 0) {
-		talloc_free(list->dn);
-		list->dn = NULL;
-	} else {
-		list->dn = talloc_realloc(list, list->dn, struct ldb_val, list->count);
-	}
-
-	ret = ldb_kv_dn_list_store(module, dn_key, list);
-
-	talloc_free(dn_key);
-
-	return ret;
-}
-
-/*
-  delete the index entries for a element
-  return -1 on failure
-*/
-int ldb_kv_index_del_element(struct ldb_module *module,
-			     struct ldb_kv_private *ldb_kv,
-			     const struct ldb_message *msg,
-			     struct ldb_message_element *el)
-{
-	const char *dn_str;
-	int ret;
-	unsigned int i;
-
-	if (!ldb_kv->cache->attribute_indexes) {
-		/* no indexed fields */
-		return LDB_SUCCESS;
-	}
-
-	dn_str = ldb_dn_get_linearized(msg->dn);
-	if (dn_str == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	if (dn_str[0] == '@') {
-		return LDB_SUCCESS;
-	}
-
-	if (!ldb_kv_is_indexed(module, ldb_kv, el->name)) {
-		return LDB_SUCCESS;
-	}
-	for (i = 0; i < el->num_values; i++) {
-		ret = ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
-		if (ret != LDB_SUCCESS) {
-			return ret;
-		}
-	}
-
-	return LDB_SUCCESS;
-}
-
-/*
-  delete the index entries for a record
-  return -1 on failure
-*/
-int ldb_kv_index_delete(struct ldb_module *module,
-			const struct ldb_message *msg)
-{
-	struct ldb_kv_private *ldb_kv = talloc_get_type(
-	    ldb_module_get_private(module), struct ldb_kv_private);
-	int ret;
-	unsigned int i;
-
-	if (ldb_dn_is_special(msg->dn)) {
-		return LDB_SUCCESS;
-	}
-
-	ret = ldb_kv_index_onelevel(module, msg, 0);
-	if (ret != LDB_SUCCESS) {
-		return ret;
-	}
-
-	ret = ldb_kv_write_index_dn_guid(module, msg, 0);
-	if (ret != LDB_SUCCESS) {
-		return ret;
-	}
-
-	if (!ldb_kv->cache->attribute_indexes) {
-		/* no indexed fields */
-		return LDB_SUCCESS;
-	}
-
-	for (i = 0; i < msg->num_elements; i++) {
-		ret = ldb_kv_index_del_element(
-		    module, ldb_kv, msg, &msg->elements[i]);
-		if (ret != LDB_SUCCESS) {
-			return ret;
-		}
-	}
-
-	return LDB_SUCCESS;
-}
-
-
-/*
-  traversal function that deletes all @INDEX records in the in-memory
-  TDB.
-
-  This does not touch the actual DB, that is done at transaction
-  commit, which in turn greatly reduces DB churn as we will likely
-  be able to do a direct update into the old record.
-*/
-static int delete_index(struct ldb_kv_private *ldb_kv,
-			struct ldb_val key,
-			struct ldb_val data,
-			void *state)
-{
-	struct ldb_module *module = state;
-	const char *dnstr = "DN=" LTDB_INDEX ":";
-	struct dn_list list;
-	struct ldb_dn *dn;
-	struct ldb_val v;
-	int ret;
-
-	if (strncmp((char *)key.data, dnstr, strlen(dnstr)) != 0) {
-		return 0;
-	}
-	/* we need to put a empty list in the internal tdb for this
-	 * index entry */
-	list.dn = NULL;
-	list.count = 0;
-
-	/* the offset of 3 is to remove the DN= prefix. */
-	v.data = key.data + 3;
-	v.length = strnlen((char *)key.data, key.length) - 3;
-
-	dn = ldb_dn_from_ldb_val(ldb_kv, ldb_module_get_ctx(module), &v);
-
-	/*
-	 * This does not actually touch the DB quite yet, just
-         * the in-memory index cache
-	 */
-	ret = ldb_kv_dn_list_store(module, dn, &list);
-	if (ret != LDB_SUCCESS) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       "Unable to store null index for %s\n",
-						ldb_dn_get_linearized(dn));
-		talloc_free(dn);
-		return -1;
-	}
-	talloc_free(dn);
-	return 0;
-}
-
-/*
-  traversal function that adds @INDEX records during a re index TODO wrong comment
-*/
-static int re_key(struct ldb_kv_private *ldb_kv,
-		  struct ldb_val ldb_key,
-		  struct ldb_val val,
-		  void *state)
-{
-	struct ldb_context *ldb;
-	struct ldb_kv_reindex_context *ctx =
-	    (struct ldb_kv_reindex_context *)state;
-	struct ldb_module *module = ctx->module;
-	struct ldb_message *msg;
-	unsigned int nb_elements_in_db;
-	int ret;
-	TDB_DATA key2;
-	bool is_record;
-	TDB_DATA key = {
-		.dptr = ldb_key.data,
-		.dsize = ldb_key.length
-	};
-	
-	ldb = ldb_module_get_ctx(module);
-
-	if (key.dsize > 4 &&
-	    memcmp(key.dptr, "DN=@", 4) == 0) {
-		return 0;
-	}
-
-	is_record = ldb_kv_key_is_record(key);
-	if (is_record == false) {
-		return 0;
-	}
-	
-	msg = ldb_msg_new(module);
-	if (msg == NULL) {
-		return -1;
-	}
-
-	ret = ldb_unpack_data_only_attr_list_flags(ldb, &val,
-						   msg,
-						   NULL, 0,
-						   LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC,
-						   &nb_elements_in_db);
-	if (ret != 0) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %s\n",
-						ldb_dn_get_linearized(msg->dn));
-		ctx->error = ret;
-		talloc_free(msg);
-		return -1;
-	}
-
-	if (msg->dn == NULL) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR,
-			  "Refusing to re-index as GUID "
-			  "key %*.*s with no DN\n",
-			  (int)key.dsize, (int)key.dsize,
-			  (char *)key.dptr);
-		talloc_free(msg);
-		return -1;
-	}
-	
-	/* check if the DN key has changed, perhaps due to the case
-	   insensitivity of an element changing, or a change from DN
-	   to GUID keys */
-	key2 = ldb_kv_key_msg(module, msg, msg);
-	if (key2.dptr == NULL) {
-		/* probably a corrupt record ... darn */
-		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid DN in re_index: %s",
-						ldb_dn_get_linearized(msg->dn));
-		talloc_free(msg);
-		return 0;
-	}
-	if (key.dsize != key2.dsize ||
-	    (memcmp(key.dptr, key2.dptr, key.dsize) != 0)) {
-		struct ldb_val ldb_key2 = {
-			.data = key2.dptr,
-			.length = key2.dsize
-		};
-		ldb_kv->kv_ops->update_in_iterate(
-		    ldb_kv, ldb_key, ldb_key2, val, ctx);
-	}
-	talloc_free(key2.dptr);
-
-	talloc_free(msg);
-
-	ctx->count++;
-	if (ctx->count % 10000 == 0) {
-		ldb_debug(ldb, LDB_DEBUG_WARNING,
-			  "Reindexing: re-keyed %u records so far",
-			  ctx->count);
-	}
-
-	return 0;
-}
-
-/*
-  traversal function that adds @INDEX records during a re index
-*/
-static int re_index(struct ldb_kv_private *ldb_kv,
-		    struct ldb_val ldb_key,
-		    struct ldb_val val,
-		    void *state)
-{
-	struct ldb_context *ldb;
-	struct ldb_kv_reindex_context *ctx =
-	    (struct ldb_kv_reindex_context *)state;
-	struct ldb_module *module = ctx->module;
-	struct ldb_message *msg;
-	unsigned int nb_elements_in_db;
-	TDB_DATA key = {
-		.dptr = ldb_key.data,
-		.dsize = ldb_key.length
-	};
-	int ret;
-	bool is_record;
-	
-	ldb = ldb_module_get_ctx(module);
-
-	if (key.dsize > 4 &&
-	    memcmp(key.dptr, "DN=@", 4) == 0) {
-		return 0;
-	}
-
-	is_record = ldb_kv_key_is_record(key);
-	if (is_record == false) {
-		return 0;
-	}
-	
-	msg = ldb_msg_new(module);
-	if (msg == NULL) {
-		return -1;
-	}
-
-	ret = ldb_unpack_data_only_attr_list_flags(ldb, &val,
-						   msg,
-						   NULL, 0,
-						   LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC,
-						   &nb_elements_in_db);
-	if (ret != 0) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %s\n",
-						ldb_dn_get_linearized(msg->dn));
-		ctx->error = ret;
-		talloc_free(msg);
-		return -1;
-	}
-
-	if (msg->dn == NULL) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR,
-			  "Refusing to re-index as GUID "
-			  "key %*.*s with no DN\n",
-			  (int)key.dsize, (int)key.dsize,
-			  (char *)key.dptr);
-		talloc_free(msg);
-		return -1;
-	}
-
-	ret = ldb_kv_index_onelevel(module, msg, 1);
-	if (ret != LDB_SUCCESS) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR,
-			  "Adding special ONE LEVEL index failed (%s)!",
-						ldb_dn_get_linearized(msg->dn));
-		talloc_free(msg);
-		return -1;
-	}
-
-	ret = ldb_kv_index_add_all(module, ldb_kv, msg);
-
-	if (ret != LDB_SUCCESS) {
-		ctx->error = ret;
-		talloc_free(msg);
-		return -1;
-	}
-
-	talloc_free(msg);
-
-	ctx->count++;
-	if (ctx->count % 10000 == 0) {
-		ldb_debug(ldb, LDB_DEBUG_WARNING,
-			  "Reindexing: re-indexed %u records so far",
-			  ctx->count);
-	}
-
-	return 0;
-}
-
-/*
-  force a complete reindex of the database
-*/
-int ldb_kv_reindex(struct ldb_module *module)
-{
-	struct ldb_kv_private *ldb_kv = talloc_get_type(
-	    ldb_module_get_private(module), struct ldb_kv_private);
-	int ret;
-	struct ldb_kv_reindex_context ctx;
-
-	/*
-	 * Only triggered after a modification, but make clear we do
-	 * not re-index a read-only DB
-	 */
-	if (ldb_kv->read_only) {
-		return LDB_ERR_UNWILLING_TO_PERFORM;
-	}
-
-	if (ldb_kv_cache_reload(module) != 0) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	/*
-	 * Ensure we read (and so remove) the entries from the real
-	 * DB, no values stored so far are any use as we want to do a
-	 * re-index
-	 */
-	ldb_kv_index_transaction_cancel(module);
-
-	ret = ldb_kv_index_transaction_start(module);
-	if (ret != LDB_SUCCESS) {
-		return ret;
-	}
-
-	/* first traverse the database deleting any @INDEX records by
-	 * putting NULL entries in the in-memory tdb
-	 */
-	ret = ldb_kv->kv_ops->iterate(ldb_kv, delete_index, module);
-	if (ret < 0) {
-		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb, "index deletion traverse failed: %s",
-				       ldb_errstring(ldb));
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ctx.module = module;
-	ctx.error = 0;
-	ctx.count = 0;
-
-	ret = ldb_kv->kv_ops->iterate(ldb_kv, re_key, &ctx);
-	if (ret < 0) {
-		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb, "key correction traverse failed: %s",
-				       ldb_errstring(ldb));
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	if (ctx.error != LDB_SUCCESS) {
-		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb, "reindexing failed: %s", ldb_errstring(ldb));
-		return ctx.error;
-	}
-
-	ctx.error = 0;
-	ctx.count = 0;
-
-	/* now traverse adding any indexes for normal LDB records */
-	ret = ldb_kv->kv_ops->iterate(ldb_kv, re_index, &ctx);
-	if (ret < 0) {
-		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb, "reindexing traverse failed: %s",
-				       ldb_errstring(ldb));
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	if (ctx.error != LDB_SUCCESS) {
-		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb, "reindexing failed: %s", ldb_errstring(ldb));
-		return ctx.error;
-	}
-
-	if (ctx.count > 10000) {
-		ldb_debug(ldb_module_get_ctx(module),
-			  LDB_DEBUG_WARNING,
-			  "Reindexing: re_index successful on %s, "
-			  "final index write-out will be in transaction commit",
-			  ldb_kv->kv_ops->name(ldb_kv));
-	}
-	return LDB_SUCCESS;
-}
diff --git a/lib/ldb/ldb_tdb/ldb_search.c b/lib/ldb/ldb_tdb/ldb_search.c
deleted file mode 100644
index 0138f92..0000000
--- a/lib/ldb/ldb_tdb/ldb_search.c
+++ /dev/null
@@ -1,866 +0,0 @@
-/* 
-   ldb database library
-
-   Copyright (C) Andrew Tridgell  2004
-
-     ** NOTE! The following LGPL license applies to the ldb
-     ** library. This does NOT imply that all of Samba is released
-     ** under the LGPL
-   
-   This library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Lesser General Public
-   License as published by the Free Software Foundation; either
-   version 3 of the License, or (at your option) any later version.
-
-   This library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Lesser General Public License for more details.
-
-   You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, see <http://www.gnu.org/licenses/>.
-*/
-
-/*
- *  Name: ldb
- *
- *  Component: ldb search functions
- *
- *  Description: functions to search ldb+tdb databases
- *
- *  Author: Andrew Tridgell
- */
-
-#include "ldb_tdb.h"
-#include "ldb_private.h"
-#include <tdb.h>
-
-/*
-  add one element to a message
-*/
-static int msg_add_element(struct ldb_message *ret, 
-			   const struct ldb_message_element *el,
-			   int check_duplicates)
-{
-	unsigned int i;
-	struct ldb_message_element *e2, *elnew;
-
-	if (check_duplicates && ldb_msg_find_element(ret, el->name)) {
-		/* its already there */
-		return 0;
-	}
-
-	e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
-	if (!e2) {
-		return -1;
-	}
-	ret->elements = e2;
-	
-	elnew = &e2[ret->num_elements];
-
-	elnew->name = talloc_strdup(ret->elements, el->name);
-	if (!elnew->name) {
-		return -1;
-	}
-
-	if (el->num_values) {
-		elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
-		if (!elnew->values) {
-			return -1;
-		}
-	} else {
-		elnew->values = NULL;
-	}
-
-	for (i=0;i<el->num_values;i++) {
-		elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
-		if (elnew->values[i].length != el->values[i].length) {
-			return -1;
-		}
-	}
-
-	elnew->num_values = el->num_values;
-	elnew->flags = el->flags;
-
-	ret->num_elements++;
-
-	return 0;
-}
-
-/*
-  add the special distinguishedName element
-*/
-static int msg_add_distinguished_name(struct ldb_message *msg)
-{
-	struct ldb_message_element el;
-	struct ldb_val val;
-	int ret;
-
-	el.flags = 0;
-	el.name = "distinguishedName";
-	el.num_values = 1;
-	el.values = &val;
-	el.flags = 0;
-	val.data = (uint8_t *)ldb_dn_alloc_linearized(msg, msg->dn);
-	if (val.data == NULL) {
-		return -1;
-	}
-	val.length = strlen((char *)val.data);
-
-	ret = msg_add_element(msg, &el, 1);
-	return ret;
-}
-
-/*
-  search the database for a single simple dn.
-  return LDB_ERR_NO_SUCH_OBJECT on record-not-found
-  and LDB_SUCCESS on success
-*/
-int ldb_kv_search_base(struct ldb_module *module,
-		       TALLOC_CTX *mem_ctx,
-		       struct ldb_dn *dn,
-		       struct ldb_dn **ret_dn)
-{
-	int exists;
-	int ret;
-	struct ldb_message *msg = NULL;
-
-	if (ldb_dn_is_null(dn)) {
-		return LDB_ERR_NO_SUCH_OBJECT;
-	}
-
-	/*
-	 * We can't use tdb_exists() directly on a key when the TDB
-	 * key is the GUID one, not the DN based one.  So we just do a
-	 * normal search and avoid most of the allocation with the
-	 * LDB_UNPACK_DATA_FLAG_NO_DN and
-	 * LDB_UNPACK_DATA_FLAG_NO_ATTRS flags
-	 */
-	msg = ldb_msg_new(module);
-	if (msg == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ret = ldb_kv_search_dn1(module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_ATTRS);
-	if (ret == LDB_SUCCESS) {
-		const char *dn_linearized
-			= ldb_dn_get_linearized(dn);
-		const char *msg_dn_linearlized
-			= ldb_dn_get_linearized(msg->dn);
-
-		if (strcmp(dn_linearized, msg_dn_linearlized) == 0) {
-			/*
-			 * Re-use the full incoming DN for
-			 * subtree checks
-			 */
-			*ret_dn = dn;
-		} else {
-			/*
-			 * Use the string DN from the unpack, so that
-			 * we have a case-exact match of the base
-			 */
-			*ret_dn = talloc_steal(mem_ctx, msg->dn);
-		}
-		exists = true;
-	} else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-		exists = false;
-	} else {
-		talloc_free(msg);
-		return ret;
-	}
-	talloc_free(msg);
-	if (exists) {
-		return LDB_SUCCESS;
-	}
-	return LDB_ERR_NO_SUCH_OBJECT;
-}
-
-struct ldb_kv_parse_data_unpack_ctx {
-	struct ldb_message *msg;
-	struct ldb_module *module;
-	unsigned int unpack_flags;
-};
-
-static int ldb_kv_parse_data_unpack(struct ldb_val key,
-				    struct ldb_val data,
-				    void *private_data)
-{
-	struct ldb_kv_parse_data_unpack_ctx *ctx = private_data;
-	unsigned int nb_elements_in_db;
-	int ret;
-	struct ldb_context *ldb = ldb_module_get_ctx(ctx->module);
-	struct ldb_val data_parse = data;
-
-	if (ctx->unpack_flags & LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC) {
-		/*
-		 * If we got LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC
-		 * we need at least do a memdup on the whole
-		 * data buffer as that may change later
-		 * and the caller needs a stable result.
-		 */
-		data_parse.data = talloc_memdup(ctx->msg,
-						data.data,
-						data.length);
-		if (data_parse.data == NULL) {
-			ldb_debug(ldb, LDB_DEBUG_ERROR,
-				  "Unable to allocate data(%d) for %*.*s\n",
-				  (int)data.length,
-				  (int)key.length, (int)key.length, key.data);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-	}
-
-	ret = ldb_unpack_data_only_attr_list_flags(ldb, &data_parse,
-						   ctx->msg,
-						   NULL, 0,
-						   ctx->unpack_flags,
-						   &nb_elements_in_db);
-	if (ret == -1) {
-		if (data_parse.data != data.data) {
-			talloc_free(data_parse.data);
-		}
-
-		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %*.*s\n",
-			  (int)key.length, (int)key.length, key.data);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	return ret;
-}
-
-/*
-  search the database for a single simple dn, returning all attributes
-  in a single message
-
-  return LDB_ERR_NO_SUCH_OBJECT on record-not-found
-  and LDB_SUCCESS on success
-*/
-int ldb_kv_search_key(struct ldb_module *module,
-		      struct ldb_kv_private *ldb_kv,
-		      const struct TDB_DATA tdb_key,
-		      struct ldb_message *msg,
-		      unsigned int unpack_flags)
-{
-	int ret;
-	struct ldb_kv_parse_data_unpack_ctx ctx = {
-		.msg = msg,
-		.module = module,
-		.unpack_flags = unpack_flags
-	};
-	struct ldb_val ldb_key = {
-		.data = tdb_key.dptr,
-		.length = tdb_key.dsize
-	};
-
-	memset(msg, 0, sizeof(*msg));
-
-	msg->num_elements = 0;
-	msg->elements = NULL;
-
-	ret = ldb_kv->kv_ops->fetch_and_parse(
-	    ldb_kv, ldb_key, ldb_kv_parse_data_unpack, &ctx);
-
-	if (ret == -1) {
-		ret = ldb_kv->kv_ops->error(ldb_kv);
-		if (ret == LDB_SUCCESS) {
-			/*
-			 * Just to be sure we don't turn errors
-			 * into success
-			 */
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-		return ret;
-	} else if (ret != LDB_SUCCESS) {
-		return ret;
-	}
-
-	return LDB_SUCCESS;
-}
-
-/*
-  search the database for a single simple dn, returning all attributes
-  in a single message
-
-  return LDB_ERR_NO_SUCH_OBJECT on record-not-found
-  and LDB_SUCCESS on success
-*/
-int ldb_kv_search_dn1(struct ldb_module *module,
-		      struct ldb_dn *dn,
-		      struct ldb_message *msg,
-		      unsigned int unpack_flags)
-{
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	int ret;
-	uint8_t guid_key[LTDB_GUID_KEY_SIZE];
-	TDB_DATA tdb_key = {
-		.dptr = guid_key,
-		.dsize = sizeof(guid_key)
-	};
-	TALLOC_CTX *tdb_key_ctx = NULL;
-
-	if (ldb_kv->cache->GUID_index_attribute == NULL ||
-	    ldb_dn_is_special(dn)) {
-
-		tdb_key_ctx = talloc_new(msg);
-		if (!tdb_key_ctx) {
-			return ldb_module_oom(module);
-		}
-
-		/* form the key */
-		tdb_key = ldb_kv_key_dn(module, tdb_key_ctx, dn);
-		if (!tdb_key.dptr) {
-			TALLOC_FREE(tdb_key_ctx);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-	} else {
-		/*
-		 * Look in the index to find the key for this DN.
-		 *
-		 * the tdb_key memory is allocated above, msg is just
-		 * used for internal memory.
-		 *
-		 */
-		ret = ldb_kv_key_dn_from_idx(module, ldb_kv, msg, dn, &tdb_key);
-		if (ret != LDB_SUCCESS) {
-			return ret;
-		}
-	}
-
-	ret = ldb_kv_search_key(module, ldb_kv, tdb_key, msg, unpack_flags);
-
-	TALLOC_FREE(tdb_key_ctx);
-
-	if (ret != LDB_SUCCESS) {
-		return ret;
-	}
-
-	if ((unpack_flags & LDB_UNPACK_DATA_FLAG_NO_DN) == 0) {
-		if (!msg->dn) {
-			msg->dn = ldb_dn_copy(msg, dn);
-		}
-		if (!msg->dn) {
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-	}
-
-	return LDB_SUCCESS;
-}
-
-/*
-  filter the specified list of attributes from a message
-  removing not requested attrs from the new message constructed.
-
-  The reason this makes a new message is that the old one may not be
-  individually allocated, which is what our callers expect.
-
- */
-int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
-			const struct ldb_message *msg,
-			const char *const *attrs,
-			struct ldb_message **filtered_msg)
-{
-	unsigned int i;
-	bool keep_all = false;
-	bool add_dn = false;
-	uint32_t num_elements;
-	uint32_t elements_size;
-	struct ldb_message *msg2;
-
-	msg2 = ldb_msg_new(mem_ctx);
-	if (msg2 == NULL) {
-		goto failed;
-	}
-
-	msg2->dn = ldb_dn_copy(msg2, msg->dn);
-	if (msg2->dn == NULL) {
-		goto failed;
-	}
-
-	if (attrs) {
-		/* check for special attrs */
-		for (i = 0; attrs[i]; i++) {
-			int cmp = strcmp(attrs[i], "*");
-			if (cmp == 0) {
-				keep_all = true;
-				break;
-			}
-			cmp = ldb_attr_cmp(attrs[i], "distinguishedName");
-			if (cmp == 0) {
-				add_dn = true;
-			}
-		}
-	} else {
-		keep_all = true;
-	}
-
-	if (keep_all) {
-		add_dn = true;
-		elements_size = msg->num_elements + 1;
-
-	/* Shortcuts for the simple cases */
-	} else if (add_dn && i == 1) {
-		if (msg_add_distinguished_name(msg2) != 0) {
-			goto failed;
-		}
-		*filtered_msg = msg2;
-		return 0;
-	} else if (i == 0) {
-		*filtered_msg = msg2;
-		return 0;
-
-	/* Otherwise we are copying at most as many element as we have attributes */
-	} else {
-		elements_size = i;
-	}
-
-	msg2->elements = talloc_array(msg2, struct ldb_message_element,
-				      elements_size);
-	if (msg2->elements == NULL) goto failed;
-
-	num_elements = 0;
-
-	for (i = 0; i < msg->num_elements; i++) {
-		struct ldb_message_element *el = &msg->elements[i];
-		struct ldb_message_element *el2 = &msg2->elements[num_elements];
-		unsigned int j;
-
-		if (keep_all == false) {
-			bool found = false;
-			for (j = 0; attrs[j]; j++) {
-				int cmp = ldb_attr_cmp(el->name, attrs[j]);
-				if (cmp == 0) {
-					found = true;
-					break;
-				}
-			}
-			if (found == false) {
-				continue;
-			}
-		}
-		*el2 = *el;
-		el2->name = talloc_strdup(msg2->elements, el->name);
-		if (el2->name == NULL) {
-			goto failed;
-		}
-		el2->values = talloc_array(msg2->elements, struct ldb_val, el->num_values);
-		if (el2->values == NULL) {
-			goto failed;
-		}
-		for (j=0;j<el->num_values;j++) {
-			el2->values[j] = ldb_val_dup(el2->values, &el->values[j]);
-			if (el2->values[j].data == NULL && el->values[j].length != 0) {
-				goto failed;
-			}
-		}
-		num_elements++;
-
-		/* Pidginhole principle: we can't have more elements
-		 * than the number of attributes if they are unique in
-		 * the DB */
-		if (num_elements > elements_size) {
-			goto failed;
-		}
-	}
-
-	msg2->num_elements = num_elements;
-
-	if (add_dn) {
-		if (msg_add_distinguished_name(msg2) != 0) {
-			goto failed;
-		}
-	}
-
-	if (msg2->num_elements > 0) {
-		msg2->elements = talloc_realloc(msg2, msg2->elements,
-						struct ldb_message_element,
-						msg2->num_elements);
-		if (msg2->elements == NULL) {
-			goto failed;
-		}
-	} else {
-		talloc_free(msg2->elements);
-		msg2->elements = NULL;
-	}
-
-	*filtered_msg = msg2;
-
-	return 0;
-failed:
-	TALLOC_FREE(msg2);
-	return -1;
-}
-
-/*
-  search function for a non-indexed search
- */
-static int search_func(struct ldb_kv_private *ldb_kv,
-		       struct ldb_val key,
-		       struct ldb_val val,
-		       void *state)
-{
-	struct ldb_context *ldb;
-	struct ldb_kv_context *ac;
-	struct ldb_message *msg, *filtered_msg;
-	int ret;
-	bool matched;
-	unsigned int nb_elements_in_db;
-	TDB_DATA tdb_key = {
-		.dptr = key.data,
-		.dsize = key.length
-	};
-
-	ac = talloc_get_type(state, struct ldb_kv_context);
-	ldb = ldb_module_get_ctx(ac->module);
-
-	if (ldb_kv_key_is_record(tdb_key) == false) {
-		return 0;
-	}
-
-	msg = ldb_msg_new(ac);
-	if (!msg) {
-		ac->error = LDB_ERR_OPERATIONS_ERROR;
-		return -1;
-	}
-
-	/* unpack the record */
-	ret = ldb_unpack_data_only_attr_list_flags(ldb, &val,
-						   msg,
-						   NULL, 0,
-						   LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC|
-						   LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC,
-						   &nb_elements_in_db);
-	if (ret == -1) {
-		talloc_free(msg);
-		ac->error = LDB_ERR_OPERATIONS_ERROR;
-		return -1;
-	}
-
-	if (!msg->dn) {
-		msg->dn = ldb_dn_new(msg, ldb,
-				     (char *)key.data + 3);
-		if (msg->dn == NULL) {
-			talloc_free(msg);
-			ac->error = LDB_ERR_OPERATIONS_ERROR;
-			return -1;
-		}
-	}
-
-	/* see if it matches the given expression */
-	ret = ldb_match_msg_error(ldb, msg,
-				  ac->tree, ac->base, ac->scope, &matched);
-	if (ret != LDB_SUCCESS) {
-		talloc_free(msg);
-		ac->error = LDB_ERR_OPERATIONS_ERROR;
-		return -1;
-	}
-	if (!matched) {
-		talloc_free(msg);
-		return 0;
-	}
-
-	/* filter the attributes that the user wants */
-	ret = ldb_kv_filter_attrs(ac, msg, ac->attrs, &filtered_msg);
-	talloc_free(msg);
-
-	if (ret == -1) {
-		ac->error = LDB_ERR_OPERATIONS_ERROR;
-		return -1;
-	}
-
-	ret = ldb_module_send_entry(ac->req, filtered_msg, NULL);
-	if (ret != LDB_SUCCESS) {
-		ac->request_terminated = true;
-		/* the callback failed, abort the operation */
-		ac->error = LDB_ERR_OPERATIONS_ERROR;
-		return -1;
-	}
-
-	return 0;
-}
-
-
-/*
-  search the database with a LDAP-like expression.
-  this is the "full search" non-indexed variant
-*/
-static int ldb_kv_search_full(struct ldb_kv_context *ctx)
-{
-	void *data = ldb_module_get_private(ctx->module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	int ret;
-
-	ctx->error = LDB_SUCCESS;
-	ret = ldb_kv->kv_ops->iterate(ldb_kv, search_func, ctx);
-
-	if (ret < 0) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	return ctx->error;
-}
-
-static int ldb_kv_search_and_return_base(struct ldb_kv_private *ldb_kv,
-					 struct ldb_kv_context *ctx)
-{
-	struct ldb_message *msg, *filtered_msg;
-	struct ldb_context *ldb = ldb_module_get_ctx(ctx->module);
-	const char *dn_linearized;
-	const char *msg_dn_linearlized;
-	int ret;
-	bool matched;
-
-	msg = ldb_msg_new(ctx);
-	if (!msg) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	ret = ldb_kv_search_dn1(ctx->module,
-				ctx->base,
-				msg,
-				LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
-				    LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC);
-
-	if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-		if (ldb_kv->check_base == false) {
-			/*
-			 * In this case, we are done, as no base
-			 * checking is allowed in this DB
-			 */
-			talloc_free(msg);
-			return LDB_SUCCESS;
-		}
-		ldb_asprintf_errstring(ldb,
-				       "No such Base DN: %s",
-				       ldb_dn_get_linearized(ctx->base));
-	}
-	if (ret != LDB_SUCCESS) {
-		talloc_free(msg);
-		return ret;
-	}
-
-
-	/*
-	 * We use this, not ldb_match_msg_error() as we know
-	 * we matched on the scope BASE, as we just fetched
-	 * the base DN
-	 */
-
-	ret = ldb_match_message(ldb, msg,
-				ctx->tree,
-				ctx->scope,
-				&matched);
-	if (ret != LDB_SUCCESS) {
-		talloc_free(msg);
-		return ret;
-	}
-	if (!matched) {
-		talloc_free(msg);
-		return LDB_SUCCESS;
-	}
-
-	dn_linearized = ldb_dn_get_linearized(ctx->base);
-	msg_dn_linearlized = ldb_dn_get_linearized(msg->dn);
-
-	if (strcmp(dn_linearized, msg_dn_linearlized) == 0) {
-		/*
-		 * If the DN is exactly the same string, then
-		 * re-use the full incoming DN for the
-		 * returned result, as it has already been
-		 * casefolded
-		 */
-		msg->dn = ctx->base;
-	}
-
-	/*
-	 * filter the attributes that the user wants.
-	 *
-	 * This copies msg->dn including the casefolding, so the above
-	 * assignment is safe
-	 */
-	ret = ldb_kv_filter_attrs(ctx, msg, ctx->attrs, &filtered_msg);
-
-	/*
-	 * Remove any extended components possibly copied in from
-	 * msg->dn, we just want the casefold components
-	 */
-	ldb_dn_remove_extended_components(filtered_msg->dn);
-	talloc_free(msg);
-
-	if (ret == -1) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ret = ldb_module_send_entry(ctx->req, filtered_msg, NULL);
-	if (ret != LDB_SUCCESS) {
-		/* Regardless of success or failure, the msg
-		 * is the callbacks responsiblity, and should
-		 * not be talloc_free()'ed */
-		ctx->request_terminated = true;
-		return ret;
-	}
-
-	return LDB_SUCCESS;
-}
-
-/*
-  search the database with a LDAP-like expression.
-  choses a search method
-*/
-int ldb_kv_search(struct ldb_kv_context *ctx)
-{
-	struct ldb_context *ldb;
-	struct ldb_module *module = ctx->module;
-	struct ldb_request *req = ctx->req;
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	int ret;
-
-	ldb = ldb_module_get_ctx(module);
-
-	ldb_request_set_state(req, LDB_ASYNC_PENDING);
-
-	if (ldb_kv->kv_ops->lock_read(module) != 0) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	if (ldb_kv_cache_load(module) != 0) {
-		ldb_kv->kv_ops->unlock_read(module);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	if (req->op.search.tree == NULL) {
-		ldb_kv->kv_ops->unlock_read(module);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ctx->tree = req->op.search.tree;
-	ctx->scope = req->op.search.scope;
-	ctx->base = req->op.search.base;
-	ctx->attrs = req->op.search.attrs;
-
-	if ((req->op.search.base == NULL) || (ldb_dn_is_null(req->op.search.base) == true)) {
-
-		/* Check what we should do with a NULL dn */
-		switch (req->op.search.scope) {
-		case LDB_SCOPE_BASE:
-			ldb_asprintf_errstring(ldb, 
-					       "NULL Base DN invalid for a base search");
-			ret = LDB_ERR_INVALID_DN_SYNTAX;
-			break;
-		case LDB_SCOPE_ONELEVEL:
-			ldb_asprintf_errstring(ldb, 
-					       "NULL Base DN invalid for a one-level search");
-			ret = LDB_ERR_INVALID_DN_SYNTAX;	
-			break;
-		case LDB_SCOPE_SUBTREE:
-		default:
-			/* We accept subtree searches from a NULL base DN, ie over the whole DB */
-			ret = LDB_SUCCESS;
-		}
-	} else if (ldb_dn_is_valid(req->op.search.base) == false) {
-
-		/* We don't want invalid base DNs here */
-		ldb_asprintf_errstring(ldb, 
-				       "Invalid Base DN: %s", 
-				       ldb_dn_get_linearized(req->op.search.base));
-		ret = LDB_ERR_INVALID_DN_SYNTAX;
-
-	} else if (req->op.search.scope == LDB_SCOPE_BASE) {
-
-		/*
-		 * If we are LDB_SCOPE_BASE, do just one search and
-		 * return early.  This is critical to ensure we do not
-		 * go into the index code for special DNs, as that
-		 * will try to look up an index record for a special
-		 * record (which doesn't exist).
-		 */
-		ret = ldb_kv_search_and_return_base(ldb_kv, ctx);
-
-		ldb_kv->kv_ops->unlock_read(module);
-
-		return ret;
-
-	} else if (ldb_kv->check_base) {
-		/*
-		 * This database has been marked as
-		 * 'checkBaseOnSearch', so do a spot check of the base
-		 * dn.  Also optimise the subsequent filter by filling
-		 * in the ctx->base to be exactly case correct
-		 */
-		ret = ldb_kv_search_base(
-		    module, ctx, req->op.search.base, &ctx->base);
-
-		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-			ldb_asprintf_errstring(ldb, 
-					       "No such Base DN: %s", 
-					       ldb_dn_get_linearized(req->op.search.base));
-		}
-			
-	} else {
-		/* If we are not checking the base DN life is easy */
-		ret = LDB_SUCCESS;
-	}
-
-	if (ret == LDB_SUCCESS) {
-		uint32_t match_count = 0;
-
-		ret = ldb_kv_search_indexed(ctx, &match_count);
-		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-			/* Not in the index, therefore OK! */
-			ret = LDB_SUCCESS;
-			
-		}
-		/* Check if we got just a normal error.
-		 * In that case proceed to a full search unless we got a
-		 * callback error */
-		if ( ! ctx->request_terminated && ret != LDB_SUCCESS) {
-			/* Not indexed, so we need to do a full scan */
-			if (ldb_kv->warn_unindexed ||
-			    ldb_kv->disable_full_db_scan) {
-				/* useful for debugging when slow performance
-				 * is caused by unindexed searches */
-				char *expression = ldb_filter_from_tree(ctx, ctx->tree);
-				ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb FULL SEARCH: %s SCOPE: %s DN: %s",
-							expression,
-							req->op.search.scope==LDB_SCOPE_BASE?"base":
-							req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
-							req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN",
-							ldb_dn_get_linearized(req->op.search.base));
-
-				talloc_free(expression);
-			}
-
-			if (match_count != 0) {
-				/* the indexing code gave an error
-				 * after having returned at least one
-				 * entry. This means the indexes are
-				 * corrupt or a database record is
-				 * corrupt. We cannot continue with a
-				 * full search or we may return
-				 * duplicate entries
-				 */
-				ldb_kv->kv_ops->unlock_read(module);
-				return LDB_ERR_OPERATIONS_ERROR;
-			}
-
-			if (ldb_kv->disable_full_db_scan) {
-				ldb_set_errstring(ldb,
-						  "ldb FULL SEARCH disabled");
-				ldb_kv->kv_ops->unlock_read(module);
-				return LDB_ERR_INAPPROPRIATE_MATCHING;
-			}
-
-			ret = ldb_kv_search_full(ctx);
-			if (ret != LDB_SUCCESS) {
-				ldb_set_errstring(ldb, "Indexed and full searches both failed!\n");
-			}
-		}
-	}
-
-	ldb_kv->kv_ops->unlock_read(module);
-
-	return ret;
-}
-
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.c b/lib/ldb/ldb_tdb/ldb_tdb.c
index 9519090..812ddd3 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.c
+++ b/lib/ldb/ldb_tdb/ldb_tdb.c
@@ -51,47 +51,10 @@
 
 #include "ldb_tdb.h"
 #include "ldb_private.h"
+#include "../ldb_key_value/ldb_kv.h"
 #include <tdb.h>
 
 /*
-  prevent memory errors on callbacks
-*/
-struct ldb_kv_req_spy {
-	struct ldb_kv_context *ctx;
-};
-
-/*
-  map a tdb error code to a ldb error code
-*/
-int ltdb_err_map(enum TDB_ERROR tdb_code)
-{
-	switch (tdb_code) {
-	case TDB_SUCCESS:
-		return LDB_SUCCESS;
-	case TDB_ERR_CORRUPT:
-	case TDB_ERR_OOM:
-	case TDB_ERR_EINVAL:
-		return LDB_ERR_OPERATIONS_ERROR;
-	case TDB_ERR_IO:
-		return LDB_ERR_PROTOCOL_ERROR;
-	case TDB_ERR_LOCK:
-	case TDB_ERR_NOLOCK:
-		return LDB_ERR_BUSY;
-	case TDB_ERR_LOCK_TIMEOUT:
-		return LDB_ERR_TIME_LIMIT_EXCEEDED;
-	case TDB_ERR_EXISTS:
-		return LDB_ERR_ENTRY_ALREADY_EXISTS;
-	case TDB_ERR_NOEXIST:
-		return LDB_ERR_NO_SUCH_OBJECT;
-	case TDB_ERR_RDONLY:
-		return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
-	default:
-		break;
-	}
-	return LDB_ERR_OTHER;
-}
-
-/*
   lock the database for read - use by ltdb_search and ltdb_sequence_number
 */
 static int ltdb_lock_read(struct ldb_module *module)
@@ -162,292 +125,6 @@ static int ltdb_unlock_read(struct ldb_module *module)
 	return 0;
 }
 
-
-/* 
- * Determine if this key could hold a record.  We allow the new GUID
- * index, the old DN index and a possible future ID=
- */
-bool ldb_kv_key_is_record(TDB_DATA key)
-{
-	if (key.dsize < 4) {
-		return false;
-	}
-
-	if (memcmp(key.dptr, "DN=", 3) == 0) {
-		return true;
-	}
-	
-	if (memcmp(key.dptr, "ID=", 3) == 0) {
-		return true;
-	}
-
-	if (key.dsize < sizeof(LTDB_GUID_KEY_PREFIX)) {
-		return false;
-	}
-
-	if (memcmp(key.dptr, LTDB_GUID_KEY_PREFIX,
-		   sizeof(LTDB_GUID_KEY_PREFIX) - 1) == 0) {
-		return true;
-	}
-	
-	return false;
-}
-
-/*
-  form a TDB_DATA for a record key
-  caller frees
-
-  note that the key for a record can depend on whether the
-  dn refers to a case sensitive index record or not
-*/
-TDB_DATA ldb_kv_key_dn(struct ldb_module *module,
-		       TALLOC_CTX *mem_ctx,
-		       struct ldb_dn *dn)
-{
-	TDB_DATA key;
-	char *key_str = NULL;
-	const char *dn_folded = NULL;
-
-	/*
-	  most DNs are case insensitive. The exception is index DNs for
-	  case sensitive attributes
-
-	  there are 3 cases dealt with in this code:
-
-	  1) if the dn doesn't start with @ then uppercase the attribute
-             names and the attributes values of case insensitive attributes
-	  2) if the dn starts with @ then leave it alone -
-	     the indexing code handles the rest
-	*/
-
-	dn_folded = ldb_dn_get_casefold(dn);
-	if (!dn_folded) {
-		goto failed;
-	}
-
-	key_str = talloc_strdup(mem_ctx, "DN=");
-	if (!key_str) {
-		goto failed;
-	}
-
-	key_str = talloc_strdup_append_buffer(key_str, dn_folded);
-	if (!key_str) {
-		goto failed;
-	}
-
-	key.dptr = (uint8_t *)key_str;
-	key.dsize = strlen(key_str) + 1;
-
-	return key;
-
-failed:
-	errno = ENOMEM;
-	key.dptr = NULL;
-	key.dsize = 0;
-	return key;
-}
-
-/* The caller is to provide a correctly sized key */
-int ldb_kv_guid_to_key(struct ldb_module *module,
-		       struct ldb_kv_private *ldb_kv,
-		       const struct ldb_val *GUID_val,
-		       TDB_DATA *key)
-{
-	const char *GUID_prefix = LTDB_GUID_KEY_PREFIX;
-	const int GUID_prefix_len = sizeof(LTDB_GUID_KEY_PREFIX) - 1;
-
-	if (key->dsize != (GUID_val->length+GUID_prefix_len)) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	memcpy(key->dptr, GUID_prefix, GUID_prefix_len);
-	memcpy(&key->dptr[GUID_prefix_len],
-	       GUID_val->data, GUID_val->length);
-	return LDB_SUCCESS;
-}
-
-/*
- * The caller is to provide a correctly sized key, used only in
- * the GUID index mode
- */
-int ldb_kv_idx_to_key(struct ldb_module *module,
-		      struct ldb_kv_private *ldb_kv,
-		      TALLOC_CTX *mem_ctx,
-		      const struct ldb_val *idx_val,
-		      TDB_DATA *key)
-{
-	struct ldb_context *ldb = ldb_module_get_ctx(module);
-	struct ldb_dn *dn;
-
-	if (ldb_kv->cache->GUID_index_attribute != NULL) {
-		return ldb_kv_guid_to_key(module, ldb_kv, idx_val, key);
-	}
-
-	dn = ldb_dn_from_ldb_val(mem_ctx, ldb, idx_val);
-	if (dn == NULL) {
-		/*
-		 * LDB_ERR_INVALID_DN_SYNTAX would just be confusing
-		 * to the caller, as this in an invalid index value
-		 */
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	/* form the key */
-	*key = ldb_kv_key_dn(module, mem_ctx, dn);
-	TALLOC_FREE(dn);
-	if (!key->dptr) {
-		return ldb_module_oom(module);
-	}
-	return LDB_SUCCESS;
-}
-
-/*
-  form a TDB_DATA for a record key
-  caller frees mem_ctx, which may or may not have the key
-  as a child.
-
-  note that the key for a record can depend on whether a
-  GUID index is in use, or the DN is used as the key
-*/
-TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
-			TALLOC_CTX *mem_ctx,
-			const struct ldb_message *msg)
-{
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	TDB_DATA key;
-	const struct ldb_val *guid_val;
-	int ret;
-
-	if (ldb_kv->cache->GUID_index_attribute == NULL) {
-		return ldb_kv_key_dn(module, mem_ctx, msg->dn);
-	}
-
-	if (ldb_dn_is_special(msg->dn)) {
-		return ldb_kv_key_dn(module, mem_ctx, msg->dn);
-	}
-
-	guid_val =
-	    ldb_msg_find_ldb_val(msg, ldb_kv->cache->GUID_index_attribute);
-	if (guid_val == NULL) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       "Did not find GUID attribute %s "
-				       "in %s, required for TDB record "
-				       "key in " LTDB_IDXGUID " mode.",
-				       ldb_kv->cache->GUID_index_attribute,
-				       ldb_dn_get_linearized(msg->dn));
-		errno = EINVAL;
-		key.dptr = NULL;
-		key.dsize = 0;
-		return key;
-	}
-
-	/* In this case, allocate with talloc */
-	key.dptr = talloc_size(mem_ctx, LTDB_GUID_KEY_SIZE);
-	if (key.dptr == NULL) {
-		errno = ENOMEM;
-		key.dptr = NULL;
-		key.dsize = 0;
-		return key;
-	}
-	key.dsize = talloc_get_size(key.dptr);
-
-	ret = ldb_kv_guid_to_key(module, ldb_kv, guid_val, &key);
-
-	if (ret != LDB_SUCCESS) {
-		errno = EINVAL;
-		key.dptr = NULL;
-		key.dsize = 0;
-		return key;
-	}
-	return key;
-}
-
-/*
-  check special dn's have valid attributes
-  currently only @ATTRIBUTES is checked
-*/
-static int ldb_kv_check_special_dn(struct ldb_module *module,
-				   const struct ldb_message *msg)
-{
-	struct ldb_context *ldb = ldb_module_get_ctx(module);
-	unsigned int i, j;
-
-	if (! ldb_dn_is_special(msg->dn) ||
-	    ! ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
-		return LDB_SUCCESS;
-	}
-
-	/* we have @ATTRIBUTES, let's check attributes are fine */
-	/* should we check that we deny multivalued attributes ? */
-	for (i = 0; i < msg->num_elements; i++) {
-		if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) continue;
-
-		for (j = 0; j < msg->elements[i].num_values; j++) {
-			if (ldb_kv_check_at_attributes_values(
-				&msg->elements[i].values[j]) != 0) {
-				ldb_set_errstring(ldb, "Invalid attribute value in an @ATTRIBUTES entry");
-				return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
-			}
-		}
-	}
-
-	return LDB_SUCCESS;
-}
-
-
-/*
-  we've made a modification to a dn - possibly reindex and
-  update sequence number
-*/
-static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
-{
-	int ret = LDB_SUCCESS;
-	struct ldb_kv_private *ldb_kv = talloc_get_type(
-	    ldb_module_get_private(module), struct ldb_kv_private);
-
-	/* only allow modifies inside a transaction, otherwise the
-	 * ldb is unsafe */
-	if (ldb_kv->kv_ops->transaction_active(ldb_kv) == false) {
-		ldb_set_errstring(ldb_module_get_ctx(module), "ltdb modify without transaction");
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	if (ldb_dn_is_special(dn) &&
-	    (ldb_dn_check_special(dn, LTDB_INDEXLIST) ||
-	     ldb_dn_check_special(dn, LTDB_ATTRIBUTES)) )
-	{
-		if (ldb_kv->warn_reindex) {
-			ldb_debug(ldb_module_get_ctx(module),
-				  LDB_DEBUG_ERROR,
-				  "Reindexing %s due to modification on %s",
-				  ldb_kv->kv_ops->name(ldb_kv),
-				  ldb_dn_get_linearized(dn));
-		}
-		ret = ldb_kv_reindex(module);
-	}
-
-	/* If the modify was to a normal record, or any special except @BASEINFO, update the seq number */
-	if (ret == LDB_SUCCESS &&
-	    !(ldb_dn_is_special(dn) &&
-	      ldb_dn_check_special(dn, LTDB_BASEINFO)) ) {
-		ret = ldb_kv_increase_sequence_number(module);
-	}
-
-	/* If the modify was to @OPTIONS, reload the cache */
-	if (ret == LDB_SUCCESS &&
-	    ldb_dn_is_special(dn) &&
-	    (ldb_dn_check_special(dn, LTDB_OPTIONS)) ) {
-		ret = ldb_kv_cache_reload(module);
-	}
-
-	if (ret != LDB_SUCCESS) {
-		ldb_kv->reindex_failed = true;
-	}
-
-	return ret;
-}
-
 static int ltdb_store(struct ldb_kv_private *ldb_kv,
 		      struct ldb_val ldb_key,
 		      struct ldb_val ldb_data,
@@ -477,1050 +154,21 @@ static const char *ltdb_errorstr(struct ldb_kv_private *ldb_kv)
 {
 	return tdb_errorstr(ldb_kv->tdb);
 }
-
-/*
-  store a record into the db
-*/
-int ldb_kv_store(struct ldb_module *module,
-		 const struct ldb_message *msg,
-		 int flgs)
-{
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	TDB_DATA tdb_key;
-	struct ldb_val ldb_key;
-	struct ldb_val ldb_data;
-	int ret = LDB_SUCCESS;
-	TALLOC_CTX *key_ctx = talloc_new(module);
-
-	if (key_ctx == NULL) {
-		return ldb_module_oom(module);
-	}
-
-	if (ldb_kv->read_only) {
-		talloc_free(key_ctx);
-		return LDB_ERR_UNWILLING_TO_PERFORM;
-	}
-
-	tdb_key = ldb_kv_key_msg(module, key_ctx, msg);
-	if (tdb_key.dptr == NULL) {
-		TALLOC_FREE(key_ctx);
-		return LDB_ERR_OTHER;
-	}
-
-	ret = ldb_pack_data(ldb_module_get_ctx(module),
-			    msg, &ldb_data);
-	if (ret == -1) {
-		TALLOC_FREE(key_ctx);
-		return LDB_ERR_OTHER;
-	}
-
-	ldb_key.data = tdb_key.dptr;
-	ldb_key.length = tdb_key.dsize;
-
-	ret = ldb_kv->kv_ops->store(ldb_kv, ldb_key, ldb_data, flgs);
-	if (ret != 0) {
-		bool is_special = ldb_dn_is_special(msg->dn);
-		ret = ldb_kv->kv_ops->error(ldb_kv);
-
-		/*
-		 * LDB_ERR_ENTRY_ALREADY_EXISTS means the DN, not
-		 * the GUID, so re-map
-		 */
-		if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS && !is_special &&
-		    ldb_kv->cache->GUID_index_attribute != NULL) {
-			ret = LDB_ERR_CONSTRAINT_VIOLATION;
-		}
-		goto done;
-	}
-
-done:
-	TALLOC_FREE(key_ctx);
-	talloc_free(ldb_data.data);
-
-	return ret;
-}
-
-
-/*
-  check if a attribute is a single valued, for a given element
- */
-static bool ldb_kv_single_valued(const struct ldb_schema_attribute *a,
-				 struct ldb_message_element *el)
-{
-	if (!a) return false;
-	if (el != NULL) {
-		if (el->flags & LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK) {
-			/* override from a ldb module, for example
-			   used for the description field, which is
-			   marked multi-valued in the schema but which
-			   should not actually accept multiple
-			   values */
-			return true;
-		}
-		if (el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK) {
-			/* override from a ldb module, for example used for
-			   deleted linked attribute entries */
-			return false;
-		}
-	}
-	if (a->flags & LDB_ATTR_FLAG_SINGLE_VALUE) {
-		return true;
-	}
-	return false;
-}
-
-static int ldb_kv_add_internal(struct ldb_module *module,
-			       struct ldb_kv_private *ldb_kv,
-			       const struct ldb_message *msg,
-			       bool check_single_value)
-{
-	struct ldb_context *ldb = ldb_module_get_ctx(module);
-	int ret = LDB_SUCCESS;
-	unsigned int i;
-
-	for (i=0;i<msg->num_elements;i++) {
-		struct ldb_message_element *el = &msg->elements[i];
-		const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
-
-		if (el->num_values == 0) {
-			ldb_asprintf_errstring(ldb, "attribute '%s' on '%s' specified, but with 0 values (illegal)",
-					       el->name, ldb_dn_get_linearized(msg->dn));
-			return LDB_ERR_CONSTRAINT_VIOLATION;
-		}
-		if (check_single_value && el->num_values > 1 &&
-		    ldb_kv_single_valued(a, el)) {
-			ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
-					       el->name, ldb_dn_get_linearized(msg->dn));
-			return LDB_ERR_CONSTRAINT_VIOLATION;
-		}
-
-		/* Do not check "@ATTRIBUTES" for duplicated values */
-		if (ldb_dn_is_special(msg->dn) &&
-		    ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
-			continue;
-		}
-
-		if (check_single_value &&
-		    !(el->flags &
-		      LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
-			struct ldb_val *duplicate = NULL;
-
-			ret = ldb_msg_find_duplicate_val(ldb, discard_const(msg),
-							 el, &duplicate, 0);
-			if (ret != LDB_SUCCESS) {
-				return ret;
-			}
-			if (duplicate != NULL) {
-				ldb_asprintf_errstring(
-					ldb,
-					"attribute '%s': value '%.*s' on '%s' "
-					"provided more than once in ADD object",
-					el->name,
-					(int)duplicate->length,
-					duplicate->data,
-					ldb_dn_get_linearized(msg->dn));
-				return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
-			}
-		}
-	}
-
-	ret = ldb_kv_store(module, msg, TDB_INSERT);
-	if (ret != LDB_SUCCESS) {
-		/*
-		 * Try really hard to get the right error code for
-		 * a re-add situation, as this can matter!
-		 */
-		if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
-			int ret2;
-			struct ldb_dn *dn2 = NULL;
-			TALLOC_CTX *mem_ctx = talloc_new(module);
-			if (mem_ctx == NULL) {
-				return ldb_module_operr(module);
-			}
-			ret2 =
-			    ldb_kv_search_base(module, mem_ctx, msg->dn, &dn2);
-			TALLOC_FREE(mem_ctx);
-			if (ret2 == LDB_SUCCESS) {
-				ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
-			}
-		}
-		if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
-			ldb_asprintf_errstring(ldb,
-					       "Entry %s already exists",
-					       ldb_dn_get_linearized(msg->dn));
-		}
-		return ret;
-	}
-
-	ret = ldb_kv_index_add_new(module, ldb_kv, msg);
-	if (ret != LDB_SUCCESS) {
-		/*
-		 * If we failed to index, delete the message again.
-		 *
-		 * This is particularly important for the GUID index
-		 * case, which will only fail for a duplicate DN
-		 * in the index add.
-		 *
-		 * Note that the caller may not cancel the transation
-		 * and this means the above add might really show up!
-		 */
-		ldb_kv_delete_noindex(module, msg);
-		return ret;
-	}
-
-	ret = ldb_kv_modified(module, msg->dn);
-
-	return ret;
-}
-
-/*
-  add a record to the database
-*/
-static int ldb_kv_add(struct ldb_kv_context *ctx)
-{
-	struct ldb_module *module = ctx->module;
-	struct ldb_request *req = ctx->req;
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	int ret = LDB_SUCCESS;
-
-	if (ldb_kv->max_key_length != 0 &&
-	    ldb_kv->cache->GUID_index_attribute == NULL &&
-	    !ldb_dn_is_special(req->op.add.message->dn)) {
-		ldb_set_errstring(ldb_module_get_ctx(module),
-				  "Must operate ldb_mdb in GUID "
-				  "index mode, but " LTDB_IDXGUID " not set.");
-		return LDB_ERR_UNWILLING_TO_PERFORM;
-	}
-
-	ret = ldb_kv_check_special_dn(module, req->op.add.message);
-	if (ret != LDB_SUCCESS) {
-		return ret;
-	}
-
-	ldb_request_set_state(req, LDB_ASYNC_PENDING);
-
-	if (ldb_kv_cache_load(module) != 0) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ret = ldb_kv_add_internal(module, ldb_kv, req->op.add.message, true);
-
-	return ret;
-}
-
-static int ltdb_delete(struct ldb_kv_private *ldb_kv, struct ldb_val ldb_key)
-{
-	TDB_DATA tdb_key = {
-		.dptr = ldb_key.data,
-		.dsize = ldb_key.length
-	};
-	bool transaction_active = tdb_transaction_active(ldb_kv->tdb);
-	if (transaction_active == false){
-		return LDB_ERR_PROTOCOL_ERROR;
-	}
-	return tdb_delete(ldb_kv->tdb, tdb_key);
-}
-
-/*
-  delete a record from the database, not updating indexes (used for deleting
-  index records)
-*/
-int ldb_kv_delete_noindex(struct ldb_module *module,
-			  const struct ldb_message *msg)
-{
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	struct ldb_val ldb_key;
-	TDB_DATA tdb_key;
-	int ret;
-	TALLOC_CTX *tdb_key_ctx = talloc_new(module);
-
-	if (tdb_key_ctx == NULL) {
-		return ldb_module_oom(module);
-	}
-
-	if (ldb_kv->read_only) {
-		talloc_free(tdb_key_ctx);
-		return LDB_ERR_UNWILLING_TO_PERFORM;
-	}
-
-	tdb_key = ldb_kv_key_msg(module, tdb_key_ctx, msg);
-	if (!tdb_key.dptr) {
-		TALLOC_FREE(tdb_key_ctx);
-		return LDB_ERR_OTHER;
-	}
-
-	ldb_key.data = tdb_key.dptr;
-	ldb_key.length = tdb_key.dsize;
-
-	ret = ldb_kv->kv_ops->delete (ldb_kv, ldb_key);
-	TALLOC_FREE(tdb_key_ctx);
-
-	if (ret != 0) {
-		ret = ldb_kv->kv_ops->error(ldb_kv);
-	}
-
-	return ret;
-}
-
-static int ldb_kv_delete_internal(struct ldb_module *module, struct ldb_dn *dn)
-{
-	struct ldb_message *msg;
-	int ret = LDB_SUCCESS;
-
-	msg = ldb_msg_new(module);
-	if (msg == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	/* in case any attribute of the message was indexed, we need
-	   to fetch the old record */
-	ret = ldb_kv_search_dn1(
-	    module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
-	if (ret != LDB_SUCCESS) {
-		/* not finding the old record is an error */
-		goto done;
-	}
-
-	ret = ldb_kv_delete_noindex(module, msg);
-	if (ret != LDB_SUCCESS) {
-		goto done;
-	}
-
-	/* remove any indexed attributes */
-	ret = ldb_kv_index_delete(module, msg);
-	if (ret != LDB_SUCCESS) {
-		goto done;
-	}
-
-	ret = ldb_kv_modified(module, dn);
-	if (ret != LDB_SUCCESS) {
-		goto done;
-	}
-
-done:
-	talloc_free(msg);
-	return ret;
-}
-
-/*
-  delete a record from the database
-*/
-static int ldb_kv_delete(struct ldb_kv_context *ctx)
-{
-	struct ldb_module *module = ctx->module;
-	struct ldb_request *req = ctx->req;
-	int ret = LDB_SUCCESS;
-
-	ldb_request_set_state(req, LDB_ASYNC_PENDING);
-
-	if (ldb_kv_cache_load(module) != 0) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ret = ldb_kv_delete_internal(module, req->op.del.dn);
-
-	return ret;
-}
-
-/*
-  find an element by attribute name. At the moment this does a linear search,
-  it should be re-coded to use a binary search once all places that modify
-  records guarantee sorted order
-
-  return the index of the first matching element if found, otherwise -1
-*/
-static int ldb_kv_find_element(const struct ldb_message *msg, const char *name)
-{
-	unsigned int i;
-	for (i=0;i<msg->num_elements;i++) {
-		if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
-			return i;
-		}
-	}
-	return -1;
-}
-
-
-/*
-  add an element to an existing record. Assumes a elements array that we
-  can call re-alloc on, and assumed that we can re-use the data pointers from
-  the passed in additional values. Use with care!
-
-  returns 0 on success, -1 on failure (and sets errno)
-*/
-static int ldb_kv_msg_add_element(struct ldb_message *msg,
-				  struct ldb_message_element *el)
-{
-	struct ldb_message_element *e2;
-	unsigned int i;
-
-	if (el->num_values == 0) {
-		/* nothing to do here - we don't add empty elements */
-		return 0;
-	}
-
-	e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
-			      msg->num_elements+1);
-	if (!e2) {
-		errno = ENOMEM;
-		return -1;
-	}
-
-	msg->elements = e2;
-
-	e2 = &msg->elements[msg->num_elements];
-
-	e2->name = el->name;
-	e2->flags = el->flags;
-	e2->values = talloc_array(msg->elements,
-				  struct ldb_val, el->num_values);
-	if (!e2->values) {
-		errno = ENOMEM;
-		return -1;
-	}
-	for (i=0;i<el->num_values;i++) {
-		e2->values[i] = el->values[i];
-	}
-	e2->num_values = el->num_values;
-
-	++msg->num_elements;
-
-	return 0;
-}
-
-/*
-  delete all elements having a specified attribute name
-*/
-static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
-				       struct ldb_kv_private *ldb_kv,
-				       struct ldb_message *msg,
-				       const char *name)
-{
-	unsigned int i;
-	int ret;
-	struct ldb_message_element *el;
-	bool is_special = ldb_dn_is_special(msg->dn);
-
-	if (!is_special && ldb_kv->cache->GUID_index_attribute != NULL &&
-	    ldb_attr_cmp(name, ldb_kv->cache->GUID_index_attribute) == 0) {
-		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb,
-				       "Must not modify GUID "
-				       "attribute %s (used as DB index)",
-				       ldb_kv->cache->GUID_index_attribute);
-		return LDB_ERR_CONSTRAINT_VIOLATION;
-	}
-
-	el = ldb_msg_find_element(msg, name);
-	if (el == NULL) {
-		return LDB_ERR_NO_SUCH_ATTRIBUTE;
-	}
-	i = el - msg->elements;
-
-	ret = ldb_kv_index_del_element(module, ldb_kv, msg, el);
-	if (ret != LDB_SUCCESS) {
-		return ret;
-	}
-
-	talloc_free(el->values);
-	if (msg->num_elements > (i+1)) {
-		memmove(el, el+1, sizeof(*el) * (msg->num_elements - (i+1)));
-	}
-	msg->num_elements--;
-	msg->elements = talloc_realloc(msg, msg->elements,
-				       struct ldb_message_element,
-				       msg->num_elements);
-	return LDB_SUCCESS;
-}
-
-/*
-  delete all elements matching an attribute name/value
-
-  return LDB Error on failure
-*/
-static int ldb_kv_msg_delete_element(struct ldb_module *module,
-				     struct ldb_kv_private *ldb_kv,
-				     struct ldb_message *msg,
-				     const char *name,
-				     const struct ldb_val *val)
-{
-	struct ldb_context *ldb = ldb_module_get_ctx(module);
-	unsigned int i;
-	int found, ret;
-	struct ldb_message_element *el;
-	const struct ldb_schema_attribute *a;
-
-	found = ldb_kv_find_element(msg, name);
-	if (found == -1) {
-		return LDB_ERR_NO_SUCH_ATTRIBUTE;
-	}
-
-	i = (unsigned int) found;
-	el = &(msg->elements[i]);
-
-	a = ldb_schema_attribute_by_name(ldb, el->name);
-
-	for (i=0;i<el->num_values;i++) {
-		bool matched;
-		if (a->syntax->operator_fn) {
-			ret = a->syntax->operator_fn(ldb, LDB_OP_EQUALITY, a,
-						     &el->values[i], val, &matched);
-			if (ret != LDB_SUCCESS) return ret;
-		} else {
-			matched = (a->syntax->comparison_fn(ldb, ldb,
-							    &el->values[i], val) == 0);
-		}
-		if (matched) {
-			if (el->num_values == 1) {
-				return ldb_kv_msg_delete_attribute(
-				    module, ldb_kv, msg, name);
-			}
-
-			ret =
-			    ldb_kv_index_del_value(module, ldb_kv, msg, el, i);
-			if (ret != LDB_SUCCESS) {
-				return ret;
-			}
-
-			if (i<el->num_values-1) {
-				memmove(&el->values[i], &el->values[i+1],
-					sizeof(el->values[i])*
-						(el->num_values-(i+1)));
-			}
-			el->num_values--;
-
-			/* per definition we find in a canonicalised message an
-			   attribute value only once. So we are finished here */
-			return LDB_SUCCESS;
-		}
-	}
-
-	/* Not found */
-	return LDB_ERR_NO_SUCH_ATTRIBUTE;
-}
-
-/*
-  modify a record - internal interface
-
-  yuck - this is O(n^2). Luckily n is usually small so we probably
-  get away with it, but if we ever have really large attribute lists
-  then we'll need to look at this again
-
-  'req' is optional, and is used to specify controls if supplied
-*/
-int ldb_kv_modify_internal(struct ldb_module *module,
-			   const struct ldb_message *msg,
-			   struct ldb_request *req)
-{
-	struct ldb_context *ldb = ldb_module_get_ctx(module);
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	struct ldb_message *msg2;
-	unsigned int i, j;
-	int ret = LDB_SUCCESS, idx;
-	struct ldb_control *control_permissive = NULL;
-	TALLOC_CTX *mem_ctx = talloc_new(req);
-
-	if (mem_ctx == NULL) {
-		return ldb_module_oom(module);
-	}
-	
-	if (req) {
-		control_permissive = ldb_request_get_control(req,
-					LDB_CONTROL_PERMISSIVE_MODIFY_OID);
-	}
-
-	msg2 = ldb_msg_new(mem_ctx);
-	if (msg2 == NULL) {
-		ret = LDB_ERR_OTHER;
-		goto done;
-	}
-
-	ret = ldb_kv_search_dn1(
-	    module, msg->dn, msg2, LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
-	if (ret != LDB_SUCCESS) {
-		goto done;
-	}
-
-	for (i=0; i<msg->num_elements; i++) {
-		struct ldb_message_element *el = &msg->elements[i], *el2;
-		struct ldb_val *vals;
-		const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
-		const char *dn;
-		uint32_t options = 0;
-		if (control_permissive != NULL) {
-			options |= LDB_MSG_FIND_COMMON_REMOVE_DUPLICATES;
-		}
-
-		switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
-		case LDB_FLAG_MOD_ADD:
-
-			if (el->num_values == 0) {
-				ldb_asprintf_errstring(ldb,
-						       "attribute '%s': attribute on '%s' specified, but with 0 values (illegal)",
-						       el->name, ldb_dn_get_linearized(msg2->dn));
-				ret = LDB_ERR_CONSTRAINT_VIOLATION;
-				goto done;
-			}
-
-			/* make a copy of the array so that a permissive
-			 * control can remove duplicates without changing the
-			 * original values, but do not copy data as we do not
-			 * need to keep it around once the operation is
-			 * finished */
-			if (control_permissive) {
-				el = talloc(msg2, struct ldb_message_element);
-				if (!el) {
-					ret = LDB_ERR_OTHER;
-					goto done;
-				}
-				*el = msg->elements[i];
-				el->values = talloc_array(el, struct ldb_val, el->num_values);
-				if (el->values == NULL) {
-					ret = LDB_ERR_OTHER;
-					goto done;
-				}
-				for (j = 0; j < el->num_values; j++) {
-					el->values[j] = msg->elements[i].values[j];
-				}
-			}
-
-			if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
-				ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
-						       el->name, ldb_dn_get_linearized(msg2->dn));
-				ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
-				goto done;
-			}
-
-			/* Checks if element already exists */
-			idx = ldb_kv_find_element(msg2, el->name);
-			if (idx == -1) {
-				if (ldb_kv_msg_add_element(msg2, el) != 0) {
-					ret = LDB_ERR_OTHER;
-					goto done;
-				}
-				ret = ldb_kv_index_add_element(
-				    module, ldb_kv, msg2, el);
-				if (ret != LDB_SUCCESS) {
-					goto done;
-				}
-			} else {
-				j = (unsigned int) idx;
-				el2 = &(msg2->elements[j]);
-
-				/* We cannot add another value on a existing one
-				   if the attribute is single-valued */
-				if (ldb_kv_single_valued(a, el)) {
-					ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
-						               el->name, ldb_dn_get_linearized(msg2->dn));
-					ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
-					goto done;
-				}
-
-				/* Check that values don't exist yet on multi-
-				   valued attributes or aren't provided twice */
-				if (!(el->flags &
-				      LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
-					struct ldb_val *duplicate = NULL;
-					ret = ldb_msg_find_common_values(ldb,
-									 msg2,
-									 el,
-									 el2,
-									 options);
-
-					if (ret ==
-					    LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
-						ldb_asprintf_errstring(ldb,
-							"attribute '%s': value "
-							"#%u on '%s' already "
-							"exists", el->name, j,
-							ldb_dn_get_linearized(msg2->dn));
-						goto done;
-					} else if (ret != LDB_SUCCESS) {
-						goto done;
-					}
-
-					ret = ldb_msg_find_duplicate_val(
-						ldb, msg2, el, &duplicate, 0);
-					if (ret != LDB_SUCCESS) {
-						goto done;
-					}
-					if (duplicate != NULL) {
-						ldb_asprintf_errstring(
-							ldb,
-							"attribute '%s': value "
-							"'%.*s' on '%s' "
-							"provided more than "
-							"once in ADD",
-							el->name,
-							(int)duplicate->length,
-							duplicate->data,
-							ldb_dn_get_linearized(msg->dn));
-						ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
-						goto done;
-					}
-				}
-
-				/* Now combine existing and new values to a new
-				   attribute record */
-				vals = talloc_realloc(msg2->elements,
-						      el2->values, struct ldb_val,
-						      el2->num_values + el->num_values);
-				if (vals == NULL) {
-					ldb_oom(ldb);
-					ret = LDB_ERR_OTHER;
-					goto done;
-				}
-
-				for (j=0; j<el->num_values; j++) {
-					vals[el2->num_values + j] =
-						ldb_val_dup(vals, &el->values[j]);
-				}
-
-				el2->values = vals;
-				el2->num_values += el->num_values;
-
-				ret = ldb_kv_index_add_element(
-				    module, ldb_kv, msg2, el);
-				if (ret != LDB_SUCCESS) {
-					goto done;
-				}
-			}
-
-			break;
-
-		case LDB_FLAG_MOD_REPLACE:
-
-			if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
-				ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
-						       el->name, ldb_dn_get_linearized(msg2->dn));
-				ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
-				goto done;
-			}
-
-			/*
-			 * We don't need to check this if we have been
-			 * pre-screened by the repl_meta_data module
-			 * in Samba, or someone else who can claim to
-			 * know what they are doing. 
-			 */
-			if (!(el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
-				struct ldb_val *duplicate = NULL;
-
-				ret = ldb_msg_find_duplicate_val(ldb, msg2, el,
-								 &duplicate, 0);
-				if (ret != LDB_SUCCESS) {
-					goto done;
-				}
-				if (duplicate != NULL) {
-					ldb_asprintf_errstring(
-						ldb,
-						"attribute '%s': value '%.*s' "
-						"on '%s' provided more than "
-						"once in REPLACE",
-						el->name,
-						(int)duplicate->length,
-						duplicate->data,
-						ldb_dn_get_linearized(msg2->dn));
-					ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
-					goto done;
-				}
-			}
-
-			/* Checks if element already exists */
-			idx = ldb_kv_find_element(msg2, el->name);
-			if (idx != -1) {
-				j = (unsigned int) idx;
-				el2 = &(msg2->elements[j]);
-
-				/* we consider two elements to be
-				 * equal only if the order
-				 * matches. This allows dbcheck to
-				 * fix the ordering on attributes
-				 * where order matters, such as
-				 * objectClass
-				 */
-				if (ldb_msg_element_equal_ordered(el, el2)) {
-					continue;
-				}
-
-				/* Delete the attribute if it exists in the DB */
-				if (ldb_kv_msg_delete_attribute(
-					module, ldb_kv, msg2, el->name) != 0) {
-					ret = LDB_ERR_OTHER;
-					goto done;
-				}
-			}
-
-			/* Recreate it with the new values */
-			if (ldb_kv_msg_add_element(msg2, el) != 0) {
-				ret = LDB_ERR_OTHER;
-				goto done;
-			}
-
-			ret =
-			    ldb_kv_index_add_element(module, ldb_kv, msg2, el);
-			if (ret != LDB_SUCCESS) {
-				goto done;
-			}
-
-			break;
-
-		case LDB_FLAG_MOD_DELETE:
-			dn = ldb_dn_get_linearized(msg2->dn);
-			if (dn == NULL) {
-				ret = LDB_ERR_OTHER;
-				goto done;
-			}
-
-			if (msg->elements[i].num_values == 0) {
-				/* Delete the whole attribute */
-				ret = ldb_kv_msg_delete_attribute(
-				    module,
-				    ldb_kv,
-				    msg2,
-				    msg->elements[i].name);
-				if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
-				    control_permissive) {
-					ret = LDB_SUCCESS;
-				} else {
-					ldb_asprintf_errstring(ldb,
-							       "attribute '%s': no such attribute for delete on '%s'",
-							       msg->elements[i].name, dn);
-				}
-				if (ret != LDB_SUCCESS) {
-					goto done;
-				}
-			} else {
-				/* Delete specified values from an attribute */
-				for (j=0; j < msg->elements[i].num_values; j++) {
-					ret = ldb_kv_msg_delete_element(
-					    module,
-					    ldb_kv,
-					    msg2,
-					    msg->elements[i].name,
-					    &msg->elements[i].values[j]);
-					if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
-					    control_permissive) {
-						ret = LDB_SUCCESS;
-					} else if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
-						ldb_asprintf_errstring(ldb,
-								       "attribute '%s': no matching attribute value while deleting attribute on '%s'",
-								       msg->elements[i].name, dn);
-					}
-					if (ret != LDB_SUCCESS) {
-						goto done;
-					}
-				}
-			}
-			break;
-		default:
-			ldb_asprintf_errstring(ldb,
-					       "attribute '%s': invalid modify flags on '%s': 0x%x",
-					       msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
-					       msg->elements[i].flags & LDB_FLAG_MOD_MASK);
-			ret = LDB_ERR_PROTOCOL_ERROR;
-			goto done;
-		}
-	}
-
-	ret = ldb_kv_store(module, msg2, TDB_MODIFY);
-	if (ret != LDB_SUCCESS) {
-		goto done;
-	}
-
-	ret = ldb_kv_modified(module, msg2->dn);
-	if (ret != LDB_SUCCESS) {
-		goto done;
-	}
-
-done:
-	TALLOC_FREE(mem_ctx);
-	return ret;
-}
-
-/*
-  modify a record
-*/
-static int ldb_kv_modify(struct ldb_kv_context *ctx)
-{
-	struct ldb_module *module = ctx->module;
-	struct ldb_request *req = ctx->req;
-	int ret = LDB_SUCCESS;
-
-	ret = ldb_kv_check_special_dn(module, req->op.mod.message);
-	if (ret != LDB_SUCCESS) {
-		return ret;
-	}
-
-	ldb_request_set_state(req, LDB_ASYNC_PENDING);
-
-	if (ldb_kv_cache_load(module) != 0) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ret = ldb_kv_modify_internal(module, req->op.mod.message, req);
-
-	return ret;
-}
-
-/*
-  rename a record
-*/
-static int ldb_kv_rename(struct ldb_kv_context *ctx)
-{
-	struct ldb_module *module = ctx->module;
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	struct ldb_request *req = ctx->req;
-	struct ldb_message *msg;
-	int ret = LDB_SUCCESS;
-	TDB_DATA tdb_key, tdb_key_old;
-	struct ldb_dn *db_dn;
-
-	ldb_request_set_state(req, LDB_ASYNC_PENDING);
-
-	if (ldb_kv_cache_load(ctx->module) != 0) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	msg = ldb_msg_new(ctx);
-	if (msg == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	/* we need to fetch the old record to re-add under the new name */
-	ret = ldb_kv_search_dn1(module,
-				req->op.rename.olddn,
-				msg,
-				LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC);
-	if (ret != LDB_SUCCESS) {
-		/* not finding the old record is an error */
-		return ret;
-	}
-
-	/* We need to, before changing the DB, check if the new DN
-	 * exists, so we can return this error to the caller with an
-	 * unmodified DB
-	 *
-	 * Even in GUID index mode we use ltdb_key_dn() as we are
-	 * trying to figure out if this is just a case rename
-	 */
-	tdb_key = ldb_kv_key_dn(module, msg, req->op.rename.newdn);
-	if (!tdb_key.dptr) {
-		talloc_free(msg);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	tdb_key_old = ldb_kv_key_dn(module, msg, req->op.rename.olddn);
-	if (!tdb_key_old.dptr) {
-		talloc_free(msg);
-		talloc_free(tdb_key.dptr);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	/*
-	 * Only declare a conflict if the new DN already exists,
-	 * and it isn't a case change on the old DN
-	 */
-	if (tdb_key_old.dsize != tdb_key.dsize
-	    || memcmp(tdb_key.dptr, tdb_key_old.dptr, tdb_key.dsize) != 0) {
-		ret = ldb_kv_search_base(
-		    module, msg, req->op.rename.newdn, &db_dn);
-		if (ret == LDB_SUCCESS) {
-			ret = LDB_ERR_ENTRY_ALREADY_EXISTS;
-		} else if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-			ret = LDB_SUCCESS;
-		}
-	}
-
-	/* finding the new record already in the DB is an error */
-
-	if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       "Entry %s already exists",
-				       ldb_dn_get_linearized(req->op.rename.newdn));
-	}
-	if (ret != LDB_SUCCESS) {
-		talloc_free(tdb_key_old.dptr);
-		talloc_free(tdb_key.dptr);
-		talloc_free(msg);
-		return ret;
-	}
-
-	talloc_free(tdb_key_old.dptr);
-	talloc_free(tdb_key.dptr);
-
-	/* Always delete first then add, to avoid conflicts with
-	 * unique indexes. We rely on the transaction to make this
-	 * atomic
-	 */
-	ret = ldb_kv_delete_internal(module, msg->dn);
-	if (ret != LDB_SUCCESS) {
-		talloc_free(msg);
-		return ret;
-	}
-
-	msg->dn = ldb_dn_copy(msg, req->op.rename.newdn);
-	if (msg->dn == NULL) {
-		talloc_free(msg);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	/* We don't check single value as we can have more than 1 with
-	 * deleted attributes. We could go through all elements but that's
-	 * maybe not the most efficient way
-	 */
-	ret = ldb_kv_add_internal(module, ldb_kv, msg, false);
-
-	talloc_free(msg);
-
-	return ret;
-}
-
-static int ltdb_transaction_start(struct ldb_kv_private *ldb_kv)
-{
-	pid_t pid = getpid();
-
-	if (ldb_kv->pid != pid) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
-				       __location__
-				       ": Reusing ldb opend by pid %d in "
-				       "process %d\n",
-				       ldb_kv->pid,
-				       pid);
-		return LDB_ERR_PROTOCOL_ERROR;
-	}
-
-	return tdb_transaction_start(ldb_kv->tdb);
-}
-
-static int ltdb_transaction_cancel(struct ldb_kv_private *ldb_kv)
-{
-	pid_t pid = getpid();
-
-	if (ldb_kv->pid != pid) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
-				       __location__
-				       ": Reusing ldb opend by pid %d in "
-				       "process %d\n",
-				       ldb_kv->pid,
-				       pid);
+
+static int ltdb_delete(struct ldb_kv_private *ldb_kv, struct ldb_val ldb_key)
+{
+	TDB_DATA tdb_key = {
+		.dptr = ldb_key.data,
+		.dsize = ldb_key.length
+	};
+	bool transaction_active = tdb_transaction_active(ldb_kv->tdb);
+	if (transaction_active == false){
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
-
-	return tdb_transaction_cancel(ldb_kv->tdb);
+	return tdb_delete(ldb_kv->tdb, tdb_key);
 }
 
-static int ltdb_transaction_prepare_commit(struct ldb_kv_private *ldb_kv)
+static int ltdb_transaction_start(struct ldb_kv_private *ldb_kv)
 {
 	pid_t pid = getpid();
 
@@ -1534,10 +182,10 @@ static int ltdb_transaction_prepare_commit(struct ldb_kv_private *ldb_kv)
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	return tdb_transaction_prepare_commit(ldb_kv->tdb);
+	return tdb_transaction_start(ldb_kv->tdb);
 }
 
-static int ltdb_transaction_commit(struct ldb_kv_private *ldb_kv)
+static int ltdb_transaction_cancel(struct ldb_kv_private *ldb_kv)
 {
 	pid_t pid = getpid();
 
@@ -1551,15 +199,11 @@ static int ltdb_transaction_commit(struct ldb_kv_private *ldb_kv)
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	return tdb_transaction_commit(ldb_kv->tdb);
+	return tdb_transaction_cancel(ldb_kv->tdb);
 }
 
-static int ldb_kv_start_trans(struct ldb_module *module)
+static int ltdb_transaction_prepare_commit(struct ldb_kv_private *ldb_kv)
 {
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-
 	pid_t pid = getpid();
 
 	if (ldb_kv->pid != pid) {
@@ -1572,38 +216,15 @@ static int ldb_kv_start_trans(struct ldb_module *module)
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	/* Do not take out the transaction lock on a read-only DB */
-	if (ldb_kv->read_only) {
-		return LDB_ERR_UNWILLING_TO_PERFORM;
-	}
-
-	if (ldb_kv->kv_ops->begin_write(ldb_kv) != 0) {
-		return ldb_kv->kv_ops->error(ldb_kv);
-	}
-
-	ldb_kv_index_transaction_start(module);
-
-	ldb_kv->reindex_failed = false;
-
-	return LDB_SUCCESS;
+	return tdb_transaction_prepare_commit(ldb_kv->tdb);
 }
 
-/*
- * Forward declaration to allow prepare_commit to in fact abort the
- * transaction
- */
-static int ldb_kv_del_trans(struct ldb_module *module);
-
-static int ldb_kv_prepare_commit(struct ldb_module *module)
+static int ltdb_transaction_commit(struct ldb_kv_private *ldb_kv)
 {
-	int ret;
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
 	pid_t pid = getpid();
 
 	if (ldb_kv->pid != pid) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
+		ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv->module),
 				       __location__
 				       ": Reusing ldb opend by pid %d in "
 				       "process %d\n",
@@ -1612,287 +233,8 @@ static int ldb_kv_prepare_commit(struct ldb_module *module)
 		return LDB_ERR_PROTOCOL_ERROR;
 	}
 
-	if (!ldb_kv->kv_ops->transaction_active(ldb_kv)) {
-		ldb_set_errstring(ldb_module_get_ctx(module),
-				  "ltdb_prepare_commit() called "
-				  "without transaction active");
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	/*
-	 * Check if the last re-index failed.
-	 *
-	 * This can happen if for example a duplicate value was marked
-	 * unique.  We must not write a partial re-index into the DB.
-	 */
-	if (ldb_kv->reindex_failed) {
-		/*
-		 * We must instead abort the transaction so we get the
-		 * old values and old index back
-		 */
-		ldb_kv_del_trans(module);
-		ldb_set_errstring(ldb_module_get_ctx(module),
-				  "Failure during re-index, so "
-				  "transaction must be aborted.");
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ret = ldb_kv_index_transaction_commit(module);
-	if (ret != LDB_SUCCESS) {
-		ldb_kv->kv_ops->abort_write(ldb_kv);
-		return ret;
-	}
-
-	if (ldb_kv->kv_ops->prepare_write(ldb_kv) != 0) {
-		ret = ldb_kv->kv_ops->error(ldb_kv);
-		ldb_debug_set(ldb_module_get_ctx(module),
-			      LDB_DEBUG_FATAL,
-			      "Failure during "
-			      "prepare_write): %s -> %s",
-			      ldb_kv->kv_ops->errorstr(ldb_kv),
-			      ldb_strerror(ret));
-		return ret;
-	}
-
-	ldb_kv->prepared_commit = true;
-
-	return LDB_SUCCESS;
-}
-
-static int ldb_kv_end_trans(struct ldb_module *module)
-{
-	int ret;
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-
-	if (!ldb_kv->prepared_commit) {
-		ret = ldb_kv_prepare_commit(module);
-		if (ret != LDB_SUCCESS) {
-			return ret;
-		}
-	}
-
-	ldb_kv->prepared_commit = false;
-
-	if (ldb_kv->kv_ops->finish_write(ldb_kv) != 0) {
-		ret = ldb_kv->kv_ops->error(ldb_kv);
-		ldb_asprintf_errstring(
-		    ldb_module_get_ctx(module),
-		    "Failure during tdb_transaction_commit(): %s -> %s",
-		    ldb_kv->kv_ops->errorstr(ldb_kv),
-		    ldb_strerror(ret));
-		return ret;
-	}
-
-	return LDB_SUCCESS;
-}
-
-static int ldb_kv_del_trans(struct ldb_module *module)
-{
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-
-	if (ldb_kv_index_transaction_cancel(module) != 0) {
-		ldb_kv->kv_ops->abort_write(ldb_kv);
-		return ldb_kv->kv_ops->error(ldb_kv);
-	}
-
-	ldb_kv->kv_ops->abort_write(ldb_kv);
-	return LDB_SUCCESS;
-}
-
-/*
-  return sequenceNumber from @BASEINFO
-*/
-static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
-				  struct ldb_extended **ext)
-{
-	struct ldb_context *ldb;
-	struct ldb_module *module = ctx->module;
-	struct ldb_request *req = ctx->req;
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	TALLOC_CTX *tmp_ctx = NULL;
-	struct ldb_seqnum_request *seq;
-	struct ldb_seqnum_result *res;
-	struct ldb_message *msg = NULL;
-	struct ldb_dn *dn;
-	const char *date;
-	int ret = LDB_SUCCESS;
-
-	ldb = ldb_module_get_ctx(module);
-
-	seq = talloc_get_type(req->op.extended.data,
-				struct ldb_seqnum_request);
-	if (seq == NULL) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ldb_request_set_state(req, LDB_ASYNC_PENDING);
-
-	if (ldb_kv->kv_ops->lock_read(module) != 0) {
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	res = talloc_zero(req, struct ldb_seqnum_result);
-	if (res == NULL) {
-		ret = LDB_ERR_OPERATIONS_ERROR;
-		goto done;
-	}
-
-	tmp_ctx = talloc_new(req);
-	if (tmp_ctx == NULL) {
-		ret = LDB_ERR_OPERATIONS_ERROR;
-		goto done;
-	}
-
-	dn = ldb_dn_new(tmp_ctx, ldb, LTDB_BASEINFO);
-	if (dn == NULL) {
-		ret = LDB_ERR_OPERATIONS_ERROR;
-		goto done;
-	}
-
-	msg = ldb_msg_new(tmp_ctx);
-	if (msg == NULL) {
-		ret = LDB_ERR_OPERATIONS_ERROR;
-		goto done;
-	}
-
-	ret = ldb_kv_search_dn1(module, dn, msg, 0);
-	if (ret != LDB_SUCCESS) {
-		goto done;
-	}
-
-	switch (seq->type) {
-	case LDB_SEQ_HIGHEST_SEQ:
-		res->seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
-		break;
-	case LDB_SEQ_NEXT:
-		res->seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
-		res->seq_num++;
-		break;
-	case LDB_SEQ_HIGHEST_TIMESTAMP:
-		date = ldb_msg_find_attr_as_string(msg, LTDB_MOD_TIMESTAMP, NULL);
-		if (date) {
-			res->seq_num = ldb_string_to_time(date);
-		} else {
-			res->seq_num = 0;
-			/* zero is as good as anything when we don't know */
-		}
-		break;
-	}
-
-	*ext = talloc_zero(req, struct ldb_extended);
-	if (*ext == NULL) {
-		ret = LDB_ERR_OPERATIONS_ERROR;
-		goto done;
-	}
-	(*ext)->oid = LDB_EXTENDED_SEQUENCE_NUMBER;
-	(*ext)->data = talloc_steal(*ext, res);
-
-done:
-	talloc_free(tmp_ctx);
-
-	ldb_kv->kv_ops->unlock_read(module);
-	return ret;
-}
-
-static void ldb_kv_request_done(struct ldb_kv_context *ctx, int error)
-{
-	struct ldb_context *ldb;
-	struct ldb_request *req;
-	struct ldb_reply *ares;
-
-	ldb = ldb_module_get_ctx(ctx->module);
-	req = ctx->req;
-
-	/* if we already returned an error just return */
-	if (ldb_request_get_status(req) != LDB_SUCCESS) {
-		return;
-	}
-
-	ares = talloc_zero(req, struct ldb_reply);
-	if (!ares) {
-		ldb_oom(ldb);
-		req->callback(req, NULL);
-		return;
-	}
-	ares->type = LDB_REPLY_DONE;
-	ares->error = error;
-
-	req->callback(req, ares);
-}
-
-static void ldb_kv_timeout(struct tevent_context *ev,
-			   struct tevent_timer *te,
-			   struct timeval t,
-			   void *private_data)
-{
-	struct ldb_kv_context *ctx;
-	ctx = talloc_get_type(private_data, struct ldb_kv_context);
-
-	if (!ctx->request_terminated) {
-		/* request is done now */
-		ldb_kv_request_done(ctx, LDB_ERR_TIME_LIMIT_EXCEEDED);
-	}
-
-	if (ctx->spy) {
-		/* neutralize the spy */
-		ctx->spy->ctx = NULL;
-		ctx->spy = NULL;
-	}
-	talloc_free(ctx);
-}
-
-static void ldb_kv_request_extended_done(struct ldb_kv_context *ctx,
-					 struct ldb_extended *ext,
-					 int error)
-{
-	struct ldb_context *ldb;
-	struct ldb_request *req;
-	struct ldb_reply *ares;
-
-	ldb = ldb_module_get_ctx(ctx->module);
-	req = ctx->req;
-
-	/* if we already returned an error just return */
-	if (ldb_request_get_status(req) != LDB_SUCCESS) {
-		return;
-	}
-
-	ares = talloc_zero(req, struct ldb_reply);
-	if (!ares) {
-		ldb_oom(ldb);
-		req->callback(req, NULL);
-		return;
-	}
-	ares->type = LDB_REPLY_DONE;
-	ares->response = ext;
-	ares->error = error;
-
-	req->callback(req, ares);
-}
-
-static void ldb_kv_handle_extended(struct ldb_kv_context *ctx)
-{
-	struct ldb_extended *ext = NULL;
-	int ret;
-
-	if (strcmp(ctx->req->op.extended.oid,
-		   LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
-		/* get sequence number */
-		ret = ldb_kv_sequence_number(ctx, &ext);
-	} else {
-		/* not recognized */
-		ret = LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
-	}
-
-	ldb_kv_request_extended_done(ctx, ext, ret);
+	return tdb_transaction_commit(ldb_kv->tdb);
 }
-
 struct kv_ctx {
 	ldb_kv_traverse_fn kv_traverse_fn;
 	void *ctx;
@@ -2077,261 +419,6 @@ static const struct kv_db_ops key_value_ops = {
     .transaction_active = ltdb_transaction_active,
 };
 
-static void ldb_kv_callback(struct tevent_context *ev,
-			    struct tevent_timer *te,
-			    struct timeval t,
-			    void *private_data)
-{
-	struct ldb_kv_context *ctx;
-	int ret;
-
-	ctx = talloc_get_type(private_data, struct ldb_kv_context);
-
-	if (ctx->request_terminated) {
-		goto done;
-	}
-
-	switch (ctx->req->operation) {
-	case LDB_SEARCH:
-		ret = ldb_kv_search(ctx);
-		break;
-	case LDB_ADD:
-		ret = ldb_kv_add(ctx);
-		break;
-	case LDB_MODIFY:
-		ret = ldb_kv_modify(ctx);
-		break;
-	case LDB_DELETE:
-		ret = ldb_kv_delete(ctx);
-		break;
-	case LDB_RENAME:
-		ret = ldb_kv_rename(ctx);
-		break;
-	case LDB_EXTENDED:
-		ldb_kv_handle_extended(ctx);
-		goto done;
-	default:
-		/* no other op supported */
-		ret = LDB_ERR_PROTOCOL_ERROR;
-	}
-
-	if (!ctx->request_terminated) {
-		/* request is done now */
-		ldb_kv_request_done(ctx, ret);
-	}
-
-done:
-	if (ctx->spy) {
-		/* neutralize the spy */
-		ctx->spy->ctx = NULL;
-		ctx->spy = NULL;
-	}
-	talloc_free(ctx);
-}
-
-static int ldb_kv_request_destructor(void *ptr)
-{
-	struct ldb_kv_req_spy *spy =
-	    talloc_get_type(ptr, struct ldb_kv_req_spy);
-
-	if (spy->ctx != NULL) {
-		spy->ctx->spy = NULL;
-		spy->ctx->request_terminated = true;
-		spy->ctx = NULL;
-	}
-
-	return 0;
-}
-
-static int ldb_kv_handle_request(struct ldb_module *module,
-				 struct ldb_request *req)
-{
-	struct ldb_control *control_permissive;
-	struct ldb_context *ldb;
-	struct tevent_context *ev;
-	struct ldb_kv_context *ac;
-	struct tevent_timer *te;
-	struct timeval tv;
-	unsigned int i;
-
-	ldb = ldb_module_get_ctx(module);
-
-	control_permissive = ldb_request_get_control(req,
-					LDB_CONTROL_PERMISSIVE_MODIFY_OID);
-
-	for (i = 0; req->controls && req->controls[i]; i++) {
-		if (req->controls[i]->critical &&
-		    req->controls[i] != control_permissive) {
-			ldb_asprintf_errstring(ldb, "Unsupported critical extension %s",
-					       req->controls[i]->oid);
-			return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
-		}
-	}
-
-	if (req->starttime == 0 || req->timeout == 0) {
-		ldb_set_errstring(ldb, "Invalid timeout settings");
-		return LDB_ERR_TIME_LIMIT_EXCEEDED;
-	}
-
-	ev = ldb_handle_get_event_context(req->handle);
-
-	ac = talloc_zero(ldb, struct ldb_kv_context);
-	if (ac == NULL) {
-		ldb_oom(ldb);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	ac->module = module;
-	ac->req = req;
-
-	tv.tv_sec = 0;
-	tv.tv_usec = 0;
-	te = tevent_add_timer(ev, ac, tv, ldb_kv_callback, ac);
-	if (NULL == te) {
-		talloc_free(ac);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	if (req->timeout > 0) {
-		tv.tv_sec = req->starttime + req->timeout;
-		tv.tv_usec = 0;
-		ac->timeout_event =
-		    tevent_add_timer(ev, ac, tv, ldb_kv_timeout, ac);
-		if (NULL == ac->timeout_event) {
-			talloc_free(ac);
-			return LDB_ERR_OPERATIONS_ERROR;
-		}
-	}
-
-	/* set a spy so that we do not try to use the request context
-	 * if it is freed before ltdb_callback fires */
-	ac->spy = talloc(req, struct ldb_kv_req_spy);
-	if (NULL == ac->spy) {
-		talloc_free(ac);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	ac->spy->ctx = ac;
-
-	talloc_set_destructor((TALLOC_CTX *)ac->spy, ldb_kv_request_destructor);
-
-	return LDB_SUCCESS;
-}
-
-static int ldb_kv_init_rootdse(struct ldb_module *module)
-{
-	/* ignore errors on this - we expect it for non-sam databases */
-	ldb_mod_register_control(module, LDB_CONTROL_PERMISSIVE_MODIFY_OID);
-
-	/* there can be no module beyond the backend, just return */
-	return LDB_SUCCESS;
-}
-
-static int ldb_kv_lock_read(struct ldb_module *module)
-{
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	return ldb_kv->kv_ops->lock_read(module);
-}
-
-static int ldb_kv_unlock_read(struct ldb_module *module)
-{
-	void *data = ldb_module_get_private(module);
-	struct ldb_kv_private *ldb_kv =
-	    talloc_get_type(data, struct ldb_kv_private);
-	return ldb_kv->kv_ops->unlock_read(module);
-}
-
-static const struct ldb_module_ops ldb_kv_ops = {
-    .name = "tdb",
-    .init_context = ldb_kv_init_rootdse,
-    .search = ldb_kv_handle_request,
-    .add = ldb_kv_handle_request,
-    .modify = ldb_kv_handle_request,
-    .del = ldb_kv_handle_request,
-    .rename = ldb_kv_handle_request,
-    .extended = ldb_kv_handle_request,
-    .start_transaction = ldb_kv_start_trans,
-    .end_transaction = ldb_kv_end_trans,
-    .prepare_commit = ldb_kv_prepare_commit,
-    .del_transaction = ldb_kv_del_trans,
-    .read_lock = ldb_kv_lock_read,
-    .read_unlock = ldb_kv_unlock_read,
-};
-
-int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
-		      const char *name,
-		      struct ldb_context *ldb,
-		      const char *options[],
-		      struct ldb_module **_module)
-{
-	if (getenv("LDB_WARN_UNINDEXED")) {
-		ldb_kv->warn_unindexed = true;
-	}
-
-	if (getenv("LDB_WARN_REINDEX")) {
-		ldb_kv->warn_reindex = true;
-	}
-
-	ldb_kv->sequence_number = 0;
-
-	ldb_kv->pid = getpid();
-
-	ldb_kv->module = ldb_module_new(ldb, ldb, name, &ldb_kv_ops);
-	if (!ldb_kv->module) {
-		ldb_oom(ldb);
-		talloc_free(ldb_kv);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-	ldb_module_set_private(ldb_kv->module, ldb_kv);
-	talloc_steal(ldb_kv->module, ldb_kv);
-
-	if (ldb_kv_cache_load(ldb_kv->module) != 0) {
-		ldb_asprintf_errstring(ldb, "Unable to load ltdb cache "
-				       "records for backend '%s'", name);
-		talloc_free(ldb_kv->module);
-		return LDB_ERR_OPERATIONS_ERROR;
-	}
-
-	*_module = ldb_kv->module;
-	/*
-	 * Set or override the maximum key length
-	 *
-	 * The ldb_mdb code will have set this to 511, but our tests
-	 * set this even smaller (to make the tests more practical).
-	 *
-	 * This must only be used for the selftest as the length
-	 * becomes encoded in the index keys.
-	 */
-	{
-		const char *len_str =
-			ldb_options_find(ldb, options,
-					 "max_key_len_for_self_test");
-		if (len_str != NULL) {
-			unsigned len = strtoul(len_str, NULL, 0);
-			ldb_kv->max_key_length = len;
-		}
-	}
-
-	/*
-	 * Override full DB scans
-	 *
-	 * A full DB scan is expensive on a large database.  This
-	 * option is for testing to show that the full DB scan is not
-	 * triggered.
-	 */
-	{
-		const char *len_str =
-			ldb_options_find(ldb, options,
-					 "disable_full_db_scan_for_self_test");
-		if (len_str != NULL) {
-			ldb_kv->disable_full_db_scan = true;
-		}
-	}
-
-	return LDB_SUCCESS;
-}
-
 /*
   connect to the database
 */
diff --git a/lib/ldb/ldb_tdb/ldb_tdb.h b/lib/ldb/ldb_tdb/ldb_tdb.h
index 4cbc7c2..5395d42 100644
--- a/lib/ldb/ldb_tdb/ldb_tdb.h
+++ b/lib/ldb/ldb_tdb/ldb_tdb.h
@@ -4,266 +4,13 @@
 #include "tdb.h"
 #include "ldb_module.h"
 
-struct ldb_kv_private;
-typedef int (*ldb_kv_traverse_fn)(struct ldb_kv_private *ldb_kv,
-				  struct ldb_val key,
-				  struct ldb_val data,
-				  void *ctx);
-
-struct kv_db_ops {
-	int (*store)(struct ldb_kv_private *ldb_kv,
-		     struct ldb_val key,
-		     struct ldb_val data,
-		     int flags);
-	int (*delete)(struct ldb_kv_private *ldb_kv, struct ldb_val key);
-	int (*iterate)(struct ldb_kv_private *ldb_kv,
-		       ldb_kv_traverse_fn fn,
-		       void *ctx);
-	int (*update_in_iterate)(struct ldb_kv_private *ldb_kv,
-				 struct ldb_val key,
-				 struct ldb_val key2,
-				 struct ldb_val data,
-				 void *ctx);
-	int (*fetch_and_parse)(struct ldb_kv_private *ldb_kv,
-			       struct ldb_val key,
-			       int (*parser)(struct ldb_val key,
-					     struct ldb_val data,
-					     void *private_data),
-			       void *ctx);
-	int (*lock_read)(struct ldb_module *);
-	int (*unlock_read)(struct ldb_module *);
-	int (*begin_write)(struct ldb_kv_private *);
-	int (*prepare_write)(struct ldb_kv_private *);
-	int (*abort_write)(struct ldb_kv_private *);
-	int (*finish_write)(struct ldb_kv_private *);
-	int (*error)(struct ldb_kv_private *ldb_kv);
-	const char *(*errorstr)(struct ldb_kv_private *ldb_kv);
-	const char *(*name)(struct ldb_kv_private *ldb_kv);
-	bool (*has_changed)(struct ldb_kv_private *ldb_kv);
-	bool (*transaction_active)(struct ldb_kv_private *ldb_kv);
-};
-
-/* this private structure is used by the key value backends in the
-   ldb_context */
-struct ldb_kv_private {
-	const struct kv_db_ops *kv_ops;
-	struct ldb_module *module;
-	TDB_CONTEXT *tdb;
-	struct lmdb_private *lmdb_private;
-	unsigned int connect_flags;
-	
-	unsigned long long sequence_number;
-
-	/* the low level tdb seqnum - used to avoid loading BASEINFO when
-	   possible */
-	int tdb_seqnum;
-
-	struct ldb_kv_cache {
-		struct ldb_message *indexlist;
-		bool one_level_indexes;
-		bool attribute_indexes;
-		const char *GUID_index_attribute;
-		const char *GUID_index_dn_component;
-	} *cache;
-
-
-	bool check_base;
-	bool disallow_dn_filter;
-	struct ldb_kv_idxptr *idxptr;
-	bool prepared_commit;
-	int read_lock_count;
-
-	bool warn_unindexed;
-	bool warn_reindex;
-
-	bool read_only;
-
-	bool reindex_failed;
-
-	const struct ldb_schema_syntax *GUID_index_syntax;
-
-	/*
-	 * Maximum index key length.  If non zero keys longer than this length
-	 * will be truncated for non unique indexes. Keys for unique indexes
-	 * greater than this length will be rejected.
-	 */
-	unsigned max_key_length;
-
-	/*
-	 * To allow testing that ensures the DB does not fall back
-	 * to a full scan
-	 */
-	bool disable_full_db_scan;
-
-	/*
-	 * The PID that opened this database so we don't work in a
-	 * fork()ed child.
-	 */
-	pid_t pid;
-};
-
-struct ldb_kv_context {
-	struct ldb_module *module;
-	struct ldb_request *req;
-
-	bool request_terminated;
-	struct ldb_kv_req_spy *spy;
-
-	/* search stuff */
-	const struct ldb_parse_tree *tree;
-	struct ldb_dn *base;
-	enum ldb_scope scope;
-	const char * const *attrs;
-	struct tevent_timer *timeout_event;
-
-	/* error handling */
-	int error;
-};
-
-struct ldb_kv_reindex_context {
-	struct ldb_module *module;
-	int error;
-	uint32_t count;
-};
-
-
-/* special record types */
-#define LTDB_INDEX      "@INDEX"
-#define LTDB_INDEXLIST  "@INDEXLIST"
-#define LTDB_IDX        "@IDX"
-#define LTDB_IDXVERSION "@IDXVERSION"
-#define LTDB_IDXATTR    "@IDXATTR"
-#define LTDB_IDXONE     "@IDXONE"
-#define LTDB_IDXDN     "@IDXDN"
-#define LTDB_IDXGUID    "@IDXGUID"
-#define LTDB_IDX_DN_GUID "@IDX_DN_GUID"
-
-/*
- * This will be used to indicate when a new, yet to be developed
- * sub-database version of the indicies are in use, to ensure we do
- * not load future databases unintentionally.
- */
-
-#define LTDB_IDX_LMDB_SUBDB "@IDX_LMDB_SUBDB"
-
-#define LTDB_BASEINFO   "@BASEINFO"
-#define LTDB_OPTIONS    "@OPTIONS"
-#define LTDB_ATTRIBUTES "@ATTRIBUTES"
-
-/* special attribute types */
-#define LTDB_SEQUENCE_NUMBER "sequenceNumber"
-#define LTDB_CHECK_BASE "checkBaseOnSearch"
-#define LTDB_DISALLOW_DN_FILTER "disallowDNFilter"
-#define LTDB_MOD_TIMESTAMP "whenChanged"
-#define LTDB_OBJECTCLASS "objectClass"
-
-/* DB keys */
-#define LTDB_GUID_KEY_PREFIX "GUID="
-#define LTDB_GUID_SIZE 16
-#define LTDB_GUID_KEY_SIZE (LTDB_GUID_SIZE + sizeof(LTDB_GUID_KEY_PREFIX) - 1)
-
-/* The following definitions come from lib/ldb/ldb_tdb/ldb_cache.c  */
-
-int ldb_kv_cache_reload(struct ldb_module *module);
-int ldb_kv_cache_load(struct ldb_module *module);
-int ldb_kv_increase_sequence_number(struct ldb_module *module);
-int ldb_kv_check_at_attributes_values(const struct ldb_val *value);
-
-/* The following definitions come from lib/ldb/ldb_tdb/ldb_index.c  */
-
-struct ldb_parse_tree;
-
-int ldb_kv_search_indexed(struct ldb_kv_context *ctx, uint32_t *);
-int ldb_kv_index_add_new(struct ldb_module *module,
-			 struct ldb_kv_private *ldb_kv,
-			 const struct ldb_message *msg);
-int ldb_kv_index_delete(struct ldb_module *module,
-			const struct ldb_message *msg);
-int ldb_kv_index_del_element(struct ldb_module *module,
-			     struct ldb_kv_private *ldb_kv,
-			     const struct ldb_message *msg,
-			     struct ldb_message_element *el);
-int ldb_kv_index_add_element(struct ldb_module *module,
-			     struct ldb_kv_private *ldb_kv,
-			     const struct ldb_message *msg,
-			     struct ldb_message_element *el);
-int ldb_kv_index_del_value(struct ldb_module *module,
-			   struct ldb_kv_private *ldb_kv,
-			   const struct ldb_message *msg,
-			   struct ldb_message_element *el,
-			   unsigned int v_idx);
-int ldb_kv_reindex(struct ldb_module *module);
-int ldb_kv_index_transaction_start(struct ldb_module *module);
-int ldb_kv_index_transaction_commit(struct ldb_module *module);
-int ldb_kv_index_transaction_cancel(struct ldb_module *module);
-int ldb_kv_key_dn_from_idx(struct ldb_module *module,
-			   struct ldb_kv_private *ldb_kv,
-			   TALLOC_CTX *mem_ctx,
-			   struct ldb_dn *dn,
-			   TDB_DATA *tdb_key);
-
-/* The following definitions come from lib/ldb/ldb_tdb/ldb_search.c  */
-int ldb_kv_search_dn1(struct ldb_module *module,
-		      struct ldb_dn *dn,
-		      struct ldb_message *msg,
-		      unsigned int unpack_flags);
-int ldb_kv_search_base(struct ldb_module *module,
-		       TALLOC_CTX *mem_ctx,
-		       struct ldb_dn *dn,
-		       struct ldb_dn **ret_dn);
-int ldb_kv_search_key(struct ldb_module *module,
-		      struct ldb_kv_private *ldb_kv,
-		      struct TDB_DATA tdb_key,
-		      struct ldb_message *msg,
-		      unsigned int unpack_flags);
-int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
-			const struct ldb_message *msg,
-			const char *const *attrs,
-			struct ldb_message **filtered_msg);
-int ldb_kv_search(struct ldb_kv_context *ctx);
-
-/* The following definitions come from lib/ldb/ldb_tdb/ldb_tdb.c  */
-/* 
- * Determine if this key could hold a record.  We allow the new GUID
- * index, the old DN index and a possible future ID=
- */
-bool ldb_kv_key_is_record(TDB_DATA key);
-TDB_DATA ldb_kv_key_dn(struct ldb_module *module,
-		       TALLOC_CTX *mem_ctx,
-		       struct ldb_dn *dn);
-TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
-			TALLOC_CTX *mem_ctx,
-			const struct ldb_message *msg);
-int ldb_kv_guid_to_key(struct ldb_module *module,
-		       struct ldb_kv_private *ldb_kv,
-		       const struct ldb_val *GUID_val,
-		       TDB_DATA *key);
-int ldb_kv_idx_to_key(struct ldb_module *module,
-		      struct ldb_kv_private *ldb_kv,
-		      TALLOC_CTX *mem_ctx,
-		      const struct ldb_val *idx_val,
-		      TDB_DATA *key);
 TDB_DATA ltdb_key(struct ldb_module *module, struct ldb_dn *dn);
-int ldb_kv_store(struct ldb_module *module,
-		 const struct ldb_message *msg,
-		 int flgs);
-int ldb_kv_modify_internal(struct ldb_module *module,
-			   const struct ldb_message *msg,
-			   struct ldb_request *req);
-int ldb_kv_delete_noindex(struct ldb_module *module,
-			  const struct ldb_message *msg);
 int ltdb_err_map(enum TDB_ERROR tdb_code);
 
 struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
 				   const char *path, int hash_size, int tdb_flags,
 				   int open_flags, mode_t mode,
 				   struct ldb_context *ldb);
-int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
-		      const char *name,
-		      struct ldb_context *ldb,
-		      const char *options[],
-		      struct ldb_module **_module);
-
 int ltdb_connect(struct ldb_context *ldb, const char *url,
 		 unsigned int flags, const char *options[],
 		 struct ldb_module **_module);
diff --git a/lib/ldb/ldb_tdb/ldb_tdb_err_map.c b/lib/ldb/ldb_tdb/ldb_tdb_err_map.c
new file mode 100644
index 0000000..41e5318
--- /dev/null
+++ b/lib/ldb/ldb_tdb/ldb_tdb_err_map.c
@@ -0,0 +1,84 @@
+/*
+   ldb database library
+
+   Copyright (C) Andrew Tridgell 2004
+   Copyright (C) Stefan Metzmacher 2004
+   Copyright (C) Simo Sorce 2006-2008
+   Copyright (C) Matthias Dieter Wallnöfer 2009-2010
+
+     ** NOTE! The following LGPL license applies to the ldb
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 3 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ *  Name: ldb_tdb
+ *
+ *  Component: ldb tdb backend
+ *
+ *  Description: core functions for tdb backend
+ *
+ *  Author: Andrew Tridgell
+ *  Author: Stefan Metzmacher
+ *
+ *  Modifications:
+ *
+ *  - description: make the module use asynchronous calls
+ *    date: Feb 2006
+ *    Author: Simo Sorce
+ *
+ *  - description: make it possible to use event contexts
+ *    date: Jan 2008
+ *    Author: Simo Sorce
+ *
+ *  - description: fix up memory leaks and small bugs
+ *    date: Oct 2009
+ *    Author: Matthias Dieter Wallnöfer
+ */
+
+#include "ldb_tdb.h"
+#include <tdb.h>
+
+/*
+  map a tdb error code to a ldb error code
+*/
+int ltdb_err_map(enum TDB_ERROR tdb_code)
+{
+	switch (tdb_code) {
+	case TDB_SUCCESS:
+		return LDB_SUCCESS;
+	case TDB_ERR_CORRUPT:
+	case TDB_ERR_OOM:
+	case TDB_ERR_EINVAL:
+		return LDB_ERR_OPERATIONS_ERROR;
+	case TDB_ERR_IO:
+		return LDB_ERR_PROTOCOL_ERROR;
+	case TDB_ERR_LOCK:
+	case TDB_ERR_NOLOCK:
+		return LDB_ERR_BUSY;
+	case TDB_ERR_LOCK_TIMEOUT:
+		return LDB_ERR_TIME_LIMIT_EXCEEDED;
+	case TDB_ERR_EXISTS:
+		return LDB_ERR_ENTRY_ALREADY_EXISTS;
+	case TDB_ERR_NOEXIST:
+		return LDB_ERR_NO_SUCH_OBJECT;
+	case TDB_ERR_RDONLY:
+		return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
+	default:
+		break;
+	}
+	return LDB_ERR_OTHER;
+}
diff --git a/lib/ldb/tests/ldb_kv_ops_test.c b/lib/ldb/tests/ldb_kv_ops_test.c
index 36ea3bf..68c0486 100644
--- a/lib/ldb/tests/ldb_kv_ops_test.c
+++ b/lib/ldb/tests/ldb_kv_ops_test.c
@@ -62,6 +62,7 @@
 #include <sys/wait.h>
 
 #include "ldb_tdb/ldb_tdb.h"
+#include "ldb_key_value/ldb_kv.h"
 
 
 #define DEFAULT_BE  "tdb"
diff --git a/lib/ldb/tests/ldb_lmdb_test.c b/lib/ldb/tests/ldb_lmdb_test.c
index 364a4f6..78758bb 100644
--- a/lib/ldb/tests/ldb_lmdb_test.c
+++ b/lib/ldb/tests/ldb_lmdb_test.c
@@ -57,6 +57,7 @@
 
 #include "../ldb_tdb/ldb_tdb.h"
 #include "../ldb_mdb/ldb_mdb.h"
+#include "../ldb_key_value/ldb_kv.h"
 
 #define TEST_BE  "mdb"
 
diff --git a/lib/ldb/tests/ldb_tdb_test.c b/lib/ldb/tests/ldb_tdb_test.c
index 226fcb1..8418dbf 100644
--- a/lib/ldb/tests/ldb_tdb_test.c
+++ b/lib/ldb/tests/ldb_tdb_test.c
@@ -56,6 +56,7 @@
 #include <sys/wait.h>
 
 #include "../ldb_tdb/ldb_tdb.h"
+#include "../ldb_key_value/ldb_kv.h"
 
 #define TEST_BE  "tdb"
 
diff --git a/lib/ldb/wscript b/lib/ldb/wscript
index 33e787c..5d14413 100644
--- a/lib/ldb/wscript
+++ b/lib/ldb/wscript
@@ -396,15 +396,27 @@ def build(bld):
                          init_function='ldb_tdb_init',
                          module_init_name='ldb_init_module',
                          internal_module=False,
-                         deps='tdb ldb ldb_key_value',
+                         deps='ldb ldb_tdb_int ldb_key_value',
                          subsystem='ldb')
 
-        bld.SAMBA_LIBRARY('ldb_key_value',
+        bld.SAMBA_LIBRARY('ldb_tdb_int',
+                          bld.SUBDIR('ldb_tdb',
+                                     '''ldb_tdb_wrap.c ldb_tdb.c'''),
+                          private_library=True,
+                          deps='ldb tdb ldb_key_value ldb_tdb_err_map')
+
+        bld.SAMBA_LIBRARY('ldb_tdb_err_map',
                           bld.SUBDIR('ldb_tdb',
-                                    '''ldb_tdb.c ldb_search.c ldb_index.c
-                                    ldb_cache.c ldb_tdb_wrap.c'''),
+                                     '''ldb_tdb_err_map.c '''),
+                          private_library=True,
+                          deps='ldb tdb')
+
+        bld.SAMBA_LIBRARY('ldb_key_value',
+                          bld.SUBDIR('ldb_key_value',
+                                    '''ldb_kv.c ldb_kv_search.c ldb_kv_index.c
+                                    ldb_kv_cache.c'''),
                           private_library=True,
-                          deps='tdb ldb')
+                          deps='tdb ldb ldb_tdb_err_map')
 
         if bld.CONFIG_SET('HAVE_LMDB'):
             bld.SAMBA_MODULE('ldb_mdb',
@@ -432,7 +444,7 @@ def build(bld):
                          init_function='ldb_ldb_init',
                          module_init_name='ldb_init_module',
                          internal_module=False,
-                         deps='ldb ldb_key_value' + lmdb_deps,
+                         deps='ldb ldb_tdb ldb_key_value' + lmdb_deps,
                          subsystem='ldb')
 
         # have a separate subsystem for common/ldb.c, so it can rebuild
-- 
2.7.4


From 6570fa3701a12d5b55f428868f09713c9c71a880 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Mon, 23 Jul 2018 10:08:26 +1200
Subject: [PATCH 16/18] lib ldb: rename LTDB_* constants to LDB_KV_*

Rename all the LTDB_* constants to LDB_KV_* as they are key value level
constants and not tdb specific.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_key_value/ldb_kv.c        | 36 +++++++--------
 lib/ldb/ldb_key_value/ldb_kv.h        | 42 +++++++++---------
 lib/ldb/ldb_key_value/ldb_kv_cache.c  | 54 +++++++++++------------
 lib/ldb/ldb_key_value/ldb_kv_index.c  | 82 +++++++++++++++++------------------
 lib/ldb/ldb_key_value/ldb_kv_search.c |  2 +-
 5 files changed, 108 insertions(+), 108 deletions(-)

diff --git a/lib/ldb/ldb_key_value/ldb_kv.c b/lib/ldb/ldb_key_value/ldb_kv.c
index 2449ec2..2d6abf6 100644
--- a/lib/ldb/ldb_key_value/ldb_kv.c
+++ b/lib/ldb/ldb_key_value/ldb_kv.c
@@ -77,12 +77,12 @@ bool ldb_kv_key_is_record(TDB_DATA key)
 		return true;
 	}
 
-	if (key.dsize < sizeof(LTDB_GUID_KEY_PREFIX)) {
+	if (key.dsize < sizeof(LDB_KV_GUID_KEY_PREFIX)) {
 		return false;
 	}
 
-	if (memcmp(key.dptr, LTDB_GUID_KEY_PREFIX,
-		   sizeof(LTDB_GUID_KEY_PREFIX) - 1) == 0) {
+	if (memcmp(key.dptr, LDB_KV_GUID_KEY_PREFIX,
+		   sizeof(LDB_KV_GUID_KEY_PREFIX) - 1) == 0) {
 		return true;
 	}
 
@@ -149,8 +149,8 @@ int ldb_kv_guid_to_key(struct ldb_module *module,
 		       const struct ldb_val *GUID_val,
 		       TDB_DATA *key)
 {
-	const char *GUID_prefix = LTDB_GUID_KEY_PREFIX;
-	const int GUID_prefix_len = sizeof(LTDB_GUID_KEY_PREFIX) - 1;
+	const char *GUID_prefix = LDB_KV_GUID_KEY_PREFIX;
+	const int GUID_prefix_len = sizeof(LDB_KV_GUID_KEY_PREFIX) - 1;
 
 	if (key->dsize != (GUID_val->length+GUID_prefix_len)) {
 		return LDB_ERR_OPERATIONS_ERROR;
@@ -229,7 +229,7 @@ TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
 				       "Did not find GUID attribute %s "
 				       "in %s, required for TDB record "
-				       "key in " LTDB_IDXGUID " mode.",
+				       "key in " LDB_KV_IDXGUID " mode.",
 				       ldb_kv->cache->GUID_index_attribute,
 				       ldb_dn_get_linearized(msg->dn));
 		errno = EINVAL;
@@ -239,7 +239,7 @@ TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
 	}
 
 	/* In this case, allocate with talloc */
-	key.dptr = talloc_size(mem_ctx, LTDB_GUID_KEY_SIZE);
+	key.dptr = talloc_size(mem_ctx, LDB_KV_GUID_KEY_SIZE);
 	if (key.dptr == NULL) {
 		errno = ENOMEM;
 		key.dptr = NULL;
@@ -270,7 +270,7 @@ static int ldb_kv_check_special_dn(struct ldb_module *module,
 	unsigned int i, j;
 
 	if (! ldb_dn_is_special(msg->dn) ||
-	    ! ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
+	    ! ldb_dn_check_special(msg->dn, LDB_KV_ATTRIBUTES)) {
 		return LDB_SUCCESS;
 	}
 
@@ -310,8 +310,8 @@ static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
 	}
 
 	if (ldb_dn_is_special(dn) &&
-	    (ldb_dn_check_special(dn, LTDB_INDEXLIST) ||
-	     ldb_dn_check_special(dn, LTDB_ATTRIBUTES)) )
+	    (ldb_dn_check_special(dn, LDB_KV_INDEXLIST) ||
+	     ldb_dn_check_special(dn, LDB_KV_ATTRIBUTES)) )
 	{
 		if (ldb_kv->warn_reindex) {
 			ldb_debug(ldb_module_get_ctx(module),
@@ -326,14 +326,14 @@ static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
 	/* If the modify was to a normal record, or any special except @BASEINFO, update the seq number */
 	if (ret == LDB_SUCCESS &&
 	    !(ldb_dn_is_special(dn) &&
-	      ldb_dn_check_special(dn, LTDB_BASEINFO)) ) {
+	      ldb_dn_check_special(dn, LDB_KV_BASEINFO)) ) {
 		ret = ldb_kv_increase_sequence_number(module);
 	}
 
 	/* If the modify was to @OPTIONS, reload the cache */
 	if (ret == LDB_SUCCESS &&
 	    ldb_dn_is_special(dn) &&
-	    (ldb_dn_check_special(dn, LTDB_OPTIONS)) ) {
+	    (ldb_dn_check_special(dn, LDB_KV_OPTIONS)) ) {
 		ret = ldb_kv_cache_reload(module);
 	}
 
@@ -463,7 +463,7 @@ static int ldb_kv_add_internal(struct ldb_module *module,
 
 		/* Do not check "@ATTRIBUTES" for duplicated values */
 		if (ldb_dn_is_special(msg->dn) &&
-		    ldb_dn_check_special(msg->dn, LTDB_ATTRIBUTES)) {
+		    ldb_dn_check_special(msg->dn, LDB_KV_ATTRIBUTES)) {
 			continue;
 		}
 
@@ -557,7 +557,7 @@ static int ldb_kv_add(struct ldb_kv_context *ctx)
 	    !ldb_dn_is_special(req->op.add.message->dn)) {
 		ldb_set_errstring(ldb_module_get_ctx(module),
 				  "Must operate ldb_mdb in GUID "
-				  "index mode, but " LTDB_IDXGUID " not set.");
+				  "index mode, but " LDB_KV_IDXGUID " not set.");
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
@@ -1533,7 +1533,7 @@ static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
 		goto done;
 	}
 
-	dn = ldb_dn_new(tmp_ctx, ldb, LTDB_BASEINFO);
+	dn = ldb_dn_new(tmp_ctx, ldb, LDB_KV_BASEINFO);
 	if (dn == NULL) {
 		ret = LDB_ERR_OPERATIONS_ERROR;
 		goto done;
@@ -1552,14 +1552,14 @@ static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
 
 	switch (seq->type) {
 	case LDB_SEQ_HIGHEST_SEQ:
-		res->seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
+		res->seq_num = ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
 		break;
 	case LDB_SEQ_NEXT:
-		res->seq_num = ldb_msg_find_attr_as_uint64(msg, LTDB_SEQUENCE_NUMBER, 0);
+		res->seq_num = ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
 		res->seq_num++;
 		break;
 	case LDB_SEQ_HIGHEST_TIMESTAMP:
-		date = ldb_msg_find_attr_as_string(msg, LTDB_MOD_TIMESTAMP, NULL);
+		date = ldb_msg_find_attr_as_string(msg, LDB_KV_MOD_TIMESTAMP, NULL);
 		if (date) {
 			res->seq_num = ldb_string_to_time(date);
 		} else {
diff --git a/lib/ldb/ldb_key_value/ldb_kv.h b/lib/ldb/ldb_key_value/ldb_kv.h
index 13fe125..2ea36ca 100644
--- a/lib/ldb/ldb_key_value/ldb_kv.h
+++ b/lib/ldb/ldb_key_value/ldb_kv.h
@@ -128,15 +128,15 @@ struct ldb_kv_reindex_context {
 
 
 /* special record types */
-#define LTDB_INDEX      "@INDEX"
-#define LTDB_INDEXLIST  "@INDEXLIST"
-#define LTDB_IDX        "@IDX"
-#define LTDB_IDXVERSION "@IDXVERSION"
-#define LTDB_IDXATTR    "@IDXATTR"
-#define LTDB_IDXONE     "@IDXONE"
-#define LTDB_IDXDN     "@IDXDN"
-#define LTDB_IDXGUID    "@IDXGUID"
-#define LTDB_IDX_DN_GUID "@IDX_DN_GUID"
+#define LDB_KV_INDEX      "@INDEX"
+#define LDB_KV_INDEXLIST  "@INDEXLIST"
+#define LDB_KV_IDX        "@IDX"
+#define LDB_KV_IDXVERSION "@IDXVERSION"
+#define LDB_KV_IDXATTR    "@IDXATTR"
+#define LDB_KV_IDXONE     "@IDXONE"
+#define LDB_KV_IDXDN     "@IDXDN"
+#define LDB_KV_IDXGUID    "@IDXGUID"
+#define LDB_KV_IDX_DN_GUID "@IDX_DN_GUID"
 
 /*
  * This will be used to indicate when a new, yet to be developed
@@ -144,23 +144,23 @@ struct ldb_kv_reindex_context {
  * not load future databases unintentionally.
  */
 
-#define LTDB_IDX_LMDB_SUBDB "@IDX_LMDB_SUBDB"
+#define LDB_KV_IDX_LMDB_SUBDB "@IDX_LMDB_SUBDB"
 
-#define LTDB_BASEINFO   "@BASEINFO"
-#define LTDB_OPTIONS    "@OPTIONS"
-#define LTDB_ATTRIBUTES "@ATTRIBUTES"
+#define LDB_KV_BASEINFO   "@BASEINFO"
+#define LDB_KV_OPTIONS    "@OPTIONS"
+#define LDB_KV_ATTRIBUTES "@ATTRIBUTES"
 
 /* special attribute types */
-#define LTDB_SEQUENCE_NUMBER "sequenceNumber"
-#define LTDB_CHECK_BASE "checkBaseOnSearch"
-#define LTDB_DISALLOW_DN_FILTER "disallowDNFilter"
-#define LTDB_MOD_TIMESTAMP "whenChanged"
-#define LTDB_OBJECTCLASS "objectClass"
+#define LDB_KV_SEQUENCE_NUMBER "sequenceNumber"
+#define LDB_KV_CHECK_BASE "checkBaseOnSearch"
+#define LDB_KV_DISALLOW_DN_FILTER "disallowDNFilter"
+#define LDB_KV_MOD_TIMESTAMP "whenChanged"
+#define LDB_KV_OBJECTCLASS "objectClass"
 
 /* DB keys */
-#define LTDB_GUID_KEY_PREFIX "GUID="
-#define LTDB_GUID_SIZE 16
-#define LTDB_GUID_KEY_SIZE (LTDB_GUID_SIZE + sizeof(LTDB_GUID_KEY_PREFIX) - 1)
+#define LDB_KV_GUID_KEY_PREFIX "GUID="
+#define LDB_KV_GUID_SIZE 16
+#define LDB_KV_GUID_KEY_SIZE (LDB_KV_GUID_SIZE + sizeof(LDB_KV_GUID_KEY_PREFIX) - 1)
 
 /*
  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_cache.c
diff --git a/lib/ldb/ldb_key_value/ldb_kv_cache.c b/lib/ldb/ldb_key_value/ldb_kv_cache.c
index f0bc31b..c39273f 100644
--- a/lib/ldb/ldb_key_value/ldb_kv_cache.c
+++ b/lib/ldb/ldb_key_value/ldb_kv_cache.c
@@ -34,19 +34,19 @@
 #include "ldb_kv.h"
 #include "ldb_private.h"
 
-#define LTDB_FLAG_CASE_INSENSITIVE (1<<0)
-#define LTDB_FLAG_INTEGER          (1<<1)
-#define LTDB_FLAG_UNIQUE_INDEX     (1<<2)
+#define LDB_KV_FLAG_CASE_INSENSITIVE (1<<0)
+#define LDB_KV_FLAG_INTEGER          (1<<1)
+#define LDB_KV_FLAG_UNIQUE_INDEX     (1<<2)
 
 /* valid attribute flags */
 static const struct {
 	const char *name;
 	int value;
 } ldb_kv_valid_attr_flags[] = {
-	{ "CASE_INSENSITIVE", LTDB_FLAG_CASE_INSENSITIVE },
-	{ "INTEGER", LTDB_FLAG_INTEGER },
+	{ "CASE_INSENSITIVE", LDB_KV_FLAG_CASE_INSENSITIVE },
+	{ "INTEGER", LDB_KV_FLAG_INTEGER },
 	{ "HIDDEN", 0 },
-	{ "UNIQUE_INDEX",  LTDB_FLAG_UNIQUE_INDEX},
+	{ "UNIQUE_INDEX",  LDB_KV_FLAG_UNIQUE_INDEX},
 	{ "NONE", 0 },
 	{ NULL, 0 }
 };
@@ -119,7 +119,7 @@ static int ldb_kv_attributes_load(struct ldb_module *module)
 		goto failed;
 	}
 
-	dn = ldb_dn_new(module, ldb, LTDB_ATTRIBUTES);
+	dn = ldb_dn_new(module, ldb, LDB_KV_ATTRIBUTES);
 	if (dn == NULL) goto failed;
 
 	r = ldb_kv_search_dn1(module,
@@ -171,15 +171,15 @@ static int ldb_kv_attributes_load(struct ldb_module *module)
 			goto failed;
 		}
 
-		if (flags & LTDB_FLAG_UNIQUE_INDEX) {
+		if (flags & LDB_KV_FLAG_UNIQUE_INDEX) {
 			attr_flags = LDB_ATTR_FLAG_UNIQUE_INDEX;
 		}
-		flags &= ~LTDB_FLAG_UNIQUE_INDEX;
+		flags &= ~LDB_KV_FLAG_UNIQUE_INDEX;
 
 		/* These are not currently flags, each is exclusive */
-		if (flags == LTDB_FLAG_CASE_INSENSITIVE) {
+		if (flags == LDB_KV_FLAG_CASE_INSENSITIVE) {
 			syntax = LDB_SYNTAX_DIRECTORY_STRING;
-		} else if (flags == LTDB_FLAG_INTEGER) {
+		} else if (flags == LDB_KV_FLAG_INTEGER) {
 			syntax = LDB_SYNTAX_INTEGER;
 		} else if (flags == 0) {
 			syntax = LDB_SYNTAX_OCTET_STRING;
@@ -266,7 +266,7 @@ static int ldb_kv_index_load(struct ldb_module *module,
 	ldb_kv->cache->one_level_indexes = false;
 	ldb_kv->cache->attribute_indexes = false;
 
-	indexlist_dn = ldb_dn_new(ldb_kv, ldb, LTDB_INDEXLIST);
+	indexlist_dn = ldb_dn_new(ldb_kv, ldb, LDB_KV_INDEXLIST);
 	if (indexlist_dn == NULL) {
 		return -1;
 	}
@@ -283,21 +283,21 @@ static int ldb_kv_index_load(struct ldb_module *module,
 		return -1;
 	}
 
-	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXONE) !=
+	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LDB_KV_IDXONE) !=
 	    NULL) {
 		ldb_kv->cache->one_level_indexes = true;
 	}
-	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR) !=
+	if (ldb_msg_find_element(ldb_kv->cache->indexlist, LDB_KV_IDXATTR) !=
 	    NULL) {
 		ldb_kv->cache->attribute_indexes = true;
 	}
 	ldb_kv->cache->GUID_index_attribute = ldb_msg_find_attr_as_string(
-	    ldb_kv->cache->indexlist, LTDB_IDXGUID, NULL);
+	    ldb_kv->cache->indexlist, LDB_KV_IDXGUID, NULL);
 	ldb_kv->cache->GUID_index_dn_component = ldb_msg_find_attr_as_string(
-	    ldb_kv->cache->indexlist, LTDB_IDX_DN_GUID, NULL);
+	    ldb_kv->cache->indexlist, LDB_KV_IDX_DN_GUID, NULL);
 
 	lmdb_subdb_version = ldb_msg_find_attr_as_int(
-	    ldb_kv->cache->indexlist, LTDB_IDX_LMDB_SUBDB, 0);
+	    ldb_kv->cache->indexlist, LDB_KV_IDX_LMDB_SUBDB, 0);
 
 	if (lmdb_subdb_version != 0) {
 		ldb_set_errstring(ldb,
@@ -341,11 +341,11 @@ static int ldb_kv_baseinfo_init(struct ldb_module *module)
 
 	msg->num_elements = 1;
 	msg->elements = ⪙
-	msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
+	msg->dn = ldb_dn_new(msg, ldb, LDB_KV_BASEINFO);
 	if (!msg->dn) {
 		goto failed;
 	}
-	el.name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
+	el.name = talloc_strdup(msg, LDB_KV_SEQUENCE_NUMBER);
 	if (!el.name) {
 		goto failed;
 	}
@@ -426,7 +426,7 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	baseinfo = ldb_msg_new(ldb_kv->cache);
 	if (baseinfo == NULL) goto failed;
 
-	baseinfo_dn = ldb_dn_new(baseinfo, ldb, LTDB_BASEINFO);
+	baseinfo_dn = ldb_dn_new(baseinfo, ldb, LDB_KV_BASEINFO);
 	if (baseinfo_dn == NULL) goto failed;
 
 	r = ldb_kv->kv_ops->lock_read(module);
@@ -469,7 +469,7 @@ int ldb_kv_cache_load(struct ldb_module *module)
 
 	/* if the current internal sequence number is the same as the one
 	   in the database then assume the rest of the cache is OK */
-	seq = ldb_msg_find_attr_as_uint64(baseinfo, LTDB_SEQUENCE_NUMBER, 0);
+	seq = ldb_msg_find_attr_as_uint64(baseinfo, LDB_KV_SEQUENCE_NUMBER, 0);
 	if (seq == ldb_kv->sequence_number) {
 		goto done;
 	}
@@ -480,7 +480,7 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	options = ldb_msg_new(ldb_kv->cache);
 	if (options == NULL) goto failed_and_unlock;
 
-	options_dn = ldb_dn_new(options, ldb, LTDB_OPTIONS);
+	options_dn = ldb_dn_new(options, ldb, LDB_KV_OPTIONS);
 	if (options_dn == NULL) goto failed_and_unlock;
 
 	r = ldb_kv_search_dn1(module, options_dn, options, 0);
@@ -492,9 +492,9 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	/* set flags if they do exist */
 	if (r == LDB_SUCCESS) {
 		ldb_kv->check_base =
-		    ldb_msg_find_attr_as_bool(options, LTDB_CHECK_BASE, false);
+		    ldb_msg_find_attr_as_bool(options, LDB_KV_CHECK_BASE, false);
 		ldb_kv->disallow_dn_filter = ldb_msg_find_attr_as_bool(
-		    options, LTDB_DISALLOW_DN_FILTER, false);
+		    options, LDB_KV_DISALLOW_DN_FILTER, false);
 	} else {
 		ldb_kv->check_base = false;
 		ldb_kv->disallow_dn_filter = false;
@@ -595,13 +595,13 @@ int ldb_kv_increase_sequence_number(struct ldb_module *module)
 
 	msg->num_elements = ARRAY_SIZE(el);
 	msg->elements = el;
-	msg->dn = ldb_dn_new(msg, ldb, LTDB_BASEINFO);
+	msg->dn = ldb_dn_new(msg, ldb, LDB_KV_BASEINFO);
 	if (msg->dn == NULL) {
 		talloc_free(msg);
 		errno = ENOMEM;
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
-	el[0].name = talloc_strdup(msg, LTDB_SEQUENCE_NUMBER);
+	el[0].name = talloc_strdup(msg, LDB_KV_SEQUENCE_NUMBER);
 	if (el[0].name == NULL) {
 		talloc_free(msg);
 		errno = ENOMEM;
@@ -613,7 +613,7 @@ int ldb_kv_increase_sequence_number(struct ldb_module *module)
 	val.data = (uint8_t *)s;
 	val.length = strlen(s);
 
-	el[1].name = talloc_strdup(msg, LTDB_MOD_TIMESTAMP);
+	el[1].name = talloc_strdup(msg, LDB_KV_MOD_TIMESTAMP);
 	if (el[1].name == NULL) {
 		talloc_free(msg);
 		errno = ENOMEM;
diff --git a/lib/ldb/ldb_key_value/ldb_kv_index.c b/lib/ldb/ldb_key_value/ldb_kv_index.c
index 4cefe0a..96ebd6b 100644
--- a/lib/ldb/ldb_key_value/ldb_kv_index.c
+++ b/lib/ldb/ldb_key_value/ldb_kv_index.c
@@ -186,9 +186,9 @@ static void ldb_kv_dn_list_sort(struct ldb_kv_private *ldb_kv,
 /* we put a @IDXVERSION attribute on index entries. This
    allows us to tell if it was written by an older version
 */
-#define LTDB_INDEXING_VERSION 2
+#define LDB_KV_INDEXING_VERSION 2
 
-#define LTDB_GUID_INDEXING_VERSION 3
+#define LDB_KV_GUID_INDEXING_VERSION 3
 
 static unsigned ldb_kv_max_key_length(struct ldb_kv_private *ldb_kv)
 {
@@ -400,13 +400,13 @@ normal_index:
 		return ret;
 	}
 
-	el = ldb_msg_find_element(msg, LTDB_IDX);
+	el = ldb_msg_find_element(msg, LDB_KV_IDX);
 	if (!el) {
 		talloc_free(msg);
 		return LDB_SUCCESS;
 	}
 
-	version = ldb_msg_find_attr_as_int(msg, LTDB_IDXVERSION, 0);
+	version = ldb_msg_find_attr_as_int(msg, LDB_KV_IDXVERSION, 0);
 
 	/*
 	 * we avoid copying the strings by stealing the list.  We have
@@ -416,12 +416,12 @@ normal_index:
 	 */
 	if (ldb_kv->cache->GUID_index_attribute == NULL) {
 		/* check indexing version number */
-		if (version != LTDB_INDEXING_VERSION) {
+		if (version != LDB_KV_INDEXING_VERSION) {
 			ldb_debug_set(ldb_module_get_ctx(module),
 				      LDB_DEBUG_ERROR,
 				      "Wrong DN index version %d "
 				      "expected %d for %s",
-				      version, LTDB_INDEXING_VERSION,
+				      version, LDB_KV_INDEXING_VERSION,
 				      ldb_dn_get_linearized(dn));
 			talloc_free(msg);
 			return LDB_ERR_OPERATIONS_ERROR;
@@ -432,14 +432,14 @@ normal_index:
 		list->count = el->num_values;
 	} else {
 		unsigned int i;
-		if (version != LTDB_GUID_INDEXING_VERSION) {
+		if (version != LDB_KV_GUID_INDEXING_VERSION) {
 			/* This is quite likely during the DB startup
 			   on first upgrade to using a GUID index */
 			ldb_debug_set(ldb_module_get_ctx(module),
 				      LDB_DEBUG_ERROR,
 				      "Wrong GUID index version %d "
 				      "expected %d for %s",
-				      version, LTDB_GUID_INDEXING_VERSION,
+				      version, LDB_KV_GUID_INDEXING_VERSION,
 				      ldb_dn_get_linearized(dn));
 			talloc_free(msg);
 			return LDB_ERR_OPERATIONS_ERROR;
@@ -450,12 +450,12 @@ normal_index:
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
 
-		if ((el->values[0].length % LTDB_GUID_SIZE) != 0) {
+		if ((el->values[0].length % LDB_KV_GUID_SIZE) != 0) {
 			talloc_free(msg);
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
 
-		list->count = el->values[0].length / LTDB_GUID_SIZE;
+		list->count = el->values[0].length / LDB_KV_GUID_SIZE;
 		list->dn = talloc_array(list, struct ldb_val, list->count);
 		if (list->dn == NULL) {
 			talloc_free(msg);
@@ -469,8 +469,8 @@ normal_index:
 		talloc_steal(list->dn, msg);
 		for (i = 0; i < list->count; i++) {
 			list->dn[i].data
-				= &el->values[0].data[i * LTDB_GUID_SIZE];
-			list->dn[i].length = LTDB_GUID_SIZE;
+				= &el->values[0].data[i * LDB_KV_GUID_SIZE];
+			list->dn[i].length = LDB_KV_GUID_SIZE;
 		}
 	}
 
@@ -528,7 +528,7 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 		int i;
 		index = -1;
 		for (i=0; i < list->count; i++) {
-			uint8_t guid_key[LTDB_GUID_KEY_SIZE];
+			uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
 			TDB_DATA key = {
 				.dptr = guid_key,
 				.dsize = sizeof(guid_key)
@@ -633,15 +633,15 @@ static int ldb_kv_dn_list_store_full(struct ldb_module *module,
 	}
 
 	if (ldb_kv->cache->GUID_index_attribute == NULL) {
-		ret = ldb_msg_add_fmt(msg, LTDB_IDXVERSION, "%u",
-				      LTDB_INDEXING_VERSION);
+		ret = ldb_msg_add_fmt(msg, LDB_KV_IDXVERSION, "%u",
+				      LDB_KV_INDEXING_VERSION);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(msg);
 			return ldb_module_oom(module);
 		}
 	} else {
-		ret = ldb_msg_add_fmt(msg, LTDB_IDXVERSION, "%u",
-				      LTDB_GUID_INDEXING_VERSION);
+		ret = ldb_msg_add_fmt(msg, LDB_KV_IDXVERSION, "%u",
+				      LDB_KV_GUID_INDEXING_VERSION);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(msg);
 			return ldb_module_oom(module);
@@ -651,7 +651,7 @@ static int ldb_kv_dn_list_store_full(struct ldb_module *module,
 	if (list->count > 0) {
 		struct ldb_message_element *el;
 
-		ret = ldb_msg_add_empty(msg, LTDB_IDX, LDB_FLAG_MOD_ADD, &el);
+		ret = ldb_msg_add_empty(msg, LDB_KV_IDX, LDB_FLAG_MOD_ADD, &el);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(msg);
 			return ldb_module_oom(module);
@@ -672,7 +672,7 @@ static int ldb_kv_dn_list_store_full(struct ldb_module *module,
 
 			v.data = talloc_array_size(el->values,
 						   list->count,
-						   LTDB_GUID_SIZE);
+						   LDB_KV_GUID_SIZE);
 			if (v.data == NULL) {
 				talloc_free(msg);
 				return ldb_module_oom(module);
@@ -682,13 +682,13 @@ static int ldb_kv_dn_list_store_full(struct ldb_module *module,
 
 			for (i = 0; i < list->count; i++) {
 				if (list->dn[i].length !=
-				    LTDB_GUID_SIZE) {
+				    LDB_KV_GUID_SIZE) {
 					talloc_free(msg);
 					return ldb_module_operr(module);
 				}
-				memcpy(&v.data[LTDB_GUID_SIZE*i],
+				memcpy(&v.data[LDB_KV_GUID_SIZE*i],
 				       list->dn[i].data,
-				       LTDB_GUID_SIZE);
+				       LDB_KV_GUID_SIZE);
 			}
 			el->values[0] = v;
 			el->num_values = 1;
@@ -873,7 +873,7 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 	unsigned int max_key_length = ldb_kv_max_key_length(ldb_kv);
 	size_t key_len = 0;
 	size_t attr_len = 0;
-	const size_t indx_len = sizeof(LTDB_INDEX) - 1;
+	const size_t indx_len = sizeof(LDB_KV_INDEX) - 1;
 	unsigned frmt_len = 0;
 	const size_t additional_key_length = 4;
 	unsigned int num_separators = 3; /* Estimate for overflow check */
@@ -947,9 +947,9 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 	 * avoids embedded NUL etc.
 	 */
 	if (ldb_kv->cache->GUID_index_attribute != NULL) {
-		if (strcmp(attr, LTDB_IDXDN) == 0) {
+		if (strcmp(attr, LDB_KV_IDXDN) == 0) {
 			should_b64_encode = false;
-		} else if (strcmp(attr, LTDB_IDXONE) == 0) {
+		} else if (strcmp(attr, LDB_KV_IDXONE) == 0) {
 			/*
 			 * We can only change the behaviour for IDXONE
 			 * when the GUID index is enabled
@@ -987,7 +987,7 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 			* indicates that the following value is base64 encoded
 			*/
 			ret = ldb_dn_new_fmt(ldb, ldb, "%s#%s##%.*s",
-					     LTDB_INDEX, attr_for_dn,
+					     LDB_KV_INDEX, attr_for_dn,
 					     frmt_len, vstr);
 		} else {
 			frmt_len = vstr_len;
@@ -997,7 +997,7 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 			 * indicates that the following value is base64 encoded
 			 */
 			ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s::%.*s",
-					     LTDB_INDEX, attr_for_dn,
+					     LDB_KV_INDEX, attr_for_dn,
 					     frmt_len, vstr);
 		}
 		talloc_free(vstr);
@@ -1019,13 +1019,13 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 			 * from the non truncated keys
 			 */
 			ret = ldb_dn_new_fmt(ldb, ldb, "%s#%s#%.*s",
-					     LTDB_INDEX, attr_for_dn,
+					     LDB_KV_INDEX, attr_for_dn,
 					     frmt_len, (char *)v.data);
 		} else {
 			frmt_len = v.length;
 			*truncation = KEY_NOT_TRUNCATED;
 			ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s:%.*s",
-					     LTDB_INDEX, attr_for_dn,
+					     LDB_KV_INDEX, attr_for_dn,
 					     frmt_len, (char *)v.data);
 		}
 	}
@@ -1073,7 +1073,7 @@ static bool ldb_kv_is_indexed(struct ldb_module *module,
 		return false;
 	}
 
-	el = ldb_msg_find_element(ldb_kv->cache->indexlist, LTDB_IDXATTR);
+	el = ldb_msg_find_element(ldb_kv->cache->indexlist, LDB_KV_IDXATTR);
 	if (el == NULL) {
 		return false;
 	}
@@ -1645,7 +1645,7 @@ static int ldb_kv_index_dn_one(struct ldb_module *module,
 	/* Ensure we do not shortcut on intersection for this list */
 	list->strict = true;
 	return ldb_kv_index_dn_attr(
-	    module, ldb_kv, LTDB_IDXONE, parent_dn, list, truncation);
+	    module, ldb_kv, LDB_KV_IDXONE, parent_dn, list, truncation);
 }
 
 /*
@@ -1692,7 +1692,7 @@ static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
 	}
 
 	return ldb_kv_index_dn_attr(
-	    module, ldb_kv, LTDB_IDXDN, base_dn, dn_list, truncation);
+	    module, ldb_kv, LDB_KV_IDXDN, base_dn, dn_list, truncation);
 }
 
 /*
@@ -1752,7 +1752,7 @@ static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 	struct ldb_message *filtered_msg;
 	unsigned int i;
 	unsigned int num_keys = 0;
-	uint8_t previous_guid_key[LTDB_GUID_KEY_SIZE] = {};
+	uint8_t previous_guid_key[LDB_KV_GUID_KEY_SIZE] = {};
 	TDB_DATA *keys = NULL;
 
 	/*
@@ -1773,7 +1773,7 @@ static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 		 * small allocations)
 		 */
 		struct guid_tdb_key {
-			uint8_t guid_key[LTDB_GUID_KEY_SIZE];
+			uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
 		} *key_values = NULL;
 
 		key_values = talloc_array(keys,
@@ -2169,14 +2169,14 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 	 * messages.
 	 */
 	if (list->count > 0 &&
-	    ldb_attr_cmp(el->name, LTDB_IDXDN) == 0 &&
+	    ldb_attr_cmp(el->name, LDB_KV_IDXDN) == 0 &&
 	    truncation == KEY_NOT_TRUNCATED) {
 
 		talloc_free(list);
 		return LDB_ERR_CONSTRAINT_VIOLATION;
 
 	} else if (list->count > 0
-		   && ldb_attr_cmp(el->name, LTDB_IDXDN) == 0) {
+		   && ldb_attr_cmp(el->name, LDB_KV_IDXDN) == 0) {
 
 		/*
 		 * At least one existing entry in the DN->GUID index, which
@@ -2186,7 +2186,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		 */
 		int i;
 		for (i=0; i < list->count; i++) {
-			uint8_t guid_key[LTDB_GUID_KEY_SIZE];
+			uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
 			TDB_DATA key = {
 				.dptr = guid_key,
 				.dsize = sizeof(guid_key)
@@ -2324,7 +2324,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 			return ldb_module_operr(module);
 		}
 
-		if (key_val->length != LTDB_GUID_SIZE) {
+		if (key_val->length != LDB_KV_GUID_SIZE) {
 			talloc_free(list);
 			return ldb_module_operr(module);
 		}
@@ -2529,7 +2529,7 @@ static int ldb_kv_index_onelevel(struct ldb_module *module,
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 	ret =
-	    ldb_kv_modify_index_dn(module, ldb_kv, msg, pdn, LTDB_IDXONE, add);
+	    ldb_kv_modify_index_dn(module, ldb_kv, msg, pdn, LDB_KV_IDXONE, add);
 
 	talloc_free(pdn);
 
@@ -2553,7 +2553,7 @@ static int ldb_kv_write_index_dn_guid(struct ldb_module *module,
 	}
 
 	ret = ldb_kv_modify_index_dn(
-	    module, ldb_kv, msg, msg->dn, LTDB_IDXDN, add);
+	    module, ldb_kv, msg, msg->dn, LDB_KV_IDXDN, add);
 
 	if (ret == LDB_ERR_CONSTRAINT_VIOLATION) {
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
@@ -2809,7 +2809,7 @@ static int delete_index(struct ldb_kv_private *ldb_kv,
 			void *state)
 {
 	struct ldb_module *module = state;
-	const char *dnstr = "DN=" LTDB_INDEX ":";
+	const char *dnstr = "DN=" LDB_KV_INDEX ":";
 	struct dn_list list;
 	struct ldb_dn *dn;
 	struct ldb_val v;
diff --git a/lib/ldb/ldb_key_value/ldb_kv_search.c b/lib/ldb/ldb_key_value/ldb_kv_search.c
index 5bb780a..ee9b4bc 100644
--- a/lib/ldb/ldb_key_value/ldb_kv_search.c
+++ b/lib/ldb/ldb_key_value/ldb_kv_search.c
@@ -291,7 +291,7 @@ int ldb_kv_search_dn1(struct ldb_module *module,
 	struct ldb_kv_private *ldb_kv =
 	    talloc_get_type(data, struct ldb_kv_private);
 	int ret;
-	uint8_t guid_key[LTDB_GUID_KEY_SIZE];
+	uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
 	TDB_DATA tdb_key = {
 		.dptr = guid_key,
 		.dsize = sizeof(guid_key)
-- 
2.7.4


From 6a4877942328b1e6af4b1c320f401810a843947a Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Mon, 23 Jul 2018 13:20:46 +1200
Subject: [PATCH 17/18] lib ldb key value: convert TDB_DATA structs to ldb_val

Convert the key value functions to use ldb_val instead of TDB_DATA.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_key_value/ldb_kv.c        | 112 ++++++++++++++++------------------
 lib/ldb/ldb_key_value/ldb_kv.h        |  22 +++----
 lib/ldb/ldb_key_value/ldb_kv_index.c  |  86 +++++++++++---------------
 lib/ldb/ldb_key_value/ldb_kv_search.c |  26 +++-----
 4 files changed, 109 insertions(+), 137 deletions(-)

diff --git a/lib/ldb/ldb_key_value/ldb_kv.c b/lib/ldb/ldb_key_value/ldb_kv.c
index 2d6abf6..3ea8d5e 100644
--- a/lib/ldb/ldb_key_value/ldb_kv.c
+++ b/lib/ldb/ldb_key_value/ldb_kv.c
@@ -63,25 +63,25 @@ struct ldb_kv_req_spy {
  * Determine if this key could hold a record.  We allow the new GUID
  * index, the old DN index and a possible future ID=
  */
-bool ldb_kv_key_is_record(TDB_DATA key)
+bool ldb_kv_key_is_record(struct ldb_val key)
 {
-	if (key.dsize < 4) {
+	if (key.length < 4) {
 		return false;
 	}
 
-	if (memcmp(key.dptr, "DN=", 3) == 0) {
+	if (memcmp(key.data, "DN=", 3) == 0) {
 		return true;
 	}
 
-	if (memcmp(key.dptr, "ID=", 3) == 0) {
+	if (memcmp(key.data, "ID=", 3) == 0) {
 		return true;
 	}
 
-	if (key.dsize < sizeof(LDB_KV_GUID_KEY_PREFIX)) {
+	if (key.length < sizeof(LDB_KV_GUID_KEY_PREFIX)) {
 		return false;
 	}
 
-	if (memcmp(key.dptr, LDB_KV_GUID_KEY_PREFIX,
+	if (memcmp(key.data, LDB_KV_GUID_KEY_PREFIX,
 		   sizeof(LDB_KV_GUID_KEY_PREFIX) - 1) == 0) {
 		return true;
 	}
@@ -90,17 +90,17 @@ bool ldb_kv_key_is_record(TDB_DATA key)
 }
 
 /*
-  form a TDB_DATA for a record key
+  form a ldb_val for a record key
   caller frees
 
   note that the key for a record can depend on whether the
   dn refers to a case sensitive index record or not
 */
-TDB_DATA ldb_kv_key_dn(struct ldb_module *module,
-		       TALLOC_CTX *mem_ctx,
-		       struct ldb_dn *dn)
+struct ldb_val ldb_kv_key_dn(struct ldb_module *module,
+			     TALLOC_CTX *mem_ctx,
+			     struct ldb_dn *dn)
 {
-	TDB_DATA key;
+	struct ldb_val key;
 	char *key_str = NULL;
 	const char *dn_folded = NULL;
 
@@ -131,15 +131,15 @@ TDB_DATA ldb_kv_key_dn(struct ldb_module *module,
 		goto failed;
 	}
 
-	key.dptr = (uint8_t *)key_str;
-	key.dsize = strlen(key_str) + 1;
+	key.data = (uint8_t *)key_str;
+	key.length = strlen(key_str) + 1;
 
 	return key;
 
 failed:
 	errno = ENOMEM;
-	key.dptr = NULL;
-	key.dsize = 0;
+	key.data = NULL;
+	key.length = 0;
 	return key;
 }
 
@@ -147,17 +147,17 @@ failed:
 int ldb_kv_guid_to_key(struct ldb_module *module,
 		       struct ldb_kv_private *ldb_kv,
 		       const struct ldb_val *GUID_val,
-		       TDB_DATA *key)
+		       struct ldb_val *key)
 {
 	const char *GUID_prefix = LDB_KV_GUID_KEY_PREFIX;
 	const int GUID_prefix_len = sizeof(LDB_KV_GUID_KEY_PREFIX) - 1;
 
-	if (key->dsize != (GUID_val->length+GUID_prefix_len)) {
+	if (key->length != (GUID_val->length+GUID_prefix_len)) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	memcpy(key->dptr, GUID_prefix, GUID_prefix_len);
-	memcpy(&key->dptr[GUID_prefix_len],
+	memcpy(key->data, GUID_prefix, GUID_prefix_len);
+	memcpy(&key->data[GUID_prefix_len],
 	       GUID_val->data, GUID_val->length);
 	return LDB_SUCCESS;
 }
@@ -170,7 +170,7 @@ int ldb_kv_idx_to_key(struct ldb_module *module,
 		      struct ldb_kv_private *ldb_kv,
 		      TALLOC_CTX *mem_ctx,
 		      const struct ldb_val *idx_val,
-		      TDB_DATA *key)
+		      struct ldb_val *key)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	struct ldb_dn *dn;
@@ -190,7 +190,7 @@ int ldb_kv_idx_to_key(struct ldb_module *module,
 	/* form the key */
 	*key = ldb_kv_key_dn(module, mem_ctx, dn);
 	TALLOC_FREE(dn);
-	if (!key->dptr) {
+	if (!key->data) {
 		return ldb_module_oom(module);
 	}
 	return LDB_SUCCESS;
@@ -204,14 +204,14 @@ int ldb_kv_idx_to_key(struct ldb_module *module,
   note that the key for a record can depend on whether a
   GUID index is in use, or the DN is used as the key
 */
-TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
+struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
 			TALLOC_CTX *mem_ctx,
 			const struct ldb_message *msg)
 {
 	void *data = ldb_module_get_private(module);
 	struct ldb_kv_private *ldb_kv =
 	    talloc_get_type(data, struct ldb_kv_private);
-	TDB_DATA key;
+	struct ldb_val key;
 	const struct ldb_val *guid_val;
 	int ret;
 
@@ -233,27 +233,27 @@ TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
 				       ldb_kv->cache->GUID_index_attribute,
 				       ldb_dn_get_linearized(msg->dn));
 		errno = EINVAL;
-		key.dptr = NULL;
-		key.dsize = 0;
+		key.data = NULL;
+		key.length = 0;
 		return key;
 	}
 
 	/* In this case, allocate with talloc */
-	key.dptr = talloc_size(mem_ctx, LDB_KV_GUID_KEY_SIZE);
-	if (key.dptr == NULL) {
+	key.data = talloc_size(mem_ctx, LDB_KV_GUID_KEY_SIZE);
+	if (key.data == NULL) {
 		errno = ENOMEM;
-		key.dptr = NULL;
-		key.dsize = 0;
+		key.data = NULL;
+		key.length = 0;
 		return key;
 	}
-	key.dsize = talloc_get_size(key.dptr);
+	key.length = talloc_get_size(key.data);
 
 	ret = ldb_kv_guid_to_key(module, ldb_kv, guid_val, &key);
 
 	if (ret != LDB_SUCCESS) {
 		errno = EINVAL;
-		key.dptr = NULL;
-		key.dsize = 0;
+		key.data = NULL;
+		key.length = 0;
 		return key;
 	}
 	return key;
@@ -353,8 +353,7 @@ int ldb_kv_store(struct ldb_module *module,
 	void *data = ldb_module_get_private(module);
 	struct ldb_kv_private *ldb_kv =
 	    talloc_get_type(data, struct ldb_kv_private);
-	TDB_DATA tdb_key;
-	struct ldb_val ldb_key;
+	struct ldb_val key;
 	struct ldb_val ldb_data;
 	int ret = LDB_SUCCESS;
 	TALLOC_CTX *key_ctx = talloc_new(module);
@@ -368,8 +367,8 @@ int ldb_kv_store(struct ldb_module *module,
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
-	tdb_key = ldb_kv_key_msg(module, key_ctx, msg);
-	if (tdb_key.dptr == NULL) {
+	key = ldb_kv_key_msg(module, key_ctx, msg);
+	if (key.data == NULL) {
 		TALLOC_FREE(key_ctx);
 		return LDB_ERR_OTHER;
 	}
@@ -381,10 +380,7 @@ int ldb_kv_store(struct ldb_module *module,
 		return LDB_ERR_OTHER;
 	}
 
-	ldb_key.data = tdb_key.dptr;
-	ldb_key.length = tdb_key.dsize;
-
-	ret = ldb_kv->kv_ops->store(ldb_kv, ldb_key, ldb_data, flgs);
+	ret = ldb_kv->kv_ops->store(ldb_kv, key, ldb_data, flgs);
 	if (ret != 0) {
 		bool is_special = ldb_dn_is_special(msg->dn);
 		ret = ldb_kv->kv_ops->error(ldb_kv);
@@ -587,8 +583,7 @@ int ldb_kv_delete_noindex(struct ldb_module *module,
 	void *data = ldb_module_get_private(module);
 	struct ldb_kv_private *ldb_kv =
 	    talloc_get_type(data, struct ldb_kv_private);
-	struct ldb_val ldb_key;
-	TDB_DATA tdb_key;
+	struct ldb_val key;
 	int ret;
 	TALLOC_CTX *tdb_key_ctx = talloc_new(module);
 
@@ -601,16 +596,13 @@ int ldb_kv_delete_noindex(struct ldb_module *module,
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
-	tdb_key = ldb_kv_key_msg(module, tdb_key_ctx, msg);
-	if (!tdb_key.dptr) {
+	key = ldb_kv_key_msg(module, tdb_key_ctx, msg);
+	if (!key.data) {
 		TALLOC_FREE(tdb_key_ctx);
 		return LDB_ERR_OTHER;
 	}
 
-	ldb_key.data = tdb_key.dptr;
-	ldb_key.length = tdb_key.dsize;
-
-	ret = ldb_kv->kv_ops->delete (ldb_kv, ldb_key);
+	ret = ldb_kv->kv_ops->delete (ldb_kv, key);
 	TALLOC_FREE(tdb_key_ctx);
 
 	if (ret != 0) {
@@ -1235,7 +1227,7 @@ static int ldb_kv_rename(struct ldb_kv_context *ctx)
 	struct ldb_request *req = ctx->req;
 	struct ldb_message *msg;
 	int ret = LDB_SUCCESS;
-	TDB_DATA tdb_key, tdb_key_old;
+	struct ldb_val  key, key_old;
 	struct ldb_dn *db_dn;
 
 	ldb_request_set_state(req, LDB_ASYNC_PENDING);
@@ -1266,16 +1258,16 @@ static int ldb_kv_rename(struct ldb_kv_context *ctx)
 	 * Even in GUID index mode we use ltdb_key_dn() as we are
 	 * trying to figure out if this is just a case rename
 	 */
-	tdb_key = ldb_kv_key_dn(module, msg, req->op.rename.newdn);
-	if (!tdb_key.dptr) {
+	key = ldb_kv_key_dn(module, msg, req->op.rename.newdn);
+	if (!key.data) {
 		talloc_free(msg);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
-	tdb_key_old = ldb_kv_key_dn(module, msg, req->op.rename.olddn);
-	if (!tdb_key_old.dptr) {
+	key_old = ldb_kv_key_dn(module, msg, req->op.rename.olddn);
+	if (!key_old.data) {
 		talloc_free(msg);
-		talloc_free(tdb_key.dptr);
+		talloc_free(key.data);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
@@ -1283,8 +1275,8 @@ static int ldb_kv_rename(struct ldb_kv_context *ctx)
 	 * Only declare a conflict if the new DN already exists,
 	 * and it isn't a case change on the old DN
 	 */
-	if (tdb_key_old.dsize != tdb_key.dsize
-	    || memcmp(tdb_key.dptr, tdb_key_old.dptr, tdb_key.dsize) != 0) {
+	if (key_old.length != key.length
+	    || memcmp(key.data, key_old.data, key.length) != 0) {
 		ret = ldb_kv_search_base(
 		    module, msg, req->op.rename.newdn, &db_dn);
 		if (ret == LDB_SUCCESS) {
@@ -1302,14 +1294,14 @@ static int ldb_kv_rename(struct ldb_kv_context *ctx)
 				       ldb_dn_get_linearized(req->op.rename.newdn));
 	}
 	if (ret != LDB_SUCCESS) {
-		talloc_free(tdb_key_old.dptr);
-		talloc_free(tdb_key.dptr);
+		talloc_free(key_old.data);
+		talloc_free(key.data);
 		talloc_free(msg);
 		return ret;
 	}
 
-	talloc_free(tdb_key_old.dptr);
-	talloc_free(tdb_key.dptr);
+	talloc_free(key_old.data);
+	talloc_free(key.data);
 
 	/* Always delete first then add, to avoid conflicts with
 	 * unique indexes. We rely on the transaction to make this
diff --git a/lib/ldb/ldb_key_value/ldb_kv.h b/lib/ldb/ldb_key_value/ldb_kv.h
index 2ea36ca..5070a58 100644
--- a/lib/ldb/ldb_key_value/ldb_kv.h
+++ b/lib/ldb/ldb_key_value/ldb_kv.h
@@ -204,7 +204,7 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 			   struct ldb_kv_private *ldb_kv,
 			   TALLOC_CTX *mem_ctx,
 			   struct ldb_dn *dn,
-			   TDB_DATA *tdb_key);
+			  struct ldb_val *key);
 
 /*
  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_search.c
@@ -219,7 +219,7 @@ int ldb_kv_search_base(struct ldb_module *module,
 		       struct ldb_dn **ret_dn);
 int ldb_kv_search_key(struct ldb_module *module,
 		      struct ldb_kv_private *ldb_kv,
-		      struct TDB_DATA tdb_key,
+		      const struct ldb_val ldb_key,
 		      struct ldb_message *msg,
 		      unsigned int unpack_flags);
 int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
@@ -234,22 +234,22 @@ int ldb_kv_search(struct ldb_kv_context *ctx);
  * Determine if this key could hold a record.  We allow the new GUID
  * index, the old DN index and a possible future ID=
  */
-bool ldb_kv_key_is_record(TDB_DATA key);
-TDB_DATA ldb_kv_key_dn(struct ldb_module *module,
-		       TALLOC_CTX *mem_ctx,
-		       struct ldb_dn *dn);
-TDB_DATA ldb_kv_key_msg(struct ldb_module *module,
-			TALLOC_CTX *mem_ctx,
-			const struct ldb_message *msg);
+bool ldb_kv_key_is_record(struct ldb_val key);
+struct ldb_val ldb_kv_key_dn(struct ldb_module *module,
+			     TALLOC_CTX *mem_ctx,
+			     struct ldb_dn *dn);
+struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
+			     TALLOC_CTX *mem_ctx,
+			      const struct ldb_message *msg);
 int ldb_kv_guid_to_key(struct ldb_module *module,
 		       struct ldb_kv_private *ldb_kv,
 		       const struct ldb_val *GUID_val,
-		       TDB_DATA *key);
+		       struct ldb_val *key);
 int ldb_kv_idx_to_key(struct ldb_module *module,
 		      struct ldb_kv_private *ldb_kv,
 		      TALLOC_CTX *mem_ctx,
 		      const struct ldb_val *idx_val,
-		      TDB_DATA *key);
+		      struct ldb_val *key);
 int ldb_kv_store(struct ldb_module *module,
 		 const struct ldb_message *msg,
 		 int flgs);
diff --git a/lib/ldb/ldb_key_value/ldb_kv_index.c b/lib/ldb/ldb_key_value/ldb_kv_index.c
index 96ebd6b..550f4b6 100644
--- a/lib/ldb/ldb_key_value/ldb_kv_index.c
+++ b/lib/ldb/ldb_key_value/ldb_kv_index.c
@@ -483,7 +483,7 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 			   struct ldb_kv_private *ldb_kv,
 			   TALLOC_CTX *mem_ctx,
 			   struct ldb_dn *dn,
-			   TDB_DATA *tdb_key)
+			   struct ldb_val *ldb_key)
 {
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	int ret;
@@ -529,9 +529,9 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 		index = -1;
 		for (i=0; i < list->count; i++) {
 			uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
-			TDB_DATA key = {
-				.dptr = guid_key,
-				.dsize = sizeof(guid_key)
+			struct ldb_val key = {
+				.data = guid_key,
+				.length = sizeof(guid_key)
 			};
 			const int flags = LDB_UNPACK_DATA_FLAG_NO_ATTRS;
 			struct ldb_message *rec = ldb_msg_new(ldb);
@@ -550,8 +550,8 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 
 			ret =
 			    ldb_kv_search_key(module, ldb_kv, key, rec, flags);
-			if (key.dptr != guid_key) {
-				TALLOC_FREE(key.dptr);
+			if (key.data != guid_key) {
+				TALLOC_FREE(key.data);
 			}
 			if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 				/*
@@ -592,8 +592,8 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 		}
 	}
 
-	/* The tdb_key memory is allocated by the caller */
-	ret = ldb_kv_guid_to_key(module, ldb_kv, &list->dn[index], tdb_key);
+	/* The ldb_key memory is allocated by the caller */
+	ret = ldb_kv_guid_to_key(module, ldb_kv, &list->dn[index], ldb_key);
 	TALLOC_FREE(list);
 
 	if (ret != LDB_SUCCESS) {
@@ -1753,7 +1753,7 @@ static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 	unsigned int i;
 	unsigned int num_keys = 0;
 	uint8_t previous_guid_key[LDB_KV_GUID_KEY_SIZE] = {};
-	TDB_DATA *keys = NULL;
+	struct ldb_val *keys = NULL;
 
 	/*
 	 * We have to allocate the key list (rather than just walk the
@@ -1761,7 +1761,7 @@ static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 	 * (by modifying an indexed attribute hosted in the in-memory
 	 * index cache!)
 	 */
-	keys = talloc_array(ac, TDB_DATA, dn_list->count);
+	keys = talloc_array(ac, struct ldb_val, dn_list->count);
 	if (keys == NULL) {
 		return ldb_module_oom(ac->module);
 	}
@@ -1785,13 +1785,13 @@ static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 			return ldb_module_oom(ac->module);
 		}
 		for (i = 0; i < dn_list->count; i++) {
-			keys[i].dptr = key_values[i].guid_key;
-			keys[i].dsize = sizeof(key_values[i].guid_key);
+			keys[i].data = key_values[i].guid_key;
+			keys[i].length = sizeof(key_values[i].guid_key);
 		}
 	} else {
 		for (i = 0; i < dn_list->count; i++) {
-			keys[i].dptr = NULL;
-			keys[i].dsize = 0;
+			keys[i].data = NULL;
+			keys[i].length = 0;
 		}
 	}
 
@@ -1818,13 +1818,13 @@ static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 			 */
 
 			if (memcmp(previous_guid_key,
-				   keys[num_keys].dptr,
+				   keys[num_keys].data,
 				   sizeof(previous_guid_key)) == 0) {
 				continue;
 			}
 
 			memcpy(previous_guid_key,
-			       keys[num_keys].dptr,
+			       keys[num_keys].data,
 			       sizeof(previous_guid_key));
 		}
 		num_keys++;
@@ -2187,9 +2187,9 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		int i;
 		for (i=0; i < list->count; i++) {
 			uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
-			TDB_DATA key = {
-				.dptr = guid_key,
-				.dsize = sizeof(guid_key)
+			struct ldb_val key = {
+				.data = guid_key,
+				.length = sizeof(guid_key)
 			};
 			const int flags = LDB_UNPACK_DATA_FLAG_NO_ATTRS;
 			struct ldb_message *rec = ldb_msg_new(ldb);
@@ -2207,8 +2207,8 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 
 			ret =
 			    ldb_kv_search_key(module, ldb_kv, key, rec, flags);
-			if (key.dptr != guid_key) {
-				TALLOC_FREE(key.dptr);
+			if (key.data != guid_key) {
+				TALLOC_FREE(key.data);
 			}
 			if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 				/*
@@ -2849,7 +2849,7 @@ static int delete_index(struct ldb_kv_private *ldb_kv,
   traversal function that adds @INDEX records during a re index TODO wrong comment
 */
 static int re_key(struct ldb_kv_private *ldb_kv,
-		  struct ldb_val ldb_key,
+		  struct ldb_val key,
 		  struct ldb_val val,
 		  void *state)
 {
@@ -2860,17 +2860,13 @@ static int re_key(struct ldb_kv_private *ldb_kv,
 	struct ldb_message *msg;
 	unsigned int nb_elements_in_db;
 	int ret;
-	TDB_DATA key2;
+	struct ldb_val key2;
 	bool is_record;
-	TDB_DATA key = {
-		.dptr = ldb_key.data,
-		.dsize = ldb_key.length
-	};
 
 	ldb = ldb_module_get_ctx(module);
 
-	if (key.dsize > 4 &&
-	    memcmp(key.dptr, "DN=@", 4) == 0) {
+	if (key.length > 4 &&
+	    memcmp(key.data, "DN=@", 4) == 0) {
 		return 0;
 	}
 
@@ -2901,8 +2897,8 @@ static int re_key(struct ldb_kv_private *ldb_kv,
 		ldb_debug(ldb, LDB_DEBUG_ERROR,
 			  "Refusing to re-index as GUID "
 			  "key %*.*s with no DN\n",
-			  (int)key.dsize, (int)key.dsize,
-			  (char *)key.dptr);
+			  (int)key.length, (int)key.length,
+			  (char *)key.data);
 		talloc_free(msg);
 		return -1;
 	}
@@ -2911,23 +2907,19 @@ static int re_key(struct ldb_kv_private *ldb_kv,
 	   insensitivity of an element changing, or a change from DN
 	   to GUID keys */
 	key2 = ldb_kv_key_msg(module, msg, msg);
-	if (key2.dptr == NULL) {
+	if (key2.data == NULL) {
 		/* probably a corrupt record ... darn */
 		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid DN in re_index: %s",
 						ldb_dn_get_linearized(msg->dn));
 		talloc_free(msg);
 		return 0;
 	}
-	if (key.dsize != key2.dsize ||
-	    (memcmp(key.dptr, key2.dptr, key.dsize) != 0)) {
-		struct ldb_val ldb_key2 = {
-			.data = key2.dptr,
-			.length = key2.dsize
-		};
+	if (key.length != key2.length ||
+	    (memcmp(key.data, key2.data, key.length) != 0)) {
 		ldb_kv->kv_ops->update_in_iterate(
-		    ldb_kv, ldb_key, ldb_key2, val, ctx);
+		    ldb_kv, key, key2, val, ctx);
 	}
-	talloc_free(key2.dptr);
+	talloc_free(key2.data);
 
 	talloc_free(msg);
 
@@ -2945,7 +2937,7 @@ static int re_key(struct ldb_kv_private *ldb_kv,
   traversal function that adds @INDEX records during a re index
 */
 static int re_index(struct ldb_kv_private *ldb_kv,
-		    struct ldb_val ldb_key,
+		    struct ldb_val key,
 		    struct ldb_val val,
 		    void *state)
 {
@@ -2955,17 +2947,13 @@ static int re_index(struct ldb_kv_private *ldb_kv,
 	struct ldb_module *module = ctx->module;
 	struct ldb_message *msg;
 	unsigned int nb_elements_in_db;
-	TDB_DATA key = {
-		.dptr = ldb_key.data,
-		.dsize = ldb_key.length
-	};
 	int ret;
 	bool is_record;
 
 	ldb = ldb_module_get_ctx(module);
 
-	if (key.dsize > 4 &&
-	    memcmp(key.dptr, "DN=@", 4) == 0) {
+	if (key.length > 4 &&
+	    memcmp(key.data, "DN=@", 4) == 0) {
 		return 0;
 	}
 
@@ -2996,8 +2984,8 @@ static int re_index(struct ldb_kv_private *ldb_kv,
 		ldb_debug(ldb, LDB_DEBUG_ERROR,
 			  "Refusing to re-index as GUID "
 			  "key %*.*s with no DN\n",
-			  (int)key.dsize, (int)key.dsize,
-			  (char *)key.dptr);
+			  (int)key.length, (int)key.length,
+			  (char *)key.data);
 		talloc_free(msg);
 		return -1;
 	}
diff --git a/lib/ldb/ldb_key_value/ldb_kv_search.c b/lib/ldb/ldb_key_value/ldb_kv_search.c
index ee9b4bc..cd7ff52 100644
--- a/lib/ldb/ldb_key_value/ldb_kv_search.c
+++ b/lib/ldb/ldb_key_value/ldb_kv_search.c
@@ -235,7 +235,7 @@ static int ldb_kv_parse_data_unpack(struct ldb_val key,
 */
 int ldb_kv_search_key(struct ldb_module *module,
 		      struct ldb_kv_private *ldb_kv,
-		      const struct TDB_DATA tdb_key,
+		      const struct ldb_val ldb_key,
 		      struct ldb_message *msg,
 		      unsigned int unpack_flags)
 {
@@ -245,10 +245,6 @@ int ldb_kv_search_key(struct ldb_module *module,
 		.module = module,
 		.unpack_flags = unpack_flags
 	};
-	struct ldb_val ldb_key = {
-		.data = tdb_key.dptr,
-		.length = tdb_key.dsize
-	};
 
 	memset(msg, 0, sizeof(*msg));
 
@@ -292,9 +288,9 @@ int ldb_kv_search_dn1(struct ldb_module *module,
 	    talloc_get_type(data, struct ldb_kv_private);
 	int ret;
 	uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
-	TDB_DATA tdb_key = {
-		.dptr = guid_key,
-		.dsize = sizeof(guid_key)
+	struct ldb_val key = {
+		.data = guid_key,
+		.length = sizeof(guid_key)
 	};
 	TALLOC_CTX *tdb_key_ctx = NULL;
 
@@ -307,8 +303,8 @@ int ldb_kv_search_dn1(struct ldb_module *module,
 		}
 
 		/* form the key */
-		tdb_key = ldb_kv_key_dn(module, tdb_key_ctx, dn);
-		if (!tdb_key.dptr) {
+		key = ldb_kv_key_dn(module, tdb_key_ctx, dn);
+		if (!key.data) {
 			TALLOC_FREE(tdb_key_ctx);
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
@@ -320,13 +316,13 @@ int ldb_kv_search_dn1(struct ldb_module *module,
 		 * used for internal memory.
 		 *
 		 */
-		ret = ldb_kv_key_dn_from_idx(module, ldb_kv, msg, dn, &tdb_key);
+		ret = ldb_kv_key_dn_from_idx(module, ldb_kv, msg, dn, &key);
 		if (ret != LDB_SUCCESS) {
 			return ret;
 		}
 	}
 
-	ret = ldb_kv_search_key(module, ldb_kv, tdb_key, msg, unpack_flags);
+	ret = ldb_kv_search_key(module, ldb_kv, key, msg, unpack_flags);
 
 	TALLOC_FREE(tdb_key_ctx);
 
@@ -504,15 +500,11 @@ static int search_func(struct ldb_kv_private *ldb_kv,
 	int ret;
 	bool matched;
 	unsigned int nb_elements_in_db;
-	TDB_DATA tdb_key = {
-		.dptr = key.data,
-		.dsize = key.length
-	};
 
 	ac = talloc_get_type(state, struct ldb_kv_context);
 	ldb = ldb_module_get_ctx(ac->module);
 
-	if (ldb_kv_key_is_record(tdb_key) == false) {
+	if (ldb_kv_key_is_record(key) == false) {
 		return 0;
 	}
 
-- 
2.7.4


From 109b333372d55e9638563eadc53b3e4f641fd0e1 Mon Sep 17 00:00:00 2001
From: Gary Lockyer <gary at catalyst.net.nz>
Date: Tue, 24 Jul 2018 12:44:19 +1200
Subject: [PATCH 18/18] lib ldb key value: reformat code

Reformat the lib/ldb/ldb_key_value code with clang-format.

Signed-off-by: Gary Lockyer <gary at catalyst.net.nz>
---
 lib/ldb/ldb_key_value/ldb_kv.c        | 367 +++++++++++++++++-------------
 lib/ldb/ldb_key_value/ldb_kv.h        |  31 ++-
 lib/ldb/ldb_key_value/ldb_kv_cache.c  |  92 ++++----
 lib/ldb/ldb_key_value/ldb_kv_index.c  | 411 +++++++++++++++++++---------------
 lib/ldb/ldb_key_value/ldb_kv_search.c | 164 ++++++++------
 5 files changed, 602 insertions(+), 463 deletions(-)

diff --git a/lib/ldb/ldb_key_value/ldb_kv.c b/lib/ldb/ldb_key_value/ldb_kv.c
index 3ea8d5e..76b3059 100644
--- a/lib/ldb/ldb_key_value/ldb_kv.c
+++ b/lib/ldb/ldb_key_value/ldb_kv.c
@@ -81,7 +81,8 @@ bool ldb_kv_key_is_record(struct ldb_val key)
 		return false;
 	}
 
-	if (memcmp(key.data, LDB_KV_GUID_KEY_PREFIX,
+	if (memcmp(key.data,
+		   LDB_KV_GUID_KEY_PREFIX,
 		   sizeof(LDB_KV_GUID_KEY_PREFIX) - 1) == 0) {
 		return true;
 	}
@@ -111,7 +112,7 @@ struct ldb_val ldb_kv_key_dn(struct ldb_module *module,
 	  there are 3 cases dealt with in this code:
 
 	  1) if the dn doesn't start with @ then uppercase the attribute
-             names and the attributes values of case insensitive attributes
+	     names and the attributes values of case insensitive attributes
 	  2) if the dn starts with @ then leave it alone -
 	     the indexing code handles the rest
 	*/
@@ -152,13 +153,12 @@ int ldb_kv_guid_to_key(struct ldb_module *module,
 	const char *GUID_prefix = LDB_KV_GUID_KEY_PREFIX;
 	const int GUID_prefix_len = sizeof(LDB_KV_GUID_KEY_PREFIX) - 1;
 
-	if (key->length != (GUID_val->length+GUID_prefix_len)) {
+	if (key->length != (GUID_val->length + GUID_prefix_len)) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
 	memcpy(key->data, GUID_prefix, GUID_prefix_len);
-	memcpy(&key->data[GUID_prefix_len],
-	       GUID_val->data, GUID_val->length);
+	memcpy(&key->data[GUID_prefix_len], GUID_val->data, GUID_val->length);
 	return LDB_SUCCESS;
 }
 
@@ -205,8 +205,8 @@ int ldb_kv_idx_to_key(struct ldb_module *module,
   GUID index is in use, or the DN is used as the key
 */
 struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
-			TALLOC_CTX *mem_ctx,
-			const struct ldb_message *msg)
+			      TALLOC_CTX *mem_ctx,
+			      const struct ldb_message *msg)
 {
 	void *data = ldb_module_get_private(module);
 	struct ldb_kv_private *ldb_kv =
@@ -269,20 +269,24 @@ static int ldb_kv_check_special_dn(struct ldb_module *module,
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 	unsigned int i, j;
 
-	if (! ldb_dn_is_special(msg->dn) ||
-	    ! ldb_dn_check_special(msg->dn, LDB_KV_ATTRIBUTES)) {
+	if (!ldb_dn_is_special(msg->dn) ||
+	    !ldb_dn_check_special(msg->dn, LDB_KV_ATTRIBUTES)) {
 		return LDB_SUCCESS;
 	}
 
 	/* we have @ATTRIBUTES, let's check attributes are fine */
 	/* should we check that we deny multivalued attributes ? */
 	for (i = 0; i < msg->num_elements; i++) {
-		if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") == 0) continue;
+		if (ldb_attr_cmp(msg->elements[i].name, "distinguishedName") ==
+		    0)
+			continue;
 
 		for (j = 0; j < msg->elements[i].num_values; j++) {
 			if (ldb_kv_check_at_attributes_values(
 				&msg->elements[i].values[j]) != 0) {
-				ldb_set_errstring(ldb, "Invalid attribute value in an @ATTRIBUTES entry");
+				ldb_set_errstring(ldb, "Invalid attribute "
+						       "value in an "
+						       "@ATTRIBUTES entry");
 				return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
 			}
 		}
@@ -291,7 +295,6 @@ static int ldb_kv_check_special_dn(struct ldb_module *module,
 	return LDB_SUCCESS;
 }
 
-
 /*
   we've made a modification to a dn - possibly reindex and
   update sequence number
@@ -305,14 +308,14 @@ static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
 	/* only allow modifies inside a transaction, otherwise the
 	 * ldb is unsafe */
 	if (ldb_kv->kv_ops->transaction_active(ldb_kv) == false) {
-		ldb_set_errstring(ldb_module_get_ctx(module), "ltdb modify without transaction");
+		ldb_set_errstring(ldb_module_get_ctx(module),
+				  "ltdb modify without transaction");
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
 	if (ldb_dn_is_special(dn) &&
 	    (ldb_dn_check_special(dn, LDB_KV_INDEXLIST) ||
-	     ldb_dn_check_special(dn, LDB_KV_ATTRIBUTES)) )
-	{
+	     ldb_dn_check_special(dn, LDB_KV_ATTRIBUTES))) {
 		if (ldb_kv->warn_reindex) {
 			ldb_debug(ldb_module_get_ctx(module),
 				  LDB_DEBUG_ERROR,
@@ -323,17 +326,19 @@ static int ldb_kv_modified(struct ldb_module *module, struct ldb_dn *dn)
 		ret = ldb_kv_reindex(module);
 	}
 
-	/* If the modify was to a normal record, or any special except @BASEINFO, update the seq number */
+	/*
+	 * If the modify was to a normal record, or any special except
+	 * @BASEINFO, update the seq number
+	 */
 	if (ret == LDB_SUCCESS &&
 	    !(ldb_dn_is_special(dn) &&
-	      ldb_dn_check_special(dn, LDB_KV_BASEINFO)) ) {
+	      ldb_dn_check_special(dn, LDB_KV_BASEINFO))) {
 		ret = ldb_kv_increase_sequence_number(module);
 	}
 
 	/* If the modify was to @OPTIONS, reload the cache */
-	if (ret == LDB_SUCCESS &&
-	    ldb_dn_is_special(dn) &&
-	    (ldb_dn_check_special(dn, LDB_KV_OPTIONS)) ) {
+	if (ret == LDB_SUCCESS && ldb_dn_is_special(dn) &&
+	    (ldb_dn_check_special(dn, LDB_KV_OPTIONS))) {
 		ret = ldb_kv_cache_reload(module);
 	}
 
@@ -373,8 +378,7 @@ int ldb_kv_store(struct ldb_module *module,
 		return LDB_ERR_OTHER;
 	}
 
-	ret = ldb_pack_data(ldb_module_get_ctx(module),
-			    msg, &ldb_data);
+	ret = ldb_pack_data(ldb_module_get_ctx(module), msg, &ldb_data);
 	if (ret == -1) {
 		TALLOC_FREE(key_ctx);
 		return LDB_ERR_OTHER;
@@ -403,14 +407,14 @@ done:
 	return ret;
 }
 
-
 /*
   check if a attribute is a single valued, for a given element
  */
 static bool ldb_kv_single_valued(const struct ldb_schema_attribute *a,
 				 struct ldb_message_element *el)
 {
-	if (!a) return false;
+	if (!a)
+		return false;
 	if (el != NULL) {
 		if (el->flags & LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK) {
 			/* override from a ldb module, for example
@@ -441,19 +445,27 @@ static int ldb_kv_add_internal(struct ldb_module *module,
 	int ret = LDB_SUCCESS;
 	unsigned int i;
 
-	for (i=0;i<msg->num_elements;i++) {
+	for (i = 0; i < msg->num_elements; i++) {
 		struct ldb_message_element *el = &msg->elements[i];
-		const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
+		const struct ldb_schema_attribute *a =
+		    ldb_schema_attribute_by_name(ldb, el->name);
 
 		if (el->num_values == 0) {
-			ldb_asprintf_errstring(ldb, "attribute '%s' on '%s' specified, but with 0 values (illegal)",
-					       el->name, ldb_dn_get_linearized(msg->dn));
+			ldb_asprintf_errstring(ldb,
+					       "attribute '%s' on '%s' "
+					       "specified, but with 0 values "
+					       "(illegal)",
+					       el->name,
+					       ldb_dn_get_linearized(msg->dn));
 			return LDB_ERR_CONSTRAINT_VIOLATION;
 		}
 		if (check_single_value && el->num_values > 1 &&
 		    ldb_kv_single_valued(a, el)) {
-			ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
-					       el->name, ldb_dn_get_linearized(msg->dn));
+			ldb_asprintf_errstring(ldb,
+					       "SINGLE-VALUE attribute %s on "
+					       "%s specified more than once",
+					       el->name,
+					       ldb_dn_get_linearized(msg->dn));
 			return LDB_ERR_CONSTRAINT_VIOLATION;
 		}
 
@@ -468,20 +480,20 @@ static int ldb_kv_add_internal(struct ldb_module *module,
 		      LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
 			struct ldb_val *duplicate = NULL;
 
-			ret = ldb_msg_find_duplicate_val(ldb, discard_const(msg),
-							 el, &duplicate, 0);
+			ret = ldb_msg_find_duplicate_val(
+			    ldb, discard_const(msg), el, &duplicate, 0);
 			if (ret != LDB_SUCCESS) {
 				return ret;
 			}
 			if (duplicate != NULL) {
 				ldb_asprintf_errstring(
-					ldb,
-					"attribute '%s': value '%.*s' on '%s' "
-					"provided more than once in ADD object",
-					el->name,
-					(int)duplicate->length,
-					duplicate->data,
-					ldb_dn_get_linearized(msg->dn));
+				    ldb,
+				    "attribute '%s': value '%.*s' on '%s' "
+				    "provided more than once in ADD object",
+				    el->name,
+				    (int)duplicate->length,
+				    duplicate->data,
+				    ldb_dn_get_linearized(msg->dn));
 				return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
 			}
 		}
@@ -553,7 +565,8 @@ static int ldb_kv_add(struct ldb_kv_context *ctx)
 	    !ldb_dn_is_special(req->op.add.message->dn)) {
 		ldb_set_errstring(ldb_module_get_ctx(module),
 				  "Must operate ldb_mdb in GUID "
-				  "index mode, but " LDB_KV_IDXGUID " not set.");
+				  "index mode, but " LDB_KV_IDXGUID
+				  " not set.");
 		return LDB_ERR_UNWILLING_TO_PERFORM;
 	}
 
@@ -682,7 +695,7 @@ static int ldb_kv_delete(struct ldb_kv_context *ctx)
 static int ldb_kv_find_element(const struct ldb_message *msg, const char *name)
 {
 	unsigned int i;
-	for (i=0;i<msg->num_elements;i++) {
+	for (i = 0; i < msg->num_elements; i++) {
 		if (ldb_attr_cmp(msg->elements[i].name, name) == 0) {
 			return i;
 		}
@@ -690,7 +703,6 @@ static int ldb_kv_find_element(const struct ldb_message *msg, const char *name)
 	return -1;
 }
 
-
 /*
   add an element to an existing record. Assumes a elements array that we
   can call re-alloc on, and assumed that we can re-use the data pointers from
@@ -709,8 +721,10 @@ static int ldb_kv_msg_add_element(struct ldb_message *msg,
 		return 0;
 	}
 
-	e2 = talloc_realloc(msg, msg->elements, struct ldb_message_element,
-			      msg->num_elements+1);
+	e2 = talloc_realloc(msg,
+			    msg->elements,
+			    struct ldb_message_element,
+			    msg->num_elements + 1);
 	if (!e2) {
 		errno = ENOMEM;
 		return -1;
@@ -722,13 +736,13 @@ static int ldb_kv_msg_add_element(struct ldb_message *msg,
 
 	e2->name = el->name;
 	e2->flags = el->flags;
-	e2->values = talloc_array(msg->elements,
-				  struct ldb_val, el->num_values);
+	e2->values =
+	    talloc_array(msg->elements, struct ldb_val, el->num_values);
 	if (!e2->values) {
 		errno = ENOMEM;
 		return -1;
 	}
-	for (i=0;i<el->num_values;i++) {
+	for (i = 0; i < el->num_values; i++) {
 		e2->values[i] = el->values[i];
 	}
 	e2->num_values = el->num_values;
@@ -773,13 +787,13 @@ static int ldb_kv_msg_delete_attribute(struct ldb_module *module,
 	}
 
 	talloc_free(el->values);
-	if (msg->num_elements > (i+1)) {
-		memmove(el, el+1, sizeof(*el) * (msg->num_elements - (i+1)));
+	if (msg->num_elements > (i + 1)) {
+		memmove(
+		    el, el + 1, sizeof(*el) * (msg->num_elements - (i + 1)));
 	}
 	msg->num_elements--;
-	msg->elements = talloc_realloc(msg, msg->elements,
-				       struct ldb_message_element,
-				       msg->num_elements);
+	msg->elements = talloc_realloc(
+	    msg, msg->elements, struct ldb_message_element, msg->num_elements);
 	return LDB_SUCCESS;
 }
 
@@ -805,20 +819,25 @@ static int ldb_kv_msg_delete_element(struct ldb_module *module,
 		return LDB_ERR_NO_SUCH_ATTRIBUTE;
 	}
 
-	i = (unsigned int) found;
+	i = (unsigned int)found;
 	el = &(msg->elements[i]);
 
 	a = ldb_schema_attribute_by_name(ldb, el->name);
 
-	for (i=0;i<el->num_values;i++) {
+	for (i = 0; i < el->num_values; i++) {
 		bool matched;
 		if (a->syntax->operator_fn) {
-			ret = a->syntax->operator_fn(ldb, LDB_OP_EQUALITY, a,
-						     &el->values[i], val, &matched);
-			if (ret != LDB_SUCCESS) return ret;
+			ret = a->syntax->operator_fn(ldb,
+						     LDB_OP_EQUALITY,
+						     a,
+						     &el->values[i],
+						     val,
+						     &matched);
+			if (ret != LDB_SUCCESS)
+				return ret;
 		} else {
-			matched = (a->syntax->comparison_fn(ldb, ldb,
-							    &el->values[i], val) == 0);
+			matched = (a->syntax->comparison_fn(
+				       ldb, ldb, &el->values[i], val) == 0);
 		}
 		if (matched) {
 			if (el->num_values == 1) {
@@ -832,10 +851,11 @@ static int ldb_kv_msg_delete_element(struct ldb_module *module,
 				return ret;
 			}
 
-			if (i<el->num_values-1) {
-				memmove(&el->values[i], &el->values[i+1],
-					sizeof(el->values[i])*
-						(el->num_values-(i+1)));
+			if (i < el->num_values - 1) {
+				memmove(&el->values[i],
+					&el->values[i + 1],
+					sizeof(el->values[i]) *
+					    (el->num_values - (i + 1)));
 			}
 			el->num_values--;
 
@@ -877,8 +897,8 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 	}
 
 	if (req) {
-		control_permissive = ldb_request_get_control(req,
-					LDB_CONTROL_PERMISSIVE_MODIFY_OID);
+		control_permissive = ldb_request_get_control(
+		    req, LDB_CONTROL_PERMISSIVE_MODIFY_OID);
 	}
 
 	msg2 = ldb_msg_new(mem_ctx);
@@ -893,10 +913,11 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 		goto done;
 	}
 
-	for (i=0; i<msg->num_elements; i++) {
+	for (i = 0; i < msg->num_elements; i++) {
 		struct ldb_message_element *el = &msg->elements[i], *el2;
 		struct ldb_val *vals;
-		const struct ldb_schema_attribute *a = ldb_schema_attribute_by_name(ldb, el->name);
+		const struct ldb_schema_attribute *a =
+		    ldb_schema_attribute_by_name(ldb, el->name);
 		const char *dn;
 		uint32_t options = 0;
 		if (control_permissive != NULL) {
@@ -907,9 +928,12 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 		case LDB_FLAG_MOD_ADD:
 
 			if (el->num_values == 0) {
-				ldb_asprintf_errstring(ldb,
-						       "attribute '%s': attribute on '%s' specified, but with 0 values (illegal)",
-						       el->name, ldb_dn_get_linearized(msg2->dn));
+				ldb_asprintf_errstring(
+				    ldb,
+				    "attribute '%s': attribute on '%s' "
+				    "specified, but with 0 values (illegal)",
+				    el->name,
+				    ldb_dn_get_linearized(msg2->dn));
 				ret = LDB_ERR_CONSTRAINT_VIOLATION;
 				goto done;
 			}
@@ -926,19 +950,25 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 					goto done;
 				}
 				*el = msg->elements[i];
-				el->values = talloc_array(el, struct ldb_val, el->num_values);
+				el->values = talloc_array(
+				    el, struct ldb_val, el->num_values);
 				if (el->values == NULL) {
 					ret = LDB_ERR_OTHER;
 					goto done;
 				}
 				for (j = 0; j < el->num_values; j++) {
-					el->values[j] = msg->elements[i].values[j];
+					el->values[j] =
+					    msg->elements[i].values[j];
 				}
 			}
 
 			if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
-				ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
-						       el->name, ldb_dn_get_linearized(msg2->dn));
+				ldb_asprintf_errstring(
+				    ldb,
+				    "SINGLE-VALUE attribute %s on %s specified "
+				    "more than once",
+				    el->name,
+				    ldb_dn_get_linearized(msg2->dn));
 				ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
 				goto done;
 			}
@@ -956,14 +986,18 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 					goto done;
 				}
 			} else {
-				j = (unsigned int) idx;
+				j = (unsigned int)idx;
 				el2 = &(msg2->elements[j]);
 
 				/* We cannot add another value on a existing one
 				   if the attribute is single-valued */
 				if (ldb_kv_single_valued(a, el)) {
-					ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
-						               el->name, ldb_dn_get_linearized(msg2->dn));
+					ldb_asprintf_errstring(
+					    ldb,
+					    "SINGLE-VALUE attribute %s on %s "
+					    "specified more than once",
+					    el->name,
+					    ldb_dn_get_linearized(msg2->dn));
 					ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
 					goto done;
 				}
@@ -973,41 +1007,44 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 				if (!(el->flags &
 				      LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
 					struct ldb_val *duplicate = NULL;
-					ret = ldb_msg_find_common_values(ldb,
-									 msg2,
-									 el,
-									 el2,
-									 options);
+					ret = ldb_msg_find_common_values(
+					    ldb, msg2, el, el2, options);
 
 					if (ret ==
 					    LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS) {
-						ldb_asprintf_errstring(ldb,
-							"attribute '%s': value "
-							"#%u on '%s' already "
-							"exists", el->name, j,
-							ldb_dn_get_linearized(msg2->dn));
+						ldb_asprintf_errstring(
+						    ldb,
+						    "attribute '%s': value "
+						    "#%u on '%s' already "
+						    "exists",
+						    el->name,
+						    j,
+						    ldb_dn_get_linearized(
+							msg2->dn));
 						goto done;
 					} else if (ret != LDB_SUCCESS) {
 						goto done;
 					}
 
 					ret = ldb_msg_find_duplicate_val(
-						ldb, msg2, el, &duplicate, 0);
+					    ldb, msg2, el, &duplicate, 0);
 					if (ret != LDB_SUCCESS) {
 						goto done;
 					}
 					if (duplicate != NULL) {
 						ldb_asprintf_errstring(
-							ldb,
-							"attribute '%s': value "
-							"'%.*s' on '%s' "
-							"provided more than "
-							"once in ADD",
-							el->name,
-							(int)duplicate->length,
-							duplicate->data,
-							ldb_dn_get_linearized(msg->dn));
-						ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
+						    ldb,
+						    "attribute '%s': value "
+						    "'%.*s' on '%s' "
+						    "provided more than "
+						    "once in ADD",
+						    el->name,
+						    (int)duplicate->length,
+						    duplicate->data,
+						    ldb_dn_get_linearized(
+							msg->dn));
+						ret =
+						    LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
 						goto done;
 					}
 				}
@@ -1015,17 +1052,19 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 				/* Now combine existing and new values to a new
 				   attribute record */
 				vals = talloc_realloc(msg2->elements,
-						      el2->values, struct ldb_val,
-						      el2->num_values + el->num_values);
+						      el2->values,
+						      struct ldb_val,
+						      el2->num_values +
+							  el->num_values);
 				if (vals == NULL) {
 					ldb_oom(ldb);
 					ret = LDB_ERR_OTHER;
 					goto done;
 				}
 
-				for (j=0; j<el->num_values; j++) {
+				for (j = 0; j < el->num_values; j++) {
 					vals[el2->num_values + j] =
-						ldb_val_dup(vals, &el->values[j]);
+					    ldb_val_dup(vals, &el->values[j]);
 				}
 
 				el2->values = vals;
@@ -1043,8 +1082,12 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 		case LDB_FLAG_MOD_REPLACE:
 
 			if (el->num_values > 1 && ldb_kv_single_valued(a, el)) {
-				ldb_asprintf_errstring(ldb, "SINGLE-VALUE attribute %s on %s specified more than once",
-						       el->name, ldb_dn_get_linearized(msg2->dn));
+				ldb_asprintf_errstring(
+				    ldb,
+				    "SINGLE-VALUE attribute %s on %s specified "
+				    "more than once",
+				    el->name,
+				    ldb_dn_get_linearized(msg2->dn));
 				ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
 				goto done;
 			}
@@ -1055,24 +1098,25 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 			 * in Samba, or someone else who can claim to
 			 * know what they are doing.
 			 */
-			if (!(el->flags & LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
+			if (!(el->flags &
+			      LDB_FLAG_INTERNAL_DISABLE_SINGLE_VALUE_CHECK)) {
 				struct ldb_val *duplicate = NULL;
 
-				ret = ldb_msg_find_duplicate_val(ldb, msg2, el,
-								 &duplicate, 0);
+				ret = ldb_msg_find_duplicate_val(
+				    ldb, msg2, el, &duplicate, 0);
 				if (ret != LDB_SUCCESS) {
 					goto done;
 				}
 				if (duplicate != NULL) {
 					ldb_asprintf_errstring(
-						ldb,
-						"attribute '%s': value '%.*s' "
-						"on '%s' provided more than "
-						"once in REPLACE",
-						el->name,
-						(int)duplicate->length,
-						duplicate->data,
-						ldb_dn_get_linearized(msg2->dn));
+					    ldb,
+					    "attribute '%s': value '%.*s' "
+					    "on '%s' provided more than "
+					    "once in REPLACE",
+					    el->name,
+					    (int)duplicate->length,
+					    duplicate->data,
+					    ldb_dn_get_linearized(msg2->dn));
 					ret = LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
 					goto done;
 				}
@@ -1081,7 +1125,7 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 			/* Checks if element already exists */
 			idx = ldb_kv_find_element(msg2, el->name);
 			if (idx != -1) {
-				j = (unsigned int) idx;
+				j = (unsigned int)idx;
 				el2 = &(msg2->elements[j]);
 
 				/* we consider two elements to be
@@ -1095,7 +1139,8 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 					continue;
 				}
 
-				/* Delete the attribute if it exists in the DB */
+				/* Delete the attribute if it exists in the DB
+				 */
 				if (ldb_kv_msg_delete_attribute(
 					module, ldb_kv, msg2, el->name) != 0) {
 					ret = LDB_ERR_OTHER;
@@ -1135,16 +1180,20 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 				    control_permissive) {
 					ret = LDB_SUCCESS;
 				} else {
-					ldb_asprintf_errstring(ldb,
-							       "attribute '%s': no such attribute for delete on '%s'",
-							       msg->elements[i].name, dn);
+					ldb_asprintf_errstring(
+					    ldb,
+					    "attribute '%s': no such attribute "
+					    "for delete on '%s'",
+					    msg->elements[i].name,
+					    dn);
 				}
 				if (ret != LDB_SUCCESS) {
 					goto done;
 				}
 			} else {
 				/* Delete specified values from an attribute */
-				for (j=0; j < msg->elements[i].num_values; j++) {
+				for (j = 0; j < msg->elements[i].num_values;
+				     j++) {
 					ret = ldb_kv_msg_delete_element(
 					    module,
 					    ldb_kv,
@@ -1154,10 +1203,16 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 					if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE &&
 					    control_permissive) {
 						ret = LDB_SUCCESS;
-					} else if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
-						ldb_asprintf_errstring(ldb,
-								       "attribute '%s': no matching attribute value while deleting attribute on '%s'",
-								       msg->elements[i].name, dn);
+					} else if (ret ==
+						   LDB_ERR_NO_SUCH_ATTRIBUTE) {
+						ldb_asprintf_errstring(
+						    ldb,
+						    "attribute '%s': no "
+						    "matching attribute value "
+						    "while deleting attribute "
+						    "on '%s'",
+						    msg->elements[i].name,
+						    dn);
 					}
 					if (ret != LDB_SUCCESS) {
 						goto done;
@@ -1167,9 +1222,12 @@ int ldb_kv_modify_internal(struct ldb_module *module,
 			break;
 		default:
 			ldb_asprintf_errstring(ldb,
-					       "attribute '%s': invalid modify flags on '%s': 0x%x",
-					       msg->elements[i].name, ldb_dn_get_linearized(msg->dn),
-					       msg->elements[i].flags & LDB_FLAG_MOD_MASK);
+					       "attribute '%s': invalid modify "
+					       "flags on '%s': 0x%x",
+					       msg->elements[i].name,
+					       ldb_dn_get_linearized(msg->dn),
+					       msg->elements[i].flags &
+						   LDB_FLAG_MOD_MASK);
 			ret = LDB_ERR_PROTOCOL_ERROR;
 			goto done;
 		}
@@ -1227,7 +1285,7 @@ static int ldb_kv_rename(struct ldb_kv_context *ctx)
 	struct ldb_request *req = ctx->req;
 	struct ldb_message *msg;
 	int ret = LDB_SUCCESS;
-	struct ldb_val  key, key_old;
+	struct ldb_val key, key_old;
 	struct ldb_dn *db_dn;
 
 	ldb_request_set_state(req, LDB_ASYNC_PENDING);
@@ -1275,8 +1333,8 @@ static int ldb_kv_rename(struct ldb_kv_context *ctx)
 	 * Only declare a conflict if the new DN already exists,
 	 * and it isn't a case change on the old DN
 	 */
-	if (key_old.length != key.length
-	    || memcmp(key.data, key_old.data, key.length) != 0) {
+	if (key_old.length != key.length ||
+	    memcmp(key.data, key_old.data, key.length) != 0) {
 		ret = ldb_kv_search_base(
 		    module, msg, req->op.rename.newdn, &db_dn);
 		if (ret == LDB_SUCCESS) {
@@ -1289,9 +1347,10 @@ static int ldb_kv_rename(struct ldb_kv_context *ctx)
 	/* finding the new record already in the DB is an error */
 
 	if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       "Entry %s already exists",
-				       ldb_dn_get_linearized(req->op.rename.newdn));
+		ldb_asprintf_errstring(
+		    ldb_module_get_ctx(module),
+		    "Entry %s already exists",
+		    ldb_dn_get_linearized(req->op.rename.newdn));
 	}
 	if (ret != LDB_SUCCESS) {
 		talloc_free(key_old.data);
@@ -1501,8 +1560,7 @@ static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
 
 	ldb = ldb_module_get_ctx(module);
 
-	seq = talloc_get_type(req->op.extended.data,
-				struct ldb_seqnum_request);
+	seq = talloc_get_type(req->op.extended.data, struct ldb_seqnum_request);
 	if (seq == NULL) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
@@ -1544,14 +1602,17 @@ static int ldb_kv_sequence_number(struct ldb_kv_context *ctx,
 
 	switch (seq->type) {
 	case LDB_SEQ_HIGHEST_SEQ:
-		res->seq_num = ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
+		res->seq_num =
+		    ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
 		break;
 	case LDB_SEQ_NEXT:
-		res->seq_num = ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
+		res->seq_num =
+		    ldb_msg_find_attr_as_uint64(msg, LDB_KV_SEQUENCE_NUMBER, 0);
 		res->seq_num++;
 		break;
 	case LDB_SEQ_HIGHEST_TIMESTAMP:
-		date = ldb_msg_find_attr_as_string(msg, LDB_KV_MOD_TIMESTAMP, NULL);
+		date = ldb_msg_find_attr_as_string(
+		    msg, LDB_KV_MOD_TIMESTAMP, NULL);
 		if (date) {
 			res->seq_num = ldb_string_to_time(date);
 		} else {
@@ -1657,8 +1718,8 @@ static void ldb_kv_handle_extended(struct ldb_kv_context *ctx)
 	struct ldb_extended *ext = NULL;
 	int ret;
 
-	if (strcmp(ctx->req->op.extended.oid,
-		   LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
+	if (strcmp(ctx->req->op.extended.oid, LDB_EXTENDED_SEQUENCE_NUMBER) ==
+	    0) {
 		/* get sequence number */
 		ret = ldb_kv_sequence_number(ctx, &ext);
 	} else {
@@ -1748,14 +1809,16 @@ static int ldb_kv_handle_request(struct ldb_module *module,
 
 	ldb = ldb_module_get_ctx(module);
 
-	control_permissive = ldb_request_get_control(req,
-					LDB_CONTROL_PERMISSIVE_MODIFY_OID);
+	control_permissive =
+	    ldb_request_get_control(req, LDB_CONTROL_PERMISSIVE_MODIFY_OID);
 
 	for (i = 0; req->controls && req->controls[i]; i++) {
 		if (req->controls[i]->critical &&
 		    req->controls[i] != control_permissive) {
-			ldb_asprintf_errstring(ldb, "Unsupported critical extension %s",
-					       req->controls[i]->oid);
+			ldb_asprintf_errstring(
+			    ldb,
+			    "Unsupported critical extension %s",
+			    req->controls[i]->oid);
 			return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
 		}
 	}
@@ -1879,8 +1942,10 @@ int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
 	talloc_steal(ldb_kv->module, ldb_kv);
 
 	if (ldb_kv_cache_load(ldb_kv->module) != 0) {
-		ldb_asprintf_errstring(ldb, "Unable to load ltdb cache "
-				       "records for backend '%s'", name);
+		ldb_asprintf_errstring(ldb,
+				       "Unable to load ltdb cache "
+				       "records for backend '%s'",
+				       name);
 		talloc_free(ldb_kv->module);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
@@ -1897,8 +1962,7 @@ int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
 	 */
 	{
 		const char *len_str =
-			ldb_options_find(ldb, options,
-					 "max_key_len_for_self_test");
+		    ldb_options_find(ldb, options, "max_key_len_for_self_test");
 		if (len_str != NULL) {
 			unsigned len = strtoul(len_str, NULL, 0);
 			ldb_kv->max_key_length = len;
@@ -1913,9 +1977,8 @@ int ldb_kv_init_store(struct ldb_kv_private *ldb_kv,
 	 * triggered.
 	 */
 	{
-		const char *len_str =
-			ldb_options_find(ldb, options,
-					 "disable_full_db_scan_for_self_test");
+		const char *len_str = ldb_options_find(
+		    ldb, options, "disable_full_db_scan_for_self_test");
 		if (len_str != NULL) {
 			ldb_kv->disable_full_db_scan = true;
 		}
diff --git a/lib/ldb/ldb_key_value/ldb_kv.h b/lib/ldb/ldb_key_value/ldb_kv.h
index 5070a58..78e11a4 100644
--- a/lib/ldb/ldb_key_value/ldb_kv.h
+++ b/lib/ldb/ldb_key_value/ldb_kv.h
@@ -64,8 +64,7 @@ struct ldb_kv_private {
 		bool attribute_indexes;
 		const char *GUID_index_attribute;
 		const char *GUID_index_dn_component;
-	} *cache;
-
+	} * cache;
 
 	bool check_base;
 	bool disallow_dn_filter;
@@ -113,7 +112,7 @@ struct ldb_kv_context {
 	const struct ldb_parse_tree *tree;
 	struct ldb_dn *base;
 	enum ldb_scope scope;
-	const char * const *attrs;
+	const char *const *attrs;
 	struct tevent_timer *timeout_event;
 
 	/* error handling */
@@ -126,16 +125,15 @@ struct ldb_kv_reindex_context {
 	uint32_t count;
 };
 
-
 /* special record types */
-#define LDB_KV_INDEX      "@INDEX"
-#define LDB_KV_INDEXLIST  "@INDEXLIST"
-#define LDB_KV_IDX        "@IDX"
+#define LDB_KV_INDEX "@INDEX"
+#define LDB_KV_INDEXLIST "@INDEXLIST"
+#define LDB_KV_IDX "@IDX"
 #define LDB_KV_IDXVERSION "@IDXVERSION"
-#define LDB_KV_IDXATTR    "@IDXATTR"
-#define LDB_KV_IDXONE     "@IDXONE"
-#define LDB_KV_IDXDN     "@IDXDN"
-#define LDB_KV_IDXGUID    "@IDXGUID"
+#define LDB_KV_IDXATTR "@IDXATTR"
+#define LDB_KV_IDXONE "@IDXONE"
+#define LDB_KV_IDXDN "@IDXDN"
+#define LDB_KV_IDXGUID "@IDXGUID"
 #define LDB_KV_IDX_DN_GUID "@IDX_DN_GUID"
 
 /*
@@ -146,8 +144,8 @@ struct ldb_kv_reindex_context {
 
 #define LDB_KV_IDX_LMDB_SUBDB "@IDX_LMDB_SUBDB"
 
-#define LDB_KV_BASEINFO   "@BASEINFO"
-#define LDB_KV_OPTIONS    "@OPTIONS"
+#define LDB_KV_BASEINFO "@BASEINFO"
+#define LDB_KV_OPTIONS "@OPTIONS"
 #define LDB_KV_ATTRIBUTES "@ATTRIBUTES"
 
 /* special attribute types */
@@ -160,7 +158,8 @@ struct ldb_kv_reindex_context {
 /* DB keys */
 #define LDB_KV_GUID_KEY_PREFIX "GUID="
 #define LDB_KV_GUID_SIZE 16
-#define LDB_KV_GUID_KEY_SIZE (LDB_KV_GUID_SIZE + sizeof(LDB_KV_GUID_KEY_PREFIX) - 1)
+#define LDB_KV_GUID_KEY_SIZE                                                   \
+	(LDB_KV_GUID_SIZE + sizeof(LDB_KV_GUID_KEY_PREFIX) - 1)
 
 /*
  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_cache.c
@@ -204,7 +203,7 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 			   struct ldb_kv_private *ldb_kv,
 			   TALLOC_CTX *mem_ctx,
 			   struct ldb_dn *dn,
-			  struct ldb_val *key);
+			   struct ldb_val *key);
 
 /*
  * The following definitions come from lib/ldb/ldb_key_value/ldb_kv_search.c
@@ -239,7 +238,7 @@ struct ldb_val ldb_kv_key_dn(struct ldb_module *module,
 			     TALLOC_CTX *mem_ctx,
 			     struct ldb_dn *dn);
 struct ldb_val ldb_kv_key_msg(struct ldb_module *module,
-			     TALLOC_CTX *mem_ctx,
+			      TALLOC_CTX *mem_ctx,
 			      const struct ldb_message *msg);
 int ldb_kv_guid_to_key(struct ldb_module *module,
 		       struct ldb_kv_private *ldb_kv,
diff --git a/lib/ldb/ldb_key_value/ldb_kv_cache.c b/lib/ldb/ldb_key_value/ldb_kv_cache.c
index c39273f..2428a55 100644
--- a/lib/ldb/ldb_key_value/ldb_kv_cache.c
+++ b/lib/ldb/ldb_key_value/ldb_kv_cache.c
@@ -34,9 +34,9 @@
 #include "ldb_kv.h"
 #include "ldb_private.h"
 
-#define LDB_KV_FLAG_CASE_INSENSITIVE (1<<0)
-#define LDB_KV_FLAG_INTEGER          (1<<1)
-#define LDB_KV_FLAG_UNIQUE_INDEX     (1<<2)
+#define LDB_KV_FLAG_CASE_INSENSITIVE (1 << 0)
+#define LDB_KV_FLAG_INTEGER (1 << 1)
+#define LDB_KV_FLAG_UNIQUE_INDEX (1 << 2)
 
 /* valid attribute flags */
 static const struct {
@@ -59,7 +59,6 @@ static void ldb_kv_attributes_unload(struct ldb_module *module)
 	struct ldb_context *ldb = ldb_module_get_ctx(module);
 
 	ldb_schema_attribute_remove_flagged(ldb, LDB_ATTR_FLAG_FROM_DB);
-
 }
 
 /*
@@ -69,7 +68,7 @@ static int ldb_kv_attributes_flags(struct ldb_message_element *el, unsigned *v)
 {
 	unsigned int i;
 	unsigned value = 0;
-	for (i=0;i<el->num_values;i++) {
+	for (i = 0; i < el->num_values; i++) {
 		unsigned int j;
 		for (j = 0; ldb_kv_valid_attr_flags[j].name; j++) {
 			if (strcmp(ldb_kv_valid_attr_flags[j].name,
@@ -88,8 +87,10 @@ static int ldb_kv_attributes_flags(struct ldb_message_element *el, unsigned *v)
 
 static int ldb_schema_attribute_compare(const void *p1, const void *p2)
 {
-	const struct ldb_schema_attribute *sa1 = (const struct ldb_schema_attribute *)p1;
-	const struct ldb_schema_attribute *sa2 = (const struct ldb_schema_attribute *)p2;
+	const struct ldb_schema_attribute *sa1 =
+	    (const struct ldb_schema_attribute *)p1;
+	const struct ldb_schema_attribute *sa2 =
+	    (const struct ldb_schema_attribute *)p2;
 	return ldb_attr_cmp(sa1->name, sa2->name);
 }
 
@@ -109,7 +110,8 @@ static int ldb_kv_attributes_load(struct ldb_module *module)
 	ldb = ldb_module_get_ctx(module);
 
 	if (ldb->schema.attribute_handler_override) {
-		/* we skip loading the @ATTRIBUTES record when a module is supplying
+		/* we skip loading the @ATTRIBUTES record when a module is
+		   supplying
 		   its own attribute handling */
 		return 0;
 	}
@@ -120,7 +122,8 @@ static int ldb_kv_attributes_load(struct ldb_module *module)
 	}
 
 	dn = ldb_dn_new(module, ldb, LDB_KV_ATTRIBUTES);
-	if (dn == NULL) goto failed;
+	if (dn == NULL)
+		goto failed;
 
 	r = ldb_kv_search_dn1(module,
 			      dn,
@@ -137,10 +140,10 @@ static int ldb_kv_attributes_load(struct ldb_module *module)
 		return 0;
 	}
 
-	attrs = talloc_array(attrs_msg,
-			     struct ldb_schema_attribute,
-			     attrs_msg->num_elements
-			     + ldb->schema.num_attributes);
+	attrs =
+	    talloc_array(attrs_msg,
+			 struct ldb_schema_attribute,
+			 attrs_msg->num_elements + ldb->schema.num_attributes);
 	if (attrs == NULL) {
 		goto failed;
 	}
@@ -151,13 +154,13 @@ static int ldb_kv_attributes_load(struct ldb_module *module)
 
 	/* mapping these flags onto ldap 'syntaxes' isn't strictly correct,
 	   but its close enough for now */
-	for (i=0;i<attrs_msg->num_elements;i++) {
+	for (i = 0; i < attrs_msg->num_elements; i++) {
 		unsigned flags = 0, attr_flags = 0;
 		const char *syntax;
 		const struct ldb_schema_syntax *s;
 		const struct ldb_schema_attribute *a =
-			ldb_schema_attribute_by_name(ldb,
-						     attrs_msg->elements[i].name);
+		    ldb_schema_attribute_by_name(ldb,
+						 attrs_msg->elements[i].name);
 		if (a != NULL && a->flags & LDB_ATTR_FLAG_FIXED) {
 			/* Must already be set in the array, and kept */
 			continue;
@@ -165,7 +168,8 @@ static int ldb_kv_attributes_load(struct ldb_module *module)
 
 		if (ldb_kv_attributes_flags(&attrs_msg->elements[i], &flags) !=
 		    0) {
-			ldb_debug(ldb, LDB_DEBUG_ERROR,
+			ldb_debug(ldb,
+				  LDB_DEBUG_ERROR,
 				  "Invalid @ATTRIBUTES element for '%s'",
 				  attrs_msg->elements[i].name);
 			goto failed;
@@ -184,29 +188,35 @@ static int ldb_kv_attributes_load(struct ldb_module *module)
 		} else if (flags == 0) {
 			syntax = LDB_SYNTAX_OCTET_STRING;
 		} else {
-			ldb_debug(ldb, LDB_DEBUG_ERROR,
+			ldb_debug(ldb,
+				  LDB_DEBUG_ERROR,
 				  "Invalid flag combination 0x%x for '%s' "
 				  "in @ATTRIBUTES",
-				  flags, attrs_msg->elements[i].name);
+				  flags,
+				  attrs_msg->elements[i].name);
 			goto failed;
 		}
 
 		s = ldb_standard_syntax_by_name(ldb, syntax);
 		if (s == NULL) {
-			ldb_debug(ldb, LDB_DEBUG_ERROR,
+			ldb_debug(ldb,
+				  LDB_DEBUG_ERROR,
 				  "Invalid attribute syntax '%s' for '%s' "
 				  "in @ATTRIBUTES",
-				  syntax, attrs_msg->elements[i].name);
+				  syntax,
+				  attrs_msg->elements[i].name);
 			goto failed;
 		}
 
 		attr_flags |= LDB_ATTR_FLAG_ALLOCATED | LDB_ATTR_FLAG_FROM_DB;
 
-		r = ldb_schema_attribute_fill_with_syntax(ldb,
-							  attrs,
-							  attrs_msg->elements[i].name,
-							  attr_flags, s,
-							  &attrs[num_loaded_attrs + ldb->schema.num_attributes]);
+		r = ldb_schema_attribute_fill_with_syntax(
+		    ldb,
+		    attrs,
+		    attrs_msg->elements[i].name,
+		    attr_flags,
+		    s,
+		    &attrs[num_loaded_attrs + ldb->schema.num_attributes]);
 		if (r != 0) {
 			goto failed;
 		}
@@ -214,16 +224,19 @@ static int ldb_kv_attributes_load(struct ldb_module *module)
 	}
 
 	attrs = talloc_realloc(attrs_msg,
-			       attrs, struct ldb_schema_attribute,
+			       attrs,
+			       struct ldb_schema_attribute,
 			       num_loaded_attrs + ldb->schema.num_attributes);
 	if (attrs == NULL) {
 		goto failed;
 	}
-	TYPESAFE_QSORT(attrs, num_loaded_attrs + ldb->schema.num_attributes,
+	TYPESAFE_QSORT(attrs,
+		       num_loaded_attrs + ldb->schema.num_attributes,
 		       ldb_schema_attribute_compare);
 	talloc_unlink(ldb, ldb->schema.attributes);
 	ldb->schema.attributes = talloc_steal(ldb, attrs);
-	ldb->schema.num_attributes = num_loaded_attrs + ldb->schema.num_attributes;
+	ldb->schema.num_attributes =
+	    num_loaded_attrs + ldb->schema.num_attributes;
 	TALLOC_FREE(attrs_msg);
 
 	return 0;
@@ -304,8 +317,7 @@ static int ldb_kv_index_load(struct ldb_module *module,
 				  "FATAL: This ldb_mdb database has "
 				  "been written in a new verson of LDB "
 				  "using a sub-database index that "
-				  "is not understood by ldb "
-				  LDB_VERSION);
+				  "is not understood by ldb " LDB_VERSION);
 		return -1;
 	}
 
@@ -424,10 +436,12 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	}
 
 	baseinfo = ldb_msg_new(ldb_kv->cache);
-	if (baseinfo == NULL) goto failed;
+	if (baseinfo == NULL)
+		goto failed;
 
 	baseinfo_dn = ldb_dn_new(baseinfo, ldb, LDB_KV_BASEINFO);
-	if (baseinfo_dn == NULL) goto failed;
+	if (baseinfo_dn == NULL)
+		goto failed;
 
 	r = ldb_kv->kv_ops->lock_read(module);
 	if (r != LDB_SUCCESS) {
@@ -461,7 +475,6 @@ int ldb_kv_cache_load(struct ldb_module *module)
 		    LDB_SUCCESS) {
 			goto failed_and_unlock;
 		}
-
 	}
 
 	/* Ignore the result, and update the sequence number */
@@ -478,10 +491,12 @@ int ldb_kv_cache_load(struct ldb_module *module)
 	/* Read an interpret database options */
 
 	options = ldb_msg_new(ldb_kv->cache);
-	if (options == NULL) goto failed_and_unlock;
+	if (options == NULL)
+		goto failed_and_unlock;
 
 	options_dn = ldb_dn_new(options, ldb, LDB_KV_OPTIONS);
-	if (options_dn == NULL) goto failed_and_unlock;
+	if (options_dn == NULL)
+		goto failed_and_unlock;
 
 	r = ldb_kv_search_dn1(module, options_dn, options, 0);
 	talloc_free(options_dn);
@@ -491,8 +506,8 @@ int ldb_kv_cache_load(struct ldb_module *module)
 
 	/* set flags if they do exist */
 	if (r == LDB_SUCCESS) {
-		ldb_kv->check_base =
-		    ldb_msg_find_attr_as_bool(options, LDB_KV_CHECK_BASE, false);
+		ldb_kv->check_base = ldb_msg_find_attr_as_bool(
+		    options, LDB_KV_CHECK_BASE, false);
 		ldb_kv->disallow_dn_filter = ldb_msg_find_attr_as_bool(
 		    options, LDB_KV_DISALLOW_DN_FILTER, false);
 	} else {
@@ -560,7 +575,6 @@ failed:
 	return -1;
 }
 
-
 /*
   increase the sequence number to indicate a database change
 */
diff --git a/lib/ldb/ldb_key_value/ldb_kv_index.c b/lib/ldb/ldb_key_value/ldb_kv_index.c
index 550f4b6..759cc82 100644
--- a/lib/ldb/ldb_key_value/ldb_kv_index.c
+++ b/lib/ldb/ldb_key_value/ldb_kv_index.c
@@ -126,17 +126,18 @@ C Override functions
 --------------------
 
 void ldb_schema_set_override_GUID_index(struct ldb_context *ldb,
-                                        const char *GUID_index_attribute,
-                                        const char *GUID_index_dn_component)
+					const char *GUID_index_attribute,
+					const char *GUID_index_dn_component)
 
 This is used, particularly in combination with the below, instead of
 the @IDXGUID and @IDX_DN_GUID values in @INDEXLIST.
 
 void ldb_schema_set_override_indexlist(struct ldb_context *ldb,
-                                       bool one_level_indexes);
+				       bool one_level_indexes);
 void ldb_schema_attribute_set_override_handler(struct ldb_context *ldb,
-                                               ldb_attribute_handler_override_fn_t override,
-                                               void *private_data);
+					       ldb_attribute_handler_override_fn_t
+override,
+					       void *private_data);
 
 When the above two functions are called in combination, the @INDEXLIST
 values are not read from the DB, so
@@ -243,7 +244,6 @@ static int ldb_val_equal_exact_ordered(const struct ldb_val v1,
 	return memcmp(v1.data, v2->data, v1.length);
 }
 
-
 /*
   find a entry in a dn_list, using a ldb_val. Uses a case sensitive
   binary-safe comparison for the 'dn' returns -1 if not found
@@ -258,7 +258,7 @@ static int ldb_kv_dn_list_find_val(struct ldb_kv_private *ldb_kv,
 	struct ldb_val *exact = NULL, *next = NULL;
 
 	if (ldb_kv->cache->GUID_index_attribute == NULL) {
-		for (i=0; i<list->count; i++) {
+		for (i = 0; i < list->count; i++) {
 			if (ldb_val_equal_exact(&list->dn[i], v) == 1) {
 				return i;
 			}
@@ -266,9 +266,12 @@ static int ldb_kv_dn_list_find_val(struct ldb_kv_private *ldb_kv,
 		return -1;
 	}
 
-	BINARY_ARRAY_SEARCH_GTE(list->dn, list->count,
-				*v, ldb_val_equal_exact_ordered,
-				exact, next);
+	BINARY_ARRAY_SEARCH_GTE(list->dn,
+				list->count,
+				*v,
+				ldb_val_equal_exact_ordered,
+				exact,
+				next);
 	if (exact == NULL) {
 		return -1;
 	}
@@ -318,7 +321,8 @@ static struct dn_list *ldb_kv_index_idxptr(struct ldb_module *module,
 	struct dn_list *list;
 	if (rec.dsize != sizeof(void *)) {
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       "Bad data size for idxptr %u", (unsigned)rec.dsize);
+				       "Bad data size for idxptr %u",
+				       (unsigned)rec.dsize);
 		return NULL;
 	}
 	/* note that we can't just use a cast here, as rec.dptr may
@@ -333,9 +337,10 @@ static struct dn_list *ldb_kv_index_idxptr(struct ldb_module *module,
 		return NULL;
 	}
 	if (check_parent && list->dn && talloc_parent(list->dn) != list) {
-		ldb_asprintf_errstring(ldb_module_get_ctx(module),
-				       "Bad parent '%s' for idxptr",
-				       talloc_get_name(talloc_parent(list->dn)));
+		ldb_asprintf_errstring(
+		    ldb_module_get_ctx(module),
+		    "Bad parent '%s' for idxptr",
+		    talloc_get_name(talloc_parent(list->dn)));
 		return NULL;
 	}
 	return list;
@@ -421,7 +426,8 @@ normal_index:
 				      LDB_DEBUG_ERROR,
 				      "Wrong DN index version %d "
 				      "expected %d for %s",
-				      version, LDB_KV_INDEXING_VERSION,
+				      version,
+				      LDB_KV_INDEXING_VERSION,
 				      ldb_dn_get_linearized(dn));
 			talloc_free(msg);
 			return LDB_ERR_OPERATIONS_ERROR;
@@ -439,7 +445,8 @@ normal_index:
 				      LDB_DEBUG_ERROR,
 				      "Wrong GUID index version %d "
 				      "expected %d for %s",
-				      version, LDB_KV_GUID_INDEXING_VERSION,
+				      version,
+				      LDB_KV_GUID_INDEXING_VERSION,
 				      ldb_dn_get_linearized(dn));
 			talloc_free(msg);
 			return LDB_ERR_OPERATIONS_ERROR;
@@ -468,8 +475,8 @@ normal_index:
 		 */
 		talloc_steal(list->dn, msg);
 		for (i = 0; i < list->count; i++) {
-			list->dn[i].data
-				= &el->values[0].data[i * LDB_KV_GUID_SIZE];
+			list->dn[i].data =
+			    &el->values[0].data[i * LDB_KV_GUID_SIZE];
 			list->dn[i].length = LDB_KV_GUID_SIZE;
 		}
 	}
@@ -506,7 +513,7 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 		return LDB_ERR_NO_SUCH_OBJECT;
 	}
 
-	if (list->count > 1 && truncation == KEY_NOT_TRUNCATED)  {
+	if (list->count > 1 && truncation == KEY_NOT_TRUNCATED) {
 		const char *dn_str = ldb_dn_get_linearized(dn);
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
 				       __location__
@@ -520,19 +527,17 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 		return LDB_ERR_CONSTRAINT_VIOLATION;
 	}
 
-	if (list->count > 0 && truncation == KEY_TRUNCATED)  {
+	if (list->count > 0 && truncation == KEY_TRUNCATED) {
 		/*
 		 * DN key has been truncated, need to inspect the actual
 		 * records to locate the actual DN
 		 */
 		int i;
 		index = -1;
-		for (i=0; i < list->count; i++) {
+		for (i = 0; i < list->count; i++) {
 			uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
-			struct ldb_val key = {
-				.data = guid_key,
-				.length = sizeof(guid_key)
-			};
+			struct ldb_val key = {.data = guid_key,
+					      .length = sizeof(guid_key)};
 			const int flags = LDB_UNPACK_DATA_FLAG_NO_ATTRS;
 			struct ldb_message *rec = ldb_msg_new(ldb);
 			if (rec == NULL) {
@@ -603,8 +608,6 @@ int ldb_kv_key_dn_from_idx(struct ldb_module *module,
 	return LDB_SUCCESS;
 }
 
-
-
 /*
   save a dn_list into a full @IDX style record
  */
@@ -633,15 +636,15 @@ static int ldb_kv_dn_list_store_full(struct ldb_module *module,
 	}
 
 	if (ldb_kv->cache->GUID_index_attribute == NULL) {
-		ret = ldb_msg_add_fmt(msg, LDB_KV_IDXVERSION, "%u",
-				      LDB_KV_INDEXING_VERSION);
+		ret = ldb_msg_add_fmt(
+		    msg, LDB_KV_IDXVERSION, "%u", LDB_KV_INDEXING_VERSION);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(msg);
 			return ldb_module_oom(module);
 		}
 	} else {
-		ret = ldb_msg_add_fmt(msg, LDB_KV_IDXVERSION, "%u",
-				      LDB_KV_GUID_INDEXING_VERSION);
+		ret = ldb_msg_add_fmt(
+		    msg, LDB_KV_IDXVERSION, "%u", LDB_KV_GUID_INDEXING_VERSION);
 		if (ret != LDB_SUCCESS) {
 			talloc_free(msg);
 			return ldb_module_oom(module);
@@ -663,16 +666,14 @@ static int ldb_kv_dn_list_store_full(struct ldb_module *module,
 		} else {
 			struct ldb_val v;
 			unsigned int i;
-			el->values = talloc_array(msg,
-						  struct ldb_val, 1);
+			el->values = talloc_array(msg, struct ldb_val, 1);
 			if (el->values == NULL) {
 				talloc_free(msg);
 				return ldb_module_oom(module);
 			}
 
-			v.data = talloc_array_size(el->values,
-						   list->count,
-						   LDB_KV_GUID_SIZE);
+			v.data = talloc_array_size(
+			    el->values, list->count, LDB_KV_GUID_SIZE);
 			if (v.data == NULL) {
 				talloc_free(msg);
 				return ldb_module_oom(module);
@@ -681,12 +682,11 @@ static int ldb_kv_dn_list_store_full(struct ldb_module *module,
 			v.length = talloc_get_size(v.data);
 
 			for (i = 0; i < list->count; i++) {
-				if (list->dn[i].length !=
-				    LDB_KV_GUID_SIZE) {
+				if (list->dn[i].length != LDB_KV_GUID_SIZE) {
 					talloc_free(msg);
 					return ldb_module_operr(module);
 				}
-				memcpy(&v.data[LDB_KV_GUID_SIZE*i],
+				memcpy(&v.data[LDB_KV_GUID_SIZE * i],
 				       list->dn[i].data,
 				       LDB_KV_GUID_SIZE);
 			}
@@ -754,7 +754,6 @@ static int ldb_kv_dn_list_store(struct ldb_module *module,
 	rec.dptr = (uint8_t *)&list2;
 	rec.dsize = sizeof(void *);
 
-
 	/*
 	 * This is not a store into the main DB, but into an in-memory
 	 * TDB, so we don't need a guard on ltdb->read_only
@@ -793,7 +792,12 @@ static int ldb_kv_index_traverse_store(struct tdb_context *tdb,
 
 	dn = ldb_dn_from_ldb_val(module, ldb, &v);
 	if (dn == NULL) {
-		ldb_asprintf_errstring(ldb, "Failed to parse index key %*.*s as an LDB DN", (int)v.length, (int)v.length, (const char *)v.data);
+		ldb_asprintf_errstring(
+		    ldb,
+		    "Failed to parse index key %*.*s as an LDB DN",
+		    (int)v.length,
+		    (int)v.length,
+		    (const char *)v.data);
 		ldb_kv->idxptr->error = LDB_ERR_OPERATIONS_ERROR;
 		return -1;
 	}
@@ -829,7 +833,10 @@ int ldb_kv_index_transaction_commit(struct ldb_module *module)
 		if (!ldb_errstring(ldb)) {
 			ldb_set_errstring(ldb, ldb_strerror(ret));
 		}
-		ldb_asprintf_errstring(ldb, "Failed to store index records in transaction commit: %s", ldb_errstring(ldb));
+		ldb_asprintf_errstring(
+		    ldb,
+		    "Failed to store index records in transaction commit: %s",
+		    ldb_errstring(ldb));
 	}
 
 	talloc_free(ldb_kv->idxptr);
@@ -850,7 +857,6 @@ int ldb_kv_index_transaction_cancel(struct ldb_module *module)
 	return LDB_SUCCESS;
 }
 
-
 /*
   return the dn key to be used for an index
   the caller is responsible for freeing
@@ -878,8 +884,8 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 	const size_t additional_key_length = 4;
 	unsigned int num_separators = 3; /* Estimate for overflow check */
 	const size_t min_data = 1;
-	const size_t min_key_length = additional_key_length
-		+ indx_len + num_separators + min_data;
+	const size_t min_key_length =
+	    additional_key_length + indx_len + num_separators + min_data;
 
 	if (attr[0] == '@') {
 		attr_for_dn = attr;
@@ -909,9 +915,10 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 			ldb_asprintf_errstring(ldb,
 					       "Failed to create index "
 					       "key for attribute '%s':%s%s%s",
-					       attr, ldb_strerror(r),
-					       (errstr?":":""),
-					       (errstr?errstr:""));
+					       attr,
+					       ldb_strerror(r),
+					       (errstr ? ":" : ""),
+					       (errstr ? errstr : ""));
 			talloc_free(attr_folded);
 			return NULL;
 		}
@@ -925,12 +932,11 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 	 * check for too long keys
 	 */
 	if (max_key_length - attr_len < min_key_length) {
-		ldb_asprintf_errstring(
-			ldb,
-			__location__ ": max_key_length "
-			"is too small (%u) < (%u)",
-			max_key_length,
-			(unsigned)(min_key_length + attr_len));
+		ldb_asprintf_errstring(ldb,
+				       __location__ ": max_key_length "
+						    "is too small (%u) < (%u)",
+				       max_key_length,
+				       (unsigned)(min_key_length + attr_len));
 		talloc_free(attr_folded);
 		return NULL;
 	}
@@ -956,8 +962,7 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 			 */
 			should_b64_encode = false;
 		} else {
-			should_b64_encode
-				= ldb_should_b64_encode(ldb, &v);
+			should_b64_encode = ldb_should_b64_encode(ldb, &v);
 		}
 	} else {
 		should_b64_encode = ldb_should_b64_encode(ldb, &v);
@@ -986,9 +991,13 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 			* Note: the double hash "##" is not a typo and
 			* indicates that the following value is base64 encoded
 			*/
-			ret = ldb_dn_new_fmt(ldb, ldb, "%s#%s##%.*s",
-					     LDB_KV_INDEX, attr_for_dn,
-					     frmt_len, vstr);
+			ret = ldb_dn_new_fmt(ldb,
+					     ldb,
+					     "%s#%s##%.*s",
+					     LDB_KV_INDEX,
+					     attr_for_dn,
+					     frmt_len,
+					     vstr);
 		} else {
 			frmt_len = vstr_len;
 			*truncation = KEY_NOT_TRUNCATED;
@@ -996,9 +1005,13 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 			 * Note: the double colon "::" is not a typo and
 			 * indicates that the following value is base64 encoded
 			 */
-			ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s::%.*s",
-					     LDB_KV_INDEX, attr_for_dn,
-					     frmt_len, vstr);
+			ret = ldb_dn_new_fmt(ldb,
+					     ldb,
+					     "%s:%s::%.*s",
+					     LDB_KV_INDEX,
+					     attr_for_dn,
+					     frmt_len,
+					     vstr);
 		}
 		talloc_free(vstr);
 	} else {
@@ -1018,15 +1031,23 @@ static struct ldb_dn *ldb_kv_index_key(struct ldb_context *ldb,
 			 * Truncated keys are placed in a separate key space
 			 * from the non truncated keys
 			 */
-			ret = ldb_dn_new_fmt(ldb, ldb, "%s#%s#%.*s",
-					     LDB_KV_INDEX, attr_for_dn,
-					     frmt_len, (char *)v.data);
+			ret = ldb_dn_new_fmt(ldb,
+					     ldb,
+					     "%s#%s#%.*s",
+					     LDB_KV_INDEX,
+					     attr_for_dn,
+					     frmt_len,
+					     (char *)v.data);
 		} else {
 			frmt_len = v.length;
 			*truncation = KEY_NOT_TRUNCATED;
-			ret = ldb_dn_new_fmt(ldb, ldb, "%s:%s:%.*s",
-					     LDB_KV_INDEX, attr_for_dn,
-					     frmt_len, (char *)v.data);
+			ret = ldb_dn_new_fmt(ldb,
+					     ldb,
+					     "%s:%s:%.*s",
+					     LDB_KV_INDEX,
+					     attr_for_dn,
+					     frmt_len,
+					     (char *)v.data);
 		}
 	}
 
@@ -1055,8 +1076,8 @@ static bool ldb_kv_is_indexed(struct ldb_module *module,
 		return false;
 	}
 	if (ldb->schema.index_handler_override) {
-		const struct ldb_schema_attribute *a
-			= ldb_schema_attribute_by_name(ldb, attr);
+		const struct ldb_schema_attribute *a =
+		    ldb_schema_attribute_by_name(ldb, attr);
 
 		if (a == NULL) {
 			return false;
@@ -1079,7 +1100,7 @@ static bool ldb_kv_is_indexed(struct ldb_module *module,
 	}
 
 	/* TODO: this is too expensive! At least use a binary search */
-	for (i=0; i<el->num_values; i++) {
+	for (i = 0; i < el->num_values; i++) {
 		if (ldb_attr_cmp((char *)el->values[i].data, attr) == 0) {
 			return true;
 		}
@@ -1096,7 +1117,7 @@ static bool ldb_kv_is_indexed(struct ldb_module *module,
      LDB_ERR_NO_SUCH_OBJECT: we know for sure that no object matches
 
      LDB_ERR_OPERATIONS_ERROR: indexing could not answer the call,
-                               we'll need a full search
+			       we'll need a full search
  */
 
 /*
@@ -1137,7 +1158,8 @@ static int ldb_kv_index_dn_simple(struct ldb_module *module,
 	 * as ltdb_search_indexed will filter out the wrong one in
 	 * ltdb_index_filter() which calls ldb_match_message().
 	 */
-	if (!dn) return LDB_ERR_OPERATIONS_ERROR;
+	if (!dn)
+		return LDB_ERR_OPERATIONS_ERROR;
 
 	ret = ldb_kv_dn_list_load(module, ldb_kv, dn, list);
 	talloc_free(dn);
@@ -1172,10 +1194,8 @@ static int ldb_kv_index_dn_leaf(struct ldb_module *module,
 	}
 	if (ldb_attr_dn(tree->u.equality.attr) == 0) {
 		enum key_truncation truncation = KEY_NOT_TRUNCATED;
-		struct ldb_dn *dn
-			= ldb_dn_from_ldb_val(list,
-					      ldb_module_get_ctx(module),
-					      &tree->u.equality.value);
+		struct ldb_dn *dn = ldb_dn_from_ldb_val(
+		    list, ldb_module_get_ctx(module), &tree->u.equality.value);
 		if (dn == NULL) {
 			/* If we can't parse it, no match */
 			list->dn = NULL;
@@ -1225,7 +1245,6 @@ static int ldb_kv_index_dn_leaf(struct ldb_module *module,
 	return ldb_kv_index_dn_simple(module, ldb_kv, tree, list);
 }
 
-
 /*
   list intersection
   list = list & list2
@@ -1281,15 +1300,15 @@ static bool list_intersect(struct ldb_context *ldb,
 		return false;
 	}
 
-	list3->dn = talloc_array(list3, struct ldb_val,
-				 MIN(list->count, list2->count));
+	list3->dn =
+	    talloc_array(list3, struct ldb_val, MIN(list->count, list2->count));
 	if (!list3->dn) {
 		talloc_free(list3);
 		return false;
 	}
 	list3->count = 0;
 
-	for (i=0;i<short_list->count;i++) {
+	for (i = 0; i < short_list->count; i++) {
 		/* For the GUID index case, this is a binary search */
 		if (ldb_kv_dn_list_find_val(
 			ldb_kv, long_list, &short_list->dn[i]) != -1) {
@@ -1306,7 +1325,6 @@ static bool list_intersect(struct ldb_context *ldb,
 	return true;
 }
 
-
 /*
   list union
   list = list | list2
@@ -1409,7 +1427,7 @@ static int ldb_kv_index_dn_or(struct ldb_module *module,
 	list->dn = NULL;
 	list->count = 0;
 
-	for (i=0; i<tree->u.list.num_elements; i++) {
+	for (i = 0; i < tree->u.list.num_elements; i++) {
 		struct dn_list *list2;
 		int ret;
 
@@ -1446,7 +1464,6 @@ static int ldb_kv_index_dn_or(struct ldb_module *module,
 	return LDB_SUCCESS;
 }
 
-
 /*
   NOT an index results
  */
@@ -1512,7 +1529,7 @@ static int ldb_kv_index_dn_and(struct ldb_module *module,
 	/* in the first pass we only look for unique simple
 	   equality tests, in the hope of avoiding having to look
 	   at any others */
-	for (i=0; i<tree->u.list.num_elements; i++) {
+	for (i = 0; i < tree->u.list.num_elements; i++) {
 		const struct ldb_parse_tree *subtree = tree->u.list.elements[i];
 		int ret;
 
@@ -1539,7 +1556,7 @@ static int ldb_kv_index_dn_and(struct ldb_module *module,
 	/* now do a full intersection */
 	found = false;
 
-	for (i=0; i<tree->u.list.num_elements; i++) {
+	for (i = 0; i < tree->u.list.num_elements; i++) {
 		const struct ldb_parse_tree *subtree = tree->u.list.elements[i];
 		struct dn_list *list2;
 		int ret;
@@ -1663,8 +1680,8 @@ static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
 		if (dn_list->dn == NULL) {
 			return ldb_module_oom(module);
 		}
-		dn_list->dn[0].data = discard_const_p(unsigned char,
-						      ldb_dn_get_linearized(base_dn));
+		dn_list->dn[0].data = discard_const_p(
+		    unsigned char, ldb_dn_get_linearized(base_dn));
 		if (dn_list->dn[0].data == NULL) {
 			return ldb_module_oom(module);
 		}
@@ -1697,7 +1714,8 @@ static int ldb_kv_index_dn_base_dn(struct ldb_module *module,
 
 /*
   return a list of dn's that might match a indexed search or
-  an error. return LDB_ERR_NO_SUCH_OBJECT for no matches, or LDB_SUCCESS for matches
+  an error. return LDB_ERR_NO_SUCH_OBJECT for no matches, or LDB_SUCCESS for
+  matches
  */
 static int ldb_kv_index_dn(struct ldb_module *module,
 			   struct ldb_kv_private *ldb_kv,
@@ -1776,9 +1794,8 @@ static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 			uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
 		} *key_values = NULL;
 
-		key_values = talloc_array(keys,
-					  struct guid_tdb_key,
-					  dn_list->count);
+		key_values =
+		    talloc_array(keys, struct guid_tdb_key, dn_list->count);
 
 		if (key_values == NULL) {
 			talloc_free(keys);
@@ -1830,7 +1847,6 @@ static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 		num_keys++;
 	}
 
-
 	/*
 	 * Now that the list is a safe copy, send the callbacks
 	 */
@@ -1877,12 +1893,11 @@ static int ldb_kv_index_filter(struct ldb_kv_private *ldb_kv,
 		if (ac->scope == LDB_SCOPE_ONELEVEL &&
 		    ldb_kv->cache->one_level_indexes &&
 		    scope_one_truncation == KEY_NOT_TRUNCATED) {
-			ret = ldb_match_message(ldb, msg, ac->tree,
-						ac->scope, &matched);
+			ret = ldb_match_message(
+			    ldb, msg, ac->tree, ac->scope, &matched);
 		} else {
-			ret = ldb_match_msg_error(ldb, msg,
-						  ac->tree, ac->base,
-						  ac->scope, &matched);
+			ret = ldb_match_msg_error(
+			    ldb, msg, ac->tree, ac->base, ac->scope, &matched);
 		}
 
 		if (ret != LDB_SUCCESS) {
@@ -1937,8 +1952,7 @@ static void ldb_kv_dn_list_sort(struct ldb_kv_private *ltdb,
 		return;
 	}
 
-	TYPESAFE_QSORT(list->dn, list->count,
-		       ldb_val_equal_exact_for_qsort);
+	TYPESAFE_QSORT(list->dn, list->count, ldb_val_equal_exact_for_qsort);
 }
 
 /*
@@ -2018,8 +2032,8 @@ int ldb_kv_search_indexed(struct ldb_kv_context *ac, uint32_t *match_count)
 		 * fast enough in the small case.
 		 */
 		if (ldb_kv->cache->GUID_index_attribute != NULL) {
-			struct dn_list *idx_one_tree_list
-				= talloc_zero(ac, struct dn_list);
+			struct dn_list *idx_one_tree_list =
+			    talloc_zero(ac, struct dn_list);
 			if (idx_one_tree_list == NULL) {
 				return ldb_module_oom(ac->module);
 			}
@@ -2120,7 +2134,6 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 	unsigned alloc_len;
 	enum key_truncation truncation = KEY_TRUNCATED;
 
-
 	ldb = ldb_module_get_ctx(module);
 
 	list = talloc_zero(module, struct dn_list);
@@ -2139,9 +2152,10 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 	 * so if a unique index key exceeds the maximum length there is a
 	 * problem.
 	 */
-	if ((truncation == KEY_TRUNCATED) && (a != NULL &&
-		(a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX ||
-		(el->flags & LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX)))) {
+	if ((truncation == KEY_TRUNCATED) &&
+	    (a != NULL &&
+	     (a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX ||
+	      (el->flags & LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX)))) {
 
 		ldb_asprintf_errstring(
 		    ldb,
@@ -2168,15 +2182,14 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 	 * was attempted, so don't set the error string or print scary
 	 * messages.
 	 */
-	if (list->count > 0 &&
-	    ldb_attr_cmp(el->name, LDB_KV_IDXDN) == 0 &&
+	if (list->count > 0 && ldb_attr_cmp(el->name, LDB_KV_IDXDN) == 0 &&
 	    truncation == KEY_NOT_TRUNCATED) {
 
 		talloc_free(list);
 		return LDB_ERR_CONSTRAINT_VIOLATION;
 
-	} else if (list->count > 0
-		   && ldb_attr_cmp(el->name, LDB_KV_IDXDN) == 0) {
+	} else if (list->count > 0 &&
+		   ldb_attr_cmp(el->name, LDB_KV_IDXDN) == 0) {
 
 		/*
 		 * At least one existing entry in the DN->GUID index, which
@@ -2185,12 +2198,10 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		 * So need to pull the DN's to check if it's really a duplicate
 		 */
 		int i;
-		for (i=0; i < list->count; i++) {
+		for (i = 0; i < list->count; i++) {
 			uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
-			struct ldb_val key = {
-				.data = guid_key,
-				.length = sizeof(guid_key)
-			};
+			struct ldb_val key = {.data = guid_key,
+					      .length = sizeof(guid_key)};
 			const int flags = LDB_UNPACK_DATA_FLAG_NO_ATTRS;
 			struct ldb_message *rec = ldb_msg_new(ldb);
 			if (rec == NULL) {
@@ -2245,9 +2256,9 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 	 * at the start of this function.
 	 */
 	if (list->count > 0 &&
-	    ((a != NULL
-	      && (a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX ||
-		  (el->flags & LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX))))) {
+	    ((a != NULL &&
+	      (a->flags & LDB_ATTR_FLAG_UNIQUE_INDEX ||
+	       (el->flags & LDB_FLAG_INTERNAL_FORCE_UNIQUE_INDEX))))) {
 		/*
 		 * We do not want to print info about a possibly
 		 * confidential DN that the conflict was with in the
@@ -2255,11 +2266,13 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		 */
 
 		if (ldb_kv->cache->GUID_index_attribute == NULL) {
-			ldb_debug(ldb, LDB_DEBUG_WARNING,
+			ldb_debug(ldb,
+				  LDB_DEBUG_WARNING,
 				  __location__
 				  ": unique index violation on %s in %s, "
 				  "conficts with %*.*s in %s",
-				  el->name, ldb_dn_get_linearized(msg->dn),
+				  el->name,
+				  ldb_dn_get_linearized(msg->dn),
 				  (int)list->dn[0].length,
 				  (int)list->dn[0].length,
 				  list->dn[0].data,
@@ -2270,8 +2283,8 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 			    ldb_schema_attribute_by_name(
 				ldb, ldb_kv->cache->GUID_index_attribute);
 			struct ldb_val v;
-			ret = attr->syntax->ldif_write_fn(ldb, list,
-							  &list->dn[0], &v);
+			ret = attr->syntax->ldif_write_fn(
+			    ldb, list, &list->dn[0], &v);
 			if (ret == LDB_SUCCESS) {
 				ldb_debug(ldb,
 					  LDB_DEBUG_WARNING,
@@ -2289,7 +2302,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		}
 		ldb_asprintf_errstring(ldb,
 				       __location__ ": unique index violation "
-				       "on %s in %s",
+						    "on %s in %s",
 				       el->name,
 				       ldb_dn_get_linearized(msg->dn));
 		talloc_free(list);
@@ -2298,7 +2311,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 
 	/* overallocate the list a bit, to reduce the number of
 	 * realloc trigered copies */
-	alloc_len = ((list->count+1)+7) & ~7;
+	alloc_len = ((list->count + 1) + 7) & ~7;
 	list->dn = talloc_realloc(list, list->dn, struct ldb_val, alloc_len);
 	if (list->dn == NULL) {
 		talloc_free(list);
@@ -2307,8 +2320,8 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 
 	if (ldb_kv->cache->GUID_index_attribute == NULL) {
 		const char *dn_str = ldb_dn_get_linearized(msg->dn);
-		list->dn[list->count].data
-			= (uint8_t *)talloc_strdup(list->dn, dn_str);
+		list->dn[list->count].data =
+		    (uint8_t *)talloc_strdup(list->dn, dn_str);
 		if (list->dn[list->count].data == NULL) {
 			talloc_free(list);
 			return LDB_ERR_OPERATIONS_ERROR;
@@ -2329,9 +2342,12 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 			return ldb_module_operr(module);
 		}
 
-		BINARY_ARRAY_SEARCH_GTE(list->dn, list->count,
-					*key_val, ldb_val_equal_exact_ordered,
-					exact, next);
+		BINARY_ARRAY_SEARCH_GTE(list->dn,
+					list->count,
+					*key_val,
+					ldb_val_equal_exact_ordered,
+					exact,
+					next);
 
 		/*
 		 * Give a warning rather than fail, this could be a
@@ -2345,8 +2361,7 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 			    ldb_schema_attribute_by_name(
 				ldb, ldb_kv->cache->GUID_index_attribute);
 			struct ldb_val v;
-			ret = attr->syntax->ldif_write_fn(ldb, list,
-							  exact, &v);
+			ret = attr->syntax->ldif_write_fn(ldb, list, exact, &v);
 			if (ret == LDB_SUCCESS) {
 				ldb_debug(ldb,
 					  LDB_DEBUG_WARNING,
@@ -2367,8 +2382,10 @@ static int ldb_kv_index_add1(struct ldb_module *module,
 		if (next == NULL) {
 			next = &list->dn[list->count];
 		} else {
-			memmove(&next[1], next,
-				sizeof(*next) * (list->count - (next - list->dn)));
+			memmove(&next[1],
+				next,
+				sizeof(*next) *
+				    (list->count - (next - list->dn)));
 		}
 		*next = ldb_val_dup(list->dn, key_val);
 		if (next->data == NULL) {
@@ -2442,10 +2459,12 @@ static int ldb_kv_index_add_all(struct ldb_module *module,
 		ret = ldb_kv_index_add_el(module, ldb_kv, msg, &elements[i]);
 		if (ret != LDB_SUCCESS) {
 			struct ldb_context *ldb = ldb_module_get_ctx(module);
-			ldb_asprintf_errstring(ldb,
-					       __location__ ": Failed to re-index %s in %s - %s",
-					       elements[i].name, dn_str,
-					       ldb_errstring(ldb));
+			ldb_asprintf_errstring(
+			    ldb,
+			    __location__ ": Failed to re-index %s in %s - %s",
+			    elements[i].name,
+			    dn_str,
+			    ldb_errstring(ldb));
 			return ret;
 		}
 	}
@@ -2453,7 +2472,6 @@ static int ldb_kv_index_add_all(struct ldb_module *module,
 	return LDB_SUCCESS;
 }
 
-
 /*
   insert a DN index for a message
 */
@@ -2528,8 +2546,8 @@ static int ldb_kv_index_onelevel(struct ldb_module *module,
 	if (pdn == NULL) {
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
-	ret =
-	    ldb_kv_modify_index_dn(module, ldb_kv, msg, pdn, LDB_KV_IDXONE, add);
+	ret = ldb_kv_modify_index_dn(
+	    module, ldb_kv, msg, pdn, LDB_KV_IDXONE, add);
 
 	talloc_free(pdn);
 
@@ -2622,7 +2640,6 @@ int ldb_kv_index_add_new(struct ldb_module *module,
 	return ret;
 }
 
-
 /*
   delete an index entry for one message element
 */
@@ -2672,7 +2689,8 @@ int ldb_kv_index_del_value(struct ldb_module *module,
 
 	ret = ldb_kv_dn_list_load(module, ldb_kv, dn_key, list);
 	if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-		/* it wasn't indexed. Did we have an earlier error? If we did then
+		/* it wasn't indexed. Did we have an earlier error? If we did
+		   then
 		   its gone now */
 		talloc_free(dn_key);
 		return LDB_SUCCESS;
@@ -2693,16 +2711,19 @@ int ldb_kv_index_del_value(struct ldb_module *module,
 		return LDB_SUCCESS;
 	}
 
-	j = (unsigned int) i;
+	j = (unsigned int)i;
 	if (j != list->count - 1) {
-		memmove(&list->dn[j], &list->dn[j+1], sizeof(list->dn[0])*(list->count - (j+1)));
+		memmove(&list->dn[j],
+			&list->dn[j + 1],
+			sizeof(list->dn[0]) * (list->count - (j + 1)));
 	}
 	list->count--;
 	if (list->count == 0) {
 		talloc_free(list->dn);
 		list->dn = NULL;
 	} else {
-		list->dn = talloc_realloc(list, list->dn, struct ldb_val, list->count);
+		list->dn =
+		    talloc_realloc(list, list->dn, struct ldb_val, list->count);
 	}
 
 	ret = ldb_kv_dn_list_store(module, dn_key, list);
@@ -2794,7 +2815,6 @@ int ldb_kv_index_delete(struct ldb_module *module,
 	return LDB_SUCCESS;
 }
 
-
 /*
   traversal function that deletes all @INDEX records in the in-memory
   TDB.
@@ -2831,13 +2851,13 @@ static int delete_index(struct ldb_kv_private *ldb_kv,
 
 	/*
 	 * This does not actually touch the DB quite yet, just
-         * the in-memory index cache
+	 * the in-memory index cache
 	 */
 	ret = ldb_kv_dn_list_store(module, dn, &list);
 	if (ret != LDB_SUCCESS) {
 		ldb_asprintf_errstring(ldb_module_get_ctx(module),
 				       "Unable to store null index for %s\n",
-						ldb_dn_get_linearized(dn));
+				       ldb_dn_get_linearized(dn));
 		talloc_free(dn);
 		return -1;
 	}
@@ -2846,7 +2866,8 @@ static int delete_index(struct ldb_kv_private *ldb_kv,
 }
 
 /*
-  traversal function that adds @INDEX records during a re index TODO wrong comment
+  traversal function that adds @INDEX records during a re index TODO wrong
+  comment
 */
 static int re_key(struct ldb_kv_private *ldb_kv,
 		  struct ldb_val key,
@@ -2865,8 +2886,7 @@ static int re_key(struct ldb_kv_private *ldb_kv,
 
 	ldb = ldb_module_get_ctx(module);
 
-	if (key.length > 4 &&
-	    memcmp(key.data, "DN=@", 4) == 0) {
+	if (key.length > 4 && memcmp(key.data, "DN=@", 4) == 0) {
 		return 0;
 	}
 
@@ -2880,24 +2900,31 @@ static int re_key(struct ldb_kv_private *ldb_kv,
 		return -1;
 	}
 
-	ret = ldb_unpack_data_only_attr_list_flags(ldb, &val,
-						   msg,
-						   NULL, 0,
-						   LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC,
-						   &nb_elements_in_db);
+	ret = ldb_unpack_data_only_attr_list_flags(
+	    ldb,
+	    &val,
+	    msg,
+	    NULL,
+	    0,
+	    LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC,
+	    &nb_elements_in_db);
 	if (ret != 0) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %s\n",
-						ldb_dn_get_linearized(msg->dn));
+		ldb_debug(ldb,
+			  LDB_DEBUG_ERROR,
+			  "Invalid data for index %s\n",
+			  ldb_dn_get_linearized(msg->dn));
 		ctx->error = ret;
 		talloc_free(msg);
 		return -1;
 	}
 
 	if (msg->dn == NULL) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR,
+		ldb_debug(ldb,
+			  LDB_DEBUG_ERROR,
 			  "Refusing to re-index as GUID "
 			  "key %*.*s with no DN\n",
-			  (int)key.length, (int)key.length,
+			  (int)key.length,
+			  (int)key.length,
 			  (char *)key.data);
 		talloc_free(msg);
 		return -1;
@@ -2909,15 +2936,16 @@ static int re_key(struct ldb_kv_private *ldb_kv,
 	key2 = ldb_kv_key_msg(module, msg, msg);
 	if (key2.data == NULL) {
 		/* probably a corrupt record ... darn */
-		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid DN in re_index: %s",
-						ldb_dn_get_linearized(msg->dn));
+		ldb_debug(ldb,
+			  LDB_DEBUG_ERROR,
+			  "Invalid DN in re_index: %s",
+			  ldb_dn_get_linearized(msg->dn));
 		talloc_free(msg);
 		return 0;
 	}
 	if (key.length != key2.length ||
 	    (memcmp(key.data, key2.data, key.length) != 0)) {
-		ldb_kv->kv_ops->update_in_iterate(
-		    ldb_kv, key, key2, val, ctx);
+		ldb_kv->kv_ops->update_in_iterate(ldb_kv, key, key2, val, ctx);
 	}
 	talloc_free(key2.data);
 
@@ -2925,7 +2953,8 @@ static int re_key(struct ldb_kv_private *ldb_kv,
 
 	ctx->count++;
 	if (ctx->count % 10000 == 0) {
-		ldb_debug(ldb, LDB_DEBUG_WARNING,
+		ldb_debug(ldb,
+			  LDB_DEBUG_WARNING,
 			  "Reindexing: re-keyed %u records so far",
 			  ctx->count);
 	}
@@ -2952,8 +2981,7 @@ static int re_index(struct ldb_kv_private *ldb_kv,
 
 	ldb = ldb_module_get_ctx(module);
 
-	if (key.length > 4 &&
-	    memcmp(key.data, "DN=@", 4) == 0) {
+	if (key.length > 4 && memcmp(key.data, "DN=@", 4) == 0) {
 		return 0;
 	}
 
@@ -2967,24 +2995,31 @@ static int re_index(struct ldb_kv_private *ldb_kv,
 		return -1;
 	}
 
-	ret = ldb_unpack_data_only_attr_list_flags(ldb, &val,
-						   msg,
-						   NULL, 0,
-						   LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC,
-						   &nb_elements_in_db);
+	ret = ldb_unpack_data_only_attr_list_flags(
+	    ldb,
+	    &val,
+	    msg,
+	    NULL,
+	    0,
+	    LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC,
+	    &nb_elements_in_db);
 	if (ret != 0) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %s\n",
-						ldb_dn_get_linearized(msg->dn));
+		ldb_debug(ldb,
+			  LDB_DEBUG_ERROR,
+			  "Invalid data for index %s\n",
+			  ldb_dn_get_linearized(msg->dn));
 		ctx->error = ret;
 		talloc_free(msg);
 		return -1;
 	}
 
 	if (msg->dn == NULL) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR,
+		ldb_debug(ldb,
+			  LDB_DEBUG_ERROR,
 			  "Refusing to re-index as GUID "
 			  "key %*.*s with no DN\n",
-			  (int)key.length, (int)key.length,
+			  (int)key.length,
+			  (int)key.length,
 			  (char *)key.data);
 		talloc_free(msg);
 		return -1;
@@ -2992,9 +3027,10 @@ static int re_index(struct ldb_kv_private *ldb_kv,
 
 	ret = ldb_kv_index_onelevel(module, msg, 1);
 	if (ret != LDB_SUCCESS) {
-		ldb_debug(ldb, LDB_DEBUG_ERROR,
+		ldb_debug(ldb,
+			  LDB_DEBUG_ERROR,
 			  "Adding special ONE LEVEL index failed (%s)!",
-						ldb_dn_get_linearized(msg->dn));
+			  ldb_dn_get_linearized(msg->dn));
 		talloc_free(msg);
 		return -1;
 	}
@@ -3011,7 +3047,8 @@ static int re_index(struct ldb_kv_private *ldb_kv,
 
 	ctx->count++;
 	if (ctx->count % 10000 == 0) {
-		ldb_debug(ldb, LDB_DEBUG_WARNING,
+		ldb_debug(ldb,
+			  LDB_DEBUG_WARNING,
 			  "Reindexing: re-indexed %u records so far",
 			  ctx->count);
 	}
@@ -3059,7 +3096,8 @@ int ldb_kv_reindex(struct ldb_module *module)
 	ret = ldb_kv->kv_ops->iterate(ldb_kv, delete_index, module);
 	if (ret < 0) {
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb, "index deletion traverse failed: %s",
+		ldb_asprintf_errstring(ldb,
+				       "index deletion traverse failed: %s",
 				       ldb_errstring(ldb));
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
@@ -3071,14 +3109,16 @@ int ldb_kv_reindex(struct ldb_module *module)
 	ret = ldb_kv->kv_ops->iterate(ldb_kv, re_key, &ctx);
 	if (ret < 0) {
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb, "key correction traverse failed: %s",
+		ldb_asprintf_errstring(ldb,
+				       "key correction traverse failed: %s",
 				       ldb_errstring(ldb));
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
 	if (ctx.error != LDB_SUCCESS) {
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb, "reindexing failed: %s", ldb_errstring(ldb));
+		ldb_asprintf_errstring(
+		    ldb, "reindexing failed: %s", ldb_errstring(ldb));
 		return ctx.error;
 	}
 
@@ -3089,14 +3129,15 @@ int ldb_kv_reindex(struct ldb_module *module)
 	ret = ldb_kv->kv_ops->iterate(ldb_kv, re_index, &ctx);
 	if (ret < 0) {
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb, "reindexing traverse failed: %s",
-				       ldb_errstring(ldb));
+		ldb_asprintf_errstring(
+		    ldb, "reindexing traverse failed: %s", ldb_errstring(ldb));
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 
 	if (ctx.error != LDB_SUCCESS) {
 		struct ldb_context *ldb = ldb_module_get_ctx(module);
-		ldb_asprintf_errstring(ldb, "reindexing failed: %s", ldb_errstring(ldb));
+		ldb_asprintf_errstring(
+		    ldb, "reindexing failed: %s", ldb_errstring(ldb));
 		return ctx.error;
 	}
 
diff --git a/lib/ldb/ldb_key_value/ldb_kv_search.c b/lib/ldb/ldb_key_value/ldb_kv_search.c
index cd7ff52..def6241 100644
--- a/lib/ldb/ldb_key_value/ldb_kv_search.c
+++ b/lib/ldb/ldb_key_value/ldb_kv_search.c
@@ -49,7 +49,10 @@ static int msg_add_element(struct ldb_message *ret,
 		return 0;
 	}
 
-	e2 = talloc_realloc(ret, ret->elements, struct ldb_message_element, ret->num_elements+1);
+	e2 = talloc_realloc(ret,
+			    ret->elements,
+			    struct ldb_message_element,
+			    ret->num_elements + 1);
 	if (!e2) {
 		return -1;
 	}
@@ -63,7 +66,8 @@ static int msg_add_element(struct ldb_message *ret,
 	}
 
 	if (el->num_values) {
-		elnew->values = talloc_array(ret->elements, struct ldb_val, el->num_values);
+		elnew->values =
+		    talloc_array(ret->elements, struct ldb_val, el->num_values);
 		if (!elnew->values) {
 			return -1;
 		}
@@ -71,7 +75,7 @@ static int msg_add_element(struct ldb_message *ret,
 		elnew->values = NULL;
 	}
 
-	for (i=0;i<el->num_values;i++) {
+	for (i = 0; i < el->num_values; i++) {
 		elnew->values[i] = ldb_val_dup(elnew->values, &el->values[i]);
 		if (elnew->values[i].length != el->values[i].length) {
 			return -1;
@@ -142,10 +146,8 @@ int ldb_kv_search_base(struct ldb_module *module,
 
 	ret = ldb_kv_search_dn1(module, dn, msg, LDB_UNPACK_DATA_FLAG_NO_ATTRS);
 	if (ret == LDB_SUCCESS) {
-		const char *dn_linearized
-			= ldb_dn_get_linearized(dn);
-		const char *msg_dn_linearlized
-			= ldb_dn_get_linearized(msg->dn);
+		const char *dn_linearized = ldb_dn_get_linearized(dn);
+		const char *msg_dn_linearlized = ldb_dn_get_linearized(msg->dn);
 
 		if (strcmp(dn_linearized, msg_dn_linearlized) == 0) {
 			/*
@@ -197,21 +199,25 @@ static int ldb_kv_parse_data_unpack(struct ldb_val key,
 		 * data buffer as that may change later
 		 * and the caller needs a stable result.
 		 */
-		data_parse.data = talloc_memdup(ctx->msg,
-						data.data,
-						data.length);
+		data_parse.data =
+		    talloc_memdup(ctx->msg, data.data, data.length);
 		if (data_parse.data == NULL) {
-			ldb_debug(ldb, LDB_DEBUG_ERROR,
+			ldb_debug(ldb,
+				  LDB_DEBUG_ERROR,
 				  "Unable to allocate data(%d) for %*.*s\n",
 				  (int)data.length,
-				  (int)key.length, (int)key.length, key.data);
+				  (int)key.length,
+				  (int)key.length,
+				  key.data);
 			return LDB_ERR_OPERATIONS_ERROR;
 		}
 	}
 
-	ret = ldb_unpack_data_only_attr_list_flags(ldb, &data_parse,
+	ret = ldb_unpack_data_only_attr_list_flags(ldb,
+						   &data_parse,
 						   ctx->msg,
-						   NULL, 0,
+						   NULL,
+						   0,
 						   ctx->unpack_flags,
 						   &nb_elements_in_db);
 	if (ret == -1) {
@@ -219,8 +225,12 @@ static int ldb_kv_parse_data_unpack(struct ldb_val key,
 			talloc_free(data_parse.data);
 		}
 
-		ldb_debug(ldb, LDB_DEBUG_ERROR, "Invalid data for index %*.*s\n",
-			  (int)key.length, (int)key.length, key.data);
+		ldb_debug(ldb,
+			  LDB_DEBUG_ERROR,
+			  "Invalid data for index %*.*s\n",
+			  (int)key.length,
+			  (int)key.length,
+			  key.data);
 		return LDB_ERR_OPERATIONS_ERROR;
 	}
 	return ret;
@@ -241,10 +251,7 @@ int ldb_kv_search_key(struct ldb_module *module,
 {
 	int ret;
 	struct ldb_kv_parse_data_unpack_ctx ctx = {
-		.msg = msg,
-		.module = module,
-		.unpack_flags = unpack_flags
-	};
+	    .msg = msg, .module = module, .unpack_flags = unpack_flags};
 
 	memset(msg, 0, sizeof(*msg));
 
@@ -288,10 +295,7 @@ int ldb_kv_search_dn1(struct ldb_module *module,
 	    talloc_get_type(data, struct ldb_kv_private);
 	int ret;
 	uint8_t guid_key[LDB_KV_GUID_KEY_SIZE];
-	struct ldb_val key = {
-		.data = guid_key,
-		.length = sizeof(guid_key)
-	};
+	struct ldb_val key = {.data = guid_key, .length = sizeof(guid_key)};
 	TALLOC_CTX *tdb_key_ctx = NULL;
 
 	if (ldb_kv->cache->GUID_index_attribute == NULL ||
@@ -393,7 +397,7 @@ int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
 		add_dn = true;
 		elements_size = msg->num_elements + 1;
 
-	/* Shortcuts for the simple cases */
+		/* Shortcuts for the simple cases */
 	} else if (add_dn && i == 1) {
 		if (msg_add_distinguished_name(msg2) != 0) {
 			goto failed;
@@ -404,14 +408,16 @@ int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
 		*filtered_msg = msg2;
 		return 0;
 
-	/* Otherwise we are copying at most as many element as we have attributes */
+		/* Otherwise we are copying at most as many element as we have
+		 * attributes */
 	} else {
 		elements_size = i;
 	}
 
-	msg2->elements = talloc_array(msg2, struct ldb_message_element,
-				      elements_size);
-	if (msg2->elements == NULL) goto failed;
+	msg2->elements =
+	    talloc_array(msg2, struct ldb_message_element, elements_size);
+	if (msg2->elements == NULL)
+		goto failed;
 
 	num_elements = 0;
 
@@ -438,13 +444,16 @@ int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
 		if (el2->name == NULL) {
 			goto failed;
 		}
-		el2->values = talloc_array(msg2->elements, struct ldb_val, el->num_values);
+		el2->values = talloc_array(
+		    msg2->elements, struct ldb_val, el->num_values);
 		if (el2->values == NULL) {
 			goto failed;
 		}
-		for (j=0;j<el->num_values;j++) {
-			el2->values[j] = ldb_val_dup(el2->values, &el->values[j]);
-			if (el2->values[j].data == NULL && el->values[j].length != 0) {
+		for (j = 0; j < el->num_values; j++) {
+			el2->values[j] =
+			    ldb_val_dup(el2->values, &el->values[j]);
+			if (el2->values[j].data == NULL &&
+			    el->values[j].length != 0) {
 				goto failed;
 			}
 		}
@@ -467,7 +476,8 @@ int ldb_kv_filter_attrs(TALLOC_CTX *mem_ctx,
 	}
 
 	if (msg2->num_elements > 0) {
-		msg2->elements = talloc_realloc(msg2, msg2->elements,
+		msg2->elements = talloc_realloc(msg2,
+						msg2->elements,
 						struct ldb_message_element,
 						msg2->num_elements);
 		if (msg2->elements == NULL) {
@@ -515,12 +525,15 @@ static int search_func(struct ldb_kv_private *ldb_kv,
 	}
 
 	/* unpack the record */
-	ret = ldb_unpack_data_only_attr_list_flags(ldb, &val,
-						   msg,
-						   NULL, 0,
-						   LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC|
-						   LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC,
-						   &nb_elements_in_db);
+	ret = ldb_unpack_data_only_attr_list_flags(
+	    ldb,
+	    &val,
+	    msg,
+	    NULL,
+	    0,
+	    LDB_UNPACK_DATA_FLAG_NO_DATA_ALLOC |
+		LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC,
+	    &nb_elements_in_db);
 	if (ret == -1) {
 		talloc_free(msg);
 		ac->error = LDB_ERR_OPERATIONS_ERROR;
@@ -528,8 +541,7 @@ static int search_func(struct ldb_kv_private *ldb_kv,
 	}
 
 	if (!msg->dn) {
-		msg->dn = ldb_dn_new(msg, ldb,
-				     (char *)key.data + 3);
+		msg->dn = ldb_dn_new(msg, ldb, (char *)key.data + 3);
 		if (msg->dn == NULL) {
 			talloc_free(msg);
 			ac->error = LDB_ERR_OPERATIONS_ERROR;
@@ -538,8 +550,8 @@ static int search_func(struct ldb_kv_private *ldb_kv,
 	}
 
 	/* see if it matches the given expression */
-	ret = ldb_match_msg_error(ldb, msg,
-				  ac->tree, ac->base, ac->scope, &matched);
+	ret = ldb_match_msg_error(
+	    ldb, msg, ac->tree, ac->base, ac->scope, &matched);
 	if (ret != LDB_SUCCESS) {
 		talloc_free(msg);
 		ac->error = LDB_ERR_OPERATIONS_ERROR;
@@ -570,7 +582,6 @@ static int search_func(struct ldb_kv_private *ldb_kv,
 	return 0;
 }
 
-
 /*
   search the database with a LDAP-like expression.
   this is the "full search" non-indexed variant
@@ -630,17 +641,13 @@ static int ldb_kv_search_and_return_base(struct ldb_kv_private *ldb_kv,
 		return ret;
 	}
 
-
 	/*
 	 * We use this, not ldb_match_msg_error() as we know
 	 * we matched on the scope BASE, as we just fetched
 	 * the base DN
 	 */
 
-	ret = ldb_match_message(ldb, msg,
-				ctx->tree,
-				ctx->scope,
-				&matched);
+	ret = ldb_match_message(ldb, msg, ctx->tree, ctx->scope, &matched);
 	if (ret != LDB_SUCCESS) {
 		talloc_free(msg);
 		return ret;
@@ -731,31 +738,34 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 	ctx->base = req->op.search.base;
 	ctx->attrs = req->op.search.attrs;
 
-	if ((req->op.search.base == NULL) || (ldb_dn_is_null(req->op.search.base) == true)) {
+	if ((req->op.search.base == NULL) ||
+	    (ldb_dn_is_null(req->op.search.base) == true)) {
 
 		/* Check what we should do with a NULL dn */
 		switch (req->op.search.scope) {
 		case LDB_SCOPE_BASE:
-			ldb_asprintf_errstring(ldb,
-					       "NULL Base DN invalid for a base search");
+			ldb_asprintf_errstring(
+			    ldb, "NULL Base DN invalid for a base search");
 			ret = LDB_ERR_INVALID_DN_SYNTAX;
 			break;
 		case LDB_SCOPE_ONELEVEL:
-			ldb_asprintf_errstring(ldb,
-					       "NULL Base DN invalid for a one-level search");
+			ldb_asprintf_errstring(
+			    ldb, "NULL Base DN invalid for a one-level search");
 			ret = LDB_ERR_INVALID_DN_SYNTAX;
 			break;
 		case LDB_SCOPE_SUBTREE:
 		default:
-			/* We accept subtree searches from a NULL base DN, ie over the whole DB */
+			/* We accept subtree searches from a NULL base DN, ie
+			 * over the whole DB */
 			ret = LDB_SUCCESS;
 		}
 	} else if (ldb_dn_is_valid(req->op.search.base) == false) {
 
 		/* We don't want invalid base DNs here */
-		ldb_asprintf_errstring(ldb,
-				       "Invalid Base DN: %s",
-				       ldb_dn_get_linearized(req->op.search.base));
+		ldb_asprintf_errstring(
+		    ldb,
+		    "Invalid Base DN: %s",
+		    ldb_dn_get_linearized(req->op.search.base));
 		ret = LDB_ERR_INVALID_DN_SYNTAX;
 
 	} else if (req->op.search.scope == LDB_SCOPE_BASE) {
@@ -784,9 +794,10 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 		    module, ctx, req->op.search.base, &ctx->base);
 
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
-			ldb_asprintf_errstring(ldb,
-					       "No such Base DN: %s",
-					       ldb_dn_get_linearized(req->op.search.base));
+			ldb_asprintf_errstring(
+			    ldb,
+			    "No such Base DN: %s",
+			    ldb_dn_get_linearized(req->op.search.base));
 		}
 
 	} else {
@@ -801,7 +812,6 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 		if (ret == LDB_ERR_NO_SUCH_OBJECT) {
 			/* Not in the index, therefore OK! */
 			ret = LDB_SUCCESS;
-
 		}
 		/* Check if we got just a normal error.
 		 * In that case proceed to a full search unless we got a
@@ -812,13 +822,23 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 			    ldb_kv->disable_full_db_scan) {
 				/* useful for debugging when slow performance
 				 * is caused by unindexed searches */
-				char *expression = ldb_filter_from_tree(ctx, ctx->tree);
-				ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb FULL SEARCH: %s SCOPE: %s DN: %s",
-							expression,
-							req->op.search.scope==LDB_SCOPE_BASE?"base":
-							req->op.search.scope==LDB_SCOPE_ONELEVEL?"one":
-							req->op.search.scope==LDB_SCOPE_SUBTREE?"sub":"UNKNOWN",
-							ldb_dn_get_linearized(req->op.search.base));
+				char *expression =
+				    ldb_filter_from_tree(ctx, ctx->tree);
+				ldb_debug(
+				    ldb,
+				    LDB_DEBUG_ERROR,
+				    "ldb FULL SEARCH: %s SCOPE: %s DN: %s",
+				    expression,
+				    req->op.search.scope == LDB_SCOPE_BASE
+					? "base"
+					: req->op.search.scope ==
+						  LDB_SCOPE_ONELEVEL
+					      ? "one"
+					      : req->op.search.scope ==
+							LDB_SCOPE_SUBTREE
+						    ? "sub"
+						    : "UNKNOWN",
+				    ldb_dn_get_linearized(req->op.search.base));
 
 				talloc_free(expression);
 			}
@@ -845,7 +865,9 @@ int ldb_kv_search(struct ldb_kv_context *ctx)
 
 			ret = ldb_kv_search_full(ctx);
 			if (ret != LDB_SUCCESS) {
-				ldb_set_errstring(ldb, "Indexed and full searches both failed!\n");
+				ldb_set_errstring(
+				    ldb,
+				    "Indexed and full searches both failed!\n");
 			}
 		}
 	}
-- 
2.7.4

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://lists.samba.org/pipermail/samba-technical/attachments/20180725/e98cdfa2/signature-0001.sig>


More information about the samba-technical mailing list