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