ReadOnly: Dont update the record header from the calling client. While it is convenie...
[sahlberg/ctdb.git] / server / ctdb_call.c
index 443e6d2d3e832f74a10bb9af2459eedd7b214436..9fc8b339012a06b9853b94ccd77cb000930cf938 100644 (file)
@@ -21,7 +21,7 @@
   protocol design and packet details
 */
 #include "includes.h"
-#include "lib/events/events.h"
+#include "lib/tevent/tevent.h"
 #include "lib/tdb/include/tdb.h"
 #include "lib/util/dlinklist.h"
 #include "system/network.h"
@@ -43,7 +43,6 @@
        return ctdb_db;
 }
 
-
 /*
   a varient of input packet that can be used in lock requeue
 */
@@ -70,7 +69,7 @@ static void ctdb_send_error(struct ctdb_context *ctdb,
        int msglen, len;
 
        if (ctdb->methods == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " Failed to send error. Transport is DOWN\n"));
+               DEBUG(DEBUG_INFO,(__location__ " Failed to send error. Transport is DOWN\n"));
                return;
        }
 
@@ -99,9 +98,30 @@ static void ctdb_send_error(struct ctdb_context *ctdb,
 }
 
 
-/*
-  send a redirect reply
-*/
+/**
+ * send a redirect reply
+ *
+ * The logic behind this function is this:
+ *
+ * A client wants to grab a record and sends a CTDB_REQ_CALL packet
+ * to its local ctdb (ctdb_request_call). If the node is not itself
+ * the record's DMASTER, it first redirects the packet to  the
+ * record's LMASTER. The LMASTER then redirects the call packet to
+ * the current DMASTER. But there is a race: The record may have
+ * been migrated off the DMASTER while the redirected packet is
+ * on the wire (or in the local queue). So in case the record has
+ * migrated off the new destinaton of the call packet, instead of
+ * going back to the LMASTER to get the new DMASTER, we try to
+ * reduce rountrips by fist chasing the record a couple of times
+ * before giving up the direct chase and finally going back to the
+ * LMASTER (again). Note that this works because auf this: When
+ * a record is migrated off a node, then the new DMASTER is stored
+ * in the record's copy on the former DMASTER.
+ *
+ * The maxiumum number of attempts for direct chase to make before
+ * going back to the LMASTER is configurable by the tunable
+ * "MaxRedirectCount".
+ */
 static void ctdb_call_send_redirect(struct ctdb_context *ctdb, 
                                    TDB_DATA key,
                                    struct ctdb_req_call *c, 
@@ -151,7 +171,7 @@ static void ctdb_send_dmaster_reply(struct ctdb_db_context *ctdb_db,
        }
 
        if (ctdb->methods == NULL) {
-               ctdb_fatal(ctdb, "ctdb_send_dmaster_reply cant update dmaster sicne transport is down");
+               ctdb_fatal(ctdb, "ctdb_send_dmaster_reply cant update dmaster since transport is down");
                return;
        }
 
@@ -160,7 +180,7 @@ static void ctdb_send_dmaster_reply(struct ctdb_db_context *ctdb_db,
        tmp_ctx = talloc_new(ctdb);
 
        /* send the CTDB_REPLY_DMASTER */
-       len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize;
+       len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize + sizeof(uint32_t);
        r = ctdb_transport_allocate(ctdb, tmp_ctx, CTDB_REPLY_DMASTER, len,
                                    struct ctdb_reply_dmaster);
        CTDB_NO_MEMORY_FATAL(ctdb, r);
@@ -173,6 +193,7 @@ static void ctdb_send_dmaster_reply(struct ctdb_db_context *ctdb_db,
        r->db_id         = ctdb_db->db_id;
        memcpy(&r->data[0], key.dptr, key.dsize);
        memcpy(&r->data[key.dsize], data.dptr, data.dsize);
+       memcpy(&r->data[key.dsize+data.dsize], &header->flags, sizeof(uint32_t));
 
        ctdb_queue_packet(ctdb, &r->hdr);
 
@@ -201,13 +222,18 @@ static void ctdb_call_send_dmaster(struct ctdb_db_context *ctdb_db,
                return;
        }
 
+       if (data->dsize != 0) {
+               header->flags |= CTDB_REC_FLAG_MIGRATED_WITH_DATA;
+       }
+
        if (lmaster == ctdb->pnn) {
                ctdb_send_dmaster_reply(ctdb_db, header, *key, *data, 
                                        c->hdr.srcnode, c->hdr.reqid);
                return;
        }
        
-       len = offsetof(struct ctdb_req_dmaster, data) + key->dsize + data->dsize;
+       len = offsetof(struct ctdb_req_dmaster, data) + key->dsize + data->dsize
+                       + sizeof(uint32_t);
        r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_DMASTER, len, 
                                    struct ctdb_req_dmaster);
        CTDB_NO_MEMORY_FATAL(ctdb, r);
@@ -220,6 +246,7 @@ static void ctdb_call_send_dmaster(struct ctdb_db_context *ctdb_db,
        r->datalen       = data->dsize;
        memcpy(&r->data[0], key->dptr, key->dsize);
        memcpy(&r->data[key->dsize], data->dptr, data->dsize);
+       memcpy(&r->data[key->dsize + data->dsize], &header->flags, sizeof(uint32_t));
 
        header->dmaster = c->hdr.srcnode;
        if (ctdb_ltdb_store(ctdb_db, *key, header, *data) != 0) {
@@ -231,78 +258,92 @@ static void ctdb_call_send_dmaster(struct ctdb_db_context *ctdb_db,
        talloc_free(r);
 }
 
-static void ctdb_hold_back_key(struct ctdb_db_context *db, TDB_DATA key)
-{
-       size_t num_keys;
-       uint8_t **tmp;
-
-       DEBUG(DEBUG_INFO, ("Holding back key %08x\n", ctdb_hash(&key)));
-
-       num_keys = talloc_array_length(db->holdback_keys);
-       tmp = talloc_realloc(db, db->holdback_keys, uint8_t *, num_keys+1);
-       if (tmp == NULL) {
-               DEBUG(DEBUG_ERR, ("talloc_realloc failed\n"));
-               return;
-       }
-       db->holdback_keys = tmp;
-
-       db->holdback_keys[num_keys] = (uint8_t *)talloc_memdup(
-               db->holdback_keys, key.dptr, key.dsize);
-       if (db->holdback_keys[num_keys] == NULL) {
-               DEBUG(DEBUG_ERR, ("talloc_memdup failed\n"));
-               db->holdback_keys = talloc_realloc(db, db->holdback_keys,
-                                                  uint8_t *, num_keys);
-       }
-}
-
 /*
   called when a CTDB_REPLY_DMASTER packet comes in, or when the lmaster
   gets a CTDB_REQUEST_DMASTER for itself. We become the dmaster.
 
   must be called with the chainlock held. This function releases the chainlock
 */
-static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db, 
+static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db,
                                struct ctdb_req_header *hdr,
                                TDB_DATA key, TDB_DATA data,
-                               uint64_t rsn)
+                               uint64_t rsn, uint32_t record_flags)
 {
        struct ctdb_call_state *state;
        struct ctdb_context *ctdb = ctdb_db->ctdb;
        struct ctdb_ltdb_header header;
+       int ret;
 
-       DEBUG(DEBUG_INFO,("pnn %u dmaster response %08x\n", ctdb->pnn, ctdb_hash(&key)));
+       DEBUG(DEBUG_DEBUG,("pnn %u dmaster response %08x\n", ctdb->pnn, ctdb_hash(&key)));
 
        ZERO_STRUCT(header);
        header.rsn = rsn + 1;
        header.dmaster = ctdb->pnn;
+       header.flags = record_flags;
+
+       state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
+
+       if (state) {
+               if (state->call->flags & CTDB_CALL_FLAG_VACUUM_MIGRATION) {
+                       /*
+                        * We temporarily add the VACUUM_MIGRATED flag to
+                        * the record flags, so that ctdb_ltdb_store can
+                        * decide whether the record should be stored or
+                        * deleted.
+                        */
+                       header.flags |= CTDB_REC_FLAG_VACUUM_MIGRATED;
+               }
+       }
 
        if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
                ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n");
-               ctdb_ltdb_unlock(ctdb_db, key);
+
+               ret = ctdb_ltdb_unlock(ctdb_db, key);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
+               }
                return;
        }
 
-       state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
 
        if (state == NULL) {
                DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_become_dmaster from node %u\n",
                         ctdb->pnn, hdr->reqid, hdr->srcnode));
