Revert "Add a new header flag for "migrated with data" and set this to 1"
[sahlberg/ctdb.git] / server / ctdb_call.c
1 /* 
2    ctdb_call protocol code
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20   see http://wiki.samba.org/index.php/Samba_%26_Clustering for
21   protocol design and packet details
22 */
23 #include "includes.h"
24 #include "lib/tevent/tevent.h"
25 #include "lib/tdb/include/tdb.h"
26 #include "lib/util/dlinklist.h"
27 #include "system/network.h"
28 #include "system/filesys.h"
29 #include "../include/ctdb_private.h"
30
31 /*
32   find the ctdb_db from a db index
33  */
34  struct ctdb_db_context *find_ctdb_db(struct ctdb_context *ctdb, uint32_t id)
35 {
36         struct ctdb_db_context *ctdb_db;
37
38         for (ctdb_db=ctdb->db_list; ctdb_db; ctdb_db=ctdb_db->next) {
39                 if (ctdb_db->db_id == id) {
40                         break;
41                 }
42         }
43         return ctdb_db;
44 }
45
46
47 /*
48   a varient of input packet that can be used in lock requeue
49 */
50 static void ctdb_call_input_pkt(void *p, struct ctdb_req_header *hdr)
51 {
52         struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
53         ctdb_input_pkt(ctdb, hdr);
54 }
55
56
57 /*
58   send an error reply
59 */
60 static void ctdb_send_error(struct ctdb_context *ctdb, 
61                             struct ctdb_req_header *hdr, uint32_t status,
62                             const char *fmt, ...) PRINTF_ATTRIBUTE(4,5);
63 static void ctdb_send_error(struct ctdb_context *ctdb, 
64                             struct ctdb_req_header *hdr, uint32_t status,
65                             const char *fmt, ...)
66 {
67         va_list ap;
68         struct ctdb_reply_error *r;
69         char *msg;
70         int msglen, len;
71
72         if (ctdb->methods == NULL) {
73                 DEBUG(DEBUG_INFO,(__location__ " Failed to send error. Transport is DOWN\n"));
74                 return;
75         }
76
77         va_start(ap, fmt);
78         msg = talloc_vasprintf(ctdb, fmt, ap);
79         if (msg == NULL) {
80                 ctdb_fatal(ctdb, "Unable to allocate error in ctdb_send_error\n");
81         }
82         va_end(ap);
83
84         msglen = strlen(msg)+1;
85         len = offsetof(struct ctdb_reply_error, msg);
86         r = ctdb_transport_allocate(ctdb, msg, CTDB_REPLY_ERROR, len + msglen, 
87                                     struct ctdb_reply_error);
88         CTDB_NO_MEMORY_FATAL(ctdb, r);
89
90         r->hdr.destnode  = hdr->srcnode;
91         r->hdr.reqid     = hdr->reqid;
92         r->status        = status;
93         r->msglen        = msglen;
94         memcpy(&r->msg[0], msg, msglen);
95
96         ctdb_queue_packet(ctdb, &r->hdr);
97
98         talloc_free(msg);
99 }
100
101
102 /*
103   send a redirect reply
104 */
105 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 (data->dsize != 0) {
205                 header->flags &= CTDB_REC_FLAG_MIGRATED_WITH_DATA;
206         }
207
208         if (lmaster == ctdb->pnn) {
209                 ctdb_send_dmaster_reply(ctdb_db, header, *key, *data, 
210                                         c->hdr.srcnode, c->hdr.reqid);
211                 return;
212         }
213         
214         len = offsetof(struct ctdb_req_dmaster, data) + key->dsize + data->dsize;
215         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_DMASTER, len, 
216                                     struct ctdb_req_dmaster);
217         CTDB_NO_MEMORY_FATAL(ctdb, r);
218         r->hdr.destnode  = lmaster;
219         r->hdr.reqid     = c->hdr.reqid;
220         r->db_id         = c->db_id;
221         r->rsn           = header->rsn;
222         r->dmaster       = c->hdr.srcnode;
223         r->keylen        = key->dsize;
224         r->datalen       = data->dsize;
225         memcpy(&r->data[0], key->dptr, key->dsize);
226         memcpy(&r->data[key->dsize], data->dptr, data->dsize);
227
228         header->dmaster = c->hdr.srcnode;
229         if (ctdb_ltdb_store(ctdb_db, *key, header, *data) != 0) {
230                 ctdb_fatal(ctdb, "Failed to store record in ctdb_call_send_dmaster");
231         }
232         
233         ctdb_queue_packet(ctdb, &r->hdr);
234
235         talloc_free(r);
236 }
237
238 /*
239   called when a CTDB_REPLY_DMASTER packet comes in, or when the lmaster
240   gets a CTDB_REQUEST_DMASTER for itself. We become the dmaster.
241
242   must be called with the chainlock held. This function releases the chainlock
243 */
244 static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db, 
245                                 struct ctdb_req_header *hdr,
246                                 TDB_DATA key, TDB_DATA data,
247                                 uint64_t rsn)
248 {
249         struct ctdb_call_state *state;
250         struct ctdb_context *ctdb = ctdb_db->ctdb;
251         struct ctdb_ltdb_header header;
252         int ret;
253
254         DEBUG(DEBUG_DEBUG,("pnn %u dmaster response %08x\n", ctdb->pnn, ctdb_hash(&key)));
255
256         ZERO_STRUCT(header);
257         header.rsn = rsn + 1;
258         header.dmaster = ctdb->pnn;
259
260         if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
261                 ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n");
262
263                 ret = ctdb_ltdb_unlock(ctdb_db, key);
264                 if (ret != 0) {
265                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
266                 }
267                 return;
268         }
269
270         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
271
272         if (state == NULL) {
273                 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_become_dmaster from node %u\n",
274                          ctdb->pnn, hdr->reqid, hdr->srcnode));
275
276                 ret = ctdb_ltdb_unlock(ctdb_db, key);
277                 if (ret != 0) {
278                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
279                 }
280                 return;
281         }
282
283         if (key.dsize != state->call->key.dsize || memcmp(key.dptr, state->call->key.dptr, key.dsize)) {
284                 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));
285
286                 ret = ctdb_ltdb_unlock(ctdb_db, key);
287                 if (ret != 0) {
288                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
289                 }
290                 return;
291         }
292
293         if (hdr->reqid != state->reqid) {
294                 /* we found a record  but it was the wrong one */
295                 DEBUG(DEBUG_ERR, ("Dropped orphan in ctdb_become_dmaster with reqid:%u\n from node %u", hdr->reqid, hdr->srcnode));
296
297                 ret = ctdb_ltdb_unlock(ctdb_db, key);
298                 if (ret != 0) {
299                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
300                 }
301                 return;
302         }
303
304         ctdb_call_local(ctdb_db, state->call, &header, state, &data);
305
306         ret = ctdb_ltdb_unlock(ctdb_db, state->call->key);
307         if (ret != 0) {
308                 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
309         }
310
311         state->state = CTDB_CALL_DONE;
312         if (state->async.fn) {
313                 state->async.fn(state);
314         }
315 }
316
317
318
319 /*
320   called when a CTDB_REQ_DMASTER packet comes in
321
322   this comes into the lmaster for a record when the current dmaster
323   wants to give up the dmaster role and give it to someone else
324 */
325 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
326 {
327         struct ctdb_req_dmaster *c = (struct ctdb_req_dmaster *)hdr;
328         TDB_DATA key, data, data2;
329         struct ctdb_ltdb_header header;
330         struct ctdb_db_context *ctdb_db;
331         int ret;
332
333         key.dptr = c->data;
334         key.dsize = c->keylen;
335         data.dptr = c->data + c->keylen;
336         data.dsize = c->datalen;
337
338         ctdb_db = find_ctdb_db(ctdb, c->db_id);
339         if (!ctdb_db) {
340                 ctdb_send_error(ctdb, hdr, -1,
341                                 "Unknown database in request. db_id==0x%08x",
342                                 c->db_id);
343                 return;
344         }
345         
346         /* fetch the current record */
347         ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, hdr, &data2,
348                                            ctdb_call_input_pkt, ctdb, False);
349         if (ret == -1) {
350                 ctdb_fatal(ctdb, "ctdb_req_dmaster failed to fetch record");
351                 return;
352         }
353         if (ret == -2) {
354                 DEBUG(DEBUG_INFO,(__location__ " deferring ctdb_request_dmaster\n"));
355                 return;
356         }
357
358         if (ctdb_lmaster(ctdb, &key) != ctdb->pnn) {
359                 DEBUG(DEBUG_ALERT,("pnn %u dmaster request to non-lmaster lmaster=%u gen=%u curgen=%u\n",
360                          ctdb->pnn, ctdb_lmaster(ctdb, &key), 
361                          hdr->generation, ctdb->vnn_map->generation));
362                 ctdb_fatal(ctdb, "ctdb_req_dmaster to non-lmaster");
363         }
364
365         DEBUG(DEBUG_DEBUG,("pnn %u dmaster request on %08x for %u from %u\n", 
366                  ctdb->pnn, ctdb_hash(&key), c->dmaster, c->hdr.srcnode));
367
368         /* its a protocol error if the sending node is not the current dmaster */
369         if (header.dmaster != hdr->srcnode) {
370                 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",
371                          ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
372                          ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
373                          (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid,
374                          (key.dsize >= 4)?(*(uint32_t *)key.dptr):0));
375                 if (header.rsn != 0 || header.dmaster != ctdb->pnn) {
376                         ctdb_fatal(ctdb, "ctdb_req_dmaster from non-master");
377                         return;
378                 }
379         }
380
381         if (header.rsn > c->rsn) {
382                 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",
383                          ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
384                          ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
385                          (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid));
386         }
387
388         /* use the rsn from the sending node */
389         header.rsn = c->rsn;
390
391         /* check if the new dmaster is the lmaster, in which case we
392            skip the dmaster reply */
393         if (c->dmaster == ctdb->pnn) {
394                 ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn);
395         } else {
396                 ctdb_send_dmaster_reply(ctdb_db, &header, key, data, c->dmaster, hdr->reqid);
397
398                 ret = ctdb_ltdb_unlock(ctdb_db, key);
399                 if (ret != 0) {
400                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
401                 }
402         }
403 }
404
405
406 /*
407   called when a CTDB_REQ_CALL packet comes in
408 */
409 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
410 {
411         struct ctdb_req_call *c = (struct ctdb_req_call *)hdr;
412         TDB_DATA data;
413         struct ctdb_reply_call *r;
414         int ret, len;
415         struct ctdb_ltdb_header header;
416         struct ctdb_call *call;
417         struct ctdb_db_context *ctdb_db;
418
419         if (ctdb->methods == NULL) {
420                 DEBUG(DEBUG_INFO,(__location__ " Failed ctdb_request_call. Transport is DOWN\n"));
421                 return;
422         }
423
424
425         ctdb_db = find_ctdb_db(ctdb, c->db_id);
426         if (!ctdb_db) {
427                 ctdb_send_error(ctdb, hdr, -1,
428                                 "Unknown database in request. db_id==0x%08x",
429                                 c->db_id);
430                 return;
431         }
432
433         call = talloc(hdr, struct ctdb_call);
434         CTDB_NO_MEMORY_FATAL(ctdb, call);
435
436         call->call_id  = c->callid;
437         call->key.dptr = c->data;
438         call->key.dsize = c->keylen;
439         call->call_data.dptr = c->data + c->keylen;
440         call->call_data.dsize = c->calldatalen;
441
442         /* determine if we are the dmaster for this key. This also
443            fetches the record data (if any), thus avoiding a 2nd fetch of the data 
444            if the call will be answered locally */
445
446         ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, call->key, &header, hdr, &data,
447                                            ctdb_call_input_pkt, ctdb, False);
448         if (ret == -1) {
449                 ctdb_send_error(ctdb, hdr, ret, "ltdb fetch failed in ctdb_request_call");
450                 return;
451         }
452         if (ret == -2) {
453                 DEBUG(DEBUG_INFO,(__location__ " deferred ctdb_request_call\n"));
454                 return;
455         }
456
457         /* if we are not the dmaster, then send a redirect to the
458            requesting node */
459         if (header.dmaster != ctdb->pnn) {
460                 talloc_free(data.dptr);
461                 ctdb_call_send_redirect(ctdb, call->key, c, &header);
462
463                 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
464                 if (ret != 0) {
465                         DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
466                 }
467                 return;
468         }
469
470         CTDB_UPDATE_STAT(ctdb, max_hop_count, c->hopcount);
471
472         /* Try if possible to migrate the record off to the caller node.
473          * From the clients perspective a fetch of the data is just as 
474          * expensive as a migration.
475          */
476         if (c->hdr.srcnode != ctdb->pnn) {
477                 if (ctdb_db->transaction_active) {
478                         DEBUG(DEBUG_INFO, (__location__ " refusing migration"
479                               " of key %s while transaction is active\n",
480                               (char *)call->key.dptr));
481                 } else {
482                         DEBUG(DEBUG_DEBUG,("pnn %u starting migration of %08x to %u\n",
483                                  ctdb->pnn, ctdb_hash(&(call->key)), c->hdr.srcnode));
484                         ctdb_call_send_dmaster(ctdb_db, c, &header, &(call->key), &data);
485                         talloc_free(data.dptr);
486
487                         ret = ctdb_ltdb_unlock(ctdb_db, call->key);
488                         if (ret != 0) {
489                                 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
490                         }
491                         return;
492                 }
493         }
494
495         ctdb_call_local(ctdb_db, call, &header, hdr, &data);
496
497         ret = ctdb_ltdb_unlock(ctdb_db, call->key);
498         if (ret != 0) {
499                 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
500         }
501
502         len = offsetof(struct ctdb_reply_call, data) + call->reply_data.dsize;
503         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len, 
504                                     struct ctdb_reply_call);
505         CTDB_NO_MEMORY_FATAL(ctdb, r);
506         r->hdr.destnode  = hdr->srcnode;
507         r->hdr.reqid     = hdr->reqid;
508         r->status        = call->status;
509         r->datalen       = call->reply_data.dsize;
510         if (call->reply_data.dsize) {
511                 memcpy(&r->data[0], call->reply_data.dptr, call->reply_data.dsize);
512         }
513
514         ctdb_queue_packet(ctdb, &r->hdr);
515
516         talloc_free(r);
517 }
518
519 /*
520   called when a CTDB_REPLY_CALL packet comes in
521
522   This packet comes in response to a CTDB_REQ_CALL request packet. It
523   contains any reply data from the call
524 */
525 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
526 {
527         struct ctdb_reply_call *c = (struct ctdb_reply_call *)hdr;
528         struct ctdb_call_state *state;
529
530         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
531         if (state == NULL) {
532                 DEBUG(DEBUG_ERR, (__location__ " reqid %u not found\n", hdr->reqid));
533                 return;
534         }
535
536         if (hdr->reqid != state->reqid) {
537                 /* we found a record  but it was the wrong one */
538                 DEBUG(DEBUG_ERR, ("Dropped orphaned call reply with reqid:%u\n",hdr->reqid));
539                 return;
540         }
541
542         state->call->reply_data.dptr = c->data;
543         state->call->reply_data.dsize = c->datalen;
544         state->call->status = c->status;
545
546         talloc_steal(state, c);
547
548         state->state = CTDB_CALL_DONE;
549         if (state->async.fn) {
550                 state->async.fn(state);
551         }
552 }
553
554
555 /*
556   called when a CTDB_REPLY_DMASTER packet comes in
557
558   This packet comes in from the lmaster response to a CTDB_REQ_CALL
559   request packet. It means that the current dmaster wants to give us
560   the dmaster role
561 */
562 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
563 {
564         struct ctdb_reply_dmaster *c = (struct ctdb_reply_dmaster *)hdr;
565         struct ctdb_db_context *ctdb_db;
566         TDB_DATA key, data;
567         int ret;
568
569         ctdb_db = find_ctdb_db(ctdb, c->db_id);
570         if (ctdb_db == NULL) {
571                 DEBUG(DEBUG_ERR,("Unknown db_id 0x%x in ctdb_reply_dmaster\n", c->db_id));
572                 return;
573         }
574         
575         key.dptr = c->data;
576         key.dsize = c->keylen;
577         data.dptr = &c->data[key.dsize];
578         data.dsize = c->datalen;
579
580         ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
581                                      ctdb_call_input_pkt, ctdb, False);
582         if (ret == -2) {
583                 return;
584         }
585         if (ret != 0) {
586                 DEBUG(DEBUG_ERR,(__location__ " Failed to get lock in ctdb_reply_dmaster\n"));
587                 return;
588         }
589
590         ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn);
591 }
592
593
594 /*
595   called when a CTDB_REPLY_ERROR packet comes in
596 */
597 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
598 {
599         struct ctdb_reply_error *c = (struct ctdb_reply_error *)hdr;
600         struct ctdb_call_state *state;
601
602         state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
603         if (state == NULL) {
604                 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_error\n",
605                          ctdb->pnn, hdr->reqid));
606                 return;
607         }
608
609         if (hdr->reqid != state->reqid) {
610                 /* we found a record  but it was the wrong one */
611                 DEBUG(DEBUG_ERR, ("Dropped orphaned error reply with reqid:%u\n",hdr->reqid));
612                 return;
613         }
614
615         talloc_steal(state, c);
616
617         state->state  = CTDB_CALL_ERROR;
618         state->errmsg = (char *)c->msg;
619         if (state->async.fn) {
620                 state->async.fn(state);
621         }
622 }
623
624
625 /*
626   destroy a ctdb_call
627 */
628 static int ctdb_call_destructor(struct ctdb_call_state *state)
629 {
630         DLIST_REMOVE(state->ctdb_db->ctdb->pending_calls, state);
631         ctdb_reqid_remove(state->ctdb_db->ctdb, state->reqid);
632         return 0;
633 }
634
635
636 /*
637   called when a ctdb_call needs to be resent after a reconfigure event
638 */
639 static void ctdb_call_resend(struct ctdb_call_state *state)
640 {
641         struct ctdb_context *ctdb = state->ctdb_db->ctdb;
642
643         state->generation = ctdb->vnn_map->generation;
644
645         /* use a new reqid, in case the old reply does eventually come in */
646         ctdb_reqid_remove(ctdb, state->reqid);
647         state->reqid = ctdb_reqid_new(ctdb, state);
648         state->c->hdr.reqid = state->reqid;
649
650         /* update the generation count for this request, so its valid with the new vnn_map */
651         state->c->hdr.generation = state->generation;
652
653         /* send the packet to ourselves, it will be redirected appropriately */
654         state->c->hdr.destnode = ctdb->pnn;
655
656         ctdb_queue_packet(ctdb, &state->c->hdr);
657         DEBUG(DEBUG_NOTICE,("resent ctdb_call\n"));
658 }
659
660 /*
661   resend all pending calls on recovery
662  */
663 void ctdb_call_resend_all(struct ctdb_context *ctdb)
664 {
665         struct ctdb_call_state *state, *next;
666         for (state=ctdb->pending_calls;state;state=next) {
667                 next = state->next;
668                 ctdb_call_resend(state);
669         }
670 }
671
672 /*
673   this allows the caller to setup a async.fn 
674 */
675 static void call_local_trigger(struct event_context *ev, struct timed_event *te, 
676                        struct timeval t, void *private_data)
677 {
678         struct ctdb_call_state *state = talloc_get_type(private_data, struct ctdb_call_state);
679         if (state->async.fn) {
680                 state->async.fn(state);
681         }
682 }       
683
684
685 /*
686   construct an event driven local ctdb_call
687
688   this is used so that locally processed ctdb_call requests are processed
689   in an event driven manner
690 */
691 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db, 
692                                              struct ctdb_call *call,
693                                              struct ctdb_ltdb_header *header,
694                                              TDB_DATA *data)
695 {
696         struct ctdb_call_state *state;
697         struct ctdb_context *ctdb = ctdb_db->ctdb;
698         int ret;
699
700         state = talloc_zero(ctdb_db, struct ctdb_call_state);
701         CTDB_NO_MEMORY_NULL(ctdb, state);
702
703         talloc_steal(state, data->dptr);
704
705         state->state = CTDB_CALL_DONE;
706         state->call  = talloc(state, struct ctdb_call);
707         CTDB_NO_MEMORY_NULL(ctdb, state->call);
708         *(state->call) = *call;
709         state->ctdb_db = ctdb_db;
710
711         ret = ctdb_call_local(ctdb_db, state->call, header, state, data);
712
713         event_add_timed(ctdb->ev, state, timeval_zero(), call_local_trigger, state);
714
715         return state;
716 }
717
718
719 /*
720   make a remote ctdb call - async send. Called in daemon context.
721
722   This constructs a ctdb_call request and queues it for processing. 
723   This call never blocks.
724 */
725 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db, 
726                                                      struct ctdb_call *call, 
727                                                      struct ctdb_ltdb_header *header)
728 {
729         uint32_t len;
730         struct ctdb_call_state *state;
731         struct ctdb_context *ctdb = ctdb_db->ctdb;
732
733         if (ctdb->methods == NULL) {
734                 DEBUG(DEBUG_INFO,(__location__ " Failed send packet. Transport is down\n"));
735                 return NULL;
736         }
737
738         state = talloc_zero(ctdb_db, struct ctdb_call_state);
739         CTDB_NO_MEMORY_NULL(ctdb, state);
740         state->call = talloc(state, struct ctdb_call);
741         CTDB_NO_MEMORY_NULL(ctdb, state->call);
742
743         state->reqid = ctdb_reqid_new(ctdb, state);
744         state->ctdb_db = ctdb_db;
745         talloc_set_destructor(state, ctdb_call_destructor);
746
747         len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
748         state->c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CALL, len, 
749                                            struct ctdb_req_call);
750         CTDB_NO_MEMORY_NULL(ctdb, state->c);
751         state->c->hdr.destnode  = header->dmaster;
752
753         /* this limits us to 16k outstanding messages - not unreasonable */
754         state->c->hdr.reqid     = state->reqid;
755         state->c->flags         = call->flags;
756         state->c->db_id         = ctdb_db->db_id;
757         state->c->callid        = call->call_id;
758         state->c->hopcount      = 0;
759         state->c->keylen        = call->key.dsize;
760         state->c->calldatalen   = call->call_data.dsize;
761         memcpy(&state->c->data[0], call->key.dptr, call->key.dsize);
762         memcpy(&state->c->data[call->key.dsize], 
763                call->call_data.dptr, call->call_data.dsize);
764         *(state->call)              = *call;
765         state->call->call_data.dptr = &state->c->data[call->key.dsize];
766         state->call->key.dptr       = &state->c->data[0];
767
768         state->state  = CTDB_CALL_WAIT;
769         state->generation = ctdb->vnn_map->generation;
770
771         DLIST_ADD(ctdb->pending_calls, state);
772
773         ctdb_queue_packet(ctdb, &state->c->hdr);
774
775         return state;
776 }
777
778 /*
779   make a remote ctdb call - async recv - called in daemon context
780
781   This is called when the program wants to wait for a ctdb_call to complete and get the 
782   results. This call will block unless the call has already completed.
783 */
784 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call)
785 {
786         while (state->state < CTDB_CALL_DONE) {
787                 event_loop_once(state->ctdb_db->ctdb->ev);
788         }
789         if (state->state != CTDB_CALL_DONE) {
790                 ctdb_set_error(state->ctdb_db->ctdb, "%s", state->errmsg);
791                 talloc_free(state);
792                 return -1;
793         }
794
795         if (state->call->reply_data.dsize) {
796                 call->reply_data.dptr = talloc_memdup(call,
797                                                       state->call->reply_data.dptr,
798                                                       state->call->reply_data.dsize);
799                 call->reply_data.dsize = state->call->reply_data.dsize;
800         } else {
801                 call->reply_data.dptr = NULL;
802                 call->reply_data.dsize = 0;
803         }
804         call->status = state->call->status;
805         talloc_free(state);
806         return 0;
807 }
808
809
810 /* 
811    send a keepalive packet to the other node
812 */
813 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode)
814 {
815         struct ctdb_req_keepalive *r;
816         
817         if (ctdb->methods == NULL) {
818                 DEBUG(DEBUG_INFO,(__location__ " Failed to send keepalive. Transport is DOWN\n"));
819                 return;
820         }
821
822         r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_KEEPALIVE,
823                                     sizeof(struct ctdb_req_keepalive), 
824                                     struct ctdb_req_keepalive);
825         CTDB_NO_MEMORY_FATAL(ctdb, r);
826         r->hdr.destnode  = destnode;
827         r->hdr.reqid     = 0;
828         
829         CTDB_INCREMENT_STAT(ctdb, keepalive_packets_sent);
830
831         ctdb_queue_packet(ctdb, &r->hdr);
832
833         talloc_free(r);
834 }