Merge commit 'obnox/ctdb-wip-trans3' into trans3
[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/events/events.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_ERR,(__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 static void ctdb_call_send_redirect(struct ctdb_context *ctdb, 
106                                     TDB_DATA key,
107                                     struct ctdb_req_call *c, 
108                                     struct ctdb_ltdb_header *header)
109 {
110         
111         uint32_t lmaster = ctdb_lmaster(ctdb, &key);
112         if (ctdb->pnn == lmaster) {
113                 c->hdr.destnode = header->dmaster;
114         } else if ((c->hopcount % ctdb->tunable.max_redirect_count) == 0) {
115                 c->hdr.destnode = lmaster;
116         } else {
117                 c->hdr.destnode = header->dmaster;
118         }
119         c->hopcount++;
120         ctdb_queue_packet(ctdb, &c->hdr);
121 }
122
123
124 /*
125   send a dmaster reply
126
127   caller must have the chainlock before calling this routine. Caller must be
128   the lmaster
129 */
130 static void ctdb_send_dmaster_reply(struct ctdb_db_context *ctdb_db,
131                                     struct ctdb_ltdb_header *header,
132                                     TDB_DATA key, TDB_DATA data,
133                                     uint32_t new_dmaster,
134                                     uint32_t reqid)
135 {
136         struct ctdb_context *ctdb = ctdb_db->ctdb;
137         struct ctdb_reply_dmaster *r;
138         int ret, len;
139         TALLOC_CTX *tmp_ctx;
140
141         if (ctdb->pnn != ctdb_lmaster(ctdb, &key)) {
142                 DEBUG(DEBUG_ALERT,(__location__ " Caller is not lmaster!\n"));
143                 return;
144         }
145
146         header->dmaster = new_dmaster;
147         ret = ctdb_ltdb_store(ctdb_db, key, header, data);
148         if (ret != 0) {
149                 ctdb_fatal(ctdb, "ctdb_send_dmaster_reply unable to update dmaster");
150                 return;
151         }
152
153         if (ctdb->methods == NULL) {
154                 ctdb_fatal(ctdb, "ctdb_send_dmaster_reply cant update dmaster sicne transport is down");
155                 return;
156         }
157
158         /* put the packet on a temporary context, allowing us to safely free
159            it below even if ctdb_reply_dmaster() has freed it already */
160         tmp_ctx = talloc_new(ctdb);
161
162         /* send the CTDB_REPLY_DMASTER */
163         len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize;
164         r = ctdb_transport_allocate(ctdb, tmp_ctx, CTDB_REPLY_DMASTER, len,
165                                     struct ctdb_reply_dmaster);
166         CTDB_NO_MEMORY_FATAL(ctdb, r);
167
168         r->hdr.destnode  = new_dmaster;
169         r->hdr.reqid     = reqid;
170         r->rsn           = header->rsn;
171         r->keylen        = key.dsize;
172         r->datalen       = data.dsize;
173         r->db_id         = ctdb_db->db_id;
174         memcpy(&r->data[0], key.dptr, key.dsize);
175         memcpy(&r->data[key.dsize], data.dptr, data.dsize);
176
177         ctdb_queue_packet(ctdb, &r->hdr);
178
179         talloc_free(tmp_ctx);
180 }
181
182 /*
183   send a dmaster request (give another node the dmaster for a record)
184
185   This is always sent to the lmaster, which ensures that the lmaster
186   always knows who the dmaster is. The lmaster will then send a
187   CTDB_REPLY_DMASTER to the new dmaster
188 */
189 static void ctdb_call_send_dmaster(struct ctdb_db_context *ctdb_db, 
190                                    struct ctdb_req_call *c, 
191                                    struct ctdb_ltdb_header *header,
192                                    TDB_DATA *key, TDB_DATA *data)
193 {
194         struct ctdb_req_dmaster *r;
195         struct ctdb_context *ctdb = ctdb_db->ctdb;
196         int len;
197         uint32_t lmaster = ctdb_lmaster(ctdb, key);
198
199         if (ctdb->methods == NULL) {
200                 ctdb_fatal(ctdb, "Failed ctdb_call_send_dmaster since transport is down");
201                 return;
202         }
203
204         if (lmaster == ctdb->pnn) {
205                 ctdb_send_dmaster_reply(ctdb_db, header, *key, *data, 
206                                         c->hdr.srcnode, c->hdr.reqid);
207                 return;
208         }
209         
210         len = offsetof(struct ctdb_req_dmaster, data) + key->dsize + data->dsize;
211         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_DMASTER, len, 
212                                     struct ctdb_req_dmaster);
213         CTDB_NO_MEMORY_FATAL(ctdb, r);
214         r->hdr.destnode  = lmaster;
215         r->hdr.reqid     = c->hdr.reqid;
216         r->db_id         = c->db_id;
217         r->rsn           = header->rsn;
218         r->dmaster       = c->hdr.srcnode;
219         r->keylen        = key->dsize;
220         r->datalen       = data->dsize;
221         memcpy(&r->data[0], key->dptr, key->dsize);
222         memcpy(&r->data[key->dsize], data->dptr, data->dsize);
223
224         header->dmaster = c->hdr.srcnode;
225         if (ctdb_ltdb_store(ctdb_db, *key, header, *data) != 0) {
226                 ctdb_fatal(ctdb, "Failed to store record in ctdb_call_send_dmaster");
227         }
228         
229         ctdb_queue_packet(ctdb, &r->hdr);
230
231         talloc_free(r);
232 }
233
234 static void ctdb_hold_back_key(struct ctdb_db_context *db, TDB_DATA key)
235 {
236         size_t num_keys;
237         uint8_t **tmp;
238
239         DEBUG(DEBUG_INFO, ("Holding back key %08x\n", ctdb_hash(&key)));
240
241         num_keys = talloc_array_length(db->holdback_keys);
242         tmp = talloc_realloc(db, db->holdback_keys, uint8_t *, num_keys+1);
243         if (tmp == NULL) {
244                 DEBUG(DEBUG_ERR, ("talloc_realloc failed\n"));
245                 return;
246         }
247         db->holdback_keys = tmp;
248
249         db->holdback_keys[num_keys] = (uint8_t *)talloc_memdup(
250                 db->holdback_keys, key.dptr, key.dsize);
251         if (db->holdback_keys[num_keys] == NULL) {
252                 DEBUG(DEBUG_ERR, ("talloc_memdup failed\n"));
253                 db->holdback_keys = talloc_realloc(db, db->holdback_keys,
254                                                    uint8_t *, num_keys);
255         }
256 }
257
258 /*
259   called when a CTDB_REPLY_DMASTER packet comes in, or when the lmaster
260   gets a CTDB_REQUEST_DMASTER for itself. We become the dmaster.
261
262   must be called with the chainlock held. This function releases the chainlock
263 */
264 static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db, 
265                                 struct ctdb_req_header *hdr,
266                                 TDB_DATA key, TDB_DATA data,
267                                 uint64_t rsn)
268 {
269         struct ctdb_call_state *state;
270         struct ctdb_context *ctdb = ctdb_db->ctdb;
271         struct ctdb_ltdb_header header;
272
273         DEBUG(DEBUG_INFO,("pnn %u dmaster response %08x\n", ctdb->pnn, ctdb_hash(&key)));
274
275         ZERO_STRUCT(header);
276         header.rsn = rsn + 1;
277         header.dmaster = ctdb->pnn;
278
279         if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
280                 ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n");
281                 ctdb_ltdb_unlock(ctdb_db, key);
282                 return;
283         }
284
285         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
286
287         if (state == NULL) {
288                 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_become_dmaster from node %u\n",
289                          ctdb->pnn, hdr->reqid, hdr->srcnode));
290                 ctdb_ltdb_unlock(ctdb_db, key);
291                 return;
292         }
293
294         if (hdr->reqid != state->reqid) {
295                 /* we found a record  but it was the wrong one */
296                 DEBUG(DEBUG_ERR, ("Dropped orphan in ctdb_become_dmaster with reqid:%u\n from node %u", hdr->reqid, hdr->srcnode));
297                 ctdb_ltdb_unlock(ctdb_db, key);
298                 return;
299         }
300
301         ctdb_hold_back_key(ctdb_db, key);
302
303         ctdb_call_local(ctdb_db, state->call, &header, state, &data, ctdb->pnn);
304
305         ctdb_ltdb_unlock(ctdb_db, state->call->key);
306
307         state->state = CTDB_CALL_DONE;
308         if (state->async.fn) {
309                 state->async.fn(state);
310         }
311 }
312
313
314
315 /*
316   called when a CTDB_REQ_DMASTER packet comes in
317
318   this comes into the lmaster for a record when the current dmaster
319   wants to give up the dmaster role and give it to someone else
320 */
321 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
322 {
323         struct ctdb_req_dmaster *c = (struct ctdb_req_dmaster *)hdr;
324         TDB_DATA key, data, data2;
325         struct ctdb_ltdb_header header;
326         struct ctdb_db_context *ctdb_db;
327         int ret;
328
329         key.dptr = c->data;
330         key.dsize = c->keylen;
331         data.dptr = c->data + c->keylen;
332         data.dsize = c->datalen;
333
334         ctdb_db = find_ctdb_db(ctdb, c->db_id);
335         if (!ctdb_db) {
336                 ctdb_send_error(ctdb, hdr, -1,
337                                 "Unknown database in request. db_id==0x%08x",
338                                 c->db_id);
339                 return;
340         }
341         
342         /* fetch the current record */
343         ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, hdr, &data2,
344                                            ctdb_call_input_pkt, ctdb, False);
345         if (ret == -1) {
346                 ctdb_fatal(ctdb, "ctdb_req_dmaster failed to fetch record");
347                 return;
348         }
349         if (ret == -2) {
350                 DEBUG(DEBUG_INFO,(__location__ " deferring ctdb_request_dmaster\n"));
351                 return;
352         }
353
354         if (ctdb_lmaster(ctdb, &key) != ctdb->pnn) {
355                 DEBUG(DEBUG_ALERT,("pnn %u dmaster request to non-lmaster lmaster=%u gen=%u curgen=%u\n",
356                          ctdb->pnn, ctdb_lmaster(ctdb, &key), 
357                          hdr->generation, ctdb->vnn_map->generation));
358                 ctdb_fatal(ctdb, "ctdb_req_dmaster to non-lmaster");
359         }
360
361         DEBUG(DEBUG_INFO,("pnn %u dmaster request on %08x for %u from %u\n", 
362                  ctdb->pnn, ctdb_hash(&key), c->dmaster, c->hdr.srcnode));
363
364         /* its a protocol error if the sending node is not the current dmaster */
365         if (header.dmaster != hdr->srcnode) {
366                 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",
367                          ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
368                          ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
369                          (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid,
370                          (key.dsize >= 4)?(*(uint32_t *)key.dptr):0));
371                 if (header.rsn != 0 || header.dmaster != ctdb->pnn) {
372                         ctdb_fatal(ctdb, "ctdb_req_dmaster from non-master");
373                         return;
374                 }
375         }
376
377         if (header.rsn > c->rsn) {
378                 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",
379                          ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
380                          ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
381                          (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid));
382         }
383
384         /* use the rsn from the sending node */
385         header.rsn = c->rsn;
386
387         /* check if the new dmaster is the lmaster, in which case we
388            skip the dmaster reply */
389         if (c->dmaster == ctdb->pnn) {
390                 ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn);
391         } else {
392                 ctdb_send_dmaster_reply(ctdb_db, &header, key, data, c->dmaster, hdr->reqid);
393                 ctdb_ltdb_unlock(ctdb_db, key);
394         }
395 }
396
397 /*
398  * Did we just pull the dmaster of the record for a client fetch_lock,
399  * so should we hold it back a while and thus give our client the
400  * chance to do its own tdb_lock?
401  */
402
403 static bool ctdb_held_back(struct ctdb_db_context *db, TDB_DATA key,
404                            struct ctdb_req_header *hdr)
405 {
406         int i;
407         size_t num_keys = talloc_array_length(db->holdback_keys);
408         size_t num_hdrs;
409         struct ctdb_req_header **tmp;
410
411         for (i=0; i<num_keys; i++) {
412                 uint8_t *hold_key = db->holdback_keys[i];
413                 size_t keylength = talloc_array_length(hold_key);
414
415                 if ((keylength == key.dsize)
416                     && (memcmp(hold_key, key.dptr, keylength) == 0)) {
417                         break;
418                 }
419         }
420         if (i == num_keys) {
421                 return false;
422         }
423         DEBUG(DEBUG_DEBUG, ("holding back record %08x after migration\n",
424                             ctdb_hash(&key)));
425
426         num_hdrs = talloc_array_length(db->held_back);
427
428         tmp = talloc_realloc(db, db->held_back, struct ctdb_req_header *,
429                              num_hdrs + 1);
430         if (tmp == NULL) {
431                 DEBUG(DEBUG_ERR, (__location__ "talloc_realloc failed\n"));
432                 return false;
433         }
434         db->held_back = tmp;
435         db->held_back[num_hdrs] = talloc_move(db->held_back, &hdr);
436         return true;
437 }
438
439 /*
440   called when a CTDB_REQ_CALL packet comes in
441 */
442 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
443 {
444         struct ctdb_req_call *c = (struct ctdb_req_call *)hdr;
445         TDB_DATA data;
446         struct ctdb_reply_call *r;
447         int ret, len;
448         struct ctdb_ltdb_header header;
449         struct ctdb_call *call;
450         struct ctdb_db_context *ctdb_db;
451
452         if (ctdb->methods == NULL) {
453                 DEBUG(DEBUG_ERR,(__location__ " Failed ctdb_request_call. Transport is DOWN\n"));
454                 return;
455         }
456
457
458         ctdb_db = find_ctdb_db(ctdb, c->db_id);
459         if (!ctdb_db) {
460                 ctdb_send_error(ctdb, hdr, -1,
461                                 "Unknown database in request. db_id==0x%08x",
462                                 c->db_id);
463                 return;
464         }
465
466         call = talloc(hdr, struct ctdb_call);
467         CTDB_NO_MEMORY_FATAL(ctdb, call);
468
469         call->call_id  = c->callid;
470         call->key.dptr = c->data;
471         call->key.dsize = c->keylen;
472         call->call_data.dptr = c->data + c->keylen;
473         call->call_data.dsize = c->calldatalen;
474
475         /* determine if we are the dmaster for this key. This also
476            fetches the record data (if any), thus avoiding a 2nd fetch of the data 
477            if the call will be answered locally */
478
479         ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, call->key, &header, hdr, &data,
480                                            ctdb_call_input_pkt, ctdb, False);
481         if (ret == -1) {
482                 ctdb_send_error(ctdb, hdr, ret, "ltdb fetch failed in ctdb_request_call");
483                 return;
484         }
485         if (ret == -2) {
486                 DEBUG(DEBUG_INFO,(__location__ " deferred ctdb_request_call\n"));
487                 return;
488         }
489
490         /* if we are not the dmaster, then send a redirect to the
491            requesting node */
492         if (header.dmaster != ctdb->pnn) {
493                 talloc_free(data.dptr);
494                 ctdb_call_send_redirect(ctdb, call->key, c, &header);
495                 ctdb_ltdb_unlock(ctdb_db, call->key);
496                 return;
497         }
498
499         if (c->hopcount > ctdb->statistics.max_hop_count) {
500                 ctdb->statistics.max_hop_count = c->hopcount;
501         }
502
503         if ((c->flags & CTDB_IMMEDIATE_MIGRATION)
504             && (ctdb_held_back(ctdb_db, call->key, hdr))) {
505                 talloc_free(data.dptr);
506                 ctdb_ltdb_unlock(ctdb_db, call->key);
507                 return;
508         }
509
510         /* if this nodes has done enough consecutive calls on the same record
511            then give them the record
512            or if the node requested an immediate migration
513         */
514         if ( c->hdr.srcnode != ctdb->pnn &&
515              ((header.laccessor == c->hdr.srcnode
516                && header.lacount >= ctdb->tunable.max_lacount)
517               || (c->flags & CTDB_IMMEDIATE_MIGRATION)) ) {
518                 if (ctdb_db->transaction_active) {
519                         DEBUG(DEBUG_INFO, (__location__ " refusing migration"
520                               " of key %s while transaction is active\n",
521                               (char *)call->key.dptr));
522                 } else {
523                         DEBUG(DEBUG_INFO,("pnn %u starting migration of %08x to %u\n",
524                                  ctdb->pnn, ctdb_hash(&(call->key)), c->hdr.srcnode));
525                         ctdb_call_send_dmaster(ctdb_db, c, &header, &(call->key), &data);
526                         talloc_free(data.dptr);
527                         ctdb_ltdb_unlock(ctdb_db, call->key);
528                         return;
529                 }
530         }
531
532         ctdb_call_local(ctdb_db, call, &header, hdr, &data, c->hdr.srcnode);
533
534         ctdb_ltdb_unlock(ctdb_db, call->key);
535
536         len = offsetof(struct ctdb_reply_call, data) + call->reply_data.dsize;
537         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len, 
538                                     struct ctdb_reply_call);
539         CTDB_NO_MEMORY_FATAL(ctdb, r);
540         r->hdr.destnode  = hdr->srcnode;
541         r->hdr.reqid     = hdr->reqid;
542         r->status        = call->status;
543         r->datalen       = call->reply_data.dsize;
544         if (call->reply_data.dsize) {
545                 memcpy(&r->data[0], call->reply_data.dptr, call->reply_data.dsize);
546         }
547
548         ctdb_queue_packet(ctdb, &r->hdr);
549
550         talloc_free(r);
551 }
552
553 /*
554   called when a CTDB_REPLY_CALL packet comes in
555
556   This packet comes in response to a CTDB_REQ_CALL request packet. It
557   contains any reply data from the call
558 */
559 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
560 {
561         struct ctdb_reply_call *c = (struct ctdb_reply_call *)hdr;
562         struct ctdb_call_state *state;
563
564         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
565         if (state == NULL) {
566                 DEBUG(DEBUG_ERR, (__location__ " reqid %u not found\n", hdr->reqid));
567                 return;
568         }
569
570         if (hdr->reqid != state->reqid) {
571                 /* we found a record  but it was the wrong one */
572                 DEBUG(DEBUG_ERR, ("Dropped orphaned call reply with reqid:%u\n",hdr->reqid));
573                 return;
574         }
575
576         state->call->reply_data.dptr = c->data;
577         state->call->reply_data.dsize = c->datalen;
578         state->call->status = c->status;
579
580         talloc_steal(state, c);
581
582         state->state = CTDB_CALL_DONE;
583         if (state->async.fn) {
584                 state->async.fn(state);
585         }
586 }
587
588
589 /*
590   called when a CTDB_REPLY_DMASTER packet comes in
591
592   This packet comes in from the lmaster response to a CTDB_REQ_CALL
593   request packet. It means that the current dmaster wants to give us
594   the dmaster role
595 */
596 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
597 {
598         struct ctdb_reply_dmaster *c = (struct ctdb_reply_dmaster *)hdr;
599         struct ctdb_db_context *ctdb_db;
600         TDB_DATA key, data;
601         int ret;
602
603         ctdb_db = find_ctdb_db(ctdb, c->db_id);
604         if (ctdb_db == NULL) {
605                 DEBUG(DEBUG_ERR,("Unknown db_id 0x%x in ctdb_reply_dmaster\n", c->db_id));
606                 return;
607         }
608         
609         key.dptr = c->data;
610         key.dsize = c->keylen;
611         data.dptr = &c->data[key.dsize];
612         data.dsize = c->datalen;
613
614         ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
615                                      ctdb_call_input_pkt, ctdb, False);
616         if (ret == -2) {
617                 return;
618         }
619         if (ret != 0) {
620                 DEBUG(DEBUG_ERR,(__location__ " Failed to get lock in ctdb_reply_dmaster\n"));
621                 return;
622         }
623
624         ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn);
625 }
626
627
628 /*
629   called when a CTDB_REPLY_ERROR packet comes in
630 */
631 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
632 {
633         struct ctdb_reply_error *c = (struct ctdb_reply_error *)hdr;
634         struct ctdb_call_state *state;
635
636         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
637         if (state == NULL) {
638                 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_error\n",
639                          ctdb->pnn, hdr->reqid));
640                 return;
641         }
642
643         if (hdr->reqid != state->reqid) {
644                 /* we found a record  but it was the wrong one */
645                 DEBUG(DEBUG_ERR, ("Dropped orphaned error reply with reqid:%u\n",hdr->reqid));
646                 return;
647         }
648
649         talloc_steal(state, c);
650
651         state->state  = CTDB_CALL_ERROR;
652         state->errmsg = (char *)c->msg;
653         if (state->async.fn) {
654                 state->async.fn(state);
655         }
656 }
657
658
659 /*
660   destroy a ctdb_call
661 */
662 static int ctdb_call_destructor(struct ctdb_call_state *state)
663 {
664         DLIST_REMOVE(state->ctdb_db->ctdb->pending_calls, state);
665         ctdb_reqid_remove(state->ctdb_db->ctdb, state->reqid);
666         return 0;
667 }
668
669
670 /*
671   called when a ctdb_call needs to be resent after a reconfigure event
672 */
673 static void ctdb_call_resend(struct ctdb_call_state *state)
674 {
675         struct ctdb_context *ctdb = state->ctdb_db->ctdb;
676
677         state->generation = ctdb->vnn_map->generation;
678
679         /* use a new reqid, in case the old reply does eventually come in */
680         ctdb_reqid_remove(ctdb, state->reqid);
681         state->reqid = ctdb_reqid_new(ctdb, state);
682         state->c->hdr.reqid = state->reqid;
683
684         /* update the generation count for this request, so its valid with the new vnn_map */
685         state->c->hdr.generation = state->generation;
686
687         /* send the packet to ourselves, it will be redirected appropriately */
688         state->c->hdr.destnode = ctdb->pnn;
689
690         ctdb_queue_packet(ctdb, &state->c->hdr);
691         DEBUG(DEBUG_NOTICE,("resent ctdb_call\n"));
692 }
693
694 /*
695   resend all pending calls on recovery
696  */
697 void ctdb_call_resend_all(struct ctdb_context *ctdb)
698 {
699         struct ctdb_call_state *state, *next;
700         for (state=ctdb->pending_calls;state;state=next) {
701                 next = state->next;
702                 ctdb_call_resend(state);
703         }
704 }
705
706 /*
707   this allows the caller to setup a async.fn 
708 */
709 static void call_local_trigger(struct event_context *ev, struct timed_event *te, 
710                        struct timeval t, void *private_data)
711 {
712         struct ctdb_call_state *state = talloc_get_type(private_data, struct ctdb_call_state);
713         if (state->async.fn) {
714                 state->async.fn(state);
715         }
716 }       
717
718
719 /*
720   construct an event driven local ctdb_call
721
722   this is used so that locally processed ctdb_call requests are processed
723   in an event driven manner
724 */
725 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
726                                              struct ctdb_call *call,
727                                              struct ctdb_ltdb_header *header,
728                                              TDB_DATA *data)
729 {
730         struct ctdb_call_state *state;
731         struct ctdb_context *ctdb = ctdb_db->ctdb;
732         int ret;
733
734         state = talloc_zero(ctdb_db, struct ctdb_call_state);
735         CTDB_NO_MEMORY_NULL(ctdb, state);
736
737         talloc_steal(state, data->dptr);
738
739         state->state = CTDB_CALL_DONE;
740         state->call  = talloc(state, struct ctdb_call);
741         CTDB_NO_MEMORY_NULL(ctdb, state->call);
742         *(state->call) = *call;
743         state->ctdb_db = ctdb_db;
744
745         ret = ctdb_call_local(ctdb_db, state->call, header, state, data, ctdb->pnn);
746
747         event_add_timed(ctdb->ev, state, timeval_zero(), call_local_trigger, state);
748
749         return state;
750 }
751
752
753 /*
754   make a remote ctdb call - async send. Called in daemon context.
755
756   This constructs a ctdb_call request and queues it for processing. 
757   This call never blocks.
758 */
759 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
760                                                      struct ctdb_call *call, 
761                                                      struct ctdb_ltdb_header *header)
762 {
763         uint32_t len;
764         struct ctdb_call_state *state;
765         struct ctdb_context *ctdb = ctdb_db->ctdb;
766
767         if (ctdb->methods == NULL) {
768                 DEBUG(DEBUG_ERR,(__location__ " Failed send packet. Transport is down\n"));
769                 return NULL;
770         }
771
772         state = talloc_zero(ctdb_db, struct ctdb_call_state);
773         CTDB_NO_MEMORY_NULL(ctdb, state);
774         state->call = talloc(state, struct ctdb_call);
775         CTDB_NO_MEMORY_NULL(ctdb, state->call);
776
777         state->reqid = ctdb_reqid_new(ctdb, state);
778         state->ctdb_db = ctdb_db;
779         talloc_set_destructor(state, ctdb_call_destructor);
780
781         len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
782         state->c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CALL, len, 
783                                            struct ctdb_req_call);
784         CTDB_NO_MEMORY_NULL(ctdb, state->c);
785         state->c->hdr.destnode  = header->dmaster;
786
787         /* this limits us to 16k outstanding messages - not unreasonable */
788         state->c->hdr.reqid     = state->reqid;
789         state->c->flags         = call->flags;
790         state->c->db_id         = ctdb_db->db_id;
791         state->c->callid        = call->call_id;
792         state->c->hopcount      = 0;
793         state->c->keylen        = call->key.dsize;
794         state->c->calldatalen   = call->call_data.dsize;
795         memcpy(&state->c->data[0], call->key.dptr, call->key.dsize);
796         memcpy(&state->c->data[call->key.dsize], 
797                call->call_data.dptr, call->call_data.dsize);
798         *(state->call)              = *call;
799         state->call->call_data.dptr = &state->c->data[call->key.dsize];
800         state->call->key.dptr       = &state->c->data[0];
801
802         state->state  = CTDB_CALL_WAIT;
803         state->generation = ctdb->vnn_map->generation;
804
805         DLIST_ADD(ctdb->pending_calls, state);
806
807         ctdb_queue_packet(ctdb, &state->c->hdr);
808
809         return state;
810 }
811
812 /*
813   make a remote ctdb call - async recv - called in daemon context
814
815   This is called when the program wants to wait for a ctdb_call to complete and get the 
816   results. This call will block unless the call has already completed.
817 */
818 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call)
819 {
820         while (state->state < CTDB_CALL_DONE) {
821                 event_loop_once(state->ctdb_db->ctdb->ev);
822         }
823         if (state->state != CTDB_CALL_DONE) {
824                 ctdb_set_error(state->ctdb_db->ctdb, "%s", state->errmsg);
825                 talloc_free(state);
826                 return -1;
827         }
828
829         if (state->call->reply_data.dsize) {
830                 call->reply_data.dptr = talloc_memdup(call,
831                                                       state->call->reply_data.dptr,
832                                                       state->call->reply_data.dsize);
833                 call->reply_data.dsize = state->call->reply_data.dsize;
834         } else {
835                 call->reply_data.dptr = NULL;
836                 call->reply_data.dsize = 0;
837         }
838         call->status = state->call->status;
839         talloc_free(state);
840         return 0;
841 }
842
843
844 /* 
845    send a keepalive packet to the other node
846 */
847 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode)
848 {
849         struct ctdb_req_keepalive *r;
850         
851         if (ctdb->methods == NULL) {
852                 DEBUG(DEBUG_ERR,(__location__ " Failed to send keepalive. Transport is DOWN\n"));
853                 return;
854         }
855
856         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_KEEPALIVE,
857                                     sizeof(struct ctdb_req_keepalive), 
858                                     struct ctdb_req_keepalive);
859         CTDB_NO_MEMORY_FATAL(ctdb, r);
860         r->hdr.destnode  = destnode;
861         r->hdr.reqid     = 0;
862         
863         ctdb->statistics.keepalive_packets_sent++;
864
865         ctdb_queue_packet(ctdb, &r->hdr);
866
867         talloc_free(r);
868 }
869
870 static void ctdb_holdback_cleanup(struct event_context *ev,
871                                   struct timed_event *te,
872                                   struct timeval t, void *private_data)
873 {
874         struct ctdb_context *ctdb = talloc_get_type(private_data,
875                                                     struct ctdb_context);
876         struct ctdb_db_context *ctdb_db;
877
878         DEBUG(DEBUG_INFO, ("running ctdb_holdback_cleanup\n"));
879
880         if (te != ctdb->holdback_cleanup_te) {
881                 ctdb_fatal(ctdb, "te != ctdb->holdback_cleanup_te");
882         }
883
884         for (ctdb_db=ctdb->db_list; ctdb_db; ctdb_db=ctdb_db->next) {
885                 size_t i, num_heldback;
886
887                 talloc_free(ctdb_db->holdback_keys);
888                 ctdb_db->holdback_keys = NULL;
889
890                 num_heldback = talloc_array_length(ctdb_db->held_back);
891                 for (i=0; i<num_heldback; i++) {
892                         ctdb_queue_packet(ctdb, ctdb_db->held_back[i]);
893                 }
894                 talloc_free(ctdb_db->held_back);
895                 ctdb_db->held_back = NULL;
896         }
897
898         ctdb->holdback_cleanup_te = event_add_timed(
899                 ctdb->ev, ctdb, timeval_current_ofs(
900                         0, ctdb->tunable.holdback_cleanup_interval * 1000),
901                 ctdb_holdback_cleanup, ctdb);
902 }
903
904 void ctdb_start_holdback_cleanup(struct ctdb_context *ctdb)
905 {
906         ctdb->holdback_cleanup_te = event_add_timed(
907                 ctdb->ev, ctdb, timeval_current_ofs(
908                         0, ctdb->tunable.holdback_cleanup_interval * 1000),
909                 ctdb_holdback_cleanup, ctdb);
910
911         CTDB_NO_MEMORY_FATAL(ctdb, ctdb->holdback_cleanup_te);
912
913         DEBUG(DEBUG_NOTICE,("Holdback cleanup has been started\n"));
914 }
915
916 void ctdb_stop_holdback_cleanup(struct ctdb_context *ctdb)
917 {
918         talloc_free(ctdb->holdback_cleanup_te);
919         ctdb->holdback_cleanup_te = NULL;
920 }
921
922 int32_t ctdb_control_gotit(struct ctdb_context *ctdb, TDB_DATA indata)
923 {
924         struct ctdb_control_gotit *c =
925                 (struct ctdb_control_gotit *)indata.dptr;
926         struct ctdb_db_context *db;
927         size_t i, num_keys, num_heldback;
928         TDB_DATA key;
929
930         if (indata.dsize < sizeof(struct ctdb_control_gotit)) {
931                 DEBUG(DEBUG_ERR, (__location__ "Invalid data size %d\n",
932                                   (int)indata.dsize));
933                 return -1;
934         }
935         db = find_ctdb_db(ctdb, c->db_id);
936         if (db == NULL) {
937                 DEBUG(DEBUG_ERR, ("Unknown db_id 0x%x in ctdb_reply_dmaster\n",
938                                   (int)c->db_id));
939                 return -1;
940         }
941
942         key.dptr = c->key;
943         key.dsize = indata.dsize - offsetof(struct ctdb_control_gotit, key);
944
945         num_keys = talloc_array_length(db->holdback_keys);
946         for (i=0; i<num_keys; i++) {
947                 uint8_t *hold_key = db->holdback_keys[i];
948                 size_t keylength = talloc_array_length(hold_key);
949
950                 if ((keylength == key.dsize)
951                     && (memcmp(hold_key, key.dptr, keylength) == 0)) {
952                         break;
953                 }
954         }
955         if (i == num_keys) {
956                 /*
957                  * ctdb_holdback_cleanup has kicked in. This is okay,
958                  * we will just potentially have to retry.
959                  */
960                 return 0;
961         }
962
963         talloc_free(db->holdback_keys[i]);
964         if (i < num_keys-1) {
965                 db->holdback_keys[i] = db->holdback_keys[num_keys-1];
966         }
967         db->holdback_keys = talloc_realloc(db, db->holdback_keys, uint8_t *,
968                                            num_keys-1);
969
970         num_heldback = talloc_array_length(db->held_back);
971         for (i=0; i<num_heldback; i++) {
972                 ctdb_queue_packet(ctdb, db->held_back[i]);
973         }
974         talloc_free(db->held_back);
975         db->held_back = NULL;
976         return 0;
977 }