-               ctdb_ltdb_unlock(ctdb_db, key);
+
+               ret = ctdb_ltdb_unlock(ctdb_db, key);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
+               }
+               return;
+       }
+
+       if (key.dsize != state->call->key.dsize || memcmp(key.dptr, state->call->key.dptr, key.dsize)) {
+               DEBUG(DEBUG_ERR, ("Got bogus DMASTER packet reqid:%u from node %u. Key does not match key held in matching idr.\n", hdr->reqid, hdr->srcnode));
+
+               ret = ctdb_ltdb_unlock(ctdb_db, key);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
+               }
                return;
        }
 
        if (hdr->reqid != state->reqid) {
                /* we found a record  but it was the wrong one */
                DEBUG(DEBUG_ERR, ("Dropped orphan in ctdb_become_dmaster with reqid:%u\n from node %u", hdr->reqid, hdr->srcnode));
-               ctdb_ltdb_unlock(ctdb_db, key);
+
+               ret = ctdb_ltdb_unlock(ctdb_db, key);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
+               }
                return;
        }
 
-       ctdb_hold_back_key(ctdb_db, key);
+       ctdb_call_local(ctdb_db, state->call, &header, state, &data, true);
 
-       ctdb_call_local(ctdb_db, state->call, &header, state, &data, ctdb->pnn);
-
-       ctdb_ltdb_unlock(ctdb_db, state->call->key);
+       ret = ctdb_ltdb_unlock(ctdb_db, state->call->key);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
+       }
 
        state->state = CTDB_CALL_DONE;
        if (state->async.fn) {
@@ -324,12 +365,19 @@ void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr
        TDB_DATA key, data, data2;
        struct ctdb_ltdb_header header;
        struct ctdb_db_context *ctdb_db;
+       uint32_t record_flags = 0;
+       size_t len;
        int ret;
 
        key.dptr = c->data;
        key.dsize = c->keylen;
        data.dptr = c->data + c->keylen;
        data.dsize = c->datalen;
+       len = offsetof(struct ctdb_req_dmaster, data) + key.dsize + data.dsize
+                       + sizeof(uint32_t);
+       if (len <= c->hdr.length) {
+               record_flags = *(uint32_t *)&c->data[c->keylen + c->datalen];
+       }
 
        ctdb_db = find_ctdb_db(ctdb, c->db_id);
        if (!ctdb_db) {
@@ -358,7 +406,7 @@ void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr
                ctdb_fatal(ctdb, "ctdb_req_dmaster to non-lmaster");
        }
 
-       DEBUG(DEBUG_INFO,("pnn %u dmaster request on %08x for %u from %u\n", 
+       DEBUG(DEBUG_DEBUG,("pnn %u dmaster request on %08x for %u from %u\n", 
                 ctdb->pnn, ctdb_hash(&key), c->dmaster, c->hdr.srcnode));
 
        /* its a protocol error if the sending node is not the current dmaster */
@@ -369,7 +417,10 @@ void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr
                         (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid,
                         (key.dsize >= 4)?(*(uint32_t *)key.dptr):0));
                if (header.rsn != 0 || header.dmaster != ctdb->pnn) {
-                       ctdb_fatal(ctdb, "ctdb_req_dmaster from non-master");
+                       DEBUG(DEBUG_ERR,("ctdb_req_dmaster from non-master. Force a recovery.\n"));
+
+                       ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
+                       ctdb_ltdb_unlock(ctdb_db, key);
                        return;
                }
        }
@@ -384,58 +435,24 @@ void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr
        /* use the rsn from the sending node */
        header.rsn = c->rsn;
 
+       /* store the record flags from the sending node */
+       header.flags = record_flags;
+
        /* check if the new dmaster is the lmaster, in which case we
           skip the dmaster reply */
        if (c->dmaster == ctdb->pnn) {
-               ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn);
+               ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn, record_flags);
        } else {
                ctdb_send_dmaster_reply(ctdb_db, &header, key, data, c->dmaster, hdr->reqid);
-               ctdb_ltdb_unlock(ctdb_db, key);
-       }
-}
-
-/*
- * Did we just pull the dmaster of the record for a client fetch_lock,
- * so should we hold it back a while and thus give our client the
- * chance to do its own tdb_lock?
- */
-
-static bool ctdb_held_back(struct ctdb_db_context *db, TDB_DATA key,
-                          struct ctdb_req_header *hdr)
-{
-       int i;
-       size_t num_keys = talloc_array_length(db->holdback_keys);
-       size_t num_hdrs;
-       struct ctdb_req_header **tmp;
 
-       for (i=0; i<num_keys; i++) {
-               uint8_t *hold_key = db->holdback_keys[i];
-               size_t keylength = talloc_array_length(hold_key);
-
-               if ((keylength == key.dsize)
-                   && (memcmp(hold_key, key.dptr, keylength) == 0)) {
-                       break;
+               ret = ctdb_ltdb_unlock(ctdb_db, key);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
                }
        }
-       if (i == num_keys) {
-               return false;
-       }
-       DEBUG(DEBUG_DEBUG, ("holding back record %08x after migration\n",
-                           ctdb_hash(&key)));
-
-       num_hdrs = talloc_array_length(db->held_back);
-
-       tmp = talloc_realloc(db, db->held_back, struct ctdb_req_header *,
-                            num_hdrs + 1);
-       if (tmp == NULL) {
-               DEBUG(DEBUG_ERR, (__location__ "talloc_realloc failed\n"));
-               return false;
-       }
-       db->held_back = tmp;
-       db->held_back[num_hdrs] = talloc_move(db->held_back, &hdr);
-       return true;
 }
 
