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