s3:dbwrap_ctdb: add a function db_ctdb_ltdb_fetch()
[amitay/samba.git] / source3 / lib / dbwrap_ctdb.c
index 8261e2f7330463f07f59442ce2c1dfaaa8818ceb..145c0f65ea831bd9def68af6dcc21d3949df5470 100644 (file)
@@ -2,17 +2,17 @@
    Unix SMB/CIFS implementation.
    Database interface wrapper around ctdbd
    Copyright (C) Volker Lendecke 2007
-   
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program 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 General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 struct db_ctdb_transaction_handle {
        struct db_ctdb_ctx *ctx;
        bool in_replay;
-       /* we store the reads and writes done under a transaction one
-          list stores both reads and writes, the other just writes
-       */
+       /*
+        * we store the reads and writes done under a transaction:
+        * - one list stores both reads and writes (m_all),
+        * - the other just writes (m_write)
+        */
        struct ctdb_marshall_buffer *m_all;
        struct ctdb_marshall_buffer *m_write;
+       uint32_t nesting;
+       bool nested_cancel;
 };
 
 struct db_ctdb_ctx {
@@ -71,10 +75,95 @@ static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
 }
 
 
+/**
+ * fetch a record from the tdb, separating out the header
+ * information and returning the body of the record.
+ */
+static NTSTATUS db_ctdb_ltdb_fetch(struct db_ctdb_ctx *db,
+                                  TDB_DATA key,
+                                  struct ctdb_ltdb_header *header,
+                                  TALLOC_CTX *mem_ctx,
+                                  TDB_DATA *data)
+{
+       TDB_DATA rec;
+       NTSTATUS status;
+
+       rec = tdb_fetch(db->wtdb->tdb, key);
+       if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
+               status = NT_STATUS_NOT_FOUND;
+               if (data) {
+                       ZERO_STRUCTP(data);
+               }
+               if (header) {
+                       header->dmaster = (uint32_t)-1;
+                       header->rsn = 0;
+               }
+               goto done;
+       }
+
+       if (header) {
+               *header = *(struct ctdb_ltdb_header *)rec.dptr;
+       }
+
+       if (data) {
+               data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
+               if (data->dsize == 0) {
+                       data->dptr = NULL;
+               } else {
+                       data->dptr = (unsigned char *)talloc_memdup(mem_ctx,
+                                       rec.dptr
+                                        + sizeof(struct ctdb_ltdb_header),
+                                       data->dsize);
+                       if (data->dptr == NULL) {
+                               status = NT_STATUS_NO_MEMORY;
+                               goto done;
+                       }
+               }
+       }
+
+       status = NT_STATUS_OK;
+
+done:
+       SAFE_FREE(rec.dptr);
+       return status;
+}
+
+/*
+ * Store a record together with the ctdb record header
+ * in the local copy of the database.
+ */
+static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
+                                  TDB_DATA key,
+                                  struct ctdb_ltdb_header *header,
+                                  TDB_DATA data)
+{
+       TALLOC_CTX *tmp_ctx = talloc_stackframe();
+       TDB_DATA rec;
+       int ret;
+
+       rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
+       rec.dptr = (uint8_t *)talloc_size(tmp_ctx, rec.dsize);
+
+       if (rec.dptr == NULL) {
+               talloc_free(tmp_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
+       memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
+
+       ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
+
+       talloc_free(tmp_ctx);
+
+       return (ret == 0) ? NT_STATUS_OK
+                         : tdb_error_to_ntstatus(db->wtdb->tdb);
+
+}
 
 /*
   form a ctdb_rec_data record from a key/data pair
-  
+
   note that header may be NULL. If not NULL then it is included in the data portion
   of the record
  */
@@ -119,18 +208,19 @@ static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
 {
        struct ctdb_rec_data *r;
        size_t m_size, r_size;
-       struct ctdb_marshall_buffer *m2;
+       struct ctdb_marshall_buffer *m2 = NULL;
 
-       r = db_ctdb_marshall_record(mem_ctx, reqid, key, header, data);
+       r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
        if (r == NULL) {
                talloc_free(m);
                return NULL;
        }
 
        if (m == NULL) {
-               m = talloc_zero_size(mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
+               m = (struct ctdb_marshall_buffer *)talloc_zero_size(
+                       mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
                if (m == NULL) {
-                       return NULL;
+                       goto done;
                }
                m->db_id = db_id;
        }
@@ -138,18 +228,19 @@ static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
        m_size = talloc_get_size(m);
        r_size = talloc_get_size(r);
 
-       m2 = talloc_realloc_size(mem_ctx, m,  m_size + r_size);
+       m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
+               mem_ctx, m,  m_size + r_size);
        if (m2 == NULL) {
                talloc_free(m);
-               return NULL;
+               goto done;
        }
 
        memcpy(m_size + (uint8_t *)m2, r, r_size);
 
-       talloc_free(r);
-
        m2->count++;
 
+done:
+       talloc_free(r);
        return m2;
 }
 
@@ -164,7 +255,7 @@ static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
 
 /* 
    loop over a marshalling buffer 
-   
+
      - pass r==NULL to start
      - loop the number of times indicated by m->count
 */
@@ -182,7 +273,7 @@ static struct ctdb_rec_data *db_ctdb_marshall_loop_next(struct ctdb_marshall_buf
        if (reqid != NULL) {
                *reqid = r->reqid;
        }
-       
+
        if (key != NULL) {
                key->dptr   = &r->data[0];
                key->dsize  = r->keylen;
@@ -208,14 +299,20 @@ static struct ctdb_rec_data *db_ctdb_marshall_loop_next(struct ctdb_marshall_buf
 
 
 
-/* start a transaction on a database */
+/**
+ * CTDB transaction destructor
+ */
 static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
 {
        tdb_transaction_cancel(h->ctx->wtdb->tdb);
        return 0;
 }
 
-/* start a transaction on a database */
+/**
+ * start a transaction on a ctdb database:
+ * - lock the transaction lock key
+ * - start the tdb transaction
+ */
 static int db_ctdb_transaction_fetch_start(struct db_ctdb_transaction_handle *h)
 {
        struct db_record *rh;
@@ -226,7 +323,7 @@ static int db_ctdb_transaction_fetch_start(struct db_ctdb_transaction_handle *h)
        struct db_ctdb_ctx *ctx = h->ctx;
        TDB_DATA data;
 
-       key.dptr = discard_const(keyname);
+       key.dptr = (uint8_t *)discard_const(keyname);
        key.dsize = strlen(keyname);
 
 again:
@@ -264,7 +361,10 @@ again:
 }
 
 
-/* start a transaction on a database */
+/**
+ * CTDB dbwrap API: transaction_start function
+ * starts a transaction on a persistent database
+ */
 static int db_ctdb_transaction_start(struct db_context *db)
 {
        struct db_ctdb_transaction_handle *h;
@@ -279,8 +379,8 @@ static int db_ctdb_transaction_start(struct db_context *db)
        }
 
        if (ctx->transaction) {
-               DEBUG(0,("Nested transactions not supported on db 0x%08x\n", ctx->db_id));
-               return -1;
+               ctx->transaction->nesting++;
+               return 0;
        }
 
        h = talloc_zero(db, struct db_ctdb_transaction_handle);
@@ -401,8 +501,9 @@ static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ct
        return result;
 }
 
-static int db_ctdb_record_destructor(struct db_record *rec)
+static int db_ctdb_record_destructor(struct db_record **recp)
 {
+       struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
        struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
                rec->private_data, struct db_ctdb_transaction_handle);
        int ret = h->ctx->db->transaction_commit(h->ctx->db);
@@ -420,7 +521,7 @@ static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx
                                                         TDB_DATA key)
 {
        int res;
-       struct db_record *rec;
+       struct db_record *rec, **recp;
 
        res = db_ctdb_transaction_start(ctx->db);
        if (res == -1) {
@@ -434,7 +535,14 @@ static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx
        }
 
        /* destroy this transaction when we release the lock */
-       talloc_set_destructor((struct db_record *)talloc_new(rec), db_ctdb_record_destructor);
+       recp = talloc(rec, struct db_record *);
+       if (recp == NULL) {
+               ctx->db->transaction_cancel(ctx->db);
+               talloc_free(rec);
+               return NULL;
+       }
+       *recp = rec;
+       talloc_set_destructor(recp, db_ctdb_record_destructor);
        return rec;
 }
 
@@ -449,6 +557,7 @@ static int db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
        int ret;
        TDB_DATA rec;
        struct ctdb_ltdb_header header;
+       NTSTATUS status;
 
        /* we need the header so we can update the RSN */
        rec = tdb_fetch(h->ctx->wtdb->tdb, key);
@@ -457,7 +566,6 @@ static int db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
                   This is only safe because we are in a transaction and this
                   is a persistent database */
                ZERO_STRUCT(header);
-               header.dmaster = get_my_vnn();
        } else {
                memcpy(&header, rec.dptr, sizeof(struct ctdb_ltdb_header));
                rec.dsize -= sizeof(struct ctdb_ltdb_header);
@@ -471,6 +579,7 @@ static int db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
                SAFE_FREE(rec.dptr);
        }
 
+       header.dmaster = get_my_vnn();
        header.rsn++;
 
        if (!h->in_replay) {
@@ -481,28 +590,23 @@ static int db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
                        return -1;
                }
        }
-               
+
        h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
        if (h->m_write == NULL) {
                DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
                talloc_free(tmp_ctx);
                return -1;
        }
-       
-       rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
-       rec.dptr = talloc_size(tmp_ctx, rec.dsize);
-       if (rec.dptr == NULL) {
-               DEBUG(0,(__location__ " Failed to alloc record\n"));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
-       memcpy(rec.dptr, &header, sizeof(struct ctdb_ltdb_header));
-       memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
 
-       ret = tdb_store(h->ctx->wtdb->tdb, key, rec, TDB_REPLACE);
+       status = db_ctdb_ltdb_store(h->ctx, key, &header, data);
+       if (NT_STATUS_IS_OK(status)) {
+               ret = 0;
+       } else {
+               ret = -1;
+       }
 
        talloc_free(tmp_ctx);
-       
+
        return ret;
 }
 
@@ -588,7 +692,7 @@ static int ctdb_replay_transaction(struct db_ctdb_transaction_handle *h)
                        talloc_free(tmp_ctx);
                }
        }