+
 /*
   called when a CTDB_REQ_CALL packet comes in
 */
@@ -450,7 +467,7 @@ void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
        struct ctdb_db_context *ctdb_db;
 
        if (ctdb->methods == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " Failed ctdb_request_call. Transport is DOWN\n"));
+               DEBUG(DEBUG_INFO,(__location__ " Failed ctdb_request_call. Transport is DOWN\n"));
                return;
        }
 
@@ -471,6 +488,8 @@ void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
        call->key.dsize = c->keylen;
        call->call_data.dptr = c->data + c->keylen;
        call->call_data.dsize = c->calldatalen;
+       call->reply_data.dptr  = NULL;
+       call->reply_data.dsize = 0;
 
        /* determine if we are the dmaster for this key. This also
           fetches the record data (if any), thus avoiding a 2nd fetch of the data 
@@ -487,51 +506,159 @@ void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
                return;
        }
 
-       /* if we are not the dmaster, then send a redirect to the
-          requesting node */
-       if (header.dmaster != ctdb->pnn) {
+       /* Dont do READONLY if we dont have a tracking database */
+       if ((c->flags & CTDB_WANT_READONLY) && !ctdb_db->readonly) {
+               c->flags &= ~CTDB_WANT_READONLY;
+       }
+
+       if (header.flags & CTDB_REC_RO_REVOKE_COMPLETE) {
+               header.flags &= ~(CTDB_REC_RO_HAVE_DELEGATIONS|CTDB_REC_RO_HAVE_READONLY|CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_REVOKE_COMPLETE);
+               if (ctdb_ltdb_store(ctdb_db, call->key, &header, data) != 0) {
+                       ctdb_fatal(ctdb, "Failed to write header with cleared REVOKE flag");
+               }
+               /* and clear out the tracking data */
+               if (tdb_delete(ctdb_db->rottdb, call->key) != 0) {
+                       DEBUG(DEBUG_ERR,(__location__ " Failed to clear out trackingdb record\n"));
+               }
+       }
+
+       /* if we are revoking, we must defer all other calls until the revoke
+        * had completed.
+        */
+       if (header.flags & CTDB_REC_RO_REVOKING_READONLY) {
                talloc_free(data.dptr);
-               ctdb_call_send_redirect(ctdb, call->key, c, &header);
-               ctdb_ltdb_unlock(ctdb_db, call->key);
+               ret = ctdb_ltdb_unlock(ctdb_db, call->key);
+
+               if (ctdb_add_revoke_deferred_call(ctdb, ctdb_db, call->key, hdr, ctdb_call_input_pkt, ctdb) != 0) {
+                       ctdb_fatal(ctdb, "Failed to add deferred call for revoke child");
+               }
+               talloc_free(call);
                return;
        }
 
-       if (c->hopcount > ctdb->statistics.max_hop_count) {
-               ctdb->statistics.max_hop_count = c->hopcount;
+       /* if we are not the dmaster and are not hosting any delegations,
+          then send a redirect to the requesting node */
+       if ((header.dmaster != ctdb->pnn) 
+           && (!(header.flags & CTDB_REC_RO_HAVE_DELEGATIONS)) ) {
+               talloc_free(data.dptr);
+               ctdb_call_send_redirect(ctdb, call->key, c, &header);
+
+               ret = ctdb_ltdb_unlock(ctdb_db, call->key);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
+               }
+               return;
        }
 
-       if ((c->flags & CTDB_IMMEDIATE_MIGRATION)
-           && (ctdb_held_back(ctdb_db, call->key, hdr))) {
+       if ( (!(c->flags & CTDB_WANT_READONLY))
+       && (header.flags & (CTDB_REC_RO_HAVE_DELEGATIONS|CTDB_REC_RO_HAVE_READONLY)) ) {
+               header.flags   |= CTDB_REC_RO_REVOKING_READONLY;
+               if (ctdb_ltdb_store(ctdb_db, call->key, &header, data) != 0) {
+                       ctdb_fatal(ctdb, "Failed to store record with HAVE_DELEGATIONS set");
+               }
+               ret = ctdb_ltdb_unlock(ctdb_db, call->key);
+
+               if (ctdb_start_revoke_ro_record(ctdb, ctdb_db, call->key, &header, data) != 0) {
+                       ctdb_fatal(ctdb, "Failed to start record revoke");
+               }
                talloc_free(data.dptr);
-               ctdb_ltdb_unlock(ctdb_db, call->key);
+
+               if (ctdb_add_revoke_deferred_call(ctdb, ctdb_db, call->key, hdr, ctdb_call_input_pkt, ctdb) != 0) {
+                       ctdb_fatal(ctdb, "Failed to add deferred call for revoke child");
+               }
+               talloc_free(call);
+
+               return;
+       }               
+
+       /* If this is the first request for delegation. bump rsn and set
+        * the delegations flag
+        */
+       if ((c->flags & CTDB_WANT_READONLY)
+       &&  (c->callid == CTDB_FETCH_WITH_HEADER_FUNC)
+       &&  (!(header.flags & CTDB_REC_RO_HAVE_DELEGATIONS))) {
+               header.rsn     += 3;
+               header.flags   |= CTDB_REC_RO_HAVE_DELEGATIONS;
+               if (ctdb_ltdb_store(ctdb_db, call->key, &header, data) != 0) {
+                       ctdb_fatal(ctdb, "Failed to store record with HAVE_DELEGATIONS set");
+               }
+       }
+       if ((c->flags & CTDB_WANT_READONLY) 
+       &&  (call->call_id == CTDB_FETCH_WITH_HEADER_FUNC)) {
+               TDB_DATA tdata;
+
+               tdata = tdb_fetch(ctdb_db->rottdb, call->key);
+               if (ctdb_trackingdb_add_pnn(ctdb, &tdata, c->hdr.srcnode) != 0) {
+                       ctdb_fatal(ctdb, "Failed to add node to trackingdb");
+               }
+               if (tdb_store(ctdb_db->rottdb, call->key, tdata, TDB_REPLACE) != 0) {
+                       ctdb_fatal(ctdb, "Failed to store trackingdb data");
+               }
+               free(tdata.dptr);
+
+               ret = ctdb_ltdb_unlock(ctdb_db, call->key);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
+               }
+
+               len = offsetof(struct ctdb_reply_call, data) + data.dsize + sizeof(struct ctdb_ltdb_header);
+               r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len, 
+                                           struct ctdb_reply_call);
+               CTDB_NO_MEMORY_FATAL(ctdb, r);
+               r->hdr.destnode  = c->hdr.srcnode;
+               r->hdr.reqid     = c->hdr.reqid;
+               r->status        = 0;
+               r->datalen       = data.dsize + sizeof(struct ctdb_ltdb_header);
+               header.rsn      -= 2;
+               header.flags   |= CTDB_REC_RO_HAVE_READONLY;
+               header.flags   &= ~CTDB_REC_RO_HAVE_DELEGATIONS;
+               memcpy(&r->data[0], &header, sizeof(struct ctdb_ltdb_header));
+
+               if (data.dsize) {
+                       memcpy(&r->data[sizeof(struct ctdb_ltdb_header)], data.dptr, data.dsize);
+               }
+
+               ctdb_queue_packet(ctdb, &r->hdr);
+
+               talloc_free(r);
                return;
        }
 
