Browse Source

Import c1c0ede32dc00ed619d1cf5fda40a9de43995f3a from ctdb:
tdb: add TDB_DISALLOW_NESTING and make TDB_ALLOW_NESTING the default behavior

We need to keep TDB_ALLOW_NESTING as default behavior,
so that existing code continues to work.

However we may change the default together with a major version
number change in future.

metze
(cherry picked from samba commit 3b9f19ed919fef2e88b2f92ae541e07bc7379cd1)

Signed-off-by: Stefan Metzmacher <metze@samba.org>

Rusty Russell 16 years ago
parent
commit
4364f6a8e2
4 changed files with 59 additions and 3 deletions
  1. 17 0
      ccan/tdb/open.c
  2. 30 0
      ccan/tdb/tdb.c
  3. 1 0
      ccan/tdb/tdb.h
  4. 11 3
      ccan/tdb/transaction.c

+ 17 - 0
ccan/tdb/open.c

@@ -203,6 +203,23 @@ struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
 		tdb->flags &= ~TDB_CLEAR_IF_FIRST;
 		tdb->flags &= ~TDB_CLEAR_IF_FIRST;
 	}
 	}
 
 
+	if ((tdb->flags & TDB_ALLOW_NESTING) &&
+	    (tdb->flags & TDB_DISALLOW_NESTING)) {
+		tdb->ecode = TDB_ERR_NESTING;
+		TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_open_ex: "
+			"allow_nesting and disallow_nesting are not allowed together!"));
+		errno = EINVAL;
+		goto fail;
+	}
+
+	/*
+	 * TDB_ALLOW_NESTING is the default behavior.
+	 * Note: this may change in future versions!
+	 */
+	if (!(tdb->flags & TDB_DISALLOW_NESTING)) {
+		tdb->flags |= TDB_ALLOW_NESTING;
+	}
+
 	/* internal databases don't mmap or lock, and start off cleared */
 	/* internal databases don't mmap or lock, and start off cleared */
 	if (tdb->flags & TDB_INTERNAL) {
 	if (tdb->flags & TDB_INTERNAL) {
 		tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
 		tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);

+ 30 - 0
ccan/tdb/tdb.c

@@ -735,11 +735,41 @@ int tdb_get_flags(struct tdb_context *tdb)
 
 
 void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
 void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
 {
 {
+	if ((flags & TDB_ALLOW_NESTING) &&
+	    (flags & TDB_DISALLOW_NESTING)) {
+		tdb->ecode = TDB_ERR_NESTING;
+		TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_add_flags: "
+			"allow_nesting and disallow_nesting are not allowed together!"));
+		return;
+	}
+
+	if (flags & TDB_ALLOW_NESTING) {
+		tdb->flags &= ~TDB_DISALLOW_NESTING;
+	}
+	if (flags & TDB_DISALLOW_NESTING) {
+		tdb->flags &= ~TDB_ALLOW_NESTING;
+	}
+
 	tdb->flags |= flags;
 	tdb->flags |= flags;
 }
 }
 
 
 void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
 void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
 {
 {
+	if ((flags & TDB_ALLOW_NESTING) &&
+	    (flags & TDB_DISALLOW_NESTING)) {
+		tdb->ecode = TDB_ERR_NESTING;
+		TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_remove_flags: "
+			"allow_nesting and disallow_nesting are not allowed together!"));
+		return;
+	}
+
+	if (flags & TDB_ALLOW_NESTING) {
+		tdb->flags |= TDB_DISALLOW_NESTING;
+	}
+	if (flags & TDB_DISALLOW_NESTING) {
+		tdb->flags |= TDB_ALLOW_NESTING;
+	}
+
 	tdb->flags &= ~flags;
 	tdb->flags &= ~flags;
 }
 }
 
 

+ 1 - 0
ccan/tdb/tdb.h

@@ -56,6 +56,7 @@ extern "C" {
 #define TDB_SEQNUM   128 /* maintain a sequence number */
 #define TDB_SEQNUM   128 /* maintain a sequence number */
 #define TDB_VOLATILE   256 /* Activate the per-hashchain freelist, default 5 */
 #define TDB_VOLATILE   256 /* Activate the per-hashchain freelist, default 5 */
 #define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */
 #define TDB_ALLOW_NESTING 512 /* Allow transactions to nest */
+#define TDB_DISALLOW_NESTING 1024 /* Disallow transactions to nest */
 
 
 /* error codes */
 /* error codes */
 enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK, 
 enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK, 

+ 11 - 3
ccan/tdb/transaction.c

@@ -86,12 +86,20 @@
     fsync/msync calls are made.
     fsync/msync calls are made.
 
 
   - if TDB_ALLOW_NESTING is passed to flags in tdb open, or added using
   - if TDB_ALLOW_NESTING is passed to flags in tdb open, or added using
-    tdb_add_flags() transaction is enabled.
-    The default is that transaction nesting is not allowed and an attempt
-    to create a nested transaction will fail with TDB_ERR_NESTING.
+    tdb_add_flags() transaction nesting is enabled.
+    It resets the TDB_DISALLOW_NESTING flag, as both cannot be used together.
+    The default is that transaction nesting is allowed.
+    Note: this default may change in future versions of tdb.
 
 
     Beware. when transactions are nested a transaction successfully
     Beware. when transactions are nested a transaction successfully
     completed with tdb_transaction_commit() can be silently unrolled later.
     completed with tdb_transaction_commit() can be silently unrolled later.
+
+  - if TDB_DISALLOW_NESTING is passed to flags in tdb open, or added using
+    tdb_add_flags() transaction nesting is disabled.
+    It resets the TDB_ALLOW_NESTING flag, as both cannot be used together.
+    An attempt create a nested transaction will fail with TDB_ERR_NESTING.
+    The default is that transaction nesting is allowed.
+    Note: this default may change in future versions of tdb.
 */
 */