-       
+
        return 0;
 
 failed:
@@ -616,6 +720,17 @@ static int db_ctdb_transaction_commit(struct db_context *db)
                return -1;
        }
 
+       if (h->nested_cancel) {
+               db->transaction_cancel(db);
+               DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
+               return -1;
+       }
+
+       if (h->nesting != 0) {
+               h->nesting--;
+               return 0;
+       }
+
        DEBUG(5,(__location__ " Commit transaction on db 0x%08x\n", ctx->db_id));
 
        talloc_set_destructor(h, NULL);
@@ -646,8 +761,9 @@ again:
 
        /* tell ctdbd to commit to the other nodes */
        rets = ctdbd_control_local(messaging_ctdbd_connection(), 
-                                 CTDB_CONTROL_TRANS2_COMMIT, h->ctx->db_id, 0,
-                                 db_ctdb_marshall_finish(h->m_write), NULL, NULL, &status);
+                                  retries==0?CTDB_CONTROL_TRANS2_COMMIT:CTDB_CONTROL_TRANS2_COMMIT_RETRY, 
+                                  h->ctx->db_id, 0,
+                                  db_ctdb_marshall_finish(h->m_write), NULL, NULL, &status);
        if (!NT_STATUS_IS_OK(rets) || status != 0) {
                tdb_transaction_cancel(h->ctx->wtdb->tdb);
                sleep(1);
@@ -669,7 +785,7 @@ again:
                        }
                }
 