-       /* if this nodes has done enough consecutive calls on the same record
-          then give them the record
-          or if the node requested an immediate migration
-       */
-       if ( c->hdr.srcnode != ctdb->pnn &&
-            ((header.laccessor == c->hdr.srcnode
-              && header.lacount >= ctdb->tunable.max_lacount)
-             || (c->flags & CTDB_IMMEDIATE_MIGRATION)) ) {
+       CTDB_UPDATE_STAT(ctdb, max_hop_count, c->hopcount);
+
+       /* Try if possible to migrate the record off to the caller node.
+        * From the clients perspective a fetch of the data is just as 
+        * expensive as a migration.
+        */
+       if (c->hdr.srcnode != ctdb->pnn) {
                if (ctdb_db->transaction_active) {
                        DEBUG(DEBUG_INFO, (__location__ " refusing migration"
                              " of key %s while transaction is active\n",
                              (char *)call->key.dptr));
                } else {
-                       DEBUG(DEBUG_INFO,("pnn %u starting migration of %08x to %u\n",
+                       DEBUG(DEBUG_DEBUG,("pnn %u starting migration of %08x to %u\n",
                                 ctdb->pnn, ctdb_hash(&(call->key)), c->hdr.srcnode));
                        ctdb_call_send_dmaster(ctdb_db, c, &header, &(call->key), &data);
                        talloc_free(data.dptr);
-                       ctdb_ltdb_unlock(ctdb_db, call->key);
+
+                       ret = ctdb_ltdb_unlock(ctdb_db, call->key);
+                       if (ret != 0) {
+                               DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
+                       }
                        return;
                }
        }
 
-       ctdb_call_local(ctdb_db, call, &header, hdr, &data, c->hdr.srcnode);
+       ret = ctdb_call_local(ctdb_db, call, &header, hdr, &data, true);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " ctdb_call_local failed\n"));
+               call->status = -1;
+       }
 
