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