-               if (++retries == 10) {
+               if (++retries == 5) {
                        DEBUG(0,(__location__ " Giving up transaction on db 0x%08x after %d retries failure_control=%u\n", 
                                 h->ctx->db_id, retries, (unsigned)failure_control));
                        ctdbd_control_local(messaging_ctdbd_connection(), failure_control,
@@ -733,6 +849,12 @@ static int db_ctdb_transaction_cancel(struct db_context *db)
                return -1;
        }
 
+       if (h->nesting != 0) {
+               h->nesting--;
+               h->nested_cancel = true;
+               return 0;
+       }
+
        DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
 
        ctx->transaction = NULL;
@@ -745,24 +867,8 @@ static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
 {
        struct db_ctdb_rec *crec = talloc_get_type_abort(
                rec->private_data, struct db_ctdb_rec);
-       TDB_DATA cdata;
-       int ret;
-
-       cdata.dsize = sizeof(crec->header) + data.dsize;
 
-       if (!(cdata.dptr = SMB_MALLOC_ARRAY(uint8, cdata.dsize))) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       memcpy(cdata.dptr, &crec->header, sizeof(crec->header));
-       memcpy(cdata.dptr + sizeof(crec->header), data.dptr, data.dsize);
-
-       ret = tdb_store(crec->ctdb_ctx->wtdb->tdb, rec->key, cdata, TDB_REPLACE);
-
-       SAFE_FREE(cdata.dptr);
-
-       return (ret == 0) ? NT_STATUS_OK
-                         : tdb_error_to_ntstatus(crec->ctdb_ctx->wtdb->tdb);
+       return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
 }
 
 
@@ -791,7 +897,7 @@ static int db_ctdb_record_destr(struct db_record* data)
                   ? "Unlocking db %u key %s\n"
                   : "Unlocking db %u key %.20s\n",
                   (int)crec->ctdb_ctx->db_id,
-                  hex_encode(data, (unsigned char *)data->key.dptr,
+                  hex_encode_talloc(data, (unsigned char *)data->key.dptr,
                              data->key.dsize)));
 
        if (tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key) != 0) {
@@ -841,14 +947,14 @@ static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
 again:
 
        if (DEBUGLEVEL >= 10) {
-               char *keystr = hex_encode(result, key.dptr, key.dsize);
+               char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
                DEBUG(10, (DEBUGLEVEL > 10
                           ? "Locking db %u key %s\n"
                           : "Locking db %u key %.20s\n",
                           (int)crec->ctdb_ctx->db_id, keystr));
                TALLOC_FREE(keystr);
        }
-       
+
        if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
                DEBUG(3, ("tdb_chainlock failed\n"));
                TALLOC_FREE(result);
@@ -1128,6 +1234,13 @@ static int db_ctdb_get_seqnum(struct db_context *db)
        return tdb_get_seqnum(ctx->wtdb->tdb);
 }
 
+static int db_ctdb_get_flags(struct db_context *db)
+{
+        struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
+                                                        struct db_ctdb_ctx);
+       return tdb_get_flags(ctx->wtdb->tdb);
+}
+
 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
                                const char *name,
                                int hash_size, int tdb_flags,
@@ -1189,6 +1302,7 @@ struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
        result->traverse = db_ctdb_traverse;
        result->traverse_read = db_ctdb_traverse_read;
        result->get_seqnum = db_ctdb_get_seqnum;
+       result->get_flags = db_ctdb_get_flags;
        result->transaction_start = db_ctdb_transaction_start;
        result->transaction_commit = db_ctdb_transaction_commit;
        result->transaction_cancel = db_ctdb_transaction_cancel;