-       ctdb_ltdb_unlock(ctdb_db, call->key);
+       ret = ctdb_ltdb_unlock(ctdb_db, call->key);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
+       }
 
        len = offsetof(struct ctdb_reply_call, data) + call->reply_data.dsize;
        r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len, 
@@ -573,6 +700,60 @@ void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
                return;
        }
 
+
+       /* read only delegation processing */
+       /* If we got a FETCH_WITH_HEADER we should check if this is a ro
+        * delegation since we may need to update the record header
+        */
+       if (state->c->callid == CTDB_FETCH_WITH_HEADER_FUNC) {
+               struct ctdb_db_context *ctdb_db = state->ctdb_db;
+               struct ctdb_ltdb_header *header = (struct ctdb_ltdb_header *)&c->data[0];
+               struct ctdb_ltdb_header oldheader;
+               TDB_DATA key, data, olddata;
+               int ret;
+
+               if (!(header->flags & CTDB_REC_RO_HAVE_READONLY)) {
+                       goto finished_ro;
+                       return;
+               }
+
+               key.dsize = state->c->keylen;
+               key.dptr  = state->c->data;
+               ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
+                                    ctdb_call_input_pkt, ctdb, False);
+               if (ret == -2) {
+                       return;
+               }
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR,(__location__ " Failed to get lock in ctdb_reply_call\n"));
+                       return;
+               }
+
+               ret = ctdb_ltdb_fetch(ctdb_db, key, &oldheader, state, &olddata);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR, ("Failed to fetch old record in ctdb_reply_call\n"));
+                       ctdb_ltdb_unlock(ctdb_db, key);
+                       goto finished_ro;
+               }                       
+
+               if (header->rsn <= oldheader.rsn) {
+                       ctdb_ltdb_unlock(ctdb_db, key);
+                       goto finished_ro;
+               }
+
+               data.dptr  = &c->data[sizeof(struct ctdb_ltdb_header)];
+               data.dsize = sizeof(struct ctdb_ltdb_header);
+               ret = ctdb_ltdb_store(ctdb_db, key, header, data);
+               if (ret != 0) {
+                       DEBUG(DEBUG_ERR, ("Failed to store new record in ctdb_reply_call\n"));
+                       ctdb_ltdb_unlock(ctdb_db, key);
+                       goto finished_ro;
+               }                       
+
+               ctdb_ltdb_unlock(ctdb_db, key);
+       }
+finished_ro:
+
        state->call->reply_data.dptr = c->data;
        state->call->reply_data.dsize = c->datalen;
        state->call->status = c->status;
@@ -598,6 +779,8 @@ void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
        struct ctdb_reply_dmaster *c = (struct ctdb_reply_dmaster *)hdr;
        struct ctdb_db_context *ctdb_db;
        TDB_DATA key, data;
+       uint32_t record_flags = 0;
+       size_t len;
        int ret;
 
        ctdb_db = find_ctdb_db(ctdb, c->db_id);
@@ -610,6 +793,11 @@ void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
        key.dsize = c->keylen;
        data.dptr = &c->data[key.dsize];
        data.dsize = c->datalen;
+       len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize
+               + sizeof(uint32_t);
+       if (len <= c->hdr.length) {
+               record_flags = *(uint32_t *)&c->data[c->keylen + c->datalen];
+       }
 
        ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
                                     ctdb_call_input_pkt, ctdb, False);
@@ -621,7 +809,7 @@ void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
                return;
        }
 
-       ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn);
+       ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn, record_flags);
 }
 
 
@@ -742,7 +930,7 @@ struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db,
        *(state->call) = *call;
        state->ctdb_db = ctdb_db;
 
-       ret = ctdb_call_local(ctdb_db, state->call, header, state, data, ctdb->pnn);
+       ret = ctdb_call_local(ctdb_db, state->call, header, state, data, true);
 
        event_add_timed(ctdb->ev, state, timeval_zero(), call_local_trigger, state);
 
@@ -765,7 +953,7 @@ struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctd
        struct ctdb_context *ctdb = ctdb_db->ctdb;
 
        if (ctdb->methods == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " Failed send packet. Transport is down\n"));
+               DEBUG(DEBUG_INFO,(__location__ " Failed send packet. Transport is down\n"));
                return NULL;
        }
 
@@ -849,7 +1037,7 @@ void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode)
        struct ctdb_req_keepalive *r;
        
        if (ctdb->methods == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " Failed to send keepalive. Transport is DOWN\n"));
+               DEBUG(DEBUG_INFO,(__location__ " Failed to send keepalive. Transport is DOWN\n"));
                return;
        }
 
@@ -860,118 +1048,368 @@ void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode)
        r->hdr.destnode  = destnode;
        r->hdr.reqid     = 0;
        
-       ctdb->statistics.keepalive_packets_sent++;
+       CTDB_INCREMENT_STAT(ctdb, keepalive_packets_sent);
 
        ctdb_queue_packet(ctdb, &r->hdr);
 
        talloc_free(r);
 }
 
