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 GUID indexing was toggled in this transaction, we repack at
	 * format version 2 if GUID indexing was enabled, or version 1 if
	 * it was disabled.
	 */
	ret = ldb_kv_maybe_repack(ldb_kv);
	if (ret != LDB_SUCCESS) {
		ldb_kv_del_trans(module);
		ldb_set_errstring(ldb_module_get_ctx(module),
				  "Failure during re-pack, so "
				  "transaction must be aborted.");
		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_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;
	}

	 
	if (ldb_kv->reindex_failed) {
		 
		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;
	}

	 
	ret = ldb_kv_maybe_repack(ldb_kv);
	if (ret != LDB_SUCCESS) {
		ldb_kv_del_trans(module);
		ldb_set_errstring(ldb_module_get_ctx(module),
				  "Failure during re-pack, so "
				  "transaction must be aborted.");
		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;
}