ReadOnly: When releasing all deferred calls that blocked during revoke of all previou...
[sahlberg/ctdb.git] / server / ctdb_call.c
1 /* 
2    ctdb_call protocol code
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20   see http://wiki.samba.org/index.php/Samba_%26_Clustering for
21   protocol design and packet details
22 */
23 #include "includes.h"
24 #include "lib/tevent/tevent.h"
25 #include "lib/tdb/include/tdb.h"
26 #include "lib/util/dlinklist.h"
27 #include "system/network.h"
28 #include "system/filesys.h"
29 #include "../include/ctdb_private.h"
30
31 /*
32   find the ctdb_db from a db index
33  */
34  struct ctdb_db_context *find_ctdb_db(struct ctdb_context *ctdb, uint32_t id)
35 {
36         struct ctdb_db_context *ctdb_db;
37
38         for (ctdb_db=ctdb->db_list; ctdb_db; ctdb_db=ctdb_db->next) {
39                 if (ctdb_db->db_id == id) {
40                         break;
41                 }
42         }
43         return ctdb_db;
44 }
45
46
47 /*
48   a varient of input packet that can be used in lock requeue
49 */
50 static void ctdb_call_input_pkt(void *p, struct ctdb_req_header *hdr)
51 {
52         struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
53         ctdb_input_pkt(ctdb, hdr);
54 }
55
56
57 /*
58   send an error reply
59 */
60 static void ctdb_send_error(struct ctdb_context *ctdb, 
61                             struct ctdb_req_header *hdr, uint32_t status,
62                             const char *fmt, ...) PRINTF_ATTRIBUTE(4,5);
63 static void ctdb_send_error(struct ctdb_context *ctdb, 
64                             struct ctdb_req_header *hdr, uint32_t status,
65                             const char *fmt, ...)
66 {
67         va_list ap;
68         struct ctdb_reply_error *r;
69         char *msg;
70         int msglen, len;
71
72         if (ctdb->methods == NULL) {
73                 DEBUG(DEBUG_INFO,(__location__ " Failed to send error. Transport is DOWN\n"));
74                 return;
75         }
76
77         va_start(ap, fmt);
78         msg = talloc_vasprintf(ctdb, fmt, ap);
79         if (msg == NULL) {
80                 ctdb_fatal(ctdb, "Unable to allocate error in ctdb_send_error\n");
81         }
82         va_end(ap);
83
84         msglen = strlen(msg)+1;
85         len = offsetof(struct ctdb_reply_error, msg);
86         r = ctdb_transport_allocate(ctdb, msg, CTDB_REPLY_ERROR, len + msglen, 
87                                     struct ctdb_reply_error);
88         CTDB_NO_MEMORY_FATAL(ctdb, r);
89
90         r->hdr.destnode  = hdr->srcnode;
91         r->hdr.reqid     = hdr->reqid;
92         r->status        = status;
93         r->msglen        = msglen;
94         memcpy(&r->msg[0], msg, msglen);
95
96         ctdb_queue_packet(ctdb, &r->hdr);
97
98         talloc_free(msg);
99 }
100
101
102 /**
103  * send a redirect reply
104  *
105  * The logic behind this function is this:
106  *
107  * A client wants to grab a record and sends a CTDB_REQ_CALL packet
108  * to its local ctdb (ctdb_request_call). If the node is not itself
109  * the record's DMASTER, it first redirects the packet to  the
110  * record's LMASTER. The LMASTER then redirects the call packet to
111  * the current DMASTER. But there is a race: The record may have
112  * been migrated off the DMASTER while the redirected packet is
113  * on the wire (or in the local queue). So in case the record has
114  * migrated off the new destinaton of the call packet, instead of
115  * going back to the LMASTER to get the new DMASTER, we try to
116  * reduce rountrips by fist chasing the record a couple of times
117  * before giving up the direct chase and finally going back to the
118  * LMASTER (again). Note that this works because auf this: When
119  * a record is migrated off a node, then the new DMASTER is stored
120  * in the record's copy on the former DMASTER.
121  *
122  * The maxiumum number of attempts for direct chase to make before
123  * going back to the LMASTER is configurable by the tunable
124  * "MaxRedirectCount".
125  */
126 static void ctdb_call_send_redirect(struct ctdb_context *ctdb, 
127                                     TDB_DATA key,
128                                     struct ctdb_req_call *c, 
129                                     struct ctdb_ltdb_header *header)
130 {
131         
132         uint32_t lmaster = ctdb_lmaster(ctdb, &key);
133         if (ctdb->pnn == lmaster) {
134                 c->hdr.destnode = header->dmaster;
135         } else if ((c->hopcount % ctdb->tunable.max_redirect_count) == 0) {
136                 c->hdr.destnode = lmaster;
137         } else {
138                 c->hdr.destnode = header->dmaster;
139         }
140         c->hopcount++;
141         ctdb_queue_packet(ctdb, &c->hdr);
142 }
143
144
145 /*
146   send a dmaster reply
147
148   caller must have the chainlock before calling this routine. Caller must be
149   the lmaster
150 */
151 static void ctdb_send_dmaster_reply(struct ctdb_db_context *ctdb_db,
152                                     struct ctdb_ltdb_header *header,
153                                     TDB_DATA key, TDB_DATA data,
154                                     uint32_t new_dmaster,
155                                     uint32_t reqid)
156 {
157         struct ctdb_context *ctdb = ctdb_db->ctdb;
158         struct ctdb_reply_dmaster *r;
159         int ret, len;
160         TALLOC_CTX *tmp_ctx;
161
162         if (ctdb->pnn != ctdb_lmaster(ctdb, &key)) {
163                 DEBUG(DEBUG_ALERT,(__location__ " Caller is not lmaster!\n"));
164                 return;
165         }
166
167         header->dmaster = new_dmaster;
168         ret = ctdb_ltdb_store(ctdb_db, key, header, data);
169         if (ret != 0) {
170                 ctdb_fatal(ctdb, "ctdb_send_dmaster_reply unable to update dmaster");
171                 return;
172         }
173
174         if (ctdb->methods == NULL) {
175                 ctdb_fatal(ctdb, "ctdb_send_dmaster_reply cant update dmaster since transport is down");
176                 return;
177         }
178
179         /* put the packet on a temporary context, allowing us to safely free
180            it below even if ctdb_reply_dmaster() has freed it already */
181         tmp_ctx = talloc_new(ctdb);
182
183         /* send the CTDB_REPLY_DMASTER */
184         len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize + sizeof(uint32_t);
185         r = ctdb_transport_allocate(ctdb, tmp_ctx, CTDB_REPLY_DMASTER, len,
186                                     struct ctdb_reply_dmaster);
187         CTDB_NO_MEMORY_FATAL(ctdb, r);
188
189         r->hdr.destnode  = new_dmaster;
190         r->hdr.reqid     = reqid;
191         r->rsn           = header->rsn;
192         r->keylen        = key.dsize;
193         r->datalen       = data.dsize;
194         r->db_id         = ctdb_db->db_id;
195         memcpy(&r->data[0], key.dptr, key.dsize);
196         memcpy(&r->data[key.dsize], data.dptr, data.dsize);
197         memcpy(&r->data[key.dsize+data.dsize], &header->flags, sizeof(uint32_t));
198
199         ctdb_queue_packet(ctdb, &r->hdr);
200
201         talloc_free(tmp_ctx);
202 }
203
204 /*
205   send a dmaster request (give another node the dmaster for a record)
206
207   This is always sent to the lmaster, which ensures that the lmaster
208   always knows who the dmaster is. The lmaster will then send a
209   CTDB_REPLY_DMASTER to the new dmaster
210 */
211 static void ctdb_call_send_dmaster(struct ctdb_db_context *ctdb_db, 
212                                    struct ctdb_req_call *c, 
213                                    struct ctdb_ltdb_header *header,
214                                    TDB_DATA *key, TDB_DATA *data)
215 {
216         struct ctdb_req_dmaster *r;
217         struct ctdb_context *ctdb = ctdb_db->ctdb;
218         int len;
219         uint32_t lmaster = ctdb_lmaster(ctdb, key);
220
221         if (ctdb->methods == NULL) {
222                 ctdb_fatal(ctdb, "Failed ctdb_call_send_dmaster since transport is down");
223                 return;
224         }
225
226         if (data->dsize != 0) {
227                 header->flags |= CTDB_REC_FLAG_MIGRATED_WITH_DATA;
228         }
229
230         if (lmaster == ctdb->pnn) {
231                 ctdb_send_dmaster_reply(ctdb_db, header, *key, *data, 
232                                         c->hdr.srcnode, c->hdr.reqid);
233                 return;
234         }
235         
236         len = offsetof(struct ctdb_req_dmaster, data) + key->dsize + data->dsize
237                         + sizeof(uint32_t);
238         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_DMASTER, len, 
239                                     struct ctdb_req_dmaster);
240         CTDB_NO_MEMORY_FATAL(ctdb, r);
241         r->hdr.destnode  = lmaster;
242         r->hdr.reqid     = c->hdr.reqid;
243         r->db_id         = c->db_id;
244         r->rsn           = header->rsn;
245         r->dmaster       = c->hdr.srcnode;
246         r->keylen        = key->dsize;
247         r->datalen       = data->dsize;
248         memcpy(&r->data[0], key->dptr, key->dsize);
249         memcpy(&r->data[key->dsize], data->dptr, data->dsize);
250         memcpy(&r->data[key->dsize + data->dsize], &header->flags, sizeof(uint32_t));
251
252         header->dmaster = c->hdr.srcnode;
253         if (ctdb_ltdb_store(ctdb_db, *key, header, *data) != 0) {
254                 ctdb_fatal(ctdb, "Failed to store record in ctdb_call_send_dmaster");
255         }
256         
257         ctdb_queue_packet(ctdb, &r->hdr);
258
259         talloc_free(r);
260 }
261
262 /*
263   called when a CTDB_REPLY_DMASTER packet comes in, or when the lmaster
264   gets a CTDB_REQUEST_DMASTER for itself. We become the dmaster.
265
266   must be called with the chainlock held. This function releases the chainlock
267 */
268 static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db,
269                                 struct ctdb_req_header *hdr,
270                                 TDB_DATA key, TDB_DATA data,
271                                 uint64_t rsn, uint32_t record_flags)
272 {
273         struct ctdb_call_state *state;
274         struct ctdb_context *ctdb = ctdb_db->ctdb;
275         struct ctdb_ltdb_header header;
276         int ret;
277
278         DEBUG(DEBUG_DEBUG,("pnn %u dmaster response %08x\n", ctdb->pnn, ctdb_hash(&key)));
279
280         ZERO_STRUCT(header);
281         header.rsn = rsn + 1;
282         header.dmaster = ctdb->pnn;
283         header.flags = record_flags;
284
285         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
286
287         if (state) {
288                 if (state->call->flags & CTDB_CALL_FLAG_VACUUM_MIGRATION) {
289                         /*
290                          * We temporarily add the VACUUM_MIGRATED flag to
291                          * the record flags, so that ctdb_ltdb_store can
292                          * decide whether the record should be stored or
293                          * deleted.
294                          */
295                         header.flags |= CTDB_REC_FLAG_VACUUM_MIGRATED;
296                 }
297         }
298
299         if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
300                 ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n");
301
302                 ret = ctdb_ltdb_unlock(ctdb_db, key);
303                 if (ret != 0) {
304                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
305                 }
306                 return;
307         }
308
309
310         if (state == NULL) {
311                 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_become_dmaster from node %u\n",
312                          ctdb->pnn, hdr->reqid, hdr->srcnode));
313
314                 ret = ctdb_ltdb_unlock(ctdb_db, key);
315                 if (ret != 0) {
316                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
317                 }
318                 return;
319         }
320
321         if (key.dsize != state->call->key.dsize || memcmp(key.dptr, state->call->key.dptr, key.dsize)) {
322                 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));
323
324                 ret = ctdb_ltdb_unlock(ctdb_db, key);
325                 if (ret != 0) {
326                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
327                 }
328                 return;
329         }
330
331         if (hdr->reqid != state->reqid) {
332                 /* we found a record  but it was the wrong one */
333                 DEBUG(DEBUG_ERR, ("Dropped orphan in ctdb_become_dmaster with reqid:%u\n from node %u", hdr->reqid, hdr->srcnode));
334
335                 ret = ctdb_ltdb_unlock(ctdb_db, key);
336                 if (ret != 0) {
337                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
338                 }
339                 return;
340         }
341
342         ctdb_call_local(ctdb_db, state->call, &header, state, &data, true);
343
344         ret = ctdb_ltdb_unlock(ctdb_db, state->call->key);
345         if (ret != 0) {
346                 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
347         }
348
349         state->state = CTDB_CALL_DONE;
350         if (state->async.fn) {
351                 state->async.fn(state);
352         }
353 }
354
355
356
357 /*
358   called when a CTDB_REQ_DMASTER packet comes in
359
360   this comes into the lmaster for a record when the current dmaster
361   wants to give up the dmaster role and give it to someone else
362 */
363 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
364 {
365         struct ctdb_req_dmaster *c = (struct ctdb_req_dmaster *)hdr;
366         TDB_DATA key, data, data2;
367         struct ctdb_ltdb_header header;
368         struct ctdb_db_context *ctdb_db;
369         uint32_t record_flags = 0;
370         size_t len;
371         int ret;
372
373         key.dptr = c->data;
374         key.dsize = c->keylen;
375         data.dptr = c->data + c->keylen;
376         data.dsize = c->datalen;
377         len = offsetof(struct ctdb_req_dmaster, data) + key.dsize + data.dsize
378                         + sizeof(uint32_t);
379         if (len <= c->hdr.length) {
380                 record_flags = *(uint32_t *)&c->data[c->keylen + c->datalen];
381         }
382
383         ctdb_db = find_ctdb_db(ctdb, c->db_id);
384         if (!ctdb_db) {
385                 ctdb_send_error(ctdb, hdr, -1,
386                                 "Unknown database in request. db_id==0x%08x",
387                                 c->db_id);
388                 return;
389         }
390         
391         /* fetch the current record */
392         ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, hdr, &data2,
393                                            ctdb_call_input_pkt, ctdb, False);
394         if (ret == -1) {
395                 ctdb_fatal(ctdb, "ctdb_req_dmaster failed to fetch record");
396                 return;
397         }
398         if (ret == -2) {
399                 DEBUG(DEBUG_INFO,(__location__ " deferring ctdb_request_dmaster\n"));
400                 return;
401         }
402
403         if (ctdb_lmaster(ctdb, &key) != ctdb->pnn) {
404                 DEBUG(DEBUG_ALERT,("pnn %u dmaster request to non-lmaster lmaster=%u gen=%u curgen=%u\n",
405                          ctdb->pnn, ctdb_lmaster(ctdb, &key), 
406                          hdr->generation, ctdb->vnn_map->generation));
407                 ctdb_fatal(ctdb, "ctdb_req_dmaster to non-lmaster");
408         }
409
410         DEBUG(DEBUG_DEBUG,("pnn %u dmaster request on %08x for %u from %u\n", 
411                  ctdb->pnn, ctdb_hash(&key), c->dmaster, c->hdr.srcnode));
412
413         /* its a protocol error if the sending node is not the current dmaster */
414         if (header.dmaster != hdr->srcnode) {
415                 DEBUG(DEBUG_ALERT,("pnn %u dmaster request for new-dmaster %u from non-master %u real-dmaster=%u key %08x dbid 0x%08x gen=%u curgen=%u c->rsn=%llu header.rsn=%llu reqid=%u keyval=0x%08x\n",
416                          ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
417                          ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
418                          (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid,
419                          (key.dsize >= 4)?(*(uint32_t *)key.dptr):0));
420                 if (header.rsn != 0 || header.dmaster != ctdb->pnn) {
421                         DEBUG(DEBUG_ERR,("ctdb_req_dmaster from non-master. Force a recovery.\n"));
422
423                         ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
424                         ctdb_ltdb_unlock(ctdb_db, key);
425                         return;
426                 }
427         }
428
429         if (header.rsn > c->rsn) {
430                 DEBUG(DEBUG_ALERT,("pnn %u dmaster request with older RSN new-dmaster %u from %u real-dmaster=%u key %08x dbid 0x%08x gen=%u curgen=%u c->rsn=%llu header.rsn=%llu reqid=%u\n",
431                          ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
432                          ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
433                          (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid));
434         }
435
436         /* use the rsn from the sending node */
437         header.rsn = c->rsn;
438
439         /* store the record flags from the sending node */
440         header.flags = record_flags;
441
442         /* check if the new dmaster is the lmaster, in which case we
443            skip the dmaster reply */
444         if (c->dmaster == ctdb->pnn) {
445                 ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn, record_flags);
446         } else {
447                 ctdb_send_dmaster_reply(ctdb_db, &header, key, data, c->dmaster, hdr->reqid);
448
449                 ret = ctdb_ltdb_unlock(ctdb_db, key);
450                 if (ret != 0) {
451                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
452                 }
453         }
454 }
455
456
457 /*
458   called when a CTDB_REQ_CALL packet comes in
459 */
460 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
461 {
462         struct ctdb_req_call *c = (struct ctdb_req_call *)hdr;
463         TDB_DATA data;
464         struct ctdb_reply_call *r;
465         int ret, len;
466         struct ctdb_ltdb_header header;
467         struct ctdb_call *call;
468         struct ctdb_db_context *ctdb_db;
469
470         if (ctdb->methods == NULL) {
471                 DEBUG(DEBUG_INFO,(__location__ " Failed ctdb_request_call. Transport is DOWN\n"));
472                 return;
473         }
474
475
476         ctdb_db = find_ctdb_db(ctdb, c->db_id);
477         if (!ctdb_db) {
478                 ctdb_send_error(ctdb, hdr, -1,
479                                 "Unknown database in request. db_id==0x%08x",
480                                 c->db_id);
481                 return;
482         }
483
484         call = talloc(hdr, struct ctdb_call);
485         CTDB_NO_MEMORY_FATAL(ctdb, call);
486
487         call->call_id  = c->callid;
488         call->key.dptr = c->data;
489         call->key.dsize = c->keylen;
490         call->call_data.dptr = c->data + c->keylen;
491         call->call_data.dsize = c->calldatalen;
492
493         /* determine if we are the dmaster for this key. This also
494            fetches the record data (if any), thus avoiding a 2nd fetch of the data 
495            if the call will be answered locally */
496
497         ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, call->key, &header, hdr, &data,
498                                            ctdb_call_input_pkt, ctdb, False);
499         if (ret == -1) {
500                 ctdb_send_error(ctdb, hdr, ret, "ltdb fetch failed in ctdb_request_call");
501                 return;
502         }
503         if (ret == -2) {
504                 DEBUG(DEBUG_INFO,(__location__ " deferred ctdb_request_call\n"));
505                 return;
506         }
507
508         /* if we are not the dmaster, then send a redirect to the
509            requesting node */
510         if (header.dmaster != ctdb->pnn) {
511                 talloc_free(data.dptr);
512                 ctdb_call_send_redirect(ctdb, call->key, c, &header);
513
514                 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
515                 if (ret != 0) {
516                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
517                 }
518                 return;
519         }
520
521         CTDB_UPDATE_STAT(ctdb, max_hop_count, c->hopcount);
522
523         /* Try if possible to migrate the record off to the caller node.
524          * From the clients perspective a fetch of the data is just as 
525          * expensive as a migration.
526          */
527         if (c->hdr.srcnode != ctdb->pnn) {
528                 if (ctdb_db->transaction_active) {
529                         DEBUG(DEBUG_INFO, (__location__ " refusing migration"
530                               " of key %s while transaction is active\n",
531                               (char *)call->key.dptr));
532                 } else {
533                         DEBUG(DEBUG_DEBUG,("pnn %u starting migration of %08x to %u\n",
534                                  ctdb->pnn, ctdb_hash(&(call->key)), c->hdr.srcnode));
535                         ctdb_call_send_dmaster(ctdb_db, c, &header, &(call->key), &data);
536                         talloc_free(data.dptr);
537
538                         ret = ctdb_ltdb_unlock(ctdb_db, call->key);
539                         if (ret != 0) {
540                                 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
541                         }
542                         return;
543                 }
544         }
545
546         ret = ctdb_call_local(ctdb_db, call, &header, hdr, &data, true);
547         if (ret != 0) {
548                 DEBUG(DEBUG_ERR,(__location__ " ctdb_call_local failed\n"));
549                 call->status = -1;
550         }
551
552         ret = ctdb_ltdb_unlock(ctdb_db, call->key);
553         if (ret != 0) {
554                 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
555         }
556
557         len = offsetof(struct ctdb_reply_call, data) + call->reply_data.dsize;
558         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len, 
559                                     struct ctdb_reply_call);
560         CTDB_NO_MEMORY_FATAL(ctdb, r);
561         r->hdr.destnode  = hdr->srcnode;
562         r->hdr.reqid     = hdr->reqid;
563         r->status        = call->status;
564         r->datalen       = call->reply_data.dsize;
565         if (call->reply_data.dsize) {
566                 memcpy(&r->data[0], call->reply_data.dptr, call->reply_data.dsize);
567         }
568
569         ctdb_queue_packet(ctdb, &r->hdr);
570
571         talloc_free(r);
572 }
573
574 /*
575   called when a CTDB_REPLY_CALL packet comes in
576
577   This packet comes in response to a CTDB_REQ_CALL request packet. It
578   contains any reply data from the call
579 */
580 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
581 {
582         struct ctdb_reply_call *c = (struct ctdb_reply_call *)hdr;
583         struct ctdb_call_state *state;
584
585         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
586         if (state == NULL) {
587                 DEBUG(DEBUG_ERR, (__location__ " reqid %u not found\n", hdr->reqid));
588                 return;
589         }
590
591         if (hdr->reqid != state->reqid) {
592                 /* we found a record  but it was the wrong one */
593                 DEBUG(DEBUG_ERR, ("Dropped orphaned call reply with reqid:%u\n",hdr->reqid));
594                 return;
595         }
596
597         state->call->reply_data.dptr = c->data;
598         state->call->reply_data.dsize = c->datalen;
599         state->call->status = c->status;
600
601         talloc_steal(state, c);
602
603         state->state = CTDB_CALL_DONE;
604         if (state->async.fn) {
605                 state->async.fn(state);
606         }
607 }
608
609
610 /*
611   called when a CTDB_REPLY_DMASTER packet comes in
612
613   This packet comes in from the lmaster response to a CTDB_REQ_CALL
614   request packet. It means that the current dmaster wants to give us
615   the dmaster role
616 */
617 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
618 {
619         struct ctdb_reply_dmaster *c = (struct ctdb_reply_dmaster *)hdr;
620         struct ctdb_db_context *ctdb_db;
621         TDB_DATA key, data;
622         uint32_t record_flags = 0;
623         size_t len;
624         int ret;
625
626         ctdb_db = find_ctdb_db(ctdb, c->db_id);
627         if (ctdb_db == NULL) {
628                 DEBUG(DEBUG_ERR,("Unknown db_id 0x%x in ctdb_reply_dmaster\n", c->db_id));
629                 return;
630         }
631         
632         key.dptr = c->data;
633         key.dsize = c->keylen;
634         data.dptr = &c->data[key.dsize];
635         data.dsize = c->datalen;
636         len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize
637                 + sizeof(uint32_t);
638         if (len <= c->hdr.length) {
639                 record_flags = *(uint32_t *)&c->data[c->keylen + c->datalen];
640         }
641
642         ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
643                                      ctdb_call_input_pkt, ctdb, False);
644         if (ret == -2) {
645                 return;
646         }
647         if (ret != 0) {
648                 DEBUG(DEBUG_ERR,(__location__ " Failed to get lock in ctdb_reply_dmaster\n"));
649                 return;
650         }
651
652         ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn, record_flags);
653 }
654
655
656 /*
657   called when a CTDB_REPLY_ERROR packet comes in
658 */
659 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
660 {
661         struct ctdb_reply_error *c = (struct ctdb_reply_error *)hdr;
662         struct ctdb_call_state *state;
663
664         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
665         if (state == NULL) {
666                 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_error\n",
667                          ctdb->pnn, hdr->reqid));
668                 return;
669         }
670
671         if (hdr->reqid != state->reqid) {
672                 /* we found a record  but it was the wrong one */
673                 DEBUG(DEBUG_ERR, ("Dropped orphaned error reply with reqid:%u\n",hdr->reqid));
674                 return;
675         }
676
677         talloc_steal(state, c);
678
679         state->state  = CTDB_CALL_ERROR;
680         state->errmsg = (char *)c->msg;
681         if (state->async.fn) {
682                 state->async.fn(state);
683         }
684 }
685
686
687 /*
688   destroy a ctdb_call
689 */
690 static int ctdb_call_destructor(struct ctdb_call_state *state)
691 {
692         DLIST_REMOVE(state->ctdb_db->ctdb->pending_calls, state);
693         ctdb_reqid_remove(state->ctdb_db->ctdb, state->reqid);
694         return 0;
695 }
696
697
698 /*
699   called when a ctdb_call needs to be resent after a reconfigure event
700 */
701 static void ctdb_call_resend(struct ctdb_call_state *state)
702 {
703         struct ctdb_context *ctdb = state->ctdb_db->ctdb;
704
705         state->generation = ctdb->vnn_map->generation;
706
707         /* use a new reqid, in case the old reply does eventually come in */
708         ctdb_reqid_remove(ctdb, state->reqid);
709         state->reqid = ctdb_reqid_new(ctdb, state);
710         state->c->hdr.reqid = state->reqid;
711
712         /* update the generation count for this request, so its valid with the new vnn_map */
713         state->c->hdr.generation = state->generation;
714
715         /* send the packet to ourselves, it will be redirected appropriately */
716         state->c->hdr.destnode = ctdb->pnn;
717
718         ctdb_queue_packet(ctdb, &state->c->hdr);
719         DEBUG(DEBUG_NOTICE,("resent ctdb_call\n"));
720 }
721
722 /*
723   resend all pending calls on recovery
724  */
725 void ctdb_call_resend_all(struct ctdb_context *ctdb)
726 {
727         struct ctdb_call_state *state, *next;
728         for (state=ctdb->pending_calls;state;state=next) {
729                 next = state->next;
730                 ctdb_call_resend(state);
731         }
732 }
733
734 /*
735   this allows the caller to setup a async.fn 
736 */
737 static void call_local_trigger(struct event_context *ev, struct timed_event *te, 
738                        struct timeval t, void *private_data)
739 {
740         struct ctdb_call_state *state = talloc_get_type(private_data, struct ctdb_call_state);
741         if (state->async.fn) {
742                 state->async.fn(state);
743         }
744 }       
745
746
747 /*
748   construct an event driven local ctdb_call
749
750   this is used so that locally processed ctdb_call requests are processed
751   in an event driven manner
752 */
753 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
754                                              struct ctdb_call *call,
755                                              struct ctdb_ltdb_header *header,
756                                              TDB_DATA *data)
757 {
758         struct ctdb_call_state *state;
759         struct ctdb_context *ctdb = ctdb_db->ctdb;
760         int ret;
761
762         state = talloc_zero(ctdb_db, struct ctdb_call_state);
763         CTDB_NO_MEMORY_NULL(ctdb, state);
764
765         talloc_steal(state, data->dptr);
766
767         state->state = CTDB_CALL_DONE;
768         state->call  = talloc(state, struct ctdb_call);
769         CTDB_NO_MEMORY_NULL(ctdb, state->call);
770         *(state->call) = *call;
771         state->ctdb_db = ctdb_db;
772
773         ret = ctdb_call_local(ctdb_db, state->call, header, state, data, true);
774
775         event_add_timed(ctdb->ev, state, timeval_zero(), call_local_trigger, state);
776
777         return state;
778 }
779
780
781 /*
782   make a remote ctdb call - async send. Called in daemon context.
783
784   This constructs a ctdb_call request and queues it for processing. 
785   This call never blocks.
786 */
787 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
788                                                      struct ctdb_call *call, 
789                                                      struct ctdb_ltdb_header *header)
790 {
791         uint32_t len;
792         struct ctdb_call_state *state;
793         struct ctdb_context *ctdb = ctdb_db->ctdb;
794
795         if (ctdb->methods == NULL) {
796                 DEBUG(DEBUG_INFO,(__location__ " Failed send packet. Transport is down\n"));
797                 return NULL;
798         }
799
800         state = talloc_zero(ctdb_db, struct ctdb_call_state);
801         CTDB_NO_MEMORY_NULL(ctdb, state);
802         state->call = talloc(state, struct ctdb_call);
803         CTDB_NO_MEMORY_NULL(ctdb, state->call);
804
805         state->reqid = ctdb_reqid_new(ctdb, state);
806         state->ctdb_db = ctdb_db;
807         talloc_set_destructor(state, ctdb_call_destructor);
808
809         len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
810         state->c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CALL, len, 
811                                            struct ctdb_req_call);
812         CTDB_NO_MEMORY_NULL(ctdb, state->c);
813         state->c->hdr.destnode  = header->dmaster;
814
815         /* this limits us to 16k outstanding messages - not unreasonable */
816         state->c->hdr.reqid     = state->reqid;
817         state->c->flags         = call->flags;
818         state->c->db_id         = ctdb_db->db_id;
819         state->c->callid        = call->call_id;
820         state->c->hopcount      = 0;
821         state->c->keylen        = call->key.dsize;
822         state->c->calldatalen   = call->call_data.dsize;
823         memcpy(&state->c->data[0], call->key.dptr, call->key.dsize);
824         memcpy(&state->c->data[call->key.dsize], 
825                call->call_data.dptr, call->call_data.dsize);
826         *(state->call)              = *call;
827         state->call->call_data.dptr = &state->c->data[call->key.dsize];
828         state->call->key.dptr       = &state->c->data[0];
829
830         state->state  = CTDB_CALL_WAIT;
831         state->generation = ctdb->vnn_map->generation;
832
833         DLIST_ADD(ctdb->pending_calls, state);
834
835         ctdb_queue_packet(ctdb, &state->c->hdr);
836
837         return state;
838 }
839
840 /*
841   make a remote ctdb call - async recv - called in daemon context
842
843   This is called when the program wants to wait for a ctdb_call to complete and get the 
844   results. This call will block unless the call has already completed.
845 */
846 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call)
847 {
848         while (state->state < CTDB_CALL_DONE) {
849                 event_loop_once(state->ctdb_db->ctdb->ev);
850         }
851         if (state->state != CTDB_CALL_DONE) {
852                 ctdb_set_error(state->ctdb_db->ctdb, "%s", state->errmsg);
853                 talloc_free(state);
854                 return -1;
855         }
856
857         if (state->call->reply_data.dsize) {
858                 call->reply_data.dptr = talloc_memdup(call,
859                                                       state->call->reply_data.dptr,
860                                                       state->call->reply_data.dsize);
861                 call->reply_data.dsize = state->call->reply_data.dsize;
862         } else {
863                 call->reply_data.dptr = NULL;
864                 call->reply_data.dsize = 0;
865         }
866         call->status = state->call->status;
867         talloc_free(state);
868         return 0;
869 }
870
871
872 /* 
873    send a keepalive packet to the other node
874 */
875 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode)
876 {
877         struct ctdb_req_keepalive *r;
878         
879         if (ctdb->methods == NULL) {
880                 DEBUG(DEBUG_INFO,(__location__ " Failed to send keepalive. Transport is DOWN\n"));
881                 return;
882         }
883
884         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_KEEPALIVE,
885                                     sizeof(struct ctdb_req_keepalive), 
886                                     struct ctdb_req_keepalive);
887         CTDB_NO_MEMORY_FATAL(ctdb, r);
888         r->hdr.destnode  = destnode;
889         r->hdr.reqid     = 0;
890         
891         CTDB_INCREMENT_STAT(ctdb, keepalive_packets_sent);
892
893         ctdb_queue_packet(ctdb, &r->hdr);
894
895         talloc_free(r);
896 }
897
898
899
900 struct revokechild_deferred_call {
901         struct ctdb_context *ctdb;
902         struct ctdb_req_header *hdr;
903         deferred_requeue_fn fn;
904         void *ctx;
905 };
906
907 struct revokechild_handle {
908         struct revokechild_handle *next, *prev;
909         struct ctdb_context *ctdb;
910         struct ctdb_db_context *ctdb_db;
911         struct fd_event *fde;
912         int status;
913         int fd[2];
914         pid_t child;
915         TDB_DATA key;
916 };
917
918 struct revokechild_requeue_handle {
919         struct ctdb_context *ctdb;
920         struct ctdb_req_header *hdr;
921         deferred_requeue_fn fn;
922         void *ctx;
923 };
924
925 static void deferred_call_requeue(struct event_context *ev, struct timed_event *te,
926                       struct timeval t, void *private_data)
927 {
928         struct revokechild_requeue_handle *requeue_handle = talloc_get_type(private_data, struct revokechild_requeue_handle);
929
930         requeue_handle->fn(requeue_handle->ctx, requeue_handle->hdr);
931         talloc_free(requeue_handle);
932 }
933
934 static int deferred_call_destructor(struct revokechild_deferred_call *deferred_call)
935 {
936         struct ctdb_context *ctdb = deferred_call->ctdb;
937         struct revokechild_requeue_handle *requeue_handle = talloc(ctdb, struct revokechild_requeue_handle);
938         struct ctdb_req_call *c = (struct ctdb_req_call *)deferred_call->hdr;
939
940         requeue_handle->ctdb = ctdb;
941         requeue_handle->hdr  = deferred_call->hdr;
942         requeue_handle->fn   = deferred_call->fn;
943         requeue_handle->ctx  = deferred_call->ctx;
944         talloc_steal(requeue_handle, requeue_handle->hdr);
945
946         /* when revoking, any READONLY requests have 1 second grace to let read/write finish first */
947         event_add_timed(ctdb->ev, requeue_handle, timeval_current_ofs(c->flags & CTDB_WANT_READONLY ? 1 : 0, 0), deferred_call_requeue, requeue_handle);
948
949         return 0;
950 }
951
952
953 static int revokechild_destructor(struct revokechild_handle *rc)
954 {
955         if (rc->fde != NULL) {
956                 talloc_free(rc->fde);
957         }
958
959         if (rc->fd[0] != -1) {
960                 close(rc->fd[0]);
961         }
962         if (rc->fd[1] != -1) {
963                 close(rc->fd[1]);
964         }
965         kill(rc->child, SIGKILL);
966
967         DLIST_REMOVE(rc->ctdb_db->revokechild_active, rc);
968         return 0;
969 }
970
971 static void revokechild_handler(struct event_context *ev, struct fd_event *fde,
972                      uint16_t flags, void *private_data)
973 {
974         struct revokechild_handle *rc = talloc_get_type(private_data,
975                                                      struct revokechild_handle);
976         int ret;
977         char c;
978
979         ret = read(rc->fd[0], &c, 1);
980         if (ret != 1) {
981                 DEBUG(DEBUG_ERR,("Failed to read status from revokechild. errno:%d\n", errno));
982                 rc->status = -1;
983                 talloc_free(rc);
984                 return;
985         }
986         if (c != 0) {
987                 DEBUG(DEBUG_ERR,("revokechild returned failure. status:%d\n", c));
988                 rc->status = -1;
989                 talloc_free(rc);
990                 return;
991         }
992
993         talloc_free(rc);
994 }
995
996 struct ctdb_revoke_state {
997         struct ctdb_db_context *ctdb_db;
998         TDB_DATA key;
999         struct ctdb_ltdb_header *header;
1000         TDB_DATA data;
1001         int count;
1002         int status;
1003         int finished;
1004 };
1005
1006 static void update_record_cb(struct ctdb_client_control_state *state)
1007 {
1008         struct ctdb_revoke_state *revoke_state;
1009         int ret;
1010         int32_t res;
1011
1012         if (state == NULL) {
1013                 return;
1014         }
1015         revoke_state = state->async.private_data;
1016
1017         state->async.fn = NULL;
1018          ret = ctdb_control_recv(state->ctdb, state, state, NULL, &res, NULL);
1019          if ((ret != 0) || (res != 0)) {
1020                 DEBUG(DEBUG_ERR,("Recv for revoke update record failed ret:%d res:%d\n", ret, res));
1021                 revoke_state->status = -1;
1022         }
1023
1024         revoke_state->count--;
1025         if (revoke_state->count <= 0) {
1026                 revoke_state->finished = 1;
1027         }
1028 }
1029
1030 static void revoke_send_cb(struct ctdb_context *ctdb, uint32_t pnn, void *private_data)
1031 {
1032         struct ctdb_revoke_state *revoke_state = private_data;
1033         struct ctdb_client_control_state *state;
1034
1035         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);
1036         if (state == NULL) {
1037                 DEBUG(DEBUG_ERR,("Failure to send update record to revoke readonly delegation\n"));
1038                 revoke_state->status = -1;
1039                 return;
1040         }
1041         state->async.fn           = update_record_cb;
1042         state->async.private_data = revoke_state;
1043
1044         revoke_state->count++;
1045
1046 }
1047
1048 static void ctdb_revoke_timeout_handler(struct event_context *ev, struct timed_event *te,
1049                              struct timeval yt, void *private_data)
1050 {
1051         struct ctdb_revoke_state *state = private_data;
1052
1053         DEBUG(DEBUG_ERR,("Timed out waiting for revoke to finish\n"));
1054         state->finished = 1;
1055         state->status   = -1;
1056 }
1057
1058 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)
1059 {
1060         struct ctdb_revoke_state *state = talloc_zero(ctdb, struct ctdb_revoke_state);
1061         int status;
1062
1063         state->ctdb_db = ctdb_db;
1064         state->key     = key;
1065         state->header  = header;
1066         state->data    = data;
1067
1068         ctdb_trackingdb_traverse(ctdb, tdata, revoke_send_cb, state);
1069
1070         event_add_timed(ctdb->ev, state, timeval_current_ofs(5, 0), ctdb_revoke_timeout_handler, state);
1071
1072         while (state->finished == 0) {
1073                 event_loop_once(ctdb->ev);
1074         }
1075
1076         status = state->status;
1077
1078         if (status == 0) {
1079                 struct ctdb_ltdb_header new_header;
1080                 TDB_DATA new_data;
1081
1082                 if (ctdb_ltdb_lock(ctdb_db, key) != 0) {
1083                         DEBUG(DEBUG_ERR,("Failed to chainlock the database in revokechild\n"));
1084                         talloc_free(state);
1085                         return -1;
1086                 }
1087                 if (ctdb_ltdb_fetch(ctdb_db, key, &new_header, state, &new_data) != 0) {
1088                         ctdb_ltdb_unlock(ctdb_db, key);
1089                         DEBUG(DEBUG_ERR,("Failed for fetch tdb record in revokechild\n"));
1090                         talloc_free(state);
1091                         return -1;
1092                 }
1093                 header->rsn++;
1094                 if (new_header.rsn > header->rsn) {
1095                         ctdb_ltdb_unlock(ctdb_db, key);
1096                         DEBUG(DEBUG_ERR,("RSN too high in tdb record in revokechild\n"));
1097                         talloc_free(state);
1098                         return -1;
1099                 }
1100                 if ( (new_header.flags & (CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_HAVE_DELEGATIONS)) != (CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_HAVE_DELEGATIONS) ) {
1101                         ctdb_ltdb_unlock(ctdb_db, key);
1102                         DEBUG(DEBUG_ERR,("Flags are wrong in tdb record in revokechild\n"));
1103                         talloc_free(state);
1104                         return -1;
1105                 }
1106                 new_header.rsn++;
1107                 new_header.flags |= CTDB_REC_RO_REVOKE_COMPLETE;
1108                 if (ctdb_ltdb_store(ctdb_db, key, &new_header, new_data) != 0) {
1109                         ctdb_ltdb_unlock(ctdb_db, key);
1110                         DEBUG(DEBUG_ERR,("Failed to write new record in revokechild\n"));
1111                         talloc_free(state);
1112                         return -1;
1113                 }
1114                 ctdb_ltdb_unlock(ctdb_db, key);
1115         }
1116
1117         talloc_free(state);
1118         return status;
1119 }
1120
1121
1122 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)
1123 {
1124         TDB_DATA tdata;
1125         struct revokechild_handle *rc;
1126         pid_t parent = getpid();
1127         int ret;
1128
1129         header->flags &= ~(CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_HAVE_DELEGATIONS|CTDB_REC_RO_HAVE_READONLY);
1130         header->rsn   -= 1;
1131
1132         if ((rc = talloc_zero(ctdb_db, struct revokechild_handle)) == NULL) {
1133                 DEBUG(DEBUG_ERR,("Failed to allocate revokechild_handle\n"));
1134                 return -1;
1135         }
1136
1137         tdata = tdb_fetch(ctdb_db->rottdb, key);
1138         if (tdata.dsize > 0) {
1139                 uint8_t *tmp;
1140
1141                 tmp = tdata.dptr;
1142                 tdata.dptr = talloc_memdup(rc, tdata.dptr, tdata.dsize);
1143                 free(tmp);
1144         }
1145
1146         rc->status    = 0;
1147         rc->ctdb      = ctdb;
1148         rc->ctdb_db   = ctdb_db;
1149         rc->fd[0]     = -1;
1150         rc->fd[1]     = -1;
1151
1152         talloc_set_destructor(rc, revokechild_destructor);
1153
1154         rc->key.dsize = key.dsize;
1155         rc->key.dptr  = talloc_memdup(rc, key.dptr, key.dsize);
1156         if (rc->key.dptr == NULL) {
1157                 DEBUG(DEBUG_ERR,("Failed to allocate key for revokechild_handle\n"));
1158                 talloc_free(rc);
1159                 return -1;
1160         }
1161
1162         ret = pipe(rc->fd);
1163         if (ret != 0) {
1164                 DEBUG(DEBUG_ERR,("Failed to allocate key for revokechild_handle\n"));
1165                 talloc_free(rc);
1166                 return -1;
1167         }
1168
1169
1170         rc->child = ctdb_fork(ctdb);
1171         if (rc->child == (pid_t)-1) {
1172                 DEBUG(DEBUG_ERR,("Failed to fork child for revokechild\n"));
1173                 talloc_free(rc);
1174                 return -1;
1175         }
1176
1177         if (rc->child == 0) {
1178                 char c = 0;
1179                 close(rc->fd[0]);
1180                 debug_extra = talloc_asprintf(NULL, "revokechild-%s:", ctdb_db->db_name);
1181
1182                 if (switch_from_server_to_client(ctdb, "revokechild-%s", ctdb_db->db_name) != 0) {
1183                         DEBUG(DEBUG_ERR,("Failed to switch from server to client for revokechild process\n"));
1184                         c = 1;
1185                         goto child_finished;
1186                 }
1187
1188                 c = ctdb_revoke_all_delegations(ctdb, ctdb_db, tdata, key, header, data);
1189
1190 child_finished:
1191                 write(rc->fd[1], &c, 1);
1192                 /* make sure we die when our parent dies */
1193                 while (kill(parent, 0) == 0 || errno != ESRCH) {
1194                         sleep(5);
1195                 }
1196                 _exit(0);
1197         }
1198
1199         close(rc->fd[1]);
1200         rc->fd[1] = -1;
1201         set_close_on_exec(rc->fd[0]);
1202
1203         /* This is an active revokechild child process */
1204         DLIST_ADD_END(ctdb_db->revokechild_active, rc, NULL);
1205
1206         rc->fde = event_add_fd(ctdb->ev, rc, rc->fd[0],
1207                                    EVENT_FD_READ, revokechild_handler,
1208                                    (void *)rc);
1209         if (rc->fde == NULL) {
1210                 DEBUG(DEBUG_ERR,("Failed to set up fd event for revokechild process\n"));
1211                 talloc_free(rc);
1212         }
1213         tevent_fd_set_auto_close(rc->fde);
1214
1215         return 0;
1216 }
1217
1218 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)
1219 {
1220         struct revokechild_handle *rc;
1221         struct revokechild_deferred_call *deferred_call;
1222
1223         for (rc = ctdb_db->revokechild_active; rc; rc = rc->next) {
1224                 if (rc->key.dsize == 0) {
1225                         continue;
1226                 }
1227                 if (rc->key.dsize != key.dsize) {
1228                         continue;
1229                 }
1230                 if (!memcmp(rc->key.dptr, key.dptr, key.dsize)) {
1231                         break;
1232                 }
1233         }
1234
1235         if (rc == NULL) {
1236                 DEBUG(DEBUG_ERR,("Failed to add deferred call to revoke list. revoke structure not found\n"));
1237                 return -1;
1238         }
1239
1240         deferred_call = talloc(rc, struct revokechild_deferred_call);
1241         if (deferred_call == NULL) {
1242                 DEBUG(DEBUG_ERR,("Failed to allocate deferred call structure for revoking record\n"));
1243                 return -1;
1244         }
1245
1246         deferred_call->ctdb = ctdb;
1247         deferred_call->hdr  = hdr;
1248         deferred_call->fn   = fn;
1249         deferred_call->ctx  = call_context;
1250
1251         talloc_set_destructor(deferred_call, deferred_call_destructor);
1252         talloc_steal(deferred_call, hdr);
1253
1254         return 0;
1255 }