2 ctdb_call protocol code
4 Copyright (C) Andrew Tridgell 2006
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.
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.
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/>.
20 see http://wiki.samba.org/index.php/Samba_%26_Clustering for
21 protocol design and packet details
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"
32 find the ctdb_db from a db index
34 struct ctdb_db_context *find_ctdb_db(struct ctdb_context *ctdb, uint32_t id)
36 struct ctdb_db_context *ctdb_db;
38 for (ctdb_db=ctdb->db_list; ctdb_db; ctdb_db=ctdb_db->next) {
39 if (ctdb_db->db_id == id) {
47 a varient of input packet that can be used in lock requeue
49 static void ctdb_call_input_pkt(void *p, struct ctdb_req_header *hdr)
51 struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
52 ctdb_input_pkt(ctdb, hdr);
59 static void ctdb_send_error(struct ctdb_context *ctdb,
60 struct ctdb_req_header *hdr, uint32_t status,
61 const char *fmt, ...) PRINTF_ATTRIBUTE(4,5);
62 static void ctdb_send_error(struct ctdb_context *ctdb,
63 struct ctdb_req_header *hdr, uint32_t status,
67 struct ctdb_reply_error *r;
71 if (ctdb->methods == NULL) {
72 DEBUG(DEBUG_INFO,(__location__ " Failed to send error. Transport is DOWN\n"));
77 msg = talloc_vasprintf(ctdb, fmt, ap);
79 ctdb_fatal(ctdb, "Unable to allocate error in ctdb_send_error\n");
83 msglen = strlen(msg)+1;
84 len = offsetof(struct ctdb_reply_error, msg);
85 r = ctdb_transport_allocate(ctdb, msg, CTDB_REPLY_ERROR, len + msglen,
86 struct ctdb_reply_error);
87 CTDB_NO_MEMORY_FATAL(ctdb, r);
89 r->hdr.destnode = hdr->srcnode;
90 r->hdr.reqid = hdr->reqid;
93 memcpy(&r->msg[0], msg, msglen);
95 ctdb_queue_packet(ctdb, &r->hdr);
102 * send a redirect reply
104 * The logic behind this function is this:
106 * A client wants to grab a record and sends a CTDB_REQ_CALL packet
107 * to its local ctdb (ctdb_request_call). If the node is not itself
108 * the record's DMASTER, it first redirects the packet to the
109 * record's LMASTER. The LMASTER then redirects the call packet to
110 * the current DMASTER. But there is a race: The record may have
111 * been migrated off the DMASTER while the redirected packet is
112 * on the wire (or in the local queue). So in case the record has
113 * migrated off the new destinaton of the call packet, instead of
114 * going back to the LMASTER to get the new DMASTER, we try to
115 * reduce round-trips by first chasing the record a couple of times
116 * before giving up the direct chase and finally going back to the
117 * LMASTER (again). Note that this works because of this: When
118 * a record is migrated off a node, then the new DMASTER is stored
119 * in the record's copy on the former DMASTER.
121 * The maximum number of attempts for direct chase to make before
122 * going back to the LMASTER is configurable by the tunable
123 * "MaxRedirectCount".
125 static void ctdb_call_send_redirect(struct ctdb_context *ctdb,
127 struct ctdb_req_call *c,
128 struct ctdb_ltdb_header *header)
131 uint32_t lmaster = ctdb_lmaster(ctdb, &key);
132 if (ctdb->pnn == lmaster) {
133 c->hdr.destnode = header->dmaster;
134 } else if ((c->hopcount % ctdb->tunable.max_redirect_count) == 0) {
135 c->hdr.destnode = lmaster;
137 c->hdr.destnode = header->dmaster;
140 ctdb_queue_packet(ctdb, &c->hdr);
147 caller must have the chainlock before calling this routine. Caller must be
150 static void ctdb_send_dmaster_reply(struct ctdb_db_context *ctdb_db,
151 struct ctdb_ltdb_header *header,
152 TDB_DATA key, TDB_DATA data,
153 uint32_t new_dmaster,
156 struct ctdb_context *ctdb = ctdb_db->ctdb;
157 struct ctdb_reply_dmaster *r;
161 if (ctdb->pnn != ctdb_lmaster(ctdb, &key)) {
162 DEBUG(DEBUG_ALERT,(__location__ " Caller is not lmaster!\n"));
166 header->dmaster = new_dmaster;
167 ret = ctdb_ltdb_store(ctdb_db, key, header, data);
169 ctdb_fatal(ctdb, "ctdb_send_dmaster_reply unable to update dmaster");
173 if (ctdb->methods == NULL) {
174 ctdb_fatal(ctdb, "ctdb_send_dmaster_reply cant update dmaster since transport is down");
178 /* put the packet on a temporary context, allowing us to safely free
179 it below even if ctdb_reply_dmaster() has freed it already */
180 tmp_ctx = talloc_new(ctdb);
182 /* send the CTDB_REPLY_DMASTER */
183 len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize + sizeof(uint32_t);
184 r = ctdb_transport_allocate(ctdb, tmp_ctx, CTDB_REPLY_DMASTER, len,
185 struct ctdb_reply_dmaster);
186 CTDB_NO_MEMORY_FATAL(ctdb, r);
188 r->hdr.destnode = new_dmaster;
189 r->hdr.reqid = reqid;
190 r->rsn = header->rsn;
191 r->keylen = key.dsize;
192 r->datalen = data.dsize;
193 r->db_id = ctdb_db->db_id;
194 memcpy(&r->data[0], key.dptr, key.dsize);
195 memcpy(&r->data[key.dsize], data.dptr, data.dsize);
196 memcpy(&r->data[key.dsize+data.dsize], &header->flags, sizeof(uint32_t));
198 ctdb_queue_packet(ctdb, &r->hdr);
200 talloc_free(tmp_ctx);
204 send a dmaster request (give another node the dmaster for a record)
206 This is always sent to the lmaster, which ensures that the lmaster
207 always knows who the dmaster is. The lmaster will then send a
208 CTDB_REPLY_DMASTER to the new dmaster
210 static void ctdb_call_send_dmaster(struct ctdb_db_context *ctdb_db,
211 struct ctdb_req_call *c,
212 struct ctdb_ltdb_header *header,
213 TDB_DATA *key, TDB_DATA *data)
215 struct ctdb_req_dmaster *r;
216 struct ctdb_context *ctdb = ctdb_db->ctdb;
218 uint32_t lmaster = ctdb_lmaster(ctdb, key);
220 if (ctdb->methods == NULL) {
221 ctdb_fatal(ctdb, "Failed ctdb_call_send_dmaster since transport is down");
225 if (data->dsize != 0) {
226 header->flags |= CTDB_REC_FLAG_MIGRATED_WITH_DATA;
229 if (lmaster == ctdb->pnn) {
230 ctdb_send_dmaster_reply(ctdb_db, header, *key, *data,
231 c->hdr.srcnode, c->hdr.reqid);
235 len = offsetof(struct ctdb_req_dmaster, data) + key->dsize + data->dsize
237 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_DMASTER, len,
238 struct ctdb_req_dmaster);
239 CTDB_NO_MEMORY_FATAL(ctdb, r);
240 r->hdr.destnode = lmaster;
241 r->hdr.reqid = c->hdr.reqid;
243 r->rsn = header->rsn;
244 r->dmaster = c->hdr.srcnode;
245 r->keylen = key->dsize;
246 r->datalen = data->dsize;
247 memcpy(&r->data[0], key->dptr, key->dsize);
248 memcpy(&r->data[key->dsize], data->dptr, data->dsize);
249 memcpy(&r->data[key->dsize + data->dsize], &header->flags, sizeof(uint32_t));
251 header->dmaster = c->hdr.srcnode;
252 if (ctdb_ltdb_store(ctdb_db, *key, header, *data) != 0) {
253 ctdb_fatal(ctdb, "Failed to store record in ctdb_call_send_dmaster");
256 ctdb_queue_packet(ctdb, &r->hdr);
262 called when a CTDB_REPLY_DMASTER packet comes in, or when the lmaster
263 gets a CTDB_REQUEST_DMASTER for itself. We become the dmaster.
265 must be called with the chainlock held. This function releases the chainlock
267 static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db,
268 struct ctdb_req_header *hdr,
269 TDB_DATA key, TDB_DATA data,
270 uint64_t rsn, uint32_t record_flags)
272 struct ctdb_call_state *state;
273 struct ctdb_context *ctdb = ctdb_db->ctdb;
274 struct ctdb_ltdb_header header;
277 DEBUG(DEBUG_DEBUG,("pnn %u dmaster response %08x\n", ctdb->pnn, ctdb_hash(&key)));
280 header.rsn = rsn + 1;
281 header.dmaster = ctdb->pnn;
282 header.flags = record_flags;
284 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
287 if (state->call->flags & CTDB_CALL_FLAG_VACUUM_MIGRATION) {
289 * We temporarily add the VACUUM_MIGRATED flag to
290 * the record flags, so that ctdb_ltdb_store can
291 * decide whether the record should be stored or
294 header.flags |= CTDB_REC_FLAG_VACUUM_MIGRATED;
298 if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
299 ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n");
301 ret = ctdb_ltdb_unlock(ctdb_db, key);
303 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
310 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_become_dmaster from node %u\n",
311 ctdb->pnn, hdr->reqid, hdr->srcnode));
313 ret = ctdb_ltdb_unlock(ctdb_db, key);
315 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
320 if (key.dsize != state->call->key.dsize || memcmp(key.dptr, state->call->key.dptr, key.dsize)) {
321 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));
323 ret = ctdb_ltdb_unlock(ctdb_db, key);
325 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
330 if (hdr->reqid != state->reqid) {
331 /* we found a record but it was the wrong one */
332 DEBUG(DEBUG_ERR, ("Dropped orphan in ctdb_become_dmaster with reqid:%u\n from node %u", hdr->reqid, hdr->srcnode));
334 ret = ctdb_ltdb_unlock(ctdb_db, key);
336 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
341 ctdb_call_local(ctdb_db, state->call, &header, state, &data, true);
343 ret = ctdb_ltdb_unlock(ctdb_db, state->call->key);
345 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
348 state->state = CTDB_CALL_DONE;
349 if (state->async.fn) {
350 state->async.fn(state);
357 called when a CTDB_REQ_DMASTER packet comes in
359 this comes into the lmaster for a record when the current dmaster
360 wants to give up the dmaster role and give it to someone else
362 void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
364 struct ctdb_req_dmaster *c = (struct ctdb_req_dmaster *)hdr;
365 TDB_DATA key, data, data2;
366 struct ctdb_ltdb_header header;
367 struct ctdb_db_context *ctdb_db;
368 uint32_t record_flags = 0;
373 key.dsize = c->keylen;
374 data.dptr = c->data + c->keylen;
375 data.dsize = c->datalen;
376 len = offsetof(struct ctdb_req_dmaster, data) + key.dsize + data.dsize
378 if (len <= c->hdr.length) {
379 record_flags = *(uint32_t *)&c->data[c->keylen + c->datalen];
382 ctdb_db = find_ctdb_db(ctdb, c->db_id);
384 ctdb_send_error(ctdb, hdr, -1,
385 "Unknown database in request. db_id==0x%08x",
390 /* fetch the current record */
391 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, hdr, &data2,
392 ctdb_call_input_pkt, ctdb, False);
394 ctdb_fatal(ctdb, "ctdb_req_dmaster failed to fetch record");
398 DEBUG(DEBUG_INFO,(__location__ " deferring ctdb_request_dmaster\n"));
402 if (ctdb_lmaster(ctdb, &key) != ctdb->pnn) {
403 DEBUG(DEBUG_ALERT,("pnn %u dmaster request to non-lmaster lmaster=%u gen=%u curgen=%u\n",
404 ctdb->pnn, ctdb_lmaster(ctdb, &key),
405 hdr->generation, ctdb->vnn_map->generation));
406 ctdb_fatal(ctdb, "ctdb_req_dmaster to non-lmaster");
409 DEBUG(DEBUG_DEBUG,("pnn %u dmaster request on %08x for %u from %u\n",
410 ctdb->pnn, ctdb_hash(&key), c->dmaster, c->hdr.srcnode));
412 /* its a protocol error if the sending node is not the current dmaster */
413 if (header.dmaster != hdr->srcnode) {
414 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",
415 ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
416 ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
417 (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid,
418 (key.dsize >= 4)?(*(uint32_t *)key.dptr):0));
419 if (header.rsn != 0 || header.dmaster != ctdb->pnn) {
420 DEBUG(DEBUG_ERR,("ctdb_req_dmaster from non-master. Force a recovery.\n"));
422 ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
423 ctdb_ltdb_unlock(ctdb_db, key);
428 if (header.rsn > c->rsn) {
429 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",
430 ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
431 ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
432 (unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid));
435 /* use the rsn from the sending node */
438 /* store the record flags from the sending node */
439 header.flags = record_flags;
441 /* check if the new dmaster is the lmaster, in which case we
442 skip the dmaster reply */
443 if (c->dmaster == ctdb->pnn) {
444 ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn, record_flags);
446 ctdb_send_dmaster_reply(ctdb_db, &header, key, data, c->dmaster, hdr->reqid);
448 ret = ctdb_ltdb_unlock(ctdb_db, key);
450 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
457 called when a CTDB_REQ_CALL packet comes in
459 void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
461 struct ctdb_req_call *c = (struct ctdb_req_call *)hdr;
463 struct ctdb_reply_call *r;
465 struct ctdb_ltdb_header header;
466 struct ctdb_call *call;
467 struct ctdb_db_context *ctdb_db;
469 if (ctdb->methods == NULL) {
470 DEBUG(DEBUG_INFO,(__location__ " Failed ctdb_request_call. Transport is DOWN\n"));
475 ctdb_db = find_ctdb_db(ctdb, c->db_id);
477 ctdb_send_error(ctdb, hdr, -1,
478 "Unknown database in request. db_id==0x%08x",
483 call = talloc(hdr, struct ctdb_call);
484 CTDB_NO_MEMORY_FATAL(ctdb, call);
486 call->call_id = c->callid;
487 call->key.dptr = c->data;
488 call->key.dsize = c->keylen;
489 call->call_data.dptr = c->data + c->keylen;
490 call->call_data.dsize = c->calldatalen;
491 call->reply_data.dptr = NULL;
492 call->reply_data.dsize = 0;
494 /* determine if we are the dmaster for this key. This also
495 fetches the record data (if any), thus avoiding a 2nd fetch of the data
496 if the call will be answered locally */
498 ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, call->key, &header, hdr, &data,
499 ctdb_call_input_pkt, ctdb, False);
501 ctdb_send_error(ctdb, hdr, ret, "ltdb fetch failed in ctdb_request_call");
505 DEBUG(DEBUG_INFO,(__location__ " deferred ctdb_request_call\n"));
509 /* Dont do READONLY if we dont have a tracking database */
510 if ((c->flags & CTDB_WANT_READONLY) && !ctdb_db->readonly) {
511 c->flags &= ~CTDB_WANT_READONLY;
514 if (header.flags & CTDB_REC_RO_REVOKE_COMPLETE) {
515 header.flags &= ~(CTDB_REC_RO_HAVE_DELEGATIONS|CTDB_REC_RO_HAVE_READONLY|CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_REVOKE_COMPLETE);
516 CTDB_INCREMENT_STAT(ctdb, total_ro_revokes);
517 CTDB_INCREMENT_DB_STAT(ctdb_db, db_ro_revokes);
518 if (ctdb_ltdb_store(ctdb_db, call->key, &header, data) != 0) {
519 ctdb_fatal(ctdb, "Failed to write header with cleared REVOKE flag");
521 /* and clear out the tracking data */
522 if (tdb_delete(ctdb_db->rottdb, call->key) != 0) {
523 DEBUG(DEBUG_ERR,(__location__ " Failed to clear out trackingdb record\n"));
527 /* if we are revoking, we must defer all other calls until the revoke
530 if (header.flags & CTDB_REC_RO_REVOKING_READONLY) {
531 talloc_free(data.dptr);
532 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
534 if (ctdb_add_revoke_deferred_call(ctdb, ctdb_db, call->key, hdr, ctdb_call_input_pkt, ctdb) != 0) {
535 ctdb_fatal(ctdb, "Failed to add deferred call for revoke child");
541 /* if we are not the dmaster and are not hosting any delegations,
542 then send a redirect to the requesting node */
543 if ((header.dmaster != ctdb->pnn)
544 && (!(header.flags & CTDB_REC_RO_HAVE_DELEGATIONS)) ) {
545 talloc_free(data.dptr);
546 ctdb_call_send_redirect(ctdb, call->key, c, &header);
548 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
550 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
555 if ( (!(c->flags & CTDB_WANT_READONLY))
556 && (header.flags & (CTDB_REC_RO_HAVE_DELEGATIONS|CTDB_REC_RO_HAVE_READONLY)) ) {
557 header.flags |= CTDB_REC_RO_REVOKING_READONLY;
558 if (ctdb_ltdb_store(ctdb_db, call->key, &header, data) != 0) {
559 ctdb_fatal(ctdb, "Failed to store record with HAVE_DELEGATIONS set");
561 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
563 if (ctdb_start_revoke_ro_record(ctdb, ctdb_db, call->key, &header, data) != 0) {
564 ctdb_fatal(ctdb, "Failed to start record revoke");
566 talloc_free(data.dptr);
568 if (ctdb_add_revoke_deferred_call(ctdb, ctdb_db, call->key, hdr, ctdb_call_input_pkt, ctdb) != 0) {
569 ctdb_fatal(ctdb, "Failed to add deferred call for revoke child");
576 /* If this is the first request for delegation. bump rsn and set
577 * the delegations flag
579 if ((c->flags & CTDB_WANT_READONLY)
580 && (c->callid == CTDB_FETCH_WITH_HEADER_FUNC)
581 && (!(header.flags & CTDB_REC_RO_HAVE_DELEGATIONS))) {
583 header.flags |= CTDB_REC_RO_HAVE_DELEGATIONS;
584 if (ctdb_ltdb_store(ctdb_db, call->key, &header, data) != 0) {
585 ctdb_fatal(ctdb, "Failed to store record with HAVE_DELEGATIONS set");
588 if ((c->flags & CTDB_WANT_READONLY)
589 && (call->call_id == CTDB_FETCH_WITH_HEADER_FUNC)) {
592 tdata = tdb_fetch(ctdb_db->rottdb, call->key);
593 if (ctdb_trackingdb_add_pnn(ctdb, &tdata, c->hdr.srcnode) != 0) {
594 ctdb_fatal(ctdb, "Failed to add node to trackingdb");
596 if (tdb_store(ctdb_db->rottdb, call->key, tdata, TDB_REPLACE) != 0) {
597 ctdb_fatal(ctdb, "Failed to store trackingdb data");
601 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
603 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
606 len = offsetof(struct ctdb_reply_call, data) + data.dsize + sizeof(struct ctdb_ltdb_header);
607 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len,
608 struct ctdb_reply_call);
609 CTDB_NO_MEMORY_FATAL(ctdb, r);
610 r->hdr.destnode = c->hdr.srcnode;
611 r->hdr.reqid = c->hdr.reqid;
613 r->datalen = data.dsize + sizeof(struct ctdb_ltdb_header);
615 header.flags |= CTDB_REC_RO_HAVE_READONLY;
616 header.flags &= ~CTDB_REC_RO_HAVE_DELEGATIONS;
617 memcpy(&r->data[0], &header, sizeof(struct ctdb_ltdb_header));
620 memcpy(&r->data[sizeof(struct ctdb_ltdb_header)], data.dptr, data.dsize);
623 ctdb_queue_packet(ctdb, &r->hdr);
624 CTDB_INCREMENT_STAT(ctdb, total_ro_delegations);
625 CTDB_INCREMENT_DB_STAT(ctdb_db, db_ro_delegations);
631 CTDB_UPDATE_STAT(ctdb, max_hop_count, c->hopcount);
633 /* Try if possible to migrate the record off to the caller node.
634 * From the clients perspective a fetch of the data is just as
635 * expensive as a migration.
637 if (c->hdr.srcnode != ctdb->pnn) {
638 if (ctdb_db->transaction_active) {
639 DEBUG(DEBUG_INFO, (__location__ " refusing migration"
640 " of key %s while transaction is active\n",
641 (char *)call->key.dptr));
643 DEBUG(DEBUG_DEBUG,("pnn %u starting migration of %08x to %u\n",
644 ctdb->pnn, ctdb_hash(&(call->key)), c->hdr.srcnode));
645 ctdb_call_send_dmaster(ctdb_db, c, &header, &(call->key), &data);
646 talloc_free(data.dptr);
648 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
650 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
656 ret = ctdb_call_local(ctdb_db, call, &header, hdr, &data, true);
658 DEBUG(DEBUG_ERR,(__location__ " ctdb_call_local failed\n"));
662 ret = ctdb_ltdb_unlock(ctdb_db, call->key);
664 DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
667 len = offsetof(struct ctdb_reply_call, data) + call->reply_data.dsize;
668 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len,
669 struct ctdb_reply_call);
670 CTDB_NO_MEMORY_FATAL(ctdb, r);
671 r->hdr.destnode = hdr->srcnode;
672 r->hdr.reqid = hdr->reqid;
673 r->status = call->status;
674 r->datalen = call->reply_data.dsize;
675 if (call->reply_data.dsize) {
676 memcpy(&r->data[0], call->reply_data.dptr, call->reply_data.dsize);
679 ctdb_queue_packet(ctdb, &r->hdr);
685 called when a CTDB_REPLY_CALL packet comes in
687 This packet comes in response to a CTDB_REQ_CALL request packet. It
688 contains any reply data from the call
690 void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
692 struct ctdb_reply_call *c = (struct ctdb_reply_call *)hdr;
693 struct ctdb_call_state *state;
695 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
697 DEBUG(DEBUG_ERR, (__location__ " reqid %u not found\n", hdr->reqid));
701 if (hdr->reqid != state->reqid) {
702 /* we found a record but it was the wrong one */
703 DEBUG(DEBUG_ERR, ("Dropped orphaned call reply with reqid:%u\n",hdr->reqid));
708 /* read only delegation processing */
709 /* If we got a FETCH_WITH_HEADER we should check if this is a ro
710 * delegation since we may need to update the record header
712 if (state->c->callid == CTDB_FETCH_WITH_HEADER_FUNC) {
713 struct ctdb_db_context *ctdb_db = state->ctdb_db;
714 struct ctdb_ltdb_header *header = (struct ctdb_ltdb_header *)&c->data[0];
715 struct ctdb_ltdb_header oldheader;
716 TDB_DATA key, data, olddata;
719 if (!(header->flags & CTDB_REC_RO_HAVE_READONLY)) {
724 key.dsize = state->c->keylen;
725 key.dptr = state->c->data;
726 ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
727 ctdb_call_input_pkt, ctdb, False);
732 DEBUG(DEBUG_ERR,(__location__ " Failed to get lock in ctdb_reply_call\n"));
736 ret = ctdb_ltdb_fetch(ctdb_db, key, &oldheader, state, &olddata);
738 DEBUG(DEBUG_ERR, ("Failed to fetch old record in ctdb_reply_call\n"));
739 ctdb_ltdb_unlock(ctdb_db, key);
743 if (header->rsn <= oldheader.rsn) {
744 ctdb_ltdb_unlock(ctdb_db, key);
748 if (c->datalen < sizeof(struct ctdb_ltdb_header)) {
749 DEBUG(DEBUG_ERR,(__location__ " Got FETCH_WITH_HEADER reply with too little data: %d bytes\n", c->datalen));
750 ctdb_ltdb_unlock(ctdb_db, key);
754 data.dsize = c->datalen - sizeof(struct ctdb_ltdb_header);
755 data.dptr = &c->data[sizeof(struct ctdb_ltdb_header)];
756 ret = ctdb_ltdb_store(ctdb_db, key, header, data);
758 DEBUG(DEBUG_ERR, ("Failed to store new record in ctdb_reply_call\n"));
759 ctdb_ltdb_unlock(ctdb_db, key);
763 ctdb_ltdb_unlock(ctdb_db, key);
767 state->call->reply_data.dptr = c->data;
768 state->call->reply_data.dsize = c->datalen;
769 state->call->status = c->status;
771 talloc_steal(state, c);
773 state->state = CTDB_CALL_DONE;
774 if (state->async.fn) {
775 state->async.fn(state);
781 called when a CTDB_REPLY_DMASTER packet comes in
783 This packet comes in from the lmaster response to a CTDB_REQ_CALL
784 request packet. It means that the current dmaster wants to give us
787 void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
789 struct ctdb_reply_dmaster *c = (struct ctdb_reply_dmaster *)hdr;
790 struct ctdb_db_context *ctdb_db;
792 uint32_t record_flags = 0;
796 ctdb_db = find_ctdb_db(ctdb, c->db_id);
797 if (ctdb_db == NULL) {
798 DEBUG(DEBUG_ERR,("Unknown db_id 0x%x in ctdb_reply_dmaster\n", c->db_id));
803 key.dsize = c->keylen;
804 data.dptr = &c->data[key.dsize];
805 data.dsize = c->datalen;
806 len = offsetof(struct ctdb_reply_dmaster, data) + key.dsize + data.dsize
808 if (len <= c->hdr.length) {
809 record_flags = *(uint32_t *)&c->data[c->keylen + c->datalen];
812 ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
813 ctdb_call_input_pkt, ctdb, False);
818 DEBUG(DEBUG_ERR,(__location__ " Failed to get lock in ctdb_reply_dmaster\n"));
822 ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn, record_flags);
827 called when a CTDB_REPLY_ERROR packet comes in
829 void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
831 struct ctdb_reply_error *c = (struct ctdb_reply_error *)hdr;
832 struct ctdb_call_state *state;
834 state = ctdb_reqid_find(ctdb, hdr->reqid, struct ctdb_call_state);
836 DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_error\n",
837 ctdb->pnn, hdr->reqid));
841 if (hdr->reqid != state->reqid) {
842 /* we found a record but it was the wrong one */
843 DEBUG(DEBUG_ERR, ("Dropped orphaned error reply with reqid:%u\n",hdr->reqid));
847 talloc_steal(state, c);
849 state->state = CTDB_CALL_ERROR;
850 state->errmsg = (char *)c->msg;
851 if (state->async.fn) {
852 state->async.fn(state);
860 static int ctdb_call_destructor(struct ctdb_call_state *state)
862 DLIST_REMOVE(state->ctdb_db->ctdb->pending_calls, state);
863 ctdb_reqid_remove(state->ctdb_db->ctdb, state->reqid);
869 called when a ctdb_call needs to be resent after a reconfigure event
871 static void ctdb_call_resend(struct ctdb_call_state *state)
873 struct ctdb_context *ctdb = state->ctdb_db->ctdb;
875 state->generation = ctdb->vnn_map->generation;
877 /* use a new reqid, in case the old reply does eventually come in */
878 ctdb_reqid_remove(ctdb, state->reqid);
879 state->reqid = ctdb_reqid_new(ctdb, state);
880 state->c->hdr.reqid = state->reqid;
882 /* update the generation count for this request, so its valid with the new vnn_map */
883 state->c->hdr.generation = state->generation;
885 /* send the packet to ourselves, it will be redirected appropriately */
886 state->c->hdr.destnode = ctdb->pnn;
888 ctdb_queue_packet(ctdb, &state->c->hdr);
889 DEBUG(DEBUG_NOTICE,("resent ctdb_call\n"));
893 resend all pending calls on recovery
895 void ctdb_call_resend_all(struct ctdb_context *ctdb)
897 struct ctdb_call_state *state, *next;
898 for (state=ctdb->pending_calls;state;state=next) {
900 ctdb_call_resend(state);
905 this allows the caller to setup a async.fn
907 static void call_local_trigger(struct event_context *ev, struct timed_event *te,
908 struct timeval t, void *private_data)
910 struct ctdb_call_state *state = talloc_get_type(private_data, struct ctdb_call_state);
911 if (state->async.fn) {
912 state->async.fn(state);
918 construct an event driven local ctdb_call
920 this is used so that locally processed ctdb_call requests are processed
921 in an event driven manner
923 struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db,
924 struct ctdb_call *call,
925 struct ctdb_ltdb_header *header,
928 struct ctdb_call_state *state;
929 struct ctdb_context *ctdb = ctdb_db->ctdb;
932 state = talloc_zero(ctdb_db, struct ctdb_call_state);
933 CTDB_NO_MEMORY_NULL(ctdb, state);
935 talloc_steal(state, data->dptr);
937 state->state = CTDB_CALL_DONE;
938 state->call = talloc(state, struct ctdb_call);
939 CTDB_NO_MEMORY_NULL(ctdb, state->call);
940 *(state->call) = *call;
941 state->ctdb_db = ctdb_db;
943 ret = ctdb_call_local(ctdb_db, state->call, header, state, data, true);
945 DEBUG(DEBUG_DEBUG,("ctdb_call_local() failed, ignoring return code %d\n", ret));
948 event_add_timed(ctdb->ev, state, timeval_zero(), call_local_trigger, state);
955 make a remote ctdb call - async send. Called in daemon context.
957 This constructs a ctdb_call request and queues it for processing.
958 This call never blocks.
960 struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db,
961 struct ctdb_call *call,
962 struct ctdb_ltdb_header *header)
965 struct ctdb_call_state *state;
966 struct ctdb_context *ctdb = ctdb_db->ctdb;
968 if (ctdb->methods == NULL) {
969 DEBUG(DEBUG_INFO,(__location__ " Failed send packet. Transport is down\n"));
973 state = talloc_zero(ctdb_db, struct ctdb_call_state);
974 CTDB_NO_MEMORY_NULL(ctdb, state);
975 state->call = talloc(state, struct ctdb_call);
976 CTDB_NO_MEMORY_NULL(ctdb, state->call);
978 state->reqid = ctdb_reqid_new(ctdb, state);
979 state->ctdb_db = ctdb_db;
980 talloc_set_destructor(state, ctdb_call_destructor);
982 len = offsetof(struct ctdb_req_call, data) + call->key.dsize + call->call_data.dsize;
983 state->c = ctdb_transport_allocate(ctdb, state, CTDB_REQ_CALL, len,
984 struct ctdb_req_call);
985 CTDB_NO_MEMORY_NULL(ctdb, state->c);
986 state->c->hdr.destnode = header->dmaster;
988 /* this limits us to 16k outstanding messages - not unreasonable */
989 state->c->hdr.reqid = state->reqid;
990 state->c->flags = call->flags;
991 state->c->db_id = ctdb_db->db_id;
992 state->c->callid = call->call_id;
993 state->c->hopcount = 0;
994 state->c->keylen = call->key.dsize;
995 state->c->calldatalen = call->call_data.dsize;
996 memcpy(&state->c->data[0], call->key.dptr, call->key.dsize);
997 memcpy(&state->c->data[call->key.dsize],
998 call->call_data.dptr, call->call_data.dsize);
999 *(state->call) = *call;
1000 state->call->call_data.dptr = &state->c->data[call->key.dsize];
1001 state->call->key.dptr = &state->c->data[0];
1003 state->state = CTDB_CALL_WAIT;
1004 state->generation = ctdb->vnn_map->generation;
1006 DLIST_ADD(ctdb->pending_calls, state);
1008 ctdb_queue_packet(ctdb, &state->c->hdr);
1014 make a remote ctdb call - async recv - called in daemon context
1016 This is called when the program wants to wait for a ctdb_call to complete and get the
1017 results. This call will block unless the call has already completed.
1019 int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call)
1021 while (state->state < CTDB_CALL_DONE) {
1022 event_loop_once(state->ctdb_db->ctdb->ev);
1024 if (state->state != CTDB_CALL_DONE) {
1025 ctdb_set_error(state->ctdb_db->ctdb, "%s", state->errmsg);
1030 if (state->call->reply_data.dsize) {
1031 call->reply_data.dptr = talloc_memdup(call,
1032 state->call->reply_data.dptr,
1033 state->call->reply_data.dsize);
1034 call->reply_data.dsize = state->call->reply_data.dsize;
1036 call->reply_data.dptr = NULL;
1037 call->reply_data.dsize = 0;
1039 call->status = state->call->status;
1046 send a keepalive packet to the other node
1048 void ctdb_send_keepalive(struct ctdb_context *ctdb, uint32_t destnode)
1050 struct ctdb_req_keepalive *r;
1052 if (ctdb->methods == NULL) {
1053 DEBUG(DEBUG_INFO,(__location__ " Failed to send keepalive. Transport is DOWN\n"));
1057 r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_KEEPALIVE,
1058 sizeof(struct ctdb_req_keepalive),
1059 struct ctdb_req_keepalive);
1060 CTDB_NO_MEMORY_FATAL(ctdb, r);
1061 r->hdr.destnode = destnode;
1064 CTDB_INCREMENT_STAT(ctdb, keepalive_packets_sent);
1066 ctdb_queue_packet(ctdb, &r->hdr);
1073 struct revokechild_deferred_call {
1074 struct ctdb_context *ctdb;
1075 struct ctdb_req_header *hdr;
1076 deferred_requeue_fn fn;
1080 struct revokechild_handle {
1081 struct revokechild_handle *next, *prev;
1082 struct ctdb_context *ctdb;
1083 struct ctdb_db_context *ctdb_db;
1084 struct fd_event *fde;
1091 struct revokechild_requeue_handle {
1092 struct ctdb_context *ctdb;
1093 struct ctdb_req_header *hdr;
1094 deferred_requeue_fn fn;
1098 static void deferred_call_requeue(struct event_context *ev, struct timed_event *te,
1099 struct timeval t, void *private_data)
1101 struct revokechild_requeue_handle *requeue_handle = talloc_get_type(private_data, struct revokechild_requeue_handle);
1103 requeue_handle->fn(requeue_handle->ctx, requeue_handle->hdr);
1104 talloc_free(requeue_handle);
1107 static int deferred_call_destructor(struct revokechild_deferred_call *deferred_call)
1109 struct ctdb_context *ctdb = deferred_call->ctdb;
1110 struct revokechild_requeue_handle *requeue_handle = talloc(ctdb, struct revokechild_requeue_handle);
1111 struct ctdb_req_call *c = (struct ctdb_req_call *)deferred_call->hdr;
1113 requeue_handle->ctdb = ctdb;
1114 requeue_handle->hdr = deferred_call->hdr;
1115 requeue_handle->fn = deferred_call->fn;
1116 requeue_handle->ctx = deferred_call->ctx;
1117 talloc_steal(requeue_handle, requeue_handle->hdr);
1119 /* when revoking, any READONLY requests have 1 second grace to let read/write finish first */
1120 event_add_timed(ctdb->ev, requeue_handle, timeval_current_ofs(c->flags & CTDB_WANT_READONLY ? 1 : 0, 0), deferred_call_requeue, requeue_handle);
1126 static int revokechild_destructor(struct revokechild_handle *rc)
1128 if (rc->fde != NULL) {
1129 talloc_free(rc->fde);
1132 if (rc->fd[0] != -1) {
1135 if (rc->fd[1] != -1) {
1138 kill(rc->child, SIGKILL);
1140 DLIST_REMOVE(rc->ctdb_db->revokechild_active, rc);
1144 static void revokechild_handler(struct event_context *ev, struct fd_event *fde,
1145 uint16_t flags, void *private_data)
1147 struct revokechild_handle *rc = talloc_get_type(private_data,
1148 struct revokechild_handle);
1152 ret = read(rc->fd[0], &c, 1);
1154 DEBUG(DEBUG_ERR,("Failed to read status from revokechild. errno:%d\n", errno));
1160 DEBUG(DEBUG_ERR,("revokechild returned failure. status:%d\n", c));
1169 struct ctdb_revoke_state {
1170 struct ctdb_db_context *ctdb_db;
1172 struct ctdb_ltdb_header *header;
1179 static void update_record_cb(struct ctdb_client_control_state *state)
1181 struct ctdb_revoke_state *revoke_state;
1185 if (state == NULL) {
1188 revoke_state = state->async.private_data;
1190 state->async.fn = NULL;
1191 ret = ctdb_control_recv(state->ctdb, state, state, NULL, &res, NULL);
1192 if ((ret != 0) || (res != 0)) {
1193 DEBUG(DEBUG_ERR,("Recv for revoke update record failed ret:%d res:%d\n", ret, res));
1194 revoke_state->status = -1;
1197 revoke_state->count--;
1198 if (revoke_state->count <= 0) {
1199 revoke_state->finished = 1;
1203 static void revoke_send_cb(struct ctdb_context *ctdb, uint32_t pnn, void *private_data)
1205 struct ctdb_revoke_state *revoke_state = private_data;
1206 struct ctdb_client_control_state *state;
1208 state = ctdb_ctrl_updaterecord_send(ctdb, revoke_state, timeval_current_ofs(5,0), pnn, revoke_state->ctdb_db, revoke_state->key, revoke_state->header, revoke_state->data);
1209 if (state == NULL) {
1210 DEBUG(DEBUG_ERR,("Failure to send update record to revoke readonly delegation\n"));
1211 revoke_state->status = -1;
1214 state->async.fn = update_record_cb;
1215 state->async.private_data = revoke_state;
1217 revoke_state->count++;
1221 static void ctdb_revoke_timeout_handler(struct event_context *ev, struct timed_event *te,
1222 struct timeval yt, void *private_data)
1224 struct ctdb_revoke_state *state = private_data;
1226 DEBUG(DEBUG_ERR,("Timed out waiting for revoke to finish\n"));
1227 state->finished = 1;
1231 static int ctdb_revoke_all_delegations(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA tdata, TDB_DATA key, struct ctdb_ltdb_header *header, TDB_DATA data)
1233 struct ctdb_revoke_state *state = talloc_zero(ctdb, struct ctdb_revoke_state);
1236 state->ctdb_db = ctdb_db;
1238 state->header = header;
1241 ctdb_trackingdb_traverse(ctdb, tdata, revoke_send_cb, state);
1243 event_add_timed(ctdb->ev, state, timeval_current_ofs(5, 0), ctdb_revoke_timeout_handler, state);
1245 while (state->finished == 0) {
1246 event_loop_once(ctdb->ev);
1249 status = state->status;
1252 struct ctdb_ltdb_header new_header;
1255 if (ctdb_ltdb_lock(ctdb_db, key) != 0) {
1256 DEBUG(DEBUG_ERR,("Failed to chainlock the database in revokechild\n"));
1260 if (ctdb_ltdb_fetch(ctdb_db, key, &new_header, state, &new_data) != 0) {
1261 ctdb_ltdb_unlock(ctdb_db, key);
1262 DEBUG(DEBUG_ERR,("Failed for fetch tdb record in revokechild\n"));
1267 if (new_header.rsn > header->rsn) {
1268 ctdb_ltdb_unlock(ctdb_db, key);
1269 DEBUG(DEBUG_ERR,("RSN too high in tdb record in revokechild\n"));
1273 if ( (new_header.flags & (CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_HAVE_DELEGATIONS)) != (CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_HAVE_DELEGATIONS) ) {
1274 ctdb_ltdb_unlock(ctdb_db, key);
1275 DEBUG(DEBUG_ERR,("Flags are wrong in tdb record in revokechild\n"));
1280 new_header.flags |= CTDB_REC_RO_REVOKE_COMPLETE;
1281 if (ctdb_ltdb_store(ctdb_db, key, &new_header, new_data) != 0) {
1282 ctdb_ltdb_unlock(ctdb_db, key);
1283 DEBUG(DEBUG_ERR,("Failed to write new record in revokechild\n"));
1287 ctdb_ltdb_unlock(ctdb_db, key);
1295 int ctdb_start_revoke_ro_record(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key, struct ctdb_ltdb_header *header, TDB_DATA data)
1298 struct revokechild_handle *rc;
1299 pid_t parent = getpid();
1302 header->flags &= ~(CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_HAVE_DELEGATIONS|CTDB_REC_RO_HAVE_READONLY);
1303 header->flags |= CTDB_REC_FLAG_MIGRATED_WITH_DATA;
1306 if ((rc = talloc_zero(ctdb_db, struct revokechild_handle)) == NULL) {
1307 DEBUG(DEBUG_ERR,("Failed to allocate revokechild_handle\n"));
1311 tdata = tdb_fetch(ctdb_db->rottdb, key);
1312 if (tdata.dsize > 0) {
1316 tdata.dptr = talloc_memdup(rc, tdata.dptr, tdata.dsize);
1322 rc->ctdb_db = ctdb_db;
1326 talloc_set_destructor(rc, revokechild_destructor);
1328 rc->key.dsize = key.dsize;
1329 rc->key.dptr = talloc_memdup(rc, key.dptr, key.dsize);
1330 if (rc->key.dptr == NULL) {
1331 DEBUG(DEBUG_ERR,("Failed to allocate key for revokechild_handle\n"));
1338 DEBUG(DEBUG_ERR,("Failed to allocate key for revokechild_handle\n"));
1344 rc->child = ctdb_fork(ctdb);
1345 if (rc->child == (pid_t)-1) {
1346 DEBUG(DEBUG_ERR,("Failed to fork child for revokechild\n"));
1351 if (rc->child == 0) {
1354 debug_extra = talloc_asprintf(NULL, "revokechild-%s:", ctdb_db->db_name);
1356 if (switch_from_server_to_client(ctdb, "revokechild-%s", ctdb_db->db_name) != 0) {
1357 DEBUG(DEBUG_ERR,("Failed to switch from server to client for revokechild process\n"));
1359 goto child_finished;
1362 c = ctdb_revoke_all_delegations(ctdb, ctdb_db, tdata, key, header, data);
1365 write(rc->fd[1], &c, 1);
1366 /* make sure we die when our parent dies */
1367 while (kill(parent, 0) == 0 || errno != ESRCH) {
1375 set_close_on_exec(rc->fd[0]);
1377 /* This is an active revokechild child process */
1378 DLIST_ADD_END(ctdb_db->revokechild_active, rc, NULL);
1380 rc->fde = event_add_fd(ctdb->ev, rc, rc->fd[0],
1381 EVENT_FD_READ, revokechild_handler,
1383 if (rc->fde == NULL) {
1384 DEBUG(DEBUG_ERR,("Failed to set up fd event for revokechild process\n"));
1387 tevent_fd_set_auto_close(rc->fde);
1392 int ctdb_add_revoke_deferred_call(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key, struct ctdb_req_header *hdr, deferred_requeue_fn fn, void *call_context)
1394 struct revokechild_handle *rc;
1395 struct revokechild_deferred_call *deferred_call;
1397 for (rc = ctdb_db->revokechild_active; rc; rc = rc->next) {
1398 if (rc->key.dsize == 0) {
1401 if (rc->key.dsize != key.dsize) {
1404 if (!memcmp(rc->key.dptr, key.dptr, key.dsize)) {
1410 DEBUG(DEBUG_ERR,("Failed to add deferred call to revoke list. revoke structure not found\n"));
1414 deferred_call = talloc(rc, struct revokechild_deferred_call);
1415 if (deferred_call == NULL) {
1416 DEBUG(DEBUG_ERR,("Failed to allocate deferred call structure for revoking record\n"));
1420 deferred_call->ctdb = ctdb;
1421 deferred_call->hdr = hdr;
1422 deferred_call->fn = fn;
1423 deferred_call->ctx = call_context;
1425 talloc_set_destructor(deferred_call, deferred_call_destructor);
1426 talloc_steal(deferred_call, hdr);