s3:idmap_tdb: convert idmap_tdb_allocate_id() to use transaction wrappers
authorMichael Adam <obnox@samba.org>
Wed, 16 Jun 2010 07:19:21 +0000 (09:19 +0200)
committerMichael Adam <obnox@samba.org>
Wed, 23 Jun 2010 10:22:10 +0000 (12:22 +0200)
source3/winbindd/idmap_tdb.c

index 42e660b40ab51b049732a932cd65a84d93c1c604..3767b02d9684b5a6d522c7d59fed505dda30ef0c 100644 (file)
@@ -399,14 +399,67 @@ static NTSTATUS idmap_tdb_alloc_init( const char *params )
  Allocate a new id. 
 **********************************/
 
-static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
+struct idmap_tdb_allocate_id_context {
+       const char *hwmkey;
+       const char *hwmtype;
+       uint32_t high_hwm;
+       uint32_t hwm;
+};
+
+static NTSTATUS idmap_tdb_allocate_id_action(struct db_context *db,
+                                            void *private_data)
 {
        NTSTATUS ret;
+       struct idmap_tdb_allocate_id_context *state;
+       uint32_t hwm;
+
+       state = (struct idmap_tdb_allocate_id_context *)private_data;
+
+       hwm = dbwrap_fetch_int32(db, state->hwmkey);
+       if (hwm == -1) {
+               ret = NT_STATUS_INTERNAL_DB_ERROR;
+               goto done;
+       }
+
+       /* check it is in the range */
+       if (hwm > state->high_hwm) {
+               DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
+                         state->hwmtype, (unsigned long)state->high_hwm));
+               ret = NT_STATUS_UNSUCCESSFUL;
+               goto done;
+       }
+
+       /* fetch a new id and increment it */
+       ret = dbwrap_trans_change_uint32_atomic(db, state->hwmkey, &hwm, 1);
+       if (!NT_STATUS_IS_OK(ret)) {
+               DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
+                         state->hwmtype, nt_errstr(ret)));
+               goto done;
+       }
+
+       /* recheck it is in the range */
+       if (hwm > state->high_hwm) {
+               DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n",
+                         state->hwmtype, (unsigned long)state->high_hwm));
+               ret = NT_STATUS_UNSUCCESSFUL;
+               goto done;
+       }
+
+       ret = NT_STATUS_OK;
+       state->hwm = hwm;
+
+done:
+       return ret;
+}
+
+static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
+{
        const char *hwmkey;
        const char *hwmtype;
        uint32_t high_hwm;
-       uint32_t hwm;
-       int res;
+       uint32_t hwm = 0;
+       NTSTATUS status;
+       struct idmap_tdb_allocate_id_context state;
 
        /* Get current high water mark */
        switch (xid->type) {
@@ -428,52 +481,22 @@ static NTSTATUS idmap_tdb_allocate_id(struct unixid *xid)
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       res = idmap_alloc_db->transaction_start(idmap_alloc_db);
-       if (res != 0) {
-               DEBUG(1, (__location__ " Failed to start transaction.\n"));
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       if ((hwm = dbwrap_fetch_int32(idmap_alloc_db, hwmkey)) == -1) {
-               idmap_alloc_db->transaction_cancel(idmap_alloc_db);
-               return NT_STATUS_INTERNAL_DB_ERROR;
-       }
-
-       /* check it is in the range */
-       if (hwm > high_hwm) {
-               DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n", 
-                         hwmtype, (unsigned long)high_hwm));
-               idmap_alloc_db->transaction_cancel(idmap_alloc_db);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
+       state.hwm = hwm;
+       state.high_hwm = high_hwm;
+       state.hwmtype = hwmtype;
+       state.hwmkey = hwmkey;
 
-       /* fetch a new id and increment it */
-       ret = dbwrap_change_uint32_atomic(idmap_alloc_db, hwmkey, &hwm, 1);
-       if (!NT_STATUS_IS_OK(ret)) {
-               DEBUG(0, ("Fatal error while fetching a new %s value: %s\n!",
-                         hwmtype, nt_errstr(ret)));
-               idmap_alloc_db->transaction_cancel(idmap_alloc_db);
-               return ret;
-       }
+       status = dbwrap_trans_do(idmap_alloc_db, idmap_tdb_allocate_id_action,
+                                &state);
 
-       /* recheck it is in the range */
-       if (hwm > high_hwm) {
-               DEBUG(1, ("Fatal Error: %s range full!! (max: %lu)\n", 
-                         hwmtype, (unsigned long)high_hwm));
-               idmap_alloc_db->transaction_cancel(idmap_alloc_db);
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       res = idmap_alloc_db->transaction_commit(idmap_alloc_db);
-       if (res != 0) {
-               DEBUG(1, (__location__ " Failed to commit transaction.\n"));
-               return NT_STATUS_UNSUCCESSFUL;
+       if (NT_STATUS_IS_OK(status)) {
+               xid->id = state.hwm;
+               DEBUG(10,("New %s = %d\n", hwmtype, state.hwm));
+       } else {
+               DEBUG(1, ("Error allocating a new %s\n", hwmtype));
        }
 
-       xid->id = hwm;
-       DEBUG(10,("New %s = %d\n", hwmtype, hwm));
-
-       return NT_STATUS_OK;
+       return status;
 }
 
 /**********************************