6ef73fe12bd378b521e8f1d035559856e2494a7b
[rusty/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 since 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 /*
235   called when a CTDB_REPLY_DMASTER packet comes in, or when the lmaster
236   gets a CTDB_REQUEST_DMASTER for itself. We become the dmaster.
237
238   must be called with the chainlock held. This function releases the chainlock
239 */
240 static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db, 
241                                 struct ctdb_req_header *hdr,
242                                 TDB_DATA key, TDB_DATA data,
243                                 uint64_t rsn)
244 {
245         struct ctdb_call_state *state;
246         struct ctdb_context *ctdb = ctdb_db->ctdb;
247         struct ctdb_ltdb_header header;
248         int ret;
249
250         DEBUG(DEBUG_DEBUG,("pnn %u dmaster response %08x\n", ctdb->pnn, ctdb_hash(&key)));
251
252         ZERO_STRUCT(header);
253         header.rsn = rsn + 1;
254         header.dmaster = ctdb->pnn;
255
256         if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
257                 ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n");
258
259                 ret = ctdb_ltdb_unlock(ctdb_db, key);
260                 if (ret != 0) {
261                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
262                 }
263                 return;
264         }
265
266         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
267
268         if (state == NULL) {
269                 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_become_dmaster from node %u\n",
270                          ctdb->pnn, hdr->reqid, hdr->srcnode));
271
272                 ret = ctdb_ltdb_unlock(ctdb_db, key);
273                 if (ret != 0) {
274                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
275                 }
276                 return;
277         }
278
279         if (hdr->reqid != state->reqid) {
280                 /* we found a record  but it was the wrong one */
281                 DEBUG(DEBUG_ERR, ("Dropped orphan in ctdb_become_dmaster with reqid:%u\n from node %u", hdr->reqid, hdr->srcnode));
282
283                 ret = ctdb_ltdb_unlock(ctdb_db, key);
284                 if (ret != 0) {
285                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
286                 }
287                 return;
288         }
289
290         ctdb_call_local(ctdb_db, state->call, &header, state, &data, ctdb->pnn);
291
292         ret = ctdb_ltdb_unlock(ctdb_db, key);
293         if (ret != 0) {
294                 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
295         }
296
297         state->state = CTDB_CALL_DONE;
298         if (state->async.fn) {
299                 state->async.fn(state);
300         }
301 }
302
303
304
305 /*
306   called when a CTDB_REQ_DMASTER packet comes in
307
308   this comes into the lmaster for a record when the current dmaster
309   wants to give up the dmaster role and give it to someone else
310 */
311 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
312 {
313         struct ctdb_req_dmaster *c = (struct ctdb_req_dmaster *)hdr;
314         TDB_DATA key, data, data2;
315         struct ctdb_ltdb_header header;
316         struct ctdb_db_context *ctdb_db;
317         int ret;
318
319         key.dptr = c->data;
320         key.dsize = c->keylen;
321         data.dptr = c->data + c->keylen;
322         data.dsize = c->datalen;
323
324         ctdb_db = find_ctdb_db(ctdb, c->db_id);
325         if (!ctdb_db) {
326                 ctdb_send_error(ctdb, hdr, -1,
327                                 "Unknown database in request. db_id==0x%08x",
328                                 c->db_id);
329                 return;
330         }
331         
332         /* fetch the current record */
333         ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, hdr, &data2,
334                                            ctdb_call_input_pkt, ctdb, False);
335         if (ret == -1) {
336                 ctdb_fatal(ctdb, "ctdb_req_dmaster failed to fetch record");
337                 return;
338         }
339         if (ret == -2) {
340                 DEBUG(DEBUG_INFO,(__location__ " deferring ctdb_request_dmaster\n"));
341                 return;
342         }
343
344         if (ctdb_lmaster(ctdb, &key) != ctdb->pnn) {
345                 DEBUG(DEBUG_ALERT,("pnn %u dmaster request to non-lmaster lmaster=%u gen=%u curgen=%u\n",
346                          ctdb->pnn, ctdb_lmaster(ctdb, &key), 
347                          hdr->generation, ctdb->vnn_map->generation));
348                 ctdb_fatal(ctdb, "ctdb_req_dmaster to non-lmaster");
349         }
350
351         DEBUG(DEBUG_DEBUG,("pnn %u dmaster request on %08x for %u from %u\n", 
352                  ctdb->pnn, ctdb_hash(&key), c->dmaster, c->hdr.srcnode));
353
354         /* its a protocol error if the sending node is not the current dmaster */
355         if (header.dmaster != hdr->srcnode) {
356                 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",
357                          ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
358                          ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
359                          (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid,
360                          (key.dsize >= 4)?(*(uint32_t *)key.dptr):0));
361                 if (header.rsn != 0 || header.dmaster != ctdb->pnn) {
362                         ctdb_fatal(ctdb, "ctdb_req_dmaster from non-master");
363                         return;
364                 }
365         }
366
367         if (header.rsn > c->rsn) {
368                 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",
369                          ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
370                          ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
371                          (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid));
372         }
373
374         /* use the rsn from the sending node */
375         header.rsn = c->rsn;
376
377         /* check if the new dmaster is the lmaster, in which case we
378            skip the dmaster reply */
379         if (c->dmaster == ctdb->pnn) {
380                 ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn);
381         } else {
382                 ctdb_send_dmaster_reply(ctdb_db, &header, key, data, c->dmaster, hdr->reqid);
383
384                 ret = ctdb_ltdb_unlock(ctdb_db, key);
385                 if (ret != 0) {
386                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
387                 }
388         }
389 }
390
391
392 /*
393   called when a CTDB_REQ_CALL packet comes in
394 */
395 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
396 {
397         struct ctdb_req_call *c = (struct ctdb_req_call *)hdr;
398         TDB_DATA data;
399         struct ctdb_reply_call *r;
400         int ret, len;
401         struct ctdb_ltdb_header header;
402         struct ctdb_call *call;
403         struct ctdb_db_context *ctdb_db;
404
405         if (ctdb->methods == NULL) {
406                 DEBUG(DEBUG_ERR,(__location__ " Failed ctdb_request_call. Transport is DOWN\n"));
407                 return;
408         }
409
410
411         ctdb_db = find_ctdb_db(ctdb, c->db_id);
412         if (!ctdb_db) {
413                 ctdb_send_error(ctdb, hdr, -1,
414                                 "Unknown database in request. db_id==0x%08x",
415                                 c->db_id);
416                 return;
417         }
418
419         call = talloc(hdr, struct ctdb_call);
420         CTDB_NO_MEMORY_FATAL(ctdb, call);
421
422         call->call_id  = c->callid;
423         call->key.dptr = c->data;
424         call->key.dsize = c->keylen;
425         call->call_data.dptr = c->data + c->keylen;
426         call->call_data.dsize = c->calldatalen;
427
428         /* determine if we are the dmaster for this key. This also
429            fetches the record data (if any), thus avoiding a 2nd fetch of the data 
430            if the call will be answered locally */
431
432         ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, call->key, &header, hdr, &data,
433                                            ctdb_call_input_pkt, ctdb, False);
434         if (ret == -1) {
435                 ctdb_send_error(ctdb, hdr, ret, "ltdb fetch failed in ctdb_request_call");
436                 return;
437         }
438         if (ret == -2) {
439                 DEBUG(DEBUG_INFO,(__location__ " deferred ctdb_request_call\n"));
440                 return;
441         }
442
443         /* if we are not the dmaster, then send a redirect to the
444            requesting node */
445         if (header.dmaster != ctdb->pnn) {
446                 talloc_free(data.dptr);
447                 ctdb_call_send_redirect(ctdb, call->key, c, &header);
448
449                 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
450                 if (ret != 0) {
451                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
452                 }
453                 return;
454         }
455
456         if (c->hopcount > ctdb->statistics.max_hop_count) {
457                 ctdb->statistics.max_hop_count = c->hopcount;
458         }
459
460         /* if this nodes has done enough consecutive calls on the same record
461            then give them the record
462            or if the node requested an immediate migration
463         */
464         if ( c->hdr.srcnode != ctdb->pnn &&
465              ((header.laccessor == c->hdr.srcnode
466                && header.lacount >= ctdb->tunable.max_lacount)
467               || (c->flags & CTDB_IMMEDIATE_MIGRATION)) ) {
468                 if (ctdb_db->transaction_active) {
469                         DEBUG(DEBUG_INFO, (__location__ " refusing migration"
470                               " of key %s while transaction is active\n",
471                               (char *)call->key.dptr));
472                 } else {
473                         DEBUG(DEBUG_DEBUG,("pnn %u starting migration of %08x to %u\n",
474                                  ctdb->pnn, ctdb_hash(&(call->key)), c->hdr.srcnode));
475                         ctdb_call_send_dmaster(ctdb_db, c, &header, &(call->key), &data);
476                         talloc_free(data.dptr);
477
478                         ret = ctdb_ltdb_unlock(ctdb_db, call->key);
479                         if (ret != 0) {
480                                 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
481                         }
482                         return;
483                 }
484         }
485
486         ctdb_call_local(ctdb_db, call, &header, hdr, &data, c->hdr.srcnode);
487
488         ret = ctdb_ltdb_unlock(ctdb_db, call->key);
489         if (ret != 0) {
490                 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
491         }
492
493         len = offsetof(struct ctdb_reply_call, data) + call->reply_data.dsize;
494         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len, 
495                                     struct ctdb_reply_call);
496         CTDB_NO_MEMORY_FATAL(ctdb, r);
497         r->hdr.destnode  = hdr->srcnode;
498         r->hdr.reqid     = hdr->reqid;
499         r->status        = call->status;
500         r->datalen       = call->reply_data.dsize;
501         if (call->reply_data.dsize) {
502                 memcpy(&r->data[0], call->reply_data.dptr, call->reply_data.dsize);
503         }
504
505         ctdb_queue_packet(ctdb, &r->hdr);
506
507         talloc_free(r);
508 }
509
510 /*
511   called when a CTDB_REPLY_CALL packet comes in
512
513   This packet comes in response to a CTDB_REQ_CALL request packet. It
514   contains any reply data from the call
515 */
516 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
517 {
518         struct ctdb_reply_call *c = (struct ctdb_reply_call *)hdr;
519         struct ctdb_call_state *state;
520
521         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
522         if (state == NULL) {
523                 DEBUG(DEBUG_ERR, (__location__ " reqid %u not found\n", hdr->reqid));
524                 return;
525         }
526
527         if (hdr->reqid != state->reqid) {
528                 /* we found a record  but it was the wrong one */
529                 DEBUG(DEBUG_ERR, ("Dropped orphaned call reply with reqid:%u\n",hdr->reqid));
530                 return;
531         }
532
533         state->call->reply_data.dptr = c->data;
534         state->call->reply_data.dsize = c->datalen;
535         state->call->status = c->status;
536
537         talloc_steal(state, c);
538
539         state->state = CTDB_CALL_DONE;
540         if (state->async.fn) {
541                 state->async.fn(state);
542         }
543 }
544
545
546 /*
547   called when a CTDB_REPLY_DMASTER packet comes in
548
549   This packet comes in from the lmaster response to a CTDB_REQ_CALL
550   request packet. It means that the current dmaster wants to give us
551   the dmaster role
552 */
553 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
554 {
555         struct ctdb_reply_dmaster *c = (struct ctdb_reply_dmaster *)hdr;
556         struct ctdb_db_context *ctdb_db;
557         TDB_DATA key, data;
558         int ret;
559
560         ctdb_db = find_ctdb_db(ctdb, c->db_id);
561         if (ctdb_db == NULL) {
562                 DEBUG(DEBUG_ERR,("Unknown db_id 0x%x in ctdb_reply_dmaster\n", c->db_id));
563                 return;
564         }
565         
566         key.dptr = c->data;
567         key.dsize = c->keylen;
568         data.dptr = &c->data[key.dsize];
569         data.dsize = c->datalen;
570
571         ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
572                                      ctdb_call_input_pkt, ctdb, False);
573         if (ret == -2) {
574                 return;
575         }
576         if (ret != 0) {
577                 DEBUG(DEBUG_ERR,(__location__ " Failed to get lock in ctdb_reply_dmaster\n"));
578                 return;
579         }
580
581         ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn);
582 }
583
584
585 /*
586   called when a CTDB_REPLY_ERROR packet comes in
587 */
588 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
589 {
590         struct ctdb_reply_error *c = (struct ctdb_reply_error *)hdr;
591         struct ctdb_call_state *state;
592
593         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
594         if (state == NULL) {
595                 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_error\n",
596                          ctdb->pnn, hdr->reqid));
597                 return;
598         }
599
600         if (hdr->reqid != state->reqid) {
601                 /* we found a record  but it was the wrong one */
602                 DEBUG(DEBUG_ERR, ("Dropped orphaned error reply with reqid:%u\n",hdr->reqid));
603                 return;
604         }
605
606         talloc_steal(state, c);
607
608         state->state  = CTDB_CALL_ERROR;
609         state->errmsg = (char *)c->msg;
610         if (state->async.fn) {
611                 state->async.fn(state);
612         }
613 }
614
615
616 /*
617   destroy a ctdb_call
618 */
619 static int ctdb_call_destructor(struct ctdb_call_state *state)
620 {
621         DLIST_REMOVE(state->ctdb_db->ctdb->pending_calls, state);
622         ctdb_reqid_remove(state->ctdb_db->ctdb, state->reqid);
623         return 0;
624 }
625
626
627 /*
628   called when a ctdb_call needs to be resent after a reconfigure event
629 */
630 static void ctdb_call_resend(struct ctdb_call_state *state)
631 {
632         struct ctdb_context *ctdb = state->ctdb_db->ctdb;
633
634         state->generation = ctdb->vnn_map->generation;
635
636         /* use a new reqid, in case the old reply does eventually come in */
637         ctdb_reqid_remove(ctdb, state->reqid);
638         state->reqid = ctdb_reqid_new(ctdb, state);
639         state->c->hdr.reqid = state->reqid;
640
641         /* update the generation count for this request, so its valid with the new vnn_map */
642         state->c->hdr.generation = state->generation;
643
644         /* send the packet to ourselves, it will be redirected appropriately */
645         state->c->hdr.destnode = ctdb->pnn;
646
647         ctdb_queue_packet(ctdb, &state->c->hdr);
648         DEBUG(DEBUG_NOTICE,("resent ctdb_call\n"));
649 }
650
651 /*
652   resend all pending calls on recovery
653  */
654 void ctdb_call_resend_all(struct ctdb_context *ctdb)
655 {
656         struct ctdb_call_state *state, *next;
657         for (state=ctdb->pending_calls;state;state=next) {
658                 next = state->next;
659                 ctdb_call_resend(state);
660         }
661 }
662
663 /*
664   this allows the caller to setup a async.fn 
665 */
666 static void call_local_trigger(struct event_context *ev, struct timed_event *te, 
667                        struct timeval t, void *private_data)
668 {
669         struct ctdb_call_state *state = talloc_get_type(private_data, struct ctdb_call_state);
670         if (state->async.fn) {
671                 state->async.fn(state);
672         }
673 }       
674
675
676 /*
677   construct an event driven local ctdb_call
678
679   this is used so that locally processed ctdb_call requests are processed
680   in an event driven manner
681 */
682 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
683                                              struct ctdb_call *call,
684                                              struct ctdb_ltdb_header *header,
685                                              TDB_DATA *data)
686 {
687         struct ctdb_call_state *state;
688         struct ctdb_context *ctdb = ctdb_db->ctdb;
689         int ret;
690
691         state = talloc_zero(ctdb_db, struct ctdb_call_state);
692         CTDB_NO_MEMORY_NULL(ctdb, state);
693
694         talloc_steal(state, data->dptr);
695
696         state->state = CTDB_CALL_DONE;
697         state->call  = talloc(state, struct ctdb_call);
698         CTDB_NO_MEMORY_NULL(ctdb, state->call);
699         *(state->call) = *call;
700         state->ctdb_db = ctdb_db;
701
702         ret = ctdb_call_local(ctdb_db, state->call, header, state, data, ctdb->pnn);
703
704         event_add_timed(ctdb->ev, state, timeval_zero(), call_local_trigger, state);
705
706         return state;
707 }
708
709
710 /*
711   make a remote ctdb call - async send. Called in daemon context.
712
713   This constructs a ctdb_call request and queues it for processing. 
714   This call never blocks.
715 */
716 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
717                                                      struct ctdb_call *call, 
718                                                      struct ctdb_ltdb_header *header)
719 {
720         uint32_t len;
721         struct ctdb_call_state *state;
722         struct ctdb_context *ctdb = ctdb_db->ctdb;
723
724         if (ctdb->methods == NULL) {
725                 DEBUG(DEBUG_ERR,(__location__ " Failed send packet. Transport is down\n"));
726                 return NULL;
727         }
728
729         state = talloc_zero(ctdb_db, struct ctdb_call_state);
730         CTDB_NO_MEMORY_NULL(ctdb, state);
731         state->call = talloc(state, struct ctdb_call);
732         CTDB_NO_MEMORY_NULL(ctdb, state->call);
733
734         state->reqid = ctdb_reqid_new(ctdb, state);
735         state->ctdb_db = ctdb_db;
736         talloc_set_destructor(state, ctdb_call_destructor);
737
738         len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
739         state->c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CALL, len, 
740                                            struct ctdb_req_call);
741         CTDB_NO_MEMORY_NULL(ctdb, state->c);
742         state->c->hdr.destnode  = header->dmaster;
743
744         /* this limits us to 16k outstanding messages - not unreasonable */
745         state->c->hdr.reqid     = state->reqid;
746         state->c->flags         = call->flags;
747         state->c->db_id         = ctdb_db->db_id;
748         state->c->callid        = call->call_id;
749         state->c->hopcount      = 0;
750         state->c->keylen        = call->key.dsize;
751         state->c->calldatalen   = call->call_data.dsize;
752         memcpy(&state->c->data[0], call->key.dptr, call->key.dsize);
753         memcpy(&state->c->data[call->key.dsize], 
754                call->call_data.dptr, call->call_data.dsize);
755         *(state->call)              = *call;
756         state->call->call_data.dptr = &state->c->data[call->key.dsize];
757         state->call->key.dptr       = &state->c->data[0];
758
759         state->state  = CTDB_CALL_WAIT;
760         state->generation = ctdb->vnn_map->generation;
761
762         DLIST_ADD(ctdb->pending_calls, state);
763
764         ctdb_queue_packet(ctdb, &state->c->hdr);
765
766         return state;
767 }
768
769 /*
770   make a remote ctdb call - async recv - called in daemon context
771
772   This is called when the program wants to wait for a ctdb_call to complete and get the 
773   results. This call will block unless the call has already completed.
774 */
775 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call)
776 {
777         while (state->state < CTDB_CALL_DONE) {
778                 event_loop_once(state->ctdb_db->ctdb->ev);
779         }
780         if (state->state != CTDB_CALL_DONE) {
781                 ctdb_set_error(state->ctdb_db->ctdb, "%s", state->errmsg);
782                 talloc_free(state);
783                 return -1;
784         }
785
786         if (state->call->reply_data.dsize) {
787                 call->reply_data.dptr = talloc_memdup(call,
788                                                       state->call->reply_data.dptr,
789                                                       state->call->reply_data.dsize);
790                 call->reply_data.dsize = state->call->reply_data.dsize;
791         } else {
792                 call->reply_data.dptr = NULL;
793                 call->reply_data.dsize = 0;
794         }
795         call->status = state->call->status;
796         talloc_free(state);
797         return 0;
798 }
799
800
801 /* 
802    send a keepalive packet to the other node
803 */
804 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode)
805 {
806         struct ctdb_req_keepalive *r;
807         
808         if (ctdb->methods == NULL) {
809                 DEBUG(DEBUG_ERR,(__location__ " Failed to send keepalive. Transport is DOWN\n"));
810                 return;
811         }
812
813         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_KEEPALIVE,
814                                     sizeof(struct ctdb_req_keepalive), 
815                                     struct ctdb_req_keepalive);
816         CTDB_NO_MEMORY_FATAL(ctdb, r);
817         r->hdr.destnode  = destnode;
818         r->hdr.reqid     = 0;
819         
820         ctdb->statistics.keepalive_packets_sent++;
821
822         ctdb_queue_packet(ctdb, &r->hdr);
823
824         talloc_free(r);
825 }