-static void ctdb_holdback_cleanup(struct event_context *ev,
-                                 struct timed_event *te,
-                                 struct timeval t, void *private_data)
+
+
+struct revokechild_deferred_call {
+       struct ctdb_context *ctdb;
+       struct ctdb_req_header *hdr;
+       deferred_requeue_fn fn;
+       void *ctx;
+};
+
+struct revokechild_handle {
+       struct revokechild_handle *next, *prev;
+       struct ctdb_context *ctdb;
+       struct ctdb_db_context *ctdb_db;
+       struct fd_event *fde;
+       int status;
+       int fd[2];
+       pid_t child;
+       TDB_DATA key;
+};
+
+struct revokechild_requeue_handle {
+       struct ctdb_context *ctdb;
+       struct ctdb_req_header *hdr;
+       deferred_requeue_fn fn;
+       void *ctx;
+};
+
+static void deferred_call_requeue(struct event_context *ev, struct timed_event *te, 
+                      struct timeval t, void *private_data)
+{
+       struct revokechild_requeue_handle *requeue_handle = talloc_get_type(private_data, struct revokechild_requeue_handle);
+
+       requeue_handle->fn(requeue_handle->ctx, requeue_handle->hdr);
+       talloc_free(requeue_handle);
+}
+
+static int deferred_call_destructor(struct revokechild_deferred_call *deferred_call)
 {
-       struct ctdb_context *ctdb = talloc_get_type(private_data,
-                                                   struct ctdb_context);
-        struct ctdb_db_context *ctdb_db;
+       struct ctdb_context *ctdb = deferred_call->ctdb;
+       struct revokechild_requeue_handle *requeue_handle = talloc(ctdb, struct revokechild_requeue_handle);
+       struct ctdb_req_call *c = (struct ctdb_req_call *)deferred_call->hdr;
+
+       requeue_handle->ctdb = ctdb;
+       requeue_handle->hdr  = deferred_call->hdr;
+       requeue_handle->fn   = deferred_call->fn;
+       requeue_handle->ctx  = deferred_call->ctx;
+       talloc_steal(requeue_handle, requeue_handle->hdr);
 
-       DEBUG(DEBUG_INFO, ("running ctdb_holdback_cleanup\n"));
+       /* when revoking, any READONLY requests have 1 second grace to let read/write finish first */
+       event_add_timed(ctdb->ev, requeue_handle, timeval_current_ofs(c->flags & CTDB_WANT_READONLY ? 1 : 0, 0), deferred_call_requeue, requeue_handle);
 
-       if (te != ctdb->holdback_cleanup_te) {
-               ctdb_fatal(ctdb, "te != ctdb->holdback_cleanup_te");
+       return 0;
+}
+
+
+static int revokechild_destructor(struct revokechild_handle *rc)
+{
+       if (rc->fde != NULL) {
+               talloc_free(rc->fde);
+       }
+
+       if (rc->fd[0] != -1) {
+               close(rc->fd[0]);
        }
+       if (rc->fd[1] != -1) {
+               close(rc->fd[1]);
+       }
+       kill(rc->child, SIGKILL);
 
-        for (ctdb_db=ctdb->db_list; ctdb_db; ctdb_db=ctdb_db->next) {
-               size_t i, num_heldback;
+       DLIST_REMOVE(rc->ctdb_db->revokechild_active, rc);
+       return 0;
+}
 
-               talloc_free(ctdb_db->holdback_keys);
-               ctdb_db->holdback_keys = NULL;
+static void revokechild_handler(struct event_context *ev, struct fd_event *fde, 
+                            uint16_t flags, void *private_data)
+{
+       struct revokechild_handle *rc = talloc_get_type(private_data, 
+                                                    struct revokechild_handle);
+       int ret;
+       char c;
 
-               num_heldback = talloc_array_length(ctdb_db->held_back);
-               for (i=0; i<num_heldback; i++) {
-                       ctdb_queue_packet(ctdb, ctdb_db->held_back[i]);
-               }
-               talloc_free(ctdb_db->held_back);
-               ctdb_db->held_back = NULL;
-        }
+       ret = read(rc->fd[0], &c, 1);
+       if (ret != 1) {
+               DEBUG(DEBUG_ERR,("Failed to read status from revokechild. errno:%d\n", errno));
+               rc->status = -1;
+               talloc_free(rc);
+               return;
+       }
+       if (c != 0) {
+               DEBUG(DEBUG_ERR,("revokechild returned failure. status:%d\n", c));
+               rc->status = -1;
+               talloc_free(rc);
+               return;
+       }
+
+       talloc_free(rc);
+}
+
+struct ctdb_revoke_state {
+       struct ctdb_db_context *ctdb_db;
+       TDB_DATA key;
+       struct ctdb_ltdb_header *header;
+       TDB_DATA data;
+       int count;
+       int status;
+       int finished;
+};
+
+static void update_record_cb(struct ctdb_client_control_state *state)
+{
+       struct ctdb_revoke_state *revoke_state;
+       int ret;
+       int32_t res;
+
+       if (state == NULL) {
+               return;
+       }
+       revoke_state = state->async.private_data;
+
+       state->async.fn = NULL;
+        ret = ctdb_control_recv(state->ctdb, state, state, NULL, &res, NULL);
+        if ((ret != 0) || (res != 0)) {
+               DEBUG(DEBUG_ERR,("Recv for revoke update record failed ret:%d res:%d\n", ret, res));
+               revoke_state->status = -1;
+       }
 
-       ctdb->holdback_cleanup_te = event_add_timed(
-               ctdb->ev, ctdb, timeval_current_ofs(
-                       0, ctdb->tunable.holdback_cleanup_interval * 1000),
-               ctdb_holdback_cleanup, ctdb);
+       revoke_state->count--;
+       if (revoke_state->count <= 0) {
+               revoke_state->finished = 1;
+       }
 }
 
-void ctdb_start_holdback_cleanup(struct ctdb_context *ctdb)
+static void revoke_send_cb(struct ctdb_context *ctdb, uint32_t pnn, void *private_data)
 {
-       ctdb->holdback_cleanup_te = event_add_timed(
-               ctdb->ev, ctdb, timeval_current_ofs(
-                       0, ctdb->tunable.holdback_cleanup_interval * 1000),
-               ctdb_holdback_cleanup, ctdb);
+       struct ctdb_revoke_state *revoke_state = private_data;
+       struct ctdb_client_control_state *state;
+
+       state = ctdb_ctrl_updaterecord_send(ctdb, revoke_state, timeval_current_ofs(5,0), pnn, revoke_state->ctdb_db, revoke_state->key, revoke_state->header, revoke_state->data);
+       if (state == NULL) {
+               DEBUG(DEBUG_ERR,("Failure to send update record to revoke readonly delegation\n"));
+               revoke_state->status = -1;
+               return;
+       }
+       state->async.fn           = update_record_cb;
+       state->async.private_data = revoke_state;
 
-       CTDB_NO_MEMORY_FATAL(ctdb, ctdb->holdback_cleanup_te);
+       revoke_state->count++;
 
-       DEBUG(DEBUG_NOTICE,("Holdback cleanup has been started\n"));
 }
 
-void ctdb_stop_holdback_cleanup(struct ctdb_context *ctdb)
+static void ctdb_revoke_timeout_handler(struct event_context *ev, struct timed_event *te, 
+                             struct timeval yt, void *private_data)
 {
-       talloc_free(ctdb->holdback_cleanup_te);
-       ctdb->holdback_cleanup_te = NULL;
+       struct ctdb_revoke_state *state = private_data;
+
+       DEBUG(DEBUG_ERR,("Timed out waiting for revoke to finish\n"));
+       state->finished = 1;
+       state->status   = -1;
 }
 
-int32_t ctdb_control_gotit(struct ctdb_context *ctdb, TDB_DATA indata)
+static int ctdb_revoke_all_delegations(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA tdata, TDB_DATA key, struct ctdb_ltdb_header *header, TDB_DATA data)
 {
-       struct ctdb_control_gotit *c =
-               (struct ctdb_control_gotit *)indata.dptr;
-       struct ctdb_db_context *db;
-       size_t i, num_keys, num_heldback;
-       TDB_DATA key;
+       struct ctdb_revoke_state *state = talloc_zero(ctdb, struct ctdb_revoke_state);
+       int status;
+
+       state->ctdb_db = ctdb_db;
+       state->key     = key;
+       state->header  = header;
+       state->data    = data;
+       ctdb_trackingdb_traverse(ctdb, tdata, revoke_send_cb, state);
+
+       event_add_timed(ctdb->ev, state, timeval_current_ofs(5, 0), ctdb_revoke_timeout_handler, state);
 
-       if (indata.dsize < sizeof(struct ctdb_control_gotit)) {
-               DEBUG(DEBUG_ERR, (__location__ "Invalid data size %d\n",
-                                 (int)indata.dsize));
+       while (state->finished == 0) {
+               event_loop_once(ctdb->ev);
+       }
+
+       status = state->status;
+
+       if (status == 0) {
+               struct ctdb_ltdb_header new_header;
+               TDB_DATA new_data;
+
+               if (ctdb_ltdb_lock(ctdb_db, key) != 0) {
+                       DEBUG(DEBUG_ERR,("Failed to chainlock the database in revokechild\n"));
+                       talloc_free(state);
+                       return -1;
+               }
+               if (ctdb_ltdb_fetch(ctdb_db, key, &new_header, state, &new_data) != 0) {
+                       ctdb_ltdb_unlock(ctdb_db, key);
+                       DEBUG(DEBUG_ERR,("Failed for fetch tdb record in revokechild\n"));
+                       talloc_free(state);
+                       return -1;
+               }
+               header->rsn++;
+               if (new_header.rsn > header->rsn) {
+                       ctdb_ltdb_unlock(ctdb_db, key);
+                       DEBUG(DEBUG_ERR,("RSN too high in tdb record in revokechild\n"));
+                       talloc_free(state);
+                       return -1;
+               }
+               if ( (new_header.flags & (CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_HAVE_DELEGATIONS)) != (CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_HAVE_DELEGATIONS) ) {
+                       ctdb_ltdb_unlock(ctdb_db, key);
+                       DEBUG(DEBUG_ERR,("Flags are wrong in tdb record in revokechild\n"));
+                       talloc_free(state);
+                       return -1;
+               }
+               new_header.rsn++;
+               new_header.flags |= CTDB_REC_RO_REVOKE_COMPLETE;
+               if (ctdb_ltdb_store(ctdb_db, key, &new_header, new_data) != 0) {
+                       ctdb_ltdb_unlock(ctdb_db, key);
+                       DEBUG(DEBUG_ERR,("Failed to write new record in revokechild\n"));
+                       talloc_free(state);
+                       return -1;
+               }
+               ctdb_ltdb_unlock(ctdb_db, key);
+       }
+
+       talloc_free(state);
+       return status;
+}
+
+
+int ctdb_start_revoke_ro_record(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key, struct ctdb_ltdb_header *header, TDB_DATA data)
+{
+       TDB_DATA tdata;
+       struct revokechild_handle *rc;
+       pid_t parent = getpid();
+       int ret;
+
+       header->flags &= ~(CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_HAVE_DELEGATIONS|CTDB_REC_RO_HAVE_READONLY);
+       header->rsn   -= 1;
+
+       if ((rc = talloc_zero(ctdb_db, struct revokechild_handle)) == NULL) {
+               DEBUG(DEBUG_ERR,("Failed to allocate revokechild_handle\n"));
+               return -1;
+       }
+
+       tdata = tdb_fetch(ctdb_db->rottdb, key);
+       if (tdata.dsize > 0) {
+               uint8_t *tmp;
+
+               tmp = tdata.dptr;
+               tdata.dptr = talloc_memdup(rc, tdata.dptr, tdata.dsize);
+               free(tmp);
+       }
+
+       rc->status    = 0;
+       rc->ctdb      = ctdb;
+       rc->ctdb_db   = ctdb_db;
+       rc->fd[0]     = -1;
+       rc->fd[1]     = -1;
+
+       talloc_set_destructor(rc, revokechild_destructor);
+
+       rc->key.dsize = key.dsize;
+       rc->key.dptr  = talloc_memdup(rc, key.dptr, key.dsize);
+       if (rc->key.dptr == NULL) {
+               DEBUG(DEBUG_ERR,("Failed to allocate key for revokechild_handle\n"));
+               talloc_free(rc);
                return -1;
        }
-       db = find_ctdb_db(ctdb, c->db_id);
-       if (db == NULL) {
-               DEBUG(DEBUG_ERR, ("Unknown db_id 0x%x in ctdb_reply_dmaster\n",
-                                 (int)c->db_id));
+
+       ret = pipe(rc->fd);
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR,("Failed to allocate key for revokechild_handle\n"));
+               talloc_free(rc);
                return -1;
        }
 
-       key.dptr = c->key;
-       key.dsize = indata.dsize - offsetof(struct ctdb_control_gotit, key);
 
-       num_keys = talloc_array_length(db->holdback_keys);
-       for (i=0; i<num_keys; i++) {
-               uint8_t *hold_key = db->holdback_keys[i];
-               size_t keylength = talloc_array_length(hold_key);
+       rc->child = ctdb_fork(ctdb);
+       if (rc->child == (pid_t)-1) {
+               DEBUG(DEBUG_ERR,("Failed to fork child for revokechild\n"));
+               talloc_free(rc);
+               return -1;
+       }
 
-               if ((keylength == key.dsize)
-                   && (memcmp(hold_key, key.dptr, keylength) == 0)) {
-                       break;
+       if (rc->child == 0) {
+               char c = 0;
+               close(rc->fd[0]);
+               debug_extra = talloc_asprintf(NULL, "revokechild-%s:", ctdb_db->db_name);
+
+               if (switch_from_server_to_client(ctdb, "revokechild-%s", ctdb_db->db_name) != 0) {
+                       DEBUG(DEBUG_ERR,("Failed to switch from server to client for revokechild process\n"));
+                       c = 1;
+                       goto child_finished;
+               }
+
+               c = ctdb_revoke_all_delegations(ctdb, ctdb_db, tdata, key, header, data);
+
+child_finished:
+               write(rc->fd[1], &c, 1);
+               /* make sure we die when our parent dies */
+               while (kill(parent, 0) == 0 || errno != ESRCH) {
+                       sleep(5);
                }
+               _exit(0);
        }
-       if (i == num_keys) {
-               /*
-                * ctdb_holdback_cleanup has kicked in. This is okay,
-                * we will just potentially have to retry.
-                */
-               return 0;
+
+       close(rc->fd[1]);
+       rc->fd[1] = -1;
+       set_close_on_exec(rc->fd[0]);
+
+       /* This is an active revokechild child process */
+       DLIST_ADD_END(ctdb_db->revokechild_active, rc, NULL);
+
+       rc->fde = event_add_fd(ctdb->ev, rc, rc->fd[0],
+                                  EVENT_FD_READ, revokechild_handler,
+                                  (void *)rc);
+       if (rc->fde == NULL) {
+               DEBUG(DEBUG_ERR,("Failed to set up fd event for revokechild process\n"));
+               talloc_free(rc);
+       }
+       tevent_fd_set_auto_close(rc->fde);
+
+       return 0;
+}
+
+int ctdb_add_revoke_deferred_call(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key, struct ctdb_req_header *hdr, deferred_requeue_fn fn, void *call_context)
+{
+       struct revokechild_handle *rc;
+       struct revokechild_deferred_call *deferred_call;
+
+       for (rc = ctdb_db->revokechild_active; rc; rc = rc->next) {
+               if (rc->key.dsize == 0) {
+                       continue;
+               }
+               if (rc->key.dsize != key.dsize) {
+                       continue;
+               }
+               if (!memcmp(rc->key.dptr, key.dptr, key.dsize)) {
+                       break;
+               }
        }
 
-       talloc_free(db->holdback_keys[i]);
-       if (i < num_keys-1) {
-               db->holdback_keys[i] = db->holdback_keys[num_keys-1];
+       if (rc == NULL) {
+               DEBUG(DEBUG_ERR,("Failed to add deferred call to revoke list. revoke structure not found\n"));
+               return -1;
        }
-       db->holdback_keys = talloc_realloc(db, db->holdback_keys, uint8_t *,
-                                          num_keys-1);
 
-       num_heldback = talloc_array_length(db->held_back);
-       for (i=0; i<num_heldback; i++) {
-               ctdb_queue_packet(ctdb, db->held_back[i]);
+       deferred_call = talloc(rc, struct revokechild_deferred_call);
+       if (deferred_call == NULL) {
+               DEBUG(DEBUG_ERR,("Failed to allocate deferred call structure for revoking record\n"));
+               return -1;
        }
-       talloc_free(db->held_back);
-       db->held_back = NULL;
+
+       deferred_call->ctdb = ctdb;
+       deferred_call->hdr  = hdr;
+       deferred_call->fn   = fn;
+       deferred_call->ctx  = call_context;
+
+       talloc_set_destructor(deferred_call, deferred_call_destructor);
+       talloc_steal(deferred_call, hdr);
+
        return 0;
 }