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